xref: /libCEED/backends/sycl-shared/ceed-sycl-shared-basis.sycl.cpp (revision 22070f9510d4ff69aa6119a59aa3c46d57cc1cc7)
1bd882c8aSJames Wright // Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors.
2bd882c8aSJames Wright // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3bd882c8aSJames Wright //
4bd882c8aSJames Wright // SPDX-License-Identifier: BSD-2-Clause
5bd882c8aSJames Wright //
6bd882c8aSJames Wright // This file is part of CEED:  http://github.com/ceed
7bd882c8aSJames Wright 
8bd882c8aSJames Wright #include <ceed/backend.h>
9bd882c8aSJames Wright #include <ceed/ceed.h>
10bd882c8aSJames Wright #include <ceed/jit-tools.h>
11bd882c8aSJames Wright 
12bd882c8aSJames Wright #include <map>
13bd882c8aSJames Wright #include <string_view>
14bd882c8aSJames Wright #include <sycl/sycl.hpp>
15bd882c8aSJames Wright 
16bd882c8aSJames Wright #include "../sycl/ceed-sycl-compile.hpp"
17bd882c8aSJames Wright #include "ceed-sycl-shared.hpp"
18bd882c8aSJames Wright 
19bd882c8aSJames Wright //------------------------------------------------------------------------------
20bd882c8aSJames Wright // Compute the local range of for basis kernels
21bd882c8aSJames Wright //------------------------------------------------------------------------------
22bd882c8aSJames Wright static int ComputeLocalRange(Ceed ceed, CeedInt dim, CeedInt thread_1d, CeedInt *local_range, CeedInt max_group_size = 256) {
23bd882c8aSJames Wright   local_range[0]               = thread_1d;
24bd882c8aSJames Wright   local_range[1]               = (dim > 1) ? thread_1d : 1;
25bd882c8aSJames Wright   const CeedInt min_group_size = local_range[0] * local_range[1];
26dd64fc84SJeremy L Thompson 
27bd882c8aSJames Wright   CeedCheck(min_group_size <= max_group_size, ceed, CEED_ERROR_BACKEND, "Requested group size is smaller than the required minimum.");
28bd882c8aSJames Wright 
29bd882c8aSJames Wright   local_range[2] = max_group_size / min_group_size;  // elements per group
30bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
31bd882c8aSJames Wright }
32bd882c8aSJames Wright 
33bd882c8aSJames Wright //------------------------------------------------------------------------------
34bd882c8aSJames Wright // Apply basis
35bd882c8aSJames Wright //------------------------------------------------------------------------------
36bd882c8aSJames Wright int CeedBasisApplyTensor_Sycl_shared(CeedBasis basis, const CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector u,
37bd882c8aSJames Wright                                      CeedVector v) {
38bd882c8aSJames Wright   Ceed                   ceed;
39bd882c8aSJames Wright   Ceed_Sycl             *ceed_Sycl;
40dd64fc84SJeremy L Thompson   const CeedScalar      *d_u;
41dd64fc84SJeremy L Thompson   CeedScalar            *d_v;
42bd882c8aSJames Wright   CeedBasis_Sycl_shared *impl;
43dd64fc84SJeremy L Thompson 
44dd64fc84SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
45dd64fc84SJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &ceed_Sycl));
46bd882c8aSJames Wright   CeedCallBackend(CeedBasisGetData(basis, &impl));
47bd882c8aSJames Wright 
48bd882c8aSJames Wright   // Read vectors
49bd882c8aSJames Wright   if (eval_mode != CEED_EVAL_WEIGHT) {
50bd882c8aSJames Wright     CeedCallBackend(CeedVectorGetArrayRead(u, CEED_MEM_DEVICE, &d_u));
51bd882c8aSJames Wright   }
52bd882c8aSJames Wright   CeedCallBackend(CeedVectorGetArrayWrite(v, CEED_MEM_DEVICE, &d_v));
53bd882c8aSJames Wright 
54bd882c8aSJames Wright   // Apply basis operation
55bd882c8aSJames Wright   switch (eval_mode) {
56bd882c8aSJames Wright     case CEED_EVAL_INTERP: {
57bd882c8aSJames Wright       CeedInt       *lrange         = impl->interp_local_range;
58bd882c8aSJames Wright       const CeedInt &elem_per_group = lrange[2];
59bd882c8aSJames Wright       const CeedInt  group_count    = (num_elem / elem_per_group) + !!(num_elem % elem_per_group);
60bd882c8aSJames Wright       //-----------
61bd882c8aSJames Wright       sycl::range<3>    local_range(lrange[2], lrange[1], lrange[0]);
62bd882c8aSJames Wright       sycl::range<3>    global_range(group_count * lrange[2], lrange[1], lrange[0]);
63bd882c8aSJames Wright       sycl::nd_range<3> kernel_range(global_range, local_range);
64bd882c8aSJames Wright       //-----------
65bd882c8aSJames Wright       sycl::kernel *interp_kernel = (t_mode == CEED_TRANSPOSE) ? impl->interp_transpose_kernel : impl->interp_kernel;
66bd882c8aSJames Wright 
67bd882c8aSJames Wright       // Order queue
68bd882c8aSJames Wright       sycl::event e = ceed_Sycl->sycl_queue.ext_oneapi_submit_barrier();
69bd882c8aSJames Wright 
70bd882c8aSJames Wright       ceed_Sycl->sycl_queue.submit([&](sycl::handler &cgh) {
71bd882c8aSJames Wright         cgh.depends_on(e);
72bd882c8aSJames Wright         cgh.set_args(num_elem, impl->d_interp_1d, d_u, d_v);
73bd882c8aSJames Wright         cgh.parallel_for(kernel_range, *interp_kernel);
74bd882c8aSJames Wright       });
75bd882c8aSJames Wright 
76bd882c8aSJames Wright     } break;
77bd882c8aSJames Wright     case CEED_EVAL_GRAD: {
78bd882c8aSJames Wright       CeedInt       *lrange         = impl->grad_local_range;
79bd882c8aSJames Wright       const CeedInt &elem_per_group = lrange[2];
80bd882c8aSJames Wright       const CeedInt  group_count    = (num_elem / elem_per_group) + !!(num_elem % elem_per_group);
81bd882c8aSJames Wright       //-----------
82bd882c8aSJames Wright       sycl::range<3>    local_range(lrange[2], lrange[1], lrange[0]);
83bd882c8aSJames Wright       sycl::range<3>    global_range(group_count * lrange[2], lrange[1], lrange[0]);
84bd882c8aSJames Wright       sycl::nd_range<3> kernel_range(global_range, local_range);
85bd882c8aSJames Wright       //-----------
86bd882c8aSJames Wright       sycl::kernel     *grad_kernel = (t_mode == CEED_TRANSPOSE) ? impl->grad_transpose_kernel : impl->grad_kernel;
87bd882c8aSJames Wright       const CeedScalar *d_grad_1d   = (impl->d_collo_grad_1d) ? impl->d_collo_grad_1d : impl->d_grad_1d;
88bd882c8aSJames Wright       // Order queue
89bd882c8aSJames Wright       sycl::event e = ceed_Sycl->sycl_queue.ext_oneapi_submit_barrier();
90bd882c8aSJames Wright 
91bd882c8aSJames Wright       ceed_Sycl->sycl_queue.submit([&](sycl::handler &cgh) {
92bd882c8aSJames Wright         cgh.depends_on(e);
93bd882c8aSJames Wright         cgh.set_args(num_elem, impl->d_interp_1d, d_grad_1d, d_u, d_v);
94bd882c8aSJames Wright         cgh.parallel_for(kernel_range, *grad_kernel);
95bd882c8aSJames Wright       });
96bd882c8aSJames Wright     } break;
97bd882c8aSJames Wright     case CEED_EVAL_WEIGHT: {
98bd882c8aSJames Wright       CeedInt       *lrange         = impl->weight_local_range;
99bd882c8aSJames Wright       const CeedInt &elem_per_group = lrange[2];
100bd882c8aSJames Wright       const CeedInt  group_count    = (num_elem / elem_per_group) + !!(num_elem % elem_per_group);
101bd882c8aSJames Wright       //-----------
102bd882c8aSJames Wright       sycl::range<3>    local_range(lrange[2], lrange[1], lrange[0]);
103bd882c8aSJames Wright       sycl::range<3>    global_range(group_count * lrange[2], lrange[1], lrange[0]);
104bd882c8aSJames Wright       sycl::nd_range<3> kernel_range(global_range, local_range);
105bd882c8aSJames Wright       //-----------
106bd882c8aSJames Wright       // Order queue
107bd882c8aSJames Wright       sycl::event e = ceed_Sycl->sycl_queue.ext_oneapi_submit_barrier();
108bd882c8aSJames Wright 
109bd882c8aSJames Wright       ceed_Sycl->sycl_queue.submit([&](sycl::handler &cgh) {
110bd882c8aSJames Wright         cgh.depends_on(e);
111bd882c8aSJames Wright         cgh.set_args(num_elem, impl->d_q_weight_1d, d_v);
112bd882c8aSJames Wright         cgh.parallel_for(kernel_range, *(impl->weight_kernel));
113bd882c8aSJames Wright       });
114bd882c8aSJames Wright     } break;
115bd882c8aSJames Wright     // LCOV_EXCL_START
116bd882c8aSJames Wright     // Evaluate the divergence to/from the quadrature points
117bd882c8aSJames Wright     case CEED_EVAL_DIV:
118bd882c8aSJames Wright       return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_DIV not supported");
119bd882c8aSJames Wright     // Evaluate the curl to/from the quadrature points
120bd882c8aSJames Wright     case CEED_EVAL_CURL:
121bd882c8aSJames Wright       return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_CURL not supported");
122bd882c8aSJames Wright     // Take no action, BasisApply should not have been called
123bd882c8aSJames Wright     case CEED_EVAL_NONE:
124bd882c8aSJames Wright       return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_NONE does not make sense in this context");
125bd882c8aSJames Wright       // LCOV_EXCL_STOP
126bd882c8aSJames Wright   }
127bd882c8aSJames Wright 
128bd882c8aSJames Wright   // Restore vectors
129bd882c8aSJames Wright   if (eval_mode != CEED_EVAL_WEIGHT) {
130bd882c8aSJames Wright     CeedCallBackend(CeedVectorRestoreArrayRead(u, &d_u));
131bd882c8aSJames Wright   }
132bd882c8aSJames Wright   CeedCallBackend(CeedVectorRestoreArray(v, &d_v));
133bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
134bd882c8aSJames Wright }
135bd882c8aSJames Wright 
136bd882c8aSJames Wright //------------------------------------------------------------------------------
137bd882c8aSJames Wright // Destroy basis
138bd882c8aSJames Wright //------------------------------------------------------------------------------
139bd882c8aSJames Wright static int CeedBasisDestroy_Sycl_shared(CeedBasis basis) {
140bd882c8aSJames Wright   Ceed                   ceed;
141bd882c8aSJames Wright   Ceed_Sycl             *data;
142dd64fc84SJeremy L Thompson   CeedBasis_Sycl_shared *impl;
143bd882c8aSJames Wright 
144dd64fc84SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
145dd64fc84SJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &impl));
146dd64fc84SJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &data));
147bd882c8aSJames Wright   CeedCallSycl(ceed, data->sycl_queue.wait_and_throw());
148bd882c8aSJames Wright   CeedCallSycl(ceed, sycl::free(impl->d_q_weight_1d, data->sycl_context));
149bd882c8aSJames Wright   CeedCallSycl(ceed, sycl::free(impl->d_interp_1d, data->sycl_context));
150bd882c8aSJames Wright   CeedCallSycl(ceed, sycl::free(impl->d_grad_1d, data->sycl_context));
151bd882c8aSJames Wright   CeedCallSycl(ceed, sycl::free(impl->d_collo_grad_1d, data->sycl_context));
152bd882c8aSJames Wright 
153bd882c8aSJames Wright   delete impl->interp_kernel;
154bd882c8aSJames Wright   delete impl->interp_transpose_kernel;
155bd882c8aSJames Wright   delete impl->grad_kernel;
156bd882c8aSJames Wright   delete impl->grad_transpose_kernel;
157bd882c8aSJames Wright   delete impl->weight_kernel;
158bd882c8aSJames Wright   delete impl->sycl_module;
159bd882c8aSJames Wright 
160bd882c8aSJames Wright   CeedCallBackend(CeedFree(&impl));
161bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
162bd882c8aSJames Wright }
163bd882c8aSJames Wright 
164bd882c8aSJames Wright //------------------------------------------------------------------------------
165bd882c8aSJames Wright // Create tensor basis
166bd882c8aSJames Wright // TODO: Refactor
167bd882c8aSJames Wright //------------------------------------------------------------------------------
168bd882c8aSJames Wright int CeedBasisCreateTensorH1_Sycl_shared(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, const CeedScalar *grad_1d,
169bd882c8aSJames Wright                                         const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis basis) {
170bd882c8aSJames Wright   Ceed                   ceed;
171bd882c8aSJames Wright   Ceed_Sycl             *data;
172*22070f95SJeremy L Thompson   char                  *basis_kernel_source;
173*22070f95SJeremy L Thompson   const char            *basis_kernel_path;
174bd882c8aSJames Wright   CeedInt                num_comp;
175dd64fc84SJeremy L Thompson   CeedBasis_Sycl_shared *impl;
176dd64fc84SJeremy L Thompson 
177dd64fc84SJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
178dd64fc84SJeremy L Thompson   CeedCallBackend(CeedCalloc(1, &impl));
179dd64fc84SJeremy L Thompson   CeedCallBackend(CeedGetData(ceed, &data));
180bd882c8aSJames Wright   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
181bd882c8aSJames Wright 
182bd882c8aSJames Wright   const CeedInt thread_1d = CeedIntMax(Q_1d, P_1d);
183bd882c8aSJames Wright   const CeedInt num_nodes = CeedIntPow(P_1d, dim);
184bd882c8aSJames Wright   const CeedInt num_qpts  = CeedIntPow(Q_1d, dim);
185bd882c8aSJames Wright 
186bd882c8aSJames Wright   CeedInt *interp_lrange = impl->interp_local_range;
187dd64fc84SJeremy L Thompson 
188bd882c8aSJames Wright   CeedCallBackend(ComputeLocalRange(ceed, dim, thread_1d, interp_lrange));
189bd882c8aSJames Wright   const CeedInt interp_group_size = interp_lrange[0] * interp_lrange[1] * interp_lrange[2];
190bd882c8aSJames Wright 
191bd882c8aSJames Wright   CeedInt *grad_lrange = impl->grad_local_range;
192dd64fc84SJeremy L Thompson 
193bd882c8aSJames Wright   CeedCallBackend(ComputeLocalRange(ceed, dim, thread_1d, grad_lrange));
194bd882c8aSJames Wright   const CeedInt grad_group_size = grad_lrange[0] * grad_lrange[1] * grad_lrange[2];
195bd882c8aSJames Wright 
196bd882c8aSJames Wright   CeedCallBackend(ComputeLocalRange(ceed, dim, Q_1d, impl->weight_local_range));
197bd882c8aSJames Wright 
198bd882c8aSJames Wright   // Copy basis data to GPU
199bd882c8aSJames Wright   CeedCallSycl(ceed, impl->d_q_weight_1d = sycl::malloc_device<CeedScalar>(Q_1d, data->sycl_device, data->sycl_context));
200bd882c8aSJames Wright   sycl::event copy_weight = data->sycl_queue.copy<CeedScalar>(q_weight_1d, impl->d_q_weight_1d, Q_1d);
201bd882c8aSJames Wright 
202bd882c8aSJames Wright   const CeedInt interp_length = Q_1d * P_1d;
203bd882c8aSJames Wright   CeedCallSycl(ceed, impl->d_interp_1d = sycl::malloc_device<CeedScalar>(interp_length, data->sycl_device, data->sycl_context));
204bd882c8aSJames Wright   sycl::event copy_interp = data->sycl_queue.copy<CeedScalar>(interp_1d, impl->d_interp_1d, interp_length);
205bd882c8aSJames Wright 
206bd882c8aSJames Wright   CeedCallSycl(ceed, impl->d_grad_1d = sycl::malloc_device<CeedScalar>(interp_length, data->sycl_device, data->sycl_context));
207bd882c8aSJames Wright   sycl::event copy_grad = data->sycl_queue.copy<CeedScalar>(grad_1d, impl->d_grad_1d, interp_length);
208bd882c8aSJames Wright 
209bd882c8aSJames Wright   CeedCallSycl(ceed, sycl::event::wait_and_throw({copy_weight, copy_interp, copy_grad}));
210bd882c8aSJames Wright 
211bd882c8aSJames Wright   // Compute collocated gradient and copy to GPU
212bd882c8aSJames Wright   impl->d_collo_grad_1d          = NULL;
213bd882c8aSJames Wright   const bool has_collocated_grad = (dim == 3) && (Q_1d >= P_1d);
214dd64fc84SJeremy L Thompson 
215bd882c8aSJames Wright   if (has_collocated_grad) {
216bd882c8aSJames Wright     CeedScalar   *collo_grad_1d;
217dd64fc84SJeremy L Thompson     const CeedInt cgrad_length = Q_1d * Q_1d;
218dd64fc84SJeremy L Thompson 
219bd882c8aSJames Wright     CeedCallBackend(CeedMalloc(Q_1d * Q_1d, &collo_grad_1d));
220bd882c8aSJames Wright     CeedCallBackend(CeedBasisGetCollocatedGrad(basis, collo_grad_1d));
221bd882c8aSJames Wright     CeedCallSycl(ceed, impl->d_collo_grad_1d = sycl::malloc_device<CeedScalar>(cgrad_length, data->sycl_device, data->sycl_context));
222bd882c8aSJames Wright     CeedCallSycl(ceed, data->sycl_queue.copy<CeedScalar>(collo_grad_1d, impl->d_collo_grad_1d, cgrad_length).wait_and_throw());
223bd882c8aSJames Wright     CeedCallBackend(CeedFree(&collo_grad_1d));
224bd882c8aSJames Wright   }
225bd882c8aSJames Wright 
226bd882c8aSJames Wright   // ---[Refactor into separate function]------>
227bd882c8aSJames Wright   // Define compile-time constants
228bd882c8aSJames Wright   std::map<std::string, CeedInt> jit_constants;
229bd882c8aSJames Wright   jit_constants["BASIS_DIM"]                 = dim;
230bd882c8aSJames Wright   jit_constants["BASIS_Q_1D"]                = Q_1d;
231bd882c8aSJames Wright   jit_constants["BASIS_P_1D"]                = P_1d;
232bd882c8aSJames Wright   jit_constants["T_1D"]                      = thread_1d;
233bd882c8aSJames Wright   jit_constants["BASIS_NUM_COMP"]            = num_comp;
234bd882c8aSJames Wright   jit_constants["BASIS_NUM_NODES"]           = num_nodes;
235bd882c8aSJames Wright   jit_constants["BASIS_NUM_QPTS"]            = num_qpts;
236bd882c8aSJames Wright   jit_constants["BASIS_HAS_COLLOCATED_GRAD"] = has_collocated_grad;
237bd882c8aSJames Wright   jit_constants["BASIS_INTERP_SCRATCH_SIZE"] = interp_group_size;
238bd882c8aSJames Wright   jit_constants["BASIS_GRAD_SCRATCH_SIZE"]   = grad_group_size;
239bd882c8aSJames Wright 
240bd882c8aSJames Wright   // Load kernel source
241bd882c8aSJames Wright   CeedCallBackend(CeedGetJitAbsolutePath(ceed, "ceed/jit-source/sycl/sycl-shared-basis-tensor.h", &basis_kernel_path));
24223d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source -----\n");
243*22070f95SJeremy L Thompson   {
244*22070f95SJeremy L Thompson     char *source;
245*22070f95SJeremy L Thompson 
246*22070f95SJeremy L Thompson     CeedCallBackend(CeedLoadSourceToBuffer(ceed, basis_kernel_path, &source));
247*22070f95SJeremy L Thompson     basis_kernel_source = source;
248*22070f95SJeremy L Thompson   }
24923d4529eSJeremy L Thompson   CeedDebug256(ceed, CEED_DEBUG_COLOR_SUCCESS, "----- Loading Basis Kernel Source Complete -----\n");
250bd882c8aSJames Wright 
251bd882c8aSJames Wright   // Compile kernels into a kernel bundle
252eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedBuildModule_Sycl(ceed, basis_kernel_source, &impl->sycl_module, jit_constants));
253bd882c8aSJames Wright 
254bd882c8aSJames Wright   // Load kernel functions
255eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Sycl(ceed, impl->sycl_module, "Interp", &impl->interp_kernel));
256eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Sycl(ceed, impl->sycl_module, "InterpTranspose", &impl->interp_transpose_kernel));
257eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Sycl(ceed, impl->sycl_module, "Grad", &impl->grad_kernel));
258eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Sycl(ceed, impl->sycl_module, "GradTranspose", &impl->grad_transpose_kernel));
259eb7e6cafSJeremy L Thompson   CeedCallBackend(CeedGetKernel_Sycl(ceed, impl->sycl_module, "Weight", &impl->weight_kernel));
260bd882c8aSJames Wright 
261bd882c8aSJames Wright   // Clean-up
262bd882c8aSJames Wright   CeedCallBackend(CeedFree(&basis_kernel_path));
263bd882c8aSJames Wright   CeedCallBackend(CeedFree(&basis_kernel_source));
264bd882c8aSJames Wright   // <---[Refactor into separate function]------
265bd882c8aSJames Wright 
266bd882c8aSJames Wright   CeedCallBackend(CeedBasisSetData(basis, impl));
267bd882c8aSJames Wright 
268bd882c8aSJames Wright   // Register backend functions
269bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Basis", basis, "Apply", CeedBasisApplyTensor_Sycl_shared));
270bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "Basis", basis, "Destroy", CeedBasisDestroy_Sycl_shared));
271bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
272bd882c8aSJames Wright }
273ff1e7120SSebastian Grimberg 
274bd882c8aSJames Wright //------------------------------------------------------------------------------
275