device_memory_resource.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2021, 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 
21 #include <cuda/memory_resource>
22 
23 #include <cstddef>
24 #include <utility>
25 
26 namespace rmm::mr {
90  public:
91  device_memory_resource() = default;
92  virtual ~device_memory_resource() = default;
95  default;
97  default;
99  default;
100 
116  void* allocate(std::size_t bytes, cuda_stream_view stream = cuda_stream_view{})
117  {
118  return do_allocate(bytes, stream);
119  }
120 
137  void deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream = cuda_stream_view{})
138  {
139  do_deallocate(ptr, bytes, stream);
140  }
141 
155  [[nodiscard]] bool is_equal(device_memory_resource const& other) const noexcept
156  {
157  return do_is_equal(other);
158  }
159 
172  void* allocate(std::size_t bytes, std::size_t alignment)
173  {
174  return do_allocate(rmm::detail::align_up(bytes, alignment), cuda_stream_view{});
175  }
176 
190  void deallocate(void* ptr, std::size_t bytes, std::size_t alignment)
191  {
192  do_deallocate(ptr, rmm::detail::align_up(bytes, alignment), cuda_stream_view{});
193  }
194 
208  void* allocate_async(std::size_t bytes, std::size_t alignment, cuda_stream_view stream)
209  {
210  return do_allocate(rmm::detail::align_up(bytes, alignment), stream);
211  }
212 
225  void* allocate_async(std::size_t bytes, cuda_stream_view stream)
226  {
227  return do_allocate(bytes, stream);
228  }
229 
244  void deallocate_async(void* ptr,
245  std::size_t bytes,
246  std::size_t alignment,
247  cuda_stream_view stream)
248  {
249  do_deallocate(ptr, rmm::detail::align_up(bytes, alignment), stream);
250  }
251 
265  void deallocate_async(void* ptr, std::size_t bytes, cuda_stream_view stream)
266  {
267  do_deallocate(ptr, bytes, stream);
268  }
269 
277  [[nodiscard]] bool operator==(device_memory_resource const& other) const noexcept
278  {
279  return do_is_equal(other);
280  }
281 
289  [[nodiscard]] bool operator!=(device_memory_resource const& other) const noexcept
290  {
291  return !do_is_equal(other);
292  }
293 
300  [[nodiscard]] virtual bool supports_streams() const noexcept = 0;
301 
307  [[nodiscard]] virtual bool supports_get_mem_info() const noexcept = 0;
308 
317  [[nodiscard]] std::pair<std::size_t, std::size_t> get_mem_info(cuda_stream_view stream) const
318  {
319  return do_get_mem_info(stream);
320  }
321 
327  friend void get_property(device_memory_resource const&, cuda::mr::device_accessible) noexcept {}
328 
329  private:
342  virtual void* do_allocate(std::size_t bytes, cuda_stream_view stream) = 0;
343 
355  virtual void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) = 0;
356 
371  [[nodiscard]] virtual bool do_is_equal(device_memory_resource const& other) const noexcept
372  {
373  return this == &other;
374  }
375 
384  [[nodiscard]] virtual std::pair<std::size_t, std::size_t> do_get_mem_info(
385  cuda_stream_view stream) const = 0;
386 };
387 static_assert(cuda::mr::async_resource_with<device_memory_resource, cuda::mr::device_accessible>); // end of group
389 } // namespace rmm::mr
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:89
std::pair< std::size_t, std::size_t > get_mem_info(cuda_stream_view stream) const
Queries the amount of free and total memory for the resource.
Definition: device_memory_resource.hpp:317
virtual bool supports_streams() const noexcept=0
Query whether the resource supports use of non-null CUDA streams for allocation/deallocation.
void deallocate_async(void *ptr, std::size_t bytes, cuda_stream_view stream)
Deallocate memory pointed to by p.
Definition: device_memory_resource.hpp:265
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:327
device_memory_resource(device_memory_resource &&) noexcept=default
Default move constructor.
void * allocate(std::size_t bytes, cuda_stream_view stream=cuda_stream_view{})
Allocates memory of size at least bytes.
Definition: device_memory_resource.hpp:116
void deallocate(void *ptr, std::size_t bytes, std::size_t alignment)
Deallocate memory pointed to by p.
Definition: device_memory_resource.hpp:190
void * allocate(std::size_t bytes, std::size_t alignment)
Allocates memory of size at least bytes.
Definition: device_memory_resource.hpp:172
void * allocate_async(std::size_t bytes, cuda_stream_view stream)
Allocates memory of size at least bytes.
Definition: device_memory_resource.hpp:225
bool operator==(device_memory_resource const &other) const noexcept
Comparison operator with another device_memory_resource.
Definition: device_memory_resource.hpp:277
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:137
virtual bool supports_get_mem_info() const noexcept=0
Query whether the resource supports the get_mem_info API.
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:244
bool operator!=(device_memory_resource const &other) const noexcept
Comparison operator with another device_memory_resource.
Definition: device_memory_resource.hpp:289
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:208
bool is_equal(device_memory_resource const &other) const noexcept
Compare this resource to another.
Definition: device_memory_resource.hpp:155