kspaceFirstOrder3D-OMP  1.2
The C++ implementation of the k-wave toolbox for the time-domain simulation of acoustic wave fields in 3D
RealMatrix.cpp
Go to the documentation of this file.
1 /**
2  * @file RealMatrix.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 containing the class for real matrices.
10  *
11  * @version kspaceFirstOrder3D 2.16
12  *
13  * @date 11 July 2011, 10:30 (created) \n
14  * 04 September 2017, 11:02 (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 
35 #include <Logger/Logger.h>
36 
37 
38 using std::ios;
39 //--------------------------------------------------------------------------------------------------------------------//
40 //---------------------------------------------------- Constants -----------------------------------------------------//
41 //--------------------------------------------------------------------------------------------------------------------//
42 
43 
44 //--------------------------------------------------------------------------------------------------------------------//
45 //------------------------------------------------- Public methods ---------------------------------------------------//
46 //--------------------------------------------------------------------------------------------------------------------//
47 
48 
49 /**
50  * Constructor.
51  */
52 RealMatrix::RealMatrix(const DimensionSizes& dimensionSizes)
53  : BaseFloatMatrix()
54 {
55  initDimensions(dimensionSizes);
57 }// end of RealMatrix
58 //----------------------------------------------------------------------------------------------------------------------
59 
60 /**
61  * Destructor.
62  */
64 {
65  freeMemory();
66 }// end of ~RealMatrix
67 //----------------------------------------------------------------------------------------------------------------------
68 
69 /**
70  * Read data data from HDF5 file (only from the root group).
71  */
73  MatrixName& matrixName)
74 {
75  // test matrix datatype
77  {
78  throw std::ios::failure(Logger::formatMessage(kErrFmtMatrixNotFloat, matrixName.c_str()));
79  }
80 
82  {
83  throw std::ios::failure(Logger::formatMessage(kErrFmtMatrixNotReal, matrixName.c_str()));
84  }
85 
86  // Read matrix
87  file.readCompleteDataset(file.getRootGroup(), matrixName, mDimensionSizes, mData);
88 }// end of readData
89 //----------------------------------------------------------------------------------------------------------------------
90 
91 /**
92  * Write data to HDF5 file (only from the root group)
93  */
95  MatrixName& matrixName,
96  const size_t compressionLevel)
97 {
99  chunks.nz = 1;
100 
101  //1D matrices
102  if ((mDimensionSizes.ny == 1) && (mDimensionSizes.nz == 1))
103  {
104  // Chunk = 4MB
106  {
107  chunks.nx = kChunkSize1D4MB;
108  }
109  else if (mDimensionSizes.nx > 4 * kChunkSize1D1MB)
110  {
111  chunks.nx = kChunkSize1D1MB;
112  }
113  else if (mDimensionSizes.nx > 4 * kChunkSize1D256kB)
114  {
115  chunks.nx = kChunkSize1D256kB;
116  }
117  }
118 
119  hid_t dataset = file.createDataset(file.getRootGroup(),
120  matrixName,
122  chunks,
124  compressionLevel);
125 
126  file.writeHyperSlab(dataset, DimensionSizes(0, 0, 0), mDimensionSizes, mData);
127 
128  file.closeDataset(dataset);
129 
130  // Write data and domain type
133 }// end of writeData
134 //----------------------------------------------------------------------------------------------------------------------
135 
136 //--------------------------------------------------------------------------------------------------------------------//
137 //------------------------------------------------ Protected methods -------------------------------------------------//
138 //--------------------------------------------------------------------------------------------------------------------//
139 
140 //--------------------------------------------------------------------------------------------------------------------//
141 //------------------------------------------------- Private methods --------------------------------------------------//
142 //--------------------------------------------------------------------------------------------------------------------//
143 
144 /**
145  * Set necessary dimensions and auxiliary variables.
146  */
147 void RealMatrix::initDimensions(const DimensionSizes& dimensionSizes)
148 {
149  mDimensionSizes = dimensionSizes;
150 
151  mSize = dimensionSizes.nx * dimensionSizes.ny * dimensionSizes.nz;
152 
153  mCapacity = mSize;
154 
155  mRowSize = dimensionSizes.nx;
156  mSlabSize = dimensionSizes.nx * dimensionSizes.ny;
157 }// end of initDimensions
158 //----------------------------------------------------------------------------------------------------------------------
static constexpr size_t kChunkSize1D256kB
Number of elements to get 256KB block of data.
Definition: RealMatrix.h:135
size_t mRowSize
Size of a 1D row in X dimension.
virtual void allocateMemory()
Aligned memory allocation (both on CPU and GPU).
virtual void writeData(Hdf5File &file, MatrixName &matrixName, const size_t compressionLevel)
Write data into HDF5 file.
Definition: RealMatrix.cpp:94
hid_t getRootGroup() const
Get handle to the root group of the file.
Definition: Hdf5File.h:608
size_t nz
Number of elements in the z direction.
The header file containing the class for real matrices.
virtual void readData(Hdf5File &file, MatrixName &matrixName)
Read matrix from HDF5 file.
Definition: RealMatrix.cpp:72
void initDimensions(const DimensionSizes &dimensionSizes)
Init dimension.
Definition: RealMatrix.cpp:147
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
float * mData
Raw matrix data.
DimensionSizes mDimensionSizes
Dimension sizes.
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
The header file containing a class responsible for printing out info and error messages (stdout...
Class wrapping the HDF5 routines.
Definition: Hdf5File.h:490
static std::string formatMessage(const std::string &format, Args ... args)
C++-11 replacement for sprintf that works with std::string instead of char*.
Definition: Logger.h:157
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).
static constexpr size_t kChunkSize1D4MB
Number of elements to get 4MB block of data.
Definition: RealMatrix.h:131
The matrix is stored in floating point 32b wide format.
MatrixDataType readMatrixDataType(const hid_t parentGroup, MatrixName &datasetName)
Read matrix data type from the dataset at a specified place in the file tree.
Definition: Hdf5File.cpp:821
The header file with the class for complex matrices.
size_t ny
Number of elements in the y direction.
virtual void freeMemory()
Memory allocation (both on CPU and GPU).
size_t mSize
Total number of used elements.
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
ErrorMessage kErrFmtMatrixNotReal
Matrix class error message.
static constexpr size_t kChunkSize1D1MB
Number of elements to get 1MB block of data.
Definition: RealMatrix.h:133
size_t nx
Number of elements in the x direction.
size_t mSlabSize
Size of a XY slab.
virtual ~RealMatrix()
Destructor.
Definition: RealMatrix.cpp:63
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
RealMatrix()=delete
Default constructor is not allowed.
MatrixDomainType readMatrixDomainType(const hid_t parentGroup, MatrixName &datasetName)
Read matrix dataset domain type at a specified place in the file tree.
Definition: Hdf5File.cpp:848
ErrorMessage kErrFmtMatrixNotFloat
Matrix class error message.
The matrix is defined on real domain.
size_t mCapacity
Total number of allocated elements (in terms of floats).
Abstract base class for float based matrices defining basic interface. Higher dimensional matrices st...