Neko 1.99.6
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
math_kernel.h
Go to the documentation of this file.
1#ifndef __MATH_MATH_KERNEL_H__
2#define __MATH_MATH_KERNEL_H__
3/*
4 Copyright (c) 2021-2025, The Neko Authors
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 * Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13
14 * Redistributions in binary form must reproduce the above
15 copyright notice, this list of conditions and the following
16 disclaimer in the documentation and/or other materials provided
17 with the distribution.
18
19 * Neither the name of the authors nor the names of its
20 contributors may be used to endorse or promote products derived
21 from this software without specific prior written permission.
22
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 POSSIBILITY OF SUCH DAMAGE.
35*/
36
38
42template< typename T >
44 const T c,
45 const int n) {
46
47 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
48 const int str = blockDim.x * gridDim.x;
49
50 for (int i = idx; i < n; i += str) {
51 a[i] = c * a[i];
52 }
53}
54
58template< typename T >
60 T * __restrict__ b,
61 int * __restrict__ mask,
62 const int n,
63 const int n_mask) {
64
65 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
66 const int str = blockDim.x * gridDim.x;
67
68 for (int i = idx; i < n_mask; i += str) {
69 a[i] = b[mask[i+1]-1];
70 }
71}
72
73
77template< typename T >
79 T * __restrict__ b,
80 int * __restrict__ mask,
81 const int n,
82 const int n_mask) {
83
84 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
85 const int str = blockDim.x * gridDim.x;
86
87 for (int i = idx; i < n_mask; i += str) {
88 a[i] = b[mask[i]];
89 }
90}
91
93void face_gather_nonlinear_index(int *index, const int idx, const int lx,
94 const int ly, const int lz) {
95 const int idx2 = idx - 1;
96 index[3] = idx2 / (lx * ly * lz);
97 index[2] = (idx2 - (lx * ly * lz) * index[3]) / (lx * ly);
98 index[1] = (idx2 - (lx * ly * lz) * index[3] - (lx * ly) * index[2]) / lx;
99 index[0] = (idx2 - (lx * ly * lz) * index[3] - (lx * ly) * index[2]) -
100 lx * index[1];
101 index[0]++;
102 index[1]++;
103 index[2]++;
104 index[3]++;
105}
106
108int face_gather_idx(const int i, const int j, const int k, const int l,
109 const int n1, const int n2, const int nf) {
110 return ((i) + (n1) * (((j) - 1) + (n2) * (((k) - 1) + (nf) * (((l) - 1))))) - 1;
111}
112
116template< typename T >
118 const T * __restrict__ b,
119 const int * __restrict__ mask,
120 const int * __restrict__ facet,
121 const int n1,
122 const int n2,
123 const int lx,
124 const int ly,
125 const int lz,
126 const int n_mask) {
127 int index[4];
128
129 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
130 const int str = blockDim.x * gridDim.x;
131
132 for (int m = idx; m < n_mask; m += str) {
133 const int f = facet[m + 1];
134 face_gather_nonlinear_index(index, mask[m + 1], lx, ly, lz);
135
136 switch (f) {
137 case 1:
138 case 2:
139 a[m] = b[face_gather_idx(index[1], index[2], f, index[3], n1, n2, 6)];
140 break;
141 case 3:
142 case 4:
143 a[m] = b[face_gather_idx(index[0], index[2], f, index[3], n1, n2, 6)];
144 break;
145 case 5:
146 case 6:
147 a[m] = b[face_gather_idx(index[0], index[1], f, index[3], n1, n2, 6)];
148 break;
149 }
150 }
151}
152
153
157template< typename T >
159 T * __restrict__ b,
160 int * __restrict__ mask,
161 const int n,
162 const int n_mask) {
163
164 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
165 const int str = blockDim.x * gridDim.x;
166
167 for (int i = idx; i < n_mask; i += str) {
168 a[mask[i+1]-1] = b[i];
169 }
170}
171
175template< typename T >
177 T * __restrict__ b,
178 int * __restrict__ mask,
179 const int n,
180 const int n_mask) {
181
182 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
183 const int str = blockDim.x * gridDim.x;
184
185 for (int i = idx; i < n_mask; i += str) {
186 a[mask[i]] = b[i];
187 }
188}
189
190#if __CUDA_ARCH__ < 600
191#include <cassert>
192#endif
193
197template< typename T >
199 T * __restrict__ b,
200 int * __restrict__ mask,
201 const int n,
202 const int m) {
203
204 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
205 const int str = blockDim.x * gridDim.x;
206
207#if __CUDA_ARCH__ >= 600
208 for (int i = idx; i < m; i += str)
209 atomicAdd( &(a[mask[i+1]-1]), b[i]);
210#else
211 if (idx == 0)
212 assert(0 && "masked_atomic_reduction_kernel requires compute capability 6.0 or higher.");
213#endif
214}
215
219template< typename T >
221 T * __restrict__ b,
222 int * __restrict__ mask,
223 const int n,
224 const int n_mask) {
225
226 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
227 const int str = blockDim.x * gridDim.x;
228
229 for (int i = idx; i < n_mask; i += str) {
230 a[mask[i+1]-1] = b[mask[i+1]-1];
231 }
232}
233
237template< typename T >
239 T * __restrict__ b,
240 int * __restrict__ mask,
241 const int n,
242 const int n_mask) {
243
244 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
245 const int str = blockDim.x * gridDim.x;
246
247 for (int i = idx; i < n_mask; i += str) {
248 a[mask[i]] = b[mask[i]];
249 }
250}
251
255template <typename T>
257 const T c,
258 const int size,
259 int* __restrict__ mask,
260 const int mask_size) {
261
262 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
263 const int str = blockDim.x * gridDim.x;
264
265 for (int i = idx; i < mask_size; i += str) { a[mask[i]] = c; }
266}
267
271template< typename T >
273 T * __restrict__ b,
274 const T c,
275 const int n) {
276
277 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
278 const int str = blockDim.x * gridDim.x;
279
280 for (int i = idx; i < n; i += str) {
281 a[i] = c * b[i];
282 }
283}
284
288template< typename T >
290 const T c,
291 const int n) {
292
293 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
294 const int str = blockDim.x * gridDim.x;
295
296 for (int i = idx; i < n; i += str) {
297 a[i] = c / a[i];
298 }
299}
300
304template< typename T >
306 T * __restrict__ b,
307 const T c,
308 const int n) {
309
310 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
311 const int str = blockDim.x * gridDim.x;
312
313 for (int i = idx; i < n; i += str) {
314 a[i] = c / b[i];
315 }
316}
317
321template< typename T >
323 const T c,
324 const int n) {
325
326 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
327 const int str = blockDim.x * gridDim.x;
328
329 for (int i = idx; i < n; i += str) {
330 a[i] = a[i] + c;
331 }
332}
333
337template< typename T >
339 T * __restrict__ b,
340 const T c,
341 const int n) {
342
343 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
344 const int str = blockDim.x * gridDim.x;
345
346 for (int i = idx; i < n; i += str) {
347 a[i] = b[i] + c;
348 }
349}
350
354template< typename T >
356 const T min_val,
357 const T max_val,
358 const int n) {
359
360 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
361 const int str = blockDim.x * gridDim.x;
362 const T l = max_val - min_val;
363
364 for (int i = idx; i < n; i += str) {
365 a[i] = min_val + fmod(fmod(a[i] - min_val, l) + l, l);
366 }
367}
368
372template< typename T >
374 const int n) {
375
376 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
377 const int str = blockDim.x * gridDim.x;
378
379 for (int i = idx; i < n; i += str) {
380 a[i] = sqrt(a[i]);
381 }
382}
383
387template< typename T >
389 const T * __restrict__ a,
390 const T p,
391 const int n) {
392
393 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
394 const int str = blockDim.x * gridDim.x;
395
396 for (int i = idx; i < n; i += str) {
397 ap[i] = pow(a[i], p);
398 }
399}
400
404template< typename T >
406 const T c,
407 const int n) {
408
409 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
410 const int str = blockDim.x * gridDim.x;
411
412 for (int i = idx; i < n; i += str) {
413 a[i] = c;
414 }
415}
416
420template< typename T >
422 const T * __restrict__ b,
423 const int n) {
424
425 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
426 const int str = blockDim.x * gridDim.x;
427
428 for (int i = idx; i < n; i += str) {
429 a[i] = a[i] + b[i];
430 }
431}
432
436template< typename T >
438 const T * __restrict__ b,
439 const T * __restrict__ c,
440 const int n) {
441
442 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
443 const int str = blockDim.x * gridDim.x;
444
445 for (int i = idx; i < n; i += str) {
446 a[i] = b[i] + c[i];
447 }
448}
449
453template< typename T >
455 const T * __restrict__ b,
456 const T * __restrict__ c,
457 const T * __restrict__ d,
458 const int n) {
459
460 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
461 const int str = blockDim.x * gridDim.x;
462
463 for (int i = idx; i < n; i += str) {
464 a[i] = b[i] + c[i] + d[i];
465 }
466}
467
471template< typename T >
473 const T * __restrict__ b,
474 const T c1,
475 const int n) {
476
477 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
478 const int str = blockDim.x * gridDim.x;
479
480 for (int i = idx; i < n; i += str) {
481 a[i] = c1 * a[i] + b[i];
482 }
483}
484
488template< typename T >
490 const T ** p,
491 const T * alpha,
492 const int p_cur,
493 const int n) {
494
495 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
496 const int str = blockDim.x * gridDim.x;
497
498
499 for (int i = idx; i < n; i+= str) {
500 T tmp = 0.0;
501 for (int j = 0; j < p_cur; j ++) {
502 tmp += p[j][i]*alpha[j];
503 }
504 x[i] += tmp;
505 }
506}
507
511template< typename T >
513 const T * __restrict__ b,
514 const T c1,
515 const int n) {
516
517 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
518 const int str = blockDim.x * gridDim.x;
519
520 for (int i = idx; i < n; i += str) {
521 a[i] = a[i] + c1 * b[i];
522 }
523}
524
528template< typename T >
530 const T * __restrict__ b,
531 const T c1,
532 const int n) {
533
534 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
535 const int str = blockDim.x * gridDim.x;
536
537 for (int i = idx; i < n; i += str) {
538 a[i] = a[i] + c1 * (b[i] * b[i]);
539 }
540}
541
545template< typename T >
547 const T * __restrict__ b,
548 const T * __restrict__ c,
549 const T c1,
550 const T c2,
551 const int n) {
552
553 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
554 const int str = blockDim.x * gridDim.x;
555
556 for (int i = idx; i < n; i += str) {
557 a[i] = c1 * b[i] + c2 * c[i];
558 }
559}
560
564template< typename T >
566 const T * __restrict__ b,
567 const T * __restrict__ c,
568 const T * __restrict__ d,
569 const T c1,
570 const T c2,
571 const T c3,
572 const int n) {
573
574 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
575 const int str = blockDim.x * gridDim.x;
576
577 for (int i = idx; i < n; i += str) {
578 a[i] = c1 * b[i] + c2 * c[i] + c3 * d[i];
579 }
580}
581
585template< typename T >
587 const T * __restrict__ b,
588 const T * __restrict__ c,
589 const T * __restrict__ d,
590 const T * __restrict__ e,
591 const T c1,
592 const T c2,
593 const T c3,
594 const T c4,
595 const int n) {
596
597 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
598 const int str = blockDim.x * gridDim.x;
599
600 for (int i = idx; i < n; i += str) {
601 a[i] = a[i] + c1 * b[i] + c2 * c[i] + c3 * d[i] + c4 * e[i];
602 }
603}
604
608template< typename T >
610 const int n) {
611
612 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
613 const int str = blockDim.x * gridDim.x;
614 const T one = 1.0;
615
616 for (int i = idx; i < n; i += str) {
617 a[i] = one / a[i];
618 }
619}
620
624template< typename T >
626 const T * __restrict__ b,
627 const int n) {
628
629 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
630 const int str = blockDim.x * gridDim.x;
631
632 for (int i = idx; i < n; i += str) {
633 a[i] = a[i] / b[i];
634 }
635}
636
640template< typename T >
642 const T * __restrict__ b,
643 const T * __restrict__ c,
644 const int n) {
645
646 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
647 const int str = blockDim.x * gridDim.x;
648
649 for (int i = idx; i < n; i += str) {
650 a[i] = b[i] / c[i];
651 }
652}
653
657template< typename T >
659 const T * __restrict__ b,
660 const int n) {
661
662 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
663 const int str = blockDim.x * gridDim.x;
664
665 for (int i = idx; i < n; i += str) {
666 a[i] = a[i] * b[i];
667 }
668}
669
673template< typename T >
675 const T * __restrict__ b,
676 const T * __restrict__ c,
677 const int n) {
678
679 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
680 const int str = blockDim.x * gridDim.x;
681
682 for (int i = idx; i < n; i += str) {
683 a[i] = b[i] * c[i];
684 }
685}
686
690template< typename T >
692 const T * __restrict__ b,
693 const T * __restrict__ c,
694 const int n) {
695
696 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
697 const int str = blockDim.x * gridDim.x;
698
699 for (int i = idx; i < n; i += str) {
700 a[i] = a[i] - b[i] * c[i];
701 }
702}
703
707template< typename T >
709 const T * __restrict__ b,
710 const int n) {
711
712 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
713 const int str = blockDim.x * gridDim.x;
714
715 for (int i = idx; i < n; i += str) {
716 a[i] = a[i] - b[i];
717 }
718}
719
723template< typename T >
725 const T * __restrict__ b,
726 const T * __restrict__ c,
727 const int n) {
728
729 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
730 const int str = blockDim.x * gridDim.x;
731
732 for (int i = idx; i < n; i += str) {
733 a[i] = b[i] - c[i];
734 }
735}
736
740template< typename T >
742 const T * __restrict__ b,
743 const T * __restrict__ c,
744 const int n) {
745
746 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
747 const int str = blockDim.x * gridDim.x;
748
749 for (int i = idx; i < n; i += str) {
750 a[i] = a[i] + b[i] * c[i];
751 }
752
753}
754
758template< typename T >
760 const T * __restrict__ b,
761 const T * __restrict__ c,
762 const T * __restrict__ d,
763 const int n) {
764
765 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
766 const int str = blockDim.x * gridDim.x;
767
768 for (int i = idx; i < n; i += str) {
769 a[i] = a[i] + b[i] * c[i] * d[i];
770 }
771
772}
773
777template< typename T >
779 const T * __restrict__ b,
780 const T * __restrict__ c,
781 const T s,
782 const int n) {
783
784 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
785 const int str = blockDim.x * gridDim.x;
786
787 for (int i = idx; i < n; i += str) {
788 a[i] = a[i] + s * b[i] * c[i];
789 }
790
791}
792
796template< typename T >
798 const T * __restrict__ u1,
799 const T * __restrict__ u2,
800 const T * __restrict__ u3,
801 const T * __restrict__ v1,
802 const T * __restrict__ v2,
803 const T * __restrict__ v3,
804 const int n) {
805
806 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
807 const int str = blockDim.x * gridDim.x;
808
809 for (int i = idx; i < n; i += str) {
810 dot[i] = u1[i] * v1[i] + u2[i] * v2[i] + u3[i] * v3[i];
811 }
812
813}
814
818template< typename T >
820 T * __restrict__ u2,
821 T * __restrict__ u3,
822 const T * __restrict__ v1,
823 const T * __restrict__ v2,
824 const T * __restrict__ v3,
825 const T * __restrict__ w1,
826 const T * __restrict__ w2,
827 const T * __restrict__ w3,
828 const int n) {
829
830 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
831 const int str = blockDim.x * gridDim.x;
832
833 for (int i = idx; i < n; i += str) {
834 u1[i] = v2[i]*w3[i] - v3[i]*w2[i];
835 u2[i] = v3[i]*w1[i] - v1[i]*w3[i];
836 u3[i] = v1[i]*w2[i] - v2[i]*w1[i];
837 }
838
839}
840
841
845template< typename T>
847 val += __shfl_down_sync(0xffffffff, val, 16);
848 val += __shfl_down_sync(0xffffffff, val, 8);
849 val += __shfl_down_sync(0xffffffff, val, 4);
850 val += __shfl_down_sync(0xffffffff, val, 2);
851 val += __shfl_down_sync(0xffffffff, val, 1);
852 return val;
853}
854
858template< typename T>
860 val = max(val, __shfl_down_sync(0xffffffff, val, 16));
861 val = max(val, __shfl_down_sync(0xffffffff, val, 8));
862 val = max(val, __shfl_down_sync(0xffffffff, val, 4));
863 val = max(val, __shfl_down_sync(0xffffffff, val, 2));
864 val = max(val, __shfl_down_sync(0xffffffff, val, 1));
865 return val;
866}
867
871template< typename T>
873 val = min(val, __shfl_down_sync(0xffffffff, val, 16));
874 val = min(val, __shfl_down_sync(0xffffffff, val, 8));
875 val = min(val, __shfl_down_sync(0xffffffff, val, 4));
876 val = min(val, __shfl_down_sync(0xffffffff, val, 2));
877 val = min(val, __shfl_down_sync(0xffffffff, val, 1));
878 return val;
879}
880
884template< typename T >
885__global__ void reduce_kernel(T * bufred, const int n) {
886
887 T sum = 0;
888 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
889 const int str = blockDim.x * gridDim.x;
890 for (int i = idx; i<n ; i += str)
891 {
892 sum += bufred[i];
893 }
894
895 __shared__ T shared[32];
896 unsigned int lane = threadIdx.x % warpSize;
897 unsigned int wid = threadIdx.x / warpSize;
898
900 if (lane == 0)
901 shared[wid] = sum;
903
904 sum = (threadIdx.x < blockDim.x / warpSize) ? shared[lane] : 0;
905 if (wid == 0)
907
908 if (threadIdx.x == 0)
909 bufred[blockIdx.x] = sum;
910}
911
915template< typename T >
916__global__ void reduce_max_kernel(T * bufred, const T ninf, const int n) {
917
918 T max_val = ninf;
919 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
920 const int str = blockDim.x * gridDim.x;
921 for (int i = idx; i<n ; i += str)
922 {
924 }
925
926 __shared__ T shared[32];
927 unsigned int lane = threadIdx.x % warpSize;
928 unsigned int wid = threadIdx.x / warpSize;
929
931 if (lane == 0)
932 shared[wid] = max_val;
934
936 if (wid == 0)
938
939 if (threadIdx.x == 0)
941}
942
946template< typename T >
947__global__ void reduce_min_kernel(T * bufred, const T pinf, const int n) {
948
949 T min_val = pinf;
950 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
951 const int str = blockDim.x * gridDim.x;
952 for (int i = idx; i<n ; i += str)
953 {
955 }
956
957 __shared__ T shared[32];
958 unsigned int lane = threadIdx.x % warpSize;
959 unsigned int wid = threadIdx.x / warpSize;
960
962 if (lane == 0)
963 shared[wid] = min_val;
965
967 if (wid == 0)
969
970 if (threadIdx.x == 0)
972}
973
978template< typename T_acc >
980 const int n,
981 const int j
982 ) {
983 __shared__ T_acc buf[1024] ;
984 const int idx = threadIdx.x;
985 const int y= blockIdx.x;
986 const int step = blockDim.x;
987
988 buf[idx]=0;
989 for (int i=idx ; i<n ; i+=step)
990 {
991 buf[idx] += bufred[i*j + y];
992 }
994
995 int i = 512;
996 while (i != 0)
997 {
998 if(threadIdx.x < i && (threadIdx.x + i) < n )
999 {
1000 buf[threadIdx.x] += buf[threadIdx.x + i] ;
1001 }
1002 i = i>>1;
1003 __syncthreads();
1004 }
1005
1006 bufred[y] = buf[0];
1007}
1008
1009
1013template< typename T >
1015 const T * b,
1016 const T * c,
1017 T * buf_h,
1018 const int n) {
1019
1020 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1021 const int str = blockDim.x * gridDim.x;
1022
1023 const unsigned int lane = threadIdx.x % warpSize;
1024 const unsigned int wid = threadIdx.x / warpSize;
1025
1026 __shared__ T shared[32];
1027 T sum = 0.0;
1028 for (int i = idx; i < n; i+= str) {
1029 sum += a[i] * b[i] * c[i];
1030 }
1031
1033 if (lane == 0)
1034 shared[wid] = sum;
1035 __syncthreads();
1036
1037 sum = (threadIdx.x < blockDim.x / warpSize) ? shared[lane] : 0;
1038 if (wid == 0)
1040
1041 if (threadIdx.x == 0)
1042 buf_h[blockIdx.x] = sum;
1043}
1044
1048template< typename T, typename T_acc >
1050 const T * b,
1051 const T * c,
1052 T_acc * buf_h,
1053 const int n) {
1054
1055 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1056 const int str = blockDim.x * gridDim.x;
1057
1058 const unsigned int lane = threadIdx.x % warpSize;
1059 const unsigned int wid = threadIdx.x / warpSize;
1060
1062 T_acc sum = 0.0;
1063 for (int i = idx; i < n; i+= str) {
1064 sum += static_cast<T_acc>(a[i] * b[i] * c[i]);
1065 }
1066
1068 if (lane == 0)
1069 shared[wid] = sum;
1070 __syncthreads();
1071
1072 sum = (threadIdx.x < blockDim.x / warpSize) ? shared[lane] : 0;
1073 if (wid == 0)
1075
1076 if (threadIdx.x == 0)
1077 buf_h[blockIdx.x] = sum;
1078}
1079
1083template< typename T, typename T_acc >
1085 const T ** b,
1086 const T * c,
1087 T_acc * buf_h,
1088 const int j,
1089 const int n) {
1090
1091 const int idx = blockIdx.x * blockDim.y + threadIdx.y;
1092 const int str = blockDim.y * gridDim.x;
1093 const int y = threadIdx.x;
1094
1095 __shared__ T_acc buf[1024];
1096 T_acc tmp = 0;
1097 if(y < j){
1098 for (int i = idx; i < n; i+= str) {
1099 tmp += static_cast<T_acc>(a[i] * b[threadIdx.x][i] * c[i]);
1100 }
1101 }
1102
1103 buf[threadIdx.y*blockDim.x+y] = tmp;
1104 __syncthreads();
1105
1106 int i = blockDim.y>>1;
1107 while (i != 0) {
1108 if (threadIdx.y < i) {
1109 buf[threadIdx.y*blockDim.x +y] += buf[(threadIdx.y + i)*blockDim.x+y];
1110 }
1111 __syncthreads();
1112 i = i>>1;
1113 }
1114 if (threadIdx.y == 0) {
1115 if( y < j) {
1116 buf_h[j*blockIdx.x+y] = buf[y];
1117 }
1118 }
1119}
1120
1124template< typename T, typename T_acc >
1126 const T * b,
1127 T_acc * buf_h,
1128 const int n) {
1129
1130 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1131 const int str = blockDim.x * gridDim.x;
1132
1133 const unsigned int lane = threadIdx.x % warpSize;
1134 const unsigned int wid = threadIdx.x / warpSize;
1135
1137 T_acc sum = 0.0;
1138 for (int i = idx; i < n; i+= str) {
1139 sum += static_cast<T_acc>(a[i] * b[i]);
1140 }
1141
1143 if (lane == 0)
1144 shared[wid] = sum;
1145 __syncthreads();
1146
1147 sum = (threadIdx.x < blockDim.x / warpSize) ? shared[lane] : 0;
1148 if (wid == 0)
1150
1151 if (threadIdx.x == 0)
1152 buf_h[blockIdx.x] = sum;
1153
1154}
1155
1159template< typename T, typename T_acc >
1161 const T * b,
1162 T_acc * buf_h,
1163 const int n) {
1164
1165 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1166 const int str = blockDim.x * gridDim.x;
1167
1168 const unsigned int lane = threadIdx.x % warpSize;
1169 const unsigned int wid = threadIdx.x / warpSize;
1170
1172 T_acc sum = 0.0;
1173 for (int i = idx; i < n; i+= str) {
1174 sum += static_cast<T_acc>(pow(a[i] - b[i], 2.0));
1175 }
1176
1178 if (lane == 0)
1179 shared[wid] = sum;
1180 __syncthreads();
1181
1182 sum = (threadIdx.x < blockDim.x / warpSize) ? shared[lane] : 0;
1183 if (wid == 0)
1185
1186 if (threadIdx.x == 0)
1187 buf_h[blockIdx.x] = sum;
1188
1189}
1190
1194template< typename T, typename T_acc >
1196 T_acc * buf_h,
1197 const int n) {
1198
1199 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1200 const int str = blockDim.x * gridDim.x;
1201
1202 const unsigned int lane = threadIdx.x % warpSize;
1203 const unsigned int wid = threadIdx.x / warpSize;
1204
1206 T_acc sum = 0;
1207 for (int i = idx; i<n ; i += str)
1208 {
1209 sum += static_cast<T_acc>(a[i]);
1210 }
1211
1213 if (lane == 0)
1214 shared[wid] = sum;
1215 __syncthreads();
1216
1217 sum = (threadIdx.x < blockDim.x / warpSize) ? shared[lane] : 0;
1218 if (wid == 0)
1220
1221 if (threadIdx.x == 0)
1222 buf_h[blockIdx.x] = sum;
1223
1224}
1225
1229template< typename T >
1231 const T ninf,
1232 T * buf_h,
1233 const int n) {
1234
1235 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1236 const int str = blockDim.x * gridDim.x;
1237
1238 const unsigned int lane = threadIdx.x % warpSize;
1239 const unsigned int wid = threadIdx.x / warpSize;
1240
1241 __shared__ T shared[32];
1242 T max_val = ninf;
1243 for (int i = idx; i<n ; i += str)
1244 {
1245 max_val = max(max_val, a[i]);
1246 }
1247
1249 if (lane == 0)
1250 shared[wid] = max_val;
1251 __syncthreads();
1252
1254 if (wid == 0)
1256
1257 if (threadIdx.x == 0)
1258 buf_h[blockIdx.x] = max_val;
1259
1260}
1261
1265template< typename T >
1267 const T pinf,
1268 T * buf_h,
1269 const int n) {
1270
1271 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1272 const int str = blockDim.x * gridDim.x;
1273
1274 const unsigned int lane = threadIdx.x % warpSize;
1275 const unsigned int wid = threadIdx.x / warpSize;
1276
1277 __shared__ T shared[32];
1278 T min_val = pinf;
1279 for (int i = idx; i<n ; i += str)
1280 {
1281 min_val = min(min_val, a[i]);
1282 }
1283
1285 if (lane == 0)
1286 shared[wid] = min_val;
1287 __syncthreads();
1288
1290 if (wid == 0)
1292
1293 if (threadIdx.x == 0)
1294 buf_h[blockIdx.x] = min_val;
1295
1296}
1297
1301template< typename T >
1303 const int n) {
1304
1305 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1306 const int str = blockDim.x * gridDim.x;
1307
1308 for (int i = idx; i < n; i += str) {
1309 a[i] = fabs(a[i]);
1310 }
1311}
1312
1313// ========================================================================== //
1314// Kernels for the point-wise operations
1315
1320template <typename T>
1321__global__ void
1322 pwmax_vec2_kernel(T* __restrict__ a, const T* __restrict__ b, const int n) {
1323
1324 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1325 const int str = blockDim.x * gridDim.x;
1326
1327 for (int i = idx; i < n; i += str) a[i] = max(a[i], b[i]);
1328}
1329
1334template <typename T>
1336 T* __restrict__ a, const T* __restrict__ b, const T* __restrict__ c,
1337 const int n) {
1338
1339 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1340 const int str = blockDim.x * gridDim.x;
1341
1342 for (int i = idx; i < n; i += str) a[i] = max(b[i], c[i]);
1343}
1344
1349template <typename T>
1350__global__ void pwmax_sca2_kernel(T* __restrict__ a, const T c, const int n) {
1351
1352 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1353 const int str = blockDim.x * gridDim.x;
1354
1355 for (int i = idx; i < n; i += str) a[i] = max(a[i], c);
1356}
1357
1362template <typename T>
1364 T* __restrict__ a, const T* __restrict b, const T c, const int n) {
1365
1366 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1367 const int str = blockDim.x * gridDim.x;
1368
1369 for (int i = idx; i < n; i += str) a[i] = max(b[i], c);
1370}
1371
1376template <typename T>
1377__global__ void
1378 pwmin_vec2_kernel(T* __restrict__ a, const T* __restrict__ b, const int n) {
1379
1380 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1381 const int str = blockDim.x * gridDim.x;
1382
1383 for (int i = idx; i < n; i += str) a[i] = min(a[i], b[i]);
1384}
1385
1390template <typename T>
1392 T* __restrict__ a, const T* __restrict__ b, const T* __restrict__ c,
1393 const int n) {
1394
1395 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1396 const int str = blockDim.x * gridDim.x;
1397
1398 for (int i = idx; i < n; i += str) a[i] = min(b[i], c[i]);
1399}
1400
1405template <typename T>
1406__global__ void pwmin_sca2_kernel(T* __restrict__ a, const T c, const int n) {
1407
1408 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1409 const int str = blockDim.x * gridDim.x;
1410
1411 for (int i = idx; i < n; i += str) a[i] = min(a[i], c);
1412}
1413
1418template <typename T>
1420 T* __restrict__ a, const T* __restrict b, const T c, const int n) {
1421
1422 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1423 const int str = blockDim.x * gridDim.x;
1424
1425 for (int i = idx; i < n; i += str) a[i] = min(b[i], c);
1426}
1427
1428#endif // __MATH_MATH_KERNEL_H__
__global__ void ale_add_kinematics_kernel(const int n, T *__restrict__ wx, T *__restrict__ wy, T *__restrict__ wz, const T *__restrict__ x_ref, const T *__restrict__ y_ref, const T *__restrict__ z_ref, const T *__restrict__ phi, const T *__restrict__ x, const T *__restrict__ y, const T *__restrict__ z, const kinematics_params_t kin_params)
const int i
const int e
const int j
__syncthreads()
__global__ void const T *__restrict__ x
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ w3
__global__ void addcol4_kernel(T *__restrict__ a, const T *__restrict__ b, const T *__restrict__ c, const T *__restrict__ d, const int n)
__global__ void cwrap_kernel(T *__restrict__ a, const T min_val, const T max_val, const int n)
__global__ void pwmin_vec3_kernel(T *__restrict__ a, const T *__restrict__ b, const T *__restrict__ c, const int n)
__global__ void reduce_kernel(T *bufred, const int n)
__global__ void cdiv2_kernel(T *__restrict__ a, T *__restrict__ b, const T c, const int n)
__global__ void invcol2_kernel(T *__restrict__ a, const T *__restrict__ b, const int n)
__global__ void add2_kernel(T *__restrict__ a, const T *__restrict__ b, const int n)
__global__ void add4s3_kernel(T *__restrict__ a, const T *__restrict__ b, const T *__restrict__ c, const T *__restrict__ d, const T c1, const T c2, const T c3, const int n)
__inline__ __device__ T reduce_warp(T val)
__global__ void masked_scatter_copy_aligned_kernel(T *__restrict__ a, T *__restrict__ b, int *__restrict__ mask, const int n, const int n_mask)
__global__ void masked_atomic_reduction_kernel(T *__restrict__ a, T *__restrict__ b, int *__restrict__ mask, const int n, const int m)
__global__ void pwmax_vec3_kernel(T *__restrict__ a, const T *__restrict__ b, const T *__restrict__ c, const int n)
__global__ void glsc2_kernel(const T *a, const T *b, T_acc *buf_h, const int n)
__global__ void addcol3s2_kernel(T *__restrict__ a, const T *__restrict__ b, const T *__restrict__ c, const T s, const int n)
__global__ void cfill_mask_kernel(T *__restrict__ a, const T c, const int size, int *__restrict__ mask, const int mask_size)
__global__ void cdiv_kernel(T *__restrict__ a, const T c, const int n)
__global__ void masked_copy_kernel_aligned(T *__restrict__ a, T *__restrict__ b, int *__restrict__ mask, const int n, const int n_mask)
__global__ void face_masked_gather_copy_kernel(T *__restrict__ a, const T *__restrict__ b, const int *__restrict__ mask, const int *__restrict__ facet, const int n1, const int n2, const int lx, const int ly, const int lz, const int n_mask)
__global__ void masked_gather_copy_aligned_kernel(T *__restrict__ a, T *__restrict__ b, int *__restrict__ mask, const int n, const int n_mask)
Definition math_kernel.h:78
__global__ void pwmax_sca2_kernel(T *__restrict__ a, const T c, const int n)
__device__ __forceinline__ void face_gather_nonlinear_index(int *index, const int idx, const int lx, const int ly, const int lz)
Definition math_kernel.h:93
__global__ void reduce_max_kernel(T *bufred, const T ninf, const int n)
__global__ void pwmin_vec2_kernel(T *__restrict__ a, const T *__restrict__ b, const int n)
__global__ void add3s2_kernel(T *__restrict__ a, const T *__restrict__ b, const T *__restrict__ c, const T c1, const T c2, const int n)
__inline__ __device__ T reduce_max_warp(T val)
__global__ void glsum_kernel(const T *a, T_acc *buf_h, const int n)
__global__ void add2s1_kernel(T *__restrict__ a, const T *__restrict__ b, const T c1, const int n)
__device__ __forceinline__ int face_gather_idx(const int i, const int j, const int k, const int l, const int n1, const int n2, const int nf)
__global__ void add5s4_kernel(T *__restrict__ a, const T *__restrict__ b, const T *__restrict__ c, const T *__restrict__ d, const T *__restrict__ e, const T c1, const T c2, const T c3, const T c4, const int n)
__global__ void masked_gather_copy_kernel(T *__restrict__ a, T *__restrict__ b, int *__restrict__ mask, const int n, const int n_mask)
Definition math_kernel.h:59
__global__ void add2s2_many_kernel(T *__restrict__ x, const T **p, const T *alpha, const int p_cur, const int n)
__global__ void glsc3_many_kernel(const T *a, const T **b, const T *c, T_acc *buf_h, const int j, const int n)
__global__ void pwmax_vec2_kernel(T *__restrict__ a, const T *__restrict__ b, const int n)
__global__ void cmult_kernel(T *__restrict__ a, const T c, const int n)
Definition math_kernel.h:43
__global__ void addcol3_kernel(T *__restrict__ a, const T *__restrict__ b, const T *__restrict__ c, const int n)
__global__ void pwmin_sca3_kernel(T *__restrict__ a, const T *__restrict b, const T c, const int n)
__global__ void pwmax_sca3_kernel(T *__restrict__ a, const T *__restrict b, const T c, const int n)
__global__ void power_kernel(T *__restrict__ ap, const T *__restrict__ a, const T p, const int n)
__global__ void col2_kernel(T *__restrict__ a, const T *__restrict__ b, const int n)
__global__ void masked_copy_kernel_0(T *__restrict__ a, T *__restrict__ b, int *__restrict__ mask, const int n, const int n_mask)
__global__ void sqrt_inplace_kernel(T *__restrict__ a, const int n)
__global__ void col3_kernel(T *__restrict__ a, const T *__restrict__ b, const T *__restrict__ c, const int n)
__global__ void sub2_kernel(T *__restrict__ a, const T *__restrict__ b, const int n)
__global__ void glmin_kernel(const T *a, const T pinf, T *buf_h, const int n)
__global__ void cmult2_kernel(T *__restrict__ a, T *__restrict__ b, const T c, const int n)
__global__ void pwmin_sca2_kernel(T *__restrict__ a, const T c, const int n)
__global__ void sub3_kernel(T *__restrict__ a, const T *__restrict__ b, const T *__restrict__ c, const int n)
__global__ void glsubnorm2_kernel(const T *a, const T *b, T_acc *buf_h, const int n)
__global__ void glsc3_reduce_kernel(T_acc *bufred, const int n, const int j)
__global__ void add2s2_kernel(T *__restrict__ a, const T *__restrict__ b, const T c1, const int n)
__global__ void vdot3_kernel(T *__restrict__ dot, const T *__restrict__ u1, const T *__restrict__ u2, const T *__restrict__ u3, const T *__restrict__ v1, const T *__restrict__ v2, const T *__restrict__ v3, const int n)
__global__ void invcol1_kernel(T *__restrict__ a, const int n)
__global__ void add3_kernel(T *__restrict__ a, const T *__restrict__ b, const T *__restrict__ c, const int n)
__global__ void glmax_kernel(const T *a, const T ninf, T *buf_h, const int n)
__global__ void vlsc3_kernel(const T *a, const T *b, const T *c, T *buf_h, const int n)
__global__ void add4_kernel(T *__restrict__ a, const T *__restrict__ b, const T *__restrict__ c, const T *__restrict__ d, const int n)
__global__ void cfill_kernel(T *__restrict__ a, const T c, const int n)
__global__ void masked_scatter_copy_kernel(T *__restrict__ a, T *__restrict__ b, int *__restrict__ mask, const int n, const int n_mask)
__global__ void glsc3_kernel(const T *a, const T *b, const T *c, T_acc *buf_h, const int n)
__global__ void reduce_min_kernel(T *bufred, const T pinf, const int n)
__global__ void vcross_kernel(T *__restrict__ u1, T *__restrict__ u2, T *__restrict__ u3, const T *__restrict__ v1, const T *__restrict__ v2, const T *__restrict__ v3, const T *__restrict__ w1, const T *__restrict__ w2, const T *__restrict__ w3, const int n)
__global__ void addsqr2s2_kernel(T *__restrict__ a, const T *__restrict__ b, const T c1, const int n)
__global__ void cadd2_kernel(T *__restrict__ a, T *__restrict__ b, const T c, const int n)
__global__ void absval_kernel(T *__restrict__ a, const int n)
__global__ void invcol3_kernel(T *__restrict__ a, const T *__restrict__ b, const T *__restrict__ c, const int n)
__global__ void subcol3_kernel(T *__restrict__ a, const T *__restrict__ b, const T *__restrict__ c, const int n)
__global__ void cadd_kernel(T *__restrict__ a, const T c, const int n)
__inline__ __device__ T reduce_min_warp(T val)
Object for handling masks in Neko.
Definition mask.f90:34
#define max(a, b)
Definition tensor.cu:40