kspaceFirstOrder3D-OMP  1.1
The C++ implementation of the k-wave toolbox for the time-domain simulation of acoustic wave fields in 3D
 All Classes Files Functions Variables Typedefs Enumerations Friends Pages
IndexMatrix.cpp
Go to the documentation of this file.
1 /**
2  * @file IndexMatrix.cpp
3  * @author Jiri Jaros \n
4  * Faculty of Information Technology\n
5  * Brno University of Technology \n
6  * jarosjir@fit.vutbr.cz
7  *
8  * @brief The implementation file containing the class for 64b integer matrices
9  *
10  * @version kspaceFirstOrder3D 2.15
11  *
12  * @date 26 July 2011, 15:16 (created) \n
13  * 25 September 2014, 17:29 (revised)
14  *
15  * @section License
16  * This file is part of the C++ extension of the k-Wave Toolbox (http://www.k-wave.org).\n
17  * Copyright (C) 2015 Jiri Jaros and Bradley Treeby
18  *
19  * This file is part of k-Wave. k-Wave is free software: you can redistribute it
20  * and/or modify it under the terms of the GNU Lesser General Public License as
21  * published by the Free Software Foundation, either version 3 of the License,
22  * or (at your option) any later version.
23  *
24  * k-Wave is distributed in the hope that it will be useful, but
25  * WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
27  * See the GNU Lesser General Public License for more details.
28  *
29  * You should have received a copy of the GNU Lesser General Public License
30  * along with k-Wave. If not, see <http://www.gnu.org/licenses/>.
31  */
32 
33 
34 
35 #include <iostream>
36 
38 
39 #include <Utils/ErrorMessages.h>
40 
41 //----------------------------------------------------------------------------//
42 // Constants //
43 //----------------------------------------------------------------------------//
44 
45 
46 //----------------------------------------------------------------------------//
47 // Definitions //
48 //----------------------------------------------------------------------------//
49 
50 
51 
52 //----------------------------------------------------------------------------//
53 // Implementation //
54 // public methods //
55 //----------------------------------------------------------------------------//
56 
57 /**
58  * Constructor allocating memory.
59  * @param [in] DimensionSizes - Dimension sizes
60  */
63 {
64  pDimensionSizes = DimensionSizes;
65 
69 
71 
73 
76 
78 }// end of TRealMatrixData
79 //-----------------------------------------------------------------------------
80 
81 
82 
83 /**
84  * Read data from HDF5 file (only from the root group).
85  * @param [in] HDF5_File - HDF5 file handle
86  * @param [in] MatrixName - HDF5 dataset name
87  *
88  * @throw ios:failure if there's an error
89  */
91  const char * MatrixName)
92 {
93  // check the datatype
94  if (HDF5_File.ReadMatrixDataType(HDF5_File.GetRootGroup(),MatrixName) != THDF5_File::hdf5_mdt_long)
95  {
96  char ErrorMessage[256];
97  sprintf(ErrorMessage,Matrix_ERR_FMT_MatrixNotLong,MatrixName);
98  throw ios::failure(ErrorMessage);
99  }
100 
101  // check the domain type
102  if (HDF5_File.ReadMatrixDomainType(HDF5_File.GetRootGroup(), MatrixName) != THDF5_File::hdf5_mdt_real)
103  {
104  char ErrorMessage[256];
105  sprintf(ErrorMessage,Matrix_ERR_FMT_MatrixNotReal,MatrixName);
106  throw ios::failure(ErrorMessage);
107  }
108 
109  // read data
110  HDF5_File.ReadCompleteDataset(HDF5_File.GetRootGroup(),
111  MatrixName,
113  pMatrixData);
114 
115 }// end of LoadDataFromMatlabFile
116 //------------------------------------------------------------------------------
117 
118 
119 
120 
121 /**
122  * Recompute indeces, MATLAB -> C++.
123  */
125 {
126  #pragma omp parallel for if (pTotalElementCount > 1e5)
127  for (size_t i = 0; i < pTotalElementCount; i++)
128  {
129  pMatrixData[i]--;
130  }
131 }// end of RecomputeIndices
132 //------------------------------------------------------------------------------
133 
134 /**
135  * Recompute indeces, C++ -> MATLAB.
136  */
138 {
139  #pragma omp parallel for if (pTotalElementCount > 1e5)
140  for (size_t i = 0; i < pTotalElementCount; i++)
141  {
142  pMatrixData[i]++;
143  }
144 }// end of RecomputeIndicesToMatlab
145 //------------------------------------------------------------------------------
146 
147 
148 /**
149  * Get total number of elements in all cuboids to be able to allocate output file.
150  * @return Total sampled grid points
151  */
153 {
154  size_t ElementSum = 0;
155  for (size_t cuboidIdx = 0; cuboidIdx < pDimensionSizes.Y; cuboidIdx++)
156  {
157  ElementSum += (GetBottomRightCorner(cuboidIdx) - GetTopLeftCorner(cuboidIdx)).GetElementCount();
158  }
159 
160  return ElementSum;
161 }// end of GetTotalNumberOfElementsInAllCuboids
162 //------------------------------------------------------------------------------
163 
164 /**
165  * Write data to HDF5 file
166  *
167  * @param [in] HDF5_File - HDF5 file handle
168  * @param [in] MatrixName - HDF5 Dataset name
169  * @param [in] CompressionLevel - Compression level
170  *
171  * @throw ios:failure if there's an error
172  */
174  const char * MatrixName,
175  const size_t CompressionLevel)
176 {
177  // set chunks - may be necessary for long index based sensor masks
179  Chunks.Z = 1;
180 
181  //1D matrices
182  if ((pDimensionSizes.Y == 1) && (pDimensionSizes.Z == 1))
183  {
184  // Chunk = 4MB
186  {
187  Chunks.X = ChunkSize_1D_4MB;
188  }
189  else if (pDimensionSizes.X > 4 * ChunkSize_1D_1MB)
190  {
191  Chunks.X = ChunkSize_1D_1MB;
192  }
193  else if (pDimensionSizes.X > 4 * ChunkSize_1D_256KB)
194  {
195  Chunks.X = ChunkSize_1D_256KB;
196  }
197  }
198 
199  hid_t HDF5_Dataset_id = HDF5_File.CreateIndexDataset(HDF5_File.GetRootGroup(),
200  MatrixName,
202  Chunks,
203  CompressionLevel);
204 
205  HDF5_File.WriteHyperSlab(HDF5_Dataset_id,
206  TDimensionSizes(0, 0, 0),
207  pDimensionSizes,
208  pMatrixData);
209 
210  HDF5_File.CloseDataset(HDF5_Dataset_id);
211 
212  // write data and domain types
213  HDF5_File.WriteMatrixDataType (HDF5_File.GetRootGroup(),
214  MatrixName,
215  THDF5_File::hdf5_mdt_long);
216 
217  HDF5_File.WriteMatrixDomainType(HDF5_File.GetRootGroup(),
218  MatrixName,
219  THDF5_File::hdf5_mdt_real);
220 }// end of WriteDataToHDF5File
221 //---------------------------------------------------------------------------
222 
223 
224 //----------------------------------------------------------------------------//
225 // Implementation //
226 // protected methods //
227 //----------------------------------------------------------------------------//
228 
229 
230 //----------------------------------------------------------------------------//
231 // Implementation //
232 // private methods //
233 //----------------------------------------------------------------------------//
size_t Z
Z dimension size.
static const size_t ChunkSize_1D_256KB
Number of elements to get 256KB block of data.
Definition: IndexMatrix.h:153
size_t GetTotalNumberOfElementsInAllCuboids() const
Get the total number of elements to be sampled within all cuboids.
THDF5_File::THDF5_MatrixDataType ReadMatrixDataType(const hid_t ParentGroup, const char *DatasetName)
Read matrix data type from the dataset.
Definition: HDF5_File.cpp:1155
TDimensionSizes GetBottomRightCorner(const size_t &index) const
Get the bottom right corner of the index-th cuboid.
Definition: IndexMatrix.h:116
size_t X
X dimension size.
void RecomputeIndicesToMatlab()
Recompute indices C++ -> MATLAB.
TIndexMatrix()
Default constructor not allowed for public.
Definition: IndexMatrix.h:138
void WriteHyperSlab(const hid_t HDF5_Dataset_id, const TDimensionSizes &Position, const TDimensionSizes &Size, const float *Data)
Write a hyper-slab into the dataset - float dataset.
Definition: HDF5_File.cpp:467
virtual void AllocateMemory()
Memory allocation.
void WriteMatrixDataType(const hid_t ParentGroup, const char *DatasetName, const THDF5_MatrixDataType &MatrixDataType)
Write matrix data type into the dataset under a specified group.
Definition: HDF5_File.cpp:1116
size_t pTotalAllocatedElementCount
Total number of allocated elements (the array size).
void WriteMatrixDomainType(const hid_t ParentGroup, const char *DatasetName, const THDF5_MatrixDomainType &MatrixDomainType)
Write matrix domain type into the dataset under the root group.
Definition: HDF5_File.cpp:1135
static const size_t ChunkSize_1D_1MB
Number of elements to get 1MB block of data.
Definition: IndexMatrix.h:151
TDimensionSizes GetTopLeftCorner(const size_t &index) const
Get the top left corner of the index-th cuboid.
Definition: IndexMatrix.h:99
The header file containing the class for 64b integer matrices.
hid_t GetRootGroup() const
Get handle to the root group.
Definition: HDF5_File.h:581
The header file containing all error messages of the project.
Abstract base class for index based matrices defining basic interface. Higher dimensional matrices st...
THDF5_File::THDF5_MatrixDomainType ReadMatrixDomainType(const hid_t ParentGroup, const char *DatasetName)
Read matrix domain type from the dataset under a specified group.
Definition: HDF5_File.cpp:1190
struct TDimensionSizes pDimensionSizes
Dimension sizes.
void RecomputeIndicesToCPP()
Recompute indices MATALAB->C++.
size_t Y
Y dimension size.
size_t * pMatrixData
Raw matrix data.
size_t p2DDataSliceSize
Size of 2D slab (X,Y).
void ReadCompleteDataset(const hid_t ParentGroup, const char *DatasetName, const TDimensionSizes &DimensionSizes, float *Data)
Read data from the dataset under a specified group - float dataset.
Definition: HDF5_File.cpp:954
size_t pTotalElementCount
Total number of elements.
virtual void WriteDataToHDF5File(THDF5_File &HDF5_File, const char *MatrixName, const size_t CompressionLevel)
Write data into the HDF5 file.
const char *const Matrix_ERR_FMT_MatrixNotLong
Matrix class error message.
Definition: ErrorMessages.h:89
hid_t CreateIndexDataset(const hid_t ParentGroup, const char *DatasetName, const TDimensionSizes &DimensionSizes, const TDimensionSizes &ChunkSizes, const size_t CompressionLevel)
Create the HDF5 dataset at a specified place in the file tree (3D only).
Definition: HDF5_File.cpp:385
virtual void ReadDataFromHDF5File(THDF5_File &HDF5_File, const char *MatrixName)
Read data from the HDF5 file.
Definition: IndexMatrix.cpp:90
void CloseDataset(const hid_t HDF5_Dataset_id)
Close the HDF5 dataset.
Definition: HDF5_File.cpp:451
const char *const Matrix_ERR_FMT_MatrixNotReal
Matrix class error message.
Definition: ErrorMessages.h:85
static const size_t ChunkSize_1D_4MB
Number of elements to get 4MB block of data.
Definition: IndexMatrix.h:149
Class wrapping the HDF5 routines.
Definition: HDF5_File.h:506
size_t pDataRowSize
Size of 1D row in X dimension.
Structure with 4D dimension sizes (3 in space and 1 in time).