From c8f469ff8b9c3ccfc93994e488b43110b39fed96 Mon Sep 17 00:00:00 2001 From: Nikita Grigorian Date: Thu, 3 Dec 2026 14:56:34 -0800 Subject: [PATCH 1/3] Keep libtensor arguments alive through dpctl's deferred releases dpctl 0.24.0dev0 (IntelPython/dpctl#2359) no longer uses `host_task` to keep Python objects alive during offload: `dpctl::utils::keep_args_alive` now hands a release callback to `SyclQueue_DeferRelease`, which runs it with the GIL held once an empty kernel submitted after the dependencies completes. `dpnp::utils::keep_args_alive` in `dpnp4pybind11.hpp` reimplements the dpctl helper so that it also recognizes dpnp's `usm_ndarray`, and still submitted up to two host tasks: one holding the shared pointers owning USM allocations, and one taking the GIL to decref every other object. It now fills a `detail::held_args` the same way and defers its release with the public `dpctl::utils::defer_release`. The returned event is that of the empty kernel, so its completion means that the arguments are no longer in use rather than that they have been released. The build requirement in pyproject.toml keeps dpctl 0.23.0, the latest on PyPI, with a TODO until dpctl 0.24.0 is released. test_vm_gil.py no longer describes host tasks: it now checks that the work keeping arguments alive needs no GIL. --- CHANGELOG.md | 1 + dpnp/include/dpnp4pybind11.hpp | 97 +++++++++++++++------------------- dpnp/libtensor/AGENTS.md | 2 +- dpnp/tests/test_vm_gil.py | 17 +++--- pyproject.toml | 1 + 5 files changed, 55 insertions(+), 63 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3957a444ffcf..f1836e2e01a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ This release drops support for Python 3.10, making Python 3.11 the minimum requi * Changed the CI to build and test the conda packages of `conda-recipe` and the wheels with Intel CI (`ci/`, `.github/workflows/intel-ci.yml`); the conda packages build against conda-forge and the wheels against PyPI, both with `dpctl` dev builds from `dppy/label/dev`, and the wheels declare the oneAPI runtime wheels in their metadata [#3151](https://github.com/IntelPython/dpnp/pull/3151) * Changed the install instructions in `README.md` and the quick start guide to conda-forge and PyPI [#3151](https://github.com/IntelPython/dpnp/pull/3151) * Changed the conda recipe's tests to `pip check`, the import and `python -m dpctl -f`, as in the conda-forge feedstock, so `conda build` and `conda build --test` no longer run the test suites and no longer read `SKIP_TENSOR_TESTS`; run the suites with `ci/hooks.py`, or with `python -m pytest --pyargs dpnp.tests` in an environment with dpnp installed [#3151](https://github.com/IntelPython/dpnp/pull/3151) +* Changed `dpnp::utils::keep_args_alive` in `dpnp4pybind11.hpp` to defer the release of its arguments with `dpctl::utils::defer_release` instead of submitting a `host_task`, so the returned event is now for an empty kernel and its completion means that the arguments are no longer in use rather than that they have been released [#3224](https://github.com/IntelPython/dpnp/pull/3224) ### Deprecated diff --git a/dpnp/include/dpnp4pybind11.hpp b/dpnp/include/dpnp4pybind11.hpp index 690fbe9f3d0b..001728fcdde9 100644 --- a/dpnp/include/dpnp4pybind11.hpp +++ b/dpnp/include/dpnp4pybind11.hpp @@ -648,10 +648,6 @@ namespace utils { namespace detail { -// TODO: future version of dpctl will include a more general way of passing -// shared_ptrs to keep_args_alive, so that future overload can be used here -// instead of reimplementing keep_args_alive - struct ManagedMemory { // TODO: do we need to check for memory here? Or can we assume only @@ -688,74 +684,67 @@ struct ManagedMemory "Attempted extraction of shared_ptr on an unrecognized type"); } }; + +/*! @brief What `keep_args_alive` holds for the work using its objects. */ +template +struct held_args +{ + std::size_t n_usm_owners = 0; + std::array, num> usm_owners{}; + std::size_t n_objects = 0; + std::array objects{}; + + /*! @brief Drop what `self` holds, and delete it. Called with the GIL. */ + static void release(void *self) + { + std::unique_ptr held(static_cast(self)); + + for (std::size_t i = 0; i < held->n_objects; ++i) { + held->objects[i].dec_ref(); + } + } +}; } // end of namespace detail +/*! + * @brief Keeps `py_objs` alive until the work covered by `depends` completes. + * + * A usm_ndarray or USM memory object is held through the shared pointer owning + * its allocation, and any other object through a reference. The release is + * deferred with `dpctl::utils::defer_release`, which runs it with the GIL held + * once the work covered by `depends` completes. + * + * Returns the event of the empty kernel the release waits for, so its + * completion means that `py_objs` are no longer in use, not that they have + * been released. + * + * Expects the caller to hold the GIL. + */ template sycl::event keep_args_alive(sycl::queue &q, const py::object (&py_objs)[num], const std::vector &depends = {}) { - std::size_t n_objects_held = 0; - std::array, num> shp_arr{}; + using held_t = detail::held_args; - std::size_t n_usm_owners_held = 0; - std::array, num> shp_usm{}; + // leaked if anything below throws, as the work in `depends` may still be + // using what it holds + held_t *held = new held_t{}; for (std::size_t i = 0; i < num; ++i) { const auto &py_obj_i = py_objs[i]; if (detail::ManagedMemory::is_usm_managed_by_shared_ptr(py_obj_i)) { - const auto &shp = + held->usm_owners[held->n_usm_owners] = detail::ManagedMemory::extract_shared_ptr(py_obj_i); - shp_usm[n_usm_owners_held] = shp; - ++n_usm_owners_held; + ++held->n_usm_owners; } else { - shp_arr[n_objects_held] = std::make_shared(py_obj_i); - shp_arr[n_objects_held]->inc_ref(); - ++n_objects_held; + held->objects[held->n_objects] = py_obj_i.inc_ref(); + ++held->n_objects; } } - bool use_depends = true; - sycl::event host_task_ev; - - if (n_usm_owners_held > 0) { - host_task_ev = q.submit([&](sycl::handler &cgh) { - if (use_depends) { - cgh.depends_on(depends); - use_depends = false; - } - else { - cgh.depends_on(host_task_ev); - } - cgh.host_task([shp_usm = std::move(shp_usm)]() { - // no body, but shared pointers are captured in - // the lambda, ensuring that USM allocation is - // kept alive - }); - }); - } - - if (n_objects_held > 0) { - host_task_ev = q.submit([&](sycl::handler &cgh) { - if (use_depends) { - cgh.depends_on(depends); - use_depends = false; - } - else { - cgh.depends_on(host_task_ev); - } - cgh.host_task([n_objects_held, shp_arr = std::move(shp_arr)]() { - py::gil_scoped_acquire acquire; - - for (std::size_t i = 0; i < n_objects_held; ++i) { - shp_arr[i]->dec_ref(); - } - }); - }); - } - - return host_task_ev; + return ::dpctl::utils::defer_release(q, depends, &held_t::release, held); } // add to namespace for convenience diff --git a/dpnp/libtensor/AGENTS.md b/dpnp/libtensor/AGENTS.md index 1c81104d5747..84deb2bd05c1 100644 --- a/dpnp/libtensor/AGENTS.md +++ b/dpnp/libtensor/AGENTS.md @@ -17,7 +17,7 @@ A new extension's `CMakeLists.txt` lists its sources and calls `build_dpnp_pybin ## Rules for Changes - C++20. Include what you use (``, ``, ...), use `std::`-qualified fixed-width types (`std::int64_t`, `std::size_t`), make by-value parameters and locals `const` (`constexpr` where possible), and pass non-output parameters as `const T &`. - Headers use `#pragma once`. New files in `include/` and `source/` add a `/// \file` block after the license header. -- Python-facing functions take `sycl::queue &exec_q` and `const std::vector &depends`, and return `std::pair` (keep-alive host-task event, compute event). Keep arguments alive with `dpnp::utils::keep_args_alive`. +- Python-facing functions take `sycl::queue &exec_q` and `const std::vector &depends`, and return `std::pair` (keep-alive event, compute event). Keep arguments alive with `dpnp::utils::keep_args_alive`. - Validate inputs before launching: queue compatibility, contiguity, writability, memory overlap (including the same logical tensor), dtype and ndim. Reuse the helpers neighbouring files use: `check_queue`, `check_c_contig`, `check_no_overlap`, `check_writable` from `include/ext/validation_utils.hpp` (`putmask`, `statistics`, `isclose`, `interpolate`), or `dpnp::utils::queues_are_compatible`, `dpnp::tensor::overlap::MemoryOverlap` and `dpnp::tensor::validation::CheckWritable` (extensions and the Array API functions). Don't silently accept strided input. - Throw `py::value_error` for bad arguments and `std::runtime_error` for internal failures; catch oneMKL and SYCL exceptions and rethrow them. - Guard double, complex and half with `dpnp::tensor::type_utils::validate_type_for_device(q)`; don't use double literals or casts in kernels that must run without fp64. diff --git a/dpnp/tests/test_vm_gil.py b/dpnp/tests/test_vm_gil.py index 8dccdbbd26b1..50b12f58fcfc 100644 --- a/dpnp/tests/test_vm_gil.py +++ b/dpnp/tests/test_vm_gil.py @@ -1,9 +1,9 @@ """oneMKL VM calls in the VM extension must release the GIL. On a CPU device oneMKL VM waits on the dependent events and computes on the -calling thread. On an in-order queue those events are ordered after the host -tasks dpnp submits to keep arguments alive, and these host tasks acquire the -GIL, so a VM call holding the GIL never returns. +calling thread. On an in-order queue those events are ordered after the work +dpnp submits to keep arguments alive, so if that work needed the GIL, a VM call +holding it would never return. """ import subprocess @@ -15,7 +15,7 @@ import dpnp.libtensor._vm_impl as vmi # Prints "novm" if oneMKL VM is not used for the subtraction, or "ok". -_VM_AFTER_HOST_TASKS = """ +_VM_AFTER_KEEP_ARGS_ALIVE = """ import dpctl, dpnp import dpnp.libtensor._vm_impl as vmi q = dpctl.SyclQueue("cpu", property="in_order") @@ -26,7 +26,7 @@ print("novm") raise SystemExit for _ in range(100): - xs = xp[dpnp.argsort(xp)] # submits host tasks keeping arguments alive + xs = xp[dpnp.argsort(xp)] # keeps arguments alive xs[-1:] - xs[:1] q.wait() print("ok") @@ -47,10 +47,10 @@ def _cpu_device_with_fp64(): _cpu_device_with_fp64() is None, reason="requires a CPU device with fp64 support", ) -def test_in_order_queue_after_host_tasks(): +def test_in_order_queue_after_keep_args_alive(): try: res = subprocess.run( - [sys.executable, "-c", _VM_AFTER_HOST_TASKS], + [sys.executable, "-c", _VM_AFTER_KEEP_ARGS_ALIVE], capture_output=True, text=True, timeout=_TIMEOUT, @@ -58,7 +58,8 @@ def test_in_order_queue_after_host_tasks(): except subprocess.TimeoutExpired: pytest.fail( f"no progress in {_TIMEOUT}s: a oneMKL VM call holds the GIL " - "while waiting on events behind host tasks. It needs " + "while waiting on events behind work keeping arguments alive. " + "It needs " "py::gil_scoped_release." ) assert res.returncode == 0, res.stderr diff --git a/pyproject.toml b/pyproject.toml index 2974429ccb44..e923315ac329 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,6 +6,7 @@ requires = [ "cmake>=3.31.6", "cython>=3.1.0", # WARNING: use only dpctl version available on PyPi + # TODO: raise to 0.24.0 once released, which dpnp::utils::keep_args_alive needs for dpctl::utils::defer_release "dpctl>=0.23.0", # dpctl's import at build time needs them, which its dev wheels don't declare "intel-cmplr-lib-rt", From 6177b74062ba452f588175d5e5b2b818791b7823 Mon Sep 17 00:00:00 2001 From: Nikita Grigorian Date: Mon, 9 Nov 2026 23:11:11 -0800 Subject: [PATCH 2/3] Drop host_task from libtensor in favor of deferred releases libtensor and the `lapack` and `sparse` extensions used `host_task` to keep host data alive until copies complete and to free USM temporaries and scratchpads once the work using them is done. A `host_task` may not free USM per the SYCL spec, and AdaptiveCpp does not implement it. `dpnp::tensor::alloc_utils::async_release(q, depends, objs...)` in `utils/sycl_alloc_utils.hpp` moves the objects into a release deferred with `dpctl::utils::defer_release`, which destroys them once the work covered by `depends` completes. If the release cannot be deferred, it waits for that work before rethrowing, so nothing in use is freed. `async_smart_free` and `device_allocate_and_pack` are built on it, and the remaining sites in `choose`, `integer_advanced_indexing`, `triul_ctor`, the LAPACK routines (including `potrs`) and sparse `gemv` call it directly. The events these return are now those of an empty kernel, so the vectors and variables holding them are renamed from `host_task_events`, `host_tasks`, `ht_ev` and the like to `cleanup_events`, `cleanup_ev`, `cleanup_event` and `keep_args_alive_ev`. With the previous commit alone, the GPU test suite hung intermittently on an Intel iGPU with the default Level Zero adapter, and a loop of strided binary operations lost the device after about 2M calls. With this commit, the same loop completes 3M calls and the suite passes. The loop also completes on the v2 adapter without this commit, so the hang appears to be in the v1 adapter; a standalone SYCL loop of a copy followed by a kernel depending on it stalls there too. --- CHANGELOG.md | 1 + .../extensions/lapack/evd_batch_common.hpp | 4 +- .../extensions/lapack/evd_common.hpp | 4 +- dpnp/libtensor/extensions/lapack/geqrf.cpp | 22 ++-- .../extensions/lapack/geqrf_batch.cpp | 22 ++-- dpnp/libtensor/extensions/lapack/gesv.cpp | 21 ++-- .../extensions/lapack/gesv_batch.cpp | 32 ++--- dpnp/libtensor/extensions/lapack/gesvd.cpp | 19 ++- .../extensions/lapack/gesvd_batch.cpp | 26 +++-- dpnp/libtensor/extensions/lapack/getrf.cpp | 22 ++-- .../extensions/lapack/getrf_batch.cpp | 22 ++-- .../extensions/lapack/getri_batch.cpp | 22 ++-- dpnp/libtensor/extensions/lapack/getrs.cpp | 22 ++-- .../extensions/lapack/getrs_batch.cpp | 22 ++-- dpnp/libtensor/extensions/lapack/heevd.cpp | 15 ++- .../extensions/lapack/heevd_batch.cpp | 22 ++-- dpnp/libtensor/extensions/lapack/orgqr.cpp | 22 ++-- .../extensions/lapack/orgqr_batch.cpp | 22 ++-- dpnp/libtensor/extensions/lapack/potrf.cpp | 22 ++-- .../extensions/lapack/potrf_batch.cpp | 22 ++-- dpnp/libtensor/extensions/lapack/potrs.cpp | 25 ++-- dpnp/libtensor/extensions/lapack/syevd.cpp | 15 ++- .../extensions/lapack/syevd_batch.cpp | 22 ++-- dpnp/libtensor/extensions/lapack/ungqr.cpp | 22 ++-- .../extensions/lapack/ungqr_batch.cpp | 22 ++-- dpnp/libtensor/extensions/sparse/gemv.cpp | 33 +++--- dpnp/libtensor/extensions/sparse/gemv.hpp | 2 +- .../libtensor/extensions/sparse/sparse_py.cpp | 4 +- dpnp/libtensor/extensions/vm/common.hpp | 4 +- .../include/kernels/accumulators.hpp | 32 ++--- .../kernels/elementwise_functions/add.hpp | 14 +-- .../kernels/elementwise_functions/common.hpp | 8 +- .../elementwise_functions/common_inplace.hpp | 4 +- .../elementwise_functions/multiply.hpp | 18 +-- .../elementwise_functions/subtract.hpp | 20 ++-- .../elementwise_functions/true_divide.hpp | 16 +-- .../kernels/linalg_functions/dot_product.hpp | 16 ++- .../include/kernels/linalg_functions/gemm.hpp | 64 +++++----- dpnp/libtensor/include/kernels/reductions.hpp | 77 ++++++------ .../include/kernels/sorting/topk.hpp | 14 +-- dpnp/libtensor/include/utils/offset_utils.hpp | 16 +-- .../include/utils/sycl_alloc_utils.hpp | 73 ++++++++---- dpnp/libtensor/source/accumulators.cpp | 28 ++--- .../accumulators/accumulate_over_axis.hpp | 28 ++--- .../source/boolean_advanced_indexing.cpp | 48 ++++---- dpnp/libtensor/source/choose.cpp | 36 ++---- dpnp/libtensor/source/clip.cpp | 14 +-- .../source/copy_and_cast_usm_to_usm.cpp | 10 +- dpnp/libtensor/source/copy_as_contig.cpp | 36 +++--- dpnp/libtensor/source/copy_for_reshape.cpp | 11 +- dpnp/libtensor/source/copy_for_roll.cpp | 28 ++--- .../copy_numpy_ndarray_into_usm_ndarray.cpp | 10 +- .../elementwise_functions.hpp | 110 +++++++++--------- .../source/elementwise_functions/isclose.cpp | 15 +-- .../elementwise_functions/nan_to_num.cpp | 18 +-- .../elementwise_functions/true_divide.cpp | 9 +- dpnp/libtensor/source/full_ctor.cpp | 10 +- .../source/integer_advanced_indexing.cpp | 49 ++++---- .../libtensor/source/linalg_functions/dot.cpp | 16 +-- dpnp/libtensor/source/putmask.cpp | 18 +-- dpnp/libtensor/source/reductions/minmax.cpp | 8 +- .../source/reductions/reduction_over_axis.hpp | 32 ++--- dpnp/libtensor/source/repeat.cpp | 56 ++++----- dpnp/libtensor/source/sorting/isin.cpp | 14 +-- .../libtensor/source/sorting/searchsorted.cpp | 14 +-- dpnp/libtensor/source/tensor_ctors.cpp | 18 +-- dpnp/libtensor/source/triul_ctor.cpp | 19 +-- dpnp/libtensor/source/where.cpp | 14 +-- 68 files changed, 765 insertions(+), 811 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1836e2e01a8..452bd764fcca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ This release drops support for Python 3.10, making Python 3.11 the minimum requi * Changed the install instructions in `README.md` and the quick start guide to conda-forge and PyPI [#3151](https://github.com/IntelPython/dpnp/pull/3151) * Changed the conda recipe's tests to `pip check`, the import and `python -m dpctl -f`, as in the conda-forge feedstock, so `conda build` and `conda build --test` no longer run the test suites and no longer read `SKIP_TENSOR_TESTS`; run the suites with `ci/hooks.py`, or with `python -m pytest --pyargs dpnp.tests` in an environment with dpnp installed [#3151](https://github.com/IntelPython/dpnp/pull/3151) * Changed `dpnp::utils::keep_args_alive` in `dpnp4pybind11.hpp` to defer the release of its arguments with `dpctl::utils::defer_release` instead of submitting a `host_task`, so the returned event is now for an empty kernel and its completion means that the arguments are no longer in use rather than that they have been released [#3224](https://github.com/IntelPython/dpnp/pull/3224) +* Changed libtensor and the `lapack` and `sparse` extensions to free temporaries and keep host data alive with releases deferred through `dpctl::utils::defer_release` instead of with `host_task`, and added `async_release` to the libtensor header `utils/sycl_alloc_utils.hpp` for that. `async_smart_free` and `device_allocate_and_pack` now return the event of an empty kernel, and `utils/sycl_alloc_utils.hpp` and `utils/offset_utils.hpp` now include `dpctl4pybind11.hpp` [#3224](https://github.com/IntelPython/dpnp/pull/3224) ### Deprecated diff --git a/dpnp/libtensor/extensions/lapack/evd_batch_common.hpp b/dpnp/libtensor/extensions/lapack/evd_batch_common.hpp index f57e68af2a35..67953e238b92 100644 --- a/dpnp/libtensor/extensions/lapack/evd_batch_common.hpp +++ b/dpnp/libtensor/extensions/lapack/evd_batch_common.hpp @@ -119,9 +119,9 @@ std::pair evd_batch_fn(exec_q, jobz_val, uplo_val, batch_size, n, eig_vecs_data, eig_vals_data, depends); - sycl::event ht_ev = dpnp::utils::keep_args_alive( + sycl::event keep_args_alive_ev = dpnp::utils::keep_args_alive( exec_q, {eig_vecs, eig_vals}, {evd_batch_ev}); - return std::make_pair(ht_ev, evd_batch_ev); + return std::make_pair(keep_args_alive_ev, evd_batch_ev); } } // namespace dpnp::extensions::lapack::evd diff --git a/dpnp/libtensor/extensions/lapack/evd_common.hpp b/dpnp/libtensor/extensions/lapack/evd_common.hpp index c16f1995f8a8..3abd5d1f18d2 100644 --- a/dpnp/libtensor/extensions/lapack/evd_common.hpp +++ b/dpnp/libtensor/extensions/lapack/evd_common.hpp @@ -111,9 +111,9 @@ std::pair sycl::event evd_ev = evd_fn(exec_q, jobz_val, uplo_val, n, eig_vecs_data, eig_vals_data, depends); - sycl::event ht_ev = + sycl::event keep_args_alive_ev = dpnp::utils::keep_args_alive(exec_q, {eig_vecs, eig_vals}, {evd_ev}); - return std::make_pair(ht_ev, evd_ev); + return std::make_pair(keep_args_alive_ev, evd_ev); } } // namespace dpnp::extensions::lapack::evd diff --git a/dpnp/libtensor/extensions/lapack/geqrf.cpp b/dpnp/libtensor/extensions/lapack/geqrf.cpp index 03e9c5060fdd..58c7978b1bda 100644 --- a/dpnp/libtensor/extensions/lapack/geqrf.cpp +++ b/dpnp/libtensor/extensions/lapack/geqrf.cpp @@ -27,6 +27,7 @@ //***************************************************************************** #include +#include #include #include @@ -68,7 +69,7 @@ static sycl::event geqrf_impl(sycl::queue &exec_q, char *in_a, std::int64_t lda, char *in_tau, - std::vector &host_task_events, + std::vector &cleanup_events, const std::vector &depends) { type_utils::validate_type_for_device(exec_q); @@ -136,14 +137,11 @@ static sycl::event geqrf_impl(sycl::queue &exec_q, throw std::runtime_error(error_msg.str()); } - sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) { - cgh.depends_on(geqrf_event); - auto ctx = exec_q.get_context(); - cgh.host_task([ctx, scratchpad]() { - dpnp::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx); - }); - }); - host_task_events.push_back(clean_up_event); + using dpnp::tensor::alloc_utils::USMDeleter; + sycl::event clean_up_event = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {geqrf_event}, + std::unique_ptr(scratchpad, USMDeleter(exec_q))); + cleanup_events.push_back(clean_up_event); return geqrf_event; } @@ -235,12 +233,12 @@ std::pair std::to_string(min_m_n) + " array is expected."); } - std::vector host_task_events; + std::vector cleanup_events; sycl::event geqrf_ev = geqrf_fn(exec_q, m, n, a_array_data, lda, - tau_array_data, host_task_events, depends); + tau_array_data, cleanup_events, depends); sycl::event args_ev = dpnp::utils::keep_args_alive( - exec_q, {a_array, tau_array}, host_task_events); + exec_q, {a_array, tau_array}, cleanup_events); return std::make_pair(args_ev, geqrf_ev); } diff --git a/dpnp/libtensor/extensions/lapack/geqrf_batch.cpp b/dpnp/libtensor/extensions/lapack/geqrf_batch.cpp index 739e2a82b543..ea8e6e7e1e9f 100644 --- a/dpnp/libtensor/extensions/lapack/geqrf_batch.cpp +++ b/dpnp/libtensor/extensions/lapack/geqrf_batch.cpp @@ -26,6 +26,7 @@ // THE POSSIBILITY OF SUCH DAMAGE. //***************************************************************************** +#include #include #include @@ -75,7 +76,7 @@ static sycl::event geqrf_batch_impl(sycl::queue &exec_q, char *in_tau, std::int64_t stride_tau, std::int64_t batch_size, - std::vector &host_task_events, + std::vector &cleanup_events, const std::vector &depends) { type_utils::validate_type_for_device(exec_q); @@ -157,14 +158,11 @@ static sycl::event geqrf_batch_impl(sycl::queue &exec_q, throw std::runtime_error(error_msg.str()); } - sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) { - cgh.depends_on(geqrf_batch_event); - auto ctx = exec_q.get_context(); - cgh.host_task([ctx, scratchpad]() { - dpnp::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx); - }); - }); - host_task_events.push_back(clean_up_event); + using dpnp::tensor::alloc_utils::USMDeleter; + sycl::event clean_up_event = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {geqrf_batch_event}, + std::unique_ptr(scratchpad, USMDeleter(exec_q))); + cleanup_events.push_back(clean_up_event); return geqrf_batch_event; } @@ -243,13 +241,13 @@ std::pair const std::int64_t lda = std::max(1UL, m); - std::vector host_task_events; + std::vector cleanup_events; sycl::event geqrf_batch_ev = geqrf_batch_fn( exec_q, m, n, a_array_data, lda, stride_a, tau_array_data, stride_tau, - batch_size, host_task_events, depends); + batch_size, cleanup_events, depends); sycl::event args_ev = dpnp::utils::keep_args_alive( - exec_q, {a_array, tau_array}, host_task_events); + exec_q, {a_array, tau_array}, cleanup_events); return std::make_pair(args_ev, geqrf_batch_ev); } diff --git a/dpnp/libtensor/extensions/lapack/gesv.cpp b/dpnp/libtensor/extensions/lapack/gesv.cpp index 3bab2e5b35a3..890a839735a5 100644 --- a/dpnp/libtensor/extensions/lapack/gesv.cpp +++ b/dpnp/libtensor/extensions/lapack/gesv.cpp @@ -27,6 +27,7 @@ //***************************************************************************** #include +#include #include #include @@ -35,6 +36,7 @@ #include "ext/common.hpp" // dpnp tensor headers +#include "utils/sycl_alloc_utils.hpp" #include "utils/type_utils.hpp" #include "common_helpers.hpp" @@ -214,16 +216,13 @@ static sycl::event gesv_impl(sycl::queue &exec_q, throw std::runtime_error(error_msg.str()); } - sycl::event ht_ev = exec_q.submit([&](sycl::handler &cgh) { - cgh.depends_on(comp_event); - auto ctx = exec_q.get_context(); - cgh.host_task([ctx, scratchpad, ipiv]() { - sycl_free_noexcept(scratchpad, ctx); - sycl_free_noexcept(ipiv, ctx); - }); - }); + using dpnp::tensor::alloc_utils::USMDeleter; + sycl::event cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {comp_event}, + std::unique_ptr(scratchpad, USMDeleter(exec_q)), + std::unique_ptr(ipiv, USMDeleter(exec_q))); - return ht_ev; + return cleanup_ev; } std::pair @@ -276,10 +275,10 @@ std::pair sycl::event gesv_ev = gesv_fn(exec_q, n, nrhs, coeff_matrix_data, dependent_vals_data, depends); - sycl::event ht_ev = dpnp::utils::keep_args_alive( + sycl::event keep_args_alive_ev = dpnp::utils::keep_args_alive( exec_q, {coeff_matrix, dependent_vals}, {gesv_ev}); - return std::make_pair(ht_ev, gesv_ev); + return std::make_pair(keep_args_alive_ev, gesv_ev); } template diff --git a/dpnp/libtensor/extensions/lapack/gesv_batch.cpp b/dpnp/libtensor/extensions/lapack/gesv_batch.cpp index 63101e8db56d..68c1642ea554 100644 --- a/dpnp/libtensor/extensions/lapack/gesv_batch.cpp +++ b/dpnp/libtensor/extensions/lapack/gesv_batch.cpp @@ -27,6 +27,7 @@ //***************************************************************************** #include +#include #include #include @@ -35,6 +36,7 @@ #include "ext/common.hpp" // dpnp tensor headers +#include "utils/sycl_alloc_utils.hpp" #include "utils/type_utils.hpp" #include "common_helpers.hpp" @@ -299,22 +301,22 @@ static sycl::event gesv_batch_impl(sycl::queue &exec_q, throw std::runtime_error(error_msg.str()); } - sycl::event ht_ev = exec_q.submit([&](sycl::handler &cgh) { #if defined(USE_ONEMATH) - cgh.depends_on(comp_event); + const std::vector all_comp_evs{comp_event}; #else - for (const auto &ev : comp_evs) { - cgh.depends_on(ev); - } + std::vector all_comp_evs; + for (const auto &evs : comp_evs) { + all_comp_evs.insert(all_comp_evs.end(), evs.begin(), evs.end()); + } #endif // USE_ONEMATH - auto ctx = exec_q.get_context(); - cgh.host_task([ctx, scratchpad, ipiv]() { - sycl_free_noexcept(scratchpad, ctx); - sycl_free_noexcept(ipiv, ctx); - }); - }); - - return ht_ev; + + using dpnp::tensor::alloc_utils::USMDeleter; + sycl::event cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, all_comp_evs, + std::unique_ptr(scratchpad, USMDeleter(exec_q)), + std::unique_ptr(ipiv, USMDeleter(exec_q))); + + return cleanup_ev; } std::pair @@ -409,10 +411,10 @@ std::pair dependent_vals_data, depends); #endif // USE_ONEMATH - sycl::event ht_ev = dpnp::utils::keep_args_alive( + sycl::event keep_args_alive_ev = dpnp::utils::keep_args_alive( exec_q, {coeff_matrix, dependent_vals}, {gesv_ev}); - return std::make_pair(ht_ev, gesv_ev); + return std::make_pair(keep_args_alive_ev, gesv_ev); } template diff --git a/dpnp/libtensor/extensions/lapack/gesvd.cpp b/dpnp/libtensor/extensions/lapack/gesvd.cpp index 02b388977144..7730e00b7535 100644 --- a/dpnp/libtensor/extensions/lapack/gesvd.cpp +++ b/dpnp/libtensor/extensions/lapack/gesvd.cpp @@ -26,6 +26,7 @@ // THE POSSIBILITY OF SUCH DAMAGE. //***************************************************************************** +#include #include #include @@ -34,6 +35,7 @@ #include "ext/common.hpp" // dpnp tensor headers +#include "utils/sycl_alloc_utils.hpp" #include "utils/type_utils.hpp" #include "common_helpers.hpp" @@ -144,15 +146,12 @@ static sycl::event gesvd_impl(sycl::queue &exec_q, throw std::runtime_error(error_msg.str()); } - sycl::event ht_ev = exec_q.submit([&](sycl::handler &cgh) { - cgh.depends_on(gesvd_event); - auto ctx = exec_q.get_context(); - cgh.host_task([ctx, scratchpad]() { - dpnp::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx); - }); - }); + using dpnp::tensor::alloc_utils::USMDeleter; + sycl::event cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {gesvd_event}, + std::unique_ptr(scratchpad, USMDeleter(exec_q))); - return ht_ev; + return cleanup_ev; } std::pair @@ -215,10 +214,10 @@ std::pair gesvd_fn(exec_q, jobu, jobvt, m, n, a_array_data, lda, out_s_data, out_u_data, ldu, out_vt_data, ldvt, depends); - sycl::event ht_ev = dpnp::utils::keep_args_alive( + sycl::event keep_args_alive_ev = dpnp::utils::keep_args_alive( exec_q, {a_array, out_s, out_u, out_vt}, {gesvd_ev}); - return std::make_pair(ht_ev, gesvd_ev); + return std::make_pair(keep_args_alive_ev, gesvd_ev); } template diff --git a/dpnp/libtensor/extensions/lapack/gesvd_batch.cpp b/dpnp/libtensor/extensions/lapack/gesvd_batch.cpp index cb114c44a255..927a155278f6 100644 --- a/dpnp/libtensor/extensions/lapack/gesvd_batch.cpp +++ b/dpnp/libtensor/extensions/lapack/gesvd_batch.cpp @@ -27,6 +27,7 @@ //***************************************************************************** #include +#include #include #include @@ -35,6 +36,7 @@ #include "ext/common.hpp" // dpnp tensor headers +#include "utils/sycl_alloc_utils.hpp" #include "utils/type_utils.hpp" #include "common_helpers.hpp" @@ -204,17 +206,17 @@ static sycl::event gesvd_batch_impl(sycl::queue &exec_q, throw std::runtime_error(error_msg.str()); } - sycl::event ht_ev = exec_q.submit([&](sycl::handler &cgh) { - for (const auto &ev : comp_evs) { - cgh.depends_on(ev); - } - auto ctx = exec_q.get_context(); - cgh.host_task([ctx, scratchpad]() { - dpnp::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx); - }); - }); + std::vector all_comp_evs; + for (const auto &evs : comp_evs) { + all_comp_evs.insert(all_comp_evs.end(), evs.begin(), evs.end()); + } + + using dpnp::tensor::alloc_utils::USMDeleter; + sycl::event cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, all_comp_evs, + std::unique_ptr(scratchpad, USMDeleter(exec_q))); - return ht_ev; + return cleanup_ev; } std::pair @@ -280,10 +282,10 @@ std::pair gesvd_batch_fn(exec_q, jobu, jobvt, m, n, batch_size, a_array_data, lda, out_s_data, out_u_data, ldu, out_vt_data, ldvt, depends); - sycl::event ht_ev = dpnp::utils::keep_args_alive( + sycl::event keep_args_alive_ev = dpnp::utils::keep_args_alive( exec_q, {a_array, out_s, out_u, out_vt}, {gesvd_ev}); - return std::make_pair(ht_ev, gesvd_ev); + return std::make_pair(keep_args_alive_ev, gesvd_ev); } template diff --git a/dpnp/libtensor/extensions/lapack/getrf.cpp b/dpnp/libtensor/extensions/lapack/getrf.cpp index c31213b38620..d91132971ff9 100644 --- a/dpnp/libtensor/extensions/lapack/getrf.cpp +++ b/dpnp/libtensor/extensions/lapack/getrf.cpp @@ -27,6 +27,7 @@ //***************************************************************************** #include +#include #include #include @@ -70,7 +71,7 @@ static sycl::event getrf_impl(sycl::queue &exec_q, std::int64_t lda, std::int64_t *ipiv, py::list dev_info, - std::vector &host_task_events, + std::vector &cleanup_events, const std::vector &depends) { type_utils::validate_type_for_device(exec_q); @@ -157,14 +158,11 @@ static sycl::event getrf_impl(sycl::queue &exec_q, throw std::runtime_error(error_msg.str()); } - sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) { - cgh.depends_on(getrf_event); - auto ctx = exec_q.get_context(); - cgh.host_task([ctx, scratchpad]() { - dpnp::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx); - }); - }); - host_task_events.push_back(clean_up_event); + using dpnp::tensor::alloc_utils::USMDeleter; + sycl::event clean_up_event = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {getrf_event}, + std::unique_ptr(scratchpad, USMDeleter(exec_q))); + cleanup_events.push_back(clean_up_event); return getrf_event; } @@ -247,12 +245,12 @@ std::pair char *ipiv_array_data = ipiv_array.get_data(); std::int64_t *d_ipiv = reinterpret_cast(ipiv_array_data); - std::vector host_task_events; + std::vector cleanup_events; sycl::event getrf_ev = getrf_fn(exec_q, m, n, a_array_data, lda, d_ipiv, - dev_info, host_task_events, depends); + dev_info, cleanup_events, depends); sycl::event args_ev = dpnp::utils::keep_args_alive( - exec_q, {a_array, ipiv_array}, host_task_events); + exec_q, {a_array, ipiv_array}, cleanup_events); return std::make_pair(args_ev, getrf_ev); } diff --git a/dpnp/libtensor/extensions/lapack/getrf_batch.cpp b/dpnp/libtensor/extensions/lapack/getrf_batch.cpp index 917794557378..ad5c4bc0f077 100644 --- a/dpnp/libtensor/extensions/lapack/getrf_batch.cpp +++ b/dpnp/libtensor/extensions/lapack/getrf_batch.cpp @@ -27,6 +27,7 @@ //***************************************************************************** #include +#include #include #include @@ -78,7 +79,7 @@ static sycl::event getrf_batch_impl(sycl::queue &exec_q, std::int64_t stride_ipiv, std::int64_t batch_size, py::list dev_info, - std::vector &host_task_events, + std::vector &cleanup_events, const std::vector &depends) { type_utils::validate_type_for_device(exec_q); @@ -176,14 +177,11 @@ static sycl::event getrf_batch_impl(sycl::queue &exec_q, throw std::runtime_error(error_msg.str()); } - sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) { - cgh.depends_on(getrf_batch_event); - auto ctx = exec_q.get_context(); - cgh.host_task([ctx, scratchpad]() { - dpnp::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx); - }); - }); - host_task_events.push_back(clean_up_event); + using dpnp::tensor::alloc_utils::USMDeleter; + sycl::event clean_up_event = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {getrf_batch_event}, + std::unique_ptr(scratchpad, USMDeleter(exec_q))); + cleanup_events.push_back(clean_up_event); return getrf_batch_event; } @@ -279,13 +277,13 @@ std::pair char *ipiv_array_data = ipiv_array.get_data(); std::int64_t *d_ipiv = reinterpret_cast(ipiv_array_data); - std::vector host_task_events; + std::vector cleanup_events; sycl::event getrf_batch_ev = getrf_batch_fn( exec_q, m, n, a_array_data, lda, stride_a, d_ipiv, stride_ipiv, - batch_size, dev_info, host_task_events, depends); + batch_size, dev_info, cleanup_events, depends); sycl::event args_ev = dpnp::utils::keep_args_alive( - exec_q, {a_array, ipiv_array}, host_task_events); + exec_q, {a_array, ipiv_array}, cleanup_events); return std::make_pair(args_ev, getrf_batch_ev); } diff --git a/dpnp/libtensor/extensions/lapack/getri_batch.cpp b/dpnp/libtensor/extensions/lapack/getri_batch.cpp index 1b5e2c94065f..e70abdffa7b7 100644 --- a/dpnp/libtensor/extensions/lapack/getri_batch.cpp +++ b/dpnp/libtensor/extensions/lapack/getri_batch.cpp @@ -26,6 +26,7 @@ // THE POSSIBILITY OF SUCH DAMAGE. //***************************************************************************** +#include #include #include @@ -75,7 +76,7 @@ static sycl::event getri_batch_impl(sycl::queue &exec_q, std::int64_t stride_ipiv, std::int64_t batch_size, py::list dev_info, - std::vector &host_task_events, + std::vector &cleanup_events, const std::vector &depends) { type_utils::validate_type_for_device(exec_q); @@ -167,14 +168,11 @@ static sycl::event getri_batch_impl(sycl::queue &exec_q, throw std::runtime_error(error_msg.str()); } - sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) { - cgh.depends_on(getri_batch_event); - auto ctx = exec_q.get_context(); - cgh.host_task([ctx, scratchpad]() { - dpnp::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx); - }); - }); - host_task_events.push_back(clean_up_event); + using dpnp::tensor::alloc_utils::USMDeleter; + sycl::event clean_up_event = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {getri_batch_event}, + std::unique_ptr(scratchpad, USMDeleter(exec_q))); + cleanup_events.push_back(clean_up_event); return getri_batch_event; } @@ -261,13 +259,13 @@ std::pair char *ipiv_array_data = ipiv_array.get_data(); std::int64_t *d_ipiv = reinterpret_cast(ipiv_array_data); - std::vector host_task_events; + std::vector cleanup_events; sycl::event getri_batch_ev = getri_batch_fn( exec_q, n, a_array_data, lda, stride_a, d_ipiv, stride_ipiv, batch_size, - dev_info, host_task_events, depends); + dev_info, cleanup_events, depends); sycl::event args_ev = dpnp::utils::keep_args_alive( - exec_q, {a_array, ipiv_array}, host_task_events); + exec_q, {a_array, ipiv_array}, cleanup_events); return std::make_pair(args_ev, getri_batch_ev); } diff --git a/dpnp/libtensor/extensions/lapack/getrs.cpp b/dpnp/libtensor/extensions/lapack/getrs.cpp index b4d745d230a0..399345c34936 100644 --- a/dpnp/libtensor/extensions/lapack/getrs.cpp +++ b/dpnp/libtensor/extensions/lapack/getrs.cpp @@ -26,6 +26,7 @@ // THE POSSIBILITY OF SUCH DAMAGE. //***************************************************************************** +#include #include #include @@ -74,7 +75,7 @@ static sycl::event getrs_impl(sycl::queue &exec_q, std::int64_t *ipiv, char *in_b, const std::int64_t ldb, - std::vector &host_task_events, + std::vector &cleanup_events, const std::vector &depends) { type_utils::validate_type_for_device(exec_q); @@ -156,14 +157,11 @@ static sycl::event getrs_impl(sycl::queue &exec_q, throw std::runtime_error(error_msg.str()); } - sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) { - cgh.depends_on(getrs_event); - auto ctx = exec_q.get_context(); - cgh.host_task([ctx, scratchpad]() { - dpnp::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx); - }); - }); - host_task_events.push_back(clean_up_event); + using dpnp::tensor::alloc_utils::USMDeleter; + sycl::event clean_up_event = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {getrs_event}, + std::unique_ptr(scratchpad, USMDeleter(exec_q))); + cleanup_events.push_back(clean_up_event); return getrs_event; } @@ -276,13 +274,13 @@ std::pair std::int64_t *ipiv = reinterpret_cast(ipiv_array_data); - std::vector host_task_events; + std::vector cleanup_events; sycl::event getrs_ev = getrs_fn(exec_q, trans, n, nrhs, a_array_data, lda, ipiv, b_array_data, - ldb, host_task_events, depends); + ldb, cleanup_events, depends); sycl::event args_ev = dpnp::utils::keep_args_alive( - exec_q, {a_array, b_array, ipiv_array}, host_task_events); + exec_q, {a_array, b_array, ipiv_array}, cleanup_events); return std::make_pair(args_ev, getrs_ev); } diff --git a/dpnp/libtensor/extensions/lapack/getrs_batch.cpp b/dpnp/libtensor/extensions/lapack/getrs_batch.cpp index 8e9c69833097..fc88d0aaee3b 100644 --- a/dpnp/libtensor/extensions/lapack/getrs_batch.cpp +++ b/dpnp/libtensor/extensions/lapack/getrs_batch.cpp @@ -27,6 +27,7 @@ //***************************************************************************** #include +#include #include #include @@ -87,7 +88,7 @@ static sycl::event getrs_batch_impl(sycl::queue &exec_q, const std::int64_t ldb, const std::int64_t stride_b, const std::int64_t batch_size, - std::vector &host_task_events, + std::vector &cleanup_events, const std::vector &depends) { type_utils::validate_type_for_device(exec_q); @@ -190,14 +191,11 @@ static sycl::event getrs_batch_impl(sycl::queue &exec_q, throw std::runtime_error(error_msg.str()); } - sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) { - cgh.depends_on(getrs_batch_event); - auto ctx = exec_q.get_context(); - cgh.host_task([ctx, scratchpad]() { - dpnp::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx); - }); - }); - host_task_events.push_back(clean_up_event); + using dpnp::tensor::alloc_utils::USMDeleter; + sycl::event clean_up_event = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {getrs_batch_event}, + std::unique_ptr(scratchpad, USMDeleter(exec_q))); + cleanup_events.push_back(clean_up_event); return getrs_batch_event; } @@ -319,13 +317,13 @@ std::pair std::int64_t *ipiv = reinterpret_cast(ipiv_array_data); - std::vector host_task_events; + std::vector cleanup_events; sycl::event getrs_batch_ev = getrs_batch_fn( exec_q, trans, n, nrhs, a_array_data, lda, stride_a, ipiv, stride_ipiv, - b_array_data, ldb, stride_b, batch_size, host_task_events, depends); + b_array_data, ldb, stride_b, batch_size, cleanup_events, depends); sycl::event args_ev = dpnp::utils::keep_args_alive( - exec_q, {a_array, b_array, ipiv_array}, host_task_events); + exec_q, {a_array, b_array, ipiv_array}, cleanup_events); return std::make_pair(args_ev, getrs_batch_ev); } diff --git a/dpnp/libtensor/extensions/lapack/heevd.cpp b/dpnp/libtensor/extensions/lapack/heevd.cpp index 86ba97765884..9d34e2620123 100644 --- a/dpnp/libtensor/extensions/lapack/heevd.cpp +++ b/dpnp/libtensor/extensions/lapack/heevd.cpp @@ -26,6 +26,7 @@ // THE POSSIBILITY OF SUCH DAMAGE. //***************************************************************************** +#include #include #include @@ -38,6 +39,7 @@ #include "ext/common.hpp" // dpnp tensor headers +#include "utils/sycl_alloc_utils.hpp" #include "utils/type_utils.hpp" namespace dpnp::extensions::lapack @@ -114,15 +116,12 @@ static sycl::event heevd_impl(sycl::queue &exec_q, throw std::runtime_error(error_msg.str()); } - sycl::event ht_ev = exec_q.submit([&](sycl::handler &cgh) { - cgh.depends_on(heevd_event); - auto ctx = exec_q.get_context(); - cgh.host_task([ctx, scratchpad]() { - dpnp::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx); - }); - }); + using dpnp::tensor::alloc_utils::USMDeleter; + sycl::event cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {heevd_event}, + std::unique_ptr(scratchpad, USMDeleter(exec_q))); - return ht_ev; + return cleanup_ev; } template diff --git a/dpnp/libtensor/extensions/lapack/heevd_batch.cpp b/dpnp/libtensor/extensions/lapack/heevd_batch.cpp index 0b23e06c4415..c542ce70d51c 100644 --- a/dpnp/libtensor/extensions/lapack/heevd_batch.cpp +++ b/dpnp/libtensor/extensions/lapack/heevd_batch.cpp @@ -26,6 +26,7 @@ // THE POSSIBILITY OF SUCH DAMAGE. //***************************************************************************** +#include #include #include @@ -39,6 +40,7 @@ #include "ext/common.hpp" // dpnp tensor headers +#include "utils/sycl_alloc_utils.hpp" #include "utils/type_utils.hpp" namespace dpnp::extensions::lapack @@ -144,17 +146,17 @@ static sycl::event heevd_batch_impl(sycl::queue &exec_q, throw std::runtime_error(error_msg.str()); } - sycl::event ht_ev = exec_q.submit([&](sycl::handler &cgh) { - for (const auto &ev : comp_evs) { - cgh.depends_on(ev); - } - auto ctx = exec_q.get_context(); - cgh.host_task([ctx, scratchpad]() { - dpnp::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx); - }); - }); + std::vector all_comp_evs; + for (const auto &evs : comp_evs) { + all_comp_evs.insert(all_comp_evs.end(), evs.begin(), evs.end()); + } + + using dpnp::tensor::alloc_utils::USMDeleter; + sycl::event cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, all_comp_evs, + std::unique_ptr(scratchpad, USMDeleter(exec_q))); - return ht_ev; + return cleanup_ev; } template diff --git a/dpnp/libtensor/extensions/lapack/orgqr.cpp b/dpnp/libtensor/extensions/lapack/orgqr.cpp index 09c2523fd482..74a7e03de3ec 100644 --- a/dpnp/libtensor/extensions/lapack/orgqr.cpp +++ b/dpnp/libtensor/extensions/lapack/orgqr.cpp @@ -26,6 +26,7 @@ // THE POSSIBILITY OF SUCH DAMAGE. //***************************************************************************** +#include #include #include @@ -69,7 +70,7 @@ static sycl::event orgqr_impl(sycl::queue &exec_q, char *in_a, std::int64_t lda, char *in_tau, - std::vector &host_task_events, + std::vector &cleanup_events, const std::vector &depends) { type_utils::validate_type_for_device(exec_q); @@ -144,14 +145,11 @@ static sycl::event orgqr_impl(sycl::queue &exec_q, throw std::runtime_error(error_msg.str()); } - sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) { - cgh.depends_on(orgqr_event); - auto ctx = exec_q.get_context(); - cgh.host_task([ctx, scratchpad]() { - dpnp::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx); - }); - }); - host_task_events.push_back(clean_up_event); + using dpnp::tensor::alloc_utils::USMDeleter; + sycl::event clean_up_event = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {orgqr_event}, + std::unique_ptr(scratchpad, USMDeleter(exec_q))); + cleanup_events.push_back(clean_up_event); return orgqr_event; } @@ -240,12 +238,12 @@ std::pair char *tau_array_data = tau_array.get_data(); - std::vector host_task_events; + std::vector cleanup_events; sycl::event orgqr_ev = orgqr_fn(exec_q, m, n, k, a_array_data, lda, - tau_array_data, host_task_events, depends); + tau_array_data, cleanup_events, depends); sycl::event args_ev = dpnp::utils::keep_args_alive( - exec_q, {a_array, tau_array}, host_task_events); + exec_q, {a_array, tau_array}, cleanup_events); return std::make_pair(args_ev, orgqr_ev); } diff --git a/dpnp/libtensor/extensions/lapack/orgqr_batch.cpp b/dpnp/libtensor/extensions/lapack/orgqr_batch.cpp index 0d996bef2b87..1465248dd23e 100644 --- a/dpnp/libtensor/extensions/lapack/orgqr_batch.cpp +++ b/dpnp/libtensor/extensions/lapack/orgqr_batch.cpp @@ -26,6 +26,7 @@ // THE POSSIBILITY OF SUCH DAMAGE. //***************************************************************************** +#include #include #include @@ -77,7 +78,7 @@ static sycl::event orgqr_batch_impl(sycl::queue &exec_q, char *in_tau, std::int64_t stride_tau, std::int64_t batch_size, - std::vector &host_task_events, + std::vector &cleanup_events, const std::vector &depends) { type_utils::validate_type_for_device(exec_q); @@ -161,14 +162,11 @@ static sycl::event orgqr_batch_impl(sycl::queue &exec_q, throw std::runtime_error(error_msg.str()); } - sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) { - cgh.depends_on(orgqr_batch_event); - auto ctx = exec_q.get_context(); - cgh.host_task([ctx, scratchpad]() { - dpnp::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx); - }); - }); - host_task_events.push_back(clean_up_event); + using dpnp::tensor::alloc_utils::USMDeleter; + sycl::event clean_up_event = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {orgqr_batch_event}, + std::unique_ptr(scratchpad, USMDeleter(exec_q))); + cleanup_events.push_back(clean_up_event); return orgqr_batch_event; } @@ -248,13 +246,13 @@ std::pair const std::int64_t lda = std::max(1UL, m); - std::vector host_task_events; + std::vector cleanup_events; sycl::event orgqr_batch_ev = orgqr_batch_fn( exec_q, m, n, k, a_array_data, lda, stride_a, tau_array_data, - stride_tau, batch_size, host_task_events, depends); + stride_tau, batch_size, cleanup_events, depends); sycl::event args_ev = dpnp::utils::keep_args_alive( - exec_q, {a_array, tau_array}, host_task_events); + exec_q, {a_array, tau_array}, cleanup_events); return std::make_pair(args_ev, orgqr_batch_ev); } diff --git a/dpnp/libtensor/extensions/lapack/potrf.cpp b/dpnp/libtensor/extensions/lapack/potrf.cpp index a14ce223458c..336066c799ef 100644 --- a/dpnp/libtensor/extensions/lapack/potrf.cpp +++ b/dpnp/libtensor/extensions/lapack/potrf.cpp @@ -26,6 +26,7 @@ // THE POSSIBILITY OF SUCH DAMAGE. //***************************************************************************** +#include #include #include @@ -66,7 +67,7 @@ static sycl::event potrf_impl(sycl::queue &exec_q, const std::int64_t n, char *in_a, std::int64_t lda, - std::vector &host_task_events, + std::vector &cleanup_events, const std::vector &depends) { type_utils::validate_type_for_device(exec_q); @@ -136,14 +137,11 @@ static sycl::event potrf_impl(sycl::queue &exec_q, throw std::runtime_error(error_msg.str()); } - sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) { - cgh.depends_on(potrf_event); - auto ctx = exec_q.get_context(); - cgh.host_task([ctx, scratchpad]() { - dpnp::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx); - }); - }); - host_task_events.push_back(clean_up_event); + using dpnp::tensor::alloc_utils::USMDeleter; + sycl::event clean_up_event = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {potrf_event}, + std::unique_ptr(scratchpad, USMDeleter(exec_q))); + cleanup_events.push_back(clean_up_event); return potrf_event; } @@ -193,12 +191,12 @@ std::pair const oneapi::mkl::uplo uplo_val = static_cast(upper_lower); - std::vector host_task_events; + std::vector cleanup_events; sycl::event potrf_ev = potrf_fn(exec_q, uplo_val, n, a_array_data, lda, - host_task_events, depends); + cleanup_events, depends); sycl::event args_ev = - dpnp::utils::keep_args_alive(exec_q, {a_array}, host_task_events); + dpnp::utils::keep_args_alive(exec_q, {a_array}, cleanup_events); return std::make_pair(args_ev, potrf_ev); } diff --git a/dpnp/libtensor/extensions/lapack/potrf_batch.cpp b/dpnp/libtensor/extensions/lapack/potrf_batch.cpp index 8193b32d4f05..ac140052f25a 100644 --- a/dpnp/libtensor/extensions/lapack/potrf_batch.cpp +++ b/dpnp/libtensor/extensions/lapack/potrf_batch.cpp @@ -26,6 +26,7 @@ // THE POSSIBILITY OF SUCH DAMAGE. //***************************************************************************** +#include #include #include @@ -72,7 +73,7 @@ static sycl::event potrf_batch_impl(sycl::queue &exec_q, const std::int64_t lda, const std::int64_t stride_a, const std::int64_t batch_size, - std::vector &host_task_events, + std::vector &cleanup_events, const std::vector &depends) { type_utils::validate_type_for_device(exec_q); @@ -166,14 +167,11 @@ static sycl::event potrf_batch_impl(sycl::queue &exec_q, throw std::runtime_error(error_msg.str()); } - sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) { - cgh.depends_on(potrf_batch_event); - auto ctx = exec_q.get_context(); - cgh.host_task([ctx, scratchpad]() { - dpnp::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx); - }); - }); - host_task_events.push_back(clean_up_event); + using dpnp::tensor::alloc_utils::USMDeleter; + sycl::event clean_up_event = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {potrf_batch_event}, + std::unique_ptr(scratchpad, USMDeleter(exec_q))); + cleanup_events.push_back(clean_up_event); return potrf_batch_event; } @@ -227,13 +225,13 @@ std::pair const oneapi::mkl::uplo uplo_val = static_cast(upper_lower); - std::vector host_task_events; + std::vector cleanup_events; sycl::event potrf_batch_ev = potrf_batch_fn(exec_q, uplo_val, n, a_array_data, lda, stride_a, - batch_size, host_task_events, depends); + batch_size, cleanup_events, depends); sycl::event args_ev = - dpnp::utils::keep_args_alive(exec_q, {a_array}, host_task_events); + dpnp::utils::keep_args_alive(exec_q, {a_array}, cleanup_events); return std::make_pair(args_ev, potrf_batch_ev); } diff --git a/dpnp/libtensor/extensions/lapack/potrs.cpp b/dpnp/libtensor/extensions/lapack/potrs.cpp index a1588cb58871..fd5e80d3d4ec 100644 --- a/dpnp/libtensor/extensions/lapack/potrs.cpp +++ b/dpnp/libtensor/extensions/lapack/potrs.cpp @@ -28,6 +28,7 @@ #include #include +#include #include #include #include @@ -78,7 +79,7 @@ static sycl::event potrs_impl(sycl::queue &exec_q, const std::int64_t lda, char *in_b, const std::int64_t ldb, - std::vector &host_task_events, + std::vector &cleanup_events, const std::vector &depends) { type_utils::validate_type_for_device(exec_q); @@ -168,14 +169,12 @@ static sycl::event potrs_impl(sycl::queue &exec_q, } if (scratchpad != nullptr) { - sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) { - cgh.depends_on(potrs_event); - auto ctx = exec_q.get_context(); - cgh.host_task([ctx, scratchpad]() { - dpnp::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx); - }); - }); - host_task_events.push_back(clean_up_event); + using dpnp::tensor::alloc_utils::USMDeleter; + sycl::event clean_up_event = + dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {potrs_event}, + std::unique_ptr(scratchpad, USMDeleter(exec_q))); + cleanup_events.push_back(clean_up_event); } return potrs_event; } @@ -270,14 +269,14 @@ std::pair const oneapi::mkl::uplo uplo_val = static_cast(upper_lower); - std::vector host_task_events; + std::vector cleanup_events; sycl::event potrs_ev = potrs_fn(exec_q, uplo_val, n, nrhs, a_array_data, lda, b_array_data, - ldb, host_task_events, depends); + ldb, cleanup_events, depends); - host_task_events.push_back(potrs_ev); + cleanup_events.push_back(potrs_ev); sycl::event args_ev = dpnp::utils::keep_args_alive( - exec_q, {a_array, b_array}, host_task_events); + exec_q, {a_array, b_array}, cleanup_events); return std::make_pair(args_ev, potrs_ev); } diff --git a/dpnp/libtensor/extensions/lapack/syevd.cpp b/dpnp/libtensor/extensions/lapack/syevd.cpp index e98882718240..e8949c2fed64 100644 --- a/dpnp/libtensor/extensions/lapack/syevd.cpp +++ b/dpnp/libtensor/extensions/lapack/syevd.cpp @@ -26,6 +26,7 @@ // THE POSSIBILITY OF SUCH DAMAGE. //***************************************************************************** +#include #include #include @@ -38,6 +39,7 @@ #include "ext/common.hpp" // dpnp tensor headers +#include "utils/sycl_alloc_utils.hpp" #include "utils/type_utils.hpp" namespace dpnp::extensions::lapack @@ -114,15 +116,12 @@ static sycl::event syevd_impl(sycl::queue &exec_q, throw std::runtime_error(error_msg.str()); } - sycl::event ht_ev = exec_q.submit([&](sycl::handler &cgh) { - cgh.depends_on(syevd_event); - auto ctx = exec_q.get_context(); - cgh.host_task([ctx, scratchpad]() { - dpnp::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx); - }); - }); + using dpnp::tensor::alloc_utils::USMDeleter; + sycl::event cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {syevd_event}, + std::unique_ptr(scratchpad, USMDeleter(exec_q))); - return ht_ev; + return cleanup_ev; } template diff --git a/dpnp/libtensor/extensions/lapack/syevd_batch.cpp b/dpnp/libtensor/extensions/lapack/syevd_batch.cpp index c88f22e909c3..841fb01239b0 100644 --- a/dpnp/libtensor/extensions/lapack/syevd_batch.cpp +++ b/dpnp/libtensor/extensions/lapack/syevd_batch.cpp @@ -26,6 +26,7 @@ // THE POSSIBILITY OF SUCH DAMAGE. //***************************************************************************** +#include #include #include @@ -39,6 +40,7 @@ #include "ext/common.hpp" // dpnp tensor headers +#include "utils/sycl_alloc_utils.hpp" #include "utils/type_utils.hpp" namespace dpnp::extensions::lapack @@ -144,17 +146,17 @@ static sycl::event syevd_batch_impl(sycl::queue &exec_q, throw std::runtime_error(error_msg.str()); } - sycl::event ht_ev = exec_q.submit([&](sycl::handler &cgh) { - for (const auto &ev : comp_evs) { - cgh.depends_on(ev); - } - auto ctx = exec_q.get_context(); - cgh.host_task([ctx, scratchpad]() { - dpnp::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx); - }); - }); + std::vector all_comp_evs; + for (const auto &evs : comp_evs) { + all_comp_evs.insert(all_comp_evs.end(), evs.begin(), evs.end()); + } + + using dpnp::tensor::alloc_utils::USMDeleter; + sycl::event cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, all_comp_evs, + std::unique_ptr(scratchpad, USMDeleter(exec_q))); - return ht_ev; + return cleanup_ev; } template diff --git a/dpnp/libtensor/extensions/lapack/ungqr.cpp b/dpnp/libtensor/extensions/lapack/ungqr.cpp index 42b14059176f..283244af0585 100644 --- a/dpnp/libtensor/extensions/lapack/ungqr.cpp +++ b/dpnp/libtensor/extensions/lapack/ungqr.cpp @@ -26,6 +26,7 @@ // THE POSSIBILITY OF SUCH DAMAGE. //***************************************************************************** +#include #include #include @@ -69,7 +70,7 @@ static sycl::event ungqr_impl(sycl::queue &exec_q, char *in_a, std::int64_t lda, char *in_tau, - std::vector &host_task_events, + std::vector &cleanup_events, const std::vector &depends) { type_utils::validate_type_for_device(exec_q); @@ -139,14 +140,11 @@ static sycl::event ungqr_impl(sycl::queue &exec_q, throw std::runtime_error(error_msg.str()); } - sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) { - cgh.depends_on(ungqr_event); - auto ctx = exec_q.get_context(); - cgh.host_task([ctx, scratchpad]() { - dpnp::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx); - }); - }); - host_task_events.push_back(clean_up_event); + using dpnp::tensor::alloc_utils::USMDeleter; + sycl::event clean_up_event = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {ungqr_event}, + std::unique_ptr(scratchpad, USMDeleter(exec_q))); + cleanup_events.push_back(clean_up_event); return ungqr_event; } @@ -235,12 +233,12 @@ std::pair char *tau_array_data = tau_array.get_data(); - std::vector host_task_events; + std::vector cleanup_events; sycl::event ungqr_ev = ungqr_fn(exec_q, m, n, k, a_array_data, lda, - tau_array_data, host_task_events, depends); + tau_array_data, cleanup_events, depends); sycl::event args_ev = dpnp::utils::keep_args_alive( - exec_q, {a_array, tau_array}, host_task_events); + exec_q, {a_array, tau_array}, cleanup_events); return std::make_pair(args_ev, ungqr_ev); } diff --git a/dpnp/libtensor/extensions/lapack/ungqr_batch.cpp b/dpnp/libtensor/extensions/lapack/ungqr_batch.cpp index 1ac7220064be..0b7672af95da 100644 --- a/dpnp/libtensor/extensions/lapack/ungqr_batch.cpp +++ b/dpnp/libtensor/extensions/lapack/ungqr_batch.cpp @@ -26,6 +26,7 @@ // THE POSSIBILITY OF SUCH DAMAGE. //***************************************************************************** +#include #include #include @@ -77,7 +78,7 @@ static sycl::event ungqr_batch_impl(sycl::queue &exec_q, char *in_tau, std::int64_t stride_tau, std::int64_t batch_size, - std::vector &host_task_events, + std::vector &cleanup_events, const std::vector &depends) { type_utils::validate_type_for_device(exec_q); @@ -161,14 +162,11 @@ static sycl::event ungqr_batch_impl(sycl::queue &exec_q, throw std::runtime_error(error_msg.str()); } - sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) { - cgh.depends_on(ungqr_batch_event); - auto ctx = exec_q.get_context(); - cgh.host_task([ctx, scratchpad]() { - dpnp::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx); - }); - }); - host_task_events.push_back(clean_up_event); + using dpnp::tensor::alloc_utils::USMDeleter; + sycl::event clean_up_event = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {ungqr_batch_event}, + std::unique_ptr(scratchpad, USMDeleter(exec_q))); + cleanup_events.push_back(clean_up_event); return ungqr_batch_event; } @@ -248,13 +246,13 @@ std::pair const std::int64_t lda = std::max(1UL, m); - std::vector host_task_events; + std::vector cleanup_events; sycl::event ungqr_batch_ev = ungqr_batch_fn( exec_q, m, n, k, a_array_data, lda, stride_a, tau_array_data, - stride_tau, batch_size, host_task_events, depends); + stride_tau, batch_size, cleanup_events, depends); sycl::event args_ev = dpnp::utils::keep_args_alive( - exec_q, {a_array, tau_array}, host_task_events); + exec_q, {a_array, tau_array}, cleanup_events); return std::make_pair(args_ev, ungqr_batch_ev); } diff --git a/dpnp/libtensor/extensions/sparse/gemv.cpp b/dpnp/libtensor/extensions/sparse/gemv.cpp index 05207320f97d..184a001abc10 100644 --- a/dpnp/libtensor/extensions/sparse/gemv.cpp +++ b/dpnp/libtensor/extensions/sparse/gemv.cpp @@ -28,6 +28,7 @@ #include #include +#include #include #include #include @@ -42,6 +43,7 @@ // dpnp tensor headers #include "utils/memory_overlap.hpp" #include "utils/output_validation.hpp" +#include "utils/sycl_alloc_utils.hpp" #include "utils/type_utils.hpp" #include "gemv.hpp" @@ -84,8 +86,7 @@ typedef std::pair (*gemv_init_fn_ptr_t)( /** * compute_impl: fires a single sparse matrix-vector product using a - * pre-built handle. Returns the kernel event directly -- no host_task - * wrapping. + * pre-built handle. Returns the kernel event directly. */ typedef sycl::event (*gemv_compute_fn_ptr_t)( sycl::queue &, @@ -604,18 +605,22 @@ sycl::event sparse_gemv_release(sycl::queue &exec_q, // Free the USM workspace and delete the cache only after all // releases complete (the spec forbids freeing the workspace before - // the spmv using it has finished). A host_task orders this without - // blocking the caller. - sycl::event cleanup_ev = exec_q.submit([&](sycl::handler &cgh) { - cgh.depends_on(rel_evs); - cgh.host_task([cache, exec_q]() { - if (cache->workspace != nullptr) - sycl::free(cache->workspace, exec_q); - delete cache; - }); - }); - - return cleanup_ev; + // the spmv using it has finished), without blocking the caller. + struct CacheDeleter + { + sycl::queue q; + + void operator()(SpmvCache *c) const + { + if (c->workspace != nullptr) + dpnp::tensor::alloc_utils::sycl_free_noexcept(c->workspace, q); + delete c; + } + }; + + return dpnp::tensor::alloc_utils::async_release( + exec_q, rel_evs, + std::unique_ptr(cache, CacheDeleter{exec_q})); } #else // legacy oneMKL sparse API (v1) diff --git a/dpnp/libtensor/extensions/sparse/gemv.hpp b/dpnp/libtensor/extensions/sparse/gemv.hpp index 605a69ea3148..7918367be94c 100644 --- a/dpnp/libtensor/extensions/sparse/gemv.hpp +++ b/dpnp/libtensor/extensions/sparse/gemv.hpp @@ -97,7 +97,7 @@ extern std::tuple * becomes complex(d, 0). This API cannot express a complex-valued * alpha/beta. Solvers use alpha=1, beta=0, which is exact. * - * Returns (host_task_event, gemv_event) + * Returns (cleanup_event, gemv_event) */ extern std::pair sparse_gemv_compute(sycl::queue &exec_q, diff --git a/dpnp/libtensor/extensions/sparse/sparse_py.cpp b/dpnp/libtensor/extensions/sparse/sparse_py.cpp index 09f0d77e213d..b291d845c57b 100644 --- a/dpnp/libtensor/extensions/sparse/sparse_py.cpp +++ b/dpnp/libtensor/extensions/sparse/sparse_py.cpp @@ -107,7 +107,7 @@ PYBIND11_MODULE(_sparse_impl, m, py::mod_gil_not_used()) // dtype match val_type_id from init, and that shapes agree with // op(A) dimensions (swapped for trans != N). // - // Returns (host_task_event, gemv_event); the host_task_event keeps + // Returns (cleanup_event, gemv_event); the cleanup_event keeps // x / y alive until the gemv kernel completes. // ------------------------------------------------------------------ m.def( @@ -127,7 +127,7 @@ PYBIND11_MODULE(_sparse_impl, m, py::mod_gil_not_used()) py::arg("y"), py::arg("num_rows"), py::arg("num_cols"), py::arg("depends"), "Execute sparse::gemv using a pre-built handle. " - "Returns (host_task_event, gemv_event)."); + "Returns (cleanup_event, gemv_event)."); // ------------------------------------------------------------------ // _sparse_gemv_release(exec_q, handle, depends) -> event diff --git a/dpnp/libtensor/extensions/vm/common.hpp b/dpnp/libtensor/extensions/vm/common.hpp index 90fc5f00fed6..96de5488f3fa 100644 --- a/dpnp/libtensor/extensions/vm/common.hpp +++ b/dpnp/libtensor/extensions/vm/common.hpp @@ -53,8 +53,8 @@ namespace td_ns = dpnp::tensor::type_dispatch; namespace dpnp::extensions::vm { // Releases the GIL around a oneMKL VM call. On a CPU device VM waits on its -// `depends` and computes on the calling thread, so holding the GIL can -// deadlock with host tasks that need it (e.g. on an in-order queue). +// `depends` and computes on the calling thread, and holding the GIL meanwhile +// would stall dpctl's deferred releases, which take it. using vm_gil_release = py::gil_scoped_release; namespace py_internal diff --git a/dpnp/libtensor/include/kernels/accumulators.hpp b/dpnp/libtensor/include/kernels/accumulators.hpp index 3c862c181818..861ffcd85565 100644 --- a/dpnp/libtensor/include/kernels/accumulators.hpp +++ b/dpnp/libtensor/include/kernels/accumulators.hpp @@ -671,7 +671,7 @@ sycl::event inclusive_scan_iter_1d(sycl::queue &exec_q, const std::size_t s1, const IndexerT &indexer, const TransformerT &transformer, - std::vector &host_tasks, + std::vector &cleanup_events, const std::vector &depends = {}) { static constexpr ScanOpT scan_op{}; @@ -764,7 +764,7 @@ sycl::event inclusive_scan_iter_1d(sycl::queue &exec_q, sycl::event free_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {dependent_event}, temp_owner); - host_tasks.push_back(free_ev); + cleanup_events.push_back(free_ev); } return dependent_event; @@ -788,7 +788,7 @@ sycl::event std::size_t n_elems, const char *src, char *dst, - std::vector &host_tasks, + std::vector &cleanup_events, const std::vector &depends = {}) { const srcT *src_data_ptr = reinterpret_cast(src); @@ -810,7 +810,7 @@ sycl::event transformerT, AccumulateOpT, include_initial>( q, wg_size, n_elems, src_data_ptr, dst_data_ptr, s0, s1, - flat_indexer, transformer, host_tasks, depends); + flat_indexer, transformer, cleanup_events, depends); } else { static constexpr nwiT n_wi_for_gpu = 4; @@ -822,7 +822,7 @@ sycl::event transformerT, AccumulateOpT, include_initial>( q, wg_size, n_elems, src_data_ptr, dst_data_ptr, s0, s1, - flat_indexer, transformer, host_tasks, depends); + flat_indexer, transformer, cleanup_events, depends); } return comp_ev; } @@ -966,7 +966,7 @@ sycl::event inclusive_scan_iter(sycl::queue &exec_q, const InpIndexerT &inp_indexer, const OutIndexerT &out_indexer, const TransformerT &transformer, - std::vector &host_tasks, + std::vector &cleanup_events, const std::vector &depends = {}) { static constexpr ScanOpT scan_op{}; @@ -1122,7 +1122,7 @@ sycl::event inclusive_scan_iter(sycl::queue &exec_q, sycl::event free_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {dependent_event}, temp_owner); - host_tasks.push_back(free_ev); + cleanup_events.push_back(free_ev); } return dependent_event; @@ -1160,7 +1160,7 @@ sycl::event int acc_nd, const ssize_t *acc_shape_strides, char *dst, - std::vector &host_tasks, + std::vector &cleanup_events, const std::vector &depends = {}) { const srcT *src_data_ptr = reinterpret_cast(src); @@ -1194,7 +1194,7 @@ sycl::event transformerT, AccumulateOpT, include_initial>( q, wg_size, iter_nelems, acc_nelems, src_data_ptr, dst_data_ptr, s0, s1, inp_iter_indexer, out_iter_indexer, inp_axis_indexer, - out_axis_indexer, transformer, host_tasks, depends); + out_axis_indexer, transformer, cleanup_events, depends); } else { static constexpr nwiT n_wi_for_gpu = 4; @@ -1208,7 +1208,7 @@ sycl::event transformerT, AccumulateOpT, include_initial>( q, wg_size, iter_nelems, acc_nelems, src_data_ptr, dst_data_ptr, s0, s1, inp_iter_indexer, out_iter_indexer, inp_axis_indexer, - out_axis_indexer, transformer, host_tasks, depends); + out_axis_indexer, transformer, cleanup_events, depends); } return comp_ev; @@ -1227,7 +1227,7 @@ std::size_t cumsum_val_contig_impl(sycl::queue &q, std::size_t n_elems, const char *mask, char *cumsum, - std::vector &host_tasks, + std::vector &cleanup_events, const std::vector &depends = {}) { const maskT *mask_data_ptr = reinterpret_cast(mask); @@ -1251,7 +1251,7 @@ std::size_t cumsum_val_contig_impl(sycl::queue &q, NoOpIndexerT, transformerT, AccumulateOpT, include_initial>( q, wg_size, n_elems, mask_data_ptr, cumsum_data_ptr, s0, s1, - flat_indexer, transformer, host_tasks, depends); + flat_indexer, transformer, cleanup_events, depends); } else { static constexpr nwiT n_wi_for_gpu = 4; @@ -1263,7 +1263,7 @@ std::size_t cumsum_val_contig_impl(sycl::queue &q, NoOpIndexerT, transformerT, AccumulateOpT, include_initial>( q, wg_size, n_elems, mask_data_ptr, cumsum_data_ptr, s0, s1, - flat_indexer, transformer, host_tasks, depends); + flat_indexer, transformer, cleanup_events, depends); } cumsumT *last_elem = cumsum_data_ptr + (n_elems - 1); @@ -1346,7 +1346,7 @@ std::size_t int nd, const ssize_t *shape_strides, char *cumsum, - std::vector &host_tasks, + std::vector &cleanup_events, const std::vector &depends = {}) { const maskT *mask_data_ptr = reinterpret_cast(mask); @@ -1370,7 +1370,7 @@ std::size_t StridedIndexerT, transformerT, AccumulateOpT, include_initial>( q, wg_size, n_elems, mask_data_ptr, cumsum_data_ptr, s0, s1, - strided_indexer, transformer, host_tasks, depends); + strided_indexer, transformer, cleanup_events, depends); } else { static constexpr nwiT n_wi_for_gpu = 4; @@ -1382,7 +1382,7 @@ std::size_t StridedIndexerT, transformerT, AccumulateOpT, include_initial>( q, wg_size, n_elems, mask_data_ptr, cumsum_data_ptr, s0, s1, - strided_indexer, transformer, host_tasks, depends); + strided_indexer, transformer, cleanup_events, depends); } cumsumT *last_elem = cumsum_data_ptr + (n_elems - 1); diff --git a/dpnp/libtensor/include/kernels/elementwise_functions/add.hpp b/dpnp/libtensor/include/kernels/elementwise_functions/add.hpp index a4d5b4252e99..8265cc2e95e9 100644 --- a/dpnp/libtensor/include/kernels/elementwise_functions/add.hpp +++ b/dpnp/libtensor/include/kernels/elementwise_functions/add.hpp @@ -367,7 +367,7 @@ using AddContigMatrixContigRowBroadcastingFunctor = template sycl::event add_contig_matrix_contig_row_broadcast_impl( sycl::queue &exec_q, - std::vector &host_tasks, + std::vector &cleanup_events, std::size_t n0, std::size_t n1, const char *mat_p, // typeless pointer to (n0, n1) C-contiguous matrix @@ -381,7 +381,7 @@ sycl::event add_contig_matrix_contig_row_broadcast_impl( { return elementwise_common::binary_contig_matrix_contig_row_broadcast_impl< argT1, argT2, resT, AddContigMatrixContigRowBroadcastingFunctor, - add_matrix_row_broadcast_sg_krn>(exec_q, host_tasks, n0, n1, mat_p, + add_matrix_row_broadcast_sg_krn>(exec_q, cleanup_events, n0, n1, mat_p, mat_offset, vec_p, vec_offset, res_p, res_offset, depends); } @@ -415,7 +415,7 @@ struct AddContigMatrixContigRowBroadcastFactory template sycl::event add_contig_row_contig_matrix_broadcast_impl( sycl::queue &exec_q, - std::vector &host_tasks, + std::vector &cleanup_events, std::size_t n0, std::size_t n1, const char *vec_p, // typeless pointer to (n1,) contiguous row @@ -428,8 +428,8 @@ sycl::event add_contig_row_contig_matrix_broadcast_impl( const std::vector &depends = {}) { return add_contig_matrix_contig_row_broadcast_impl( - exec_q, host_tasks, n0, n1, mat_p, mat_offset, vec_p, vec_offset, res_p, - res_offset, depends); + exec_q, cleanup_events, n0, n1, mat_p, mat_offset, vec_p, vec_offset, + res_p, res_offset, depends); }; template @@ -637,7 +637,7 @@ using AddInplaceRowMatrixBroadcastingFunctor = template sycl::event add_inplace_row_matrix_broadcast_impl( sycl::queue &exec_q, - std::vector &host_tasks, + std::vector &cleanup_events, std::size_t n0, std::size_t n1, const char *vec_p, // typeless pointer to (n1,) contiguous row @@ -648,7 +648,7 @@ sycl::event add_inplace_row_matrix_broadcast_impl( { return elementwise_common::binary_inplace_row_matrix_broadcast_impl< argT, resT, AddInplaceRowMatrixBroadcastingFunctor, - add_inplace_row_matrix_broadcast_sg_krn>(exec_q, host_tasks, n0, n1, + add_inplace_row_matrix_broadcast_sg_krn>(exec_q, cleanup_events, n0, n1, vec_p, vec_offset, mat_p, mat_offset, depends); } diff --git a/dpnp/libtensor/include/kernels/elementwise_functions/common.hpp b/dpnp/libtensor/include/kernels/elementwise_functions/common.hpp index 39e472ddb727..eb798649593d 100644 --- a/dpnp/libtensor/include/kernels/elementwise_functions/common.hpp +++ b/dpnp/libtensor/include/kernels/elementwise_functions/common.hpp @@ -905,7 +905,7 @@ template < template class kernel_name> sycl::event binary_contig_matrix_contig_row_broadcast_impl( sycl::queue &exec_q, - std::vector &host_tasks, + std::vector &cleanup_events, std::size_t n0, std::size_t n1, const char *mat_p, // typeless pointer to (n0, n1) C-contiguous matrix @@ -965,7 +965,7 @@ sycl::event binary_contig_matrix_contig_row_broadcast_impl( sycl::event tmp_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {comp_ev}, padded_vec_owner); - host_tasks.push_back(tmp_cleanup_ev); + cleanup_events.push_back(tmp_cleanup_ev); return comp_ev; } @@ -980,7 +980,7 @@ template < template class kernel_name> sycl::event binary_contig_row_contig_matrix_broadcast_impl( sycl::queue &exec_q, - std::vector &host_tasks, + std::vector &cleanup_events, std::size_t n0, std::size_t n1, const char *vec_p, // typeless pointer to (n1,) contiguous row @@ -1040,7 +1040,7 @@ sycl::event binary_contig_row_contig_matrix_broadcast_impl( sycl::event tmp_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {comp_ev}, padded_vec_owner); - host_tasks.push_back(tmp_cleanup_ev); + cleanup_events.push_back(tmp_cleanup_ev); return comp_ev; }; diff --git a/dpnp/libtensor/include/kernels/elementwise_functions/common_inplace.hpp b/dpnp/libtensor/include/kernels/elementwise_functions/common_inplace.hpp index fa5d3ce58d22..f6a9d7769a94 100644 --- a/dpnp/libtensor/include/kernels/elementwise_functions/common_inplace.hpp +++ b/dpnp/libtensor/include/kernels/elementwise_functions/common_inplace.hpp @@ -432,7 +432,7 @@ template class kernel_name> sycl::event binary_inplace_row_matrix_broadcast_impl( sycl::queue &exec_q, - std::vector &host_tasks, + std::vector &cleanup_events, std::size_t n0, std::size_t n1, const char *vec_p, // typeless pointer to (n1,) contiguous row @@ -485,7 +485,7 @@ sycl::event binary_inplace_row_matrix_broadcast_impl( sycl::event tmp_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {comp_ev}, padded_vec_owner); - host_tasks.push_back(tmp_cleanup_ev); + cleanup_events.push_back(tmp_cleanup_ev); return comp_ev; } diff --git a/dpnp/libtensor/include/kernels/elementwise_functions/multiply.hpp b/dpnp/libtensor/include/kernels/elementwise_functions/multiply.hpp index d34c17bbbe35..408283ec2263 100644 --- a/dpnp/libtensor/include/kernels/elementwise_functions/multiply.hpp +++ b/dpnp/libtensor/include/kernels/elementwise_functions/multiply.hpp @@ -325,7 +325,7 @@ using MultiplyContigMatrixContigRowBroadcastingFunctor = template sycl::event multiply_contig_matrix_contig_row_broadcast_impl( sycl::queue &exec_q, - std::vector &host_tasks, + std::vector &cleanup_events, std::size_t n0, std::size_t n1, const char *mat_p, // typeless pointer to (n0, n1) C-contiguous matrix @@ -339,9 +339,9 @@ sycl::event multiply_contig_matrix_contig_row_broadcast_impl( { return elementwise_common::binary_contig_matrix_contig_row_broadcast_impl< argT1, argT2, resT, MultiplyContigMatrixContigRowBroadcastingFunctor, - multiply_matrix_row_broadcast_sg_krn>(exec_q, host_tasks, n0, n1, mat_p, - mat_offset, vec_p, vec_offset, - res_p, res_offset, depends); + multiply_matrix_row_broadcast_sg_krn>( + exec_q, cleanup_events, n0, n1, mat_p, mat_offset, vec_p, vec_offset, + res_p, res_offset, depends); } template @@ -374,7 +374,7 @@ struct MultiplyContigMatrixContigRowBroadcastFactory template sycl::event multiply_contig_row_contig_matrix_broadcast_impl( sycl::queue &exec_q, - std::vector &host_tasks, + std::vector &cleanup_events, std::size_t n0, std::size_t n1, const char *vec_p, // typeless pointer to (n1,) contiguous row @@ -387,8 +387,8 @@ sycl::event multiply_contig_row_contig_matrix_broadcast_impl( const std::vector &depends = {}) { return multiply_contig_matrix_contig_row_broadcast_impl( - exec_q, host_tasks, n0, n1, mat_p, mat_offset, vec_p, vec_offset, res_p, - res_offset, depends); + exec_q, cleanup_events, n0, n1, mat_p, mat_offset, vec_p, vec_offset, + res_p, res_offset, depends); }; template @@ -599,7 +599,7 @@ using MultiplyInplaceRowMatrixBroadcastingFunctor = template sycl::event multiply_inplace_row_matrix_broadcast_impl( sycl::queue &exec_q, - std::vector &host_tasks, + std::vector &cleanup_events, std::size_t n0, std::size_t n1, const char *vec_p, // typeless pointer to (n1,) contiguous row @@ -611,7 +611,7 @@ sycl::event multiply_inplace_row_matrix_broadcast_impl( return elementwise_common::binary_inplace_row_matrix_broadcast_impl< argT, resT, MultiplyInplaceRowMatrixBroadcastingFunctor, multiply_inplace_row_matrix_broadcast_sg_krn>( - exec_q, host_tasks, n0, n1, vec_p, vec_offset, mat_p, mat_offset, + exec_q, cleanup_events, n0, n1, vec_p, vec_offset, mat_p, mat_offset, depends); } diff --git a/dpnp/libtensor/include/kernels/elementwise_functions/subtract.hpp b/dpnp/libtensor/include/kernels/elementwise_functions/subtract.hpp index a1b6df1d3fa7..e7adc6b22b58 100644 --- a/dpnp/libtensor/include/kernels/elementwise_functions/subtract.hpp +++ b/dpnp/libtensor/include/kernels/elementwise_functions/subtract.hpp @@ -323,7 +323,7 @@ class subtract_row_matrix_broadcast_sg_krn; template sycl::event subtract_contig_matrix_contig_row_broadcast_impl( sycl::queue &exec_q, - std::vector &host_tasks, + std::vector &cleanup_events, std::size_t n0, std::size_t n1, const char *mat_p, // typeless pointer to (n0, n1) C-contiguous matrix @@ -337,9 +337,9 @@ sycl::event subtract_contig_matrix_contig_row_broadcast_impl( { return elementwise_common::binary_contig_matrix_contig_row_broadcast_impl< argT1, argT2, resT, SubtractContigMatrixContigRowBroadcastingFunctor, - subtract_matrix_row_broadcast_sg_krn>(exec_q, host_tasks, n0, n1, mat_p, - mat_offset, vec_p, vec_offset, - res_p, res_offset, depends); + subtract_matrix_row_broadcast_sg_krn>( + exec_q, cleanup_events, n0, n1, mat_p, mat_offset, vec_p, vec_offset, + res_p, res_offset, depends); } template @@ -372,7 +372,7 @@ struct SubtractContigMatrixContigRowBroadcastFactory template sycl::event subtract_contig_row_contig_matrix_broadcast_impl( sycl::queue &exec_q, - std::vector &host_tasks, + std::vector &cleanup_events, std::size_t n0, std::size_t n1, const char *vec_p, // typeless pointer to (n1,) contiguous row @@ -386,9 +386,9 @@ sycl::event subtract_contig_row_contig_matrix_broadcast_impl( { return elementwise_common::binary_contig_row_contig_matrix_broadcast_impl< argT1, argT2, resT, SubtractContigRowContigMatrixBroadcastingFunctor, - subtract_row_matrix_broadcast_sg_krn>(exec_q, host_tasks, n0, n1, vec_p, - vec_offset, mat_p, mat_offset, - res_p, res_offset, depends); + subtract_row_matrix_broadcast_sg_krn>( + exec_q, cleanup_events, n0, n1, vec_p, vec_offset, mat_p, mat_offset, + res_p, res_offset, depends); } template @@ -598,7 +598,7 @@ using SubtractInplaceRowMatrixBroadcastingFunctor = template sycl::event subtract_inplace_row_matrix_broadcast_impl( sycl::queue &exec_q, - std::vector &host_tasks, + std::vector &cleanup_events, std::size_t n0, std::size_t n1, const char *vec_p, // typeless pointer to (n1,) contiguous row @@ -610,7 +610,7 @@ sycl::event subtract_inplace_row_matrix_broadcast_impl( return elementwise_common::binary_inplace_row_matrix_broadcast_impl< argT, resT, SubtractInplaceRowMatrixBroadcastingFunctor, subtract_inplace_row_matrix_broadcast_sg_krn>( - exec_q, host_tasks, n0, n1, vec_p, vec_offset, mat_p, mat_offset, + exec_q, cleanup_events, n0, n1, vec_p, vec_offset, mat_p, mat_offset, depends); } diff --git a/dpnp/libtensor/include/kernels/elementwise_functions/true_divide.hpp b/dpnp/libtensor/include/kernels/elementwise_functions/true_divide.hpp index 2c3ee16e3118..d4127436502c 100644 --- a/dpnp/libtensor/include/kernels/elementwise_functions/true_divide.hpp +++ b/dpnp/libtensor/include/kernels/elementwise_functions/true_divide.hpp @@ -327,7 +327,7 @@ class true_divide_row_matrix_broadcast_sg_krn; template sycl::event true_divide_contig_matrix_contig_row_broadcast_impl( sycl::queue &exec_q, - std::vector &host_tasks, + std::vector &cleanup_events, std::size_t n0, std::size_t n1, const char *mat_p, // typeless pointer to (n0, n1) C-contiguous matrix @@ -342,8 +342,8 @@ sycl::event true_divide_contig_matrix_contig_row_broadcast_impl( return elementwise_common::binary_contig_matrix_contig_row_broadcast_impl< argT1, argT2, resT, TrueDivideContigMatrixContigRowBroadcastingFunctor, true_divide_matrix_row_broadcast_sg_krn>( - exec_q, host_tasks, n0, n1, mat_p, mat_offset, vec_p, vec_offset, res_p, - res_offset, depends); + exec_q, cleanup_events, n0, n1, mat_p, mat_offset, vec_p, vec_offset, + res_p, res_offset, depends); } template @@ -376,7 +376,7 @@ struct TrueDivideContigMatrixContigRowBroadcastFactory template sycl::event true_divide_contig_row_contig_matrix_broadcast_impl( sycl::queue &exec_q, - std::vector &host_tasks, + std::vector &cleanup_events, std::size_t n0, std::size_t n1, const char *vec_p, // typeless pointer to (n1,) contiguous row @@ -391,8 +391,8 @@ sycl::event true_divide_contig_row_contig_matrix_broadcast_impl( return elementwise_common::binary_contig_row_contig_matrix_broadcast_impl< argT1, argT2, resT, TrueDivideContigRowContigMatrixBroadcastingFunctor, true_divide_row_matrix_broadcast_sg_krn>( - exec_q, host_tasks, n0, n1, vec_p, vec_offset, mat_p, mat_offset, res_p, - res_offset, depends); + exec_q, cleanup_events, n0, n1, vec_p, vec_offset, mat_p, mat_offset, + res_p, res_offset, depends); }; template @@ -620,7 +620,7 @@ using TrueDivideInplaceRowMatrixBroadcastingFunctor = template sycl::event true_divide_inplace_row_matrix_broadcast_impl( sycl::queue &exec_q, - std::vector &host_tasks, + std::vector &cleanup_events, std::size_t n0, std::size_t n1, const char *vec_p, // typeless pointer to (n1,) contiguous row @@ -632,7 +632,7 @@ sycl::event true_divide_inplace_row_matrix_broadcast_impl( return elementwise_common::binary_inplace_row_matrix_broadcast_impl< argT, resT, TrueDivideInplaceRowMatrixBroadcastingFunctor, true_divide_inplace_row_matrix_broadcast_sg_krn>( - exec_q, host_tasks, n0, n1, vec_p, vec_offset, mat_p, mat_offset, + exec_q, cleanup_events, n0, n1, vec_p, vec_offset, mat_p, mat_offset, depends); } diff --git a/dpnp/libtensor/include/kernels/linalg_functions/dot_product.hpp b/dpnp/libtensor/include/kernels/linalg_functions/dot_product.hpp index 40b239e4e127..f86a05296ba2 100644 --- a/dpnp/libtensor/include/kernels/linalg_functions/dot_product.hpp +++ b/dpnp/libtensor/include/kernels/linalg_functions/dot_product.hpp @@ -1148,12 +1148,11 @@ sycl::event dot_product_tree_impl(sycl::queue &exec_q, remaining_reduction_nelems, reductions_per_wi, reduction_groups, in_out_iter_indexer, reduction_indexer, {dependent_ev}); - // transfer ownership of USM allocation to host_task - sycl::event cleanup_host_task_event = - dpnp::tensor::alloc_utils::async_smart_free( - exec_q, {final_reduction_ev}, partially_reduced_tmp_owner); + // free the temporary once the reduction completes + sycl::event cleanup_event = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {final_reduction_ev}, partially_reduced_tmp_owner); - return cleanup_host_task_event; + return cleanup_event; } } @@ -1385,11 +1384,10 @@ sycl::event remaining_reduction_nelems, reductions_per_wi, reduction_groups, in_out_iter_indexer, reduction_indexer, {dependent_ev}); - sycl::event cleanup_host_task_event = - dpnp::tensor::alloc_utils::async_smart_free( - exec_q, {final_reduction_ev}, partially_reduced_tmp_owner); + sycl::event cleanup_event = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {final_reduction_ev}, partially_reduced_tmp_owner); - return cleanup_host_task_event; + return cleanup_event; } } diff --git a/dpnp/libtensor/include/kernels/linalg_functions/gemm.hpp b/dpnp/libtensor/include/kernels/linalg_functions/gemm.hpp index b6f5c4249b7f..58aae81be370 100644 --- a/dpnp/libtensor/include/kernels/linalg_functions/gemm.hpp +++ b/dpnp/libtensor/include/kernels/linalg_functions/gemm.hpp @@ -2397,11 +2397,11 @@ sycl::event batch_nd + res_outer_nd, res_batch_offset, res_shape_strides, {gemm_ev}); - sycl::event cleanup_host_task_event = + sycl::event cleanup_event = dpnp::tensor::alloc_utils::async_smart_free(exec_q, {red_ev}, tmp_owner); - return cleanup_host_task_event; + return cleanup_event; } else { assert(reduction_groups > 1); @@ -2460,11 +2460,11 @@ sycl::event batch_nd + res_outer_nd, res_batch_offset, res_shape_strides, {gemm_ev}); - sycl::event cleanup_host_task_event = + sycl::event cleanup_event = dpnp::tensor::alloc_utils::async_smart_free(exec_q, {red_ev}, tmp_owner); - return cleanup_host_task_event; + return cleanup_event; } } } @@ -2688,11 +2688,11 @@ sycl::event batch_nd + res_outer_nd, res_batch_offset, res_shape_strides, {gemm_ev}); - sycl::event cleanup_host_task_event = + sycl::event cleanup_event = dpnp::tensor::alloc_utils::async_smart_free(exec_q, {red_ev}, tmp_owner); - return cleanup_host_task_event; + return cleanup_event; } else { assert(reduction_groups > 1); @@ -2752,11 +2752,11 @@ sycl::event batch_nd + res_outer_nd, res_batch_offset, res_shape_strides, {gemm_ev}); - sycl::event cleanup_host_task_event = + sycl::event cleanup_event = dpnp::tensor::alloc_utils::async_smart_free(exec_q, {red_ev}, tmp_owner); - return cleanup_host_task_event; + return cleanup_event; } } } @@ -3055,11 +3055,11 @@ sycl::event reduction_nelems, reduction_groups, wg, max_wg, preferred_reductions_per_wi, reductions_per_wi, {gemm_ev}); - sycl::event cleanup_host_task_event = + sycl::event cleanup_event = dpnp::tensor::alloc_utils::async_smart_free(exec_q, {red_ev}, tmp_owner); - return cleanup_host_task_event; + return cleanup_event; } else { assert(reduction_groups > 1); @@ -3109,11 +3109,11 @@ sycl::event reduction_groups, wg, max_wg, preferred_reductions_per_wi, reductions_per_wi, {gemm_ev}); - sycl::event cleanup_host_task_event = + sycl::event cleanup_event = dpnp::tensor::alloc_utils::async_smart_free(exec_q, {red_ev}, tmp_owner); - return cleanup_host_task_event; + return cleanup_event; } } } @@ -3243,11 +3243,11 @@ sycl::event reduction_nelems, reduction_groups, wg, max_wg, preferred_reductions_per_wi, reductions_per_wi, {gemm_ev}); - sycl::event cleanup_host_task_event = + sycl::event cleanup_event = dpnp::tensor::alloc_utils::async_smart_free(exec_q, {red_ev}, tmp_owner); - return cleanup_host_task_event; + return cleanup_event; } else { assert(reduction_groups > 1); @@ -3299,11 +3299,11 @@ sycl::event reduction_groups, wg, max_wg, preferred_reductions_per_wi, reductions_per_wi, {gemm_ev}); - sycl::event cleanup_host_task_event = + sycl::event cleanup_event = dpnp::tensor::alloc_utils::async_smart_free(exec_q, {red_ev}, tmp_owner); - return cleanup_host_task_event; + return cleanup_event; } } } @@ -3599,10 +3599,10 @@ sycl::event gemm_tree_k_impl(sycl::queue &exec_q, preferred_reductions_per_wi, reductions_per_wi, res_nd, 0, res_shapes_strides, {gemm_ev}); - sycl::event cleanup_host_task_event = + sycl::event cleanup_event = dpnp::tensor::alloc_utils::async_smart_free(exec_q, {red_ev}, tmp_owner); - return cleanup_host_task_event; + return cleanup_event; } else { assert(reduction_groups > 1); @@ -3636,11 +3636,11 @@ sycl::event gemm_tree_k_impl(sycl::queue &exec_q, wg, max_wg, preferred_reductions_per_wi, reductions_per_wi, res_nd, 0, res_shapes_strides, {gemm_ev}); - sycl::event cleanup_host_task_event = + sycl::event cleanup_event = dpnp::tensor::alloc_utils::async_smart_free(exec_q, {red_ev}, tmp_owner); - return cleanup_host_task_event; + return cleanup_event; } } } @@ -3751,11 +3751,11 @@ sycl::event gemm_tree_nm_impl(sycl::queue &exec_q, preferred_reductions_per_wi, reductions_per_wi, res_nd, 0, res_shapes_strides, {gemm_ev}); - sycl::event cleanup_host_task_event = + sycl::event cleanup_event = dpnp::tensor::alloc_utils::async_smart_free(exec_q, {red_ev}, tmp_owner); - return cleanup_host_task_event; + return cleanup_event; } else { assert(reduction_groups > 1); @@ -3788,11 +3788,11 @@ sycl::event gemm_tree_nm_impl(sycl::queue &exec_q, wg, max_wg, preferred_reductions_per_wi, reductions_per_wi, res_nd, 0, res_shapes_strides, {gemm_ev}); - sycl::event cleanup_host_task_event = + sycl::event cleanup_event = dpnp::tensor::alloc_utils::async_smart_free(exec_q, {red_ev}, tmp_owner); - return cleanup_host_task_event; + return cleanup_event; } } } @@ -3986,10 +3986,10 @@ sycl::event gemm_contig_tree_k_impl(sycl::queue &exec_q, reduction_nelems, reduction_groups, wg, max_wg, preferred_reductions_per_wi, reductions_per_wi, {gemm_ev}); - sycl::event cleanup_host_task_event = + sycl::event cleanup_event = dpnp::tensor::alloc_utils::async_smart_free(exec_q, {red_ev}, tmp_owner); - return cleanup_host_task_event; + return cleanup_event; } else { assert(reduction_groups > 1); @@ -4021,11 +4021,11 @@ sycl::event gemm_contig_tree_k_impl(sycl::queue &exec_q, reduction_groups, wg, max_wg, preferred_reductions_per_wi, reductions_per_wi, {gemm_ev}); - sycl::event cleanup_host_task_event = + sycl::event cleanup_event = dpnp::tensor::alloc_utils::async_smart_free(exec_q, {red_ev}, tmp_owner); - return cleanup_host_task_event; + return cleanup_event; } } } @@ -4125,10 +4125,10 @@ sycl::event gemm_contig_tree_nm_impl(sycl::queue &exec_q, reduction_nelems, reduction_groups, wg, max_wg, preferred_reductions_per_wi, reductions_per_wi, {gemm_ev}); - sycl::event cleanup_host_task_event = + sycl::event cleanup_event = dpnp::tensor::alloc_utils::async_smart_free(exec_q, {red_ev}, tmp_owner); - return cleanup_host_task_event; + return cleanup_event; } else { assert(reduction_groups > 1); @@ -4159,11 +4159,11 @@ sycl::event gemm_contig_tree_nm_impl(sycl::queue &exec_q, reduction_groups, wg, max_wg, preferred_reductions_per_wi, reductions_per_wi, {gemm_ev}); - sycl::event cleanup_host_task_event = + sycl::event cleanup_event = dpnp::tensor::alloc_utils::async_smart_free(exec_q, {red_ev}, tmp_owner); - return cleanup_host_task_event; + return cleanup_event; } } } diff --git a/dpnp/libtensor/include/kernels/reductions.hpp b/dpnp/libtensor/include/kernels/reductions.hpp index 8d06fc1da22f..18278391c1e6 100644 --- a/dpnp/libtensor/include/kernels/reductions.hpp +++ b/dpnp/libtensor/include/kernels/reductions.hpp @@ -1382,14 +1382,13 @@ sycl::event reduction_over_group_temps_strided_impl( remaining_reduction_nelems, reductions_per_wi, reduction_groups, in_out_iter_indexer, reduction_indexer, {dependent_ev}); - sycl::event cleanup_host_task_event = - dpnp::tensor::alloc_utils::async_smart_free( - exec_q, {final_reduction_ev}, tmp_owner); + sycl::event cleanup_event = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {final_reduction_ev}, tmp_owner); - // FIXME: do not return host-task event - // Instead collect all host-tasks to a list + // FIXME: do not return cleanup event + // Instead collect all cleanup events to a list - return cleanup_host_task_event; + return cleanup_event; } } @@ -1613,14 +1612,13 @@ sycl::event reduction_axis1_over_group_temps_contig_impl( remaining_reduction_nelems, reductions_per_wi, reduction_groups, in_out_iter_indexer, reduction_indexer, {dependent_ev}); - sycl::event cleanup_host_task_event = - dpnp::tensor::alloc_utils::async_smart_free( - exec_q, {final_reduction_ev}, tmp_owner); + sycl::event cleanup_event = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {final_reduction_ev}, tmp_owner); - // FIXME: do not return host-task event - // Instead collect all host-tasks to a list + // FIXME: do not return cleanup event + // Instead collect all cleanup events to a list - return cleanup_host_task_event; + return cleanup_event; } } @@ -1846,14 +1844,13 @@ sycl::event reduction_axis0_over_group_temps_contig_impl( remaining_reduction_nelems, reductions_per_wi, reduction_groups, in_out_iter_indexer, reduction_indexer, {dependent_ev}); - sycl::event cleanup_host_task_event = - dpnp::tensor::alloc_utils::async_smart_free( - exec_q, {final_reduction_ev}, tmp_owner); + sycl::event cleanup_event = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {final_reduction_ev}, tmp_owner); - // FIXME: do not return host-task event - // Instead collect all host-tasks to a list + // FIXME: do not return cleanup event + // Instead collect all cleanup events to a list - return cleanup_host_task_event; + return cleanup_event; } } @@ -2760,14 +2757,13 @@ sycl::event search_over_group_temps_strided_impl( reductions_per_wi, reduction_groups, in_out_iter_indexer, reduction_indexer, {dependent_ev}); - sycl::event cleanup_host_task_event = - dpnp::tensor::alloc_utils::async_smart_free( - exec_q, {final_reduction_ev}, tmp_owner, val_tmp_owner); + sycl::event cleanup_event = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {final_reduction_ev}, tmp_owner, val_tmp_owner); - // FIXME: do not return host-task event - // Instead collect all host-tasks to a list + // FIXME: do not return cleanup event + // Instead collect all cleanup events to a list - return cleanup_host_task_event; + return cleanup_event; } } @@ -3033,14 +3029,13 @@ sycl::event search_axis1_over_group_temps_contig_impl( reductions_per_wi, reduction_groups, in_out_iter_indexer, reduction_indexer, {dependent_ev}); - sycl::event cleanup_host_task_event = - dpnp::tensor::alloc_utils::async_smart_free( - exec_q, {final_reduction_ev}, tmp_owner, val_tmp_owner); + sycl::event cleanup_event = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {final_reduction_ev}, tmp_owner, val_tmp_owner); - // FIXME: do not return host-task event - // Instead collect all host-tasks to a list + // FIXME: do not return cleanup event + // Instead collect all cleanup events to a list - return cleanup_host_task_event; + return cleanup_event; } } @@ -3301,14 +3296,13 @@ sycl::event search_axis0_over_group_temps_contig_impl( reductions_per_wi, reduction_groups, in_out_iter_indexer, reduction_indexer, {dependent_ev}); - sycl::event cleanup_host_task_event = - dpnp::tensor::alloc_utils::async_smart_free( - exec_q, {final_reduction_ev}, tmp_owner, vals_tmp_owner); + sycl::event cleanup_event = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {final_reduction_ev}, tmp_owner, vals_tmp_owner); - // FIXME: do not return host-task event - // Instead collect all host-tasks to a list + // FIXME: do not return cleanup event + // Instead collect all cleanup events to a list - return cleanup_host_task_event; + return cleanup_event; } } @@ -3891,14 +3885,13 @@ sycl::event remaining_reduction_nelems, reductions_per_wi, reduction_groups, in_out_iter_indexer, tmp_reduction_indexer, {dependent_ev}); - sycl::event cleanup_host_task_event = - dpnp::tensor::alloc_utils::async_smart_free( - exec_q, {final_reduction_ev}, tmp_owner); + sycl::event cleanup_event = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {final_reduction_ev}, tmp_owner); - // FIXME: do not return host-task event - // Instead collect all host-tasks to a list + // FIXME: do not return cleanup event + // Instead collect all cleanup events to a list - return cleanup_host_task_event; + return cleanup_event; } typedef sycl::event (*minmax_strided_impl_fn_ptr)( diff --git a/dpnp/libtensor/include/kernels/sorting/topk.hpp b/dpnp/libtensor/include/kernels/sorting/topk.hpp index e084102ec685..b9f988c00858 100644 --- a/dpnp/libtensor/include/kernels/sorting/topk.hpp +++ b/dpnp/libtensor/include/kernels/sorting/topk.hpp @@ -190,11 +190,10 @@ sycl::event exec_q, iter_nelems, k, arg_tp, index_data, axis_nelems, axis_nelems, vals_tp, inds_tp, {merges_ev}); - sycl::event cleanup_host_task_event = - dpnp::tensor::alloc_utils::async_smart_free(exec_q, {write_out_ev}, - index_data_owner); + sycl::event cleanup_event = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {write_out_ev}, index_data_owner); - return cleanup_host_task_event; + return cleanup_event; }; template @@ -431,11 +430,10 @@ sycl::event topk_merge_impl( exec_q, iter_nelems, k, arg_tp, index_data, alloc_len, axis_nelems, vals_tp, inds_tp, {merges_ev}); - sycl::event cleanup_host_task_event = - dpnp::tensor::alloc_utils::async_smart_free(exec_q, {write_topk_ev}, - index_data_owner); + sycl::event cleanup_event = dpnp::tensor::alloc_utils::async_smart_free( + exec_q, {write_topk_ev}, index_data_owner); - return cleanup_host_task_event; + return cleanup_event; } } diff --git a/dpnp/libtensor/include/utils/offset_utils.hpp b/dpnp/libtensor/include/utils/offset_utils.hpp index 05ae82657168..23f1fede3664 100644 --- a/dpnp/libtensor/include/utils/offset_utils.hpp +++ b/dpnp/libtensor/include/utils/offset_utils.hpp @@ -95,7 +95,7 @@ std::tuple, std::size_t, sycl::event> device_allocate_and_pack(sycl::queue &q, - std::vector &host_task_events, + std::vector &cleanup_events, Vs &&...vs) { @@ -111,7 +111,7 @@ std::tuple, shT packed_shape_strides = detail::concat(std::move(empty), vs...); auto packed_shape_strides_owner = - std::make_shared(std::move(packed_shape_strides)); + std::make_unique(std::move(packed_shape_strides)); auto sz = packed_shape_strides_owner->size(); auto shape_strides_owner = @@ -121,15 +121,9 @@ std::tuple, sycl::event copy_ev = q.copy(packed_shape_strides_owner->data(), shape_strides, sz); - sycl::event cleanup_host_task_ev = q.submit([&](sycl::handler &cgh) { - cgh.depends_on(copy_ev); - cgh.host_task([packed_shape_strides_owner = - std::move(packed_shape_strides_owner)] { - // increment shared pointer ref-count to keep it alive - // till copy operation completes; - }); - }); - host_task_events.push_back(cleanup_host_task_ev); + // keep the host data alive until the copy completes + cleanup_events.push_back(dpnp::tensor::alloc_utils::async_release( + q, {copy_ev}, std::move(packed_shape_strides_owner))); return std::make_tuple(std::move(shape_strides_owner), sz, copy_ev); } diff --git a/dpnp/libtensor/include/utils/sycl_alloc_utils.hpp b/dpnp/libtensor/include/utils/sycl_alloc_utils.hpp index a3891a888430..78fd641e8f46 100644 --- a/dpnp/libtensor/include/utils/sycl_alloc_utils.hpp +++ b/dpnp/libtensor/include/utils/sycl_alloc_utils.hpp @@ -38,12 +38,15 @@ #include #include #include +#include #include #include #include #include +#include "dpctl4pybind11.hpp" + namespace dpnp::tensor::alloc_utils { template @@ -181,7 +184,50 @@ struct all_valid_smart_ptrs }; } // end of namespace detail -/*! @brief Submit host_task and transfer ownership from smart pointers to it */ +/*! + * @brief Keep `objs` alive until the work covered by `depends` completes. + * + * `objs` are moved into a release deferred with `dpctl::utils::defer_release`, + * which destroys them with the GIL held once that work is done. Unlike a + * `host_task`, the release may free USM. Returns the event of the empty kernel + * the release waits for. + */ +template +sycl::event async_release(sycl::queue &exec_q, + const std::vector &depends, + Ts &&...objs) +{ + static_assert(sizeof...(Ts) > 0, + "async_release requires at least one object to release"); + + using held_t = std::tuple...>; + auto held = std::make_unique(std::forward(objs)...); + + sycl::event marker_ev; + try { + marker_ev = ::dpctl::utils::defer_release( + exec_q, depends, [](void *p) { delete static_cast(p); }, + held.get()); + } catch (...) { + // what is held is destroyed on unwinding, so the work using it must be + // done first + sycl::event::wait(depends); + throw; + } + + // the deferred release owns what is held now + held.release(); + + return marker_ev; +} + +/*! + * @brief Free the USM allocations owned by `unique_pointers` once the work + * covered by `depends` completes, taking ownership from them. + * + * Returns the event of the empty kernel the free waits for, see + * `async_release`. + */ template sycl::event async_smart_free(sycl::queue &exec_q, const std::vector &depends, @@ -195,29 +241,6 @@ sycl::event async_smart_free(sycl::queue &exec_q, detail::all_valid_smart_ptrs::value, "async_smart_free requires unique_ptr created with smart_malloc"); - std::vector ptrs; - ptrs.reserve(n); - (ptrs.push_back(reinterpret_cast(unique_pointers.get())), ...); - - std::vector dels; - dels.reserve(n); - (dels.emplace_back(unique_pointers.get_deleter()), ...); - - sycl::event ht_e = exec_q.submit([&](sycl::handler &cgh) { - cgh.depends_on(depends); - - cgh.host_task([ptrs = std::move(ptrs), dels = std::move(dels)]() { - for (std::size_t i = 0; i < ptrs.size(); ++i) { - dels[i](ptrs[i]); - } - }); - }); - - // Upon successful submission of host_task, USM allocations are owned - // by the host_task. Release smart pointer ownership to avoid double - // deallocation - (unique_pointers.release(), ...); - - return ht_e; + return async_release(exec_q, depends, std::move(unique_pointers)...); } } // namespace dpnp::tensor::alloc_utils diff --git a/dpnp/libtensor/source/accumulators.cpp b/dpnp/libtensor/source/accumulators.cpp index e4341698ffac..4a3c69a92d4e 100644 --- a/dpnp/libtensor/source/accumulators.cpp +++ b/dpnp/libtensor/source/accumulators.cpp @@ -167,7 +167,7 @@ std::size_t py_mask_positions(const dpnp::tensor::usm_ndarray &mask, const bool use_i32 = (cumsum_typeid == int32_typeid); - std::vector host_task_events; + std::vector cleanup_events; if (mask.is_c_contiguous()) { auto fn = (use_i32) @@ -180,9 +180,9 @@ std::size_t py_mask_positions(const dpnp::tensor::usm_ndarray &mask, py::gil_scoped_release release; total_set = fn(exec_q, mask_size, mask_data, cumsum_data, - host_task_events, depends); + cleanup_events, depends); - sycl::event::wait(host_task_events); + sycl::event::wait(cleanup_events); } return total_set; } @@ -207,7 +207,7 @@ std::size_t py_mask_positions(const dpnp::tensor::usm_ndarray &mask, using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple = device_allocate_and_pack( - exec_q, host_task_events, compact_shape, compact_strides); + exec_q, cleanup_events, compact_shape, compact_strides); auto shape_strides_owner = std::move(std::get<0>(ptr_size_event_tuple)); sycl::event copy_shape_ev = std::get<2>(ptr_size_event_tuple); const py::ssize_t *shape_strides = shape_strides_owner.get(); @@ -217,7 +217,7 @@ std::size_t py_mask_positions(const dpnp::tensor::usm_ndarray &mask, py::gil_scoped_release release; copy_shape_ev.wait(); - sycl::event::wait(host_task_events); + sycl::event::wait(cleanup_events); // ensure deleter of smart pointer is invoked with GIL released shape_strides_owner.reset(nullptr); @@ -237,9 +237,9 @@ std::size_t py_mask_positions(const dpnp::tensor::usm_ndarray &mask, py::gil_scoped_release release; total_set = strided_fn(exec_q, mask_size, mask_data, nd, shape_strides, - cumsum_data, host_task_events, dependent_events); + cumsum_data, cleanup_events, dependent_events); - sycl::event::wait(host_task_events); + sycl::event::wait(cleanup_events); // ensure deleter of smart pointer is invoked with GIL released shape_strides_owner.reset(nullptr); } @@ -324,7 +324,7 @@ std::size_t py_cumsum_1d(const dpnp::tensor::usm_ndarray &src, "Cumulative sum array must have int64 data-type."); } - std::vector host_task_events; + std::vector cleanup_events; if (src.is_c_contiguous()) { auto fn = cumsum_1d_contig_dispatch_vector[src_typeid]; @@ -334,10 +334,10 @@ std::size_t py_cumsum_1d(const dpnp::tensor::usm_ndarray &src, std::to_string(src_typeid)); } std::size_t total = fn(exec_q, src_size, src_data, cumsum_data, - host_task_events, depends); + cleanup_events, depends); { py::gil_scoped_release release; - sycl::event::wait(host_task_events); + sycl::event::wait(cleanup_events); } return total; } @@ -365,7 +365,7 @@ std::size_t py_cumsum_1d(const dpnp::tensor::usm_ndarray &src, using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple = device_allocate_and_pack( - exec_q, host_task_events, compact_shape, compact_strides); + exec_q, cleanup_events, compact_shape, compact_strides); auto shape_strides_owner = std::move(std::get<0>(ptr_size_event_tuple)); sycl::event copy_shape_ev = std::get<2>(ptr_size_event_tuple); const py::ssize_t *shape_strides = shape_strides_owner.get(); @@ -375,7 +375,7 @@ std::size_t py_cumsum_1d(const dpnp::tensor::usm_ndarray &src, py::gil_scoped_release release; copy_shape_ev.wait(); - sycl::event::wait(host_task_events); + sycl::event::wait(cleanup_events); // ensure USM deleter is called with GIL released shape_strides_owner.reset(nullptr); @@ -391,11 +391,11 @@ std::size_t py_cumsum_1d(const dpnp::tensor::usm_ndarray &src, std::size_t total = strided_fn(exec_q, src_size, src_data, nd, shape_strides, cumsum_data, - host_task_events, dependent_events); + cleanup_events, dependent_events); { py::gil_scoped_release release; - sycl::event::wait(host_task_events); + sycl::event::wait(cleanup_events); // ensure USM deleter is called with GIL released shape_strides_owner.reset(nullptr); diff --git a/dpnp/libtensor/source/accumulators/accumulate_over_axis.hpp b/dpnp/libtensor/source/accumulators/accumulate_over_axis.hpp index d842d031eaa2..19ac63d7247a 100644 --- a/dpnp/libtensor/source/accumulators/accumulate_over_axis.hpp +++ b/dpnp/libtensor/source/accumulators/accumulate_over_axis.hpp @@ -143,7 +143,7 @@ std::pair bool is_src_c_contig = src.is_c_contiguous(); bool is_dst_c_contig = dst.is_c_contiguous(); - std::vector host_task_events; + std::vector cleanup_events; if ((is_src_c_contig && is_dst_c_contig) && iter_nd == 0) { auto fn = contig_dispatch_table[src_typeid][dst_typeid]; @@ -151,8 +151,8 @@ std::pair throw std::runtime_error("Datatypes are not supported"); } - sycl::event acc_ev = fn(exec_q, acc_nelems, src_data, dst_data, - host_task_events, depends); + sycl::event acc_ev = + fn(exec_q, acc_nelems, src_data, dst_data, cleanup_events, depends); return std::make_pair( dpnp::utils::keep_args_alive(exec_q, {src, dst}, {acc_ev}), acc_ev); @@ -210,7 +210,7 @@ std::pair using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple = device_allocate_and_pack( - exec_q, host_task_events, simplified_iter_shape, + exec_q, cleanup_events, simplified_iter_shape, simplified_iter_src_strides, simplified_iter_dst_strides, acc_shape, acc_src_strides, acc_dst_strides); auto packed_shapes_and_strides_owner = @@ -231,14 +231,14 @@ std::pair sycl::event acc_ev = strided_fn( exec_q, iter_nelems, acc_nelems, src_data, iter_nd, iter_shape_and_strides, iter_src_offset, iter_dst_offset, acc_nd, - acc_shapes_and_strides, dst_data, host_task_events, all_deps); + acc_shapes_and_strides, dst_data, cleanup_events, all_deps); sycl::event temp_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {acc_ev}, packed_shapes_and_strides_owner); - host_task_events.push_back(temp_cleanup_ev); + cleanup_events.push_back(temp_cleanup_ev); return std::make_pair( - dpnp::utils::keep_args_alive(exec_q, {src, dst}, host_task_events), + dpnp::utils::keep_args_alive(exec_q, {src, dst}, cleanup_events), acc_ev); } @@ -322,7 +322,7 @@ std::pair py_accumulate_final_axis_include_initial( bool is_src_c_contig = src.is_c_contiguous(); bool is_dst_c_contig = dst.is_c_contiguous(); - std::vector host_task_events; + std::vector cleanup_events; if ((is_src_c_contig && is_dst_c_contig) && iter_nd == 0) { auto fn = contig_dispatch_table[src_typeid][dst_typeid]; @@ -330,8 +330,8 @@ std::pair py_accumulate_final_axis_include_initial( throw std::runtime_error("Datatypes are not supported"); } - sycl::event acc_ev = fn(exec_q, acc_nelems, src_data, dst_data, - host_task_events, depends); + sycl::event acc_ev = + fn(exec_q, acc_nelems, src_data, dst_data, cleanup_events, depends); return std::make_pair( dpnp::utils::keep_args_alive(exec_q, {src, dst}, {acc_ev}), acc_ev); @@ -387,7 +387,7 @@ std::pair py_accumulate_final_axis_include_initial( using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple = device_allocate_and_pack( - exec_q, host_task_events, simplified_iter_shape, + exec_q, cleanup_events, simplified_iter_shape, simplified_iter_src_strides, simplified_iter_dst_strides, acc_shape, acc_src_strides, acc_dst_strides); auto packed_shapes_and_strides_owner = @@ -408,14 +408,14 @@ std::pair py_accumulate_final_axis_include_initial( sycl::event acc_ev = strided_fn( exec_q, iter_nelems, acc_nelems, src_data, iter_nd, iter_shape_and_strides, iter_src_offset, iter_dst_offset, acc_nd, - acc_shapes_and_strides, dst_data, host_task_events, all_deps); + acc_shapes_and_strides, dst_data, cleanup_events, all_deps); sycl::event temp_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {acc_ev}, packed_shapes_and_strides_owner); - host_task_events.push_back(temp_cleanup_ev); + cleanup_events.push_back(temp_cleanup_ev); return std::make_pair( - dpnp::utils::keep_args_alive(exec_q, {src, dst}, host_task_events), + dpnp::utils::keep_args_alive(exec_q, {src, dst}, cleanup_events), acc_ev); } diff --git a/dpnp/libtensor/source/boolean_advanced_indexing.cpp b/dpnp/libtensor/source/boolean_advanced_indexing.cpp index 9ddc7e25de35..71593348e507 100644 --- a/dpnp/libtensor/source/boolean_advanced_indexing.cpp +++ b/dpnp/libtensor/source/boolean_advanced_indexing.cpp @@ -267,7 +267,7 @@ std::pair auto dst_strides_vec = dst.get_strides_vector(); sycl::event extract_ev; - std::vector host_task_events{}; + std::vector cleanup_events{}; if (axis_start == 0 && axis_end == src_nd) { assert(dst_shape_vec.size() == 1); assert(dst_strides_vec.size() == 1); @@ -285,7 +285,7 @@ std::pair dst_shape_vec[0], dst_strides_vec[0], depends); // - host_task_events.push_back(extract_ev); + cleanup_events.push_back(extract_ev); } else { // empty orthogonal directions @@ -298,7 +298,7 @@ std::pair using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple1 = device_allocate_and_pack( - exec_q, host_task_events, src_shape_vec, src_strides_vec); + exec_q, cleanup_events, src_shape_vec, src_strides_vec); auto packed_src_shape_strides_owner = std::move(std::get<0>(ptr_size_event_tuple1)); sycl::event copy_src_shape_strides_ev = @@ -320,7 +320,7 @@ std::pair sycl::event cleanup_tmp_allocations_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {extract_ev}, packed_src_shape_strides_owner); - host_task_events.push_back(cleanup_tmp_allocations_ev); + cleanup_events.push_back(cleanup_tmp_allocations_ev); } } else { @@ -380,7 +380,7 @@ std::pair using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple1 = device_allocate_and_pack( - exec_q, host_task_events, simplified_ortho_shape, + exec_q, cleanup_events, simplified_ortho_shape, simplified_ortho_src_strides, simplified_ortho_dst_strides, masked_src_shape, masked_src_strides); auto packed_shapes_strides_owner = @@ -416,13 +416,13 @@ std::pair sycl::event cleanup_tmp_allocations_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {extract_ev}, packed_shapes_strides_owner); - host_task_events.push_back(cleanup_tmp_allocations_ev); + cleanup_events.push_back(cleanup_tmp_allocations_ev); } - sycl::event py_obj_management_host_task_ev = dpnp::utils::keep_args_alive( - exec_q, {src, cumsum, dst}, host_task_events); + sycl::event keep_args_alive_ev = dpnp::utils::keep_args_alive( + exec_q, {src, cumsum, dst}, cleanup_events); - return std::make_pair(py_obj_management_host_task_ev, extract_ev); + return std::make_pair(keep_args_alive_ev, extract_ev); } // Masked placement @@ -597,7 +597,7 @@ std::pair auto rhs_strides_vec = rhs.get_strides_vector(); sycl::event place_ev; - std::vector host_task_events{}; + std::vector cleanup_events{}; if (axis_start == 0 && axis_end == dst_nd) { // empty orthogonal directions auto fn = (use_i32) @@ -611,7 +611,7 @@ std::pair using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple1 = device_allocate_and_pack( - exec_q, host_task_events, dst_shape_vec, dst_strides_vec); + exec_q, cleanup_events, dst_shape_vec, dst_strides_vec); auto packed_dst_shape_strides_owner = std::move(std::get<0>(ptr_size_event_tuple1)); sycl::event copy_dst_shape_strides_ev = @@ -633,7 +633,7 @@ std::pair sycl::event cleanup_tmp_allocations_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {place_ev}, packed_dst_shape_strides_owner); - host_task_events.push_back(cleanup_tmp_allocations_ev); + cleanup_events.push_back(cleanup_tmp_allocations_ev); } else { // non-empty orthogonal directions @@ -691,7 +691,7 @@ std::pair using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple1 = device_allocate_and_pack( - exec_q, host_task_events, simplified_ortho_shape, + exec_q, cleanup_events, simplified_ortho_shape, simplified_ortho_dst_strides, simplified_ortho_rhs_strides, masked_dst_shape, masked_dst_strides); auto packed_shapes_strides_owner = @@ -725,13 +725,13 @@ std::pair sycl::event cleanup_tmp_allocations_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {place_ev}, packed_shapes_strides_owner); - host_task_events.push_back(cleanup_tmp_allocations_ev); + cleanup_events.push_back(cleanup_tmp_allocations_ev); } - sycl::event py_obj_management_host_task_ev = dpnp::utils::keep_args_alive( - exec_q, {dst, cumsum, rhs}, host_task_events); + sycl::event keep_args_alive_ev = dpnp::utils::keep_args_alive( + exec_q, {dst, cumsum, rhs}, cleanup_events); - return std::make_pair(py_obj_management_host_task_ev, place_ev); + return std::make_pair(keep_args_alive_ev, place_ev); } // Non-zero @@ -816,12 +816,12 @@ std::pair dpnp::tensor::validation::AmpleMemory::throw_if_not_ample(indexes, nz_elems * _ndim); - std::vector host_task_events; - host_task_events.reserve(2); + std::vector cleanup_events; + cleanup_events.reserve(2); using dpnp::tensor::offset_utils::device_allocate_and_pack; auto mask_shape_copying_tuple = device_allocate_and_pack( - exec_q, host_task_events, mask_shape); + exec_q, cleanup_events, mask_shape); auto src_shape_device_owner = std::move(std::get<0>(mask_shape_copying_tuple)); sycl::event copy_ev = std::get<2>(mask_shape_copying_tuple); @@ -852,12 +852,12 @@ std::pair sycl::event temporaries_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {non_zero_indexes_ev}, src_shape_device_owner); - host_task_events.push_back(temporaries_cleanup_ev); + cleanup_events.push_back(temporaries_cleanup_ev); - sycl::event py_obj_management_host_task_ev = dpnp::utils::keep_args_alive( - exec_q, {cumsum, indexes}, host_task_events); + sycl::event keep_args_alive_ev = + dpnp::utils::keep_args_alive(exec_q, {cumsum, indexes}, cleanup_events); - return std::make_pair(py_obj_management_host_task_ev, non_zero_indexes_ev); + return std::make_pair(keep_args_alive_ev, non_zero_indexes_ev); } } // namespace dpnp::tensor::py_internal diff --git a/dpnp/libtensor/source/choose.cpp b/dpnp/libtensor/source/choose.cpp index 015fd5140abc..db0db89e0c1e 100644 --- a/dpnp/libtensor/source/choose.cpp +++ b/dpnp/libtensor/source/choose.cpp @@ -230,28 +230,6 @@ std::vector batched_copy(sycl::queue &exec_q, return copy_evs; } -/* This function takes as input a queue, sycl::event dependencies, - and a non-trivial number of shared_ptrs and moves them into - a host_task lambda capture, ensuring their lifetime until the - host_task executes. -*/ -template -sycl::event async_shp_free(sycl::queue &exec_q, - const std::vector &depends, - Shps &&...shps) -{ - constexpr std::size_t n = sizeof...(Shps); - static_assert(n > 0, "async_shp_free requires at least one argument"); - - const sycl::event &shared_ptr_cleanup_ev = - exec_q.submit([&](sycl::handler &cgh) { - cgh.depends_on(depends); - cgh.host_task([capture = std::tuple(std::move(shps)...)]() {}); - }); - - return shared_ptr_cleanup_ev; -} - // was copied from dpctl // TODO: remove by consolidating with tensor post-migration std::vector parse_py_chcs(const sycl::queue &q, @@ -433,8 +411,8 @@ std::pair dpnp::tensor::alloc_utils::smart_malloc_device(n_chcs, exec_q); - std::vector host_task_events; - host_task_events.reserve(2); + std::vector cleanup_events; + cleanup_events.reserve(2); std::vector pack_deps; std::vector common_shape; @@ -464,9 +442,9 @@ std::pair std::make_pair(packed_chc_offsets.get(), host_chc_offsets), std::make_pair(packed_shapes_strides.get(), host_shape_strides)); - host_task_events.push_back( - detail::async_shp_free(exec_q, pack_deps, host_chc_ptrs, - host_chc_offsets, host_shape_strides)); + cleanup_events.push_back(dpnp::tensor::alloc_utils::async_release( + exec_q, pack_deps, std::move(host_chc_ptrs), + std::move(host_chc_offsets), std::move(host_shape_strides))); std::vector all_deps; all_deps.reserve(depends.size() + pack_deps.size()); @@ -485,11 +463,11 @@ std::pair exec_q, {choose_generic_ev}, packed_chc_ptrs, packed_shapes_strides, packed_chc_offsets); - host_task_events.push_back(temporaries_cleanup_ev); + cleanup_events.push_back(temporaries_cleanup_ev); using dpnp::utils::keep_args_alive; sycl::event arg_cleanup_ev = - keep_args_alive(exec_q, {src, py_chcs, dst}, host_task_events); + keep_args_alive(exec_q, {src, py_chcs, dst}, cleanup_events); return std::make_pair(arg_cleanup_ev, choose_generic_ev); } diff --git a/dpnp/libtensor/source/clip.cpp b/dpnp/libtensor/source/clip.cpp index 2dfd98c9a2cd..a0cb94bce605 100644 --- a/dpnp/libtensor/source/clip.cpp +++ b/dpnp/libtensor/source/clip.cpp @@ -192,10 +192,10 @@ std::pair sycl::event clip_ev = fn(exec_q, nelems, src_data, min_data, max_data, dst_data, depends); - sycl::event ht_ev = + sycl::event keep_args_alive_ev = keep_args_alive(exec_q, {src, min, max, dst}, {clip_ev}); - return std::make_pair(ht_ev, clip_ev); + return std::make_pair(keep_args_alive_ev, clip_ev); } auto const &src_strides = src.get_strides_vector(); @@ -223,12 +223,12 @@ std::pair auto fn = clip_strided_dispatch_vector[src_typeid]; - std::vector host_task_events; - host_task_events.reserve(2); + std::vector cleanup_events; + cleanup_events.reserve(2); using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple = device_allocate_and_pack( - exec_q, host_task_events, + exec_q, cleanup_events, // common shape and strides simplified_shape, simplified_src_strides, simplified_min_strides, simplified_max_strides, simplified_dst_strides); @@ -252,10 +252,10 @@ std::pair sycl::event temporaries_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free(exec_q, {clip_ev}, packed_shape_strides_owner); - host_task_events.push_back(temporaries_cleanup_ev); + cleanup_events.push_back(temporaries_cleanup_ev); sycl::event arg_cleanup_ev = - keep_args_alive(exec_q, {src, min, max, dst}, host_task_events); + keep_args_alive(exec_q, {src, min, max, dst}, cleanup_events); return std::make_pair(arg_cleanup_ev, clip_ev); } diff --git a/dpnp/libtensor/source/copy_and_cast_usm_to_usm.cpp b/dpnp/libtensor/source/copy_and_cast_usm_to_usm.cpp index d39db9aba2ca..63e893321204 100644 --- a/dpnp/libtensor/source/copy_and_cast_usm_to_usm.cpp +++ b/dpnp/libtensor/source/copy_and_cast_usm_to_usm.cpp @@ -245,12 +245,12 @@ std::pair copy_usm_ndarray_into_usm_ndarray( auto copy_and_cast_fn = copy_and_cast_generic_dispatch_table[dst_type_id][src_type_id]; - std::vector host_task_events; - host_task_events.reserve(2); + std::vector cleanup_events; + cleanup_events.reserve(2); using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple = device_allocate_and_pack( - exec_q, host_task_events, simplified_shape, simplified_src_strides, + exec_q, cleanup_events, simplified_shape, simplified_src_strides, simplified_dst_strides); auto shape_strides_owner = std::move(std::get<0>(ptr_size_event_tuple)); const sycl::event ©_shape_ev = std::get<2>(ptr_size_event_tuple); @@ -264,9 +264,9 @@ std::pair copy_usm_ndarray_into_usm_ndarray( const auto &temporaries_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {copy_and_cast_generic_ev}, shape_strides_owner); - host_task_events.push_back(temporaries_cleanup_ev); + cleanup_events.push_back(temporaries_cleanup_ev); - return std::make_pair(keep_args_alive(exec_q, {src, dst}, host_task_events), + return std::make_pair(keep_args_alive(exec_q, {src, dst}, cleanup_events), copy_and_cast_generic_ev); } diff --git a/dpnp/libtensor/source/copy_as_contig.cpp b/dpnp/libtensor/source/copy_as_contig.cpp index 43b3b9517559..11a20fee8cc6 100644 --- a/dpnp/libtensor/source/copy_as_contig.cpp +++ b/dpnp/libtensor/source/copy_as_contig.cpp @@ -236,10 +236,10 @@ std::pair "Unexpected result of simplifying iteration space, 1"); } - std::vector host_task_events{}; + std::vector cleanup_events{}; auto ptr_size_event_tuple = dpnp::tensor::offset_utils::device_allocate_and_pack( - exec_q, host_task_events, simplified_shape, simplified_src_strides); + exec_q, cleanup_events, simplified_shape, simplified_src_strides); auto shape_stride_owner = std::move(std::get<0>(ptr_size_event_tuple)); const sycl::event ©_shape_ev = std::get<2>(ptr_size_event_tuple); const py::ssize_t *shape_stride = shape_stride_owner.get(); @@ -259,9 +259,9 @@ std::pair const auto &temporaries_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free(exec_q, {ascontig_ev}, shape_stride_owner); - host_task_events.push_back(temporaries_cleanup_ev); + cleanup_events.push_back(temporaries_cleanup_ev); - return std::make_pair(keep_args_alive(exec_q, {src, dst}, host_task_events), + return std::make_pair(keep_args_alive(exec_q, {src, dst}, cleanup_events), ascontig_ev); } @@ -370,10 +370,10 @@ std::pair "Unexpected result of simplifying iteration space, 1"); } - std::vector host_task_events{}; + std::vector cleanup_events{}; auto ptr_size_event_tuple = dpnp::tensor::offset_utils::device_allocate_and_pack( - exec_q, host_task_events, simplified_shape, simplified_src_strides); + exec_q, cleanup_events, simplified_shape, simplified_src_strides); auto shape_stride_owner = std::move(std::get<0>(ptr_size_event_tuple)); const sycl::event ©_shape_ev = std::get<2>(ptr_size_event_tuple); const py::ssize_t *shape_stride = shape_stride_owner.get(); @@ -393,9 +393,9 @@ std::pair const auto &temporaries_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free(exec_q, {ascontig_ev}, shape_stride_owner); - host_task_events.push_back(temporaries_cleanup_ev); + cleanup_events.push_back(temporaries_cleanup_ev); - return std::make_pair(keep_args_alive(exec_q, {src, dst}, host_task_events), + return std::make_pair(keep_args_alive(exec_q, {src, dst}, cleanup_events), ascontig_ev); } @@ -556,12 +556,12 @@ std::pair auto impl_fn = as_c_contig_nd_batch_of_square_matrices_dispatch_vector[src_type_id]; - std::vector host_task_events; - host_task_events.reserve(2); + std::vector cleanup_events; + cleanup_events.reserve(2); using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple = device_allocate_and_pack( - exec_q, host_task_events, simplified_shape, simplified_src_strides); + exec_q, cleanup_events, simplified_shape, simplified_src_strides); auto packed_shape_strides_owner = std::move(std::get<0>(ptr_size_event_tuple)); const sycl::event ©_shape_ev = std::get<2>(ptr_size_event_tuple); @@ -582,9 +582,9 @@ std::pair sycl::event temporaries_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free(exec_q, {ascontig_ev}, packed_shape_strides_owner); - host_task_events.push_back(temporaries_cleanup_ev); + cleanup_events.push_back(temporaries_cleanup_ev); - return std::make_pair(keep_args_alive(exec_q, {src, dst}, host_task_events), + return std::make_pair(keep_args_alive(exec_q, {src, dst}, cleanup_events), ascontig_ev); } @@ -747,12 +747,12 @@ std::pair auto impl_fn = as_c_contig_nd_batch_of_square_matrices_dispatch_vector[src_type_id]; - std::vector host_task_events; - host_task_events.reserve(2); + std::vector cleanup_events; + cleanup_events.reserve(2); using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple = device_allocate_and_pack( - exec_q, host_task_events, simplified_shape, simplified_src_strides); + exec_q, cleanup_events, simplified_shape, simplified_src_strides); auto packed_shape_strides_owner = std::move(std::get<0>(ptr_size_event_tuple)); const sycl::event ©_shape_ev = std::get<2>(ptr_size_event_tuple); @@ -773,9 +773,9 @@ std::pair sycl::event temporaries_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free(exec_q, {ascontig_ev}, packed_shape_strides_owner); - host_task_events.push_back(temporaries_cleanup_ev); + cleanup_events.push_back(temporaries_cleanup_ev); - return std::make_pair(keep_args_alive(exec_q, {src, dst}, host_task_events), + return std::make_pair(keep_args_alive(exec_q, {src, dst}, cleanup_events), ascontig_ev); } diff --git a/dpnp/libtensor/source/copy_for_reshape.cpp b/dpnp/libtensor/source/copy_for_reshape.cpp index 4ccd91c848bc..0407fc763085 100644 --- a/dpnp/libtensor/source/copy_for_reshape.cpp +++ b/dpnp/libtensor/source/copy_for_reshape.cpp @@ -137,14 +137,13 @@ std::pair auto dst_shape = dst.get_shape_vector(); auto dst_strides = dst.get_strides_vector(); - std::vector host_task_events; - host_task_events.reserve(2); + std::vector cleanup_events; + cleanup_events.reserve(2); // shape_strides = [src_shape, src_strides, dst_shape, dst_strides] using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple = device_allocate_and_pack( - exec_q, host_task_events, src_shape, src_strides, dst_shape, - dst_strides); + exec_q, cleanup_events, src_shape, src_strides, dst_shape, dst_strides); auto copy_shape_ev = std::get<2>(ptr_size_event_tuple); auto shape_strides_owner = std::move(std::get<0>(ptr_size_event_tuple)); const py::ssize_t *shape_strides = shape_strides_owner.get(); @@ -165,9 +164,9 @@ std::pair dpnp::tensor::alloc_utils::async_smart_free( exec_q, {copy_for_reshape_event}, shape_strides_owner); - host_task_events.push_back(temporaries_cleanup_ev); + cleanup_events.push_back(temporaries_cleanup_ev); - return std::make_pair(keep_args_alive(exec_q, {src, dst}, host_task_events), + return std::make_pair(keep_args_alive(exec_q, {src, dst}, cleanup_events), copy_for_reshape_event); } diff --git a/dpnp/libtensor/source/copy_for_roll.cpp b/dpnp/libtensor/source/copy_for_roll.cpp index 3a397540e28d..df45aa580493 100644 --- a/dpnp/libtensor/source/copy_for_roll.cpp +++ b/dpnp/libtensor/source/copy_for_roll.cpp @@ -176,10 +176,10 @@ std::pair fn(exec_q, offset, src_nelems, src_data, zero_offset, dst_data, zero_offset, depends); - sycl::event ht_ev = + sycl::event keep_args_alive_ev = keep_args_alive(exec_q, {src, dst}, {copy_for_roll_ev}); - return std::make_pair(ht_ev, copy_for_roll_ev); + return std::make_pair(keep_args_alive_ev, copy_for_roll_ev); } } @@ -212,22 +212,22 @@ std::pair fn(exec_q, offset, src_nelems, src_data, src_offset, dst_data, dst_offset, depends); - sycl::event ht_ev = + sycl::event keep_args_alive_ev = keep_args_alive(exec_q, {src, dst}, {copy_for_roll_ev}); - return std::make_pair(ht_ev, copy_for_roll_ev); + return std::make_pair(keep_args_alive_ev, copy_for_roll_ev); } } auto fn = copy_for_roll_strided_dispatch_vector[type_id]; - std::vector host_task_events; - host_task_events.reserve(2); + std::vector cleanup_events; + cleanup_events.reserve(2); // shape_strides = [src_shape, src_strides, dst_strides] using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple = device_allocate_and_pack( - exec_q, host_task_events, simplified_shape, simplified_src_strides, + exec_q, cleanup_events, simplified_shape, simplified_src_strides, simplified_dst_strides); auto shape_strides_owner = std::move(std::get<0>(ptr_size_event_tuple)); sycl::event copy_shape_ev = std::get<2>(ptr_size_event_tuple); @@ -245,9 +245,9 @@ std::pair sycl::event temporaries_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {copy_for_roll_event}, shape_strides_owner); - host_task_events.push_back(temporaries_cleanup_ev); + cleanup_events.push_back(temporaries_cleanup_ev); - return std::make_pair(keep_args_alive(exec_q, {src, dst}, host_task_events), + return std::make_pair(keep_args_alive(exec_q, {src, dst}, cleanup_events), copy_for_roll_event); } @@ -345,13 +345,13 @@ std::pair auto fn = copy_for_roll_ndshift_dispatch_vector[type_id]; - std::vector host_task_events; - host_task_events.reserve(2); + std::vector cleanup_events; + cleanup_events.reserve(2); // shape_strides = [src_shape, src_strides, dst_strides] using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple = device_allocate_and_pack( - exec_q, host_task_events, common_shape, src_strides, dst_strides, + exec_q, cleanup_events, common_shape, src_strides, dst_strides, normalized_shifts); auto shape_strides_shifts_owner = std::move(std::get<0>(ptr_size_event_tuple)); @@ -369,9 +369,9 @@ std::pair auto temporaries_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {copy_for_roll_event}, shape_strides_shifts_owner); - host_task_events.push_back(temporaries_cleanup_ev); + cleanup_events.push_back(temporaries_cleanup_ev); - return std::make_pair(keep_args_alive(exec_q, {src, dst}, host_task_events), + return std::make_pair(keep_args_alive(exec_q, {src, dst}, cleanup_events), copy_for_roll_event); } diff --git a/dpnp/libtensor/source/copy_numpy_ndarray_into_usm_ndarray.cpp b/dpnp/libtensor/source/copy_numpy_ndarray_into_usm_ndarray.cpp index 2b89bc2098a6..7469e95ba5f0 100644 --- a/dpnp/libtensor/source/copy_numpy_ndarray_into_usm_ndarray.cpp +++ b/dpnp/libtensor/source/copy_numpy_ndarray_into_usm_ndarray.cpp @@ -156,7 +156,7 @@ void copy_numpy_ndarray_into_usm_ndarray( { // wait for copy_ev to complete - // release GIL to allow other threads (host_tasks) + // release GIL to allow other threads (such as dpctl's releases) // a chance to acquire GIL py::gil_scoped_release lock{}; copy_ev.wait(); @@ -270,7 +270,7 @@ void copy_numpy_ndarray_into_usm_ndarray( { // wait for copy_ev to complete - // release GIL to allow other threads (host_tasks) + // release GIL to allow other threads (such as dpctl's releases) // a chance to acquire GIL py::gil_scoped_release lock{}; @@ -309,13 +309,13 @@ void copy_numpy_ndarray_into_usm_ndarray( return; } - std::vector host_task_events; - host_task_events.reserve(1); + std::vector cleanup_events; + cleanup_events.reserve(1); // Copy shape strides into device memory using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple = device_allocate_and_pack( - exec_q, host_task_events, simplified_shape, simplified_src_strides, + exec_q, cleanup_events, simplified_shape, simplified_src_strides, simplified_dst_strides); auto shape_strides_owner = std::move(std::get<0>(ptr_size_event_tuple)); const sycl::event ©_shape_ev = std::get<2>(ptr_size_event_tuple); diff --git a/dpnp/libtensor/source/elementwise_functions/elementwise_functions.hpp b/dpnp/libtensor/source/elementwise_functions/elementwise_functions.hpp index 9b1f2c8fcede..5050bea1fa3f 100644 --- a/dpnp/libtensor/source/elementwise_functions/elementwise_functions.hpp +++ b/dpnp/libtensor/source/elementwise_functions/elementwise_functions.hpp @@ -164,10 +164,10 @@ std::pair } auto comp_ev = contig_fn(q, src_nelems, src_data, dst_data, depends); - sycl::event ht_ev = + sycl::event keep_args_alive_ev = dpnp::utils::keep_args_alive(q, {src, dst}, {comp_ev}); - return std::make_pair(ht_ev, comp_ev); + return std::make_pair(keep_args_alive_ev, comp_ev); } // simplify iteration space @@ -209,10 +209,10 @@ std::pair contig_fn(q, src_nelems, src_data + src_elem_size * src_offset, dst_data + dst_elem_size * dst_offset, depends); - sycl::event ht_ev = + sycl::event keep_args_alive_ev = dpnp::utils::keep_args_alive(q, {src, dst}, {comp_ev}); - return std::make_pair(ht_ev, comp_ev); + return std::make_pair(keep_args_alive_ev, comp_ev); } // Strided implementation @@ -226,11 +226,11 @@ std::pair using dpnp::tensor::offset_utils::device_allocate_and_pack; - std::vector host_tasks{}; - host_tasks.reserve(2); + std::vector cleanup_events{}; + cleanup_events.reserve(2); auto ptr_size_event_triple_ = device_allocate_and_pack( - q, host_tasks, simplified_shape, simplified_src_strides, + q, cleanup_events, simplified_shape, simplified_src_strides, simplified_dst_strides); auto shape_strides_owner = std::move(std::get<0>(ptr_size_event_triple_)); const auto ©_shape_ev = std::get<2>(ptr_size_event_triple_); @@ -244,10 +244,11 @@ std::pair sycl::event tmp_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( q, {strided_fn_ev}, shape_strides_owner); - host_tasks.push_back(tmp_cleanup_ev); + cleanup_events.push_back(tmp_cleanup_ev); return std::make_pair( - dpnp::utils::keep_args_alive(q, {src, dst}, host_tasks), strided_fn_ev); + dpnp::utils::keep_args_alive(q, {src, dst}, cleanup_events), + strided_fn_ev); } /*! @brief Template implementing Python API for querying of type support by @@ -399,10 +400,10 @@ std::pair auto comp_ev = contig_fn(q, src_nelems, src_data, dst1_data, dst2_data, depends); - sycl::event ht_ev = + sycl::event keep_args_alive_ev = dpnp::utils::keep_args_alive(q, {src, dst1, dst2}, {comp_ev}); - return std::make_pair(ht_ev, comp_ev); + return std::make_pair(keep_args_alive_ev, comp_ev); } // simplify iteration space @@ -450,10 +451,10 @@ std::pair dst1_data + dst1_elem_size * dst1_offset, dst2_data + dst2_elem_size * dst2_offset, depends); - sycl::event ht_ev = + sycl::event keep_args_alive_ev = dpnp::utils::keep_args_alive(q, {src, dst1, dst2}, {comp_ev}); - return std::make_pair(ht_ev, comp_ev); + return std::make_pair(keep_args_alive_ev, comp_ev); } // Strided implementation @@ -467,11 +468,11 @@ std::pair using dpnp::tensor::offset_utils::device_allocate_and_pack; - std::vector host_tasks{}; - host_tasks.reserve(2); + std::vector cleanup_events{}; + cleanup_events.reserve(2); auto ptr_size_event_triple_ = device_allocate_and_pack( - q, host_tasks, simplified_shape, simplified_src_strides, + q, cleanup_events, simplified_shape, simplified_src_strides, simplified_dst1_strides, simplified_dst2_strides); auto shape_strides_owner = std::move(std::get<0>(ptr_size_event_triple_)); const auto ©_shape_ev = std::get<2>(ptr_size_event_triple_); @@ -485,10 +486,10 @@ std::pair sycl::event tmp_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( q, {strided_fn_ev}, shape_strides_owner); - host_tasks.push_back(tmp_cleanup_ev); + cleanup_events.push_back(tmp_cleanup_ev); return std::make_pair( - dpnp::utils::keep_args_alive(q, {src, dst1, dst2}, host_tasks), + dpnp::utils::keep_args_alive(q, {src, dst1, dst2}, cleanup_events), strided_fn_ev); } @@ -657,10 +658,10 @@ std::pair py_binary_ufunc( if (contig_fn != nullptr) { auto comp_ev = contig_fn(exec_q, src_nelems, src1_data, 0, src2_data, 0, dst_data, 0, depends); - sycl::event ht_ev = dpnp::utils::keep_args_alive( + sycl::event keep_args_alive_ev = dpnp::utils::keep_args_alive( exec_q, {src1, src2, dst}, {comp_ev}); - return std::make_pair(ht_ev, comp_ev); + return std::make_pair(keep_args_alive_ev, comp_ev); } } @@ -687,7 +688,7 @@ std::pair py_binary_ufunc( simplified_shape, simplified_src1_strides, simplified_src2_strides, simplified_dst_strides, src1_offset, src2_offset, dst_offset); - std::vector host_tasks{}; + std::vector cleanup_events{}; if (nd < 3) { static constexpr auto unit_stride = std::initializer_list{1}; @@ -701,10 +702,10 @@ std::pair py_binary_ufunc( auto comp_ev = contig_fn(exec_q, src_nelems, src1_data, src1_offset, src2_data, src2_offset, dst_data, dst_offset, depends); - sycl::event ht_ev = dpnp::utils::keep_args_alive( + sycl::event keep_args_alive_ev = dpnp::utils::keep_args_alive( exec_q, {src1, src2, dst}, {comp_ev}); - return std::make_pair(ht_ev, comp_ev); + return std::make_pair(keep_args_alive_ev, comp_ev); } } if (nd == 2) { @@ -734,13 +735,13 @@ std::pair py_binary_ufunc( std::size_t n0 = simplified_shape[0]; std::size_t n1 = simplified_shape[1]; sycl::event comp_ev = matrix_row_broadcast_fn( - exec_q, host_tasks, n0, n1, src1_data, src1_offset, - src2_data, src2_offset, dst_data, dst_offset, - depends); + exec_q, cleanup_events, n0, n1, src1_data, + src1_offset, src2_data, src2_offset, dst_data, + dst_offset, depends); return std::make_pair( dpnp::utils::keep_args_alive( - exec_q, {src1, src2, dst}, host_tasks), + exec_q, {src1, src2, dst}, cleanup_events), comp_ev); } } @@ -766,13 +767,13 @@ std::pair py_binary_ufunc( std::size_t n0 = simplified_shape[1]; std::size_t n1 = simplified_shape[0]; sycl::event comp_ev = row_matrix_broadcast_fn( - exec_q, host_tasks, n0, n1, src1_data, src1_offset, - src2_data, src2_offset, dst_data, dst_offset, - depends); + exec_q, cleanup_events, n0, n1, src1_data, + src1_offset, src2_data, src2_offset, dst_data, + dst_offset, depends); return std::make_pair( dpnp::utils::keep_args_alive( - exec_q, {src1, src2, dst}, host_tasks), + exec_q, {src1, src2, dst}, cleanup_events), comp_ev); } } @@ -792,7 +793,7 @@ std::pair py_binary_ufunc( using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_sz_event_triple_ = device_allocate_and_pack( - exec_q, host_tasks, simplified_shape, simplified_src1_strides, + exec_q, cleanup_events, simplified_shape, simplified_src1_strides, simplified_src2_strides, simplified_dst_strides); auto shape_strides_owner = std::move(std::get<0>(ptr_sz_event_triple_)); auto ©_shape_ev = std::get<2>(ptr_sz_event_triple_); @@ -806,10 +807,10 @@ std::pair py_binary_ufunc( // async free of shape_strides temporary sycl::event tmp_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {strided_fn_ev}, shape_strides_owner); - host_tasks.push_back(tmp_cleanup_ev); + cleanup_events.push_back(tmp_cleanup_ev); return std::make_pair( - dpnp::utils::keep_args_alive(exec_q, {src1, src2, dst}, host_tasks), + dpnp::utils::keep_args_alive(exec_q, {src1, src2, dst}, cleanup_events), strided_fn_ev); } @@ -983,10 +984,10 @@ std::pair auto comp_ev = contig_fn(exec_q, src_nelems, src1_data, 0, src2_data, 0, dst1_data, 0, dst2_data, 0, depends); - sycl::event ht_ev = dpnp::utils::keep_args_alive( + sycl::event keep_args_alive_ev = dpnp::utils::keep_args_alive( exec_q, {src1, src2, dst1, dst2}, {comp_ev}); - return std::make_pair(ht_ev, comp_ev); + return std::make_pair(keep_args_alive_ev, comp_ev); } } @@ -1017,7 +1018,7 @@ std::pair simplified_dst1_strides, simplified_dst2_strides, src1_offset, src2_offset, dst1_offset, dst2_offset); - std::vector host_tasks{}; + std::vector cleanup_events{}; static constexpr auto unit_stride = std::initializer_list{1}; if ((nd == 1) && isEqual(simplified_src1_strides, unit_stride) && @@ -1031,10 +1032,10 @@ std::pair contig_fn(exec_q, src_nelems, src1_data, src1_offset, src2_data, src2_offset, dst1_data, dst1_offset, dst2_data, dst2_offset, depends); - sycl::event ht_ev = dpnp::utils::keep_args_alive( + sycl::event keep_args_alive_ev = dpnp::utils::keep_args_alive( exec_q, {src1, src2, dst1, dst2}, {comp_ev}); - return std::make_pair(ht_ev, comp_ev); + return std::make_pair(keep_args_alive_ev, comp_ev); } } @@ -1050,7 +1051,7 @@ std::pair using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_sz_event_triple_ = device_allocate_and_pack( - exec_q, host_tasks, simplified_shape, simplified_src1_strides, + exec_q, cleanup_events, simplified_shape, simplified_src1_strides, simplified_src2_strides, simplified_dst1_strides, simplified_dst2_strides); auto shape_strides_owner = std::move(std::get<0>(ptr_sz_event_triple_)); @@ -1066,10 +1067,10 @@ std::pair // async free of shape_strides temporary sycl::event tmp_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {strided_fn_ev}, shape_strides_owner); - host_tasks.push_back(tmp_cleanup_ev); + cleanup_events.push_back(tmp_cleanup_ev); return std::make_pair(dpnp::utils::keep_args_alive( - exec_q, {src1, src2, dst1, dst2}, host_tasks), + exec_q, {src1, src2, dst1, dst2}, cleanup_events), strided_fn_ev); } @@ -1221,10 +1222,10 @@ std::pair if (contig_fn != nullptr) { auto comp_ev = contig_fn(exec_q, rhs_nelems, rhs_data, 0, lhs_data, 0, depends); - sycl::event ht_ev = + sycl::event keep_args_alive_ev = dpnp::utils::keep_args_alive(exec_q, {rhs, lhs}, {comp_ev}); - return std::make_pair(ht_ev, comp_ev); + return std::make_pair(keep_args_alive_ev, comp_ev); } } @@ -1247,7 +1248,7 @@ std::pair simplified_shape, simplified_rhs_strides, simplified_lhs_strides, rhs_offset, lhs_offset); - std::vector host_tasks{}; + std::vector cleanup_events{}; if (nd < 3) { static constexpr auto unit_stride = std::initializer_list{1}; @@ -1260,10 +1261,10 @@ std::pair auto comp_ev = contig_fn(exec_q, rhs_nelems, rhs_data, rhs_offset, lhs_data, lhs_offset, depends); - sycl::event ht_ev = + sycl::event keep_args_alive_ev = dpnp::utils::keep_args_alive(exec_q, {rhs, lhs}, {comp_ev}); - return std::make_pair(ht_ev, comp_ev); + return std::make_pair(keep_args_alive_ev, comp_ev); } } if (nd == 2) { @@ -1280,12 +1281,13 @@ std::pair std::size_t n0 = simplified_shape[1]; std::size_t n1 = simplified_shape[0]; sycl::event comp_ev = row_matrix_broadcast_fn( - exec_q, host_tasks, n0, n1, rhs_data, rhs_offset, + exec_q, cleanup_events, n0, n1, rhs_data, rhs_offset, lhs_data, lhs_offset, depends); - return std::make_pair(dpnp::utils::keep_args_alive( - exec_q, {lhs, rhs}, host_tasks), - comp_ev); + return std::make_pair( + dpnp::utils::keep_args_alive(exec_q, {lhs, rhs}, + cleanup_events), + comp_ev); } } } @@ -1303,7 +1305,7 @@ std::pair using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_sz_event_triple_ = device_allocate_and_pack( - exec_q, host_tasks, simplified_shape, simplified_rhs_strides, + exec_q, cleanup_events, simplified_shape, simplified_rhs_strides, simplified_lhs_strides); auto shape_strides_owner = std::move(std::get<0>(ptr_sz_event_triple_)); auto copy_shape_ev = std::get<2>(ptr_sz_event_triple_); @@ -1318,10 +1320,10 @@ std::pair sycl::event tmp_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {strided_fn_ev}, shape_strides_owner); - host_tasks.push_back(tmp_cleanup_ev); + cleanup_events.push_back(tmp_cleanup_ev); return std::make_pair( - dpnp::utils::keep_args_alive(exec_q, {rhs, lhs}, host_tasks), + dpnp::utils::keep_args_alive(exec_q, {rhs, lhs}, cleanup_events), strided_fn_ev); } diff --git a/dpnp/libtensor/source/elementwise_functions/isclose.cpp b/dpnp/libtensor/source/elementwise_functions/isclose.cpp index 4f8d0e8d95dc..84176d39ad1a 100644 --- a/dpnp/libtensor/source/elementwise_functions/isclose.cpp +++ b/dpnp/libtensor/source/elementwise_functions/isclose.cpp @@ -237,10 +237,10 @@ std::pair auto comp_ev = contig_fn(exec_q, nelems, py_rtol, py_atol, py_equal_nan, a_data, b_data, res_data, depends); - sycl::event ht_ev = + sycl::event keep_args_alive_ev = dpnp::utils::keep_args_alive(exec_q, {a, b, res}, {comp_ev}); - return std::make_pair(ht_ev, comp_ev); + return std::make_pair(keep_args_alive_ev, comp_ev); } // simplify iteration space @@ -280,11 +280,11 @@ std::pair using dpnp::tensor::offset_utils::device_allocate_and_pack; - std::vector host_tasks{}; - host_tasks.reserve(2); + std::vector cleanup_events{}; + cleanup_events.reserve(2); auto ptr_size_event_triple_ = device_allocate_and_pack( - exec_q, host_tasks, simplified_shape, simplified_a_strides, + exec_q, cleanup_events, simplified_shape, simplified_a_strides, simplified_b_strides, simplified_res_strides); auto shape_strides_owner = std::move(std::get<0>(ptr_size_event_triple_)); const sycl::event ©_shape_ev = std::get<2>(ptr_size_event_triple_); @@ -303,10 +303,11 @@ std::pair sycl::event tmp_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {comp_ev}, shape_strides_owner); - host_tasks.push_back(tmp_cleanup_ev); + cleanup_events.push_back(tmp_cleanup_ev); return std::make_pair( - dpnp::utils::keep_args_alive(exec_q, {a, b, res}, host_tasks), comp_ev); + dpnp::utils::keep_args_alive(exec_q, {a, b, res}, cleanup_events), + comp_ev); } /** diff --git a/dpnp/libtensor/source/elementwise_functions/nan_to_num.cpp b/dpnp/libtensor/source/elementwise_functions/nan_to_num.cpp index bad533298cdc..1e5ea362ecdd 100644 --- a/dpnp/libtensor/source/elementwise_functions/nan_to_num.cpp +++ b/dpnp/libtensor/source/elementwise_functions/nan_to_num.cpp @@ -236,10 +236,10 @@ std::pair auto comp_ev = contig_fn(q, nelems, py_nan, py_posinf, py_neginf, src_data, dst_data, depends); - sycl::event ht_ev = + sycl::event keep_args_alive_ev = dpnp::utils::keep_args_alive(q, {src, dst}, {comp_ev}); - return std::make_pair(ht_ev, comp_ev); + return std::make_pair(keep_args_alive_ev, comp_ev); } // simplify iteration space @@ -283,10 +283,10 @@ std::pair src_data + src_elem_size * src_offset, dst_data + dst_elem_size * dst_offset, depends); - sycl::event ht_ev = + sycl::event keep_args_alive_ev = dpnp::utils::keep_args_alive(q, {src, dst}, {comp_ev}); - return std::make_pair(ht_ev, comp_ev); + return std::make_pair(keep_args_alive_ev, comp_ev); } auto fn = nan_to_num_dispatch_vector[src_typeid]; @@ -299,11 +299,11 @@ std::pair using dpnp::tensor::offset_utils::device_allocate_and_pack; - std::vector host_tasks{}; - host_tasks.reserve(2); + std::vector cleanup_events{}; + cleanup_events.reserve(2); auto ptr_size_event_triple_ = device_allocate_and_pack( - q, host_tasks, simplified_shape, simplified_src_strides, + q, cleanup_events, simplified_shape, simplified_src_strides, simplified_dst_strides); auto shape_strides_owner = std::move(std::get<0>(ptr_size_event_triple_)); const sycl::event ©_shape_ev = std::get<2>(ptr_size_event_triple_); @@ -322,10 +322,10 @@ std::pair sycl::event tmp_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( q, {comp_ev}, shape_strides_owner); - host_tasks.push_back(tmp_cleanup_ev); + cleanup_events.push_back(tmp_cleanup_ev); return std::make_pair( - dpnp::utils::keep_args_alive(q, {src, dst}, host_tasks), comp_ev); + dpnp::utils::keep_args_alive(q, {src, dst}, cleanup_events), comp_ev); } /** diff --git a/dpnp/libtensor/source/elementwise_functions/true_divide.cpp b/dpnp/libtensor/source/elementwise_functions/true_divide.cpp index df1682c91a77..62dd46742597 100644 --- a/dpnp/libtensor/source/elementwise_functions/true_divide.cpp +++ b/dpnp/libtensor/source/elementwise_functions/true_divide.cpp @@ -376,7 +376,7 @@ std::pair int nd = dst_nd; const py::ssize_t *shape = src_shape; - std::vector host_tasks{}; + std::vector cleanup_events{}; simplify_iteration_space(nd, shape, src_strides, dst_strides, // outputs simplified_shape, simplified_src_strides, @@ -394,7 +394,7 @@ std::pair using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_sz_event_triple_ = device_allocate_and_pack( - exec_q, host_tasks, simplified_shape, simplified_src_strides, + exec_q, cleanup_events, simplified_shape, simplified_src_strides, simplified_dst_strides); auto shape_strides_owner = std::move(std::get<0>(ptr_sz_event_triple_)); auto ©_metadata_ev = std::get<2>(ptr_sz_event_triple_); @@ -415,10 +415,11 @@ std::pair sycl::event tmp_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {div_ev}, shape_strides_owner); - host_tasks.push_back(tmp_cleanup_ev); + cleanup_events.push_back(tmp_cleanup_ev); return std::make_pair( - dpnp::utils::keep_args_alive(exec_q, {src, dst}, host_tasks), div_ev); + dpnp::utils::keep_args_alive(exec_q, {src, dst}, cleanup_events), + div_ev); } } // namespace impl diff --git a/dpnp/libtensor/source/full_ctor.cpp b/dpnp/libtensor/source/full_ctor.cpp index c2820af5d536..01667d75a350 100644 --- a/dpnp/libtensor/source/full_ctor.cpp +++ b/dpnp/libtensor/source/full_ctor.cpp @@ -269,11 +269,11 @@ std::pair auto fn = full_strided_dispatch_vector[dst_typeid]; - std::vector host_task_events; - host_task_events.reserve(2); + std::vector cleanup_events; + cleanup_events.reserve(2); using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple = device_allocate_and_pack( - exec_q, host_task_events, dst_shape, dst_strides); + exec_q, cleanup_events, dst_shape, dst_strides); auto shape_strides_owner = std::move(std::get<0>(ptr_size_event_tuple)); const sycl::event ©_shape_ev = std::get<2>(ptr_size_event_tuple); py::ssize_t *shape_strides = shape_strides_owner.get(); @@ -292,9 +292,9 @@ std::pair const auto &temporaries_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {full_strided_ev}, shape_strides_owner); - host_task_events.push_back(temporaries_cleanup_ev); + cleanup_events.push_back(temporaries_cleanup_ev); - return std::make_pair(keep_args_alive(exec_q, {dst}, host_task_events), + return std::make_pair(keep_args_alive(exec_q, {dst}, cleanup_events), full_strided_ev); } } diff --git a/dpnp/libtensor/source/integer_advanced_indexing.cpp b/dpnp/libtensor/source/integer_advanced_indexing.cpp index e38fd4d8662a..f453e9c4498d 100644 --- a/dpnp/libtensor/source/integer_advanced_indexing.cpp +++ b/dpnp/libtensor/source/integer_advanced_indexing.cpp @@ -338,7 +338,7 @@ std::size_t validate_and_compute_ind_nelems(int ind_nd, std::vector _populate_kernel_params(sycl::queue &exec_q, - std::vector &host_task_events, + std::vector &cleanup_events, char **device_ind_ptrs, py::ssize_t *device_ind_sh_st, py::ssize_t *device_ind_offsets, @@ -397,20 +397,15 @@ std::vector exec_q, host_along_sh_st_shp->data(), device_along_sh_st, host_along_sh_st_shp->size()); - const sycl::event shared_ptr_cleanup_ev = - exec_q.submit([&](sycl::handler &cgh) { - cgh.depends_on({device_along_sh_st_copy_ev, - device_orthog_sh_st_copy_ev, - device_ind_offsets_copy_ev, - device_ind_sh_st_copy_ev, device_ind_ptrs_copy_ev}); - cgh.host_task( - [host_ind_offsets_shp = std::move(host_ind_offsets_shp), - host_ind_sh_st_shp = std::move(host_ind_sh_st_shp), - host_ind_ptrs_shp = std::move(host_ind_ptrs_shp), - host_orthog_sh_st_shp = std::move(host_orthog_sh_st_shp), - host_along_sh_st_shp = std::move(host_along_sh_st_shp)] {}); - }); - host_task_events.push_back(shared_ptr_cleanup_ev); + // keep the host data alive until the copies complete + cleanup_events.push_back(dpnp::tensor::alloc_utils::async_release( + exec_q, + {device_along_sh_st_copy_ev, device_orthog_sh_st_copy_ev, + device_ind_offsets_copy_ev, device_ind_sh_st_copy_ev, + device_ind_ptrs_copy_ev}, + std::move(host_ind_offsets_shp), std::move(host_ind_sh_st_shp), + std::move(host_ind_ptrs_shp), std::move(host_orthog_sh_st_shp), + std::move(host_along_sh_st_shp))); return {device_ind_ptrs_copy_ev, device_ind_sh_st_copy_ev, device_ind_offsets_copy_ev, device_orthog_sh_st_copy_ev, @@ -571,11 +566,11 @@ std::pair auto src_strides = src.get_strides_vector(); auto dst_strides = dst.get_strides_vector(); - std::vector host_task_events; - host_task_events.reserve(2); + std::vector cleanup_events; + cleanup_events.reserve(2); std::vector pack_deps = _populate_kernel_params( - exec_q, host_task_events, packed_ind_ptrs_owner.get(), + exec_q, cleanup_events, packed_ind_ptrs_owner.get(), packed_ind_shapes_strides_owner.get(), packed_ind_offsets_owner.get(), packed_shapes_strides_owner.get(), packed_axes_shapes_strides_owner.get(), src_shape, dst_shape, @@ -592,7 +587,7 @@ std::pair : take_wrap_dispatch_table[src_type_id][ind_type_id]; if (fn == nullptr) { - sycl::event::wait(host_task_events); + sycl::event::wait(cleanup_events); throw std::runtime_error("Indices must be integer type, got " + std::to_string(ind_type_id)); } @@ -611,11 +606,11 @@ std::pair exec_q, {take_generic_ev}, packed_shapes_strides_owner, packed_axes_shapes_strides_owner, packed_ind_shapes_strides_owner, packed_ind_ptrs_owner, packed_ind_offsets_owner); - host_task_events.push_back(temporaries_cleanup_ev); + cleanup_events.push_back(temporaries_cleanup_ev); using dpnp::utils::keep_args_alive; sycl::event arg_cleanup_ev = - keep_args_alive(exec_q, {src, py_ind, dst}, host_task_events); + keep_args_alive(exec_q, {src, py_ind, dst}, cleanup_events); return std::make_pair(arg_cleanup_ev, take_generic_ev); } @@ -738,11 +733,11 @@ std::pair auto dst_strides = dst.get_strides_vector(); auto val_strides = val.get_strides_vector(); - std::vector host_task_events; - host_task_events.reserve(2); + std::vector cleanup_events; + cleanup_events.reserve(2); std::vector pack_deps = _populate_kernel_params( - exec_q, host_task_events, packed_ind_ptrs_owner.get(), + exec_q, cleanup_events, packed_ind_ptrs_owner.get(), packed_ind_shapes_strides_owner.get(), packed_ind_offsets_owner.get(), packed_shapes_strides_owner.get(), packed_axes_shapes_strides_owner.get(), dst_shape, val_shape, @@ -759,7 +754,7 @@ std::pair : put_wrap_dispatch_table[dst_type_id][ind_type_id]; if (fn == nullptr) { - sycl::event::wait(host_task_events); + sycl::event::wait(cleanup_events); throw std::runtime_error("Indices must be integer type, got " + std::to_string(ind_type_id)); } @@ -778,11 +773,11 @@ std::pair exec_q, {put_generic_ev}, packed_shapes_strides_owner, packed_axes_shapes_strides_owner, packed_ind_shapes_strides_owner, packed_ind_ptrs_owner, packed_ind_offsets_owner); - host_task_events.push_back(temporaries_cleanup_ev); + cleanup_events.push_back(temporaries_cleanup_ev); using dpnp::utils::keep_args_alive; sycl::event arg_cleanup_ev = - keep_args_alive(exec_q, {dst, py_ind, val}, host_task_events); + keep_args_alive(exec_q, {dst, py_ind, val}, cleanup_events); return std::make_pair(arg_cleanup_ev, put_generic_ev); } diff --git a/dpnp/libtensor/source/linalg_functions/dot.cpp b/dpnp/libtensor/source/linalg_functions/dot.cpp index 925ea9165c16..e56f12198219 100644 --- a/dpnp/libtensor/source/linalg_functions/dot.cpp +++ b/dpnp/libtensor/source/linalg_functions/dot.cpp @@ -327,7 +327,7 @@ std::pair (x2_outer_dims == 0 && x2_outer_nelems == 1)); bool call_batched = (batch_dims != 0 || batches > 1); - std::vector host_task_events{}; + std::vector cleanup_events{}; sycl::event dot_ev; if (call_vecdot) { if ((is_x1_c_contig && is_x2_c_contig && is_dst_c_contig) || @@ -470,7 +470,7 @@ std::pair using dpnp::tensor::offset_utils::device_allocate_and_pack; auto arrays_metainfo_packing_triple_ = device_allocate_and_pack( - exec_q, host_task_events, + exec_q, cleanup_events, // iteration metadata simplified_batch_shape, simplified_batch_x1_strides, simplified_batch_x2_strides, simplified_batch_dst_strides, @@ -503,7 +503,7 @@ std::pair sycl::event temp_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free(exec_q, {dot_ev}, tmp_alloc_owner); - host_task_events.push_back(temp_cleanup_ev); + cleanup_events.push_back(temp_cleanup_ev); } else { // if (!call_vecdot) if (!call_batched) { @@ -542,7 +542,7 @@ std::pair } using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple1 = device_allocate_and_pack( - exec_q, host_task_events, x1_shape_vec, x1_strides_vec, + exec_q, cleanup_events, x1_shape_vec, x1_strides_vec, x2_shape_vec, x2_strides_vec, dst_shape_vec, dst_strides_vec); auto packed_shapes_strides_owner = std::move(std::get<0>(ptr_size_event_tuple1)); @@ -572,7 +572,7 @@ std::pair sycl::event cleanup_tmp_allocations_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {dot_ev}, packed_shapes_strides_owner); - host_task_events.push_back(cleanup_tmp_allocations_ev); + cleanup_events.push_back(cleanup_tmp_allocations_ev); } else { // if (call_batched) using shT = std::vector; @@ -722,7 +722,7 @@ std::pair } using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple1 = device_allocate_and_pack( - exec_q, host_task_events, simplified_batch_shape, + exec_q, cleanup_events, simplified_batch_shape, simplified_batch_x1_strides, simplified_batch_x2_strides, simplified_batch_dst_strides, outer_inner_x1_shape, outer_inner_x1_strides, outer_inner_x2_shape, @@ -769,11 +769,11 @@ std::pair sycl::event cleanup_tmp_allocations_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {dot_ev}, packed_shapes_strides_owner); - host_task_events.push_back(cleanup_tmp_allocations_ev); + cleanup_events.push_back(cleanup_tmp_allocations_ev); } } return std::make_pair( - dpnp::utils::keep_args_alive(exec_q, {x1, x2, dst}, host_task_events), + dpnp::utils::keep_args_alive(exec_q, {x1, x2, dst}, cleanup_events), dot_ev); } diff --git a/dpnp/libtensor/source/putmask.cpp b/dpnp/libtensor/source/putmask.cpp index 92f65c9a6680..2101915bc0fd 100644 --- a/dpnp/libtensor/source/putmask.cpp +++ b/dpnp/libtensor/source/putmask.cpp @@ -197,10 +197,10 @@ std::pair auto comp_ev = contig_fn(exec_q, nelems, dst_p, mask_p, values_p, values_size, depends); - sycl::event ht_ev = dpnp::utils::keep_args_alive( + sycl::event keep_args_alive_ev = dpnp::utils::keep_args_alive( exec_q, {dst, mask, values}, {comp_ev}); - return std::make_pair(ht_ev, comp_ev); + return std::make_pair(keep_args_alive_ev, comp_ev); } // strided path: the iteration space is intentionally not simplified, so @@ -223,11 +223,11 @@ std::pair using dpnp::tensor::offset_utils::device_allocate_and_pack; - std::vector host_tasks; - host_tasks.reserve(2); + std::vector cleanup_events; + cleanup_events.reserve(2); auto pack = device_allocate_and_pack( - exec_q, host_tasks, common_shape, s_dst_strides, s_mask_strides); + exec_q, cleanup_events, common_shape, s_dst_strides, s_mask_strides); auto shape_strides_owner = std::move(std::get<0>(pack)); const py::ssize_t *shape_strides_dev = shape_strides_owner.get(); @@ -242,12 +242,12 @@ std::pair sycl::event cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {comp_ev}, shape_strides_owner); - host_tasks.push_back(cleanup_ev); + cleanup_events.push_back(cleanup_ev); - sycl::event ht_ev = - dpnp::utils::keep_args_alive(exec_q, {dst, mask, values}, host_tasks); + sycl::event keep_args_alive_ev = dpnp::utils::keep_args_alive( + exec_q, {dst, mask, values}, cleanup_events); - return std::make_pair(ht_ev, comp_ev); + return std::make_pair(keep_args_alive_ev, comp_ev); } template diff --git a/dpnp/libtensor/source/reductions/minmax.cpp b/dpnp/libtensor/source/reductions/minmax.cpp index 870fb783f133..4350b30886c1 100644 --- a/dpnp/libtensor/source/reductions/minmax.cpp +++ b/dpnp/libtensor/source/reductions/minmax.cpp @@ -410,11 +410,11 @@ std::pair throw std::runtime_error("Datatypes are not supported"); } - std::vector host_task_events{}; + std::vector cleanup_events{}; using dpnp::tensor::offset_utils::device_allocate_and_pack; auto arrays_metainfo_packing_triple_ = device_allocate_and_pack( - exec_q, host_task_events, + exec_q, cleanup_events, // iteration metadata simplified_iteration_shape, simplified_iteration_src_strides, simplified_iteration_dst_min_strides, @@ -444,10 +444,10 @@ std::pair sycl::event temp_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {minmax_ev}, tmp_owner); - host_task_events.push_back(temp_cleanup_ev); + cleanup_events.push_back(temp_cleanup_ev); sycl::event keep_args_event = dpnp::utils::keep_args_alive( - exec_q, {src, dst_min, dst_max}, host_task_events); + exec_q, {src, dst_min, dst_max}, cleanup_events); return std::make_pair(keep_args_event, minmax_ev); } diff --git a/dpnp/libtensor/source/reductions/reduction_over_axis.hpp b/dpnp/libtensor/source/reductions/reduction_over_axis.hpp index cbbbf5018d89..e990928188d9 100644 --- a/dpnp/libtensor/source/reductions/reduction_over_axis.hpp +++ b/dpnp/libtensor/source/reductions/reduction_over_axis.hpp @@ -458,11 +458,11 @@ std::pair py_reduction_over_axis( } } - std::vector host_task_events{}; + std::vector cleanup_events{}; using dpnp::tensor::offset_utils::device_allocate_and_pack; auto arrays_metainfo_packing_triple_ = device_allocate_and_pack( - exec_q, host_task_events, + exec_q, cleanup_events, // iteration metadata simplified_iteration_shape, simplified_iteration_src_strides, simplified_iteration_dst_strides, @@ -492,10 +492,10 @@ std::pair py_reduction_over_axis( sycl::event temp_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {reduction_ev}, tmp_alloc_owner); - host_task_events.push_back(temp_cleanup_ev); + cleanup_events.push_back(temp_cleanup_ev); sycl::event keep_args_event = - dpnp::utils::keep_args_alive(exec_q, {src, dst}, host_task_events); + dpnp::utils::keep_args_alive(exec_q, {src, dst}, cleanup_events); return std::make_pair(keep_args_event, reduction_ev); } @@ -735,11 +735,11 @@ std::pair py_tree_reduction_over_axis( throw std::runtime_error("Datatypes are not supported"); } - std::vector host_task_events{}; + std::vector cleanup_events{}; using dpnp::tensor::offset_utils::device_allocate_and_pack; auto arrays_metainfo_packing_triple_ = device_allocate_and_pack( - exec_q, host_task_events, + exec_q, cleanup_events, // iteration metadata simplified_iteration_shape, simplified_iteration_src_strides, simplified_iteration_dst_strides, @@ -768,10 +768,10 @@ std::pair py_tree_reduction_over_axis( sycl::event temp_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {reduction_ev}, tmp_owner); - host_task_events.push_back(temp_cleanup_ev); + cleanup_events.push_back(temp_cleanup_ev); sycl::event keep_args_event = - dpnp::utils::keep_args_alive(exec_q, {src, dst}, host_task_events); + dpnp::utils::keep_args_alive(exec_q, {src, dst}, cleanup_events); return std::make_pair(keep_args_event, reduction_ev); } @@ -1004,13 +1004,13 @@ std::pair py_search_over_axis( throw std::runtime_error("Datatypes are not supported"); } - std::vector host_task_events{}; + std::vector cleanup_events{}; using dpnp::tensor::offset_utils::device_allocate_and_pack; auto arrays_metainfo_packing_triple_ = device_allocate_and_pack( - exec_q, host_task_events, + exec_q, cleanup_events, // iteration metadata simplified_iteration_shape, simplified_iteration_src_strides, simplified_iteration_dst_strides, @@ -1038,10 +1038,10 @@ std::pair py_search_over_axis( sycl::event temp_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {comp_ev}, tmp_owner); - host_task_events.push_back(temp_cleanup_ev); + cleanup_events.push_back(temp_cleanup_ev); sycl::event keep_args_event = - dpnp::utils::keep_args_alive(exec_q, {src, dst}, host_task_events); + dpnp::utils::keep_args_alive(exec_q, {src, dst}, cleanup_events); return std::make_pair(keep_args_event, comp_ev); } @@ -1274,10 +1274,10 @@ std::pair auto fn = strided_dispatch_vector[src_typeid]; - std::vector host_task_events{}; + std::vector cleanup_events{}; auto iter_red_metadata_packing_triple_ = dpnp::tensor::offset_utils::device_allocate_and_pack( - exec_q, host_task_events, simplified_iter_shape, + exec_q, cleanup_events, simplified_iter_shape, simplified_iter_src_strides, simplified_iter_dst_strides, simplified_red_shape, simplified_red_src_strides); auto packed_shapes_strides_owner = @@ -1304,10 +1304,10 @@ std::pair sycl::event temp_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {red_ev}, packed_shapes_strides_owner); - host_task_events.push_back(temp_cleanup_ev); + cleanup_events.push_back(temp_cleanup_ev); sycl::event keep_args_event = - dpnp::utils::keep_args_alive(exec_q, {src, dst}, host_task_events); + dpnp::utils::keep_args_alive(exec_q, {src, dst}, cleanup_events); return std::make_pair(keep_args_event, red_ev); } diff --git a/dpnp/libtensor/source/repeat.cpp b/dpnp/libtensor/source/repeat.cpp index afd664d4fb42..a79b4b40645d 100644 --- a/dpnp/libtensor/source/repeat.cpp +++ b/dpnp/libtensor/source/repeat.cpp @@ -230,7 +230,7 @@ std::pair auto reps_strides_vec = reps.get_strides_vector(); sycl::event repeat_ev; - std::vector host_task_events{}; + std::vector cleanup_events{}; if (axis == 0 && src_nd < 2) { // empty orthogonal directions @@ -246,7 +246,7 @@ std::pair using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple1 = device_allocate_and_pack( - exec_q, host_task_events, src_shape_vec, src_strides_vec); + exec_q, cleanup_events, src_shape_vec, src_strides_vec); auto packed_src_shape_strides_owner = std::move(std::get<0>(ptr_size_event_tuple1)); sycl::event copy_shapes_strides_ev = std::get<2>(ptr_size_event_tuple1); @@ -269,7 +269,7 @@ std::pair sycl::event cleanup_tmp_allocations_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {repeat_ev}, packed_src_shape_strides_owner); - host_task_events.push_back(cleanup_tmp_allocations_ev); + cleanup_events.push_back(cleanup_tmp_allocations_ev); } else { // non-empty orthogonal directions @@ -317,7 +317,7 @@ std::pair using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple1 = device_allocate_and_pack( - exec_q, host_task_events, simplified_orthog_shape, + exec_q, cleanup_events, simplified_orthog_shape, simplified_orthog_src_strides, simplified_orthog_dst_strides); auto packed_shapes_strides_owner = std::move(std::get<0>(ptr_size_event_tuple1)); @@ -347,13 +347,13 @@ std::pair sycl::event cleanup_tmp_allocations_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {repeat_ev}, packed_shapes_strides_owner); - host_task_events.push_back(cleanup_tmp_allocations_ev); + cleanup_events.push_back(cleanup_tmp_allocations_ev); } - sycl::event py_obj_management_host_task_ev = dpnp::utils::keep_args_alive( - exec_q, {src, reps, cumsum, dst}, host_task_events); + sycl::event keep_args_alive_ev = dpnp::utils::keep_args_alive( + exec_q, {src, reps, cumsum, dst}, cleanup_events); - return std::make_pair(py_obj_management_host_task_ev, repeat_ev); + return std::make_pair(keep_args_alive_ev, repeat_ev); } std::pair @@ -461,13 +461,13 @@ std::pair auto reps_shape_vec = reps.get_shape_vector(); auto reps_strides_vec = reps.get_strides_vector(); - std::vector host_task_events{}; + std::vector cleanup_events{}; auto fn = repeat_by_sequence_1d_dispatch_vector[src_typeid]; using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple1 = device_allocate_and_pack( - exec_q, host_task_events, src_shape_vec, src_strides_vec); + exec_q, cleanup_events, src_shape_vec, src_strides_vec); auto packed_src_shapes_strides_owner = std::move(std::get<0>(ptr_size_event_tuple1)); sycl::event copy_shapes_strides_ev = std::get<2>(ptr_size_event_tuple1); @@ -489,12 +489,12 @@ std::pair sycl::event cleanup_tmp_allocations_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {repeat_ev}, packed_src_shapes_strides_owner); - host_task_events.push_back(cleanup_tmp_allocations_ev); + cleanup_events.push_back(cleanup_tmp_allocations_ev); - sycl::event py_obj_management_host_task_ev = dpnp::utils::keep_args_alive( - exec_q, {src, reps, cumsum, dst}, host_task_events); + sycl::event keep_args_alive_ev = dpnp::utils::keep_args_alive( + exec_q, {src, reps, cumsum, dst}, cleanup_events); - return std::make_pair(py_obj_management_host_task_ev, repeat_ev); + return std::make_pair(keep_args_alive_ev, repeat_ev); } std::pair @@ -586,7 +586,7 @@ std::pair auto dst_strides_vec = dst.get_strides_vector(); sycl::event repeat_ev; - std::vector host_task_events{}; + std::vector cleanup_events{}; if (axis == 0 && src_nd < 2) { // empty orthogonal directions @@ -602,7 +602,7 @@ std::pair using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple1 = device_allocate_and_pack( - exec_q, host_task_events, src_shape_vec, src_strides_vec); + exec_q, cleanup_events, src_shape_vec, src_strides_vec); auto packed_src_shape_strides_owner = std::move(std::get<0>(ptr_size_event_tuple1)); sycl::event copy_shapes_strides_ev = std::get<2>(ptr_size_event_tuple1); @@ -624,7 +624,7 @@ std::pair dpnp::tensor::alloc_utils::async_smart_free( exec_q, {repeat_ev}, packed_src_shape_strides_owner); - host_task_events.push_back(cleanup_tmp_allocations_ev); + cleanup_events.push_back(cleanup_tmp_allocations_ev); } else { // non-empty orthogonal directions @@ -673,7 +673,7 @@ std::pair using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple1 = device_allocate_and_pack( - exec_q, host_task_events, simplified_orthog_shape, + exec_q, cleanup_events, simplified_orthog_shape, simplified_orthog_src_strides, simplified_orthog_dst_strides); auto packed_shapes_strides_owner = std::move(std::get<0>(ptr_size_event_tuple1)); @@ -701,13 +701,13 @@ std::pair sycl::event cleanup_tmp_allocations_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {repeat_ev}, packed_shapes_strides_owner); - host_task_events.push_back(cleanup_tmp_allocations_ev); + cleanup_events.push_back(cleanup_tmp_allocations_ev); } - sycl::event py_obj_management_host_task_ev = - dpnp::utils::keep_args_alive(exec_q, {src, dst}, host_task_events); + sycl::event keep_args_alive_ev = + dpnp::utils::keep_args_alive(exec_q, {src, dst}, cleanup_events); - return std::make_pair(py_obj_management_host_task_ev, repeat_ev); + return std::make_pair(keep_args_alive_ev, repeat_ev); } std::pair @@ -779,13 +779,13 @@ std::pair auto dst_shape_vec = dst.get_shape_vector(); auto dst_strides_vec = dst.get_strides_vector(); - std::vector host_task_events{}; + std::vector cleanup_events{}; auto fn = repeat_by_scalar_1d_dispatch_vector[src_typeid]; using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple1 = device_allocate_and_pack( - exec_q, host_task_events, src_shape_vec, src_strides_vec); + exec_q, cleanup_events, src_shape_vec, src_strides_vec); auto packed_src_shape_strides_owner = std::move(std::get<0>(ptr_size_event_tuple1)); sycl::event copy_shapes_strides_ev = std::get<2>(ptr_size_event_tuple1); @@ -806,12 +806,12 @@ std::pair sycl::event cleanup_tmp_allocations_ev = dpnp::tensor::alloc_utils::async_smart_free( exec_q, {repeat_ev}, packed_src_shape_strides_owner); - host_task_events.push_back(cleanup_tmp_allocations_ev); + cleanup_events.push_back(cleanup_tmp_allocations_ev); - sycl::event py_obj_management_host_task_ev = - dpnp::utils::keep_args_alive(exec_q, {src, dst}, host_task_events); + sycl::event keep_args_alive_ev = + dpnp::utils::keep_args_alive(exec_q, {src, dst}, cleanup_events); - return std::make_pair(py_obj_management_host_task_ev, repeat_ev); + return std::make_pair(keep_args_alive_ev, repeat_ev); } } // namespace dpnp::tensor::py_internal diff --git a/dpnp/libtensor/source/sorting/isin.cpp b/dpnp/libtensor/source/sorting/isin.cpp index 4855441833b3..17bda4a856d6 100644 --- a/dpnp/libtensor/source/sorting/isin.cpp +++ b/dpnp/libtensor/source/sorting/isin.cpp @@ -265,13 +265,13 @@ std::pair simplified_common_shape, simplified_needles_strides, simplified_dst_strides, needles_offset, dst_offset); } - std::vector host_task_events; - host_task_events.reserve(2); + std::vector cleanup_events; + cleanup_events.reserve(2); using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple = device_allocate_and_pack( - exec_q, host_task_events, + exec_q, cleanup_events, // vectors being packed simplified_common_shape, simplified_needles_strides, simplified_dst_strides); @@ -306,11 +306,11 @@ std::pair dpnp::tensor::alloc_utils::async_smart_free(exec_q, {comp_ev}, packed_shape_strides_owner); - host_task_events.push_back(temporaries_cleanup_ev); - const sycl::event &ht_ev = dpnp::utils::keep_args_alive( - exec_q, {hay, needles, dst}, host_task_events); + cleanup_events.push_back(temporaries_cleanup_ev); + const sycl::event &keep_args_alive_ev = dpnp::utils::keep_args_alive( + exec_q, {hay, needles, dst}, cleanup_events); - return std::make_pair(ht_ev, comp_ev); + return std::make_pair(keep_args_alive_ev, comp_ev); } void init_isin_functions(py::module_ m) diff --git a/dpnp/libtensor/source/sorting/searchsorted.cpp b/dpnp/libtensor/source/sorting/searchsorted.cpp index 78d694c7ac95..faa028f2d8d0 100644 --- a/dpnp/libtensor/source/sorting/searchsorted.cpp +++ b/dpnp/libtensor/source/sorting/searchsorted.cpp @@ -376,13 +376,13 @@ std::pair simplified_common_shape, simplified_needles_strides, simplified_positions_strides, needles_offset, positions_offset); } - std::vector host_task_events; - host_task_events.reserve(2); + std::vector cleanup_events; + cleanup_events.reserve(2); using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple = device_allocate_and_pack( - exec_q, host_task_events, + exec_q, cleanup_events, // vectors being packed simplified_common_shape, simplified_needles_strides, simplified_positions_strides); @@ -422,11 +422,11 @@ std::pair dpnp::tensor::alloc_utils::async_smart_free(exec_q, {comp_ev}, packed_shape_strides_owner); - host_task_events.push_back(temporaries_cleanup_ev); - const sycl::event &ht_ev = dpnp::utils::keep_args_alive( - exec_q, {hay, needles, positions}, host_task_events); + cleanup_events.push_back(temporaries_cleanup_ev); + const sycl::event &keep_args_alive_ev = dpnp::utils::keep_args_alive( + exec_q, {hay, needles, positions}, cleanup_events); - return std::make_pair(ht_ev, comp_ev); + return std::make_pair(keep_args_alive_ev, comp_ev); } /*! @brief search for needle from needles in sorted hay, diff --git a/dpnp/libtensor/source/tensor_ctors.cpp b/dpnp/libtensor/source/tensor_ctors.cpp index 06edb6a8efe3..97c43f5c9c30 100644 --- a/dpnp/libtensor/source/tensor_ctors.cpp +++ b/dpnp/libtensor/source/tensor_ctors.cpp @@ -199,21 +199,21 @@ PYBIND11_MODULE(_tensor_impl, m, py::mod_gil_not_used()) ©_usm_ndarray_into_usm_ndarray, "Copies from usm_ndarray `src` into usm_ndarray `dst` of the same " "shape. " - "Returns a tuple of events: (host_task_event, compute_task_event)", + "Returns a tuple of events: (cleanup_event, compute_task_event)", py::arg("src"), py::arg("dst"), py::arg("sycl_queue"), py::arg("depends") = py::list()); m.def("_as_c_contig", &py_as_c_contig, "Copies from usm_ndarray `src` into C-contiguous usm_ndarray " "`dst` of the same shape and the same data type. " - "Returns a tuple of events: (host_task_event, compute_task_event)", + "Returns a tuple of events: (cleanup_event, compute_task_event)", py::arg("src"), py::arg("dst"), py::arg("sycl_queue"), py::arg("depends") = py::list()); m.def("_as_f_contig", &py_as_f_contig, "Copies from usm_ndarray `src` into F-contiguous usm_ndarray " "`dst` of the same shape and the same data type. " - "Returns a tuple of events: (host_task_event, compute_task_event)", + "Returns a tuple of events: (cleanup_event, compute_task_event)", py::arg("src"), py::arg("dst"), py::arg("sycl_queue"), py::arg("depends") = py::list()); @@ -284,7 +284,7 @@ PYBIND11_MODULE(_tensor_impl, m, py::mod_gil_not_used()) "Copies from usm_ndarray `src` into usm_ndarray `dst` with the same " "number of elements using underlying 'C'-contiguous order for flat " "traversal. " - "Returns a tuple of events: (ht_event, comp_event)", + "Returns a tuple of events: (cleanup_event, comp_event)", py::arg("src"), py::arg("dst"), py::arg("sycl_queue"), py::arg("depends") = py::list()); @@ -292,7 +292,7 @@ PYBIND11_MODULE(_tensor_impl, m, py::mod_gil_not_used()) "Copies from usm_ndarray `src` into usm_ndarray `dst` with the same " "shapes using underlying 'C'-contiguous order for flat " "traversal with shift. " - "Returns a tuple of events: (ht_event, comp_event)", + "Returns a tuple of events: (cleanup_event, comp_event)", py::arg("src"), py::arg("dst"), py::arg("shift"), py::arg("sycl_queue"), py::arg("depends") = py::list()); @@ -300,7 +300,7 @@ PYBIND11_MODULE(_tensor_impl, m, py::mod_gil_not_used()) "Copies from usm_ndarray `src` into usm_ndarray `dst` with the same " "shapes using underlying 'C'-contiguous order for " "traversal with shifts along each axis. " - "Returns a tuple of events: (ht_event, comp_event)", + "Returns a tuple of events: (cleanup_event, comp_event)", py::arg("src"), py::arg("dst"), py::arg("shifts"), py::arg("sycl_queue"), py::arg("depends") = py::list()); @@ -308,7 +308,7 @@ PYBIND11_MODULE(_tensor_impl, m, py::mod_gil_not_used()) "Fills input 1D contiguous usm_ndarray `dst` with linear sequence " "specified by " "starting point `start` and step `dt`. " - "Returns a tuple of events: (ht_event, comp_event)", + "Returns a tuple of events: (cleanup_event, comp_event)", py::arg("start"), py::arg("dt"), py::arg("dst"), py::arg("sycl_queue"), py::arg("depends") = py::list()); @@ -316,7 +316,7 @@ PYBIND11_MODULE(_tensor_impl, m, py::mod_gil_not_used()) "Fills input 1D contiguous usm_ndarray `dst` with linear sequence " "specified by " "starting point `start` and end point `end`. " - "Returns a tuple of events: (ht_event, comp_event)", + "Returns a tuple of events: (cleanup_event, comp_event)", py::arg("start"), py::arg("end"), py::arg("dst"), py::arg("include_endpoint"), py::arg("sycl_queue"), py::arg("depends") = py::list()); @@ -360,7 +360,7 @@ PYBIND11_MODULE(_tensor_impl, m, py::mod_gil_not_used()) "specified by " "the diagonal index `k` " "which is filled with ones." - "Returns a tuple of events: (ht_event, comp_event)", + "Returns a tuple of events: (cleanup_event, comp_event)", py::arg("k"), py::arg("dst"), py::arg("sycl_queue"), py::arg("depends") = py::list()); diff --git a/dpnp/libtensor/source/triul_ctor.cpp b/dpnp/libtensor/source/triul_ctor.cpp index 83c232be4fd8..fd8fa2d91e09 100644 --- a/dpnp/libtensor/source/triul_ctor.cpp +++ b/dpnp/libtensor/source/triul_ctor.cpp @@ -209,21 +209,10 @@ std::pair dev_shape_and_strides, k, depends, {copy_shape_and_strides}); } - const auto &temporaries_cleanup_ev = exec_q.submit([&](sycl::handler &cgh) { - cgh.depends_on(tri_ev); - const auto &ctx = exec_q.get_context(); - using dpnp::tensor::alloc_utils::sycl_free_noexcept; - cgh.host_task( - [shp_host_shape_and_strides = std::move(shp_host_shape_and_strides), - dev_shape_and_strides, ctx]() { - // capture of shp_host_shape_and_strides ensure the underlying - // vector exists for the entire execution of copying kernel - sycl_free_noexcept(dev_shape_and_strides, ctx); - }); - }); - // since host_task now owns USM allocation, release ownership by smart - // pointer - dev_shape_and_strides_owner.release(); + const sycl::event temporaries_cleanup_ev = + dpnp::tensor::alloc_utils::async_release( + exec_q, {tri_ev}, std::move(shp_host_shape_and_strides), + std::move(dev_shape_and_strides_owner)); return std::make_pair( keep_args_alive(exec_q, {src, dst}, {temporaries_cleanup_ev}), tri_ev); diff --git a/dpnp/libtensor/source/where.cpp b/dpnp/libtensor/source/where.cpp index f140e07d1cc9..036429070326 100644 --- a/dpnp/libtensor/source/where.cpp +++ b/dpnp/libtensor/source/where.cpp @@ -176,10 +176,10 @@ std::pair auto where_ev = contig_fn(exec_q, nelems, cond_data, x1_data, x2_data, dst_data, depends); - sycl::event ht_ev = + sycl::event keep_args_alive_ev = keep_args_alive(exec_q, {x1, x2, dst, condition}, {where_ev}); - return std::make_pair(ht_ev, where_ev); + return std::make_pair(keep_args_alive_ev, where_ev); } auto const &cond_strides = condition.get_strides_vector(); @@ -207,12 +207,12 @@ std::pair auto fn = where_strided_dispatch_table[x1_typeid][cond_typeid]; - std::vector host_task_events; - host_task_events.reserve(2); + std::vector cleanup_events; + cleanup_events.reserve(2); using dpnp::tensor::offset_utils::device_allocate_and_pack; auto ptr_size_event_tuple = device_allocate_and_pack( - exec_q, host_task_events, + exec_q, cleanup_events, // common shape and strides simplified_shape, simplified_cond_strides, simplified_x1_strides, simplified_x2_strides, simplified_dst_strides); @@ -236,10 +236,10 @@ std::pair sycl::event temporaries_cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free(exec_q, {where_ev}, packed_shape_strides_owner); - host_task_events.push_back(temporaries_cleanup_ev); + cleanup_events.push_back(temporaries_cleanup_ev); sycl::event arg_cleanup_ev = - keep_args_alive(exec_q, {x1, x2, condition, dst}, host_task_events); + keep_args_alive(exec_q, {x1, x2, condition, dst}, cleanup_events); return std::make_pair(arg_cleanup_ev, where_ev); } From b9fd59660b91e99d7edc1eef9507d81953a2b0b8 Mon Sep 17 00:00:00 2001 From: Nikita Grigorian Date: Thu, 3 Dec 2026 14:57:22 -0800 Subject: [PATCH 3/3] Keep the FFT descriptor alive in the FFT extension The FFT extension kept only the input and output arrays alive, and _compute_result drops the descriptor on return, so the transform could run on a freed descriptor. A blocking scalar copy when scaling the result has hidden it so far; builds with oneMath hit it once that copy is asynchronous (#3182). With keep_args_alive deferring releases through dpctl, the extension now keeps the Python object wrapping the descriptor alive too, which a host task could not release with oneMath without deadlocking. The FFT functions also return the keep-alive event first and the transform's event second, as the other extensions do and as _compute_result already unpacks them. --- dpnp/libtensor/extensions/fft/in_place.tpp | 6 ++- .../libtensor/extensions/fft/out_of_place.tpp | 6 ++- dpnp/tests/test_fft.py | 41 +++++++++++++++++++ 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/dpnp/libtensor/extensions/fft/in_place.tpp b/dpnp/libtensor/extensions/fft/in_place.tpp index 3c6cf91fc152..9edcd546f058 100644 --- a/dpnp/libtensor/extensions/fft/in_place.tpp +++ b/dpnp/libtensor/extensions/fft/in_place.tpp @@ -118,10 +118,12 @@ std::pair throw std::runtime_error(error_msg.str()); } + // the Python object already wrapping `descr`, which the FFT uses until done + const py::object py_descr = py::cast(&descr); sycl::event args_ev = - dpnp::utils::keep_args_alive(exec_q, {in_out}, {fft_event}); + dpnp::utils::keep_args_alive(exec_q, {py_descr, in_out}, {fft_event}); - return std::make_pair(fft_event, args_ev); + return std::make_pair(args_ev, fft_event); } } // namespace dpnp::extensions::fft diff --git a/dpnp/libtensor/extensions/fft/out_of_place.tpp b/dpnp/libtensor/extensions/fft/out_of_place.tpp index dcde7d78b10b..747443df305e 100644 --- a/dpnp/libtensor/extensions/fft/out_of_place.tpp +++ b/dpnp/libtensor/extensions/fft/out_of_place.tpp @@ -183,10 +183,12 @@ std::pair throw std::runtime_error(error_msg.str()); } + // the Python object already wrapping `descr`, which the FFT uses until done + const py::object py_descr = py::cast(&descr); sycl::event args_ev = - dpnp::utils::keep_args_alive(exec_q, {in, out}, {fft_event}); + dpnp::utils::keep_args_alive(exec_q, {py_descr, in, out}, {fft_event}); - return std::make_pair(fft_event, args_ev); + return std::make_pair(args_ev, fft_event); } } // namespace dpnp::extensions::fft diff --git a/dpnp/tests/test_fft.py b/dpnp/tests/test_fft.py index b0c11cd21091..a843693b8a08 100644 --- a/dpnp/tests/test_fft.py +++ b/dpnp/tests/test_fft.py @@ -1,6 +1,13 @@ +import weakref + import dpctl import numpy import pytest +from dpctl._sycl_queue import ( + _drain_deferred_releases, + _start_deferred_release_watcher, + _stop_deferred_release_watcher, +) from numpy.testing import assert_raises import dpnp @@ -974,3 +981,37 @@ def test_1d_array(self): expected = numpy.fft.irfftn(a) flag = True if numpy_version() < "2.0.0" else False assert_dtype_allclose(result, expected, check_only_type_kind=flag) + + +@pytest.fixture +def stopped_release_watcher(): + """Leaves dpctl's deferred releases to the test rather than to its thread.""" + _stop_deferred_release_watcher() + yield + _start_deferred_release_watcher() + + +@pytest.mark.parametrize("in_place", [False, True]) +def test_descriptor_outlives_its_fft( + monkeypatch, stopped_release_watcher, in_place +): + import dpnp.libtensor._fft_impl as fi + + name = "_fft_in_place" if in_place else "_fft_out_of_place" + fft_fn = getattr(fi, name) + calls = [] + + def _fft(dsc, *args, **kwargs): + ht_ev, fft_ev = fft_fn(dsc, *args, **kwargs) + calls.append((weakref.ref(dsc), ht_ev)) + return ht_ev, fft_ev + + monkeypatch.setattr(fi, name, _fft) + a = dpnp.ones(64, dtype=dpnp.complex64) + dpnp.fft.fft(a, out=a if in_place else None) + dsc_ref, ht_ev = calls[0] + assert dsc_ref() is not None + + ht_ev.wait() + _drain_deferred_releases() + assert dsc_ref() is None