kspaceFirstOrder3D-OMP  1.1
The C++ implementation of the k-wave toolbox for the time-domain simulation of acoustic wave fields in 3D
 All Classes Files Functions Variables Typedefs Enumerations Friends Pages
BaseFloatMatrix.cpp
Go to the documentation of this file.
1 /**
2  * @file BaseFloatMatrix.cpp
3  * @author Jiri Jaros \n
4  * Faculty of Information Technology\n
5  * Brno University of Technology \n
6  * jarosjir@fit.vutbr.cz
7  *
8  * @brief The implementation file containing the base class for
9  * single precisions floating point numbers (floats)
10  *
11  * @version kspaceFirstOrder3D 2.15
12  *
13  * @date 11 July 2011, 12:13 (created) \n
14  * 24 September 2014, 14:54 (revised)
15  *
16  * @section license
17  * This file is part of the C++ extension of the k-Wave Toolbox (http://www.k-wave.org).\n
18  * Copyright (C) 2014 Jiri Jaros and Bradley Treeby
19  *
20  * This file is part of k-Wave. k-Wave is free software: you can redistribute it
21  * and/or modify it under the terms of the GNU Lesser General Public License as
22  * published by the Free Software Foundation, either version 3 of the License,
23  * or (at your option) any later version.
24  *
25  * k-Wave is distributed in the hope that it will be useful, but
26  * WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28  * See the GNU Lesser General Public License for more details.
29  *
30  * You should have received a copy of the GNU Lesser General Public License
31  * along with k-Wave. If not, see <http://www.gnu.org/licenses/>.
32  */
33 
34 
35 
36 #include <string.h>
37 #include <immintrin.h>
38 #include <assert.h>
39 
41 
42 #include <Utils/DimensionSizes.h>
43 #include <Utils/ErrorMessages.h>
44 
45 
46 //----------------------------------------------------------------------------//
47 // Constants //
48 //----------------------------------------------------------------------------//
49 
50 //----------------------------------------------------------------------------//
51 // Definitions //
52 //----------------------------------------------------------------------------//
53 
54 //----------------------------------------------------------------------------//
55 // Implementation //
56 // public methods //
57 //----------------------------------------------------------------------------//
58 
59 
60 /**
61  * Copy data from another matrix with same size.
62  *
63  * @param [in] src - source matrix
64  *
65  */
67 {
68  memcpy(pMatrixData, src.pMatrixData, sizeof(float) * pTotalAllocatedElementCount);
69 }// end of CopyDataSameSize
70 //------------------------------------------------------------------------------
71 
72 
73 /**
74  * Zero all allocated elements in parallel. \n
75  * Also work as the first touch strategy on NUMA machines
76  *
77  */
79 {
80  #pragma omp parallel for schedule (static)
81  for (size_t i=0; i < pTotalAllocatedElementCount; i++)
82  {
83  pMatrixData[i] = 0.0f;
84  }
85 }// end of ZeroMatrix
86 //------------------------------------------------------------------------------
87 
88 
89 /**
90  * Divide a scalar by the elements of matrix.
91  * @param [in] scalar - scalar to be divided
92  *
93  */
94 void TBaseFloatMatrix::ScalarDividedBy(const float scalar)
95 {
96  #pragma omp parallel for schedule (static)
97  for (size_t i=0; i < pTotalAllocatedElementCount; i++)
98  {
99  pMatrixData[i] = scalar / pMatrixData[i];
100  }
101 }// end of ScalarDividedBy
102 //------------------------------------------------------------------------------
103 
104 
105 
106 //----------------------------------------------------------------------------//
107 // Implementation //
108 // protected methods //
109 //----------------------------------------------------------------------------//
110 
111 /**
112  * Memory allocation based on the total number of elements. \n
113  * Memory is aligned by the DATA_ALIGNMENT and all elements are zeroed.
114  */
116 {
117  /* No memory allocated before this function*/
118  assert(pMatrixData == NULL);
119 
120  pMatrixData = (float *) _mm_malloc(pTotalAllocatedElementCount * sizeof (float), DATA_ALIGNMENT);
121 
122  if (!pMatrixData)
123  {
124  fprintf(stderr,Matrix_ERR_FMT_NotEnoughMemory, "TBaseFloatMatrix");
125  throw bad_alloc();
126  }
127 
128  ZeroMatrix();
129 }// end of AllocateMemory
130 //------------------------------------------------------------------------------
131 
132 /**
133  * Free memory.
134  */
136  {
137  if (pMatrixData) _mm_free(pMatrixData);
138 
139  pMatrixData = NULL;
140 }// end of MemoryDealocation
141 //------------------------------------------------------------------------------
142 
143 
144 //----------------------------------------------------------------------------//
145 // Implementation //
146 // private methods //
147 //----------------------------------------------------------------------------//
Abstract base class for float based matrices defining basic interface. Higher dimensional matrices st...
virtual void FreeMemory()
Memory deallocation.
virtual void ZeroMatrix()
Zero all elements of the matrix (NUMA first touch).
virtual void AllocateMemory()
Memory allocation.
size_t pTotalAllocatedElementCount
Total number of allocated elements (in terms of floats).
The header file containing all error messages of the project.
The header file containing the structure with 3D dimension sizes.
virtual void CopyData(const TBaseFloatMatrix &src)
Copy data from other matrix with the same size.
The header file containing the base class for single precisions floating point numbers (floats) ...
const int DATA_ALIGNMENT
memory alignment for SSE, SSE2, SSE3, SSE4 (16B)
virtual void ScalarDividedBy(const float scalar)
Divide scalar/ matrix_element[i].
float * pMatrixData
Raw matrix data.
const char *const Matrix_ERR_FMT_NotEnoughMemory
Matrix class error message.
Definition: ErrorMessages.h:81