Neko  0.9.0
A portable framework for high-order spectral element flow simulations
rhs_maker.c
Go to the documentation of this file.
1 /*
2  Copyright (c) 2022, 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 <stdio.h>
42 #include <device/device_config.h>
43 #include <device/opencl/jit.h>
44 #include <device/opencl/prgm_lib.h>
45 #include <device/opencl/check.h>
46 
47 #include "rhs_maker_kernel.cl.h"
48 
49 void rhs_maker_sumab_opencl(void *u, void *v, void *w,
50  void *uu, void *vv, void *ww,
51  void *ulag1, void *ulag2, void *vlag1,
52  void *vlag2, void *wlag1, void *wlag2,
53  real *ext1, real *ext2, real *ext3, int *nab, int *n) {
54  cl_int err;
55 
56  if (rhs_maker_program == NULL)
57  opencl_kernel_jit(rhs_maker_kernel, (cl_program *) &rhs_maker_program);
58 
59  cl_kernel kernel = clCreateKernel(rhs_maker_program, "sumab_kernel", &err);
60  CL_CHECK(err);
61 
62  CL_CHECK(clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *) &u));
63  CL_CHECK(clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *) &v));
64  CL_CHECK(clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *) &w));
65  CL_CHECK(clSetKernelArg(kernel, 3, sizeof(cl_mem), (void *) &uu));
66  CL_CHECK(clSetKernelArg(kernel, 4, sizeof(cl_mem), (void *) &vv));
67  CL_CHECK(clSetKernelArg(kernel, 5, sizeof(cl_mem), (void *) &ww));
68  CL_CHECK(clSetKernelArg(kernel, 6, sizeof(cl_mem), (void *) &ulag1));
69  CL_CHECK(clSetKernelArg(kernel, 7, sizeof(cl_mem), (void *) &ulag2));
70  CL_CHECK(clSetKernelArg(kernel, 8, sizeof(cl_mem), (void *) &vlag1));
71  CL_CHECK(clSetKernelArg(kernel, 9, sizeof(cl_mem), (void *) &vlag2));
72  CL_CHECK(clSetKernelArg(kernel, 10, sizeof(cl_mem), (void *) &wlag1));
73  CL_CHECK(clSetKernelArg(kernel, 11, sizeof(cl_mem), (void *) &wlag2));
74  CL_CHECK(clSetKernelArg(kernel, 12, sizeof(real), ext1));
75  CL_CHECK(clSetKernelArg(kernel, 13, sizeof(real), ext2));
76  CL_CHECK(clSetKernelArg(kernel, 14, sizeof(real), ext3));
77  CL_CHECK(clSetKernelArg(kernel, 15, sizeof(int), nab));
78  CL_CHECK(clSetKernelArg(kernel, 16, sizeof(int), n));
79 
80  const int nb = ((*n) + 256 - 1) / 256;
81  const size_t global_item_size = 256 * nb;
82  const size_t local_item_size = 256;
83 
84  CL_CHECK(clEnqueueNDRangeKernel((cl_command_queue) glb_cmd_queue, kernel, 1,
85  NULL, &global_item_size, &local_item_size,
86  0, NULL, NULL));
87 }
88 
89 void rhs_maker_ext_opencl(void *abx1, void *aby1, void *abz1,
90  void *abx2, void *aby2, void *abz2,
91  void *bfx, void *bfy, void *bfz,
92  real *rho, real *ext1, real *ext2, real *ext3, int *n) {
93  cl_int err;
94 
95  if (rhs_maker_program == NULL)
96  opencl_kernel_jit(rhs_maker_kernel, (cl_program *) &rhs_maker_program);
97 
98  cl_kernel kernel = clCreateKernel(rhs_maker_program, "makeext_kernel", &err);
99  CL_CHECK(err);
100 
101  CL_CHECK(clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *) &abx1));
102  CL_CHECK(clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *) &aby1));
103  CL_CHECK(clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *) &abz1));
104  CL_CHECK(clSetKernelArg(kernel, 3, sizeof(cl_mem), (void *) &abx2));
105  CL_CHECK(clSetKernelArg(kernel, 4, sizeof(cl_mem), (void *) &aby2));
106  CL_CHECK(clSetKernelArg(kernel, 5, sizeof(cl_mem), (void *) &abz2));
107  CL_CHECK(clSetKernelArg(kernel, 6, sizeof(cl_mem), (void *) &bfx));
108  CL_CHECK(clSetKernelArg(kernel, 7, sizeof(cl_mem), (void *) &bfy));
109  CL_CHECK(clSetKernelArg(kernel, 8, sizeof(cl_mem), (void *) &bfz));
110  CL_CHECK(clSetKernelArg(kernel, 9, sizeof(real), rho));
111  CL_CHECK(clSetKernelArg(kernel, 10, sizeof(real), ext1));
112  CL_CHECK(clSetKernelArg(kernel, 11, sizeof(real), ext2));
113  CL_CHECK(clSetKernelArg(kernel, 12, sizeof(real), ext3));
114  CL_CHECK(clSetKernelArg(kernel, 13, sizeof(int), n));
115 
116  const int nb = ((*n) + 256 - 1) / 256;
117  const size_t global_item_size = 256 * nb;
118  const size_t local_item_size = 256;
119 
120  CL_CHECK(clEnqueueNDRangeKernel((cl_command_queue) glb_cmd_queue, kernel, 1,
121  NULL, &global_item_size, &local_item_size,
122  0, NULL, NULL));
123 
124 }
125 
126 void scalar_rhs_maker_ext_opencl(void *fs_lag, void *fs_laglag, void *fs,
127  real *rho, real *ext1, real *ext2,
128  real *ext3, int *n) {
129  cl_int err;
130 
131  if (rhs_maker_program == NULL)
132  opencl_kernel_jit(rhs_maker_kernel, (cl_program *) &rhs_maker_program);
133 
134  cl_kernel kernel = clCreateKernel(rhs_maker_program, "scalar_makeext_kernel", &err);
135  CL_CHECK(err);
136 
137  CL_CHECK(clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *) &fs_lag));
138  CL_CHECK(clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *) &fs_laglag));
139  CL_CHECK(clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *) &fs));
140  CL_CHECK(clSetKernelArg(kernel, 3, sizeof(real), rho));
141  CL_CHECK(clSetKernelArg(kernel, 4, sizeof(real), ext1));
142  CL_CHECK(clSetKernelArg(kernel, 5, sizeof(real), ext2));
143  CL_CHECK(clSetKernelArg(kernel, 6, sizeof(real), ext3));
144  CL_CHECK(clSetKernelArg(kernel, 7, sizeof(int), n));
145 
146  const int nb = ((*n) + 256 - 1) / 256;
147  const size_t global_item_size = 256 * nb;
148  const size_t local_item_size = 256;
149 
150  CL_CHECK(clEnqueueNDRangeKernel((cl_command_queue) glb_cmd_queue, kernel, 1,
151  NULL, &global_item_size, &local_item_size,
152  0, NULL, NULL));
153 
154 }
155 
156 void rhs_maker_bdf_opencl(void *ulag1, void *ulag2, void *vlag1,
157  void *vlag2, void *wlag1, void *wlag2,
158  void *bfx, void *bfy, void *bfz,
159  void *u, void *v, void *w, void *B,
160  real *rho, real *dt, real *bd2,
161  real *bd3, real *bd4, int *nbd, int *n) {
162  cl_int err;
163 
164  if (rhs_maker_program == NULL)
165  opencl_kernel_jit(rhs_maker_kernel, (cl_program *) &rhs_maker_program);
166 
167  cl_kernel kernel = clCreateKernel(rhs_maker_program, "makebdf_kernel", &err);
168  CL_CHECK(err);
169 
170  CL_CHECK(clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *) &ulag1));
171  CL_CHECK(clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *) &ulag2));
172  CL_CHECK(clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *) &vlag1));
173  CL_CHECK(clSetKernelArg(kernel, 3, sizeof(cl_mem), (void *) &vlag2));
174  CL_CHECK(clSetKernelArg(kernel, 4, sizeof(cl_mem), (void *) &wlag1));
175  CL_CHECK(clSetKernelArg(kernel, 5, sizeof(cl_mem), (void *) &wlag2));
176  CL_CHECK(clSetKernelArg(kernel, 6, sizeof(cl_mem), (void *) &bfx));
177  CL_CHECK(clSetKernelArg(kernel, 7, sizeof(cl_mem), (void *) &bfy));
178  CL_CHECK(clSetKernelArg(kernel, 8, sizeof(cl_mem), (void *) &bfz));
179  CL_CHECK(clSetKernelArg(kernel, 9, sizeof(cl_mem), (void *) &u));
180  CL_CHECK(clSetKernelArg(kernel, 10, sizeof(cl_mem), (void *) &v));
181  CL_CHECK(clSetKernelArg(kernel, 11, sizeof(cl_mem), (void *) &w));
182  CL_CHECK(clSetKernelArg(kernel, 12, sizeof(cl_mem), (void *) &B));
183  CL_CHECK(clSetKernelArg(kernel, 13, sizeof(real), rho));
184  CL_CHECK(clSetKernelArg(kernel, 14, sizeof(real), dt));
185  CL_CHECK(clSetKernelArg(kernel, 15, sizeof(real), bd2));
186  CL_CHECK(clSetKernelArg(kernel, 16, sizeof(real), bd3));
187  CL_CHECK(clSetKernelArg(kernel, 17, sizeof(real), bd4));
188  CL_CHECK(clSetKernelArg(kernel, 18, sizeof(int), nbd));
189  CL_CHECK(clSetKernelArg(kernel, 19, sizeof(int), n));
190 
191  const int nb = ((*n) + 256 - 1) / 256;
192  const size_t global_item_size = 256 * nb;
193  const size_t local_item_size = 256;
194 
195  CL_CHECK(clEnqueueNDRangeKernel((cl_command_queue) glb_cmd_queue, kernel, 1,
196  NULL, &global_item_size, &local_item_size,
197  0, NULL, NULL));
198 
199 }
200 
201 void scalar_rhs_maker_bdf_opencl(void *s_lag, void *s_laglag, void *fs,
202  void *s, void *B, real *rho, real *dt,
203  real *bd2, real *bd3, real *bd4,
204  int *nbd, int *n) {
205  cl_int err;
206 
207  if (rhs_maker_program == NULL)
208  opencl_kernel_jit(rhs_maker_kernel, (cl_program *) &rhs_maker_program);
209 
210  cl_kernel kernel = clCreateKernel(rhs_maker_program, "scalar_makebdf_kernel", &err);
211  CL_CHECK(err);
212 
213  CL_CHECK(clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *) &s_lag));
214  CL_CHECK(clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *) &s_laglag));
215  CL_CHECK(clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *) &fs));
216  CL_CHECK(clSetKernelArg(kernel, 3, sizeof(cl_mem), (void *) &s));
217  CL_CHECK(clSetKernelArg(kernel, 4, sizeof(cl_mem), (void *) &B));
218  CL_CHECK(clSetKernelArg(kernel, 5, sizeof(real), rho));
219  CL_CHECK(clSetKernelArg(kernel, 6, sizeof(real), dt));
220  CL_CHECK(clSetKernelArg(kernel, 7, sizeof(real), bd2));
221  CL_CHECK(clSetKernelArg(kernel, 8, sizeof(real), bd3));
222  CL_CHECK(clSetKernelArg(kernel, 9, sizeof(real), bd4));
223  CL_CHECK(clSetKernelArg(kernel, 10, sizeof(int), nbd));
224  CL_CHECK(clSetKernelArg(kernel, 11, sizeof(int), n));
225 
226  const int nb = ((*n) + 256 - 1) / 256;
227  const size_t global_item_size = 256 * nb;
228  const size_t local_item_size = 256;
229 
230  CL_CHECK(clEnqueueNDRangeKernel((cl_command_queue) glb_cmd_queue, kernel, 1,
231  NULL, &global_item_size, &local_item_size,
232  0, NULL, NULL));
233 
234 }
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ w
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ u
__global__ void T *__restrict__ T *__restrict__ const T *__restrict__ const T *__restrict__ v
__global__ void const T *__restrict__ const T *__restrict__ const T *__restrict__ const T *__restrict__ dt
Definition: cdtp_kernel.h:109
double real
Definition: device_config.h:12
void * glb_cmd_queue
void opencl_kernel_jit(const char *kernel, cl_program *program)
Definition: jit.c:50
#define CL_CHECK(err)
Definition: check.h:12
void * rhs_maker_program
void scalar_rhs_maker_bdf_opencl(void *s_lag, void *s_laglag, void *fs, void *s, void *B, real *rho, real *dt, real *bd2, real *bd3, real *bd4, int *nbd, int *n)
Definition: rhs_maker.c:201
void rhs_maker_sumab_opencl(void *u, void *v, void *w, void *uu, void *vv, void *ww, void *ulag1, void *ulag2, void *vlag1, void *vlag2, void *wlag1, void *wlag2, real *ext1, real *ext2, real *ext3, int *nab, int *n)
Definition: rhs_maker.c:49
void rhs_maker_ext_opencl(void *abx1, void *aby1, void *abz1, void *abx2, void *aby2, void *abz2, void *bfx, void *bfy, void *bfz, real *rho, real *ext1, real *ext2, real *ext3, int *n)
Definition: rhs_maker.c:89
void scalar_rhs_maker_ext_opencl(void *fs_lag, void *fs_laglag, void *fs, real *rho, real *ext1, real *ext2, real *ext3, int *n)
Definition: rhs_maker.c:126
void rhs_maker_bdf_opencl(void *ulag1, void *ulag2, void *vlag1, void *vlag2, void *wlag1, void *wlag2, void *bfx, void *bfy, void *bfz, void *u, void *v, void *w, void *B, real *rho, real *dt, real *bd2, real *bd3, real *bd4, int *nbd, int *n)
Definition: rhs_maker.c:156