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:   PETSC_NODISCARD static PetscErrorCode finalize_() noexcept
 31:   {
 32:     initialized_ = false;
 33:     return 0;
 34:   }

 36:   PETSC_NODISCARD static PetscErrorCode initialize_(PetscInt id, DeviceContext *dci) noexcept
 37:   {
 38:     PetscDeviceCheckDeviceCount_Internal(id);
 39:     if (!initialized_) {
 40:       initialized_ = true;
 41:       PetscRegisterFinalize(finalize_);
 42:     }
 43:     return 0;
 44:   }

 46: public:
 47:   const struct _DeviceContextOps ops = {destroy, changeStreamType, setUp, query, waitForContext, synchronize, getBlasHandle, getSolverHandle, getStreamHandle, beginTimer, endTimer, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr};

 49:   // default constructor
 50:   DeviceContext() noexcept = default;

 52:   // All of these functions MUST be static in order to be callable from C, otherwise they
 53:   // get the implicit 'this' pointer tacked on
 54:   PETSC_NODISCARD static PetscErrorCode destroy(PetscDeviceContext dctx) noexcept
 55:   {
 56:     delete static_cast<PetscDeviceContext_IMPLS *>(dctx->data);
 57:     dctx->data = nullptr;
 58:     return 0;
 59:   };
 60:   PETSC_NODISCARD static PetscErrorCode changeStreamType(PetscDeviceContext, PetscStreamType) noexcept { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented"); };
 61:   PETSC_NODISCARD static PetscErrorCode setUp(PetscDeviceContext) noexcept { return 0; }; // Nothing to setup
 62:   PETSC_NODISCARD static PetscErrorCode query(PetscDeviceContext, PetscBool *) noexcept { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented"); };
 63:   PETSC_NODISCARD static PetscErrorCode waitForContext(PetscDeviceContext, PetscDeviceContext) noexcept { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented"); };
 64:   PETSC_NODISCARD static PetscErrorCode synchronize(PetscDeviceContext) noexcept { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented"); };
 65:   PETSC_NODISCARD static PetscErrorCode getBlasHandle(PetscDeviceContext, void *) noexcept { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented"); };
 66:   PETSC_NODISCARD static PetscErrorCode getSolverHandle(PetscDeviceContext, void *) noexcept { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented"); };
 67:   PETSC_NODISCARD static PetscErrorCode getStreamHandle(PetscDeviceContext, void *) noexcept { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented"); };
 68:   PETSC_NODISCARD static PetscErrorCode beginTimer(PetscDeviceContext) noexcept { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented"); };
 69:   PETSC_NODISCARD static PetscErrorCode endTimer(PetscDeviceContext, PetscLogDouble *) noexcept { SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Not implemented"); };
 70: };

 72: } // namespace impl

 74: } // namespace sycl

 76: } // namespace device

 78: } // namespace Petsc

 80: PetscErrorCode PetscDeviceContextCreate_SYCL(PetscDeviceContext dctx)
 81: {
 82:   using namespace Petsc::device::sycl::impl;

 84:   static const DeviceContext syclctx;

 86:   dctx->data = new DeviceContext::PetscDeviceContext_IMPLS();
 87:   PetscMemcpy(dctx->ops, &syclctx.ops, sizeof(syclctx.ops));
 88:   return 0;
 89: }