OmniSciDB  06b3bd477c
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cuda_mapd_rt.cu
Go to the documentation of this file.
1 #include <cuda.h>
2 #include <float.h>
3 #include <stdint.h>
4 #include <stdio.h>
5 #include <limits>
6 #include "BufferCompaction.h"
7 #include "ExtensionFunctions.hpp"
8 #include "GpuRtConstants.h"
9 #include "HyperLogLogRank.h"
11 
12 #if CUDA_VERSION < 10000
13 static_assert(false, "CUDA v10.0 or later is required.");
14 #endif
15 
16 #if (defined(__CUDA_ARCH__) && __CUDA_ARCH__ < 350)
17 static_assert(false, "CUDA Compute Capability of 3.5 or greater is required.");
18 #endif
19 
20 extern "C" __device__ int64_t get_thread_index() {
21  return threadIdx.x;
22 }
23 
24 extern "C" __device__ int64_t get_block_index() {
25  return blockIdx.x;
26 }
27 
28 extern "C" __device__ int32_t pos_start_impl(const int32_t* row_index_resume) {
29  return blockIdx.x * blockDim.x + threadIdx.x;
30 }
31 
32 extern "C" __device__ int32_t group_buff_idx_impl() {
33  return pos_start_impl(NULL);
34 }
35 
36 extern "C" __device__ int32_t pos_step_impl() {
37  return blockDim.x * gridDim.x;
38 }
39 
40 extern "C" __device__ int8_t thread_warp_idx(const int8_t warp_sz) {
41  return threadIdx.x % warp_sz;
42 }
43 
44 extern "C" __device__ const int64_t* init_shared_mem_nop(
45  const int64_t* groups_buffer,
46  const int32_t groups_buffer_size) {
47  return groups_buffer;
48 }
49 
50 extern "C" __device__ void write_back_nop(int64_t* dest, int64_t* src, const int32_t sz) {
51 }
52 
53 /*
54  * Just declares and returns a dynamic shared memory pointer. Total size should be
55  * properly set during kernel launch
56  */
57 extern "C" __device__ int64_t* declare_dynamic_shared_memory() {
58  extern __shared__ int64_t shared_mem_buffer[];
59  return shared_mem_buffer;
60 }
61 
67 extern "C" __device__ const int64_t* init_shared_mem(const int64_t* global_groups_buffer,
68  const int32_t groups_buffer_size) {
69  // dynamic shared memory declaration
70  extern __shared__ int64_t shared_groups_buffer[];
71 
72  // it is assumed that buffer size is aligned with 64-bit units
73  // so it is safe to assign 64-bit to each thread
74  const int32_t buffer_units = groups_buffer_size >> 3;
75 
76  for (int32_t pos = threadIdx.x; pos < buffer_units; pos += blockDim.x) {
77  shared_groups_buffer[pos] = global_groups_buffer[pos];
78  }
79  __syncthreads();
80  return shared_groups_buffer;
81 }
82 
83 #define init_group_by_buffer_gpu_impl init_group_by_buffer_gpu
84 
85 #include "GpuInitGroups.cu"
86 
87 #undef init_group_by_buffer_gpu_impl
88 
89 // Dynamic watchdog: monitoring up to 64 SMs. E.g. GP100 config may have 60:
90 // 6 Graphics Processing Clusters (GPCs) * 10 Streaming Multiprocessors
91 // TODO(Saman): move these into a kernel parameter, allocated and initialized through CUDA
92 __device__ int64_t dw_sm_cycle_start[128]; // Set from host before launching the kernel
93 // TODO(Saman): make this cycle budget something constant in codegen level
94 __device__ int64_t dw_cycle_budget = 0; // Set from host before launching the kernel
95 __device__ int32_t dw_abort = 0; // TBD: set from host (async)
96 __device__ int32_t runtime_interrupt_flag = 0;
97 
98 __inline__ __device__ uint32_t get_smid(void) {
99  uint32_t ret;
100  asm("mov.u32 %0, %%smid;" : "=r"(ret));
101  return ret;
102 }
103 
104 /*
105  * The main objective of this function is to return true, if any of the following two
106  * scenarios happen:
107  * 1. receives a host request for aborting the kernel execution
108  * 2. kernel execution takes longer clock cycles than it was initially allowed
109  * The assumption is that all (or none) threads within a block return true for the
110  * watchdog, and the first thread within each block compares the recorded clock cycles for
111  * its occupying SM with the allowed budget. It also assumes that all threads entering
112  * this function are active (no critical edge exposure)
113  * NOTE: dw_cycle_budget, dw_abort, and dw_sm_cycle_start[] are all variables in global
114  * memory scope.
115  */
116 extern "C" __device__ bool dynamic_watchdog() {
117  // check for dynamic watchdog, if triggered all threads return true
118  if (dw_cycle_budget == 0LL) {
119  return false; // Uninitialized watchdog can't check time
120  }
121  if (dw_abort == 1) {
122  return true; // Received host request to abort
123  }
124  uint32_t smid = get_smid();
125  if (smid >= 128) {
126  return false;
127  }
128  __shared__ volatile int64_t dw_block_cycle_start; // Thread block shared cycle start
129  __shared__ volatile bool
130  dw_should_terminate; // all threads within a block should return together if
131  // watchdog criteria is met
132 
133  // thread 0 either initializes or read the initial clock cycle, the result is stored
134  // into shared memory. Since all threads wihtin a block shares the same SM, there's no
135  // point in using more threads here.
136  if (threadIdx.x == 0) {
137  dw_block_cycle_start = 0LL;
138  int64_t cycle_count = static_cast<int64_t>(clock64());
139  // Make sure the block hasn't switched SMs
140  if (smid == get_smid()) {
141  dw_block_cycle_start = static_cast<int64_t>(
142  atomicCAS(reinterpret_cast<unsigned long long*>(&dw_sm_cycle_start[smid]),
143  0ULL,
144  static_cast<unsigned long long>(cycle_count)));
145  }
146 
147  int64_t cycles = cycle_count - dw_block_cycle_start;
148  if ((smid == get_smid()) && (dw_block_cycle_start > 0LL) &&
149  (cycles > dw_cycle_budget)) {
150  // Check if we're out of time on this particular SM
151  dw_should_terminate = true;
152  } else {
153  dw_should_terminate = false;
154  }
155  }
156  __syncthreads();
157  return dw_should_terminate;
158 }
159 
160 extern "C" __device__ bool check_interrupt() {
161  return (runtime_interrupt_flag == 1) ? true : false;
162 }
163 
164 template <typename T = unsigned long long>
165 inline __device__ T get_empty_key() {
166  return EMPTY_KEY_64;
167 }
168 
169 template <>
170 inline __device__ unsigned int get_empty_key() {
171  return EMPTY_KEY_32;
172 }
173 
174 template <typename T>
175 inline __device__ int64_t* get_matching_group_value(int64_t* groups_buffer,
176  const uint32_t h,
177  const T* key,
178  const uint32_t key_count,
179  const uint32_t row_size_quad) {
180  const T empty_key = get_empty_key<T>();
181  uint32_t off = h * row_size_quad;
182  auto row_ptr = reinterpret_cast<T*>(groups_buffer + off);
183  {
184  const T old = atomicCAS(row_ptr, empty_key, *key);
185  if (empty_key == old && key_count > 1) {
186  for (size_t i = 1; i <= key_count - 1; ++i) {
187  atomicExch(row_ptr + i, key[i]);
188  }
189  }
190  }
191  if (key_count > 1) {
192  while (atomicAdd(row_ptr + key_count - 1, 0) == empty_key) {
193  // spin until the winning thread has finished writing the entire key and the init
194  // value
195  }
196  }
197  bool match = true;
198  for (uint32_t i = 0; i < key_count; ++i) {
199  if (row_ptr[i] != key[i]) {
200  match = false;
201  break;
202  }
203  }
204 
205  if (match) {
206  auto row_ptr_i8 = reinterpret_cast<int8_t*>(row_ptr + key_count);
207  return reinterpret_cast<int64_t*>(align_to_int64(row_ptr_i8));
208  }
209  return NULL;
210 }
211 
212 extern "C" __device__ int64_t* get_matching_group_value(int64_t* groups_buffer,
213  const uint32_t h,
214  const int64_t* key,
215  const uint32_t key_count,
216  const uint32_t key_width,
217  const uint32_t row_size_quad,
218  const int64_t* init_vals) {
219  switch (key_width) {
220  case 4:
221  return get_matching_group_value(groups_buffer,
222  h,
223  reinterpret_cast<const unsigned int*>(key),
224  key_count,
225  row_size_quad);
226  case 8:
227  return get_matching_group_value(groups_buffer,
228  h,
229  reinterpret_cast<const unsigned long long*>(key),
230  key_count,
231  row_size_quad);
232  default:
233  return NULL;
234  }
235 }
236 
237 template <typename T>
239  const uint32_t entry_count,
240  const uint32_t h,
241  const T* key,
242  const uint32_t key_count) {
243  const T empty_key = get_empty_key<T>();
244  const uint64_t old =
245  atomicCAS(reinterpret_cast<T*>(groups_buffer + h), empty_key, *key);
246  // the winner thread proceeds with writing the rest fo the keys
247  if (old == empty_key) {
248  uint32_t offset = h + entry_count;
249  for (size_t i = 1; i < key_count; ++i) {
250  *reinterpret_cast<T*>(groups_buffer + offset) = key[i];
251  offset += entry_count;
252  }
253  }
254 
255  __threadfence();
256  // for all threads except the winning thread, memory content of the keys
257  // related to the hash offset are checked again. In case of a complete match
258  // the hash offset is returned, otherwise -1 is returned
259  if (old != empty_key) {
260  uint32_t offset = h;
261  for (uint32_t i = 0; i < key_count; ++i) {
262  if (*reinterpret_cast<T*>(groups_buffer + offset) != key[i]) {
263  return -1;
264  }
265  offset += entry_count;
266  }
267  }
268  return h;
269 }
270 
271 extern "C" __device__ int32_t
273  const uint32_t entry_count,
274  const uint32_t h,
275  const int64_t* key,
276  const uint32_t key_count,
277  const uint32_t key_width) {
278  switch (key_width) {
279  case 4:
281  groups_buffer,
282  entry_count,
283  h,
284  reinterpret_cast<const unsigned int*>(key),
285  key_count);
286  case 8:
288  groups_buffer,
289  entry_count,
290  h,
291  reinterpret_cast<const unsigned long long*>(key),
292  key_count);
293  default:
294  return -1;
295  }
296 }
297 
298 extern "C" __device__ int64_t* get_matching_group_value_columnar(
299  int64_t* groups_buffer,
300  const uint32_t h,
301  const int64_t* key,
302  const uint32_t key_qw_count,
303  const size_t entry_count) {
304  uint32_t off = h;
305  {
306  const uint64_t old = atomicCAS(
307  reinterpret_cast<unsigned long long*>(groups_buffer + off), EMPTY_KEY_64, *key);
308  if (EMPTY_KEY_64 == old) {
309  for (size_t i = 0; i < key_qw_count; ++i) {
310  groups_buffer[off] = key[i];
311  off += entry_count;
312  }
313  return &groups_buffer[off];
314  }
315  }
316  __syncthreads();
317  off = h;
318  for (size_t i = 0; i < key_qw_count; ++i) {
319  if (groups_buffer[off] != key[i]) {
320  return NULL;
321  }
322  off += entry_count;
323  }
324  return &groups_buffer[off];
325 }
326 
327 #include "GroupByRuntime.cpp"
329 #include "MurmurHash.cpp"
330 #include "TopKRuntime.cpp"
331 
332 __device__ int64_t atomicMax64(int64_t* address, int64_t val) {
333  unsigned long long int* address_as_ull = (unsigned long long int*)address;
334  unsigned long long int old = *address_as_ull, assumed;
335 
336  do {
337  assumed = old;
338  old = atomicCAS(address_as_ull, assumed, max((long long)val, (long long)assumed));
339  } while (assumed != old);
340 
341  return old;
342 }
343 
344 __device__ int64_t atomicMin64(int64_t* address, int64_t val) {
345  unsigned long long int* address_as_ull = (unsigned long long int*)address;
346  unsigned long long int old = *address_as_ull, assumed;
347 
348  do {
349  assumed = old;
350  old = atomicCAS(address_as_ull, assumed, min((long long)val, (long long)assumed));
351  } while (assumed != old);
352 
353  return old;
354 }
355 
356 #if (defined(__CUDA_ARCH__) && __CUDA_ARCH__ < 600)
357 __device__ double atomicAdd(double* address, double val) {
358  unsigned long long int* address_as_ull = (unsigned long long int*)address;
359  unsigned long long int old = *address_as_ull, assumed;
360 
361  do {
362  assumed = old;
363  old = atomicCAS(address_as_ull,
364  assumed,
365  __double_as_longlong(val + __longlong_as_double(assumed)));
366 
367  // Note: uses integer comparison to avoid hang in case of NaN (since NaN != NaN)
368  } while (assumed != old);
369 
370  return __longlong_as_double(old);
371 }
372 #endif
373 
374 __device__ double atomicMax(double* address, double val) {
375  unsigned long long int* address_as_ull = (unsigned long long int*)address;
376  unsigned long long int old = *address_as_ull, assumed;
377 
378  do {
379  assumed = old;
380  old = atomicCAS(address_as_ull,
381  assumed,
382  __double_as_longlong(max(val, __longlong_as_double(assumed))));
383 
384  // Note: uses integer comparison to avoid hang in case of NaN (since NaN != NaN)
385  } while (assumed != old);
386 
387  return __longlong_as_double(old);
388 }
389 
390 __device__ float atomicMax(float* address, float val) {
391  int* address_as_int = (int*)address;
392  int old = *address_as_int, assumed;
393 
394  do {
395  assumed = old;
396  old = atomicCAS(
397  address_as_int, assumed, __float_as_int(max(val, __int_as_float(assumed))));
398 
399  // Note: uses integer comparison to avoid hang in case of NaN (since NaN != NaN)
400  } while (assumed != old);
401 
402  return __int_as_float(old);
403 }
404 
405 __device__ double atomicMin(double* address, double val) {
406  unsigned long long int* address_as_ull = (unsigned long long int*)address;
407  unsigned long long int old = *address_as_ull, assumed;
408 
409  do {
410  assumed = old;
411  old = atomicCAS(address_as_ull,
412  assumed,
413  __double_as_longlong(min(val, __longlong_as_double(assumed))));
414  } while (assumed != old);
415 
416  return __longlong_as_double(old);
417 }
418 
419 __device__ double atomicMin(float* address, float val) {
420  int* address_as_ull = (int*)address;
421  int old = *address_as_ull, assumed;
422 
423  do {
424  assumed = old;
425  old = atomicCAS(
426  address_as_ull, assumed, __float_as_int(min(val, __int_as_float(assumed))));
427  } while (assumed != old);
428 
429  return __int_as_float(old);
430 }
431 
432 extern "C" __device__ uint64_t agg_count_shared(uint64_t* agg, const int64_t val) {
433  return static_cast<uint64_t>(atomicAdd(reinterpret_cast<uint32_t*>(agg), 1UL));
434 }
435 
436 extern "C" __device__ uint32_t agg_count_int32_shared(uint32_t* agg, const int32_t val) {
437  return atomicAdd(agg, 1UL);
438 }
439 
440 extern "C" __device__ uint64_t agg_count_double_shared(uint64_t* agg, const double val) {
441  return agg_count_shared(agg, val);
442 }
443 
444 extern "C" __device__ uint32_t agg_count_float_shared(uint32_t* agg, const float val) {
445  return agg_count_int32_shared(agg, val);
446 }
447 
448 extern "C" __device__ int64_t agg_sum_shared(int64_t* agg, const int64_t val) {
449  return atomicAdd(reinterpret_cast<unsigned long long*>(agg), val);
450 }
451 
452 extern "C" __device__ int32_t agg_sum_int32_shared(int32_t* agg, const int32_t val) {
453  return atomicAdd(agg, val);
454 }
455 
456 extern "C" __device__ void agg_sum_float_shared(int32_t* agg, const float val) {
457  atomicAdd(reinterpret_cast<float*>(agg), val);
458 }
459 
460 extern "C" __device__ void agg_sum_double_shared(int64_t* agg, const double val) {
461  atomicAdd(reinterpret_cast<double*>(agg), val);
462 }
463 
464 extern "C" __device__ void agg_max_shared(int64_t* agg, const int64_t val) {
465  atomicMax64(agg, val);
466 }
467 
468 extern "C" __device__ void agg_max_int32_shared(int32_t* agg, const int32_t val) {
469  atomicMax(agg, val);
470 }
471 
472 extern "C" __device__ void agg_max_double_shared(int64_t* agg, const double val) {
473  atomicMax(reinterpret_cast<double*>(agg), val);
474 }
475 
476 extern "C" __device__ void agg_max_float_shared(int32_t* agg, const float val) {
477  atomicMax(reinterpret_cast<float*>(agg), val);
478 }
479 
480 extern "C" __device__ void agg_min_shared(int64_t* agg, const int64_t val) {
481  atomicMin64(agg, val);
482 }
483 
484 extern "C" __device__ void agg_min_int32_shared(int32_t* agg, const int32_t val) {
485  atomicMin(agg, val);
486 }
487 
488 #if CUDA_VERSION > 10000 && defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 700
489 __device__ void atomicMax16(int16_t* agg, const int16_t val) {
490  unsigned short int* address_as_us = reinterpret_cast<unsigned short int*>(agg);
491  unsigned short int old = *address_as_us, assumed;
492 
493  do {
494  assumed = old;
495  old = atomicCAS(address_as_us,
496  assumed,
497  static_cast<unsigned short>(max(static_cast<short int>(val),
498  static_cast<short int>(assumed))));
499  } while (assumed != old);
500 }
501 #else
502 __device__ void atomicMax16(int16_t* agg, const int16_t val) {
503  // properly align the input pointer:
504  unsigned int* base_address_u32 =
505  reinterpret_cast<unsigned int*>(reinterpret_cast<size_t>(agg) & ~0x3);
506 
507  unsigned int old_value = *base_address_u32;
508  unsigned int swap_value, compare_value;
509  do {
510  compare_value = old_value;
511  swap_value =
512  (reinterpret_cast<size_t>(agg) & 0x2)
513  ? static_cast<unsigned int>(max(static_cast<int16_t>(old_value >> 16), val))
514  << 16 |
515  (old_value & 0xFFFF)
516  : (old_value & 0xFFFF0000) |
517  static_cast<unsigned int>(
518  max(static_cast<int16_t>(old_value & 0xFFFF), val));
519  old_value = atomicCAS(base_address_u32, compare_value, swap_value);
520  } while (old_value != compare_value);
521 }
522 #endif
523 
524 __device__ void atomicMax8(int8_t* agg, const int8_t val) {
525  // properly align the input pointer:
526  unsigned int* base_address_u32 =
527  reinterpret_cast<unsigned int*>(reinterpret_cast<size_t>(agg) & ~0x3);
528 
529  // __byte_perm(unsigned int A, unsigned int B, unsigned int s):
530  // if s == 0x3214 returns {A[31..24], A[23..16], A[15..8], B[7..0]}
531  // if s == 0x3240 returns {A[31..24], A[23..16], B[7...0], A[7..0]}
532  // if s == 0x3410 returns {A[31..24], B[7....0], A[15..8], A[7..0]}
533  // if s == 0x4210 returns {B[7....0], A[23..16], A[15..8], A[7..0]}
534  constexpr unsigned int byte_permutations[] = {0x3214, 0x3240, 0x3410, 0x4210};
535  unsigned int old_value = *base_address_u32;
536  unsigned int swap_value, compare_value;
537  do {
538  compare_value = old_value;
539  auto max_value = static_cast<unsigned int>(
540  // compare val with its corresponding bits in the compare_value
541  max(val,
542  static_cast<int8_t>(__byte_perm(
543  compare_value, 0, (reinterpret_cast<size_t>(agg) & 0x3) | 0x4440))));
544  swap_value = __byte_perm(
545  compare_value, max_value, byte_permutations[reinterpret_cast<size_t>(agg) & 0x3]);
546  old_value = atomicCAS(base_address_u32, compare_value, swap_value);
547  } while (compare_value != old_value);
548 }
549 
550 #if CUDA_VERSION > 10000 && defined(__CUDA_ARCH__) && __CUDA_ARCH__ >= 700
551 __device__ void atomicMin16(int16_t* agg, const int16_t val) {
552  unsigned short int* address_as_us = reinterpret_cast<unsigned short int*>(agg);
553  unsigned short int old = *address_as_us, assumed;
554 
555  do {
556  assumed = old;
557  old = atomicCAS(address_as_us,
558  assumed,
559  static_cast<unsigned short>(min(static_cast<short int>(val),
560  static_cast<short int>(assumed))));
561  } while (assumed != old);
562 }
563 #else
564 __device__ void atomicMin16(int16_t* agg, const int16_t val) {
565  // properly align the input pointer:
566  unsigned int* base_address_u32 =
567  reinterpret_cast<unsigned int*>(reinterpret_cast<size_t>(agg) & ~0x3);
568 
569  unsigned int old_value = *base_address_u32;
570  unsigned int swap_value, compare_value;
571  do {
572  compare_value = old_value;
573  swap_value =
574  (reinterpret_cast<size_t>(agg) & 0x2)
575  ? static_cast<unsigned int>(min(static_cast<int16_t>(old_value >> 16), val))
576  << 16 |
577  (old_value & 0xFFFF)
578  : (old_value & 0xFFFF0000) |
579  static_cast<unsigned int>(
580  min(static_cast<int16_t>(old_value & 0xFFFF), val));
581  old_value = atomicCAS(base_address_u32, compare_value, swap_value);
582  } while (old_value != compare_value);
583 }
584 #endif
585 
586 __device__ void atomicMin16SkipVal(int16_t* agg,
587  const int16_t val,
588  const int16_t skip_val) {
589  // properly align the input pointer:
590  unsigned int* base_address_u32 =
591  reinterpret_cast<unsigned int*>(reinterpret_cast<size_t>(agg) & ~0x3);
592 
593  unsigned int old_value = *base_address_u32;
594  unsigned int swap_value, compare_value;
595  do {
596  compare_value = old_value;
597  int16_t selected_old_val = (reinterpret_cast<size_t>(agg) & 0x2)
598  ? static_cast<int16_t>(old_value >> 16)
599  : static_cast<int16_t>(old_value & 0xFFFF);
600 
601  swap_value =
602  (reinterpret_cast<size_t>(agg) & 0x2)
603  ? static_cast<unsigned int>(
604  selected_old_val == skip_val ? val : min(selected_old_val, val))
605  << 16 |
606  (old_value & 0xFFFF)
607  : (old_value & 0xFFFF0000) |
608  static_cast<unsigned int>(
609  selected_old_val == skip_val ? val : min(selected_old_val, val));
610  old_value = atomicCAS(base_address_u32, compare_value, swap_value);
611  } while (old_value != compare_value);
612 }
613 
614 __device__ void atomicMin8(int8_t* agg, const int8_t val) {
615  // properly align the input pointer:
616  unsigned int* base_address_u32 =
617  reinterpret_cast<unsigned int*>(reinterpret_cast<size_t>(agg) & ~0x3);
618 
619  constexpr unsigned int byte_permutations[] = {0x3214, 0x3240, 0x3410, 0x4210};
620  unsigned int old_value = *base_address_u32;
621  unsigned int swap_value, compare_value;
622  do {
623  compare_value = old_value;
624  auto min_value = static_cast<unsigned int>(
625  min(val,
626  static_cast<int8_t>(__byte_perm(
627  compare_value, 0, (reinterpret_cast<size_t>(agg) & 0x3) | 0x4440))));
628  swap_value = __byte_perm(
629  compare_value, min_value, byte_permutations[reinterpret_cast<size_t>(agg) & 0x3]);
630  old_value = atomicCAS(base_address_u32, compare_value, swap_value);
631  } while (compare_value != old_value);
632 }
633 
634 __device__ void atomicMin8SkipVal(int8_t* agg, const int8_t val, const int8_t skip_val) {
635  // properly align the input pointer:
636  unsigned int* base_address_u32 =
637  reinterpret_cast<unsigned int*>(reinterpret_cast<size_t>(agg) & ~0x3);
638 
639  constexpr unsigned int byte_permutations[] = {0x3214, 0x3240, 0x3410, 0x4210};
640  unsigned int old_value = *base_address_u32;
641  unsigned int swap_value, compare_value;
642  do {
643  compare_value = old_value;
644  int8_t selected_old_val = static_cast<int8_t>(
645  __byte_perm(compare_value, 0, (reinterpret_cast<size_t>(agg) & 0x3) | 0x4440));
646  auto min_value = static_cast<unsigned int>(
647  selected_old_val == skip_val ? val : min(val, selected_old_val));
648  swap_value = __byte_perm(
649  compare_value, min_value, byte_permutations[reinterpret_cast<size_t>(agg) & 0x3]);
650  old_value = atomicCAS(base_address_u32, compare_value, swap_value);
651  } while (compare_value != old_value);
652 }
653 
654 extern "C" __device__ void agg_max_int16_shared(int16_t* agg, const int16_t val) {
655  return atomicMax16(agg, val);
656 }
657 
658 extern "C" __device__ void agg_max_int8_shared(int8_t* agg, const int8_t val) {
659  return atomicMax8(agg, val);
660 }
661 
662 extern "C" __device__ void agg_min_int16_shared(int16_t* agg, const int16_t val) {
663  return atomicMin16(agg, val);
664 }
665 
666 extern "C" __device__ void agg_min_int8_shared(int8_t* agg, const int8_t val) {
667  return atomicMin8(agg, val);
668 }
669 
670 extern "C" __device__ void agg_min_double_shared(int64_t* agg, const double val) {
671  atomicMin(reinterpret_cast<double*>(agg), val);
672 }
673 
674 extern "C" __device__ void agg_min_float_shared(int32_t* agg, const float val) {
675  atomicMin(reinterpret_cast<float*>(agg), val);
676 }
677 
678 extern "C" __device__ void agg_id_shared(int64_t* agg, const int64_t val) {
679  *agg = val;
680 }
681 
682 extern "C" __device__ int32_t checked_single_agg_id_shared(int64_t* agg,
683  const int64_t val,
684  const int64_t null_val) {
685  unsigned long long int* address_as_ull = reinterpret_cast<unsigned long long int*>(agg);
686  unsigned long long int old = *address_as_ull, assumed;
687 
688  if (val == null_val) {
689  return 0;
690  }
691 
692  do {
693  if (static_cast<int64_t>(old) != null_val) {
694  if (static_cast<int64_t>(old) != val) {
695  // see Execute::ERR_SINGLE_VALUE_FOUND_MULTIPLE_VALUES
696  return 15;
697  } else {
698  break;
699  }
700  }
701 
702  assumed = old;
703  old = atomicCAS(address_as_ull, assumed, val);
704  } while (assumed != old);
705 
706  return 0;
707 }
708 
709 #define DEF_AGG_ID_INT_SHARED(n) \
710  extern "C" __device__ void agg_id_int##n##_shared(int##n##_t* agg, \
711  const int##n##_t val) { \
712  *agg = val; \
713  }
714 
718 
719 #undef DEF_AGG_ID_INT_SHARED
720 
721 extern "C" __device__ void agg_id_double_shared(int64_t* agg, const double val) {
722  *agg = *(reinterpret_cast<const int64_t*>(&val));
723 }
724 
725 extern "C" __device__ int32_t checked_single_agg_id_double_shared(int64_t* agg,
726  const double val,
727  const double null_val) {
728  unsigned long long int* address_as_ull = reinterpret_cast<unsigned long long int*>(agg);
729  unsigned long long int old = *address_as_ull, assumed;
730 
731  if (val == null_val) {
732  return 0;
733  }
734 
735  do {
736  if (static_cast<int64_t>(old) != __double_as_longlong(null_val)) {
737  if (static_cast<int64_t>(old) != __double_as_longlong(val)) {
738  // see Execute::ERR_SINGLE_VALUE_FOUND_MULTIPLE_VALUES
739  return 15;
740  } else {
741  break;
742  }
743  }
744 
745  assumed = old;
746  old = atomicCAS(address_as_ull, assumed, __double_as_longlong(val));
747  } while (assumed != old);
748 
749  return 0;
750 }
751 
752 extern "C" __device__ void agg_id_double_shared_slow(int64_t* agg, const double* val) {
753  *agg = *(reinterpret_cast<const int64_t*>(val));
754 }
755 
756 extern "C" __device__ int32_t
758  const double* valp,
759  const double null_val) {
760  unsigned long long int* address_as_ull = reinterpret_cast<unsigned long long int*>(agg);
761  unsigned long long int old = *address_as_ull, assumed;
762  double val = *valp;
763 
764  if (val == null_val) {
765  return 0;
766  }
767 
768  do {
769  if (static_cast<int64_t>(old) != __double_as_longlong(null_val)) {
770  if (static_cast<int64_t>(old) != __double_as_longlong(val)) {
771  // see Execute::ERR_SINGLE_VALUE_FOUND_MULTIPLE_VALUES
772  return 15;
773  } else {
774  break;
775  }
776  }
777 
778  assumed = old;
779  old = atomicCAS(address_as_ull, assumed, __double_as_longlong(val));
780  } while (assumed != old);
781 
782  return 0;
783 }
784 
785 extern "C" __device__ void agg_id_float_shared(int32_t* agg, const float val) {
786  *agg = __float_as_int(val);
787 }
788 
789 extern "C" __device__ int32_t checked_single_agg_id_float_shared(int32_t* agg,
790  const float val,
791  const float null_val) {
792  int* address_as_ull = reinterpret_cast<int*>(agg);
793  int old = *address_as_ull, assumed;
794 
795  if (val == null_val) {
796  return 0;
797  }
798 
799  do {
800  if (old != __float_as_int(null_val)) {
801  if (old != __float_as_int(val)) {
802  // see Execute::ERR_SINGLE_VALUE_FOUND_MULTIPLE_VALUES
803  return 15;
804  } else {
805  break;
806  }
807  }
808 
809  assumed = old;
810  old = atomicCAS(address_as_ull, assumed, __float_as_int(val));
811  } while (assumed != old);
812 
813  return 0;
814 }
815 
816 #define DEF_SKIP_AGG(base_agg_func) \
817  extern "C" __device__ ADDR_T base_agg_func##_skip_val_shared( \
818  ADDR_T* agg, const DATA_T val, const DATA_T skip_val) { \
819  if (val != skip_val) { \
820  return base_agg_func##_shared(agg, val); \
821  } \
822  return 0; \
823  }
824 
825 #define DATA_T int64_t
826 #define ADDR_T uint64_t
828 #undef DATA_T
829 #undef ADDR_T
830 
831 #define DATA_T int32_t
832 #define ADDR_T uint32_t
834 #undef DATA_T
835 #undef ADDR_T
836 
837 // Initial value for nullable column is INT32_MIN
838 extern "C" __device__ void agg_max_int32_skip_val_shared(int32_t* agg,
839  const int32_t val,
840  const int32_t skip_val) {
841  if (val != skip_val) {
842  agg_max_int32_shared(agg, val);
843  }
844 }
845 
846 extern "C" __device__ void agg_max_int16_skip_val_shared(int16_t* agg,
847  const int16_t val,
848  const int16_t skip_val) {
849  if (val != skip_val) {
850  agg_max_int16_shared(agg, val);
851  }
852 }
853 
854 extern "C" __device__ void agg_min_int16_skip_val_shared(int16_t* agg,
855  const int16_t val,
856  const int16_t skip_val) {
857  if (val != skip_val) {
858  atomicMin16SkipVal(agg, val, skip_val);
859  }
860 }
861 
862 extern "C" __device__ void agg_max_int8_skip_val_shared(int8_t* agg,
863  const int8_t val,
864  const int8_t skip_val) {
865  if (val != skip_val) {
866  agg_max_int8_shared(agg, val);
867  }
868 }
869 
870 extern "C" __device__ void agg_min_int8_skip_val_shared(int8_t* agg,
871  const int8_t val,
872  const int8_t skip_val) {
873  if (val != skip_val) {
874  atomicMin8SkipVal(agg, val, skip_val);
875  }
876 }
877 
878 __device__ int32_t atomicMin32SkipVal(int32_t* address,
879  int32_t val,
880  const int32_t skip_val) {
881  int32_t old = atomicExch(address, INT_MAX);
882  return atomicMin(address, old == skip_val ? val : min(old, val));
883 }
884 
885 extern "C" __device__ void agg_min_int32_skip_val_shared(int32_t* agg,
886  const int32_t val,
887  const int32_t skip_val) {
888  if (val != skip_val) {
889  atomicMin32SkipVal(agg, val, skip_val);
890  }
891 }
892 
893 __device__ int32_t atomicSum32SkipVal(int32_t* address,
894  const int32_t val,
895  const int32_t skip_val) {
896  unsigned int* address_as_int = (unsigned int*)address;
897  int32_t old = atomicExch(address_as_int, 0);
898  int32_t old2 = atomicAdd(address_as_int, old == skip_val ? val : (val + old));
899  return old == skip_val ? old2 : (old2 + old);
900 }
901 
902 extern "C" __device__ int32_t agg_sum_int32_skip_val_shared(int32_t* agg,
903  const int32_t val,
904  const int32_t skip_val) {
905  if (val != skip_val) {
906  const int32_t old = atomicSum32SkipVal(agg, val, skip_val);
907  return old;
908  }
909  return 0;
910 }
911 
912 __device__ int64_t atomicSum64SkipVal(int64_t* address,
913  const int64_t val,
914  const int64_t skip_val) {
915  unsigned long long int* address_as_ull = (unsigned long long int*)address;
916  int64_t old = atomicExch(address_as_ull, 0);
917  int64_t old2 = atomicAdd(address_as_ull, old == skip_val ? val : (val + old));
918  return old == skip_val ? old2 : (old2 + old);
919 }
920 
921 extern "C" __device__ int64_t agg_sum_skip_val_shared(int64_t* agg,
922  const int64_t val,
923  const int64_t skip_val) {
924  if (val != skip_val) {
925  return atomicSum64SkipVal(agg, val, skip_val);
926  }
927  return 0;
928 }
929 
930 __device__ int64_t atomicMin64SkipVal(int64_t* address,
931  int64_t val,
932  const int64_t skip_val) {
933  unsigned long long int* address_as_ull =
934  reinterpret_cast<unsigned long long int*>(address);
935  unsigned long long int old = *address_as_ull, assumed;
936 
937  do {
938  assumed = old;
939  old = atomicCAS(address_as_ull,
940  assumed,
941  assumed == skip_val ? val : min((long long)val, (long long)assumed));
942  } while (assumed != old);
943 
944  return old;
945 }
946 
947 extern "C" __device__ void agg_min_skip_val_shared(int64_t* agg,
948  const int64_t val,
949  const int64_t skip_val) {
950  if (val != skip_val) {
951  atomicMin64SkipVal(agg, val, skip_val);
952  }
953 }
954 
955 __device__ int64_t atomicMax64SkipVal(int64_t* address,
956  int64_t val,
957  const int64_t skip_val) {
958  unsigned long long int* address_as_ull =
959  reinterpret_cast<unsigned long long int*>(address);
960  unsigned long long int old = *address_as_ull, assumed;
961 
962  do {
963  assumed = old;
964  old = atomicCAS(address_as_ull,
965  assumed,
966  assumed == skip_val ? val : max((long long)val, (long long)assumed));
967  } while (assumed != old);
968 
969  return old;
970 }
971 
972 extern "C" __device__ void agg_max_skip_val_shared(int64_t* agg,
973  const int64_t val,
974  const int64_t skip_val) {
975  if (val != skip_val) {
976  atomicMax64SkipVal(agg, val, skip_val);
977  }
978 }
979 
980 #undef DEF_SKIP_AGG
981 #define DEF_SKIP_AGG(base_agg_func) \
982  extern "C" __device__ ADDR_T base_agg_func##_skip_val_shared( \
983  ADDR_T* agg, const DATA_T val, const DATA_T skip_val) { \
984  if (val != skip_val) { \
985  return base_agg_func##_shared(agg, val); \
986  } \
987  return *agg; \
988  }
989 
990 #define DATA_T double
991 #define ADDR_T uint64_t
993 #undef ADDR_T
994 #undef DATA_T
995 
996 #define DATA_T float
997 #define ADDR_T uint32_t
999 #undef ADDR_T
1000 #undef DATA_T
1001 
1002 // Initial value for nullable column is FLOAT_MIN
1003 extern "C" __device__ void agg_max_float_skip_val_shared(int32_t* agg,
1004  const float val,
1005  const float skip_val) {
1006  if (__float_as_int(val) != __float_as_int(skip_val)) {
1007  float old = atomicExch(reinterpret_cast<float*>(agg), -FLT_MAX);
1008  atomicMax(reinterpret_cast<float*>(agg),
1009  __float_as_int(old) == __float_as_int(skip_val) ? val : fmaxf(old, val));
1010  }
1011 }
1012 
1013 __device__ float atomicMinFltSkipVal(int32_t* address, float val, const float skip_val) {
1014  float old = atomicExch(reinterpret_cast<float*>(address), FLT_MAX);
1015  return atomicMin(
1016  reinterpret_cast<float*>(address),
1017  __float_as_int(old) == __float_as_int(skip_val) ? val : fminf(old, val));
1018 }
1019 
1020 extern "C" __device__ void agg_min_float_skip_val_shared(int32_t* agg,
1021  const float val,
1022  const float skip_val) {
1023  if (__float_as_int(val) != __float_as_int(skip_val)) {
1024  atomicMinFltSkipVal(agg, val, skip_val);
1025  }
1026 }
1027 
1028 __device__ void atomicSumFltSkipVal(float* address,
1029  const float val,
1030  const float skip_val) {
1031  float old = atomicExch(address, 0.f);
1032  atomicAdd(address, __float_as_int(old) == __float_as_int(skip_val) ? val : (val + old));
1033 }
1034 
1035 extern "C" __device__ void agg_sum_float_skip_val_shared(int32_t* agg,
1036  const float val,
1037  const float skip_val) {
1038  if (__float_as_int(val) != __float_as_int(skip_val)) {
1039  atomicSumFltSkipVal(reinterpret_cast<float*>(agg), val, skip_val);
1040  }
1041 }
1042 
1043 __device__ void atomicSumDblSkipVal(double* address,
1044  const double val,
1045  const double skip_val) {
1046  unsigned long long int* address_as_ull = (unsigned long long int*)address;
1047  double old = __longlong_as_double(atomicExch(address_as_ull, __double_as_longlong(0.)));
1048  atomicAdd(
1049  address,
1050  __double_as_longlong(old) == __double_as_longlong(skip_val) ? val : (val + old));
1051 }
1052 
1053 extern "C" __device__ void agg_sum_double_skip_val_shared(int64_t* agg,
1054  const double val,
1055  const double skip_val) {
1056  if (__double_as_longlong(val) != __double_as_longlong(skip_val)) {
1057  atomicSumDblSkipVal(reinterpret_cast<double*>(agg), val, skip_val);
1058  }
1059 }
1060 
1061 __device__ double atomicMinDblSkipVal(double* address,
1062  double val,
1063  const double skip_val) {
1064  unsigned long long int* address_as_ull =
1065  reinterpret_cast<unsigned long long int*>(address);
1066  unsigned long long int old = *address_as_ull;
1067  unsigned long long int skip_val_as_ull =
1068  *reinterpret_cast<const unsigned long long*>(&skip_val);
1069  unsigned long long int assumed;
1070 
1071  do {
1072  assumed = old;
1073  old = atomicCAS(address_as_ull,
1074  assumed,
1075  assumed == skip_val_as_ull
1076  ? *reinterpret_cast<unsigned long long*>(&val)
1077  : __double_as_longlong(min(val, __longlong_as_double(assumed))));
1078  } while (assumed != old);
1079 
1080  return __longlong_as_double(old);
1081 }
1082 
1083 extern "C" __device__ void agg_min_double_skip_val_shared(int64_t* agg,
1084  const double val,
1085  const double skip_val) {
1086  if (val != skip_val) {
1087  atomicMinDblSkipVal(reinterpret_cast<double*>(agg), val, skip_val);
1088  }
1089 }
1090 
1091 extern "C" __device__ void agg_max_double_skip_val_shared(int64_t* agg,
1092  const double val,
1093  const double skip_val) {
1094  if (__double_as_longlong(val) != __double_as_longlong(skip_val)) {
1095  double old = __longlong_as_double(atomicExch(
1096  reinterpret_cast<unsigned long long int*>(agg), __double_as_longlong(-DBL_MAX)));
1097  atomicMax(reinterpret_cast<double*>(agg),
1098  __double_as_longlong(old) == __double_as_longlong(skip_val)
1099  ? val
1100  : fmax(old, val));
1101  }
1102 }
1103 
1104 #undef DEF_SKIP_AGG
1105 
1106 extern "C" __device__ bool slotEmptyKeyCAS(int64_t* slot,
1107  int64_t new_val,
1108  int64_t init_val) {
1109  auto slot_address = reinterpret_cast<unsigned long long int*>(slot);
1110  const auto empty_key =
1111  static_cast<unsigned long long int*>(static_cast<void*>(&init_val));
1112  const auto new_val_cast =
1113  static_cast<unsigned long long int*>(static_cast<void*>(&new_val));
1114 
1115  const auto old_val = atomicCAS(slot_address, *empty_key, *new_val_cast);
1116  if (old_val == *empty_key) {
1117  return true;
1118  } else {
1119  return false;
1120  }
1121 }
1122 
1123 extern "C" __device__ bool slotEmptyKeyCAS_int32(int32_t* slot,
1124  int32_t new_val,
1125  int32_t init_val) {
1126  unsigned int* slot_address = reinterpret_cast<unsigned int*>(slot);
1127  unsigned int compare_value = static_cast<unsigned int>(init_val);
1128  unsigned int swap_value = static_cast<unsigned int>(new_val);
1129 
1130  const unsigned int old_value = atomicCAS(slot_address, compare_value, swap_value);
1131  return old_value == compare_value;
1132 }
1133 
1134 extern "C" __device__ bool slotEmptyKeyCAS_int16(int16_t* slot,
1135  int16_t new_val,
1136  int16_t init_val) {
1137  unsigned int* base_slot_address =
1138  reinterpret_cast<unsigned int*>(reinterpret_cast<size_t>(slot) & ~0x3);
1139  unsigned int old_value = *base_slot_address;
1140  unsigned int swap_value, compare_value;
1141  do {
1142  compare_value = old_value;
1143  // exit criteria: if init_val does not exist in the slot (some other thread has
1144  // succeeded)
1145  if (static_cast<unsigned int>(init_val) !=
1146  __byte_perm(
1147  compare_value, 0, (reinterpret_cast<size_t>(slot) & 0x2 ? 0x3244 : 0x4410))) {
1148  return false;
1149  }
1150  swap_value = __byte_perm(compare_value,
1151  static_cast<unsigned int>(new_val),
1152  (reinterpret_cast<size_t>(slot) & 0x2) ? 0x5410 : 0x3254);
1153  old_value = atomicCAS(base_slot_address, compare_value, swap_value);
1154  } while (compare_value != old_value);
1155  return true;
1156 }
1157 
1158 extern "C" __device__ bool slotEmptyKeyCAS_int8(int8_t* slot,
1159  int8_t new_val,
1160  int8_t init_val) {
1161  // properly align the slot address:
1162  unsigned int* base_slot_address =
1163  reinterpret_cast<unsigned int*>(reinterpret_cast<size_t>(slot) & ~0x3);
1164  constexpr unsigned int byte_permutations[] = {0x3214, 0x3240, 0x3410, 0x4210};
1165  unsigned int old_value = *base_slot_address;
1166  unsigned int swap_value, compare_value;
1167  do {
1168  compare_value = old_value;
1169  // exit criteria: if init_val does not exist in the slot (some other thread has
1170  // succeeded)
1171  if (static_cast<unsigned int>(init_val) !=
1172  __byte_perm(compare_value, 0, (reinterpret_cast<size_t>(slot) & 0x3) | 0x4440)) {
1173  return false;
1174  }
1175  swap_value = __byte_perm(compare_value,
1176  static_cast<unsigned int>(new_val),
1177  byte_permutations[reinterpret_cast<size_t>(slot) & 0x3]);
1178  old_value = atomicCAS(base_slot_address, compare_value, swap_value);
1179  } while (compare_value != old_value);
1180  return true;
1181 }
1182 
1183 #include "../Utils/ChunkIter.cpp"
1184 #include "DateTruncate.cpp"
1185 #include "ExtractFromTime.cpp"
1186 #define EXECUTE_INCLUDE
1187 #include "ArrayOps.cpp"
1188 #include "DateAdd.cpp"
1189 #include "StringFunctions.cpp"
1190 #undef EXECUTE_INCLUDE
1191 #include "../Utils/Regexp.cpp"
1192 #include "../Utils/StringLike.cpp"
1193 
1194 extern "C" __device__ uint64_t string_decode(int8_t* chunk_iter_, int64_t pos) {
1195  // TODO(alex): de-dup, the x64 version is basically identical
1196  ChunkIter* chunk_iter = reinterpret_cast<ChunkIter*>(chunk_iter_);
1197  VarlenDatum vd;
1198  bool is_end;
1199  ChunkIter_get_nth(chunk_iter, pos, false, &vd, &is_end);
1200  return vd.is_null ? 0
1201  : (reinterpret_cast<uint64_t>(vd.pointer) & 0xffffffffffff) |
1202  (static_cast<uint64_t>(vd.length) << 48);
1203 }
1204 
1205 extern "C" __device__ void linear_probabilistic_count(uint8_t* bitmap,
1206  const uint32_t bitmap_bytes,
1207  const uint8_t* key_bytes,
1208  const uint32_t key_len) {
1209  const uint32_t bit_pos = MurmurHash1(key_bytes, key_len, 0) % (bitmap_bytes * 8);
1210  const uint32_t word_idx = bit_pos / 32;
1211  const uint32_t bit_idx = bit_pos % 32;
1212  atomicOr(((uint32_t*)bitmap) + word_idx, 1 << bit_idx);
1213 }
1214 
1215 extern "C" __device__ void agg_count_distinct_bitmap_gpu(int64_t* agg,
1216  const int64_t val,
1217  const int64_t min_val,
1218  const int64_t base_dev_addr,
1219  const int64_t base_host_addr,
1220  const uint64_t sub_bitmap_count,
1221  const uint64_t bitmap_bytes) {
1222  const uint64_t bitmap_idx = val - min_val;
1223  const uint32_t byte_idx = bitmap_idx >> 3;
1224  const uint32_t word_idx = byte_idx >> 2;
1225  const uint32_t byte_word_idx = byte_idx & 3;
1226  const int64_t host_addr = *agg;
1227  uint32_t* bitmap = (uint32_t*)(base_dev_addr + host_addr - base_host_addr +
1228  (threadIdx.x & (sub_bitmap_count - 1)) * bitmap_bytes);
1229  switch (byte_word_idx) {
1230  case 0:
1231  atomicOr(&bitmap[word_idx], 1 << (bitmap_idx & 7));
1232  break;
1233  case 1:
1234  atomicOr(&bitmap[word_idx], 1 << ((bitmap_idx & 7) + 8));
1235  break;
1236  case 2:
1237  atomicOr(&bitmap[word_idx], 1 << ((bitmap_idx & 7) + 16));
1238  break;
1239  case 3:
1240  atomicOr(&bitmap[word_idx], 1 << ((bitmap_idx & 7) + 24));
1241  break;
1242  default:
1243  break;
1244  }
1245 }
1246 
1247 extern "C" __device__ void agg_count_distinct_bitmap_skip_val_gpu(
1248  int64_t* agg,
1249  const int64_t val,
1250  const int64_t min_val,
1251  const int64_t skip_val,
1252  const int64_t base_dev_addr,
1253  const int64_t base_host_addr,
1254  const uint64_t sub_bitmap_count,
1255  const uint64_t bitmap_bytes) {
1256  if (val != skip_val) {
1258  agg, val, min_val, base_dev_addr, base_host_addr, sub_bitmap_count, bitmap_bytes);
1259  }
1260 }
1261 
1262 extern "C" __device__ void agg_approximate_count_distinct_gpu(
1263  int64_t* agg,
1264  const int64_t key,
1265  const uint32_t b,
1266  const int64_t base_dev_addr,
1267  const int64_t base_host_addr) {
1268  const uint64_t hash = MurmurHash64A(&key, sizeof(key), 0);
1269  const uint32_t index = hash >> (64 - b);
1270  const int32_t rank = get_rank(hash << b, 64 - b);
1271  const int64_t host_addr = *agg;
1272  int32_t* M = (int32_t*)(base_dev_addr + host_addr - base_host_addr);
1273  atomicMax(&M[index], rank);
1274 }
1275 
1276 extern "C" __device__ void force_sync() {
1277  __threadfence_block();
1278 }
1279 
1280 extern "C" __device__ void sync_warp() {
1281  __syncwarp();
1282 }
1283 
1291 extern "C" __device__ void sync_warp_protected(int64_t thread_pos, int64_t row_count) {
1292  // only syncing if NOT within the same warp as those threads experiencing the critical
1293  // edge
1294  if ((((row_count - 1) | 0x1F) - thread_pos) >= 32) {
1295  __syncwarp();
1296  }
1297 }
1298 
1299 extern "C" __device__ void sync_threadblock() {
1300  __syncthreads();
1301 }
1302 
1303 /*
1304  * Currently, we just use this function for handling non-grouped aggregates
1305  * with COUNT queries (with GPU shared memory used). Later, we should generate code for
1306  * this depending on the type of aggregate functions.
1307  * TODO: we should use one contiguous global memory buffer, rather than current default
1308  * behaviour of multiple buffers, each for one aggregate. Once that's resolved, we can
1309  * do much cleaner than this function
1310  */
1311 extern "C" __device__ void write_back_non_grouped_agg(int64_t* input_buffer,
1312  int64_t* output_buffer,
1313  const int32_t agg_idx) {
1314  if (threadIdx.x == agg_idx) {
1315  agg_sum_shared(output_buffer, input_buffer[agg_idx]);
1316  }
1317 }
__device__ void sync_warp_protected(int64_t thread_pos, int64_t row_count)
__device__ int32_t checked_single_agg_id_double_shared_slow(int64_t *agg, const double *valp, const double null_val)
NEVER_INLINE DEVICE uint32_t MurmurHash1(const void *key, int len, const uint32_t seed)
Definition: MurmurHash.cpp:20
int64_t * src
const int32_t groups_buffer_size return groups_buffer
__device__ void agg_max_float_shared(int32_t *agg, const float val)
__device__ uint32_t agg_count_float_shared(uint32_t *agg, const float val)
__device__ void agg_count_distinct_bitmap_skip_val_gpu(int64_t *agg, const int64_t val, const int64_t min_val, const int64_t skip_val, const int64_t base_dev_addr, const int64_t base_host_addr, const uint64_t sub_bitmap_count, const uint64_t bitmap_bytes)
__device__ bool dynamic_watchdog()
__device__ int64_t * get_matching_group_value_columnar(int64_t *groups_buffer, const uint32_t h, const int64_t *key, const uint32_t key_qw_count, const size_t entry_count)
ALWAYS_INLINE uint32_t agg_count_float(uint32_t *agg, const float val)
__device__ void agg_max_shared(int64_t *agg, const int64_t val)
#define EMPTY_KEY_64
__device__ void write_back_nop(int64_t *dest, int64_t *src, const int32_t sz)
Definition: cuda_mapd_rt.cu:50
__device__ void agg_sum_float_skip_val_shared(int32_t *agg, const float val, const float skip_val)
ALWAYS_INLINE uint64_t agg_count(uint64_t *agg, const int64_t)
FORCE_INLINE uint8_t get_rank(uint64_t x, uint32_t b)
__device__ void agg_min_int32_shared(int32_t *agg, const int32_t val)
__device__ int8_t thread_warp_idx(const int8_t warp_sz)
Definition: cuda_mapd_rt.cu:40
__device__ int64_t dw_sm_cycle_start[128]
Definition: cuda_mapd_rt.cu:92
bool is_null
Definition: sqltypes.h:75
__device__ void agg_id_float_shared(int32_t *agg, const float val)
__device__ void agg_min_double_shared(int64_t *agg, const double val)
__device__ int64_t get_thread_index()
Definition: cuda_mapd_rt.cu:20
__device__ int32_t atomicMin32SkipVal(int32_t *address, int32_t val, const int32_t skip_val)
__device__ int32_t pos_step_impl()
Definition: cuda_mapd_rt.cu:36
__device__ void write_back_non_grouped_agg(int64_t *input_buffer, int64_t *output_buffer, const int32_t agg_idx)
__device__ void agg_min_int8_shared(int8_t *agg, const int8_t val)
__device__ int32_t checked_single_agg_id_double_shared(int64_t *agg, const double val, const double null_val)
__device__ float atomicMinFltSkipVal(int32_t *address, float val, const float skip_val)
__device__ const int64_t * init_shared_mem_nop(const int64_t *groups_buffer, const int32_t groups_buffer_size)
Definition: cuda_mapd_rt.cu:44
__device__ double atomicMin(double *address, double val)
__device__ void agg_max_int8_shared(int8_t *agg, const int8_t val)
__device__ int32_t checked_single_agg_id_float_shared(int32_t *agg, const float val, const float null_val)
__device__ void atomicMin8SkipVal(int8_t *agg, const int8_t val, const int8_t skip_val)
__device__ int64_t * get_matching_group_value(int64_t *groups_buffer, const uint32_t h, const T *key, const uint32_t key_count, const uint32_t row_size_quad)
__device__ uint32_t agg_count_int32_shared(uint32_t *agg, const int32_t val)
__device__ int64_t dw_cycle_budget
Definition: cuda_mapd_rt.cu:94
__device__ int64_t agg_sum_shared(int64_t *agg, const int64_t val)
__device__ void agg_id_double_shared_slow(int64_t *agg, const double *val)
ALWAYS_INLINE uint32_t agg_count_int32(uint32_t *agg, const int32_t)
const int64_t const uint32_t const uint32_t key_qw_count
DEVICE void ChunkIter_get_nth(ChunkIter *it, int n, bool uncompress, VarlenDatum *result, bool *is_end)
Definition: ChunkIter.cpp:181
__device__ void agg_min_float_shared(int32_t *agg, const float val)
__device__ int64_t atomicMin64(int64_t *address, int64_t val)
__device__ int64_t * declare_dynamic_shared_memory()
Definition: cuda_mapd_rt.cu:57
__device__ void agg_max_double_shared(int64_t *agg, const double val)
__device__ void atomicSumDblSkipVal(double *address, const double val, const double skip_val)
int8_t * pointer
Definition: sqltypes.h:74
__device__ int32_t agg_sum_int32_shared(int32_t *agg, const int32_t val)
__device__ int64_t agg_sum_skip_val_shared(int64_t *agg, const int64_t val, const int64_t skip_val)
__device__ void agg_sum_float_shared(int32_t *agg, const float val)
__device__ void agg_id_double_shared(int64_t *agg, const double val)
__device__ void agg_max_skip_val_shared(int64_t *agg, const int64_t val, const int64_t skip_val)
__device__ void atomicMax16(int16_t *agg, const int16_t val)
#define DEF_SKIP_AGG(base_agg_func)
__device__ int64_t get_block_index()
Definition: cuda_mapd_rt.cu:24
__device__ void agg_min_float_skip_val_shared(int32_t *agg, const float val, const float skip_val)
__device__ bool check_interrupt()
__device__ bool slotEmptyKeyCAS_int32(int32_t *slot, int32_t new_val, int32_t init_val)
NEVER_INLINE DEVICE uint64_t MurmurHash64A(const void *key, int len, uint64_t seed)
Definition: MurmurHash.cpp:26
__device__ int64_t atomicSum64SkipVal(int64_t *address, const int64_t val, const int64_t skip_val)
__device__ int32_t agg_sum_int32_skip_val_shared(int32_t *agg, const int32_t val, const int32_t skip_val)
__device__ void agg_min_int32_skip_val_shared(int32_t *agg, const int32_t val, const int32_t skip_val)
__device__ void linear_probabilistic_count(uint8_t *bitmap, const uint32_t bitmap_bytes, const uint8_t *key_bytes, const uint32_t key_len)
__device__ void agg_count_distinct_bitmap_gpu(int64_t *agg, const int64_t val, const int64_t min_val, const int64_t base_dev_addr, const int64_t base_host_addr, const uint64_t sub_bitmap_count, const uint64_t bitmap_bytes)
__device__ void atomicSumFltSkipVal(float *address, const float val, const float skip_val)
__device__ void agg_sum_double_shared(int64_t *agg, const double val)
__inline__ __device__ uint32_t get_smid(void)
Definition: cuda_mapd_rt.cu:98
__device__ void agg_min_skip_val_shared(int64_t *agg, const int64_t val, const int64_t skip_val)
__device__ uint64_t agg_count_shared(uint64_t *agg, const int64_t val)
__device__ int64_t atomicMax64(int64_t *address, int64_t val)
__device__ bool slotEmptyKeyCAS(int64_t *slot, int64_t new_val, int64_t init_val)
__device__ int32_t pos_start_impl(const int32_t *row_index_resume)
Definition: cuda_mapd_rt.cu:28
__device__ int64_t atomicMax64SkipVal(int64_t *address, int64_t val, const int64_t skip_val)
__device__ void atomicMin16(int16_t *agg, const int16_t val)
__device__ void agg_max_float_skip_val_shared(int32_t *agg, const float val, const float skip_val)
ALWAYS_INLINE uint64_t agg_count_double(uint64_t *agg, const double val)
__device__ int32_t runtime_interrupt_flag
Definition: cuda_mapd_rt.cu:96
__device__ void agg_approximate_count_distinct_gpu(int64_t *agg, const int64_t key, const uint32_t b, const int64_t base_dev_addr, const int64_t base_host_addr)
__device__ void sync_warp()
__device__ void atomicMin16SkipVal(int16_t *agg, const int16_t val, const int16_t skip_val)
__device__ void agg_sum_double_skip_val_shared(int64_t *agg, const double val, const double skip_val)
__device__ void agg_max_int8_skip_val_shared(int8_t *agg, const int8_t val, const int8_t skip_val)
__device__ void agg_max_int16_skip_val_shared(int16_t *agg, const int16_t val, const int16_t skip_val)
__device__ void atomicMin8(int8_t *agg, const int8_t val)
__device__ void agg_min_int16_shared(int16_t *agg, const int16_t val)
__device__ void agg_max_int16_shared(int16_t *agg, const int16_t val)
__device__ const int64_t * init_shared_mem(const int64_t *global_groups_buffer, const int32_t groups_buffer_size)
Definition: cuda_mapd_rt.cu:67
__device__ void agg_min_double_skip_val_shared(int64_t *agg, const double val, const double skip_val)
#define DEF_AGG_ID_INT_SHARED(n)
__device__ uint64_t agg_count_double_shared(uint64_t *agg, const double val)
#define EMPTY_KEY_32
__device__ T get_empty_key()
__device__ void agg_min_int16_skip_val_shared(int16_t *agg, const int16_t val, const int16_t skip_val)
__device__ void sync_threadblock()
__device__ void agg_min_int8_skip_val_shared(int8_t *agg, const int8_t val, const int8_t skip_val)
__device__ void atomicMax8(int8_t *agg, const int8_t val)
__device__ void agg_id_shared(int64_t *agg, const int64_t val)
__device__ double atomicMax(double *address, double val)
__device__ uint64_t string_decode(int8_t *chunk_iter_, int64_t pos)
const int64_t * init_vals
__device__ int32_t atomicSum32SkipVal(int32_t *address, const int32_t val, const int32_t skip_val)
__device__ double atomicMinDblSkipVal(double *address, double val, const double skip_val)
__device__ int32_t get_matching_group_value_columnar_slot(int64_t *groups_buffer, const uint32_t entry_count, const uint32_t h, const T *key, const uint32_t key_count)
__device__ void agg_max_int32_shared(int32_t *agg, const int32_t val)
__device__ int32_t checked_single_agg_id_shared(int64_t *agg, const int64_t val, const int64_t null_val)
__device__ void agg_max_int32_skip_val_shared(int32_t *agg, const int32_t val, const int32_t skip_val)
__device__ int32_t dw_abort
Definition: cuda_mapd_rt.cu:95
__device__ bool slotEmptyKeyCAS_int16(int16_t *slot, int16_t new_val, int16_t init_val)
__device__ void agg_max_double_skip_val_shared(int64_t *agg, const double val, const double skip_val)
FORCE_INLINE HOST DEVICE T align_to_int64(T addr)
__device__ int64_t atomicMin64SkipVal(int64_t *address, int64_t val, const int64_t skip_val)
Functions to support array operations used by the executor.
__device__ void force_sync()
__device__ void agg_min_shared(int64_t *agg, const int64_t val)
size_t length
Definition: sqltypes.h:73
__device__ bool slotEmptyKeyCAS_int8(int8_t *slot, int8_t new_val, int8_t init_val)
__device__ int32_t group_buff_idx_impl()
Definition: cuda_mapd_rt.cu:32