binning_memory_resource.hpp
1 /*
2  * Copyright (c) 2020-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/detail/aligned.hpp>
19 #include <rmm/mr/device/device_memory_resource.hpp>
20 #include <rmm/mr/device/fixed_size_memory_resource.hpp>
21 
22 #include <cuda_runtime_api.h>
23 
24 #include <algorithm>
25 #include <cassert>
26 #include <map>
27 #include <memory>
28 #include <vector>
29 
30 namespace rmm::mr {
31 
38 template <typename Upstream>
40  public:
51  explicit binning_memory_resource(Upstream* upstream_resource)
52  : upstream_mr_{[upstream_resource]() {
53  RMM_EXPECTS(nullptr != upstream_resource, "Unexpected null upstream pointer.");
54  return upstream_resource;
55  }()}
56  {
57  }
58 
71  binning_memory_resource(Upstream* upstream_resource,
72  int8_t min_size_exponent, // NOLINT(bugprone-easily-swappable-parameters)
73  int8_t max_size_exponent)
74  : upstream_mr_{[upstream_resource]() {
75  RMM_EXPECTS(nullptr != upstream_resource, "Unexpected null upstream pointer.");
76  return upstream_resource;
77  }()}
78  {
79  for (auto i = min_size_exponent; i <= max_size_exponent; i++) {
80  add_bin(1 << i);
81  }
82  }
83 
88  ~binning_memory_resource() override = default;
89 
90  binning_memory_resource() = delete;
93  binning_memory_resource& operator=(binning_memory_resource const&) = delete;
94  binning_memory_resource& operator=(binning_memory_resource&&) = delete;
95 
102  [[nodiscard]] bool supports_streams() const noexcept override { return true; }
103 
109  [[nodiscard]] bool supports_get_mem_info() const noexcept override { return false; }
110 
116  [[nodiscard]] Upstream* get_upstream() const noexcept { return upstream_mr_; }
117 
134  void add_bin(std::size_t allocation_size, device_memory_resource* bin_resource = nullptr)
135  {
136  allocation_size =
137  rmm::detail::align_up(allocation_size, rmm::detail::CUDA_ALLOCATION_ALIGNMENT);
138 
139  if (nullptr != bin_resource) {
140  resource_bins_.insert({allocation_size, bin_resource});
141  } else if (resource_bins_.count(allocation_size) == 0) { // do nothing if bin already exists
142 
143  owned_bin_resources_.push_back(
144  std::make_unique<fixed_size_memory_resource<Upstream>>(upstream_mr_, allocation_size));
145  resource_bins_.insert({allocation_size, owned_bin_resources_.back().get()});
146  }
147  }
148 
149  private:
158  device_memory_resource* get_resource(std::size_t bytes)
159  {
160  auto iter = resource_bins_.lower_bound(bytes);
161  return (iter != resource_bins_.cend()) ? iter->second
162  : static_cast<device_memory_resource*>(get_upstream());
163  }
164 
174  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
175  {
176  if (bytes <= 0) { return nullptr; }
177  return get_resource(bytes)->allocate(bytes, stream);
178  }
179 
190  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) override
191  {
192  auto res = get_resource(bytes);
193  if (res != nullptr) { res->deallocate(ptr, bytes, stream); }
194  }
195 
204  [[nodiscard]] std::pair<std::size_t, std::size_t> do_get_mem_info(
205  cuda_stream_view stream) const override
206  {
207  return std::make_pair(0, 0);
208  }
209 
210  Upstream* upstream_mr_; // The upstream memory_resource from which to allocate blocks.
211 
212  std::vector<std::unique_ptr<fixed_size_memory_resource<Upstream>>> owned_bin_resources_;
213 
214  std::map<std::size_t, device_memory_resource*> resource_bins_;
215 };
216 
217 } // namespace rmm::mr
rmm::mr::device_memory_resource::allocate
void * allocate(std::size_t bytes, cuda_stream_view stream=cuda_stream_view{})
Allocates memory of size at least bytes.
Definition: device_memory_resource.hpp:106
rmm::cuda_stream_view
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:34
rmm::mr::binning_memory_resource::add_bin
void add_bin(std::size_t allocation_size, device_memory_resource *bin_resource=nullptr)
Add a bin allocator to this resource.
Definition: binning_memory_resource.hpp:134
rmm::mr::binning_memory_resource
Allocates memory from upstream resources associated with bin sizes.
Definition: binning_memory_resource.hpp:39
rmm::mr::binning_memory_resource::supports_streams
bool supports_streams() const noexcept override
Query whether the resource supports use of non-null streams for allocation/deallocation.
Definition: binning_memory_resource.hpp:102
rmm::mr::fixed_size_memory_resource
A device_memory_resource which allocates memory blocks of a single fixed size.
Definition: fixed_size_memory_resource.hpp:46
rmm::mr::binning_memory_resource::binning_memory_resource
binning_memory_resource(Upstream *upstream_resource, int8_t min_size_exponent, int8_t max_size_exponent)
Construct a new binning memory resource object with a range of initial bins.
Definition: binning_memory_resource.hpp:71
rmm::mr::binning_memory_resource::supports_get_mem_info
bool supports_get_mem_info() const noexcept override
Query whether the resource supports the get_mem_info API.
Definition: binning_memory_resource.hpp:109
rmm::mr::binning_memory_resource::binning_memory_resource
binning_memory_resource(Upstream *upstream_resource)
Construct a new binning memory resource object.
Definition: binning_memory_resource.hpp:51
rmm::mr::binning_memory_resource::get_upstream
Upstream * get_upstream() const noexcept
Get the upstream memory_resource object.
Definition: binning_memory_resource.hpp:116
rmm::mr::device_memory_resource
Base class for all libcudf device memory allocation.
Definition: device_memory_resource.hpp:82
rmm::mr::binning_memory_resource::~binning_memory_resource
~binning_memory_resource() override=default
Destroy the binning_memory_resource and free all memory allocated from the upstream resource.