Neko 1.99.7
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
56extern "C" {
57
61 void hip_opgrad(void *ux, void *uy, void *uz, void *u,
62 void *dx, void *dy, void *dz,
63 void *drdx, void *dsdx, void *dtdx,
64 void *drdy, void *dsdy, void *dtdy,
65 void *drdz, void *dsdz, void *dtdz,
66 void *w3, int *nel, int *lx) {
67
68 static int autotune[19] = { 0 };
69 /* elements per block candidate chosen by the tuner */
70 static int autotune_eb[19] = { 0 };
71 /* chunk candidate chosen for the 1d variant */
72 static int autotune_ch[19] = { 0 };
73
74 const dim3 nthrds_1d(1024, 1, 1);
75 const dim3 nthrds_kstep((*lx), (*lx), 1);
76 const dim3 nblcks((*nel), 1, 1);
77
78#define CASE_1D(LX, C) \
79 hipLaunchKernelGGL( HIP_KERNEL_NAME( \
80 opgrad_kernel_1d<real, LX, NEKO_CHUNKS(LX, C)> ), \
81 nblcks, NEKO_CHUNKS_NTHRDS(LX, C), 0, \
82 (hipStream_t) glb_cmd_queue, \
83 (real *) ux, (real *) uy, (real *) uz, (real *) u, \
84 (real *) dx, (real *) dy, (real *) dz, \
85 (real *) drdx, (real *) dsdx, (real *) dtdx, \
86 (real *) drdy, (real *) dsdy, (real *) dtdy, \
87 (real *) drdz, (real *) dsdz, (real *) dtdz, \
88 (real *) w3); \
89 HIP_CHECK(hipGetLastError());
90
91/* Runtime dispatch onto the tuned chunk candidate */
92#define CASE_1D_SEL(LX, SEL) \
93 switch (SEL) { \
94 case 0: CASE_1D(LX, 0); break; \
95 case 1: CASE_1D(LX, 1); break; \
96 case 2: CASE_1D(LX, 2); break; \
97 default: CASE_1D(LX, 3); break; \
98 }
99
100
101#define CASE_KSTEP(LX, C) \
102 hipLaunchKernelGGL( HIP_KERNEL_NAME( \
103 opgrad_kernel_kstep<real, LX, NEKO_EB(LX, C)> ), \
104 NEKO_EB_NBLCKS(*nel, LX, C), NEKO_EB_NTHRDS(LX, C), 0, \
105 (hipStream_t) glb_cmd_queue, \
106 (real *) ux, (real *) uy, (real *) uz, (real *) u, \
107 (real *) dx, (real *) dy, (real *) dz, \
108 (real *) drdx, (real *) dsdx, (real *) dtdx, \
109 (real *) drdy, (real *) dsdy, (real *) dtdy, \
110 (real *) drdz, (real *) dsdz, (real *) dtdz, \
111 (real *) w3, *nel); \
112 HIP_CHECK(hipGetLastError());
113
114/* Runtime dispatch onto the tuned candidate */
115#define CASE_KSTEP_SEL(LX, SEL) \
116 switch (SEL) { \
117 case 0: CASE_KSTEP(LX, 0); break; \
118 case 1: CASE_KSTEP(LX, 1); break; \
119 default: CASE_KSTEP(LX, 2); break; \
120 }
121
122#define CASE(LX) \
123 case LX: \
124 if(autotune[LX] == 0 ) { \
125 autotune[LX]=tune_opgrad<LX>(ux, uy, uz, u, \
126 dx, dy, dz, \
127 drdx, dsdx, dtdx, \
128 drdy, dsdy, dtdy, \
129 drdz, dsdz, dtdz, \
130 w3, nel, lx, &autotune_eb[LX], \
131 &autotune_ch[LX]); \
132 } else if (autotune[LX] == 1 ) { \
133 CASE_1D_SEL(LX, autotune_ch[LX]); \
134 } else if (autotune[LX] == 2 ) { \
135 CASE_KSTEP_SEL(LX, autotune_eb[LX]); \
136 } \
137 break
138
139 switch(*lx) {
140 CASE(2);
141 CASE(3);
142 CASE(4);
143 CASE(5);
144 CASE(6);
145 CASE(7);
146 CASE(8);
147 CASE(9);
148 CASE(10);
149 CASE(11);
150 CASE(12);
151 CASE(13);
152 CASE(14);
153 CASE(15);
154 CASE(16);
155 CASE(17);
156 CASE(18);
157 default:
158 {
159 fprintf(stderr, __FILE__ ": size not supported: %d\n", *lx);
160 exit(1);
161 }
162 }
163 }
164}
165
166template < const int LX >
167int tune_opgrad(void *ux, void *uy, void *uz, void *u,
168 void *dx, void *dy, void *dz,
169 void *drdx, void *dsdx, void *dtdx,
170 void *drdy, void *dsdy, void *dtdy,
171 void *drdz, void *dsdz, void *dtdz,
172 void *w3, int *nel, int *lx, int *eb_sel, int *ch_sel) {
175 int best1 = 0;
177 const int rounds = neko_tune_rounds();
178 const int iters = neko_tune_iters();
179 const int sweep = neko_eb_sweep();
180 int best = 0;
181 int retval;
182
183 for (int c = 0; c < NEKO_EB_CANDIDATES; c++) {
185 }
186 for (int c = 0; c < NEKO_CHUNKS_CANDIDATES; c++) {
188 }
189
190 const dim3 nthrds_1d(1024, 1, 1);
191 const dim3 nthrds_kstep((*lx), (*lx), 1);
192 const dim3 nblcks((*nel), 1, 1);
193 const hipStream_t stream = (hipStream_t) glb_cmd_queue;
194
195 char *env_value = NULL;
196 char neko_log_buf[80];
197
198 env_value=getenv("NEKO_AUTOTUNE");
199
200 sprintf(neko_log_buf, "Autotune opgrad (lx: %d)", *lx);
202
203 *eb_sel = 0;
204 *ch_sel = 0;
205
206 if(env_value) {
207 if( !strcmp(env_value,"1D") ) {
210 sprintf(neko_log_buf,"Set by env : 1 (1D, %d chunk)",
214 return 1;
215 } else if( !strcmp(env_value,"KSTEP") ) {
216 {
217 const int c = neko_eb_env();
218 *eb_sel = c;
219 CASE_KSTEP_SEL(LX, c);
220 sprintf(neko_log_buf,"Set by env : 2 (KSTEP, %d elem/block)",
221 NEKO_EB_SEL(LX, c));
222 }
225 return 2;
226 } else {
227 sprintf(neko_log_buf, "Invalid value set for NEKO_AUTOTUNE");
229 }
230 }
231
234
235 /* Warm every variant before timing anything: each specialisation has to be
236 resident and the clocks at steady state, or whichever is timed first is
237 measured on a colder part */
238 for (int i = 0; i < NEKO_TUNE_WARMUP; i++) {
239 CASE_1D(LX, 0);
240 CASE_1D(LX, 1);
241 CASE_1D(LX, 2);
242 CASE_1D(LX, 3);
243 CASE_KSTEP(LX, 0);
244 if (sweep) {
245 CASE_KSTEP(LX, 1);
246 CASE_KSTEP(LX, 2);
247 }
248 }
249
250 /* Interleaved rounds, best time per variant */
251 for (int r = 0; r < rounds; r++) {
257 if (sweep) {
260 }
261 }
262
266 *eb_sel = best;
267 *ch_sel = best1;
268
269 if (time1[best1] < time2[best]) {
270 retval = 1;
271 } else {
272 retval = 2;
273 }
274
275 /* Leave the chosen kernel's output in place: the tuner stands in for a real
276 evaluation and the variants do not sum in the same order */
277 if (retval == 1) {
279 } else {
281 }
282
283 if (retval == 1) {
284 sprintf(neko_log_buf, "Chose : 1 (1D, %d chunk)",
286 } else {
287 sprintf(neko_log_buf, "Chose : 2 (KSTEP, %d elem/block)",
289 }
292 return retval;
293}
__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__ drdy
__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__ 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__ 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
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__ 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__ 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__ 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__ 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__ 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__ 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__ 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 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
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_KSTEP(LX, C)
#define CASE_1D(LX, C)
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)
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)