|
| 1 | +//==----------- memory_pool.hpp --- SYCL asynchronous allocation -----------==// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#pragma once |
| 10 | +#include <sycl/context.hpp> // for context |
| 11 | +#include <sycl/device.hpp> // for device |
| 12 | +#include <sycl/ext/oneapi/experimental/async_alloc/memory_pool_properties.hpp> |
| 13 | +#include <sycl/queue.hpp> // for queue |
| 14 | +#include <sycl/usm/usm_enums.hpp> // for usm::alloc |
| 15 | + |
| 16 | +namespace sycl { |
| 17 | +inline namespace _V1 { |
| 18 | +namespace ext::oneapi::experimental { |
| 19 | + |
| 20 | +// Forward declare memory_pool_impl. |
| 21 | +namespace detail { |
| 22 | +class memory_pool_impl; |
| 23 | +} // namespace detail |
| 24 | + |
| 25 | +/// Memory pool |
| 26 | +class __SYCL_EXPORT memory_pool { |
| 27 | + |
| 28 | +public: |
| 29 | + // NOT SUPPORTED: Host side pools unsupported. |
| 30 | + memory_pool(const sycl::context &, sycl::usm::alloc kind, |
| 31 | + const property_list & = {}) { |
| 32 | + if (kind == sycl::usm::alloc::device || kind == sycl::usm::alloc::shared) |
| 33 | + throw sycl::exception(sycl::make_error_code(sycl::errc::invalid), |
| 34 | + "Device and shared allocation kinds are disallowed " |
| 35 | + "without specifying a device!"); |
| 36 | + if (kind == sycl::usm::alloc::unknown) |
| 37 | + throw sycl::exception(sycl::make_error_code(sycl::errc::invalid), |
| 38 | + "Unknown allocation kinds are disallowed!"); |
| 39 | + |
| 40 | + throw sycl::exception( |
| 41 | + sycl::make_error_code(sycl::errc::feature_not_supported), |
| 42 | + "Host allocated pools are unsupported!"); |
| 43 | + } |
| 44 | + |
| 45 | + memory_pool(const sycl::context &ctx, const sycl::device &dev, |
| 46 | + sycl::usm::alloc kind, const property_list &props = {}); |
| 47 | + |
| 48 | + memory_pool(const sycl::queue &q, sycl::usm::alloc kind, |
| 49 | + const property_list &props = {}) |
| 50 | + : memory_pool(q.get_context(), q.get_device(), kind, props) {} |
| 51 | + |
| 52 | + // NOT SUPPORTED: Creating a pool from an existing allocation is unsupported. |
| 53 | + memory_pool(const sycl::context &, void *, size_t, |
| 54 | + const property_list & = {}) { |
| 55 | + throw sycl::exception( |
| 56 | + sycl::make_error_code(sycl::errc::feature_not_supported), |
| 57 | + "Creating a pool from an existing allocation is unsupported!"); |
| 58 | + } |
| 59 | + |
| 60 | + ~memory_pool() = default; |
| 61 | + |
| 62 | + // Copy constructible/assignable, move constructible/assignable. |
| 63 | + memory_pool(const memory_pool &) = default; |
| 64 | + memory_pool(memory_pool &&) = default; |
| 65 | + memory_pool &operator=(const memory_pool &) = default; |
| 66 | + memory_pool &operator=(memory_pool &&) = default; |
| 67 | + |
| 68 | + // Equality comparison. |
| 69 | + bool operator==(const memory_pool &rhs) const { return impl == rhs.impl; } |
| 70 | + bool operator!=(const memory_pool &rhs) const { return !(*this == rhs); } |
| 71 | + |
| 72 | + // Impl handles getters and setters. |
| 73 | + sycl::context get_context() const; |
| 74 | + sycl::device get_device() const; |
| 75 | + sycl::usm::alloc get_alloc_kind() const; |
| 76 | + size_t get_threshold() const; |
| 77 | + size_t get_reserved_size_current() const; |
| 78 | + size_t get_used_size_current() const; |
| 79 | + |
| 80 | + void increase_threshold_to(size_t newThreshold); |
| 81 | + |
| 82 | + // Property getters. |
| 83 | + template <typename PropertyT> bool has_property() const noexcept { |
| 84 | + return getPropList().template has_property<PropertyT>(); |
| 85 | + } |
| 86 | + template <typename PropertyT> PropertyT get_property() const { |
| 87 | + return getPropList().template get_property<PropertyT>(); |
| 88 | + } |
| 89 | + |
| 90 | +protected: |
| 91 | + std::shared_ptr<detail::memory_pool_impl> impl; |
| 92 | + |
| 93 | + memory_pool(std::shared_ptr<detail::memory_pool_impl> Impl) : impl(Impl) {} |
| 94 | + |
| 95 | + template <class Obj> |
| 96 | + friend const decltype(Obj::impl) & |
| 97 | + sycl::detail::getSyclObjImpl(const Obj &SyclObject); |
| 98 | + |
| 99 | + template <class T> |
| 100 | + friend T sycl::detail::createSyclObjFromImpl( |
| 101 | + std::add_rvalue_reference_t<decltype(T::impl)> ImplObj); |
| 102 | + template <class T> |
| 103 | + friend T sycl::detail::createSyclObjFromImpl( |
| 104 | + std::add_lvalue_reference_t<const decltype(T::impl)> ImplObj); |
| 105 | + |
| 106 | + const property_list &getPropList() const; |
| 107 | +}; |
| 108 | + |
| 109 | +} // namespace ext::oneapi::experimental |
| 110 | +} // namespace _V1 |
| 111 | +} // namespace sycl |
| 112 | + |
| 113 | +namespace std { |
| 114 | +template <> struct hash<sycl::ext::oneapi::experimental::memory_pool> { |
| 115 | + size_t operator()( |
| 116 | + const sycl::ext::oneapi::experimental::memory_pool &mem_pool) const { |
| 117 | + return hash<std::shared_ptr< |
| 118 | + sycl::ext::oneapi::experimental::detail::memory_pool_impl>>()( |
| 119 | + sycl::detail::getSyclObjImpl(mem_pool)); |
| 120 | + } |
| 121 | +}; |
| 122 | +} // namespace std |
0 commit comments