Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cereal for Array, ArrayBase #372

Merged
merged 11 commits into from
Apr 7, 2019
Merged

Cereal for Array, ArrayBase #372

merged 11 commits into from
Apr 7, 2019

Conversation

dkeeney
Copy link

@dkeeney dkeeney commented Apr 5, 2019

This adds Array and ArrayBase to Cereal serialization.

@dkeeney dkeeney changed the title Array, ArrayBase Cereal for Array, ArrayBase Apr 5, 2019
@dkeeney dkeeney self-assigned this Apr 5, 2019
Copy link
Member

@breznak breznak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wow, this was a more complex serialization, hope ArrayBase is the only (bad) exception.
I don't understand ArrayBase that deeply, but:

  • I like the cleanup of veriables & methods
  • if the new cerialization works, we're golden 👌
  • please use c++ casts (static_cast<T>(val)) at the few places, rather than plain C-casts ( (T)val)

src/nupic/engine/Link.cpp Show resolved Hide resolved
src/nupic/ntypes/Array.hpp Show resolved Hide resolved
src/nupic/ntypes/ArrayBase.cpp Show resolved Hide resolved
NTA_CHECK(type_ == NTA_BasicType_SDR) << "Does not contain an SDR object";
if (buffer_ == nullptr) {
std::vector<UInt> zeroDim;
zeroDim.push_back(0u);
allocateBuffer(zeroDim); // Create an empty SDR object.
}
SDR& sdr = *((SDR *)buffer_.get());
sdr::SDR& sdr = *((sdr::SDR *)buffer_.get());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, can you use static_cast ? (or other relevant c++ casting), it does better checks that the plain C-cast

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, can you use static_cast ?

I just ever got into the habit of using static_cast. I know what C casting does. Not so sure about the C++ casting. But I guess I should lean to use the new stuff.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have a look, it's a nice c++ feature: https://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-const-cast-and-reinterpret-cast-be-used

My layman's version:

  • it's possible to grep sourcecode for the cast
  • no performance penalty
  • does some checks
  • 1st goto version: static_cast<T>(val)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

..I'd like to see these implemented for the PR

src/nupic/ntypes/ArrayBase.cpp Outdated Show resolved Hide resolved
a.setCount(maxsize);
}
// TODO: Comment this out until we are sure that it is not needed.
//if (offset == 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what was this for?Why we don't use it but you wanna keep that?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was for handling Fan-in merge when buffers could be variable length (Sparse arrays). I don't think any other code was depending on this logic being here so if nothing breaks I will remove the commented out code.

src/nupic/utils/StringUtils.cpp Show resolved Hide resolved
src/test/unit/ntypes/ArrayTest.cpp Show resolved Hide resolved
@dkeeney
Copy link
Author

dkeeney commented Apr 6, 2019

I will need another review before the master changes again. Thanks.

@breznak
Copy link
Member

breznak commented Apr 6, 2019

I will need another review before the master changes again. Thanks.

I will, but you haven't made changes on the last review (?)

@dkeeney
Copy link
Author

dkeeney commented Apr 6, 2019

I will, but you haven't made changes on the last review (?)

So, you really are going to make me change my coding style.
Your are right you know....but that does not make it any easier.

@dkeeney
Copy link
Author

dkeeney commented Apr 6, 2019

Hmmm,
When I compile now after pulling in the master, I am getting the following:

2>c:\projects\ai\cppb\src\nupic\algorithms\temporalmemory.cpp(141): warning C4267: 'argument': conversion from 'size_t' to 'nupic::algorithms::connections::CellIdx', possible loss of data
2>c:\projects\ai\cppb\src\nupic\algorithms\temporalmemory.cpp(160): warning C4267: 'initializing': conversion from 'size_t' to 'nupic::UInt32', possible loss of data
2>c:\projects\ai\cppb\src\nupic\algorithms\temporalmemory.cpp(160): warning C4267: 'initializing': conversion from 'size_t' to 'const nupic::UInt32', possible loss of data
2>c:\projects\ai\cppb\src\nupic\algorithms\temporalmemory.cpp(286): warning C4267: 'argument': conversion from 'size_t' to 'nupic::Int', possible loss of data
2>c:\projects\ai\cppb\src\nupic\algorithms\temporalmemory.cpp(571): warning C4267: 'argument': conversion from 'size_t' to '_Ty', possible loss of data
2>c:\projects\ai\cppb\src\nupic\algorithms\temporalmemory.cpp(575): warning C4267: 'argument': conversion from 'size_t' to '_Ty', possible loss of data
2>c:\projects\ai\cppb\src\nupic\algorithms\temporalmemory.cpp(585): warning C4267: 'initializing': conversion from 'size_t' to 'nupic::UInt32', possible loss of data
2>c:\projects\ai\cppb\src\nupic\algorithms\temporalmemory.cpp(585): warning C4267: 'initializing': conversion from 'size_t' to 'const nupic::UInt32', possible loss of data

and lots of them

I am going to assume that is not me.

@dkeeney
Copy link
Author

dkeeney commented Apr 6, 2019

I am going to be away from my computer for the rest of the day.
Will have the casts fixed shortly.

Copy link
Member

@breznak breznak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the c++ casting fixes, David, the code looks 💯 now!!

  • I've fixed the un/signed compared in the test;
    • gtest is really bad at hinting where (such small) problem happens, so these are always a PIA

src/test/unit/ntypes/ArrayTest.cpp Outdated Show resolved Hide resolved
@dkeeney
Copy link
Author

dkeeney commented Apr 6, 2019

I have lots more cast fixes coming...so don't push to master yet.

breznak
breznak previously approved these changes Apr 6, 2019
Copy link
Member

@breznak breznak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great having the harderst class Cerealed 👍 👍
Thank you!

@breznak
Copy link
Member

breznak commented Apr 6, 2019

I have lots more cast fixes coming...so don't push to master yet.

oh, you..stop it, you are spoiling me with goodness 😄

This PR has already 👍 from me, so do as you want. You can merge and make the other casts a separate PR.

@dkeeney
Copy link
Author

dkeeney commented Apr 6, 2019

I am still getting errors caused by the type of CellIdx

2>TemporalMemory.cpp
2>c:\projects\ai\cppb\src\nupic\algorithms\temporalmemory.cpp(141): warning C4267: 'argument': conversion from 'size_t' to 'nupic::algorithms::connections::CellIdx', possible loss of data
2>c:\projects\ai\cppb\src\nupic\algorithms\temporalmemory.cpp(160): warning C4267: 'initializing': conversion from 'size_t' to 'nupic::UInt32', possible loss of data
2>c:\projects\ai\cppb\src\nupic\algorithms\temporalmemory.cpp(160): warning C4267: 'initializing': conversion from 'size_t' to 'const nupic::UInt32', possible loss of data
2>c:\projects\ai\cppb\src\nupic\algorithms\temporalmemory.cpp(286): warning C4267: 'argument': conversion from 'size_t' to 'nupic::Int', possible loss of data
2>c:\projects\ai\cppb\src\nupic\algorithms\temporalmemory.cpp(571): warning C4267: 'argument': conversion from 'size_t' to '_Ty', possible loss of data
2>        with
2>        [
2>            _Ty=std::seed_seq::result_type
2>        ]
2>c:\projects\ai\cppb\src\nupic\algorithms\temporalmemory.cpp(575): warning C4267: 'argument': conversion from 'size_t' to '_Ty', possible loss of data
2>        with
2>        [
2>            _Ty=std::seed_seq::result_type
2>        ]
2>c:\projects\ai\cppb\src\nupic\algorithms\temporalmemory.cpp(585): warning C4267: 'initializing': conversion from 'size_t' to 'nupic::UInt32', possible loss of data
2>c:\projects\ai\cppb\src\nupic\algorithms\temporalmemory.cpp(585): warning C4267: 'initializing': conversion from 'size_t' to 'const nupic::UInt32', possible loss of data

@dkeeney
Copy link
Author

dkeeney commented Apr 6, 2019

Does your compiler not show those warnings?

@dkeeney
Copy link
Author

dkeeney commented Apr 6, 2019

And....

7>TemporalMemoryTest.cpp
7>c:\projects\ai\cppb\src\test\unit\algorithms\temporalmemorytest.cpp(1360): warning C4267: 'initializing': conversion from 'size_t' to 'int', possible loss of data
7>c:\projects\ai\cppb\src\test\unit\algorithms\temporalmemorytest.cpp(1368): warning C4267: 'initializing': conversion from 'size_t' to 'nupic::Int', possible loss of data
7>c:\projects\ai\cppb\src\test\unit\algorithms\temporalmemorytest.cpp(1560): warning C4267: '/=': conversion from 'size_t' to '_Ty', possible loss of data
7>        with
7>        [
7>            _Ty=std::seed_seq::result_type
7>        ]

@dkeeney
Copy link
Author

dkeeney commented Apr 6, 2019

To be consistent....we should make ALL size related things size_t. This includes the dimensions.

@dkeeney
Copy link
Author

dkeeney commented Apr 6, 2019

Oh, and what about the sparse array elements... aren't they also size related? Probably should keep those at UInt32.

@breznak
Copy link
Member

breznak commented Apr 6, 2019

I am still getting errors caused by the type of CellIdx

No! (gcc 7.3, linux), I thought those had gone away for you. Try a clean build...

Oh, and what about the sparse array elements... aren't they also size related? Probably should keep those at UInt32.

we can't end up with everything size_t'ed :) SDR_sparse_t elements (are now sdr::ElemSparse type) and are/must be matched with connections::CellIdx (so yes, now UInt32, but can change)

@dkeeney
Copy link
Author

dkeeney commented Apr 7, 2019

I am still getting errors caused by the type of CellIdx

I see them only when I do a clean build.

@breznak
Copy link
Member

breznak commented Apr 7, 2019

I see them [CellIdx typedef related errors] only when I do a clean build.

That's very strange! can you open an issue for that? Event the CI guys here are satisfied 🤔

@dkeeney
Copy link
Author

dkeeney commented Apr 7, 2019

I am running a unit test and if that passes I will push some more changes.

@dkeeney
Copy link
Author

dkeeney commented Apr 7, 2019

crashed.
Guess I have more work to do.

@breznak
Copy link
Member

breznak commented Apr 7, 2019

crashed.
Guess I have more work to do.

let's merge this, and the other can come later..

@dkeeney
Copy link
Author

dkeeney commented Apr 7, 2019

Maybe you can help with this....

  template<class Archive>
  void load_ar(Archive & ar) {
    std::vector<UInt>& v = *(reinterpret_cast<std::vector<UInt>*>(this));
    ar(v);
  }

Trying to serialize the Dimensions class. This is a subclass of std::vector
What I thought I could do was use the this pointer to tell Cereal that this is really a vector.

The above code crashes deep inside the cereal templates (about 5 levels deep).
Access violation reading location 0xFFFFFFFFFFFFFF in other words it was lost.

So obviously it does not like something.

I have a social engagement in 15 min so I will take this up in the morning. Will push what I have so you can review all of casts..... you asked for them.

@breznak
Copy link
Member

breznak commented Apr 7, 2019

can review all of casts..... you asked for them.

I've reverted the last 2 changes in this PR, to merge this as "cereal", and I'll put them in a new one.

std::vector& v = (reinterpret_cast<std::vector>(this));

maybe a static/dynamic cast?

PS: the new extra casts break some tests, we can fix them, but no need to sweat it. I meant please use the casts for new code, but we don't have to convert all the existing codebase atonce now.

Copy link
Member

@breznak breznak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@breznak breznak merged commit 8eacb12 into master Apr 7, 2019
@breznak breznak deleted the cereal-arraybase branch April 7, 2019 01:55
@dkeeney
Copy link
Author

dkeeney commented Apr 7, 2019

I've reverted the last 2 changes in this PR, to merge this as "cereal", and I'll put them in a new one.

Ok, I will continue with the Cereal conversions. I have one more PR for Spec and friends (Collection, Scaler, and Value). That would complete all of the lower level modules in ntypes and types. Then I can start in on algorithms, followed by regions and then NetworkAPI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants