OmniSciDB  eb3a3d0a03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GpuMemUtils.h File Reference
#include "CompilationOptions.h"
#include <cstddef>
#include <cstdint>
#include <memory>
#include <utility>
#include <vector>
#include "../Shared/nocuda.h"
+ Include dependency graph for GpuMemUtils.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  GpuGroupByBuffers
 

Namespaces

 CudaMgr_Namespace
 
 Data_Namespace
 

Functions

void copy_to_gpu (Data_Namespace::DataMgr *data_mgr, CUdeviceptr dst, const void *src, const size_t num_bytes, const int device_id)
 
void copy_from_gpu (Data_Namespace::DataMgr *data_mgr, void *dst, const CUdeviceptr src, const size_t num_bytes, const int device_id)
 
GpuGroupByBuffers create_dev_group_by_buffers (DeviceAllocator *device_allocator, const std::vector< int64_t * > &group_by_buffers, const QueryMemoryDescriptor &, const unsigned block_size_x, const unsigned grid_size_x, const int device_id, const ExecutorDispatchMode dispatch_mode, const int64_t num_input_rows, const bool prepend_index_buffer, const bool always_init_group_by_on_host, const bool use_bump_allocator, const bool has_varlen_output, Allocator *insitu_allocator)
 
void copy_group_by_buffers_from_gpu (Data_Namespace::DataMgr *data_mgr, const std::vector< int64_t * > &group_by_buffers, const size_t groups_buffer_size, const CUdeviceptr group_by_dev_buffers_mem, const QueryMemoryDescriptor &query_mem_desc, const unsigned block_size_x, const unsigned grid_size_x, const int device_id, const bool prepend_index_buffer, const bool has_varlen_output)
 
size_t get_num_allocated_rows_from_gpu (Data_Namespace::DataMgr *data_mgr, CUdeviceptr projection_size_gpu, const int device_id)
 
void copy_projection_buffer_from_gpu_columnar (Data_Namespace::DataMgr *data_mgr, const GpuGroupByBuffers &gpu_query_buffers, const QueryMemoryDescriptor &query_mem_desc, int8_t *projection_buffer, const size_t projection_count, const int device_id)
 

Function Documentation

void copy_from_gpu ( Data_Namespace::DataMgr data_mgr,
void *  dst,
const CUdeviceptr  src,
const size_t  num_bytes,
const int  device_id 
)

Definition at line 225 of file GpuMemUtils.cpp.

References CHECK, and Data_Namespace::DataMgr::getCudaMgr().

Referenced by RangeJoinHashTable::approximateTupleCount(), BaselineJoinHashTable::approximateTupleCount(), OverlapsJoinHashTable::approximateTupleCount(), copy_group_by_buffers_from_gpu(), copy_projection_buffer_from_gpu_columnar(), anonymous_namespace{ResultSetIteration.cpp}::fetch_data_from_gpu(), get_num_allocated_rows_from_gpu(), ResultSet::getVarlenOrderEntry(), BaselineJoinHashTableBuilder::initHashTableOnGpu(), QueryExecutionContext::launchGpuCode(), ResultSet::makeVarlenTargetValue(), ResultSet::syncEstimatorBuffer(), PerfectJoinHashTable::toSet(), BaselineJoinHashTable::toSet(), OverlapsJoinHashTable::toSet(), PerfectJoinHashTable::toString(), BaselineJoinHashTable::toString(), and OverlapsJoinHashTable::toString().

229  {
230  const auto cuda_mgr = data_mgr->getCudaMgr();
231  CHECK(cuda_mgr);
232  cuda_mgr->copyDeviceToHost(static_cast<int8_t*>(dst),
233  reinterpret_cast<const int8_t*>(src),
234  num_bytes,
235  device_id);
236 }
CudaMgr_Namespace::CudaMgr * getCudaMgr() const
Definition: DataMgr.h:208
#define CHECK(condition)
Definition: Logger.h:209

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void copy_group_by_buffers_from_gpu ( Data_Namespace::DataMgr data_mgr,
const std::vector< int64_t * > &  group_by_buffers,
const size_t  groups_buffer_size,
const CUdeviceptr  group_by_dev_buffers_mem,
const QueryMemoryDescriptor query_mem_desc,
const unsigned  block_size_x,
const unsigned  grid_size_x,
const int  device_id,
const bool  prepend_index_buffer,
const bool  has_varlen_output 
)

Definition at line 238 of file GpuMemUtils.cpp.

References QueryMemoryDescriptor::blocksShareMemory(), CHECK_EQ, CHECK_LT, anonymous_namespace{GpuMemUtils.cpp}::coalesced_size(), copy_from_gpu(), QueryMemoryDescriptor::getEntryCount(), and i.

Referenced by QueryMemoryInitializer::copyGroupByBuffersFromGpu(), and ResultSet::radixSortOnGpu().

247  {
248  if (group_by_buffers.empty()) {
249  return;
250  }
251  const size_t first_group_buffer_idx = has_varlen_output ? 1 : 0;
252 
253  const unsigned block_buffer_count{query_mem_desc.blocksShareMemory() ? 1 : grid_size_x};
254  if (block_buffer_count == 1 && !prepend_index_buffer) {
255  CHECK_EQ(coalesced_size(query_mem_desc, groups_buffer_size, block_buffer_count),
256  groups_buffer_size);
257  copy_from_gpu(data_mgr,
258  group_by_buffers[first_group_buffer_idx],
259  group_by_dev_buffers_mem,
260  groups_buffer_size,
261  device_id);
262  return;
263  }
264  const size_t index_buffer_sz{
265  prepend_index_buffer ? query_mem_desc.getEntryCount() * sizeof(int64_t) : 0};
266  std::vector<int8_t> buff_from_gpu(
267  coalesced_size(query_mem_desc, groups_buffer_size, block_buffer_count) +
268  index_buffer_sz);
269  copy_from_gpu(data_mgr,
270  &buff_from_gpu[0],
271  group_by_dev_buffers_mem - index_buffer_sz,
272  buff_from_gpu.size(),
273  device_id);
274  auto buff_from_gpu_ptr = &buff_from_gpu[0];
275  for (size_t i = 0; i < block_buffer_count; ++i) {
276  const size_t buffer_idx = (i * block_size_x) + first_group_buffer_idx;
277  CHECK_LT(buffer_idx, group_by_buffers.size());
278  memcpy(group_by_buffers[buffer_idx],
279  buff_from_gpu_ptr,
280  groups_buffer_size + index_buffer_sz);
281  buff_from_gpu_ptr += groups_buffer_size;
282  }
283 }
#define CHECK_EQ(x, y)
Definition: Logger.h:217
void copy_from_gpu(Data_Namespace::DataMgr *data_mgr, void *dst, const CUdeviceptr src, const size_t num_bytes, const int device_id)
#define CHECK_LT(x, y)
Definition: Logger.h:219
size_t coalesced_size(const QueryMemoryDescriptor &query_mem_desc, const size_t group_by_one_buffer_size, const unsigned grid_size_x)
Definition: GpuMemUtils.cpp:51

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void copy_projection_buffer_from_gpu_columnar ( Data_Namespace::DataMgr data_mgr,
const GpuGroupByBuffers gpu_group_by_buffers,
const QueryMemoryDescriptor query_mem_desc,
int8_t *  projection_buffer,
const size_t  projection_count,
const int  device_id 
)

For projection queries we only copy back as many elements as necessary, not the whole output buffer. The goal is to be able to build a compact ResultSet, particularly useful for columnar outputs.

NOTE: Saman: we should revisit this function when we have a bump allocator

Definition at line 307 of file GpuMemUtils.cpp.

References align_to_int64(), CHECK, copy_from_gpu(), GpuGroupByBuffers::data, QueryMemoryDescriptor::didOutputColumnar(), QueryMemoryDescriptor::getColOffInBytes(), QueryMemoryDescriptor::getPaddedSlotWidthBytes(), QueryMemoryDescriptor::getQueryDescriptionType(), QueryMemoryDescriptor::getSlotCount(), i, and Projection.

Referenced by QueryMemoryInitializer::compactProjectionBuffersGpu().

313  {
314  CHECK(query_mem_desc.didOutputColumnar());
316  constexpr size_t row_index_width = sizeof(int64_t);
317  // copy all the row indices back to the host
318  copy_from_gpu(data_mgr,
319  reinterpret_cast<int64_t*>(projection_buffer),
320  gpu_group_by_buffers.data,
321  projection_count * row_index_width,
322  device_id);
323  size_t buffer_offset_cpu{projection_count * row_index_width};
324  // other columns are actual non-lazy columns for the projection:
325  for (size_t i = 0; i < query_mem_desc.getSlotCount(); i++) {
326  if (query_mem_desc.getPaddedSlotWidthBytes(i) > 0) {
327  const auto column_proj_size =
328  projection_count * query_mem_desc.getPaddedSlotWidthBytes(i);
329  copy_from_gpu(data_mgr,
330  projection_buffer + buffer_offset_cpu,
331  gpu_group_by_buffers.data + query_mem_desc.getColOffInBytes(i),
332  column_proj_size,
333  device_id);
334  buffer_offset_cpu += align_to_int64(column_proj_size);
335  }
336  }
337 }
CUdeviceptr data
Definition: GpuMemUtils.h:61
void copy_from_gpu(Data_Namespace::DataMgr *data_mgr, void *dst, const CUdeviceptr src, const size_t num_bytes, const int device_id)
const int8_t getPaddedSlotWidthBytes(const size_t slot_idx) const
QueryDescriptionType getQueryDescriptionType() const
#define CHECK(condition)
Definition: Logger.h:209
size_t getColOffInBytes(const size_t col_idx) const
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:

void copy_to_gpu ( Data_Namespace::DataMgr data_mgr,
CUdeviceptr  dst,
const void *  src,
const size_t  num_bytes,
const int  device_id 
)

Definition at line 30 of file GpuMemUtils.cpp.

References CHECK, and Data_Namespace::DataMgr::getCudaMgr().

Referenced by RangeJoinHashTable::approximateTupleCount(), OverlapsJoinHashTable::approximateTupleCount(), anonymous_namespace{ResultSetSortImpl.cu}::get_device_copy_ptr(), PerfectJoinHashTable::initHashTableForDevice(), BaselineJoinHashTable::initHashTableForDevice(), BaselineJoinHashTableBuilder::initHashTableOnGpu(), InValuesBitmap::InValuesBitmap(), and QueryExecutionContext::launchGpuCode().

34  {
35 #ifdef HAVE_CUDA
36  if (!data_mgr) { // only for unit tests
37  cuMemcpyHtoD(dst, src, num_bytes);
38  return;
39  }
40 #endif // HAVE_CUDA
41  const auto cuda_mgr = data_mgr->getCudaMgr();
42  CHECK(cuda_mgr);
43  cuda_mgr->copyHostToDevice(reinterpret_cast<int8_t*>(dst),
44  static_cast<const int8_t*>(src),
45  num_bytes,
46  device_id);
47 }
CudaMgr_Namespace::CudaMgr * getCudaMgr() const
Definition: DataMgr.h:208
#define CHECK(condition)
Definition: Logger.h:209

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

GpuGroupByBuffers create_dev_group_by_buffers ( DeviceAllocator device_allocator,
const std::vector< int64_t * > &  group_by_buffers,
const QueryMemoryDescriptor ,
const unsigned  block_size_x,
const unsigned  grid_size_x,
const int  device_id,
const ExecutorDispatchMode  dispatch_mode,
const int64_t  num_input_rows,
const bool  prepend_index_buffer,
const bool  always_init_group_by_on_host,
const bool  use_bump_allocator,
const bool  has_varlen_output,
Allocator insitu_allocator 
)

Definition at line 60 of file GpuMemUtils.cpp.

References align_to_int64(), Allocator::alloc(), QueryMemoryDescriptor::blocksShareMemory(), CHECK, CHECK_GT, CHECK_LE, anonymous_namespace{GpuMemUtils.cpp}::coalesced_size(), DeviceAllocator::copyToDevice(), g_bump_allocator_step_reduction, g_max_memory_allocation_size, g_min_memory_allocation_size, QueryMemoryDescriptor::getBufferSizeBytes(), QueryMemoryDescriptor::getEntryCount(), QueryMemoryDescriptor::getRowSize(), GPU, i, logger::INFO, KernelPerFragment, QueryMemoryDescriptor::lazyInitGroups(), LOG, QueryMemoryDescriptor::threadsShareMemory(), to_string(), QueryMemoryDescriptor::varlenOutputBufferElemSize(), and logger::WARNING.

Referenced by ResultSet::radixSortOnGpu().

73  {
74  if (group_by_buffers.empty() && !insitu_allocator) {
75  return {0, 0, 0, 0};
76  }
77  CHECK(cuda_allocator);
78 
79  size_t groups_buffer_size{0};
80  CUdeviceptr group_by_dev_buffers_mem{0};
81  size_t mem_size{0};
82  size_t entry_count{0};
83 
84  if (use_bump_allocator) {
85  CHECK(!prepend_index_buffer);
86  CHECK(!insitu_allocator);
87 
88  if (dispatch_mode == ExecutorDispatchMode::KernelPerFragment) {
89  // Allocate an output buffer equal to the size of the number of rows in the
90  // fragment. The kernel per fragment path is only used for projections with lazy
91  // fetched outputs. Therefore, the resulting output buffer should be relatively
92  // narrow compared to the width of an input row, offsetting the larger allocation.
93 
94  CHECK_GT(num_input_rows, int64_t(0));
95  entry_count = num_input_rows;
96  groups_buffer_size =
97  query_mem_desc.getBufferSizeBytes(ExecutorDeviceType::GPU, entry_count);
98  mem_size = coalesced_size(query_mem_desc,
99  groups_buffer_size,
100  query_mem_desc.blocksShareMemory() ? 1 : grid_size_x);
101  // TODO(adb): render allocator support
102  group_by_dev_buffers_mem =
103  reinterpret_cast<CUdeviceptr>(cuda_allocator->alloc(mem_size));
104  } else {
105  // Attempt to allocate increasingly small buffers until we have less than 256B of
106  // memory remaining on the device. This may have the side effect of evicting
107  // memory allocated for previous queries. However, at current maximum slab sizes
108  // (2GB) we expect these effects to be minimal.
109  size_t max_memory_size{g_max_memory_allocation_size};
110  while (true) {
111  entry_count = max_memory_size / query_mem_desc.getRowSize();
112  groups_buffer_size =
113  query_mem_desc.getBufferSizeBytes(ExecutorDeviceType::GPU, entry_count);
114 
115  try {
116  mem_size = coalesced_size(query_mem_desc,
117  groups_buffer_size,
118  query_mem_desc.blocksShareMemory() ? 1 : grid_size_x);
119  CHECK_LE(entry_count, std::numeric_limits<uint32_t>::max());
120 
121  // TODO(adb): render allocator support
122  group_by_dev_buffers_mem =
123  reinterpret_cast<CUdeviceptr>(cuda_allocator->alloc(mem_size));
124  } catch (const OutOfMemory& e) {
125  LOG(WARNING) << e.what();
126  max_memory_size = max_memory_size * g_bump_allocator_step_reduction;
127  if (max_memory_size < g_min_memory_allocation_size) {
128  throw;
129  }
130 
131  LOG(WARNING) << "Ran out of memory for projection query output. Retrying with "
132  << std::to_string(max_memory_size) << " bytes";
133 
134  continue;
135  }
136  break;
137  }
138  }
139  LOG(INFO) << "Projection query allocation succeeded with " << groups_buffer_size
140  << " bytes allocated (max entry count " << entry_count << ")";
141  } else {
142  entry_count = query_mem_desc.getEntryCount();
143  CHECK_GT(entry_count, size_t(0));
144  groups_buffer_size =
145  query_mem_desc.getBufferSizeBytes(ExecutorDeviceType::GPU, entry_count);
146  mem_size = coalesced_size(query_mem_desc,
147  groups_buffer_size,
148  query_mem_desc.blocksShareMemory() ? 1 : grid_size_x);
149  const size_t prepended_buff_size{
150  prepend_index_buffer ? align_to_int64(entry_count * sizeof(int32_t)) : 0};
151 
152  int8_t* group_by_dev_buffers_allocation{nullptr};
153  if (insitu_allocator) {
154  group_by_dev_buffers_allocation =
155  insitu_allocator->alloc(mem_size + prepended_buff_size);
156  } else {
157  group_by_dev_buffers_allocation =
158  cuda_allocator->alloc(mem_size + prepended_buff_size);
159  }
160  CHECK(group_by_dev_buffers_allocation);
161 
162  group_by_dev_buffers_mem =
163  reinterpret_cast<CUdeviceptr>(group_by_dev_buffers_allocation) +
164  prepended_buff_size;
165  }
166  CHECK_GT(groups_buffer_size, size_t(0));
167  CHECK(group_by_dev_buffers_mem);
168 
169  CHECK(query_mem_desc.threadsShareMemory());
170  const size_t step{block_size_x};
171 
172  if (!insitu_allocator && (always_init_group_by_on_host ||
173  !query_mem_desc.lazyInitGroups(ExecutorDeviceType::GPU))) {
174  std::vector<int8_t> buff_to_gpu(mem_size);
175  auto buff_to_gpu_ptr = buff_to_gpu.data();
176 
177  const size_t start = has_varlen_output ? 1 : 0;
178  for (size_t i = start; i < group_by_buffers.size(); i += step) {
179  memcpy(buff_to_gpu_ptr, group_by_buffers[i], groups_buffer_size);
180  buff_to_gpu_ptr += groups_buffer_size;
181  }
182  cuda_allocator->copyToDevice(reinterpret_cast<int8_t*>(group_by_dev_buffers_mem),
183  buff_to_gpu.data(),
184  buff_to_gpu.size());
185  }
186 
187  auto group_by_dev_buffer = group_by_dev_buffers_mem;
188 
189  const size_t num_ptrs =
190  (block_size_x * grid_size_x) + (has_varlen_output ? size_t(1) : size_t(0));
191 
192  std::vector<CUdeviceptr> group_by_dev_buffers(num_ptrs);
193 
194  const size_t start_index = has_varlen_output ? 1 : 0;
195  for (size_t i = start_index; i < num_ptrs; i += step) {
196  for (size_t j = 0; j < step; ++j) {
197  group_by_dev_buffers[i + j] = group_by_dev_buffer;
198  }
199  if (!query_mem_desc.blocksShareMemory()) {
200  group_by_dev_buffer += groups_buffer_size;
201  }
202  }
203 
204  CUdeviceptr varlen_output_buffer{0};
205  if (has_varlen_output) {
206  const auto varlen_buffer_elem_size_opt = query_mem_desc.varlenOutputBufferElemSize();
207  CHECK(varlen_buffer_elem_size_opt); // TODO(adb): relax
208 
209  group_by_dev_buffers[0] = reinterpret_cast<CUdeviceptr>(cuda_allocator->alloc(
210  query_mem_desc.getEntryCount() * varlen_buffer_elem_size_opt.value()));
211  varlen_output_buffer = group_by_dev_buffers[0];
212  }
213 
214  auto group_by_dev_ptr = cuda_allocator->alloc(num_ptrs * sizeof(CUdeviceptr));
215  cuda_allocator->copyToDevice(group_by_dev_ptr,
216  reinterpret_cast<int8_t*>(group_by_dev_buffers.data()),
217  num_ptrs * sizeof(CUdeviceptr));
218 
219  return {reinterpret_cast<CUdeviceptr>(group_by_dev_ptr),
220  group_by_dev_buffers_mem,
221  entry_count,
222  varlen_output_buffer};
223 }
double g_bump_allocator_step_reduction
Definition: Execute.cpp:114
#define LOG(tag)
Definition: Logger.h:203
unsigned long long CUdeviceptr
Definition: nocuda.h:27
virtual int8_t * alloc(const size_t num_bytes)=0
#define CHECK_GT(x, y)
Definition: Logger.h:221
std::string to_string(char const *&&v)
size_t g_max_memory_allocation_size
Definition: Execute.cpp:109
size_t g_min_memory_allocation_size
Definition: Execute.cpp:110
#define CHECK_LE(x, y)
Definition: Logger.h:220
#define CHECK(condition)
Definition: Logger.h:209
size_t coalesced_size(const QueryMemoryDescriptor &query_mem_desc, const size_t group_by_one_buffer_size, const unsigned grid_size_x)
Definition: GpuMemUtils.cpp:51
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:

size_t get_num_allocated_rows_from_gpu ( Data_Namespace::DataMgr data_mgr,
CUdeviceptr  projection_size_gpu,
const int  device_id 
)

Returns back total number of allocated rows per device (i.e., number of matched elements in projections).

TODO(Saman): revisit this for bump allocators

Definition at line 291 of file GpuMemUtils.cpp.

References CHECK, and copy_from_gpu().

Referenced by QueryExecutionContext::launchGpuCode().

293  {
294  int32_t num_rows{0};
295  copy_from_gpu(data_mgr, &num_rows, projection_size_gpu, sizeof(num_rows), device_id);
296  CHECK(num_rows >= 0);
297  return static_cast<size_t>(num_rows);
298 }
void copy_from_gpu(Data_Namespace::DataMgr *data_mgr, void *dst, const CUdeviceptr src, const size_t num_bytes, const int device_id)
#define CHECK(condition)
Definition: Logger.h:209

+ Here is the call graph for this function:

+ Here is the caller graph for this function: