OmniSciDB  6686921089
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GpuMemUtils.cpp File Reference
+ Include dependency graph for GpuMemUtils.cpp:

Go to the source code of this file.

Namespaces

 anonymous_namespace{GpuMemUtils.cpp}
 

Functions

void copy_to_nvidia_gpu (Data_Namespace::DataMgr *data_mgr, CUdeviceptr dst, const void *src, const size_t num_bytes, const int device_id)
 
size_t anonymous_namespace{GpuMemUtils.cpp}::coalesced_size (const QueryMemoryDescriptor &query_mem_desc, const size_t group_by_one_buffer_size, const unsigned grid_size_x)
 
GpuGroupByBuffers create_dev_group_by_buffers (DeviceAllocator *device_allocator, const std::vector< int64_t * > &group_by_buffers, const QueryMemoryDescriptor &query_mem_desc, 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 (DeviceAllocator &device_allocator, const std::vector< int64_t * > &group_by_buffers, const size_t groups_buffer_size, const int8_t *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 (DeviceAllocator &device_allocator, int8_t *projection_size_gpu, const int device_id)
 
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)
 

Variables

size_t g_max_memory_allocation_size
 
size_t g_min_memory_allocation_size
 
double g_bump_allocator_step_reduction
 

Function Documentation

void copy_group_by_buffers_from_gpu ( DeviceAllocator device_allocator,
const std::vector< int64_t * > &  group_by_buffers,
const size_t  groups_buffer_size,
const int8_t *  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 218 of file GpuMemUtils.cpp.

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

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

227  {
228  if (group_by_buffers.empty()) {
229  return;
230  }
231  const size_t first_group_buffer_idx = has_varlen_output ? 1 : 0;
232 
233  const unsigned block_buffer_count{query_mem_desc.blocksShareMemory() ? 1 : grid_size_x};
234  if (block_buffer_count == 1 && !prepend_index_buffer) {
235  CHECK_EQ(coalesced_size(query_mem_desc, groups_buffer_size, block_buffer_count),
236  groups_buffer_size);
237  device_allocator.copyFromDevice(group_by_buffers[first_group_buffer_idx],
238  group_by_dev_buffers_mem,
239  groups_buffer_size);
240  return;
241  }
242  const size_t index_buffer_sz{
243  prepend_index_buffer ? query_mem_desc.getEntryCount() * sizeof(int64_t) : 0};
244  std::vector<int8_t> buff_from_gpu(
245  coalesced_size(query_mem_desc, groups_buffer_size, block_buffer_count) +
246  index_buffer_sz);
247  device_allocator.copyFromDevice(&buff_from_gpu[0],
248  group_by_dev_buffers_mem - index_buffer_sz,
249  buff_from_gpu.size());
250  auto buff_from_gpu_ptr = &buff_from_gpu[0];
251  for (size_t i = 0; i < block_buffer_count; ++i) {
252  const size_t buffer_idx = (i * block_size_x) + first_group_buffer_idx;
253  CHECK_LT(buffer_idx, group_by_buffers.size());
254  memcpy(group_by_buffers[buffer_idx],
255  buff_from_gpu_ptr,
256  groups_buffer_size + index_buffer_sz);
257  buff_from_gpu_ptr += groups_buffer_size;
258  }
259 }
#define CHECK_EQ(x, y)
Definition: Logger.h:217
#define CHECK_LT(x, y)
Definition: Logger.h:219
virtual void copyFromDevice(void *host_dst, const void *device_src, const size_t num_bytes) const =0
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 283 of file GpuMemUtils.cpp.

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

Referenced by QueryMemoryInitializer::compactProjectionBuffersGpu().

289  {
290  CHECK(query_mem_desc.didOutputColumnar());
292  constexpr size_t row_index_width = sizeof(int64_t);
293 
294  auto allocator = data_mgr->createGpuAllocator(device_id);
295  // copy all the row indices back to the host
296  allocator->copyFromDevice(
297  projection_buffer, gpu_group_by_buffers.data, projection_count * row_index_width);
298  size_t buffer_offset_cpu{projection_count * row_index_width};
299  // other columns are actual non-lazy columns for the projection:
300  for (size_t i = 0; i < query_mem_desc.getSlotCount(); i++) {
301  if (query_mem_desc.getPaddedSlotWidthBytes(i) > 0) {
302  const auto column_proj_size =
303  projection_count * query_mem_desc.getPaddedSlotWidthBytes(i);
304  allocator->copyFromDevice(
305  projection_buffer + buffer_offset_cpu,
306  gpu_group_by_buffers.data + query_mem_desc.getColOffInBytes(i),
307  column_proj_size);
308  buffer_offset_cpu += align_to_int64(column_proj_size);
309  }
310  }
311 }
std::unique_ptr< DeviceAllocator > createGpuAllocator(int device_id)
Definition: DataMgr.cpp:522
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_nvidia_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 anonymous_namespace{ResultSetSortImpl.cu}::get_device_copy_ptr().

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:218
#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 query_mem_desc,
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 QueryMemoryInitializer::createAndInitializeGroupByBufferGpu(), and ResultSet::radixSortOnGpu().

73  {
74  if (group_by_buffers.empty() && !insitu_allocator) {
75  return {0, 0, 0, 0};
76  }
77  CHECK(device_allocator);
78 
79  size_t groups_buffer_size{0};
80  int8_t* group_by_dev_buffers_mem{nullptr};
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 = device_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 = device_allocator->alloc(mem_size);
122  } catch (const OutOfMemory& e) {
123  LOG(WARNING) << e.what();
124  max_memory_size = max_memory_size * g_bump_allocator_step_reduction;
125  if (max_memory_size < g_min_memory_allocation_size) {
126  throw;
127  }
128 
129  LOG(WARNING) << "Ran out of memory for projection query output. Retrying with "
130  << std::to_string(max_memory_size) << " bytes";
131 
132  continue;
133  }
134  break;
135  }
136  }
137  LOG(INFO) << "Projection query allocation succeeded with " << groups_buffer_size
138  << " bytes allocated (max entry count " << entry_count << ")";
139  } else {
140  entry_count = query_mem_desc.getEntryCount();
141  CHECK_GT(entry_count, size_t(0));
142  groups_buffer_size =
143  query_mem_desc.getBufferSizeBytes(ExecutorDeviceType::GPU, entry_count);
144  mem_size = coalesced_size(query_mem_desc,
145  groups_buffer_size,
146  query_mem_desc.blocksShareMemory() ? 1 : grid_size_x);
147  const size_t prepended_buff_size{
148  prepend_index_buffer ? align_to_int64(entry_count * sizeof(int32_t)) : 0};
149 
150  int8_t* group_by_dev_buffers_allocation{nullptr};
151  if (insitu_allocator) {
152  group_by_dev_buffers_allocation =
153  insitu_allocator->alloc(mem_size + prepended_buff_size);
154  } else {
155  group_by_dev_buffers_allocation =
156  device_allocator->alloc(mem_size + prepended_buff_size);
157  }
158  CHECK(group_by_dev_buffers_allocation);
159 
160  group_by_dev_buffers_mem = group_by_dev_buffers_allocation + prepended_buff_size;
161  }
162  CHECK_GT(groups_buffer_size, size_t(0));
163  CHECK(group_by_dev_buffers_mem);
164 
165  CHECK(query_mem_desc.threadsShareMemory());
166  const size_t step{block_size_x};
167 
168  if (!insitu_allocator && (always_init_group_by_on_host ||
169  !query_mem_desc.lazyInitGroups(ExecutorDeviceType::GPU))) {
170  std::vector<int8_t> buff_to_gpu(mem_size);
171  auto buff_to_gpu_ptr = buff_to_gpu.data();
172 
173  const size_t start = has_varlen_output ? 1 : 0;
174  for (size_t i = start; i < group_by_buffers.size(); i += step) {
175  memcpy(buff_to_gpu_ptr, group_by_buffers[i], groups_buffer_size);
176  buff_to_gpu_ptr += groups_buffer_size;
177  }
178  device_allocator->copyToDevice(reinterpret_cast<int8_t*>(group_by_dev_buffers_mem),
179  buff_to_gpu.data(),
180  buff_to_gpu.size());
181  }
182 
183  auto group_by_dev_buffer = group_by_dev_buffers_mem;
184 
185  const size_t num_ptrs =
186  (block_size_x * grid_size_x) + (has_varlen_output ? size_t(1) : size_t(0));
187 
188  std::vector<int8_t*> group_by_dev_buffers(num_ptrs);
189 
190  const size_t start_index = has_varlen_output ? 1 : 0;
191  for (size_t i = start_index; 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  int8_t* varlen_output_buffer{nullptr};
201  if (has_varlen_output) {
202  const auto varlen_buffer_elem_size_opt = query_mem_desc.varlenOutputBufferElemSize();
203  CHECK(varlen_buffer_elem_size_opt); // TODO(adb): relax
204 
205  group_by_dev_buffers[0] = device_allocator->alloc(
206  query_mem_desc.getEntryCount() * varlen_buffer_elem_size_opt.value());
207  varlen_output_buffer = group_by_dev_buffers[0];
208  }
209 
210  auto group_by_dev_ptr = device_allocator->alloc(num_ptrs * sizeof(CUdeviceptr));
211  device_allocator->copyToDevice(group_by_dev_ptr,
212  reinterpret_cast<int8_t*>(group_by_dev_buffers.data()),
213  num_ptrs * sizeof(CUdeviceptr));
214 
215  return {group_by_dev_ptr, group_by_dev_buffers_mem, entry_count, varlen_output_buffer};
216 }
size_t getBufferSizeBytes(const RelAlgExecutionUnit &ra_exe_unit, const unsigned thread_count, const ExecutorDeviceType device_type) const
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)
virtual void copyToDevice(void *device_dst, const void *host_src, const size_t num_bytes) const =0
bool lazyInitGroups(const ExecutorDeviceType) const
size_t g_max_memory_allocation_size
Definition: Execute.cpp:109
size_t g_min_memory_allocation_size
Definition: Execute.cpp:110
std::optional< size_t > varlenOutputBufferElemSize() const
#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 ( DeviceAllocator device_allocator,
int8_t *  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 267 of file GpuMemUtils.cpp.

References CHECK, and DeviceAllocator::copyFromDevice().

Referenced by QueryExecutionContext::launchGpuCode().

269  {
270  int32_t num_rows{0};
271  device_allocator.copyFromDevice(&num_rows, projection_size_gpu, sizeof(num_rows));
272  CHECK(num_rows >= 0);
273  return static_cast<size_t>(num_rows);
274 }
virtual void copyFromDevice(void *host_dst, const void *device_src, const size_t num_bytes) const =0
#define CHECK(condition)
Definition: Logger.h:209

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Variable Documentation

double g_bump_allocator_step_reduction

Definition at line 114 of file Execute.cpp.

size_t g_max_memory_allocation_size

Definition at line 109 of file Execute.cpp.

size_t g_min_memory_allocation_size

Definition at line 110 of file Execute.cpp.