Debian Codegraph @

src/aslUtilities.h

raw
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
24 /// \file aslUtilities.h useful common utilities
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
43 /// Advanced Simulation Library
44 namespace asl
45 {
46
47 /// Converts numbers or another type to string \ingroup Utilities
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
56 /// Converts numbers or another type to string with given value of positions with 0 before \ingroup Utilities
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
65 /// Converts string to number, exits if not able to convert
66 template <typename T> T strToNum(std::string s);
67
68 /// This class is used in order to overload function according to an integer parameter
69 template <int I>class I2T{public: static const int i = I;};
70
71 /// Realization of \f$ a^I \f$
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
76 /// \cond NEVER
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;}
80 /// \endcond
81
82
83 /// Checks the belonging to a closed interval [x1,x2], \f$ xx \in [x1,x2] \f$
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
89 /// Checks the belonging to an open interval (x1,x2), \f$ xx \in (x1,x2) \f$
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.); ///<\f$\pi\f$
96
97 /// Translation radians to degrees
98 inline const double deg(double a){return a/pi*180.;}
99 inline const float deg(float a){return a/pi*180.;}
100 /// Translation degrees to radians
101 inline const double rad(double a){return a*pi/180.;}
102 inline const float rad(float a){return a*pi/180.;}
103
104 /// Approximately equal; the precision is defined as \p p_
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
116 /// Prints errorMessage and exits depending on the status \ingroup ErrorMessaging
117 void errorMessage(cl_int status, const char *errorMessage);
118
119 /// Prints errorMessage and exits depending on the status \ingroup ErrorMessaging
120 void errorMessage(cl_int status, const std::string & errorMessage);
121
122 /// Prints " Ok" or " ERROR" depending on status \ingroup ErrorMessaging
123 void errorMessage(bool status);
124
125 /// Prints errorMessage and exits \ingroup ErrorMessaging
126 void errorMessage(const char *errorMessage);
127
128 /// Prints errorMessage and exits \ingroup ErrorMessaging
129 void errorMessage(const std::string & errorMessage);
130
131
132 /// Prints warningMessage \ingroup ErrorMessaging
133 void warningMessage(const char *warningMessage);
134
135
136 /// Prints warningMessage \ingroup ErrorMessaging
137 void warningMessage(const std::string & warningMessage);
138
139
140 /// Returns warningMessage \ingroup ErrorMessaging
141 std::string warningString(const char *warningMessage);
142
143
144 /// Prints elements of the vector separated by space
145 template <typename T> std::ostream & operator<<(std::ostream & output,
146 const std::vector<T> & vector);
147
148
149 /// Compares two vectors
150 template <typename T> bool operator==(const std::vector<T> & vector1,
151 const std::vector<T> & vector2);
152
153
154 /// Makes output 1 of \p n times
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
163 /// sorts two vectors with respect of the fist one
164 template <class T1, class T2> inline void sortTwoVectors(std::vector<T1> & v1, std::vector<T2> & v2);
165
166 /// reorders vector according to indeces
167 template <class T> inline void reorderVector(std::vector<unsigned int> & ind, std::vector<T> & v);
168
169
170 //---------------------------- Implementations ----------------------------
171
172 /// Prints elements of the vector separated by space
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
184 /// compares two vectors
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);
244 reorderVector(ind,v2);
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