Actual source code: petscdevice_interface_internal.hpp

  1: #ifndef PETSCDEVICE_INTERFACE_INTERNAL_HPP
  2: #define PETSCDEVICE_INTERFACE_INTERNAL_HPP

  4: #include <petsc/private/deviceimpl.h>

  6: #include <petsc/private/cpp/utility.hpp>
  7: #include <petsc/private/cpp/functional.hpp>

  9: #include <unordered_map>
 10: #include <unordered_set>

 12: #if PetscDefined(USE_DEBUG) && PetscDefined(USE_INFO)
 13:   #define PETSC_USE_DEBUG_AND_INFO  1
 14:   #define PetscDebugInfo(dctx, ...) PetscInfo(dctx, __VA_ARGS__)
 15: #else
 16:   #define PetscDebugInfo(dctx, ...) 0
 17: #endif

 19: // this file contains functions needed to bridge the gap between dcontext.cxx and device.cxx
 20: // but are not useful enough to put in the impl header
 21: PETSC_INTERN PetscErrorCode PetscDeviceContextSetDefaultDeviceForType_Internal(PetscDeviceContext, PetscDeviceType);
 22: PETSC_INTERN PetscErrorCode PetscDeviceContextSetRootDeviceType_Internal(PetscDeviceType);
 23: PETSC_INTERN PetscErrorCode PetscDeviceContextSetRootStreamType_Internal(PetscStreamType);
 24: PETSC_INTERN PetscErrorCode PetscDeviceContextSyncClearMap_Internal(PetscDeviceContext);
 25: PETSC_INTERN PetscErrorCode PetscDeviceContextCheckNotOrphaned_Internal(PetscDeviceContext);

 27: // open up namespace std to specialize equal_to for unordered_map
 28: namespace std
 29: {

 31: template <>
 32: struct equal_to<PetscDeviceContext> {
 33: #if PETSC_CPP_VERSION <= 17
 34:   using result_type          = bool;
 35:   using first_argument_type  = PetscDeviceContext;
 36:   using second_argument_type = PetscDeviceContext;
 37: #endif

 39:   constexpr bool operator()(const PetscDeviceContext &x, const PetscDeviceContext &y) const noexcept { return PetscObjectCast(x)->id == PetscObjectCast(y)->id; }
 40: };

 42: } // namespace std

 44: namespace
 45: {

 47: struct CxxData {
 48:   struct parent_type {
 49:     PetscObjectId    id    = 0;
 50:     PetscObjectState state = 0;

 52:     constexpr parent_type() noexcept = default;

 54:     constexpr explicit parent_type(PetscDeviceContext dctx) noexcept : parent_type(PetscObjectCast(dctx)->id, PetscObjectCast(dctx)->state) { }

 56:     constexpr parent_type(const parent_type &) noexcept                     = default;
 57:     PETSC_CONSTEXPR_14 parent_type &operator=(const parent_type &) noexcept = default;
 58:     constexpr parent_type(parent_type &&) noexcept                          = default;
 59:     PETSC_CONSTEXPR_14 parent_type &operator=(parent_type &&) noexcept      = default;

 61:   private:
 62:     // make this private, we do not want to accept any old id and state pairing
 63:     constexpr parent_type(PetscObjectId id_, PetscObjectState state_) noexcept : id(id_), state(state_) { }
 64:   };

 66:   using upstream_type = std::unordered_map<PetscDeviceContext, parent_type>;
 67:   using dep_type      = std::unordered_set<PetscObjectId>;

 69:   // double check we didn't specialize for no reason
 70:   static_assert(std::is_same<typename upstream_type::key_equal, std::equal_to<PetscDeviceContext>>::value, "");

 72:   upstream_type upstream{};
 73:   dep_type      deps{};

 75:   PETSC_NODISCARD PetscErrorCode clear() noexcept;
 76: };

 78: inline PetscErrorCode CxxData::clear() noexcept
 79: {
 80:   this->upstream.clear();
 81:   this->deps.clear();
 82:   return 0;
 83: }

 85: PETSC_NODISCARD inline CxxData *CxxDataCast(PetscDeviceContext dctx) noexcept
 86: {
 87:   return static_cast<CxxData *>(PetscObjectCast(dctx)->cpp);
 88: }

 90: /*
 91:   needed because PetscInitialize() needs to also query these options to set the defaults. Since
 92:   it does not yet have a PetscDeviceContext to call this with, the actual options queries are
 93:   abstracted out, so you can call this without one.
 94: */
 95: inline PetscErrorCode PetscDeviceContextQueryOptions_Internal(PetscOptionItems *PetscOptionsObject, std::pair<PetscDeviceType, PetscBool> &deviceType, std::pair<PetscStreamType, PetscBool> &streamType)
 96: {
 97:   auto dtype = static_cast<PetscInt>(deviceType.first);
 98:   auto stype = static_cast<PetscInt>(streamType.first);

100:   /* set the device type first */
101:   PetscOptionsEList("-device_context_device_type", "Underlying PetscDevice", "PetscDeviceContextSetDevice", PetscDeviceTypes, PETSC_DEVICE_MAX, PetscDeviceTypes[dtype], &dtype, &deviceType.second);
102:   PetscOptionsEList("-device_context_stream_type", "PetscDeviceContext PetscStreamType", "PetscDeviceContextSetStreamType", PetscStreamTypes, PETSC_STREAM_MAX, PetscStreamTypes[stype], &stype, &streamType.second);
103:   deviceType.first = PetscDeviceTypeCast(dtype);
104:   streamType.first = PetscStreamTypeCast(stype);
105:   return 0;
106: }

108: } // anonymous namespace

110: #endif // PETSCDEVICE_INTERFACE_INTERNAL_HPP