Neko 1.99.7
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
opr_lambda2.cu
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 "lambda2_kernel.h"
39#include "elem_block_tune.h"
41#include <device/cuda/check.h>
42
43extern "C" {
44 #include <common/neko_log.h>
45}
46
47template < const int >
48int tune_lambda2(void *ux, void *uy, void *uz, void *u,
49 void *dx, void *dy, void *dz,
50 void *drdx, void *dsdx, void *dtdx,
51 void *drdy, void *dsdy, void *dtdy,
52 void *drdz, void *dsdz, void *dtdz,
53 void *jacinv, int *nel, int *lx, int *eb_sel, int *ch_sel);
54
55extern "C" {
56
60 void cuda_lambda2(void *lambda2, void *u, void *v, void *w,
61 void *dx, void *dy, void *dz,
62 void *drdx, void *dsdx, void *dtdx,
63 void *drdy, void *dsdy, void *dtdy,
64 void *drdz, void *dsdz, void *dtdz,
65 void *jacinv, int *nel, int *lx) {
66
67 static int autotune[17] = { 0 };
68 /* elements per block candidate chosen by the tuner */
69 static int autotune_eb[17] = { 0 };
70 /* chunk candidate chosen for the 1d variant */
71 static int autotune_ch[17] = { 0 };
72
73 const dim3 nthrds_1d(1024, 1, 1);
74 const dim3 nthrds_kstep((*lx), (*lx), 1);
75 const dim3 nblcks((*nel), 1, 1);
76 const cudaStream_t stream = (cudaStream_t) glb_cmd_queue;
77
78#define CASE_1D(LX, C) \
79 lambda2_kernel_1d<real, LX, NEKO_CHUNKS(LX, C)> \
80 <<<nblcks, NEKO_CHUNKS_NTHRDS(LX, C), 0, stream>>> \
81 ((real *) lambda2, (real *) u, (real *) v, (real *) w, \
82 (real *) dx, (real *) dy, (real *) dz, \
83 (real *) drdx, (real *) dsdx, (real *) dtdx, \
84 (real *) drdy, (real *) dsdy, (real *) dtdy, \
85 (real *) drdz, (real *) dsdz, (real *) dtdz, \
86 (real *) jacinv); \
87 CUDA_CHECK(cudaGetLastError());
88
89/* Runtime dispatch onto the tuned chunk candidate */
90#define CASE_1D_SEL(LX, SEL) \
91 switch (SEL) { \
92 case 0: CASE_1D(LX, 0); break; \
93 case 1: CASE_1D(LX, 1); break; \
94 case 2: CASE_1D(LX, 2); break; \
95 default: CASE_1D(LX, 3); break; \
96 }
97
98
99#define CASE_KSTEP(LX, C) \
100 lambda2_kernel_kstep<real, LX, NEKO_EB(LX, C)> \
101 <<<NEKO_EB_NBLCKS(*nel, LX, C), NEKO_EB_NTHRDS(LX, C), 0, stream>>> \
102 ((real *) lambda2, (real *) u, (real *) v, (real *) w, \
103 (real *) dx, (real *) dy, (real *) dz, \
104 (real *) drdx, (real *) dsdx, (real *) dtdx, \
105 (real *) drdy, (real *) dsdy, (real *) dtdy, \
106 (real *) drdz, (real *) dsdz, (real *) dtdz, \
107 (real *) jacinv, *nel); \
108 CUDA_CHECK(cudaGetLastError());
109
110/* Runtime dispatch onto the tuned candidate */
111#define CASE_KSTEP_SEL(LX, SEL) \
112 switch (SEL) { \
113 case 0: CASE_KSTEP(LX, 0); break; \
114 case 1: CASE_KSTEP(LX, 1); break; \
115 default: CASE_KSTEP(LX, 2); break; \
116 }
117
118#define CASE(LX) \
119 case LX: \
120 if(autotune[LX] == 0 ) { \
121 autotune[LX]=tune_lambda2<LX>(lambda2, u, v, w, \
122 dx, dy, dz, \
123 drdx, dsdx, dtdx, \
124 drdy, dsdy, dtdy, \
125 drdz, dsdz, dtdz, \
126 jacinv, nel, lx, &autotune_eb[LX], \
127 &autotune_ch[LX]); \
128 } else if (autotune[LX] == 1 ) { \
129 CASE_1D_SEL(LX, autotune_ch[LX]); \
130 } else if (autotune[LX] == 2 ) { \
131 CASE_KSTEP_SEL(LX, autotune_eb[LX]); \
132 } \
133 break
134
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 CASE(11);
146 CASE(12);
147 default:
148 {
149 fprintf(stderr, __FILE__ ": size not supported: %d\n", *lx);
150 exit(1);
151 }
152 }
153 }
154}
155
156template < const int LX >
157int tune_lambda2(void *lambda2, void *u, void *v, void *w,
158 void *dx, void *dy, void *dz,
159 void *drdx, void *dsdx, void *dtdx,
160 void *drdy, void *dsdy, void *dtdy,
161 void *drdz, void *dsdz, void *dtdz,
162 void *jacinv, int *nel, int *lx, int *eb_sel, int *ch_sel) {
165 int best1 = 0;
167 const int rounds = neko_tune_rounds();
168 const int iters = neko_tune_iters();
169 const int sweep = neko_eb_sweep();
170 int best = 0;
171 int retval;
172
173 for (int c = 0; c < NEKO_EB_CANDIDATES; c++) {
175 }
176 for (int c = 0; c < NEKO_CHUNKS_CANDIDATES; c++) {
178 }
179
180 const dim3 nthrds_1d(1024, 1, 1);
181 const dim3 nthrds_kstep((*lx), (*lx), 1);
182 const dim3 nblcks((*nel), 1, 1);
183 const cudaStream_t stream = (cudaStream_t) glb_cmd_queue;
184
185 char *env_value = NULL;
186 char neko_log_buf[80];
187
188 env_value=getenv("NEKO_AUTOTUNE");
189
190 sprintf(neko_log_buf, "Autotune lambda2 (lx: %d)", *lx);
192
193 if(env_value) {
194 if( !strcmp(env_value,"1D") ) {
197 sprintf(neko_log_buf,"Set by env : 1 (1D, %d chunk)",
201 return 1;
202 } else if( !strcmp(env_value,"KSTEP") ) {
203 *eb_sel = neko_eb_env();
205 sprintf(neko_log_buf,"Set by env : 2 (KSTEP, %d elem/block)",
209 return 2;
210 } else {
211 sprintf(neko_log_buf, "Invalid value set for NEKO_AUTOTUNE");
213 }
214 }
215
218 /* Warm every variant before timing anything: each specialisation has to be
219 resident and the clocks at steady state, or whichever is timed first is
220 measured on a colder part */
221 for (int i = 0; i < NEKO_TUNE_WARMUP; i++) {
222 CASE_1D(LX, 0);
223 CASE_1D(LX, 1);
224 CASE_1D(LX, 2);
225 CASE_1D(LX, 3);
226 CASE_KSTEP(LX, 0);
227 if (sweep) {
228 CASE_KSTEP(LX, 1);
229 CASE_KSTEP(LX, 2);
230 }
231 }
232
233 /* Interleaved rounds, best time per variant */
234 for (int r = 0; r < rounds; r++) {
240 if (sweep) {
243 }
244 }
245
249 *eb_sel = best;
250 *ch_sel = best1;
251
252 if (time1[best1] < time2[best]) {
253 retval = 1;
254 } else {
255 retval = 2;
256 }
257
258 /* Leave the chosen kernel's output in place: the tuner stands in for a real
259 evaluation and the variants do not sum in the same order */
260 if (retval == 1) {
262 } else {
264 }
265
266 if (retval == 1) {
267 sprintf(neko_log_buf, "Chose : 1 (1D, %d chunk)",
269 } else {
270 sprintf(neko_log_buf, "Chose : 2 (KSTEP, %d elem/block)",
272 }
275 return retval;
276}
277
__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
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ w
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__ v
__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 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
#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
A simulation component that computes lambda2 The values are stored in the field registry under the na...
Definition lambda2.f90:37
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)
int tune_lambda2(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 *jacinv, int *nel, int *lx, int *eb_sel, int *ch_sel)
#define CASE_1D(LX, C)
void cuda_lambda2(void *lambda2, void *u, void *v, void *w, 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 *lx)