OmniSciDB  7bf56492aa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
QueryExecutionContext Class Reference

#include <QueryExecutionContext.h>

+ Inheritance diagram for QueryExecutionContext:
+ Collaboration diagram for QueryExecutionContext:

Public Member Functions

 QueryExecutionContext (const RelAlgExecutionUnit &ra_exe_unit, const QueryMemoryDescriptor &, 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 > row_set_mem_owner, const bool output_columnar, const bool sort_on_gpu, RenderInfo *)
 
ResultSetPtr getRowSet (const RelAlgExecutionUnit &ra_exe_unit, const QueryMemoryDescriptor &query_mem_desc) const
 
ResultSetPtr groupBufferToResults (const size_t i) const
 
std::vector< int64_t * > launchGpuCode (const RelAlgExecutionUnit &ra_exe_unit, const std::vector< std::pair< void *, void * >> &cu_functions, const bool hoist_literals, const std::vector< int8_t > &literal_buff, std::vector< std::vector< const int8_t * >> col_buffers, const std::vector< std::vector< int64_t >> &num_rows, const std::vector< std::vector< uint64_t >> &frag_row_offsets, const int32_t scan_limit, Data_Namespace::DataMgr *data_mgr, const unsigned block_size_x, const unsigned grid_size_x, const int device_id, int32_t *error_code, const uint32_t num_tables, const std::vector< int64_t > &join_hash_tables, RenderAllocatorMap *render_allocator_map)
 
std::vector< int64_t * > launchCpuCode (const RelAlgExecutionUnit &ra_exe_unit, const std::vector< std::pair< void *, void * >> &fn_ptrs, const bool hoist_literals, const std::vector< int8_t > &literal_buff, std::vector< std::vector< const int8_t * >> col_buffers, const std::vector< std::vector< int64_t >> &num_rows, const std::vector< std::vector< uint64_t >> &frag_row_offsets, const int32_t scan_limit, int32_t *error_code, const uint32_t num_tables, const std::vector< int64_t > &join_hash_tables)
 
int64_t getAggInitValForIndex (const size_t index) const
 

Private Member Functions

ResultSetPtr groupBufferToDeinterleavedResults (const size_t i) const
 

Private Attributes

std::unique_ptr< CudaAllocatorgpu_allocator_
 
const QueryMemoryDescriptor query_mem_desc_
 
const Executorexecutor_
 
const ExecutorDeviceType device_type_
 
const ExecutorDispatchMode dispatch_mode_
 
std::shared_ptr
< RowSetMemoryOwner
row_set_mem_owner_
 
const bool output_columnar_
 
std::unique_ptr
< QueryMemoryInitializer
query_buffers_
 
std::unique_ptr< ResultSetestimator_result_set_
 

Friends

class Executor
 

Detailed Description

Definition at line 35 of file QueryExecutionContext.h.

Constructor & Destructor Documentation

QueryExecutionContext::QueryExecutionContext ( const RelAlgExecutionUnit ra_exe_unit,
const QueryMemoryDescriptor query_mem_desc,
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 row_set_mem_owner,
const bool  output_columnar,
const bool  sort_on_gpu,
RenderInfo render_info 
)

Definition at line 29 of file QueryExecutionContext.cpp.

References CHECK(), GPU, gpu_allocator_, RenderInfo::isPotentialInSituRender(), num_rows, query_buffers_, query_mem_desc, RenderInfo::render_allocator_map_ptr, and sort_on_gpu().

43  : query_mem_desc_(query_mem_desc)
44  , executor_(executor)
45  , device_type_(device_type)
46  , dispatch_mode_(dispatch_mode)
47  , row_set_mem_owner_(row_set_mem_owner)
48  , output_columnar_(output_columnar) {
49  CHECK(executor);
50  auto& data_mgr = executor->catalog_->getDataMgr();
51  if (device_type == ExecutorDeviceType::GPU) {
52  gpu_allocator_ = std::make_unique<CudaAllocator>(&data_mgr, device_id);
53  }
54 
55  auto render_allocator_map = render_info && render_info->isPotentialInSituRender()
56  ? render_info->render_allocator_map_ptr.get()
57  : nullptr;
58  query_buffers_ = std::make_unique<QueryMemoryInitializer>(ra_exe_unit,
60  device_id,
61  device_type,
62  dispatch_mode,
63  output_columnar,
65  num_rows,
66  col_buffers,
67  frag_offsets,
68  render_allocator_map,
69  render_info,
70  row_set_mem_owner,
71  gpu_allocator_.get(),
72  executor);
73 }
const int8_t const int64_t * num_rows
const ExecutorDispatchMode dispatch_mode_
const ExecutorDeviceType device_type_
std::unique_ptr< QueryMemoryInitializer > query_buffers_
CHECK(cgen_state)
std::shared_ptr< RowSetMemoryOwner > row_set_mem_owner_
std::unique_ptr< RenderAllocatorMap > render_allocator_map_ptr
Definition: RenderInfo.h:32
std::unique_ptr< CudaAllocator > gpu_allocator_
bool isPotentialInSituRender() const
Definition: RenderInfo.cpp:61
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 QueryMemoryDescriptor query_mem_desc_

+ Here is the call graph for this function:

Member Function Documentation

int64_t QueryExecutionContext::getAggInitValForIndex ( const size_t  index) const

Definition at line 126 of file QueryExecutionContext.cpp.

References CHECK(), and query_buffers_.

Referenced by Executor::executePlanWithoutGroupBy().

126  {
128  return query_buffers_->getAggInitValForIndex(index);
129 }
std::unique_ptr< QueryMemoryInitializer > query_buffers_
CHECK(cgen_state)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ResultSetPtr QueryExecutionContext::getRowSet ( const RelAlgExecutionUnit ra_exe_unit,
const QueryMemoryDescriptor query_mem_desc 
) const

Definition at line 131 of file QueryExecutionContext.cpp.

References CHECK(), CHECK_EQ, CPU, device_type_, executor_, GPU, groupBufferToResults(), query_buffers_, query_mem_desc_, row_set_mem_owner_, and QueryMemoryDescriptor::threadsShareMemory().

Referenced by Executor::executePlanWithGroupBy().

133  {
134  std::vector<std::pair<ResultSetPtr, std::vector<size_t>>> results_per_sm;
136  const auto group_by_buffers_size = query_buffers_->getNumBuffers();
138  CHECK_EQ(size_t(1), group_by_buffers_size);
139  return groupBufferToResults(0);
140  }
141  size_t step{query_mem_desc_.threadsShareMemory() ? executor_->blockSize() : 1};
142  for (size_t i = 0; i < group_by_buffers_size; i += step) {
143  results_per_sm.emplace_back(groupBufferToResults(i), std::vector<size_t>{});
144  }
146  return executor_->reduceMultiDeviceResults(
147  ra_exe_unit, results_per_sm, row_set_mem_owner_, query_mem_desc);
148 }
#define CHECK_EQ(x, y)
Definition: Logger.h:205
const ExecutorDeviceType device_type_
std::unique_ptr< QueryMemoryInitializer > query_buffers_
CHECK(cgen_state)
std::shared_ptr< RowSetMemoryOwner > row_set_mem_owner_
ResultSetPtr groupBufferToResults(const size_t i) const
const QueryMemoryDescriptor query_mem_desc_

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ResultSetPtr QueryExecutionContext::groupBufferToDeinterleavedResults ( const size_t  i) const
private

Definition at line 75 of file QueryExecutionContext.cpp.

References agg_col_count, CHECK(), CPU, executor_, ResultSet::fixupQueryMemoryDescriptor(), QueryMemoryDescriptor::getColOffInBytes(), QueryMemoryDescriptor::getColOffInBytesInNextBin(), QueryMemoryDescriptor::getSlotCount(), output_columnar_, query_buffers_, query_mem_desc_, ResultSetStorage::reduceSingleRow(), and row_set_mem_owner_.

Referenced by groupBufferToResults().

76  {
78  const auto& result_set = query_buffers_->getResultSet(i);
79  auto deinterleaved_query_mem_desc =
81  deinterleaved_query_mem_desc.setHasInterleavedBinsOnGpu(false);
82  deinterleaved_query_mem_desc.useConsistentSlotWidthSize(8);
83 
84  auto deinterleaved_result_set =
85  std::make_shared<ResultSet>(result_set->getTargetInfos(),
86  std::vector<ColumnLazyFetchInfo>{},
87  std::vector<std::vector<const int8_t*>>{},
88  std::vector<std::vector<int64_t>>{},
89  std::vector<int64_t>{},
91  -1,
92  deinterleaved_query_mem_desc,
94  executor_);
95  auto deinterleaved_storage =
96  deinterleaved_result_set->allocateStorage(executor_->plan_state_->init_agg_vals_);
97  auto deinterleaved_buffer =
98  reinterpret_cast<int64_t*>(deinterleaved_storage->getUnderlyingBuffer());
99  const auto rows_ptr = result_set->getStorage()->getUnderlyingBuffer();
100  size_t deinterleaved_buffer_idx = 0;
102  for (size_t bin_base_off = query_mem_desc_.getColOffInBytes(0), bin_idx = 0;
103  bin_idx < result_set->entryCount();
104  ++bin_idx, bin_base_off += query_mem_desc_.getColOffInBytesInNextBin(0)) {
105  std::vector<int64_t> agg_vals(agg_col_count, 0);
106  memcpy(&agg_vals[0],
107  &executor_->plan_state_->init_agg_vals_[0],
108  agg_col_count * sizeof(agg_vals[0]));
109  ResultSetStorage::reduceSingleRow(rows_ptr + bin_base_off,
110  executor_->warpSize(),
111  false,
112  true,
113  agg_vals,
115  result_set->getTargetInfos(),
116  executor_->plan_state_->init_agg_vals_);
117  for (size_t agg_idx = 0; agg_idx < agg_col_count;
118  ++agg_idx, ++deinterleaved_buffer_idx) {
119  deinterleaved_buffer[deinterleaved_buffer_idx] = agg_vals[agg_idx];
120  }
121  }
122  query_buffers_->resetResultSet(i);
123  return deinterleaved_result_set;
124 }
const int64_t const uint32_t const uint32_t const uint32_t agg_col_count
std::unique_ptr< QueryMemoryInitializer > query_buffers_
CHECK(cgen_state)
std::shared_ptr< RowSetMemoryOwner > row_set_mem_owner_
static QueryMemoryDescriptor fixupQueryMemoryDescriptor(const QueryMemoryDescriptor &)
Definition: ResultSet.cpp:457
size_t getColOffInBytes(const size_t col_idx) const
static bool reduceSingleRow(const int8_t *row_ptr, const int8_t warp_count, const bool is_columnar, const bool replace_bitmap_ptr_with_bitmap_sz, std::vector< int64_t > &agg_vals, const QueryMemoryDescriptor &query_mem_desc, const std::vector< TargetInfo > &targets, const std::vector< int64_t > &agg_init_vals)
size_t getColOffInBytesInNextBin(const size_t col_idx) const
const QueryMemoryDescriptor query_mem_desc_

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ResultSetPtr QueryExecutionContext::groupBufferToResults ( const size_t  i) const

Definition at line 150 of file QueryExecutionContext.cpp.

References device_type_, groupBufferToDeinterleavedResults(), QueryMemoryDescriptor::interleavedBins(), query_buffers_, and query_mem_desc_.

Referenced by getRowSet().

150  {
153  }
154  return query_buffers_->getResultSetOwned(i);
155 }
const ExecutorDeviceType device_type_
std::unique_ptr< QueryMemoryInitializer > query_buffers_
ResultSetPtr groupBufferToDeinterleavedResults(const size_t i) const
bool interleavedBins(const ExecutorDeviceType) const
const QueryMemoryDescriptor query_mem_desc_

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::vector< int64_t * > QueryExecutionContext::launchCpuCode ( const RelAlgExecutionUnit ra_exe_unit,
const std::vector< std::pair< void *, void * >> &  fn_ptrs,
const bool  hoist_literals,
const std::vector< int8_t > &  literal_buff,
std::vector< std::vector< const int8_t * >>  col_buffers,
const std::vector< std::vector< int64_t >> &  num_rows,
const std::vector< std::vector< uint64_t >> &  frag_row_offsets,
const int32_t  scan_limit,
int32_t *  error_code,
const uint32_t  num_tables,
const std::vector< int64_t > &  join_hash_tables 
)

Definition at line 546 of file QueryExecutionContext.cpp.

References align_to_int64(), CHECK(), CHECK_EQ, compact_init_vals(), CPU, DEBUG_TIMER, QueryMemoryDescriptor::didOutputColumnar(), error_code, RelAlgExecutionUnit::estimator, estimator_result_set_, QueryMemoryDescriptor::getColsSize(), QueryMemoryDescriptor::getQueryDescriptionType(), INJECT_TIMER, QueryMemoryDescriptor::isGroupBy(), num_rows, Projection, query_buffers_, query_mem_desc_, and QueryMemoryDescriptor::useStreamingTopN().

Referenced by Executor::executePlanWithGroupBy(), and Executor::executePlanWithoutGroupBy().

557  {
558  auto timer = DEBUG_TIMER(__func__);
559  INJECT_TIMER(lauchCpuCode);
560 
562  const auto& init_agg_vals = query_buffers_->init_agg_vals_;
563 
564  std::vector<const int8_t**> multifrag_col_buffers;
565  for (auto& col_buffer : col_buffers) {
566  multifrag_col_buffers.push_back(&col_buffer[0]);
567  }
568  const int8_t*** multifrag_cols_ptr{
569  multifrag_col_buffers.empty() ? nullptr : &multifrag_col_buffers[0]};
570  const uint64_t num_fragments =
571  multifrag_cols_ptr ? static_cast<uint64_t>(col_buffers.size()) : uint64_t(0);
572  const auto num_out_frags = multifrag_cols_ptr ? num_fragments : uint64_t(0);
573 
574  const bool is_group_by{query_mem_desc_.isGroupBy()};
575  std::vector<int64_t*> out_vec;
576  if (ra_exe_unit.estimator) {
577  estimator_result_set_.reset(
578  new ResultSet(ra_exe_unit.estimator, ExecutorDeviceType::CPU, 0, nullptr));
579  out_vec.push_back(
580  reinterpret_cast<int64_t*>(estimator_result_set_->getHostEstimatorBuffer()));
581  } else {
582  if (!is_group_by) {
583  for (size_t i = 0; i < init_agg_vals.size(); ++i) {
584  auto buff = new int64_t[num_out_frags];
585  out_vec.push_back(static_cast<int64_t*>(buff));
586  }
587  }
588  }
589 
590  CHECK_EQ(num_rows.size(), col_buffers.size());
591  std::vector<int64_t> flatened_num_rows;
592  for (auto& nums : num_rows) {
593  flatened_num_rows.insert(flatened_num_rows.end(), nums.begin(), nums.end());
594  }
595  std::vector<uint64_t> flatened_frag_offsets;
596  for (auto& offsets : frag_offsets) {
597  flatened_frag_offsets.insert(
598  flatened_frag_offsets.end(), offsets.begin(), offsets.end());
599  }
600  int64_t rowid_lookup_num_rows{*error_code ? *error_code + 1 : 0};
601  auto num_rows_ptr =
602  rowid_lookup_num_rows ? &rowid_lookup_num_rows : &flatened_num_rows[0];
603  int32_t total_matched_init{0};
604 
605  std::vector<int64_t> cmpt_val_buff;
606  if (is_group_by) {
607  cmpt_val_buff =
609  init_agg_vals,
611  }
612 
613  const int64_t* join_hash_tables_ptr =
614  join_hash_tables.size() == 1
615  ? reinterpret_cast<int64_t*>(join_hash_tables[0])
616  : (join_hash_tables.size() > 1 ? &join_hash_tables[0] : nullptr);
617  if (hoist_literals) {
618  using agg_query = void (*)(const int8_t***, // col_buffers
619  const uint64_t*, // num_fragments
620  const int8_t*, // literals
621  const int64_t*, // num_rows
622  const uint64_t*, // frag_row_offsets
623  const int32_t*, // max_matched
624  int32_t*, // total_matched
625  const int64_t*, // init_agg_value
626  int64_t**, // out
627  int32_t*, // error_code
628  const uint32_t*, // num_tables
629  const int64_t*); // join_hash_tables_ptr
630  if (is_group_by) {
631  reinterpret_cast<agg_query>(fn_ptrs[0].first)(
632  multifrag_cols_ptr,
633  &num_fragments,
634  &literal_buff[0],
635  num_rows_ptr,
636  &flatened_frag_offsets[0],
637  &scan_limit,
638  &total_matched_init,
639  &cmpt_val_buff[0],
640  query_buffers_->getGroupByBuffersPtr(),
641  error_code,
642  &num_tables,
643  join_hash_tables_ptr);
644  } else {
645  reinterpret_cast<agg_query>(fn_ptrs[0].first)(multifrag_cols_ptr,
646  &num_fragments,
647  &literal_buff[0],
648  num_rows_ptr,
649  &flatened_frag_offsets[0],
650  &scan_limit,
651  &total_matched_init,
652  &init_agg_vals[0],
653  &out_vec[0],
654  error_code,
655  &num_tables,
656  join_hash_tables_ptr);
657  }
658  } else {
659  using agg_query = void (*)(const int8_t***, // col_buffers
660  const uint64_t*, // num_fragments
661  const int64_t*, // num_rows
662  const uint64_t*, // frag_row_offsets
663  const int32_t*, // max_matched
664  int32_t*, // total_matched
665  const int64_t*, // init_agg_value
666  int64_t**, // out
667  int32_t*, // error_code
668  const uint32_t*, // num_tables
669  const int64_t*); // join_hash_tables_ptr
670  if (is_group_by) {
671  reinterpret_cast<agg_query>(fn_ptrs[0].first)(
672  multifrag_cols_ptr,
673  &num_fragments,
674  num_rows_ptr,
675  &flatened_frag_offsets[0],
676  &scan_limit,
677  &total_matched_init,
678  &cmpt_val_buff[0],
679  query_buffers_->getGroupByBuffersPtr(),
680  error_code,
681  &num_tables,
682  join_hash_tables_ptr);
683  } else {
684  reinterpret_cast<agg_query>(fn_ptrs[0].first)(multifrag_cols_ptr,
685  &num_fragments,
686  num_rows_ptr,
687  &flatened_frag_offsets[0],
688  &scan_limit,
689  &total_matched_init,
690  &init_agg_vals[0],
691  &out_vec[0],
692  error_code,
693  &num_tables,
694  join_hash_tables_ptr);
695  }
696  }
697 
698  if (ra_exe_unit.estimator) {
699  return {};
700  }
701 
702  if (rowid_lookup_num_rows && *error_code < 0) {
703  *error_code = 0;
704  }
705 
707  query_buffers_->applyStreamingTopNOffsetCpu(query_mem_desc_, ra_exe_unit);
708  }
709 
712  query_buffers_->compactProjectionBuffersCpu(query_mem_desc_, total_matched_init);
713  }
714 
715  return out_vec;
716 }
#define CHECK_EQ(x, y)
Definition: Logger.h:205
const int8_t const int64_t const uint64_t const int32_t const int64_t int64_t uint32_t const int64_t * join_hash_tables
const int8_t const int64_t * num_rows
std::unique_ptr< QueryMemoryInitializer > query_buffers_
CHECK(cgen_state)
#define INJECT_TIMER(DESC)
Definition: measure.h:91
std::vector< int64_t > compact_init_vals(const size_t cmpt_size, const std::vector< int64_t > &init_vec, const QueryMemoryDescriptor &query_mem_desc)
const int8_t const int64_t const uint64_t const int32_t const int64_t int64_t uint32_t const int64_t int32_t * error_code
const std::shared_ptr< Analyzer::Estimator > estimator
QueryDescriptionType getQueryDescriptionType() const
#define DEBUG_TIMER(name)
Definition: Logger.h:313
std::unique_ptr< ResultSet > estimator_result_set_
const QueryMemoryDescriptor query_mem_desc_
FORCE_INLINE HOST DEVICE T align_to_int64(T addr)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::vector< int64_t * > QueryExecutionContext::launchGpuCode ( const RelAlgExecutionUnit ra_exe_unit,
const std::vector< std::pair< void *, void * >> &  cu_functions,
const bool  hoist_literals,
const std::vector< int8_t > &  literal_buff,
std::vector< std::vector< const int8_t * >>  col_buffers,
const std::vector< std::vector< int64_t >> &  num_rows,
const std::vector< std::vector< uint64_t >> &  frag_row_offsets,
const int32_t  scan_limit,
Data_Namespace::DataMgr data_mgr,
const unsigned  block_size_x,
const unsigned  grid_size_x,
const int  device_id,
int32_t *  error_code,
const uint32_t  num_tables,
const std::vector< int64_t > &  join_hash_tables,
RenderAllocatorMap render_allocator_map 
)

Definition at line 178 of file QueryExecutionContext.cpp.

References agg_col_count, CHECK(), CHECK_EQ, checkCudaErrors(), copy_from_gpu(), copy_to_gpu(), DEBUG_TIMER, QueryMemoryDescriptor::didOutputColumnar(), dispatch_mode_, RelAlgExecutionUnit::estimator, estimator_result_set_, executor_, g_enable_dynamic_watchdog, g_enable_runtime_query_interrupt, get_num_allocated_rows_from_gpu(), QueryMemoryDescriptor::getEntryCount(), QueryMemoryDescriptor::getQueryDescriptionType(), RenderAllocatorMap::getRenderAllocator(), GPU, gpu_allocator_, QueryMemoryDescriptor::hasKeylessHash(), INJECT_TIMER, inplace_sort_gpu(), QueryMemoryDescriptor::isGroupBy(), max_matched, num_rows, SortInfo::order_entries, output_columnar_, Projection, query_buffers_, query_mem_desc_, QueryMemoryDescriptor::sharedMemBytes(), RelAlgExecutionUnit::sort_info, QueryMemoryDescriptor::sortOnGpu(), to_string(), RelAlgExecutionUnit::use_bump_allocator, use_speculative_top_n(), QueryMemoryDescriptor::useStreamingTopN(), and VLOG.

Referenced by Executor::executePlanWithGroupBy(), and Executor::executePlanWithoutGroupBy().

194  {
195  auto timer = DEBUG_TIMER(__func__);
196  INJECT_TIMER(lauchGpuCode);
197 #ifdef HAVE_CUDA
200  const auto& init_agg_vals = query_buffers_->init_agg_vals_;
201 
202  bool is_group_by{query_mem_desc_.isGroupBy()};
203 
204  RenderAllocator* render_allocator = nullptr;
205  if (render_allocator_map) {
206  render_allocator = render_allocator_map->getRenderAllocator(device_id);
207  }
208 
209  auto cu_func = static_cast<CUfunction>(cu_functions[device_id].first);
210  std::vector<int64_t*> out_vec;
211  uint32_t num_fragments = col_buffers.size();
212  std::vector<int32_t> error_codes(grid_size_x * block_size_x);
213 
214  CUevent start0, stop0; // preparation
215  cuEventCreate(&start0, 0);
216  cuEventCreate(&stop0, 0);
217  CUevent start1, stop1; // cuLaunchKernel
218  cuEventCreate(&start1, 0);
219  cuEventCreate(&stop1, 0);
220  CUevent start2, stop2; // finish
221  cuEventCreate(&start2, 0);
222  cuEventCreate(&stop2, 0);
223 
225  cuEventRecord(start0, 0);
226  }
227 
229  initializeDynamicWatchdog(cu_functions[device_id].second, device_id);
230  }
231 
233  initializeRuntimeInterrupter(cu_functions[device_id].second, device_id);
234  }
235 
236  auto kernel_params = prepareKernelParams(col_buffers,
237  literal_buff,
238  num_rows,
239  frag_offsets,
240  scan_limit,
241  init_agg_vals,
242  error_codes,
243  num_tables,
245  data_mgr,
246  device_id,
247  hoist_literals,
248  is_group_by);
249 
250  CHECK_EQ(static_cast<size_t>(KERN_PARAM_COUNT), kernel_params.size());
251  CHECK_EQ(CUdeviceptr(0), kernel_params[GROUPBY_BUF]);
252 
253  const unsigned block_size_y = 1;
254  const unsigned block_size_z = 1;
255  const unsigned grid_size_y = 1;
256  const unsigned grid_size_z = 1;
257  const auto total_thread_count = block_size_x * grid_size_x;
258  const auto err_desc = kernel_params[ERROR_CODE];
259 
260  if (is_group_by) {
261  CHECK(!(query_buffers_->getGroupByBuffersSize() == 0) || render_allocator);
262  bool can_sort_on_gpu = query_mem_desc_.sortOnGpu();
263  auto gpu_group_by_buffers =
264  query_buffers_->createAndInitializeGroupByBufferGpu(ra_exe_unit,
266  kernel_params[INIT_AGG_VALS],
267  device_id,
269  block_size_x,
270  grid_size_x,
271  executor_->warpSize(),
272  can_sort_on_gpu,
274  render_allocator);
275  if (ra_exe_unit.use_bump_allocator) {
276  const auto max_matched = static_cast<int32_t>(gpu_group_by_buffers.entry_count);
277  copy_to_gpu(data_mgr,
278  kernel_params[MAX_MATCHED],
279  &max_matched,
280  sizeof(max_matched),
281  device_id);
282  }
283 
284  kernel_params[GROUPBY_BUF] = gpu_group_by_buffers.first;
285  std::vector<void*> param_ptrs;
286  for (auto& param : kernel_params) {
287  param_ptrs.push_back(&param);
288  }
289 
291  cuEventRecord(stop0, 0);
292  cuEventSynchronize(stop0);
293  float milliseconds0 = 0;
294  cuEventElapsedTime(&milliseconds0, start0, stop0);
295  VLOG(1) << "Device " << std::to_string(device_id)
296  << ": launchGpuCode: group-by prepare: " << std::to_string(milliseconds0)
297  << " ms";
298  cuEventRecord(start1, 0);
299  }
300 
301  if (hoist_literals) {
303  cuLaunchKernel(cu_func,
304  grid_size_x,
305  grid_size_y,
306  grid_size_z,
307  block_size_x,
308  block_size_y,
309  block_size_z,
311  nullptr,
312  &param_ptrs[0],
313  nullptr));
314  } else {
315  param_ptrs.erase(param_ptrs.begin() + LITERALS); // TODO(alex): remove
317  cuLaunchKernel(cu_func,
318  grid_size_x,
319  grid_size_y,
320  grid_size_z,
321  block_size_x,
322  block_size_y,
323  block_size_z,
325  nullptr,
326  &param_ptrs[0],
327  nullptr));
328  }
330  executor_->registerActiveModule(cu_functions[device_id].second, device_id);
331  cuEventRecord(stop1, 0);
332  cuEventSynchronize(stop1);
333  executor_->unregisterActiveModule(cu_functions[device_id].second, device_id);
334  float milliseconds1 = 0;
335  cuEventElapsedTime(&milliseconds1, start1, stop1);
336  VLOG(1) << "Device " << std::to_string(device_id)
337  << ": launchGpuCode: group-by cuLaunchKernel: "
338  << std::to_string(milliseconds1) << " ms";
339  cuEventRecord(start2, 0);
340  }
341 
342  gpu_allocator_->copyFromDevice(reinterpret_cast<int8_t*>(error_codes.data()),
343  reinterpret_cast<int8_t*>(err_desc),
344  error_codes.size() * sizeof(error_codes[0]));
345  *error_code = aggregate_error_codes(error_codes);
346  if (*error_code > 0) {
347  return {};
348  }
349 
350  if (!render_allocator) {
352  query_buffers_->applyStreamingTopNOffsetGpu(data_mgr,
354  gpu_group_by_buffers,
355  ra_exe_unit,
356  total_thread_count,
357  device_id);
358  } else {
359  if (use_speculative_top_n(ra_exe_unit, query_mem_desc_)) {
362  gpu_group_by_buffers,
363  data_mgr,
364  device_id);
365  }
369  query_buffers_->compactProjectionBuffersGpu(
371  data_mgr,
372  gpu_group_by_buffers,
374  data_mgr, kernel_params[TOTAL_MATCHED], device_id),
375  device_id);
376  } else {
377  size_t num_allocated_rows{0};
378  if (ra_exe_unit.use_bump_allocator) {
379  num_allocated_rows = get_num_allocated_rows_from_gpu(
380  data_mgr, kernel_params[TOTAL_MATCHED], device_id);
381  // First, check the error code. If we ran out of slots, don't copy data back
382  // into the ResultSet or update ResultSet entry count
383  if (*error_code < 0) {
384  return {};
385  }
386  }
387  query_buffers_->copyGroupByBuffersFromGpu(
388  data_mgr,
390  ra_exe_unit.use_bump_allocator ? num_allocated_rows
392  gpu_group_by_buffers,
393  &ra_exe_unit,
394  block_size_x,
395  grid_size_x,
396  device_id,
397  can_sort_on_gpu && query_mem_desc_.hasKeylessHash());
398  if (num_allocated_rows) {
399  CHECK(ra_exe_unit.use_bump_allocator);
400  CHECK(!query_buffers_->result_sets_.empty());
401  query_buffers_->result_sets_.front()->updateStorageEntryCount(
402  num_allocated_rows);
403  }
404  }
405  } else {
406  query_buffers_->copyGroupByBuffersFromGpu(
407  data_mgr,
410  gpu_group_by_buffers,
411  &ra_exe_unit,
412  block_size_x,
413  grid_size_x,
414  device_id,
415  can_sort_on_gpu && query_mem_desc_.hasKeylessHash());
416  }
417  }
418  }
419  } else {
420  std::vector<CUdeviceptr> out_vec_dev_buffers;
421  const size_t agg_col_count{ra_exe_unit.estimator ? size_t(1) : init_agg_vals.size()};
422  if (ra_exe_unit.estimator) {
423  estimator_result_set_.reset(new ResultSet(
424  ra_exe_unit.estimator, ExecutorDeviceType::GPU, device_id, data_mgr));
425  out_vec_dev_buffers.push_back(reinterpret_cast<CUdeviceptr>(
426  estimator_result_set_->getDeviceEstimatorBuffer()));
427  } else {
428  for (size_t i = 0; i < agg_col_count; ++i) {
429  CUdeviceptr out_vec_dev_buffer =
430  num_fragments
431  ? reinterpret_cast<CUdeviceptr>(gpu_allocator_->alloc(
432  block_size_x * grid_size_x * sizeof(int64_t) * num_fragments))
433  : 0;
434  out_vec_dev_buffers.push_back(out_vec_dev_buffer);
435  }
436  }
437  auto out_vec_dev_ptr = gpu_allocator_->alloc(agg_col_count * sizeof(CUdeviceptr));
438  gpu_allocator_->copyToDevice(out_vec_dev_ptr,
439  reinterpret_cast<int8_t*>(out_vec_dev_buffers.data()),
440  agg_col_count * sizeof(CUdeviceptr));
441  kernel_params[GROUPBY_BUF] = reinterpret_cast<CUdeviceptr>(out_vec_dev_ptr);
442  std::vector<void*> param_ptrs;
443  for (auto& param : kernel_params) {
444  param_ptrs.push_back(&param);
445  }
446 
448  cuEventRecord(stop0, 0);
449  cuEventSynchronize(stop0);
450  float milliseconds0 = 0;
451  cuEventElapsedTime(&milliseconds0, start0, stop0);
452  VLOG(1) << "Device " << std::to_string(device_id)
453  << ": launchGpuCode: prepare: " << std::to_string(milliseconds0) << " ms";
454  cuEventRecord(start1, 0);
455  }
456 
457  if (hoist_literals) {
458  checkCudaErrors(cuLaunchKernel(cu_func,
459  grid_size_x,
460  grid_size_y,
461  grid_size_z,
462  block_size_x,
463  block_size_y,
464  block_size_z,
465  0,
466  nullptr,
467  &param_ptrs[0],
468  nullptr));
469  } else {
470  param_ptrs.erase(param_ptrs.begin() + LITERALS); // TODO(alex): remove
471  checkCudaErrors(cuLaunchKernel(cu_func,
472  grid_size_x,
473  grid_size_y,
474  grid_size_z,
475  block_size_x,
476  block_size_y,
477  block_size_z,
478  0,
479  nullptr,
480  &param_ptrs[0],
481  nullptr));
482  }
483 
485  executor_->registerActiveModule(cu_functions[device_id].second, device_id);
486  cuEventRecord(stop1, 0);
487  cuEventSynchronize(stop1);
488  executor_->unregisterActiveModule(cu_functions[device_id].second, device_id);
489  float milliseconds1 = 0;
490  cuEventElapsedTime(&milliseconds1, start1, stop1);
491  VLOG(1) << "Device " << std::to_string(device_id)
492  << ": launchGpuCode: cuLaunchKernel: " << std::to_string(milliseconds1)
493  << " ms";
494  cuEventRecord(start2, 0);
495  }
496 
497  copy_from_gpu(data_mgr,
498  &error_codes[0],
499  err_desc,
500  error_codes.size() * sizeof(error_codes[0]),
501  device_id);
502  *error_code = aggregate_error_codes(error_codes);
503  if (*error_code > 0) {
504  return {};
505  }
506  if (ra_exe_unit.estimator) {
508  estimator_result_set_->syncEstimatorBuffer();
509  return {};
510  }
511  for (size_t i = 0; i < agg_col_count; ++i) {
512  int64_t* host_out_vec =
513  new int64_t[block_size_x * grid_size_x * sizeof(int64_t) * num_fragments];
514  copy_from_gpu(data_mgr,
515  host_out_vec,
516  out_vec_dev_buffers[i],
517  block_size_x * grid_size_x * sizeof(int64_t) * num_fragments,
518  device_id);
519  out_vec.push_back(host_out_vec);
520  }
521  }
522  const auto count_distinct_bitmap_mem = query_buffers_->getCountDistinctBitmapPtr();
523  if (count_distinct_bitmap_mem) {
524  copy_from_gpu(data_mgr,
525  query_buffers_->getCountDistinctHostPtr(),
526  count_distinct_bitmap_mem,
527  query_buffers_->getCountDistinctBitmapBytes(),
528  device_id);
529  }
530 
532  cuEventRecord(stop2, 0);
533  cuEventSynchronize(stop2);
534  float milliseconds2 = 0;
535  cuEventElapsedTime(&milliseconds2, start2, stop2);
536  VLOG(1) << "Device " << std::to_string(device_id)
537  << ": launchGpuCode: finish: " << std::to_string(milliseconds2) << " ms";
538  }
539 
540  return out_vec;
541 #else
542  return {};
543 #endif
544 }
#define CHECK_EQ(x, y)
Definition: Logger.h:205
RenderAllocator * getRenderAllocator(size_t device_id)
const int8_t const int64_t const uint64_t const int32_t const int64_t int64_t uint32_t const int64_t * join_hash_tables
const int64_t const uint32_t const uint32_t const uint32_t agg_col_count
const int8_t const int64_t * num_rows
void checkCudaErrors(CUresult err)
Definition: sample.cpp:38
const std::list< Analyzer::OrderEntry > order_entries
unsigned long long CUdeviceptr
Definition: nocuda.h:27
bool use_speculative_top_n(const RelAlgExecutionUnit &ra_exe_unit, const QueryMemoryDescriptor &query_mem_desc)
const ExecutorDispatchMode dispatch_mode_
bool g_enable_dynamic_watchdog
Definition: Execute.cpp:72
void inplace_sort_gpu(const std::list< Analyzer::OrderEntry > &order_entries, const QueryMemoryDescriptor &query_mem_desc, const GpuGroupByBuffers &group_by_buffers, Data_Namespace::DataMgr *data_mgr, const int device_id)
std::string to_string(char const *&&v)
std::unique_ptr< QueryMemoryInitializer > query_buffers_
CHECK(cgen_state)
void copy_to_gpu(Data_Namespace::DataMgr *data_mgr, CUdeviceptr dst, const void *src, const size_t num_bytes, const int device_id)
Definition: GpuMemUtils.cpp:31
const SortInfo sort_info
#define INJECT_TIMER(DESC)
Definition: measure.h:91
const int8_t const int64_t const uint64_t const int32_t const int64_t int64_t uint32_t const int64_t int32_t * error_code
void copy_from_gpu(Data_Namespace::DataMgr *data_mgr, void *dst, const CUdeviceptr src, const size_t num_bytes, const int device_id)
void * CUfunction
Definition: nocuda.h:24
const int8_t const int64_t const uint64_t const int32_t * max_matched
QueryDescriptionType getQueryDescriptionType() const
size_t sharedMemBytes(const ExecutorDeviceType) const
size_t get_num_allocated_rows_from_gpu(Data_Namespace::DataMgr *data_mgr, CUdeviceptr projection_size_gpu, const int device_id)
std::unique_ptr< CudaAllocator > gpu_allocator_
#define DEBUG_TIMER(name)
Definition: Logger.h:313
std::unique_ptr< ResultSet > estimator_result_set_
bool g_enable_runtime_query_interrupt
Definition: Execute.cpp:104
const QueryMemoryDescriptor query_mem_desc_
#define VLOG(n)
Definition: Logger.h:291

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Friends And Related Function Documentation

friend class Executor
friend

Definition at line 142 of file QueryExecutionContext.h.

Member Data Documentation

const ExecutorDeviceType QueryExecutionContext::device_type_
private

Definition at line 135 of file QueryExecutionContext.h.

Referenced by getRowSet(), and groupBufferToResults().

const ExecutorDispatchMode QueryExecutionContext::dispatch_mode_
private

Definition at line 136 of file QueryExecutionContext.h.

Referenced by launchGpuCode().

std::unique_ptr<ResultSet> QueryExecutionContext::estimator_result_set_
mutableprivate
const Executor* QueryExecutionContext::executor_
private
std::unique_ptr<CudaAllocator> QueryExecutionContext::gpu_allocator_
private

Definition at line 130 of file QueryExecutionContext.h.

Referenced by launchGpuCode(), and QueryExecutionContext().

const bool QueryExecutionContext::output_columnar_
private

Definition at line 138 of file QueryExecutionContext.h.

Referenced by groupBufferToDeinterleavedResults(), and launchGpuCode().

std::shared_ptr<RowSetMemoryOwner> QueryExecutionContext::row_set_mem_owner_
private

Definition at line 137 of file QueryExecutionContext.h.

Referenced by getRowSet(), and groupBufferToDeinterleavedResults().


The documentation for this class was generated from the following files: