Neko 1.99.9
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
tensor.cu
Go to the documentation of this file.
1/*
2 Copyright (c) 2021-2023, The Neko Authors
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11
12 * Redistributions in binary form must reproduce the above
13 copyright notice, this list of conditions and the following
14 disclaimer in the documentation and/or other materials provided
15 with the distribution.
16
17 * Neither the name of the authors nor the names of its
18 contributors may be used to endorse or promote products derived
19 from this software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 POSSIBILITY OF SUCH DAMAGE.
33*/
34
36#include <device/cuda/check.h>
37#include "tensor_kernel.h"
38#include <stdio.h>
39
40#define max(a,b) \
41 ({ __typeof__ (a) _a = (a); \
42 __typeof__ (b) _b = (b); \
43 _a > _b ? _a : _b; })
44
45template<const int N>
46static void set_tnsr3d_large_shmem_attr(const size_t shmem_size) {
47 static bool is_set = false;
48
49 if (!is_set) {
52 shmem_size));
53 is_set = true;
54 }
55}
56
57/*
58 * Threads per block for tnsr3d.
59 *
60 * One thread per output point of the widest of the three phases, rounded up
61 * to whole warps and capped at the block maximum. The three phases
62 * produce nu*nu*nv, nu*nv*nv and nv*nv*nv points, so the widest is nu*nu*nv
63 * when restricting and nv*nv*nv when prolonging.
64 *
65 * This used to be a flat 1024 regardless, which on a p-transfer left most of
66 * the block with nothing to do: an 8 -> 4 restriction has 256, 128 and 64
67 * points in its phases, so three quarters to fifteen sixteenths of the
68 * threads idled. p-multigrid transfers between adjacent levels constantly,
69 * which is where that showed up.
70 *
71 * Any block size is correct here: the phases are grid stride loops and the
72 * barriers between them sit outside the loop bodies, so a thread with no
73 * iterations still reaches every barrier.
74 */
75static int cuda_tnsr3d_nthrds(const int nu, const int nv)
76{
77 const int w1 = nu * nu * nv;
78 const int w3 = nv * nv * nv;
79 int work = (w1 > w3) ? w1 : w3;
80
81 work = ((work + 32 - 1) / 32) * 32;
82 if (work > 1024) work = 1024;
83 if (work < 32) work = 32;
84 return work;
85}
86
87extern "C" {
88
90 void cuda_tnsr3d(void *v, int *nv, void *u, int *nu,
91 void *A, void *Bt, void *Ct, int *nel) {
92 const dim3 nthrds(cuda_tnsr3d_nthrds(*nu, *nv), 1, 1);
93 const dim3 nblcks(*nel, 1, 1);
94 const cudaStream_t stream = (cudaStream_t) glb_cmd_queue;
95
96 int n = max(*nu,*nv);
97#define CASE(N) \
98 case N: \
99 tnsr3d_kernel<real, N> \
100 <<<nblcks, nthrds, 0, stream>>>((real *) v, *nv, \
101 (real *) u, *nu, \
102 (real *) A, (real *) Bt, (real *) Ct); \
103 CUDA_CHECK(cudaGetLastError()); \
104 break
105
106#define CASE_LARGE(N) \
107 case N: { \
108 const size_t shmem_size = 2 * N * N * N * sizeof(real); \
109 set_tnsr3d_large_shmem_attr<N>(shmem_size); \
110 tnsr3d_kernel_large<real, N> \
111 <<<nblcks, nthrds, shmem_size, stream>>>((real *) v, *nv, \
112 (real *) u, *nu, \
113 (real *) A, (real *) Bt, \
114 (real *) Ct); \
115 CUDA_CHECK(cudaGetLastError()); \
116 } \
117 break
118
119 switch(n) {
120 CASE(2);
121 CASE(3);
122 CASE(4);
123 CASE(5);
124 CASE(6);
125 CASE(7);
126 CASE(8);
127 CASE(9);
128 CASE(10);
129 CASE(11);
130 CASE(12);
131 CASE(13);
132 CASE(14);
133 CASE_LARGE(15);
134 CASE_LARGE(16);
135 default:
136 {
137 fprintf(stderr, __FILE__ ": size not supported: %d\n", n);
138 exit(1);
139 }
140 }
141 }
142
144 void cuda_tnsr3d_el_list(void *v, int *nv, void *u, int *nu,
145 void *A, void *Bt, void *Ct, int * elements, int* n_points) {
146 const dim3 nthrds(cuda_tnsr3d_nthrds(*nu, *nv), 1, 1);
147 const dim3 nblcks(*n_points, 1, 1);
148 const cudaStream_t stream = (cudaStream_t) glb_cmd_queue;
149
150 int n = max(*nu,*nv);
151#define CASE2(N) \
152 case N: \
153 tnsr3d_el_kernel<real, N> \
154 <<<nblcks, nthrds, 0, stream>>>((real *) v, *nv, \
155 (real *) u, *nu, \
156 (real *) A, (real *) Bt, (real *) Ct, \
157 (int *) elements, *n_points); \
158 CUDA_CHECK(cudaGetLastError()); \
159 break
160
161 switch(n) {
162 CASE2(2);
163 CASE2(3);
164 CASE2(4);
165 CASE2(5);
166 CASE2(6);
167 CASE2(7);
168 CASE2(8);
169 CASE2(9);
170 CASE2(10);
171 CASE2(11);
172 CASE2(12);
173 CASE2(13);
174 CASE2(14);
175 default:
176 {
177 fprintf(stderr, __FILE__ ": size not supported: %d\n", n);
178 exit(1);
179 }
180 }
181 }
182
183}
__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)
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ u
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ v
__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
#define CUDA_CHECK(err)
Definition check.h:6
void cuda_tnsr3d(void *v, int *nv, void *u, int *nu, void *A, void *Bt, void *Ct, int *nel)
Definition tensor.cu:90
void cuda_tnsr3d_el_list(void *v, int *nv, void *u, int *nu, void *A, void *Bt, void *Ct, int *elements, int *n_points)
Definition tensor.cu:144
#define CASE(N)
#define CASE_LARGE(N)
#define CASE2(N)
static int cuda_tnsr3d_nthrds(const int nu, const int nv)
Definition tensor.cu:75
static void set_tnsr3d_large_shmem_attr(const size_t shmem_size)
Definition tensor.cu:46
#define max(a, b)
Definition tensor.cu:40