OmniSciDB  cde582ebc3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
QueryMemoryInitializer.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 
17 #pragma once
18 
20 
22 #include "GpuMemUtils.h"
23 #include "ResultSet.h"
24 
26 
27 #include <memory>
28 
29 #ifdef HAVE_CUDA
30 #include <cuda.h>
31 #else
32 #include <Shared/nocuda.h>
33 #endif
34 
36  public:
37  // Row-based execution constructor
40  const int device_id,
41  const ExecutorDeviceType device_type,
42  const ExecutorDispatchMode dispatch_mode,
43  const bool output_columnar,
44  const bool sort_on_gpu,
45  const int outer_table_id,
46  const int64_t num_rows,
47  const std::vector<std::vector<const int8_t*>>& col_buffers,
48  const std::vector<std::vector<uint64_t>>& frag_offsets,
49  RenderAllocatorMap* render_allocator_map,
50  RenderInfo* render_info,
51  std::shared_ptr<RowSetMemoryOwner> row_set_mem_owner,
52  DeviceAllocator* gpu_allocator,
53  const size_t thread_idx,
54  const Executor* executor);
55 
56  // Table functions execution constructor
58  const QueryMemoryDescriptor& query_mem_desc,
59  const int device_id,
60  const ExecutorDeviceType device_type,
61  const int64_t num_rows,
62  const std::vector<std::vector<const int8_t*>>& col_buffers,
63  const std::vector<std::vector<uint64_t>>& frag_offsets,
64  std::shared_ptr<RowSetMemoryOwner> row_set_mem_owner,
65  DeviceAllocator* device_allocator,
66  const Executor* executor);
67 
69 
71 
72  const auto getCountDistinctBitmapBytes() const {
74  }
75 
76  // TODO: lazy init (maybe lazy init count distinct above, too?)
78 
79  const auto getVarlenOutputPtr() const { return varlen_output_buffer_; }
80 
81  ResultSet* getResultSet(const size_t index) const {
82  CHECK_LT(index, result_sets_.size());
83  return result_sets_[index].get();
84  }
85 
86  std::unique_ptr<ResultSet> getResultSetOwned(const size_t index) {
87  CHECK_LT(index, result_sets_.size());
88  return std::move(result_sets_[index]);
89  }
90 
91  void resetResultSet(const size_t index) {
92  CHECK_LT(index, result_sets_.size());
93  result_sets_[index].reset();
94  }
95 
96  int64_t getAggInitValForIndex(const size_t index) const {
97  CHECK_LT(index, init_agg_vals_.size());
98  return init_agg_vals_[index];
99  }
100 
101  const auto getGroupByBuffersPtr() {
102  return reinterpret_cast<int64_t**>(group_by_buffers_.data());
103  }
104 
105  const auto getGroupByBuffersSize() const { return group_by_buffers_.size(); }
106 
107  const auto getNumBuffers() const {
109  return num_buffers_;
110  }
111 
113  const QueryMemoryDescriptor& query_mem_desc,
114  const int device_id,
115  const unsigned block_size_x,
116  const unsigned grid_size_x,
117  const bool zero_initialize_buffers);
118 
120  const QueryMemoryDescriptor& query_mem_desc,
121  const size_t entry_count,
122  const GpuGroupByBuffers& gpu_group_by_buffers,
123  const int device_id,
124  const unsigned block_size_x,
125  const unsigned grid_size_x);
126 
127  void copyGroupByBuffersFromGpu(DeviceAllocator& device_allocator,
128  const QueryMemoryDescriptor& query_mem_desc,
129  const size_t entry_count,
130  const GpuGroupByBuffers& gpu_group_by_buffers,
131  const RelAlgExecutionUnit* ra_exe_unit,
132  const unsigned block_size_x,
133  const unsigned grid_size_x,
134  const int device_id,
135  const bool prepend_index_buffer) const;
136 
137  private:
138  void initGroupByBuffer(int64_t* buffer,
139  const RelAlgExecutionUnit& ra_exe_unit,
140  const QueryMemoryDescriptor& query_mem_desc,
141  const ExecutorDeviceType device_type,
142  const bool output_columnar,
143  const Executor* executor);
144 
145  void initRowGroups(const QueryMemoryDescriptor& query_mem_desc,
146  int64_t* groups_buffer,
147  const std::vector<int64_t>& init_vals,
148  const int32_t groups_buffer_entry_count,
149  const size_t warp_size,
150  const Executor* executor);
151 
152  void initColumnarGroups(const QueryMemoryDescriptor& query_mem_desc,
153  int64_t* groups_buffer,
154  const std::vector<int64_t>& init_vals,
155  const Executor* executor);
156 
157  using QuantileParam = std::optional<double>;
158  void initColumnsPerRow(const QueryMemoryDescriptor& query_mem_desc,
159  int8_t* row_ptr,
160  const std::vector<int64_t>& init_vals,
161  const std::vector<int64_t>& bitmap_sizes,
162  const std::vector<QuantileParam>& quantile_params);
163 
164  void allocateCountDistinctGpuMem(const QueryMemoryDescriptor& query_mem_desc);
165 
166  std::vector<int64_t> allocateCountDistinctBuffers(
167  const QueryMemoryDescriptor& query_mem_desc,
168  const bool deferred,
169  const Executor* executor);
170 
171  int64_t allocateCountDistinctBitmap(const size_t bitmap_byte_sz);
172 
173  int64_t allocateCountDistinctSet();
174 
175  std::vector<QuantileParam> allocateTDigests(const QueryMemoryDescriptor& query_mem_desc,
176  const bool deferred,
177  const Executor* executor);
178 
180  const int8_t* init_agg_vals_dev_ptr,
181  const size_t n,
182  const int device_id,
183  const unsigned block_size_x,
184  const unsigned grid_size_x);
185 
187  const RelAlgExecutionUnit& ra_exe_unit,
188  const QueryMemoryDescriptor& query_mem_desc,
189  const int8_t* init_agg_vals_dev_ptr,
190  const int device_id,
191  const ExecutorDispatchMode dispatch_mode,
192  const unsigned block_size_x,
193  const unsigned grid_size_x,
194  const int8_t warp_size,
195  const bool can_sort_on_gpu,
196  const bool output_columnar,
197  RenderAllocator* render_allocator);
198 
199  size_t computeNumberOfBuffers(const QueryMemoryDescriptor& query_mem_desc,
200  const ExecutorDeviceType device_type,
201  const Executor* executor) const;
202 
203  void compactProjectionBuffersCpu(const QueryMemoryDescriptor& query_mem_desc,
204  const size_t projection_count);
205  void compactProjectionBuffersGpu(const QueryMemoryDescriptor& query_mem_desc,
206  Data_Namespace::DataMgr* data_mgr,
207  const GpuGroupByBuffers& gpu_group_by_buffers,
208  const size_t projection_count,
209  const int device_id);
210 
211  void applyStreamingTopNOffsetCpu(const QueryMemoryDescriptor& query_mem_desc,
212  const RelAlgExecutionUnit& ra_exe_unit);
213 
215  const QueryMemoryDescriptor& query_mem_desc,
216  const GpuGroupByBuffers& gpu_group_by_buffers,
217  const RelAlgExecutionUnit& ra_exe_unit,
218  const unsigned total_thread_count,
219  const int device_id);
220 
221  std::shared_ptr<VarlenOutputInfo> getVarlenOutputInfo();
222 
223  const int64_t num_rows_;
224  std::shared_ptr<RowSetMemoryOwner> row_set_mem_owner_;
225  std::vector<std::unique_ptr<ResultSet>> result_sets_;
226 
227  std::vector<int64_t> init_agg_vals_;
228 
229  size_t num_buffers_;
230  std::vector<int64_t*> group_by_buffers_;
231  std::shared_ptr<VarlenOutputInfo> varlen_output_info_;
234 
239 
241  std::vector<Data_Namespace::AbstractBuffer*> temporary_buffers_;
242 
243  const size_t thread_idx_;
244 
245  friend class Executor; // Accesses result_sets_
246  friend class QueryExecutionContext;
247 };
GpuGroupByBuffers setupTableFunctionGpuBuffers(const QueryMemoryDescriptor &query_mem_desc, const int device_id, const unsigned block_size_x, const unsigned grid_size_x, const bool zero_initialize_buffers)
#define CHECK_EQ(x, y)
Definition: Logger.h:230
GpuGroupByBuffers prepareTopNHeapsDevBuffer(const QueryMemoryDescriptor &query_mem_desc, const int8_t *init_agg_vals_dev_ptr, const size_t n, const int device_id, const unsigned block_size_x, const unsigned grid_size_x)
void resetResultSet(const size_t index)
GpuGroupByBuffers createAndInitializeGroupByBufferGpu(const RelAlgExecutionUnit &ra_exe_unit, const QueryMemoryDescriptor &query_mem_desc, const int8_t *init_agg_vals_dev_ptr, const int device_id, const ExecutorDispatchMode dispatch_mode, const unsigned block_size_x, const unsigned grid_size_x, const int8_t warp_size, const bool can_sort_on_gpu, const bool output_columnar, RenderAllocator *render_allocator)
void initColumnsPerRow(const QueryMemoryDescriptor &query_mem_desc, int8_t *row_ptr, const std::vector< int64_t > &init_vals, const std::vector< int64_t > &bitmap_sizes, const std::vector< QuantileParam > &quantile_params)
DeviceAllocator * device_allocator_
ExecutorDeviceType
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::vector< int64_t > allocateCountDistinctBuffers(const QueryMemoryDescriptor &query_mem_desc, const bool deferred, const Executor *executor)
unsigned long long CUdeviceptr
Definition: nocuda.h:28
std::optional< double > QuantileParam
size_t computeNumberOfBuffers(const QueryMemoryDescriptor &query_mem_desc, const ExecutorDeviceType device_type, const Executor *executor) const
std::vector< QuantileParam > allocateTDigests(const QueryMemoryDescriptor &query_mem_desc, const bool deferred, const Executor *executor)
std::shared_ptr< RowSetMemoryOwner > row_set_mem_owner_
ExecutorDispatchMode
void compactProjectionBuffersGpu(const QueryMemoryDescriptor &query_mem_desc, Data_Namespace::DataMgr *data_mgr, const GpuGroupByBuffers &gpu_group_by_buffers, const size_t projection_count, const int device_id)
std::vector< int64_t > init_agg_vals_
std::unique_ptr< ResultSet > getResultSetOwned(const size_t index)
void applyStreamingTopNOffsetCpu(const QueryMemoryDescriptor &query_mem_desc, const RelAlgExecutionUnit &ra_exe_unit)
int64_t getAggInitValForIndex(const size_t index) const
const auto getCountDistinctBitmapPtr() const
QueryMemoryInitializer(const RelAlgExecutionUnit &ra_exe_unit, const QueryMemoryDescriptor &query_mem_desc, const int device_id, const ExecutorDeviceType device_type, const ExecutorDispatchMode dispatch_mode, const bool output_columnar, const bool sort_on_gpu, const int outer_table_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, RenderAllocatorMap *render_allocator_map, RenderInfo *render_info, std::shared_ptr< RowSetMemoryOwner > row_set_mem_owner, DeviceAllocator *gpu_allocator, const size_t thread_idx, const Executor *executor)
void initGroupByBuffer(int64_t *buffer, const RelAlgExecutionUnit &ra_exe_unit, const QueryMemoryDescriptor &query_mem_desc, const ExecutorDeviceType device_type, const bool output_columnar, const Executor *executor)
void compactProjectionBuffersCpu(const QueryMemoryDescriptor &query_mem_desc, const size_t projection_count)
std::vector< int64_t * > group_by_buffers_
void initColumnarGroups(const QueryMemoryDescriptor &query_mem_desc, int64_t *groups_buffer, const std::vector< int64_t > &init_vals, const Executor *executor)
void copyGroupByBuffersFromGpu(DeviceAllocator &device_allocator, const QueryMemoryDescriptor &query_mem_desc, const size_t entry_count, const GpuGroupByBuffers &gpu_group_by_buffers, const RelAlgExecutionUnit *ra_exe_unit, const unsigned block_size_x, const unsigned grid_size_x, const int device_id, const bool prepend_index_buffer) const
#define CHECK_LT(x, y)
Definition: Logger.h:232
std::shared_ptr< VarlenOutputInfo > getVarlenOutputInfo()
const auto getNumBuffers() const
Abstract class for managing device memory allocations.
const auto getCountDistinctBitmapBytes() const
Descriptor for the result set buffer layout.
const auto getVarlenOutputPtr() const
std::vector< Data_Namespace::AbstractBuffer * > temporary_buffers_
void copyFromTableFunctionGpuBuffers(Data_Namespace::DataMgr *data_mgr, const QueryMemoryDescriptor &query_mem_desc, const size_t entry_count, const GpuGroupByBuffers &gpu_group_by_buffers, const int device_id, const unsigned block_size_x, const unsigned grid_size_x)
void applyStreamingTopNOffsetGpu(Data_Namespace::DataMgr *data_mgr, const QueryMemoryDescriptor &query_mem_desc, const GpuGroupByBuffers &gpu_group_by_buffers, const RelAlgExecutionUnit &ra_exe_unit, const unsigned total_thread_count, const int device_id)
const auto getGroupByBuffersSize() const
Basic constructors and methods of the row set interface.
std::shared_ptr< VarlenOutputInfo > varlen_output_info_
constexpr double n
Definition: Utm.h:38
ResultSet * getResultSet(const size_t index) const
std::vector< std::unique_ptr< ResultSet > > result_sets_
void allocateCountDistinctGpuMem(const QueryMemoryDescriptor &query_mem_desc)
const auto getVarlenOutputHostPtr() const
int64_t allocateCountDistinctBitmap(const size_t bitmap_byte_sz)
const auto getCountDistinctHostPtr() const
void initRowGroups(const QueryMemoryDescriptor &query_mem_desc, int64_t *groups_buffer, const std::vector< int64_t > &init_vals, const int32_t groups_buffer_entry_count, const size_t warp_size, const Executor *executor)