Neko 1.99.2
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
opr_conv1.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 "conv1_kernel.cl.h"
51
53
57void opencl_conv1(void *du, void *u,
58 void *vx, void *vy, void *vz,
59 void *dx, void *dy, void *dz,
60 void *drdx, void *dsdx, void *dtdx,
61 void *drdy, void *dsdy, void *dtdy,
62 void *drdz, void *dsdz, void *dtdz,
63 void *jacinv, int *nel, int *gdim, int *lx) {
64 cl_int err;
65
66 if (conv1_program == NULL)
68
69 const size_t global_item_size = 256 * (*nel);
70 const size_t local_item_size = 256;
71
72 size_t global_kstep[2];
73 size_t local_kstep[2];
74 local_kstep[0] = (*lx);
75 local_kstep[1] = (*lx);
76 global_kstep[0] = (*nel) * (*lx);
77 global_kstep[1] = (*lx);
78
79 if (autotune_conv1 == NULL) {
80 autotune_conv1 = malloc(17 * sizeof(int));
81 memset(autotune_conv1, 0, 17 * sizeof(int));
82 }
83
84#define STR(X) #X
85#define CASE_1D(LX, QUEUE, EVENT) \
86 { \
87 cl_kernel kernel = clCreateKernel(conv1_program, \
88 STR(conv1_kernel_lx##LX), &err); \
89 CL_CHECK(err); \
90 \
91 CL_CHECK(clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *) &du)); \
92 CL_CHECK(clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *) &u)); \
93 CL_CHECK(clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *) &vx)); \
94 CL_CHECK(clSetKernelArg(kernel, 3, sizeof(cl_mem), (void *) &vy)); \
95 CL_CHECK(clSetKernelArg(kernel, 4, sizeof(cl_mem), (void *) &vz)); \
96 CL_CHECK(clSetKernelArg(kernel, 5, sizeof(cl_mem), (void *) &dx)); \
97 CL_CHECK(clSetKernelArg(kernel, 6, sizeof(cl_mem), (void *) &dy)); \
98 CL_CHECK(clSetKernelArg(kernel, 7, sizeof(cl_mem), (void *) &dz)); \
99 CL_CHECK(clSetKernelArg(kernel, 8, sizeof(cl_mem), (void *) &drdx)); \
100 CL_CHECK(clSetKernelArg(kernel, 9, sizeof(cl_mem), (void *) &dsdx)); \
101 CL_CHECK(clSetKernelArg(kernel, 10, sizeof(cl_mem), (void *) &dtdx)); \
102 CL_CHECK(clSetKernelArg(kernel, 11, sizeof(cl_mem), (void *) &drdy)); \
103 CL_CHECK(clSetKernelArg(kernel, 12, sizeof(cl_mem), (void *) &dsdy)); \
104 CL_CHECK(clSetKernelArg(kernel, 13, sizeof(cl_mem), (void *) &dtdy)); \
105 CL_CHECK(clSetKernelArg(kernel, 14, sizeof(cl_mem), (void *) &drdz)); \
106 CL_CHECK(clSetKernelArg(kernel, 15, sizeof(cl_mem), (void *) &dsdz)); \
107 CL_CHECK(clSetKernelArg(kernel, 16, sizeof(cl_mem), (void *) &dtdz)); \
108 CL_CHECK(clSetKernelArg(kernel, 17, sizeof(cl_mem), (void *) &jacinv)); \
109 \
110 CL_CHECK(clEnqueueNDRangeKernel((cl_command_queue) QUEUE, \
111 kernel, 1, NULL, &global_item_size, \
112 &local_item_size, 0, NULL, EVENT)); \
113 CL_CHECK(clReleaseKernel(kernel)); \
114 }
115
116#define CASE_KSTEP(LX, QUEUE, EVENT) \
117 { \
118 cl_kernel kernel = clCreateKernel(conv1_program, \
119 STR(conv1_kernel_kstep_lx##LX), &err); \
120 CL_CHECK(err); \
121 \
122 CL_CHECK(clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *) &du)); \
123 CL_CHECK(clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *) &u)); \
124 CL_CHECK(clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *) &vx)); \
125 CL_CHECK(clSetKernelArg(kernel, 3, sizeof(cl_mem), (void *) &vy)); \
126 CL_CHECK(clSetKernelArg(kernel, 4, sizeof(cl_mem), (void *) &vz)); \
127 CL_CHECK(clSetKernelArg(kernel, 5, sizeof(cl_mem), (void *) &dx)); \
128 CL_CHECK(clSetKernelArg(kernel, 6, sizeof(cl_mem), (void *) &dy)); \
129 CL_CHECK(clSetKernelArg(kernel, 7, sizeof(cl_mem), (void *) &dz)); \
130 CL_CHECK(clSetKernelArg(kernel, 8, sizeof(cl_mem), (void *) &drdx)); \
131 CL_CHECK(clSetKernelArg(kernel, 9, sizeof(cl_mem), (void *) &dsdx)); \
132 CL_CHECK(clSetKernelArg(kernel, 10, sizeof(cl_mem), (void *) &dtdx)); \
133 CL_CHECK(clSetKernelArg(kernel, 11, sizeof(cl_mem), (void *) &drdy)); \
134 CL_CHECK(clSetKernelArg(kernel, 12, sizeof(cl_mem), (void *) &dsdy)); \
135 CL_CHECK(clSetKernelArg(kernel, 13, sizeof(cl_mem), (void *) &dtdy)); \
136 CL_CHECK(clSetKernelArg(kernel, 14, sizeof(cl_mem), (void *) &drdz)); \
137 CL_CHECK(clSetKernelArg(kernel, 15, sizeof(cl_mem), (void *) &dsdz)); \
138 CL_CHECK(clSetKernelArg(kernel, 16, sizeof(cl_mem), (void *) &dtdz)); \
139 CL_CHECK(clSetKernelArg(kernel, 17, sizeof(cl_mem), (void *) &jacinv)); \
140 \
141 CL_CHECK(clEnqueueNDRangeKernel((cl_command_queue) QUEUE, \
142 kernel, 2, NULL, global_kstep, \
143 local_kstep, 0, NULL, EVENT)); \
144 CL_CHECK(clReleaseKernel(kernel)); \
145 }
146
147
148#define CASE(LX) \
149 case LX: \
150 if(autotune_conv1[LX] == 0 ) { \
151 char *env_value = NULL; \
152 char neko_log_buf[80]; \
153 env_value = getenv("NEKO_AUTOTUNE"); \
154 \
155 sprintf(neko_log_buf, "Autotune conv1 (lx: %d)", *lx); \
156 log_section(neko_log_buf); \
157 if(env_value) { \
158 if( !strcmp(env_value,"1D") ) { \
159 CASE_1D(LX, glb_cmd_queue, NULL); \
160 sprintf(neko_log_buf,"Set by env : 1 (1D)"); \
161 log_message(neko_log_buf); \
162 autotune_conv1[LX] = 1; \
163 } else if( !strcmp(env_value,"KSTEP") ) { \
164 CASE_KSTEP(LX, glb_cmd_queue, NULL); \
165 sprintf(neko_log_buf,"Set by env : 2 (KSTEP)"); \
166 log_message(neko_log_buf); \
167 autotune_conv1[LX] = 2; \
168 } else { \
169 sprintf(neko_log_buf, "Invalid value set for NEKO_AUTOTUNE"); \
170 log_error(neko_log_buf); \
171 } \
172 } \
173 else { \
174 CL_CHECK(clFinish(glb_cmd_queue)); \
175 cl_event perf_event, sync_event; \
176 cl_ulong start, end; \
177 CL_CHECK(clEnqueueMarker(glb_cmd_queue, &sync_event)); \
178 CL_CHECK(clEnqueueBarrier(prf_cmd_queue)); \
179 CL_CHECK(clEnqueueWaitForEvents(prf_cmd_queue, 1, &sync_event)); \
180 \
181 double elapsed1 = 0.0; \
182 for(int i = 0; i < 100; i++) { \
183 CASE_1D(LX, prf_cmd_queue, &perf_event); \
184 CL_CHECK(clWaitForEvents(1, &perf_event)); \
185 CL_CHECK(clGetEventProfilingInfo(perf_event, \
186 CL_PROFILING_COMMAND_START, \
187 sizeof(cl_ulong), &start, NULL)); \
188 CL_CHECK(clGetEventProfilingInfo(perf_event, \
189 CL_PROFILING_COMMAND_END, \
190 sizeof(cl_ulong), &end, NULL)); \
191 elapsed1 += (end - start)*1.0e-6; \
192 } \
193 \
194 double elapsed2 = 0.0; \
195 for(int i = 0; i < 100; i++) { \
196 CASE_KSTEP(LX, prf_cmd_queue, &perf_event); \
197 CL_CHECK(clWaitForEvents(1, &perf_event)); \
198 CL_CHECK(clGetEventProfilingInfo(perf_event, \
199 CL_PROFILING_COMMAND_START, \
200 sizeof(cl_ulong), &start, NULL)); \
201 CL_CHECK(clGetEventProfilingInfo(perf_event, \
202 CL_PROFILING_COMMAND_END, \
203 sizeof(cl_ulong), &end, NULL)); \
204 elapsed2 += (end - start)*1.0e-6; \
205 } \
206 \
207 CL_CHECK(clFinish(prf_cmd_queue)); \
208 CL_CHECK(clEnqueueMarker(prf_cmd_queue, &sync_event)); \
209 int krnl_strtgy = (elapsed1 < elapsed2 ? 1 : 2); \
210 sprintf(neko_log_buf, "Chose : %d (%s)", krnl_strtgy, \
211 (krnl_strtgy > 1 ? "KSTEP" : "1D")); \
212 autotune_conv1[LX] = krnl_strtgy; \
213 log_message(neko_log_buf); \
214 clEnqueueBarrier(glb_cmd_queue); \
215 clEnqueueWaitForEvents(glb_cmd_queue, 1, &sync_event) ; \
216 } \
217 log_end_section(); \
218 } else if (autotune_conv1[LX] == 1 ) { \
219 CASE_1D(LX, glb_cmd_queue, NULL); \
220 } else if (autotune_conv1[LX] == 2 ) { \
221 CASE_KSTEP(LX, glb_cmd_queue, NULL); \
222 } \
223 break
224
225#define CASE_LARGE(LX) \
226 case LX: \
227 CASE_KSTEP(LX, glb_cmd_queue, NULL); \
228 break
229
230
231 if ((*lx) < 12) {
232 switch(*lx) {
233 CASE(2);
234 CASE(3);
235 CASE(4);
236 CASE(5);
237 CASE(6);
238 CASE(7);
239 CASE(8);
240 CASE(9);
241 CASE(10);
242 CASE(11);
243 default:
244 {
245 fprintf(stderr, __FILE__ ": size not supported: %d\n", *lx);
246 exit(1);
247 }
248 }
249 }
250 else {
251 switch(*lx) {
252 CASE_LARGE(12);
253 CASE_LARGE(13);
254 CASE_LARGE(14);
255 CASE_LARGE(15);
256 CASE_LARGE(16);
257 default:
258 {
259 fprintf(stderr, __FILE__ ": size not supported: %d\n", *lx);
260 exit(1);
261 }
262 }
263 }
264}
265
__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__ 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 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__ const T *__restrict__ const T *__restrict__ vz
__global__ void const T *__restrict__ const T *__restrict__ vx
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ vy
__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)
int * autotune_conv1
Definition opr_conv1.c:52
void opencl_conv1(void *du, void *u, void *vx, void *vy, void *vz, 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 *gdim, int *lx)
Definition opr_conv1.c:57
void * conv1_program