Neko 1.99.2
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 CL_CHECK(clReleaseKernel(kernel)); \
82 } \
83 break
84
85 switch(n) {
86 CASE(2);
87 CASE(3);
88 CASE(4);
89 CASE(5);
90 CASE(6);
91 CASE(7);
92 CASE(8);
93 CASE(9);
94 CASE(10);
95 CASE(11);
96 CASE(12);
97 CASE(13);
98 CASE(14);
99 }
100}
101
102void opencl_tnsr3d_el_list(void *v, int *nv, void *u, int *nu,
103 void *A, void *Bt, void *Ct, int *elements,
104 int *n_points) {
105 cl_int err;
106 const int n = MAX(*nu, *nv);
107
108 if (tensor_program == NULL)
110
111 const size_t global_item_size = 256 * (*n_points);
112 const size_t local_item_size = 256;
113
114#define STR(X) #X
115#define CASE(N) \
116 case N: \
117 { \
118 cl_kernel kernel = clCreateKernel(tensor_program, \
119 STR(tnsr3d_el_kernel_n##N), &err); \
120 CL_CHECK(err); \
121 \
122 CL_CHECK(clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *) &v)); \
123 CL_CHECK(clSetKernelArg(kernel, 1, sizeof(int), nv)); \
124 CL_CHECK(clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *) &u)); \
125 CL_CHECK(clSetKernelArg(kernel, 3, sizeof(int), nu)); \
126 CL_CHECK(clSetKernelArg(kernel, 4, sizeof(cl_mem), (void *) &A)); \
127 CL_CHECK(clSetKernelArg(kernel, 5, sizeof(cl_mem), (void *) &Bt)); \
128 CL_CHECK(clSetKernelArg(kernel, 6, sizeof(cl_mem), (void *) &Ct)); \
129 CL_CHECK(clSetKernelArg(kernel, 7, sizeof(cl_mem), (void *) &elements)); \
130 CL_CHECK(clSetKernelArg(kernel, 8, sizeof(int), n_points)); \
131 \
132 CL_CHECK(clEnqueueNDRangeKernel((cl_command_queue) glb_cmd_queue, kernel,\
133 1, NULL, &global_item_size, \
134 &local_item_size, 0, NULL, NULL)); \
135 CL_CHECK(clReleaseKernel(kernel)); \
136 } \
137 break
138
139 switch(n) {
140 CASE(2);
141 CASE(3);
142 CASE(4);
143 CASE(5);
144 CASE(6);
145 CASE(7);
146 CASE(8);
147 CASE(9);
148 CASE(10);
149 CASE(11);
150 CASE(12);
151 CASE(13);
152 CASE(14);
153 }
154}
__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:102
#define MAX(a, b)
Definition tensor.c:49