Astaroth  3.1
Scalable Multi-GPU Stencil Computations
Loading...
Searching...
No Matches
math_utils.h
Go to the documentation of this file.
1/*/
2 Copyright (C) 2014-2026, Johannes Pekkila, Miikka Vaisala, Touko Puro.
3
4 This file is part of Astaroth.
5
6 Astaroth is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Astaroth is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Astaroth. If not, see <http://www.gnu.org/licenses/>.
18*/
19
27#pragma once
28#include <math.h> // isnan, isinf
29#include <stdint.h> // uint64_t
30#include <stdlib.h> // rand
31
32#include "datatypes.h"
33#include "errchk.h"
34#include "func_attributes.h"
35#include "math_utils_base.h"
36
37#define __ac__max(a,b) (a > b ? a : b)
38#define __ac__min(a,b) (a < b ? a : b)
39
40#if AC_DOUBLE_PRECISION != 1
41#ifndef __cplusplus
42#define exp(x) expf(x)
43#define sin(x) sinf(x)
44#define cos(x) cosf(x)
45#define sqrt(x) sqrtf(x)
46#define fabs(x) fabsf(x)
47#endif
48#endif
49
50#ifdef __cplusplus
51
52static HOST_DEVICE_INLINE int3
53operator/(const int3& a, const int& b)
54{
55 return (int3)
56 {
57 a.x/b,
58 a.y/b,
59 a.z/b
60 };
61}
62
63static HOST_DEVICE_INLINE int3
64operator*(const int3& a, const int b)
65{
66 return (int3){a.x * b, a.y * b, a.z * b};
67}
68
69#if AC_USE_HIP
70#else
71static HOST_DEVICE_INLINE void
72operator -=(int3& lhs, const int3& rhs)
73{
74 lhs.x -= rhs.x;
75 lhs.y -= rhs.y;
76 lhs.z -= rhs.z;
77}
78
79static HOST_DEVICE_INLINE int3
80operator*(const int3& a, const int3& b)
81{
82 return (int3){a.x * b.x, a.y * b.y, a.z * b.z};
83}
84
85
86static HOST_DEVICE_INLINE dim3
87operator*(const dim3& a, const dim3& b)
88{
89 return (dim3){a.x * b.x, a.y * b.y, a.z * b.z};
90}
91
92static HOST_DEVICE_INLINE int3
93operator*(const dim3& a, const int3& b)
94{
95 return (int3){(int)(a.x * b.x),(int)(a.y * b.y),(int)(a.z * b.z)};
96}
97
98static HOST_DEVICE_INLINE int3
99operator-(const int3& a, const uint3& b)
100{
101 return (int3){(int)(a.x - b.x), (int)(a.y - b.y), (int)(a.z - b.z)};
102}
103static HOST_DEVICE_INLINE int3
104operator+(const int3& a, const uint3& b)
105{
106 return (int3){(int)(a.x + b.x), (int)(a.y + b.y), (int)(a.z + b.z)};
107}
108
109#endif
110static HOST_DEVICE_INLINE AcComplex
111exp(const AcComplex& val)
112{
113 return (AcComplex){exp(val.x) * cos(val.y), exp(val.x) * sin(val.y)};
114}
115
116static HOST_DEVICE_INLINE AcComplex
117operator*(const AcComplex& a, const AcComplex& b)
118{
119 return (AcComplex){a.x*b.x - a.y*b.y,a.x*b.y + b.x*a.y};
120}
121static HOST_DEVICE_INLINE AcComplex
122operator/(const AcComplex& a, const AcComplex& b)
123{
124 const AcReal divisor = b.x*b.x + b.y*b.y;
125 return (AcComplex){(a.x*b.x + a.y*b.y)/(divisor),(a.y*b.x - a.x*b.y)/(divisor)};
126}
127
128static HOST_DEVICE_INLINE AcComplex
129operator/(const AcReal& a, const AcComplex& b)
130{
131 const AcReal divisor = b.x*b.x + b.y*b.y;
132 return (AcComplex){(a*b.x)/(divisor),(-a*b.y)/(divisor)};
133}
134
135static HOST_DEVICE_INLINE AcComplex
136operator/(const AcComplex& a, const AcReal& b)
137{
138 return
139 (AcComplex)
140 {
141 a.x/b,
142 a.y/b
143 };
144}
145
146static HOST_DEVICE_INLINE AcComplex
147operator-(const AcReal& a, const AcComplex& b)
148{
149 return (AcComplex)
150 {
151 a-b.x,
152 -b.y
153 };
154}
155
156static HOST_DEVICE_INLINE AcComplex
157operator+(const AcReal& a, const AcComplex& b)
158{
159 return (AcComplex)
160 {
161 b.x+a,
162 b.y
163 };
164}
165
166static HOST_DEVICE_INLINE AcComplex
167operator-(const AcComplex& a, const AcReal& b)
168{
169 return (AcComplex)
170 {
171 a.x-b,
172 a.y
173 };
174}
175
176static HOST_DEVICE_INLINE AcComplex
177operator+(const AcComplex& a, const AcReal& b)
178{
179 return (AcComplex)
180 {
181 a.x+b,
182 a.y
183 };
184}
185
186
187static HOST_DEVICE_INLINE AcComplex
188sqrt(const AcComplex& a)
189{
190 const AcReal r = sqrt(a.x*a.x + a.y*a.y);
191 const AcReal y_sign = (a.y < AcReal(0.0)) ? AcReal(-1.0) : AcReal(1.0);
192 return (AcComplex){sqrt((r + a.x)*AcReal(0.5)),y_sign * sqrt((r-a.x)*AcReal(0.5))};
193}
194
195typedef struct uint3_64 {
196 uint64_t x, y, z;
197 uint3_64(const int3& a):
198 x(static_cast<uint64_t>(a.x)), y(static_cast<uint64_t>(a.y)), z(static_cast<uint64_t>(a.z)) {}
199 uint3_64(const uint64_t _x, const uint64_t _y, const uint64_t _z):
200 x(_x), y(_y), z(_z) {}
201 uint3_64():
202 x(), y(), z() {}
203 explicit inline constexpr operator int3() const
204 {
205 return (int3){(int)x, (int)y, (int)z};
206 }
207} uint3_64;
208
209template <class T>
210static HOST_DEVICE_INLINE const T
211val(const T& a)
212{
213 return a;
214}
215
216template <class T>
217static HOST_DEVICE_INLINE const T
218sum(const T& a, const T& b)
219{
220 return a + b;
221}
222
223template <class T>
224static HOST_DEVICE_INLINE const T
225max(const T& a, const T& b, const T& c)
226{
227 const auto tmp = a > b ? a : b;
228 return (tmp > c) ? tmp : c;
229}
230template <typename T>
232max(const T& a,const T& b,const T& c,const T& d)
233{
234 auto res = a > b ? a : b;
235 res = res > c ? res : c;
236 res = res > d ? res : d;
237 return res;
238}
239
240template <typename T>
242max(const T& a,const T& b,const T& c,const T& d, const T& e)
243{
244 auto res = a > b ? a : b;
245 res = res > c ? res : c;
246 res = res > d ? res : d;
247 res = res > e ? res : e;
248 return res;
249}
250
251template <typename T>
253max(const T& a,const T& b,const T& c,const T& d, const T& e, const T& f)
254{
255 auto res = a > b ? a : b;
256 res = res > c ? res : c;
257 res = res > d ? res : d;
258 res = res > e ? res : e;
259 res = res > f ? res : f;
260 return res;
261}
262
263template <typename T>
265max(const T& a,const T& b,const T& c,const T& d, const T& e, const T& f,const T& g)
266{
267 auto res = a > b ? a : b;
268 res = res > c ? res : c;
269 res = res > d ? res : d;
270 res = res > e ? res : e;
271 res = res > f ? res : f;
272 res = res > g ? res : g;
273 return res;
274}
275
276template <typename T>
278max(const T& a,const T& b,const T& c,const T& d, const T& e, const T& f, const T& g, const T& h)
279{
280 auto res = a > b ? a : b;
281 res = res > c ? res : c;
282 res = res > d ? res : d;
283 res = res > e ? res : e;
284 res = res > f ? res : f;
285 res = res > g ? res : g;
286 res = res > h ? res : h;
287 return res;
288}
289
290
291template <class T>
292static HOST_DEVICE_INLINE const T
293min(const T& a, const T& b, const T& c)
294{
295 const auto tmp = a < b ? a : b;
296 return (tmp < c) ? tmp : c;
297}
298
299static inline const int3
300max(const int3& a, const int3& b)
301{
302 return (int3){max(a.x, b.x), max(a.y, b.y), max(a.z, b.z)};
303}
304
305
306
307static inline const int3
308min(const int3& a, const int3& b)
309{
310 return (int3){min(a.x, b.x), min(a.y, b.y), min(a.z, b.z)};
311}
312
313
314template <class T>
315static inline const T
316clamp(const T& val, const T& min, const T& max)
317{
318 return val < min ? min : val > max ? max : val;
319}
320
321static HOST_DEVICE_INLINE uint64_t
322mod(const int a, const int b)
323{
324 return ((a%b) + b) % b;
325}
326
327
328static inline bool
329is_power_of_two(const unsigned val)
330{
331 return val && !(val & (val - 1));
332}
333
334/*
335 * INT3
336 */
337static HOST_DEVICE_INLINE int3
338operator+(const int3& a, const int3& b)
339{
340 return (int3){a.x + b.x, a.y + b.y, a.z + b.z};
341}
342
343static HOST_DEVICE_INLINE int3
344operator-(const int3& a)
345{
346 return (int3){-a.x, -a.y, -a.z};
347}
348
349static HOST_DEVICE_INLINE AcReal3
350operator*(const int3& a, const AcReal3& b)
351{
352 return (AcReal3){a.x * b.x, a.y * b.y, a.z * b.z};
353}
354
355static HOST_DEVICE_INLINE AcReal3
356operator*(const AcReal3& a, const int3& b)
357{
358 return (AcReal3){a.x * b.x, a.y * b.y, a.z * b.z};
359}
360
361static HOST_DEVICE_INLINE int3
362operator*(const int& a, const int3& b)
363{
364 return (int3){a * b.x, a * b.y, a * b.z};
365}
366
367static HOST_DEVICE_INLINE int3
368operator+(const int3& a, const int& b)
369{
370 return (int3){a.x + b, a.y + b, a.z + b};
371}
372
373static HOST_DEVICE_INLINE int3
374operator+(const int3& a, const AcBool3& b)
375{
376 return (int3){a.x + b.x, a.y + b.y, a.z + b.z};
377}
378
379static HOST_DEVICE_INLINE int3
380operator+(const AcBool3& a, const int3& b)
381{
382 return (int3){a.x + b.x, a.y + b.y, a.z + b.z};
383}
384
385static HOST_DEVICE_INLINE int3
386operator+(const int& a, const int3& b)
387{
388 return (int3){a + b.x, a + b.y, a + b.z};
389}
390
391static HOST_DEVICE_INLINE int3
392operator-(const int3& a, const int3& b)
393{
394 return (int3){a.x - b.x, a.y - b.y, a.z - b.z};
395}
396
397
398static HOST_DEVICE_INLINE int3
399operator-(const int& a, const int3& b)
400{
401 return (int3){a - b.x, a - b.y, a - b.z};
402}
403
404static HOST_DEVICE_INLINE int3
405operator-(const int3& a, const int& b)
406{
407 return (int3){a.x - b, a.y - b, a.z - b};
408}
409
410
411
412static HOST_DEVICE_INLINE bool
413operator>=(const int3& a, const int3& b)
414{
415 return a.x >= b.x && a.y >= b.y && a.z >= b.z;
416}
417
418static HOST_DEVICE_INLINE bool
419operator>(const int3& a, const int3& b)
420{
421 return a.x > b.x && a.y > b.y && a.z > b.z;
422}
423
424static HOST_DEVICE_INLINE bool
425operator<=(const int3& a, const int3& b)
426{
427 return a.x <= b.x && a.y <= b.y && a.z <= b.z;
428}
429
430
431static HOST_DEVICE_INLINE bool
432operator>=(const int3& a, const int& b)
433{
434 return a.x >= b && a.y >= b && a.z >= b;
435}
436
437static HOST_DEVICE_INLINE bool
438operator<=(const int3& a, const int& b)
439{
440 return a.x <= b && a.y <= b && a.z <= b;
441}
442
443static HOST_DEVICE_INLINE bool
444operator<=(const Volume& a, const Volume& b)
445{
446 return a.x <= b.x && a.y <= b.y && a.z <= b.z;
447}
448
449
450
451/*
452 * UINT3_64
453 */
454static HOST_INLINE uint3_64
455operator+(const uint3_64& a, const uint3_64& b)
456{
457 return (uint3_64){a.x + b.x, a.y + b.y, a.z + b.z};
458}
459
460static HOST_INLINE uint3_64
461operator-(const uint3_64& a, const uint3_64& b)
462{
463 return (uint3_64){a.x - b.x, a.y - b.y, a.z - b.z};
464}
465
466static HOST_INLINE uint3_64
467operator*(const uint3_64& a, const uint3_64& b)
468{
469 return (uint3_64){a.x * b.x, a.y * b.y, a.z * b.z};
470}
471
472static inline uint3_64
473operator*(const int& a, const uint3_64& b)
474{
475 return (uint3_64){as_size_t(a) * b.x, as_size_t(a) * b.y, as_size_t(a) * b.z};
476}
477
478static inline uint3_64
479operator/(const int3& a, const uint3_64& b)
480{
481 return (uint3_64){as_size_t(a.x) / b.x, as_size_t(a.y) / b.y, as_size_t(a.z) / b.z};
482}
483
484static inline uint3_64
485operator/(const uint3_64& a, const int3& b)
486{
487 return (uint3_64){a.x / as_size_t(b.x), a.y / as_size_t(b.y), a.z / as_size_t(b.z)};
488}
489
490
491static inline uint3_64
492operator/(const Volume& a, const uint3_64& b)
493{
494 return (uint3_64){a.x / b.x, a.y / b.y, a.z / b.z};
495}
496
497static inline uint3_64
498operator+(const uint3_64& a, const Volume& b)
499{
500 return (uint3_64){a.x + b.x, a.y + b.y, a.z + b.z};
501}
502
503static inline uint3_64
504operator+(const Volume& a, const uint3_64& b)
505{
506 return (uint3_64){a.x + b.x, a.y + b.y, a.z + b.z};
507}
508
509static inline uint3_64
510operator/(const uint3_64& a, const Volume& b)
511{
512 return (uint3_64){a.x / b.x, a.y / b.y, a.z / b.z};
513}
514
515static HOST_DEVICE_INLINE bool
516operator==(const uint3_64& a, const uint3_64& b)
517{
518 return a.x == b.x && a.y == b.y && a.z == b.z;
519}
520
521
522
523static HOST_DEVICE_INLINE bool
524operator==(const dim3& a, const dim3& b)
525{
526 return
527 a.x == b.x &&
528 a.y == b.y &&
529 a.z == b.z;
530
531}
532template <class T>
533static int
534volume_size(const T a)
535{
536 return a.x*a.y*a.z;
537}
538
539/*
540 * AcReal
541 */
542static HOST_DEVICE_INLINE bool
543is_valid(const AcReal a)
544{
545 return !isnan(a) && !isinf(a);
546}
547
548
550AC_dot(const AcReal2& a, const AcReal2& b)
551{
552 return a.x * b.x + a.y * b.y;
553}
554
555static HOST_DEVICE_INLINE bool
556is_valid(const AcReal2& a)
557{
558 return is_valid(a.x) && is_valid(a.y);
559}
560
561/*
562 * AcReal3
563 */
564
565
566//static HOST_DEVICE_INLINE AcBool3
567//operator!=(const AcReal3& a, const AcReal b)
568//{
569// return (AcBool3){
570// a.x != b,
571// a.y != b,
572// a.z != b
573// };
574//}
575//
576//
577//static HOST_DEVICE_INLINE AcBool3
578//operator==(const AcReal3& a, const AcReal b)
579//{
580// return (AcBool3){
581// a.x == b,
582// a.y == b,
583// a.z == b
584// };
585//}
586
587static HOST_DEVICE_INLINE const AcReal3
588max(const AcReal3& v, const AcReal& s)
589{
590 return (AcReal3)
591 {
592 __ac__max(v.x,s),
593 __ac__max(v.y,s),
594 __ac__max(v.z,s)
595 };
596}
597
598static HOST_DEVICE_INLINE const AcReal3
599max(const AcReal& s, const AcReal3& v)
600{
601 return (AcReal3)
602 {
603 __ac__max(v.x,s),
604 __ac__max(v.y,s),
605 __ac__max(v.z,s)
606 };
607}
608
610sum(const AcReal3& a)
611{
612 return a.x+a.y+a.z;
613}
614
616AC_dot(const AcReal3& a, const AcReal3& b)
617{
618 return a.x * b.x + a.y * b.y + a.z * b.z;
619}
620
622AC_dot(const int3& a, const AcReal3& b)
623{
624 return a.x * b.x + a.y * b.y + a.z * b.z;
625}
626
628AC_dot(const AcReal3& a, const int3& b)
629{
630 return a.x * b.x + a.y * b.y + a.z * b.z;
631}
632
633static HOST_DEVICE_INLINE AcReal3
634AC_cross(const AcReal3& a, const AcReal3& b)
635{
636 return
637 {
638 a.y * b.z - a.z * b.y,
639 a.z * b.x - a.x * b.z,
640 a.x * b.y - a.y * b.x,
641 };
642}
643
644static HOST_DEVICE_INLINE bool
645is_valid(const AcReal3& a)
646{
647 return is_valid(a.x) && is_valid(a.y) && is_valid(a.z);
648}
649
650/*
651 * AcMatrix
652 */
653
654
655typedef struct AcMatrix {
656 AcReal data[3][3]{};
657 HOST_DEVICE_INLINE AcMatrix() {}
658
659 HOST_DEVICE_INLINE AcMatrix(const AcReal3& row0, const AcReal3& row1,
660 const AcReal3& row2)
661 {
662 data[0][0] = row0.x;
663 data[0][1] = row0.y;
664 data[0][2] = row0.z;
665
666 data[1][0] = row1.x;
667 data[1][1] = row1.y;
668 data[1][2] = row1.z;
669
670 data[2][0] = row2.x;
671 data[2][1] = row2.y;
672 data[2][2] = row2.z;
673 }
674
675 HOST_DEVICE_INLINE AcReal3 row(const int row) const
676 {
677 return (AcReal3){data[row][0], data[row][1], data[row][2]};
678 }
679 HOST_DEVICE_INLINE AcReal3 col(const int col) const
680 {
681 return (AcReal3){data[0][col], data[1][col], data[2][col]};
682 }
683
684 HOST_DEVICE_INLINE AcReal3 operator*(const AcReal3& v) const
685 {
686 return (AcReal3){
687 AC_dot(row(0), v),
688 AC_dot(row(1), v),
689 AC_dot(row(2), v),
690 };
691 }
692
693 HOST_DEVICE_INLINE AcMatrix operator-() const
694 {
695 return AcMatrix(-row(0), -row(1), -row(2));
696 }
697 HOST_DEVICE_INLINE AcMatrix& operator=(const AcReal& s)
698 {
699 data[0][0] = s;
700 data[1][0] = s;
701 data[2][0] = s;
702
703 data[0][1] = s;
704 data[1][1] = s;
705 data[2][1] = s;
706
707 data[0][2] = s;
708 data[1][2] = s;
709 data[2][2] = s;
710
711 return *this;
712 }
713} AcMatrix;
714
716multm2_sym(const AcMatrix m)
717{
718 AcReal res = m.data[0][0]*m.data[0][0];
719 for(int i = 1; i < 3; ++i)
720 {
721 res += m.data[i][i]*m.data[i][i];
722 for(int j = 0; j < i; ++j)
723 {
724 res += 2*m.data[i][j]*m.data[i][j];
725 }
726 }
727 return res;
728}
729
730static HOST_DEVICE_INLINE AcMatrix
731operator*(const AcReal& v, const AcMatrix& m)
732{
733 AcMatrix out;
734
735 out.data[0][0] = v * m.data[0][0];
736 out.data[0][1] = v * m.data[0][1];
737 out.data[0][2] = v * m.data[0][2];
738
739 out.data[1][0] = v * m.data[1][0];
740 out.data[1][1] = v * m.data[1][1];
741 out.data[1][2] = v * m.data[1][2];
742
743 out.data[2][0] = v * m.data[2][0];
744 out.data[2][1] = v * m.data[2][1];
745 out.data[2][2] = v * m.data[2][2];
746
747 return out;
748}
749
750static HOST_DEVICE_INLINE AcMatrix
751operator/(const AcMatrix& m, const AcReal& v)
752{
753 AcMatrix out;
754
755 out.data[0][0] = m.data[0][0] / v;
756 out.data[0][1] = m.data[0][1] / v;
757 out.data[0][2] = m.data[0][2] / v;
758
759 out.data[1][0] = m.data[1][0] / v;
760 out.data[1][1] = m.data[1][1] / v;
761 out.data[1][2] = m.data[1][2] / v;
762
763 out.data[2][0] = m.data[2][0] / v;
764 out.data[2][1] = m.data[2][1] / v;
765 out.data[2][2] = m.data[2][2] / v;
766
767 return out;
768}
769
770static HOST_DEVICE_INLINE AcMatrix
771operator+(const AcMatrix& a, const AcMatrix& b)
772{
773 AcMatrix out;
774
775 out.data[0][0] = a.data[0][0] + b.data[0][0];
776 out.data[0][1] = a.data[0][1] + b.data[0][1];
777 out.data[0][2] = a.data[0][2] + b.data[0][2];
778
779 out.data[1][0] = a.data[1][0] + b.data[1][0];
780 out.data[1][1] = a.data[1][1] + b.data[1][1];
781 out.data[1][2] = a.data[1][2] + b.data[1][2];
782
783 out.data[2][0] = a.data[2][0] + b.data[2][0];
784 out.data[2][1] = a.data[2][1] + b.data[2][1];
785 out.data[2][2] = a.data[2][2] + b.data[2][2];
786
787 return out;
788}
789
790static HOST_DEVICE_INLINE AcMatrix
791operator-(const AcMatrix& a, const AcMatrix& b)
792{
793 AcMatrix out;
794
795 out.data[0][0] = a.data[0][0] - b.data[0][0];
796 out.data[0][1] = a.data[0][1] - b.data[0][1];
797 out.data[0][2] = a.data[0][2] - b.data[0][2];
798
799 out.data[1][0] = a.data[1][0] - b.data[1][0];
800 out.data[1][1] = a.data[1][1] - b.data[1][1];
801 out.data[1][2] = a.data[1][2] - b.data[1][2];
802
803 out.data[2][0] = a.data[2][0] - b.data[2][0];
804 out.data[2][1] = a.data[2][1] - b.data[2][1];
805 out.data[2][2] = a.data[2][2] - b.data[2][2];
806
807 return out;
808}
809
810static HOST_DEVICE_INLINE AcReal3
811operator*(AcReal3 v, const AcMatrix& m)
812{
813 return (AcReal3){
814 AC_dot(m.col(0), v),
815 AC_dot(m.col(1), v),
816 AC_dot(m.col(2), v),
817 };
818}
819
820
821static HOST_DEVICE_INLINE AcMatrix
822operator*(AcMatrix a, const AcMatrix& b)
823{
824 AcMatrix out;
825
826 out.data[0][0] = AC_dot(a.row(0),b.col(0));
827 out.data[0][1] = AC_dot(a.row(0),b.col(1));
828 out.data[0][2] = AC_dot(a.row(0),b.col(2));
829
830 out.data[1][0] = AC_dot(a.row(1),b.col(0));
831 out.data[1][1] = AC_dot(a.row(1),b.col(1));
832 out.data[1][2] = AC_dot(a.row(1),b.col(2));
833
834 out.data[2][0] = AC_dot(a.row(2),b.col(0));
835 out.data[2][1] = AC_dot(a.row(2),b.col(1));
836 out.data[2][2] = AC_dot(a.row(2),b.col(2));
837
838 return out;
839
840}
841
918static HOST_INLINE Volume
919operator*(const int3& a, const Volume& b)
920{
921 ERRCHK_ALWAYS(a.x >= 0);
922 ERRCHK_ALWAYS(a.y >= 0);
923 ERRCHK_ALWAYS(a.z >= 0);
924 return
925 {
926 b.x*(size_t)a.x,
927 b.y*(size_t)a.y,
928 b.z*(size_t)a.z
929 };
930
931}
932static HOST_INLINE Volume
933operator+(const int3& a, const Volume& b)
934{
935 ERRCHK_ALWAYS((int)b.x >= -a.x);
936 ERRCHK_ALWAYS((int)b.y >= -a.y);
937 ERRCHK_ALWAYS((int)b.z >= -a.z);
938 return
939 {
940 (size_t)(b.x+a.x),
941 (size_t)(b.y+a.y),
942 (size_t)(b.z+a.z)
943 };
944
945}
946static HOST_INLINE AcReal3
947operator*(const Volume& a, const AcReal3& b)
948{
949 return
950 {
951 a.x*b.x,
952 a.y*b.y,
953 a.z*b.z
954 };
955
956}
957static HOST_INLINE AcReal3
958operator*(const AcReal3& a, const Volume& b)
959{
960 return
961 {
962 a.x*b.x,
963 a.y*b.y,
964 a.z*b.z
965 };
966
967}
968
969static HOST_INLINE Volume&
970operator-=(Volume& a, const int3& b)
971{
972 ERRCHK_ALWAYS((int)a.x >= b.x);
973 ERRCHK_ALWAYS((int)a.y >= b.y);
974 ERRCHK_ALWAYS((int)a.z >= b.z);
975 a.x -= b.x;
976 a.y -= b.y;
977 a.z -= b.z;
978 return a;
979}
980static HOST_INLINE Volume&
981operator-=(Volume& a, const Volume& b)
982{
983 ERRCHK_ALWAYS(a.x >= b.x);
984 ERRCHK_ALWAYS(a.y >= b.y);
985 ERRCHK_ALWAYS(a.z >= b.z);
986 a.x -= b.x;
987 a.y -= b.y;
988 a.z -= b.z;
989 return a;
990}
991
992static HOST_DEVICE_INLINE bool
993operator==(const Volume& a, const Volume& b)
994{
995 return
996 a.x == b.x &&
997 a.y == b.y &&
998 a.z == b.z;
999
1000}
1001static HOST_INLINE bool
1002operator!=(const Volume& a, const Volume& b)
1003{
1004 return
1005 a.x != b.x ||
1006 a.y != b.y ||
1007 a.z != b.z;
1008
1009}
1010typedef struct AcTensor {
1011 AcReal data[3][3][3]{};
1012
1013 HOST_DEVICE_INLINE AcTensor() {}
1014
1015 HOST_DEVICE_INLINE AcTensor(const AcMatrix& m0, const AcMatrix& m1,
1016 const AcMatrix& m2)
1017 {
1018 data[0][0][0] = m0.data[0][0];
1019 data[0][1][0] = m0.data[0][1];
1020 data[0][2][0] = m0.data[0][2];
1021
1022 data[1][0][0] = m0.data[1][0];
1023 data[1][1][0] = m0.data[1][1];
1024 data[1][2][0] = m0.data[1][2];
1025
1026 data[2][0][0] = m0.data[2][0];
1027 data[2][1][0] = m0.data[2][1];
1028 data[2][2][0] = m0.data[2][2];
1029
1030 data[0][0][1] = m1.data[0][0];
1031 data[0][1][1] = m1.data[0][1];
1032 data[0][2][1] = m1.data[0][2];
1033
1034 data[1][0][1] = m1.data[1][0];
1035 data[1][1][1] = m1.data[1][1];
1036 data[1][2][1] = m1.data[1][2];
1037
1038 data[2][0][1] = m1.data[2][0];
1039 data[2][1][1] = m1.data[2][1];
1040 data[2][2][1] = m1.data[2][2];
1041
1042 data[0][0][2] = m2.data[0][0];
1043 data[0][1][2] = m2.data[0][1];
1044 data[0][2][2] = m2.data[0][2];
1045
1046 data[1][0][2] = m2.data[1][0];
1047 data[1][1][2] = m2.data[1][1];
1048 data[1][2][2] = m2.data[1][2];
1049
1050 data[2][0][2] = m2.data[2][0];
1051 data[2][1][2] = m2.data[2][1];
1052 data[2][2][2] = m2.data[2][2];
1053
1054
1055 }
1056} AcTensor;
1057
1058HOST_DEVICE_INLINE AcReal3
1059matmul_arr(const AcReal* m, const AcReal3& v)
1060{
1061 AcReal x = m[0 + 3*0]*v.x;
1062 x += m[0 + 3*1]*v.y;
1063 x += m[0 + 3*2]*v.z;
1064
1065 AcReal y = m[1 + 3*0]*v.x;
1066 y += m[1 + 3*1]*v.y;
1067 y += m[1 + 3*2]*v.z;
1068
1069 AcReal z = m[2 + 3*0]*v.x;
1070 z += m[2 + 3*1]*v.y;
1071 z += m[2 + 3*2]*v.z;
1072 return (AcReal3){x,y,z};
1073}
1074HOST_DEVICE_INLINE AcReal3
1075matmul_arr(AcReal* m, const AcReal3& v)
1076{
1077 AcReal x = m[0 + 3*0]*v.x;
1078 x += m[0 + 3*1]*v.y;
1079 x += m[0 + 3*2]*v.z;
1080
1081 AcReal y = m[1 + 3*0]*v.x;
1082 y += m[1 + 3*1]*v.y;
1083 y += m[1 + 3*2]*v.z;
1084
1085 AcReal z = m[2 + 3*0]*v.x;
1086 z += m[2 + 3*1]*v.y;
1087 z += m[2 + 3*2]*v.z;
1088 return (AcReal3){x,y,z};
1089}
1090template <typename T>
1092broadcast_scalar(T& arr, const AcReal& val)
1093{
1094 const size_t arr_len = sizeof(arr)/sizeof(arr[0]);
1095 for(size_t i = 0; i < arr_len; ++i)
1096 arr[i] = val;
1097}
1098template <const size_t N1, const size_t N2>
1100broadcast_scalar_2d(AcReal (&arr)[N1][N2], const AcReal& val)
1101{
1102 for(size_t i = 0; i < N1; ++i)
1103 for(size_t j= 0; j < N2; ++j)
1104 arr[i][j] = val;
1105}
1106template <typename T>
1108broadcast_scalar_to_vec(T& arr, const AcReal& val)
1109{
1110 const size_t arr_len = sizeof(arr)/sizeof(arr[0]);
1111 for(size_t i = 0; i < arr_len; ++i)
1112 arr[i] = (AcReal3){val,val,val};
1113}
1114template <typename T1,typename T2>
1116copy_arr(T1& a, const T2& b)
1117{
1118 const size_t arr_len = sizeof(a)/sizeof(a[0]);
1119 for(size_t i = 0; i < arr_len; ++i)
1120 a[i] = b[i];
1121}
1122HOST_DEVICE_INLINE uint64_t
1123max(const uint64_t& a, const int& b)
1124{
1125 return a > (uint64_t)b ? a : (uint64_t)b;
1126}
1127template <size_t N>
1128HOST_DEVICE_INLINE size_t
1129AC_get_array_len(const Field (&arr)[N])
1130{
1131 (void)arr;
1132 return N;
1133}
1134
1135static HOST_INLINE int3
1136to_int3(const uint3_64 a)
1137{
1138 return
1139 (int3)
1140 {
1141 (int)a.x,
1142 (int)a.y,
1143 (int)a.z
1144 };
1145}
1146
1147
1148#include "generated_comp_funcs.h"
1149
1150static HOST_DEVICE_INLINE bool
1151operator==(const AcMeshDims& a, const AcMeshDims& b)
1152{
1153 return
1154 a.n0 == b.n0 &&
1155 a.n1 == b.n1 &&
1156 a.m0 == b.m0 &&
1157 a.m1 == b.m1 &&
1158 a.nn == b.nn &&
1160}
1161
1162static HOST_DEVICE_INLINE int
1163AC_abs(const int& x) {return abs(x);}
1164
1165#endif /* __cplusplus */
float AcReal
Definition acreal.h:11
Brief info.
#define as_size_t(i)
Definition errchk.h:101
#define ERRCHK_ALWAYS(retval)
Definition errchk.h:95
#define HOST_INLINE
Definition func_attributes.h:16
#define HOST_DEVICE_INLINE
Definition func_attributes.h:15
#define sin(x)
Definition math_utils.h:43
#define cos(x)
Definition math_utils.h:44
#define __ac__max(a, b)
Definition math_utils.h:37
#define sqrt(x)
Definition math_utils.h:45
#define exp(x)
Definition math_utils.h:42
Definition host_datatypes.h:84
size3_t n0
Definition host_datatypes.h:85
size3_t m1
Definition host_datatypes.h:86
size3_t n1
Definition host_datatypes.h:85
size3_t reduction_tile
Definition host_datatypes.h:88
size3_t nn
Definition host_datatypes.h:87
size3_t m0
Definition host_datatypes.h:86