1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
<center><a href="https://gitlab.com/petsc/petsc/-/blob/9fe822445bcdd45fb93170ff68fa7403d3f52f09/src/sys/objects/device/impls/cupm/cupmevent.hpp">Actual source code: cupmevent.hpp</a></center><br>
<html>
<head>
<title></title>
<meta name="generator" content="c2html 0.9.6">
<meta name="date" content="2025-03-28T21:11:12+00:00">
</head>
<body bgcolor="#FFFFFF">
<pre width=80>
<a name="line1"> 1: </a><font color="#A020F0">#pragma once</font>
<a name="line3"> 3: </a>#include <A href="../../../../include/petsc/private/cupminterface.hpp.html"><petsc/private/cupminterface.hpp></A>
<a name="line4"> 4: </a>#include <A href="../../../../include/petsc/private/cpp/memory.hpp.html"><petsc/private/cpp/memory.hpp></A>
<a name="line5"> 5: </a>#include <A href="../../../../include/petsc/private/cpp/object_pool.hpp.html"><petsc/private/cpp/object_pool.hpp></A>
<a name="line7"> 7: </a><font color="#A020F0">#include <stack></font>
<a name="line9"> 9: </a>namespace Petsc
<a name="line10"> 10: </a>{
<a name="line12"> 12: </a>namespace device
<a name="line13"> 13: </a>{
<a name="line15"> 15: </a>namespace cupm
<a name="line16"> 16: </a>{
<a name="line18"> 18: </a><font color="#B22222">// A pool for allocating cupmEvent_t's. While events are generally very cheap to create and</font>
<a name="line19"> 19: </a><font color="#B22222">// destroy, they are not free. Using the pool vs on-demand creation and destruction yields a ~20%</font>
<a name="line20"> 20: </a><font color="#B22222">// speedup.</font>
<a name="line21"> 21: </a>template <DeviceType T, unsigned long flags>
<a name="line22"> 22: </a>class PETSC_SINGLE_LIBRARY_VISIBILITY_INTERNAL CUPMEventPool : impl::Interface<T>, public RegisterFinalizeable<CUPMEventPool<T, flags>> {
<a name="line23"> 23: </a>public:
<a name="line24"> 24: </a> PETSC_CUPM_INHERIT_INTERFACE_TYPEDEFS_USING(T);
<a name="line26"> 26: </a> <a href="../../../../../../manualpages/Sys/PetscErrorCode.html">PetscErrorCode</a> allocate(cupmEvent_t *) noexcept;
<a name="line27"> 27: </a> <a href="../../../../../../manualpages/Sys/PetscErrorCode.html">PetscErrorCode</a> deallocate(cupmEvent_t *) noexcept;
<a name="line29"> 29: </a> <a href="../../../../../../manualpages/Sys/PetscErrorCode.html">PetscErrorCode</a> finalize_() noexcept;
<a name="line31"> 31: </a>private:
<a name="line32"> 32: </a> std::stack<cupmEvent_t> pool_;
<a name="line33"> 33: </a>};
<a name="line35"> 35: </a>template <DeviceType T, unsigned long flags>
<a name="line36"> 36: </a>inline <a href="../../../../../../manualpages/Sys/PetscErrorCode.html">PetscErrorCode</a> CUPMEventPool<T, flags>::finalize_() noexcept
<a name="line37"> 37: </a>{
<a name="line38"> 38: </a> <a href="../../../../../../manualpages/Sys/PetscFunctionBegin.html">PetscFunctionBegin</a>;
<a name="line39"> 39: </a> <font color="#4169E1">while</font> (!pool_.empty()) {
<a name="line40"> 40: </a> PetscCallCUPM(cupmEventDestroy(std::move(pool_.top())));
<a name="line41"> 41: </a> <a href="../../../../../../manualpages/Sys/PetscCallCXX.html">PetscCallCXX</a>(pool_.pop());
<a name="line42"> 42: </a> }
<a name="line43"> 43: </a> <a href="../../../../../../manualpages/Sys/PetscFunctionReturn.html">PetscFunctionReturn</a>(<a href="../../../../../../manualpages/Sys/PetscErrorCode.html">PETSC_SUCCESS</a>);
<a name="line44"> 44: </a>}
<a name="line46"> 46: </a>template <DeviceType T, unsigned long flags>
<a name="line47"> 47: </a>inline <a href="../../../../../../manualpages/Sys/PetscErrorCode.html">PetscErrorCode</a> CUPMEventPool<T, flags>::allocate(cupmEvent_t *event) noexcept
<a name="line48"> 48: </a>{
<a name="line49"> 49: </a> <a href="../../../../../../manualpages/Sys/PetscFunctionBegin.html">PetscFunctionBegin</a>;
<a name="line50"> 50: </a> PetscAssertPointer(event, 1);
<a name="line51"> 51: </a> <font color="#4169E1">if</font> (pool_.empty()) {
<a name="line52"> 52: </a> <a href="../../../../../../manualpages/Sys/PetscCall.html">PetscCall</a>(this->register_finalize());
<a name="line53"> 53: </a> PetscCallCUPM(cupmEventCreateWithFlags(event, (unsigned int)flags));
<a name="line54"> 54: </a> } <font color="#4169E1">else</font> {
<a name="line55"> 55: </a> <a href="../../../../../../manualpages/Sys/PetscCallCXX.html">PetscCallCXX</a>(*event = std::move(pool_.top()));
<a name="line56"> 56: </a> <a href="../../../../../../manualpages/Sys/PetscCallCXX.html">PetscCallCXX</a>(pool_.pop());
<a name="line57"> 57: </a> }
<a name="line58"> 58: </a> <a href="../../../../../../manualpages/Sys/PetscFunctionReturn.html">PetscFunctionReturn</a>(<a href="../../../../../../manualpages/Sys/PetscErrorCode.html">PETSC_SUCCESS</a>);
<a name="line59"> 59: </a>}
<a name="line61"> 61: </a>template <DeviceType T, unsigned long flags>
<a name="line62"> 62: </a>inline <a href="../../../../../../manualpages/Sys/PetscErrorCode.html">PetscErrorCode</a> CUPMEventPool<T, flags>::deallocate(cupmEvent_t *in_event) noexcept
<a name="line63"> 63: </a>{
<a name="line64"> 64: </a> <a href="../../../../../../manualpages/Sys/PetscFunctionBegin.html">PetscFunctionBegin</a>;
<a name="line65"> 65: </a> PetscAssertPointer(in_event, 1);
<a name="line66"> 66: </a> <font color="#4169E1">if</font> (auto event = std::exchange(*in_event, cupmEvent_t{})) {
<a name="line67"> 67: </a> <font color="#4169E1">if</font> (this->registered()) {
<a name="line68"> 68: </a> <a href="../../../../../../manualpages/Sys/PetscCallCXX.html">PetscCallCXX</a>(pool_.push(std::move(event)));
<a name="line69"> 69: </a> } <font color="#4169E1">else</font> {
<a name="line70"> 70: </a> PetscCallCUPM(cupmEventDestroy(event));
<a name="line71"> 71: </a> }
<a name="line72"> 72: </a> }
<a name="line73"> 73: </a> <a href="../../../../../../manualpages/Sys/PetscFunctionReturn.html">PetscFunctionReturn</a>(<a href="../../../../../../manualpages/Sys/PetscErrorCode.html">PETSC_SUCCESS</a>);
<a name="line74"> 74: </a>}
<a name="line76"> 76: </a>template <DeviceType T, unsigned long flags>
<a name="line77"> 77: </a>CUPMEventPool<T, flags> &cupm_event_pool() noexcept
<a name="line78"> 78: </a>{
<a name="line79"> 79: </a> static CUPMEventPool<T, flags> pool;
<a name="line80"> 80: </a> <font color="#4169E1">return</font> pool;
<a name="line81"> 81: </a>}
<a name="line83"> 83: </a><font color="#B22222">// pool of events with timing disabled</font>
<a name="line84"> 84: </a>template <DeviceType T>
<a name="line85"> 85: </a><strong><font color="#4169E1"><a name="cupm_fast_event_pool"></a>inline auto cupm_fast_event_pool()</font></strong> noexcept -> decltype(cupm_event_pool<T, impl::Interface<T>::cupmEventDisableTiming>()) &
<a name="line86"> 86: </a>{
<a name="line87"> 87: </a> <font color="#4169E1">return</font> cupm_event_pool<T, impl::Interface<T>::cupmEventDisableTiming>();
<a name="line88"> 88: </a>}
<a name="line90"> 90: </a><font color="#B22222">// pool of events with timing enabled</font>
<a name="line91"> 91: </a>template <DeviceType T>
<a name="line92"> 92: </a><strong><font color="#4169E1"><a name="cupm_timer_event_pool"></a>inline auto cupm_timer_event_pool()</font></strong> noexcept -> decltype(cupm_event_pool<T, impl::Interface<T>::cupmEventDefault>()) &
<a name="line93"> 93: </a>{
<a name="line94"> 94: </a> <font color="#4169E1">return</font> cupm_event_pool<T, impl::Interface<T>::cupmEventDefault>();
<a name="line95"> 95: </a>}
<a name="line97"> 97: </a><font color="#B22222">// A simple wrapper of cupmEvent_t. This is used in conjunction with CUPMStream to build the</font>
<a name="line98"> 98: </a><font color="#B22222">// event-stream pairing for the async allocator. It is also used as the data member of</font>
<a name="line99"> 99: </a><font color="#B22222">// PetscEvent.</font>
<a name="line100">100: </a>template <DeviceType T>
<a name="line101">101: </a>class PETSC_SINGLE_LIBRARY_VISIBILITY_INTERNAL CUPMEvent : impl::Interface<T>, public memory::PoolAllocated {
<a name="line102">102: </a> using pool_type = memory::PoolAllocated;
<a name="line104">104: </a>public:
<a name="line105">105: </a> PETSC_CUPM_INHERIT_INTERFACE_TYPEDEFS_USING(T);
<a name="line107">107: </a> constexpr CUPMEvent() noexcept = <font color="#4169E1">default</font>;
<a name="line108">108: </a> ~CUPMEvent() noexcept;
<a name="line110">110: </a> CUPMEvent(CUPMEvent &&) noexcept;
<a name="line111">111: </a> CUPMEvent &operator=(CUPMEvent &&) noexcept;
<a name="line113">113: </a> <font color="#B22222">// event is not copyable</font>
<a name="line114">114: </a> CUPMEvent(const CUPMEvent &) = delete;
<a name="line115">115: </a> CUPMEvent &operator=(const CUPMEvent &) = delete;
<a name="line117">117: </a> <a href="../../../../../../manualpages/Sys/PETSC_NODISCARD.html">PETSC_NODISCARD</a> cupmEvent_t get() noexcept;
<a name="line118">118: </a> <a href="../../../../../../manualpages/Sys/PetscErrorCode.html">PetscErrorCode</a> record(cupmStream_t) noexcept;
<a name="line120">120: </a> explicit operator bool() const noexcept;
<a name="line122">122: </a>private:
<a name="line123">123: </a> cupmEvent_t event_{};
<a name="line124">124: </a>};
<a name="line126">126: </a>template <DeviceType T>
<a name="line127">127: </a>inline CUPMEvent<T>::~CUPMEvent() noexcept
<a name="line128">128: </a>{
<a name="line129">129: </a> <a href="../../../../../../manualpages/Sys/PetscFunctionBegin.html">PetscFunctionBegin</a>;
<a name="line130">130: </a> <a href="../../../../../../manualpages/Sys/PetscCallAbort.html">PetscCallAbort</a>(<a href="../../../../../../manualpages/Sys/PETSC_COMM_SELF.html">PETSC_COMM_SELF</a>, cupm_fast_event_pool<T>().deallocate(&event_));
<a name="line131">131: </a> <a href="../../../../../../manualpages/Sys/PetscFunctionReturnVoid.html">PetscFunctionReturnVoid</a>();
<a name="line132">132: </a>}
<a name="line134">134: </a>template <DeviceType T>
<a name="line135">135: </a>inline CUPMEvent<T>::CUPMEvent(CUPMEvent &&other) noexcept : pool_type(std::move(other)), event_(util::exchange(other.event_, cupmEvent_t{}))
<a name="line136">136: </a>{
<a name="line137">137: </a> static_assert(std::is_empty<impl::Interface<T>>::value, <font color="#666666">""</font>);
<a name="line138">138: </a>}
<a name="line140">140: </a>template <DeviceType T>
<a name="line141">141: </a>inline CUPMEvent<T> &CUPMEvent<T>::operator=(CUPMEvent &&other) noexcept
<a name="line142">142: </a>{
<a name="line143">143: </a> <a href="../../../../../../manualpages/Sys/PetscFunctionBegin.html">PetscFunctionBegin</a>;
<a name="line144">144: </a> <font color="#4169E1">if</font> (this != &other) {
<a name="line145">145: </a> pool_type::operator=(std::move(other));
<a name="line146">146: </a> <a href="../../../../../../manualpages/Sys/PetscCallAbort.html">PetscCallAbort</a>(<a href="../../../../../../manualpages/Sys/PETSC_COMM_SELF.html">PETSC_COMM_SELF</a>, cupm_fast_event_pool<T>().deallocate(&event_));
<a name="line147">147: </a> event_ = util::exchange(other.event_, cupmEvent_t{});
<a name="line148">148: </a> }
<a name="line149">149: </a> <a href="../../../../../../manualpages/Sys/PetscFunctionReturn.html">PetscFunctionReturn</a>(*this);
<a name="line150">150: </a>}
<a name="line152">152: </a>template <DeviceType T>
<a name="line153">153: </a>inline typename CUPMEvent<T>::cupmEvent_t CUPMEvent<T>::get() noexcept
<a name="line154">154: </a>{
<a name="line155">155: </a> <a href="../../../../../../manualpages/Sys/PetscFunctionBegin.html">PetscFunctionBegin</a>;
<a name="line156">156: </a> <font color="#4169E1">if</font> (<a href="../../../../../../manualpages/Sys/PetscUnlikely.html">PetscUnlikely</a>(!event_)) <a href="../../../../../../manualpages/Sys/PetscCallAbort.html">PetscCallAbort</a>(<a href="../../../../../../manualpages/Sys/PETSC_COMM_SELF.html">PETSC_COMM_SELF</a>, cupm_fast_event_pool<T>().allocate(&event_));
<a name="line157">157: </a> <a href="../../../../../../manualpages/Sys/PetscFunctionReturn.html">PetscFunctionReturn</a>(event_);
<a name="line158">158: </a>}
<a name="line160">160: </a>template <DeviceType T>
<a name="line161">161: </a>inline <a href="../../../../../../manualpages/Sys/PetscErrorCode.html">PetscErrorCode</a> CUPMEvent<T>::record(cupmStream_t stream) noexcept
<a name="line162">162: </a>{
<a name="line163">163: </a> <a href="../../../../../../manualpages/Sys/PetscFunctionBegin.html">PetscFunctionBegin</a>;
<a name="line164">164: </a> PetscCallCUPM(cupmEventRecord(get(), stream));
<a name="line165">165: </a> <a href="../../../../../../manualpages/Sys/PetscFunctionReturn.html">PetscFunctionReturn</a>(<a href="../../../../../../manualpages/Sys/PetscErrorCode.html">PETSC_SUCCESS</a>);
<a name="line166">166: </a>}
<a name="line168">168: </a>template <DeviceType T>
<a name="line169">169: </a>inline CUPMEvent<T>::operator bool() const noexcept
<a name="line170">170: </a>{
<a name="line171">171: </a> <font color="#4169E1">return</font> event_ != cupmEvent_t{};
<a name="line172">172: </a>}
<a name="line174">174: </a>} <font color="#B22222">// namespace cupm</font>
<a name="line176">176: </a>} <font color="#B22222">// namespace device</font>
<a name="line178">178: </a>} <font color="#B22222">// namespace Petsc</font>
</pre>
</body>
</html>
|