OmniSciDB  8fa3bf436f
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 int64_t num_rows,
46  const std::vector<std::vector<const int8_t*>>& col_buffers,
47  const std::vector<std::vector<uint64_t>>& frag_offsets,
48  RenderAllocatorMap* render_allocator_map,
49  RenderInfo* render_info,
50  std::shared_ptr<RowSetMemoryOwner> row_set_mem_owner,
51  DeviceAllocator* gpu_allocator,
52  const size_t thread_idx,
53  const Executor* executor);
54 
55  // Table functions execution constructor
57  const QueryMemoryDescriptor& query_mem_desc,
58  const int device_id,
59  const ExecutorDeviceType device_type,
60  const int64_t num_rows,
61  const std::vector<std::vector<const int8_t*>>& col_buffers,
62  const std::vector<std::vector<uint64_t>>& frag_offsets,
63  std::shared_ptr<RowSetMemoryOwner> row_set_mem_owner,
64  DeviceAllocator* device_allocator,
65  const Executor* executor);
66 
68 
70 
71  const auto getCountDistinctBitmapBytes() const {
73  }
74 
75  ResultSet* getResultSet(const size_t index) const {
76  CHECK_LT(index, result_sets_.size());
77  return result_sets_[index].get();
78  }
79 
80  std::unique_ptr<ResultSet> getResultSetOwned(const size_t index) {
81  CHECK_LT(index, result_sets_.size());
82  return std::move(result_sets_[index]);
83  }
84 
85  void resetResultSet(const size_t index) {
86  CHECK_LT(index, result_sets_.size());
87  result_sets_[index].reset();
88  }
89 
90  int64_t getAggInitValForIndex(const size_t index) const {
91  CHECK_LT(index, init_agg_vals_.size());
92  return init_agg_vals_[index];
93  }
94 
95  const auto getGroupByBuffersPtr() {
96  return reinterpret_cast<int64_t**>(group_by_buffers_.data());
97  }
98 
99  const auto getGroupByBuffersSize() const { return group_by_buffers_.size(); }
100 
101  const auto getNumBuffers() const {
103  return num_buffers_;
104  }
105 
106 #ifdef HAVE_CUDA
107  GpuGroupByBuffers setupTableFunctionGpuBuffers(
108  const QueryMemoryDescriptor& query_mem_desc,
109  const int device_id,
110  const unsigned block_size_x,
111  const unsigned grid_size_x);
112  void copyFromTableFunctionGpuBuffers(Data_Namespace::DataMgr* data_mgr,
113  const QueryMemoryDescriptor& query_mem_desc,
114  const size_t entry_count,
115  const GpuGroupByBuffers& gpu_group_by_buffers,
116  const int device_id,
117  const unsigned block_size_x,
118  const unsigned grid_size_x);
119 #endif
120 
122  const QueryMemoryDescriptor& query_mem_desc,
123  const size_t entry_count,
124  const GpuGroupByBuffers& gpu_group_by_buffers,
125  const RelAlgExecutionUnit* ra_exe_unit,
126  const unsigned block_size_x,
127  const unsigned grid_size_x,
128  const int device_id,
129  const bool prepend_index_buffer) const;
130 
131  private:
132  void initGroupByBuffer(int64_t* buffer,
133  const RelAlgExecutionUnit& ra_exe_unit,
134  const QueryMemoryDescriptor& query_mem_desc,
135  const ExecutorDeviceType device_type,
136  const bool output_columnar,
137  const Executor* executor);
138 
139  void initRowGroups(const QueryMemoryDescriptor& query_mem_desc,
140  int64_t* groups_buffer,
141  const std::vector<int64_t>& init_vals,
142  const int32_t groups_buffer_entry_count,
143  const size_t warp_size,
144  const Executor* executor);
145 
146  void initColumnarGroups(const QueryMemoryDescriptor& query_mem_desc,
147  int64_t* groups_buffer,
148  const std::vector<int64_t>& init_vals,
149  const Executor* executor);
150 
151  void initColumnsPerRow(const QueryMemoryDescriptor& query_mem_desc,
152  int8_t* row_ptr,
153  const std::vector<int64_t>& init_vals,
154  const std::vector<int64_t>& bitmap_sizes,
155  const std::vector<bool>& tdigest_deferred);
156 
157  void allocateCountDistinctGpuMem(const QueryMemoryDescriptor& query_mem_desc);
158 
159  std::vector<int64_t> allocateCountDistinctBuffers(
160  const QueryMemoryDescriptor& query_mem_desc,
161  const bool deferred,
162  const Executor* executor);
163 
164  int64_t allocateCountDistinctBitmap(const size_t bitmap_byte_sz);
165 
166  int64_t allocateCountDistinctSet();
167 
168  std::vector<bool> allocateTDigests(const QueryMemoryDescriptor& query_mem_desc,
169  const bool deferred,
170  const Executor* executor);
171 
172 #ifdef HAVE_CUDA
173  GpuGroupByBuffers prepareTopNHeapsDevBuffer(const QueryMemoryDescriptor& query_mem_desc,
174  const CUdeviceptr init_agg_vals_dev_ptr,
175  const size_t n,
176  const int device_id,
177  const unsigned block_size_x,
178  const unsigned grid_size_x);
179 
180  GpuGroupByBuffers createAndInitializeGroupByBufferGpu(
181  const RelAlgExecutionUnit& ra_exe_unit,
182  const QueryMemoryDescriptor& query_mem_desc,
183  const CUdeviceptr init_agg_vals_dev_ptr,
184  const int device_id,
185  const ExecutorDispatchMode dispatch_mode,
186  const unsigned block_size_x,
187  const unsigned grid_size_x,
188  const int8_t warp_size,
189  const bool can_sort_on_gpu,
190  const bool output_columnar,
191  RenderAllocator* render_allocator);
192 #endif
193 
194  size_t computeNumberOfBuffers(const QueryMemoryDescriptor& query_mem_desc,
195  const ExecutorDeviceType device_type,
196  const Executor* executor) const;
197 
198  void compactProjectionBuffersCpu(const QueryMemoryDescriptor& query_mem_desc,
199  const size_t projection_count);
200  void compactProjectionBuffersGpu(const QueryMemoryDescriptor& query_mem_desc,
201  Data_Namespace::DataMgr* data_mgr,
202  const GpuGroupByBuffers& gpu_group_by_buffers,
203  const size_t projection_count,
204  const int device_id);
205 
206  void applyStreamingTopNOffsetCpu(const QueryMemoryDescriptor& query_mem_desc,
207  const RelAlgExecutionUnit& ra_exe_unit);
208 
210  const QueryMemoryDescriptor& query_mem_desc,
211  const GpuGroupByBuffers& gpu_group_by_buffers,
212  const RelAlgExecutionUnit& ra_exe_unit,
213  const unsigned total_thread_count,
214  const int device_id);
215 
216  const int64_t num_rows_;
217  std::shared_ptr<RowSetMemoryOwner> row_set_mem_owner_;
218  std::vector<std::unique_ptr<ResultSet>> result_sets_;
219 
220  std::vector<int64_t> init_agg_vals_;
221 
222  const size_t num_buffers_;
223  std::vector<int64_t*> group_by_buffers_;
224 
229 
231  std::vector<Data_Namespace::AbstractBuffer*> temporary_buffers_;
232 
233  const size_t thread_idx_;
234 
235  friend class Executor; // Accesses result_sets_
236  friend class QueryExecutionContext;
237 };
#define CHECK_EQ(x, y)
Definition: Logger.h:211
void resetResultSet(const size_t index)
DeviceAllocator * device_allocator_
ExecutorDeviceType
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< bool > &tdigest_deferred)
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::vector< bool > allocateTDigests(const QueryMemoryDescriptor &query_mem_desc, const bool deferred, const Executor *executor)
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 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)
size_t computeNumberOfBuffers(const QueryMemoryDescriptor &query_mem_desc, const ExecutorDeviceType device_type, const Executor *executor) const
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
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)
#define CHECK_LT(x, y)
Definition: Logger.h:213
const auto getNumBuffers() const
Abstract class for managing device memory allocations.
const auto getCountDistinctBitmapBytes() const
Descriptor for the result set buffer layout.
void copyGroupByBuffersFromGpu(Data_Namespace::DataMgr *data_mgr, 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
std::vector< Data_Namespace::AbstractBuffer * > temporary_buffers_
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)
ResultSet * getResultSet(const size_t index) const
std::vector< std::unique_ptr< ResultSet > > result_sets_
void allocateCountDistinctGpuMem(const QueryMemoryDescriptor &query_mem_desc)
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)