OmniSciDB  95562058bd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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, 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)
 
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 210 of file GpuMemUtils.cpp.

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

Referenced by OverlapsJoinHashTable::approximateTupleCount(), BaselineJoinHashTable::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(), OverlapsJoinHashTable::initHashTableOnGpu(), BaselineJoinHashTable::initHashTableOnGpu(), JoinHashTable::initOneToOneHashTable(), QueryExecutionContext::launchGpuCode(), ResultSet::makeVarlenTargetValue(), ResultSet::syncEstimatorBuffer(), BaselineJoinHashTable::toSet(), JoinHashTable::toSet(), BaselineJoinHashTable::toString(), and JoinHashTable::toString().

214  {
215  const auto cuda_mgr = data_mgr->getCudaMgr();
216  CHECK(cuda_mgr);
217  cuda_mgr->copyDeviceToHost(static_cast<int8_t*>(dst),
218  reinterpret_cast<const int8_t*>(src),
219  num_bytes,
220  device_id);
221 }
CudaMgr_Namespace::CudaMgr * getCudaMgr() const
Definition: DataMgr.h:208
int64_t * src
#define CHECK(condition)
Definition: Logger.h:197

+ 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 
)

Definition at line 223 of file GpuMemUtils.cpp.

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

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

231  {
232  if (group_by_buffers.empty()) {
233  return;
234  }
235  const unsigned block_buffer_count{query_mem_desc.blocksShareMemory() ? 1 : grid_size_x};
236  if (block_buffer_count == 1 && !prepend_index_buffer) {
237  CHECK_EQ(coalesced_size(query_mem_desc, groups_buffer_size, block_buffer_count),
238  groups_buffer_size);
239  copy_from_gpu(data_mgr,
240  group_by_buffers[0],
241  group_by_dev_buffers_mem,
242  groups_buffer_size,
243  device_id);
244  return;
245  }
246  const size_t index_buffer_sz{
247  prepend_index_buffer ? query_mem_desc.getEntryCount() * sizeof(int64_t) : 0};
248  std::vector<int8_t> buff_from_gpu(
249  coalesced_size(query_mem_desc, groups_buffer_size, block_buffer_count) +
250  index_buffer_sz);
251  copy_from_gpu(data_mgr,
252  &buff_from_gpu[0],
253  group_by_dev_buffers_mem - index_buffer_sz,
254  buff_from_gpu.size(),
255  device_id);
256  auto buff_from_gpu_ptr = &buff_from_gpu[0];
257  for (size_t i = 0; i < block_buffer_count; ++i) {
258  CHECK_LT(i * block_size_x, group_by_buffers.size());
259  memcpy(group_by_buffers[i * block_size_x],
260  buff_from_gpu_ptr,
261  groups_buffer_size + index_buffer_sz);
262  buff_from_gpu_ptr += groups_buffer_size;
263  }
264 }
#define CHECK_EQ(x, y)
Definition: Logger.h:205
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:207
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 288 of file GpuMemUtils.cpp.

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

Referenced by QueryMemoryInitializer::compactProjectionBuffersGpu().

294  {
295  CHECK(query_mem_desc.didOutputColumnar());
297  constexpr size_t row_index_width = sizeof(int64_t);
298  // copy all the row indices back to the host
299  copy_from_gpu(data_mgr,
300  reinterpret_cast<int64_t*>(projection_buffer),
301  gpu_group_by_buffers.second,
302  projection_count * row_index_width,
303  device_id);
304  size_t buffer_offset_cpu{projection_count * row_index_width};
305  // other columns are actual non-lazy columns for the projection:
306  for (size_t i = 0; i < query_mem_desc.getSlotCount(); i++) {
307  if (query_mem_desc.getPaddedSlotWidthBytes(i) > 0) {
308  const auto column_proj_size =
309  projection_count * query_mem_desc.getPaddedSlotWidthBytes(i);
310  copy_from_gpu(data_mgr,
311  projection_buffer + buffer_offset_cpu,
312  gpu_group_by_buffers.second + query_mem_desc.getColOffInBytes(i),
313  column_proj_size,
314  device_id);
315  buffer_offset_cpu += align_to_int64(column_proj_size);
316  }
317  }
318 }
CUdeviceptr second
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:197
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 OverlapsJoinHashTable::approximateTupleCount(), anonymous_namespace{ResultSetSortImpl.cu}::get_device_copy_ptr(), BaselineJoinHashTable::initHashTableForDevice(), OverlapsJoinHashTable::initHashTableOnGpu(), BaselineJoinHashTable::initHashTableOnGpu(), JoinHashTable::initOneToManyHashTable(), JoinHashTable::initOneToOneHashTable(), 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
int64_t * src
#define CHECK(condition)
Definition: Logger.h:197

+ 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,
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, logger::INFO, KernelPerFragment, QueryMemoryDescriptor::lazyInitGroups(), LOG, QueryMemoryDescriptor::threadsShareMemory(), to_string(), and logger::WARNING.

Referenced by ResultSet::radixSortOnGpu().

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

References CHECK, copy_from_gpu(), and num_rows.

Referenced by QueryExecutionContext::launchGpuCode().

274  {
275  int32_t num_rows{0};
276  copy_from_gpu(data_mgr, &num_rows, projection_size_gpu, sizeof(num_rows), device_id);
277  CHECK(num_rows >= 0);
278  return static_cast<size_t>(num_rows);
279 }
const int8_t const int64_t * num_rows
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:197

+ Here is the call graph for this function:

+ Here is the caller graph for this function: