Neko 1.99.5
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
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
71
75template< typename T >
77 T * __restrict__ b,
78 int * __restrict__ mask,
79 const int n,
80 const int n_mask) {
81
82 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
83 const int str = blockDim.x * gridDim.x;
84
85 for (int i = idx; i < n_mask; i += str) {
86 a[i] = b[mask[i]];
87 }
88}
89
91void face_gather_nonlinear_index(int *index, const int idx, const int lx,
92 const int ly, const int lz) {
93 const int idx2 = idx - 1;
94 index[3] = idx2 / (lx * ly * lz);
95 index[2] = (idx2 - (lx * ly * lz) * index[3]) / (lx * ly);
96 index[1] = (idx2 - (lx * ly * lz) * index[3] - (lx * ly) * index[2]) / lx;
97 index[0] = (idx2 - (lx * ly * lz) * index[3] - (lx * ly) * index[2]) -
98 lx * index[1];
99 index[0]++;
100 index[1]++;
101 index[2]++;
102 index[3]++;
103}
104
106int face_gather_idx(const int i, const int j, const int k, const int l,
107 const int n1, const int n2, const int nf) {
108 return ((i) + (n1) * (((j) - 1) + (n2) * (((k) - 1) + (nf) * (((l) - 1))))) - 1;
109}
110
114template< typename T >
116 const T * __restrict__ b,
117 const int * __restrict__ mask,
118 const int * __restrict__ facet,
119 const int n1,
120 const int n2,
121 const int lx,
122 const int ly,
123 const int lz,
124 const int n_mask) {
125 int index[4];
126
127 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
128 const int str = blockDim.x * gridDim.x;
129
130 for (int m = idx; m < n_mask; m += str) {
131 const int f = facet[m + 1];
132 face_gather_nonlinear_index(index, mask[m + 1], lx, ly, lz);
133
134 switch (f) {
135 case 1:
136 case 2:
137 a[m] = b[face_gather_idx(index[1], index[2], f, index[3], n1, n2, 6)];
138 break;
139 case 3:
140 case 4:
141 a[m] = b[face_gather_idx(index[0], index[2], f, index[3], n1, n2, 6)];
142 break;
143 case 5:
144 case 6:
145 a[m] = b[face_gather_idx(index[0], index[1], f, index[3], n1, n2, 6)];
146 break;
147 }
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
188#if __CUDA_ARCH__ < 600
189#include <cassert>
190#endif
191
195template< typename T >
197 T * __restrict__ b,
198 int * __restrict__ mask,
199 const int n,
200 const int m) {
201
202 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
203 const int str = blockDim.x * gridDim.x;
204
205#if __CUDA_ARCH__ >= 600
206 for (int i = idx; i < m; i += str)
207 atomicAdd( &(a[mask[i+1]-1]), b[i]);
208#else
209 if (idx == 0)
210 assert(0 && "masked_atomic_reduction_kernel requires compute capability 6.0 or higher.");
211#endif
212}
213
217template< typename T >
219 T * __restrict__ b,
220 int * __restrict__ mask,
221 const int n,
222 const int n_mask) {
223
224 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
225 const int str = blockDim.x * gridDim.x;
226
227 for (int i = idx; i < n_mask; i += str) {
228 a[mask[i+1]-1] = b[mask[i+1]-1];
229 }
230}
231
235template< typename T >
237 T * __restrict__ b,
238 int * __restrict__ mask,
239 const int n,
240 const int n_mask) {
241
242 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
243 const int str = blockDim.x * gridDim.x;
244
245 for (int i = idx; i < n_mask; i += str) {
246 a[mask[i]] = b[mask[i]];
247 }
248}
249
253template <typename T>
255 const T c,
256 const int size,
257 int* __restrict__ mask,
258 const int mask_size) {
259
260 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
261 const int str = blockDim.x * gridDim.x;
262
263 for (int i = idx; i < mask_size; i += str) { a[mask[i]] = c; }
264}
265
269template< typename T >
271 T * __restrict__ b,
272 const T c,
273 const int n) {
274
275 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
276 const int str = blockDim.x * gridDim.x;
277
278 for (int i = idx; i < n; i += str) {
279 a[i] = c * b[i];
280 }
281}
282
286template< typename T >
288 const T c,
289 const int n) {
290
291 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
292 const int str = blockDim.x * gridDim.x;
293
294 for (int i = idx; i < n; i += str) {
295 a[i] = c / a[i];
296 }
297}
298
302template< typename T >
304 T * __restrict__ b,
305 const T c,
306 const int n) {
307
308 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
309 const int str = blockDim.x * gridDim.x;
310
311 for (int i = idx; i < n; i += str) {
312 a[i] = c / b[i];
313 }
314}
315
319template< typename T >
321 const T c,
322 const int n) {
323
324 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
325 const int str = blockDim.x * gridDim.x;
326
327 for (int i = idx; i < n; i += str) {
328 a[i] = a[i] + c;
329 }
330}
331
335template< typename T >
337 T * __restrict__ b,
338 const T c,
339 const int n) {
340
341 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
342 const int str = blockDim.x * gridDim.x;
343
344 for (int i = idx; i < n; i += str) {
345 a[i] = b[i] + c;
346 }
347}
348
352template< typename T >
354 const T min_val,
355 const T max_val,
356 const int n) {
357
358 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
359 const int str = blockDim.x * gridDim.x;
360 const T l = max_val - min_val;
361
362 for (int i = idx; i < n; i += str) {
363 a[i] = min_val + fmod(fmod(a[i] - min_val, l) + l, l);
364 }
365}
366
370template< typename T >
372 const T c,
373 const int n) {
374
375 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
376 const int str = blockDim.x * gridDim.x;
377
378 for (int i = idx; i < n; i += str) {
379 a[i] = c;
380 }
381}
382
386template< typename T >
388 const T * __restrict__ b,
389 const int n) {
390
391 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
392 const int str = blockDim.x * gridDim.x;
393
394 for (int i = idx; i < n; i += str) {
395 a[i] = a[i] + b[i];
396 }
397}
398
402template< typename T >
404 const T * __restrict__ b,
405 const T * __restrict__ c,
406 const int n) {
407
408 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
409 const int str = blockDim.x * gridDim.x;
410
411 for (int i = idx; i < n; i += str) {
412 a[i] = b[i] + c[i];
413 }
414}
415
419template< typename T >
421 const T * __restrict__ b,
422 const T * __restrict__ c,
423 const T * __restrict__ d,
424 const int n) {
425
426 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
427 const int str = blockDim.x * gridDim.x;
428
429 for (int i = idx; i < n; i += str) {
430 a[i] = b[i] + c[i] + d[i];
431 }
432}
433
437template< typename T >
439 const T * __restrict__ b,
440 const T c1,
441 const int n) {
442
443 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
444 const int str = blockDim.x * gridDim.x;
445
446 for (int i = idx; i < n; i += str) {
447 a[i] = c1 * a[i] + b[i];
448 }
449}
450
454template< typename T >
456 const T ** p,
457 const T * alpha,
458 const int p_cur,
459 const int n) {
460
461 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
462 const int str = blockDim.x * gridDim.x;
463
464
465 for (int i = idx; i < n; i+= str) {
466 T tmp = 0.0;
467 for (int j = 0; j < p_cur; j ++) {
468 tmp += p[j][i]*alpha[j];
469 }
470 x[i] += tmp;
471 }
472}
473
477template< typename T >
479 const T * __restrict__ b,
480 const T c1,
481 const int n) {
482
483 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
484 const int str = blockDim.x * gridDim.x;
485
486 for (int i = idx; i < n; i += str) {
487 a[i] = a[i] + c1 * b[i];
488 }
489}
490
494template< typename T >
496 const T * __restrict__ b,
497 const T c1,
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 for (int i = idx; i < n; i += str) {
504 a[i] = a[i] + c1 * (b[i] * b[i]);
505 }
506}
507
511template< typename T >
513 const T * __restrict__ b,
514 const T * __restrict__ c,
515 const T c1,
516 const T c2,
517 const int n) {
518
519 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
520 const int str = blockDim.x * gridDim.x;
521
522 for (int i = idx; i < n; i += str) {
523 a[i] = c1 * b[i] + c2 * c[i];
524 }
525}
526
530template< typename T >
532 const T * __restrict__ b,
533 const T * __restrict__ c,
534 const T * __restrict__ d,
535 const T c1,
536 const T c2,
537 const T c3,
538 const int n) {
539
540 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
541 const int str = blockDim.x * gridDim.x;
542
543 for (int i = idx; i < n; i += str) {
544 a[i] = c1 * b[i] + c2 * c[i] + c3 * d[i];
545 }
546}
547
551template< typename T >
553 const T * __restrict__ b,
554 const T * __restrict__ c,
555 const T * __restrict__ d,
556 const T * __restrict__ e,
557 const T c1,
558 const T c2,
559 const T c3,
560 const T c4,
561 const int n) {
562
563 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
564 const int str = blockDim.x * gridDim.x;
565
566 for (int i = idx; i < n; i += str) {
567 a[i] = a[i] + c1 * b[i] + c2 * c[i] + c3 * d[i] + c4 * e[i];
568 }
569}
570
574template< typename T >
576 const int n) {
577
578 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
579 const int str = blockDim.x * gridDim.x;
580 const T one = 1.0;
581
582 for (int i = idx; i < n; i += str) {
583 a[i] = one / a[i];
584 }
585}
586
590template< typename T >
592 const T * __restrict__ b,
593 const int n) {
594
595 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
596 const int str = blockDim.x * gridDim.x;
597
598 for (int i = idx; i < n; i += str) {
599 a[i] = a[i] / b[i];
600 }
601}
602
606template< typename T >
608 const T * __restrict__ b,
609 const T * __restrict__ c,
610 const int n) {
611
612 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
613 const int str = blockDim.x * gridDim.x;
614
615 for (int i = idx; i < n; i += str) {
616 a[i] = b[i] / c[i];
617 }
618}
619
623template< typename T >
625 const T * __restrict__ b,
626 const int n) {
627
628 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
629 const int str = blockDim.x * gridDim.x;
630
631 for (int i = idx; i < n; i += str) {
632 a[i] = a[i] * b[i];
633 }
634}
635
639template< typename T >
641 const T * __restrict__ b,
642 const T * __restrict__ c,
643 const int n) {
644
645 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
646 const int str = blockDim.x * gridDim.x;
647
648 for (int i = idx; i < n; i += str) {
649 a[i] = b[i] * c[i];
650 }
651}
652
656template< typename T >
658 const T * __restrict__ b,
659 const T * __restrict__ c,
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] * c[i];
667 }
668}
669
673template< typename T >
675 const T * __restrict__ b,
676 const int n) {
677
678 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
679 const int str = blockDim.x * gridDim.x;
680
681 for (int i = idx; i < n; i += str) {
682 a[i] = a[i] - b[i];
683 }
684}
685
689template< typename T >
691 const T * __restrict__ b,
692 const T * __restrict__ c,
693 const int n) {
694
695 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
696 const int str = blockDim.x * gridDim.x;
697
698 for (int i = idx; i < n; i += str) {
699 a[i] = b[i] - c[i];
700 }
701}
702
706template< typename T >
708 const T * __restrict__ b,
709 const T * __restrict__ c,
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] * c[i];
717 }
718
719}
720
724template< typename T >
726 const T * __restrict__ b,
727 const T * __restrict__ c,
728 const T * __restrict__ d,
729 const int n) {
730
731 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
732 const int str = blockDim.x * gridDim.x;
733
734 for (int i = idx; i < n; i += str) {
735 a[i] = a[i] + b[i] * c[i] * d[i];
736 }
737
738}
739
743template< typename T >
745 const T * __restrict__ b,
746 const T * __restrict__ c,
747 const T s,
748 const int n) {
749
750 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
751 const int str = blockDim.x * gridDim.x;
752
753 for (int i = idx; i < n; i += str) {
754 a[i] = a[i] + s * b[i] * c[i];
755 }
756
757}
758
762template< typename T >
764 const T * __restrict__ u1,
765 const T * __restrict__ u2,
766 const T * __restrict__ u3,
767 const T * __restrict__ v1,
768 const T * __restrict__ v2,
769 const T * __restrict__ v3,
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 dot[i] = u1[i] * v1[i] + u2[i] * v2[i] + u3[i] * v3[i];
777 }
778
779}
780
784template< typename T >
786 T * __restrict__ u2,
787 T * __restrict__ u3,
788 const T * __restrict__ v1,
789 const T * __restrict__ v2,
790 const T * __restrict__ v3,
791 const T * __restrict__ w1,
792 const T * __restrict__ w2,
793 const T * __restrict__ w3,
794 const int n) {
795
796 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
797 const int str = blockDim.x * gridDim.x;
798
799 for (int i = idx; i < n; i += str) {
800 u1[i] = v2[i]*w3[i] - v3[i]*w2[i];
801 u2[i] = v3[i]*w1[i] - v1[i]*w3[i];
802 u3[i] = v1[i]*w2[i] - v2[i]*w1[i];
803 }
804
805}
806
807
811template< typename T>
813 val += __shfl_down_sync(0xffffffff, val, 16);
814 val += __shfl_down_sync(0xffffffff, val, 8);
815 val += __shfl_down_sync(0xffffffff, val, 4);
816 val += __shfl_down_sync(0xffffffff, val, 2);
817 val += __shfl_down_sync(0xffffffff, val, 1);
818 return val;
819}
820
824template< typename T>
826 val = max(val, __shfl_down_sync(0xffffffff, val, 16));
827 val = max(val, __shfl_down_sync(0xffffffff, val, 8));
828 val = max(val, __shfl_down_sync(0xffffffff, val, 4));
829 val = max(val, __shfl_down_sync(0xffffffff, val, 2));
830 val = max(val, __shfl_down_sync(0xffffffff, val, 1));
831 return val;
832}
833
837template< typename T>
839 val = min(val, __shfl_down_sync(0xffffffff, val, 16));
840 val = min(val, __shfl_down_sync(0xffffffff, val, 8));
841 val = min(val, __shfl_down_sync(0xffffffff, val, 4));
842 val = min(val, __shfl_down_sync(0xffffffff, val, 2));
843 val = min(val, __shfl_down_sync(0xffffffff, val, 1));
844 return val;
845}
846
850template< typename T >
851__global__ void reduce_kernel(T * bufred, const int n) {
852
853 T sum = 0;
854 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
855 const int str = blockDim.x * gridDim.x;
856 for (int i = idx; i<n ; i += str)
857 {
858 sum += bufred[i];
859 }
860
861 __shared__ T shared[32];
862 unsigned int lane = threadIdx.x % warpSize;
863 unsigned int wid = threadIdx.x / warpSize;
864
866 if (lane == 0)
867 shared[wid] = sum;
869
870 sum = (threadIdx.x < blockDim.x / warpSize) ? shared[lane] : 0;
871 if (wid == 0)
873
874 if (threadIdx.x == 0)
875 bufred[blockIdx.x] = sum;
876}
877
881template< typename T >
882__global__ void reduce_max_kernel(T * bufred, const T ninf, const int n) {
883
884 T max_val = ninf;
885 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
886 const int str = blockDim.x * gridDim.x;
887 for (int i = idx; i<n ; i += str)
888 {
890 }
891
892 __shared__ T shared[32];
893 unsigned int lane = threadIdx.x % warpSize;
894 unsigned int wid = threadIdx.x / warpSize;
895
897 if (lane == 0)
898 shared[wid] = max_val;
900
902 if (wid == 0)
904
905 if (threadIdx.x == 0)
907}
908
912template< typename T >
913__global__ void reduce_min_kernel(T * bufred, const T pinf, const int n) {
914
915 T min_val = pinf;
916 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
917 const int str = blockDim.x * gridDim.x;
918 for (int i = idx; i<n ; i += str)
919 {
921 }
922
923 __shared__ T shared[32];
924 unsigned int lane = threadIdx.x % warpSize;
925 unsigned int wid = threadIdx.x / warpSize;
926
928 if (lane == 0)
929 shared[wid] = min_val;
931
933 if (wid == 0)
935
936 if (threadIdx.x == 0)
938}
939
944template< typename T >
946 const int n,
947 const int j
948 ) {
949 __shared__ T buf[1024] ;
950 const int idx = threadIdx.x;
951 const int y= blockIdx.x;
952 const int step = blockDim.x;
953
954 buf[idx]=0;
955 for (int i=idx ; i<n ; i+=step)
956 {
957 buf[idx] += bufred[i*j + y];
958 }
960
961 int i = 512;
962 while (i != 0)
963 {
964 if(threadIdx.x < i && (threadIdx.x + i) < n )
965 {
966 buf[threadIdx.x] += buf[threadIdx.x + i] ;
967 }
968 i = i>>1;
970 }
971
972 bufred[y] = buf[0];
973}
974
975
979template< typename T >
981 const T * b,
982 const T * c,
983 T * buf_h,
984 const int n) {
985
986 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
987 const int str = blockDim.x * gridDim.x;
988
989 const unsigned int lane = threadIdx.x % warpSize;
990 const unsigned int wid = threadIdx.x / warpSize;
991
992 __shared__ T shared[32];
993 T sum = 0.0;
994 for (int i = idx; i < n; i+= str) {
995 sum += a[i] * b[i] * c[i];
996 }
997
999 if (lane == 0)
1000 shared[wid] = sum;
1001 __syncthreads();
1002
1003 sum = (threadIdx.x < blockDim.x / warpSize) ? shared[lane] : 0;
1004 if (wid == 0)
1006
1007 if (threadIdx.x == 0)
1008 buf_h[blockIdx.x] = sum;
1009}
1010
1014template< typename T >
1016 const T ** b,
1017 const T * c,
1018 T * buf_h,
1019 const int j,
1020 const int n) {
1021
1022 const int idx = blockIdx.x * blockDim.y + threadIdx.y;
1023 const int str = blockDim.y * gridDim.x;
1024 const int y = threadIdx.x;
1025
1026 __shared__ T buf[1024];
1027 T tmp = 0;
1028 if(y < j){
1029 for (int i = idx; i < n; i+= str) {
1030 tmp += a[i] * b[threadIdx.x][i] * c[i];
1031 }
1032 }
1033
1034 buf[threadIdx.y*blockDim.x+y] = tmp;
1035 __syncthreads();
1036
1037 int i = blockDim.y>>1;
1038 while (i != 0) {
1039 if (threadIdx.y < i) {
1040 buf[threadIdx.y*blockDim.x +y] += buf[(threadIdx.y + i)*blockDim.x+y];
1041 }
1042 __syncthreads();
1043 i = i>>1;
1044 }
1045 if (threadIdx.y == 0) {
1046 if( y < j) {
1047 buf_h[j*blockIdx.x+y] = buf[y];
1048 }
1049 }
1050}
1051
1055template< typename T >
1057 const T * b,
1058 T * buf_h,
1059 const int n) {
1060
1061 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1062 const int str = blockDim.x * gridDim.x;
1063
1064 const unsigned int lane = threadIdx.x % warpSize;
1065 const unsigned int wid = threadIdx.x / warpSize;
1066
1067 __shared__ T shared[32];
1068 T sum = 0.0;
1069 for (int i = idx; i < n; i+= str) {
1070 sum += a[i] * b[i];
1071 }
1072
1074 if (lane == 0)
1075 shared[wid] = sum;
1076 __syncthreads();
1077
1078 sum = (threadIdx.x < blockDim.x / warpSize) ? shared[lane] : 0;
1079 if (wid == 0)
1081
1082 if (threadIdx.x == 0)
1083 buf_h[blockIdx.x] = sum;
1084
1085}
1086
1090template< typename T >
1092 const T * b,
1093 T * buf_h,
1094 const int n) {
1095
1096 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1097 const int str = blockDim.x * gridDim.x;
1098
1099 const unsigned int lane = threadIdx.x % warpSize;
1100 const unsigned int wid = threadIdx.x / warpSize;
1101
1102 __shared__ T shared[32];
1103 T sum = 0.0;
1104 for (int i = idx; i < n; i+= str) {
1105 sum += pow(a[i] - b[i], 2.0);
1106 }
1107
1109 if (lane == 0)
1110 shared[wid] = sum;
1111 __syncthreads();
1112
1113 sum = (threadIdx.x < blockDim.x / warpSize) ? shared[lane] : 0;
1114 if (wid == 0)
1116
1117 if (threadIdx.x == 0)
1118 buf_h[blockIdx.x] = sum;
1119
1120}
1121
1125template< typename T >
1127 T * 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
1136 __shared__ T shared[32];
1137 T sum = 0;
1138 for (int i = idx; i<n ; i += str)
1139 {
1140 sum += a[i];
1141 }
1142
1144 if (lane == 0)
1145 shared[wid] = sum;
1146 __syncthreads();
1147
1148 sum = (threadIdx.x < blockDim.x / warpSize) ? shared[lane] : 0;
1149 if (wid == 0)
1151
1152 if (threadIdx.x == 0)
1153 buf_h[blockIdx.x] = sum;
1154
1155}
1156
1160template< typename T >
1162 const T ninf,
1163 T * buf_h,
1164 const int n) {
1165
1166 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1167 const int str = blockDim.x * gridDim.x;
1168
1169 const unsigned int lane = threadIdx.x % warpSize;
1170 const unsigned int wid = threadIdx.x / warpSize;
1171
1172 __shared__ T shared[32];
1173 T max_val = ninf;
1174 for (int i = idx; i<n ; i += str)
1175 {
1176 max_val = max(max_val, a[i]);
1177 }
1178
1180 if (lane == 0)
1181 shared[wid] = max_val;
1182 __syncthreads();
1183
1185 if (wid == 0)
1187
1188 if (threadIdx.x == 0)
1189 buf_h[blockIdx.x] = max_val;
1190
1191}
1192
1196template< typename T >
1198 const T pinf,
1199 T * buf_h,
1200 const int n) {
1201
1202 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1203 const int str = blockDim.x * gridDim.x;
1204
1205 const unsigned int lane = threadIdx.x % warpSize;
1206 const unsigned int wid = threadIdx.x / warpSize;
1207
1208 __shared__ T shared[32];
1209 T min_val = pinf;
1210 for (int i = idx; i<n ; i += str)
1211 {
1212 min_val = min(min_val, a[i]);
1213 }
1214
1216 if (lane == 0)
1217 shared[wid] = min_val;
1218 __syncthreads();
1219
1221 if (wid == 0)
1223
1224 if (threadIdx.x == 0)
1225 buf_h[blockIdx.x] = min_val;
1226
1227}
1228
1232template< typename T >
1234 const int n) {
1235
1236 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1237 const int str = blockDim.x * gridDim.x;
1238
1239 for (int i = idx; i < n; i += str) {
1240 a[i] = fabs(a[i]);
1241 }
1242}
1243
1244// ========================================================================== //
1245// Kernels for the point-wise operations
1246
1251template <typename T>
1252__global__ void
1253 pwmax_vec2_kernel(T* __restrict__ a, const T* __restrict__ b, const int n) {
1254
1255 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1256 const int str = blockDim.x * gridDim.x;
1257
1258 for (int i = idx; i < n; i += str) a[i] = max(a[i], b[i]);
1259}
1260
1265template <typename T>
1267 T* __restrict__ a, const T* __restrict__ b, const T* __restrict__ c,
1268 const int n) {
1269
1270 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1271 const int str = blockDim.x * gridDim.x;
1272
1273 for (int i = idx; i < n; i += str) a[i] = max(b[i], c[i]);
1274}
1275
1280template <typename T>
1281__global__ void pwmax_sca2_kernel(T* __restrict__ a, const T c, const int n) {
1282
1283 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1284 const int str = blockDim.x * gridDim.x;
1285
1286 for (int i = idx; i < n; i += str) a[i] = max(a[i], c);
1287}
1288
1293template <typename T>
1295 T* __restrict__ a, const T* __restrict b, const T c, const int n) {
1296
1297 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1298 const int str = blockDim.x * gridDim.x;
1299
1300 for (int i = idx; i < n; i += str) a[i] = max(b[i], c);
1301}
1302
1307template <typename T>
1308__global__ void
1309 pwmin_vec2_kernel(T* __restrict__ a, const T* __restrict__ b, const int n) {
1310
1311 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1312 const int str = blockDim.x * gridDim.x;
1313
1314 for (int i = idx; i < n; i += str) a[i] = min(a[i], b[i]);
1315}
1316
1321template <typename T>
1323 T* __restrict__ a, const T* __restrict__ b, const T* __restrict__ c,
1324 const int n) {
1325
1326 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1327 const int str = blockDim.x * gridDim.x;
1328
1329 for (int i = idx; i < n; i += str) a[i] = min(b[i], c[i]);
1330}
1331
1336template <typename T>
1337__global__ void pwmin_sca2_kernel(T* __restrict__ a, const T c, 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] = min(a[i], c);
1343}
1344
1349template <typename T>
1351 T* __restrict__ a, const T* __restrict b, const T c, const int n) {
1352
1353 const int idx = blockIdx.x * blockDim.x + threadIdx.x;
1354 const int str = blockDim.x * gridDim.x;
1355
1356 for (int i = idx; i < n; i += str) a[i] = min(b[i], c);
1357}
1358
1359#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 glsc3_many_kernel(const T *a, const T **b, const T *c, T *buf_h, const int j, 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 glsc3_reduce_kernel(T *bufred, const int n, const int j)
__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:76
__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:91
__global__ void reduce_max_kernel(T *bufred, const T ninf, const int n)
__global__ void glsubnorm2_kernel(const T *a, const T *b, T *buf_h, 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 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:57
__global__ void add2s2_many_kernel(T *__restrict__ x, const T **p, const T *alpha, const int p_cur, 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:41
__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 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 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 glsc2_kernel(const T *a, const T *b, 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 glsum_kernel(const T *a, T *buf_h, const int n)
__global__ void glsc3_kernel(const T *a, const T *b, const T *c, T *buf_h, const int n)
__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 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 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)
real * bufred
Definition math.cu:679
Object for handling masks in Neko.
Definition mask.f90:34
real * buf
Definition pipecg_aux.cu:42
#define max(a, b)
Definition tensor.cu:40