kspaceFirstOrder3D-OMP  1.2
The C++ implementation of the k-wave toolbox for the time-domain simulation of acoustic wave fields in 3D
BaseFloatMatrix.cpp
Go to the documentation of this file.
1 /**
2  * @file BaseFloatMatrix.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 base class for single precisions floating point numbers (floats).
10  *
11  * @version kspaceFirstOrder3D 2.16
12  *
13  * @date 11 July 2011, 12:13 (created) \n
14  * 04 September 2017, 15:03 (revised)
15  *
16  * @copyright Copyright (C) 2017 Jiri Jaros and Bradley Treeby.
17  *
18  * This file is part of the C++ extension of the [k-Wave Toolbox](http://www.k-wave.org).
19  *
20  * This file is part of the k-Wave. k-Wave is free software: you can redistribute it and/or modify it under the terms
21  * of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the
22  * 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 the implied
25  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
26  * 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/](http://www.gnu.org/licenses/).
30  */
31 
32 
33 
34 #include <string.h>
35 #include <immintrin.h>
36 #include <assert.h>
37 
39 
40 #include <Utils/DimensionSizes.h>
41 
42 using std::string;
43 
44 //--------------------------------------------------------------------------------------------------------------------//
45 //---------------------------------------------------- Constants -----------------------------------------------------//
46 //--------------------------------------------------------------------------------------------------------------------//
47 
48 
49 //--------------------------------------------------------------------------------------------------------------------//
50 //------------------------------------------------- Public methods ---------------------------------------------------//
51 //--------------------------------------------------------------------------------------------------------------------//
52 
53 /**
54  * Default constructor.
55  */
57  : BaseMatrix(),
58  mSize(0), mCapacity(0),
59  mDimensionSizes(),
60  mRowSize(0), mSlabSize(0),
61  mData(nullptr)
62 {
63 
64 }// end of BaseFloatMatrix
65 //----------------------------------------------------------------------------------------------------------------------
66 
67 /**
68  * Copy data from another matrix with same size.
69  */
71 {
72  const float* srcData = src.getData();
73 
74  #pragma omp parallel for simd schedule(static) firstprivate(srcData)
75  for (size_t i = 0; i < mCapacity; i++)
76  mData[i] = srcData[i];
77 }// end of copyData
78 //----------------------------------------------------------------------------------------------------------------------
79 
80 /**
81  * Zero all allocated elements in parallel for NUMA first touch.
82  */
84 {
85  #pragma omp parallel for simd schedule(static)
86  for (size_t i = 0; i < mCapacity; i++)
87  {
88  mData[i] = 0.0f;
89  }
90 }// end of zeroMatrix
91 //----------------------------------------------------------------------------------------------------------------------
92 
93 /**
94  * Calculate matrix = scalar / matrix.
95  */
96 void BaseFloatMatrix::scalarDividedBy(const float scalar)
97 {
98  #pragma omp parallel for simd schedule(static) firstprivate(scalar)
99  for (size_t i = 0; i < mCapacity; i++)
100  {
101  mData[i] = scalar / mData[i];
102  }
103 }// end of scalarDividedBy
104 //----------------------------------------------------------------------------------------------------------------------
105 
106 
107 //--------------------------------------------------------------------------------------------------------------------//
108 //------------------------------------------------ Protected methods -------------------------------------------------//
109 //--------------------------------------------------------------------------------------------------------------------//
110 
111 /**
112  * Memory allocation based on the capacity and aligned at kDataAlignment
113  */
115 {
116  // No memory allocated before this function
117  assert(mData == nullptr);
118 
119  mData = (float*) _mm_malloc(mCapacity * sizeof (float), kDataAlignment);
120 
121  if (!mData)
122  {
123  throw std::bad_alloc();
124  }
125 
126  zeroMatrix();
127 }// end of allocateMemory
128 //----------------------------------------------------------------------------------------------------------------------
129 
130 /**
131  * Free memory.
132  */
134  {
135  if (mData) _mm_free(mData);
136 
137  mData = nullptr;
138 }// end of freeMemory
139 //----------------------------------------------------------------------------------------------------------------------
140 
141 //--------------------------------------------------------------------------------------------------------------------//
142 //------------------------------------------------- Private methods --------------------------------------------------//
143 //--------------------------------------------------------------------------------------------------------------------//
virtual void allocateMemory()
Aligned memory allocation (both on CPU and GPU).
BaseFloatMatrix()
Default constructor.
constexpr int kDataAlignment
memory alignment for SSE, SSE2, SSE3, SSE4 (16B)
float * mData
Raw matrix data.
Abstract base class. The common ancestor defining the common interface and allowing derived classes t...
Definition: BaseMatrix.h:48
virtual void copyData(const BaseFloatMatrix &src)
Copy data from other matrix with the same size.
The header file containing the structure with 3D dimension sizes.
virtual void freeMemory()
Memory allocation (both on CPU and GPU).
virtual float * getData()
Get raw data out of the class (for direct kernel access).
The header file containing the base class for single precisions floating point numbers (floats)...
virtual void scalarDividedBy(const float scalar)
Calculate matrix = scalar / matrix.
virtual void zeroMatrix()
Zero all elements of the matrix (NUMA first touch).
size_t mCapacity
Total number of allocated elements (in terms of floats).
Abstract base class for float based matrices defining basic interface. Higher dimensional matrices st...