RMM  23.12
RAPIDS Memory Manager
pinned_memory_resource.hpp
Go to the documentation of this file.
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/cuda_stream_view.hpp>
19 #include <rmm/detail/aligned.hpp>
20 #include <rmm/detail/error.hpp>
22 
23 #include <cstddef>
24 #include <utility>
25 
26 namespace rmm::mr {
40  public:
41  pinned_memory_resource() = default;
42  ~pinned_memory_resource() override = default;
46  default;
48  default;
49 
56  [[nodiscard]] bool supports_streams() const noexcept { return false; }
57 
63  [[nodiscard]] bool supports_get_mem_info() const noexcept { return false; }
64 
73  [[nodiscard]] std::pair<std::size_t, std::size_t> get_mem_info(cuda_stream_view stream) const
74  {
75  return std::make_pair(0, 0);
76  }
77 
88  [[nodiscard]] void* allocate_async(std::size_t bytes, std::size_t alignment, cuda_stream_view)
89  {
90  return do_allocate(bytes, alignment);
91  }
92 
102  [[nodiscard]] void* allocate_async(std::size_t bytes, cuda_stream_view)
103  {
104  return do_allocate(bytes);
105  }
106 
115  void deallocate_async(void* ptr, std::size_t bytes, std::size_t alignment, cuda_stream_view)
116  {
117  do_deallocate(ptr, rmm::detail::align_up(bytes, alignment));
118  }
119 
125  friend void get_property(pinned_memory_resource const&, cuda::mr::device_accessible) noexcept {}
126 
127  private:
140  void* do_allocate(std::size_t bytes, std::size_t alignment = alignof(std::max_align_t)) override
141  {
142  // don't allocate anything if the user requested zero bytes
143  if (0 == bytes) { return nullptr; }
144 
145  // If the requested alignment isn't supported, use default
146  alignment = (rmm::detail::is_supported_alignment(alignment))
147  ? alignment
148  : rmm::detail::RMM_DEFAULT_HOST_ALIGNMENT;
149 
150  return rmm::detail::aligned_allocate(bytes, alignment, [](std::size_t size) {
151  void* ptr{nullptr};
152  auto status = cudaMallocHost(&ptr, size);
153  if (cudaSuccess != status) { throw std::bad_alloc{}; }
154  return ptr;
155  });
156  }
157 
171  void do_deallocate(void* ptr,
172  std::size_t bytes,
173  std::size_t alignment = alignof(std::max_align_t)) override
174  {
175  if (nullptr == ptr) { return; }
176  rmm::detail::aligned_deallocate(
177  ptr, bytes, alignment, [](void* ptr) { RMM_ASSERT_CUDA_SUCCESS(cudaFreeHost(ptr)); });
178  }
179 };
180 static_assert(cuda::mr::async_resource_with<pinned_memory_resource,
181  cuda::mr::host_accessible,
182  cuda::mr::device_accessible>); // end of group
184 } // namespace rmm::mr
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:41
Base class for host memory allocation.
Definition: host_memory_resource.hpp:54
A host_memory_resource that uses cudaMallocHost to allocate pinned/page-locked host memory.
Definition: pinned_memory_resource.hpp:39
friend void get_property(pinned_memory_resource const &, cuda::mr::device_accessible) noexcept
Enables the cuda::mr::device_accessible property.
Definition: pinned_memory_resource.hpp:125
void * allocate_async(std::size_t bytes, cuda_stream_view)
Pretend to support the allocate_async interface, falling back to stream 0.
Definition: pinned_memory_resource.hpp:102
pinned_memory_resource & operator=(pinned_memory_resource const &)=default
Default copy assignment operator.
pinned_memory_resource(pinned_memory_resource &&)=default
Default move constructor.
bool supports_streams() const noexcept
Query whether the pinned_memory_resource supports use of non-null CUDA streams for allocation/dealloc...
Definition: pinned_memory_resource.hpp:56
std::pair< std::size_t, std::size_t > get_mem_info(cuda_stream_view stream) const
Queries the amount of free and total memory for the resource.
Definition: pinned_memory_resource.hpp:73
bool supports_get_mem_info() const noexcept
Query whether the resource supports the get_mem_info API.
Definition: pinned_memory_resource.hpp:63
pinned_memory_resource & operator=(pinned_memory_resource &&)=default
Default move assignment operator.
void * allocate_async(std::size_t bytes, std::size_t alignment, cuda_stream_view)
Pretend to support the allocate_async interface, falling back to stream 0.
Definition: pinned_memory_resource.hpp:88
void deallocate_async(void *ptr, std::size_t bytes, std::size_t alignment, cuda_stream_view)
Pretend to support the deallocate_async interface, falling back to stream 0.
Definition: pinned_memory_resource.hpp:115
pinned_memory_resource(pinned_memory_resource const &)=default
Default copy constructor.