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