device_memory_resource.hpp
1 /*
2  * Copyright (c) 2019, 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 <cstddef>
22 #include <utility>
23 
24 namespace rmm {
25 
26 namespace mr {
84  public:
85  virtual ~device_memory_resource() = default;
86 
102  void* allocate(std::size_t bytes, cuda_stream_view stream = cuda_stream_view{})
103  {
104  return do_allocate(rmm::detail::align_up(bytes, 8), stream);
105  }
106 
125  void deallocate(void* p, std::size_t bytes, cuda_stream_view stream = cuda_stream_view{})
126  {
127  do_deallocate(p, rmm::detail::align_up(bytes, 8), stream);
128  }
129 
143  bool is_equal(device_memory_resource const& other) const noexcept { return do_is_equal(other); }
144 
151  virtual bool supports_streams() const noexcept = 0;
152 
158  virtual bool supports_get_mem_info() const noexcept = 0;
159 
168  std::pair<std::size_t, std::size_t> get_mem_info(cuda_stream_view stream) const
169  {
170  return do_get_mem_info(stream);
171  }
172 
173  private:
186  virtual void* do_allocate(std::size_t bytes, cuda_stream_view stream) = 0;
187 
199  virtual void do_deallocate(void* p, std::size_t bytes, cuda_stream_view stream) = 0;
200 
215  virtual bool do_is_equal(device_memory_resource const& other) const noexcept
216  {
217  return this == &other;
218  }
219 
228  virtual std::pair<std::size_t, std::size_t> do_get_mem_info(cuda_stream_view stream) const = 0;
229 };
230 } // namespace mr
231 } // namespace rmm
rmm::mr::device_memory_resource::supports_streams
virtual bool supports_streams() const noexcept=0
Query whether the resource supports use of non-null CUDA streams for allocation/deallocation.
rmm::mr::device_memory_resource::allocate
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:102
rmm::cuda_stream_view
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:34
rmm::mr::device_memory_resource::deallocate
void deallocate(void *p, std::size_t bytes, cuda_stream_view stream=cuda_stream_view{})
Deallocate memory pointed to by p.
Definition: device_memory_resource.hpp:125
rmm::mr::device_memory_resource::is_equal
bool is_equal(device_memory_resource const &other) const noexcept
Compare this resource to another.
Definition: device_memory_resource.hpp:143
rmm::mr::device_memory_resource::supports_get_mem_info
virtual bool supports_get_mem_info() const noexcept=0
Query whether the resource supports the get_mem_info API.
rmm::mr::device_memory_resource::get_mem_info
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:168
rmm::mr::device_memory_resource
Base class for all libcudf device memory allocation.
Definition: device_memory_resource.hpp:83