kspaceFirstOrder3D-CUDA  1.1
The CUDA/C++ implementation of the k-wave toolbox for the time-domain simulation of acoustic wave fields in 3D
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OutputStreamContainer.cpp
Go to the documentation of this file.
1  /**
2  * @file OutputStreamContainer.cpp
3  *
4  * @author Jiri Jaros & Beau Johnston \n
5  * Faculty of Information Technology \n
6  * Brno University of Technology \n
7  * jarosjir@fit.vutbr.cz
8  *
9  * @brief The implementation file for the output stream container.
10  *
11  * @version kspaceFirstOrder3D 3.4
12  *
13  * @date 04 December 2014, 11:41 (created) \n
14  * 19 July 2016, 17:28 (revised)
15  *
16  * @section License
17  * This file is part of the C++ extension of the k-Wave Toolbox
18  * (http://www.k-wave.org).\n Copyright (C) 2016 Jiri Jaros and Bradley Treeby.
19  *
20  * This file is part of the k-Wave. k-Wave is free software: you can redistribute it and/or modify
21  * it under the terms of the GNU Lesser General Public License as published by the Free Software
22  * Foundation, either version 3 of the 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
25  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
26  * General Public License for 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/. If not, see http://www.gnu.org/licenses/.
30  */
31 
32 #include <Parameters/Parameters.h>
34 
39 
40 
41 //------------------------------------------------------------------------------------------------//
42 //-------------------------------------- Constants -----------------------------------------------//
43 //------------------------------------------------------------------------------------------------//
44 
45 
46 //------------------------------------------------------------------------------------------------//
47 //--------------------------------------- Public methods -----------------------------------------//
48 //------------------------------------------------------------------------------------------------//
49 
50 /**
51  * Destructor.
52  */
54 {
55  outputStreamContainer.clear();
56 }// end of Destructor
57 //--------------------------------------------------------------------------------------------------
58 
59 /**
60  * Add all streams in the simulation to the container, set all streams records here! \n
61  * Please note, the Matrix container has to be populated before calling this routine.
62  *
63  * @param [in] matrixContainer - Matrix container to link the steams with sampled matrices and
64  * sensor masks
65  */
67 {
69 
70  //----------------------------------------- pressure ------------------------------------------//
71  if (params.IsStore_p_raw())
72  {
73  outputStreamContainer[p_sensor_raw] = CreateNewOutputStream(matrixContainer,
74  p,
75  p_NAME,
77  }
78 
79  if (params.IsStore_p_rms())
80  {
81  outputStreamContainer[p_sensor_rms] = CreateNewOutputStream(matrixContainer,
82  p,
83  p_rms_NAME,
85  }
86 
87  if (params.IsStore_p_max())
88  {
89  outputStreamContainer[p_sensor_max] = CreateNewOutputStream(matrixContainer,
90  p,
91  p_max_NAME,
93  }
94 
95  if (params.IsStore_p_min())
96  {
97  outputStreamContainer[p_sensor_min] = CreateNewOutputStream(matrixContainer,
98  p,
99  p_min_NAME,
101  }
102 
103  if (params.IsStore_p_max_all())
104  {
105  outputStreamContainer[p_sensor_max_all] =
108  matrixContainer.GetMatrix<TRealMatrix>(p),
110  }
111 
112  if (params.IsStore_p_min_all())
113  {
114  outputStreamContainer[p_sensor_min_all] =
117  matrixContainer.GetMatrix<TRealMatrix>(p),
119  }
120 
121  //---------------------------------------- velocity --------------------------------------------//
122  if (params.IsStore_u_raw())
123  {
124  outputStreamContainer[ux_sensor_raw] = CreateNewOutputStream(matrixContainer,
125  ux_sgx,
126  ux_NAME,
128  outputStreamContainer[uy_sensor_raw] = CreateNewOutputStream(matrixContainer,
129  uy_sgy,
130  uy_NAME,
132  outputStreamContainer[uz_sensor_raw] = CreateNewOutputStream(matrixContainer,
133  uz_sgz,
134  uz_NAME,
136  }
137 
138  if (params.IsStore_u_non_staggered_raw())
139  {
140  outputStreamContainer[ux_shifted_sensor_raw] = CreateNewOutputStream(matrixContainer,
141  ux_shifted,
144  outputStreamContainer[uy_shifted_sensor_raw] = CreateNewOutputStream(matrixContainer,
145  uy_shifted,
148  outputStreamContainer[uz_shifted_sensor_raw] = CreateNewOutputStream(matrixContainer,
149  uz_shifted,
152  }
153 
154  if (params.IsStore_u_rms())
155  {
156  outputStreamContainer[ux_sensor_rms] = CreateNewOutputStream(matrixContainer,
157  ux_sgx,
158  ux_rms_NAME,
160  outputStreamContainer[uy_sensor_rms] = CreateNewOutputStream(matrixContainer,
161  uy_sgy,
162  uy_rms_NAME,
164  outputStreamContainer[uz_sensor_rms] = CreateNewOutputStream(matrixContainer,
165  uz_sgz,
166  uz_rms_NAME,
168  }
169 
170  if (params.IsStore_u_max())
171  {
172  outputStreamContainer[ux_sensor_max] = CreateNewOutputStream(matrixContainer,
173  ux_sgx,
174  ux_max_NAME,
176  outputStreamContainer[uy_sensor_max] = CreateNewOutputStream(matrixContainer,
177  uy_sgy,
178  uy_max_NAME,
180  outputStreamContainer[uz_sensor_max] = CreateNewOutputStream(matrixContainer,
181  uz_sgz,
182  uz_max_NAME,
184  }
185 
186  if (params.IsStore_u_min())
187  {
188  outputStreamContainer[ux_sensor_min] = CreateNewOutputStream(matrixContainer,
189  ux_sgx,
190  ux_min_NAME,
192  outputStreamContainer[uy_sensor_min] = CreateNewOutputStream(matrixContainer,
193  uy_sgy,
194  uy_min_NAME,
196  outputStreamContainer[uz_sensor_min] = CreateNewOutputStream(matrixContainer,
197  uz_sgz,
198  uz_min_NAME,
200  }
201 
202  if (params.IsStore_u_max_all())
203  {
204  outputStreamContainer[ux_sensor_max_all] =
207  matrixContainer.GetMatrix<TRealMatrix>(ux_sgx),
209  outputStreamContainer[uy_sensor_max_all] =
212  matrixContainer.GetMatrix<TRealMatrix>(uy_sgy),
214  outputStreamContainer[uz_sensor_max_all] =
217  matrixContainer.GetMatrix<TRealMatrix>(uz_sgz),
219  }
220 
221  if (params.IsStore_u_min_all())
222  {
223  outputStreamContainer[ux_sensor_min_all] =
226  matrixContainer.GetMatrix<TRealMatrix>(ux_sgx),
228  outputStreamContainer[uy_sensor_min_all] =
231  matrixContainer.GetMatrix<TRealMatrix>(uy_sgy),
233  outputStreamContainer[uz_sensor_min_all] =
236  matrixContainer.GetMatrix<TRealMatrix>(uz_sgz),
238  }
239 }// end of AddStreams
240 //--------------------------------------------------------------------------------------------------
241 
242 /**
243  * Create all streams.
244  */
246 {
247  for (const auto& it : outputStreamContainer)
248  {
249  if (it.second)
250  {
251  it.second->Create();
252  }
253  }
254 }// end of CreateStreams
255 //--------------------------------------------------------------------------------------------------
256 
257 /**
258  * Reopen all streams after restarting from checkpoint.
259  */
261 {
262  for (const auto& it : outputStreamContainer)
263  {
264  if (it.second)
265  {
266  it.second->Reopen();
267  }
268  }
269 }// end of ReopenStreams
270 //--------------------------------------------------------------------------------------------------
271 
272 
273 /**
274  * Sample all streams.
275  * @warning In the GPU implementation, no data is flushed on disk (just data is sampled)
276  */
278 {
279  for (const auto& it : outputStreamContainer)
280  {
281  if (it.second)
282  {
283  it.second->Sample();
284  }
285  }
286 }// end of SampleStreams
287 //--------------------------------------------------------------------------------------------------
288 
289 /**
290  * Flush stream data to disk.
291  * @warning In GPU implementation, data from raw streams is flushed here. Aggregated streams are
292  * ignored.
293  */
295 {
296  for (const auto& it : outputStreamContainer)
297  {
298  if (it.second)
299  {
300  it.second->FlushRaw();
301  }
302  }
303 }// end of SampleStreams
304 //--------------------------------------------------------------------------------------------------
305 
306 /**
307  * Checkpoint streams without post-processing (flush to the file).
308  */
310 {
311  for (const auto& it : outputStreamContainer)
312  {
313  if (it.second)
314  {
315  it.second->Checkpoint();
316  }
317  }
318 }// end of CheckpointStreams
319 //--------------------------------------------------------------------------------------------------
320 
321 /**
322  * Post-process all streams and flush them to the file.
323  */
325 {
326  for (const auto& it : outputStreamContainer)
327  {
328  if (it.second)
329  {
330  it.second->PostProcess();
331  }
332  }
333 }// end of CheckpointStreams
334 //--------------------------------------------------------------------------------------------------
335 
336 
337 /**
338  * Close all streams (apply post-processing if necessary, flush data and close).
339  */
341 {
342  for (const auto& it : outputStreamContainer)
343  {
344  if (it.second)
345  {
346  it.second->Close();
347  }
348  }
349 }// end of CloseStreams
350 //--------------------------------------------------------------------------------------------------
351 
352 /**
353  * Free all streams - destroy them.
354  */
356 {
357  for (auto& it : outputStreamContainer)
358  {
359  if (it.second)
360  {
361  delete it.second;
362  it.second = nullptr;
363  }
364  }
365  outputStreamContainer.clear();
366 }// end of FreeAllStreams
367 //--------------------------------------------------------------------------------------------------
368 
369 
370 //------------------------------------------------------------------------------------------------//
371 //-------------------------------------- Protected methods ---------------------------------------//
372 //------------------------------------------------------------------------------------------------//
373 
374 
375 /**
376  * Create a new output stream.
377  * @param [in] matrixContainer - Name of the HDF5 dataset or group
378  * @param [in] sampledMatrixIdx - Code id of the matrix
379  * @param [in] fileDatasetName - Name of the HDF5 dataset or group
380  * @param [in] reduceOp - Reduce operator
381  *
382  * @return New output stream with defined links
383  *
384  */
386  const TMatrixIdx sampledMatrixIdx,
387  const TMatrixName& fileDatasetName,
389 {
391 
392 
393 
394  if (params.Get_sensor_mask_type() == TParameters::INDEX)
395  {
396  return (new TIndexOutputHDF5Stream(params.GetOutputFile(),
397  fileDatasetName,
398  matrixContainer.GetMatrix<TRealMatrix>(sampledMatrixIdx),
399  matrixContainer.GetMatrix<TIndexMatrix>(sensor_mask_index),
400  reduceOp)
401  );
402  }
403  else
404  {
405  return (new TCuboidOutputHDF5Stream(params.GetOutputFile(),
406  fileDatasetName,
407  matrixContainer.GetMatrix<TRealMatrix>(sampledMatrixIdx),
408  matrixContainer.GetMatrix<TIndexMatrix>(sensor_mask_corners),
409  reduceOp)
410  );
411  }
412 }// end of CreateNewOutputStream
413 //--------------------------------------------------------------------------------------------------
414 
415 
416 //------------------------------------------------------------------------------------------------//
417 //--------------------------------------- Private methods ----------------------------------------//
418 //------------------------------------------------------------------------------------------------//
TMatrixName uz_max_NAME
uz_max variable name
Definition: MatrixNames.h:293
Output stream for quantities sampled in the whole domain.
TMatrixName ux_max_all_NAME
ux_max_all variable name
Definition: MatrixNames.h:302
TMatrixName uz_min_all_NAME
uz_min_all variable name
Definition: MatrixNames.h:312
The header file of the class saving RealMatrix data into the output HDF5 file.
Output stream for quantities sampled by a cuboid corner sensor mask.
TMatrixName uy_rms_NAME
uy_rms variable name
Definition: MatrixNames.h:284
TMatrixName p_rms_NAME
p_rms variable name
Definition: MatrixNames.h:269
bool IsStore_u_min() const
Is –u_min specified at the command line?
Definition: Parameters.h:258
calculate root mean square
static TParameters & GetInstance()
Get instance of the singleton class.
Definition: Parameters.cpp:70
bool IsStore_u_raw() const
Is -u or –u_raw specified at the command line?
Definition: Parameters.h:250
TMatrixName p_max_NAME
p_max variable name
Definition: MatrixNames.h:271
void FreeStreams()
Free all streams - destroy them.
T & GetMatrix(const TMatrixIdx matrixIdx)
Get the matrix with a specific type from the container.
The header file containing the parameters of the simulation.
void AddStreams(TMatrixContainer &matrixContainer)
Add all streams into the container.
const std::string TMatrixName
Datatype for matrix names.
Definition: MatrixNames.h:45
TBaseOutputHDF5Stream * CreateNewOutputStream(TMatrixContainer &matrixContainer, const TMatrixIdx sampledMatrixIdx, const TMatrixName &fileDatasetName, const TBaseOutputHDF5Stream::TReduceOperator reduceOp)
Create a new output stream.
TMatrixName uy_NAME
uy variable name
Definition: MatrixNames.h:197
bool IsStore_u_rms() const
Is –u_raw specified at the command line?
Definition: Parameters.h:254
TMatrixName uy_min_all_NAME
uy_min_all variable name
Definition: MatrixNames.h:310
bool IsStore_p_raw() const
Is -p or –p_raw specified at the command line?
Definition: Parameters.h:235
bool IsStore_p_rms() const
Is –p_rms specified at the command line?
Definition: Parameters.h:237
bool IsStore_p_min_all() const
Is –p_min_all specified at the command line?
Definition: Parameters.h:245
bool IsStore_p_min() const
Is –p_min specified at the command line?
Definition: Parameters.h:241
Output stream for quantities sampled by an index sensor mask.
bool IsStore_u_non_staggered_raw() const
Is –u_non_staggered_raw set?
Definition: Parameters.h:252
bool IsStore_u_min_all() const
Is –u_min_all specified at the command line?
Definition: Parameters.h:262
Class storing all parameters of the simulation.
Definition: Parameters.h:49
TMatrixName uz_max_all_NAME
uz_max_all variable name
Definition: MatrixNames.h:306
TMatrixName uz_NAME
uz variable name
Definition: MatrixNames.h:199
The header file of the class saving whole RealMatrix into the output HDF5 file, e.g. p_max_all.
void SampleStreams()
Sample all streams (only sample, no disk operations).
TReduceOperator
How to aggregate data.
TMatrixName uz_rms_NAME
uz_rms variable name
Definition: MatrixNames.h:286
TMatrixName ux_min_all_NAME
ux_min_all variable name
Definition: MatrixNames.h:308
void ReopenStreams()
Reopen streams after checkpoint file (datasets).
store actual data (time series)
TMatrixIdx
Matrix identifers of all matrices in the k-space code, names based on the Matlab notation.
Abstract base class for output data streams (sampled data).
Class implementing the matrix container.
void FlushRawStreams()
Flush streams to disk - only raw streams.
TMatrixName uy_max_all_NAME
uy_max_all variable name
Definition: MatrixNames.h:304
The class for real matrices.
Definition: RealMatrix.h:45
TOutputStreamMap outputStreamContainer
Map with output streams.
TMatrixName uy_max_NAME
uy_max variable name
Definition: MatrixNames.h:291
bool IsStore_p_max_all() const
Is –p_max_all specified at the command line?
Definition: Parameters.h:243
void CheckpointStreams()
Checkpoint streams.
The class for 64b unsigned integers (indices). It is used for sensor_mask_index or sensor_corners_mas...
Definition: IndexMatrix.h:46
The header file of the class saving data based on the index senor mask into the output HDF5 file...
TMatrixName ux_min_NAME
ux_min variable name
Definition: MatrixNames.h:295
TMatrixName ux_NAME
ux variable name
Definition: MatrixNames.h:195
bool IsStore_u_max() const
Is –u_max specified at the command line?
Definition: Parameters.h:256
bool IsStore_u_max_all() const
Is –u_max_all specified at the command line?
Definition: Parameters.h:260
bool IsStore_p_max() const
Is –p_max specified at the command line?
Definition: Parameters.h:239
TMatrixName p_min_NAME
p_min variable name
Definition: MatrixNames.h:273
The header file of classes responsible for storing output quantities based on the cuboid sensor mask ...
The header file defining the output stream container.
void CreateStreams()
Create all streams - opens the datasets.
TMatrixName ux_rms_NAME
ux_rms variable name
Definition: MatrixNames.h:282
TMatrixName uz_min_NAME
uz_min variable name
Definition: MatrixNames.h:299
TMatrixName uz_non_staggered_NAME
uz_non_staggered variable name
Definition: MatrixNames.h:213
TMatrixName p_min_all_NAME
p_min_all variable name
Definition: MatrixNames.h:277
TMatrixName uy_min_NAME
uy_min variable name
Definition: MatrixNames.h:297
void CloseStreams()
Close all streams.
TMatrixName p_max_all_NAME
p_max_all variable name
Definition: MatrixNames.h:275
TMatrixName ux_max_NAME
ux_max variable name
Definition: MatrixNames.h:289
TMatrixName uy_non_staggered_NAME
uy_non_staggered variable name
Definition: MatrixNames.h:211
TSensorMaskType Get_sensor_mask_type() const
Get sensor mask type (linear or corners).
Definition: Parameters.h:166
TMatrixName p_NAME
p variable name
Definition: MatrixNames.h:186
THDF5_File & GetOutputFile()
Get output file handle.
Definition: Parameters.h:83
TMatrixName ux_non_staggered_NAME
ux_non_staggered variable name
Definition: MatrixNames.h:209
void PostProcessStreams()
Post-process all streams and flush them to the file.