Neko 1.99.1
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
tensor.c
Go to the documentation of this file.
1/*
2 Copyright (c) 2022-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 <stdio.h>
43#include <device/opencl/jit.h>
45#include <device/opencl/check.h>
46
47#include "tensor_kernel.cl.h"
48
49#define MAX(a,b) (((a)>(b))?(a):(b))
50
51void opencl_tnsr3d(void *v, int *nv, void *u, int *nu,
52 void *A, void *Bt, void *Ct, int *nel) {
53 cl_int err;
54 const int n = MAX(*nu, *nv);
55
56 if (tensor_program == NULL)
58
59 const size_t global_item_size = 256 * (*nel);
60 const size_t local_item_size = 256;
61
62#define STR(X) #X
63#define CASE(N) \
64 case N: \
65 { \
66 cl_kernel kernel = clCreateKernel(tensor_program, \
67 STR(tnsr3d_kernel_n##N), &err); \
68 CL_CHECK(err); \
69 \
70 CL_CHECK(clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *) &v)); \
71 CL_CHECK(clSetKernelArg(kernel, 1, sizeof(int), nv)); \
72 CL_CHECK(clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *) &u)); \
73 CL_CHECK(clSetKernelArg(kernel, 3, sizeof(int), nu)); \
74 CL_CHECK(clSetKernelArg(kernel, 4, sizeof(cl_mem), (void *) &A)); \
75 CL_CHECK(clSetKernelArg(kernel, 5, sizeof(cl_mem), (void *) &Bt)); \
76 CL_CHECK(clSetKernelArg(kernel, 6, sizeof(cl_mem), (void *) &Ct)); \
77 \
78 CL_CHECK(clEnqueueNDRangeKernel((cl_command_queue) glb_cmd_queue, kernel,\
79 1, NULL, &global_item_size, \
80 &local_item_size, 0, NULL, NULL)); \
81 } \
82 break
83
84 switch(n) {
85 CASE(2);
86 CASE(3);
87 CASE(4);
88 CASE(5);
89 CASE(6);
90 CASE(7);
91 CASE(8);
92 CASE(9);
93 CASE(10);
94 CASE(11);
95 CASE(12);
96 CASE(13);
97 CASE(14);
98 }
99}
100
101void opencl_tnsr3d_el_list(void *v, int *nv, void *u, int *nu,
102 void *A, void *Bt, void *Ct, int *elements,
103 int *n_points) {
104 cl_int err;
105 const int n = MAX(*nu, *nv);
106
107 if (tensor_program == NULL)
109
110 const size_t global_item_size = 256 * (*n_points);
111 const size_t local_item_size = 256;
112
113#define STR(X) #X
114#define CASE(N) \
115 case N: \
116 { \
117 cl_kernel kernel = clCreateKernel(tensor_program, \
118 STR(tnsr3d_el_kernel_n##N), &err); \
119 CL_CHECK(err); \
120 \
121 CL_CHECK(clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *) &v)); \
122 CL_CHECK(clSetKernelArg(kernel, 1, sizeof(int), nv)); \
123 CL_CHECK(clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *) &u)); \
124 CL_CHECK(clSetKernelArg(kernel, 3, sizeof(int), nu)); \
125 CL_CHECK(clSetKernelArg(kernel, 4, sizeof(cl_mem), (void *) &A)); \
126 CL_CHECK(clSetKernelArg(kernel, 5, sizeof(cl_mem), (void *) &Bt)); \
127 CL_CHECK(clSetKernelArg(kernel, 6, sizeof(cl_mem), (void *) &Ct)); \
128 CL_CHECK(clSetKernelArg(kernel, 7, sizeof(cl_mem), (void *) &elements)); \
129 CL_CHECK(clSetKernelArg(kernel, 8, sizeof(int), n_points)); \
130 \
131 CL_CHECK(clEnqueueNDRangeKernel((cl_command_queue) glb_cmd_queue, kernel,\
132 1, NULL, &global_item_size, \
133 &local_item_size, 0, NULL, NULL)); \
134 } \
135 break
136
137 switch(n) {
138 CASE(2);
139 CASE(3);
140 CASE(4);
141 CASE(5);
142 CASE(6);
143 CASE(7);
144 CASE(8);
145 CASE(9);
146 CASE(10);
147 CASE(11);
148 CASE(12);
149 CASE(13);
150 CASE(14);
151 }
152}
__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 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
void * tensor_program
void opencl_tnsr3d(void *v, int *nv, void *u, int *nu, void *A, void *Bt, void *Ct, int *nel)
Definition tensor.c:51
#define CASE(N)
void opencl_tnsr3d_el_list(void *v, int *nv, void *u, int *nu, void *A, void *Bt, void *Ct, int *elements, int *n_points)
Definition tensor.c:101
#define MAX(a, b)
Definition tensor.c:49