| kspaceFirstOrder3D-OMP 1.1 The C++ implementation of the k-wave toolbox for the time-domain simulation of acoustic wave fields in 3D

- Main Page
- Related Pages
- Classes
- Files

- File List
- File Members

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
37 #include <MatrixClasses/IndexMatrix.h>
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 */
61 TIndexMatrix::TIndexMatrix(const TDimensionSizes & DimensionSizes)
62 : TBaseIndexMatrix()
63 {
64 pDimensionSizes = DimensionSizes;
65
66 pTotalElementCount = pDimensionSizes.X *
67 pDimensionSizes.Y *
68 pDimensionSizes.Z;
69
70 pTotalAllocatedElementCount = pTotalElementCount;
71
72 pDataRowSize = pDimensionSizes.X;
73
74 p2DDataSliceSize = pDimensionSizes.X *
75 pDimensionSizes.Y;
76
77 AllocateMemory();
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 */
90 void TIndexMatrix::ReadDataFromHDF5File(THDF5_File & HDF5_File,
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,
112 pDimensionSizes,
113 pMatrixData);
114
115 }// end of LoadDataFromMatlabFile
116 //------------------------------------------------------------------------------
117
118
119
120
121 /**
122 * Recompute indeces, MATLAB -> C++.
123 */
124 void TIndexMatrix::RecomputeIndicesToCPP()
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 */
137 void TIndexMatrix::RecomputeIndicesToMatlab()
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 */
152 size_t TIndexMatrix::GetTotalNumberOfElementsInAllCuboids() const
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 */
173 void TIndexMatrix::WriteDataToHDF5File(THDF5_File & HDF5_File,
174 const char * MatrixName,
175 const size_t CompressionLevel)
176 {
177 // set chunks - may be necessary for long index based sensor masks
178 TDimensionSizes Chunks = pDimensionSizes;
179 Chunks.Z = 1;
180
181 //1D matrices
182 if ((pDimensionSizes.Y == 1) && (pDimensionSizes.Z == 1))
183 {
184 // Chunk = 4MB
185 if (pDimensionSizes.X > 4 * ChunkSize_1D_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,
201 pDimensionSizes,
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 //----------------------------------------------------------------------------//
TDimensionSizes::Z
size_t Z
Z dimension size.
Definition: DimensionSizes.h:70
TIndexMatrix::ChunkSize_1D_256KB
static const size_t ChunkSize_1D_256KB
Number of elements to get 256KB block of data.
Definition: IndexMatrix.h:153
TIndexMatrix::GetTotalNumberOfElementsInAllCuboids
size_t GetTotalNumberOfElementsInAllCuboids() const
Get the total number of elements to be sampled within all cuboids.
Definition: IndexMatrix.cpp:152
THDF5_File::ReadMatrixDataType
THDF5_File::THDF5_MatrixDataType ReadMatrixDataType(const hid_t ParentGroup, const char *DatasetName)
Read matrix data type from the dataset.
Definition: HDF5_File.cpp:1155
TIndexMatrix::GetBottomRightCorner
TDimensionSizes GetBottomRightCorner(const size_t &index) const
Get the bottom right corner of the index-th cuboid.
Definition: IndexMatrix.h:116
TDimensionSizes::X
size_t X
X dimension size.
Definition: DimensionSizes.h:66
TIndexMatrix::RecomputeIndicesToMatlab
void RecomputeIndicesToMatlab()
Recompute indices C++ -> MATLAB.
Definition: IndexMatrix.cpp:137
TIndexMatrix::TIndexMatrix
TIndexMatrix()
Default constructor not allowed for public.
Definition: IndexMatrix.h:138
THDF5_File::WriteHyperSlab
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
TBaseIndexMatrix::AllocateMemory
virtual void AllocateMemory()
Memory allocation.
Definition: BaseIndexMatrix.cpp:84
THDF5_File::WriteMatrixDataType
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
TBaseIndexMatrix::pTotalAllocatedElementCount
size_t pTotalAllocatedElementCount
Total number of allocated elements (the array size).
Definition: BaseIndexMatrix.h:105
THDF5_File::WriteMatrixDomainType
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
TIndexMatrix::ChunkSize_1D_1MB
static const size_t ChunkSize_1D_1MB
Number of elements to get 1MB block of data.
Definition: IndexMatrix.h:151
TIndexMatrix::GetTopLeftCorner
TDimensionSizes GetTopLeftCorner(const size_t &index) const
Get the top left corner of the index-th cuboid.
Definition: IndexMatrix.h:99
IndexMatrix.h
The header file containing the class for 64b integer matrices.
THDF5_File::GetRootGroup
hid_t GetRootGroup() const
Get handle to the root group.
Definition: HDF5_File.h:581
ErrorMessages.h
The header file containing all error messages of the project.
TBaseIndexMatrix
Abstract base class for index based matrices defining basic interface. Higher dimensional matrices st...
Definition: BaseIndexMatrix.h:54
THDF5_File::ReadMatrixDomainType
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
TBaseIndexMatrix::pDimensionSizes
struct TDimensionSizes pDimensionSizes
Dimension sizes.
Definition: BaseIndexMatrix.h:108
TIndexMatrix::RecomputeIndicesToCPP
void RecomputeIndicesToCPP()
Recompute indices MATALAB->C++.
Definition: IndexMatrix.cpp:124
TDimensionSizes::Y
size_t Y
Y dimension size.
Definition: DimensionSizes.h:68
TBaseIndexMatrix::pMatrixData
size_t * pMatrixData
Raw matrix data.
Definition: BaseIndexMatrix.h:116
TBaseIndexMatrix::p2DDataSliceSize
size_t p2DDataSliceSize
Size of 2D slab (X,Y).
Definition: BaseIndexMatrix.h:113
THDF5_File::ReadCompleteDataset
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
TBaseIndexMatrix::pTotalElementCount
size_t pTotalElementCount
Total number of elements.
Definition: BaseIndexMatrix.h:103
TIndexMatrix::WriteDataToHDF5File
virtual void WriteDataToHDF5File(THDF5_File &HDF5_File, const char *MatrixName, const size_t CompressionLevel)
Write data into the HDF5 file.
Definition: IndexMatrix.cpp:173
Matrix_ERR_FMT_MatrixNotLong
const char *const Matrix_ERR_FMT_MatrixNotLong
Matrix class error message.
Definition: ErrorMessages.h:89
THDF5_File::CreateIndexDataset
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
TIndexMatrix::ReadDataFromHDF5File
virtual void ReadDataFromHDF5File(THDF5_File &HDF5_File, const char *MatrixName)
Read data from the HDF5 file.
Definition: IndexMatrix.cpp:90
THDF5_File::CloseDataset
void CloseDataset(const hid_t HDF5_Dataset_id)
Close the HDF5 dataset.
Definition: HDF5_File.cpp:451
Matrix_ERR_FMT_MatrixNotReal
const char *const Matrix_ERR_FMT_MatrixNotReal
Matrix class error message.
Definition: ErrorMessages.h:85
TIndexMatrix::ChunkSize_1D_4MB
static const size_t ChunkSize_1D_4MB
Number of elements to get 4MB block of data.
Definition: IndexMatrix.h:149
THDF5_File
Class wrapping the HDF5 routines.
Definition: HDF5_File.h:506
TBaseIndexMatrix::pDataRowSize
size_t pDataRowSize
Size of 1D row in X dimension.
Definition: BaseIndexMatrix.h:111
TDimensionSizes
Structure with 4D dimension sizes (3 in space and 1 in time).
Definition: DimensionSizes.h:63