Initial public release.
[OpenCLIPER] / include / OpenCLIPER / OpenCLIPERDataModelCommonDefs.hpp
1 /* Copyright (C) 2018 Federico Simmross Wattenberg,
2  *                    Manuel Rodríguez Cayetano,
3  *                    Javier Royuela del Val,
4  *                    Elena Martín González,
5  *                    Elisa Moya Sáez,
6  *                    Marcos Martín Fernández and
7  *                    Carlos Alberola López
8  *
9  * This file is part of OpenCLIPER.
10  *
11  * OpenCLIPER is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; version 3 of the License.
14  *
15  * OpenCLIPER is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with OpenCLIPER; If not, see <http://www.gnu.org/licenses/>.
22  *
23  *
24  *  Contact:
25  *
26  *  Federico Simmross Wattenberg
27  *  E.T.S.I. Telecomunicación
28  *  Universidad de Valladolid
29  *  Paseo de Belén 15
30  *  47011 Valladolid, Spain.
31  *  fedsim@tel.uva.es
32  */
33 /**
34  * @file 
35  * @brief File with macros and type definitions used by several classes
36  */
37 #ifndef INCLUDE_OPENCLIPER_LPIDATAMODELCOMMONDEFS_HPP_
38 #define INCLUDE_OPENCLIPER_LPIDATAMODELCOMMONDEFS_HPP_
39 #include <OpenCLIPER/OpenCLIPERDataModelConfig.hpp>
40
41
42 #ifdef __cplusplus // OpenCL kernels may include this header and they only support C language syntax
43 #include <complex>
44 #include <vector>
45 #include <exception>
46 #include <stdexcept>    //g++ 4.8 doesn't seem to include <stdexcept> from <exception>
47 #include <memory> // for smart pointers
48 #include <chrono> // Execution times measurement
49 #include <stdint.h>
50 // Needed by libmatio, library for reading matlab data files (.mat)
51 #include <matio.h>
52 #include <typeindex>
53
54 #endif // __cplusplus
55
56 #ifdef DOUBLE_PREC
57 /// Type for storing float point values
58 #define realType double
59 #else
60 /// Type for storing float point values
61 #define realType float
62 #endif
63
64 #ifdef DOUBLE_PREC
65     #define IMAGEELELEMENTTYPE double4
66 #else
67     /// Data type for an OpenCL image element
68     #define IMAGEELEMENTTYPE float4
69 #endif
70
71 /// Size of data type used for OpenCL vector operations (in number of elements)
72 #define VECTORDATATYPESIZE 16
73 /*
74 #if defined(__APPLE__) || defined(__MACOSX)
75 #include<OpenCL/cl_platform.h>
76 #else
77 #include<CL/cl_platform.h>
78 #endif
79 */
80
81 /// Position of width value in dimensions vector
82 #define WIDTHPOS 0
83 /// Position of height value in dimensions vector
84 #define HEIGHTPOS 1
85 /// Position of depth value in dimensions vector
86 #define DEPTHPOS 2
87 /// Macro for getting width of an ndarray object (returns 1 if that dimension is not defined)
88 #define NDARRAYWIDTH(ndarray) (((ndarray)->getNDims() > WIDTHPOS) ? ((ndarray)->getDims()->at(WIDTHPOS)) : (1))
89 /// Macro for getting height of an ndarray object (returns 1 if that dimension is not defined)
90 #define NDARRAYHEIGHT(ndarray) (((ndarray)->getNDims() > HEIGHTPOS) ? ((ndarray)->getDims()->at(HEIGHTPOS)) : (1))
91 /// Macro for getting depth of an ndarray object (returns 1 if that dimension is not defined)
92 #define NDARRAYDEPTH(ndarray) (((ndarray)->getNDims() > DEPTHPOS) ? ((ndarray)->getDims()->at(DEPTHPOS)) : (1))
93 /// Macro for getting first NDArray from a Data object
94 #define PNDARRAY0 getData()->at(0)
95
96 /// Type of array with Data dimensions information
97 typedef uint* dimsInfo_t;
98
99 /// Type of array with Data stride dimensions information
100 typedef uint* stridesInfo_t;
101
102 /// Minimum number of positions of dimensions info array
103 #define NUMINITIALPOSITIONSDIMSINFO 4
104 /// Known positions for array storing data dimensions (spatial, temporal and number of coils)
105 enum knownDimPos {NumSpatialDimsPos=0, AllSizesEqualPos=1, NumCoilsPos=2, NumTemporalDimsPos=3, FirstTemporalDimPos=4};
106 /// Known positions for array storing data strides (spatial, temporal and number of coils)
107 // Note: stride position for element and column are the same thing (sure!)
108 enum knownStridePos {ElementStridePos=0, FirstSpatialStridePos=0};
109
110 /// Known positions for spatial dimensions in array storing data dimensions
111 enum knownSpatialDimPos{COLUMNS=WIDTHPOS, ROWS=HEIGHTPOS, SLICES=DEPTHPOS}; 
112
113 #ifdef __cplusplus // OpenCL kernels may include this header and they only support C language syntax
114 /// Type used for storing complex values
115 #define complexType std::complex<realType>
116 /// Set type
117 #define set std::vector<bool>
118
119 using namespace std; // for using vector instead of std::vector everywhere
120
121 /// String with prefix for error messages (the name of the namespace)
122 #define ERRORNAMESPACEPREFIX "OpenCLIPER::"
123 /// Name of OpenCL kernels base dir based on project base directory from CMakeLists.txt
124 #define KERNEL_SOURCE_DIR PROJECT_SOURCE_DIR "/src/kernels"
125 /// Type for specifying fragments of images
126 #define FragmentSpecif uint8_t
127
128 /// Handle for a process bound to an OpenCL context
129 typedef unsigned int ProcessHandle;
130 /// Value for an invalid process handle
131 #define INVALIDPROCESSHANDLE 0
132 /// Value for first valid process handle
133 #define FIRSTVALIDPROCESSHANDLE (INVALIDPROCESSHANDLE+1)
134
135 /// Handle for a Data object bound to an OpenCL context
136 typedef unsigned int DataHandle;
137 /// Value for an invalid data handle
138 #define INVALIDDATAHANDLE 0
139 /// Value for first valid data handle
140 #define FIRSTVALIDDATAHANDLE (INVALIDDATAHANDLE+1)
141
142 /// data type for variables storing number of coils or coils index
143 typedef uint32_t numCoilsType;
144
145 /// data type for variables storing a 1-dimensional index calculated from indexes of several dimensions
146 typedef uint32_t index1DType;
147
148 /// data type for variables storing an index from a several dimensions image
149 typedef uint32_t dimIndexType;
150
151 /// data type for variables storing the number of dimensions of an image
152 typedef uint32_t numberOfDimensionsType;
153
154 #endif // __cplusplus
155
156 //typedef cl_uint indexType;
157
158 /// Macro for calculation of modulus of a complex number
159 #define MOD(complex) sqrt(pow((complex).real(), 2) + pow((complex).imag(), 2))
160 /// Macro for calculation of modulus of a real number
161 #define MODREALS(real1, real2) sqrt(pow((real1), 2) + pow((real2), 2))
162 /// Format used for storing complex numbers as image data with two color channels (one channel for the real part and the other for the imaginary part)
163 #define OPENCLCHANNELFORMAT CL_RG
164
165 /// Macro for declaring bT (begin time) variable and storing current time in it
166 #define BEGIN_TIME(bT) std::chrono::high_resolution_clock::time_point bT = \
167                                       std::chrono::high_resolution_clock::now();
168 /// Macro for declaring eT (end time) variable and storing current time in it
169 #define END_TIME(eT) std::chrono::high_resolution_clock::time_point eT = \
170                                       std::chrono::high_resolution_clock::now();
171 /// Data type for a number storing the difference between two time values
172 #define TIME_DIFF_TYPE double
173 /// Macro for calculation of time difference betweent eT and bT and storing result in diffT
174 #define TIME_DIFF(diffT, bT,eT) diffT = \
175 (std::chrono::duration_cast<std::chrono::nanoseconds>(eT - bT).count()) / 1e9
176
177 #ifdef __cplusplus
178 /// Main namespace of the OpenCLIPER framework
179 namespace OpenCLIPER {
180 /*
181 /// Struct for storing messages related to performance analysis and debugging
182 struct InfoItem {
183         /// title for the info element
184         std::string name;
185         /// value for the info element
186         std::string value;
187 };
188
189 /// Type storing a list of infoItem elements
190 typedef std::vector<InfoItem> InfoItems;
191 */
192 /** 
193   * @brief Enumerated type for device memory formats
194   *
195   * Enumerated type for describing device memory formats for storing data. Used by 
196   * host2device method to get format of data to store in device memory and by 
197   * device2host method to get format of data to be read from device memory.
198   */
199 enum class SyncSource {
200     /// Both buffer and image formats used (not valid for device2host method)
201     ALL, 
202     /// Buffer format used
203     BUFFER_ONLY,
204     /// Image format used
205     IMAGE_ONLY 
206 };
207
208 /// Default source of data (buffers, images, etc.)
209 #define SYNCSOURCEDEFAULT SyncSource::BUFFER_ONLY
210
211 }
212
213 /// Macro for printing a vector <i>vector</i> of type <i>baseType</i> prefixed by title <i>title</i>
214 #define PRINTVECTOR(labelText, vector, baseType) CERR((labelText) << ": ");\
215         std::copy((vector).begin(), (vector).end(), std::ostream_iterator<baseType>(std::cerr, " ")); CERR(std::endl);
216 /// Macro for printing an array <i>array</i> of size <i>size</i> prefixed by title <i>title</i>
217 #define PRINTARRAY(labelText, array, size) CERR(labelText << ": "); for (uint i = 0; i < (size); i++)  CERR((array)[i] << " ");
218
219 /// @brief Data type of NDArray elements
220 typedef std::type_index ElementDataType;
221 #endif // __cplusplus
222
223 /// @brief Name of file with common (kernel/host) utility functions
224 #define HOSTKERNELFUNCTIONSFILE "hostKernelFunctions.cl"
225
226 #endif // INCLUDE_OPENCLIPER_LPIDATAMODELCOMMONDEFS_HPP_