|
| 1 | +//===--- _async_dec_ref.hpp - Implements async DECREF ---------------------===// |
| 2 | +// |
| 3 | +// Data Parallel Control (dpctl) |
| 4 | +// |
| 5 | +// Copyright 2026 Intel Corporation |
| 6 | +// |
| 7 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +// you may not use this file except in compliance with the License. |
| 9 | +// You may obtain a copy of the License at |
| 10 | +// |
| 11 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +// |
| 13 | +// Unless required by applicable law or agreed to in writing, software |
| 14 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +// See the License for the specific language governing permissions and |
| 17 | +// limitations under the License. |
| 18 | +// |
| 19 | +//===----------------------------------------------------------------------===// |
| 20 | +/// |
| 21 | +/// \file |
| 22 | +/// This file implements the functions that defer releasing what offloaded work |
| 23 | +/// uses until that work has completed: decrementing the reference counts of an |
| 24 | +/// array of Python objects, or calling a release function another module |
| 25 | +/// passes in. |
| 26 | +/// |
| 27 | +/// N.B.: The reference counts are dropped by the thread `dpctl` keeps for |
| 28 | +/// running deferred releases, or by a thread that drains them. |
| 29 | +/// |
| 30 | +//===----------------------------------------------------------------------===// |
| 31 | + |
| 32 | +#pragma once |
| 33 | +#include <memory> |
| 34 | +#include <stddef.h> |
| 35 | +#include <stdexcept> |
| 36 | +#include <sycl/sycl.hpp> |
| 37 | +#include <utility> |
| 38 | +#include <vector> |
| 39 | + |
| 40 | +#include "Python.h" |
| 41 | + |
| 42 | +#include "_deferred_release_watcher.hpp" |
| 43 | +#include "syclinterface/dpctl_data_types.h" |
| 44 | +#include "syclinterface/dpctl_sycl_type_casters.hpp" |
| 45 | + |
| 46 | +namespace dpctl |
| 47 | +{ |
| 48 | +namespace detail |
| 49 | +{ |
| 50 | + |
| 51 | +/*! |
| 52 | + * @brief Name of the kernel submitted by `submit_keep_alive_marker`. |
| 53 | + */ |
| 54 | +class keep_alive_marker; |
| 55 | + |
| 56 | +/*! |
| 57 | + * @brief An event that gates the release of objects used by work on `q`. |
| 58 | + * |
| 59 | + * Submits an empty kernel after `deps`. On an in-order queue the submission is |
| 60 | + * also ordered after everything already submitted there, so the event covers |
| 61 | + * uses of the objects that the caller never tracked. |
| 62 | + */ |
| 63 | +inline sycl::event |
| 64 | +submit_keep_alive_marker(sycl::queue &q, const std::vector<sycl::event> &deps) |
| 65 | +{ |
| 66 | + return q.single_task<keep_alive_marker>(deps, []() {}); |
| 67 | +} |
| 68 | + |
| 69 | +} // namespace detail |
| 70 | +} // namespace dpctl |
| 71 | + |
| 72 | +namespace |
| 73 | +{ |
| 74 | + |
| 75 | +/*! |
| 76 | + * @brief Copy the events behind `nDepERefs` references into a vector. |
| 77 | + */ |
| 78 | +std::vector<sycl::event> unwrap_events(DPCTLSyclEventRef *depERefs, |
| 79 | + size_t nDepERefs) |
| 80 | +{ |
| 81 | + using dpctl::syclinterface::unwrap; |
| 82 | + |
| 83 | + std::vector<sycl::event> depends; |
| 84 | + depends.reserve(nDepERefs); |
| 85 | + for (size_t ev_id = 0; ev_id < nDepERefs; ++ev_id) { |
| 86 | + depends.push_back(*(unwrap<sycl::event>(depERefs[ev_id]))); |
| 87 | + } |
| 88 | + |
| 89 | + return depends; |
| 90 | +} |
| 91 | + |
| 92 | +/*! |
| 93 | + * @brief The queue behind `QRef`, which must not be null. |
| 94 | + */ |
| 95 | +sycl::queue &unwrap_queue(DPCTLSyclQueueRef QRef) |
| 96 | +{ |
| 97 | + sycl::queue *q = dpctl::syclinterface::unwrap<sycl::queue>(QRef); |
| 98 | + if (!q) { |
| 99 | + throw std::invalid_argument("A queue is required"); |
| 100 | + } |
| 101 | + return *q; |
| 102 | +} |
| 103 | + |
| 104 | +/*! |
| 105 | + * @brief Schedule DECREFs of `obj_vec` for once `depends` have completed. |
| 106 | + */ |
| 107 | +void submit_dec_ref(std::vector<PyObject *> obj_vec, |
| 108 | + std::vector<sycl::event> depends) |
| 109 | +{ |
| 110 | + dpctl::detail::local_deferred_releases().defer( |
| 111 | + std::move(depends), [obj_vec = std::move(obj_vec)]() { |
| 112 | + for (PyObject *obj : obj_vec) { |
| 113 | + Py_DECREF(obj); |
| 114 | + } |
| 115 | + }); |
| 116 | +} |
| 117 | + |
| 118 | +} // namespace |
| 119 | + |
| 120 | +/*! |
| 121 | + * @brief Schedule DECREFs of `obj_array` for once `obj_array` is out of use. |
| 122 | + * |
| 123 | + * An empty kernel is submitted to `QRef` after `depERefs`, and the DECREFs wait |
| 124 | + * for it. On an in-order queue submission also orders it after the work already |
| 125 | + * submitted there, so the objects are covered regardless of providing dependent |
| 126 | + * events. `QRef` should be the queue running the work that uses the objects. |
| 127 | + * An event for the kernel is returned. |
| 128 | + * |
| 129 | + * Sets `*status` to 0 on success and 1 if scheduling threw, and returns nullptr |
| 130 | + * on failure. A failed call has scheduled nothing, so the caller still owns the |
| 131 | + * references it took. |
| 132 | + */ |
| 133 | +inline DPCTLSyclEventRef async_dec_ref(DPCTLSyclQueueRef QRef, |
| 134 | + PyObject **obj_array, |
| 135 | + size_t obj_array_size, |
| 136 | + DPCTLSyclEventRef *depERefs, |
| 137 | + size_t nDepERefs, |
| 138 | + int *status) |
| 139 | +{ |
| 140 | + using dpctl::syclinterface::wrap; |
| 141 | + |
| 142 | + try { |
| 143 | + sycl::queue &q = unwrap_queue(QRef); |
| 144 | + |
| 145 | + std::vector<sycl::event> depends = unwrap_events(depERefs, nDepERefs); |
| 146 | + std::vector<PyObject *> obj_vec(obj_array, obj_array + obj_array_size); |
| 147 | + |
| 148 | + static constexpr int result_ok = 0; |
| 149 | + |
| 150 | + const sycl::event marker = |
| 151 | + dpctl::detail::submit_keep_alive_marker(q, depends); |
| 152 | + |
| 153 | + // allocated before scheduling, as failing afterwards could not be |
| 154 | + // reported: the caller would drop a reference the scheduled DECREFs own |
| 155 | + std::unique_ptr<sycl::event> e_ptr(new sycl::event(marker)); |
| 156 | + |
| 157 | + submit_dec_ref(std::move(obj_vec), {marker}); |
| 158 | + |
| 159 | + *status = result_ok; |
| 160 | + |
| 161 | + return wrap<sycl::event>(e_ptr.release()); |
| 162 | + } catch (...) { |
| 163 | + // no exception may escape into the calling Cython code |
| 164 | + static constexpr int result_exception = 1; |
| 165 | + *status = result_exception; |
| 166 | + return nullptr; |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +/*! |
| 171 | + * @brief Call `release(data)` once the work using what it releases is done. |
| 172 | + * |
| 173 | + * Submits an empty kernel to `QRef` after `depERefs`, as `async_dec_ref` does, |
| 174 | + * and assigns its event to the `sycl::event` behind `markerERef`. `release` is |
| 175 | + * called with the GIL held, on `dpctl`'s thread or on one that drains. |
| 176 | + * |
| 177 | + * @return 0 on success, and 1 if nothing was deferred, in which case `release` |
| 178 | + * is never called. |
| 179 | + */ |
| 180 | +inline int defer_release(DPCTLSyclQueueRef QRef, |
| 181 | + DPCTLSyclEventRef *depERefs, |
| 182 | + size_t nDepERefs, |
| 183 | + void (*release)(void *), |
| 184 | + void *data, |
| 185 | + DPCTLSyclEventRef markerERef) |
| 186 | +{ |
| 187 | + using dpctl::syclinterface::unwrap; |
| 188 | + |
| 189 | + try { |
| 190 | + sycl::queue &q = unwrap_queue(QRef); |
| 191 | + sycl::event *marker = unwrap<sycl::event>(markerERef); |
| 192 | + if (!release || !marker) { |
| 193 | + throw std::invalid_argument("A release and an event are required"); |
| 194 | + } |
| 195 | + |
| 196 | + *marker = dpctl::detail::submit_keep_alive_marker( |
| 197 | + q, unwrap_events(depERefs, nDepERefs)); |
| 198 | + |
| 199 | + dpctl::detail::local_deferred_releases().defer( |
| 200 | + {*marker}, [release, data]() { release(data); }); |
| 201 | + |
| 202 | + return 0; |
| 203 | + } catch (...) { |
| 204 | + // no exception may escape into the calling Cython code |
| 205 | + return 1; |
| 206 | + } |
| 207 | +} |
0 commit comments