xref: /petsc/src/sys/objects/device/impls/sycl/syclcontext.sycl.cxx (revision 5e81a9049f99b3e299d3dfd50df0d9eed5e5e59b)
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     PetscErrorCode ierr;
41 
42     PetscFunctionBegin;
43     ierr = PetscDeviceCheckDeviceCount_Internal(id);CHKERRQ(ierr);
44     if (!initialized_) {
45       initialized_ = true;
46       ierr = PetscRegisterFinalize(finalize_);CHKERRQ(ierr);
47     }
48     PetscFunctionReturn(0);
49   }
50 
51 public:
52   const struct _DeviceContextOps ops = {
53     destroy,
54     changeStreamType,
55     setUp,
56     query,
57     waitForContext,
58     synchronize,
59     getBlasHandle,
60     getSolverHandle,
61     getStreamHandle,
62     beginTimer,
63     endTimer
64   };
65 
66   // default constructor
67   DeviceContext() noexcept = default;
68 
69   // All of these functions MUST be static in order to be callable from C, otherwise they
70   // get the implicit 'this' pointer tacked on
71   PETSC_NODISCARD static PetscErrorCode destroy(PetscDeviceContext dctx) noexcept
72   {
73     PetscFunctionBegin;
74     delete static_cast<PetscDeviceContext_IMPLS*>(dctx->data);
75     dctx->data = nullptr;
76     PetscFunctionReturn(0);
77   };
78   PETSC_NODISCARD static PetscErrorCode changeStreamType(PetscDeviceContext,PetscStreamType) noexcept { SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not implemented"); };
79   PETSC_NODISCARD static PetscErrorCode setUp(PetscDeviceContext) noexcept {return 0;}; // Nothing to setup
80   PETSC_NODISCARD static PetscErrorCode query(PetscDeviceContext,PetscBool*) noexcept { SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not implemented"); };
81   PETSC_NODISCARD static PetscErrorCode waitForContext(PetscDeviceContext,PetscDeviceContext) noexcept { SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not implemented"); };
82   PETSC_NODISCARD static PetscErrorCode synchronize(PetscDeviceContext) noexcept { SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not implemented"); };
83   PETSC_NODISCARD static PetscErrorCode getBlasHandle(PetscDeviceContext,void*) noexcept { SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not implemented"); };
84   PETSC_NODISCARD static PetscErrorCode getSolverHandle(PetscDeviceContext,void*) noexcept { SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not implemented"); };
85   PETSC_NODISCARD static PetscErrorCode getStreamHandle(PetscDeviceContext,void*) noexcept { SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not implemented"); };
86   PETSC_NODISCARD static PetscErrorCode beginTimer(PetscDeviceContext) noexcept { SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not implemented"); };
87   PETSC_NODISCARD static PetscErrorCode endTimer(PetscDeviceContext,PetscLogDouble*) noexcept { SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not implemented"); };
88 };
89 
90 } // namespace Impl
91 
92 } // namespace SYCL
93 
94 } // namespace Device
95 
96 } // namespace Petsc
97 
98 PetscErrorCode PetscDeviceContextCreate_SYCL(PetscDeviceContext dctx)
99 {
100   using namespace Petsc::Device::SYCL::Impl;
101 
102   PetscErrorCode             ierr;
103   static const DeviceContext syclctx;
104 
105   PetscFunctionBegin;
106   dctx->data = new DeviceContext::PetscDeviceContext_IMPLS();
107   ierr = PetscMemcpy(dctx->ops,&syclctx.ops,sizeof(syclctx.ops));CHKERRQ(ierr);
108   PetscFunctionReturn(0);
109 }
110