Skip to content

Commit 3995e40

Browse files
committed
Random: remove serialization via operator<<
replaced by Cereal
1 parent d66c1e9 commit 3995e40

File tree

4 files changed

+15
-148
lines changed

4 files changed

+15
-148
lines changed

bindings/py/cpp_src/bindings/math/py_Random.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,12 @@ namespace htm_ext {
4242
py::class_<Random_t> Random(m, "Random");
4343

4444
Random.def(py::init<htm::UInt64>(), py::arg("seed") = 0)
45-
.def("getUInt32", &Random_t::getUInt32, py::arg("max") = (htm::UInt32)-1l)
46-
.def("getReal64", &Random_t::getReal64)
47-
.def("getSeed", &Random_t::getSeed)
48-
.def("max", &Random_t::max)
49-
.def("min", &Random_t::min)
50-
.def("__eq__", [](Random_t const & self, Random_t const & other) {//wrapping operator==
51-
return self == other;
52-
}, py::is_operator());
45+
.def("getUInt32", &Random_t::getUInt32, py::arg("max") = (htm::UInt32)-1l)
46+
.def("getReal64", &Random_t::getReal64)
47+
.def("getSeed", &Random_t::getSeed)
48+
.def("max", &Random_t::max)
49+
.def("min", &Random_t::min)
50+
.def("__eq__", [](Random_t const & self, Random_t const & other) { return self == other; }, py::is_operator()); //operator==
5351

5452
Random.def_property_readonly_static("MAX32", [](py::object) {
5553
return Random_t::MAX32;
@@ -149,9 +147,11 @@ namespace htm_ext {
149147
[](const Random_t& r)
150148
{
151149
std::stringstream ss;
152-
ss << r;
150+
cereal::JSONOutputArchive ar( ss );
151+
ar(r); //save r's state to archive (stream) with cereal
153152
return ss.str();
154153
},
154+
155155
[](const std::string& str)
156156
{
157157
if (str.empty())
@@ -160,8 +160,9 @@ namespace htm_ext {
160160
}
161161

162162
std::stringstream ss(str);
163+
cereal::JSONInputArchive ar( ss );
163164
Random_t r;
164-
ss >> r;
165+
ar(r); //load from stream to Random 'r'
165166

166167
return r;
167168
}

src/htm/utils/Random.cpp

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
/** @file
1919
Random Number Generator implementation
2020
*/
21-
#include <iostream> // for istream, ostream
22-
#include <chrono> // for random seeds
23-
2421
#include <htm/utils/Log.hpp>
2522
#include <htm/utils/Random.hpp>
2623

@@ -34,7 +31,7 @@ bool Random::operator==(const Random &o) const {
3431

3532
std::random_device rd; //HW RNG, undeterministic, platform dependant. Use only for seeding rng if random seed wanted (seed=0)
3633

37-
Random::Random(UInt64 seed) {
34+
Random::Random(const UInt64 seed) {
3835
if (seed == 0) {
3936
const unsigned int static_seed = rd();
4037
std::mt19937 static_gen(static_seed);
@@ -49,39 +46,9 @@ Random::Random(UInt64 seed) {
4946
steps_ = 0;
5047
}
5148

52-
5349
namespace htm {
54-
std::ostream &operator<<(std::ostream &outStream, const Random &r) {
55-
outStream << "random-v2" << " ";
56-
outStream << r.seed_ << " ";
57-
outStream << r.steps_ << " ";
58-
outStream << "endrandom-v2" << " ";
59-
return outStream;
60-
}
61-
62-
63-
std::istream &operator>>(std::istream &inStream, Random &r) {
64-
std::string version;
65-
66-
inStream >> version;
67-
NTA_CHECK(version == "random-v2") << "Random() deserializer -- found unexpected version string '"
68-
<< version << "'";
69-
inStream >> r.seed_;
70-
r.gen.seed(static_cast<unsigned int>(r.seed_)); //reseed
71-
inStream >> r.steps_;
72-
r.gen.discard(r.steps_); //advance n steps
73-
//FIXME we could de/serialize directly RNG gen, it should be multi-platform according to standard,
74-
//but on OSX CI it wasn't (25/11/2018). So "hacking" the above instead.
75-
std::string endtag;
76-
inStream >> endtag;
77-
NTA_CHECK(endtag == "endrandom-v2") << "Random() deserializer -- found unexpected end tag '" << endtag << "'";
78-
inStream.ignore(1);
79-
80-
return inStream;
81-
}
82-
8350
// helper function for seeding RNGs across the plugin barrier
8451
UInt32 GetRandomSeed() {
85-
return htm::Random().getUInt32();
52+
return htm::Random(0).getUInt32();
8653
}
8754
} // namespace htm

src/htm/utils/Random.hpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ namespace htm {
7171
*/
7272
class Random : public Serializable {
7373
public:
74-
Random(UInt64 seed = 0);
74+
Random(const UInt64 seed = 0);
7575

7676

77+
// Serialization
7778
CerealAdapter;
7879
template<class Archive>
7980
void save_ar(Archive & ar) const {
@@ -160,8 +161,6 @@ class Random : public Serializable {
160161

161162
protected:
162163
friend class RandomTest;
163-
friend std::ostream &operator<<(std::ostream &, const Random &);
164-
friend std::istream &operator>>(std::istream &, Random &);
165164
friend UInt32 GetRandomSeed();
166165
private:
167166
UInt64 seed_;
@@ -186,10 +185,6 @@ class Random : public Serializable {
186185
}
187186
};
188187

189-
// serialization/deserialization
190-
std::ostream &operator<<(std::ostream &, const Random &);
191-
std::istream &operator>>(std::istream &, Random &);
192-
193188
// This function returns seeds from the Random singleton in our
194189
// "universe" (application, plugin, python module). If, when the
195190
// Random constructor is called, seeder_ is NULL, then seeder_ is

src/test/unit/utils/RandomTest.cpp

Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -110,80 +110,6 @@ TEST(RandomTest, OperatorEquals) {
110110
}
111111

112112

113-
TEST(RandomTest, SerializationDeserialization) {
114-
// test serialization/deserialization
115-
Random r1(862973);
116-
for (int i = 0; i < 100; i++)
117-
r1.getUInt32();
118-
119-
EXPECT_EQ(r1.getUInt32(), 2276275187u) << "Before serialization must be same";
120-
// serialize
121-
std::stringstream ostream;
122-
ostream << r1;
123-
124-
// print out serialization for debugging
125-
std::string x(ostream.str());
126-
// NTA_INFO << "random serialize string: '" << x << "'";
127-
// Serialization should be deterministic and platform independent
128-
const std::string expectedString = "random-v2 862973 101 endrandom-v2 ";
129-
EXPECT_EQ(expectedString, x) << "De/serialization";
130-
131-
// deserialize into r2
132-
std::string s(ostream.str());
133-
std::stringstream ss(s);
134-
Random r2;
135-
ss >> r2;
136-
137-
// r1 and r2 should be identical
138-
EXPECT_EQ(r1, r2) << "load from serialization";
139-
EXPECT_EQ(r2.getUInt32(), 3537119063u) << "Deserialized is not deterministic";
140-
r1.getUInt32(); //move the same number of steps
141-
142-
UInt32 v1, v2;
143-
for (int i = 0; i < 100; i++) {
144-
v1 = r1.getUInt32();
145-
v2 = r2.getUInt32();
146-
EXPECT_EQ(v1, v2) << "serialization";
147-
}
148-
}
149-
150-
151-
TEST(RandomTest, testSerialization2) {
152-
const UInt n=1000;
153-
Random r1(7);
154-
Random r2;
155-
156-
htm::Timer testTimer;
157-
testTimer.start();
158-
for (UInt i = 0; i < n; ++i) {
159-
r1.getUInt32();
160-
161-
// Serialize
162-
ofstream os("random3.stream", ofstream::binary);
163-
os << r1;
164-
os.flush();
165-
os.close();
166-
167-
// Deserialize
168-
ifstream is("random3.stream", ifstream::binary);
169-
is >> r2;
170-
is.close();
171-
172-
// Test
173-
ASSERT_EQ(r1.getUInt32(), r2.getUInt32());
174-
ASSERT_EQ(r1.getUInt32(), r2.getUInt32());
175-
ASSERT_EQ(r1.getUInt32(), r2.getUInt32());
176-
ASSERT_EQ(r1.getUInt32(), r2.getUInt32());
177-
ASSERT_EQ(r1.getUInt32(), r2.getUInt32());
178-
}
179-
testTimer.stop();
180-
181-
remove("random3.stream");
182-
183-
cout << "Random serialization: " << testTimer.getElapsed() << endl;
184-
}
185-
186-
187113
TEST(RandomTest, testSerialization_ar) {
188114
// test serialization/deserialization
189115
const UInt SEED = 862973u;
@@ -233,28 +159,6 @@ TEST(RandomTest, ReturnInCorrectRange) {
233159
}
234160
}
235161

236-
/*
237-
TEST(RandomTest, getUInt64) {
238-
// tests for getUInt64
239-
Random r1(1);
240-
ASSERT_EQ(2469588189546311528u, r1.getUInt64())
241-
<< "check getUInt64, seed 1, first call";
242-
ASSERT_EQ(2516265689700432462u, r1.getUInt64())
243-
<< "check getUInt64, seed 1, second call";
244-
245-
Random r2(2);
246-
ASSERT_EQ(16668552215174154828u, r2.getUInt64())
247-
<< "check getUInt64, seed 2, first call";
248-
EXPECT_EQ(15684088468973760345u, r2.getUInt64())
249-
<< "check getUInt64, seed 2, second call";
250-
251-
Random r3(7464235991977222558);
252-
EXPECT_EQ(8035066300482877360u, r3.getUInt64())
253-
<< "check getUInt64, big seed, first call";
254-
EXPECT_EQ(623784303608610892u, r3.getUInt64())
255-
<< "check getUInt64, big seed, second call";
256-
}
257-
*/
258162

259163
TEST(RandomTest, getUInt32) {
260164
// tests for getUInt32

0 commit comments

Comments
 (0)