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