-
Notifications
You must be signed in to change notification settings - Fork 915
/
scalar_factories.cpp
135 lines (115 loc) · 4.51 KB
/
scalar_factories.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright (c) 2019-2020, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf/null_mask.hpp>
#include <cudf/scalar/scalar_factories.hpp>
#include <cudf/utilities/error.hpp>
#include <cudf/utilities/traits.hpp>
#include <cudf/utilities/type_dispatcher.hpp>
#include <rmm/cuda_stream_view.hpp>
namespace cudf {
namespace {
struct scalar_construction_helper {
template <typename T,
typename ScalarType = scalar_type_t<T>,
typename std::enable_if_t<is_fixed_width<T>() and not is_fixed_point<T>()>* = nullptr>
std::unique_ptr<scalar> operator()(rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr) const
{
using Type = device_storage_type_t<T>;
auto s = new ScalarType(Type{}, false, stream, mr);
return std::unique_ptr<scalar>(s);
}
template <typename T,
typename ScalarType = scalar_type_t<T>,
typename std::enable_if_t<is_fixed_point<T>()>* = nullptr>
std::unique_ptr<scalar> operator()(rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr) const
{
using Type = device_storage_type_t<T>;
auto s = new ScalarType(Type{}, numeric::scale_type{0}, false, stream, mr);
return std::unique_ptr<scalar>(s);
}
template <typename T,
typename... Args,
typename std::enable_if_t<not is_fixed_width<T>()>* = nullptr>
std::unique_ptr<scalar> operator()(Args... args) const
{
CUDF_FAIL("Invalid type.");
}
};
} // namespace
// Allocate storage for a single numeric element
std::unique_ptr<scalar> make_numeric_scalar(data_type type,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
CUDF_EXPECTS(is_numeric(type), "Invalid, non-numeric type.");
return type_dispatcher(type, scalar_construction_helper{}, stream, mr);
}
// Allocate storage for a single timestamp element
std::unique_ptr<scalar> make_timestamp_scalar(data_type type,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
CUDF_EXPECTS(is_timestamp(type), "Invalid, non-timestamp type.");
return type_dispatcher(type, scalar_construction_helper{}, stream, mr);
}
// Allocate storage for a single duration element
std::unique_ptr<scalar> make_duration_scalar(data_type type,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
CUDF_EXPECTS(is_duration(type), "Invalid, non-duration type.");
return type_dispatcher(type, scalar_construction_helper{}, stream, mr);
}
// Allocate storage for a single fixed width element
std::unique_ptr<scalar> make_fixed_width_scalar(data_type type,
rmm::cuda_stream_view stream,
rmm::mr::device_memory_resource* mr)
{
CUDF_EXPECTS(is_fixed_width(type), "Invalid, non-fixed-width type.");
return type_dispatcher(type, scalar_construction_helper{}, stream, mr);
}
namespace {
struct default_scalar_functor {
template <typename T>
std::unique_ptr<cudf::scalar> operator()()
{
using ScalarType = scalar_type_t<T>;
return std::unique_ptr<scalar>(new ScalarType);
}
};
template <>
std::unique_ptr<cudf::scalar> default_scalar_functor::operator()<dictionary32>()
{
CUDF_FAIL("dictionary type not supported");
}
template <>
std::unique_ptr<cudf::scalar> default_scalar_functor::operator()<list_view>()
{
CUDF_FAIL("list_view type not supported");
}
template <>
std::unique_ptr<cudf::scalar> default_scalar_functor::operator()<struct_view>()
{
CUDF_FAIL("struct_view type not supported");
}
} // namespace
std::unique_ptr<scalar> make_default_constructed_scalar(data_type type)
{
return type_dispatcher(type, default_scalar_functor{});
}
} // namespace cudf