device_memory_resource.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2025, 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/cuda_memory_resource.hpp>
21 #include <rmm/detail/export.hpp>
22 #include <rmm/detail/nvtx/ranges.hpp>
23 
24 #include <cstddef>
25 
26 namespace RMM_NAMESPACE {
27 namespace mr {
93  public:
94  device_memory_resource() = default;
95  virtual ~device_memory_resource() = default;
98  default;
100  default;
102  default;
103 
119  void* allocate(std::size_t bytes, cuda_stream_view stream = cuda_stream_view{})
120  {
121  RMM_FUNC_RANGE();
122  return do_allocate(bytes, stream);
123  }
124 
141  void deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream = cuda_stream_view{})
142  {
143  RMM_FUNC_RANGE();
144  do_deallocate(ptr, bytes, stream);
145  }
146 
160  [[nodiscard]] bool is_equal(device_memory_resource const& other) const noexcept
161  {
162  return do_is_equal(other);
163  }
164 
177  void* allocate(std::size_t bytes, std::size_t alignment)
178  {
179  RMM_FUNC_RANGE();
180  return do_allocate(rmm::align_up(bytes, alignment), cuda_stream_view{});
181  }
182 
196  void deallocate(void* ptr, std::size_t bytes, std::size_t alignment)
197  {
198  RMM_FUNC_RANGE();
199  do_deallocate(ptr, rmm::align_up(bytes, alignment), cuda_stream_view{});
200  }
201 
215  void* allocate_async(std::size_t bytes, std::size_t alignment, cuda_stream_view stream)
216  {
217  RMM_FUNC_RANGE();
218  return do_allocate(rmm::align_up(bytes, alignment), stream);
219  }
220 
233  void* allocate_async(std::size_t bytes, cuda_stream_view stream)
234  {
235  RMM_FUNC_RANGE();
236  return do_allocate(bytes, stream);
237  }
238 
253  void deallocate_async(void* ptr,
254  std::size_t bytes,
255  std::size_t alignment,
256  cuda_stream_view stream)
257  {
258  RMM_FUNC_RANGE();
259  do_deallocate(ptr, rmm::align_up(bytes, alignment), stream);
260  }
261 
275  void deallocate_async(void* ptr, std::size_t bytes, cuda_stream_view stream)
276  {
277  RMM_FUNC_RANGE();
278  do_deallocate(ptr, bytes, stream);
279  }
280 
288  [[nodiscard]] bool operator==(device_memory_resource const& other) const noexcept
289  {
290  return do_is_equal(other);
291  }
292 
300  [[nodiscard]] bool operator!=(device_memory_resource const& other) const noexcept
301  {
302  return !do_is_equal(other);
303  }
304 
310  friend void get_property(device_memory_resource const&, cuda::mr::device_accessible) noexcept {}
311 
312  private:
325  virtual void* do_allocate(std::size_t bytes, cuda_stream_view stream) = 0;
326 
338  virtual void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) = 0;
339 
354  [[nodiscard]] virtual bool do_is_equal(device_memory_resource const& other) const noexcept
355  {
356  return this == &other;
357  }
358 };
359 static_assert(cuda::mr::async_resource_with<device_memory_resource, cuda::mr::device_accessible>); // end of group
361 } // namespace mr
362 } // namespace RMM_NAMESPACE
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:39
Base class for all librmm device memory allocation.
Definition: device_memory_resource.hpp:92
void deallocate_async(void *ptr, std::size_t bytes, cuda_stream_view stream)
Deallocate memory pointed to by p.
Definition: device_memory_resource.hpp:275
friend void get_property(device_memory_resource const &, cuda::mr::device_accessible) noexcept
Enables the cuda::mr::device_accessible property.
Definition: device_memory_resource.hpp:310
device_memory_resource(device_memory_resource &&) noexcept=default
Default move constructor.
void deallocate(void *ptr, std::size_t bytes, std::size_t alignment)
Deallocate memory pointed to by p.
Definition: device_memory_resource.hpp:196
void * allocate(std::size_t bytes, std::size_t alignment)
Allocates memory of size at least bytes.
Definition: device_memory_resource.hpp:177
void * allocate_async(std::size_t bytes, cuda_stream_view stream)
Allocates memory of size at least bytes.
Definition: device_memory_resource.hpp:233
bool operator==(device_memory_resource const &other) const noexcept
Comparison operator with another device_memory_resource.
Definition: device_memory_resource.hpp:288
void deallocate(void *ptr, std::size_t bytes, cuda_stream_view stream=cuda_stream_view{})
Deallocate memory pointed to by p.
Definition: device_memory_resource.hpp:141
void deallocate_async(void *ptr, std::size_t bytes, std::size_t alignment, cuda_stream_view stream)
Deallocate memory pointed to by p.
Definition: device_memory_resource.hpp:253
bool operator!=(device_memory_resource const &other) const noexcept
Comparison operator with another device_memory_resource.
Definition: device_memory_resource.hpp:300
device_memory_resource(device_memory_resource const &)=default
Default copy constructor.
void * allocate_async(std::size_t bytes, std::size_t alignment, cuda_stream_view stream)
Allocates memory of size at least bytes.
Definition: device_memory_resource.hpp:215
bool is_equal(device_memory_resource const &other) const noexcept
Compare this resource to another.
Definition: device_memory_resource.hpp:160
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