RMM  23.12
RAPIDS Memory Manager
cuda_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 
19 
20 #include <rmm/cuda_stream_view.hpp>
21 #include <rmm/detail/error.hpp>
22 
23 #include <cstddef>
24 
25 namespace rmm::mr {
36  public:
37  cuda_memory_resource() = default;
38  ~cuda_memory_resource() override = default;
42  default;
44  default;
45 
52  [[nodiscard]] bool supports_streams() const noexcept override { return false; }
53 
59  [[nodiscard]] bool supports_get_mem_info() const noexcept override { return true; }
60 
61  private:
73  void* do_allocate(std::size_t bytes, [[maybe_unused]] cuda_stream_view stream) override
74  {
75  void* ptr{nullptr};
76  RMM_CUDA_TRY_ALLOC(cudaMalloc(&ptr, bytes));
77  return ptr;
78  }
79 
90  void do_deallocate(void* ptr,
91  [[maybe_unused]] std::size_t bytes,
92  [[maybe_unused]] cuda_stream_view stream) override
93  {
94  RMM_ASSERT_CUDA_SUCCESS(cudaFree(ptr));
95  }
96 
107  [[nodiscard]] bool do_is_equal(device_memory_resource const& other) const noexcept override
108  {
109  return dynamic_cast<cuda_memory_resource const*>(&other) != nullptr;
110  }
111 
119  [[nodiscard]] std::pair<std::size_t, std::size_t> do_get_mem_info(cuda_stream_view) const override
120  {
121  std::size_t free_size{};
122  std::size_t total_size{};
123  RMM_CUDA_TRY(cudaMemGetInfo(&free_size, &total_size));
124  return std::make_pair(free_size, total_size);
125  }
126 }; // end of group
128 } // namespace rmm::mr
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:41
device_memory_resource derived class that uses cudaMalloc/Free for allocation/deallocation.
Definition: cuda_memory_resource.hpp:35
cuda_memory_resource(cuda_memory_resource const &)=default
Default copy constructor.
bool supports_get_mem_info() const noexcept override
Query whether the resource supports the get_mem_info API.
Definition: cuda_memory_resource.hpp:59
cuda_memory_resource & operator=(cuda_memory_resource const &)=default
Default copy assignment operator.
cuda_memory_resource & operator=(cuda_memory_resource &&)=default
Default move assignment operator.
cuda_memory_resource(cuda_memory_resource &&)=default
Default move constructor.
bool supports_streams() const noexcept override
Query whether the resource supports use of non-null CUDA streams for allocation/deallocation....
Definition: cuda_memory_resource.hpp:52
Base class for all libcudf device memory allocation.
Definition: device_memory_resource.hpp:89