device_scalar.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 
17 #pragma once
18 
19 #include <rmm/cuda_stream_view.hpp>
20 #include <rmm/device_uvector.hpp>
23 
24 #include <type_traits>
25 
26 namespace rmm {
40 template <typename T>
42  public:
43  static_assert(std::is_trivially_copyable<T>::value, "Scalar type must be trivially copyable");
44 
48  using pointer =
52 
53  RMM_EXEC_CHECK_DISABLE
54  ~device_scalar() = default;
55 
56  RMM_EXEC_CHECK_DISABLE
57  device_scalar(device_scalar&&) noexcept = default;
58 
64  device_scalar& operator=(device_scalar&&) noexcept = default;
65 
69  device_scalar(device_scalar const&) = delete;
70 
74  device_scalar& operator=(device_scalar const&) = delete;
75 
79  device_scalar() = delete;
80 
95  explicit device_scalar(
97  rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource())
98  : _storage{1, stream, mr}
99  {
100  }
101 
118  explicit device_scalar(
119  value_type const& initial_value,
122  : _storage{1, stream, mr}
123  {
124  set_value_async(initial_value, stream);
125  }
126 
142  : _storage{other._storage, stream, mr}
143  {
144  }
145 
163  {
164  return _storage.front_element(stream);
165  }
166 
205  {
206  _storage.set_element_async(0, value, stream);
207  }
208 
209  // Disallow passing literals to set_value to avoid race conditions where the memory holding the
210  // literal can be freed before the async memcpy / memset executes.
211  void set_value_async(value_type&&, cuda_stream_view) = delete;
212 
228  {
229  _storage.set_element_to_zero_async(value_type{0}, stream);
230  }
231 
242  [[nodiscard]] pointer data() noexcept { return static_cast<pointer>(_storage.data()); }
243 
254  [[nodiscard]] const_pointer data() const noexcept
255  {
256  return static_cast<const_pointer>(_storage.data());
257  }
258 
262  [[nodiscard]] cuda_stream_view stream() const noexcept { return _storage.stream(); }
263 
269  void set_stream(cuda_stream_view stream) noexcept { _storage.set_stream(stream); }
270 
271  private:
272  rmm::device_uvector<T> _storage;
273 };
274  // end of group
276 } // namespace rmm
Strongly-typed non-owning wrapper for CUDA streams with default constructor.
Definition: cuda_stream_view.hpp:41
Container for a single object of type T in device memory.
Definition: device_scalar.hpp:41
typename device_uvector< T >::value_type value_type
T, the type of the scalar element.
Definition: device_scalar.hpp:45
const_pointer data() const noexcept
Returns const pointer to object in device memory.
Definition: device_scalar.hpp:254
typename device_uvector< T >::const_reference const_reference
const value_type&
Definition: device_scalar.hpp:47
RMM_EXEC_CHECK_DISABLE device_scalar(device_scalar &&) noexcept=default
Default move constructor.
void set_stream(cuda_stream_view stream) noexcept
Sets the stream to be used for deallocation.
Definition: device_scalar.hpp:269
device_scalar(device_scalar const &other, cuda_stream_view stream, rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Construct a new device_scalar by deep copying the contents of another device_scalar,...
Definition: device_scalar.hpp:139
typename device_uvector< T >::const_pointer const_pointer
Definition: device_scalar.hpp:51
cuda_stream_view stream() const noexcept
Stream associated with the device memory allocation.
Definition: device_scalar.hpp:262
pointer data() noexcept
Returns pointer to object in device memory.
Definition: device_scalar.hpp:242
value_type value(cuda_stream_view stream) const
Copies the value from device to host, synchronizes, and returns the value.
Definition: device_scalar.hpp:162
void set_value_to_zero_async(cuda_stream_view stream)
Sets the value of the device_scalar to zero on the specified stream.
Definition: device_scalar.hpp:227
void set_value_async(value_type const &value, cuda_stream_view stream)
Sets the value of the device_scalar to the value of v.
Definition: device_scalar.hpp:204
device_scalar(value_type const &initial_value, cuda_stream_view stream, rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Construct a new device_scalar with an initial value.
Definition: device_scalar.hpp:118
typename device_uvector< T >::pointer pointer
The type of the pointer returned by data()
Definition: device_scalar.hpp:49
typename device_uvector< T >::reference reference
value_type&
Definition: device_scalar.hpp:46
An uninitialized vector of elements in device memory.
Definition: device_uvector.hpp:76
value_type * pointer
The type of the pointer returned by data()
Definition: device_uvector.hpp:87
T value_type
T; stored value type.
Definition: device_uvector.hpp:82
value_type & reference
value_type&; reference type returned by operator[](size_type)
Definition: device_uvector.hpp:84
value_type const * const_pointer
The type of the pointer returned by data() const.
Definition: device_uvector.hpp:88
value_type const & const_reference
Definition: device_uvector.hpp:86
Base class for all libcudf device memory allocation.
Definition: device_memory_resource.hpp:89
device_memory_resource * get_current_device_resource()
Get the memory resource for the current device.
Definition: per_device_resource.hpp:207
Management of per-device device_memory_resources.