All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
device_memory_resource.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2024, 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/export.hpp>
21 #include <rmm/detail/nvtx/ranges.hpp>
22 
23 #include <cuda/memory_resource>
24 
25 #include <cstddef>
26 #include <utility>
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 
121  void* allocate(std::size_t bytes, cuda_stream_view stream = cuda_stream_view{})
122  {
123  RMM_FUNC_RANGE();
124  return do_allocate(bytes, stream);
125  }
126 
143  void deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream = cuda_stream_view{})
144  {
145  RMM_FUNC_RANGE();
146  do_deallocate(ptr, bytes, stream);
147  }
148 
162  [[nodiscard]] bool is_equal(device_memory_resource const& other) const noexcept
163  {
164  return do_is_equal(other);
165  }
166 
179  void* allocate(std::size_t bytes, std::size_t alignment)
180  {
181  RMM_FUNC_RANGE();
182  return do_allocate(rmm::align_up(bytes, alignment), cuda_stream_view{});
183  }
184 
198  void deallocate(void* ptr, std::size_t bytes, std::size_t alignment)
199  {
200  RMM_FUNC_RANGE();
201  do_deallocate(ptr, rmm::align_up(bytes, alignment), cuda_stream_view{});
202  }
203 
217  void* allocate_async(std::size_t bytes, std::size_t alignment, cuda_stream_view stream)
218  {
219  RMM_FUNC_RANGE();
220  return do_allocate(rmm::align_up(bytes, alignment), stream);
221  }
222 
235  void* allocate_async(std::size_t bytes, cuda_stream_view stream)
236  {
237  RMM_FUNC_RANGE();
238  return do_allocate(bytes, stream);
239  }
240 
255  void deallocate_async(void* ptr,
256  std::size_t bytes,
257  std::size_t alignment,
258  cuda_stream_view stream)
259  {
260  RMM_FUNC_RANGE();
261  do_deallocate(ptr, rmm::align_up(bytes, alignment), stream);
262  }
263 
277  void deallocate_async(void* ptr, std::size_t bytes, cuda_stream_view stream)
278  {
279  RMM_FUNC_RANGE();
280  do_deallocate(ptr, bytes, stream);
281  }
282 
290  [[nodiscard]] bool operator==(device_memory_resource const& other) const noexcept
291  {
292  return do_is_equal(other);
293  }
294 
302  [[nodiscard]] bool operator!=(device_memory_resource const& other) const noexcept
303  {
304  return !do_is_equal(other);
305  }
306 
312  friend void get_property(device_memory_resource const&, cuda::mr::device_accessible) noexcept {}
313 
314  private:
327  virtual void* do_allocate(std::size_t bytes, cuda_stream_view stream) = 0;
328 
340  virtual void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) = 0;
341 
356  [[nodiscard]] virtual bool do_is_equal(device_memory_resource const& other) const noexcept
357  {
358  return this == &other;
359  }
360 };
361 static_assert(cuda::mr::async_resource_with<device_memory_resource, cuda::mr::device_accessible>); // end of group
363 } // namespace mr
364 } // namespace RMM_NAMESPACE
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:41
Base class for all libcudf 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:277
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:312
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:198
void * allocate(std::size_t bytes, std::size_t alignment)
Allocates memory of size at least bytes.
Definition: device_memory_resource.hpp:179
void * allocate_async(std::size_t bytes, cuda_stream_view stream)
Allocates memory of size at least bytes.
Definition: device_memory_resource.hpp:235
bool operator==(device_memory_resource const &other) const noexcept
Comparison operator with another device_memory_resource.
Definition: device_memory_resource.hpp:290
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:143
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:255
bool operator!=(device_memory_resource const &other) const noexcept
Comparison operator with another device_memory_resource.
Definition: device_memory_resource.hpp:302
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:217
bool is_equal(device_memory_resource const &other) const noexcept
Compare this resource to another.
Definition: device_memory_resource.hpp:162
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