kspaceFirstOrder3D-OMP  1.2
The C++ implementation of the k-wave toolbox for the time-domain simulation of acoustic wave fields in 3D
BaseOutputStream.cpp
Go to the documentation of this file.
1 /**
2  * @file BaseOutputStream.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 HDF5 file.
10  *
11  * @version kspaceFirstOrder3D 2.16
12  *
13  * @date 11 July 2012, 10:30 (created) \n
14  * 04 September 2017, 11:10 (revised)
15  *
16  * @copyright Copyright (C) 2017 Jiri Jaros and Bradley Treeby.
17  *
18  * This file is part of the C++ extension of the [k-Wave Toolbox](http://www.k-wave.org).
19  *
20  * This file is part of the k-Wave. k-Wave is free software: you can redistribute it and/or modify it under the terms
21  * of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the
22  * License, or (at your option) any later version.
23  *
24  * k-Wave is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
25  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
26  * more details.
27  *
28  * You should have received a copy of the GNU Lesser General Public License along with k-Wave.
29  * If not, see [http://www.gnu.org/licenses/](http://www.gnu.org/licenses/).
30  */
31 
32 
33 #include <cmath>
34 #include <immintrin.h>
35 #include <limits>
36 
38 #include <Parameters/Parameters.h>
39 
40 
41 //--------------------------------------------------------------------------------------------------------------------//
42 //---------------------------------------------------- Constants -----------------------------------------------------//
43 //--------------------------------------------------------------------------------------------------------------------//
44 
45 //--------------------------------------------------------------------------------------------------------------------//
46 //------------------------------------------------- Public methods ---------------------------------------------------//
47 //--------------------------------------------------------------------------------------------------------------------//
48 
49 /**
50  * Constructor - there is no sensor mask by default!
51  */
53  MatrixName& rootObjectName,
54  const RealMatrix& sourceMatrix,
55  const ReduceOperator reduceOp,
56  float* bufferToReuse)
57  : mFile(file),
58  mRootObjectName(rootObjectName),
59  mSourceMatrix(sourceMatrix),
60  mReduceOp(reduceOp),
61  mBufferReuse(bufferToReuse != nullptr),
62  mBufferSize(0),
63  mStoreBuffer(bufferToReuse)
64 {
65 
66 };
67 //----------------------------------------------------------------------------------------------------------------------
68 
69 /**
70  * Apply post-processing on the buffer. It supposes the elements are independent.
71  */
73 {
74  switch (mReduceOp)
75  {
77  {
78  // do nothing
79  break;
80  }
82  {
83  const float scalingCoeff = 1.0f / (Parameters::getInstance().getNt() -
85 
86  #pragma omp parallel for simd
87  for (size_t i = 0; i < mBufferSize; i++)
88  {
89  mStoreBuffer[i] = sqrt(mStoreBuffer[i] * scalingCoeff);
90  }
91  break;
92  }
94  {
95  // do nothing
96  break;
97  }
99  {
100  // do nothing
101  break;
102  }
103  }// switch
104 
105 }// end of postProcess
106 //----------------------------------------------------------------------------------------------------------------------
107 
108 
109 
110 //--------------------------------------------------------------------------------------------------------------------//
111 //------------------------------------------------ Protected methods -------------------------------------------------//
112 //--------------------------------------------------------------------------------------------------------------------//
113 
114 /**
115  * Allocate memory using a proper memory alignment.
116  */
118 {
119  mStoreBuffer = (float*) _mm_malloc(mBufferSize * sizeof(float), kDataAlignment);
120 
121  if (!mStoreBuffer)
122  {
123  throw std::bad_alloc();
124  }
125 
126  // we need different initialization for different reduction ops
127  switch (mReduceOp)
128  {
130  {
131  // zero the matrix
132  #pragma omp parallel for simd
133  for (size_t i = 0; i < mBufferSize; i++)
134  {
135  mStoreBuffer[i] = 0.0f;
136  }
137  break;
138  }
139 
141  {
142  // zero the matrix
143  #pragma omp parallel for simd
144  for (size_t i = 0; i < mBufferSize; i++)
145  {
146  mStoreBuffer[i] = 0.0f;
147  }
148  break;
149  }
150 
152  {
153  // set the values to the highest negative float value
154  #pragma omp parallel for simd
155  for (size_t i = 0; i < mBufferSize; i++)
156  {
157  mStoreBuffer[i] = -1.0f * std::numeric_limits<float>::max();
158  }
159  break;
160  }
161 
163  {
164  // set the values to the highest float value
165  #pragma omp parallel for simd
166  for (size_t i = 0; i < mBufferSize; i++)
167  {
168  mStoreBuffer[i] = std::numeric_limits<float>::max();
169  }
170  break;
171  }
172  }// switch
173 }// end of allocateMemory
174 //----------------------------------------------------------------------------------------------------------------------
175 
176 /**
177  * Free memory.
178  */
180 {
181  if (mStoreBuffer)
182  {
183  _mm_free(mStoreBuffer);
184  mStoreBuffer = nullptr;
185  }
186 }// end of freeMemory
187 //----------------------------------------------------------------------------------------------------------------------
188 
189 //--------------------------------------------------------------------------------------------------------------------//
190 //------------------------------------------------- Private methods --------------------------------------------------//
191 //--------------------------------------------------------------------------------------------------------------------//
Calculate root mean square.
The class for real matrices.
Definition: RealMatrix.h:47
virtual void freeMemory()
Free memory.
size_t getNt() const
Get total number of time steps.
Definition: Parameters.h:203
const ReduceOperator mReduceOp
Reduction operator.
virtual void allocateMemory()
Allocate memory using proper memory alignment.
virtual void postProcess()
Apply post-processing on the buffer and flush it to the file.
Store actual data (time series).
The header file containing the parameters of the simulation.
constexpr int kDataAlignment
memory alignment for SSE, SSE2, SSE3, SSE4 (16B)
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
size_t mBufferSize
Buffer size.
const std::string MatrixName
Datatype for matrix names.
Definition: MatrixNames.h:39
float * mStoreBuffer
Temporary buffer for store - only if Buffer Reuse = false!
ReduceOperator
How to aggregate data.
The header file of the class saving RealMatrix data into the output HDF5 file.
static Parameters & getInstance()
Get instance of the singleton class.
Definition: Parameters.cpp:84
BaseOutputStream()=delete
Default constructor not allowed.