1 #ifndef PETSCDEVICE_INTERFACE_INTERNAL_HPP 2 #define PETSCDEVICE_INTERFACE_INTERNAL_HPP 3 4 #include <petsc/private/deviceimpl.h> 5 6 #include <petsc/private/cpp/utility.hpp> // std::pair 7 #include <petsc/private/cpp/functional.hpp> //std::equal_to 8 9 #include <unordered_map> 10 #include <unordered_set> 11 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 18 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); 26 27 // open up namespace std to specialize equal_to for unordered_map 28 namespace std 29 { 30 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 38 39 constexpr bool operator()(const PetscDeviceContext &x, const PetscDeviceContext &y) const noexcept { return PetscObjectCast(x)->id == PetscObjectCast(y)->id; } 40 }; 41 42 } // namespace std 43 44 namespace 45 { 46 47 struct CxxData { 48 struct parent_type { 49 PetscObjectId id = 0; 50 PetscObjectState state = 0; 51 52 constexpr parent_type() noexcept = default; 53 54 constexpr explicit parent_type(PetscDeviceContext dctx) noexcept : parent_type(PetscObjectCast(dctx)->id, PetscObjectCast(dctx)->state) { } 55 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; 60 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 }; 65 66 using upstream_type = std::unordered_map<PetscDeviceContext, parent_type>; 67 using dep_type = std::unordered_set<PetscObjectId>; 68 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, ""); 71 72 upstream_type upstream{}; 73 dep_type deps{}; 74 75 PETSC_NODISCARD PetscErrorCode clear() noexcept; 76 }; 77 78 inline PetscErrorCode CxxData::clear() noexcept 79 { 80 PetscFunctionBegin; 81 PetscCallCXX(this->upstream.clear()); 82 PetscCallCXX(this->deps.clear()); 83 PetscFunctionReturn(0); 84 } 85 86 PETSC_NODISCARD inline CxxData *CxxDataCast(PetscDeviceContext dctx) noexcept 87 { 88 return static_cast<CxxData *>(PetscObjectCast(dctx)->cpp); 89 } 90 91 /* 92 needed because PetscInitialize() needs to also query these options to set the defaults. Since 93 it does not yet have a PetscDeviceContext to call this with, the actual options queries are 94 abstracted out, so you can call this without one. 95 */ 96 inline PetscErrorCode PetscDeviceContextQueryOptions_Internal(PetscOptionItems *PetscOptionsObject, std::pair<PetscDeviceType, PetscBool> &deviceType, std::pair<PetscStreamType, PetscBool> &streamType) 97 { 98 auto dtype = static_cast<PetscInt>(deviceType.first); 99 auto stype = static_cast<PetscInt>(streamType.first); 100 101 PetscFunctionBegin; 102 /* set the device type first */ 103 PetscCall(PetscOptionsEList("-device_context_device_type", "Underlying PetscDevice", "PetscDeviceContextSetDevice", PetscDeviceTypes, PETSC_DEVICE_MAX, PetscDeviceTypes[dtype], &dtype, &deviceType.second)); 104 PetscCall(PetscOptionsEList("-device_context_stream_type", "PetscDeviceContext PetscStreamType", "PetscDeviceContextSetStreamType", PetscStreamTypes, PETSC_STREAM_MAX, PetscStreamTypes[stype], &stype, &streamType.second)); 105 deviceType.first = PetscDeviceTypeCast(dtype); 106 streamType.first = PetscStreamTypeCast(stype); 107 PetscFunctionReturn(0); 108 } 109 110 } // anonymous namespace 111 112 #endif // PETSCDEVICE_INTERFACE_INTERNAL_HPP 113