Skip to content

[ISSUE-131] Moving GPU memory management to OperationManager #841

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

Open
wants to merge 34 commits into
base: SharedDevelopment
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
2fbafc3
modify copyGPUtoCPU() and copyCPUtoGPU() to include neuron info
Apr 7, 2025
969407b
modified deleteDeviceStruct and allocDeviceStruct to use copyToCPu an…
Apr 8, 2025
6dc96c6
seperated the allocation from memory copying
Apr 9, 2025
f983191
Merge pull request #827 from UWB-Biocomputing/issue-825-refactorCopyT…
BenYang2002 Apr 9, 2025
67246ad
added the new operation to the operation.h and operationanager.cpp
Apr 9, 2025
fbaba9b
added the getter for allVerticesDevice_ and allEdgesDevice_
Apr 10, 2025
beaac28
modified the function: allocNeuronDeviceStruct so that now it does n…
Apr 10, 2025
3c4805f
fixed the getter for AllSpikingSynapsesDeviceProperties and AllSpiki…
Apr 10, 2025
09fe396
modified allocEdgeDeviceStruct and allocNeuronDeviceStruct in multip…
Apr 10, 2025
9cbc980
completed the allocateGPU for the operationManager
Apr 10, 2025
3a5c66d
Merge pull request #831 from UWB-Biocomputing/issue-828-allocateOp
BenYang2002 Apr 10, 2025
2110b50
refactor the function copySynapseIndexMapHostToDevice, so that it doe…
Apr 14, 2025
f3f5c79
refactored the copyToGPU for all the neuron classes, such that they t…
Apr 14, 2025
8ef565e
modified copyToDevice method in AllVertice class and its child class,…
Apr 14, 2025
d12e4d7
done for the documentation and implementation for registering the cop…
Apr 15, 2025
d0dd8be
Merge pull request #834 from UWB-Biocomputing/issue-833-copyFromGPU
BenYang2002 Apr 16, 2025
a48604e
modified function copyFromDevice for allVertices class as well as the…
Apr 20, 2025
886452d
Merge pull request #837 from UWB-Biocomputing/issue-835-copyFromGPU
BenYang2002 Apr 20, 2025
93aedae
modified the Refactor deleteNeuronDeviceStruct in AllVertices and all…
Apr 20, 2025
9c7e1dd
registered deleteEdgeDeviceStruct in AllEdges class and deleteNeuronD…
Apr 22, 2025
6010109
Merge pull request #839 from UWB-Biocomputing/issue-838-deallocateGPU…
BenYang2002 Apr 22, 2025
d5e6d64
using clang-format to fix the formatting issue
Apr 22, 2025
8efd1f0
registered registerHistoryVariable to the operationManager now instea…
Apr 24, 2025
d24a73e
Merge pull request #840 from UWB-Biocomputing/issue-821-registerHisto…
BenYang2002 Apr 24, 2025
ecaceb1
resolved issues from the pull request#841
Apr 25, 2025
1138682
Update format.yml
BenYang2002 May 2, 2025
7c7c8d4
Update format.yml
BenYang2002 May 2, 2025
b0b8f26
renamed the copySynapseIndexMapHostToDevice into copyCPUtoGPU, now we…
May 5, 2025
5270348
Merge branch 'BenDevelopment' of https://github.com/UWB-Biocomputing/…
May 5, 2025
e2ad9d1
resolved some issues from the pull request into the sharedDevelopment…
May 6, 2025
ac9d885
resolved merge conflict
May 8, 2025
7f48591
auto fixing code format with clang format
May 8, 2025
b93c10f
resolved more issue from the pull request
May 9, 2025
08f9e61
fixed some bug
May 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Simulator/Core/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ int Core::runSimulation(string executableName, string cmdLineArguments)
}

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

// Run simulation
LOG4CPLUS_TRACE(consoleLogger, "Starting Simulation");
Expand Down
238 changes: 136 additions & 102 deletions Simulator/Core/GPUModel.cpp

Large diffs are not rendered by default.

38 changes: 22 additions & 16 deletions Simulator/Core/GPUModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
#pragma once

#include "AllEdges.h"
#include "AllSpikingNeurons.h"
#include "AllSpikingSynapses.h"
#include "AllVertices.h"
#include "OperationManager.h"

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

/// Copy GPU edge data to CPU.
virtual void copyGPUtoCPU() override;

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

// GPUModel itself does not have anything to be copied back, this function is a
// dummy function just to make GPUModel non virtual
virtual void copyGPUtoCPU() override
{
}

/// Print out EdgeProps on the GPU.
void printGPUEdgesPropsModel() const;

/// Getter for edge (synapse) structures in device memory
AllEdgesDeviceProperties *&getAllEdgesDevice();

/// Getter for vertex (neuron) structures in device memory
AllVerticesDeviceProperties *&getAllVerticesDevice();

protected:
/// Allocates and initializes memories on CUDA device.
/// @param[out] allVerticesDevice Memory location of the pointer to the vertices list on device memory.
/// @param[out] allEdgesDevice Memory location of the pointer to the edges list on device memory.
void allocDeviceStruct(void **allVerticesDevice, void **allEdgesDevice);
void allocDeviceStruct();

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

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

void deleteEdgeIndexMap();

public: //2020/03/14 changed to public for accessing in Core
void copyEdgeIndexMapHostToDevice(EdgeIndexMap &edgeIndexMapHost, int numVertices);

private:
void updateHistory();

Expand Down
2 changes: 2 additions & 0 deletions Simulator/Core/OperationManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ string OperationManager::operationToString(const Operations &operation) const
return "copyToGPU";
case Operations::copyFromGPU:
return "copyFromGPU";
case Operations::allocateGPU:
return "allocateGPU";
default:
return "Operation isn't in OperationManager::operationToString()";
}
Expand Down
6 changes: 4 additions & 2 deletions Simulator/Core/Operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ enum class Operations {
deallocateGPUMemory, // Make sure deallocate memory isn't called until all GPU memory is copied back.
restoreToDefault, // Not sure what this refers to.
copyToGPU,
copyFromGPU
};
copyFromGPU,
allocateGPU,
registerHistoryVariables
};
3 changes: 1 addition & 2 deletions Simulator/Core/Serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ bool Serializer::deserialize()

#if defined(USE_GPU)
GPUModel &gpuModel = static_cast<GPUModel &>(simulator.getModel());
gpuModel.copyEdgeIndexMapHostToDevice(simulator.getModel().getConnections().getEdgeIndexMap(),
simulator.getTotalVertices());
gpuModel.copyCPUtoGPU();
Comment on lines 67 to +70
Copy link
Contributor

Choose a reason for hiding this comment

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

Need to ask @d-kamath : why is only the GPUModel data being copied to the GPU? Why aren't we copying everything (which can be accomplished via the OperationManager now)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I contacted Dyvia and will post her answer later she replied

#endif // USE_GPU

return true;
Expand Down
22 changes: 22 additions & 0 deletions Simulator/Edges/AllEdges.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ AllEdges::AllEdges() : totalEdgeCount_(0), maxEdgesPerVertex_(0), countVertices_
OperationManager::getInstance().registerOperation(Operations::printParameters,
printParametersFunc);

#if defined(USE_GPU)
// Register allocNeuronDeviceStruct function as a allocateGPU operation in the OperationManager
function<void()> allocateGPU
= bind(static_cast<void (AllEdges::*)()>(&AllEdges::allocEdgeDeviceStruct), this);
OperationManager::getInstance().registerOperation(Operations::allocateGPU, allocateGPU);

// Register AllEdges::copyEdgeHostToDevice function as a copyToGPU operation in the OperationManager
function<void()> copyCPUtoGPU
= bind(static_cast<void (AllEdges::*)()>(&AllEdges::copyEdgeHostToDevice), this);
OperationManager::getInstance().registerOperation(Operations::copyToGPU, copyCPUtoGPU);

// Register copyFromGPU operation for transferring edge data from device to host
function<void()> copyFromGPU = bind(&AllEdges::copyEdgeDeviceToHost, this);
OperationManager::getInstance().registerOperation(Operations::copyFromGPU, copyFromGPU);

// Register deleteEdgeDeviceStruct function as a deallocateGPUMemory operation in the OperationManager
function<void()> deallocateGPUMemory = bind(&AllEdges::deleteEdgeDeviceStruct, this);
OperationManager::getInstance().registerOperation(Operations::deallocateGPUMemory,
deallocateGPUMemory);

#endif

fileLogger_ = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("file"));
edgeLogger_ = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("edge"));
}
Expand Down
14 changes: 4 additions & 10 deletions Simulator/Edges/AllEdges.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ class AllEdges {
public:
/// Allocate GPU memories to store all edges' states,
/// and copy them from host to GPU memory.
///
/// @param allEdgesDevice GPU address of the allEdges struct on device memory.
virtual void allocEdgeDeviceStruct(void **allEdgesDevice) = 0;
virtual void allocEdgeDeviceStruct() = 0;

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

/// Delete GPU memories.
///
/// @param allEdgesDevice GPU address of the allEdges struct on device memory.
virtual void deleteEdgeDeviceStruct(void *allEdgesDevice) = 0;
virtual void deleteEdgeDeviceStruct() = 0;

/// Copy all edges' data from host to device.
///
/// @param allEdgesDevice GPU address of the allEdges struct on device memory.
virtual void copyEdgeHostToDevice(void *allEdgesDevice) = 0;
virtual void copyEdgeHostToDevice() = 0;

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

/// Copy all edges' data from device to host.
///
/// @param allEdgesDevice GPU address of the allEdges struct on device memory.
virtual void copyEdgeDeviceToHost(void *allEdgesDevice) = 0;
virtual void copyEdgeDeviceToHost() = 0;

/// Get edge_counts in AllEdges struct on device memory.
///
Expand Down
8 changes: 4 additions & 4 deletions Simulator/Edges/NG911/All911Edges.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ class All911Edges : public AllEdges {
// GPU functionality for 911 simulation is unimplemented.
// These signatures are required to make the class non-abstract
public:
virtual void allocEdgeDeviceStruct(void **allEdgesDevice) {};
virtual void allocEdgeDeviceStruct() {};
virtual void allocEdgeDeviceStruct(void **allEdgesDevice, int numVertices,
int maxEdgesPerVertex) {};
virtual void deleteEdgeDeviceStruct(void *allEdgesDevice) {};
virtual void copyEdgeHostToDevice(void *allEdgesDevice) {};
virtual void deleteEdgeDeviceStruct() {};
virtual void copyEdgeHostToDevice() {};
virtual void copyEdgeHostToDevice(void *allEdgesDevice, int numVertices, int maxEdgesPerVertex) {
};
virtual void copyEdgeDeviceToHost(void *allEdgesDevice) {};
virtual void copyEdgeDeviceToHost() {};
virtual void copyDeviceEdgeCountsToHost(void *allEdgesDevice) {};
virtual void advanceEdges(void *allEdgesDevice, void *allVerticesDevice,
void *edgeIndexMapDevice) {};
Expand Down
13 changes: 4 additions & 9 deletions Simulator/Edges/Neuro/AllDSSynapses.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ class AllDSSynapses : public AllSpikingSynapses {
public:
/// Allocate GPU memories to store all synapses' states,
/// and copy them from host to GPU memory.
///
/// @param allEdgesDevice GPU address of the allEdges struct on device memory.
virtual void allocEdgeDeviceStruct(void **allEdgesDevice) override;
virtual void allocEdgeDeviceStruct() override;

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

/// Delete GPU memories.
///
/// @param allEdgesDevice GPU address of the allEdges struct on device memory.
virtual void deleteEdgeDeviceStruct(void *allEdgesDevice) override;
virtual void deleteEdgeDeviceStruct() override;

/// Copy all synapses' data from host to device.
///
/// @param allEdgesDevice GPU address of the allEdges struct on device memory.
virtual void copyEdgeHostToDevice(void *allEdgesDevice) override;
virtual void copyEdgeHostToDevice() override;

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

/// Copy all synapses' data from device to host.
///
/// @param allEdgesDevice GPU address of the allEdges struct on device memory.
virtual void copyEdgeDeviceToHost(void *allEdgesDevice) override;
virtual void copyEdgeDeviceToHost() override;

/// Set synapse class ID defined by enumClassSynapses for the caller's Synapse class.
/// The class ID will be set to classSynapses_d in device memory,
Expand Down
30 changes: 14 additions & 16 deletions Simulator/Edges/Neuro/AllDSSynapses_d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@

/// Allocate GPU memories to store all synapses' states,
/// and copy them from host to GPU memory.
///
/// @param allEdgesDevice GPU address of the AllDSSynapsesDeviceProperties struct
/// on device memory.
void AllDSSynapses::allocEdgeDeviceStruct(void **allEdgesDevice)
void AllDSSynapses::allocEdgeDeviceStruct()
{
GPUModel *gpuModel = static_cast<GPUModel *>(&Simulator::getInstance().getModel());
void **allEdgesDevice = reinterpret_cast<void **>(&(gpuModel->getAllEdgesDevice()));
allocEdgeDeviceStruct(allEdgesDevice, Simulator::getInstance().getTotalVertices(),
Simulator::getInstance().getMaxEdgesPerVertex());
}
Expand Down Expand Up @@ -67,12 +66,11 @@ void AllDSSynapses::allocDeviceStruct(AllDSSynapsesDeviceProperties &allEdges, i

/// Delete GPU memories.
///
/// @param allEdgesDevice GPU address of the AllDSSynapsesDeviceProperties struct
/// on device memory.
void AllDSSynapses::deleteEdgeDeviceStruct(void *allEdgesDevice)
void AllDSSynapses::deleteEdgeDeviceStruct()
{
AllDSSynapsesDeviceProperties allEdges;

GPUModel *gpuModel = static_cast<GPUModel *>(&Simulator::getInstance().getModel());
void *allEdgesDevice = static_cast<void *>(gpuModel->getAllEdgesDevice());
HANDLE_ERROR(cudaMemcpy(&allEdges, allEdgesDevice, sizeof(AllDSSynapsesDeviceProperties),
cudaMemcpyDeviceToHost));

Expand Down Expand Up @@ -100,10 +98,10 @@ void AllDSSynapses::deleteDeviceStruct(AllDSSynapsesDeviceProperties &allEdgesDe

/// Copy all synapses' data from host to device.
///
/// @param allEdgesDevice GPU address of the AllDSSynapsesDeviceProperties struct
/// on device memory.
void AllDSSynapses::copyEdgeHostToDevice(void *allEdgesDevice)
void AllDSSynapses::copyEdgeHostToDevice()
{ // copy everything necessary
GPUModel *gpuModel = static_cast<GPUModel *>(&Simulator::getInstance().getModel());
void *allEdgesDevice = static_cast<void *>(gpuModel->getAllEdgesDevice());
copyEdgeHostToDevice(allEdgesDevice, Simulator::getInstance().getTotalVertices(),
Simulator::getInstance().getMaxEdgesPerVertex());
}
Expand Down Expand Up @@ -156,13 +154,12 @@ void AllDSSynapses::copyHostToDevice(void *allEdgesDevice,

/// Copy all synapses' data from device to host.
///
/// @param allEdgesDevice GPU address of the AllDSSynapsesDeviceProperties struct
/// on device memory.
void AllDSSynapses::copyEdgeDeviceToHost(void *allEdgesDevice)
void AllDSSynapses::copyEdgeDeviceToHost()
{
// copy everything necessary
AllDSSynapsesDeviceProperties allEdgesDeviceProps;

GPUModel *gpuModel = static_cast<GPUModel *>(&Simulator::getInstance().getModel());
void *allEdgesDevice = static_cast<void *>(gpuModel->getAllEdgesDevice());
HANDLE_ERROR(cudaMemcpy(&allEdgesDeviceProps, allEdgesDevice,
sizeof(AllDSSynapsesDeviceProperties), cudaMemcpyDeviceToHost));

Expand Down Expand Up @@ -328,7 +325,8 @@ void AllDSSynapses::printGPUEdgesProps(void *allEdgesDeviceProps) const
}

for (int i = 0; i < countVertices_; i++) {
cout << "GPU edge_counts:" << "neuron[" << i << "]" << synapseCountsPrint[i] << endl;
cout << "GPU edge_counts:"
<< "neuron[" << i << "]" << synapseCountsPrint[i] << endl;
}

cout << "GPU totalSynapseCount:" << totalSynapseCountPrint << endl;
Expand Down
15 changes: 5 additions & 10 deletions Simulator/Edges/Neuro/AllDynamicSTDPSynapses.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,8 @@ class AllDynamicSTDPSynapses : public AllSTDPSynapses {
#if defined(USE_GPU)
public:
/// Allocate GPU memories to store all synapses' states,
/// and copy them from host to GPU memory.
///
/// @param allEdgesDevice GPU address of the allEdges struct on device memory.
virtual void allocEdgeDeviceStruct(void **allEdgesDevice) override;
/// and copy them from host to GPU memory. memory.
virtual void allocEdgeDeviceStruct() override;

/// Allocate GPU memories to store all synapses' states,
/// and copy them from host to GPU memory.
Expand All @@ -139,13 +137,11 @@ class AllDynamicSTDPSynapses : public AllSTDPSynapses {

/// Delete GPU memories.
///
/// @param allEdgesDevice GPU address of the allEdges struct on device memory.
virtual void deleteEdgeDeviceStruct(void *allEdgesDevice) override;
virtual void deleteEdgeDeviceStruct() override;

/// Copy all synapses' data from host to device.
///
/// @param allEdgesDevice GPU address of the allEdges struct on device memory.
virtual void copyEdgeHostToDevice(void *allEdgesDevice) override;
virtual void copyEdgeHostToDevice() override;

/// Copy all synapses' data from host to device.
///
Expand All @@ -157,8 +153,7 @@ class AllDynamicSTDPSynapses : public AllSTDPSynapses {

/// Copy all synapses' data from device to host.
///
/// @param allEdgesDevice GPU address of the allEdges struct on device memory.
virtual void copyEdgeDeviceToHost(void *allEdgesDevice) override;
virtual void copyEdgeDeviceToHost() override;

/// Set synapse class ID defined by enumClassSynapses for the caller's Synapse class.
/// The class ID will be set to classSynapses_d in device memory,
Expand Down
Loading
Loading