diff --git a/CMakeLists.txt b/CMakeLists.txt index 75965c3da..5e6f9574c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,7 @@ cmake_minimum_required(VERSION 3.12) # #"YES" / GPU choice only available if CUDA library is installed and the GPU is CUDA capable. ############################################################################################ + if(NOT ENABLE_CUDA) set(ENABLE_CUDA NO) endif() @@ -161,18 +162,24 @@ include_directories( if(ENABLE_CUDA) - set(cuda_sources Simulator/Edges/Neuro/AllSynapsesDeviceFuncs_d.cpp - Simulator/Vertices/Neuro/AllVerticesDeviceFuncs_d.cpp - Simulator/Edges/Neuro/AllSpikingSynapses_d.cpp - Simulator/Edges/Neuro/AllSTDPSynapses_d.cpp - Simulator/Edges/Neuro/AllDSSynapses_d.cpp - Simulator/Edges/Neuro/AllDynamicSTDPSynapses_d.cpp - Simulator/Vertices/Neuro/AllLIFNeurons_d.cpp - Simulator/Vertices/Neuro/AllIZHNeurons_d.cpp - Simulator/Vertices/Neuro/AllIFNeurons_d.cpp - Simulator/Vertices/Neuro/AllSpikingNeurons_d.cpp + set(cuda_ConnectionsSources Simulator/Connections/Neuro/ConnGrowth_d.cpp ) - set_source_files_properties(${cuda_sources} PROPERTIES LANGUAGE CUDA) + set_source_files_properties(${cuda_ConnectionsSources} PROPERTIES LANGUAGE CUDA) + + set(cuda_EdgesSources Simulator/Edges/Neuro/AllSynapsesDeviceFuncs_d.cpp + Simulator/Edges/Neuro/AllSpikingSynapses_d.cpp + Simulator/Edges/Neuro/AllSTDPSynapses_d.cpp + Simulator/Edges/Neuro/AllDSSynapses_d.cpp + Simulator/Edges/Neuro/AllDynamicSTDPSynapses_d.cpp) + set_source_files_properties(${cuda_EdgesSources} PROPERTIES LANGUAGE CUDA) + + set(cuda_VerticesSources + Simulator/Vertices/Neuro/AllVerticesDeviceFuncs_d.cpp + Simulator/Vertices/Neuro/AllLIFNeurons_d.cpp + Simulator/Vertices/Neuro/AllIZHNeurons_d.cpp + Simulator/Vertices/Neuro/AllIFNeurons_d.cpp + Simulator/Vertices/Neuro/AllSpikingNeurons_d.cpp) + set_source_files_properties(${cuda_VerticesSources} PROPERTIES LANGUAGE CUDA) endif() #Collect source files and create libraries @@ -184,7 +191,7 @@ endif() file(GLOB_RECURSE Connections_Source Simulator/Connections/*.cpp Simulator/Connections/*.h) if(ENABLE_CUDA) - add_library(Connections STATIC ${Connections_Source} ${cuda_sources}) + add_library(Connections STATIC ${Connections_Source} ${cuda_ConnectionsSources}) else() list(REMOVE_ITEM Connections_Source "${CMAKE_CURRENT_SOURCE_DIR}/Simulator/Connections/Neuro/ConnGrowth_d.cpp") add_library(Connections STATIC ${Connections_Source}) @@ -195,7 +202,7 @@ endif() file(GLOB_RECURSE Vertices_Source Simulator/Vertices/*.cpp Simulator/Vertices/*.h) if(ENABLE_CUDA) - add_library(Vertices SHARED ${Vertices_Source} ${cuda_sources}) + add_library(Vertices STATIC ${Vertices_Source} ${cuda_VerticesSources}) else() list(REMOVE_ITEM Vertices_Source "${CMAKE_CURRENT_SOURCE_DIR}/Simulator/Vertices/Neuro/AllVerticesDeviceFuncs.h") list(REMOVE_ITEM Vertices_Source "${CMAKE_CURRENT_SOURCE_DIR}/Simulator/Vertices/Neuro/AllVerticesDeviceFuncs_d.cpp") @@ -212,7 +219,7 @@ endif() # Create Edges library file(GLOB_RECURSE Edges_Source Simulator/Edges/*.cpp Simulator/Edges/*.h) if(ENABLE_CUDA) - add_library(Edges SHARED ${Edges_Source} ${cuda_sources}) + add_library(Edges STATIC ${Edges_Source} ${cuda_EdgesSources}) else() list(REMOVE_ITEM Edges_Source "${CMAKE_CURRENT_SOURCE_DIR}/Simulator/Edges/Neuro/AllSynapsesDeviceFuncs.h") list(REMOVE_ITEM Edges_Source "${CMAKE_CURRENT_SOURCE_DIR}/Simulator/Edges/Neuro/AllSynapsesDeviceFuncs_d.cpp") diff --git a/Simulator/Connections/Neuro/ConnGrowth_d.cpp b/Simulator/Connections/Neuro/ConnGrowth_d.cpp index 58041fd8d..19f6e1ada 100644 --- a/Simulator/Connections/Neuro/ConnGrowth_d.cpp +++ b/Simulator/Connections/Neuro/ConnGrowth_d.cpp @@ -78,4 +78,7 @@ void ConnGrowth::updateSynapsesWeights(int numVertices, AllVertices &vertices, A synapses.copyDeviceEdgeCountsToHost(allEdgesDevice); // copy device synapse summation coordinate to host memory synapses.copyDeviceEdgeSumIdxToHost(allEdgesDevice); + synapses.copyDeviceEdgeWeightsToHost(allEdgesDevice); + + synapses.Setup_xml_to_file(); } diff --git a/Simulator/Edges/AllEdges.h b/Simulator/Edges/AllEdges.h index 9c208e5b7..f7236325c 100644 --- a/Simulator/Edges/AllEdges.h +++ b/Simulator/Edges/AllEdges.h @@ -139,7 +139,9 @@ class AllEdges { /// /// @param allEdgesDevice GPU address of the allEdges struct on device memory. virtual void copyDeviceEdgeSumIdxToHost(void *allEdgesDevice) = 0; - + virtual void copyDeviceEdgeWeightsToHost(void *allEdgesDevice) = 0; + virtual void Setup_xml_to_file() = 0; + //virtual std::string vectorToXML(const std::vector &matrix, int rows, int cols, const std::string &name) = 0 ; /// Advance all the Synapses in the simulation. /// Update the state of all edges for a time step. /// diff --git a/Simulator/Edges/NG911/All911Edges.h b/Simulator/Edges/NG911/All911Edges.h index 5ac9351a1..c16ed0741 100644 --- a/Simulator/Edges/NG911/All911Edges.h +++ b/Simulator/Edges/NG911/All911Edges.h @@ -75,6 +75,9 @@ class All911Edges : public AllEdges { virtual void copyEdgeDeviceToHost(void *allEdgesDevice) {}; virtual void copyDeviceEdgeCountsToHost(void *allEdgesDevice) {}; virtual void copyDeviceEdgeSumIdxToHost(void *allEdgesDevice) {}; + virtual void copyDeviceEdgeWeightsToHost(void *allEdgesDevice) {}; + virtual void Setup_xml_to_file() {}; + //virtual std::string vectorToXML(const std::vector &matrix, int rows, int cols, const std::string &name){}; virtual void advanceEdges(void *allEdgesDevice, void *allVerticesDevice, void *edgeIndexMapDevice) {}; virtual void setAdvanceEdgesDeviceParams() {}; diff --git a/Simulator/Edges/Neuro/AllSpikingSynapses.h b/Simulator/Edges/Neuro/AllSpikingSynapses.h index 1d3158388..c78f532dd 100644 --- a/Simulator/Edges/Neuro/AllSpikingSynapses.h +++ b/Simulator/Edges/Neuro/AllSpikingSynapses.h @@ -167,7 +167,9 @@ class AllSpikingSynapses : public AllNeuroEdges { /// /// @param allEdgesDevice GPU address of the allEdges struct on device memory. virtual void copyDeviceEdgeSumIdxToHost(void *allEdgesDevice) override; - + virtual void copyDeviceEdgeWeightsToHost(void *allEdgesDevice) ; + virtual void Setup_xml_to_file() ; + //virtual std::string vectorToXML(const std::vector &matrix, int rows, int cols, const std::string &name) ; /// Advance all the Synapses in the simulation. /// Update the state of all synapses for a time step. /// diff --git a/Simulator/Edges/Neuro/AllSpikingSynapses_d.cpp b/Simulator/Edges/Neuro/AllSpikingSynapses_d.cpp index bcb51b33d..42a062d0d 100644 --- a/Simulator/Edges/Neuro/AllSpikingSynapses_d.cpp +++ b/Simulator/Edges/Neuro/AllSpikingSynapses_d.cpp @@ -273,6 +273,60 @@ void AllSpikingSynapses::copyDeviceEdgeCountsToHost(void *allEdgesDevice) //allEdges.countVertices_ = 0; } +// +void AllSpikingSynapses::copyDeviceEdgeWeightsToHost(void *allEdgesDevice) +{ + AllSpikingSynapsesDeviceProperties allEdgesDeviceProps; + int numVertices = Simulator::getInstance().getTotalVertices(); + BGSIZE maxTotalSynapses = Simulator::getInstance().getMaxEdgesPerVertex() * numVertices; + int vertexCount = Simulator::getInstance().getTotalVertices(); + HANDLE_ERROR(cudaMemcpy(&allEdgesDeviceProps, allEdgesDevice, + sizeof(AllSpikingSynapsesDeviceProperties), cudaMemcpyDeviceToHost)); + // std::cout << "size: " << vertexCount * vertexCount * sizeof(BGFLOAT) << std::endl; + // std::cout << "W_.data(): " << W_.data() << std::endl; + // std::cout << "allEdgesDeviceProps.W_: " << allEdgesDeviceProps.W_ << std::endl; + HANDLE_ERROR(cudaMemcpy(W_.data(), allEdgesDeviceProps.W_, + maxTotalSynapses * sizeof(BGFLOAT), cudaMemcpyDeviceToHost)); +} + // Set countVertices_ to 0 to avoid illegal memory deallocation + // at AllSpikingSynapses deconstructor. + //allEdges.countVertices_ = 0; + +std::string vectorToXML(const std::vector& matrix, int rows, int cols, const std::string& name) { + std::ostringstream os; + os << "\n"; + + int index = 0; + std::for_each(matrix.begin(), matrix.end(), [&](BGFLOAT value) mutable { + if (index % cols == 0) os << " "; // new row + os << value << " "; + if (++index % cols == 0) os << "\n"; // New line after every row + }); + + os << ""; + return os.str(); +} + +void AllSpikingSynapses::Setup_xml_to_file() { + const std::string filename = "/DATA/Weights.xml"; // Hardcoded filename + int vertexCount = Simulator::getInstance().getTotalVertices(); + + std::ofstream outFile(filename); + if (!outFile) { + std::cerr << "Error: Unable to open file " << filename << std::endl; + std::cerr << "Error details: " << strerror(errno) << std::endl; + return; + } + + std::string xmlContent = vectorToXML(W_, vertexCount, vertexCount, "WeightMatrix"); + outFile << xmlContent; + outFile.close(); + + std::cout << "Success: XML written to " << filename << std::endl; +} + + + /// Get summationCoord and in_use in AllEdges struct on device memory. /// /// @param allEdgesDevice GPU address of the AllSpikingSynapsesDeviceProperties struct