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
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 
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  */
51 {
52  InitDimensions(dimensionSizes);
54 }// end of TRealMatrix
55 //--------------------------------------------------------------------------------------------------
56 
57 /**
58  * Destructor.
59  */
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  */
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  */
103  TMatrixName& matrixName,
104  const size_t compressionLevel)
105 {
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,
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 
159 
160  dataRowSize = dimensionSizes.nx;
161  dataSlabSize = dimensionSizes.nx * dimensionSizes.ny;
162 }// end of SetDimensions
163 //-------------------------------------------------------------------------------------------------/
164 
165 //------------------------------------------------------------------------------------------------//
166 //--------------------------------------- Private methods ----------------------------------------//
167 //------------------------------------------------------------------------------------------------//
168 
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 FreeMemory()
Memory allocation (both on CPU and GPU).
virtual void AllocateMemory()
Memory allocation (both on CPU and GPU).
The header file containing the class for real matrices.
struct TDimensionSizes dimensionSizes
Dimension sizes.
static const size_t CHUNK_SIZE_1D_256KB
Number of elements to get 256KB block of data.
Definition: RealMatrix.h:101
THDF5_File::TMatrixDataType ReadMatrixDataType(const hid_t parentGroup, TMatrixName &datasetName)
Read matrix data type from the dataset.
Definition: HDF5_File.cpp:1068
virtual void WriteDataToHDF5File(THDF5_File &file, TMatrixName &matrixName, const size_t compressionLevel)
Write data into the HDF5 file.
Definition: RealMatrix.cpp:102
TErrorMessage ERR_FMT_MATRIX_NOT_FLOAT
Matrix class error message.
size_t nAllocatedElements
Total number of allocated elements (in terms of floats).
virtual void InitDimensions(const TDimensionSizes &dimensionSizes)
Init dimension sizes.
Definition: RealMatrix.cpp:152
const std::string TMatrixName
Datatype for matrix names.
Definition: MatrixNames.h:45
static const size_t CHUNK_SIZE_1D_1MB
Number of elements to get 1MB block of data.
Definition: RealMatrix.h:99
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
virtual ~TRealMatrix()
Destructor.
Definition: RealMatrix.cpp:60
The header file containing a class responsible for printing out info and error messages (stdout...
static const size_t CHUNK_SIZE_1D_4MB
Number of elements to get 4MB block of data.
Definition: RealMatrix.h:97
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.
TErrorMessage ERR_FMT_MATRIX_NOT_REAL
Matrix class error message.
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
virtual void ReadDataFromHDF5File(THDF5_File &file, TMatrixName &matrixName)
Read data from the HDF5 file - only from the root group.
Definition: RealMatrix.cpp:74
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
TRealMatrix()
Default constructor is not allowed for public.
Definition: RealMatrix.h:85
Class wrapping the HDF5 routines.
Definition: HDF5_File.h:500
Structure with 4D dimension sizes (3 in space and 1 in time).