Neko 1.99.9
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
opr_cdtp.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 "cdtp_kernel.h"
42#include "elem_block_tune.h"
43
44extern "C" {
45 #include <common/neko_log.h>
46}
47
48template < const int >
49int tune_cdtp(void *dtx, void *x,
50 void *dr, void *ds, void *dt,
51 void *dxt, void *dyt, void *dzt,
52 void *w3, int *nel, int *lx, int *eb_sel, int *ch_sel,
53 int *nwf_sel);
54
55extern "C" {
56
60 void hip_cdtp(void *dtx, void *x,
61 void *dr, void *ds, void *dt,
62 void *dxt, void *dyt, void *dzt,
63 void *w3, int *nel, int *lx) {
64
65 static int autotune[17] = { 0 };
66 /* elements per block candidate chosen by the tuner */
67 static int autotune_eb[17] = { 0 };
68 /* chunk candidate chosen for the 1d variant */
69 static int autotune_ch[17] = { 0 };
70 /* wavefronts per block candidate chosen for the mfma variant */
71 static int autotune_nwf[17] = { 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 cdtp_kernel_1d<real, LX, NEKO_CHUNKS(LX, C)> ), \
80 nblcks, NEKO_CHUNKS_NTHRDS(LX, C), 0, \
81 (hipStream_t) glb_cmd_queue, \
82 (real *) dtx, (real *) x, \
83 (real *) dr, (real *) ds, (real *) dt, \
84 (real *) dxt, (real *) dyt, (real *) dzt, \
85 (real *) w3); \
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 cdtp_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 *) dtx, (real *) x, \
103 (real *) dr, (real *) ds, (real *) dt, \
104 (real *) dxt, (real *) dyt, (real *) dzt, \
105 (real *) w3, *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 cdtp_kernel_mfma<real, LX, NEKO_MFMA_NWF(C)> ), \
119 NEKO_MFMA_NBLCKS(*nel, LX, C), NEKO_MFMA_NTHRDS(C), 0, \
120 (hipStream_t) glb_cmd_queue, \
121 (real *) dtx, (real *) x, \
122 (real *) dr, (real *) ds, (real *) dt, \
123 (real *) dxt, (real *) dyt, (real *) dzt, \
124 (real *) w3, *nel); \
125 HIP_CHECK(hipGetLastError());
126
127/* Runtime dispatch onto the tuned wavefronts per block candidate */
128#define CASE_MFMA_SEL(LX, SEL) \
129 switch (SEL) { \
130 case 0: CASE_MFMA(LX, 0); break; \
131 case 1: CASE_MFMA(LX, 1); break; \
132 case 2: CASE_MFMA(LX, 2); break; \
133 case 3: CASE_MFMA(LX, 3); break; \
134 default: CASE_MFMA(LX, 4); break; \
135 }
136
137/* The switch above has to name every wavefront candidate, see the note in
138 ax_helm.hip: a candidate reaching the default arm launches one geometry's
139 grid at another geometry's kernel. */
140static_assert(NEKO_MFMA_NWF_CANDIDATES == 5,
141 "CASE_MFMA_SEL does not cover the candidate space");
142
143#define CASE(LX) \
144 case LX: \
145 if(autotune[LX] == 0 ) { \
146 autotune[LX]=tune_cdtp<LX>(dtx, x, \
147 dr, ds, dt, \
148 dxt, dyt, dzt, \
149 w3, nel, lx, &autotune_eb[LX], \
150 &autotune_ch[LX], \
151 &autotune_nwf[LX]); \
152 } else if (autotune[LX] == 1 ) { \
153 CASE_1D_SEL(LX, autotune_ch[LX]); \
154 } else if (autotune[LX] == 2 ) { \
155 CASE_KSTEP_SEL(LX, autotune_eb[LX]); \
156 } else if (autotune[LX] == 3 ) { \
157 CASE_MFMA_SEL(LX, autotune_nwf[LX]); \
158 } \
159 break
160
161#define CASE_LARGE(LX) \
162 case LX: \
163 CASE_KSTEP(LX, 0); \
164 break
165
166
167 if ((*lx) < 13) {
168 switch(*lx) {
169 CASE(2);
170 CASE(3);
171 CASE(4);
172 CASE(5);
173 CASE(6);
174 CASE(7);
175 CASE(8);
176 CASE(9);
177 CASE(10);
178 CASE(11);
179 CASE(12);
180 default:
181 {
182 fprintf(stderr, __FILE__ ": size not supported: %d\n", *lx);
183 exit(1);
184 }
185 }
186 }
187 else {
188 switch(*lx) {
189 CASE_LARGE(13);
190 CASE_LARGE(14);
191 CASE_LARGE(15);
192 CASE_LARGE(16);
193 default:
194 {
195 fprintf(stderr, __FILE__ ": size not supported: %d\n", *lx);
196 exit(1);
197 }
198 }
199 }
200 }
201}
202
203template < const int LX >
204int tune_cdtp(void *dtx, void *x,
205 void *dr, void *ds, void *dt,
206 void *dxt, void *dyt, void *dzt,
207 void *w3, int *nel, int *lx, int *eb_sel, int *ch_sel,
208 int *nwf_sel) {
211 int best1 = 0;
214 int best3 = 0;
215 const int rounds = neko_tune_rounds();
216 const int iters = neko_tune_iters();
217 /* Candidates of the kstep sweep, one -- the unblocked shape -- when the
218 elements per block sweep is off */
219 const int eb_cand = neko_eb_sweep() ? NEKO_EB_CANDIDATES : 1;
220 /* Geometry pinned by each formulation's own variable, -1 to sweep it */
221 const int ch_pin = neko_chunks_pin();
222 const int eb_pin = neko_eb_pin();
223 /* Wavefronts only: the tile dimension is Ax helm's, and these operators
224 stay on the default tile, see neko_mfma_nwf_pin() */
225 const int nwf_pin = neko_mfma_nwf_pin();
226 /* Formulation pinned by NEKO_AUTOTUNE, as the identifier this returns */
227 int strat = 0;
228 const bool mfma = mfma_lx_supported<LX>() && hip_have_mfma();
229 int best = 0;
230 int retval;
231
232 for (int c = 0; c < NEKO_EB_CANDIDATES; c++) {
234 }
235 for (int c = 0; c < NEKO_CHUNKS_CANDIDATES; c++) {
237 }
238 for (int c = 0; c < NEKO_MFMA_CANDIDATES; c++) {
240 }
241
242 const dim3 nthrds_1d(1024, 1, 1);
243 const dim3 nthrds_kstep((*lx), (*lx), 1);
244 const dim3 nblcks((*nel), 1, 1);
245 const hipStream_t stream = (hipStream_t) glb_cmd_queue;
246
247 char *env_value = NULL;
248 char neko_log_buf[80];
249
250 env_value=getenv("NEKO_AUTOTUNE");
251
252 sprintf(neko_log_buf, "Autotune cdtp (lx: %d)", *lx);
254
255 *eb_sel = 0;
256 *ch_sel = 0;
257 *nwf_sel = 0;
258
259 /*
260 * NEKO_AUTOTUNE names a formulation and nothing more: the sweep below is
261 * narrowed to that kernel family, but its geometry -- the chunk size, the
262 * elements per block, the wavefronts per block -- is still measured
263 * candidate against candidate
264 */
265 if(env_value) {
266 if( !strcmp(env_value,"1D") ) {
267 strat = 1;
268 } else if( !strcmp(env_value,"KSTEP") ) {
269 strat = 2;
270 } else if( !strcmp(env_value,"MFMA") ) {
271 if (mfma) {
272 strat = 3;
273 } else {
274 sprintf(neko_log_buf, "MFMA strategy not available for this config");
276 }
277 } else {
278 sprintf(neko_log_buf, "Invalid value set for NEKO_AUTOTUNE");
280 }
281 }
282
283 /* Geometry of the pinned formulation, if its own variable fixes that too.
284 Both pinned leaves nothing to measure, so the kernel is launched once and
285 reported, which is what pinning has always done */
286 const int pin = (strat == 1) ? ch_pin : (strat == 2) ? eb_pin :
287 (strat == 3) ? nwf_pin : -1;
288
289 if (pin >= 0) {
290 switch (strat) {
291 case 1:
292 *ch_sel = pin;
294 sprintf(neko_log_buf, "Set by env : 1 (1D, %d chunk)",
296 break;
297 case 2:
298 *eb_sel = pin;
300 sprintf(neko_log_buf, "Set by env : 2 (KSTEP, %d elem/block)",
301 NEKO_EB_SEL(LX, pin));
302 break;
303 default:
304 *nwf_sel = pin;
306 sprintf(neko_log_buf, "Set by env : 3 (MFMA, %d wf, %d elem/block)",
308 break;
309 }
312 return strat;
313 }
314
315 if (strat) {
316 sprintf(neko_log_buf, "Set by env : %d (%s)", strat, env_value);
318 }
319
320 /* Formulations the sweep considers, see NEKO_TUNE_FOR(). NEKO_MFMA_TUNE
321 keeps the matrix core variant out of an unpinned sweep, but naming it
322 explicitly still measures it */
323 const bool try_1d = (strat == 0 || strat == 1);
324 const bool try_kstep = (strat == 0 || strat == 2);
325 const bool try_mfma = mfma && ((strat == 3) ||
326 (strat == 0 && neko_mfma_sweep()));
327
330
331 /* Warm every variant before timing anything: each specialisation has to be
332 resident and the clocks at steady state, or whichever is timed first is
333 measured on a colder part */
334 for (int i = 0; i < NEKO_TUNE_WARMUP; i++) {
336 CASE_1D_SEL(LX, c);
337 }
339 CASE_KSTEP_SEL(LX, c);
340 }
342 CASE_MFMA_SEL(LX, c);
343 }
344 }
345
346 /* Interleaved rounds, best time per variant */
347 for (int r = 0; r < rounds; r++) {
350 }
353 }
356 }
357 }
358
361
365 *eb_sel = best;
366 *ch_sel = best1;
367 *nwf_sel = best3;
368
369 if (time1[best1] < time2[best]) {
370 retval = 1;
371 } else {
372 retval = 2;
373 }
374
375 /* The mfma variant joins the comparison only where it exists, its
376 candidates are left at NEKO_TUNE_INIT otherwise */
377 if (time3[best3] < ((retval == 1) ? time1[best1] : time2[best])) {
378 retval = 3;
379 }
380
381 /* Leave the chosen kernel's output in place: the tuner stands in for a real
382 evaluation and the variants do not sum in the same order */
383 if (retval == 1) {
385 } else if (retval == 2) {
387 } else {
389 }
390
391 if (retval == 1) {
392 sprintf(neko_log_buf, "Chose : 1 (1D, %d chunk)",
394 } else if (retval == 2) {
395 sprintf(neko_log_buf, "Chose : 2 (KSTEP, %d elem/block)",
397 } else {
398 sprintf(neko_log_buf, "Chose : 3 (MFMA, %d wf, %d elem/block)",
400 }
403 return retval;
404}
__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__ const T *__restrict__ const T *__restrict__ dyt
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ ds
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dzt
__global__ void const T *__restrict__ x
__global__ void const T *__restrict__ const T *__restrict__ dr
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dt
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dxt
__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
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
#define HIP_CHECK(err)
Definition check.h:8
#define NEKO_TUNE_LOG_MFMA(LX, T3)
#define NEKO_MFMA_NWF_CANDIDATES
#define NEKO_MFMA_CANDIDATES
static bool hip_have_mfma()
static int neko_mfma_nwf_pin()
#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)
int tune_cdtp(void *dtx, void *x, void *dr, void *ds, void *dt, void *dxt, void *dyt, void *dzt, void *w3, int *nel, int *lx, int *eb_sel, int *ch_sel, int *nwf_sel)
Definition opr_cdtp.hip:204
#define CASE_KSTEP_SEL(LX, SEL)
#define CASE(LX)
#define CASE_1D_SEL(LX, SEL)
void hip_cdtp(void *dtx, void *x, void *dr, void *ds, void *dt, void *dxt, void *dyt, void *dzt, void *w3, int *nel, int *lx)
Definition opr_cdtp.hip:60
#define CASE_MFMA_SEL(LX, SEL)
#define CASE_LARGE(LX)