| 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
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 3.4
12 *
13 * @date 11 July 2011, 10:30 (created) \n
14 * 10 August 2016, 11:59 (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/RealMatrix.h>
33 #include <MatrixClasses/ComplexMatrix.h>
34
35 #include <Logger/Logger.h>
36
37 //------------------------------------------------------------------------------------------------//
38 //------------------------------------------ Constants -------------------------------------------//
39 //------------------------------------------------------------------------------------------------//
40
41
42 //------------------------------------------------------------------------------------------------//
43 //--------------------------------------- Public methods -----------------------------------------//
44 //------------------------------------------------------------------------------------------------//
45 /**
46 * Constructor.
47 * @param [in] dimensionSizes - Dimension sizes of the matrix
48 */
49 TRealMatrix::TRealMatrix(const TDimensionSizes& dimensionSizes)
50 : TBaseFloatMatrix()
51 {
52 InitDimensions(dimensionSizes);
53 AllocateMemory();
54 }// end of TRealMatrix
55 //--------------------------------------------------------------------------------------------------
56
57 /**
58 * Destructor.
59 */
60 TRealMatrix::~TRealMatrix()
61 {
62 FreeMemory();
63 }// end of ~TRealMatrix
64 //--------------------------------------------------------------------------------------------------
65
66 /**
67 * Read data data from HDF5 file (only from the root group).
68 *
69 * @param [in] file - HDF5 file
70 * @param [in] matrixName - HDF5 dataset name
71 *
72 * @throw ios::failure if error occurred.
73 */
74 void TRealMatrix::ReadDataFromHDF5File(THDF5_File& file,
75 TMatrixName& matrixName)
76 {
77 // test matrix datatype
78 if (file.ReadMatrixDataType(file.GetRootGroup(), matrixName) != THDF5_File::FLOAT)
79 {
80 throw std::ios::failure(TLogger::FormatMessage(ERR_FMT_MATRIX_NOT_FLOAT, matrixName.c_str()));
81 }
82 // read matrix domain type
83 if (file.ReadMatrixDomainType(file.GetRootGroup(), matrixName) != THDF5_File::REAL)
84 {
85 throw std::ios::failure(TLogger::FormatMessage(ERR_FMT_MATRIX_NOT_REAL, matrixName.c_str()));
86 }
87
88 // Read matrix
89 file.ReadCompleteDataset(file.GetRootGroup(), matrixName, dimensionSizes, hostData);
90 }// end of LoadDataFromMatlabFile
91 //--------------------------------------------------------------------------------------------------
92
93 /**
94 * Write data to HDF5 file (only from the root group).
95 *
96 * @param [in] file - HDF5 file
97 * @param [in] matrixName - HDF5 Matrix name
98 * @param [in] compressionLevel - Compression level
99 *
100 * @throw ios::failure if an error occurred.
101 */
102 void TRealMatrix::WriteDataToHDF5File(THDF5_File& file,
103 TMatrixName& matrixName,
104 const size_t compressionLevel)
105 {
106 TDimensionSizes chunks = dimensionSizes;
107 chunks.nz = 1;
108
109 //1D matrices
110 if ((dimensionSizes.ny == 1) && (dimensionSizes.nz == 1))
111 {
112 // Chunk = 4MB
113 if (dimensionSizes.nx > (4 * CHUNK_SIZE_1D_4MB))
114 {
115 chunks.nx = CHUNK_SIZE_1D_4MB;
116 }
117 else if (dimensionSizes.nx > (4 * CHUNK_SIZE_1D_1MB))
118 {
119 chunks.nx = CHUNK_SIZE_1D_1MB;
120 }
121 else if (dimensionSizes.nx > (4 * CHUNK_SIZE_1D_256KB))
122 {
123 chunks.nx = CHUNK_SIZE_1D_256KB;
124 }
125 }
126
127 hid_t dataset = file.CreateFloatDataset(file.GetRootGroup(),
128 matrixName,
129 dimensionSizes,
130 chunks,
131 compressionLevel);
132
133 file.WriteHyperSlab(dataset, TDimensionSizes(0, 0, 0), dimensionSizes, hostData);
134
135 file.CloseDataset(dataset);
136
137 // Write data and domain type
138 file.WriteMatrixDataType (file.GetRootGroup(), matrixName, THDF5_File::FLOAT);
139 file.WriteMatrixDomainType(file.GetRootGroup(), matrixName, THDF5_File::REAL);
140 }// end of WriteDataToHDF5File
141 //--------------------------------------------------------------------------------------------------
142
143 //------------------------------------------------------------------------------------------------//
144 //-------------------------------------- Protected methods ---------------------------------------//
145 //------------------------------------------------------------------------------------------------//
146
147 /**
148 * Set necessary dimensions and auxiliary variables.
149 *
150 * @param [in] dimensionSizes - 3D Dimension sizes
151 */
152 void TRealMatrix::InitDimensions(const TDimensionSizes& dimensionSizes)
153 {
154 this->dimensionSizes = dimensionSizes;
155
156 nElements = dimensionSizes.nx * dimensionSizes.ny * dimensionSizes.nz;
157
158 nAllocatedElements = nElements;
159
160 dataRowSize = dimensionSizes.nx;
161 dataSlabSize = dimensionSizes.nx * dimensionSizes.ny;
162 }// end of SetDimensions
163 //-------------------------------------------------------------------------------------------------/
164
165 //------------------------------------------------------------------------------------------------//
166 //--------------------------------------- Private methods ----------------------------------------//
167 //------------------------------------------------------------------------------------------------//
168
TDimensionSizes::nx
size_t nx
number of elements in the x direction
Definition: DimensionSizes.h:145
TBaseFloatMatrix
Abstract base class for float based matrices defining basic interface. Higher dimensional matrices st...
Definition: BaseFloatMatrix.h:49
TBaseFloatMatrix::hostData
float * hostData
Raw CPU matrix data.
Definition: BaseFloatMatrix.h:138
THDF5_File::CreateFloatDataset
hid_t CreateFloatDataset(const hid_t parentGroup, TMatrixName &datasetName, const TDimensionSizes &dimensionSizes, const TDimensionSizes &chunkSizes, const size_t compressionLevel)
Create a float HDF5 dataset at a specified place in the file tree (3D/4D).
Definition: HDF5_File.cpp:296
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
TBaseFloatMatrix::FreeMemory
virtual void FreeMemory()
Memory allocation (both on CPU and GPU).
Definition: BaseFloatMatrix.cpp:162
TBaseFloatMatrix::AllocateMemory
virtual void AllocateMemory()
Memory allocation (both on CPU and GPU).
Definition: BaseFloatMatrix.cpp:133
RealMatrix.h
The header file containing the class for real matrices.
TBaseFloatMatrix::dimensionSizes
struct TDimensionSizes dimensionSizes
Dimension sizes.
Definition: BaseFloatMatrix.h:130
TRealMatrix::CHUNK_SIZE_1D_256KB
static const size_t CHUNK_SIZE_1D_256KB
Number of elements to get 256KB block of data.
Definition: RealMatrix.h:101
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
TRealMatrix::WriteDataToHDF5File
virtual void WriteDataToHDF5File(THDF5_File &file, TMatrixName &matrixName, const size_t compressionLevel)
Write data into the HDF5 file.
Definition: RealMatrix.cpp:102
ERR_FMT_MATRIX_NOT_FLOAT
TErrorMessage ERR_FMT_MATRIX_NOT_FLOAT
Matrix class error message.
Definition: ErrorMessages.h:126
TBaseFloatMatrix::nAllocatedElements
size_t nAllocatedElements
Total number of allocated elements (in terms of floats).
Definition: BaseFloatMatrix.h:127
TRealMatrix::InitDimensions
virtual void InitDimensions(const TDimensionSizes &dimensionSizes)
Init dimension sizes.
Definition: RealMatrix.cpp:152
TMatrixName
const std::string TMatrixName
Datatype for matrix names.
Definition: MatrixNames.h:45
TRealMatrix::CHUNK_SIZE_1D_1MB
static const size_t CHUNK_SIZE_1D_1MB
Number of elements to get 1MB block of data.
Definition: RealMatrix.h:99
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
TRealMatrix::~TRealMatrix
virtual ~TRealMatrix()
Destructor.
Definition: RealMatrix.cpp:60
Logger.h
The header file containing a class responsible for printing out info and error messages (stdout...
TRealMatrix::CHUNK_SIZE_1D_4MB
static const size_t CHUNK_SIZE_1D_4MB
Number of elements to get 4MB block of data.
Definition: RealMatrix.h:97
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
TBaseFloatMatrix::nElements
size_t nElements
Total number of elements.
Definition: BaseFloatMatrix.h:125
ComplexMatrix.h
The header file with the class for complex matrices.
ERR_FMT_MATRIX_NOT_REAL
TErrorMessage ERR_FMT_MATRIX_NOT_REAL
Matrix class error message.
Definition: ErrorMessages.h:129
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
TBaseFloatMatrix::dataRowSize
size_t dataRowSize
Size of a 1D row in X dimension.
Definition: BaseFloatMatrix.h:133
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
TDimensionSizes::nz
size_t nz
number of elements in the z direction
Definition: DimensionSizes.h:149
TRealMatrix::ReadDataFromHDF5File
virtual void ReadDataFromHDF5File(THDF5_File &file, TMatrixName &matrixName)
Read data from the HDF5 file - only from the root group.
Definition: RealMatrix.cpp:74
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
TBaseFloatMatrix::dataSlabSize
size_t dataSlabSize
Size of a 2D slab.
Definition: BaseFloatMatrix.h:135
THDF5_File::CloseDataset
void CloseDataset(const hid_t dataset)
Close the HDF5 dataset.
Definition: HDF5_File.cpp:457
TRealMatrix::TRealMatrix
TRealMatrix()
Default constructor is not allowed for public.
Definition: RealMatrix.h:85
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