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