callback_memory_resource.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2022-2025, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
6 
7 #include <rmm/detail/export.hpp>
9 
10 #include <cstddef>
11 #include <functional>
12 #include <utility>
13 
14 namespace RMM_NAMESPACE {
15 namespace mr {
37 using allocate_callback_t = std::function<void*(std::size_t, cuda_stream_view, void*)>;
38 
55 using deallocate_callback_t = std::function<void(void*, std::size_t, cuda_stream_view, void*)>;
56 
62  public:
79  allocate_callback_t allocate_callback,
80  deallocate_callback_t deallocate_callback,
81  void* allocate_callback_arg = nullptr, // NOLINT(bugprone-easily-swappable-parameters)
82  void* deallocate_callback_arg = nullptr) noexcept
83  : allocate_callback_(std::move(allocate_callback)),
84  deallocate_callback_(std::move(deallocate_callback)),
85  allocate_callback_arg_(allocate_callback_arg),
86  deallocate_callback_arg_(deallocate_callback_arg)
87  {
88  }
89 
90  callback_memory_resource() = delete;
91  ~callback_memory_resource() override = default;
93  callback_memory_resource& operator=(callback_memory_resource const&) = delete;
95  default;
97  default;
98 
99  private:
112  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
113  {
114  return allocate_callback_(bytes, stream, allocate_callback_arg_);
115  }
116 
128  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) noexcept override
129  {
130  deallocate_callback_(ptr, bytes, stream, deallocate_callback_arg_);
131  }
132 
133  allocate_callback_t allocate_callback_;
134  deallocate_callback_t deallocate_callback_;
135  void* allocate_callback_arg_;
136  void* deallocate_callback_arg_;
137 };
138  // end of group
140 } // namespace mr
141 } // namespace RMM_NAMESPACE
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:28
A device memory resource that uses the provided callbacks for memory allocation and deallocation.
Definition: callback_memory_resource.hpp:61
callback_memory_resource(callback_memory_resource &&) noexcept=default
Default move constructor.
callback_memory_resource(allocate_callback_t allocate_callback, deallocate_callback_t deallocate_callback, void *allocate_callback_arg=nullptr, void *deallocate_callback_arg=nullptr) noexcept
Construct a new callback memory resource.
Definition: callback_memory_resource.hpp:78
Base class for all librmm device memory allocation.
Definition: device_memory_resource.hpp:83
std::function< void *(std::size_t, cuda_stream_view, void *)> allocate_callback_t
Callback function type used by callback memory resource for allocation.
Definition: callback_memory_resource.hpp:37
std::function< void(void *, std::size_t, cuda_stream_view, void *)> deallocate_callback_t
Callback function type used by callback_memory_resource for deallocation.
Definition: callback_memory_resource.hpp:55