Skip to content

Commit

Permalink
Add static assers to async_smart_free
Browse files Browse the repository at this point in the history
One asserts that at least one unique pointer is specified.
Another that specified arguments are unique pointers with
USMDeleter.
  • Loading branch information
oleksandr-pavlyk authored and ndgrigorian committed Dec 28, 2024
1 parent 11cdf06 commit 775b423
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions dpctl/tensor/libtensor/include/utils/sycl_alloc_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <iostream>
#include <memory>
#include <stdexcept>
#include <type_traits>
#include <vector>

#include "sycl/sycl.hpp"
Expand Down Expand Up @@ -140,12 +141,50 @@ smart_malloc_host(std::size_t count,
return smart_malloc<T>(count, q, sycl::usm::alloc::host, propList);
}

namespace
{
template <typename T> struct valid_smart_ptr : public std::false_type
{
};

template <typename ValT, typename DelT>
struct valid_smart_ptr<std::unique_ptr<ValT, DelT> &>
: public std::is_same<DelT, USMDeleter>
{
};

template <typename ValT, typename DelT>
struct valid_smart_ptr<std::unique_ptr<ValT, DelT>>
: public std::is_same<DelT, USMDeleter>
{
};

// base case
template <typename... Rest> struct all_valid_smart_ptrs
{
static constexpr bool value = true;
};

template <typename Arg, typename... RestArgs>
struct all_valid_smart_ptrs<Arg, RestArgs...>
{
static constexpr bool value = valid_smart_ptr<Arg>::value &&
(all_valid_smart_ptrs<RestArgs...>::value);
};
} // namespace

template <typename... Args>
sycl::event async_smart_free(sycl::queue &exec_q,
const std::vector<sycl::event> &depends,
Args &&...args)
{
constexpr std::size_t n = sizeof...(Args);
static_assert(
n > 0, "async_smart_free requires at least one smart pointer argument");

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

std::vector<void *> ptrs;
ptrs.reserve(n);
Expand Down

0 comments on commit 775b423

Please sign in to comment.