RMM  23.12
RAPIDS Memory Manager
thread_safe_resource_adaptor.hpp
Go to the documentation of this file.
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>
21 
22 #include <cstddef>
23 #include <mutex>
24 
25 namespace rmm::mr {
40 template <typename Upstream>
42  public:
43  using lock_t = std::lock_guard<std::mutex>;
44 
55  thread_safe_resource_adaptor(Upstream* upstream) : upstream_{upstream}
56  {
57  RMM_EXPECTS(nullptr != upstream, "Unexpected null upstream resource pointer.");
58  }
59 
61  ~thread_safe_resource_adaptor() override = default;
66 
72  Upstream* get_upstream() const noexcept { return upstream_; }
73 
77  bool supports_streams() const noexcept override { return upstream_->supports_streams(); }
78 
84  bool supports_get_mem_info() const noexcept override
85  {
86  return upstream_->supports_get_mem_info();
87  }
88 
89  private:
101  void* do_allocate(std::size_t bytes, cuda_stream_view stream) override
102  {
103  lock_t lock(mtx);
104  return upstream_->allocate(bytes, stream);
105  }
106 
114  void do_deallocate(void* ptr, std::size_t bytes, cuda_stream_view stream) override
115  {
116  lock_t lock(mtx);
117  upstream_->deallocate(ptr, bytes, stream);
118  }
119 
127  bool do_is_equal(device_memory_resource const& other) const noexcept override
128  {
129  if (this == &other) { return true; }
130  auto thread_safe_other = dynamic_cast<thread_safe_resource_adaptor<Upstream> const*>(&other);
131  if (thread_safe_other != nullptr) {
132  return upstream_->is_equal(*thread_safe_other->get_upstream());
133  }
134  return upstream_->is_equal(other);
135  }
136 
145  std::pair<std::size_t, std::size_t> do_get_mem_info(cuda_stream_view stream) const override
146  {
147  lock_t lock(mtx);
148  return upstream_->get_mem_info(stream);
149  }
150 
151  std::mutex mutable mtx; // mutex for thread safe access to upstream
152  Upstream* upstream_;
153 };
154  // end of group
156 } // 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
Resource that adapts Upstream memory resource adaptor to be thread safe.
Definition: thread_safe_resource_adaptor.hpp:41
std::lock_guard< std::mutex > lock_t
Type of lock used to synchronize access.
Definition: thread_safe_resource_adaptor.hpp:43
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:77
Upstream * get_upstream() const noexcept
Get the upstream memory resource.
Definition: thread_safe_resource_adaptor.hpp:72
bool supports_get_mem_info() const noexcept override
Query whether the resource supports the get_mem_info API.
Definition: thread_safe_resource_adaptor.hpp:84
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:55