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/cccl_adaptors.hpp>
21 #include <rmm/detail/cuda_memory_resource.hpp>
22 #include <rmm/detail/error.hpp>
23 #include <rmm/detail/export.hpp>
24 #include <rmm/detail/nvtx/ranges.hpp>
25 
26 #include <cstddef>
27 
28 namespace RMM_NAMESPACE {
29 namespace mr {
95  public:
96  device_memory_resource() = default;
97  virtual ~device_memory_resource() = default;
100  default;
102  default;
104  default;
105 
124  void* allocate(std::size_t bytes, cuda_stream_view stream = cuda_stream_view{})
125  {
126  RMM_FUNC_RANGE();
127  return do_allocate(bytes, stream);
128  }
129 
146  void deallocate(void* ptr,
147  std::size_t bytes,
148  cuda_stream_view stream = cuda_stream_view{}) noexcept
149  {
150  RMM_FUNC_RANGE();
151  do_deallocate(ptr, bytes, stream);
152  }
153 
167  [[nodiscard]] bool is_equal(device_memory_resource const& other) const noexcept
168  {
169  return do_is_equal(other);
170  }
171 
187  void* allocate(std::size_t bytes, std::size_t alignment)
188  {
189  RMM_FUNC_RANGE();
190  return do_allocate(rmm::align_up(bytes, alignment), cuda_stream_view{});
191  }
192 
206  void deallocate(void* ptr, std::size_t bytes, std::size_t alignment) noexcept
207  {
208  RMM_FUNC_RANGE();
209  do_deallocate(ptr, rmm::align_up(bytes, alignment), cuda_stream_view{});
210  }
211 
228  void* allocate_async(std::size_t bytes, std::size_t alignment, cuda_stream_view stream)
229  {
230  RMM_FUNC_RANGE();
231  return do_allocate(rmm::align_up(bytes, alignment), stream);
232  }
233 
249  void* allocate_async(std::size_t bytes, cuda_stream_view stream)
250  {
251  RMM_FUNC_RANGE();
252  return do_allocate(bytes, stream);
253  }
254 
269  void deallocate_async(void* ptr,
270  std::size_t bytes,
271  std::size_t alignment,
272  cuda_stream_view stream) noexcept
273  {
274  RMM_FUNC_RANGE();
275  do_deallocate(ptr, rmm::align_up(bytes, alignment), stream);
276  }
277 
291  void deallocate_async(void* ptr, std::size_t bytes, cuda_stream_view stream) noexcept
292  {
293  RMM_FUNC_RANGE();
294  do_deallocate(ptr, bytes, stream);
295  }
296 
297 #if CCCL_MAJOR_VERSION > 3 || (CCCL_MAJOR_VERSION == 3 && CCCL_MINOR_VERSION >= 1)
298  // CCCL >= 3.1 needs a different set of methods to satisfy the memory resource concepts
299 
312  void* allocate_sync(std::size_t bytes, std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT)
313  {
314  RMM_EXPECTS(alignment <= 256 && rmm::is_supported_alignment(alignment),
315  "Alignment must be less than or equal to 256 and a power of two");
316  return do_allocate(rmm::align_up(bytes, alignment), cuda_stream_view{});
317  }
318 
327  void deallocate_sync(void* ptr,
328  std::size_t bytes,
329  std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
330  {
331  do_deallocate(ptr, rmm::align_up(bytes, alignment), cuda_stream_view{});
332  }
333 
347  void* allocate(cuda_stream_view stream,
348  std::size_t bytes,
349  std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT)
350  {
351  RMM_EXPECTS(alignment <= 256 && rmm::is_supported_alignment(alignment),
352  "Alignment must be less than or equal to 256 and a power of two");
353  return do_allocate(rmm::align_up(bytes, alignment), stream);
354  }
355 
365  void deallocate(cuda_stream_view stream,
366  void* ptr,
367  std::size_t bytes,
368  std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
369  {
370  do_deallocate(ptr, rmm::align_up(bytes, alignment), stream);
371  }
372 #endif // CCCL >= 3.1
373 
381  [[nodiscard]] bool operator==(device_memory_resource const& other) const noexcept
382  {
383  return do_is_equal(other);
384  }
385 
393  [[nodiscard]] bool operator!=(device_memory_resource const& other) const noexcept
394  {
395  return !do_is_equal(other);
396  }
397 
403  friend void get_property(device_memory_resource const&, cuda::mr::device_accessible) noexcept {}
404 
405  private:
418  virtual void* do_allocate(std::size_t bytes, cuda_stream_view stream) = 0;
419 
431  virtual void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) noexcept = 0;
432 
447  [[nodiscard]] virtual bool do_is_equal(device_memory_resource const& other) const noexcept
448  {
449  return this == &other;
450  }
451 };
452 
453 // static property checks
454 static_assert(rmm::detail::polyfill::resource<device_memory_resource>);
455 static_assert(rmm::detail::polyfill::async_resource<device_memory_resource>);
456 static_assert(
457  rmm::detail::polyfill::resource_with<device_memory_resource, cuda::mr::device_accessible>);
458 static_assert(
459  rmm::detail::polyfill::async_resource_with<device_memory_resource, cuda::mr::device_accessible>);
460  // end of group
462 } // namespace mr
463 } // 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:94
void deallocate(void *ptr, std::size_t bytes, std::size_t alignment) noexcept
Deallocate memory pointed to by p.
Definition: device_memory_resource.hpp:206
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:403
device_memory_resource(device_memory_resource &&) noexcept=default
Default move constructor.
void deallocate_async(void *ptr, std::size_t bytes, cuda_stream_view stream) noexcept
Deallocate memory pointed to by p.
Definition: device_memory_resource.hpp:291
void deallocate_async(void *ptr, std::size_t bytes, std::size_t alignment, cuda_stream_view stream) noexcept
Deallocate memory pointed to by p.
Definition: device_memory_resource.hpp:269
void * allocate(std::size_t bytes, std::size_t alignment)
Allocates memory of size at least bytes.
Definition: device_memory_resource.hpp:187
void * allocate_async(std::size_t bytes, cuda_stream_view stream)
Allocates memory of size at least bytes.
Definition: device_memory_resource.hpp:249
void deallocate(void *ptr, std::size_t bytes, cuda_stream_view stream=cuda_stream_view{}) noexcept
Deallocate memory pointed to by p.
Definition: device_memory_resource.hpp:146
bool operator==(device_memory_resource const &other) const noexcept
Comparison operator with another device_memory_resource.
Definition: device_memory_resource.hpp:381
bool operator!=(device_memory_resource const &other) const noexcept
Comparison operator with another device_memory_resource.
Definition: device_memory_resource.hpp:393
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:228
bool is_equal(device_memory_resource const &other) const noexcept
Compare this resource to another.
Definition: device_memory_resource.hpp:167
static constexpr std::size_t CUDA_ALLOCATION_ALIGNMENT
Default alignment used for CUDA memory allocation.
Definition: aligned.hpp:43
bool is_supported_alignment(std::size_t alignment) noexcept
Returns whether or not alignment is a valid memory alignment.
std::size_t align_up(std::size_t value, std::size_t alignment) noexcept
Align up to nearest multiple of specified power of 2.