Neko 1.99.9
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-2026, 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
37#include "wave.h"
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
76template< typename T >
78 T * __restrict__ b,
79 int * __restrict__ mask,
80 const int n,
81 const int n_mask) {
82
83 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
84 const int str = blockDim.x * gridDim.x;
85
86 for (int i = idx; i < n_mask; i += str) {
87 a[i] = b[mask[i]];
88 }
89}
90
92void face_gather_nonlinear_index(int *index, const int idx, const int lx,
93 const int ly, const int lz) {
94 const int idx2 = idx - 1;
95 index[3] = idx2 / (lx * ly * lz);
96 index[2] = (idx2 - (lx * ly * lz) * index[3]) / (lx * ly);
97 index[1] = (idx2 - (lx * ly * lz) * index[3] - (lx * ly) * index[2]) / lx;
98 index[0] = (idx2 - (lx * ly * lz) * index[3] - (lx * ly) * index[2]) -
99 lx * index[1];
100 index[0]++;
101 index[1]++;
102 index[2]++;
103 index[3]++;
104}
105
107int face_gather_idx(const int i, const int j, const int k, const int l,
108 const int n1, const int n2, const int nf) {
109 return ((i) + (n1) * (((j) - 1) + (n2) * (((k) - 1) + (nf) * (((l) - 1))))) - 1;
110}
111
115template< typename T >
117 const T * __restrict__ b,
118 const int * __restrict__ mask,
119 const int * __restrict__ facet,
120 const int n1,
121 const int n2,
122 const int lx,
123 const int ly,
124 const int lz,
125 const int n_mask) {
126 int index[4];
127
128 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
129 const int str = blockDim.x * gridDim.x;
130
131 for (int m = idx; m < n_mask; m += str) {
132 const int f = facet[m + 1];
133 face_gather_nonlinear_index(index, mask[m + 1], lx, ly, lz);
134
135 switch (f) {
136 case 1:
137 case 2:
138 a[m] = b[face_gather_idx(index[1], index[2], f, index[3], n1, n2, 6)];
139 break;
140 case 3:
141 case 4:
142 a[m] = b[face_gather_idx(index[0], index[2], f, index[3], n1, n2, 6)];
143 break;
144 case 5:
145 case 6:
146 a[m] = b[face_gather_idx(index[0], index[1], f, index[3], n1, n2, 6)];
147 break;
148 }
149 }
150}
151
155template< typename T >
157 T * __restrict__ b,
158 int * __restrict__ mask,
159 const int n,
160 const int n_mask) {
161
162 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
163 const int str = blockDim.x * gridDim.x;
164
165 for (int i = idx; i < n_mask; i += str) {
166 a[mask[i+1]-1] = b[i];
167 }
168}
169
173template< typename T >
175 T * __restrict__ b,
176 int * __restrict__ mask,
177 const int n,
178 const int n_mask) {
179
180 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
181 const int str = blockDim.x * gridDim.x;
182
183 for (int i = idx; i < n_mask; i += str) {
184 a[mask[i]] = b[i];
185 }
186}
187
191template< typename T >
193 T * __restrict__ b,
194 int * __restrict__ mask,
195 const int n,
196 const int n_mask) {
197
198 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
199 const int str = blockDim.x * gridDim.x;
200
201 for (int i = idx; i < n_mask; i += str) {
202 unsafeAtomicAdd( &(a[mask[i+1]-1]), b[i]);//a[mask[i]-1] = a[mask[i]-1] + b[i];
203 //atomicAdd( &(a[mask[i+1]-1]), b[i]);//a[mask[i]-1] = a[mask[i]-1] + b[i];
204 }
205}
206
210template< typename T >
212 T * __restrict__ b,
213 int * __restrict__ mask,
214 const int n,
215 const int n_mask) {
216
217 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
218 const int str = blockDim.x * gridDim.x;
219
220 for (int i = idx; i < n_mask; i += str) {
221 a[mask[i+1]-1] = b[mask[i+1]-1];
222 }
223}
224
228template< typename T >
230 T * __restrict__ b,
231 int * __restrict__ mask,
232 const int n,
233 const int n_mask) {
234
235 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
236 const int str = blockDim.x * gridDim.x;
237
238 for (int i = idx; i < n_mask; i += str) {
239 a[mask[i]] = b[mask[i]];
240 }
241}
242
246template< typename T >
248 const T c,
249 const int n,
250 int* __restrict__ mask,
251 const int n_mask) {
252
253 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
254 const int str = blockDim.x * gridDim.x;
255
256 for (int i = idx; i < n_mask; i += str) { a[mask[i]] = c; }
257}
258
262template< typename T >
264 T * __restrict__ b,
265 const T c,
266 const int n) {
267
268 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
269 const int str = blockDim.x * gridDim.x;
270
271 for (int i = idx; i < n; i += str) {
272 a[i] = c * b[i];
273 }
274}
275
279template< typename T >
281 const T c,
282 const int n) {
283
284 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
285 const int str = blockDim.x * gridDim.x;
286
287 for (int i = idx; i < n; i += str) {
288 a[i] = c / a[i];
289 }
290}
291
295template< typename T >
297 T * __restrict__ b,
298 const T c,
299 const int n) {
300
301 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
302 const int str = blockDim.x * gridDim.x;
303
304 for (int i = idx; i < n; i += str) {
305 a[i] = c / b[i];
306 }
307}
308
312template< typename T >
314 const T c,
315 const int n) {
316
317 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
318 const int str = blockDim.x * gridDim.x;
319
320 for (int i = idx; i < n; i += str) {
321 a[i] = a[i] + c;
322 }
323}
324
328template< typename T >
330 T * __restrict__ b,
331 const T c,
332 const int n) {
333
334 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
335 const int str = blockDim.x * gridDim.x;
336
337 for (int i = idx; i < n; i += str) {
338 a[i] = b[i] + c;
339 }
340}
341
345template< typename T >
347 const T min_val,
348 const T max_val,
349 const int n) {
350
351 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
352 const int str = blockDim.x * gridDim.x;
353 const T l = max_val - min_val;
354
355 for (int i = idx; i < n; i += str) {
356 a[i] = min_val + fmod(fmod(a[i] - min_val, l) + l, l);
357 }
358}
359
363template< typename T >
365 const int n) {
366
367 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
368 const int str = blockDim.x * gridDim.x;
369
370 for (int i = idx; i < n; i += str) {
371 a[i] = sqrt(a[i]);
372 }
373}
374
378template< typename T >
380 const T * __restrict__ a,
381 const T p,
382 const int n) {
383
384 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
385 const int str = blockDim.x * gridDim.x;
386
387 for (int i = idx; i < n; i += str) {
388 ap[i] = pow(a[i], p);
389 }
390}
391
395template< typename T >
397 const T c,
398 const int n) {
399
400 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
401 const int str = blockDim.x * gridDim.x;
402
403 for (int i = idx; i < n; i += str) {
404 a[i] = c;
405 }
406}
407
411template< typename T >
413 const T * __restrict__ b,
414 const int n) {
415
416 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
417 const int str = blockDim.x * gridDim.x;
418
419 for (int i = idx; i < n; i += str) {
420 a[i] = b[i];
421 }
422}
423
427template< typename T >
429 const T * __restrict__ b,
430 const int n) {
431
432 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
433 const int str = blockDim.x * gridDim.x;
434
435 for (int i = idx; i < n; i += str) {
436 a[i] = a[i] + b[i];
437 }
438}
439
443template< typename T >
445 const T * __restrict__ b,
446 const T * __restrict__ c,
447 const int n) {
448
449 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
450 const int str = blockDim.x * gridDim.x;
451
452 for (int i = idx; i < n; i += str) {
453 a[i] = b[i] + c[i];
454 }
455}
456
460template< typename T >
462 const T * __restrict__ b,
463 const T * __restrict__ c,
464 const T * __restrict__ d,
465 const int n) {
466
467 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
468 const int str = blockDim.x * gridDim.x;
469
470 for (int i = idx; i < n; i += str) {
471 a[i] = b[i] + c[i] + d[i];
472 }
473}
474
478template< typename T >
480 const T * __restrict__ b,
481 const T c1,
482 const int n) {
483
484 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
485 const int str = blockDim.x * gridDim.x;
486
487 for (int i = idx; i < n; i += str) {
488 a[i] = c1 * a[i] + b[i];
489 }
490}
491
495template< typename T >
497 const T ** p,
498 const T * alpha,
499 const int p_cur,
500 const int n) {
501
502 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
503 const int str = blockDim.x * gridDim.x;
504
505
506 for (int i = idx; i < n; i+= str) {
507 T tmp = 0.0;
508 for (int j = 0; j < p_cur; j ++) {
509 tmp += p[j][i]*alpha[j];
510 }
511 x[i] += tmp;
512 }
513}
514
518template< typename T >
520 const T * __restrict__ b,
521 const T c1,
522 const int n) {
523
524 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
525 const int str = blockDim.x * gridDim.x;
526
527 for (int i = idx; i < n; i += str) {
528 a[i] = a[i] + c1 * b[i];
529 }
530}
531
535template< typename T >
537 const T * __restrict__ b,
538 const T c1,
539 const int n) {
540
541 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
542 const int str = blockDim.x * gridDim.x;
543
544 for (int i = idx; i < n; i += str) {
545 a[i] = a[i] + c1 * (b[i] * b[i]);
546 }
547}
548
552template< typename T >
554 const T * __restrict__ b,
555 const T * __restrict__ c,
556 const T c1,
557 const T c2,
558 const int n) {
559
560 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
561 const int str = blockDim.x * gridDim.x;
562
563 for (int i = idx; i < n; i += str) {
564 a[i] = c1 * b[i] + c2 * c[i];
565 }
566}
567
571template< typename T >
573 const T * __restrict__ b,
574 const T * __restrict__ c,
575 const T * __restrict__ d,
576 const T c1,
577 const T c2,
578 const T c3,
579 const int n) {
580
581 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
582 const int str = blockDim.x * gridDim.x;
583
584 for (int i = idx; i < n; i += str) {
585 a[i] = c1 * b[i] + c2 * c[i] + c3 * d[i];
586 }
587}
588
592template< typename T >
594 const T * __restrict__ b,
595 const T * __restrict__ c,
596 const T * __restrict__ d,
597 const T * __restrict__ e,
598 const T c1,
599 const T c2,
600 const T c3,
601 const T c4,
602 const int n) {
603
604 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
605 const int str = blockDim.x * gridDim.x;
606
607 for (int i = idx; i < n; i += str) {
608 a[i] = a[i] + c1 * b[i] + c2 * c[i] + c3 * d[i] + c4 * e[i];
609 }
610}
611
615template< typename T >
617 const int n) {
618
619 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
620 const int str = blockDim.x * gridDim.x;
621 const T one = 1.0;
622
623 for (int i = idx; i < n; i += str) {
624 a[i] = one / a[i];
625 }
626}
627
631template< typename T >
633 const T * __restrict__ b,
634 const int n) {
635
636 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
637 const int str = blockDim.x * gridDim.x;
638
639 for (int i = idx; i < n; i += str) {
640 a[i] = a[i] / b[i];
641 }
642}
643
647template< typename T >
649 const T * __restrict__ b,
650 const T * __restrict__ c,
651 const int n) {
652
653 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
654 const int str = blockDim.x * gridDim.x;
655
656 for (int i = idx; i < n; i += str) {
657 a[i] = b[i] / c[i];
658 }
659}
660
664template< typename T >
666 const T * __restrict__ b,
667 const int n) {
668
669 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
670 const int str = blockDim.x * gridDim.x;
671
672 for (int i = idx; i < n; i += str) {
673 a[i] = a[i] * b[i];
674 }
675}
676
680template< typename T >
682 const T * __restrict__ b,
683 const T * __restrict__ c,
684 const int n) {
685
686 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
687 const int str = blockDim.x * gridDim.x;
688
689 for (int i = idx; i < n; i += str) {
690 a[i] = b[i] * c[i];
691 }
692}
693
697template< typename T >
699 const T * __restrict__ b,
700 const T * __restrict__ c,
701 const int n) {
702
703 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
704 const int str = blockDim.x * gridDim.x;
705
706 for (int i = idx; i < n; i += str) {
707 a[i] = a[i] - b[i] * c[i];
708 }
709}
710
714template< typename T >
716 const T * __restrict__ b,
717 const int n) {
718
719 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
720 const int str = blockDim.x * gridDim.x;
721
722 for (int i = idx; i < n; i += str) {
723 a[i] = a[i] - b[i];
724 }
725}
726
730template< typename T >
732 const T * __restrict__ b,
733 const T * __restrict__ c,
734 const int n) {
735
736 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
737 const int str = blockDim.x * gridDim.x;
738
739 for (int i = idx; i < n; i += str) {
740 a[i] = b[i] - c[i];
741 }
742}
743
747template< typename T >
749 const T * __restrict__ b,
750 const T * __restrict__ c,
751 const int n) {
752
753 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
754 const int str = blockDim.x * gridDim.x;
755
756 for (int i = idx; i < n; i += str) {
757 a[i] = a[i] + b[i] * c[i];
758 }
759
760}
761
765template< typename T >
767 const T * __restrict__ b,
768 const T * __restrict__ c,
769 const T * __restrict__ d,
770 const int n) {
771
772 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
773 const int str = blockDim.x * gridDim.x;
774
775 for (int i = idx; i < n; i += str) {
776 a[i] = a[i] + b[i] * c[i] * d[i];
777 }
778
779}
780
784template< typename T >
786 const T * __restrict__ b,
787 const T * __restrict__ c,
788 const T s,
789 const int n) {
790
791 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
792 const int str = blockDim.x * gridDim.x;
793
794 for (int i = idx; i < n; i += str) {
795 a[i] = a[i] + s * b[i] * c[i];
796 }
797
798}
799
803template< typename T >
805 const T * __restrict__ u1,
806 const T * __restrict__ u2,
807 const T * __restrict__ u3,
808 const T * __restrict__ v1,
809 const T * __restrict__ v2,
810 const T * __restrict__ v3,
811 const int n) {
812
813 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
814 const int str = blockDim.x * gridDim.x;
815
816 for (int i = idx; i < n; i += str) {
817 dot[i] = u1[i] * v1[i] + u2[i] * v2[i] + u3[i] * v3[i];
818 }
819
820}
821
825template< typename T >
827 T * __restrict__ u2,
828 T * __restrict__ u3,
829 const T * __restrict__ v1,
830 const T * __restrict__ v2,
831 const T * __restrict__ v3,
832 const T * __restrict__ w1,
833 const T * __restrict__ w2,
834 const T * __restrict__ w3,
835 const int n) {
836
837 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
838 const int str = blockDim.x * gridDim.x;
839
840 for (int i = idx; i < n; i += str) {
841 u1[i] = v2[i]*w3[i] - v3[i]*w2[i];
842 u2[i] = v3[i]*w1[i] - v1[i]*w3[i];
843 u3[i] = v1[i]*w2[i] - v2[i]*w1[i];
844 }
845}
846
856template< typename T>
858#if NEKO_WAVE_SIZE == 64
859 val += __shfl_down(val, 32);
860#endif
861 val += __shfl_down(val, 16);
862 val += __shfl_down(val, 8);
863 val += __shfl_down(val, 4);
864 val += __shfl_down(val, 2);
865 val += __shfl_down(val, 1);
866 return val;
867}
868
872template< typename T>
874#if NEKO_WAVE_SIZE == 64
875 val = max(val, __shfl_down(val, 32));
876#endif
877 val = max(val, __shfl_down(val, 16));
878 val = max(val, __shfl_down(val, 8));
879 val = max(val, __shfl_down(val, 4));
880 val = max(val, __shfl_down(val, 2));
881 val = max(val, __shfl_down(val, 1));
882 return val;
883}
884
888template< typename T>
890#if NEKO_WAVE_SIZE == 64
891 val = min(val, __shfl_down(val, 32));
892#endif
893 val = min(val, __shfl_down(val, 16));
894 val = min(val, __shfl_down(val, 8));
895 val = min(val, __shfl_down(val, 4));
896 val = min(val, __shfl_down(val, 2));
897 val = min(val, __shfl_down(val, 1));
898 return val;
899}
900
904template< typename T >
905__global__ void reduce_kernel(T * bufred, const int n) {
906
907 T sum = 0;
908 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
909 const int str = blockDim.x * gridDim.x;
910 for (int i = idx; i<n ; i += str)
911 {
912 sum += bufred[i];
913 }
914
915 __shared__ T shared[64];
916 unsigned int lane = threadIdx.x % NEKO_WAVE_SIZE;
917 unsigned int wid = threadIdx.x / NEKO_WAVE_SIZE;
918
920 if (lane == 0)
921 shared[wid] = sum;
923
924 sum = (threadIdx.x < blockDim.x / NEKO_WAVE_SIZE) ? shared[lane] : 0;
925 if (wid == 0)
927
928 if (threadIdx.x == 0)
929 bufred[blockIdx.x] = sum;
930}
931
935template< typename T >
936__global__ void reduce_max_kernel(T * bufred, const T ninf, const int n) {
937
938 T max_val = ninf;
939 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
940 const int str = blockDim.x * gridDim.x;
941 for (int i = idx; i<n ; i += str)
942 {
944 }
945
946 __shared__ T shared[64];
947 unsigned int lane = threadIdx.x % NEKO_WAVE_SIZE;
948 unsigned int wid = threadIdx.x / NEKO_WAVE_SIZE;
949
951 if (lane == 0)
952 shared[wid] = max_val;
954
956 if (wid == 0)
958
959 if (threadIdx.x == 0)
961}
962
966template< typename T >
967__global__ void reduce_min_kernel(T * bufred, const T pinf, const int n) {
968
969 T min_val = pinf;
970 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
971 const int str = blockDim.x * gridDim.x;
972 for (int i = idx; i<n ; i += str)
973 {
975 }
976
977 __shared__ T shared[64];
978 unsigned int lane = threadIdx.x % NEKO_WAVE_SIZE;
979 unsigned int wid = threadIdx.x / NEKO_WAVE_SIZE;
980
982 if (lane == 0)
983 shared[wid] = min_val;
985
987 if (wid == 0)
989
990 if (threadIdx.x == 0)
992}
993
998template< typename T_acc >
1000 const int n,
1001 const int j
1002 ) {
1003 __shared__ T_acc buf[1024] ;
1004 const int idx = threadIdx.x;
1005 const int y= blockIdx.x;
1006 const int step = blockDim.x;
1007
1008 buf[idx]=0;
1009 for (int i=idx ; i<n ; i+=step)
1010 {
1011 buf[idx] += bufred[i*j + y];
1012 }
1013 __syncthreads();
1014
1015 int i = 512;
1016 while (i != 0)
1017 {
1018 if(threadIdx.x < i && (threadIdx.x + i) < n )
1019 {
1020 buf[threadIdx.x] += buf[threadIdx.x + i] ;
1021 }
1022 i = i>>1;
1023 __syncthreads();
1024 }
1025
1026 bufred[y] = buf[0];
1027}
1028
1032template< typename T >
1034 const T * b,
1035 const T * c,
1036 T * buf_h,
1037 const int n) {
1038
1039 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1040 const int str = blockDim.x * gridDim.x;
1041
1042 const unsigned int lane = threadIdx.x % NEKO_WAVE_SIZE;
1043 const unsigned int wid = threadIdx.x / NEKO_WAVE_SIZE;
1044
1045 __shared__ T shared[64];
1046 T sum = 0.0;
1047 for (int i = idx; i < n; i+= str) {
1048 sum += a[i] * b[i] * c[i];
1049 }
1050
1052 if (lane == 0)
1053 shared[wid] = sum;
1054 __syncthreads();
1055
1056 sum = (threadIdx.x < blockDim.x / NEKO_WAVE_SIZE) ? shared[lane] : 0;
1057 if (wid == 0)
1059
1060 if (threadIdx.x == 0)
1061 buf_h[blockIdx.x] = sum;
1062}
1063
1067template< typename T, typename T_acc >
1069 const T * b,
1070 const T * c,
1071 T_acc * buf_h,
1072 const int n) {
1073
1074 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1075 const int str = blockDim.x * gridDim.x;
1076
1077 const unsigned int lane = threadIdx.x % NEKO_WAVE_SIZE;
1078 const unsigned int wid = threadIdx.x / NEKO_WAVE_SIZE;
1079
1081 T_acc sum = 0.0;
1082 for (int i = idx; i < n; i+= str) {
1083 sum += static_cast<T_acc>(a[i] * b[i] * c[i]);
1084 }
1085
1087 if (lane == 0)
1088 shared[wid] = sum;
1089 __syncthreads();
1090
1091 sum = (threadIdx.x < blockDim.x / NEKO_WAVE_SIZE) ? shared[lane] : 0;
1092 if (wid == 0)
1094
1095 if (threadIdx.x == 0)
1096 buf_h[blockIdx.x] = sum;
1097}
1098
1102template< typename T, typename T_acc >
1104 const T ** b,
1105 const T * c,
1106 T_acc * buf_h,
1107 const int j,
1108 const int n) {
1109
1110 const int idx = blockIdx.x * blockDim.y + threadIdx.y;
1111 const int str = blockDim.y * gridDim.x;
1112 const int y = threadIdx.x;
1113
1114 __shared__ T_acc buf[1024];
1115 T_acc tmp = 0;
1116 if(y < j){
1117 for (int i = idx; i < n; i+= str) {
1118 tmp += static_cast<T_acc>(a[i] * b[threadIdx.x][i] * c[i]);
1119 }
1120 }
1121
1122 buf[threadIdx.y*blockDim.x+y] = tmp;
1123 __syncthreads();
1124
1125 int i = blockDim.y>>1;
1126 while (i != 0) {
1127 if (threadIdx.y < i) {
1128 buf[threadIdx.y*blockDim.x +y] += buf[(threadIdx.y + i)*blockDim.x+y];
1129 }
1130 __syncthreads();
1131 i = i>>1;
1132 }
1133 if (threadIdx.y == 0) {
1134 if( y < j) {
1135 buf_h[j*blockIdx.x+y] = buf[y];
1136 }
1137 }
1138}
1139
1143template< typename T, typename T_acc >
1145 const T * b,
1146 T_acc * buf_h,
1147 const int n) {
1148
1149 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1150 const int str = blockDim.x * gridDim.x;
1151
1152 const unsigned int lane = threadIdx.x % NEKO_WAVE_SIZE;
1153 const unsigned int wid = threadIdx.x / NEKO_WAVE_SIZE;
1154
1156 T_acc sum = 0.0;
1157 for (int i = idx; i < n; i+= str) {
1158 sum += static_cast<T_acc>(a[i] * b[i]);
1159 }
1160
1162 if (lane == 0)
1163 shared[wid] = sum;
1164 __syncthreads();
1165
1166 sum = (threadIdx.x < blockDim.x / NEKO_WAVE_SIZE) ? shared[lane] : 0;
1167 if (wid == 0)
1169
1170 if (threadIdx.x == 0)
1171 buf_h[blockIdx.x] = sum;
1172
1173}
1174
1178template< typename T, typename T_acc >
1180 const T * b,
1181 T_acc * buf_h,
1182 const int n) {
1183
1184 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1185 const int str = blockDim.x * gridDim.x;
1186
1187 const unsigned int lane = threadIdx.x % NEKO_WAVE_SIZE;
1188 const unsigned int wid = threadIdx.x / NEKO_WAVE_SIZE;
1189
1191 T_acc sum = 0.0;
1192 for (int i = idx; i < n; i+= str) {
1193 sum += static_cast<T_acc>(pow(a[i] - b[i], 2.0));
1194 }
1195
1197 if (lane == 0)
1198 shared[wid] = sum;
1199 __syncthreads();
1200
1201 sum = (threadIdx.x < blockDim.x / NEKO_WAVE_SIZE) ? shared[lane] : 0;
1202 if (wid == 0)
1204
1205 if (threadIdx.x == 0)
1206 buf_h[blockIdx.x] = sum;
1207
1208}
1209
1213template< typename T, typename T_acc >
1215 T_acc * buf_h,
1216 const int n) {
1217
1218 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1219 const int str = blockDim.x * gridDim.x;
1220
1221 const unsigned int lane = threadIdx.x % NEKO_WAVE_SIZE;
1222 const unsigned int wid = threadIdx.x / NEKO_WAVE_SIZE;
1223
1225 T_acc sum = 0;
1226 for (int i = idx; i<n ; i += str)
1227 {
1228 sum += static_cast<T_acc>(a[i]);
1229 }
1230
1232 if (lane == 0)
1233 shared[wid] = sum;
1234 __syncthreads();
1235
1236 sum = (threadIdx.x < blockDim.x / NEKO_WAVE_SIZE) ? shared[lane] : 0;
1237 if (wid == 0)
1239
1240 if (threadIdx.x == 0)
1241 buf_h[blockIdx.x] = sum;
1242
1243}
1244
1248template< typename T >
1250 const T ninf,
1251 T * buf_h,
1252 const int n) {
1253
1254 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1255 const int str = blockDim.x * gridDim.x;
1256
1257 const unsigned int lane = threadIdx.x % NEKO_WAVE_SIZE;
1258 const unsigned int wid = threadIdx.x / NEKO_WAVE_SIZE;
1259
1260 __shared__ T shared[64];
1261 T max_val = ninf;
1262 for (int i = idx; i<n ; i += str)
1263 {
1264 max_val = max(max_val, a[i]);
1265 }
1266
1268 if (lane == 0)
1269 shared[wid] = max_val;
1270 __syncthreads();
1271
1273 if (wid == 0)
1275
1276 if (threadIdx.x == 0)
1277 buf_h[blockIdx.x] = max_val;
1278
1279}
1280
1284template< typename T >
1286 const T pinf,
1287 T * buf_h,
1288 const int n) {
1289
1290 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1291 const int str = blockDim.x * gridDim.x;
1292
1293 const unsigned int lane = threadIdx.x % NEKO_WAVE_SIZE;
1294 const unsigned int wid = threadIdx.x / NEKO_WAVE_SIZE;
1295
1296 __shared__ T shared[64];
1297 T min_val = pinf;
1298 for (int i = idx; i<n ; i += str)
1299 {
1300 min_val = min(min_val, a[i]);
1301 }
1302
1304 if (lane == 0)
1305 shared[wid] = min_val;
1306 __syncthreads();
1307
1309 if (wid == 0)
1311
1312 if (threadIdx.x == 0)
1313 buf_h[blockIdx.x] = min_val;
1314
1315}
1316
1320template< typename T >
1322 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) {
1328 a[i] = fabs(a[i]);
1329 }
1330}
1331
1332// ========================================================================== //
1333// Kernels for the point-wise operations
1334
1339template <typename T>
1340__global__ void
1341 pwmax_vec2_kernel(T* __restrict__ a, const T* __restrict__ b, const int n) {
1342
1343 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1344 const int str = blockDim.x * gridDim.x;
1345
1346 for (int i = idx; i < n; i += str) a[i] = max(a[i], b[i]);
1347}
1348
1353template <typename T>
1355 T* __restrict__ a, const T* __restrict__ b, const T* __restrict__ c,
1356 const int n) {
1357
1358 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1359 const int str = blockDim.x * gridDim.x;
1360
1361 for (int i = idx; i < n; i += str) a[i] = max(b[i], c[i]);
1362}
1363
1368template <typename T>
1369__global__ void pwmax_sca2_kernel(T* __restrict__ a, const T c, const int n) {
1370
1371 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1372 const int str = blockDim.x * gridDim.x;
1373
1374 for (int i = idx; i < n; i += str) a[i] = max(a[i], c);
1375}
1376
1381template <typename T>
1383 T* __restrict__ a, const T* __restrict b, const T c, const int n) {
1384
1385 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1386 const int str = blockDim.x * gridDim.x;
1387
1388 for (int i = idx; i < n; i += str) a[i] = max(b[i], c);
1389}
1390
1395template <typename T>
1396__global__ void
1397 pwmin_vec2_kernel(T* __restrict__ a, const T* __restrict__ b, const int n) {
1398
1399 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1400 const int str = blockDim.x * gridDim.x;
1401
1402 for (int i = idx; i < n; i += str) a[i] = min(a[i], b[i]);
1403}
1404
1409template <typename T>
1411 T* __restrict__ a, const T* __restrict__ b, const T* __restrict__ c,
1412 const int n) {
1413
1414 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1415 const int str = blockDim.x * gridDim.x;
1416
1417 for (int i = idx; i < n; i += str) a[i] = min(b[i], c[i]);
1418}
1419
1424template <typename T>
1425__global__ void pwmin_sca2_kernel(T* __restrict__ a, const T c, const int n) {
1426
1427 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1428 const int str = blockDim.x * gridDim.x;
1429
1430 for (int i = idx; i < n; i += str) a[i] = min(a[i], c);
1431}
1432
1437template <typename T>
1439 T* __restrict__ a, const T* __restrict b, const T c, const int n) {
1440
1441 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1442 const int str = blockDim.x * gridDim.x;
1443
1444 for (int i = idx; i < n; i += str) a[i] = min(b[i], c);
1445}
1446
1447#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)
__global__ void copy_kernel(T *__restrict__ a, const T *__restrict__ b, const int n)
Object for handling masks in Neko.
Definition mask.f90:34
#define max(a, b)
Definition tensor.cu:40
#define NEKO_WAVE_SIZE
Definition wave.h:119