thread_safe_resource_adaptor.hpp
1 /*
2  * Copyright (c) 2020-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/error.hpp>
20 #include <rmm/mr/device/device_memory_resource.hpp>
21 
22 #include <cstddef>
23 #include <mutex>
24 
25 namespace rmm::mr {
35 template <typename Upstream>
37  public:
38  using lock_t = std::lock_guard<std::mutex>;
39 
50  thread_safe_resource_adaptor(Upstream* upstream) : upstream_{upstream}
51  {
52  RMM_EXPECTS(nullptr != upstream, "Unexpected null upstream resource pointer.");
53  }
54 
55  thread_safe_resource_adaptor() = delete;
56  ~thread_safe_resource_adaptor() override = default;
57  thread_safe_resource_adaptor(thread_safe_resource_adaptor const&) = delete;
58  thread_safe_resource_adaptor(thread_safe_resource_adaptor&&) = delete;
59  thread_safe_resource_adaptor& operator=(thread_safe_resource_adaptor const&) = delete;
60  thread_safe_resource_adaptor& operator=(thread_safe_resource_adaptor&&) = delete;
61 
67  Upstream* get_upstream() const noexcept { return upstream_; }
68 
72  bool supports_streams() const noexcept override { return upstream_->supports_streams(); }
73 
79  bool supports_get_mem_info() const noexcept override
80  {
81  return upstream_->supports_get_mem_info();
82  }
83 
84  private:
96  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
97  {
98  lock_t lock(mtx);
99  return upstream_->allocate(bytes, stream);
100  }
101 
111  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) override
112  {
113  lock_t lock(mtx);
114  upstream_->deallocate(ptr, bytes, stream);
115  }
116 
126  bool do_is_equal(device_memory_resource const& other) const noexcept override
127  {
128  if (this == &other) { return true; }
129  auto thread_safe_other = dynamic_cast<thread_safe_resource_adaptor<Upstream> const*>(&other);
130  if (thread_safe_other != nullptr) {
131  return upstream_->is_equal(*thread_safe_other->get_upstream());
132  }
133  return upstream_->is_equal(other);
134  }
135 
144  std::pair<std::size_t, std::size_t> do_get_mem_info(cuda_stream_view stream) const override
145  {
146  lock_t lock(mtx);
147  return upstream_->get_mem_info(stream);
148  }
149 
150  std::mutex mutable mtx; // mutex for thread safe access to upstream
151  Upstream* upstream_;
152 };
153 
154 } // namespace rmm::mr
rmm::mr::thread_safe_resource_adaptor
Resource that adapts Upstream memory resource adaptor to be thread safe.
Definition: thread_safe_resource_adaptor.hpp:36
rmm::cuda_stream_view
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:34
rmm::mr::thread_safe_resource_adaptor::supports_streams
bool supports_streams() const noexcept override
Query whether the resource supports use of non-null CUDA streams for allocation/deallocation.
Definition: thread_safe_resource_adaptor.hpp:72
rmm::mr::thread_safe_resource_adaptor::thread_safe_resource_adaptor
thread_safe_resource_adaptor(Upstream *upstream)
Construct a new thread safe resource adaptor using upstream to satisfy allocation requests.
Definition: thread_safe_resource_adaptor.hpp:50
rmm::mr::thread_safe_resource_adaptor::get_upstream
Upstream * get_upstream() const noexcept
Get the upstream memory resource.
Definition: thread_safe_resource_adaptor.hpp:67
rmm::mr::device_memory_resource
Base class for all libcudf device memory allocation.
Definition: device_memory_resource.hpp:82
rmm::mr::thread_safe_resource_adaptor::supports_get_mem_info
bool supports_get_mem_info() const noexcept override
Query whether the resource supports the get_mem_info API.
Definition: thread_safe_resource_adaptor.hpp:79