| 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
ComplexMatrix.cpp
Go to the documentation of this file.
1 /**
2 * @file ComplexMatrix.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 with the class for complex matrices.
10 *
11 * @version kspaceFirstOrder3D 3.4
12 *
13 * @date 11 July 2011, 14:02 (created) \n
14 * 29 July 2016, 16:53 (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 <iostream>
33
34 #include <MatrixClasses/ComplexMatrix.h>
35 #include <Logger/Logger.h>
36
37 //------------------------------------------------------------------------------------------------//
38 //------------------------------------------ Constants -------------------------------------------//
39 //------------------------------------------------------------------------------------------------//
40
41
42 //------------------------------------------------------------------------------------------------//
43 //--------------------------------------- Public methods -----------------------------------------//
44 //------------------------------------------------------------------------------------------------//
45
46 /**
47 * Constructor.
48 * @param [in] dimensionSizes - Dimension sizes of the matrix
49 */
50
51 TComplexMatrix::TComplexMatrix(const TDimensionSizes& dimensionSizes)
52 : TBaseFloatMatrix()
53 {
54 InitDimensions(dimensionSizes);
55 AllocateMemory();
56 } // end of TComplexMatrixData
57 //--------------------------------------------------------------------------------------------------
58
59 /**
60 * Destructor.
61 */
62 TComplexMatrix::~TComplexMatrix()
63 {
64 FreeMemory();
65 }// end of TComplexMatrix
66 //--------------------------------------------------------------------------------------------------
67
68
69 /**
70 * Read data from HDF5 file (do some basic checks). Only from the root group.
71 *
72 * @param [in] file - HDF5 file
73 * @param [in] matrixName - HDF5 dataset name
74 *
75 * * @throw ios::failure when there is a problem
76 */
77 void TComplexMatrix::ReadDataFromHDF5File(THDF5_File& file,
78 TMatrixName& matrixName)
79 {
80 // check data type
81 if (file.ReadMatrixDataType(file.GetRootGroup(), matrixName) != THDF5_File::FLOAT)
82 {
83 throw std::ios::failure(TLogger::FormatMessage(ERR_FMT_MATRIX_NOT_FLOAT, matrixName.c_str()));
84 }
85
86 // check domain type
87 if (file.ReadMatrixDomainType(file.GetRootGroup(), matrixName) != THDF5_File::COMPLEX)
88 {
89 throw std::ios::failure(TLogger::FormatMessage(ERR_FMT_MATRIX_NOT_COMPLEX, matrixName.c_str()));
90 }
91
92 // Initialise dimensions
93 TDimensionSizes complexDims = dimensionSizes;
94 complexDims.nx = 2 * complexDims.nx;
95
96 // Read data from the file
97 file.ReadCompleteDataset(file.GetRootGroup(), matrixName, complexDims, hostData);
98 }// end of LoadDataFromMatlabFile
99 //--------------------------------------------------------------------------------------------------
100
101 /**
102 * Write data to HDF5 file (only from the root group).
103 *
104 * @param [in] file - HDF5 file handle
105 * @param [in] matrixName - HDF5 dataset name
106 * @param [in] compressionLevel - Compression level for the dataset
107 *
108 * @throw ios::failure an exception what the operation fails
109 */
110 void TComplexMatrix::WriteDataToHDF5File(THDF5_File& file,
111 TMatrixName& matrixName,
112 const size_t compressionLevel)
113 {
114 // set dimensions and chunks
115 TDimensionSizes complexDims = dimensionSizes;
116 complexDims.nx = 2 * complexDims.nx;
117
118 TDimensionSizes chunks = complexDims;
119 complexDims.nz = 1;
120
121 // create a dataset
122 hid_t dataset = file.CreateFloatDataset(file.GetRootGroup(),
123 matrixName,
124 complexDims,
125 chunks,
126 compressionLevel);
127 // Write write the matrix at once.
128 file.WriteHyperSlab(dataset, TDimensionSizes(0, 0, 0), dimensionSizes, hostData);
129 file.CloseDataset(dataset);
130
131 // Write data and domain type
132 file.WriteMatrixDataType(file.GetRootGroup() , matrixName, THDF5_File::FLOAT);
133 file.WriteMatrixDomainType(file.GetRootGroup(), matrixName, THDF5_File::COMPLEX);
134 }// end of WriteDataToHDF5File
135 //--------------------------------------------------------------------------------------------------
136
137
138 //------------------------------------------------------------------------------------------------//
139 //-------------------------------------- Protected methods ---------------------------------------//
140 //------------------------------------------------------------------------------------------------//
141
142 /**
143 * Initialize matrix dimension sizes.
144 *
145 * @param [in] dimensionSizes - Dimension sizes of the matrix
146 */
147 void TComplexMatrix::InitDimensions(const TDimensionSizes& dimensionSizes)
148 {
149
150 this->dimensionSizes = dimensionSizes;
151
152 nElements = dimensionSizes.nx * dimensionSizes.ny * dimensionSizes.nz;
153
154 dataRowSize = 2 * dimensionSizes.nx;
155 dataSlabSize = 2 * dimensionSizes.nx * dimensionSizes.ny;
156 // compute actual necessary memory sizes
157 nAllocatedElements = 2 * nElements;
158
159 }// end of InitDimensions
160 //--------------------------------------------------------------------------------------------------
161
162
163 //------------------------------------------------------------------------------------------------//
164 //--------------------------------------- Private methods ----------------------------------------//
165 //------------------------------------------------------------------------------------------------//
166
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
TComplexMatrix::ReadDataFromHDF5File
virtual void ReadDataFromHDF5File(THDF5_File &file, TMatrixName &matrixName)
Load data from the HDF5_File.
Definition: ComplexMatrix.cpp:77
TBaseFloatMatrix::FreeMemory
virtual void FreeMemory()
Memory allocation (both on CPU and GPU).
Definition: BaseFloatMatrix.cpp:162
TComplexMatrix::~TComplexMatrix
virtual ~TComplexMatrix()
Destructor.
Definition: ComplexMatrix.cpp:62
TBaseFloatMatrix::AllocateMemory
virtual void AllocateMemory()
Memory allocation (both on CPU and GPU).
Definition: BaseFloatMatrix.cpp:133
TBaseFloatMatrix::dimensionSizes
struct TDimensionSizes dimensionSizes
Dimension sizes.
Definition: BaseFloatMatrix.h:130
TComplexMatrix::InitDimensions
virtual void InitDimensions(const TDimensionSizes &dimensionSizes)
Initialize dimension sizes and related structures.
Definition: ComplexMatrix.cpp:147
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
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
TComplexMatrix::WriteDataToHDF5File
virtual void WriteDataToHDF5File(THDF5_File &file, TMatrixName &matrixName, const size_t compressionLevel)
Write data into the HDF5_File.
Definition: ComplexMatrix.cpp:110
TMatrixName
const std::string TMatrixName
Datatype for matrix names.
Definition: MatrixNames.h:45
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
Logger.h
The header file containing a class responsible for printing out info and error messages (stdout...
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.
TComplexMatrix::TComplexMatrix
TComplexMatrix()
Default constructor not allowed for public.
Definition: ComplexMatrix.h:97
ERR_FMT_MATRIX_NOT_COMPLEX
TErrorMessage ERR_FMT_MATRIX_NOT_COMPLEX
Matrix class error message.
Definition: ErrorMessages.h:132
TDimensionSizes::TDimensionSizes
TDimensionSizes()
Default constructor.
Definition: DimensionSizes.h:69
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
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
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