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