Debian Codegraph @

src/acl/acl.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 #ifndef ACL_H
25 #define ACL_H
26
27 #include "aclStdIncludes.h"
28 #include <memory>
29 #include "Kernels/aclKernelConfiguration.h"
30 //#include "aclHardware.h"
31 #include "aclTypes.h"
32
33 namespace cl
34 {
35 class CommandQueue;
36 }
37
38
39 /// Advanced Computational Language
40 namespace acl
41 {
42 class ExpressionContainer;
43 class VectorOfElements;
44 class MatrixOfElements;
45 class MemBlock;
46 class ElementBase;
47 template <class T> class Array;
48 typedef std::shared_ptr<MemBlock> ElementData;
49 typedef std::shared_ptr<ElementBase> Element;
50 extern const KernelConfiguration KERNEL_BASIC;
51 typedef std::shared_ptr<cl::CommandQueue> CommandQueue;
52
53
54 /// definitions of mathematical operators and functions for class ::Element
55 namespace elementOperators
56 {
57 // Math Operators
58 /**
59 \ingroup MathOperators
60 */
61 Element operator-(Element e);
62
63 /**
64 \ingroup MathOperators
65 */
66 Element operator+(Element e1, Element e2);
67
68 /**
69 \ingroup MathOperators
70 */
71 Element operator-(Element e1, Element e2);
72
73 /**
74 \ingroup MathOperators
75 */
76 Element operator*(Element e1, Element e2);
77
78 /**
79 \ingroup MathOperators
80 */
81 Element operator/(Element e1, Element e2);
82
83 /**
84 \ingroup MathOperators
85 */
86 Element operator%(Element e1, Element e2);
87
88
89
90 // Assignment Operators
91 /**
92 \ingroup AssignmentOperators
93 */
94 Element operatorAssignment(Element e1, Element e2);
95
96 /**
97 \ingroup AssignmentOperators
98 */
99 Element operatorAssignmentSafe(Element e1, Element e2);
100
101 /**
102 \ingroup AssignmentOperators
103 */
104 Element operator+=(Element e1, Element e2);
105
106 /**
107 \ingroup AssignmentOperators
108 */
109 Element operator-=(Element e1, Element e2);
110
111 /**
112 \ingroup AssignmentOperators
113 */
114 Element operator*=(Element e1, Element e2);
115
116 /**
117 \ingroup AssignmentOperators
118 */
119 Element operator/=(Element e1, Element e2);
120
121
122 /**
123 \ingroup BooleanOperators
124 */
125 Element operator>(Element e1, Element e2);
126
127 /**
128 \ingroup BooleanOperators
129 */
130 Element operator<(Element e1, Element e2);
131
132 /**
133 \ingroup BooleanOperators
134 */
135 Element operator>=(Element e1, Element e2);
136
137 /**
138 \ingroup BooleanOperators
139 */
140 Element operator<=(Element e1, Element e2);
141
142 /**
143 \ingroup BooleanOperators
144 */
145 Element isEqual(Element e1, Element e2);
146
147 /**
148 \ingroup BooleanOperators
149 */
150 Element isNotEqual(Element e1, Element e2);
151
152 /**
153 \ingroup BooleanOperators
154 */
155 Element operator&&(Element e1, Element e2);
156
157 /**
158 \ingroup BooleanOperators
159 */
160 Element operator||(Element e1, Element e2);
161
162 /**
163 \ingroup MathOperators
164 */
165 Element operator!(Element e);
166
167
168 // Mathematical Functions
169 /**
170 \ingroup MathFunctions
171 */
172 Element sin(Element e);
173
174 /**
175 \ingroup MathFunctions
176 */
177 Element cos(Element e);
178
179 /**
180 \ingroup MathFunctions
181 */
182 Element sqrt(Element e);
183
184 /**
185 \ingroup MathFunctions
186 */
187 Element rsqrt(Element e);
188
189 /**
190 \ingroup MathFunctions
191 */
192 Element log(Element e);
193
194 /**
195 \ingroup MathFunctions
196 */
197 Element log10(Element e);
198
199 /**
200 a^i
201 \ingroup MathFunctions
202 */
203 Element powI(Element a, unsigned int i);
204
205 /**
206 exp(a)
207 \ingroup MathFunctions
208 */
209 Element exp(Element a);
210
211 /**
212 fabs(a)
213 \ingroup MathFunctions
214 */
215 Element fabs(Element a);
216
217 /**
218 abs(a)
219 \ingroup MathFunctions
220 */
221 Element abs(Element a);
222
223 /**
224 abs_diff(a,b)
225 \ingroup MathFunctions
226 */
227 Element abs_diff(Element a,Element b);
228
229 /**
230 floor(a)
231 \ingroup MathFunctions
232 */
233 Element floor(Element a);
234
235 /**
236 isnan(a)
237 \ingroup MathFunctions
238 */
239 Element isnan(Element a);
240
241 /// Return nan of corresponding float type
242 /// \ingroup MathFunctions
243 Element nan(TypeID t);
244
245 /**
246 returns \p a with sign of \p b
247 copysign(a,b)
248 \ingroup MathFunctions
249 */
250 Element copysign(Element a,Element b);
251
252 /**
253 returns 1 with sign of \p a and 0 for a = +-0
254 copysign(a,b)
255 \ingroup MathFunctions
256 */
257 Element sign(Element a);
258
259 /**
260 min(a,b)
261 \ingroup MathFunctions
262 */
263 Element min(Element a,Element b);
264
265 /**
266 max(a,b)
267 \ingroup MathFunctions
268 */
269 Element max(Element a,Element b);
270
271 /// Atomic sum
272 /// \ingroup AtomicFunctions
273 Element atomic_add(Element e1, Element e2);
274
275 /// Atomic subtraction
276 /// \ingroup AtomicFunctions
277 Element atomic_sub(Element e1, Element e2);
278
279 /// Atomic exchange
280 /// \ingroup AtomicFunctions
281 Element atomic_xchg(Element e1, Element e2);
282
283
284 /// ternary mad operator e1 * e2 + e3
285 /// \ingroup MathFunctions
286 Element mad(Element e1, Element e2, Element e3);
287
288
289 // Synchronization Functions
290 /**
291 Synchronous copy: Global memory <-> Local memory
292
293 \ingroup SynchronizationFunctions
294 */
295 Element syncCopy(Element source,
296 Element destination,
297 Element srcOffset,
298 Element dstOffset,
299 Element length);
300
301
302
303 /**
304 Sets work-group barrier
305
306 \ingroup SynchronizationFunctions
307 */
308 Element barrier(std::string flags = "CLK_LOCAL_MEM_FENCE");
309
310
311
312 // Special Purpose Functions
313 /**
314 Creates excerpt from source defined by filter
315 by replacing of "INDEX" occurrences
316 in source->str() by filter->str()
317
318 \ingroup SpecialPurposeFunctions
319 */
320 Element excerpt(Element source, Element filter);
321
322 /**
323 Parses statement by replacing occurrences of
324 element's names by corresponding element->str()
325 from elementNamePairs
326
327 \ingroup SpecialPurposeFunctions
328 */
329 Element parse(const std::vector<std::pair<Element, std::string> > & elementNamePairs,
330 const std::string & statement);
331
332 /**
333 type conversion
334 \ingroup SpecialPurposeFunctions
335 */
336 Element convert(const TypeID tName, Element e1, bool strong=true);
337
338 /// printf()
339 /// \ingroup SpecialPurposeFunctions
340 Element printfFunction(std::string args);
341
342
343 /// Return statement (terminates the execution of a kernel)
344 /// \ingroup ControlStructures
345 Element returnStatement();
346
347
348 /// If-Else conditional structure
349 /// \ingroup ControlStructures
350 Element ifElse(Element condition,
351 const std::vector<Element> & thenBody,
352 const std::vector<Element> & elseBody);
353
354
355 /// ternary branching operator
356 /// \ingroup ControlStructures
357 Element select(Element e1, Element e2, Element e3);
358
359
360 /// For loop
361 /// \ingroup ControlStructures
362 Element forLoop(Element initialization,
363 Element condition,
364 Element increase,
365 const std::vector<Element> & body);
366
367 /// Corresponds to the openCL operation any
368 Element any(Element e);
369 /// Corresponds to the openCL operation all
370 Element all(Element e);
371
372
373 }
374
375
376 // RTTI functions
377
378 bool isConstant(Element e);
379
380 /// The function returns true when the input is a single valued object e.g. aclConstatnt, aclVariable
381 bool isSingleValue(Element e);
382
383 bool isMemBlock(Element e);
384
385 /// Copies source to destination, resizes destination to accommodate source.
386 /// \ingroup HostInteractionFunctions
387 template <typename T> void copy(MemBlock &source, T* destination);
388
389 /// Copies source to destination, exit(1) if sizes do not match.
390 /// \ingroup HostInteractionFunctions
391 template <typename T> void copy(T* source, MemBlock &destination);
392
393 /// Copies source to destination, resizes destination to accommodate source.
394 /// \ingroup HostInteractionFunctions
395 template <typename T> void copy(MemBlock &source, std::vector<T> &destination);
396
397 /// Copies source to destination, exit(1) if sizes do not match.
398 /// \ingroup HostInteractionFunctions
399 template <typename T> void copy(std::vector<T> &source, MemBlock &destination);
400
401 /// Copies source to destination, exit(1) if sizes do not match.
402 /// \ingroup HostInteractionFunctions
403 template <typename T> void copy(MemBlock &source, MemBlock &destination);
404
405 /// Copies source to destination, resizes destination to accommodate source.
406 /// \ingroup HostInteractionFunctions
407 template <typename T> void copy(Element source, std::vector<T> &destination);
408
409 /// Copies source to destination, exit(1) if sizes do not match.
410 /// \ingroup HostInteractionFunctions
411 template <typename T> void copy(std::vector<T> &source, Element destination);
412
413 /// Copies source to destination.
414 /// \ingroup HostInteractionFunctions
415 template <typename T> void copy(Element source, T* destination);
416
417 /// Copies source to destination.
418 /// \ingroup HostInteractionFunctions
419 template <typename T> void copy(T* source, Element destination);
420
421 /// puts a vector<Element> in ExpressionContainer
422 /**
423 \relates ExpressionContainer
424 \relates VectorOfElements
425 */
426 ExpressionContainer & operator<<(ExpressionContainer & ec,
427 const std::vector<Element> & a);
428
429 /// puts a vector<Element> in ExpressionContainer
430 /**
431 \relates ExpressionContainer
432 \relates VectorOfElements
433 */
434 std::vector<Element> & operator<<(std::vector<Element> & ec,
435 const std::vector<Element> & a);
436
437
438 /// puts a MatrixOfElements in ExpressionContainer
439 /**
440 \relates ExpressionContainer
441 \relates VectorOfElements
442 */
443 ExpressionContainer & operator<<(ExpressionContainer & ec,
444 const MatrixOfElements & a);
445
446 /// puts a ExpressionContainer in ExpressionContainer
447 /**
448 \relates ExpressionContainer
449 \relates VectorOfElements
450 */
451 ExpressionContainer & operator<<(ExpressionContainer & ec,
452 const ExpressionContainer & a);
453
454 /// function writes data from \p initializationValue to \p a
455 /// \ingroup SpecialPurposeFunctions
456 void initData(Element a,
457 Element initializationValue,
458 const KernelConfiguration & kernelConfig = KERNEL_BASIC);
459
460 /// function creates subElement with given length and offset; offset is constant
461 Element generateSubElement(Element, unsigned int size, int offset);
462
463 ///function creates subElement with given length and offset; offset can be a variable
464 Element generateSubElement(Element, unsigned int size, int * offset);
465
466 Element generateSubElement(Element, unsigned int size, Element offset);
467
468 Element generateShiftedElement(Element, int offset);
469 Element generateShiftedElement(Element, int* offset);
470 Element generateShiftedElement(Element, Element offset);
471
472 template <typename T> void swapBuffers(std::shared_ptr<Array<T> >a,
473 std::shared_ptr<Array<T> > b);
474
475
476 ElementData generateElementArray(TypeID typeID,
477 unsigned int size);
478 ElementData generateElementArray(TypeID typeID,
479 unsigned int size,
480 CommandQueue queue_);
481
482
483 Element generateElementLocalArray(TypeID typeID,
484 unsigned int size);
485
486 } // namespace acl
487 #endif // ACL_H