Neko 1.99.1
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
opr_dudxyz.c
Go to the documentation of this file.
1/*
2 Copyright (c) 2021-2025, 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#ifdef __APPLE__
36#include <OpenCL/cl.h>
37#else
38#include <CL/cl.h>
39#endif
40
41#include <stdlib.h>
42#include <stdio.h>
43#include <string.h>
45#include <device/opencl/jit.h>
47#include <device/opencl/check.h>
48#include <common/neko_log.h>
49
50#include "dudxyz_kernel.cl.h"
51
53
57void opencl_dudxyz(void *du, void *u,
58 void *dr, void *ds, void *dt,
59 void *dx, void *dy, void *dz,
60 void *jacinv, int *nel, int *lx) {
61 cl_int err;
62
63 if (dudxyz_program == NULL)
65
66 const size_t global_item_size = 256 * (*nel);
67 const size_t local_item_size = 256;
68
69 size_t global_kstep[2];
70 size_t local_kstep[2];
71 local_kstep[0] = (*lx);
72 local_kstep[1] = (*lx);
73 global_kstep[0] = (*nel) * (*lx);
74 global_kstep[1] = (*lx);
75
76 if (autotune_dudxyz == NULL) {
77 autotune_dudxyz = malloc(17 * sizeof(int));
78 memset(autotune_dudxyz, 0, 17 * sizeof(int));
79 }
80
81#define STR(X) #X
82#define CASE_1D(LX, QUEUE, EVENT) \
83 { \
84 cl_kernel kernel = clCreateKernel(dudxyz_program, \
85 STR(dudxyz_kernel_lx##LX), &err); \
86 CL_CHECK(err); \
87 \
88 CL_CHECK(clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *) &du)); \
89 CL_CHECK(clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *) &u)); \
90 CL_CHECK(clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *) &dr)); \
91 CL_CHECK(clSetKernelArg(kernel, 3, sizeof(cl_mem), (void *) &ds)); \
92 CL_CHECK(clSetKernelArg(kernel, 4, sizeof(cl_mem), (void *) &dt)); \
93 CL_CHECK(clSetKernelArg(kernel, 5, sizeof(cl_mem), (void *) &dx)); \
94 CL_CHECK(clSetKernelArg(kernel, 6, sizeof(cl_mem), (void *) &dy)); \
95 CL_CHECK(clSetKernelArg(kernel, 7, sizeof(cl_mem), (void *) &dz)); \
96 CL_CHECK(clSetKernelArg(kernel, 8, sizeof(cl_mem), (void *) &jacinv)); \
97 \
98 CL_CHECK(clEnqueueNDRangeKernel((cl_command_queue) QUEUE, \
99 kernel, 1, NULL, &global_item_size, \
100 &local_item_size, 0, NULL, EVENT)); \
101 }
102
103#define CASE_KSTEP(LX, QUEUE, EVENT) \
104 { \
105 cl_kernel kernel = clCreateKernel(dudxyz_program, \
106 STR(dudxyz_kernel_kstep_lx##LX), &err); \
107 CL_CHECK(err); \
108 \
109 CL_CHECK(clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *) &du)); \
110 CL_CHECK(clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *) &u)); \
111 CL_CHECK(clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *) &dr)); \
112 CL_CHECK(clSetKernelArg(kernel, 3, sizeof(cl_mem), (void *) &ds)); \
113 CL_CHECK(clSetKernelArg(kernel, 4, sizeof(cl_mem), (void *) &dt)); \
114 CL_CHECK(clSetKernelArg(kernel, 5, sizeof(cl_mem), (void *) &dx)); \
115 CL_CHECK(clSetKernelArg(kernel, 6, sizeof(cl_mem), (void *) &dy)); \
116 CL_CHECK(clSetKernelArg(kernel, 7, sizeof(cl_mem), (void *) &dz)); \
117 CL_CHECK(clSetKernelArg(kernel, 8, sizeof(cl_mem), (void *) &jacinv)); \
118 \
119 CL_CHECK(clEnqueueNDRangeKernel((cl_command_queue) QUEUE, \
120 kernel, 2, NULL, global_kstep, \
121 local_kstep, 0, NULL, EVENT)); \
122 }
123
124#define CASE(LX) \
125 case LX: \
126 if(autotune_dudxyz[LX] == 0 ) { \
127 char *env_value = NULL; \
128 char neko_log_buf[80]; \
129 env_value = getenv("NEKO_AUTOTUNE"); \
130 \
131 sprintf(neko_log_buf, "Autotune dudxyz (lx: %d)", *lx); \
132 log_section(neko_log_buf); \
133 if(env_value) { \
134 if( !strcmp(env_value,"1D") ) { \
135 CASE_1D(LX, glb_cmd_queue, NULL); \
136 sprintf(neko_log_buf,"Set by env : 1 (1D)"); \
137 log_message(neko_log_buf); \
138 autotune_dudxyz[LX] = 1; \
139 } else if( !strcmp(env_value,"KSTEP") ) { \
140 CASE_KSTEP(LX, glb_cmd_queue, NULL); \
141 sprintf(neko_log_buf,"Set by env : 2 (KSTEP)"); \
142 log_message(neko_log_buf); \
143 autotune_dudxyz[LX] = 2; \
144 } else { \
145 sprintf(neko_log_buf, "Invalid value set for NEKO_AUTOTUNE"); \
146 log_error(neko_log_buf); \
147 } \
148 } \
149 else { \
150 CL_CHECK(clFinish(glb_cmd_queue)); \
151 cl_event perf_event, sync_event; \
152 cl_ulong start, end; \
153 CL_CHECK(clEnqueueMarker(glb_cmd_queue, &sync_event)); \
154 CL_CHECK(clEnqueueBarrier(prf_cmd_queue)); \
155 CL_CHECK(clEnqueueWaitForEvents(prf_cmd_queue, 1, &sync_event)); \
156 \
157 double elapsed1 = 0.0; \
158 for(int i = 0; i < 100; i++) { \
159 CASE_1D(LX, prf_cmd_queue, &perf_event); \
160 CL_CHECK(clWaitForEvents(1, &perf_event)); \
161 CL_CHECK(clGetEventProfilingInfo(perf_event, \
162 CL_PROFILING_COMMAND_START, \
163 sizeof(cl_ulong), &start, NULL)); \
164 CL_CHECK(clGetEventProfilingInfo(perf_event, \
165 CL_PROFILING_COMMAND_END, \
166 sizeof(cl_ulong), &end, NULL)); \
167 elapsed1 += (end - start)*1.0e-6; \
168 } \
169 \
170 double elapsed2 = 0.0; \
171 for(int i = 0; i < 100; i++) { \
172 CASE_KSTEP(LX, prf_cmd_queue, &perf_event); \
173 CL_CHECK(clWaitForEvents(1, &perf_event)); \
174 CL_CHECK(clGetEventProfilingInfo(perf_event, \
175 CL_PROFILING_COMMAND_START, \
176 sizeof(cl_ulong), &start, NULL)); \
177 CL_CHECK(clGetEventProfilingInfo(perf_event, \
178 CL_PROFILING_COMMAND_END, \
179 sizeof(cl_ulong), &end, NULL)); \
180 elapsed2 += (end - start)*1.0e-6; \
181 } \
182 \
183 CL_CHECK(clFinish(prf_cmd_queue)); \
184 CL_CHECK(clEnqueueMarker(prf_cmd_queue, &sync_event)); \
185 int krnl_strtgy = (elapsed1 < elapsed2 ? 1 : 2); \
186 sprintf(neko_log_buf, "Chose : %d (%s)", krnl_strtgy, \
187 (krnl_strtgy > 1 ? "KSTEP" : "1D")); \
188 autotune_dudxyz[LX] = krnl_strtgy; \
189 log_message(neko_log_buf); \
190 clEnqueueBarrier(glb_cmd_queue); \
191 clEnqueueWaitForEvents(glb_cmd_queue, 1, &sync_event) ; \
192 } \
193 log_end_section(); \
194 } else if (autotune_dudxyz[LX] == 1 ) { \
195 CASE_1D(LX, glb_cmd_queue, NULL); \
196 } else if (autotune_dudxyz[LX] == 2 ) { \
197 CASE_KSTEP(LX, glb_cmd_queue, NULL); \
198 } \
199 break
200
201#define CASE_LARGE(LX) \
202 case LX: \
203 CASE_KSTEP(LX, glb_cmd_queue, NULL); \
204 break
205
206 if ((*lx) < 12) {
207 switch(*lx) {
208 CASE(2);
209 CASE(3);
210 CASE(4);
211 CASE(5);
212 CASE(6);
213 CASE(7);
214 CASE(8);
215 CASE(9);
216 CASE(10);
217 CASE(11);
218 default:
219 {
220 fprintf(stderr, __FILE__ ": size not supported: %d\n", *lx);
221 exit(1);
222 }
223 }
224 }
225 else {
226 switch(*lx) {
227 CASE_LARGE(12);
228 CASE_LARGE(13);
229 CASE_LARGE(14);
230 CASE_LARGE(15);
231 CASE_LARGE(16);
232 default:
233 {
234 fprintf(stderr, __FILE__ ": size not supported: %d\n", *lx);
235 exit(1);
236 }
237 }
238 }
239}
__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
__global__ void dirichlet_apply_scalar_kernel(const int *__restrict__ msk, T *__restrict__ x, const T g, const int m)
void opencl_kernel_jit(const char *kernel, cl_program *program)
Definition jit.c:50
#define CASE(LX)
#define CASE_LARGE(LX)
void opencl_dudxyz(void *du, void *u, void *dr, void *ds, void *dt, void *dx, void *dy, void *dz, void *jacinv, int *nel, int *lx)
Definition opr_dudxyz.c:57
int * autotune_dudxyz
Definition opr_dudxyz.c:52
void * dudxyz_program