scalar_device_view.cuh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2024, 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 <cudf/scalar/scalar.hpp>
20 #include <cudf/types.hpp>
21 
27 namespace CUDF_EXPORT cudf {
28 namespace detail {
34  public:
35  ~scalar_device_view_base() = default;
36 
42  [[nodiscard]] __host__ __device__ data_type type() const noexcept { return _type; }
43 
50  [[nodiscard]] __device__ bool is_valid() const noexcept { return *_is_valid; }
51 
57  __device__ void set_valid(bool is_valid) noexcept { *_is_valid = is_valid; }
58 
59  protected:
60  data_type _type{type_id::EMPTY};
61  bool* _is_valid{};
63 
72  scalar_device_view_base(data_type type, bool* is_valid) : _type(type), _is_valid(is_valid) {}
73 
74  scalar_device_view_base() = default;
75 };
76 
81  public:
88  template <typename T>
89  __device__ T& value() noexcept
90  {
91  return *data<T>();
92  }
93 
100  template <typename T>
101  __device__ T const& value() const noexcept
102  {
103  return *data<T>();
104  }
105 
112  template <typename T>
113  __device__ void set_value(T value)
114  {
115  *static_cast<T*>(_data) = value;
116  }
117 
124  template <typename T>
125  __device__ T* data() noexcept
126  {
127  return static_cast<T*>(_data);
128  }
129 
136  template <typename T>
137  __device__ T const* data() const noexcept
138  {
139  return static_cast<T const*>(_data);
140  }
141 
142  protected:
143  void* _data{};
144 
157  : detail::scalar_device_view_base(type, is_valid), _data(data)
158  {
159  }
160 };
161 
165 template <typename T>
167  public:
168  using value_type = T;
169 
175  __device__ T& value() noexcept { return fixed_width_scalar_device_view_base::value<T>(); }
176 
182  __device__ T const& value() const noexcept
183  {
184  return fixed_width_scalar_device_view_base::value<T>();
185  }
186 
192  __device__ void set_value(T value) { fixed_width_scalar_device_view_base::set_value<T>(value); }
193 
199  __device__ T* data() noexcept { return fixed_width_scalar_device_view_base::data<T>(); }
200 
206  __device__ T const* data() const noexcept
207  {
208  return fixed_width_scalar_device_view_base::data<T>();
209  }
210 
211  protected:
224  : detail::fixed_width_scalar_device_view_base(type, data, is_valid)
225  {
226  }
227 };
228 
229 } // namespace detail
230 
234 template <typename T>
236  public:
246  : detail::fixed_width_scalar_device_view<T>(type, data, is_valid)
247  {
248  }
249 };
250 
254 template <typename T>
256  public:
257  using rep_type = typename T::rep;
258 
268  : detail::scalar_device_view_base(type, is_valid), _data(data)
269  {
270  }
271 
277  __device__ void set_value(rep_type value) { *_data = value; }
278 
284  __device__ rep_type const& rep() const noexcept { return *_data; }
285 
286  private:
287  rep_type* _data{};
288 };
289 
294  public:
296 
307  string_scalar_device_view(data_type type, char const* data, bool* is_valid, size_type size)
308  : detail::scalar_device_view_base(type, is_valid), _data(data), _size(size)
309  {
310  }
311 
317  [[nodiscard]] __device__ ValueType value() const noexcept
318  {
319  return ValueType{this->data(), _size};
320  }
321 
327  [[nodiscard]] __device__ char const* data() const noexcept
328  {
329  return static_cast<char const*>(_data);
330  }
331 
337  [[nodiscard]] __device__ size_type size() const noexcept { return _size; }
338 
339  private:
340  char const* _data{};
341  size_type _size;
342 };
343 
347 template <typename T>
349  public:
359  : detail::fixed_width_scalar_device_view<T>(type, data, is_valid)
360  {
361  }
362 };
363 
367 template <typename T>
369  public:
379  : detail::fixed_width_scalar_device_view<T>(type, data, is_valid)
380  {
381  }
382 };
383 
390 template <typename T>
392 {
394 }
395 
403 {
404  return string_scalar_device_view(s.type(), s.data(), s.validity_data(), s.size());
405 }
406 
413 template <typename T>
415 {
417 }
418 
425 template <typename T>
427 {
429 }
430 
437 template <typename T>
439 {
441 }
442 
443 } // namespace CUDF_EXPORT cudf
Indicator for the logical data type of an element in a column.
Definition: types.hpp:243
A type-erased scalar_device_view where the value is a fixed width type.
T const & value() const noexcept
Returns const reference to stored value.
T & value() noexcept
Returns reference to stored value.
T const * data() const noexcept
Returns a const raw pointer to the value in device memory.
void set_value(T value)
Stores the value in scalar.
T * data() noexcept
Returns a raw pointer to the value in device memory.
fixed_width_scalar_device_view_base(data_type type, void *data, bool *is_valid)
Construct a new fixed width scalar device view object.
A type of scalar_device_view where the value is a fixed width type.
void set_value(T value)
Stores the value in scalar.
T & value() noexcept
Returns reference to stored value.
T * data() noexcept
Returns a raw pointer to the value in device memory.
fixed_width_scalar_device_view(data_type type, T *data, bool *is_valid)
Construct a new fixed width scalar device view object.
T const * data() const noexcept
Returns a const raw pointer to the value in device memory.
T const & value() const noexcept
Returns const reference to stored value.
T * data()
Returns a raw pointer to the value in device memory.
A non-owning view of scalar from device that is trivially copyable and usable in CUDA device code.
bool is_valid() const noexcept
Returns whether the scalar holds a valid value (i.e., not null).
void set_valid(bool is_valid) noexcept
Updates the validity of the value.
scalar_device_view_base(data_type type, bool *is_valid)
Construct a new scalar device view base object from a device pointer and a validity boolean.
data_type type() const noexcept
Returns the value type.
A type of scalar_device_view that stores a pointer to a duration value.
duration_scalar_device_view(data_type type, T *data, bool *is_valid)
Construct a new duration scalar device view object from data and validity pointers.
An owning class to represent a duration value in device memory.
Definition: scalar.hpp:682
A type of scalar_device_view that stores a pointer to a fixed_point value.
typename T::rep rep_type
The representation type of the fixed_point value.
rep_type const & rep() const noexcept
Get the value of the scalar, as a rep_type.
fixed_point_scalar_device_view(data_type type, rep_type *data, bool *is_valid)
Construct a new fixed point scalar device view object from data and validity pointers.
void set_value(rep_type value)
Stores the value in scalar.
An owning class to represent a fixed_point number in device memory.
Definition: scalar.hpp:304
rep_type * data()
Returns a raw pointer to the value in device memory.
A type of scalar_device_view that stores a pointer to a numerical value.
numeric_scalar_device_view(data_type type, T *data, bool *is_valid)
Construct a new numeric scalar device view object from data and validity pointers.
An owning class to represent a numerical value in device memory.
Definition: scalar.hpp:244
bool * validity_data()
Returns a raw pointer to the validity bool in device memory.
data_type type() const noexcept
Returns the scalar's logical value type.
A type of scalar_device_view that stores a pointer to a string value.
ValueType value() const noexcept
Returns string_view of the value of this scalar.
char const * data() const noexcept
Returns a raw pointer to the value in device memory.
size_type size() const noexcept
Returns the size of the string in bytes.
string_scalar_device_view(data_type type, char const *data, bool *is_valid, size_type size)
Construct a new string scalar device view object from string data, size and validity pointers.
An owning class to represent a string in device memory.
Definition: scalar.hpp:431
size_type size() const
Returns the size of the string in bytes.
char const * data() const
Returns a raw pointer to the string in device memory.
A non-owning, immutable view of device data that is a variable length char array representing a UTF-8...
Definition: string_view.hpp:44
A type of scalar_device_view that stores a pointer to a timestamp value.
timestamp_scalar_device_view(data_type type, T *data, bool *is_valid)
Construct a new timestamp scalar device view object.
An owning class to represent a timestamp value in device memory.
Definition: scalar.hpp:626
std::unique_ptr< cudf::column > is_valid(cudf::column_view const &input, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=rmm::mr::get_current_device_resource())
Creates a column of type_id::BOOL8 elements where for every element in input true indicates the value...
int32_t size_type
Row index type for columns and tables.
Definition: types.hpp:95
cuDF interfaces
Definition: aggregation.hpp:35
auto get_scalar_device_view(fixed_point_scalar< T > &s)
Get the device view of a fixed_point_scalar.
Class definitions for cudf::scalar.
Class definition for cudf::string_view.
Type declarations for libcudf.