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

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

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
32 #include <MatrixClasses/IndexMatrix.h>
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 */
48 TIndexMatrix::TIndexMatrix(const TDimensionSizes& dimensionSizes)
49 : TBaseIndexMatrix()
50 {
51 this->dimensionSizes = dimensionSizes;
52
53 nElements = dimensionSizes.nx * dimensionSizes.ny * dimensionSizes.nz;
54
55 nAllocatedElements = nElements;
56
57 rowSize = dimensionSizes.nx;
58 slabSize = dimensionSizes.nx * dimensionSizes.ny;
59
60 AllocateMemory();
61 }// end of TIndexMatrix
62 //--------------------------------------------------------------------------------------------------
63
64 /**
65 * Destructor.
66 */
67 TIndexMatrix::~TIndexMatrix()
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 */
81 void TIndexMatrix::ReadDataFromHDF5File(THDF5_File& file,
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 */
107 void TIndexMatrix::WriteDataToHDF5File(THDF5_File& file,
108 TMatrixName& matrixName,
109 const size_t compressionLevel)
110 {
111 // set chunks - may be necessary for long index based sensor masks
112 TDimensionSizes chunks = dimensionSizes;
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,
136 dimensionSizes,
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 */
158 TDimensionSizes TIndexMatrix::GetTopLeftCorner(const size_t& index) const
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 */
175 TDimensionSizes TIndexMatrix::GetBottomRightCorner(const size_t& index) const
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 */
189 void TIndexMatrix::RecomputeIndicesToCPP()
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 */
202 void TIndexMatrix::RecomputeIndicesToMatlab()
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 */
217 size_t TIndexMatrix::GetTotalNumberOfElementsInAllCuboids() const
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 //------------------------------------------------------------------------------------------------//
TDimensionSizes::nx
size_t nx
number of elements in the x direction
Definition: DimensionSizes.h:145
TIndexMatrix::GetTotalNumberOfElementsInAllCuboids
size_t GetTotalNumberOfElementsInAllCuboids() const
Get the total number of elements to be sampled within all cuboids.
Definition: IndexMatrix.cpp:217
THDF5_File::WriteMatrixDomainType
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
TIndexMatrix::GetBottomRightCorner
TDimensionSizes GetBottomRightCorner(const size_t &index) const
Get the bottom right corner of the index-th cuboid.
Definition: IndexMatrix.cpp:175
TIndexMatrix::WriteDataToHDF5File
virtual void WriteDataToHDF5File(THDF5_File &file, TMatrixName &matrixName, const size_t compressionLevel)
Write data into the HDF5 file.
Definition: IndexMatrix.cpp:107
THDF5_File::ReadMatrixDataType
THDF5_File::TMatrixDataType ReadMatrixDataType(const hid_t parentGroup, TMatrixName &datasetName)
Read matrix data type from the dataset.
Definition: HDF5_File.cpp:1068
TBaseIndexMatrix::nAllocatedElements
size_t nAllocatedElements
Total number of allocated elements (the array size).
Definition: BaseIndexMatrix.h:124
TMatrixName
const std::string TMatrixName
Datatype for matrix names.
Definition: MatrixNames.h:45
TIndexMatrix::RecomputeIndicesToMatlab
void RecomputeIndicesToMatlab()
Recompute indices C++ -> MATLAB.
Definition: IndexMatrix.cpp:202
TIndexMatrix::TIndexMatrix
TIndexMatrix()
Default constructor not allowed for public.
Definition: IndexMatrix.h:100
TIndexMatrix::~TIndexMatrix
virtual ~TIndexMatrix()
Destructor.
Definition: IndexMatrix.cpp:67
THDF5_File::WriteMatrixDataType
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
TBaseIndexMatrix::AllocateMemory
virtual void AllocateMemory()
Memory allocation (both on CPU and GPU)
Definition: BaseIndexMatrix.cpp:113
Logger.h
The header file containing a class responsible for printing out info and error messages (stdout...
TIndexMatrix::ReadDataFromHDF5File
virtual void ReadDataFromHDF5File(THDF5_File &file, TMatrixName &matrixName)
Read data from the HDF5 file.
Definition: IndexMatrix.cpp:81
TIndexMatrix::GetTopLeftCorner
TDimensionSizes GetTopLeftCorner(const size_t &index) const
Get the top left corner of the index-th cuboid.
Definition: IndexMatrix.cpp:158
IndexMatrix.h
The header file containing the class for 64b integer matrices.
TBaseIndexMatrix::dimensionSizes
struct TDimensionSizes dimensionSizes
Dimension sizes.
Definition: BaseIndexMatrix.h:127
THDF5_File::GetRootGroup
hid_t GetRootGroup() const
Get handle to the root group.
Definition: HDF5_File.h:573
TDimensionSizes::ny
size_t ny
number of elements in the y direction
Definition: DimensionSizes.h:147
TBaseIndexMatrix::hostData
size_t * hostData
Raw CPU matrix data.
Definition: BaseIndexMatrix.h:135
TBaseIndexMatrix
Abstract base class for index based matrices defining basic interface. Higher dimensional matrices st...
Definition: BaseIndexMatrix.h:49
TBaseIndexMatrix::GetElementCount
virtual size_t GetElementCount() const
Get total element count of the matrix.
Definition: BaseIndexMatrix.h:65
TIndexMatrix::RecomputeIndicesToCPP
void RecomputeIndicesToCPP()
Recompute indices MATALAB->C++.
Definition: IndexMatrix.cpp:189
ERR_FMT_MATRIX_NOT_REAL
TErrorMessage ERR_FMT_MATRIX_NOT_REAL
Matrix class error message.
Definition: ErrorMessages.h:129
TBaseIndexMatrix::rowSize
size_t rowSize
Size of 1D row in X dimension.
Definition: BaseIndexMatrix.h:130
TIndexMatrix::CHUNK_SIZE_1D_4MB
static const size_t CHUNK_SIZE_1D_4MB
Number of elements to get 4MB block of data.
Definition: IndexMatrix.h:108
THDF5_File::CreateIndexDataset
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
ERR_FMT_MATRIX_NOT_INDEX
TErrorMessage ERR_FMT_MATRIX_NOT_INDEX
Matrix class error message.
Definition: ErrorMessages.h:135
TLogger::FormatMessage
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::ReadMatrixDomainType
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
THDF5_File::WriteHyperSlab
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
TBaseIndexMatrix::nElements
size_t nElements
Total number of elements.
Definition: BaseIndexMatrix.h:122
TBaseIndexMatrix::slabSize
size_t slabSize
Size of 2D slab (X,Y).
Definition: BaseIndexMatrix.h:132
TDimensionSizes::nz
size_t nz
number of elements in the z direction
Definition: DimensionSizes.h:149
THDF5_File::ReadCompleteDataset
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
THDF5_File::CloseDataset
void CloseDataset(const hid_t dataset)
Close the HDF5 dataset.
Definition: HDF5_File.cpp:457
TIndexMatrix::CHUNK_SIZE_1D_1MB
static const size_t CHUNK_SIZE_1D_1MB
Number of elements to get 1MB block of data.
Definition: IndexMatrix.h:110
THDF5_File
Class wrapping the HDF5 routines.
Definition: HDF5_File.h:500
TDimensionSizes
Structure with 4D dimension sizes (3 in space and 1 in time).
Definition: DimensionSizes.h:66
TBaseIndexMatrix::FreeMemory
virtual void FreeMemory()
Memory deallocation (both on CPU and GPU)
Definition: BaseIndexMatrix.cpp:138
TIndexMatrix::CHUNK_SIZE_1D_256KB
static const size_t CHUNK_SIZE_1D_256KB
Number of elements to get 256KB block of data.
Definition: IndexMatrix.h:112