xref: /petsc/src/sys/objects/device/impls/cupm/cupmdevice.hpp (revision 1cc06b555e92f8ec64db10330b8bbd830e5bc876)
1 #ifndef PETSCCUPMDEVICE_HPP
2 #define PETSCCUPMDEVICE_HPP
3 
4 #include <petsc/private/cupminterface.hpp>
5 #include <petsc/private/cpp/memory.hpp>
6 #include <petsc/private/cpp/array.hpp>
7 
8 #include "../impldevicebase.hpp" /* I "petscdevice.h" */
9 
10 namespace Petsc
11 {
12 
13 namespace device
14 {
15 
16 namespace cupm
17 {
18 
19 #if defined(PETSC_CUPM_DEVICE_NONE)
20   #error redefinition of PETSC_CUPM_DEVICE_NONE
21 #endif
22 
23 #define PETSC_CUPM_DEVICE_NONE -3
24 
25 template <DeviceType T>
26 class Device : public ::Petsc::device::impl::DeviceBase<Device<T>>, impl::Interface<T> {
27 public:
28   PETSC_DEVICE_IMPL_BASE_CLASS_HEADER(base_type, Device<T>);
29   PETSC_CUPM_INHERIT_INTERFACE_TYPEDEFS_USING(T);
30 
31   static PetscErrorCode initialize(MPI_Comm, PetscInt *, PetscBool *, PetscDeviceInitType *) noexcept;
32 
33 private:
34   // opaque class representing a single device
35   class DeviceInternal;
36 
37   // all known devices
38   using devices_type = std::array<std::unique_ptr<DeviceInternal>, PETSC_DEVICE_MAX_DEVICES>;
39   static devices_type devices_;
40 
41   // this ranks default device, if < 0  then devices are specifically disabled
42   static int defaultDevice_;
43 
44   // have we tried looking for devices
45   static bool initialized_;
46 
47   // clean-up
48   static PetscErrorCode finalize_() noexcept;
49 
50   PETSC_NODISCARD static constexpr PetscDeviceType PETSC_DEVICE_IMPL_() noexcept { return PETSC_DEVICE_CUPM(); }
51 
52   PetscErrorCode        init_device_id_(PetscInt *) const noexcept;
53   static PetscErrorCode configure_device_(PetscDevice) noexcept;
54   static PetscErrorCode view_device_(PetscDevice, PetscViewer) noexcept;
55   static PetscErrorCode get_attribute_(PetscInt, PetscDeviceAttribute, void *) noexcept;
56 };
57 
58 // define static variables
59 template <DeviceType T>
60 typename Device<T>::devices_type Device<T>::devices_ = {};
61 
62 template <DeviceType T>
63 int Device<T>::defaultDevice_ = PETSC_CUPM_DEVICE_NONE;
64 
65 template <DeviceType T>
66 bool Device<T>::initialized_ = false;
67 
68 } // namespace cupm
69 
70 } // namespace device
71 
72 } // namespace Petsc
73 
74 #endif /* PETSCCUPMDEVICE_HPP */
75