Skip to content

Commit f26d39e

Browse files
author
Divya Kamath
committed
Updated serializer class
1 parent c5e52ee commit f26d39e

File tree

10 files changed

+96
-158
lines changed

10 files changed

+96
-158
lines changed

Simulator/Connections/Connections.cpp

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -103,31 +103,3 @@ void Connections::updateSynapsesWeights()
103103
{
104104
}
105105
#endif // !USE_GPU
106-
107-
/// Creates synapses from synapse weights saved in the serialization file.
108-
void Connections::createSynapsesFromWeights()
109-
{
110-
int numVertices = Simulator::getInstance().getTotalVertices();
111-
Layout &layout = Simulator::getInstance().getModel().getLayout();
112-
AllVertices &vertices = layout.getVertices();
113-
114-
// for each neuron
115-
for (int i = 0; i < numVertices; i++) {
116-
// for each synapse in the vertex
117-
for (BGSIZE synapseIndex = 0; synapseIndex < Simulator::getInstance().getMaxEdgesPerVertex();
118-
synapseIndex++) {
119-
BGSIZE iEdg = Simulator::getInstance().getMaxEdgesPerVertex() * i + synapseIndex;
120-
// if the synapse weight is not zero (which means there is a connection), create the synapse
121-
if (edges_->W_[iEdg] != 0.0) {
122-
BGFLOAT theW = edges_->W_[iEdg];
123-
int srcVertex = edges_->sourceVertexIndex_[iEdg];
124-
int destVertex = edges_->destVertexIndex_[iEdg];
125-
edgeType type = layout.edgType(srcVertex, destVertex);
126-
edges_->edgeCounts_[i]++;
127-
edges_->createEdge(iEdg, srcVertex, destVertex, Simulator::getInstance().getDeltaT(),
128-
type);
129-
edges_->W_[iEdg] = theW;
130-
}
131-
}
132-
}
133-
}

Simulator/Connections/Connections.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ class Connections {
7373
/// @return true if successful, false otherwise.
7474
virtual bool updateConnections(AllVertices &vertices);
7575

76-
/// Creates synapses from synapse weights saved in the serialization file.
77-
void createSynapsesFromWeights();
78-
7976
/// Cereal serialization method
8077
template <class Archive> void serialize(Archive &archive, std::uint32_t const version);
8178

Simulator/Core/Core.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ int Core::runSimulation(string executableName, string cmdLineArguments)
184184
LOG4CPLUS_TRACE(consoleLogger, "Deserializing state from file.");
185185

186186
// Deserialization
187-
if (!serializer.deserializeSynapses()) {
187+
if (!serializer.deserialize()) {
188188
LOG4CPLUS_FATAL(consoleLogger, "Failed while deserializing objects");
189189
return -1;
190190
}
@@ -214,7 +214,7 @@ int Core::runSimulation(string executableName, string cmdLineArguments)
214214
// Serializes internal state for the current simulation
215215
if (!simulator.getSerializationFileName().empty()) {
216216
LOG4CPLUS_TRACE(consoleLogger, "Serializing current state");
217-
serializer.serializeSynapses();
217+
serializer.serialize();
218218
}
219219

220220
// Tell simulation to clean-up and run any post-simulation logic.

Simulator/Core/Serializer.cpp

Lines changed: 48 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,43 @@
33
*
44
* @ingroup Simulator/Core
55
*
6-
* @brief Handles implementation details of serialization and deserialization of synapses.
6+
* @brief Provides serialization and deserialization functionality using the Cereal library.
77
*
8-
* Serialize and Deserialize synapse weights, source vertices, destination vertices,
9-
* maxEdgesPerVertex, totalVertices and radii.
8+
* This class handles the serialization and deserialization of all member variables
9+
* in the Connections, Layout, Edges, Vertices, and associated helper classes such as
10+
* EdgeIndexMap, Model, RecordableBase, RecordableVector, Matrix, RNG and EventBuffer.
11+
* Note that Recorder class is not serialized or deserialized.
12+
*
13+
* The serialization and deserialization process typically begins with the Model class,
14+
* which internally calls the serialization of the Connections and Layout classes.
15+
* Connections, in turn, handle the serialization of Edges, while Layout handles
16+
* the serialization of Vertices. This ensures a comprehensive serialization of
17+
* the entire simulation structure.
18+
*
19+
* @note As of September 2024, serialization support is currently available
20+
* for CPU-based Neuron simulations. While GPU-based Neuron serialization is functional,
21+
* the output result files differ, and this is being addressed in [Issue #701].
22+
* Serialization support for NG911 will be extended in the future [Issue #700].
23+
*
1024
*/
1125

1226
#include "Serializer.h"
1327
#include "ConnGrowth.h"
14-
#include "Connections.h"
1528
#include "GPUModel.h"
16-
#include "Simulator.h"
1729
#include <fstream>
1830

19-
// Displays Graphitti as top most element instead of the default Cereal
20-
// CEREAL_XML_STRING_VALUE should be placed before defining cereal archives library
31+
// About CEREAL_XML_STRING_VALUE
32+
// 1. Displays Graphitti as top most element instead of the default Cereal.
33+
// 2. It should be placed before defining cereal archives library
2134
#define CEREAL_XML_STRING_VALUE "Graphitti"
2235
#include <cereal/archives/binary.hpp>
2336
#include <cereal/archives/xml.hpp>
2437

25-
/// Deserializes synapse weights, source vertices, destination vertices,
26-
/// maxEdgesPerVertex, totalVertices.
27-
/// if running a connGrowth model and radii is in serialization file, deserializes radii as well
38+
/// Deserializes all member variables of the
39+
/// Connections, Layout, Edges, Vertices, and associated helper classes.
2840
///
2941
/// @returns true if successful, false otherwise.
30-
bool Serializer::deserializeSynapses()
42+
bool Serializer::deserialize()
3143
{
3244
Simulator &simulator = Simulator::getInstance();
3345

@@ -47,59 +59,24 @@ bool Serializer::deserializeSynapses()
4759
cereal::XMLInputArchive archive(memory_in);
4860
//cereal::BinaryInputArchive archive(memory_in);
4961

50-
Connections &connections = simulator.getModel().getConnections();
51-
//Layout &layout = simulator.getModel().getLayout();
52-
53-
// Deserializes synapse weights along with each synapse's source vertex and destination vertex
54-
// Uses "try catch" to catch any cereal exception
55-
// try {
56-
// archive(dynamic_cast<AllEdges &>(connections.getEdges()));
57-
// } catch (cereal::Exception e) {
58-
// cerr << e.what() << endl
59-
// << "Failed to deserialize synapse weights, source vertices, and/or destination vertices."
60-
// << endl;
61-
// return false;
62-
// }
63-
64-
// Creates synapses from weight
65-
// connections->createSynapsesFromWeights(simulator.getTotalVertices(), layout.get(),
66-
// (layout->getVertices()), (connections->getEdges()));
67-
68-
69-
// Uses "try catch" to catch any cereal exception
70-
try {
71-
archive(connections);
72-
} catch (cereal::Exception e) {
73-
cerr << e.what() << endl << "Failed to deserialize radii." << endl;
62+
if (!processArchive(archive, simulator)) {
63+
cerr << "Failed to deserialize" << endl;
7464
return false;
7565
}
7666

77-
//Creates synapses from weight
78-
// connections.createSynapsesFromWeights();
79-
80-
81-
#if defined(USE_GPU)
82-
// Copies CPU Synapse data to GPU after deserialization, if we're doing
83-
// a GPU-based simulation.
84-
simulator.copyCPUSynapseToGPU();
85-
#endif // USE_GPU
86-
87-
// Creates synapse index map (includes copy CPU index map to GPU)
88-
// connections.createEdgeIndexMap();
8967

9068
#if defined(USE_GPU)
9169
GPUModel &gpuModel = static_cast<GPUModel &>(simulator.getModel());
92-
gpuModel.copySynapseIndexMapHostToDevice(connections.getEdgeIndexMap(),
70+
gpuModel.copySynapseIndexMapHostToDevice(simulator.getModel().getConnections().getEdgeIndexMap(),
9371
simulator.getTotalVertices());
9472
#endif // USE_GPU
9573

9674
return true;
9775
}
9876

99-
/// Serializes synapse weights, source vertices, destination vertices,
100-
/// maxEdgesPerVertex, totalVertices.
101-
/// if running a connGrowth model serializes radii as well.
102-
void Serializer::serializeSynapses()
77+
/// Serializes all member variables of the
78+
/// Connections, Layout, Edges, Vertices, and associated helper classes.
79+
void Serializer::serialize()
10380
{
10481
Simulator &simulator = Simulator::getInstance();
10582

@@ -108,29 +85,27 @@ void Serializer::serializeSynapses()
10885
ofstream memory_out(simulator.getSerializationFileName().c_str());
10986
cout << "Please find the serialized file in " << simulator.getSerializationFileName().c_str();
11087

111-
// Options parameter are optional which sets
112-
// 1. Sets the Preceision of floating point number to 30
113-
// 2. Keeps indedntaion in XML file,
114-
// 3. Displays output type of element values in XML eg. float
115-
// 4. Displays if the size of a node in XML is dynamic or not.
116-
cereal::XMLOutputArchive archive(memory_out, cereal::XMLOutputArchive::Options()
117-
.precision(30)
118-
.indent(true)
119-
.outputType(false)
120-
.sizeAttributes(true));
88+
cereal::XMLOutputArchive archive(memory_out);
12189
//ofstream memory_out (simInfo->memOutputFileName.c_str(), std::ios::binary);
12290
//cereal::BinaryOutputArchive archive(memory_out);
12391

124-
#if defined(USE_GPU)
125-
// Copies GPU Synapse props data to CPU for serialization
126-
simulator.copyGPUSynapseToCPU();
127-
#endif // USE_GPU
128-
129-
Model &model = simulator.getModel();
130-
131-
// Serializes synapse weights along with each synapse's source vertex and destination vertex
132-
// archive(
133-
// cereal::make_nvp("AllEdges", dynamic_cast<AllEdges &>(model.getConnections().getEdges())));
92+
if (!processArchive(archive, simulator)) {
93+
cerr << "Failed to serialize" << endl;
94+
}
95+
}
13496

135-
archive(cereal::make_nvp("Connections", model.getConnections()));
97+
template <typename Archive> bool Serializer::processArchive(Archive &archive, Simulator &simulator)
98+
{
99+
try {
100+
// Starts the serialization/deserialization process from the Model class.
101+
// Note that the Model object gets sliced, and only
102+
// the `serialize` function of the base Model class is called.
103+
archive(simulator.getModel());
104+
// Serialize/Deserialize required global variables
105+
archive(initRNG, noiseRNG, g_simulationStep);
106+
} catch (cereal::Exception e) {
107+
cerr << e.what() << endl;
108+
return false;
109+
}
110+
return true;
136111
}

Simulator/Core/Serializer.h

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,43 @@
33
*
44
* @ingroup Simulator/Core
55
*
6-
* @brief Handles implementation of serialization and deserialization of synapses.
6+
* @brief Provides serialization and deserialization functionality using the Cereal library.
77
*
8-
* Serializes and Deserializes synapse weights, source vertices, destination vertices,
9-
* maxEdgesPerVertex, totalVertices and radii.
8+
* This class handles the serialization and deserialization of all member variables
9+
* in the Connections, Layout, Edges, Vertices, and associated helper classes such as
10+
* EdgeIndexMap, Model, RecordableBase, RecordableVector, Matrix, RNG and EventBuffer.
11+
* Note that Recorder class is not serialized or deserialized.
12+
*
13+
* The serialization and deserialization process typically begins with the Model class,
14+
* which internally calls the serialization of the Connections and Layout classes.
15+
* Connections, in turn, handle the serialization of Edges, while Layout handles
16+
* the serialization of Vertices. This ensures a comprehensive serialization of
17+
* the entire simulation structure.
18+
*
19+
* @note As of September 2024, serialization support is currently available
20+
* for CPU-based Neuron simulations. While GPU-based Neuron serialization is functional,
21+
* the output result files differ, and this is being addressed in [Issue #701].
22+
* Serialization support for NG911 will be extended in the future [Issue #700].
23+
*
1024
*/
25+
1126
#pragma once
27+
#include "Simulator.h"
1228

1329
class Serializer {
1430
public:
1531
Serializer() = default;
1632

17-
/// Serializes synapse weights, source vertices, destination vertices,
18-
/// maxEdgesPerVertex, totalVertices.
19-
/// if running a connGrowth model serializes radii as well
20-
void serializeSynapses();
33+
/// Serializes all member variables of the
34+
/// Connections, Layout, Edges, Vertices, and associated helper classes.
35+
void serialize();
2136

22-
/// Deserializes synapse weights, source vertices, destination vertices,
23-
/// maxEdgesPerVertex, totalVertices.
24-
/// if running a connGrowth model and radii is in serialization file, deserializes radii as well
37+
/// Deserializes all member variables of the
38+
/// Connections, Layout, Edges, Vertices, and associated helper classes.
2539
///
26-
/// @returns true if successful, false otherwise.
27-
bool deserializeSynapses();
40+
/// @returns true if deserialization is successful; false otherwise.
41+
bool deserialize();
42+
43+
private:
44+
template <typename Archive> static bool processArchive(Archive &archive, Simulator &simulator);
2845
};

Simulator/Core/Simulator.cpp

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,6 @@ void Simulator::printParameters() const
120120
<< "\tInitializer RNG Seed: " << initRngSeed_ << endl);
121121
}
122122

123-
// Code from STDPFix branch, doesn't do anything
124-
/// Copy GPU Synapse data to CPU.
125-
void Simulator::copyGPUSynapseToCPU()
126-
{
127-
// ToDo: Delete this method and implement using OperationManager
128-
// model->copyGPUSynapseToCPUModel();
129-
}
130-
131-
/// Copy CPU Synapse data to GPU.
132-
void Simulator::copyCPUSynapseToGPU()
133-
{
134-
// ToDo: Delete this method and implement using OperationManager
135-
// model->copyCPUSynapseToGPUModel();
136-
}
137-
138123
/// Resets all of the maps. Releases and re-allocates memory for each map,
139124
/// clearing them as necessary.
140125
void Simulator::reset()
@@ -153,16 +138,16 @@ void Simulator::reset()
153138
void Simulator::simulate()
154139
{
155140
// Main simulation loop - execute maxGrowthSteps
156-
for (int currentEpoch = 1; currentEpoch <= numEpochs_; currentEpoch++) {
157-
LOG4CPLUS_TRACE(consoleLogger_, "Performing epoch number: " << currentEpoch);
141+
for (currentEpoch_ = 1; currentEpoch_ <= numEpochs_; currentEpoch_++) {
142+
LOG4CPLUS_TRACE(consoleLogger_, "Performing epoch number: " << currentEpoch_);
158143
LOG4CPLUS_TRACE(fileLogger_, "Begin network state:");
159-
currentEpoch_ = currentEpoch;
144+
160145
#ifdef PERFORMANCE_METRICS
161146
// Start timer for advance
162147
short_timer.start();
163148
#endif
164149
// Advance simulation to next growth cycle
165-
advanceEpoch(currentEpoch);
150+
advanceEpoch(currentEpoch_);
166151
#ifdef PERFORMANCE_METRICS
167152
// Time to advance
168153
t_host_advance += short_timer.lap() / 1000000.0;
@@ -208,16 +193,20 @@ void Simulator::advanceEpoch(int currentEpoch) const
208193
while (g_simulationStep < endStep) {
209194
// Output status once every 1% of total simulation time
210195
if (count % onePercent == 0) {
211-
LOG4CPLUS_TRACE(consoleLogger_,
212-
"Epoch: " << currentEpoch << "/" << numEpochs_
213-
<< " simulating time: " << g_simulationStep * deltaT_ << "/"
214-
<< (epochDuration_ * numEpochs_) - 1);
215-
LOG4CPLUS_TRACE(workbenchLogger_,
216-
"Epoch: " << currentEpoch << "/" << numEpochs_
217-
<< " simulating time: " << g_simulationStep * deltaT_ << "/"
218-
<< (epochDuration_ * numEpochs_) - 1);
196+
uint64_t totalDuration = (epochDuration_ * numEpochs_) - 1;
197+
uint64_t simulatedTime = (uint64_t)(g_simulationStep * deltaT_) % totalDuration;
198+
199+
LOG4CPLUS_TRACE(consoleLogger_, "Epoch: " << currentEpoch << "/" << numEpochs_
200+
<< " simulating time: " << simulatedTime << "/"
201+
<< totalDuration);
202+
203+
LOG4CPLUS_TRACE(workbenchLogger_, "Epoch: " << currentEpoch << "/" << numEpochs_
204+
<< " simulating time: " << simulatedTime << "/"
205+
<< totalDuration);
206+
219207
count = 0;
220208
}
209+
221210
count++;
222211
// input stimulus
223212
/***** S_INPUT NOT IN REPO YET *******/

Simulator/Core/Simulator.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@ class Simulator {
3737

3838
void printParameters() const; /// Prints loaded parameters to logging file.
3939

40-
// Copied over from STDPFix
41-
void copyGPUSynapseToCPU(); /// Copy GPU Synapse data to CPU.
42-
43-
// Copied over from STDPFix
44-
void copyCPUSynapseToGPU(); /// Copy CPU Synapse data to GPU.
45-
4640
void reset(); /// Reset simulation objects.
4741

4842
void simulate();
@@ -126,8 +120,6 @@ class Simulator {
126120
Simulator(); /// Constructor is private to keep a singleton instance of this
127121
/// class.
128122

129-
int totalNeurons_; /// Count of neurons in the simulation
130-
131123
int currentEpoch_; /// Current epoch step
132124

133125
int numEpochs_; /// Number of simulator epochs

Simulator/Edges/AllEdges.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <vector>
1616
// cereal
1717
#include "cereal/types/vector.hpp"
18-
#include <cereal/types/polymorphic.hpp>
1918

2019
class AllVertices;
2120

0 commit comments

Comments
 (0)