Neko 1.99.7
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
54extern "C" {
55
59 void hip_dudxyz(void *du, void *u,
60 void *dr, void *ds, void *dt,
61 void *dx, void *dy, void *dz,
62 void *jacinv, int *nel, int *lx) {
63
64 static int autotune[16] = { 0 };
65 /* elements per block candidate chosen by the tuner */
66 static int autotune_eb[16] = { 0 };
67 /* chunk candidate chosen for the 1d variant */
68 static int autotune_ch[16] = { 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 dudxyz_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 *) dr, (real *) ds, (real *) dt, \
81 (real *) dx, (real *) dy, (real *) dz, \
82 (real *) jacinv); \
83 HIP_CHECK(hipGetLastError());
84
85/* Runtime dispatch onto the tuned chunk candidate */
86#define CASE_1D_SEL(LX, SEL) \
87 switch (SEL) { \
88 case 0: CASE_1D(LX, 0); break; \
89 case 1: CASE_1D(LX, 1); break; \
90 case 2: CASE_1D(LX, 2); break; \
91 default: CASE_1D(LX, 3); break; \
92 }
93
94#define CASE_KSTEP(LX, C) \
95 hipLaunchKernelGGL( HIP_KERNEL_NAME( \
96 dudxyz_kernel_kstep<real, LX, NEKO_EB(LX, C)> ), \
97 NEKO_EB_NBLCKS(*nel, LX, C), NEKO_EB_NTHRDS(LX, C), 0, \
98 (hipStream_t) glb_cmd_queue, \
99 (real *) du, (real *) u, \
100 (real *) dr, (real *) ds, (real *) dt, \
101 (real *) dx, (real *) dy, (real *) dz, \
102 (real *) jacinv, *nel); \
103 HIP_CHECK(hipGetLastError());
104
105/* Runtime dispatch onto the tuned candidate */
106#define CASE_KSTEP_SEL(LX, SEL) \
107 switch (SEL) { \
108 case 0: CASE_KSTEP(LX, 0); break; \
109 case 1: CASE_KSTEP(LX, 1); break; \
110 default: CASE_KSTEP(LX, 2); break; \
111 }
112
113 #define CASE(LX) \
114 case LX: \
115 if(autotune[LX] == 0 ) { \
116 autotune[LX]=tune_dudxyz<LX>(du, u, \
117 dr, ds, dt, \
118 dx, dy, dz, \
119 jacinv, nel, lx, &autotune_eb[LX], \
120 &autotune_ch[LX]); \
121 } else if (autotune[LX] == 1 ) { \
122 CASE_1D_SEL(LX, autotune_ch[LX]); \
123 } else if (autotune[LX] == 2 ) { \
124 CASE_KSTEP_SEL(LX, autotune_eb[LX]); \
125 } \
126 break
127
128#define CASE_LARGE(LX) \
129 case LX: \
130 CASE_KSTEP(LX, 0); \
131 break
132
133
134 if ((*lx) < 11) {
135 switch(*lx) {
136 CASE(2);
137 CASE(3);
138 CASE(4);
139 CASE(5);
140 CASE(6);
141 CASE(7);
142 CASE(8);
143 CASE(9);
144 CASE(10);
145 default:
146 {
147 fprintf(stderr, __FILE__ ": size not supported: %d\n", *lx);
148 exit(1);
149 }
150 }
151 }
152 else {
153 switch(*lx) {
154 CASE_LARGE(11);
155 CASE_LARGE(12);
156 CASE_LARGE(13);
157 CASE_LARGE(14);
158 CASE_LARGE(15);
159 CASE_LARGE(16);
160 default:
161 {
162 fprintf(stderr, __FILE__ ": size not supported: %d\n", *lx);
163 exit(1);
164 }
165 }
166 }
167 }
168}
169
170template < const int LX >
171int tune_dudxyz(void *du, void *u,
172 void *dr, void *ds, void *dt,
173 void *dx, void *dy, void *dz,
174 void *jacinv, int *nel, int *lx, int *eb_sel, int *ch_sel) {
177 int best1 = 0;
179 const int rounds = neko_tune_rounds();
180 const int iters = neko_tune_iters();
181 const int sweep = neko_eb_sweep();
182 int best = 0;
183 int retval;
184
185 for (int c = 0; c < NEKO_EB_CANDIDATES; c++) {
187 }
188 for (int c = 0; c < NEKO_CHUNKS_CANDIDATES; c++) {
190 }
191
192 const dim3 nthrds_1d(1024, 1, 1);
193 const dim3 nthrds_kstep((*lx), (*lx), 1);
194 const dim3 nblcks((*nel), 1, 1);
195 const hipStream_t stream = (hipStream_t) glb_cmd_queue;
196
197 char *env_value = NULL;
198 char neko_log_buf[80];
199
200 env_value=getenv("NEKO_AUTOTUNE");
201
202 sprintf(neko_log_buf, "Autotune dudxyz (lx: %d)", *lx);
204
205 *eb_sel = 0;
206 *ch_sel = 0;
207
208 if(env_value) {
209 if( !strcmp(env_value,"1D") ) {
212 sprintf(neko_log_buf,"Set by env : 1 (1D, %d chunk)",
216 return 1;
217 } else if( !strcmp(env_value,"KSTEP") ) {
218 *eb_sel = neko_eb_env();
220 sprintf(neko_log_buf,"Set by env : 2 (KSTEP, %d elem/block)",
224 return 2;
225 } else {
226 sprintf(neko_log_buf, "Invalid value set for NEKO_AUTOTUNE");
228 }
229 }
230
233
234 /* Warm every variant before timing anything: each specialisation has to be
235 resident and the clocks at steady state, or whichever is timed first is
236 measured on a colder part */
237 for (int i = 0; i < NEKO_TUNE_WARMUP; i++) {
238 CASE_1D(LX, 0);
239 CASE_1D(LX, 1);
240 CASE_1D(LX, 2);
241 CASE_1D(LX, 3);
242 CASE_KSTEP(LX, 0);
243 if (sweep) {
244 CASE_KSTEP(LX, 1);
245 CASE_KSTEP(LX, 2);
246 }
247 }
248
249 /* Interleaved rounds, best time per variant */
250 for (int r = 0; r < rounds; r++) {
256 if (sweep) {
259 }
260 }
261
265 *eb_sel = best;
266 *ch_sel = best1;
267
268 if (time1[best1] < time2[best]) {
269 retval = 1;
270 } else {
271 retval = 2;
272 }
273
274 /* Leave the chosen kernel's output in place: the tuner stands in for a real
275 evaluation and the variants do not sum in the same order */
276 if (retval == 1) {
278 } else {
280 }
281
282 if (retval == 1) {
283 sprintf(neko_log_buf, "Chose : 1 (1D, %d chunk)",
285 } else {
286 sprintf(neko_log_buf, "Chose : 2 (KSTEP, %d elem/block)",
288 }
291 return retval;
292}
__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 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__ dx
__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__ 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__ dr
__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__ 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
#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
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)
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)
#define CASE_KSTEP(LX, C)
#define CASE_LARGE(LX)
#define CASE_1D(LX, C)
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)