xref: /libCEED/backends/sycl-ref/ceed-sycl-ref-qfunction.sycl.cpp (revision 1f4b1b45844fd6ef75549b644944f35065f07aef)
15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other
2bd882c8aSJames Wright // CEED contributors. All Rights Reserved. See the top-level LICENSE and NOTICE
3bd882c8aSJames Wright // files for details.
4bd882c8aSJames Wright //
5bd882c8aSJames Wright // SPDX-License-Identifier: BSD-2-Clause
6bd882c8aSJames Wright //
7bd882c8aSJames Wright // This file is part of CEED:  http://github.com/ceed
8bd882c8aSJames Wright 
9bd882c8aSJames Wright #include <ceed/backend.h>
10bd882c8aSJames Wright #include <ceed/ceed.h>
11bd882c8aSJames Wright 
12bd882c8aSJames Wright #include <string>
13bd882c8aSJames Wright #include <sycl/sycl.hpp>
14bd882c8aSJames Wright #include <vector>
15bd882c8aSJames Wright 
16bd882c8aSJames Wright #include "../sycl/ceed-sycl-common.hpp"
17bd882c8aSJames Wright #include "../sycl/ceed-sycl-compile.hpp"
18bd882c8aSJames Wright #include "ceed-sycl-ref-qfunction-load.hpp"
19bd882c8aSJames Wright #include "ceed-sycl-ref.hpp"
20bd882c8aSJames Wright 
21bd882c8aSJames Wright #define WG_SIZE_QF 384
22bd882c8aSJames Wright 
23bd882c8aSJames Wright //------------------------------------------------------------------------------
24bd882c8aSJames Wright // Apply QFunction
25bd882c8aSJames Wright //------------------------------------------------------------------------------
26bd882c8aSJames Wright static int CeedQFunctionApply_Sycl(CeedQFunction qf, CeedInt Q, CeedVector *U, CeedVector *V) {
27dd64fc84SJeremy L Thompson   Ceed                ceed;
28dd64fc84SJeremy L Thompson   Ceed_Sycl          *ceed_Sycl;
29dd64fc84SJeremy L Thompson   void               *context_data;
30dd64fc84SJeremy L Thompson   CeedInt             num_input_fields, num_output_fields;
31bd882c8aSJames Wright   CeedQFunction_Sycl *impl;
32dd64fc84SJeremy L Thompson 
33bd882c8aSJames Wright   CeedCallBackend(CeedQFunctionGetData(qf, &impl));
34bd882c8aSJames Wright 
35bd882c8aSJames Wright   // Build and compile kernel, if not done
36eb7e6cafSJeremy L Thompson   if (!impl->QFunction) CeedCallBackend(CeedQFunctionBuildKernel_Sycl(qf));
37bd882c8aSJames Wright 
38bd882c8aSJames Wright   CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed));
39bd882c8aSJames Wright   CeedCallBackend(CeedGetData(ceed, &ceed_Sycl));
40bd882c8aSJames Wright 
41bd882c8aSJames Wright   CeedCallBackend(CeedQFunctionGetNumArgs(qf, &num_input_fields, &num_output_fields));
42bd882c8aSJames Wright 
43bd882c8aSJames Wright   // Read vectors
44bd882c8aSJames Wright   std::vector<const CeedScalar *> inputs(num_input_fields);
45bd882c8aSJames Wright   const CeedVector               *U_i = U;
46bd882c8aSJames Wright   for (auto &input_i : inputs) {
47bd882c8aSJames Wright     CeedCallBackend(CeedVectorGetArrayRead(*U_i, CEED_MEM_DEVICE, &input_i));
48bd882c8aSJames Wright     ++U_i;
49bd882c8aSJames Wright   }
50bd882c8aSJames Wright 
51bd882c8aSJames Wright   std::vector<CeedScalar *> outputs(num_output_fields);
52bd882c8aSJames Wright   CeedVector               *V_i = V;
53bd882c8aSJames Wright   for (auto &output_i : outputs) {
54bd882c8aSJames Wright     CeedCallBackend(CeedVectorGetArrayWrite(*V_i, CEED_MEM_DEVICE, &output_i));
55bd882c8aSJames Wright     ++V_i;
56bd882c8aSJames Wright   }
57bd882c8aSJames Wright 
58bd882c8aSJames Wright   // Get context data
59bd882c8aSJames Wright   CeedCallBackend(CeedQFunctionGetInnerContextData(qf, CEED_MEM_DEVICE, &context_data));
60bd882c8aSJames Wright 
61*1f4b1b45SUmesh Unnikrishnan   std::vector<sycl::event> e;
62*1f4b1b45SUmesh Unnikrishnan 
63*1f4b1b45SUmesh Unnikrishnan   if (!ceed_Sycl->sycl_queue.is_in_order()) e = {ceed_Sycl->sycl_queue.ext_oneapi_submit_barrier()};
64bd882c8aSJames Wright 
65bd882c8aSJames Wright   // Launch as a basic parallel_for over Q quadrature points
66bd882c8aSJames Wright   ceed_Sycl->sycl_queue.submit([&](sycl::handler &cgh) {
67*1f4b1b45SUmesh Unnikrishnan     cgh.depends_on(e);
68bd882c8aSJames Wright 
69bd882c8aSJames Wright     int iarg{};
70bd882c8aSJames Wright     cgh.set_arg(iarg, context_data);
71bd882c8aSJames Wright     ++iarg;
72bd882c8aSJames Wright     cgh.set_arg(iarg, Q);
73bd882c8aSJames Wright     ++iarg;
74bd882c8aSJames Wright     for (auto &input_i : inputs) {
75bd882c8aSJames Wright       cgh.set_arg(iarg, input_i);
76bd882c8aSJames Wright       ++iarg;
77bd882c8aSJames Wright     }
78bd882c8aSJames Wright     for (auto &output_i : outputs) {
79bd882c8aSJames Wright       cgh.set_arg(iarg, output_i);
80bd882c8aSJames Wright       ++iarg;
81bd882c8aSJames Wright     }
82bd882c8aSJames Wright     // Hard-coding the work-group size for now
83bd882c8aSJames Wright     // We could use the Level Zero API to query and set an appropriate size in future
84bd882c8aSJames Wright     // Equivalent of CUDA Occupancy Calculator
85bd882c8aSJames Wright     int               wg_size   = WG_SIZE_QF;
86bd882c8aSJames Wright     sycl::range<1>    rounded_Q = ((Q + (wg_size - 1)) / wg_size) * wg_size;
87bd882c8aSJames Wright     sycl::nd_range<1> kernel_range(rounded_Q, wg_size);
88bd882c8aSJames Wright     cgh.parallel_for(kernel_range, *(impl->QFunction));
89bd882c8aSJames Wright   });
90bd882c8aSJames Wright 
91bd882c8aSJames Wright   // Restore vectors
92bd882c8aSJames Wright   U_i = U;
93bd882c8aSJames Wright   for (auto &input_i : inputs) {
94bd882c8aSJames Wright     CeedCallBackend(CeedVectorRestoreArrayRead(*U_i, &input_i));
95bd882c8aSJames Wright     ++U_i;
96bd882c8aSJames Wright   }
97bd882c8aSJames Wright 
98bd882c8aSJames Wright   V_i = V;
99bd882c8aSJames Wright   for (auto &output_i : outputs) {
100bd882c8aSJames Wright     CeedCallBackend(CeedVectorRestoreArray(*V_i, &output_i));
101bd882c8aSJames Wright     ++V_i;
102bd882c8aSJames Wright   }
103bd882c8aSJames Wright 
104bd882c8aSJames Wright   // Restore context
105bd882c8aSJames Wright   CeedCallBackend(CeedQFunctionRestoreInnerContextData(qf, &context_data));
106bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
107bd882c8aSJames Wright }
108bd882c8aSJames Wright 
109bd882c8aSJames Wright //------------------------------------------------------------------------------
110bd882c8aSJames Wright // Destroy QFunction
111bd882c8aSJames Wright //------------------------------------------------------------------------------
112bd882c8aSJames Wright static int CeedQFunctionDestroy_Sycl(CeedQFunction qf) {
113bd882c8aSJames Wright   Ceed                ceed;
114dd64fc84SJeremy L Thompson   CeedQFunction_Sycl *impl;
115bd882c8aSJames Wright 
116dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, &impl));
117dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed));
118bd882c8aSJames Wright   delete impl->QFunction;
119bd882c8aSJames Wright   delete impl->sycl_module;
120bd882c8aSJames Wright   CeedCallBackend(CeedFree(&impl));
121bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
122bd882c8aSJames Wright }
123bd882c8aSJames Wright 
124bd882c8aSJames Wright //------------------------------------------------------------------------------
125bd882c8aSJames Wright // Create QFunction
126bd882c8aSJames Wright //------------------------------------------------------------------------------
127bd882c8aSJames Wright int CeedQFunctionCreate_Sycl(CeedQFunction qf) {
128bd882c8aSJames Wright   Ceed                ceed;
129bd882c8aSJames Wright   CeedQFunction_Sycl *impl;
130bd882c8aSJames Wright 
1316e536b99SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed));
132bd882c8aSJames Wright   CeedCallBackend(CeedCalloc(1, &impl));
133bd882c8aSJames Wright   CeedCallBackend(CeedQFunctionSetData(qf, impl));
134bd882c8aSJames Wright   // Register backend functions
135bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "QFunction", qf, "Apply", CeedQFunctionApply_Sycl));
136bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "QFunction", qf, "Destroy", CeedQFunctionDestroy_Sycl));
137bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
138bd882c8aSJames Wright }
139ff1e7120SSebastian Grimberg 
140bd882c8aSJames Wright //------------------------------------------------------------------------------
141