ASL 0.1.7
Advanced Simulation Library
Loading...
Searching...
No Matches
aslUtilities.h
Go to the documentation of this file.
1/*
2 * Advanced Simulation Library <http://asl.org.il>
3 *
4 * Copyright 2015 Avtech Scientific <http://avtechscientific.com>
5 *
6 *
7 * This file is part of Advanced Simulation Library (ASL).
8 *
9 * ASL is free software: you can redistribute it and/or modify it
10 * under the terms of the GNU Affero General Public License as
11 * published by the Free Software Foundation, version 3 of the License.
12 *
13 * ASL is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with ASL. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23
25
26#ifndef ASLUTILITIES_H
27#define ASLUTILITIES_H
28
29//#include <CL/cl.hpp>
30// Supply "cl.hpp" with ASL, since it is not present in OpenCL 2.0
31// Remove the file after switching to OpenCL 2.1
32#include "acl/cl.hpp"
33#include "acl/aclStdIncludes.h"
34#include <cmath>
35#include <iostream>
36#include <sstream>
37#include <iomanip>
38#include <vector>
39#include <memory>
40#include <list>
41#include <typeinfo>
42
44namespace asl
45{
46
48 template <typename T> inline std::string numToStr(T i)
49 {
50 std::stringstream s;
51 s << i;
52 return s.str();
53 }
54
55
57 template <typename T> std::string numToStr(T i, int numberOf0)
58 {
59 std::stringstream s;
60 s << std::setfill('0') << std::setw(numberOf0) << i;
61 return s.str();
62 }
63
64
66 template <typename T> T strToNum(std::string s);
67
69 template <int I>class I2T{public: static const int i = I;};
70
72 template <int I> inline const double P(const double& a){return P<I-1>(a)*a;}
73 template <int I> inline const float P(const float& a){return P<I-1>(a)*a;}
74 template <int I> inline const int P(const int& a){return P<I-1>(a)*a;}
75
77 template <> inline const double P<1>(const double& a){return a;}
78 template <> inline const float P<1>(const float& a){return a;}
79 template <> inline const int P<1>(const int& a){return a;}
81
82
84 template <typename T> inline bool in(const T& xx, const T& x1, const T& x2)
85 {
86 return (((x1-xx)*(x2-xx))<=0);
87 }
88
90 template <typename T> inline bool inO(const T& xx, const T& x1, const T& x2)
91 {
92 return (((x1 - xx) * (x2 - xx)) < 0);
93 }
94
95 static const double pi=4.*atan(1.);
96
98 inline const double deg(double a){return a/pi*180.;}
99 inline const float deg(float a){return a/pi*180.;}
101 inline const double rad(double a){return a*pi/180.;}
102 inline const float rad(float a){return a*pi/180.;}
103
105 inline const bool approxEqual(const double &a, const double &b, const double p_ = 1e-6)
106 {
107 return fabs(a-b) < p_ * fabs(a) && fabs(a-b) < p_ * fabs(b);
108 }
109
110 inline const bool approxEqual(const float &a, const float &b, const float p_ = 1e-6)
111 {
112 return fabs(a - b) < p_ * fabs(a) && fabs(a - b) < p_ * fabs(b);
113 }
114
115
118
120 void errorMessage(cl_int status, const std::string & errorMessage);
121
124
126 void errorMessage(const char *errorMessage);
127
129 void errorMessage(const std::string & errorMessage);
130
131
134
135
137 void warningMessage(const std::string & warningMessage);
138
139
141 std::string warningString(const char *warningMessage);
142
143
145 template <typename T> std::ostream & operator<<(std::ostream & output,
146 const std::vector<T> & vector);
147
148
150 template <typename T> bool operator==(const std::vector<T> & vector1,
151 const std::vector<T> & vector2);
152
153
155 template <class T, int N> inline void output1OfN(const std::string &s);
156
157
158 template <class T>inline void setupAll(std::vector<std::shared_ptr<T> > & v);
159
160
161 template <class T>inline void computeAll(std::vector<std::shared_ptr<T> > & v);
162
164 template <class T1, class T2> inline void sortTwoVectors(std::vector<T1> & v1, std::vector<T2> & v2);
165
167 template <class T> inline void reorderVector(std::vector<unsigned int> & ind, std::vector<T> & v);
168
169
170//---------------------------- Implementations ----------------------------
171
173 template <typename T> std::ostream & operator<<(std::ostream & output, const std::vector<T> & vector)
174 {
175 typename std::vector<T>::const_iterator iter;
176
177 for (iter = vector.begin(); iter != vector.end(); ++iter)
178 output << *iter << " ";
179 output << std::endl;
180 return output;
181 }
182
183
185 template <typename T> bool operator==(const std::vector<T> & vector1,
186 const std::vector<T> & vector2)
187 {
188 if (vector1.size() != vector2.size())
189 return false;
190
191 bool equal = false;
192 unsigned int i = 0;
193
194 do
195 {
196 equal = (vector1[i] == vector2[i]);
197 ++i;
198 } while ((i < vector1.size()) && equal);
199
200 return equal;
201 }
202
203
204 template <class T, int N> inline void output1OfN(const std::string &s)
205 {
206 static int i(0);
207 if (!(i % N))
208 std::cout << s << std::endl;
209 ++i;
210 }
211
212 template <class T> void setupAll(std::vector<std::shared_ptr<T> > &v)
213 {
214 for (unsigned int i(0); i < v.size(); ++i)
215 v[i]->setup();
216 }
217
218 template <class T> void computeAll(std::vector<std::shared_ptr<T> > &v)
219 {
220 for (unsigned int i(0); i < v.size(); ++i)
221 v[i]->compute();
222 }
223
224 template <typename T1> class comparatorIndeces {
225 private:
226 std::vector<T1> & v1;
227 public:
228 comparatorIndeces(std::vector<T1> & v): v1(v){}
229 inline bool operator()(unsigned int i, unsigned int j) {return v1[i]<v1[j];}
230 };
231
232 template <typename T1, typename T2> inline void sortTwoVectors(std::vector<T1> & v1, std::vector<T2> & v2)
233 {
234 if(v1.size()!=v2.size())
235 errorMessage("sortTwoVectors: the vectors have different sizes");
236
237 // creates index vector
238 std::vector<unsigned int> ind(v1.size());
239 for(unsigned int i(0); i<v1.size(); ++i)
240 ind[i]=i;
241
242 sort(ind.begin(), ind.end(), comparatorIndeces<T1>(v1));
243 reorderVector(ind,v1);
245 }
246
247 template <class T> inline void reorderVector(std::vector<unsigned int> & ind, std::vector<T> & v)
248 {
249 if(ind.size()!=v.size())
250 errorMessage("reorderVector: the vectors have different sizes");
251
252 unsigned int n(v.size());
253 std::vector<T> nContainer(n);
254 for(unsigned int i(0); i<n; ++i)
255 nContainer[i]=v[ind[i]];
256 v.swap(nContainer);
257 }
258
259
260} // asl namespace
261
262#endif // ASLUTILITIES_H
C++ bindings for OpenCL 1.0 (rev 48), OpenCL 1.1 (rev 33) and OpenCL 1.2 (rev 15)
This class is used in order to overload function according to an integer parameter.
static const int i
comparatorIndeces(std::vector< T1 > &v)
bool operator()(unsigned int i, unsigned int j)
SPDataWrapperACLData generateDataContainerACL_SP(const Block &b, unsigned int n=1)
generates pointer to ACL Data field with n components
void warningMessage(const char *warningMessage)
Prints warningMessage.
std::string warningString(const char *warningMessage)
Returns warningMessage.
void errorMessage(cl_int status, const char *errorMessage)
Prints errorMessage and exits depending on the status.
std::string numToStr(T i)
Converts numbers or another type to string.
Advanced Simulation Library.
Definition aslDataInc.h:31
void output1OfN(const std::string &s)
Makes output 1 of n times.
const double P(const double &a)
Realization of .
void sortTwoVectors(std::vector< T1 > &v1, std::vector< T2 > &v2)
sorts two vectors with respect of the fist one
bool in(const T &xx, const T &x1, const T &x2)
Checks the belonging to a closed interval [x1,x2], .
std::ostream & operator<<(std::ostream &output, const std::vector< T > &vector)
Prints elements of the vector separated by space.
bool inO(const T &xx, const T &x1, const T &x2)
Checks the belonging to an open interval (x1,x2), .
const double rad(double a)
Translation degrees to radians.
bool operator==(const std::vector< T > &vector1, const std::vector< T > &vector2)
Compares two vectors.
T strToNum(std::string s)
Converts string to number, exits if not able to convert.
const double deg(double a)
Translation radians to degrees.
void reorderVector(std::vector< unsigned int > &ind, std::vector< T > &v)
reorders vector according to indeces
void setupAll(std::vector< std::shared_ptr< T > > &v)
const bool approxEqual(const double &a, const double &b, const double p_=1e-6)
Approximately equal; the precision is defined as p_.
void computeAll(std::vector< std::shared_ptr< T > > &v)