Debian Codegraph @

src/acl/aclMath/aclVectorOfElementsOperations.cxx

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 #include "aclVectorOfElementsOperations.h"
25 #include "aclUtilities.h"
26 #include "acl.h"
27 #include "DataTypes/aclConstant.h"
28 #include "../aslUtilities.h"
29 #include <algorithm>
30 #include "Kernels/aclKernel.h"
31 #include "Kernels/aclKernelConfigurationTemplates.h"
32
33 #include <acl/DataTypes/aclArray.h>
34
35 #include <acl/aclGenerators.h>
36
37 using asl::errorMessage;
38
39 using namespace std;
40
41 namespace acl
42 {
43
44 VectorOfElementsData clone(VectorOfElementsData a)
45 {
46 if (a.size() == 0)
47 return VectorOfElementsData(0u);
48
49 VectorOfElementsData v(a.size());
50 auto queue(a[0]->getQueue());
51
52 for (unsigned int i(0); i < a.size(); ++i)
53 v[i] = generateElementArray(a[i]->getTypeID(), a[i]->getSize(), queue);
54 return v;
55 }
56
57 VectorOfElementsData clone(VectorOfElementsData a, unsigned int n)
58 {
59 if (a.size() < n)
60 errorMessage("clone: number n is larger than size of the input vector");
61
62 if (a.size() == 0)
63 return VectorOfElementsData(0u);
64
65 VectorOfElementsData v(n);
66 auto queue(a[0]->getQueue());
67
68 for (unsigned int i(0); i < n; ++i)
69 v[i] = generateElementArray(a[i]->getTypeID(), a[i]->getSize(), queue);
70 return v;
71 }
72
73
74
75 VectorOfElements assignmentSafe(const VectorOfElements & a,
76 const VectorOfElements &b)
77 {
78 if (a.size() != b.size())
79 errorMessage("assignmentSafe - the sizes of two VectorOfElements are incompatible: "
80 + asl::numToStr(a.size()) + " and " + asl::numToStr(b.size()));
81
82 VectorOfElements ve(a.size());
83 for (unsigned int i(0); i < a.size(); ++i)
84 ve[i] = elementOperators::operatorAssignmentSafe(a[i], b[i]);
85
86 return ve;
87 }
88
89 void copy(const vector<Element> & source,
90 VectorOfElements & destination)
91 {
92 destination.resize(source.size());
93 for (unsigned int i(0); i < destination.size(); ++i)
94 destination[i] = source[i];
95 }
96
97 void copy(const vector<ElementData> & source,
98 VectorOfElements & destination)
99 {
100 destination.resize(source.size());
101 for (unsigned int i(0); i < destination.size(); ++i)
102 destination[i] = source[i];
103 }
104
105
106 void copy(const vector<Element> & source,
107 VectorOfElements & destination, unsigned int start, unsigned int end)
108 {
109 if (source.size() <= end )
110 errorMessage("copy: attempt to copy besides the vector range");
111
112 destination.resize(1+end-start);
113 for (unsigned int i(start); i <= end; ++i)
114 destination[i] = source[i];
115 }
116
117 void copy(const VectorOfElementsData & source,
118 VectorOfElementsData & destination)
119 {
120 destination.resize(source.size());
121 for (unsigned int i(0); i < destination.size(); ++i)
122 destination[i] = source[i];
123 }
124
125
126 VectorOfElements subVE(const VectorOfElements & source,
127 unsigned int start, unsigned int end)
128 {
129 if (source.size() <= end )
130 errorMessage("subVE: attempt to copy besides the vector range");
131
132 VectorOfElements destination(1 + end - start);
133 for (unsigned int i(start); i <= end; ++i)
134 destination[i - start] = source[i];
135 return destination;
136 }
137
138 VectorOfElements subVE(const VectorOfElements & source,
139 unsigned int i)
140 {
141 if (source.size() <= i )
142 errorMessage("subVE: attempt to copy besides the vector range");
143
144 VectorOfElements destination(1);
145 destination[0] = source[i];
146 return destination;
147 }
148
149 VectorOfElements subVE(const VectorOfElements & source,
150 const vector<unsigned int> & iList)
151 {
152 VectorOfElements destination(iList.size());
153 for (unsigned int i(0); i < iList.size(); ++i)
154 {
155 if (source.size() <= iList[i] )
156 errorMessage("subVE: attempt to copy besides the vector range");
157 destination[i] = source[iList[i]];
158 }
159 return destination;
160 }
161
162
163 VectorOfElementsData subVE(const VectorOfElementsData & source,
164 unsigned int start, unsigned int end)
165 {
166 if (source.size() <= end )
167 errorMessage("subVE: attempt to copy besides the vector range");
168
169 VectorOfElementsData destination(1 + end - start);
170 for (unsigned int i(start); i <= end; ++i)
171 destination[i - start] = source[i];
172 return destination;
173 }
174
175 VectorOfElements cat(const VectorOfElements * a,
176 unsigned int n)
177 {
178 unsigned int s(0);
179 for(unsigned int j(0); j < n; ++j)
180 s+=a[j].size();
181
182 VectorOfElements c(s);
183 s=0;
184 for(unsigned int i(0); i < n; ++i)
185 for(unsigned int j(0); j < a[i].size(); ++j)
186 {
187 c[s] = a[i][j];
188 s++;
189 }
190 return c;
191 }
192
193
194 VectorOfElements catN(const VectorOfElements & a,
195 unsigned int n)
196 {
197 VectorOfElements c(a.size()*n);
198 for(unsigned int j(0); j < a.size(); ++j)
199 for(unsigned int i(0); i < n; ++i)
200 c[j+i*a.size()] = a[j];
201 return c;
202 }
203
204 void initData(VectorOfElements a,
205 VectorOfElements initializationValue)
206 {
207 initData(a, initializationValue, KERNEL_SIMD);
208 }
209
210
211 void initData(VectorOfElements a,
212 VectorOfElements initializationValue,
213 const KernelConfiguration & kernelConfig)
214 {
215 Kernel k(kernelConfig);
216 k << acl::assignmentSafe(a, initializationValue);
217 k.setup();
218 k.compute();
219 }
220
221
222 /// \todo do they have to be on the same device?
223 void swapBuffers(const VectorOfElementsData & a, const VectorOfElementsData & b)
224 {
225 if (a.size() == 0)
226 errorMessage("swapBuffers - first VectorsOfEllementsData has zero size");
227 if (b.size() == 0)
228 errorMessage("swapBuffers - second VectorsOfEllementsData has zero size");
229 if (a.size() != b.size())
230 errorMessage("swapBuffers - twoVectorOfElementsData have different sizes");
231
232 for (unsigned int i(0); i < a.size(); ++i)
233 swapBuffers(*a[i], *b[i]);
234 }
235
236
237 VectorOfElements operator-(const VectorOfElements & a)
238 {
239 VectorOfElements c(a.size());
240 using namespace elementOperators;
241 for (unsigned int i(0); i < c.size(); ++i)
242 {
243 c[i]= - a[i];
244 }
245 return c;
246 }
247
248
249 VectorOfElements operator+=(const VectorOfElements & a, const VectorOfElements & b)
250 {
251 if (a.size() != b.size())
252 errorMessage("operator+= - two VectorOfElements have different sizes");
253
254 VectorOfElements c(a.size());
255 using namespace elementOperators;
256 for (unsigned int i(0); i < c.size(); ++i)
257 c[i]=( a[i] += b[i]);
258 return c;
259 }
260
261
262 VectorOfElements operator-=(const VectorOfElements & a, const VectorOfElements & b)
263 {
264 VectorOfElements c(a.size());
265 /// \todo on same device? and all the rest of operators...
266 if (a.size() == b.size())
267 {
268 using namespace elementOperators;
269 for (unsigned int i(0); i < c.size(); ++i)
270 c[i] = (a[i] -= b[i]);
271 }
272 else
273 errorMessage("operator-= - two VectorOfElements have different sizes");
274 return c;
275 }
276
277
278 VectorOfElements operator*=(const VectorOfElements & a, const VectorOfElements & b)
279 {
280 VectorOfElements c(a.size());
281 if (b.size() == 1)
282 {
283 using namespace elementOperators;
284 for (unsigned int i(0); i < a.size(); ++i)
285 c[i] = (a[i] *= b[0]);
286 }
287 else
288 errorMessage("operator*= - the second VectorOfElements has more than 1 element");
289 return c;
290 }
291
292
293 VectorOfElements operator/=(const VectorOfElements & a, const VectorOfElements & b)
294 {
295 VectorOfElements c(a.size());
296 if (b.size() == 1)
297 {
298 using namespace elementOperators;
299 for (unsigned int i(0); i < a.size(); ++i)
300 c[i] = (a[i] /= b[0]);
301 }
302 else
303 errorMessage("operator/= - the second VectorOfElements has more than 1 element");
304 return c;
305 }
306
307
308 VectorOfElements operator+(const VectorOfElements & a, const VectorOfElements & b)
309 {
310 if (a.size() != b.size())
311 errorMessage("operator+ - two VectorOfElements have different sizes");
312
313 VectorOfElements c(a.size());
314 using namespace elementOperators;
315 for (unsigned int i(0); i < c.size(); ++i)
316 c[i] = (a[i] + b[i]);
317 return c;
318 }
319
320
321 VectorOfElements operator-(const VectorOfElements & a, const VectorOfElements & b)
322 {
323 if (a.size() != b.size())
324 errorMessage("operator- - two VectorOfElements have different sizes:"+
325 asl::numToStr(a.size()) + ", " +
326 asl::numToStr(b.size()));
327
328 VectorOfElements c(a.size());
329 using namespace elementOperators;
330 for (unsigned int i(0); i < c.size(); ++i)
331 c[i] = ( a[i] - b[i]);
332 return c;
333 }
334
335
336 VectorOfElements operator*(const VectorOfElements & a, const VectorOfElements & b)
337 {
338 VectorOfElements c(1);
339 if (a.size() == b.size())
340 {
341 using namespace elementOperators;
342 c[0] = a[0] * b[0];
343 for (unsigned int i(1); i < a.size(); ++i)
344 c[0] = c[0] + a[i] * b[i];
345 return c;
346 }
347 if (a.size() == 1)
348 {
349 c.resize(b.size());
350 using namespace elementOperators;
351 for (unsigned int i(0); i < c.size(); ++i)
352 c[i] = a[0] * b[i];
353 return c;
354 }
355 if (b.size() == 1)
356 {
357 c.resize(a.size());
358 using namespace elementOperators;
359 for (unsigned int i(0); i < c.size(); ++i)
360 c[i] = a[i] * b[0];
361 return c;
362 }
363
364 errorMessage("operator* - two VectorOfElements have different sizes or nor of one has size 1");
365 return c;
366 }
367
368
369 VectorOfElements operator/(const VectorOfElements & a, const VectorOfElements & b)
370 {
371 if (b.size() != 1u)
372 errorMessage("operator/ - size of second VectorOfElements is not 1");
373
374 VectorOfElements c(a.size());
375 using namespace elementOperators;
376 for (unsigned int i(0); i < c.size(); ++i)
377 c[i]= a[i] / b[0];
378 return c;
379 }
380
381 VectorOfElements operator%(const VectorOfElements & a,
382 const VectorOfElements & b)
383 {
384 if (b.size() != 1u)
385 errorMessage("operator% - size of second VectorOfElements is not 1");
386
387 VectorOfElements c(a.size());
388 using namespace elementOperators;
389 for (unsigned int i(0); i < c.size(); ++i)
390 c[i]= a[i] % b[0];
391 return c;
392 }
393
394 VectorOfElements operator==(const VectorOfElements & a, const VectorOfElements & b)
395 {
396 VectorOfElements c(1);
397 if (a.size() == b.size())
398 {
399 using namespace elementOperators;
400 c[0] = isEqual(a[0], b[0]);
401 for (unsigned int i(1); i < c.size(); ++i)
402 c[0] = (c[0] && isEqual(a[i], b[i]));
403 return c;
404 }
405 else
406 errorMessage("operator== - two VectorOfElements have different sizes");
407 return c;
408 }
409
410
411 VectorOfElements operator!=(const VectorOfElements & a, const VectorOfElements & b)
412 {
413 VectorOfElements c(1);
414 if (a.size() == b.size())
415 {
416 using namespace elementOperators;
417 c[0] = isNotEqual(a[0], b[0]);
418 for (unsigned int i(1); i < c.size(); ++i)
419 c[0] = (c[0] || isNotEqual(a[i], b[i]));
420 return c;
421 }
422 errorMessage("operator!= - two VectorOfElements have different sizes");
423 return c;
424 }
425
426
427 VectorOfElements operator>(const VectorOfElements & a, const VectorOfElements & b)
428 {
429 VectorOfElements c(1);
430 if (a.size() == b.size())
431 {
432 using namespace elementOperators;
433 c[0] = (a[0] > b[0]);
434 for (unsigned int i(1); i < a.size(); ++i)
435 c[0] = (c[0] && a[i] > b[i]);
436 return c;
437 }
438 errorMessage("operator> - two VectorOfElements have different sizes");
439 return c;
440 }
441
442 VectorOfElements operator&&(const VectorOfElements & a, const VectorOfElements & b)
443 {
444 if (a.size() != b.size())
445 errorMessage("operator&& - two VectorOfElements have different sizes");
446
447 VectorOfElements c(a.size());
448 using namespace elementOperators;
449 for (unsigned int i(0); i < c.size(); ++i)
450 c[i] = a[i] && b[i];
451 return c;
452 }
453
454 VectorOfElements operator||(const VectorOfElements & a, const VectorOfElements & b)
455 {
456 if (a.size() != b.size())
457 errorMessage("operator|| - two VectorOfElements have different sizes");
458
459 VectorOfElements c(a.size());
460 using namespace elementOperators;
461 for (unsigned int i(0); i < c.size(); ++i)
462 c[i] = a[i] || b[i];
463 return c;
464 }
465
466 VectorOfElements operator<(const VectorOfElements & a, const VectorOfElements & b)
467 {
468 if (a.size() != b.size())
469 errorMessage("operator< - two VectorOfElements have different sizes");
470
471 VectorOfElements c(1);
472 using namespace elementOperators;
473 c[0] = (a[0] < b[0]);
474 for (unsigned int i(1); i < a.size(); ++i)
475 c[0] = (c[0] && a[i] < b[i]);
476 return c;
477 }
478
479 VectorOfElements operator<=(const VectorOfElements & a, const VectorOfElements & b)
480 {
481 if (a.size() != b.size())
482 errorMessage("operator<= - two VectorOfElements have different sizes");
483
484 VectorOfElements c(1);
485 using namespace elementOperators;
486 c[0] = (a[0] <= b[0]);
487 for (unsigned int i(1); i < a.size(); ++i)
488 c[0] = (c[0] && a[i] <= b[i]);
489 return c;
490 }
491
492 VectorOfElements operator>=(const VectorOfElements & a, const VectorOfElements & b)
493 {
494 if (a.size() != b.size())
495 errorMessage("operator>= - two VectorOfElements have different sizes");
496
497 VectorOfElements c(1);
498 using namespace elementOperators;
499 c[0] = (a[0] >= b[0]);
500 for (unsigned int i(1); i < a.size(); ++i)
501 c[0] = (c[0] && a[i] >= b[i]);
502 return c;
503 }
504
505 VectorOfElements operator!(const VectorOfElements & a)
506 {
507 VectorOfElements c(a.size());
508 using namespace elementOperators;
509 for (unsigned int i(0); i < c.size(); ++i)
510 c[i]= !a[i];
511 return c;
512 }
513
514 VectorOfElements crossProduct(const VectorOfElements & a, const VectorOfElements & b)
515 {
516 if (a.size() != b.size())
517 errorMessage("crossProduct - two VectorOfElements have different sizes");
518 if (a.size() == 1)
519 errorMessage("crossProduct - is undefined for size 1 of VectorOfElements");
520 if (a.size() > 3)
521 errorMessage("crossProduct - is undefined for size more than 3 of VectorOfElements");
522
523 VectorOfElements c(a.size()==2 ? 1 :3);
524 using namespace elementOperators;
525
526 if (a.size() == 2)
527 c[0] = a[0] * b[1] - a[1] * b[0];
528 else
529 {
530 c[0] = a[1] * b[2] - a[2] * b[1];
531 c[1] = a[2] * b[0] - a[0] * b[2];
532 c[2] = a[0] * b[1] - a[1] * b[0];
533 }
534 return c;
535 }
536
537
538 VectorOfElements productOfElements(const VectorOfElements & a, const VectorOfElements & b)
539 {
540 if (a.size() != b.size())
541 errorMessage("productOfElements - two VectorOfElements have different sizes");
542
543 VectorOfElements c(a.size());
544 using namespace elementOperators;
545 for (unsigned int i(0); i < c.size(); ++i)
546 c[i] = a[i] * b[i];
547 return c;
548 }
549
550 VectorOfElements productOfElements(const VectorOfElements & a)
551 {
552 VectorOfElements c(1);
553 using namespace elementOperators;
554 c[0]=a[0];
555 for (unsigned int i(1); i < a.size(); ++i)
556 c[0] = c[0] * a[i];
557 return c;
558 }
559
560 VectorOfElements divisionOfElements(const VectorOfElements & a, const VectorOfElements & b)
561 {
562 if (a.size() != b.size())
563 errorMessage("divisionOfElements - two VectorOfElements have different sizes");
564
565 VectorOfElements c(a.size());
566 using namespace elementOperators;
567 for (unsigned int i(0); i < c.size(); ++i)
568 c[i] = a[i] / b[i];
569 return c;
570 }
571
572 VectorOfElements min(const VectorOfElements & a, const VectorOfElements & b)
573 {
574 if (a.size() != b.size())
575 errorMessage("min - two VectorOfElements have different sizes");
576
577 VectorOfElements c(a.size());
578 using namespace elementOperators;
579 for (unsigned int i(0); i < c.size(); ++i)
580 c[i] = min(a[i],b[i]);
581 return c;
582 }
583
584 VectorOfElements min(const VectorOfElements & a, const VectorOfElements & b, TypeID type)
585 {
586 return min(convert(type, a, false), convert(type, b, false));
587 }
588
589 VectorOfElements minAbs(const VectorOfElements & a, const VectorOfElements & b)
590 {
591 if (a.size() != b.size())
592 errorMessage("minAbs: two VectorOfElements have different sizes");
593
594 acl::TypeID type(getElementType(a));
595
596 VectorOfElements c(a.size());
597 copy(select(b, a, fabs(a)<fabs(b), type), c);
598 return c;
599 }
600
601 VectorOfElements max(const VectorOfElements & a, const VectorOfElements & b)
602 {
603 if (a.size() != b.size())
604 errorMessage("max - two VectorOfElements have different sizes");
605
606 VectorOfElements c(a.size());
607 using namespace elementOperators;
608 for (unsigned int i(0); i < c.size(); ++i)
609 c[i] = max(a[i],b[i]);
610 return c;
611 }
612
613 VectorOfElements max(const VectorOfElements & a, const VectorOfElements & b, TypeID type)
614 {
615 return max(convert(type, a, false), convert(type, b, false));
616 }
617
618
619 VectorOfElements copysign(const VectorOfElements & a, const VectorOfElements & b)
620 {
621 if (a.size() != b.size())
622 errorMessage("max - two VectorOfElements have different sizes");
623
624 VectorOfElements c(a.size());
625 using namespace elementOperators;
626 for (unsigned int i(0); i < c.size(); ++i)
627 c[i] = copysign(a[i],b[i]);
628 return c;
629 }
630
631 VectorOfElements copysign(const VectorOfElements & a,
632 const VectorOfElements & b,
633 TypeID t)
634 {
635 return copysign(convert(t,a,false),convert(t,b,false));
636 }
637
638 VectorOfElements sign(const VectorOfElements & a)
639 {
640 VectorOfElements c(a.size());
641 using namespace elementOperators;
642 for (unsigned int i(0); i < c.size(); ++i)
643 c[i] = sign(a[i]);
644 return c;
645 }
646
647 VectorOfElements excerpt(const VectorOfElements & source, const VectorOfElements & filter)
648 {
649 if (filter.size() != 1)
650 errorMessage("exerpt - filter has more than 1 component");
651
652 VectorOfElements c(source.size());
653 using namespace elementOperators;
654 for (unsigned int i(0); i < c.size(); ++i)
655 {
656 c[i] = excerpt(source[i], filter[0]);
657 }
658 return c;
659 }
660
661 VectorOfElements select(const VectorOfElements & a,
662 const VectorOfElements & b,
663 const VectorOfElements & c)
664 {
665 VectorOfElements r(a.size());
666 if (c.size() == 1)
667 {
668 if (a.size() == b.size())
669 {
670 using namespace elementOperators;
671 for (unsigned int i(0); i < a.size(); ++i)
672 r[i] = select(a[i], b[i], c[0]);
673 return r;
674 }
675 errorMessage("select - VectorOfElements a and b have different sizes");
676 }
677 else
678 {
679 if (a.size() == b.size() && c.size() == b.size())
680 {
681 using namespace elementOperators;
682 for (unsigned int i(0); i < c.size(); ++i)
683 r[i] = select(a[i], b[i], c[i]);
684 return r;
685 }
686 errorMessage("select - VectorOfElements a, b and c have different sizes");
687 }
688 return r;
689 }
690
691 VectorOfElements select(const VectorOfElements & a,
692 const VectorOfElements & b,
693 const VectorOfElements & c,
694 TypeID t)
695 {
696 return select(convert(t,a,false),
697 convert(t,b,false),
698 convert(TYPE_SELECT[t],c,false));
699 }
700
701 VectorOfElements select(const VectorOfElements & b,
702 const VectorOfElements & c,
703 TypeID t)
704 {
705 return select(convert(t,generateVEConstantN(b.size(),0),false),
706 convert(t,b,false),
707 convert(TYPE_SELECT[t],c,false));
708 }
709
710 VectorOfElements mad(const VectorOfElements & a,
711 const VectorOfElements & b,
712 const VectorOfElements & c)
713 {
714 if (a.size() != 1 || b.size() != 1 || c.size() != 1)
715 errorMessage("mad - the function defined only fo sigle value VectorOfElements");
716 using namespace elementOperators;
717 VectorOfElements r(1);
718 r[0] = mad(a[0], b[0], c[0]);
719 return r;
720 }
721
722 VectorOfElements mad(const VectorOfElements & a,
723 const VectorOfElements & b,
724 const VectorOfElements & c,
725 TypeID t)
726 {
727 return mad(convert(t,a,false),
728 convert(t,b,false),
729 convert(t,c,false));
730 }
731
732
733 VectorOfElements powI(const VectorOfElements & a, unsigned int i)
734 {
735 VectorOfElements c(1);
736 if (a.size() != 1)
737 errorMessage("powI - the input vector has number of elements unequal to 1");
738
739 using namespace elementOperators;
740 c[0] = powI(a[0], i);
741 return c;
742 }
743
744 VectorOfElements exp(const VectorOfElements & a)
745 {
746 if (a.size() != 1)
747 errorMessage("exp- the input vector has number of elements unequal to 1");
748
749 VectorOfElements c(1);
750 c[0] = elementOperators::exp(a[0]);
751 return c;
752 }
753
754 VectorOfElements sqrt(const VectorOfElements & a)
755 {
756 if (a.size() != 1)
757 errorMessage("sqrt- the input vector has number of elements unequal to 1");
758
759 VectorOfElements c(1);
760 c[0] = elementOperators::sqrt(a[0]);
761 return c;
762 }
763
764 VectorOfElements rsqrt(const VectorOfElements & a)
765 {
766 if (a.size() != 1)
767 errorMessage("rsqrt- the input vector has number of elements unequal to 1");
768
769 VectorOfElements c(1);
770 c[0] = elementOperators::rsqrt(a[0]);
771 return c;
772 }
773
774 VectorOfElements fabs(const VectorOfElements & a)
775 {
776 VectorOfElements c(a.size());
777 for(unsigned int i(0); i < a.size(); ++i)
778 c[i] = elementOperators::fabs(a[i]);
779 return c;
780 }
781
782 VectorOfElements abs(const VectorOfElements & a)
783 {
784 VectorOfElements c(a.size());
785 for(unsigned int i(0); i < a.size(); ++i)
786 c[i] = elementOperators::abs(a[i]);
787 return c;
788 }
789
790 VectorOfElements abs_diff(const VectorOfElements & a, const VectorOfElements & b)
791 {
792 if (a.size() != b.size())
793 errorMessage("abs_diff - two VectorOfElements have different sizes");
794
795 VectorOfElements c(a.size());
796 for(unsigned int i(0); i < a.size(); ++i)
797 c[i] = elementOperators::abs_diff(a[i],b[i]);
798 return c;
799 }
800
801 VectorOfElements floor(const VectorOfElements & a)
802 {
803 VectorOfElements c(a.size());
804 for(unsigned int i(0); i < a.size(); ++i)
805 c[i] = elementOperators::floor(a[i]);
806 return c;
807 }
808
809 VectorOfElements convert(acl::TypeID type,const VectorOfElements & a, bool strong)
810 {
811 VectorOfElements c(a.size());
812 using namespace elementOperators;
813 for (unsigned int i(0); i < c.size(); ++i)
814 c[i] = convert(type,a[i],strong);
815 return c;
816 }
817
818
819
820
821 VectorOfElements log(const VectorOfElements & a)
822 {
823 if (a.size() != 1)
824 errorMessage("log - the input vector has number of elements unequal to 1");
825 VectorOfElements c(1);
826 c[0] = elementOperators::log(a[0]);
827 return c;
828 }
829
830
831 VectorOfElements log10(const VectorOfElements & a)
832 {
833 if (a.size() != 1)
834 errorMessage("log10 - the input vector has number of elements unequal to 1");
835 VectorOfElements c(1);
836 c[0] = elementOperators::log10(a[0]);
837 return c;
838 }
839
840
841 VectorOfElements minElement(const VectorOfElements & a)
842 {
843 VectorOfElements c(1);
844 c[0]=min(a[0], a[1]);
845 for(unsigned int i(2); i<a.size(); ++i)
846 c[0]=acl::elementOperators::min(c[0], a[i]);
847 return c;
848 }
849
850 VectorOfElements minAbsElement(const VectorOfElements & a)
851 {
852 VectorOfElements c(1);
853 acl::TypeID type(getElementType(a));
854 using namespace elementOperators;
855 c[0]=select(a[0], a[1], convert(TYPE_SELECT[type],
856 fabs(a[1])<fabs(a[0]),
857 false));
858 for(unsigned int i(2); i<a.size(); ++i)
859 c[0]=select(c[0], a[i], convert(TYPE_SELECT[type],
860 fabs(a[i])<fabs(c[0]),
861 false));
862 return c;
863 }
864
865 VectorOfElements maxElement(const VectorOfElements & a)
866 {
867 VectorOfElements c(1);
868 c[0]=min(a[0], a[1]);
869 for(unsigned int i(2); i < a.size(); ++i)
870 c[0]=acl::elementOperators::max(c[0], a[i]);
871 return c;
872 }
873
874 VectorOfElements sumOfElements(const VectorOfElements & a)
875 {
876 VectorOfElements c(1);
877 c[0]=acl::elementOperators::operator+(a[0], a[1]);
878 for(unsigned int i(2); i < a.size(); ++i)
879 c[0]=acl::elementOperators::operator+(c[0], a[i]);
880 return c;
881 }
882
883 VectorOfElements andOfElements(const VectorOfElements & a)
884 {
885 VectorOfElements c(1);
886 c[0]=acl::elementOperators::operator&&(a[0], a[1]);
887 for(unsigned int i(2); i < a.size(); ++i)
888 c[0]=acl::elementOperators::operator&&(c[0], a[i]);
889 return c;
890 }
891
892 VectorOfElements orOfElements(const VectorOfElements & a)
893 {
894 VectorOfElements c(1);
895 c[0]=acl::elementOperators::operator||(a[0], a[1]);
896 for(unsigned int i(2); i < a.size(); ++i)
897 c[0]=acl::elementOperators::operator||(c[0], a[i]);
898 return c;
899 }
900
901 VectorOfElements cat(const VectorOfElements & a, const VectorOfElements & b)
902 {
903 VectorOfElements c(a.size() + b.size());
904 using namespace elementOperators;
905 for (unsigned int i(0); i < a.size(); ++i)
906 c[i] = ( a[i]);
907 for (unsigned int i(0); i < b.size(); ++i)
908 c[a.size() + i] = b[i];
909 return c;
910 }
911
912 VectorOfElementsData cat(const VectorOfElementsData & a, const VectorOfElementsData & b)
913 {
914 VectorOfElementsData c(a.size() + b.size());
915 using namespace elementOperators;
916 for (unsigned int i(0); i < a.size(); ++i)
917 c[i] = ( a[i]);
918 for (unsigned int i(0); i < b.size(); ++i)
919 c[a.size() + i] = b[i];
920 return c;
921 }
922
923 VectorOfElements cat(const VectorOfElements & a,
924 const VectorOfElements & b,
925 const VectorOfElements & c)
926 {
927 VectorOfElements z(cat(a,b));
928 return cat(z,c);
929 }
930
931 template <typename T> VectorOfElements operator+=(const VectorOfElements & a, const T & b)
932 {
933 return a+=generateVEConstant(b);
934 }
935
936 template VectorOfElements operator+=(const VectorOfElements & a, const cl_double & b);
937 template VectorOfElements operator+=(const VectorOfElements & a, const cl_float & b);
938 template VectorOfElements operator+=(const VectorOfElements & a, const cl_uint & b);
939 template VectorOfElements operator+=(const VectorOfElements & a, const cl_int & b);
940
941 template <typename T> VectorOfElements operator-=(const VectorOfElements & a, const T & b)
942 {
943 return a-=generateVEConstant(b);
944 }
945
946 template VectorOfElements operator-=(const VectorOfElements & a, const cl_double & b);
947 template VectorOfElements operator-=(const VectorOfElements & a, const cl_float & b);
948 template VectorOfElements operator-=(const VectorOfElements & a, const cl_uint & b);
949 template VectorOfElements operator-=(const VectorOfElements & a, const cl_int & b);
950
951 template <typename T> VectorOfElements operator*=(const VectorOfElements & a, const T & b)
952 {
953 return a*=generateVEConstant(b);
954 }
955
956 template VectorOfElements operator*=(const VectorOfElements & a, const cl_double & b);
957 template VectorOfElements operator*=(const VectorOfElements & a, const cl_float & b);
958 template VectorOfElements operator*=(const VectorOfElements & a, const cl_uint & b);
959 template VectorOfElements operator*=(const VectorOfElements & a, const cl_int & b);
960
961 template <typename T> VectorOfElements operator/=(const VectorOfElements & a, const T & b)
962 {
963 return a/=generateVEConstant(b);
964 }
965
966 template VectorOfElements operator/=(const VectorOfElements & a, const cl_double & b);
967 template VectorOfElements operator/=(const VectorOfElements & a, const cl_float & b);
968 template VectorOfElements operator/=(const VectorOfElements & a, const cl_uint & b);
969 template VectorOfElements operator/=(const VectorOfElements & a, const cl_int & b);
970
971 template <typename T> VectorOfElements operator+(const VectorOfElements & a, const T & b)
972 {
973 return a+generateVEConstant(b);
974 }
975
976 template VectorOfElements operator+(const VectorOfElements & a, const cl_double & b);
977 template VectorOfElements operator+(const VectorOfElements & a, const cl_float & b);
978 template VectorOfElements operator+(const VectorOfElements & a, const cl_uint & b);
979 template VectorOfElements operator+(const VectorOfElements & a, const cl_int & b);
980 template VectorOfElements operator+(const VectorOfElements & a, const AVec<cl_double> & b);
981 template VectorOfElements operator+(const VectorOfElements & a, const AVec<cl_float> & b);
982 template VectorOfElements operator+(const VectorOfElements & a, const AVec<cl_uint> & b);
983 template VectorOfElements operator+(const VectorOfElements & a, const AVec<cl_int> & b);
984
985 template <typename T> inline VectorOfElements operator+(const T & a, const VectorOfElements & b)
986 {
987 return generateVEConstant(a)+b;
988 }
989
990 template VectorOfElements operator+(const cl_double & a, const VectorOfElements & b);
991 template VectorOfElements operator+(const cl_float & a, const VectorOfElements & b);
992 template VectorOfElements operator+(const cl_int & a, const VectorOfElements & b);
993 template VectorOfElements operator+(const cl_uint & a, const VectorOfElements & b);
994 template VectorOfElements operator+(const AVec<cl_double> & a, const VectorOfElements & b);
995 template VectorOfElements operator+(const AVec<cl_float> & a, const VectorOfElements & b);
996 template VectorOfElements operator+(const AVec<cl_int> & a, const VectorOfElements & b);
997 template VectorOfElements operator+(const AVec<cl_uint> & a, const VectorOfElements & b);
998
999 template <typename T> VectorOfElements operator-(const VectorOfElements & a, const T & b)
1000 {
1001 return a-generateVEConstant(b);
1002 }
1003
1004 template VectorOfElements operator-(const VectorOfElements & a, const cl_double & b);
1005 template VectorOfElements operator-(const VectorOfElements & a, const cl_float & b);
1006 template VectorOfElements operator-(const VectorOfElements & a, const cl_uint & b);
1007 template VectorOfElements operator-(const VectorOfElements & a, const cl_int & b);
1008 template VectorOfElements operator-(const VectorOfElements & a, const AVec<cl_double> & b);
1009 template VectorOfElements operator-(const VectorOfElements & a, const AVec<cl_float> & b);
1010 template VectorOfElements operator-(const VectorOfElements & a, const AVec<cl_uint> & b);
1011 template VectorOfElements operator-(const VectorOfElements & a, const AVec<cl_int> & b);
1012
1013 template <typename T> VectorOfElements operator-(const T & a, const VectorOfElements & b)
1014 {
1015 return generateVEConstant(a)-b;
1016 }
1017
1018 template VectorOfElements operator-(const cl_double & a, const VectorOfElements & b);
1019 template VectorOfElements operator-(const cl_float & a, const VectorOfElements & b);
1020 template VectorOfElements operator-(const cl_int & a, const VectorOfElements & b);
1021 template VectorOfElements operator-(const cl_uint & a, const VectorOfElements & b);
1022 template VectorOfElements operator-(const AVec<cl_double> & a, const VectorOfElements & b);
1023 template VectorOfElements operator-(const AVec<cl_float> & a, const VectorOfElements & b);
1024 template VectorOfElements operator-(const AVec<cl_int> & a, const VectorOfElements & b);
1025 template VectorOfElements operator-(const AVec<cl_uint> & a, const VectorOfElements & b);
1026
1027 template <typename T> VectorOfElements operator*(const VectorOfElements & a,const T & b)
1028 {
1029 return a*generateVEConstant(b);
1030 }
1031
1032 template VectorOfElements operator*(const VectorOfElements & a, const cl_double & b);
1033 template VectorOfElements operator*(const VectorOfElements & a, const cl_float & b);
1034 template VectorOfElements operator*(const VectorOfElements & a, const cl_uint & b);
1035 template VectorOfElements operator*(const VectorOfElements & a, const cl_int & b);
1036 template VectorOfElements operator*(const VectorOfElements & a, const AVec<cl_double> & b);
1037 template VectorOfElements operator*(const VectorOfElements & a, const AVec<cl_float> & b);
1038 template VectorOfElements operator*(const VectorOfElements & a, const AVec<cl_int> & b);
1039
1040 template <typename T> VectorOfElements operator*(const T & a, const VectorOfElements & b)
1041 {
1042 return generateVEConstant(a)*b;
1043 }
1044
1045 template VectorOfElements operator*(const cl_double & a, const VectorOfElements & b);
1046 template VectorOfElements operator*(const cl_float & a, const VectorOfElements & b);
1047 template VectorOfElements operator*(const cl_int & a, const VectorOfElements & b);
1048 template VectorOfElements operator*(const cl_uint & a, const VectorOfElements & b);
1049
1050 template <typename T> VectorOfElements operator/(const VectorOfElements & a, const T & b)
1051 {
1052 return a/generateVEConstant(b);
1053 }
1054
1055 template VectorOfElements operator/(const VectorOfElements & a, const cl_double & b);
1056 template VectorOfElements operator/(const VectorOfElements & a, const cl_float & b);
1057 template VectorOfElements operator/(const VectorOfElements & a, const cl_uint & b);
1058 template VectorOfElements operator/(const VectorOfElements & a, const cl_int & b);
1059
1060 template <typename T> VectorOfElements operator/(const T & a, const VectorOfElements & b)
1061 {
1062 return generateVEConstant(a)/b;
1063 }
1064
1065 template VectorOfElements operator/(const cl_double & a, const VectorOfElements & b);
1066 template VectorOfElements operator/(const cl_float & a, const VectorOfElements & b);
1067 template VectorOfElements operator/(const cl_int & a, const VectorOfElements & b);
1068 template VectorOfElements operator/(const cl_uint & a, const VectorOfElements & b);
1069
1070 template <typename T> VectorOfElements operator%(const VectorOfElements & a, const T & b)
1071 {
1072 return a%generateVEConstant(b);
1073 }
1074
1075 template VectorOfElements operator%(const VectorOfElements & a, const cl_double & b);
1076 template VectorOfElements operator%(const VectorOfElements & a, const cl_float & b);
1077 template VectorOfElements operator%(const VectorOfElements & a, const cl_uint & b);
1078 template VectorOfElements operator%(const VectorOfElements & a, const cl_int & b);
1079
1080 template <typename T> VectorOfElements operator%(const T & a, const VectorOfElements & b)
1081 {
1082 return generateVEConstant(a)%b;
1083 }
1084
1085 template VectorOfElements operator%(const cl_double & a, const VectorOfElements & b);
1086 template VectorOfElements operator%(const cl_float & a, const VectorOfElements & b);
1087 template VectorOfElements operator%(const cl_int & a, const VectorOfElements & b);
1088 template VectorOfElements operator%(const cl_uint & a, const VectorOfElements & b);
1089
1090 template <typename T> VectorOfElements operator>(const VectorOfElements & a, const T & b)
1091 {
1092 return a>generateVEConstant(b);
1093 }
1094
1095 template VectorOfElements operator>(const VectorOfElements & a, const cl_double & b);
1096 template VectorOfElements operator>(const VectorOfElements & a, const cl_float & b);
1097 template VectorOfElements operator>(const VectorOfElements & a, const cl_uint & b);
1098 template VectorOfElements operator>(const VectorOfElements & a, const cl_int & b);
1099
1100 template <typename T> VectorOfElements operator>(const T & a, const VectorOfElements & b)
1101 {
1102 return generateVEConstant(a)>b;
1103 }
1104
1105 template VectorOfElements operator>(const cl_double & a, const VectorOfElements & b);
1106 template VectorOfElements operator>(const cl_float & a, const VectorOfElements & b);
1107 template VectorOfElements operator>(const cl_int & a, const VectorOfElements & b);
1108 template VectorOfElements operator>(const cl_uint & a, const VectorOfElements & b);
1109
1110 template <typename T> VectorOfElements operator<(const VectorOfElements & a, const T & b)
1111 {
1112 return a<generateVEConstant(b);
1113 }
1114
1115 template VectorOfElements operator<(const VectorOfElements & a, const cl_double & b);
1116 template VectorOfElements operator<(const VectorOfElements & a, const cl_float & b);
1117 template VectorOfElements operator<(const VectorOfElements & a, const cl_uint & b);
1118 template VectorOfElements operator<(const VectorOfElements & a, const cl_int & b);
1119
1120 template <typename T> VectorOfElements operator<(const T & a, const VectorOfElements & b)
1121 {
1122 return generateVEConstant(a)<b;
1123 }
1124
1125 template VectorOfElements operator<(const cl_double & a, const VectorOfElements & b);
1126 template VectorOfElements operator<(const cl_float & a, const VectorOfElements & b);
1127 template VectorOfElements operator<(const cl_int & a, const VectorOfElements & b);
1128 template VectorOfElements operator<(const cl_uint & a, const VectorOfElements & b);
1129
1130 template <typename T> VectorOfElements operator>=(const VectorOfElements & a, const T & b)
1131 {
1132 return a>=generateVEConstant(b);
1133 }
1134
1135 template VectorOfElements operator>=(const VectorOfElements & a, const cl_double & b);
1136 template VectorOfElements operator>=(const VectorOfElements & a, const cl_float & b);
1137 template VectorOfElements operator>=(const VectorOfElements & a, const cl_uint & b);
1138 template VectorOfElements operator>=(const VectorOfElements & a, const cl_int & b);
1139
1140 template <typename T> VectorOfElements operator>=(const T & a, const VectorOfElements & b)
1141 {
1142 return generateVEConstant(a)>=b;
1143 }
1144
1145 template VectorOfElements operator>=(const cl_double & a, const VectorOfElements & b);
1146 template VectorOfElements operator>=(const cl_float & a, const VectorOfElements & b);
1147 template VectorOfElements operator>=(const cl_int & a, const VectorOfElements & b);
1148 template VectorOfElements operator>=(const cl_uint & a, const VectorOfElements & b);
1149
1150 template <typename T> VectorOfElements operator<=(const VectorOfElements & a, const T & b)
1151 {
1152 return a<=generateVEConstant(b);
1153 }
1154
1155 template VectorOfElements operator<=(const VectorOfElements & a, const cl_double & b);
1156 template VectorOfElements operator<=(const VectorOfElements & a, const cl_float & b);
1157 template VectorOfElements operator<=(const VectorOfElements & a, const cl_uint & b);
1158 template VectorOfElements operator<=(const VectorOfElements & a, const cl_int & b);
1159
1160 template <typename T> VectorOfElements operator<=(const T & a, const VectorOfElements & b)
1161 {
1162 return generateVEConstant(a)<=b;
1163 }
1164
1165 template VectorOfElements operator<=(const cl_double & a, const VectorOfElements & b);
1166 template VectorOfElements operator<=(const cl_float & a, const VectorOfElements & b);
1167 template VectorOfElements operator<=(const cl_int & a, const VectorOfElements & b);
1168 template VectorOfElements operator<=(const cl_uint & a, const VectorOfElements & b);
1169
1170 template <typename T> VectorOfElements operator==(const VectorOfElements & a,
1171 const T & b)
1172 {
1173 return a == generateVEConstant(b);
1174 }
1175
1176 template VectorOfElements operator==(const VectorOfElements & a, const cl_double & b);
1177 template VectorOfElements operator==(const VectorOfElements & a, const cl_float & b);
1178 template VectorOfElements operator==(const VectorOfElements & a, const cl_uint & b);
1179 template VectorOfElements operator==(const VectorOfElements & a, const cl_int & b);
1180
1181
1182 template <typename T> VectorOfElements operator==(const T & a,
1183 const VectorOfElements & b)
1184 {
1185 return generateVEConstant(a) == b;
1186 }
1187
1188 template VectorOfElements operator==(const cl_double & a, const VectorOfElements & b);
1189 template VectorOfElements operator==(const cl_float & a, const VectorOfElements & b);
1190 template VectorOfElements operator==(const cl_int & a, const VectorOfElements & b);
1191 template VectorOfElements operator==(const cl_uint & a, const VectorOfElements & b);
1192
1193 template <typename T> VectorOfElements operator!=(const VectorOfElements & a, const T & b)
1194 {
1195 return a!=generateVEConstant(b);
1196 }
1197
1198 template VectorOfElements operator!=(const VectorOfElements & a, const cl_double & b);
1199 template VectorOfElements operator!=(const VectorOfElements & a, const cl_float & b);
1200 template VectorOfElements operator!=(const VectorOfElements & a, const cl_uint & b);
1201 template VectorOfElements operator!=(const VectorOfElements & a, const cl_int & b);
1202
1203 template <typename T> VectorOfElements operator!=(const T & a, const VectorOfElements & b)
1204 {
1205 return generateVEConstant(a)!=b;
1206 }
1207
1208 template VectorOfElements operator!=(const cl_double & a, const VectorOfElements & b);
1209 template VectorOfElements operator!=(const cl_float & a, const VectorOfElements & b);
1210 template VectorOfElements operator!=(const cl_int & a, const VectorOfElements & b);
1211 template VectorOfElements operator!=(const cl_uint & a, const VectorOfElements & b);
1212
1213 vector<Element> gcLength2(const VectorOfElements & a, const VectorOfElements & l2)
1214 {
1215 auto type(getElementType(l2));
1216 auto temp(acl::generateVEPrivateVariable(1u, type));
1217 vector<Element> code;
1218 code<<(temp=subVE(a,0));
1219 code<<(l2=temp*temp);
1220 for(unsigned int i(1); i<a.size(); ++i)
1221 {
1222 code<<(temp=subVE(a,i));
1223 code<<(l2+=temp*temp);
1224 }
1225 return code;
1226 }
1227
1228 vector<Element> gcLength(const VectorOfElements & a, const VectorOfElements & l)
1229 {
1230 auto code(gcLength2(a,l));
1231 code.push_back((l=sqrt(l))[0]);
1232 return code;
1233 }
1234
1235 vector<Element> gcNormalize(const VectorOfElements & a)
1236 {
1237 auto type(getElementType(a));
1238 auto len(acl::generateVEPrivateVariable(1u, type));
1239 auto code(gcLength(a,len));
1240 auto res(a /= len);
1241 copy(res.begin(), res.end(), back_inserter(code));
1242
1243 return code;
1244 }
1245
1246
1247
1248 } // namespace acl