Actual source code: syclcontext.sycl.cxx
1: #include "sycldevice.hpp"
2: #include <CL/sycl.hpp>
4: namespace Petsc
5: {
7: namespace device
8: {
10: namespace sycl
11: {
13: namespace impl
14: {
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: };
27: private:
28: static bool initialized_;
30: static PetscErrorCode finalize_() noexcept
31: {
32: PetscFunctionBegin;
33: initialized_ = false;
34: PetscFunctionReturn(PETSC_SUCCESS);
35: }
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: }
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};
51: // default constructor
52: DeviceContext() noexcept = default;
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: };
75: } // namespace impl
77: } // namespace sycl
79: } // namespace device
81: } // namespace Petsc
83: PetscErrorCode PetscDeviceContextCreate_SYCL(PetscDeviceContext dctx)
84: {
85: using namespace Petsc::device::sycl::impl;
87: static const DeviceContext syclctx;
89: PetscFunctionBegin;
90: dctx->data = new DeviceContext::PetscDeviceContext_IMPLS();
91: PetscCall(PetscMemcpy(dctx->ops, &syclctx.ops, sizeof(syclctx.ops)));
92: PetscFunctionReturn(PETSC_SUCCESS);
93: }