@@ -108,7 +108,8 @@ void Hdf5Recorder::saveSimData(const AllVertices &neurons)
108108{
109109 // Initialize datasets for constant variables
110110 for (auto &variableInfo : variableTable_) {
111- if (variableInfo.variableType_ == UpdatedType::CONSTANT) {
111+ if (variableInfo.variableLocation_ .getNumElements () > 0
112+ && variableInfo.variableType_ == UpdatedType::CONSTANT) {
112113 // Define dimensions for the constant dataset
113114 hsize_t constantDims[1 ]
114115 = {static_cast <hsize_t >(variableInfo.variableLocation_ .getNumElements ())};
@@ -122,6 +123,94 @@ void Hdf5Recorder::saveSimData(const AllVertices &neurons)
122123 }
123124}
124125
126+ // Processes and updates HDF5 datasets for variables marked as DYNAMIC
127+ void Hdf5Recorder::compileHistories (AllVertices &vertices)
128+ {
129+ // Define the maximum chunk size for datasets to optimize storage and access
130+ const hsize_t max_chunk_size = 1024 ;
131+
132+ // Iterate over each variableInfo object in the variable table
133+ for (auto &variableInfo : variableTable_) {
134+ // Process only variables marked as DYNAMIC, which change over time
135+ if (variableInfo.variableType_ == UpdatedType::DYNAMIC) {
136+ // Check if there are elements to process
137+ if (variableInfo.variableLocation_ .getNumElements () > 0 ) {
138+ // Determine the new dimensions for the dataset based on the number of elements
139+ hsize_t newDims[1 ]
140+ = {static_cast <hsize_t >(variableInfo.variableLocation_ .getNumElements ())};
141+
142+ // Create the dataset if it does not already exist
143+ if (!H5Iis_valid (variableInfo.hdf5DataSet_ .getId ())) {
144+ // Create initial dataspace with unlimited maximum dimensions
145+ hsize_t maxDims[1 ] = {H5S_UNLIMITED};
146+ DataSpace initialSpace (1 , newDims, maxDims);
147+
148+ // Create dataset creation properties and set chunk size for efficiency
149+ DSetCreatPropList cparms;
150+ hsize_t chunk_dims[1 ] = {std::min (newDims[0 ], max_chunk_size)};
151+ cparms.setChunk (1 , chunk_dims);
152+
153+ // Create the dataset with specified name, datatype, initial space, and properties
154+ variableInfo.hdf5DataSet_ = resultOut_->createDataSet (
155+ variableInfo.variableName_ , variableInfo.hdf5Datatype_ , initialSpace, cparms);
156+
157+ // Write the initial data to the dataset
158+ variableInfo.captureData ();
159+ } else {
160+ // Handle the case where the dataset already exists
161+
162+ // Get the current dimensions of the dataset
163+ DataSpace currentSpace = variableInfo.hdf5DataSet_ .getSpace ();
164+ hsize_t currentDims[1 ];
165+ currentSpace.getSimpleExtentDims (currentDims, nullptr );
166+
167+ // Calculate the new dimensions after appending the new data
168+ hsize_t appendDims[1 ] = {currentDims[0 ] + newDims[0 ]};
169+ variableInfo.hdf5DataSet_ .extend (appendDims);
170+
171+ // Select the hyperslab in the extended portion of the dataset
172+ DataSpace fileSpace = variableInfo.hdf5DataSet_ .getSpace ();
173+ hsize_t offset[1 ] = {currentDims[0 ]};
174+ fileSpace.selectHyperslab (H5S_SELECT_SET, newDims, offset);
175+
176+ // Create a memory dataspace for the new data
177+ DataSpace memSpace (1 , newDims);
178+
179+ // Prepare the data buffer and write the new data to the dataset
180+ if (variableInfo.hdf5Datatype_ == PredType::NATIVE_FLOAT) {
181+ std::vector<float > dataBuffer (variableInfo.variableLocation_ .getNumElements ());
182+ for (size_t i = 0 ; i < variableInfo.variableLocation_ .getNumElements (); ++i) {
183+ dataBuffer[i] = get<float >(variableInfo.variableLocation_ .getElement (i));
184+ }
185+ variableInfo.hdf5DataSet_ .write (dataBuffer.data (), variableInfo.hdf5Datatype_ ,
186+ memSpace, fileSpace);
187+ } else if (variableInfo.hdf5Datatype_ == PredType::NATIVE_INT) {
188+ std::vector<int > dataBuffer (variableInfo.variableLocation_ .getNumElements ());
189+ for (size_t i = 0 ; i < variableInfo.variableLocation_ .getNumElements (); ++i) {
190+ dataBuffer[i] = get<int >(variableInfo.variableLocation_ .getElement (i));
191+ }
192+ variableInfo.hdf5DataSet_ .write (dataBuffer.data (), variableInfo.hdf5Datatype_ ,
193+ memSpace, fileSpace);
194+ } else if (variableInfo.hdf5Datatype_ == PredType::NATIVE_UINT64) {
195+ std::vector<uint64_t > dataBuffer (variableInfo.variableLocation_ .getNumElements ());
196+ for (size_t i = 0 ; i < variableInfo.variableLocation_ .getNumElements (); ++i) {
197+ dataBuffer[i] = get<uint64_t >(variableInfo.variableLocation_ .getElement (i));
198+ }
199+ variableInfo.hdf5DataSet_ .write (dataBuffer.data (), variableInfo.hdf5Datatype_ ,
200+ memSpace, fileSpace);
201+ } else {
202+ // Throw an exception if the data type is unsupported
203+ throw std::runtime_error (" Unsupported data type" );
204+ }
205+ }
206+ }
207+ }
208+
209+ // Call startNewEpoch() to prepare for new data input
210+ variableInfo.variableLocation_ .startNewEpoch ();
211+ }
212+ }
213+
125214// / Receives a recorded variable entity from the variable owner class
126215// / used when the return type from recordable variable is supported by Recorder
127216/* *
0 commit comments