Neko 1.99.9
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
opr_opgrad.cu
Go to the documentation of this file.
1/*
2 Copyright (c) 2021-2026, 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
35#include <string.h>
36#include <stdlib.h>
37#include <stdio.h>
38#include "opgrad_kernel.h"
39#include "elem_block_tune.h"
41#include <device/cuda/check.h>
42
43extern "C" {
44 #include <common/neko_log.h>
45}
46
47template < const int >
48int tune_opgrad(void *ux, void *uy, void *uz, void *u,
49 void *dx, void *dy, void *dz,
50 void *drdx, void *dsdx, void *dtdx,
51 void *drdy, void *dsdy, void *dtdy,
52 void *drdz, void *dsdz, void *dtdz,
53 void *w3, int *nel, int *lx, int *eb_sel, int *ch_sel,
54 int *nw_sel, int *tw_sel);
55
56extern "C" {
57
61 void cuda_opgrad(void *ux, void *uy, void *uz, void *u,
62 void *dx, void *dy, void *dz,
63 void *drdx, void *dsdx, void *dtdx,
64 void *drdy, void *dsdy, void *dtdy,
65 void *drdz, void *dsdz, void *dtdz,
66 void *w3, int *nel, int *lx) {
67
68 static int autotune[17] = { 0 };
69 /* elements per block candidate chosen by the tuner */
70 static int autotune_eb[17] = { 0 };
71 /* chunk candidate chosen for the 1d variant */
72 static int autotune_ch[17] = { 0 };
73 /* warps per block candidate chosen for the dmma variant */
74 static int autotune_nw[17] = { 0 };
75 /* warps per block candidate chosen for the tma staged dmma variant */
76 static int autotune_tw[17] = { 0 };
77
78 const dim3 nthrds_1d(1024, 1, 1);
79 const dim3 nthrds_kstep((*lx), (*lx), 1);
80 const dim3 nblcks((*nel), 1, 1);
81 const cudaStream_t stream = (cudaStream_t) glb_cmd_queue;
82
83#define CASE_1D(LX, C) \
84 opgrad_kernel_1d<real, LX, NEKO_CHUNKS(LX, C)> \
85 <<<nblcks, NEKO_CHUNKS_NTHRDS(LX, C), 0, stream>>> \
86 ((real *) ux, (real *) uy, (real *) uz, (real *) u, \
87 (real *) dx, (real *) dy, (real *) dz, \
88 (real *) drdx, (real *) dsdx, (real *) dtdx, \
89 (real *) drdy, (real *) dsdy, (real *) dtdy, \
90 (real *) drdz, (real *) dsdz, (real *) dtdz, \
91 (real *) w3); \
92 CUDA_CHECK(cudaGetLastError());
93
94/* Runtime dispatch onto the tuned chunk candidate */
95#define CASE_1D_SEL(LX, SEL) \
96 switch (SEL) { \
97 case 0: CASE_1D(LX, 0); break; \
98 case 1: CASE_1D(LX, 1); break; \
99 case 2: CASE_1D(LX, 2); break; \
100 default: CASE_1D(LX, 3); break; \
101 }
102
103
104#define CASE_KSTEP(LX, C) \
105 opgrad_kernel_kstep<real, LX, NEKO_EB(LX, C)> \
106 <<<NEKO_EB_NBLCKS(*nel, LX, C), NEKO_EB_NTHRDS(LX, C), 0, stream>>> \
107 ((real *) ux, (real *) uy, (real *) uz, (real *) u, \
108 (real *) dx, (real *) dy, (real *) dz, \
109 (real *) drdx, (real *) dsdx, (real *) dtdx, \
110 (real *) drdy, (real *) dsdy, (real *) dtdy, \
111 (real *) drdz, (real *) dsdz, (real *) dtdz, \
112 (real *) w3, *nel); \
113 CUDA_CHECK(cudaGetLastError());
114
115/* Runtime dispatch onto the tuned candidate */
116#define CASE_KSTEP_SEL(LX, SEL) \
117 switch (SEL) { \
118 case 0: CASE_KSTEP(LX, 0); break; \
119 case 1: CASE_KSTEP(LX, 1); break; \
120 default: CASE_KSTEP(LX, 2); break; \
121 }
122
123#define CASE_DMMA(LX, C) \
124 opgrad_kernel_dmma<real, LX, NEKO_DMMA_NW(C)> \
125 <<<NEKO_DMMA_NBLCKS(*nel, LX), NEKO_DMMA_NTHRDS(C), 0, stream>>> \
126 ((real *) ux, (real *) uy, (real *) uz, (real *) u, \
127 (real *) dx, (real *) dy, (real *) dz, \
128 (real *) drdx, (real *) dsdx, (real *) dtdx, \
129 (real *) drdy, (real *) dsdy, (real *) dtdy, \
130 (real *) drdz, (real *) dsdz, (real *) dtdz, \
131 (real *) w3, *nel); \
132 CUDA_CHECK(cudaGetLastError());
133
134/* Runtime dispatch onto the tuned warps per block candidate */
135#define CASE_DMMA_SEL(LX, SEL) \
136 switch (SEL) { \
137 case 0: CASE_DMMA(LX, 0); break; \
138 case 1: CASE_DMMA(LX, 1); break; \
139 default: CASE_DMMA(LX, 2); break; \
140 }
141
142/*
143 * The TMA staged dmma variant. Same grid and the same warps per block
144 * candidates as CASE_DMMA -- at the one lx it supports NEKO_DMMA_PACK is 1,
145 * so the grid is nel blocks -- but unlike every other launch here it carries
146 * a dynamic shared memory allocation, 54800 B, past the 48 kB a block gets for
147 * free. The kernel has to opt into that once before its first launch; see
148 * opgrad_dmma_tma_optin(), which is a predictable branch after that.
149 */
150#define CASE_DMMA_TMA(LX, C) \
151 (void) opgrad_dmma_tma_optin<real, LX, NEKO_DMMA_NW(C)>(); \
152 opgrad_kernel_dmma_tma<real, LX, NEKO_DMMA_NW(C)> \
153 <<<NEKO_DMMA_NBLCKS(*nel, LX), NEKO_DMMA_NTHRDS(C), \
154 NEKO_OPGRAD_TMA_SMEM, stream>>> \
155 ((real *) ux, (real *) uy, (real *) uz, (real *) u, \
156 (real *) dx, (real *) dy, (real *) dz, \
157 (real *) drdx, (real *) dsdx, (real *) dtdx, \
158 (real *) drdy, (real *) dsdy, (real *) dtdy, \
159 (real *) drdz, (real *) dsdz, (real *) dtdz, \
160 (real *) w3); \
161 CUDA_CHECK(cudaGetLastError());
162
163/* Runtime dispatch onto the tuned warps per block candidate */
164#define CASE_DMMA_TMA_SEL(LX, SEL) \
165 switch (SEL) { \
166 case 0: CASE_DMMA_TMA(LX, 0); break; \
167 case 1: CASE_DMMA_TMA(LX, 1); break; \
168 default: CASE_DMMA_TMA(LX, 2); break; \
169 }
170
171#define CASE(LX) \
172 case LX: \
173 if(autotune[LX] == 0 ) { \
174 autotune[LX]=tune_opgrad<LX>(ux, uy, uz, u, \
175 dx, dy, dz, \
176 drdx, dsdx, dtdx, \
177 drdy, dsdy, dtdy, \
178 drdz, dsdz, dtdz, \
179 w3, nel, lx, &autotune_eb[LX], \
180 &autotune_ch[LX], &autotune_nw[LX], \
181 &autotune_tw[LX]); \
182 } else if (autotune[LX] == 1 ) { \
183 CASE_1D_SEL(LX, autotune_ch[LX]); \
184 } else if (autotune[LX] == 2 ) { \
185 CASE_KSTEP_SEL(LX, autotune_eb[LX]); \
186 } else if (autotune[LX] == 3 ) { \
187 CASE_DMMA_SEL(LX, autotune_nw[LX]); \
188 } else if (autotune[LX] == 4 ) { \
189 CASE_DMMA_TMA_SEL(LX, autotune_tw[LX]); \
190 } \
191 break
192
193 switch(*lx) {
194 CASE(2);
195 CASE(3);
196 CASE(4);
197 CASE(5);
198 CASE(6);
199 CASE(7);
200 CASE(8);
201 CASE(9);
202 CASE(10);
203 CASE(11);
204 CASE(12);
205 CASE(13);
206 CASE(14);
207 CASE(15);
208 CASE(16);
209 default:
210 {
211 fprintf(stderr, __FILE__ ": size not supported: %d\n", *lx);
212 exit(1);
213 }
214 }
215 }
216}
217
218template < const int LX >
219int tune_opgrad(void *ux, void *uy, void *uz, void *u,
220 void *dx, void *dy, void *dz,
221 void *drdx, void *dsdx, void *dtdx,
222 void *drdy, void *dsdy, void *dtdy,
223 void *drdz, void *dsdz, void *dtdz,
224 void *w3, int *nel, int *lx, int *eb_sel, int *ch_sel,
225 int *nw_sel, int *tw_sel) {
228 int best1 = 0;
231 int best3 = 0;
233 int best4 = 0;
234 const int rounds = neko_tune_rounds();
235 const int iters = neko_tune_iters();
236 const int sweep = neko_eb_sweep();
237 const bool dmma = dmma_lx_supported<LX>() && cuda_have_dmma();
238 /* Whether the pointers are bulk copy aligned is a property of this call
239 rather than of the kernel, so it is checked rather than assumed, see
240 dmma_tma_kernel.h. The shared memory gate is a device query too: this
241 variant asks for more than a block gets by default */
242 const bool tma = dmma_tma_opgrad_lx_supported<LX>() &&
245 drdy, dsdy, dtdy,
246 drdz, dsdz, dtdz);
247 int best = 0;
248 int retval;
249
250 for (int c = 0; c < NEKO_EB_CANDIDATES; c++) {
252 }
253 for (int c = 0; c < NEKO_CHUNKS_CANDIDATES; c++) {
255 }
256 for (int c = 0; c < NEKO_DMMA_CANDIDATES; c++) {
259 }
260
261 const dim3 nthrds_1d(1024, 1, 1);
262 const dim3 nthrds_kstep((*lx), (*lx), 1);
263 const dim3 nblcks((*nel), 1, 1);
264 const cudaStream_t stream = (cudaStream_t) glb_cmd_queue;
265
266 char *env_value = NULL;
267 char neko_log_buf[80];
268
269 env_value=getenv("NEKO_AUTOTUNE");
270
271 sprintf(neko_log_buf, "Autotune opgrad (lx: %d)", *lx);
273
274 *eb_sel = 0;
275 *ch_sel = 0;
276 *nw_sel = 0;
277 *tw_sel = 0;
278
279 if(env_value) {
280 if( !strcmp(env_value,"1D") ) {
283 sprintf(neko_log_buf,"Set by env : 1 (1D, %d chunk)",
287 return 1;
288 } else if( !strcmp(env_value,"KSTEP") ) {
289 {
290 const int c = neko_eb_env();
291 *eb_sel = c;
292 CASE_KSTEP_SEL(LX, c);
293 sprintf(neko_log_buf,"Set by env : 2 (KSTEP, %d elem/block)",
294 NEKO_EB_SEL(LX, c));
295 }
298 return 2;
299 } else if( !strcmp(env_value,"DMMA") ) {
300 if (dmma) {
301 const int c = neko_dmma_env();
302 *nw_sel = c;
303 CASE_DMMA_SEL(LX, c);
304 sprintf(neko_log_buf,"Set by env : 3 (DMMA, %d warps)",
305 NEKO_DMMA_NW(c));
308 return 3;
309 } else {
310 sprintf(neko_log_buf, "DMMA strategy not available for this config");
312 }
313 } else if( !strcmp(env_value,"DMMA_TMA") ) {
314 if (tma) {
315 const int c = neko_dmma_tma_env();
316 *tw_sel = c;
318 sprintf(neko_log_buf,"Set by env : 4 (DMMA_TMA, %d warps)",
319 NEKO_DMMA_NW(c));
322 return 4;
323 } else {
325 "DMMA_TMA strategy not available for this config");
327 }
328 } else {
329 sprintf(neko_log_buf, "Invalid value set for NEKO_AUTOTUNE");
331 }
332 }
333
336
337 /* Warm every variant before timing anything: each specialisation has to be
338 resident and the clocks at steady state, or whichever is timed first is
339 measured on a colder part */
340 for (int i = 0; i < NEKO_TUNE_WARMUP; i++) {
341 CASE_1D(LX, 0);
342 CASE_1D(LX, 1);
343 CASE_1D(LX, 2);
344 CASE_1D(LX, 3);
345 CASE_KSTEP(LX, 0);
346 if (sweep) {
347 CASE_KSTEP(LX, 1);
348 CASE_KSTEP(LX, 2);
349 }
350 if (dmma) {
351 CASE_DMMA(LX, 0);
352 CASE_DMMA(LX, 1);
353 CASE_DMMA(LX, 2);
354 }
355 if (tma) {
356 CASE_DMMA_TMA(LX, 0);
357 CASE_DMMA_TMA(LX, 1);
358 CASE_DMMA_TMA(LX, 2);
359 }
360 }
361
362 /* Interleaved rounds, best time per variant */
363 for (int r = 0; r < rounds; r++) {
369 if (sweep) {
372 }
373 if (dmma) {
377 }
378 if (tma) {
382 }
383 }
384
388
393 *eb_sel = best;
394 *ch_sel = best1;
395 *nw_sel = best3;
396 *tw_sel = best4;
397
398 if (time1[best1] < time2[best]) {
399 retval = 1;
400 } else {
401 retval = 2;
402 }
403
404 /* The dmma variants join the comparison only where they exist, their
405 candidates are left at NEKO_TUNE_INIT otherwise */
406 float tbest = (retval == 1) ? time1[best1] : time2[best];
407
408 if (time3[best3] < tbest) {
409 retval = 3;
410 tbest = time3[best3];
411 }
412 if (time4[best4] < tbest) {
413 retval = 4;
414 }
415
416 /* Leave the chosen kernel's output in place: the tuner stands in for a real
417 evaluation and the variants do not sum in the same order */
418 if (retval == 1) {
420 } else if (retval == 2) {
422 } else if (retval == 3) {
424 } else {
426 }
427
428 if (retval == 1) {
429 sprintf(neko_log_buf, "Chose : 1 (1D, %d chunk)",
431 } else if (retval == 2) {
432 sprintf(neko_log_buf, "Chose : 2 (KSTEP, %d elem/block)",
434 } else if (retval == 3) {
435 sprintf(neko_log_buf, "Chose : 3 (DMMA, %d warps, %d elem/blk)",
437 } else {
438 sprintf(neko_log_buf, "Chose : 4 (DMMA_TMA, %d warps)",
440 }
443 return retval;
444}
__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__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dtdy
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dtdx
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dtdz
const int i
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dz
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dsdz
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dx
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ u
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dy
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ drdz
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ drdx
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dsdx
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dsdy
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ drdy
__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 NEKO_CHUNKS_CANDIDATES
Definition elem_block.h:125
#define NEKO_EB_CANDIDATES
Definition elem_block.h:63
#define NEKO_EB_SEL(LX, SEL)
Definition elem_block.h:108
#define NEKO_CHUNKS_SEL(LX, SEL)
Definition elem_block.h:147
#define NEKO_TUNE_TIME(T, LAUNCH, LX, C, ITERS)
static int neko_eb_env()
static int neko_tune_rounds()
#define NEKO_TUNE_LOG(LX, T1, T2)
#define NEKO_TUNE_INIT
#define NEKO_TUNE_BEST(T, BEST, N)
static int neko_tune_iters()
static int neko_chunks_env()
static int neko_eb_sweep()
#define NEKO_TUNE_WARMUP
__global__ void T *__restrict__ uy
__global__ void T *__restrict__ T *__restrict__ uz
#define NEKO_DMMA_CANDIDATES
static bool cuda_have_dmma()
static int neko_dmma_env()
#define NEKO_DMMA_PACK(LX)
#define NEKO_TUNE_LOG_DMMA(LX, T3)
#define NEKO_DMMA_NW(C)
#define NEKO_TUNE_LOG_DMMA_TMA(LX, T4)
static bool cuda_have_tma_opgrad()
static int neko_dmma_tma_env()
static bool dmma_tma_opgrad_aligned(const void *u, const void *drdx, const void *dsdx, const void *dtdx, const void *drdy, const void *dsdy, const void *dtdy, const void *drdz, const void *dsdz, const void *dtdz)
void log_error(char *msg)
void log_message(char *msg)
void log_end_section()
void log_section(char *msg)
#define CASE_DMMA(LX, C)
#define CASE_DMMA_SEL(LX, SEL)
#define CASE_KSTEP_SEL(LX, SEL)
#define CASE(LX)
#define CASE_1D_SEL(LX, SEL)
int tune_opgrad(void *ux, void *uy, void *uz, void *u, void *dx, void *dy, void *dz, void *drdx, void *dsdx, void *dtdx, void *drdy, void *dsdy, void *dtdy, void *drdz, void *dsdz, void *dtdz, void *w3, int *nel, int *lx, int *eb_sel, int *ch_sel, int *nw_sel, int *tw_sel)
void cuda_opgrad(void *ux, void *uy, void *uz, void *u, void *dx, void *dy, void *dz, void *drdx, void *dsdx, void *dtdx, void *drdy, void *dsdy, void *dtdy, void *drdz, void *dsdz, void *dtdz, void *w3, int *nel, int *lx)
Definition opr_opgrad.cu:61
#define CASE_KSTEP(LX, C)
#define CASE_DMMA_TMA_SEL(LX, SEL)
#define CASE_DMMA_TMA(LX, C)
#define CASE_1D(LX, C)