kspaceFirstOrder3D-OMP  1.2
The C++ implementation of the k-wave toolbox for the time-domain simulation of acoustic wave fields in 3D
WholeDomainOutputStream.cpp
Go to the documentation of this file.
1 /**
2  * @file WholeDomainOutputStream.cpp
3  *
4  * @author Jiri Jaros \n
5  * Faculty of Information Technology \n
6  * Brno University of Technology \n
7  * jarosjir@fit.vutbr.cz
8  *
9  * @brief The implementation file of the class saving RealMatrix data into the output
10  * HDF5 file, e.g. p_max_all.
11  *
12  * @version kspaceFirstOrder3D 2.16
13  *
14  * @date 26 August 2017, 17:03 (created) \n
15  * 04 September 2017, 11:10 (revised)
16  *
17  * @copyright Copyright (C) 2017 Jiri Jaros and Bradley Treeby.
18  *
19  * This file is part of the C++ extension of the [k-Wave Toolbox](http://www.k-wave.org).
20  *
21  * This file is part of the k-Wave. k-Wave is free software: you can redistribute it and/or modify it under the terms
22  * of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the
23  * License, or (at your option) any later version.
24  *
25  * k-Wave is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
26  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
27  * more details.
28  *
29  * You should have received a copy of the GNU Lesser General Public License along with k-Wave.
30  * If not, see [http://www.gnu.org/licenses/](http://www.gnu.org/licenses/).
31  */
32 
33 #include <algorithm>
34 
36 #include <Parameters/Parameters.h>
37 
38 //--------------------------------------------------------------------------------------------------------------------//
39 //--------------------------------------------------- Constants ------------------------------------------------------//
40 //--------------------------------------------------------------------------------------------------------------------//
41 
42 //--------------------------------------------------------------------------------------------------------------------//
43 //------------------------------------------------- Public methods ---------------------------------------------------//
44 //--------------------------------------------------------------------------------------------------------------------//
45 
46 
47 /**
48  * Constructor - links the HDF5 dataset and SourceMatrix.
49  * @param [in] file - HDF5 file to write the output to
50  * @param [in] datasetName - The name of the HDF5 group. This group contains datasets for particular cuboids
51  * @param [in] sourceMatrix - Source matrix to be sampled
52  * @param [in] reductionOp - Reduction operator
53  * @param [in] bufferToReuse - If there is a memory space to be reused, provide a pointer
54  */
56  MatrixName& datasetName,
57  const RealMatrix& sourceMatrix,
58  const ReduceOperator reductionOp,
59  float* bufferToReuse)
60  : BaseOutputStream(file, datasetName, sourceMatrix, reductionOp, bufferToReuse),
61  mDataset(H5I_BADID),
62  mSampledTimeStep(0)
63 {
64 
65 }// end of WholeDomainOutputStream
66 //----------------------------------------------------------------------------------------------------------------------
67 
68 /**
69  * Destructor.
70  */
72 {
73  close();
74  // free memory only if it was allocated
75  if (!mBufferReuse) freeMemory();
76 }// end of Destructor
77 //----------------------------------------------------------------------------------------------------------------------
78 
79 /**
80  * Create a HDF5 stream for the whole domain and allocate data for it.
81  */
83 {
85 
86  // Create a dataset under the root group
90  chunkSize,
93 
94  // Write dataset parameters
101 
102  // Set buffer size
104 
105  // Allocate memory if needed
107 }//end of create
108 //----------------------------------------------------------------------------------------------------------------------
109 
110 /**
111  * Reopen the output stream after restart and reload data.
112  */
114 {
115  const Parameters& params = Parameters::getInstance();
116 
117  // Set buffer size
119 
120  // Allocate memory if needed
122 
123  // Open the dataset under the root group
125 
126  mSampledTimeStep = 0;
128  { // seek in the dataset
130  0 : (params.getTimeIndex() - params.getSamplingStartTimeIndex());
131  }
132  else
133  { // reload data
134  if (params.getTimeIndex() > params.getSamplingStartTimeIndex())
135  {
139  mStoreBuffer);
140  }
141  }
142 }// end of reopen
143 //----------------------------------------------------------------------------------------------------------------------
144 
145 /**
146  * Sample all grid points, line them up in the buffer an flush to the disk unless a reduction operator is applied.
147  */
149 {
150  const float* sourceData = mSourceMatrix.getData();
151 
152  switch (mReduceOp)
153  {
154  case ReduceOperator::kNone :
155  {
156  // We sample it as a single cuboid of full dimensions.
157  /* We use here direct HDF5 offload using MEMSPACE - seems to be faster for bigger datasets*/
158  const DimensionSizes datasetPosition(0, 0, 0, mSampledTimeStep); //4D position in the dataset
159 
160  DimensionSizes cuboidSize(mSourceMatrix.getDimensionSizes());// Size of the cuboid
161  cuboidSize.nt = 1;
162 
163  // iterate over all cuboid to be sampled
165  datasetPosition,
166  DimensionSizes(0, 0, 0, 0), // position in the SourceMatrix
167  cuboidSize,
170 
171  mSampledTimeStep++; // Move forward in time
172 
173  break;
174  }// case kNone
175 
177  {
178  #pragma omp parallel for simd
179  for (size_t i = 0; i < mBufferSize; i++)
180  {
181  mStoreBuffer[i] += (sourceData[i] * sourceData[i]);
182  }
183  break;
184  }// case kRms
185 
187  {
188  #pragma omp parallel for simd
189  for (size_t i = 0; i < mBufferSize; i++)
190  {
191  mStoreBuffer[i] = std::max(mStoreBuffer[i], sourceData[i]);
192  }
193  break;
194  }//case roMAX
195 
197  {
198  #pragma omp parallel for simd
199  for (size_t i = 0; i < mBufferSize; i++)
200  {
201  mStoreBuffer[i] = std::min(mStoreBuffer[i], sourceData[i]);
202  }
203  break;
204  } //case kMin
205  }// switch
206 }// end of sample
207 //----------------------------------------------------------------------------------------------------------------------
208 
209 /**
210  * Apply post-processing on the buffer and flush it to the file.
211  */
213 {
214  // run inherited method
216  // When no reduction operator is applied, the data is flushed after every time step
218 }// end of postProcessing
219 //----------------------------------------------------------------------------------------------------------------------
220 
221 /**
222  * Checkpoint the stream
223  */
225 {
226  // raw data has already been flushed, others has to be flushed here.
228 }// end of checkpoint
229 //----------------------------------------------------------------------------------------------------------------------
230 
231 /**
232  * Close stream (apply post-processing if necessary, flush data and close).
233  */
235 {
236  // the dataset is still opened
237  if (mDataset != H5I_BADID)
238  {
240  }
241 
242  mDataset = H5I_BADID;
243 }// end of close
244 //----------------------------------------------------------------------------------------------------------------------
245 
246 //--------------------------------------------------------------------------------------------------------------------//
247 //------------------------------------------------ Protected methods -------------------------------------------------//
248 //--------------------------------------------------------------------------------------------------------------------//
249 
250 
251 /**
252  * Flush the buffer down to the file at the actual position.
253  */
255 {
257  DimensionSizes position(0, 0, 0);
258 
259  // Not used for roNONE now!
261  {
262  position.nt = mSampledTimeStep;
263  size.nt = mSampledTimeStep;
264  }
265 
266  mFile.writeHyperSlab(mDataset, position, size, mStoreBuffer);
268 }// end of flushToFile
269 //----------------------------------------------------------------------------------------------------------------------
270 
271 //--------------------------------------------------------------------------------------------------------------------//
272 //------------------------------------------------- Private methods --------------------------------------------------//
273 //--------------------------------------------------------------------------------------------------------------------//
virtual void create()
Create a HDF5 stream and allocate data for it.
hid_t openDataset(const hid_t parentGroup, MatrixName &datasetName)
Open a dataset at a specified place in the file tree.
Definition: Hdf5File.cpp:223
Calculate root mean square.
The class for real matrices.
Definition: RealMatrix.h:47
virtual void freeMemory()
Free memory.
WholeDomainOutputStream()=delete
Default constructor not allowed.
size_t getCompressionLevel() const
Get compression level.
Definition: Parameters.h:127
std::string mRootObjectName
HDF5 group/dataset in the output file where to store data in.
hid_t getRootGroup() const
Get handle to the root group of the file.
Definition: Hdf5File.h:608
const ReduceOperator mReduceOp
Reduction operator.
void writeCuboidToHyperSlab(const hid_t dataset, const DimensionSizes &hyperslabPosition, const DimensionSizes &cuboidPosition, const DimensionSizes &cuboidSize, const DimensionSizes &matrixDimensions, const float *matrixData)
Write a cuboid selected within the matrixData into a hyperslab.
Definition: Hdf5File.cpp:431
hid_t mDataset
Handle to a HDF5 dataset.
Class storing all parameters of the simulation.
Definition: Parameters.h:50
virtual void allocateMemory()
Allocate memory using proper memory alignment.
size_t mSampledTimeStep
Time step to store (N/A for aggregated).
virtual void postProcess()
Apply post-processing on the buffer and flush it to the file.
Store actual data (time series).
virtual size_t size() const
Size of the matrix.
virtual void flushBufferToFile()
Flush the buffer to the file.
The header file containing the parameters of the simulation.
void writeMatrixDomainType(const hid_t parentGroup, MatrixName &datasetName, const MatrixDomainType &matrixDomainType)
Write matrix data type into the dataset at a specified place in the file tree.
Definition: Hdf5File.cpp:807
void closeDataset(const hid_t dataset)
Close dataset.
Definition: Hdf5File.cpp:325
size_t nt
Number of time steps (for time series datasets).
void writeMatrixDataType(const hid_t parentGroup, MatrixName &datasetName, const MatrixDataType &matrixDataType)
Write matrix data type into the dataset at a specified place in the file tree.
Definition: Hdf5File.cpp:793
size_t getSamplingStartTimeIndex() const
Get start time index when sensor data collection begins.
Definition: Parameters.h:499
Class wrapping the HDF5 routines.
Definition: Hdf5File.h:490
virtual void checkpoint()
Checkpoint the stream and close.
size_t mBufferSize
Buffer size.
void writeHyperSlab(const hid_t dataset, const DimensionSizes &position, const DimensionSizes &size, const T *data)
Write a hyperslab into the dataset.
Definition: Hdf5File.cpp:335
const std::string MatrixName
Datatype for matrix names.
Definition: MatrixNames.h:39
Structure with 4D dimension sizes (3 in space and 1 in time).
The matrix is stored in floating point 32b wide format.
Hdf5File & mFile
Handle to HDF5 output file.
virtual void close()
Close stream (apply post-processing if necessary, flush data and close).
size_t ny
Number of elements in the y direction.
size_t getTimeIndex() const
Get actual simulation time step.
Definition: Parameters.h:208
const RealMatrix & mSourceMatrix
Source matrix to be sampled.
float * mStoreBuffer
Temporary buffer for store - only if Buffer Reuse = false!
virtual void postProcess()
Apply post-processing on the buffer and flush it to the file.
ReduceOperator
How to aggregate data.
virtual float * getData()
Get raw data out of the class (for direct kernel access).
void readCompleteDataset(const hid_t parentGroup, MatrixName &datasetName, const DimensionSizes &dimensionSizes, T *data)
Read data from the dataset at a specified place in the file tree.
Definition: Hdf5File.cpp:678
virtual ~WholeDomainOutputStream()
Destructor.
size_t nx
Number of elements in the x direction.
static Parameters & getInstance()
Get instance of the singleton class.
Definition: Parameters.cpp:84
virtual void reopen()
Reopen the output stream after restart and reload data.
virtual const DimensionSizes & getDimensionSizes() const
Get dimension sizes of the matrix.
The header file of the class saving whole RealMatrix into the output HDF5 file, e.g. p_max_all.
hid_t createDataset(const hid_t parentGroup, MatrixName &datasetName, const DimensionSizes &dimensionSizes, const DimensionSizes &chunkSizes, const MatrixDataType matrixDataType, const size_t compressionLevel)
Create a float HDF5 dataset at a specified place in the file tree (3D/4D).
Definition: Hdf5File.cpp:241
Abstract base class for output data streams (sampled data).
virtual void sample()
Sample data into buffer and apply reduction, or flush to disk (no sensor mask here).
The matrix is defined on real domain.
bool mBufferReuse
if true, the container reuses another matrix as scratch place, e.g. Temp_1_RS3D, Temp_2_RS3D, Temp_3_RS3D.