OmniSciDB  d2f719934e
 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 2019 OmniSci, 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);
118  const QueryMemoryDescriptor& query_mem_desc,
119  const size_t entry_count,
120  const GpuGroupByBuffers& gpu_group_by_buffers,
121  const int device_id,
122  const unsigned block_size_x,
123  const unsigned grid_size_x);
124 
125  void copyGroupByBuffersFromGpu(DeviceAllocator& device_allocator,
126  const QueryMemoryDescriptor& query_mem_desc,
127  const size_t entry_count,
128  const GpuGroupByBuffers& gpu_group_by_buffers,
129  const RelAlgExecutionUnit* ra_exe_unit,
130  const unsigned block_size_x,
131  const unsigned grid_size_x,
132  const int device_id,
133  const bool prepend_index_buffer) const;
134 
135  private:
136  void initGroupByBuffer(int64_t* buffer,
137  const RelAlgExecutionUnit& ra_exe_unit,
138  const QueryMemoryDescriptor& query_mem_desc,
139  const ExecutorDeviceType device_type,
140  const bool output_columnar,
141  const Executor* executor);
142 
143  void initRowGroups(const QueryMemoryDescriptor& query_mem_desc,
144  int64_t* groups_buffer,
145  const std::vector<int64_t>& init_vals,
146  const int32_t groups_buffer_entry_count,
147  const size_t warp_size,
148  const Executor* executor);
149 
150  void initColumnarGroups(const QueryMemoryDescriptor& query_mem_desc,
151  int64_t* groups_buffer,
152  const std::vector<int64_t>& init_vals,
153  const Executor* executor);
154 
155  using QuantileParam = std::optional<double>;
156  void initColumnsPerRow(const QueryMemoryDescriptor& query_mem_desc,
157  int8_t* row_ptr,
158  const std::vector<int64_t>& init_vals,
159  const std::vector<int64_t>& bitmap_sizes,
160  const std::vector<QuantileParam>& quantile_params);
161 
162  void allocateCountDistinctGpuMem(const QueryMemoryDescriptor& query_mem_desc);
163 
164  std::vector<int64_t> allocateCountDistinctBuffers(
165  const QueryMemoryDescriptor& query_mem_desc,
166  const bool deferred,
167  const Executor* executor);
168 
169  int64_t allocateCountDistinctBitmap(const size_t bitmap_byte_sz);
170 
171  int64_t allocateCountDistinctSet();
172 
173  std::vector<QuantileParam> allocateTDigests(const QueryMemoryDescriptor& query_mem_desc,
174  const bool deferred,
175  const Executor* executor);
176 
178  const int8_t* init_agg_vals_dev_ptr,
179  const size_t n,
180  const int device_id,
181  const unsigned block_size_x,
182  const unsigned grid_size_x);
183 
185  const RelAlgExecutionUnit& ra_exe_unit,
186  const QueryMemoryDescriptor& query_mem_desc,
187  const int8_t* init_agg_vals_dev_ptr,
188  const int device_id,
189  const ExecutorDispatchMode dispatch_mode,
190  const unsigned block_size_x,
191  const unsigned grid_size_x,
192  const int8_t warp_size,
193  const bool can_sort_on_gpu,
194  const bool output_columnar,
195  RenderAllocator* render_allocator);
196 
197  size_t computeNumberOfBuffers(const QueryMemoryDescriptor& query_mem_desc,
198  const ExecutorDeviceType device_type,
199  const Executor* executor) const;
200 
201  void compactProjectionBuffersCpu(const QueryMemoryDescriptor& query_mem_desc,
202  const size_t projection_count);
203  void compactProjectionBuffersGpu(const QueryMemoryDescriptor& query_mem_desc,
204  Data_Namespace::DataMgr* data_mgr,
205  const GpuGroupByBuffers& gpu_group_by_buffers,
206  const size_t projection_count,
207  const int device_id);
208 
209  void applyStreamingTopNOffsetCpu(const QueryMemoryDescriptor& query_mem_desc,
210  const RelAlgExecutionUnit& ra_exe_unit);
211 
213  const QueryMemoryDescriptor& query_mem_desc,
214  const GpuGroupByBuffers& gpu_group_by_buffers,
215  const RelAlgExecutionUnit& ra_exe_unit,
216  const unsigned total_thread_count,
217  const int device_id);
218 
219  std::shared_ptr<VarlenOutputInfo> getVarlenOutputInfo();
220 
221  const int64_t num_rows_;
222  std::shared_ptr<RowSetMemoryOwner> row_set_mem_owner_;
223  std::vector<std::unique_ptr<ResultSet>> result_sets_;
224 
225  std::vector<int64_t> init_agg_vals_;
226 
227  size_t num_buffers_;
228  std::vector<int64_t*> group_by_buffers_;
229  std::shared_ptr<VarlenOutputInfo> varlen_output_info_;
232 
237 
239  std::vector<Data_Namespace::AbstractBuffer*> temporary_buffers_;
240 
241  const size_t thread_idx_;
242 
243  friend class Executor; // Accesses result_sets_
244  friend class QueryExecutionContext;
245 };
#define CHECK_EQ(x, y)
Definition: Logger.h:219
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
std::vector< int64_t > allocateCountDistinctBuffers(const QueryMemoryDescriptor &query_mem_desc, const bool deferred, const Executor *executor)
unsigned long long CUdeviceptr
Definition: nocuda.h:27
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:221
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
GpuGroupByBuffers setupTableFunctionGpuBuffers(const QueryMemoryDescriptor &query_mem_desc, const int device_id, const unsigned block_size_x, const unsigned grid_size_x)
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.
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)
std::shared_ptr< VarlenOutputInfo > varlen_output_info_
constexpr double n
Definition: Utm.h:39
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)