All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
pinned_memory_resource.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020-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/aligned.hpp>
19 #include <rmm/cuda_stream_view.hpp>
20 #include <rmm/detail/aligned.hpp>
21 #include <rmm/detail/error.hpp>
22 #include <rmm/detail/export.hpp>
24 
25 #include <cstddef>
26 #include <utility>
27 
28 namespace RMM_NAMESPACE {
29 namespace mr {
43  public:
44  pinned_memory_resource() = default;
45  ~pinned_memory_resource() override = default;
49  default;
51  default;
52 
63  [[nodiscard]] void* allocate_async(std::size_t bytes, std::size_t alignment, cuda_stream_view)
64  {
65  return do_allocate(bytes, alignment);
66  }
67 
77  [[nodiscard]] void* allocate_async(std::size_t bytes, cuda_stream_view)
78  {
79  return do_allocate(bytes);
80  }
81 
90  void deallocate_async(void* ptr, std::size_t bytes, std::size_t alignment, cuda_stream_view)
91  {
92  do_deallocate(ptr, rmm::align_up(bytes, alignment));
93  }
94 
100  friend void get_property(pinned_memory_resource const&, cuda::mr::device_accessible) noexcept {}
101 
102  private:
115  void* do_allocate(std::size_t bytes, std::size_t alignment = alignof(std::max_align_t)) override
116  {
117  // don't allocate anything if the user requested zero bytes
118  if (0 == bytes) { return nullptr; }
119 
120  // If the requested alignment isn't supported, use default
121  alignment =
123 
124  return rmm::detail::aligned_host_allocate(bytes, alignment, [](std::size_t size) {
125  void* ptr{nullptr};
126  auto status = cudaMallocHost(&ptr, size);
127  if (cudaSuccess != status) { throw std::bad_alloc{}; }
128  return ptr;
129  });
130  }
131 
145  void do_deallocate(void* ptr,
146  std::size_t bytes,
147  std::size_t alignment = alignof(std::max_align_t)) override
148  {
149  if (nullptr == ptr) { return; }
150  rmm::detail::aligned_host_deallocate(
151  ptr, bytes, alignment, [](void* ptr) { RMM_ASSERT_CUDA_SUCCESS(cudaFreeHost(ptr)); });
152  }
153 };
154 static_assert(cuda::mr::async_resource_with<pinned_memory_resource,
155  cuda::mr::host_accessible,
156  cuda::mr::device_accessible>); // end of group
158 } // namespace mr
159 } // namespace RMM_NAMESPACE
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:58
A host_memory_resource that uses cudaMallocHost to allocate pinned/page-locked host memory.
Definition: pinned_memory_resource.hpp:42
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:100
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:77
pinned_memory_resource & operator=(pinned_memory_resource const &)=default
Default copy assignment operator.
pinned_memory_resource(pinned_memory_resource &&)=default
Default move constructor.
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:63
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:90
pinned_memory_resource(pinned_memory_resource const &)=default
Default copy constructor.
static constexpr std::size_t RMM_DEFAULT_HOST_ALIGNMENT
Default alignment used for host memory allocated by RMM.
Definition: aligned.hpp:37
constexpr bool is_supported_alignment(std::size_t alignment) noexcept
Returns whether or not alignment is a valid memory alignment.
Definition: aligned.hpp:64
constexpr std::size_t align_up(std::size_t value, std::size_t alignment) noexcept
Align up to nearest multiple of specified power of 2.
Definition: aligned.hpp:77