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-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
40template< typename T >
42 const T c,
43 const int n) {
44
45 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
46 const int str = blockDim.x * gridDim.x;
47
48 for (int i = idx; i < n; i += str) {
49 a[i] = c * a[i];
50 }
51}
52
56template< typename T >
58 T * __restrict__ b,
59 int * __restrict__ mask,
60 const int n,
61 const int n_mask) {
62
63 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
64 const int str = blockDim.x * gridDim.x;
65
66 for (int i = idx; i < n_mask; i += str) {
67 a[i] = b[mask[i+1]-1];
68 }
69}
70
74template< typename T >
76 T * __restrict__ b,
77 int * __restrict__ mask,
78 const int n,
79 const int n_mask) {
80
81 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
82 const int str = blockDim.x * gridDim.x;
83
84 for (int i = idx; i < n_mask; i += str) {
85 a[i] = b[mask[i]];
86 }
87}
88
90void face_gather_nonlinear_index(int *index, const int idx, const int lx,
91 const int ly, const int lz) {
92 const int idx2 = idx - 1;
93 index[3] = idx2 / (lx * ly * lz);
94 index[2] = (idx2 - (lx * ly * lz) * index[3]) / (lx * ly);
95 index[1] = (idx2 - (lx * ly * lz) * index[3] - (lx * ly) * index[2]) / lx;
96 index[0] = (idx2 - (lx * ly * lz) * index[3] - (lx * ly) * index[2]) -
97 lx * index[1];
98 index[0]++;
99 index[1]++;
100 index[2]++;
101 index[3]++;
102}
103
105int face_gather_idx(const int i, const int j, const int k, const int l,
106 const int n1, const int n2, const int nf) {
107 return ((i) + (n1) * (((j) - 1) + (n2) * (((k) - 1) + (nf) * (((l) - 1))))) - 1;
108}
109
113template< typename T >
115 const T * __restrict__ b,
116 const int * __restrict__ mask,
117 const int * __restrict__ facet,
118 const int n1,
119 const int n2,
120 const int lx,
121 const int ly,
122 const int lz,
123 const int n_mask) {
124 int index[4];
125
126 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
127 const int str = blockDim.x * gridDim.x;
128
129 for (int m = idx; m < n_mask; m += str) {
130 const int f = facet[m + 1];
131 face_gather_nonlinear_index(index, mask[m + 1], lx, ly, lz);
132
133 switch (f) {
134 case 1:
135 case 2:
136 a[m] = b[face_gather_idx(index[1], index[2], f, index[3], n1, n2, 6)];
137 break;
138 case 3:
139 case 4:
140 a[m] = b[face_gather_idx(index[0], index[2], f, index[3], n1, n2, 6)];
141 break;
142 case 5:
143 case 6:
144 a[m] = b[face_gather_idx(index[0], index[1], f, index[3], n1, n2, 6)];
145 break;
146 }
147 }
148}
149
153template< typename T >
155 T * __restrict__ b,
156 int * __restrict__ mask,
157 const int n,
158 const int n_mask) {
159
160 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
161 const int str = blockDim.x * gridDim.x;
162
163 for (int i = idx; i < n_mask; i += str) {
164 a[mask[i+1]-1] = b[i];
165 }
166}
167
171template< typename T >
173 T * __restrict__ b,
174 int * __restrict__ mask,
175 const int n,
176 const int n_mask) {
177
178 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
179 const int str = blockDim.x * gridDim.x;
180
181 for (int i = idx; i < n_mask; i += str) {
182 a[mask[i]] = b[i];
183 }
184}
185
189template< typename T >
191 T * __restrict__ b,
192 int * __restrict__ mask,
193 const int n,
194 const int n_mask) {
195
196 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
197 const int str = blockDim.x * gridDim.x;
198
199 for (int i = idx; i < n_mask; i += str) {
200 unsafeAtomicAdd( &(a[mask[i+1]-1]), b[i]);//a[mask[i]-1] = a[mask[i]-1] + b[i];
201 //atomicAdd( &(a[mask[i+1]-1]), b[i]);//a[mask[i]-1] = a[mask[i]-1] + b[i];
202 }
203}
204
208template< typename T >
210 T * __restrict__ b,
211 int * __restrict__ mask,
212 const int n,
213 const int n_mask) {
214
215 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
216 const int str = blockDim.x * gridDim.x;
217
218 for (int i = idx; i < n_mask; i += str) {
219 a[mask[i+1]-1] = b[mask[i+1]-1];
220 }
221}
222
226template< typename T >
228 T * __restrict__ b,
229 int * __restrict__ mask,
230 const int n,
231 const int n_mask) {
232
233 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
234 const int str = blockDim.x * gridDim.x;
235
236 for (int i = idx; i < n_mask; i += str) {
237 a[mask[i]] = b[mask[i]];
238 }
239}
240
244template< typename T >
246 const T c,
247 const int n,
248 int* __restrict__ mask,
249 const int n_mask) {
250
251 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
252 const int str = blockDim.x * gridDim.x;
253
254 for (int i = idx; i < n_mask; i += str) { a[mask[i]] = c; }
255}
256
260template< typename T >
262 T * __restrict__ b,
263 const T c,
264 const int n) {
265
266 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
267 const int str = blockDim.x * gridDim.x;
268
269 for (int i = idx; i < n; i += str) {
270 a[i] = c * b[i];
271 }
272}
273
277template< typename T >
279 const T c,
280 const int n) {
281
282 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
283 const int str = blockDim.x * gridDim.x;
284
285 for (int i = idx; i < n; i += str) {
286 a[i] = c / a[i];
287 }
288}
289
293template< typename T >
295 T * __restrict__ b,
296 const T c,
297 const int n) {
298
299 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
300 const int str = blockDim.x * gridDim.x;
301
302 for (int i = idx; i < n; i += str) {
303 a[i] = c / b[i];
304 }
305}
306
310template< typename T >
312 const T c,
313 const int n) {
314
315 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
316 const int str = blockDim.x * gridDim.x;
317
318 for (int i = idx; i < n; i += str) {
319 a[i] = a[i] + c;
320 }
321}
322
326template< typename T >
328 T * __restrict__ b,
329 const T c,
330 const int n) {
331
332 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
333 const int str = blockDim.x * gridDim.x;
334
335 for (int i = idx; i < n; i += str) {
336 a[i] = b[i] + c;
337 }
338}
339
343template< typename T >
345 const T min_val,
346 const T max_val,
347 const int n) {
348
349 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
350 const int str = blockDim.x * gridDim.x;
351 const T l = max_val - min_val;
352
353 for (int i = idx; i < n; i += str) {
354 a[i] = min_val + fmod(fmod(a[i] - min_val, l) + l, l);
355 }
356}
357
361template< typename T >
363 const int n) {
364
365 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
366 const int str = blockDim.x * gridDim.x;
367
368 for (int i = idx; i < n; i += str) {
369 a[i] = sqrt(a[i]);
370 }
371}
372
376template< typename T >
378 const T * __restrict__ a,
379 const T p,
380 const int n) {
381
382 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
383 const int str = blockDim.x * gridDim.x;
384
385 for (int i = idx; i < n; i += str) {
386 ap[i] = pow(a[i], p);
387 }
388}
389
393template< typename T >
395 const T c,
396 const int n) {
397
398 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
399 const int str = blockDim.x * gridDim.x;
400
401 for (int i = idx; i < n; i += str) {
402 a[i] = c;
403 }
404}
405
409template< typename T >
411 const T * __restrict__ b,
412 const int n) {
413
414 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
415 const int str = blockDim.x * gridDim.x;
416
417 for (int i = idx; i < n; i += str) {
418 a[i] = b[i];
419 }
420}
421
425template< typename T >
427 const T * __restrict__ b,
428 const int n) {
429
430 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
431 const int str = blockDim.x * gridDim.x;
432
433 for (int i = idx; i < n; i += str) {
434 a[i] = a[i] + b[i];
435 }
436}
437
441template< typename T >
443 const T * __restrict__ b,
444 const T * __restrict__ c,
445 const int n) {
446
447 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
448 const int str = blockDim.x * gridDim.x;
449
450 for (int i = idx; i < n; i += str) {
451 a[i] = b[i] + c[i];
452 }
453}
454
458template< typename T >
460 const T * __restrict__ b,
461 const T * __restrict__ c,
462 const T * __restrict__ d,
463 const int n) {
464
465 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
466 const int str = blockDim.x * gridDim.x;
467
468 for (int i = idx; i < n; i += str) {
469 a[i] = b[i] + c[i] + d[i];
470 }
471}
472
476template< typename T >
478 const T * __restrict__ b,
479 const T c1,
480 const int n) {
481
482 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
483 const int str = blockDim.x * gridDim.x;
484
485 for (int i = idx; i < n; i += str) {
486 a[i] = c1 * a[i] + b[i];
487 }
488}
489
493template< typename T >
495 const T ** p,
496 const T * alpha,
497 const int p_cur,
498 const int n) {
499
500 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
501 const int str = blockDim.x * gridDim.x;
502
503
504 for (int i = idx; i < n; i+= str) {
505 T tmp = 0.0;
506 for (int j = 0; j < p_cur; j ++) {
507 tmp += p[j][i]*alpha[j];
508 }
509 x[i] += tmp;
510 }
511}
512
516template< typename T >
518 const T * __restrict__ b,
519 const T c1,
520 const int n) {
521
522 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
523 const int str = blockDim.x * gridDim.x;
524
525 for (int i = idx; i < n; i += str) {
526 a[i] = a[i] + c1 * b[i];
527 }
528}
529
533template< typename T >
535 const T * __restrict__ b,
536 const T c1,
537 const int n) {
538
539 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
540 const int str = blockDim.x * gridDim.x;
541
542 for (int i = idx; i < n; i += str) {
543 a[i] = a[i] + c1 * (b[i] * b[i]);
544 }
545}
546
550template< typename T >
552 const T * __restrict__ b,
553 const T * __restrict__ c,
554 const T c1,
555 const T c2,
556 const int n) {
557
558 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
559 const int str = blockDim.x * gridDim.x;
560
561 for (int i = idx; i < n; i += str) {
562 a[i] = c1 * b[i] + c2 * c[i];
563 }
564}
565
569template< typename T >
571 const T * __restrict__ b,
572 const T * __restrict__ c,
573 const T * __restrict__ d,
574 const T c1,
575 const T c2,
576 const T c3,
577 const int n) {
578
579 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
580 const int str = blockDim.x * gridDim.x;
581
582 for (int i = idx; i < n; i += str) {
583 a[i] = c1 * b[i] + c2 * c[i] + c3 * d[i];
584 }
585}
586
590template< typename T >
592 const T * __restrict__ b,
593 const T * __restrict__ c,
594 const T * __restrict__ d,
595 const T * __restrict__ e,
596 const T c1,
597 const T c2,
598 const T c3,
599 const T c4,
600 const int n) {
601
602 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
603 const int str = blockDim.x * gridDim.x;
604
605 for (int i = idx; i < n; i += str) {
606 a[i] = a[i] + c1 * b[i] + c2 * c[i] + c3 * d[i] + c4 * e[i];
607 }
608}
609
613template< typename T >
615 const int n) {
616
617 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
618 const int str = blockDim.x * gridDim.x;
619 const T one = 1.0;
620
621 for (int i = idx; i < n; i += str) {
622 a[i] = one / a[i];
623 }
624}
625
629template< typename T >
631 const T * __restrict__ b,
632 const int n) {
633
634 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
635 const int str = blockDim.x * gridDim.x;
636
637 for (int i = idx; i < n; i += str) {
638 a[i] = a[i] / b[i];
639 }
640}
641
645template< typename T >
647 const T * __restrict__ b,
648 const T * __restrict__ c,
649 const int n) {
650
651 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
652 const int str = blockDim.x * gridDim.x;
653
654 for (int i = idx; i < n; i += str) {
655 a[i] = b[i] / c[i];
656 }
657}
658
662template< typename T >
664 const T * __restrict__ b,
665 const int n) {
666
667 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
668 const int str = blockDim.x * gridDim.x;
669
670 for (int i = idx; i < n; i += str) {
671 a[i] = a[i] * b[i];
672 }
673}
674
678template< typename T >
680 const T * __restrict__ b,
681 const T * __restrict__ c,
682 const int n) {
683
684 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
685 const int str = blockDim.x * gridDim.x;
686
687 for (int i = idx; i < n; i += str) {
688 a[i] = b[i] * c[i];
689 }
690}
691
695template< typename T >
697 const T * __restrict__ b,
698 const T * __restrict__ c,
699 const int n) {
700
701 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
702 const int str = blockDim.x * gridDim.x;
703
704 for (int i = idx; i < n; i += str) {
705 a[i] = a[i] - b[i] * c[i];
706 }
707}
708
712template< typename T >
714 const T * __restrict__ b,
715 const int n) {
716
717 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
718 const int str = blockDim.x * gridDim.x;
719
720 for (int i = idx; i < n; i += str) {
721 a[i] = a[i] - b[i];
722 }
723}
724
728template< typename T >
730 const T * __restrict__ b,
731 const T * __restrict__ c,
732 const int n) {
733
734 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
735 const int str = blockDim.x * gridDim.x;
736
737 for (int i = idx; i < n; i += str) {
738 a[i] = b[i] - c[i];
739 }
740}
741
745template< typename T >
747 const T * __restrict__ b,
748 const T * __restrict__ c,
749 const int n) {
750
751 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
752 const int str = blockDim.x * gridDim.x;
753
754 for (int i = idx; i < n; i += str) {
755 a[i] = a[i] + b[i] * c[i];
756 }
757
758}
759
763template< typename T >
765 const T * __restrict__ b,
766 const T * __restrict__ c,
767 const T * __restrict__ d,
768 const int n) {
769
770 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
771 const int str = blockDim.x * gridDim.x;
772
773 for (int i = idx; i < n; i += str) {
774 a[i] = a[i] + b[i] * c[i] * d[i];
775 }
776
777}
778
782template< typename T >
784 const T * __restrict__ b,
785 const T * __restrict__ c,
786 const T s,
787 const int n) {
788
789 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
790 const int str = blockDim.x * gridDim.x;
791
792 for (int i = idx; i < n; i += str) {
793 a[i] = a[i] + s * b[i] * c[i];
794 }
795
796}
797
801template< typename T >
803 const T * __restrict__ u1,
804 const T * __restrict__ u2,
805 const T * __restrict__ u3,
806 const T * __restrict__ v1,
807 const T * __restrict__ v2,
808 const T * __restrict__ v3,
809 const int n) {
810
811 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
812 const int str = blockDim.x * gridDim.x;
813
814 for (int i = idx; i < n; i += str) {
815 dot[i] = u1[i] * v1[i] + u2[i] * v2[i] + u3[i] * v3[i];
816 }
817
818}
819
823template< typename T >
825 T * __restrict__ u2,
826 T * __restrict__ u3,
827 const T * __restrict__ v1,
828 const T * __restrict__ v2,
829 const T * __restrict__ v3,
830 const T * __restrict__ w1,
831 const T * __restrict__ w2,
832 const T * __restrict__ w3,
833 const int n) {
834
835 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
836 const int str = blockDim.x * gridDim.x;
837
838 for (int i = idx; i < n; i += str) {
839 u1[i] = v2[i]*w3[i] - v3[i]*w2[i];
840 u2[i] = v3[i]*w1[i] - v1[i]*w3[i];
841 u3[i] = v1[i]*w2[i] - v2[i]*w1[i];
842 }
843}
844
848template< typename T>
850 val += __shfl_down(val, 32);
851 val += __shfl_down(val, 16);
852 val += __shfl_down(val, 8);
853 val += __shfl_down(val, 4);
854 val += __shfl_down(val, 2);
855 val += __shfl_down(val, 1);
856 return val;
857}
858
862template< typename T>
864 val = max(val, __shfl_down(val, 32));
865 val = max(val, __shfl_down(val, 16));
866 val = max(val, __shfl_down(val, 8));
867 val = max(val, __shfl_down(val, 4));
868 val = max(val, __shfl_down(val, 2));
869 val = max(val, __shfl_down(val, 1));
870 return val;
871}
872
876template< typename T>
878 val = min(val, __shfl_down(val, 32));
879 val = min(val, __shfl_down(val, 16));
880 val = min(val, __shfl_down(val, 8));
881 val = min(val, __shfl_down(val, 4));
882 val = min(val, __shfl_down(val, 2));
883 val = min(val, __shfl_down(val, 1));
884 return val;
885}
886
890template< typename T >
891__global__ void reduce_kernel(T * bufred, const int n) {
892
893 T sum = 0;
894 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
895 const int str = blockDim.x * gridDim.x;
896 for (int i = idx; i<n ; i += str)
897 {
898 sum += bufred[i];
899 }
900
901 __shared__ T shared[64];
902 unsigned int lane = threadIdx.x % warpSize;
903 unsigned int wid = threadIdx.x / warpSize;
904
906 if (lane == 0)
907 shared[wid] = sum;
909
910 sum = (threadIdx.x < blockDim.x / warpSize) ? shared[lane] : 0;
911 if (wid == 0)
913
914 if (threadIdx.x == 0)
915 bufred[blockIdx.x] = sum;
916}
917
921template< typename T >
922__global__ void reduce_max_kernel(T * bufred, const T ninf, const int n) {
923
924 T max_val = ninf;
925 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
926 const int str = blockDim.x * gridDim.x;
927 for (int i = idx; i<n ; i += str)
928 {
930 }
931
932 __shared__ T shared[64];
933 unsigned int lane = threadIdx.x % warpSize;
934 unsigned int wid = threadIdx.x / warpSize;
935
937 if (lane == 0)
938 shared[wid] = max_val;
940
942 if (wid == 0)
944
945 if (threadIdx.x == 0)
947}
948
952template< typename T >
953__global__ void reduce_min_kernel(T * bufred, const T pinf, const int n) {
954
955 T min_val = pinf;
956 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
957 const int str = blockDim.x * gridDim.x;
958 for (int i = idx; i<n ; i += str)
959 {
961 }
962
963 __shared__ T shared[64];
964 unsigned int lane = threadIdx.x % warpSize;
965 unsigned int wid = threadIdx.x / warpSize;
966
968 if (lane == 0)
969 shared[wid] = min_val;
971
973 if (wid == 0)
975
976 if (threadIdx.x == 0)
978}
979
984template< typename T_acc >
986 const int n,
987 const int j
988 ) {
989 __shared__ T_acc buf[1024] ;
990 const int idx = threadIdx.x;
991 const int y= blockIdx.x;
992 const int step = blockDim.x;
993
994 buf[idx]=0;
995 for (int i=idx ; i<n ; i+=step)
996 {
997 buf[idx] += bufred[i*j + y];
998 }
1000
1001 int i = 512;
1002 while (i != 0)
1003 {
1004 if(threadIdx.x < i && (threadIdx.x + i) < n )
1005 {
1006 buf[threadIdx.x] += buf[threadIdx.x + i] ;
1007 }
1008 i = i>>1;
1009 __syncthreads();
1010 }
1011
1012 bufred[y] = buf[0];
1013}
1014
1018template< typename T >
1020 const T * b,
1021 const T * c,
1022 T * buf_h,
1023 const int n) {
1024
1025 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1026 const int str = blockDim.x * gridDim.x;
1027
1028 const unsigned int lane = threadIdx.x % warpSize;
1029 const unsigned int wid = threadIdx.x / warpSize;
1030
1031 __shared__ T shared[64];
1032 T sum = 0.0;
1033 for (int i = idx; i < n; i+= str) {
1034 sum += a[i] * b[i] * c[i];
1035 }
1036
1038 if (lane == 0)
1039 shared[wid] = sum;
1040 __syncthreads();
1041
1042 sum = (threadIdx.x < blockDim.x / warpSize) ? shared[lane] : 0;
1043 if (wid == 0)
1045
1046 if (threadIdx.x == 0)
1047 buf_h[blockIdx.x] = sum;
1048}
1049
1053template< typename T, typename T_acc >
1055 const T * b,
1056 const T * c,
1057 T_acc * buf_h,
1058 const int n) {
1059
1060 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1061 const int str = blockDim.x * gridDim.x;
1062
1063 const unsigned int lane = threadIdx.x % warpSize;
1064 const unsigned int wid = threadIdx.x / warpSize;
1065
1067 T_acc sum = 0.0;
1068 for (int i = idx; i < n; i+= str) {
1069 sum += static_cast<T_acc>(a[i] * b[i] * c[i]);
1070 }
1071
1073 if (lane == 0)
1074 shared[wid] = sum;
1075 __syncthreads();
1076
1077 sum = (threadIdx.x < blockDim.x / warpSize) ? shared[lane] : 0;
1078 if (wid == 0)
1080
1081 if (threadIdx.x == 0)
1082 buf_h[blockIdx.x] = sum;
1083}
1084
1088template< typename T, typename T_acc >
1090 const T ** b,
1091 const T * c,
1092 T_acc * buf_h,
1093 const int j,
1094 const int n) {
1095
1096 const int idx = blockIdx.x * blockDim.y + threadIdx.y;
1097 const int str = blockDim.y * gridDim.x;
1098 const int y = threadIdx.x;
1099
1100 __shared__ T_acc buf[1024];
1101 T_acc tmp = 0;
1102 if(y < j){
1103 for (int i = idx; i < n; i+= str) {
1104 tmp += static_cast<T_acc>(a[i] * b[threadIdx.x][i] * c[i]);
1105 }
1106 }
1107
1108 buf[threadIdx.y*blockDim.x+y] = tmp;
1109 __syncthreads();
1110
1111 int i = blockDim.y>>1;
1112 while (i != 0) {
1113 if (threadIdx.y < i) {
1114 buf[threadIdx.y*blockDim.x +y] += buf[(threadIdx.y + i)*blockDim.x+y];
1115 }
1116 __syncthreads();
1117 i = i>>1;
1118 }
1119 if (threadIdx.y == 0) {
1120 if( y < j) {
1121 buf_h[j*blockIdx.x+y] = buf[y];
1122 }
1123 }
1124}
1125
1129template< typename T, typename T_acc >
1131 const T * b,
1132 T_acc * buf_h,
1133 const int n) {
1134
1135 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1136 const int str = blockDim.x * gridDim.x;
1137
1138 const unsigned int lane = threadIdx.x % warpSize;
1139 const unsigned int wid = threadIdx.x / warpSize;
1140
1142 T_acc sum = 0.0;
1143 for (int i = idx; i < n; i+= str) {
1144 sum += static_cast<T_acc>(a[i] * b[i]);
1145 }
1146
1148 if (lane == 0)
1149 shared[wid] = sum;
1150 __syncthreads();
1151
1152 sum = (threadIdx.x < blockDim.x / warpSize) ? shared[lane] : 0;
1153 if (wid == 0)
1155
1156 if (threadIdx.x == 0)
1157 buf_h[blockIdx.x] = sum;
1158
1159}
1160
1164template< typename T, typename T_acc >
1166 const T * b,
1167 T_acc * buf_h,
1168 const int n) {
1169
1170 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1171 const int str = blockDim.x * gridDim.x;
1172
1173 const unsigned int lane = threadIdx.x % warpSize;
1174 const unsigned int wid = threadIdx.x / warpSize;
1175
1177 T_acc sum = 0.0;
1178 for (int i = idx; i < n; i+= str) {
1179 sum += static_cast<T_acc>(pow(a[i] - b[i], 2.0));
1180 }
1181
1183 if (lane == 0)
1184 shared[wid] = sum;
1185 __syncthreads();
1186
1187 sum = (threadIdx.x < blockDim.x / warpSize) ? shared[lane] : 0;
1188 if (wid == 0)
1190
1191 if (threadIdx.x == 0)
1192 buf_h[blockIdx.x] = sum;
1193
1194}
1195
1199template< typename T, typename T_acc >
1201 T_acc * buf_h,
1202 const int n) {
1203
1204 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1205 const int str = blockDim.x * gridDim.x;
1206
1207 const unsigned int lane = threadIdx.x % warpSize;
1208 const unsigned int wid = threadIdx.x / warpSize;
1209
1211 T_acc sum = 0;
1212 for (int i = idx; i<n ; i += str)
1213 {
1214 sum += static_cast<T_acc>(a[i]);
1215 }
1216
1218 if (lane == 0)
1219 shared[wid] = sum;
1220 __syncthreads();
1221
1222 sum = (threadIdx.x < blockDim.x / warpSize) ? shared[lane] : 0;
1223 if (wid == 0)
1225
1226 if (threadIdx.x == 0)
1227 buf_h[blockIdx.x] = sum;
1228
1229}
1230
1234template< typename T >
1236 const T ninf,
1237 T * buf_h,
1238 const int n) {
1239
1240 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1241 const int str = blockDim.x * gridDim.x;
1242
1243 const unsigned int lane = threadIdx.x % warpSize;
1244 const unsigned int wid = threadIdx.x / warpSize;
1245
1246 __shared__ T shared[64];
1247 T max_val = ninf;
1248 for (int i = idx; i<n ; i += str)
1249 {
1250 max_val = max(max_val, a[i]);
1251 }
1252
1254 if (lane == 0)
1255 shared[wid] = max_val;
1256 __syncthreads();
1257
1259 if (wid == 0)
1261
1262 if (threadIdx.x == 0)
1263 buf_h[blockIdx.x] = max_val;
1264
1265}
1266
1270template< typename T >
1272 const T pinf,
1273 T * buf_h,
1274 const int n) {
1275
1276 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1277 const int str = blockDim.x * gridDim.x;
1278
1279 const unsigned int lane = threadIdx.x % warpSize;
1280 const unsigned int wid = threadIdx.x / warpSize;
1281
1282 __shared__ T shared[64];
1283 T min_val = pinf;
1284 for (int i = idx; i<n ; i += str)
1285 {
1286 min_val = min(min_val, a[i]);
1287 }
1288
1290 if (lane == 0)
1291 shared[wid] = min_val;
1292 __syncthreads();
1293
1295 if (wid == 0)
1297
1298 if (threadIdx.x == 0)
1299 buf_h[blockIdx.x] = min_val;
1300
1301}
1302
1306template< typename T >
1308 const int n) {
1309
1310 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1311 const int str = blockDim.x * gridDim.x;
1312
1313 for (int i = idx; i < n; i += str) {
1314 a[i] = fabs(a[i]);
1315 }
1316}
1317
1318// ========================================================================== //
1319// Kernels for the point-wise operations
1320
1325template <typename T>
1326__global__ void
1327 pwmax_vec2_kernel(T* __restrict__ a, const T* __restrict__ b, const int n) {
1328
1329 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1330 const int str = blockDim.x * gridDim.x;
1331
1332 for (int i = idx; i < n; i += str) a[i] = max(a[i], b[i]);
1333}
1334
1339template <typename T>
1341 T* __restrict__ a, const T* __restrict__ b, const T* __restrict__ c,
1342 const int n) {
1343
1344 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1345 const int str = blockDim.x * gridDim.x;
1346
1347 for (int i = idx; i < n; i += str) a[i] = max(b[i], c[i]);
1348}
1349
1354template <typename T>
1355__global__ void pwmax_sca2_kernel(T* __restrict__ a, const T c, const int n) {
1356
1357 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1358 const int str = blockDim.x * gridDim.x;
1359
1360 for (int i = idx; i < n; i += str) a[i] = max(a[i], c);
1361}
1362
1367template <typename T>
1369 T* __restrict__ a, const T* __restrict b, 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(b[i], c);
1375}
1376
1381template <typename T>
1382__global__ void
1383 pwmin_vec2_kernel(T* __restrict__ a, const T* __restrict__ b, 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] = min(a[i], b[i]);
1389}
1390
1395template <typename T>
1397 T* __restrict__ a, const T* __restrict__ b, const T* __restrict__ c,
1398 const int n) {
1399
1400 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1401 const int str = blockDim.x * gridDim.x;
1402
1403 for (int i = idx; i < n; i += str) a[i] = min(b[i], c[i]);
1404}
1405
1410template <typename T>
1411__global__ void pwmin_sca2_kernel(T* __restrict__ a, const T c, const int n) {
1412
1413 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1414 const int str = blockDim.x * gridDim.x;
1415
1416 for (int i = idx; i < n; i += str) a[i] = min(a[i], c);
1417}
1418
1423template <typename T>
1425 T* __restrict__ a, const T* __restrict b, 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(b[i], c);
1431}
1432
1433#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