aligned_resource_adaptor.hpp
1 /*
2  * Copyright (c) 2021, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <rmm/cuda_stream_view.hpp>
19 #include <rmm/detail/aligned.hpp>
20 #include <rmm/detail/error.hpp>
21 #include <rmm/mr/device/device_memory_resource.hpp>
22 
23 #include <cstddef>
24 #include <mutex>
25 #include <optional>
26 #include <unordered_map>
27 
28 namespace rmm::mr {
48 template <typename Upstream>
50  public:
62  explicit aligned_resource_adaptor(Upstream* upstream,
63  std::size_t alignment = rmm::detail::CUDA_ALLOCATION_ALIGNMENT,
64  std::size_t alignment_threshold = default_alignment_threshold)
65  : upstream_{upstream}, alignment_{alignment}, alignment_threshold_{alignment_threshold}
66  {
67  RMM_EXPECTS(nullptr != upstream, "Unexpected null upstream resource pointer.");
68  RMM_EXPECTS(rmm::detail::is_supported_alignment(alignment),
69  "Allocation alignment is not a power of 2.");
70  }
71 
72  aligned_resource_adaptor() = delete;
73  ~aligned_resource_adaptor() override = default;
74  aligned_resource_adaptor(aligned_resource_adaptor const&) = delete;
75  aligned_resource_adaptor(aligned_resource_adaptor&&) = delete;
76  aligned_resource_adaptor& operator=(aligned_resource_adaptor const&) = delete;
77  aligned_resource_adaptor& operator=(aligned_resource_adaptor&&) = delete;
78 
84  Upstream* get_upstream() const noexcept { return upstream_; }
85 
89  [[nodiscard]] bool supports_streams() const noexcept override
90  {
91  return upstream_->supports_streams();
92  }
93 
99  [[nodiscard]] bool supports_get_mem_info() const noexcept override
100  {
101  return upstream_->supports_get_mem_info();
102  }
103 
104  private:
105  static constexpr std::size_t default_alignment_threshold = 0;
106  using lock_guard = std::lock_guard<std::mutex>;
107 
119  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
120  {
121  if (alignment_ == rmm::detail::CUDA_ALLOCATION_ALIGNMENT || bytes < alignment_threshold_) {
122  return upstream_->allocate(bytes, stream);
123  }
124  auto const size = upstream_allocation_size(bytes);
125  void* pointer = upstream_->allocate(size, stream);
126  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
127  auto const address = reinterpret_cast<std::size_t>(pointer);
128  auto const aligned_address = rmm::detail::align_up(address, alignment_);
129  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast,performance-no-int-to-ptr)
130  void* aligned_pointer = reinterpret_cast<void*>(aligned_address);
131  if (pointer != aligned_pointer) {
132  lock_guard lock(mtx_);
133  pointers_.emplace(aligned_pointer, pointer);
134  }
135  return aligned_pointer;
136  }
137 
147  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) override
148  {
149  if (alignment_ == rmm::detail::CUDA_ALLOCATION_ALIGNMENT || bytes < alignment_threshold_) {
150  upstream_->deallocate(ptr, bytes, stream);
151  } else {
152  {
153  lock_guard lock(mtx_);
154  auto const iter = pointers_.find(ptr);
155  if (iter != pointers_.end()) {
156  ptr = iter->second;
157  pointers_.erase(iter);
158  }
159  }
160  upstream_->deallocate(ptr, upstream_allocation_size(bytes), stream);
161  }
162  }
163 
173  [[nodiscard]] bool do_is_equal(device_memory_resource const& other) const noexcept override
174  {
175  if (this == &other) { return true; }
176  auto cast = dynamic_cast<aligned_resource_adaptor<Upstream> const*>(&other);
177  return cast != nullptr && upstream_->is_equal(*cast->get_upstream()) &&
178  alignment_ == cast->alignment_ && alignment_threshold_ == cast->alignment_threshold_;
179  }
180 
191  [[nodiscard]] std::pair<std::size_t, std::size_t> do_get_mem_info(
192  cuda_stream_view stream) const override
193  {
194  return upstream_->get_mem_info(stream);
195  }
196 
204  std::size_t upstream_allocation_size(std::size_t bytes) const
205  {
206  auto const aligned_size = rmm::detail::align_up(bytes, alignment_);
207  return aligned_size + alignment_ - rmm::detail::CUDA_ALLOCATION_ALIGNMENT;
208  }
209 
210  Upstream* upstream_;
211  std::unordered_map<void*, void*> pointers_;
212  std::size_t alignment_;
213  std::size_t alignment_threshold_;
214  mutable std::mutex mtx_;
215 };
216 
217 } // namespace rmm::mr
rmm::mr::aligned_resource_adaptor
Resource that adapts Upstream memory resource to allocate memory in a specified alignment size.
Definition: aligned_resource_adaptor.hpp:49
rmm::cuda_stream_view
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:34
rmm::mr::aligned_resource_adaptor::aligned_resource_adaptor
aligned_resource_adaptor(Upstream *upstream, std::size_t alignment=rmm::detail::CUDA_ALLOCATION_ALIGNMENT, std::size_t alignment_threshold=default_alignment_threshold)
Construct an aligned resource adaptor using upstream to satisfy allocation requests.
Definition: aligned_resource_adaptor.hpp:62
rmm::mr::aligned_resource_adaptor::get_upstream
Upstream * get_upstream() const noexcept
Get the upstream memory resource.
Definition: aligned_resource_adaptor.hpp:84
rmm::mr::aligned_resource_adaptor::supports_streams
bool supports_streams() const noexcept override
Query whether the resource supports use of non-null CUDA streams for allocation/deallocation.
Definition: aligned_resource_adaptor.hpp:89
rmm::mr::aligned_resource_adaptor::supports_get_mem_info
bool supports_get_mem_info() const noexcept override
Query whether the resource supports the get_mem_info API.
Definition: aligned_resource_adaptor.hpp:99
rmm::mr::device_memory_resource
Base class for all libcudf device memory allocation.
Definition: device_memory_resource.hpp:82