xref: /petsc/src/sys/objects/device/impls/sycl/sycldevice.hpp (revision 9dd11ecf0918283bb567d8b33a92f53ac4ea7840)
1 #pragma once
2 
3 #include "../impldevicebase.hpp" /* I "petscdevice.h" */
4 
5 #include <array>
6 
7 namespace Petsc
8 {
9 
10 namespace device
11 {
12 
13 namespace sycl
14 {
15 
16 #define PETSC_SYCL_DEVICE_HOST -1 // Note -1 is also used by PETSC_DECIDE, so user needs to pass -2 to explicitly select the host
17 #define PETSC_SYCL_DEVICE_NONE -3
18 
19 class Device : public ::Petsc::device::impl::DeviceBase<Device> {
20 public:
21   PETSC_DEVICE_IMPL_BASE_CLASS_HEADER(base_type, Device);
22 
~Device()23   ~Device() { auto PETSC_UNUSED _ = finalize_(); }
24 
25   static PetscErrorCode initialize(MPI_Comm, PetscInt *, PetscBool *, PetscDeviceInitType *) noexcept;
26 
27 private:
28   // opaque class representing a single device instance
29   class DeviceInternal;
30 
31   // currently stores sycl host and gpu devices
32   static std::array<DeviceInternal *, PETSC_DEVICE_MAX_DEVICES> devices_array_;
33   static DeviceInternal                                       **devices_; // alias to devices_array_, but shifted to support devices_[-1] for sycl host device
34 
35   // this rank's default device. If equals to PETSC_SYCL_DEVICE_NONE, then all sycl devices are disabled
36   static int defaultDevice_;
37 
38   // have we tried looking for devices
39   static bool initialized_;
40 
41   // clean-up
42   static PetscErrorCode finalize_() noexcept;
43 
PETSC_CXX_COMPAT_DECL(constexpr PetscDeviceType PETSC_DEVICE_IMPL_ ())44   PETSC_CXX_COMPAT_DECL(constexpr PetscDeviceType PETSC_DEVICE_IMPL_()) { return PETSC_DEVICE_SYCL; }
45   PetscErrorCode        init_device_id_(PetscInt *) const noexcept;
46   static PetscErrorCode view_device_(PetscDevice, PetscViewer) noexcept;
47   static PetscErrorCode get_attribute_(PetscInt, PetscDeviceAttribute, void *) noexcept;
48 };
49 
50 } // namespace sycl
51 
52 } // namespace device
53 
54 } // namespace Petsc
55