Skip to content

Commit 151cc8d

Browse files
author
Ben Yang
committed
added changed files
1 parent 5a73bb7 commit 151cc8d

27 files changed

+368
-320
lines changed

Simulator/Core/Core.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ int Core::runSimulation(string executableName, string cmdLineArguments)
206206
}
207207

208208
// Helper function for recorder to register spike history variables for all neurons.
209-
simulator.getModel().getLayout().getVertices().registerHistoryVariables();
209+
OperationManager::getInstance().executeOperation(Operations::registerHistoryVariables);
210210

211211
// Run simulation
212212
LOG4CPLUS_TRACE(consoleLogger, "Starting Simulation");
@@ -247,4 +247,4 @@ int Core::runSimulation(string executableName, string cmdLineArguments)
247247
cout << "time elapsed: " << timeElapsed << endl;
248248
cout << "ssps (simulation seconds / real time seconds): " << ssps << endl;
249249
return 0;
250-
}
250+
}

Simulator/Core/GPUModel.cpp

Lines changed: 136 additions & 102 deletions
Large diffs are not rendered by default.

Simulator/Core/GPUModel.h

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
#pragma once
2323

2424
#include "AllEdges.h"
25+
#include "AllSpikingNeurons.h"
26+
#include "AllSpikingSynapses.h"
2527
#include "AllVertices.h"
28+
#include "OperationManager.h"
2629

2730
#ifdef VALIDATION_MODE
2831
#include <fstream>
@@ -81,25 +84,33 @@ class GPUModel : public Model {
8184
/// over the past epoch. Should be called once every epoch.
8285
virtual void updateConnections() override;
8386

84-
/// Copy GPU edge data to CPU.
85-
virtual void copyGPUtoCPU() override;
86-
87-
/// Copy CPU edge data to GPU.
87+
/// Copies neuron and synapse data from CPU to GPU memory.
88+
/// TODO: Refactor this. Currently, GPUModel handles low-level memory transfer for vertices and edges.
89+
/// Consider moving this responsibility to a more appropriate class, such as a dedicated memory manager
90+
/// or the OperationManager, to better separate concerns and keep the model focused on high-level coordination.
8891
virtual void copyCPUtoGPU() override;
8992

93+
// GPUModel itself does not have anything to be copied back, this function is a
94+
// dummy function just to make GPUModel non virtual
95+
virtual void copyGPUtoCPU() override
96+
{
97+
}
98+
9099
/// Print out EdgeProps on the GPU.
91100
void printGPUEdgesPropsModel() const;
92101

102+
/// Getter for edge (synapse) structures in device memory
103+
AllEdgesDeviceProperties *&getAllEdgesDevice();
104+
105+
/// Getter for vertex (neuron) structures in device memory
106+
AllVerticesDeviceProperties *&getAllVerticesDevice();
107+
93108
protected:
94109
/// Allocates and initializes memories on CUDA device.
95-
/// @param[out] allVerticesDevice Memory location of the pointer to the vertices list on device memory.
96-
/// @param[out] allEdgesDevice Memory location of the pointer to the edges list on device memory.
97-
void allocDeviceStruct(void **allVerticesDevice, void **allEdgesDevice);
110+
void allocDeviceStruct();
98111

99-
/// Copies device memories to host memories and deallocates them.
100-
/// @param[out] allVerticesDevice Memory location of the pointer to the vertices list on device memory.
101-
/// @param[out] allEdgesDevice Memory location of the pointer to the edges list on device memory.
102-
virtual void deleteDeviceStruct(void **allVerticesDevice, void **allEdgesDevice);
112+
/// Deallocates device memories.
113+
virtual void deleteDeviceStruct();
103114

104115
/// Pointer to device random noise array.
105116
float *randNoise_d;
@@ -118,11 +129,6 @@ class GPUModel : public Model {
118129
private:
119130
void allocEdgeIndexMap(int count);
120131

121-
void deleteEdgeIndexMap();
122-
123-
public: //2020/03/14 changed to public for accessing in Core
124-
void copyEdgeIndexMapHostToDevice(EdgeIndexMap &edgeIndexMapHost, int numVertices);
125-
126132
private:
127133
void updateHistory();
128134

@@ -144,4 +150,4 @@ void normalMTGPU(float *randNoise_d);
144150
void initMTGPU(unsigned int seed, unsigned int blocks, unsigned int threads, unsigned int nPerRng,
145151
unsigned int mt_rng_count);
146152
}
147-
#endif
153+
#endif

Simulator/Core/OperationManager.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ string OperationManager::operationToString(const Operations &operation) const
7171
return "copyToGPU";
7272
case Operations::copyFromGPU:
7373
return "copyFromGPU";
74+
case Operations::allocateGPU:
75+
return "allocateGPU";
7476
default:
7577
return "Operation isn't in OperationManager::operationToString()";
7678
}
77-
}
79+
}

Simulator/Core/Operations.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,7 @@ enum class Operations {
2020
deallocateGPUMemory, // Make sure deallocate memory isn't called until all GPU memory is copied back.
2121
restoreToDefault, // Not sure what this refers to.
2222
copyToGPU,
23-
copyFromGPU
23+
copyFromGPU,
24+
allocateGPU,
25+
registerHistoryVariables
2426
};

Simulator/Core/Serializer.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ bool Serializer::deserialize()
6767

6868
#if defined(USE_GPU)
6969
GPUModel &gpuModel = static_cast<GPUModel &>(simulator.getModel());
70-
gpuModel.copyEdgeIndexMapHostToDevice(simulator.getModel().getConnections().getEdgeIndexMap(),
71-
simulator.getTotalVertices());
70+
gpuModel.copyCPUtoGPU();
7271
#endif // USE_GPU
7372

7473
return true;
@@ -108,4 +107,4 @@ template <typename Archive> bool Serializer::processArchive(Archive &archive, Si
108107
return false;
109108
}
110109
return true;
111-
}
110+
}

Simulator/Edges/AllEdges.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,28 @@ AllEdges::AllEdges() : totalEdgeCount_(0), maxEdgesPerVertex_(0), countVertices_
2626
OperationManager::getInstance().registerOperation(Operations::printParameters,
2727
printParametersFunc);
2828

29+
#if defined(USE_GPU)
30+
// Register allocNeuronDeviceStruct function as a allocateGPU operation in the OperationManager
31+
function<void()> allocateGPU
32+
= bind(static_cast<void (AllEdges::*)()>(&AllEdges::allocEdgeDeviceStruct), this);
33+
OperationManager::getInstance().registerOperation(Operations::allocateGPU, allocateGPU);
34+
35+
// Register AllEdges::copyEdgeHostToDevice function as a copyToGPU operation in the OperationManager
36+
function<void()> copyCPUtoGPU
37+
= bind(static_cast<void (AllEdges::*)()>(&AllEdges::copyEdgeHostToDevice), this);
38+
OperationManager::getInstance().registerOperation(Operations::copyToGPU, copyCPUtoGPU);
39+
40+
// Register copyFromGPU operation for transferring edge data from device to host
41+
function<void()> copyFromGPU = bind(&AllEdges::copyEdgeDeviceToHost, this);
42+
OperationManager::getInstance().registerOperation(Operations::copyFromGPU, copyFromGPU);
43+
44+
// Register deleteEdgeDeviceStruct function as a deallocateGPUMemory operation in the OperationManager
45+
function<void()> deallocateGPUMemory = bind(&AllEdges::deleteEdgeDeviceStruct, this);
46+
OperationManager::getInstance().registerOperation(Operations::deallocateGPUMemory,
47+
deallocateGPUMemory);
48+
49+
#endif
50+
2951
fileLogger_ = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("file"));
3052
edgeLogger_ = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("edge"));
3153
}
@@ -277,4 +299,4 @@ BGSIZE AllEdges::addEdge(edgeType type, int srcVertex, int destVertex, BGFLOAT d
277299
// create an edge
278300
createEdge(iEdg, srcVertex, destVertex, deltaT, type);
279301
return iEdg;
280-
}
302+
}

Simulator/Edges/AllEdges.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,7 @@ class AllEdges {
9595
public:
9696
/// Allocate GPU memories to store all edges' states,
9797
/// and copy them from host to GPU memory.
98-
///
99-
/// @param allEdgesDevice GPU address of the allEdges struct on device memory.
100-
virtual void allocEdgeDeviceStruct(void **allEdgesDevice) = 0;
98+
virtual void allocEdgeDeviceStruct() = 0;
10199

102100
/// Allocate GPU memories to store all edges' states,
103101
/// and copy them from host to GPU memory.
@@ -110,13 +108,10 @@ class AllEdges {
110108

111109
/// Delete GPU memories.
112110
///
113-
/// @param allEdgesDevice GPU address of the allEdges struct on device memory.
114-
virtual void deleteEdgeDeviceStruct(void *allEdgesDevice) = 0;
111+
virtual void deleteEdgeDeviceStruct() = 0;
115112

116113
/// Copy all edges' data from host to device.
117-
///
118-
/// @param allEdgesDevice GPU address of the allEdges struct on device memory.
119-
virtual void copyEdgeHostToDevice(void *allEdgesDevice) = 0;
114+
virtual void copyEdgeHostToDevice() = 0;
120115

121116
/// Copy all edges' data from host to device.
122117
///
@@ -128,8 +123,7 @@ class AllEdges {
128123

129124
/// Copy all edges' data from device to host.
130125
///
131-
/// @param allEdgesDevice GPU address of the allEdges struct on device memory.
132-
virtual void copyEdgeDeviceToHost(void *allEdgesDevice) = 0;
126+
virtual void copyEdgeDeviceToHost() = 0;
133127

134128
/// Get edge_counts in AllEdges struct on device memory.
135129
///
@@ -270,4 +264,4 @@ template <class Archive> void AllEdges::serialize(Archive &archive)
270264
cereal::make_nvp("totalEdgeCount", totalEdgeCount_),
271265
cereal::make_nvp("maxEdgesPerVertex", maxEdgesPerVertex_),
272266
cereal::make_nvp("countVertices", countVertices_));
273-
}
267+
}

Simulator/Edges/NG911/All911Edges.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ class All911Edges : public AllEdges {
6565
// GPU functionality for 911 simulation is unimplemented.
6666
// These signatures are required to make the class non-abstract
6767
public:
68-
virtual void allocEdgeDeviceStruct(void **allEdgesDevice) {};
68+
virtual void allocEdgeDeviceStruct() {};
6969
virtual void allocEdgeDeviceStruct(void **allEdgesDevice, int numVertices,
7070
int maxEdgesPerVertex) {};
71-
virtual void deleteEdgeDeviceStruct(void *allEdgesDevice) {};
72-
virtual void copyEdgeHostToDevice(void *allEdgesDevice) {};
71+
virtual void deleteEdgeDeviceStruct() {};
72+
virtual void copyEdgeHostToDevice() {};
7373
virtual void copyEdgeHostToDevice(void *allEdgesDevice, int numVertices, int maxEdgesPerVertex) {
7474
};
75-
virtual void copyEdgeDeviceToHost(void *allEdgesDevice) {};
75+
virtual void copyEdgeDeviceToHost() {};
7676
virtual void copyDeviceEdgeCountsToHost(void *allEdgesDevice) {};
7777
virtual void advanceEdges(void *allEdgesDevice, void *allVerticesDevice,
7878
void *edgeIndexMapDevice) {};
@@ -107,4 +107,4 @@ class All911Edges : public AllEdges {
107107

108108
/// The call information per edge
109109
vector<Call> call_;
110-
};
110+
};

Simulator/Edges/Neuro/AllDSSynapses.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ class AllDSSynapses : public AllSpikingSynapses {
121121
public:
122122
/// Allocate GPU memories to store all synapses' states,
123123
/// and copy them from host to GPU memory.
124-
///
125-
/// @param allEdgesDevice GPU address of the allEdges struct on device memory.
126-
virtual void allocEdgeDeviceStruct(void **allEdgesDevice) override;
124+
virtual void allocEdgeDeviceStruct() override;
127125

128126
/// Allocate GPU memories to store all synapses' states,
129127
/// and copy them from host to GPU memory.
@@ -136,13 +134,11 @@ class AllDSSynapses : public AllSpikingSynapses {
136134

137135
/// Delete GPU memories.
138136
///
139-
/// @param allEdgesDevice GPU address of the allEdges struct on device memory.
140-
virtual void deleteEdgeDeviceStruct(void *allEdgesDevice) override;
137+
virtual void deleteEdgeDeviceStruct() override;
141138

142139
/// Copy all synapses' data from host to device.
143140
///
144-
/// @param allEdgesDevice GPU address of the allEdges struct on device memory.
145-
virtual void copyEdgeHostToDevice(void *allEdgesDevice) override;
141+
virtual void copyEdgeHostToDevice() override;
146142

147143
/// Copy all synapses' data from host to device.
148144
///
@@ -154,8 +150,7 @@ class AllDSSynapses : public AllSpikingSynapses {
154150

155151
/// Copy all synapses' data from device to host.
156152
///
157-
/// @param allEdgesDevice GPU address of the allEdges struct on device memory.
158-
virtual void copyEdgeDeviceToHost(void *allEdgesDevice) override;
153+
virtual void copyEdgeDeviceToHost() override;
159154

160155
/// Set synapse class ID defined by enumClassSynapses for the caller's Synapse class.
161156
/// The class ID will be set to classSynapses_d in device memory,
@@ -263,4 +258,4 @@ template <class Archive> void AllDSSynapses::serialize(Archive &archive)
263258
archive(cereal::base_class<AllSpikingSynapses>(this), cereal::make_nvp("lastSpike", lastSpike_),
264259
cereal::make_nvp("r", r_), cereal::make_nvp("u", u_), cereal::make_nvp("D", D_),
265260
cereal::make_nvp("U", U_), cereal::make_nvp("F", F_));
266-
}
261+
}

0 commit comments

Comments
 (0)