|
| 1 | +// Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#ifndef DALI_KERNELS_DYNAMIC_SCRATCHPAD_H_ |
| 16 | +#define DALI_KERNELS_DYNAMIC_SCRATCHPAD_H_ |
| 17 | + |
| 18 | +#include <array> |
| 19 | +#include <cassert> |
| 20 | +#include <tuple> |
| 21 | +#include <type_traits> |
| 22 | +#include <utility> |
| 23 | +#include "dali/core/static_switch.h" |
| 24 | +#include "dali/core/mm/fixed_order_resource.h" |
| 25 | +#include "dali/core/mm/memory.h" |
| 26 | +#include "dali/core/mm/memory_kind.h" |
| 27 | +#include "dali/core/mm/monotonic_resource.h" |
| 28 | +#include "dali/kernels/context.h" |
| 29 | +#include "dali/kernels/kernel_req.h" |
| 30 | + |
| 31 | +namespace dali { |
| 32 | +namespace kernels { |
| 33 | + |
| 34 | +namespace detail { |
| 35 | + |
| 36 | +template <typename T, typename... Ts> |
| 37 | +struct index_in_pack; |
| 38 | + |
| 39 | +template <typename T, typename... Ts> |
| 40 | +struct index_in_pack<T, T, Ts...> : std::integral_constant<int, 0> {}; |
| 41 | + |
| 42 | +template <typename T, typename U, typename... Ts> |
| 43 | +struct index_in_pack<T, U, Ts...> : |
| 44 | + std::integral_constant<int, index_in_pack<T, Ts...>::value + 1> {}; |
| 45 | + |
| 46 | +/** |
| 47 | + * @brief Implements upstream handling and ordered wrappers. |
| 48 | + */ |
| 49 | +template <typename... Kinds> |
| 50 | +class DynamicScratchpadImplT { |
| 51 | + protected: |
| 52 | + template <typename Kind> |
| 53 | + void set_upstream_resource(mm::memory_resource<Kind> *rsrc) { |
| 54 | + resource<Kind>() = mm::monotonic_memory_resource<Kind>(rsrc, initial_size<Kind>()); |
| 55 | + } |
| 56 | + |
| 57 | + template <typename Kind> |
| 58 | + void set_upstream_resource(mm::async_memory_resource<Kind> *rsrc, |
| 59 | + AccessOrder alloc_order, |
| 60 | + AccessOrder dealloc_order = {}) { |
| 61 | + static_assert(!std::is_same<Kind, mm::memory_kind::host>::value, |
| 62 | + "Cannot use a stream-ordered resource for plain host memory"); |
| 63 | + adapter<Kind>() = { rsrc, alloc_order, dealloc_order }; |
| 64 | + set_upstream_resource<Kind>(&adapter<Kind>()); |
| 65 | + } |
| 66 | + |
| 67 | + template <typename Kind> |
| 68 | + size_t &initial_size() { |
| 69 | + return initial_sizes_[index_in_pack<Kind, Kinds...>::value]; |
| 70 | + } |
| 71 | + |
| 72 | + template <typename Kind> |
| 73 | + size_t initial_size() const { |
| 74 | + return initial_sizes_[index_in_pack<Kind, Kinds...>::value]; |
| 75 | + } |
| 76 | + |
| 77 | + template <typename Kind> |
| 78 | + mm::memory_resource<Kind> *get_upstream() const { |
| 79 | + std::get<mm::monotonic_memory_resource<Kind>>(resources_)->get_upstream(); |
| 80 | + } |
| 81 | + |
| 82 | + template <typename Kind> |
| 83 | + auto &adapter() { |
| 84 | + return std::get<mm::fixed_order_resource<Kind>>(adapters_); |
| 85 | + } |
| 86 | + |
| 87 | + template <typename Kind> |
| 88 | + auto &adapter() const { |
| 89 | + return std::get<mm::fixed_order_resource<Kind>>(adapters_); |
| 90 | + } |
| 91 | + |
| 92 | + template <typename Kind> |
| 93 | + auto &resource() { |
| 94 | + return std::get<mm::monotonic_memory_resource<Kind>>(resources_); |
| 95 | + } |
| 96 | + |
| 97 | + template <typename Kind> |
| 98 | + auto &resource() const { |
| 99 | + return std::get<mm::monotonic_memory_resource<Kind>>(resources_); |
| 100 | + } |
| 101 | + |
| 102 | + std::tuple<mm::fixed_order_resource<Kinds>...> adapters_; |
| 103 | + std::tuple<mm::monotonic_memory_resource<Kinds>...> resources_; |
| 104 | + std::array<size_t, sizeof...(Kinds)> initial_sizes_ = {}; |
| 105 | +}; |
| 106 | + |
| 107 | +using DynamicScratchpadImpl = DynamicScratchpadImplT< |
| 108 | + mm::memory_kind::host, |
| 109 | + mm::memory_kind::pinned, |
| 110 | + mm::memory_kind::device, |
| 111 | + mm::memory_kind::managed>; |
| 112 | + |
| 113 | +} // namespace detail |
| 114 | + |
| 115 | +class DynamicScratchpad |
| 116 | + : public Scratchpad |
| 117 | + , private detail::DynamicScratchpadImpl { |
| 118 | + public: |
| 119 | + /** |
| 120 | + * @brief Constructs a dynamically allocated scratchpad |
| 121 | + * |
| 122 | + * @param initial_sizes Sizes, in bytes, of the initial buffers. Note that these buffers |
| 123 | + * are allocated lazily, so nothing is allocated if there's no request |
| 124 | + * for memory of any given kind. |
| 125 | + * @param device_order Allocation and deallocation order for device memory. |
| 126 | + * @param pinned_dealloc_order Deallocation order for pinned memory. Allocation is always |
| 127 | + * host-ordered. If not set, device_order is used. |
| 128 | + * @param managed_dealloc_order Deallocation order for managed memory. Allocation is always |
| 129 | + * host-ordered. If not set, device_order is used. |
| 130 | + */ |
| 131 | + explicit DynamicScratchpad(scratch_sizes_t initial_sizes = {}, |
| 132 | + AccessOrder device_order = cudaStream_t(0), |
| 133 | + AccessOrder pinned_dealloc_order = {}, |
| 134 | + AccessOrder managed_dealloc_order = {}) { |
| 135 | + initial_sizes_ = initial_sizes; |
| 136 | + for (auto &s : initial_sizes_) { |
| 137 | + if (s == 0) |
| 138 | + s = 4096; |
| 139 | + } |
| 140 | + if (!pinned_dealloc_order.has_value()) |
| 141 | + pinned_dealloc_order = device_order; |
| 142 | + if (!managed_dealloc_order.has_value()) |
| 143 | + managed_dealloc_order = device_order; |
| 144 | + |
| 145 | + set_upstream_resource<mm::memory_kind::host>(mm::GetDefaultResource<mm::memory_kind::host>()); |
| 146 | + |
| 147 | + set_upstream_resource<mm::memory_kind::pinned>( |
| 148 | + mm::GetDefaultResource<mm::memory_kind::pinned>(), |
| 149 | + AccessOrder::host(), |
| 150 | + pinned_dealloc_order); |
| 151 | + |
| 152 | + set_upstream_resource<mm::memory_kind::device>( |
| 153 | + mm::GetDefaultResource<mm::memory_kind::device>(), |
| 154 | + device_order); |
| 155 | + |
| 156 | + set_upstream_resource<mm::memory_kind::managed>( |
| 157 | + mm::GetDefaultResource<mm::memory_kind::managed>(), |
| 158 | + AccessOrder::host(), |
| 159 | + managed_dealloc_order); |
| 160 | + } |
| 161 | + |
| 162 | + virtual void *Alloc(mm::memory_kind_id kind_id, size_t bytes, size_t alignment) { |
| 163 | + void *ret = nullptr; |
| 164 | + TYPE_SWITCH(kind_id, mm::kind2id, Kind, |
| 165 | + (mm::memory_kind::host, |
| 166 | + mm::memory_kind::pinned, |
| 167 | + mm::memory_kind::device, |
| 168 | + mm::memory_kind::managed), |
| 169 | + (ret = resource<Kind>().allocate(bytes, alignment)), |
| 170 | + (assert(!"Incorrect memory kind id");)); |
| 171 | + return ret; |
| 172 | + } |
| 173 | +}; |
| 174 | + |
| 175 | +} // namespace kernels |
| 176 | +} // namespace dali |
| 177 | + |
| 178 | +#endif // DALI_KERNELS_DYNAMIC_SCRATCHPAD_H_ |
| 179 | + |
0 commit comments