Neko 1.99.9
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
opr_conv1.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 "conv1_kernel.h"
42#include "elem_block_tune.h"
43
44extern "C" {
45 #include <common/neko_log.h>
46}
47
48template < const int >
49int tune_conv1(void *du, void *u,
50 void *vx, void *vy, void *vz,
51 void *dx, void *dy, void *dz,
52 void *drdx, void *dsdx, void *dtdx,
53 void *drdy, void *dsdy, void *dtdy,
54 void *drdz, void *dsdz, void *dtdz,
55 void *jacinv, int *nel, int *gdim, int *lx, int *eb_sel,
56 int *ch_sel, int *nwf_sel);
57
58extern "C" {
59
63 void hip_conv1(void *du, void *u,
64 void *vx, void *vy, void *vz,
65 void *dx, void *dy, void *dz,
66 void *drdx, void *dsdx, void *dtdx,
67 void *drdy, void *dsdy, void *dtdy,
68 void *drdz, void *dsdz, void *dtdz,
69 void *jacinv, int *nel, int *gdim, int *lx) {
70
71 static int autotune[17] = { 0 };
72 /* elements per block candidate chosen by the tuner */
73 static int autotune_eb[17] = { 0 };
74 /* chunk candidate chosen for the 1d variant */
75 static int autotune_ch[17] = { 0 };
76 /* wavefronts per block candidate chosen for the mfma variant */
77 static int autotune_nwf[17] = { 0 };
78
79 const dim3 nthrds_1d(1024, 1, 1);
80 const dim3 nthrds_kstep((*lx), (*lx), 1);
81 const dim3 nblcks((*nel), 1, 1);
82
83#define CASE_1D(LX, C) \
84 hipLaunchKernelGGL( HIP_KERNEL_NAME( \
85 conv1_kernel_1d<real, LX, NEKO_CHUNKS(LX, C)> ), \
86 nblcks, NEKO_CHUNKS_NTHRDS(LX, C), 0, \
87 (hipStream_t) glb_cmd_queue, \
88 (real *) du, (real *) u, \
89 (real *) vx, (real *) vy, (real *) vz, \
90 (real *) dx, (real *) dy, (real *) dz, \
91 (real *) drdx, (real *) dsdx, (real *) dtdx, \
92 (real *) drdy, (real *) dsdy, (real *) dtdy, \
93 (real *) drdz, (real *) dsdz, (real *) dtdz, \
94 (real *) jacinv); \
95 HIP_CHECK(hipGetLastError());
96
97/* Runtime dispatch onto the tuned chunk candidate */
98#define CASE_1D_SEL(LX, SEL) \
99 switch (SEL) { \
100 case 0: CASE_1D(LX, 0); break; \
101 case 1: CASE_1D(LX, 1); break; \
102 case 2: CASE_1D(LX, 2); break; \
103 default: CASE_1D(LX, 3); break; \
104 }
105
106#define CASE_KSTEP(LX, C) \
107 hipLaunchKernelGGL( HIP_KERNEL_NAME( \
108 conv1_kernel_kstep<real, LX, NEKO_EB(LX, C)> ), \
109 NEKO_EB_NBLCKS(*nel, LX, C), NEKO_EB_NTHRDS(LX, C), 0, \
110 (hipStream_t) glb_cmd_queue, \
111 (real *) du, (real *) u, \
112 (real *) vx, (real *) vy, (real *) vz, \
113 (real *) dx, (real *) dy, (real *) dz, \
114 (real *) drdx, (real *) dsdx, (real *) dtdx, \
115 (real *) drdy, (real *) dsdy, (real *) dtdy, \
116 (real *) drdz, (real *) dsdz, (real *) dtdz, \
117 (real *) jacinv, *nel); \
118 HIP_CHECK(hipGetLastError());
119
120/* Runtime dispatch onto the tuned candidate */
121#define CASE_KSTEP_SEL(LX, SEL) \
122 switch (SEL) { \
123 case 0: CASE_KSTEP(LX, 0); break; \
124 case 1: CASE_KSTEP(LX, 1); break; \
125 default: CASE_KSTEP(LX, 2); break; \
126 }
127
128#define CASE_MFMA(LX, C) \
129 hipLaunchKernelGGL( HIP_KERNEL_NAME( \
130 conv1_kernel_mfma<real, LX, NEKO_MFMA_NWF(C)> ), \
131 NEKO_MFMA_NBLCKS(*nel, LX, C), NEKO_MFMA_NTHRDS(C), 0, \
132 (hipStream_t) glb_cmd_queue, \
133 (real *) du, (real *) u, \
134 (real *) vx, (real *) vy, (real *) vz, \
135 (real *) dx, (real *) dy, (real *) dz, \
136 (real *) drdx, (real *) dsdx, (real *) dtdx, \
137 (real *) drdy, (real *) dsdy, (real *) dtdy, \
138 (real *) drdz, (real *) dsdz, (real *) dtdz, \
139 (real *) jacinv, *nel); \
140 HIP_CHECK(hipGetLastError());
141
142/* Runtime dispatch onto the tuned wavefronts per block candidate */
143#define CASE_MFMA_SEL(LX, SEL) \
144 switch (SEL) { \
145 case 0: CASE_MFMA(LX, 0); break; \
146 case 1: CASE_MFMA(LX, 1); break; \
147 case 2: CASE_MFMA(LX, 2); break; \
148 default: CASE_MFMA(LX, 3); break; \
149 }
150
151#define CASE(LX) \
152 case LX: \
153 if(autotune[LX] == 0 ) { \
154 autotune[LX]=tune_conv1<LX>(du, u, \
155 vx, vy, vz, \
156 dx, dy, dz, \
157 drdx, dsdx, dtdx, \
158 drdy, dsdy, dtdy, \
159 drdz, dsdz, dtdz, \
160 jacinv, nel, gdim, lx, &autotune_eb[LX], \
161 &autotune_ch[LX], \
162 &autotune_nwf[LX]); \
163 } else if (autotune[LX] == 1 ) { \
164 CASE_1D_SEL(LX, autotune_ch[LX]); \
165 } else if (autotune[LX] == 2 ) { \
166 CASE_KSTEP_SEL(LX, autotune_eb[LX]); \
167 } else if (autotune[LX] == 3 ) { \
168 CASE_MFMA_SEL(LX, autotune_nwf[LX]); \
169 } \
170 break
171
172#define CASE_LARGE(LX) \
173 case LX: \
174 CASE_KSTEP(LX, 0); \
175 break
176
177
178 if ((*lx) < 11) {
179 switch(*lx) {
180 CASE(2);
181 CASE(3);
182 CASE(4);
183 CASE(5);
184 CASE(6);
185 CASE(7);
186 CASE(8);
187 CASE(9);
188 CASE(10);
189 default:
190 {
191 fprintf(stderr, __FILE__ ": size not supported: %d\n", *lx);
192 exit(1);
193 }
194 }
195 }
196 else {
197 switch(*lx) {
198 CASE_LARGE(11);
199 CASE_LARGE(12);
200 CASE_LARGE(13);
201 CASE_LARGE(14);
202 CASE_LARGE(15);
203 CASE_LARGE(16);
204 default:
205 {
206 fprintf(stderr, __FILE__ ": size not supported: %d\n", *lx);
207 exit(1);
208 }
209 }
210 }
211 }
212}
213
214template < const int LX >
215int tune_conv1(void *du, void *u,
216 void *vx, void *vy, void *vz,
217 void *dx, void *dy, void *dz,
218 void *drdx, void *dsdx, void *dtdx,
219 void *drdy, void *dsdy, void *dtdy,
220 void *drdz, void *dsdz, void *dtdz,
221 void *jacinv, int *nel, int *gdim, int *lx, int *eb_sel,
222 int *ch_sel, int *nwf_sel) {
225 int best1 = 0;
228 int best3 = 0;
229 const int rounds = neko_tune_rounds();
230 const int iters = neko_tune_iters();
231 const int sweep = neko_eb_sweep();
232 const bool mfma = mfma_lx_supported<LX>() && hip_have_mfma();
233 /* Whether the sweep may pick it on its own, see neko_mfma_sweep() */
234 const bool mfma_tune = mfma && neko_mfma_sweep();
235 int best = 0;
236 int retval;
237
238 for (int c = 0; c < NEKO_EB_CANDIDATES; c++) {
240 }
241 for (int c = 0; c < NEKO_CHUNKS_CANDIDATES; c++) {
243 }
244 for (int c = 0; c < NEKO_MFMA_CANDIDATES; c++) {
246 }
247
248 const dim3 nthrds_1d(1024, 1, 1);
249 const dim3 nthrds_kstep((*lx), (*lx), 1);
250 const dim3 nblcks((*nel), 1, 1);
251 const hipStream_t stream = (hipStream_t) glb_cmd_queue;
252
253 char *env_value = NULL;
254 char neko_log_buf[80];
255
256 env_value=getenv("NEKO_AUTOTUNE");
257
258 sprintf(neko_log_buf, "Autotune conv1 (lx: %d)", *lx);
260
261 *eb_sel = 0;
262 *ch_sel = 0;
263 *nwf_sel = 0;
264
265 if(env_value) {
266 if( !strcmp(env_value,"1D") ) {
269 sprintf(neko_log_buf,"Set by env : 1 (1D, %d chunk)",
273 return 1;
274 } else if( !strcmp(env_value,"KSTEP") ) {
275 *eb_sel = neko_eb_env();
277 sprintf(neko_log_buf,"Set by env : 2 (KSTEP, %d elem/block)",
281 return 2;
282 } else if( !strcmp(env_value,"MFMA") ) {
283 if (mfma) {
284 const int c = neko_mfma_env();
285 *nwf_sel = c;
286 CASE_MFMA_SEL(LX, c);
287 sprintf(neko_log_buf,"Set by env : 3 (MFMA, %d wf, %d elem/block)",
291 return 3;
292 } else {
293 sprintf(neko_log_buf, "MFMA strategy not available for this config");
295 }
296 } else {
297 sprintf(neko_log_buf, "Invalid value set for NEKO_AUTOTUNE");
299 }
300 }
301
304 /* Warm every variant before timing anything: each specialisation has to be
305 resident and the clocks at steady state, or whichever is timed first is
306 measured on a colder part */
307 for (int i = 0; i < NEKO_TUNE_WARMUP; i++) {
308 CASE_1D(LX, 0);
309 CASE_1D(LX, 1);
310 CASE_1D(LX, 2);
311 CASE_1D(LX, 3);
312 CASE_KSTEP(LX, 0);
313 if (sweep) {
314 CASE_KSTEP(LX, 1);
315 CASE_KSTEP(LX, 2);
316 }
317 if (mfma_tune) {
318 CASE_MFMA(LX, 0);
319 CASE_MFMA(LX, 1);
320 CASE_MFMA(LX, 2);
321 CASE_MFMA(LX, 3);
322 }
323 }
324
325 /* Interleaved rounds, best time per variant */
326 for (int r = 0; r < rounds; r++) {
332 if (sweep) {
335 }
336 if (mfma_tune) {
341 }
342 }
343
346
350 *eb_sel = best;
351 *ch_sel = best1;
352 *nwf_sel = best3;
353
354 if (time1[best1] < time2[best]) {
355 retval = 1;
356 } else {
357 retval = 2;
358 }
359
360 /* The mfma variant joins the comparison only where it exists, its
361 candidates are left at NEKO_TUNE_INIT otherwise */
362 if (time3[best3] < ((retval == 1) ? time1[best1] : time2[best])) {
363 retval = 3;
364 }
365
366 /* Leave the chosen kernel's output in place: the tuner stands in for a real
367 evaluation and the variants do not sum in the same order */
368 if (retval == 1) {
370 } else if (retval == 2) {
372 } else {
374 }
375
376 if (retval == 1) {
377 sprintf(neko_log_buf, "Chose : 1 (1D, %d chunk)",
379 } else if (retval == 2) {
380 sprintf(neko_log_buf, "Chose : 2 (KSTEP, %d elem/block)",
382 } else {
383 sprintf(neko_log_buf, "Chose : 3 (MFMA, %d wf, %d elem/block)",
385 }
388 return retval;
389}
__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
__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
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__ vz
__global__ void const T *__restrict__ const T *__restrict__ vx
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ vy
#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
#define HIP_CHECK(err)
Definition check.h:8
#define NEKO_TUNE_LOG_MFMA(LX, T3)
#define NEKO_MFMA_CANDIDATES
static bool hip_have_mfma()
static int neko_mfma_env()
#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)
int tune_conv1(void *du, void *u, void *vx, void *vy, void *vz, 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 *jacinv, int *nel, int *gdim, int *lx, int *eb_sel, int *ch_sel, int *nwf_sel)
#define CASE_1D_SEL(LX, SEL)
void hip_conv1(void *du, void *u, void *vx, void *vy, void *vz, 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 *jacinv, int *nel, int *gdim, int *lx)
Definition opr_conv1.hip:63
#define CASE_MFMA_SEL(LX, SEL)
#define CASE_KSTEP(LX, C)
#define CASE_LARGE(LX)
#define CASE_MFMA(LX, C)
#define CASE_1D(LX, C)