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