1212#include " AllVertices.h"
1313#include " Connections.h"
1414#include " Global.h"
15+ #include " OperationManager.h"
16+
1517#ifdef VALIDATION_MODE
1618 #include " AllIFNeurons.h"
1719 #include " OperationManager.h"
@@ -27,31 +29,34 @@ GPUModel::GPUModel() :
2729 Model::Model(), edgeIndexMapDevice_(nullptr ), randNoise_d(nullptr ), allVerticesDevice_(nullptr ),
2830 allEdgesDevice_(nullptr )
2931{
32+ // Register allocNeuronDeviceStruct function as a allocateGPU operation in the OperationManager
33+ function<void ()> allocateGPU = bind (&GPUModel::allocDeviceStruct, this );
34+ OperationManager::getInstance ().registerOperation (Operations::allocateGPU, allocateGPU);
35+
36+ // Register copyCPUtoGPU function as a copyCPUtoGPU operation in the OperationManager
37+ function<void ()> copyCPUtoGPU = bind (&GPUModel::copyCPUtoGPU, this );
38+ OperationManager::getInstance ().registerOperation (Operations::copyToGPU, copyCPUtoGPU);
39+
40+ // Note: We do not register a corresponding copyFromGPU operation here because
41+ // we are only copying the synapseIndexMap to the GPU. This map is a read-only lookup table
42+ // that gets recreated from scratch on each update. As a result, we only need to allocate,
43+ // copy to GPU, and deallocate — there is no meaningful data to copy back from the GPU.
44+
45+ // Register deleteSynapseImap function as a deallocateGPUMemory operation in the OperationManager
46+ function<void ()> deallocateGPUMemory = bind (&GPUModel::deleteDeviceStruct, this );
47+ OperationManager::getInstance ().registerOperation (Operations::deallocateGPUMemory,
48+ deallocateGPUMemory);
3049}
3150
3251// / Allocates and initializes memories on CUDA device.
33- // / @param[out] allVerticesDevice Memory location of the pointer to the vertices list on device memory.
34- // / @param[out] allEdgesDevice Memory location of the pointer to the edges list on device memory.
35- void GPUModel::allocDeviceStruct (void **allVerticesDevice, void **allEdgesDevice)
52+ void GPUModel::allocDeviceStruct ()
3653{
37- // Get vertices and edges
38- AllVertices &vertices = layout_->getVertices ();
39- AllEdges &edges = connections_->getEdges ();
40-
41- // Allocate vertices and edges structs on GPU device memory
42- vertices.allocVerticesDeviceStruct (allVerticesDevice);
43- edges.allocEdgeDeviceStruct (allEdgesDevice);
44-
4554 // Allocate memory for random noise array
4655 int numVertices = Simulator::getInstance ().getTotalVertices ();
4756 // BGSIZE randNoise_d_size = numVertices * sizeof(float); // size of random noise array
4857 // HANDLE_ERROR(cudaMalloc((void **)&randNoise_d, randNoise_d_size));
4958
50- // Copy host vertex and edge arrays into GPU device
51- vertices.copyToDevice (*allVerticesDevice);
52- edges.copyEdgeHostToDevice (*allEdgesDevice);
53-
54- // Allocate edge inverse map in device memory
59+ // Allocate synapse inverse map in device memory
5560 allocEdgeIndexMap (numVertices);
5661
5762 // Create the CUDA stream used to launch synchronous GPU kernels during the simulation.
@@ -62,16 +67,8 @@ void GPUModel::allocDeviceStruct(void **allVerticesDevice, void **allEdgesDevice
6267}
6368
6469// / Copies device memories to host memories and deallocates them.
65- // / @param[out] allVerticesDevice Memory location of the pointer to the vertices list on device memory.
66- // / @param[out] allEdgesDevice Memory location of the pointer to the edges list on device memory.
67- void GPUModel::deleteDeviceStruct (void **allVerticesDevice, void **allEdgesDevice)
70+ void GPUModel::deleteDeviceStruct ()
6871{
69- // Get vertices and edges
70- AllVertices &vertices = layout_->getVertices ();
71- AllEdges &edges = connections_->getEdges ();
72-
73- // Copy device edge and vertex structs to host memory
74- vertices.copyFromDevice (*allVerticesDevice);
7572 // Deallocate device memory
7673 vertices.deleteVerticesDeviceStruct (*allVerticesDevice);
7774 // Copy device edge and vertex structs to host memory
@@ -115,13 +112,9 @@ void GPUModel::setupSim()
115112 t_gpu_advanceSynapses = 0.0 ;
116113 t_gpu_calcSummation = 0.0 ;
117114#endif // PERFORMANCE_METRICS
118-
119- // allocates memories on CUDA device
120- allocDeviceStruct ((void **)&allVerticesDevice_, (void **)&allEdgesDevice_);
121-
122- EdgeIndexMap &edgeIndexMap = connections_->getEdgeIndexMap ();
123- // copy inverse map to the device memory
124- copyEdgeIndexMapHostToDevice (edgeIndexMap, Simulator::getInstance ().getTotalVertices ());
115+ // Allocate and copy neuron/synapse data structures to GPU memory
116+ OperationManager::getInstance ().executeOperation (Operations::allocateGPU);
117+ OperationManager::getInstance ().executeOperation (Operations::copyToGPU);
125118
126119 AllEdges &edges = connections_->getEdges ();
127120 // set some parameters used for advanceVerticesDevice
@@ -137,11 +130,14 @@ void GPUModel::setupSim()
137130// / Performs any finalization tasks on network following a simulation.
138131void GPUModel::finish ()
139132{
133+ // copy device synapse and neuron structs to host memory
134+ OperationManager::getInstance ().executeOperation (Operations::copyFromGPU);
140135 // deallocates memories on CUDA device
136+ // TODO: Resolve whether the deletion operations below that don't seem bundled
137+ // with the OperationManager are necessary, should be moved, or if they can be removed.
141138 AsyncGenerator_.deleteDeviceStruct ();
142- deleteDeviceStruct (( void **)&allVerticesDevice_, ( void **)&allEdgesDevice_ );
139+ OperationManager::getInstance (). executeOperation (Operations::deallocateGPUMemory );
143140 deleteEdgeIndexMap ();
144-
145141#ifdef PERFORMANCE_METRICS
146142 cudaEventDestroy (start);
147143 cudaEventDestroy (stop);
@@ -250,7 +246,7 @@ void GPUModel::updateConnections()
250246 AllVertices &vertices = layout_->getVertices ();
251247 AllEdges &edges = connections_->getEdges ();
252248
253- vertices.copyFromDevice (allVerticesDevice_ );
249+ vertices.copyFromDevice ();
254250
255251 // Update Connections data
256252 if (connections_->updateConnections (vertices)) {
@@ -260,8 +256,7 @@ void GPUModel::updateConnections()
260256 // create edge index map
261257 connections_->createEdgeIndexMap ();
262258 // copy index map to the device memory
263- copyEdgeIndexMapHostToDevice (connections_->getEdgeIndexMap (),
264- Simulator::getInstance ().getTotalVertices ());
259+ copyCPUtoGPU ();
265260 }
266261}
267262
@@ -298,81 +293,53 @@ void GPUModel::allocEdgeIndexMap(int count)
298293 cudaMemcpyHostToDevice));
299294}
300295
301- // / Deallocate device memory for edge inverse map.
302- void GPUModel::deleteEdgeIndexMap ()
303- {
304- EdgeIndexMapDevice edgeIndexMapDevice;
305- HANDLE_ERROR (cudaMemcpy (&edgeIndexMapDevice, edgeIndexMapDevice_, sizeof (EdgeIndexMapDevice),
306- cudaMemcpyDeviceToHost));
307- HANDLE_ERROR (cudaFree (edgeIndexMapDevice.outgoingEdgeBegin_ ));
308- HANDLE_ERROR (cudaFree (edgeIndexMapDevice.outgoingEdgeCount_ ));
309- HANDLE_ERROR (cudaFree (edgeIndexMapDevice.outgoingEdgeIndexMap_ ));
310- HANDLE_ERROR (cudaFree (edgeIndexMapDevice.incomingEdgeBegin_ ));
311- HANDLE_ERROR (cudaFree (edgeIndexMapDevice.incomingEdgeCount_ ));
312- HANDLE_ERROR (cudaFree (edgeIndexMapDevice.incomingEdgeIndexMap_ ));
313- HANDLE_ERROR (cudaFree (edgeIndexMapDevice_));
314- }
315-
316- // / Copy EdgeIndexMap in host memory to EdgeIndexMap in device memory.
317- // / @param edgeIndexMapHost Reference to the EdgeIndexMap in host memory.
318- void GPUModel::copyEdgeIndexMapHostToDevice (EdgeIndexMap &edgeIndexMapHost, int numVertices)
296+ // / Allocate and Copy CPU Synapse data to GPU.
297+ void GPUModel::copyCPUtoGPU ()
319298{
320- AllEdges &edges = connections_->getEdges ();
321- int totalEdgeCount = edges.totalEdgeCount_ ;
322- if (totalEdgeCount == 0 )
299+ EdgeIndexMap synapseIndexMapHost = connections_->getEdgeIndexMap ();
300+ int numVertices = Simulator::getInstance ().getTotalVertices ();
301+ AllEdges &synapses = connections_->getEdges ();
302+ int totalSynapseCount = dynamic_cast <AllEdges &>(synapses).totalEdgeCount_ ;
303+ if (totalSynapseCount == 0 )
323304 return ;
324- EdgeIndexMapDevice edgeIndexMapDevice ;
325- HANDLE_ERROR (cudaMemcpy (&edgeIndexMapDevice , edgeIndexMapDevice_, sizeof (EdgeIndexMapDevice),
305+ EdgeIndexMapDevice synapseIMapDevice ;
306+ HANDLE_ERROR (cudaMemcpy (&synapseIMapDevice , edgeIndexMapDevice_, sizeof (EdgeIndexMapDevice),
326307 cudaMemcpyDeviceToHost));
327- HANDLE_ERROR (cudaMemcpy (edgeIndexMapDevice .outgoingEdgeBegin_ ,
328- edgeIndexMapHost .outgoingEdgeBegin_ .data (), numVertices * sizeof (BGSIZE ),
329- cudaMemcpyHostToDevice));
330- HANDLE_ERROR (cudaMemcpy (edgeIndexMapDevice .outgoingEdgeCount_ ,
331- edgeIndexMapHost .outgoingEdgeCount_ .data (), numVertices * sizeof (BGSIZE ),
332- cudaMemcpyHostToDevice));
333- if (edgeIndexMapDevice .outgoingEdgeIndexMap_ != nullptr ) {
334- HANDLE_ERROR (cudaFree (edgeIndexMapDevice .outgoingEdgeIndexMap_ ));
308+ HANDLE_ERROR (cudaMemcpy (synapseIMapDevice .outgoingEdgeBegin_ ,
309+ synapseIndexMapHost .outgoingEdgeBegin_ .data (),
310+ numVertices * sizeof (BGSIZE), cudaMemcpyHostToDevice));
311+ HANDLE_ERROR (cudaMemcpy (synapseIMapDevice .outgoingEdgeCount_ ,
312+ synapseIndexMapHost .outgoingEdgeCount_ .data (),
313+ numVertices * sizeof (BGSIZE), cudaMemcpyHostToDevice));
314+ if (synapseIMapDevice .outgoingEdgeIndexMap_ != nullptr ) {
315+ HANDLE_ERROR (cudaFree (synapseIMapDevice .outgoingEdgeIndexMap_ ));
335316 }
336- HANDLE_ERROR (cudaMalloc ((void **)&edgeIndexMapDevice .outgoingEdgeIndexMap_ ,
337- totalEdgeCount * sizeof (BGSIZE)));
338- HANDLE_ERROR (cudaMemcpy (edgeIndexMapDevice .outgoingEdgeIndexMap_ ,
339- edgeIndexMapHost .outgoingEdgeIndexMap_ .data (),
340- totalEdgeCount * sizeof (BGSIZE), cudaMemcpyHostToDevice));
317+ HANDLE_ERROR (cudaMalloc ((void **)&synapseIMapDevice .outgoingEdgeIndexMap_ ,
318+ totalSynapseCount * sizeof (BGSIZE)));
319+ HANDLE_ERROR (cudaMemcpy (synapseIMapDevice .outgoingEdgeIndexMap_ ,
320+ synapseIndexMapHost .outgoingEdgeIndexMap_ .data (),
321+ totalSynapseCount * sizeof (BGSIZE), cudaMemcpyHostToDevice));
341322 // active synapse map
342- HANDLE_ERROR (cudaMemcpy (edgeIndexMapDevice .incomingEdgeBegin_ ,
343- edgeIndexMapHost .incomingEdgeBegin_ .data (), numVertices * sizeof (BGSIZE ),
344- cudaMemcpyHostToDevice));
345- HANDLE_ERROR (cudaMemcpy (edgeIndexMapDevice .incomingEdgeCount_ ,
346- edgeIndexMapHost .incomingEdgeCount_ .data (), numVertices * sizeof (BGSIZE ),
347- cudaMemcpyHostToDevice));
323+ HANDLE_ERROR (cudaMemcpy (synapseIMapDevice .incomingEdgeBegin_ ,
324+ synapseIndexMapHost .incomingEdgeBegin_ .data (),
325+ numVertices * sizeof (BGSIZE), cudaMemcpyHostToDevice));
326+ HANDLE_ERROR (cudaMemcpy (synapseIMapDevice .incomingEdgeCount_ ,
327+ synapseIndexMapHost .incomingEdgeCount_ .data (),
328+ numVertices * sizeof (BGSIZE), cudaMemcpyHostToDevice));
348329 // the number of synapses may change, so we reallocate the memory
349- if (edgeIndexMapDevice .incomingEdgeIndexMap_ != nullptr ) {
350- HANDLE_ERROR (cudaFree (edgeIndexMapDevice .incomingEdgeIndexMap_ ));
351- edgeIndexMapDevice .incomingEdgeIndexMap_ = nullptr ;
330+ if (synapseIMapDevice .incomingEdgeIndexMap_ != nullptr ) {
331+ HANDLE_ERROR (cudaFree (synapseIMapDevice .incomingEdgeIndexMap_ ));
332+ synapseIMapDevice .incomingEdgeIndexMap_ = nullptr ;
352333 }
353- HANDLE_ERROR (cudaMalloc ((void **)&edgeIndexMapDevice .incomingEdgeIndexMap_ ,
354- totalEdgeCount * sizeof (BGSIZE)));
355- HANDLE_ERROR (cudaMemcpy (edgeIndexMapDevice .incomingEdgeIndexMap_ ,
356- edgeIndexMapHost .incomingEdgeIndexMap_ .data (),
357- totalEdgeCount * sizeof (BGSIZE), cudaMemcpyHostToDevice));
358- HANDLE_ERROR (cudaMemcpy (edgeIndexMapDevice_, &edgeIndexMapDevice , sizeof (EdgeIndexMapDevice),
334+ HANDLE_ERROR (cudaMalloc ((void **)&synapseIMapDevice .incomingEdgeIndexMap_ ,
335+ totalSynapseCount * sizeof (BGSIZE)));
336+ HANDLE_ERROR (cudaMemcpy (synapseIMapDevice .incomingEdgeIndexMap_ ,
337+ synapseIndexMapHost .incomingEdgeIndexMap_ .data (),
338+ totalSynapseCount * sizeof (BGSIZE), cudaMemcpyHostToDevice));
339+ HANDLE_ERROR (cudaMemcpy (edgeIndexMapDevice_, &synapseIMapDevice , sizeof (EdgeIndexMapDevice),
359340 cudaMemcpyHostToDevice));
360341}
361342
362- // / Copy GPU edge data to CPU.
363- void GPUModel::copyGPUtoCPU ()
364- {
365- // copy device edge structs to host memory
366- connections_->getEdges ().copyEdgeDeviceToHost (allEdgesDevice_);
367- }
368-
369- // / Copy CPU edge data to GPU.
370- void GPUModel::copyCPUtoGPU ()
371- {
372- // copy host edge structs to device memory
373- connections_->getEdges ().copyEdgeHostToDevice (allEdgesDevice_);
374- }
375-
376343// / Print out EdgeProps on the GPU.
377344void GPUModel::printGPUEdgesPropsModel () const
378345{
0 commit comments