Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move vectors ptrs and dels into host_task lambda
Since vectors `ptrs` and `dels` are no longer needed after host_task
submission, we might as well avoid the copying and use std::move
in lambda capture initialization.

Also renamed `Args` template pack to `UniquePtrTs`, and `args`
template argument to `unique_ptrs`.

Added comments next to each include to note the entity which requires
it.
  • Loading branch information
oleksandr-pavlyk committed Jan 8, 2025
commit 80f288ccafe8de960e07effe19cdef64df5784b9
30 changes: 16 additions & 14 deletions dpctl/tensor/libtensor/include/utils/sycl_alloc_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@

#pragma once

#include <cstddef>
#include <exception>
#include <iostream>
#include <memory>
#include <stdexcept>
#include <type_traits>
#include <cstddef> // for std::size_t
#include <exception> // for std::exception
#include <iostream> // for std::cerr
#include <memory> // for std::unique_ptr
#include <stdexcept> // for std::runtime_error
#include <type_traits> // for std::true_type, std::false_type
#include <utility> // for std::move
#include <vector>

#include "sycl/sycl.hpp"
Expand Down Expand Up @@ -174,37 +175,38 @@ struct all_valid_smart_ptrs<Arg, RestArgs...>
};
} // end of namespace detail

template <typename... Args>
/*! @brief Submit host_task and transfer ownership from smart pointers to it */
template <typename... UniquePtrTs>
sycl::event async_smart_free(sycl::queue &exec_q,
const std::vector<sycl::event> &depends,
Args &&...args)
UniquePtrTs &&...unique_pointers)
{
constexpr std::size_t n = sizeof...(Args);
constexpr std::size_t n = sizeof...(UniquePtrTs);
static_assert(
n > 0, "async_smart_free requires at least one smart pointer argument");

static_assert(
detail::all_valid_smart_ptrs<Args...>::value,
detail::all_valid_smart_ptrs<UniquePtrTs...>::value,
"async_smart_free requires unique_ptr created with smart_malloc");

std::vector<void *> ptrs;
ptrs.reserve(n);
(ptrs.push_back(reinterpret_cast<void *>(args.get())), ...);
(ptrs.push_back(reinterpret_cast<void *>(unique_pointers.get())), ...);

std::vector<USMDeleter> dels;
dels.reserve(n);
(dels.push_back(args.get_deleter()), ...);
(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, dels]() {
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]);
}
});
});
(args.release(), ...);
(unique_pointers.release(), ...);

return ht_e;
}
Expand Down