kspaceFirstOrder3D-CUDA  1.1
The CUDA/C++ implementation of the k-wave toolbox for the time-domain simulation of acoustic wave fields in 3D
 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 
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 
53 {
54  InitDimensions(dimensionSizes);
56 } // end of TComplexMatrixData
57 //--------------------------------------------------------------------------------------------------
58 
59 /**
60  * Destructor.
61  */
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  */
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  */
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  */
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
158 
159 }// end of InitDimensions
160 //--------------------------------------------------------------------------------------------------
161 
162 
163 //------------------------------------------------------------------------------------------------//
164 //--------------------------------------- Private methods ----------------------------------------//
165 //------------------------------------------------------------------------------------------------//
166 
size_t nx
number of elements in the x direction
Abstract base class for float based matrices defining basic interface. Higher dimensional matrices st...
float * hostData
Raw CPU matrix data.
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
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
virtual void ReadDataFromHDF5File(THDF5_File &file, TMatrixName &matrixName)
Load data from the HDF5_File.
virtual void FreeMemory()
Memory allocation (both on CPU and GPU).
virtual ~TComplexMatrix()
Destructor.
virtual void AllocateMemory()
Memory allocation (both on CPU and GPU).
struct TDimensionSizes dimensionSizes
Dimension sizes.
virtual void InitDimensions(const TDimensionSizes &dimensionSizes)
Initialize dimension sizes and related structures.
THDF5_File::TMatrixDataType ReadMatrixDataType(const hid_t parentGroup, TMatrixName &datasetName)
Read matrix data type from the dataset.
Definition: HDF5_File.cpp:1068
TErrorMessage ERR_FMT_MATRIX_NOT_FLOAT
Matrix class error message.
size_t nAllocatedElements
Total number of allocated elements (in terms of floats).
virtual void WriteDataToHDF5File(THDF5_File &file, TMatrixName &matrixName, const size_t compressionLevel)
Write data into the HDF5_File.
const std::string TMatrixName
Datatype for matrix names.
Definition: MatrixNames.h:45
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
The header file containing a class responsible for printing out info and error messages (stdout...
hid_t GetRootGroup() const
Get handle to the root group.
Definition: HDF5_File.h:573
size_t ny
number of elements in the y direction
size_t nElements
Total number of elements.
The header file with the class for complex matrices.
TComplexMatrix()
Default constructor not allowed for public.
Definition: ComplexMatrix.h:97
TErrorMessage ERR_FMT_MATRIX_NOT_COMPLEX
Matrix class error message.
TDimensionSizes()
Default constructor.
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::TMatrixDomainType ReadMatrixDomainType(const hid_t parentGroup, TMatrixName &datasetName)
Read matrix domain type from the dataset under a specified group.
Definition: HDF5_File.cpp:1101
size_t dataRowSize
Size of a 1D row in X dimension.
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
size_t nz
number of elements in the z direction
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
size_t dataSlabSize
Size of a 2D slab.
void CloseDataset(const hid_t dataset)
Close the HDF5 dataset.
Definition: HDF5_File.cpp:457
Class wrapping the HDF5 routines.
Definition: HDF5_File.h:500
Structure with 4D dimension sizes (3 in space and 1 in time).