OmniSciDB  6686921089
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GpuMemUtils.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, 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 #include "GpuMemUtils.h"
19 #include "GpuInitGroups.h"
20 #include "Logger/Logger.h"
21 #include "StreamingTopN.h"
22 
23 #include "../CudaMgr/CudaMgr.h"
24 #include "GroupByAndAggregate.h"
25 
26 extern size_t g_max_memory_allocation_size;
27 extern size_t g_min_memory_allocation_size;
29 
31  CUdeviceptr dst,
32  const void* src,
33  const size_t num_bytes,
34  const int device_id) {
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 }
48 
49 namespace {
50 
52  const size_t group_by_one_buffer_size,
53  const unsigned grid_size_x) {
54  CHECK(query_mem_desc.threadsShareMemory());
55  return grid_size_x * group_by_one_buffer_size;
56 }
57 
58 } // namespace
59 
61  DeviceAllocator* device_allocator,
62  const std::vector<int64_t*>& group_by_buffers,
64  const unsigned block_size_x,
65  const unsigned grid_size_x,
66  const int device_id,
67  const ExecutorDispatchMode dispatch_mode,
68  const int64_t num_input_rows,
69  const bool prepend_index_buffer,
70  const bool always_init_group_by_on_host,
71  const bool use_bump_allocator,
72  const bool has_varlen_output,
73  Allocator* insitu_allocator) {
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 }
217 
219  const std::vector<int64_t*>& group_by_buffers,
220  const size_t groups_buffer_size,
221  const int8_t* group_by_dev_buffers_mem,
223  const unsigned block_size_x,
224  const unsigned grid_size_x,
225  const int device_id,
226  const bool prepend_index_buffer,
227  const bool has_varlen_output) {
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 }
260 
268  int8_t* projection_size_gpu,
269  const int device_id) {
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 }
275 
284  Data_Namespace::DataMgr* data_mgr,
285  const GpuGroupByBuffers& gpu_group_by_buffers,
286  const QueryMemoryDescriptor& query_mem_desc,
287  int8_t* projection_buffer,
288  const size_t projection_count,
289  const int device_id) {
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 }
CudaMgr_Namespace::CudaMgr * getCudaMgr() const
Definition: DataMgr.h:218
#define CHECK_EQ(x, y)
Definition: Logger.h:217
size_t getBufferSizeBytes(const RelAlgExecutionUnit &ra_exe_unit, const unsigned thread_count, const ExecutorDeviceType device_type) const
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: GpuMemUtils.cpp:60
double g_bump_allocator_step_reduction
Definition: Execute.cpp:114
size_t get_num_allocated_rows_from_gpu(DeviceAllocator &device_allocator, int8_t *projection_size_gpu, const int device_id)
Streaming Top N algorithm.
#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)
ExecutorDispatchMode
virtual void copyToDevice(void *device_dst, const void *host_src, const size_t num_bytes) const =0
std::unique_ptr< DeviceAllocator > createGpuAllocator(int device_id)
Definition: DataMgr.cpp:522
bool lazyInitGroups(const ExecutorDeviceType) const
size_t g_max_memory_allocation_size
Definition: Execute.cpp:109
const int8_t getPaddedSlotWidthBytes(const size_t slot_idx) const
QueryDescriptionType getQueryDescriptionType() const
size_t g_min_memory_allocation_size
Definition: Execute.cpp:110
std::optional< size_t > varlenOutputBufferElemSize() const
#define CHECK_LT(x, y)
Definition: Logger.h:219
#define CHECK_LE(x, y)
Definition: Logger.h:220
Abstract class for managing device memory allocations.
virtual void copyFromDevice(void *host_dst, const void *device_src, const size_t num_bytes) const =0
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: GpuMemUtils.cpp:30
#define CHECK(condition)
Definition: Logger.h:209
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)
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
size_t getColOffInBytes(const size_t col_idx) const
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)
FORCE_INLINE HOST DEVICE T align_to_int64(T addr)