All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
cuda_async_memory_resource.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021-2024, 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_device.hpp>
19 #include <rmm/cuda_stream_view.hpp>
20 #include <rmm/detail/error.hpp>
21 #include <rmm/detail/export.hpp>
22 #include <rmm/detail/runtime_async_alloc.hpp>
23 #include <rmm/detail/thrust_namespace.h>
26 
27 #include <cuda/std/type_traits>
28 #include <cuda_runtime_api.h>
29 
30 #include <cstddef>
31 #include <limits>
32 #include <optional>
33 
34 namespace RMM_NAMESPACE {
35 namespace mr {
47  public:
59  none = 0x0,
60  posix_file_descriptor = 0x1,
62  win32 = 0x2,
63  win32_kmt = 0x4
64  };
65 
83  // NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
84  cuda_async_memory_resource(std::optional<std::size_t> initial_pool_size = {},
85  std::optional<std::size_t> release_threshold = {},
86  std::optional<allocation_handle_type> export_handle_type = {})
87  {
88  // Check if cudaMallocAsync Memory pool supported
89  RMM_EXPECTS(rmm::detail::runtime_async_alloc::is_supported(),
90  "cudaMallocAsync not supported with this CUDA driver/runtime version");
91 
92  // Construct explicit pool
93  cudaMemPoolProps pool_props{};
94  pool_props.allocType = cudaMemAllocationTypePinned;
95  pool_props.handleTypes = static_cast<cudaMemAllocationHandleType>(
96  export_handle_type.value_or(allocation_handle_type::none));
97  RMM_EXPECTS(
98  rmm::detail::runtime_async_alloc::is_export_handle_type_supported(pool_props.handleTypes),
99  "Requested IPC memory handle type not supported");
100  pool_props.location.type = cudaMemLocationTypeDevice;
101  pool_props.location.id = rmm::get_current_cuda_device().value();
102  cudaMemPool_t cuda_pool_handle{};
103  RMM_CUDA_TRY(cudaMemPoolCreate(&cuda_pool_handle, &pool_props));
104  pool_ = cuda_async_view_memory_resource{cuda_pool_handle};
105 
106  // CUDA drivers before 11.5 have known incompatibilities with the async allocator.
107  // We'll disable `cudaMemPoolReuseAllowOpportunistic` if cuda driver < 11.5.
108  // See https://github.com/NVIDIA/spark-rapids/issues/4710.
109  int driver_version{};
110  RMM_CUDA_TRY(cudaDriverGetVersion(&driver_version));
111  constexpr auto min_async_version{11050};
112  if (driver_version < min_async_version) {
113  int disabled{0};
114  RMM_CUDA_TRY(
115  cudaMemPoolSetAttribute(pool_handle(), cudaMemPoolReuseAllowOpportunistic, &disabled));
116  }
117 
118  auto const [free, total] = rmm::available_device_memory();
119 
120  // Need an l-value to take address to pass to cudaMemPoolSetAttribute
121  uint64_t threshold = release_threshold.value_or(total);
122  RMM_CUDA_TRY(
123  cudaMemPoolSetAttribute(pool_handle(), cudaMemPoolAttrReleaseThreshold, &threshold));
124 
125  // Allocate and immediately deallocate the initial_pool_size to prime the pool with the
126  // specified size
127  auto const pool_size = initial_pool_size.value_or(free / 2);
128  auto* ptr = do_allocate(pool_size, cuda_stream_default);
129  do_deallocate(ptr, pool_size, cuda_stream_default);
130  }
131 
137  [[nodiscard]] cudaMemPool_t pool_handle() const noexcept { return pool_.pool_handle(); }
138 
139  ~cuda_async_memory_resource() override
140  {
141  RMM_ASSERT_CUDA_SUCCESS(cudaMemPoolDestroy(pool_handle()));
142  }
143  cuda_async_memory_resource(cuda_async_memory_resource const&) = delete;
144  cuda_async_memory_resource(cuda_async_memory_resource&&) = delete;
145  cuda_async_memory_resource& operator=(cuda_async_memory_resource const&) = delete;
146  cuda_async_memory_resource& operator=(cuda_async_memory_resource&&) = delete;
147 
148  private:
149  cuda_async_view_memory_resource pool_{};
150 
160  void* do_allocate(std::size_t bytes, rmm::cuda_stream_view stream) override
161  {
162  void* ptr{nullptr};
163  ptr = pool_.allocate(bytes, stream);
164  return ptr;
165  }
166 
175  void do_deallocate(void* ptr, std::size_t bytes, rmm::cuda_stream_view stream) override
176  {
177  pool_.deallocate(ptr, bytes, stream);
178  }
179 
187  [[nodiscard]] bool do_is_equal(device_memory_resource const& other) const noexcept override
188  {
189  auto const* async_mr = dynamic_cast<cuda_async_memory_resource const*>(&other);
190  return (async_mr != nullptr) && (this->pool_handle() == async_mr->pool_handle());
191  }
192 };
193  // end of group
195 } // namespace mr
196 } // namespace RMM_NAMESPACE
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:41
device_memory_resource derived class that uses cudaMallocAsync/cudaFreeAsync for allocation/deallocat...
Definition: cuda_async_memory_resource.hpp:46
allocation_handle_type
Flags for specifying memory allocation handle types.
Definition: cuda_async_memory_resource.hpp:58
cuda_async_memory_resource(std::optional< std::size_t > initial_pool_size={}, std::optional< std::size_t > release_threshold={}, std::optional< allocation_handle_type > export_handle_type={})
Constructs a cuda_async_memory_resource with the optionally specified initial pool size and release t...
Definition: cuda_async_memory_resource.hpp:84
cudaMemPool_t pool_handle() const noexcept
Returns the underlying native handle to the CUDA pool.
Definition: cuda_async_memory_resource.hpp:137
Base class for all libcudf device memory allocation.
Definition: device_memory_resource.hpp:94
std::pair< std::size_t, std::size_t > available_device_memory()
Returns the available and total device memory in bytes for the current device.
Definition: cuda_device.hpp:120
cuda_device_id get_current_cuda_device()
Returns a cuda_device_id for the current device.
Definition: cuda_device.hpp:96
static constexpr cuda_stream_view cuda_stream_default
Static cuda_stream_view of the default stream (stream 0), for convenience.
Definition: cuda_stream_view.hpp:127
constexpr value_type value() const noexcept
The wrapped integer value.
Definition: cuda_device.hpp:54