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