1 #include "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 public: 18 struct PetscDeviceContext_SYCL { 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 }; 26 27 private: 28 PETSC_NODISCARD static PetscDeviceContext_SYCL *impls_cast_(PetscDeviceContext dctx) noexcept { return static_cast<PetscDeviceContext_SYCL *>(dctx->data); } 29 30 public: 31 // All of these functions MUST be static in order to be callable from C, otherwise they 32 // get the implicit 'this' pointer tacked on 33 static PetscErrorCode destroy(PetscDeviceContext dctx) noexcept 34 { 35 PetscFunctionBegin; 36 delete impls_cast_(dctx); 37 dctx->data = nullptr; 38 PetscFunctionReturn(PETSC_SUCCESS); 39 }; 40 41 // clang-format off 42 static constexpr _DeviceContextOps ops = { 43 PetscDesignatedInitializer(destroy, nullptr), 44 PetscDesignatedInitializer(changestreamtype, nullptr), 45 PetscDesignatedInitializer(setup, nullptr), 46 PetscDesignatedInitializer(query, nullptr), 47 PetscDesignatedInitializer(waitforcontext, nullptr), 48 PetscDesignatedInitializer(synchronize, nullptr), 49 PetscDesignatedInitializer(getblashandle, nullptr), 50 PetscDesignatedInitializer(getsolverhandle, nullptr), 51 PetscDesignatedInitializer(getstreamhandle, nullptr), 52 PetscDesignatedInitializer(begintimer, nullptr), 53 PetscDesignatedInitializer(endtimer, nullptr), 54 PetscDesignatedInitializer(memalloc, nullptr), 55 PetscDesignatedInitializer(memfree, nullptr), 56 PetscDesignatedInitializer(memcopy, nullptr), 57 PetscDesignatedInitializer(memset, nullptr), 58 PetscDesignatedInitializer(createevent, nullptr), 59 PetscDesignatedInitializer(recordevent, nullptr), 60 PetscDesignatedInitializer(waitforevent, nullptr) 61 }; 62 // clang-format on 63 }; 64 65 constexpr _DeviceContextOps DeviceContext::ops; 66 67 } // namespace impl 68 69 } // namespace sycl 70 71 } // namespace device 72 73 } // namespace Petsc 74 75 PetscErrorCode PetscDeviceContextCreate_SYCL(PetscDeviceContext dctx) 76 { 77 using namespace Petsc::device::sycl::impl; 78 79 static constexpr DeviceContext syclctx; 80 81 PetscFunctionBegin; 82 PetscCallCXX(dctx->data = new DeviceContext::PetscDeviceContext_SYCL{}); 83 *(dctx->ops) = syclctx.ops; 84 PetscFunctionReturn(PETSC_SUCCESS); 85 } 86