OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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"
20 #include "GpuInitGroups.h"
21 #include "Shared/Logger.h"
22 #include "StreamingTopN.h"
23 
24 #include "../CudaMgr/CudaMgr.h"
25 #include "GroupByAndAggregate.h"
26 
27 extern size_t g_max_memory_allocation_size;
28 extern size_t g_min_memory_allocation_size;
30 
32  CUdeviceptr dst,
33  const void* src,
34  const size_t num_bytes,
35  const int device_id) {
36 #ifdef HAVE_CUDA
37  if (!data_mgr) { // only for unit tests
38  cuMemcpyHtoD(dst, src, num_bytes);
39  return;
40  }
41 #endif // HAVE_CUDA
42  const auto cuda_mgr = data_mgr->getCudaMgr();
43  CHECK(cuda_mgr);
44  cuda_mgr->copyHostToDevice(reinterpret_cast<int8_t*>(dst),
45  static_cast<const int8_t*>(src),
46  num_bytes,
47  device_id);
48 }
49 
50 namespace {
51 
53  const size_t group_by_one_buffer_size,
54  const unsigned grid_size_x) {
55  CHECK(query_mem_desc.threadsShareMemory());
56  return grid_size_x * group_by_one_buffer_size;
57 }
58 
59 } // namespace
60 
62  DeviceAllocator* cuda_allocator,
63  const std::vector<int64_t*>& group_by_buffers,
65  const unsigned block_size_x,
66  const unsigned grid_size_x,
67  const int device_id,
68  const ExecutorDispatchMode dispatch_mode,
69  const int64_t num_input_rows,
70  const bool prepend_index_buffer,
71  const bool always_init_group_by_on_host,
72  const bool use_bump_allocator,
73  Allocator* insitu_allocator) {
74  if (group_by_buffers.empty() && !insitu_allocator) {
75  return {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  for (size_t i = 0; i < group_by_buffers.size(); i += step) {
178  memcpy(buff_to_gpu_ptr, group_by_buffers[i], groups_buffer_size);
179  buff_to_gpu_ptr += groups_buffer_size;
180  }
181  cuda_allocator->copyToDevice(reinterpret_cast<int8_t*>(group_by_dev_buffers_mem),
182  buff_to_gpu.data(),
183  buff_to_gpu.size());
184  }
185 
186  auto group_by_dev_buffer = group_by_dev_buffers_mem;
187 
188  const size_t num_ptrs{block_size_x * grid_size_x};
189 
190  std::vector<CUdeviceptr> group_by_dev_buffers(num_ptrs);
191 
192  for (size_t i = 0; i < num_ptrs; i += step) {
193  for (size_t j = 0; j < step; ++j) {
194  group_by_dev_buffers[i + j] = group_by_dev_buffer;
195  }
196  if (!query_mem_desc.blocksShareMemory()) {
197  group_by_dev_buffer += groups_buffer_size;
198  }
199  }
200 
201  auto group_by_dev_ptr = cuda_allocator->alloc(num_ptrs * sizeof(CUdeviceptr));
202  cuda_allocator->copyToDevice(group_by_dev_ptr,
203  reinterpret_cast<int8_t*>(group_by_dev_buffers.data()),
204  num_ptrs * sizeof(CUdeviceptr));
205 
206  return {reinterpret_cast<CUdeviceptr>(group_by_dev_ptr),
207  group_by_dev_buffers_mem,
208  entry_count};
209 }
210 
212  void* dst,
213  const CUdeviceptr src,
214  const size_t num_bytes,
215  const int device_id) {
216  const auto cuda_mgr = data_mgr->getCudaMgr();
217  CHECK(cuda_mgr);
218  cuda_mgr->copyDeviceToHost(static_cast<int8_t*>(dst),
219  reinterpret_cast<const int8_t*>(src),
220  num_bytes,
221  device_id);
222 }
223 
225  const std::vector<int64_t*>& group_by_buffers,
226  const size_t groups_buffer_size,
227  const CUdeviceptr group_by_dev_buffers_mem,
228  const QueryMemoryDescriptor& query_mem_desc,
229  const unsigned block_size_x,
230  const unsigned grid_size_x,
231  const int device_id,
232  const bool prepend_index_buffer) {
233  if (group_by_buffers.empty()) {
234  return;
235  }
236  const unsigned block_buffer_count{query_mem_desc.blocksShareMemory() ? 1 : grid_size_x};
237  if (block_buffer_count == 1 && !prepend_index_buffer) {
238  CHECK_EQ(coalesced_size(query_mem_desc, groups_buffer_size, block_buffer_count),
239  groups_buffer_size);
240  copy_from_gpu(data_mgr,
241  group_by_buffers[0],
242  group_by_dev_buffers_mem,
243  groups_buffer_size,
244  device_id);
245  return;
246  }
247  const size_t index_buffer_sz{
248  prepend_index_buffer ? query_mem_desc.getEntryCount() * sizeof(int64_t) : 0};
249  std::vector<int8_t> buff_from_gpu(
250  coalesced_size(query_mem_desc, groups_buffer_size, block_buffer_count) +
251  index_buffer_sz);
252  copy_from_gpu(data_mgr,
253  &buff_from_gpu[0],
254  group_by_dev_buffers_mem - index_buffer_sz,
255  buff_from_gpu.size(),
256  device_id);
257  auto buff_from_gpu_ptr = &buff_from_gpu[0];
258  for (size_t i = 0; i < block_buffer_count; ++i) {
259  CHECK_LT(i * block_size_x, group_by_buffers.size());
260  memcpy(group_by_buffers[i * block_size_x],
261  buff_from_gpu_ptr,
262  groups_buffer_size + index_buffer_sz);
263  buff_from_gpu_ptr += groups_buffer_size;
264  }
265 }
266 
274  CUdeviceptr projection_size_gpu,
275  const int device_id) {
276  int32_t num_rows{0};
277  copy_from_gpu(data_mgr, &num_rows, projection_size_gpu, sizeof(num_rows), device_id);
278  CHECK(num_rows >= 0);
279  return static_cast<size_t>(num_rows);
280 }
281 
290  Data_Namespace::DataMgr* data_mgr,
291  const GpuGroupByBuffers& gpu_group_by_buffers,
292  const QueryMemoryDescriptor& query_mem_desc,
293  int8_t* projection_buffer,
294  const size_t projection_count,
295  const int device_id) {
296  CHECK(query_mem_desc.didOutputColumnar());
298  constexpr size_t row_index_width = sizeof(int64_t);
299  // copy all the row indices back to the host
300  copy_from_gpu(data_mgr,
301  reinterpret_cast<int64_t*>(projection_buffer),
302  gpu_group_by_buffers.second,
303  projection_count * row_index_width,
304  device_id);
305  size_t buffer_offset_cpu{projection_count * row_index_width};
306  // other columns are actual non-lazy columns for the projection:
307  for (size_t i = 0; i < query_mem_desc.getSlotCount(); i++) {
308  if (query_mem_desc.getPaddedSlotWidthBytes(i) > 0) {
309  const auto column_proj_size =
310  projection_count * query_mem_desc.getPaddedSlotWidthBytes(i);
311  copy_from_gpu(data_mgr,
312  projection_buffer + buffer_offset_cpu,
313  gpu_group_by_buffers.second + query_mem_desc.getColOffInBytes(i),
314  column_proj_size,
315  device_id);
316  buffer_offset_cpu += align_to_int64(column_proj_size);
317  }
318  }
319 }
CudaMgr_Namespace::CudaMgr * getCudaMgr() const
Definition: DataMgr.h:117
#define CHECK_EQ(x, y)
Definition: Logger.h:198
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:100
const int8_t const int64_t * num_rows
GpuGroupByBuffers create_dev_group_by_buffers(DeviceAllocator *cuda_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, Allocator *insitu_allocator)
Definition: GpuMemUtils.cpp:61
Streaming Top N algorithm.
#define LOG(tag)
Definition: Logger.h:185
unsigned long long CUdeviceptr
Definition: nocuda.h:27
virtual void copyToDevice(int8_t *device_dst, const int8_t *host_src, const size_t num_bytes) const =0
int64_t * src
virtual int8_t * alloc(const size_t num_bytes)=0
#define CHECK_GT(x, y)
Definition: Logger.h:202
std::string to_string(char const *&&v)
size_t g_max_memory_allocation_size
Definition: Execute.cpp:95
ExecutorDispatchMode
CUdeviceptr second
Definition: GpuMemUtils.h:61
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
size_t g_min_memory_allocation_size
Definition: Execute.cpp:96
bool lazyInitGroups(const ExecutorDeviceType) const
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_LT(x, y)
Definition: Logger.h:200
size_t get_num_allocated_rows_from_gpu(Data_Namespace::DataMgr *data_mgr, CUdeviceptr projection_size_gpu, const int device_id)
#define CHECK_LE(x, y)
Definition: Logger.h:201
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)
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)
Allocate GPU memory using GpuBuffers via DataMgr.
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:52
size_t getColOffInBytes(const size_t col_idx) const
FORCE_INLINE HOST DEVICE T align_to_int64(T addr)