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
IndexMatrix.cpp
Go to the documentation of this file.
1 /**
2  * @file IndexMatrix.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 64b integer matrices.
10  *
11  * @version kspaceFirstOrder3D 3.4
12  *
13  * @date 26 July 2011, 15:16 (created) \n
14  * 10 August 2016, 11:58 (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/.
30  */
31 
33 #include <Logger/Logger.h>
34 
35 //------------------------------------------------------------------------------------------------//
36 //------------------------------------------ Constants -------------------------------------------//
37 //------------------------------------------------------------------------------------------------//
38 
39 //------------------------------------------------------------------------------------------------//
40 //--------------------------------------- Public methods -----------------------------------------//
41 //------------------------------------------------------------------------------------------------//
42 
43 /**
44  * Constructor allocating memory.
45  *
46  * @param [in] dimensionSizes - Dimension sizes
47  */
50 {
51  this->dimensionSizes = dimensionSizes;
52 
53  nElements = dimensionSizes.nx * dimensionSizes.ny * dimensionSizes.nz;
54 
56 
57  rowSize = dimensionSizes.nx;
58  slabSize = dimensionSizes.nx * dimensionSizes.ny;
59 
61 }// end of TIndexMatrix
62 //--------------------------------------------------------------------------------------------------
63 
64 /**
65  * Destructor.
66  */
68 {
69  FreeMemory();
70 }
71 //--------------------------------------------------------------------------------------------------
72 
73 /**
74  * Read data from HDF5 file (only from the root group).
75  *
76  * @param [in] file - HDF5 file handle
77  * @param [in] matrixName - HDF5 dataset name
78  *
79  * @throw ios:failure if error occurs.
80  */
82  TMatrixName& matrixName)
83 {
84  if (file.ReadMatrixDataType(file.GetRootGroup(), matrixName) != THDF5_File::LONG)
85  {
86  throw std::ios::failure(TLogger::FormatMessage(ERR_FMT_MATRIX_NOT_INDEX, matrixName.c_str()));
87  }
88 
89  if (file.ReadMatrixDomainType(file.GetRootGroup(),matrixName) != THDF5_File::REAL)
90  {
91  throw std::ios::failure(TLogger::FormatMessage(ERR_FMT_MATRIX_NOT_REAL,matrixName.c_str()));
92  }
93 
94  file.ReadCompleteDataset(file.GetRootGroup(), matrixName, dimensionSizes, hostData);
95 }// end of LoadDataFromMatlabFile
96 //--------------------------------------------------------------------------------------------------
97 
98 /**
99  * Write data to HDF5 file.
100  *
101  * @param [in] file - HDF5 file handle
102  * @param [in] matrixName - HDF5 dataset name
103  * @param [in] compressionLevel - compression level
104  *
105  * @throw ios:failure if error occurs.
106  */
108  TMatrixName& matrixName,
109  const size_t compressionLevel)
110 {
111  // set chunks - may be necessary for long index based sensor masks
113  chunks.nz = 1;
114 
115  //1D matrices
116  if ((dimensionSizes.ny == 1) && (dimensionSizes.nz == 1))
117  {
118  // Chunk = 4MB
119  if (dimensionSizes.nx > (4 * CHUNK_SIZE_1D_4MB))
120  {
121  chunks.nx = CHUNK_SIZE_1D_4MB;
122  }
123  else if (dimensionSizes.nx > (4 * CHUNK_SIZE_1D_1MB))
124  {
125  chunks.nx = CHUNK_SIZE_1D_1MB;
126  }
127  else if (dimensionSizes.nx > (4 * CHUNK_SIZE_1D_256KB))
128  {
129  chunks.nx = CHUNK_SIZE_1D_256KB;
130  }
131  }
132 
133  // create dataset and write a slab
134  hid_t dataset = file.CreateIndexDataset(file.GetRootGroup(),
135  matrixName,
137  chunks,
138  compressionLevel);
139 
140  file.WriteHyperSlab(dataset, TDimensionSizes(0, 0, 0), dimensionSizes, hostData);
141 
142  file.CloseDataset(dataset);
143 
144  // write data and domain types
145  file.WriteMatrixDataType(file.GetRootGroup(), matrixName, THDF5_File::LONG);
146  file.WriteMatrixDomainType(file.GetRootGroup(), matrixName, THDF5_File::REAL);
147 }// end of WriteDataToHDF5File
148 //--------------------------------------------------------------------------------------------------
149 
150 
151 /**
152  * Get the top left corner of the index-th cuboid. Cuboids are stored as 6-tuples (two 3D
153  * coordinates). This gives the first three coordinates.
154  *
155  * @param [in] index - Index of the cuboid
156  * @return The top left corner
157  */
159 {
160  size_t x = hostData[6 * index ];
161  size_t y = hostData[6 * index + 1];
162  size_t z = hostData[6 * index + 2];
163 
164  return TDimensionSizes(x, y, z);
165 }// end of GetTopLeftCorner
166 //--------------------------------------------------------------------------------------------------
167 
168 /**
169  * Get the top bottom right of the index-th cuboid. Cuboids are stored as 6-tuples (two 3D
170  * coordinates). This gives the first three coordinates. This routine works only on the CPU side.
171  *
172  * @param [in] index -Index of the cuboid
173  * @return The bottom right corner
174 */
176 {
177  size_t x = hostData[6 * index + 3];
178  size_t y = hostData[6 * index + 4];
179  size_t z = hostData[6 * index + 5];
180 
181  return TDimensionSizes(x, y, z);
182 }// end of GetBottomRightCorner
183 //-------------------------------------------------------------------------------------------------
184 
185 
186 /**
187  * Recompute indeces, MATLAB -> C++.
188  */
190 {
191  #pragma omp parallel for if (nElements > 1e5)
192  for (size_t i = 0; i < nElements; i++)
193  {
194  hostData[i]--;
195  }
196 }// end of RecomputeIndices
197 //--------------------------------------------------------------------------------------------------
198 
199 /**
200  * Recompute indeces, C++ -> MATLAB.
201  */
203 {
204  #pragma omp parallel for if (nElements > 1e5)
205  for (size_t i = 0; i < nElements; i++)
206  {
207  hostData[i]++;
208  }
209 }// end of RecomputeIndicesToMatlab
210 //--------------------------------------------------------------------------------------------------
211 
212 /**
213  * Get total number of elements in all cuboids to be able to allocate output file.
214  *
215  * @return Total sampled grid points
216  */
218 {
219  size_t elementSum = 0;
220  for (size_t cuboidIdx = 0; cuboidIdx < dimensionSizes.ny; cuboidIdx++)
221  {
222  elementSum += (GetBottomRightCorner(cuboidIdx) - GetTopLeftCorner(cuboidIdx)).GetElementCount();
223  }
224 
225  return elementSum;
226 }// end of GetTotalNumberOfElementsInAllCuboids
227 //--------------------------------------------------------------------------------------------------
228 
229 
230 //------------------------------------------------------------------------------------------------//
231 //-------------------------------------- Protected methods ---------------------------------------//
232 //------------------------------------------------------------------------------------------------//
233 
234 //------------------------------------------------------------------------------------------------//
235 //--------------------------------------- Private methods ----------------------------------------//
236 //------------------------------------------------------------------------------------------------//
size_t nx
number of elements in the x direction
size_t GetTotalNumberOfElementsInAllCuboids() const
Get the total number of elements to be sampled within all cuboids.
void WriteMatrixDomainType(const hid_t parentGroup, TMatrixName &datasetName, const TMatrixDomainType &matrixDomainType)
Write matrix domain type into the dataset under the root group.
Definition: HDF5_File.cpp:1048
TDimensionSizes GetBottomRightCorner(const size_t &index) const
Get the bottom right corner of the index-th cuboid.
virtual void WriteDataToHDF5File(THDF5_File &file, TMatrixName &matrixName, const size_t compressionLevel)
Write data into the HDF5 file.
THDF5_File::TMatrixDataType ReadMatrixDataType(const hid_t parentGroup, TMatrixName &datasetName)
Read matrix data type from the dataset.
Definition: HDF5_File.cpp:1068
size_t nAllocatedElements
Total number of allocated elements (the array size).
const std::string TMatrixName
Datatype for matrix names.
Definition: MatrixNames.h:45
void RecomputeIndicesToMatlab()
Recompute indices C++ -> MATLAB.
TIndexMatrix()
Default constructor not allowed for public.
Definition: IndexMatrix.h:100
virtual ~TIndexMatrix()
Destructor.
Definition: IndexMatrix.cpp:67
void WriteMatrixDataType(const hid_t parentGroup, TMatrixName &datasetName, const TMatrixDataType &matrixDataType)
Write matrix data type into the dataset under a specified group.
Definition: HDF5_File.cpp:1029
virtual void AllocateMemory()
Memory allocation (both on CPU and GPU)
The header file containing a class responsible for printing out info and error messages (stdout...
virtual void ReadDataFromHDF5File(THDF5_File &file, TMatrixName &matrixName)
Read data from the HDF5 file.
Definition: IndexMatrix.cpp:81
TDimensionSizes GetTopLeftCorner(const size_t &index) const
Get the top left corner of the index-th cuboid.
The header file containing the class for 64b integer matrices.
struct TDimensionSizes dimensionSizes
Dimension sizes.
hid_t GetRootGroup() const
Get handle to the root group.
Definition: HDF5_File.h:573
size_t ny
number of elements in the y direction
size_t * hostData
Raw CPU matrix data.
Abstract base class for index based matrices defining basic interface. Higher dimensional matrices st...
virtual size_t GetElementCount() const
Get total element count of the matrix.
void RecomputeIndicesToCPP()
Recompute indices MATALAB->C++.
TErrorMessage ERR_FMT_MATRIX_NOT_REAL
Matrix class error message.
size_t rowSize
Size of 1D row in X dimension.
static const size_t CHUNK_SIZE_1D_4MB
Number of elements to get 4MB block of data.
Definition: IndexMatrix.h:108
hid_t CreateIndexDataset(const hid_t parentGroup, TMatrixName &datasetName, const TDimensionSizes &dimensionSizes, const TDimensionSizes &chunkSizes, const size_t compressionLevel)
Create an index HDF5 dataset at a specified place in the file tree (3D only).
Definition: HDF5_File.cpp:392
TErrorMessage ERR_FMT_MATRIX_NOT_INDEX
Matrix class error message.
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:126
THDF5_File::TMatrixDomainType ReadMatrixDomainType(const hid_t parentGroup, TMatrixName &datasetName)
Read matrix domain type from the dataset under a specified group.
Definition: HDF5_File.cpp:1101
void WriteHyperSlab(const hid_t dataset, const TDimensionSizes &position, const TDimensionSizes &size, const float *data)
Write a hyper-slab into the dataset - float dataset.
Definition: HDF5_File.cpp:473
size_t nElements
Total number of elements.
size_t slabSize
Size of 2D slab (X,Y).
size_t nz
number of elements in the z direction
void ReadCompleteDataset(const hid_t parentGroup, TMatrixName &datasetName, const TDimensionSizes &dimensionSizes, float *data)
Read data from the dataset under a specified group, float dataset.
Definition: HDF5_File.cpp:887
void CloseDataset(const hid_t dataset)
Close the HDF5 dataset.
Definition: HDF5_File.cpp:457
static const size_t CHUNK_SIZE_1D_1MB
Number of elements to get 1MB block of data.
Definition: IndexMatrix.h:110
Class wrapping the HDF5 routines.
Definition: HDF5_File.h:500
Structure with 4D dimension sizes (3 in space and 1 in time).
virtual void FreeMemory()
Memory deallocation (both on CPU and GPU)
static const size_t CHUNK_SIZE_1D_256KB
Number of elements to get 256KB block of data.
Definition: IndexMatrix.h:112