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