Neko 1.99.9
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
opr_opgrad.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 "opgrad_kernel.h"
42#include "elem_block_tune.h"
43
44extern "C" {
45 #include <common/neko_log.h>
46}
47
48template < const int >
49int tune_opgrad(void *ux, void *uy, void *uz, void *u,
50 void *dx, void *dy, void *dz,
51 void *drdx, void *dsdx, void *dtdx,
52 void *drdy, void *dsdy, void *dtdy,
53 void *drdz, void *dsdz, void *dtdz,
54 void *w3, int *nel, int *lx, int *eb_sel, int *ch_sel,
55 int *nwf_sel);
56
57extern "C" {
58
62 void hip_opgrad(void *ux, void *uy, void *uz, void *u,
63 void *dx, void *dy, void *dz,
64 void *drdx, void *dsdx, void *dtdx,
65 void *drdy, void *dsdy, void *dtdy,
66 void *drdz, void *dsdz, void *dtdz,
67 void *w3, int *nel, int *lx) {
68
69 static int autotune[19] = { 0 };
70 /* elements per block candidate chosen by the tuner */
71 static int autotune_eb[19] = { 0 };
72 /* chunk candidate chosen for the 1d variant */
73 static int autotune_ch[19] = { 0 };
74 /* wavefronts per block candidate chosen for the mfma variant */
75 static int autotune_nwf[19] = { 0 };
76
77 const dim3 nthrds_1d(1024, 1, 1);
78 const dim3 nthrds_kstep((*lx), (*lx), 1);
79 const dim3 nblcks((*nel), 1, 1);
80
81#define CASE_1D(LX, C) \
82 hipLaunchKernelGGL( HIP_KERNEL_NAME( \
83 opgrad_kernel_1d<real, LX, NEKO_CHUNKS(LX, C)> ), \
84 nblcks, NEKO_CHUNKS_NTHRDS(LX, C), 0, \
85 (hipStream_t) glb_cmd_queue, \
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 HIP_CHECK(hipGetLastError());
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 hipLaunchKernelGGL( HIP_KERNEL_NAME( \
106 opgrad_kernel_kstep<real, LX, NEKO_EB(LX, C)> ), \
107 NEKO_EB_NBLCKS(*nel, LX, C), NEKO_EB_NTHRDS(LX, C), 0, \
108 (hipStream_t) glb_cmd_queue, \
109 (real *) ux, (real *) uy, (real *) uz, (real *) u, \
110 (real *) dx, (real *) dy, (real *) dz, \
111 (real *) drdx, (real *) dsdx, (real *) dtdx, \
112 (real *) drdy, (real *) dsdy, (real *) dtdy, \
113 (real *) drdz, (real *) dsdz, (real *) dtdz, \
114 (real *) w3, *nel); \
115 HIP_CHECK(hipGetLastError());
116
117/* Runtime dispatch onto the tuned candidate */
118#define CASE_KSTEP_SEL(LX, SEL) \
119 switch (SEL) { \
120 case 0: CASE_KSTEP(LX, 0); break; \
121 case 1: CASE_KSTEP(LX, 1); break; \
122 default: CASE_KSTEP(LX, 2); break; \
123 }
124
125#define CASE_MFMA(LX, C) \
126 hipLaunchKernelGGL( HIP_KERNEL_NAME( \
127 opgrad_kernel_mfma<real, LX, NEKO_MFMA_NWF(C)> ), \
128 NEKO_MFMA_NBLCKS(*nel, LX, C), NEKO_MFMA_NTHRDS(C), 0, \
129 (hipStream_t) glb_cmd_queue, \
130 (real *) ux, (real *) uy, (real *) uz, (real *) u, \
131 (real *) dx, (real *) dy, (real *) dz, \
132 (real *) drdx, (real *) dsdx, (real *) dtdx, \
133 (real *) drdy, (real *) dsdy, (real *) dtdy, \
134 (real *) drdz, (real *) dsdz, (real *) dtdz, \
135 (real *) w3, *nel); \
136 HIP_CHECK(hipGetLastError());
137
138/* Runtime dispatch onto the tuned wavefronts per block candidate */
139#define CASE_MFMA_SEL(LX, SEL) \
140 switch (SEL) { \
141 case 0: CASE_MFMA(LX, 0); break; \
142 case 1: CASE_MFMA(LX, 1); break; \
143 case 2: CASE_MFMA(LX, 2); break; \
144 default: CASE_MFMA(LX, 3); break; \
145 }
146
147#define CASE(LX) \
148 case LX: \
149 if(autotune[LX] == 0 ) { \
150 autotune[LX]=tune_opgrad<LX>(ux, uy, uz, u, \
151 dx, dy, dz, \
152 drdx, dsdx, dtdx, \
153 drdy, dsdy, dtdy, \
154 drdz, dsdz, dtdz, \
155 w3, nel, lx, &autotune_eb[LX], \
156 &autotune_ch[LX], \
157 &autotune_nwf[LX]); \
158 } else if (autotune[LX] == 1 ) { \
159 CASE_1D_SEL(LX, autotune_ch[LX]); \
160 } else if (autotune[LX] == 2 ) { \
161 CASE_KSTEP_SEL(LX, autotune_eb[LX]); \
162 } else if (autotune[LX] == 3 ) { \
163 CASE_MFMA_SEL(LX, autotune_nwf[LX]); \
164 } \
165 break
166
167 switch(*lx) {
168 CASE(2);
169 CASE(3);
170 CASE(4);
171 CASE(5);
172 CASE(6);
173 CASE(7);
174 CASE(8);
175 CASE(9);
176 CASE(10);
177 CASE(11);
178 CASE(12);
179 CASE(13);
180 CASE(14);
181 CASE(15);
182 CASE(16);
183 CASE(17);
184 CASE(18);
185 default:
186 {
187 fprintf(stderr, __FILE__ ": size not supported: %d\n", *lx);
188 exit(1);
189 }
190 }
191 }
192}
193
194template < const int LX >
195int tune_opgrad(void *ux, void *uy, void *uz, void *u,
196 void *dx, void *dy, void *dz,
197 void *drdx, void *dsdx, void *dtdx,
198 void *drdy, void *dsdy, void *dtdy,
199 void *drdz, void *dsdz, void *dtdz,
200 void *w3, int *nel, int *lx, int *eb_sel, int *ch_sel,
201 int *nwf_sel) {
204 int best1 = 0;
207 int best3 = 0;
208 const int rounds = neko_tune_rounds();
209 const int iters = neko_tune_iters();
210 const int sweep = neko_eb_sweep();
211 const bool mfma = mfma_lx_supported<LX>() && hip_have_mfma();
212 /* Whether the sweep may pick it on its own, see neko_mfma_sweep() */
213 const bool mfma_tune = mfma && neko_mfma_sweep();
214 int best = 0;
215 int retval;
216
217 for (int c = 0; c < NEKO_EB_CANDIDATES; c++) {
219 }
220 for (int c = 0; c < NEKO_CHUNKS_CANDIDATES; c++) {
222 }
223 for (int c = 0; c < NEKO_MFMA_CANDIDATES; c++) {
225 }
226
227 const dim3 nthrds_1d(1024, 1, 1);
228 const dim3 nthrds_kstep((*lx), (*lx), 1);
229 const dim3 nblcks((*nel), 1, 1);
230 const hipStream_t stream = (hipStream_t) glb_cmd_queue;
231
232 char *env_value = NULL;
233 char neko_log_buf[80];
234
235 env_value=getenv("NEKO_AUTOTUNE");
236
237 sprintf(neko_log_buf, "Autotune opgrad (lx: %d)", *lx);
239
240 *eb_sel = 0;
241 *ch_sel = 0;
242 *nwf_sel = 0;
243
244 if(env_value) {
245 if( !strcmp(env_value,"1D") ) {
248 sprintf(neko_log_buf,"Set by env : 1 (1D, %d chunk)",
252 return 1;
253 } else if( !strcmp(env_value,"KSTEP") ) {
254 {
255 const int c = neko_eb_env();
256 *eb_sel = c;
257 CASE_KSTEP_SEL(LX, c);
258 sprintf(neko_log_buf,"Set by env : 2 (KSTEP, %d elem/block)",
259 NEKO_EB_SEL(LX, c));
260 }
263 return 2;
264 } else if( !strcmp(env_value,"MFMA") ) {
265 if (mfma) {
266 const int c = neko_mfma_env();
267 *nwf_sel = c;
268 CASE_MFMA_SEL(LX, c);
269 sprintf(neko_log_buf,"Set by env : 3 (MFMA, %d wf, %d elem/block)",
273 return 3;
274 } else {
275 sprintf(neko_log_buf, "MFMA strategy not available for this config");
277 }
278 } else {
279 sprintf(neko_log_buf, "Invalid value set for NEKO_AUTOTUNE");
281 }
282 }
283
286
287 /* Warm every variant before timing anything: each specialisation has to be
288 resident and the clocks at steady state, or whichever is timed first is
289 measured on a colder part */
290 for (int i = 0; i < NEKO_TUNE_WARMUP; i++) {
291 CASE_1D(LX, 0);
292 CASE_1D(LX, 1);
293 CASE_1D(LX, 2);
294 CASE_1D(LX, 3);
295 CASE_KSTEP(LX, 0);
296 if (sweep) {
297 CASE_KSTEP(LX, 1);
298 CASE_KSTEP(LX, 2);
299 }
300 if (mfma_tune) {
301 CASE_MFMA(LX, 0);
302 CASE_MFMA(LX, 1);
303 CASE_MFMA(LX, 2);
304 CASE_MFMA(LX, 3);
305 }
306 }
307
308 /* Interleaved rounds, best time per variant */
309 for (int r = 0; r < rounds; r++) {
315 if (sweep) {
318 }
319 if (mfma_tune) {
324 }
325 }
326
329
333 *eb_sel = best;
334 *ch_sel = best1;
335 *nwf_sel = best3;
336
337 if (time1[best1] < time2[best]) {
338 retval = 1;
339 } else {
340 retval = 2;
341 }
342
343 /* The mfma variant joins the comparison only where it exists, its
344 candidates are left at NEKO_TUNE_INIT otherwise */
345 if (time3[best3] < ((retval == 1) ? time1[best1] : time2[best])) {
346 retval = 3;
347 }
348
349 /* Leave the chosen kernel's output in place: the tuner stands in for a real
350 evaluation and the variants do not sum in the same order */
351 if (retval == 1) {
353 } else if (retval == 2) {
355 } else {
357 }
358
359 if (retval == 1) {
360 sprintf(neko_log_buf, "Chose : 1 (1D, %d chunk)",
362 } else if (retval == 2) {
363 sprintf(neko_log_buf, "Chose : 2 (KSTEP, %d elem/block)",
365 } else {
366 sprintf(neko_log_buf, "Chose : 3 (MFMA, %d wf, %d elem/block)",
368 }
371 return retval;
372}
__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 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)
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 *nwf_sel)
#define CASE(LX)
#define CASE_1D_SEL(LX, SEL)
#define CASE_MFMA_SEL(LX, SEL)
#define CASE_KSTEP(LX, C)
#define CASE_MFMA(LX, C)
#define CASE_1D(LX, C)
void hip_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)