Neko  0.9.99
A portable framework for high-order spectral element flow simulations
mathops.c
Go to the documentation of this file.
1 /*
2  Copyright (c) 2021-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 "mathops_kernel.cl.h"
48 
50 void opencl_opchsign(void *a1, void *a2, void *a3, int *gdim, int *n) {
51  cl_int err;
52 
53  if (mathops_program == NULL)
54  opencl_kernel_jit(mathops_kernel, (cl_program *) &mathops_program);
55 
56  cl_kernel kernel = clCreateKernel(mathops_program, "opchsign_kernel", &err);
57  CL_CHECK(err);
58 
59  CL_CHECK(clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *) &a1));
60  CL_CHECK(clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *) &a2));
61  CL_CHECK(clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *) &a3));
62  CL_CHECK(clSetKernelArg(kernel, 3, sizeof(int), gdim));
63  CL_CHECK(clSetKernelArg(kernel, 4, sizeof(int), n));
64 
65  const int nb = ((*n) + 256 - 1) / 256;
66  const size_t global_item_size = 256 * nb;
67  const size_t local_item_size = 256;
68 
69  CL_CHECK(clEnqueueNDRangeKernel((cl_command_queue) glb_cmd_queue, kernel, 1,
70  NULL, &global_item_size, &local_item_size,
71  0, NULL, NULL));
72 }
73 
75 void opencl_opcolv(void *a1, void *a2, void *a3, void *c, int *gdim, int *n) {
76  cl_int err;
77 
78  if (mathops_program == NULL)
79  opencl_kernel_jit(mathops_kernel, (cl_program *) &mathops_program);
80 
81  cl_kernel kernel = clCreateKernel(mathops_program, "opcolv_kernel", &err);
82  CL_CHECK(err);
83 
84  CL_CHECK(clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *) &a1));
85  CL_CHECK(clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *) &a2));
86  CL_CHECK(clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *) &a3));
87  CL_CHECK(clSetKernelArg(kernel, 3, sizeof(cl_mem), (void *) &c));
88  CL_CHECK(clSetKernelArg(kernel, 4, sizeof(int), gdim));
89  CL_CHECK(clSetKernelArg(kernel, 5, sizeof(int), n));
90 
91  const int nb = ((*n) + 256 - 1) / 256;
92  const size_t global_item_size = 256 * nb;
93  const size_t local_item_size = 256;
94 
95  CL_CHECK(clEnqueueNDRangeKernel((cl_command_queue) glb_cmd_queue, kernel, 1,
96  NULL, &global_item_size, &local_item_size,
97  0, NULL, NULL));
98 }
99 
101 void opencl_opcolv3c(void *a1, void *a2, void *a3,
102  void *b1, void *b2, void *b3,
103  void *c, real *d, int *gdim, int *n) {
104  cl_int err;
105 
106  if (mathops_program == NULL)
107  opencl_kernel_jit(mathops_kernel, (cl_program *) &mathops_program);
108 
109  cl_kernel kernel = clCreateKernel(mathops_program, "opcolv3c_kernel", &err);
110  CL_CHECK(err);
111 
112  CL_CHECK(clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *) &a1));
113  CL_CHECK(clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *) &a2));
114  CL_CHECK(clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *) &a3));
115  CL_CHECK(clSetKernelArg(kernel, 3, sizeof(cl_mem), (void *) &b1));
116  CL_CHECK(clSetKernelArg(kernel, 4, sizeof(cl_mem), (void *) &b2));
117  CL_CHECK(clSetKernelArg(kernel, 5, sizeof(cl_mem), (void *) &b3));
118  CL_CHECK(clSetKernelArg(kernel, 6, sizeof(cl_mem), (void *) &c));
119  CL_CHECK(clSetKernelArg(kernel, 7, sizeof(real), d));
120  CL_CHECK(clSetKernelArg(kernel, 8, sizeof(int), gdim));
121  CL_CHECK(clSetKernelArg(kernel, 9, sizeof(int), n));
122 
123  const int nb = ((*n) + 256 - 1) / 256;
124  const size_t global_item_size = 256 * nb;
125  const size_t local_item_size = 256;
126 
127  CL_CHECK(clEnqueueNDRangeKernel((cl_command_queue) glb_cmd_queue, kernel, 1,
128  NULL, &global_item_size, &local_item_size,
129  0, NULL, NULL));
130 }
131 
133 void opencl_opadd2cm(void *a1, void *a2, void *a3,
134  void *b1, void *b2, void *b3,
135  real *c, int *gdim, int *n) {
136  cl_int err;
137 
138  if (mathops_program == NULL)
139  opencl_kernel_jit(mathops_kernel, (cl_program *) &mathops_program);
140 
141  cl_kernel kernel = clCreateKernel(mathops_program, "opadd2cm_kernel", &err);
142  CL_CHECK(err);
143 
144  CL_CHECK(clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *) &a1));
145  CL_CHECK(clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *) &a2));
146  CL_CHECK(clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *) &a3));
147  CL_CHECK(clSetKernelArg(kernel, 3, sizeof(cl_mem), (void *) &b1));
148  CL_CHECK(clSetKernelArg(kernel, 4, sizeof(cl_mem), (void *) &b2));
149  CL_CHECK(clSetKernelArg(kernel, 5, sizeof(cl_mem), (void *) &b3));
150  CL_CHECK(clSetKernelArg(kernel, 6, sizeof(real), c));
151  CL_CHECK(clSetKernelArg(kernel, 7, sizeof(int), gdim));
152  CL_CHECK(clSetKernelArg(kernel, 8, sizeof(int), n));
153 
154  const int nb = ((*n) + 256 - 1) / 256;
155  const size_t global_item_size = 256 * nb;
156  const size_t local_item_size = 256;
157 
158  CL_CHECK(clEnqueueNDRangeKernel((cl_command_queue) glb_cmd_queue, kernel, 1,
159  NULL, &global_item_size, &local_item_size,
160  0, NULL, NULL));
161 }
162 
164 void opencl_opadd2col(void *a1, void *a2, void *a3,
165  void *b1, void *b2, void *b3,
166  void *c, int *gdim, int *n) {
167  cl_int err;
168 
169  if (mathops_program == NULL)
170  opencl_kernel_jit(mathops_kernel, (cl_program *) &mathops_program);
171 
172  cl_kernel kernel = clCreateKernel(mathops_program, "opadd2col_kernel", &err);
173  CL_CHECK(err);
174 
175  CL_CHECK(clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *) &a1));
176  CL_CHECK(clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *) &a2));
177  CL_CHECK(clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *) &a3));
178  CL_CHECK(clSetKernelArg(kernel, 3, sizeof(cl_mem), (void *) &b1));
179  CL_CHECK(clSetKernelArg(kernel, 4, sizeof(cl_mem), (void *) &b2));
180  CL_CHECK(clSetKernelArg(kernel, 5, sizeof(cl_mem), (void *) &b3));
181  CL_CHECK(clSetKernelArg(kernel, 6, sizeof(cl_mem), (void *) &c));
182  CL_CHECK(clSetKernelArg(kernel, 7, sizeof(int), gdim));
183  CL_CHECK(clSetKernelArg(kernel, 8, sizeof(int), n));
184 
185  const int nb = ((*n) + 256 - 1) / 256;
186  const size_t global_item_size = 256 * nb;
187  const size_t local_item_size = 256;
188 
189  CL_CHECK(clEnqueueNDRangeKernel((cl_command_queue) glb_cmd_queue, kernel, 1,
190  NULL, &global_item_size, &local_item_size,
191  0, NULL, NULL));
192 }
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
void opencl_opcolv3c(void *a1, void *a2, void *a3, void *b1, void *b2, void *b3, void *c, real *d, int *gdim, int *n)
Definition: mathops.c:101
void opencl_opadd2cm(void *a1, void *a2, void *a3, void *b1, void *b2, void *b3, real *c, int *gdim, int *n)
Definition: mathops.c:133
void opencl_opcolv(void *a1, void *a2, void *a3, void *c, int *gdim, int *n)
Definition: mathops.c:75
void opencl_opchsign(void *a1, void *a2, void *a3, int *gdim, int *n)
Definition: mathops.c:50
void opencl_opadd2col(void *a1, void *a2, void *a3, void *b1, void *b2, void *b3, void *c, int *gdim, int *n)
Definition: mathops.c:164
#define CL_CHECK(err)
Definition: check.h:12
void * mathops_program