Actual source code: cupmdevice.hpp

  1: #ifndef PETSCCUPMDEVICE_HPP
  2: #define PETSCCUPMDEVICE_HPP

  4: #include <petsc/private/deviceimpl.h>
  5: #include <petsc/private/cupminterface.hpp>
  6: #include <vector>
  7: #include <memory>

  9: namespace Petsc {

 11: template <CUPMDeviceKind T>
 12: class CUPMDevice : CUPMInterface<T>
 13: {
 14: public:
 15:   typedef PetscErrorCode (*createContextFunc_t)(PetscDeviceContext);
 16:   PETSC_INHERIT_CUPM_INTERFACE_TYPEDEFS_USING(cupmInterface_t,T)

 18:   // default constructor
 19:   explicit CUPMDevice(createContextFunc_t func) : _create{func} {}

 21:   // copy constructor
 22:   CUPMDevice(const CUPMDevice &other) noexcept = default;

 24:   // move constructor
 25:   CUPMDevice(CUPMDevice &&other) noexcept = default;

 27:   // destructor
 28:   ~CUPMDevice() noexcept = default;

 30:   // copy assignment operator
 31:   CUPMDevice& operator=(const CUPMDevice &other) = default;

 33:   // move assignment operator
 34:   CUPMDevice& operator=(CUPMDevice &&other) noexcept = default;

 36:   PETSC_NODISCARD PetscErrorCode getDevice(PetscDevice&) noexcept;

 38:   PETSC_NODISCARD PetscErrorCode configureDevice(PetscDevice&) noexcept;

 40: private:
 41:   // opaque class representing a single device
 42:   class PetscDeviceInternal;

 44:   // all known devices
 45:   static std::vector<std::unique_ptr<PetscDeviceInternal>> _devices;

 47:   // function to create a PetscDeviceContext (the (*create) function pointer usually set
 48:   // via XXXSetType() for other PETSc objects)
 49:   createContextFunc_t _create;

 51:   // have we tried looking for devices
 52:   static PetscBool _initialized;

 54:   // look for devices
 55:   PETSC_NODISCARD static PetscErrorCode __initialize() noexcept;
 56: };

 58: // define static variables
 59: template <CUPMDeviceKind T_>
 60: PetscBool CUPMDevice<T_>::_initialized = PETSC_FALSE;

 62: template <CUPMDeviceKind T_>
 63: std::vector<std::unique_ptr<typename CUPMDevice<T_>::PetscDeviceInternal>> CUPMDevice<T_>::_devices;

 65: } // namespace Petsc

 67: #endif /* PETSCCUPMDEVICE_HPP */