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, std::size_t bytes, cuda_stream_view stream = cuda_stream_view{})
147  {
148  RMM_FUNC_RANGE();
149  do_deallocate(ptr, bytes, stream);
150  }
151 
165  [[nodiscard]] bool is_equal(device_memory_resource const& other) const noexcept
166  {
167  return do_is_equal(other);
168  }
169 
185  void* allocate(std::size_t bytes, std::size_t alignment)
186  {
187  RMM_FUNC_RANGE();
188  return do_allocate(rmm::align_up(bytes, alignment), cuda_stream_view{});
189  }
190 
204  void deallocate(void* ptr, std::size_t bytes, std::size_t alignment)
205  {
206  RMM_FUNC_RANGE();
207  do_deallocate(ptr, rmm::align_up(bytes, alignment), cuda_stream_view{});
208  }
209 
226  void* allocate_async(std::size_t bytes, std::size_t alignment, cuda_stream_view stream)
227  {
228  RMM_FUNC_RANGE();
229  return do_allocate(rmm::align_up(bytes, alignment), stream);
230  }
231 
247  void* allocate_async(std::size_t bytes, cuda_stream_view stream)
248  {
249  RMM_FUNC_RANGE();
250  return do_allocate(bytes, stream);
251  }
252 
267  void deallocate_async(void* ptr,
268  std::size_t bytes,
269  std::size_t alignment,
270  cuda_stream_view stream)
271  {
272  RMM_FUNC_RANGE();
273  do_deallocate(ptr, rmm::align_up(bytes, alignment), stream);
274  }
275 
289  void deallocate_async(void* ptr, std::size_t bytes, cuda_stream_view stream)
290  {
291  RMM_FUNC_RANGE();
292  do_deallocate(ptr, bytes, stream);
293  }
294 
295 #if CCCL_MAJOR_VERSION > 3 || (CCCL_MAJOR_VERSION == 3 && CCCL_MINOR_VERSION >= 1)
296  // CCCL >= 3.1 needs a different set of methods to satisfy the memory resource concepts
297 
310  void* allocate_sync(std::size_t bytes, std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT)
311  {
312  RMM_EXPECTS(alignment <= 256 && rmm::is_supported_alignment(alignment),
313  "Alignment must be less than or equal to 256 and a power of two");
314  return do_allocate(rmm::align_up(bytes, alignment), cuda_stream_view{});
315  }
316 
325  void deallocate_sync(void* ptr,
326  std::size_t bytes,
327  std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
328  {
329  do_deallocate(ptr, rmm::align_up(bytes, alignment), cuda_stream_view{});
330  }
331 
345  void* allocate(cuda_stream_view stream,
346  std::size_t bytes,
347  std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT)
348  {
349  RMM_EXPECTS(alignment <= 256 && rmm::is_supported_alignment(alignment),
350  "Alignment must be less than or equal to 256 and a power of two");
351  return do_allocate(rmm::align_up(bytes, alignment), stream);
352  }
353 
363  void deallocate(cuda_stream_view stream,
364  void* ptr,
365  std::size_t bytes,
366  std::size_t alignment = rmm::CUDA_ALLOCATION_ALIGNMENT) noexcept
367  {
368  do_deallocate(ptr, rmm::align_up(bytes, alignment), stream);
369  }
370 #endif // CCCL >= 3.1
371 
379  [[nodiscard]] bool operator==(device_memory_resource const& other) const noexcept
380  {
381  return do_is_equal(other);
382  }
383 
391  [[nodiscard]] bool operator!=(device_memory_resource const& other) const noexcept
392  {
393  return !do_is_equal(other);
394  }
395 
401  friend void get_property(device_memory_resource const&, cuda::mr::device_accessible) noexcept {}
402 
403  private:
416  virtual void* do_allocate(std::size_t bytes, cuda_stream_view stream) = 0;
417 
429  virtual void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) = 0;
430 
445  [[nodiscard]] virtual bool do_is_equal(device_memory_resource const& other) const noexcept
446  {
447  return this == &other;
448  }
449 };
450 
451 // static property checks
452 static_assert(rmm::detail::polyfill::resource<device_memory_resource>);
453 static_assert(rmm::detail::polyfill::async_resource<device_memory_resource>);
454 static_assert(
455  rmm::detail::polyfill::resource_with<device_memory_resource, cuda::mr::device_accessible>);
456 static_assert(
457  rmm::detail::polyfill::async_resource_with<device_memory_resource, cuda::mr::device_accessible>);
458  // end of group
460 } // namespace mr
461 } // 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_async(void *ptr, std::size_t bytes, cuda_stream_view stream)
Deallocate memory pointed to by p.
Definition: device_memory_resource.hpp:289
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:401
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:204
void * allocate(std::size_t bytes, std::size_t alignment)
Allocates memory of size at least bytes.
Definition: device_memory_resource.hpp:185
void * allocate_async(std::size_t bytes, cuda_stream_view stream)
Allocates memory of size at least bytes.
Definition: device_memory_resource.hpp:247
bool operator==(device_memory_resource const &other) const noexcept
Comparison operator with another device_memory_resource.
Definition: device_memory_resource.hpp:379
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:146
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:267
bool operator!=(device_memory_resource const &other) const noexcept
Comparison operator with another device_memory_resource.
Definition: device_memory_resource.hpp:391
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:226
bool is_equal(device_memory_resource const &other) const noexcept
Compare this resource to another.
Definition: device_memory_resource.hpp:165
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.