Neko 1.99.11
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
opr_convect_scalar.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>
42#include "elem_block_tune.h"
43
44extern "C" {
45 #include <common/neko_log.h>
46}
47
48template < const int >
49int tune_convect_scalar(void *du, void *u,
50 void *cr, void *cs, void *ct,
51 void *dx, void *dy, void *dz,
52 int *nel, int *lx, int *eb_sel, int *ch_sel);
53
54extern "C" {
55
59 void hip_convect_scalar(void *du, void *u,
60 void *cr, void *cs, void *ct,
61 void *dx, void *dy, void *dz,
62 int *nel, int *lx) {
63
64 static int autotune[17] = { 0 };
65 /* elements per block candidate chosen by the tuner */
66 static int autotune_eb[17] = { 0 };
67 /* chunk candidate chosen for the 1d variant */
68 static int autotune_ch[17] = { 0 };
69
70 const dim3 nthrds_1d(1024, 1, 1);
71 const dim3 nthrds_kstep((*lx), (*lx), 1);
72 const dim3 nblcks((*nel), 1, 1);
73
74#define CASE_1D(LX, C) \
75 hipLaunchKernelGGL( HIP_KERNEL_NAME( \
76 convect_scalar_kernel_1d<real, LX, NEKO_CHUNKS(LX, C)> ),\
77 nblcks, NEKO_CHUNKS_NTHRDS(LX, C), 0, \
78 (hipStream_t) glb_cmd_queue, \
79 (real *) du, (real *) u, \
80 (real *) cr, (real *) cs, (real *) ct, \
81 (real *) dx, (real *) dy, (real *) dz); \
82 HIP_CHECK(hipGetLastError());
83
84/* Runtime dispatch onto the tuned chunk candidate */
85#define CASE_1D_SEL(LX, SEL) \
86 switch (SEL) { \
87 case 0: CASE_1D(LX, 0); break; \
88 case 1: CASE_1D(LX, 1); break; \
89 case 2: CASE_1D(LX, 2); break; \
90 default: CASE_1D(LX, 3); break; \
91 }
92
93#define CASE_KSTEP(LX, C) \
94 hipLaunchKernelGGL( HIP_KERNEL_NAME( \
95 convect_scalar_kernel_kstep<real, LX, NEKO_EB(LX, C)> ),\
96 NEKO_EB_NBLCKS(*nel, LX, C), NEKO_EB_NTHRDS(LX, C), 0, \
97 (hipStream_t) glb_cmd_queue, \
98 (real *) du, (real *) u, \
99 (real *) cr, (real *) cs, (real *) ct, \
100 (real *) dx, (real *) dy, (real *) dz, *nel); \
101 HIP_CHECK(hipGetLastError());
102
103/* Runtime dispatch onto the tuned candidate */
104#define CASE_KSTEP_SEL(LX, SEL) \
105 switch (SEL) { \
106 case 0: CASE_KSTEP(LX, 0); break; \
107 case 1: CASE_KSTEP(LX, 1); break; \
108 default: CASE_KSTEP(LX, 2); break; \
109 }
110
111#define CASE(LX) \
112 case LX: \
113 if(autotune[LX] == 0 ) { \
114 autotune[LX]=tune_convect_scalar<LX>(du, u, \
115 cr, cs, ct, \
116 dx, dy, dz, \
117 nel, lx, &autotune_eb[LX], \
118 &autotune_ch[LX]); \
119 } else if (autotune[LX] == 1 ) { \
120 CASE_1D_SEL(LX, autotune_ch[LX]); \
121 } else if (autotune[LX] == 2 ) { \
122 CASE_KSTEP_SEL(LX, autotune_eb[LX]); \
123 } \
124 break
125
126#define CASE_LARGE(LX) \
127 case LX: \
128 CASE_KSTEP(LX, 0); \
129 break
130
131
132 if ((*lx) < 11) {
133 switch(*lx) {
134 CASE(2);
135 CASE(3);
136 CASE(4);
137 CASE(5);
138 CASE(6);
139 CASE(7);
140 CASE(8);
141 CASE(9);
142 CASE(10);
143 default:
144 {
145 fprintf(stderr, __FILE__ ": size not supported: %d\n", *lx);
146 exit(1);
147 }
148 }
149 }
150 else {
151 switch(*lx) {
152 CASE_LARGE(11);
153 CASE_LARGE(12);
154 CASE_LARGE(13);
155 CASE_LARGE(14);
156 CASE_LARGE(15);
157 CASE_LARGE(16);
158 default:
159 {
160 fprintf(stderr, __FILE__ ": size not supported: %d\n", *lx);
161 exit(1);
162 }
163 }
164 }
165 }
166}
167
168template < const int LX >
169int tune_convect_scalar(void *du, void *u,
170 void *cr, void *cs, void *ct,
171 void *dx, void *dy, void *dz,
172 int *nel, int *lx, int *eb_sel, int *ch_sel) {
173 hipEvent_t start,stop;
174 const hipStream_t stream = (hipStream_t) glb_cmd_queue;
175 float time1[NEKO_CHUNKS_CANDIDATES];
176 int best1 = 0;
177 float time2[NEKO_EB_CANDIDATES];
178 const int rounds = neko_tune_rounds();
179 const int iters = neko_tune_iters();
180 /* Candidates of the kstep sweep, one -- the unblocked shape -- when the
181 elements per block sweep is off */
182 const int eb_cand = neko_eb_sweep() ? NEKO_EB_CANDIDATES : 1;
183 /* Geometry pinned by each formulation's own variable, -1 to sweep it */
184 const int ch_pin = neko_chunks_pin();
185 const int eb_pin = neko_eb_pin();
186 /* Formulation pinned by NEKO_AUTOTUNE, as the identifier this returns */
187 int strat = 0;
188 int best = 0;
189 int retval;
190
191 for (int c = 0; c < NEKO_EB_CANDIDATES; c++) {
192 time2[c] = NEKO_TUNE_INIT;
193 }
194 for (int c = 0; c < NEKO_CHUNKS_CANDIDATES; c++) {
195 time1[c] = NEKO_TUNE_INIT;
196 }
197
198 const dim3 nthrds_1d(1024, 1, 1);
199 const dim3 nthrds_kstep((*lx), (*lx), 1);
200 const dim3 nblcks((*nel), 1, 1);
201
202 char *env_value = NULL;
203 char neko_log_buf[80];
204
205 env_value=getenv("NEKO_AUTOTUNE");
206
207 sprintf(neko_log_buf, "Autotune convect_scalar (lx: %d)", *lx);
208 log_section(neko_log_buf);
209
210 *eb_sel = 0;
211 *ch_sel = 0;
212
213 /*
214 * NEKO_AUTOTUNE names a formulation, and that is all it does: the sweep
215 * below is narrowed to that one kernel family, but its geometry -- the
216 * chunk size, the elements per block -- is still measured candidate
217 * against candidate.
218 */
219 if(env_value) {
220 if( !strcmp(env_value,"1D") ) {
221 strat = 1;
222 } else if( !strcmp(env_value,"KSTEP") ) {
223 strat = 2;
224 } else {
225 sprintf(neko_log_buf, "Invalid value set for NEKO_AUTOTUNE");
226 log_error(neko_log_buf);
227 }
228 }
229
230 /* Geometry of the pinned formulation, if its own variable fixes that too.
231 Both pinned leaves nothing to measure, so the kernel is launched once and
232 reported, which is what pinning has always done */
233 const int pin = (strat == 1) ? ch_pin : (strat == 2) ? eb_pin : -1;
234
235 if (pin >= 0) {
236 switch (strat) {
237 case 1:
238 *ch_sel = pin;
239 CASE_1D_SEL(LX, pin);
240 sprintf(neko_log_buf, "Set by env : 1 (1D, %d chunk)",
241 NEKO_CHUNKS_SEL(LX, pin));
242 break;
243 default:
244 *eb_sel = pin;
245 CASE_KSTEP_SEL(LX, pin);
246 sprintf(neko_log_buf, "Set by env : 2 (KSTEP, %d elem/block)",
247 NEKO_EB_SEL(LX, pin));
248 break;
249 }
250 log_message(neko_log_buf);
252 return strat;
253 }
254
255 if (strat) {
256 sprintf(neko_log_buf, "Set by env : %d (%s)", strat, env_value);
257 log_message(neko_log_buf);
258 }
259
260 /* Formulations the sweep considers, see NEKO_TUNE_FOR() */
261 const bool try_1d = (strat == 0 || strat == 1);
262 const bool try_kstep = (strat == 0 || strat == 2);
263
264 HIP_CHECK(hipEventCreate(&start));
265 HIP_CHECK(hipEventCreate(&stop));
266 /* Warm every variant before timing anything: each specialisation has to be
267 resident and the clocks at steady state, or whichever is timed first is
268 measured on a colder part */
269 for (int i = 0; i < NEKO_TUNE_WARMUP; i++) {
270 NEKO_TUNE_FOR(c, try_1d, ch_pin, NEKO_CHUNKS_CANDIDATES) {
271 CASE_1D_SEL(LX, c);
272 }
273 NEKO_TUNE_FOR(c, try_kstep, eb_pin, eb_cand) {
274 CASE_KSTEP_SEL(LX, c);
275 }
276 }
277
278 /* Interleaved rounds, best time per variant */
279 for (int r = 0; r < rounds; r++) {
280 NEKO_TUNE_FOR(c, try_1d, ch_pin, NEKO_CHUNKS_CANDIDATES) {
281 NEKO_TUNE_TIME(time1, CASE_1D_SEL, LX, c, iters);
282 }
283 NEKO_TUNE_FOR(c, try_kstep, eb_pin, eb_cand) {
284 NEKO_TUNE_TIME(time2, CASE_KSTEP_SEL, LX, c, iters);
285 }
286 }
287
288 NEKO_TUNE_LOG(LX, time1, time2);
291 *eb_sel = best;
292 *ch_sel = best1;
293
294 if (time1[best1] < time2[best]) {
295 retval = 1;
296 } else {
297 retval = 2;
298 }
299
300 /* Leave the chosen kernel's output in place: the tuner stands in for a real
301 evaluation and the variants do not sum in the same order */
302 if (retval == 1) {
303 CASE_1D_SEL(LX, best1);
304 } else {
305 CASE_KSTEP_SEL(LX, best);
306 }
307
308 if (retval == 1) {
309 sprintf(neko_log_buf, "Chose : 1 (1D, %d chunk)",
310 NEKO_CHUNKS_SEL(LX, best1));
311 } else {
312 sprintf(neko_log_buf, "Chose : 2 (KSTEP, %d elem/block)",
313 NEKO_EB_SEL(LX, best));
314 }
315 log_message(neko_log_buf);
317 return retval;
318}
#define CASE_KSTEP_SEL(LX, SEL)
#define CASE_1D_SEL(LX, SEL)
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 const T *__restrict__ const T *__restrict__ cr
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ cs
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ ct
#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
void * glb_cmd_queue
#define HIP_CHECK(err)
Definition check.h:8
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_LARGE(LX)
void hip_convect_scalar(void *du, void *u, void *cr, void *cs, void *ct, void *dx, void *dy, void *dz, int *nel, int *lx)
int tune_convect_scalar(void *du, void *u, void *cr, void *cs, void *ct, void *dx, void *dy, void *dz, int *nel, int *lx, int *eb_sel, int *ch_sel)