Neko 1.99.9
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
opr_dudxyz.hip
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 <hip/hip_runtime.h>
40#include <device/hip/check.h>
41#include "dudxyz_kernel.h"
42#include "elem_block_tune.h"
43
44extern "C" {
45 #include <common/neko_log.h>
46}
47
48template <const int >
49int tune_dudxyz(void *du, void *u,
50 void *dr, void *ds, void *dt,
51 void *dx, void *dy, void *dz,
52 void *jacinv, int *nel, int *lx, int *eb_sel, int *ch_sel,
53 int *nwf_sel);
54
55extern "C" {
56
60 void hip_dudxyz(void *du, void *u,
61 void *dr, void *ds, void *dt,
62 void *dx, void *dy, void *dz,
63 void *jacinv, int *nel, int *lx) {
64
65 static int autotune[16] = { 0 };
66 /* elements per block candidate chosen by the tuner */
67 static int autotune_eb[16] = { 0 };
68 /* chunk candidate chosen for the 1d variant */
69 static int autotune_ch[16] = { 0 };
70 /* wavefronts per block candidate chosen for the mfma variant */
71 static int autotune_nwf[16] = { 0 };
72
73 const dim3 nthrds_1d(1024, 1, 1);
74 const dim3 nthrds_kstep((*lx), (*lx), 1);
75 const dim3 nblcks((*nel), 1, 1);
76
77#define CASE_1D(LX, C) \
78 hipLaunchKernelGGL( HIP_KERNEL_NAME( \
79 dudxyz_kernel_1d<real, LX, NEKO_CHUNKS(LX, C)> ), \
80 nblcks, NEKO_CHUNKS_NTHRDS(LX, C), 0, \
81 (hipStream_t) glb_cmd_queue, \
82 (real *) du, (real *) u, \
83 (real *) dr, (real *) ds, (real *) dt, \
84 (real *) dx, (real *) dy, (real *) dz, \
85 (real *) jacinv); \
86 HIP_CHECK(hipGetLastError());
87
88/* Runtime dispatch onto the tuned chunk candidate */
89#define CASE_1D_SEL(LX, SEL) \
90 switch (SEL) { \
91 case 0: CASE_1D(LX, 0); break; \
92 case 1: CASE_1D(LX, 1); break; \
93 case 2: CASE_1D(LX, 2); break; \
94 default: CASE_1D(LX, 3); break; \
95 }
96
97#define CASE_KSTEP(LX, C) \
98 hipLaunchKernelGGL( HIP_KERNEL_NAME( \
99 dudxyz_kernel_kstep<real, LX, NEKO_EB(LX, C)> ), \
100 NEKO_EB_NBLCKS(*nel, LX, C), NEKO_EB_NTHRDS(LX, C), 0, \
101 (hipStream_t) glb_cmd_queue, \
102 (real *) du, (real *) u, \
103 (real *) dr, (real *) ds, (real *) dt, \
104 (real *) dx, (real *) dy, (real *) dz, \
105 (real *) jacinv, *nel); \
106 HIP_CHECK(hipGetLastError());
107
108/* Runtime dispatch onto the tuned candidate */
109#define CASE_KSTEP_SEL(LX, SEL) \
110 switch (SEL) { \
111 case 0: CASE_KSTEP(LX, 0); break; \
112 case 1: CASE_KSTEP(LX, 1); break; \
113 default: CASE_KSTEP(LX, 2); break; \
114 }
115
116#define CASE_MFMA(LX, C) \
117 hipLaunchKernelGGL( HIP_KERNEL_NAME( \
118 dudxyz_kernel_mfma<real, LX, NEKO_MFMA_NWF(C), \
119 NEKO_MFMA_TILE(C)> ), \
120 NEKO_MFMA_NBLCKS(*nel, LX, C), NEKO_MFMA_NTHRDS(C), 0, \
121 (hipStream_t) glb_cmd_queue, \
122 (real *) du, (real *) u, \
123 (real *) dr, (real *) ds, (real *) dt, \
124 (real *) dx, (real *) dy, (real *) dz, \
125 (real *) jacinv, *nel); \
126 HIP_CHECK(hipGetLastError());
127
128/* Runtime dispatch onto the tuned candidate, which names both the wavefronts
129 per block and the matrix core tile, see NEKO_MFMA_TILE in mfma_kernel.h */
130#define CASE_MFMA_SEL(LX, SEL) \
131 switch (SEL) { \
132 case 0: CASE_MFMA(LX, 0); break; \
133 case 1: CASE_MFMA(LX, 1); break; \
134 case 2: CASE_MFMA(LX, 2); break; \
135 case 3: CASE_MFMA(LX, 3); break; \
136 case 4: CASE_MFMA(LX, 4); break; \
137 case 5: CASE_MFMA(LX, 5); break; \
138 case 6: CASE_MFMA(LX, 6); break; \
139 case 7: CASE_MFMA(LX, 7); break; \
140 case 8: CASE_MFMA(LX, 8); break; \
141 default: CASE_MFMA(LX, 9); break; \
142 }
143
144/* The switch above has to name every candidate, see the note in ax_helm.hip:
145 a candidate reaching the default arm launches one geometry's grid at
146 another geometry's kernel. */
147static_assert(NEKO_MFMA_CANDIDATES == 10,
148 "CASE_MFMA_SEL does not cover the candidate space");
149
150 #define CASE(LX) \
151 case LX: \
152 if(autotune[LX] == 0 ) { \
153 autotune[LX]=tune_dudxyz<LX>(du, u, \
154 dr, ds, dt, \
155 dx, dy, dz, \
156 jacinv, nel, lx, &autotune_eb[LX], \
157 &autotune_ch[LX], \
158 &autotune_nwf[LX]); \
159 } else if (autotune[LX] == 1 ) { \
160 CASE_1D_SEL(LX, autotune_ch[LX]); \
161 } else if (autotune[LX] == 2 ) { \
162 CASE_KSTEP_SEL(LX, autotune_eb[LX]); \
163 } else if (autotune[LX] == 3 ) { \
164 CASE_MFMA_SEL(LX, autotune_nwf[LX]); \
165 } \
166 break
167
168#define CASE_LARGE(LX) \
169 case LX: \
170 CASE_KSTEP(LX, 0); \
171 break
172
173
174 if ((*lx) < 11) {
175 switch(*lx) {
176 CASE(2);
177 CASE(3);
178 CASE(4);
179 CASE(5);
180 CASE(6);
181 CASE(7);
182 CASE(8);
183 CASE(9);
184 CASE(10);
185 default:
186 {
187 fprintf(stderr, __FILE__ ": size not supported: %d\n", *lx);
188 exit(1);
189 }
190 }
191 }
192 else {
193 switch(*lx) {
194 CASE_LARGE(11);
195 CASE_LARGE(12);
196 CASE_LARGE(13);
197 CASE_LARGE(14);
198 CASE_LARGE(15);
199 CASE_LARGE(16);
200 default:
201 {
202 fprintf(stderr, __FILE__ ": size not supported: %d\n", *lx);
203 exit(1);
204 }
205 }
206 }
207 }
208}
209
210template < const int LX >
211int tune_dudxyz(void *du, void *u,
212 void *dr, void *ds, void *dt,
213 void *dx, void *dy, void *dz,
214 void *jacinv, int *nel, int *lx, int *eb_sel, int *ch_sel,
215 int *nwf_sel) {
218 int best1 = 0;
221 int best3 = 0;
222 const int rounds = neko_tune_rounds();
223 const int iters = neko_tune_iters();
224 /* Candidates of the kstep sweep, one -- the unblocked shape -- when the
225 elements per block sweep is off */
226 const int eb_cand = neko_eb_sweep() ? NEKO_EB_CANDIDATES : 1;
227 /* Geometry pinned by each formulation's own variable, -1 to sweep it */
228 const int ch_pin = neko_chunks_pin();
229 const int eb_pin = neko_eb_pin();
230 /* Candidates of the matrix core sweep, see neko_mfma_candidates() */
231 const int mfma_cand = neko_mfma_candidates();
232 /* Both matrix core dimensions, pinned by their own variables or -1 */
233 const int nwf_pin = neko_mfma_pin();
234 /* Formulation pinned by NEKO_AUTOTUNE, as the identifier this returns */
235 int strat = 0;
236 /* Hardware capability, which the explicit NEKO_AUTOTUNE=MFMA pin needs */
237 const bool mfma = mfma_lx_supported<LX>() && hip_have_mfma();
238 int best = 0;
239 int retval;
240
241 for (int c = 0; c < NEKO_EB_CANDIDATES; c++) {
243 }
244 for (int c = 0; c < NEKO_CHUNKS_CANDIDATES; c++) {
246 }
247 for (int c = 0; c < NEKO_MFMA_CANDIDATES; c++) {
249 }
250
251 const dim3 nthrds_1d(1024, 1, 1);
252 const dim3 nthrds_kstep((*lx), (*lx), 1);
253 const dim3 nblcks((*nel), 1, 1);
254 const hipStream_t stream = (hipStream_t) glb_cmd_queue;
255
256 char *env_value = NULL;
257 char neko_log_buf[80];
258
259 env_value=getenv("NEKO_AUTOTUNE");
260
261 sprintf(neko_log_buf, "Autotune dudxyz (lx: %d)", *lx);
263
264 *eb_sel = 0;
265 *ch_sel = 0;
266 *nwf_sel = 0;
267
268 /*
269 * NEKO_AUTOTUNE names a formulation, and that is all it does: the sweep
270 * below is narrowed to that one kernel family, but its geometry -- the
271 * chunk size, the elements per block, the wavefronts per block -- is still
272 * measured candidate against candidate. A formulation this build or this
273 * device does not have is reported and ignored, leaving the full sweep.
274 */
275 if(env_value) {
276 if( !strcmp(env_value,"1D") ) {
277 strat = 1;
278 } else if( !strcmp(env_value,"KSTEP") ) {
279 strat = 2;
280 } else if( !strcmp(env_value,"MFMA") ) {
281 if (mfma) {
282 strat = 3;
283 } else {
284 sprintf(neko_log_buf, "MFMA strategy not available for this config");
286 }
287 } else {
288 sprintf(neko_log_buf, "Invalid value set for NEKO_AUTOTUNE");
290 }
291 }
292
293 /* Geometry of the pinned formulation, if its own variable fixes that too.
294 Both pinned leaves nothing to measure, so the kernel is launched once and
295 reported, which is what pinning has always done */
296 const int pin = (strat == 1) ? ch_pin : (strat == 2) ? eb_pin :
297 (strat == 3) ? nwf_pin : -1;
298
299 if (pin >= 0) {
300 switch (strat) {
301 case 1:
302 *ch_sel = pin;
304 sprintf(neko_log_buf, "Set by env : 1 (1D, %d chunk)",
306 break;
307 case 2:
308 *eb_sel = pin;
310 sprintf(neko_log_buf, "Set by env : 2 (KSTEP, %d elem/block)",
311 NEKO_EB_SEL(LX, pin));
312 break;
313 default:
314 *nwf_sel = pin;
316 sprintf(neko_log_buf, "Set by env : 3 (MFMA %s, %d wf, %d elem/blk)",
318 break;
319 }
322 return strat;
323 }
324
325 if (strat) {
326 sprintf(neko_log_buf, "Set by env : %d (%s)", strat, env_value);
328 }
329
330 /* Formulations the sweep considers, see NEKO_TUNE_FOR(). NEKO_MFMA_TUNE
331 keeps the matrix core variant out of an unpinned sweep, but naming it
332 explicitly still measures it */
333 const bool try_1d = (strat == 0 || strat == 1);
334 const bool try_kstep = (strat == 0 || strat == 2);
335 const bool try_mfma = mfma && ((strat == 3) ||
336 (strat == 0 && neko_mfma_sweep()));
337
340
341 /* Verify every candidate against the reference formulation on a synthetic
342 input before anything is timed, see neko_tune_verify_begin() in
343 elem_block_tune.h. A candidate that does not reproduce it is reported
344 and dropped from the comparison below. */
345 neko_tune_rej_t rej1 = {{0}}, rej2 = {{0}}, rej3 = {{0}};
346 {
348 real *vouts[] = { (real *) du };
349 void **vins[] = { &u };
350 if (neko_tune_verify_begin(&vf, vouts, 1, vins, 1,
351 (*nel) * LX * LX * LX, stream)) {
352 sprintf(neko_log_buf, "Verify : rel tol %.1e, synthetic input",
353 vf.tol);
355 CASE_1D_SEL(LX, 0);
356 neko_tune_verify_ref(&vf, stream);
359 }
362 }
365 }
366 neko_tune_verify_end(&vf, stream);
367 } else {
368 sprintf(neko_log_buf, "Verify : %s", neko_tune_verify_on() ?
369 "skipped, no memory for it" : "off (NEKO_TUNE_VERIFY=0)");
371 }
372 }
373
374 /* Warm every variant before timing anything: each specialisation has to be
375 resident and the clocks at steady state, or whichever is timed first is
376 measured on a colder part */
377 for (int i = 0; i < NEKO_TUNE_WARMUP; i++) {
379 CASE_1D_SEL(LX, c);
380 }
382 CASE_KSTEP_SEL(LX, c);
383 }
385 CASE_MFMA_SEL(LX, c);
386 }
387 }
388
389 /* Interleaved rounds, best time per variant */
390 for (int r = 0; r < rounds; r++) {
393 }
396 }
399 }
400 }
401
402 /* Candidates the verification pass rejected drop out here, after the
403 timing, so the sweep's own bookkeeping is untouched */
407
410
414 *eb_sel = best;
415 *ch_sel = best1;
416 *nwf_sel = best3;
417
418 if (time1[best1] < time2[best]) {
419 retval = 1;
420 } else {
421 retval = 2;
422 }
423
424 /* The mfma variant joins the comparison only where it exists, its
425 candidates are left at NEKO_TUNE_INIT otherwise */
426 if (time3[best3] < ((retval == 1) ? time1[best1] : time2[best])) {
427 retval = 3;
428 }
429
430 /* Leave the chosen kernel's output in place: the tuner stands in for a real
431 evaluation and the variants do not sum in the same order */
432 if (retval == 1) {
434 } else if (retval == 2) {
436 } else {
438 }
439
440 if (retval == 1) {
441 sprintf(neko_log_buf, "Chose : 1 (1D, %d chunk)",
443 } else if (retval == 2) {
444 sprintf(neko_log_buf, "Chose : 2 (KSTEP, %d elem/block)",
446 } else {
447 sprintf(neko_log_buf, "Chose : 3 (MFMA %s, %d wf, %d elem/blk)",
450 }
453 return retval;
454}
__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
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dz
__global__ void const T *__restrict__ const T *__restrict__ dx
__global__ void const T *__restrict__ u
__global__ void 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__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ jacinv
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ ds
__global__ void const T *__restrict__ const T *__restrict__ dr
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dt
#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
static int neko_chunks_pin()
#define NEKO_TUNE_TIME(T, LAUNCH, LX, C, ITERS)
static int neko_tune_rounds()
static int neko_eb_pin()
#define NEKO_TUNE_LOG(LX, T1, T2)
#define NEKO_TUNE_INIT
#define NEKO_TUNE_BEST(T, BEST, N)
static int neko_tune_iters()
#define NEKO_TUNE_FOR(C, ON, PIN, N)
static int neko_eb_sweep()
#define NEKO_TUNE_WARMUP
double real
#define HIP_CHECK(err)
Definition check.h:8
static void neko_tune_verify_ref(neko_tune_verify_t *v, hipStream_t stream)
#define NEKO_TUNE_VERIFY_CAND(VF, REJ, LAUNCH, LX, C, NAME)
static void neko_tune_verify_end(neko_tune_verify_t *v, hipStream_t stream)
static int neko_tune_verify_on()
static int neko_tune_verify_begin(neko_tune_verify_t *v, real **outs, const int nout, void ***ins, const int nin, const int n, hipStream_t stream)
#define NEKO_TUNE_APPLY_REJ(T, REJ, N)
#define NEKO_TUNE_LOG_MFMA(LX, T3)
static const char * mfma_tile_name(const int c)
#define NEKO_MFMA_CANDIDATES
static int neko_mfma_pin()
static int neko_mfma_candidates()
static bool hip_have_mfma()
#define NEKO_MFMA_NWF(C)
#define NEKO_MFMA_EB(LX, C)
static int neko_mfma_sweep()
void log_error(char *msg)
void log_message(char *msg)
void log_end_section()
void log_section(char *msg)
#define CASE_KSTEP_SEL(LX, SEL)
#define CASE(LX)
#define CASE_1D_SEL(LX, SEL)
#define CASE_MFMA_SEL(LX, SEL)
#define CASE_LARGE(LX)
int tune_dudxyz(void *du, void *u, void *dr, void *ds, void *dt, void *dx, void *dy, void *dz, void *jacinv, int *nel, int *lx, int *eb_sel, int *ch_sel, int *nwf_sel)
void hip_dudxyz(void *du, void *u, void *dr, void *ds, void *dt, void *dx, void *dy, void *dz, void *jacinv, int *nel, int *lx)