forked from heavyai/heavydb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInValuesBitmap.cpp
More file actions
142 lines (134 loc) · 5.19 KB
/
InValuesBitmap.cpp
File metadata and controls
142 lines (134 loc) · 5.19 KB
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
136
137
138
139
140
141
142
/*
* Copyright 2017 MapD Technologies, Inc.
*
* 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 "Execute.h"
#include "InValuesBitmap.h"
#ifdef HAVE_CUDA
#include "GpuMemUtils.h"
#endif // HAVE_CUDA
#include "GroupByAndAggregate.h"
#include "RuntimeFunctions.h"
#include "../Parser/ParserNode.h"
#include "../Shared/checked_alloc.h"
#include <boost/multiprecision/cpp_int.hpp>
#include <glog/logging.h>
#include <limits>
typedef boost::multiprecision::number<
boost::multiprecision::
cpp_int_backend<64, 64, boost::multiprecision::signed_magnitude, boost::multiprecision::checked, void>>
checked_int64_t;
InValuesBitmap::InValuesBitmap(const std::vector<int64_t>& values,
const int64_t null_val,
const Data_Namespace::MemoryLevel memory_level,
const int device_count,
Data_Namespace::DataMgr* data_mgr)
: rhs_has_null_(false), null_val_(null_val), memory_level_(memory_level), device_count_(device_count) {
#ifdef HAVE_CUDA
CHECK(memory_level_ == Data_Namespace::CPU_LEVEL || memory_level == Data_Namespace::GPU_LEVEL);
#else
CHECK_EQ(Data_Namespace::CPU_LEVEL, memory_level_);
#endif // HAVE_CUDA
if (values.empty()) {
return;
}
min_val_ = std::numeric_limits<int64_t>::max();
max_val_ = std::numeric_limits<int64_t>::min();
for (const auto value : values) {
if (value == null_val) {
rhs_has_null_ = true;
continue;
}
if (value < min_val_) {
min_val_ = value;
}
if (value > max_val_) {
max_val_ = value;
}
}
if (max_val_ < min_val_) {
CHECK_EQ(std::numeric_limits<int64_t>::max(), min_val_);
CHECK_EQ(std::numeric_limits<int64_t>::min(), max_val_);
CHECK(rhs_has_null_);
return;
}
const int64_t MAX_BITMAP_BITS{8 * 1000 * 1000 * 1000L};
const auto bitmap_sz_bits = static_cast<int64_t>(checked_int64_t(max_val_) - min_val_ + 1);
if (bitmap_sz_bits > MAX_BITMAP_BITS) {
throw FailedToCreateBitmap();
}
const auto bitmap_sz_bytes = bitmap_bits_to_bytes(bitmap_sz_bits);
auto cpu_bitset = static_cast<int8_t*>(checked_calloc(bitmap_sz_bytes, 1));
for (const auto value : values) {
if (value == null_val) {
continue;
}
agg_count_distinct_bitmap(reinterpret_cast<int64_t*>(&cpu_bitset), value, min_val_);
}
#ifdef HAVE_CUDA
if (memory_level_ == Data_Namespace::GPU_LEVEL) {
for (int device_id = 0; device_id < device_count_; ++device_id) {
auto gpu_bitset = alloc_gpu_mem(data_mgr, bitmap_sz_bytes, device_id, nullptr);
copy_to_gpu(data_mgr, gpu_bitset, cpu_bitset, bitmap_sz_bytes, device_id);
bitsets_.push_back(reinterpret_cast<int8_t*>(gpu_bitset));
}
free(cpu_bitset);
} else {
bitsets_.push_back(cpu_bitset);
}
#else
CHECK_EQ(1, device_count_);
bitsets_.push_back(cpu_bitset);
#endif // HAVE_CUDA
}
InValuesBitmap::~InValuesBitmap() {
if (bitsets_.empty()) {
return;
}
if (memory_level_ == Data_Namespace::CPU_LEVEL) {
CHECK_EQ(size_t(1), bitsets_.size());
free(bitsets_.front());
}
}
llvm::Value* InValuesBitmap::codegen(llvm::Value* needle, Executor* executor) const {
CHECK(!bitsets_.empty());
std::vector<std::shared_ptr<const Analyzer::Constant>> constants_owned;
std::vector<const Analyzer::Constant*> constants;
for (const auto bitset : bitsets_) {
const int64_t bitset_handle = reinterpret_cast<int64_t>(bitset);
const auto bitset_handle_literal =
std::dynamic_pointer_cast<Analyzer::Constant>(Parser::IntLiteral::analyzeValue(bitset_handle));
CHECK(bitset_handle_literal);
CHECK_EQ(kENCODING_NONE, bitset_handle_literal->get_type_info().get_compression());
constants_owned.push_back(bitset_handle_literal);
constants.push_back(bitset_handle_literal.get());
}
const auto bitset_handle_lvs = executor->codegenHoistedConstants(constants, kENCODING_NONE, 0);
CHECK_EQ(size_t(1), bitset_handle_lvs.size());
const auto needle_i64 = executor->castToTypeIn(needle, 64);
const auto null_bool_val = static_cast<int8_t>(inline_int_null_val(SQLTypeInfo(kBOOLEAN, false)));
return executor->cgen_state_->emitCall("bit_is_set",
{executor->castToTypeIn(bitset_handle_lvs.front(), 64),
needle_i64,
executor->ll_int(min_val_),
executor->ll_int(max_val_),
executor->ll_int(null_val_),
executor->ll_int(null_bool_val)});
}
bool InValuesBitmap::isEmpty() const {
return bitsets_.empty();
}
bool InValuesBitmap::hasNull() const {
return rhs_has_null_;
}