OmniSciDB  a987f07e93
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ColSlotContext.h
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 
24 #pragma once
25 
26 #include "Logger/Logger.h"
27 
28 #include <algorithm>
29 #include <string>
30 #include <unordered_map>
31 #include <vector>
32 
33 struct SlotSize {
34  int8_t padded_size; // size of the slot
35  int8_t logical_size; // size of the element in the slot
36 };
37 
38 inline bool operator==(const SlotSize& lhs, const SlotSize& rhs) {
39  return lhs.padded_size == rhs.padded_size && lhs.logical_size == rhs.logical_size;
40 }
41 
42 namespace Analyzer {
43 class Expr;
44 }
45 
47  public:
49 
50  ColSlotContext(const std::vector<Analyzer::Expr*>& col_expr_list,
51  const std::vector<int64_t>& col_exprs_to_not_project);
52 
53  void setAllSlotsSize(const int8_t slot_width_size);
54 
55  void setAllSlotsPaddedSize(const int8_t padded_size);
56 
57  void setAllUnsetSlotsPaddedSize(const int8_t padded_size);
58 
60 
61  void validate() const;
62 
63  size_t getColCount() const;
64  size_t getSlotCount() const;
65 
66  const SlotSize& getSlotInfo(const size_t slot_idx) const {
67  CHECK_LT(slot_idx, slot_sizes_.size());
68  return slot_sizes_[slot_idx];
69  }
70 
71  void setPaddedSlotWidthBytes(const size_t slot_idx, const int8_t bytes) {
72  CHECK_LT(slot_idx, slot_sizes_.size());
73  slot_sizes_[slot_idx].padded_size = bytes;
74  }
75 
76  const std::vector<size_t>& getSlotsForCol(const size_t col_idx) const {
77  CHECK_LT(col_idx, col_to_slot_map_.size());
78  return col_to_slot_map_[col_idx];
79  }
80 
81  size_t getAllSlotsPaddedSize() const;
82 
83  size_t getAllSlotsAlignedPaddedSize() const;
84 
85  size_t getAlignedPaddedSizeForRange(const size_t end) const;
86 
87  size_t getTotalBytesOfColumnarBuffers(const size_t entry_count) const;
88 
89  int8_t getMinPaddedByteSize(const int8_t actual_min_byte_width) const;
90 
91  size_t getCompactByteWidth() const;
92 
93  size_t getColOnlyOffInBytes(const size_t slot_idx) const;
94 
95  bool empty();
96 
97  void clear();
98 
99  void addColumn(const std::vector<std::tuple<int8_t, int8_t>>& slots_for_col);
100 
101  void addColumnFlatBuffer(const int64_t flatbuffer_size);
102  int64_t getFlatBufferSize(const size_t slot_idx) const;
103 
104  bool operator==(const ColSlotContext& that) const {
105  return std::equal(
106  slot_sizes_.cbegin(), slot_sizes_.cend(), that.slot_sizes_.cbegin()) &&
107  std::equal(col_to_slot_map_.cbegin(),
108  col_to_slot_map_.cend(),
109  that.col_to_slot_map_.cbegin());
110  }
111 
112  bool operator!=(const ColSlotContext& that) const { return !(*this == that); }
113 
114  void alignPaddedSlots(const bool sort_on_gpu);
115 
116  int64_t varlenOutputElementSize(const size_t slot_idx) const;
117 
118  bool hasVarlenOutput() const { return !varlen_output_slot_map_.empty(); }
119 
120  bool slotIsVarlen(const size_t slot_idx) const {
121  return varlen_output_slot_map_.count(slot_idx) > 0;
122  }
123 
124  std::string toString() const {
125  std::string str{"Col Slot Context State\n"};
126  if (slot_sizes_.empty()) {
127  str += "\tEmpty";
128  return str;
129  }
130  str += "\tN | P , L\n";
131  for (size_t i = 0; i < slot_sizes_.size(); i++) {
132  const auto& slot_size = slot_sizes_[i];
133  str += "\t" + std::to_string(i) + " | " + std::to_string(slot_size.padded_size) +
134  " , " + std::to_string(slot_size.logical_size) + "\n";
135  }
136 #ifdef HAVE_TOSTRING
137  str += "\tcol_to_slot_map=" + ::toString(col_to_slot_map_) + "\n";
138  str += "\tvarlen_output_slot_map=" + ::toString(varlen_output_slot_map_) + "\n";
139 #endif
140  return str;
141  }
142 
143  private:
144  void addSlotForColumn(const int8_t logical_size, const size_t column_idx);
145 
146  void addSlotForColumn(const int8_t padded_size,
147  const int8_t logical_size,
148  const size_t column_idx);
149 
150  std::vector<SlotSize> slot_sizes_;
151  std::vector<std::vector<size_t>> col_to_slot_map_;
152 
153  using ArraySize = int64_t;
154  using SlotIndex = size_t;
155  std::unordered_map<SlotIndex, ArraySize> varlen_output_slot_map_;
156 };
int8_t getMinPaddedByteSize(const int8_t actual_min_byte_width) const
void alignPaddedSlots(const bool sort_on_gpu)
bool slotIsVarlen(const size_t slot_idx) const
int8_t logical_size
void sort_on_gpu(int64_t *val_buff, int32_t *idx_buff, const uint64_t entry_count, const bool desc, const uint32_t chosen_bytes, ThrustAllocator &alloc, const int device_id)
std::unordered_map< SlotIndex, ArraySize > varlen_output_slot_map_
void setPaddedSlotWidthBytes(const size_t slot_idx, const int8_t bytes)
size_t getAllSlotsPaddedSize() const
size_t getAllSlotsAlignedPaddedSize() const
std::vector< std::vector< size_t > > col_to_slot_map_
bool hasVarlenOutput() const
std::vector< SlotSize > slot_sizes_
void setAllSlotsSize(const int8_t slot_width_size)
std::string to_string(char const *&&v)
const SlotSize & getSlotInfo(const size_t slot_idx) const
size_t getColOnlyOffInBytes(const size_t slot_idx) const
void addColumnFlatBuffer(const int64_t flatbuffer_size)
size_t getCompactByteWidth() const
size_t getAlignedPaddedSizeForRange(const size_t end) const
bool operator==(const ColSlotContext &that) const
void validate() const
int64_t varlenOutputElementSize(const size_t slot_idx) const
void addColumn(const std::vector< std::tuple< int8_t, int8_t >> &slots_for_col)
#define CHECK_LT(x, y)
Definition: Logger.h:299
void setAllSlotsPaddedSize(const int8_t padded_size)
int8_t padded_size
bool operator==(const SlotSize &lhs, const SlotSize &rhs)
size_t getTotalBytesOfColumnarBuffers(const size_t entry_count) const
void addSlotForColumn(const int8_t logical_size, const size_t column_idx)
std::string toString() const
size_t getSlotCount() const
void setAllSlotsPaddedSizeToLogicalSize()
size_t getColCount() const
const std::vector< size_t > & getSlotsForCol(const size_t col_idx) const
bool operator!=(const ColSlotContext &that) const
void setAllUnsetSlotsPaddedSize(const int8_t padded_size)
int64_t getFlatBufferSize(const size_t slot_idx) const