xref: /petsc/src/sys/objects/device/interface/petscdevice_interface_internal.hpp (revision d1e78c4f2649daee4d31c45b7350b666f6b9ac81)
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 template <>
31 struct equal_to<PetscDeviceContext> {
32 #if PETSC_CPP_VERSION <= 17
33   using result_type          = bool;
34   using first_argument_type  = PetscDeviceContext;
35   using second_argument_type = PetscDeviceContext;
36 #endif
37 
38   constexpr bool operator()(const PetscDeviceContext &x, const PetscDeviceContext &y) const noexcept {
39     return PetscObjectCast(x)->id == PetscObjectCast(y)->id;
40   }
41 };
42 
43 } // namespace std
44 
45 namespace {
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   PetscFunctionBegin;
80   PetscCallCXX(this->upstream.clear());
81   PetscCallCXX(this->deps.clear());
82   PetscFunctionReturn(0);
83 }
84 
85 PETSC_NODISCARD inline CxxData *CxxDataCast(PetscDeviceContext dctx) noexcept {
86   return static_cast<CxxData *>(PetscObjectCast(dctx)->cpp);
87 }
88 
89 /*
90   needed because PetscInitialize() needs to also query these options to set the defaults. Since
91   it does not yet have a PetscDeviceContext to call this with, the actual options queries are
92   abstracted out, so you can call this without one.
93 */
94 inline PetscErrorCode PetscDeviceContextQueryOptions_Internal(PetscOptionItems *PetscOptionsObject, std::pair<PetscDeviceType, PetscBool> &deviceType, std::pair<PetscStreamType, PetscBool> &streamType) {
95   auto dtype = static_cast<PetscInt>(deviceType.first);
96   auto stype = static_cast<PetscInt>(streamType.first);
97 
98   PetscFunctionBegin;
99   /* set the device type first */
100   PetscCall(PetscOptionsEList("-device_context_device_type", "Underlying PetscDevice", "PetscDeviceContextSetDevice", PetscDeviceTypes, PETSC_DEVICE_MAX, PetscDeviceTypes[dtype], &dtype, &deviceType.second));
101   PetscCall(PetscOptionsEList("-device_context_stream_type", "PetscDeviceContext PetscStreamType", "PetscDeviceContextSetStreamType", PetscStreamTypes, PETSC_STREAM_MAX, PetscStreamTypes[stype], &stype, &streamType.second));
102   deviceType.first = PetscDeviceTypeCast(dtype);
103   streamType.first = PetscStreamTypeCast(stype);
104   PetscFunctionReturn(0);
105 }
106 
107 } // anonymous namespace
108 
109 #endif // PETSCDEVICE_INTERFACE_INTERNAL_HPP
110