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 #pragma once
17 
18 #include <cudf/types.hpp>
21 
23 
24 #include <rmm/cuda_stream_view.hpp>
25 #include <rmm/device_buffer.hpp>
26 #include <rmm/device_scalar.hpp>
27 
28 #include <memory>
29 #include <utility>
30 #include <vector>
31 
37 namespace cudf {
51 class scalar {
52  public:
53  virtual ~scalar() = default;
54  scalar(scalar&& other) = default;
55  scalar(scalar const& other) = default;
56  scalar& operator=(scalar const& other) = delete;
57  scalar& operator=(scalar&& other) = delete;
58 
62  data_type type() const noexcept { return _type; }
63 
70  void set_valid(bool is_valid, rmm::cuda_stream_view stream = rmm::cuda_stream_default)
71  {
72  _is_valid.set_value(is_valid, stream);
73  }
74 
84  bool is_valid(rmm::cuda_stream_view stream = rmm::cuda_stream_default) const
85  {
86  return _is_valid.value(stream);
87  }
88 
92  bool* validity_data() { return _is_valid.data(); }
93 
97  bool const* validity_data() const { return _is_valid.data(); }
98 
99  protected:
100  data_type _type{type_id::EMPTY};
102 
103  scalar() = default;
104 
117  bool is_valid = false,
118  rmm::cuda_stream_view stream = rmm::cuda_stream_default,
119  rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource())
120  : _type(type), _is_valid(is_valid, stream, mr)
121  {
122  }
123 };
124 
125 namespace detail {
126 template <typename T>
127 class fixed_width_scalar : public scalar {
128  static_assert(is_fixed_width<T>(), "Unexpected non-fixed-width type.");
129 
130  public:
131  using value_type = T;
132 
133  ~fixed_width_scalar() = default;
134  fixed_width_scalar(fixed_width_scalar&& other) = default;
135  fixed_width_scalar(fixed_width_scalar const& other) = default;
136  fixed_width_scalar& operator=(fixed_width_scalar const& other) = delete;
137  fixed_width_scalar& operator=(fixed_width_scalar&& other) = delete;
138 
145  void set_value(T value, rmm::cuda_stream_view stream = rmm::cuda_stream_default)
146  {
147  _data.set_value(value, stream);
148  this->set_valid(true, stream);
149  }
150 
154  explicit operator value_type() const { return this->value(0); }
155 
161  T value(rmm::cuda_stream_view stream = rmm::cuda_stream_default) const
162  {
163  return _data.value(stream);
164  }
165 
169  T* data() { return _data.data(); }
170 
174  T const* data() const { return _data.data(); }
175 
176  protected:
178 
180 
190  bool is_valid = true,
191  rmm::cuda_stream_view stream = rmm::cuda_stream_default,
192  rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource())
193  : scalar(data_type(type_to_id<T>()), is_valid, stream, mr), _data(value, stream, mr)
194  {
195  }
196 
206  bool is_valid = true,
207  rmm::cuda_stream_view stream = rmm::cuda_stream_default,
208  rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource())
209  : scalar(data_type(type_to_id<T>()), is_valid, stream, mr),
210  _data{std::forward<rmm::device_scalar<T>>(data)}
211  {
212  }
213 };
214 
215 } // namespace detail
216 
222 template <typename T>
224  static_assert(is_numeric<T>(), "Unexpected non-numeric type.");
225 
226  public:
227  numeric_scalar() = default;
228  ~numeric_scalar() = default;
229  numeric_scalar(numeric_scalar&& other) = default;
230  numeric_scalar(numeric_scalar const& other) = default;
231  numeric_scalar& operator=(numeric_scalar const& other) = delete;
232  numeric_scalar& operator=(numeric_scalar&& other) = delete;
233 
243  bool is_valid = true,
244  rmm::cuda_stream_view stream = rmm::cuda_stream_default,
245  rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource())
246  : detail::fixed_width_scalar<T>(value, is_valid, stream, mr)
247  {
248  }
249 
259  bool is_valid = true,
260  rmm::cuda_stream_view stream = rmm::cuda_stream_default,
261  rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource())
262  : detail::fixed_width_scalar<T>(std::forward<rmm::device_scalar<T>>(data), is_valid, stream, mr)
263  {
264  }
265 };
266 
272 template <typename T>
273 class fixed_point_scalar : public scalar {
274  static_assert(is_fixed_point<T>(), "Unexpected non-fixed_point type.");
275 
276  public:
277  using rep_type = typename T::rep;
278 
279  fixed_point_scalar() : scalar(data_type(type_to_id<T>())){};
280  ~fixed_point_scalar() = default;
281  fixed_point_scalar(fixed_point_scalar&& other) = default;
282  fixed_point_scalar(fixed_point_scalar const& other) = default;
283  fixed_point_scalar& operator=(fixed_point_scalar const& other) = delete;
284  fixed_point_scalar& operator=(fixed_point_scalar&& other) = delete;
285 
296  numeric::scale_type scale,
297  bool is_valid = true,
298  rmm::cuda_stream_view stream = rmm::cuda_stream_default,
299  rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource())
300  : scalar{data_type{type_to_id<T>(), static_cast<int32_t>(scale)}, is_valid, stream, mr},
301  _data{value}
302  {
303  }
304 
314  bool is_valid = true,
315  rmm::cuda_stream_view stream = rmm::cuda_stream_default,
316  rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource())
317  : scalar{data_type{type_to_id<T>(), 0}, is_valid, stream, mr}, _data{value}
318  {
319  }
320 
330  bool is_valid = true,
331  rmm::cuda_stream_view stream = rmm::cuda_stream_default,
332  rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource())
333  : scalar{data_type{type_to_id<T>(), value.scale()}, is_valid, stream, mr}, _data{value.value()}
334  {
335  }
336 
347  numeric::scale_type scale,
348  bool is_valid = true,
349  rmm::cuda_stream_view stream = rmm::cuda_stream_default,
350  rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource())
351  : scalar{data_type{type_to_id<T>(), scale}, is_valid, stream, mr},
352  _data{std::forward<rmm::device_scalar<rep_type>>(data)}
353  {
354  }
355 
361  rep_type value(rmm::cuda_stream_view stream = rmm::cuda_stream_default) const
362  {
363  return _data.value(stream);
364  }
365 
371  T fixed_point_value(rmm::cuda_stream_view stream = rmm::cuda_stream_default) const
372  {
373  using namespace numeric;
374  return T{scaled_integer<rep_type>{_data.value(stream), scale_type{type().scale()}}};
375  }
376 
380  rep_type* data() { return _data.data(); }
381 
385  rep_type const* data() const { return _data.data(); }
386 
387  protected:
389 };
390 
394 class string_scalar : public scalar {
395  public:
397 
398  string_scalar() : scalar(data_type(type_id::STRING)) {}
399  ~string_scalar() = default;
400  string_scalar(string_scalar&& other) = default;
401  string_scalar(string_scalar const& other) = default;
402  string_scalar& operator=(string_scalar const& other) = delete;
403  string_scalar& operator=(string_scalar&& other) = delete;
404 
413  string_scalar(std::string const& string,
414  bool is_valid = true,
415  rmm::cuda_stream_view stream = rmm::cuda_stream_default,
416  rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource())
417  : scalar(data_type(type_id::STRING), is_valid), _data(string.data(), string.size(), stream, mr)
418  {
419  }
420 
430  string_scalar(value_type const& source,
431  bool is_valid = true,
432  rmm::cuda_stream_view stream = rmm::cuda_stream_default,
433  rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());
434 
445  bool is_valid = true,
446  rmm::cuda_stream_view stream = rmm::cuda_stream_default,
447  rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource());
448 
452  explicit operator std::string() const { return this->to_string(0); }
453 
459  std::string to_string(rmm::cuda_stream_view stream = rmm::cuda_stream_default) const;
460 
466  value_type value(rmm::cuda_stream_view stream = rmm::cuda_stream_default) const;
467 
471  size_type size() const { return _data.size(); }
472 
476  const char* data() const { return static_cast<const char*>(_data.data()); }
477 
478  protected:
480 };
481 
488 template <typename T>
490  static_assert(is_chrono<T>(), "Unexpected non-chrono type");
491 
492  public:
493  chrono_scalar() = default;
494  ~chrono_scalar() = default;
495  chrono_scalar(chrono_scalar&& other) = default;
496  chrono_scalar(chrono_scalar const& other) = default;
497  chrono_scalar& operator=(chrono_scalar const& other) = delete;
498  chrono_scalar& operator=(chrono_scalar&& other) = delete;
499 
509  bool is_valid = true,
510  rmm::cuda_stream_view stream = rmm::cuda_stream_default,
511  rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource())
512  : detail::fixed_width_scalar<T>(value, is_valid, stream, mr)
513  {
514  }
515 
525  bool is_valid = true,
526  rmm::cuda_stream_view stream = rmm::cuda_stream_default,
527  rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource())
528  : detail::fixed_width_scalar<T>(std::forward<rmm::device_scalar<T>>(data), is_valid, stream, mr)
529  {
530  }
531 };
532 
533 template <typename T>
535  static_assert(is_timestamp<T>(), "Unexpected non-timestamp type");
537 
538  timestamp_scalar() = default;
539 
550  template <typename Duration2>
551  timestamp_scalar(Duration2 const& value,
552  bool is_valid,
553  rmm::cuda_stream_view stream = rmm::cuda_stream_default,
554  rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource())
555  : chrono_scalar<T>(T{typename T::duration{value}}, is_valid, stream, mr)
556  {
557  }
558 
562  typename T::rep ticks_since_epoch() { return this->value().time_since_epoch().count(); }
563 };
564 
565 template <typename T>
567  static_assert(is_duration<T>(), "Unexpected non-duration type");
569 
570  duration_scalar() = default;
571 
580  duration_scalar(typename T::rep value,
581  bool is_valid,
582  rmm::cuda_stream_view stream = rmm::cuda_stream_default,
583  rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource())
584  : chrono_scalar<T>(T{value}, is_valid, stream, mr)
585  {
586  }
587 
591  typename T::rep count() { return this->value().count(); }
592 }; // end of group
594 } // namespace cudf
cudf::numeric_scalar::numeric_scalar
numeric_scalar(rmm::device_scalar< T > &&data, bool is_valid=true, rmm::cuda_stream_view stream=rmm::cuda_stream_default, rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Construct a new numeric scalar object from existing device memory.
Definition: scalar.hpp:258
fixed_point.hpp
Class definition for fixed point data type.
cudf::scalar::set_valid
void set_valid(bool is_valid, rmm::cuda_stream_view stream=rmm::cuda_stream_default)
Updates the validity of the value.
Definition: scalar.hpp:70
cudf::scalar::validity_data
bool * validity_data()
Returns a raw pointer to the validity bool in device memory.
Definition: scalar.hpp:92
cudf::scalar::scalar
scalar(data_type type, bool is_valid=false, rmm::cuda_stream_view stream=rmm::cuda_stream_default, rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Construct a new scalar object.
Definition: scalar.hpp:116
cudf::detail::fixed_width_scalar::data
T const * data() const
Returns a const raw pointer to the value in device memory.
Definition: scalar.hpp:174
cudf::fixed_point_scalar::fixed_point_value
T fixed_point_value(rmm::cuda_stream_view stream=rmm::cuda_stream_default) const
Get the decimal32 or decimal64.
Definition: scalar.hpp:371
cudf::scalar::_is_valid
rmm::device_scalar< bool > _is_valid
Device bool signifying validity.
Definition: scalar.hpp:101
rmm::device_scalar::set_value
auto set_value(U const &host_value, cuda_stream_view stream=cuda_stream_view{}) -> std::enable_if_t< std::is_fundamental< U >::value &&not std::is_same< U, bool >::value, void >
cudf::scalar::_type
data_type _type
Logical type of value in the scalar.
Definition: scalar.hpp:100
cudf::type_id
type_id
Identifies a column's logical element type.
Definition: types.hpp:204
cudf::fixed_point_scalar::fixed_point_scalar
fixed_point_scalar(T value, bool is_valid=true, rmm::cuda_stream_view stream=rmm::cuda_stream_default, rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Construct a new fixed_point scalar object from a fixed_point number.
Definition: scalar.hpp:329
types.hpp
Type declarations for libcudf.
cudf::string_view
A non-owning, immutable view of device data that is a variable length char array representing a UTF-8...
Definition: string_view.hpp:63
rmm::cuda_stream_view
cudf::fixed_point_scalar::fixed_point_scalar
fixed_point_scalar(rep_type value, bool is_valid=true, rmm::cuda_stream_view stream=rmm::cuda_stream_default, rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Construct a new fixed_point scalar object from a value and default 0-scale.
Definition: scalar.hpp:313
cudf::data_type::scale
constexpr int32_t scale() const noexcept
Returns the scale (for fixed_point types)
Definition: types.hpp:278
cudf::fixed_point_scalar::data
rep_type * data()
Returns a raw pointer to the value in device memory.
Definition: scalar.hpp:380
rmm::device_scalar::value
T value(cuda_stream_view stream=cuda_stream_view{}) const
cudf::fixed_point_scalar::_data
rmm::device_scalar< rep_type > _data
device memory containing the value
Definition: scalar.hpp:388
cudf::string_scalar::to_string
std::string to_string(rmm::cuda_stream_view stream=rmm::cuda_stream_default) const
Get the value of the scalar in a host std::string.
cudf::fixed_point_scalar::data
rep_type const * data() const
Returns a const raw pointer to the value in device memory.
Definition: scalar.hpp:385
cudf::chrono_scalar::chrono_scalar
chrono_scalar(rmm::device_scalar< T > &&data, bool is_valid=true, rmm::cuda_stream_view stream=rmm::cuda_stream_default, rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Construct a new chrono scalar object from existing device memory.
Definition: scalar.hpp:524
cudf::fixed_point_scalar::fixed_point_scalar
fixed_point_scalar(rmm::device_scalar< rep_type > &&data, numeric::scale_type scale, bool is_valid=true, rmm::cuda_stream_view stream=rmm::cuda_stream_default, rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Construct a new fixed_point scalar object from existing device memory.
Definition: scalar.hpp:346
cudf::string_scalar::string_scalar
string_scalar(rmm::device_scalar< value_type > &data, bool is_valid=true, rmm::cuda_stream_view stream=rmm::cuda_stream_default, rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Construct a new string scalar object from string_view in device memory Note that this function copies...
cudf::detail::fixed_width_scalar::value
T value(rmm::cuda_stream_view stream=rmm::cuda_stream_default) const
Get the value of the scalar.
Definition: scalar.hpp:161
cudf::timestamp_scalar::ticks_since_epoch
T::rep ticks_since_epoch()
Return the duration in number of ticks since the UNIX epoch.
Definition: scalar.hpp:562
rmm::device_buffer
cudf::duration_scalar::count
T::rep count()
Return the duration in number of ticks.
Definition: scalar.hpp:591
device_buffer.hpp
cudf::scalar
An owning class to represent a singular value.
Definition: scalar.hpp:51
cudf::string_scalar::data
const char * data() const
Returns a raw pointer to the string in device memory.
Definition: scalar.hpp:476
cudf::numeric_scalar
An owning class to represent a numerical value in device memory.
Definition: scalar.hpp:223
numeric::scaled_integer
Helper struct for constructing fixed_point when value is already shifted.
Definition: fixed_point.hpp:197
cudf::detail::fixed_width_scalar::fixed_width_scalar
fixed_width_scalar(T value, bool is_valid=true, rmm::cuda_stream_view stream=rmm::cuda_stream_default, rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Construct a new fixed width scalar object.
Definition: scalar.hpp:189
cudf::detail::fixed_width_scalar
Definition: scalar.hpp:127
cudf::data_type
Indicator for the logical data type of an element in a column.
Definition: types.hpp:243
cudf::duration_scalar
Definition: scalar.hpp:566
cudf::detail::fixed_width_scalar::_data
rmm::device_scalar< T > _data
device memory containing the value
Definition: scalar.hpp:177
cudf::chrono_scalar::chrono_scalar
chrono_scalar(T value, bool is_valid=true, rmm::cuda_stream_view stream=rmm::cuda_stream_default, rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Construct a new chrono scalar object.
Definition: scalar.hpp:508
cudf::fixed_point_scalar
An owning class to represent a fixed_point number in device memory.
Definition: scalar.hpp:273
cudf::chrono_scalar
An owning class to represent a timestamp/duration value in device memory.
Definition: scalar.hpp:489
cudf::detail::fixed_width_scalar::data
T * data()
Returns a raw pointer to the value in device memory.
Definition: scalar.hpp:169
cudf::detail::fixed_width_scalar::fixed_width_scalar
fixed_width_scalar(rmm::device_scalar< T > &&data, bool is_valid=true, rmm::cuda_stream_view stream=rmm::cuda_stream_default, rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Construct a new fixed width scalar object from existing device memory.
Definition: scalar.hpp:205
cudf::string_scalar::value
value_type value(rmm::cuda_stream_view stream=rmm::cuda_stream_default) const
Get the value of the scalar as a string_view.
cudf::fixed_point_scalar::value
rep_type value(rmm::cuda_stream_view stream=rmm::cuda_stream_default) const
Get the value of the scalar.
Definition: scalar.hpp:361
cudf::duration_scalar::duration_scalar
duration_scalar(typename T::rep value, bool is_valid, rmm::cuda_stream_view stream=rmm::cuda_stream_default, rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Construct a new duration scalar object from tick counts.
Definition: scalar.hpp:580
cudf
cuDF interfaces
Definition: aggregation.hpp:34
cudf::scalar::validity_data
bool const * validity_data() const
Returns a const raw pointer to the validity bool in device memory.
Definition: scalar.hpp:97
cudf::string_scalar::size
size_type size() const
Returns the size of the string in bytes.
Definition: scalar.hpp:471
cudf::string_scalar::string_scalar
string_scalar(std::string const &string, bool is_valid=true, rmm::cuda_stream_view stream=rmm::cuda_stream_default, rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Construct a new string scalar object.
Definition: scalar.hpp:413
cudf::timestamp_scalar::timestamp_scalar
timestamp_scalar(Duration2 const &value, bool is_valid, rmm::cuda_stream_view stream=rmm::cuda_stream_default, rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Construct a new timestamp scalar object from a duration that is convertible to T::duration.
Definition: scalar.hpp:551
rmm::device_scalar::data
T * data() noexcept
cudf::scalar::is_valid
bool is_valid(rmm::cuda_stream_view stream=rmm::cuda_stream_default) const
Indicates whether the scalar contains a valid value.
Definition: scalar.hpp:84
rmm::device_scalar< bool >
cudf::timestamp_scalar
Definition: scalar.hpp:534
cudf::string_scalar
An owning class to represent a string in device memory.
Definition: scalar.hpp:394
cudf::string_scalar::string_scalar
string_scalar(value_type const &source, bool is_valid=true, rmm::cuda_stream_view stream=rmm::cuda_stream_default, rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Construct a new string scalar object from string_view Note that this function copies the data pointed...
cudf::detail::fixed_width_scalar::set_value
void set_value(T value, rmm::cuda_stream_view stream=rmm::cuda_stream_default)
Set the value of the scalar.
Definition: scalar.hpp:145
cudf::scalar::type
data_type type() const noexcept
Returns the scalar's logical value type.
Definition: scalar.hpp:62
rmm::mr::device_memory_resource
type_dispatcher.hpp
Defines the mapping between cudf::type_id runtime type information and concrete C++ types.
traits.hpp
cudf::fixed_point_scalar::fixed_point_scalar
fixed_point_scalar(rep_type value, numeric::scale_type scale, bool is_valid=true, rmm::cuda_stream_view stream=rmm::cuda_stream_default, rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Construct a new fixed_point scalar object from already shifted value and scale.
Definition: scalar.hpp:295
cudf::numeric_scalar::numeric_scalar
numeric_scalar(T value, bool is_valid=true, rmm::cuda_stream_view stream=rmm::cuda_stream_default, rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Construct a new numeric scalar object.
Definition: scalar.hpp:242
cudf::type_to_id
constexpr type_id type_to_id()
Maps a C++ type to it's corresponding cudf::type_id
Definition: type_dispatcher.hpp:56
numeric
fixed_point and supporting types
Definition: fixed_point.hpp:33