OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
QueryMemoryDescriptor.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, 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 
25 #ifndef QUERYENGINE_QUERYMEMORYDESCRIPTOR_H
26 #define QUERYENGINE_QUERYMEMORYDESCRIPTOR_H
27 
28 #include "../CompilationOptions.h"
29 #include "../CountDistinct.h"
30 #include "ColSlotContext.h"
31 #include "Types.h"
32 
33 #include <boost/optional.hpp>
34 #include "Shared/Logger.h"
35 
36 #include <algorithm>
37 #include <cstddef>
38 #include <cstdint>
39 #include <memory>
40 #include <numeric>
41 #include <unordered_map>
42 #include <vector>
43 
44 #include <Shared/SqlTypesLayout.h>
45 #include <Shared/TargetInfo.h>
46 
47 extern bool g_cluster;
48 
49 class Executor;
51 class RenderInfo;
52 class RowSetMemoryOwner;
53 struct InputTableInfo;
54 
55 // Shared: threads in the same block share memory, atomic operations required
56 // SharedForKeylessOneColumnKnownRange: special case of "Shared", but for keyless
57 // aggregates with single column group by
59 
60 struct RelAlgExecutionUnit;
61 class TResultSetBufferDescriptor;
63 struct ColRangeInfo;
64 struct KeylessInfo;
65 
67  public:
69 
70  // constructor for init call
71  QueryMemoryDescriptor(const Executor* executor,
72  const RelAlgExecutionUnit& ra_exe_unit,
73  const std::vector<InputTableInfo>& query_infos,
74  const bool allow_multifrag,
75  const bool keyless_hash,
76  const bool interleaved_bins_on_gpu,
77  const int32_t idx_target_as_key,
78  const ColRangeInfo& col_range_info,
79  const ColSlotContext& col_slot_context,
80  const std::vector<int8_t>& group_col_widths,
81  const int8_t group_col_compact_width,
82  const std::vector<ssize_t>& target_groupby_indices,
83  const size_t entry_count,
84  const GroupByMemSharing sharing,
85  const bool shared_mem_for_group_by,
86  const CountDistinctDescriptors count_distinct_descriptors,
87  const bool sort_on_gpu_hint,
88  const bool output_columnar,
89  const bool render_output,
90  const bool must_use_baseline_sort);
91 
92  QueryMemoryDescriptor(const Executor* executor,
93  const size_t entry_count,
94  const QueryDescriptionType query_desc_type,
95  const bool is_table_function);
96 
97  QueryMemoryDescriptor(const QueryDescriptionType query_desc_type,
98  const int64_t min_val,
99  const int64_t max_val,
100  const bool has_nulls,
101  const std::vector<int8_t>& group_col_widths);
102 
103  // Serialization
104  QueryMemoryDescriptor(const TResultSetBufferDescriptor& thrift_query_memory_descriptor);
105  static TResultSetBufferDescriptor toThrift(const QueryMemoryDescriptor&);
106 
107  bool operator==(const QueryMemoryDescriptor& other) const;
108 
109  static std::unique_ptr<QueryMemoryDescriptor> init(
110  const Executor* executor,
111  const RelAlgExecutionUnit& ra_exe_unit,
112  const std::vector<InputTableInfo>& query_infos,
113  const ColRangeInfo& col_range_info,
114  const KeylessInfo& keyless_info,
115  const bool allow_multifrag,
116  const ExecutorDeviceType device_type,
117  const int8_t crt_min_byte_width,
118  const bool sort_on_gpu_hint,
119  const size_t shard_count,
120  const size_t max_groups_buffer_entry_count,
121  RenderInfo* render_info,
122  const CountDistinctDescriptors count_distinct_descriptors,
123  const bool must_use_baseline_sort,
124  const bool output_columnar_hint);
125 
126  std::unique_ptr<QueryExecutionContext> getQueryExecutionContext(
127  const RelAlgExecutionUnit&,
128  const Executor* executor,
129  const ExecutorDeviceType device_type,
130  const ExecutorDispatchMode dispatch_mode,
131  const int device_id,
132  const int64_t num_rows,
133  const std::vector<std::vector<const int8_t*>>& col_buffers,
134  const std::vector<std::vector<uint64_t>>& frag_offsets,
135  std::shared_ptr<RowSetMemoryOwner>,
136  const bool output_columnar,
137  const bool sort_on_gpu,
138  RenderInfo*) const;
139 
140  static bool many_entries(const int64_t max_val,
141  const int64_t min_val,
142  const int64_t bucket) {
143  return max_val - min_val > 10000 * std::max(bucket, int64_t(1));
144  }
145 
147  const CountDistinctDescriptors& count_distinct_descriptors) {
148  return std::all_of(count_distinct_descriptors.begin(),
149  count_distinct_descriptors.end(),
150  [](const CountDistinctDescriptor& desc) {
151  return desc.impl_type_ == CountDistinctImplType::Invalid;
152  });
153  }
154 
157  }
158 
159  static int8_t pick_target_compact_width(const RelAlgExecutionUnit& ra_exe_unit,
160  const std::vector<InputTableInfo>& query_infos,
161  const int8_t crt_min_byte_width);
162 
163  // Getters and Setters
164  const Executor* getExecutor() const { return executor_; }
165 
170  getGroupbyColCount() == 1;
171  }
172 
173  bool hasKeylessHash() const { return keyless_hash_; }
174  void setHasKeylessHash(const bool val) { keyless_hash_ = val; }
175 
177  void setHasInterleavedBinsOnGpu(const bool val) { interleaved_bins_on_gpu_ = val; }
178 
179  int32_t getTargetIdxForKey() const { return idx_target_as_key_; }
180  void setTargetIdxForKey(const int32_t val) { idx_target_as_key_ = val; }
181 
182  int8_t groupColWidth(const size_t key_idx) const {
183  CHECK_LT(key_idx, group_col_widths_.size());
184  return group_col_widths_[key_idx];
185  }
186  size_t getPrependedGroupColOffInBytes(const size_t group_idx) const;
187  size_t getPrependedGroupBufferSizeInBytes() const;
188 
189  const auto groupColWidthsBegin() const { return group_col_widths_.begin(); }
190  const auto groupColWidthsEnd() const { return group_col_widths_.end(); }
192 
193  bool isGroupBy() const { return !group_col_widths_.empty(); }
194 
195  void setGroupColCompactWidth(const int8_t val) { group_col_compact_width_ = val; }
196 
197  size_t getColCount() const;
198  size_t getSlotCount() const;
199 
200  const int8_t getPaddedSlotWidthBytes(const size_t slot_idx) const;
201  const int8_t getLogicalSlotWidthBytes(const size_t slot_idx) const;
202 
203  const int8_t getSlotIndexForSingleSlotCol(const size_t col_idx) const;
204 
205  size_t getPaddedColWidthForRange(const size_t offset, const size_t range) const {
206  size_t ret = 0;
207  for (size_t i = offset; i < offset + range; i++) {
208  ret += static_cast<size_t>(getPaddedSlotWidthBytes(i));
209  }
210  return ret;
211  }
212 
213  void useConsistentSlotWidthSize(const int8_t slot_width_size);
214  size_t getRowWidth() const;
215 
216  int8_t updateActualMinByteWidth(const int8_t actual_min_byte_width) const;
217 
218  void addColSlotInfo(const std::vector<std::tuple<int8_t, int8_t>>& slots_for_col);
219 
220  void clearSlotInfo();
221 
222  void alignPaddedSlots();
223 
224  ssize_t getTargetGroupbyIndex(const size_t target_idx) const {
225  CHECK_LT(target_idx, target_groupby_indices_.size());
226  return target_groupby_indices_[target_idx];
227  }
228 
229  void setAllTargetGroupbyIndices(std::vector<ssize_t> group_by_indices) {
230  target_groupby_indices_ = group_by_indices;
231  }
232 
233  size_t targetGroupbyIndicesSize() const { return target_groupby_indices_.size(); }
235  return std::count_if(
236  target_groupby_indices_.begin(),
238  [](const ssize_t& target_group_by_index) { return target_group_by_index < 0; });
239  }
241 
242  size_t getEntryCount() const { return entry_count_; }
243  void setEntryCount(const size_t val) { entry_count_ = val; }
244 
245  int64_t getMinVal() const { return min_val_; }
246  int64_t getMaxVal() const { return max_val_; }
247  int64_t getBucket() const { return bucket_; }
248 
249  bool hasNulls() const { return has_nulls_; }
251 
252  const CountDistinctDescriptor& getCountDistinctDescriptor(const size_t idx) const {
254  return count_distinct_descriptors_[idx];
255  }
257  return count_distinct_descriptors_.size();
258  }
259 
260  bool sortOnGpu() const { return sort_on_gpu_; }
261 
262  bool canOutputColumnar() const;
263  bool didOutputColumnar() const { return output_columnar_; }
264  void setOutputColumnar(const bool val);
265 
266  bool isLogicalSizedColumnsAllowed() const;
267 
269 
270  // TODO(adb): remove and store this info more naturally in another
271  // member
272  bool forceFourByteFloat() const { return force_4byte_float_; }
273  void setForceFourByteFloat(const bool val) { force_4byte_float_ = val; }
274 
275  // Getters derived from state
276  size_t getGroupbyColCount() const { return group_col_widths_.size(); }
277  size_t getKeyCount() const { return keyless_hash_ ? 0 : getGroupbyColCount(); }
278  size_t getBufferColSlotCount() const;
279 
280  size_t getBufferSizeBytes(const RelAlgExecutionUnit& ra_exe_unit,
281  const unsigned thread_count,
282  const ExecutorDeviceType device_type) const;
283  size_t getBufferSizeBytes(const ExecutorDeviceType device_type) const;
284  size_t getBufferSizeBytes(const ExecutorDeviceType device_type,
285  const size_t override_entry_count) const;
286 
288 
289  // TODO(alex): remove
290  bool usesGetGroupValueFast() const;
291 
292  bool blocksShareMemory() const;
293  bool threadsShareMemory() const;
294 
295  bool lazyInitGroups(const ExecutorDeviceType) const;
296 
297  bool interleavedBins(const ExecutorDeviceType) const;
298 
299  size_t sharedMemBytes(const ExecutorDeviceType) const;
300 
301  size_t getColOffInBytes(const size_t col_idx) const;
302  size_t getColOffInBytesInNextBin(const size_t col_idx) const;
303  size_t getNextColOffInBytes(const int8_t* col_ptr,
304  const size_t bin,
305  const size_t col_idx) const;
306  size_t getColOnlyOffInBytes(const size_t col_idx) const;
307  size_t getRowSize() const;
308  size_t getColsSize() const;
309  size_t getWarpCount() const;
310 
311  size_t getCompactByteWidth() const;
312 
313  inline size_t getEffectiveKeyWidth() const {
314  return group_col_compact_width_ ? group_col_compact_width_ : sizeof(int64_t);
315  }
316 
317  bool isWarpSyncRequired(const ExecutorDeviceType) const;
318 
319  std::string toString() const;
320 
321  std::string reductionKey() const;
322 
323  protected:
324  void resetGroupColWidths(const std::vector<int8_t>& new_group_col_widths) {
325  group_col_widths_ = new_group_col_widths;
326  }
327 
328  private:
334  int32_t idx_target_as_key_; // If keyless_hash_ enabled, then represents what target
335  // expression should be used to identify the key (e.g., in
336  // locating empty entries). Currently only valid with
337  // keyless_hash_ and single-column GroupByPerfectHash
338  std::vector<int8_t> group_col_widths_;
339  int8_t group_col_compact_width_; // compact width for all group
340  // cols if able to be consistent
341  // otherwise 0
342  std::vector<ssize_t> target_groupby_indices_;
343  size_t entry_count_; // the number of entries in the main buffer
344  int64_t min_val_; // meaningful for OneColKnownRange,
345  // MultiColPerfectHash only
346  int64_t max_val_;
347  int64_t bucket_;
349  GroupByMemSharing sharing_; // meaningful for GPU only
356 
358 
360 
361  size_t getTotalBytesOfColumnarBuffers() const;
362  size_t getTotalBytesOfColumnarBuffers(const size_t num_entries_per_column) const;
363  size_t getTotalBytesOfColumnarProjections(const size_t projection_count) const;
364 
365  friend class ResultSet;
366  friend class QueryExecutionContext;
367 
368  template <typename META_CLASS_TYPE>
370 };
371 
372 inline void set_notnull(TargetInfo& target, const bool not_null) {
373  target.skip_null_val = !not_null;
374  auto new_type = get_compact_type(target);
375  new_type.set_notnull(not_null);
376  set_compact_type(target, new_type);
377 }
378 
379 std::vector<TargetInfo> target_exprs_to_infos(
380  const std::vector<Analyzer::Expr*>& targets,
382 
383 #endif // QUERYENGINE_QUERYMEMORYDESCRIPTOR_H
void set_compact_type(TargetInfo &target, const SQLTypeInfo &new_type)
static bool many_entries(const int64_t max_val, const int64_t min_val, const int64_t bucket)
QueryDescriptionType
Definition: Types.h:26
size_t getBufferSizeBytes(const RelAlgExecutionUnit &ra_exe_unit, const unsigned thread_count, const ExecutorDeviceType device_type) const
bool countDistinctDescriptorsLogicallyEmpty() const
const int8_t const int64_t * num_rows
size_t getTotalBytesOfColumnarProjections(const size_t projection_count) const
void setEntryCount(const size_t val)
bool g_cluster
Catch-all for publicly accessible types utilized in various Query Engine Descriptors.
ExecutorDeviceType
std::unique_ptr< QueryExecutionContext > getQueryExecutionContext(const RelAlgExecutionUnit &, const Executor *executor, const ExecutorDeviceType device_type, const ExecutorDispatchMode dispatch_mode, const int device_id, const int64_t num_rows, const std::vector< std::vector< const int8_t * >> &col_buffers, const std::vector< std::vector< uint64_t >> &frag_offsets, std::shared_ptr< RowSetMemoryOwner >, const bool output_columnar, const bool sort_on_gpu, RenderInfo *) const
std::string toString() const
bool isLogicalSizedColumnsAllowed() const
void setHasKeylessHash(const bool val)
void setGroupColCompactWidth(const int8_t val)
void setAllTargetGroupbyIndices(std::vector< ssize_t > group_by_indices)
void setOutputColumnar(const bool val)
size_t getNextColOffInBytes(const int8_t *col_ptr, const size_t bin, const size_t col_idx) const
size_t getEffectiveKeyWidth() const
bool skip_null_val
Definition: TargetInfo.h:44
void setQueryDescriptionType(const QueryDescriptionType val)
size_t targetGroupbyNegativeIndicesSize() const
bool hasInterleavedBinsOnGpu() const
const auto groupColWidthsBegin() const
void useConsistentSlotWidthSize(const int8_t slot_width_size)
size_t getColOnlyOffInBytes(const size_t col_idx) const
ExecutorDispatchMode
const SQLTypeInfo get_compact_type(const TargetInfo &target)
const auto groupColWidthsEnd() const
int8_t groupColWidth(const size_t key_idx) const
void setTargetIdxForKey(const int32_t val)
std::vector< CountDistinctDescriptor > CountDistinctDescriptors
Definition: CountDistinct.h:35
Provides column info and slot info for the output buffer and some metadata helpers.
size_t getGroupbyColCount() const
bool lazyInitGroups(const ExecutorDeviceType) const
size_t targetGroupbyIndicesSize() const
size_t getPrependedGroupBufferSizeInBytes() const
size_t getTotalBytesOfColumnarBuffers() const
static int8_t pick_target_compact_width(const RelAlgExecutionUnit &ra_exe_unit, const std::vector< InputTableInfo > &query_infos, const int8_t crt_min_byte_width)
CountDistinctDescriptors count_distinct_descriptors_
size_t getPaddedColWidthForRange(const size_t offset, const size_t range) const
const int8_t getPaddedSlotWidthBytes(const size_t slot_idx) const
static TResultSetBufferDescriptor toThrift(const QueryMemoryDescriptor &)
size_t getCountDistinctDescriptorsSize() const
ssize_t getTargetGroupbyIndex(const size_t target_idx) const
QueryDescriptionType getQueryDescriptionType() const
const CountDistinctDescriptor & getCountDistinctDescriptor(const size_t idx) const
size_t sharedMemBytes(const ExecutorDeviceType) const
#define CHECK_LT(x, y)
Definition: Logger.h:200
bool isSingleColumnGroupByWithPerfectHash() const
QueryDescriptionType query_desc_type_
int8_t updateActualMinByteWidth(const int8_t actual_min_byte_width) const
bool operator==(const QueryMemoryDescriptor &other) const
void setForceFourByteFloat(const bool val)
bool isWarpSyncRequired(const ExecutorDeviceType) const
bool interleavedBins(const ExecutorDeviceType) const
const ColSlotContext & getColSlotContext() const
GroupByMemSharing
std::vector< int8_t > group_col_widths_
void resetGroupColWidths(const std::vector< int8_t > &new_group_col_widths)
std::vector< TargetInfo > target_exprs_to_infos(const std::vector< Analyzer::Expr * > &targets, const QueryMemoryDescriptor &query_mem_desc)
GroupByMemSharing getGpuMemSharing() const
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)
static std::unique_ptr< QueryMemoryDescriptor > init(const Executor *executor, const RelAlgExecutionUnit &ra_exe_unit, const std::vector< InputTableInfo > &query_infos, const ColRangeInfo &col_range_info, const KeylessInfo &keyless_info, const bool allow_multifrag, const ExecutorDeviceType device_type, const int8_t crt_min_byte_width, const bool sort_on_gpu_hint, const size_t shard_count, const size_t max_groups_buffer_entry_count, RenderInfo *render_info, const CountDistinctDescriptors count_distinct_descriptors, const bool must_use_baseline_sort, const bool output_columnar_hint)
void addColSlotInfo(const std::vector< std::tuple< int8_t, int8_t >> &slots_for_col)
void setHasInterleavedBinsOnGpu(const bool val)
static bool countDescriptorsLogicallyEmpty(const CountDistinctDescriptors &count_distinct_descriptors)
const int8_t getSlotIndexForSingleSlotCol(const size_t col_idx) const
size_t getBufferColSlotCount() const
const int8_t getLogicalSlotWidthBytes(const size_t slot_idx) const
size_t getColOffInBytes(const size_t col_idx) const
size_t getColOffInBytesInNextBin(const size_t col_idx) const
std::vector< ssize_t > target_groupby_indices_
std::string reductionKey() const
const Executor * getExecutor() const
void set_notnull(TargetInfo &target, const bool not_null)
int32_t getTargetIdxForKey() const
size_t getPrependedGroupColOffInBytes(const size_t group_idx) const