RMM  23.12
RAPIDS Memory Manager
callback_memory_resource.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022, 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 
19 
20 #include <cstddef>
21 #include <functional>
22 #include <utility>
23 
24 namespace rmm::mr {
46 using allocate_callback_t = std::function<void*(std::size_t, cuda_stream_view, void*)>;
47 
64 using deallocate_callback_t = std::function<void(void*, std::size_t, cuda_stream_view, void*)>;
65 
71  public:
88  deallocate_callback_t deallocate_callback,
89  void* allocate_callback_arg = nullptr,
90  void* deallocate_callback_arg = nullptr) noexcept
91  : allocate_callback_(allocate_callback),
92  deallocate_callback_(deallocate_callback),
93  allocate_callback_arg_(allocate_callback_arg),
94  deallocate_callback_arg_(deallocate_callback_arg)
95  {
96  }
97 
98  callback_memory_resource() = delete;
99  ~callback_memory_resource() override = default;
101  callback_memory_resource& operator=(callback_memory_resource const&) = delete;
103  default;
105  default;
106 
107  private:
120  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
121  {
122  return allocate_callback_(bytes, stream, allocate_callback_arg_);
123  }
124 
136  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) override
137  {
138  deallocate_callback_(ptr, bytes, stream, deallocate_callback_arg_);
139  }
140 
141  [[nodiscard]] std::pair<std::size_t, std::size_t> do_get_mem_info(cuda_stream_view) const override
142  {
143  throw std::runtime_error("cannot get free / total memory");
144  }
145 
146  [[nodiscard]] bool supports_streams() const noexcept override { return false; }
147  [[nodiscard]] bool supports_get_mem_info() const noexcept override { return false; }
148 
149  allocate_callback_t allocate_callback_;
150  deallocate_callback_t deallocate_callback_;
151  void* allocate_callback_arg_;
152  void* deallocate_callback_arg_;
153 };
154  // end of group
156 } // namespace rmm::mr
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:41
A device memory resource that uses the provided callbacks for memory allocation and deallocation.
Definition: callback_memory_resource.hpp:70
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:87
Base class for all libcudf device memory allocation.
Definition: device_memory_resource.hpp:89
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:46
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:64