xref: /petsc/src/sys/objects/device/impls/sycl/syclcontext.sycl.cxx (revision ae51ba108530e1a8d2aeb8bc3771a24c2fcd801e)
1 #include "sycldevice.hpp"
2 #include <CL/sycl.hpp>
3 
4 namespace Petsc
5 {
6 
7 namespace device
8 {
9 
10 namespace sycl
11 {
12 
13 namespace impl
14 {
15 
16 class DeviceContext {
17 public:
18   struct PetscDeviceContext_IMPLS {
19     ::sycl::event event;
20     ::sycl::event begin; // timer-only
21     ::sycl::event end;   // timer-only
22 #if PetscDefined(USE_DEBUG)
23     PetscBool timerInUse;
24 #endif
25   };
26 
27 private:
28   static bool initialized_;
29 
30   static PetscErrorCode finalize_() noexcept
31   {
32     PetscFunctionBegin;
33     initialized_ = false;
34     PetscFunctionReturn(PETSC_SUCCESS);
35   }
36 
37   static PetscErrorCode initialize_(PetscInt id, DeviceContext *dci) noexcept
38   {
39     PetscFunctionBegin;
40     PetscCall(PetscDeviceCheckDeviceCount_Internal(id));
41     if (!initialized_) {
42       initialized_ = true;
43       PetscCall(PetscRegisterFinalize(finalize_));
44     }
45     PetscFunctionReturn(PETSC_SUCCESS);
46   }
47 
48 public:
49   const struct _DeviceContextOps ops = {destroy, changeStreamType, setUp, query, waitForContext, synchronize, getBlasHandle, getSolverHandle, getStreamHandle, beginTimer, endTimer, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};
50 
51   // default constructor
52   DeviceContext() noexcept = default;
53 
54   // All of these functions MUST be static in order to be callable from C, otherwise they
55   // get the implicit 'this' pointer tacked on
56   static PetscErrorCode destroy(PetscDeviceContext dctx) noexcept
57   {
58     PetscFunctionBegin;
59     delete static_cast<PetscDeviceContext_IMPLS *>(dctx->data);
60     dctx->data = nullptr;
61     PetscFunctionReturn(PETSC_SUCCESS);
62   };
63   static PetscErrorCode changeStreamType(PetscDeviceContext, PetscStreamType) noexcept { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented"); };
64   static PetscErrorCode setUp(PetscDeviceContext) noexcept { return PETSC_SUCCESS; }; // Nothing to setup
65   static PetscErrorCode query(PetscDeviceContext, PetscBool *) noexcept { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented"); };
66   static PetscErrorCode waitForContext(PetscDeviceContext, PetscDeviceContext) noexcept { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented"); };
67   static PetscErrorCode synchronize(PetscDeviceContext) noexcept { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented"); };
68   static PetscErrorCode getBlasHandle(PetscDeviceContext, void *) noexcept { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented"); };
69   static PetscErrorCode getSolverHandle(PetscDeviceContext, void *) noexcept { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented"); };
70   static PetscErrorCode getStreamHandle(PetscDeviceContext, void *) noexcept { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented"); };
71   static PetscErrorCode beginTimer(PetscDeviceContext) noexcept { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented"); };
72   static PetscErrorCode endTimer(PetscDeviceContext, PetscLogDouble *) noexcept { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented"); };
73 };
74 
75 } // namespace impl
76 
77 } // namespace sycl
78 
79 } // namespace device
80 
81 } // namespace Petsc
82 
83 PetscErrorCode PetscDeviceContextCreate_SYCL(PetscDeviceContext dctx)
84 {
85   using namespace Petsc::device::sycl::impl;
86 
87   static const DeviceContext syclctx;
88 
89   PetscFunctionBegin;
90   dctx->data = new DeviceContext::PetscDeviceContext_IMPLS();
91   PetscCall(PetscMemcpy(dctx->ops, &syclctx.ops, sizeof(syclctx.ops)));
92   PetscFunctionReturn(PETSC_SUCCESS);
93 }
94