OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
InValuesBitmap.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
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 #include "InValuesBitmap.h"
18 #include "CodeGenerator.h"
19 #include "Execute.h"
20 #ifdef HAVE_CUDA
21 #include "GpuMemUtils.h"
22 #endif // HAVE_CUDA
23 #include "../Parser/ParserNode.h"
24 #include "../Shared/checked_alloc.h"
25 #include "GroupByAndAggregate.h"
26 #include "Logger/Logger.h"
29 #include "RuntimeFunctions.h"
30 
31 #include <limits>
32 
33 extern int64_t g_bitmap_memory_limit;
34 
35 InValuesBitmap::InValuesBitmap(const std::vector<int64_t>& values,
36  const int64_t null_val,
37  const Data_Namespace::MemoryLevel memory_level,
38  const int device_count,
39  Data_Namespace::DataMgr* data_mgr,
40  CompilationOptions const& co)
41  : rhs_has_null_(false)
42  , null_val_(null_val)
43  , memory_level_(memory_level)
44  , device_count_(device_count)
45  , data_mgr_(data_mgr)
46  , co_(co) {
47 #ifdef HAVE_CUDA
49  memory_level == Data_Namespace::GPU_LEVEL);
50 #else
52 #endif // HAVE_CUDA
53  if (values.empty()) {
54  return;
55  }
56  min_val_ = std::numeric_limits<int64_t>::max();
57  max_val_ = std::numeric_limits<int64_t>::min();
58  for (const auto value : values) {
59  if (value == null_val) {
60  rhs_has_null_ = true;
61  continue;
62  }
63  if (value < min_val_) {
64  min_val_ = value;
65  }
66  if (value > max_val_) {
67  max_val_ = value;
68  }
69  }
70  if (max_val_ < min_val_) {
71  CHECK_EQ(std::numeric_limits<int64_t>::max(), min_val_);
72  CHECK_EQ(std::numeric_limits<int64_t>::min(), max_val_);
74  return;
75  }
76  uint64_t const bitmap_sz_bits_minus_one = max_val_ - min_val_;
77  if (static_cast<uint64_t>(g_bitmap_memory_limit) <= bitmap_sz_bits_minus_one) {
78  throw FailedToCreateBitmap();
79  }
80  // bitmap_sz_bytes = ceil(bitmap_sz_bits / 8.0) = (bitmap_sz_bits-1) / 8 + 1
81  uint64_t const bitmap_sz_bytes = bitmap_sz_bits_minus_one / 8 + 1;
82  auto cpu_bitset = static_cast<int8_t*>(checked_calloc(bitmap_sz_bytes, 1));
83  for (const auto value : values) {
84  if (value == null_val) {
85  continue;
86  }
88  reinterpret_cast<int64_t*>(&cpu_bitset), value, min_val_, 0);
89  }
90 #ifdef HAVE_CUDA
92  for (int device_id = 0; device_id < device_count_; ++device_id) {
93  auto device_allocator = std::make_unique<CudaAllocator>(
94  data_mgr_, device_id, getQueryEngineCudaStreamForDevice(device_id));
95  gpu_buffers_.emplace_back(
96  data_mgr->alloc(Data_Namespace::GPU_LEVEL, device_id, bitmap_sz_bytes));
97  auto gpu_bitset = gpu_buffers_.back()->getMemoryPtr();
98  device_allocator->copyToDevice(gpu_bitset, cpu_bitset, bitmap_sz_bytes);
99  bitsets_.push_back(gpu_bitset);
100  }
101  free(cpu_bitset);
102  } else {
103  bitsets_.push_back(cpu_bitset);
104  }
105 #else
107  bitsets_.push_back(cpu_bitset);
108 #endif // HAVE_CUDA
109 }
110 
112  if (bitsets_.empty()) {
113  return;
114  }
116  CHECK_EQ(size_t(1), bitsets_.size());
117  free(bitsets_.front());
118  } else {
119  CHECK(data_mgr_);
120  for (auto& gpu_buffer : gpu_buffers_) {
121  data_mgr_->free(gpu_buffer);
122  }
123  }
124 }
125 
127  Executor* executor,
128  std::vector<std::shared_ptr<const Analyzer::Constant>> const& constant_owned) const {
130  auto pi8_ty =
131  llvm::PointerType::get(get_int_type(8, executor->cgen_state_->context_), 0);
132  CodeGenerator code_generator(executor);
133  params.null_val_lv =
135  &code_generator, co_, make_datum<int64_t>(null_val_), kBIGINT, device_count_)
136  .front();
137  if (bitsets_.empty()) {
138  auto const zero_lvs = CodegenUtil::hoistLiteral(
139  &code_generator, co_, make_datum<int64_t>(0), kBIGINT, device_count_);
140  params.min_val_lv = zero_lvs.front();
141  params.max_val_lv = zero_lvs.front();
142  params.bitmap_ptr_lv =
143  executor->cgen_state_->ir_builder_.CreateIntToPtr(zero_lvs.front(), pi8_ty);
144  } else {
145  params.min_val_lv =
147  &code_generator, co_, make_datum<int64_t>(min_val_), kBIGINT, device_count_)
148  .front();
149  params.max_val_lv =
151  &code_generator, co_, make_datum<int64_t>(max_val_), kBIGINT, device_count_)
152  .front();
153  auto to_raw_ptr = [](const auto& ptr) { return ptr.get(); };
154  auto begin = boost::make_transform_iterator(constant_owned.begin(), to_raw_ptr);
155  auto end = boost::make_transform_iterator(constant_owned.end(), to_raw_ptr);
156  std::vector<const Analyzer::Constant*> bitmap_constants(begin, end);
157  const auto bitset_handle_lvs =
158  code_generator.codegenHoistedConstants(bitmap_constants, kENCODING_NONE, {});
159  CHECK_EQ(size_t(1), bitset_handle_lvs.size());
160  params.bitmap_ptr_lv = executor->cgen_state_->ir_builder_.CreateIntToPtr(
161  bitset_handle_lvs.front(), pi8_ty);
162  }
163  return params;
164 }
165 
166 llvm::Value* InValuesBitmap::codegen(llvm::Value* needle, Executor* executor) const {
167  auto cgen_state = executor->getCgenStatePtr();
168  AUTOMATIC_IR_METADATA(cgen_state);
169  std::vector<std::shared_ptr<const Analyzer::Constant>> constants_owned;
170  for (const auto bitset : bitsets_) {
171  const int64_t bitset_handle = reinterpret_cast<int64_t>(bitset);
172  const auto bitset_handle_literal = std::dynamic_pointer_cast<Analyzer::Constant>(
173  Parser::IntLiteral::analyzeValue(bitset_handle));
174  CHECK(bitset_handle_literal);
175  CHECK_EQ(kENCODING_NONE, bitset_handle_literal->get_type_info().get_compression());
176  constants_owned.push_back(bitset_handle_literal);
177  }
178  const auto needle_i64 = cgen_state->castToTypeIn(needle, 64);
179  const auto null_bool_val =
180  static_cast<int8_t>(inline_int_null_val(SQLTypeInfo(kBOOLEAN, false)));
181  auto const func_params = prepareBitIsSetParams(executor, constants_owned);
182  return cgen_state->emitCall("bit_is_set",
183  {func_params.bitmap_ptr_lv,
184  needle_i64,
185  func_params.min_val_lv,
186  func_params.max_val_lv,
187  func_params.null_val_lv,
188  cgen_state->llInt(null_bool_val)});
189 }
190 
192  return bitsets_.empty();
193 }
194 
196  return rhs_has_null_;
197 }
#define CHECK_EQ(x, y)
Definition: Logger.h:301
BitIsSetParams prepareBitIsSetParams(Executor *executor, std::vector< std::shared_ptr< const Analyzer::Constant >> const &constant_owned) const
std::vector< llvm::Value * > hoistLiteral(CodeGenerator *code_generator, CompilationOptions const &co, Datum d, SQLTypeInfo type, size_t num_devices_to_hoist_literal)
CompilationOptions co_
static std::shared_ptr< Analyzer::Expr > analyzeValue(const int64_t intval)
Definition: ParserNode.cpp:166
bool hasNull() const
RUNTIME_EXPORT ALWAYS_INLINE void agg_count_distinct_bitmap(int64_t *agg, const int64_t val, const int64_t min_val, const int64_t bucket_size)
std::vector< llvm::Value * > codegenHoistedConstants(const std::vector< const Analyzer::Constant * > &constants, const EncodingType enc_type, const shared::StringDictKey &dict_id)
Definition: ConstantIR.cpp:373
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
std::vector< int8_t * > bitsets_
std::vector< Data_Namespace::AbstractBuffer * > gpu_buffers_
void * checked_calloc(const size_t nmemb, const size_t size)
Definition: checked_alloc.h:53
int64_t g_bitmap_memory_limit
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const int64_t null_val_
llvm::Value * codegen(llvm::Value *needle, Executor *executor) const
Data_Namespace::DataMgr * data_mgr_
CUstream getQueryEngineCudaStreamForDevice(int device_num)
Definition: QueryEngine.cpp:7
data_mgr_(data_mgr)
dictionary params
Definition: report.py:27
bool g_enable_watchdog false
Definition: Execute.cpp:80
const int device_count_
#define CHECK(condition)
Definition: Logger.h:291
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
device_count_(device_count)
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, CompilationOptions const &co)
void free(AbstractBuffer *buffer)
Definition: DataMgr.cpp:614
bool isEmpty() const
AbstractBuffer * alloc(const MemoryLevel memoryLevel, const int deviceId, const size_t numBytes)
Definition: DataMgr.cpp:605
const Data_Namespace::MemoryLevel memory_level_
memory_level_(memory_level)