xref: /libCEED/backends/sycl-ref/ceed-sycl-ref-qfunction.sycl.cpp (revision 6e536b992ff6bc401c55631f1bc4464446496b52)
1bd882c8aSJames Wright // Copyright (c) 2017-2022, 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 
61bd882c8aSJames Wright   // Order queue
62bd882c8aSJames Wright   sycl::event e = ceed_Sycl->sycl_queue.ext_oneapi_submit_barrier();
63bd882c8aSJames Wright 
64bd882c8aSJames Wright   // Launch as a basic parallel_for over Q quadrature points
65bd882c8aSJames Wright   ceed_Sycl->sycl_queue.submit([&](sycl::handler &cgh) {
66bd882c8aSJames Wright     cgh.depends_on({e});
67bd882c8aSJames Wright 
68bd882c8aSJames Wright     int iarg{};
69bd882c8aSJames Wright     cgh.set_arg(iarg, context_data);
70bd882c8aSJames Wright     ++iarg;
71bd882c8aSJames Wright     cgh.set_arg(iarg, Q);
72bd882c8aSJames Wright     ++iarg;
73bd882c8aSJames Wright     for (auto &input_i : inputs) {
74bd882c8aSJames Wright       cgh.set_arg(iarg, input_i);
75bd882c8aSJames Wright       ++iarg;
76bd882c8aSJames Wright     }
77bd882c8aSJames Wright     for (auto &output_i : outputs) {
78bd882c8aSJames Wright       cgh.set_arg(iarg, output_i);
79bd882c8aSJames Wright       ++iarg;
80bd882c8aSJames Wright     }
81bd882c8aSJames Wright     // Hard-coding the work-group size for now
82bd882c8aSJames Wright     // We could use the Level Zero API to query and set an appropriate size in future
83bd882c8aSJames Wright     // Equivalent of CUDA Occupancy Calculator
84bd882c8aSJames Wright     int               wg_size   = WG_SIZE_QF;
85bd882c8aSJames Wright     sycl::range<1>    rounded_Q = ((Q + (wg_size - 1)) / wg_size) * wg_size;
86bd882c8aSJames Wright     sycl::nd_range<1> kernel_range(rounded_Q, wg_size);
87bd882c8aSJames Wright     cgh.parallel_for(kernel_range, *(impl->QFunction));
88bd882c8aSJames Wright   });
89bd882c8aSJames Wright 
90bd882c8aSJames Wright   // Restore vectors
91bd882c8aSJames Wright   U_i = U;
92bd882c8aSJames Wright   for (auto &input_i : inputs) {
93bd882c8aSJames Wright     CeedCallBackend(CeedVectorRestoreArrayRead(*U_i, &input_i));
94bd882c8aSJames Wright     ++U_i;
95bd882c8aSJames Wright   }
96bd882c8aSJames Wright 
97bd882c8aSJames Wright   V_i = V;
98bd882c8aSJames Wright   for (auto &output_i : outputs) {
99bd882c8aSJames Wright     CeedCallBackend(CeedVectorRestoreArray(*V_i, &output_i));
100bd882c8aSJames Wright     ++V_i;
101bd882c8aSJames Wright   }
102bd882c8aSJames Wright 
103bd882c8aSJames Wright   // Restore context
104bd882c8aSJames Wright   CeedCallBackend(CeedQFunctionRestoreInnerContextData(qf, &context_data));
105bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
106bd882c8aSJames Wright }
107bd882c8aSJames Wright 
108bd882c8aSJames Wright //------------------------------------------------------------------------------
109bd882c8aSJames Wright // Destroy QFunction
110bd882c8aSJames Wright //------------------------------------------------------------------------------
111bd882c8aSJames Wright static int CeedQFunctionDestroy_Sycl(CeedQFunction qf) {
112bd882c8aSJames Wright   Ceed                ceed;
113dd64fc84SJeremy L Thompson   CeedQFunction_Sycl *impl;
114bd882c8aSJames Wright 
115dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetData(qf, &impl));
116dd64fc84SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed));
117bd882c8aSJames Wright   delete impl->QFunction;
118bd882c8aSJames Wright   delete impl->sycl_module;
119bd882c8aSJames Wright   CeedCallBackend(CeedFree(&impl));
120bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
121bd882c8aSJames Wright }
122bd882c8aSJames Wright 
123bd882c8aSJames Wright //------------------------------------------------------------------------------
124bd882c8aSJames Wright // Create QFunction
125bd882c8aSJames Wright //------------------------------------------------------------------------------
126bd882c8aSJames Wright int CeedQFunctionCreate_Sycl(CeedQFunction qf) {
127bd882c8aSJames Wright   Ceed                ceed;
128bd882c8aSJames Wright   CeedQFunction_Sycl *impl;
129bd882c8aSJames Wright 
130*6e536b99SJeremy L Thompson   CeedCallBackend(CeedQFunctionGetCeed(qf, &ceed));
131bd882c8aSJames Wright   CeedCallBackend(CeedCalloc(1, &impl));
132bd882c8aSJames Wright   CeedCallBackend(CeedQFunctionSetData(qf, impl));
133bd882c8aSJames Wright   // Register backend functions
134bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "QFunction", qf, "Apply", CeedQFunctionApply_Sycl));
135bd882c8aSJames Wright   CeedCallBackend(CeedSetBackendFunctionCpp(ceed, "QFunction", qf, "Destroy", CeedQFunctionDestroy_Sycl));
136bd882c8aSJames Wright   return CEED_ERROR_SUCCESS;
137bd882c8aSJames Wright }
138ff1e7120SSebastian Grimberg 
139bd882c8aSJames Wright //------------------------------------------------------------------------------
140