OmniSciDB  8fa3bf436f
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RuntimeFunctions.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2021 OmniSci, 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 #ifdef __CUDACC__
18 #error This code is not intended to be compiled with a CUDA C++ compiler
19 #endif // __CUDACC__
20 
21 #include "RuntimeFunctions.h"
22 #include "../Shared/funcannotations.h"
23 #include "BufferCompaction.h"
24 #include "HyperLogLogRank.h"
25 #include "MurmurHash.h"
26 #include "Shared/quantile.h"
27 #include "TypePunning.h"
28 
29 #include <algorithm>
30 #include <atomic>
31 #include <chrono>
32 #include <cmath>
33 #include <cstdio>
34 #include <cstring>
35 #include <thread>
36 #include <tuple>
37 
38 // decoder implementations
39 
40 #include "DecodersImpl.h"
41 
42 // arithmetic operator implementations
43 
44 #define DEF_ARITH_NULLABLE(type, null_type, opname, opsym) \
45  extern "C" ALWAYS_INLINE type opname##_##type##_nullable( \
46  const type lhs, const type rhs, const null_type null_val) { \
47  if (lhs != null_val && rhs != null_val) { \
48  return lhs opsym rhs; \
49  } \
50  return null_val; \
51  }
52 
53 #define DEF_ARITH_NULLABLE_LHS(type, null_type, opname, opsym) \
54  extern "C" ALWAYS_INLINE type opname##_##type##_nullable_lhs( \
55  const type lhs, const type rhs, const null_type null_val) { \
56  if (lhs != null_val) { \
57  return lhs opsym rhs; \
58  } \
59  return null_val; \
60  }
61 
62 #define DEF_ARITH_NULLABLE_RHS(type, null_type, opname, opsym) \
63  extern "C" ALWAYS_INLINE type opname##_##type##_nullable_rhs( \
64  const type lhs, const type rhs, const null_type null_val) { \
65  if (rhs != null_val) { \
66  return lhs opsym rhs; \
67  } \
68  return null_val; \
69  }
70 
71 #define DEF_CMP_NULLABLE(type, null_type, opname, opsym) \
72  extern "C" ALWAYS_INLINE int8_t opname##_##type##_nullable( \
73  const type lhs, \
74  const type rhs, \
75  const null_type null_val, \
76  const int8_t null_bool_val) { \
77  if (lhs != null_val && rhs != null_val) { \
78  return lhs opsym rhs; \
79  } \
80  return null_bool_val; \
81  }
82 
83 #define DEF_CMP_NULLABLE_LHS(type, null_type, opname, opsym) \
84  extern "C" ALWAYS_INLINE int8_t opname##_##type##_nullable_lhs( \
85  const type lhs, \
86  const type rhs, \
87  const null_type null_val, \
88  const int8_t null_bool_val) { \
89  if (lhs != null_val) { \
90  return lhs opsym rhs; \
91  } \
92  return null_bool_val; \
93  }
94 
95 #define DEF_CMP_NULLABLE_RHS(type, null_type, opname, opsym) \
96  extern "C" ALWAYS_INLINE int8_t opname##_##type##_nullable_rhs( \
97  const type lhs, \
98  const type rhs, \
99  const null_type null_val, \
100  const int8_t null_bool_val) { \
101  if (rhs != null_val) { \
102  return lhs opsym rhs; \
103  } \
104  return null_bool_val; \
105  }
106 
107 #define DEF_SAFE_DIV_NULLABLE(type, null_type, opname) \
108  extern "C" ALWAYS_INLINE type safe_div_##type( \
109  const type lhs, const type rhs, const null_type null_val) { \
110  if (lhs != null_val && rhs != null_val && rhs != 0) { \
111  return lhs / rhs; \
112  } \
113  return null_val; \
114  }
115 
116 #define DEF_BINARY_NULLABLE_ALL_OPS(type, null_type) \
117  DEF_ARITH_NULLABLE(type, null_type, add, +) \
118  DEF_ARITH_NULLABLE(type, null_type, sub, -) \
119  DEF_ARITH_NULLABLE(type, null_type, mul, *) \
120  DEF_ARITH_NULLABLE(type, null_type, div, /) \
121  DEF_SAFE_DIV_NULLABLE(type, null_type, safe_div) \
122  DEF_ARITH_NULLABLE_LHS(type, null_type, add, +) \
123  DEF_ARITH_NULLABLE_LHS(type, null_type, sub, -) \
124  DEF_ARITH_NULLABLE_LHS(type, null_type, mul, *) \
125  DEF_ARITH_NULLABLE_LHS(type, null_type, div, /) \
126  DEF_ARITH_NULLABLE_RHS(type, null_type, add, +) \
127  DEF_ARITH_NULLABLE_RHS(type, null_type, sub, -) \
128  DEF_ARITH_NULLABLE_RHS(type, null_type, mul, *) \
129  DEF_ARITH_NULLABLE_RHS(type, null_type, div, /) \
130  DEF_CMP_NULLABLE(type, null_type, eq, ==) \
131  DEF_CMP_NULLABLE(type, null_type, ne, !=) \
132  DEF_CMP_NULLABLE(type, null_type, lt, <) \
133  DEF_CMP_NULLABLE(type, null_type, gt, >) \
134  DEF_CMP_NULLABLE(type, null_type, le, <=) \
135  DEF_CMP_NULLABLE(type, null_type, ge, >=) \
136  DEF_CMP_NULLABLE_LHS(type, null_type, eq, ==) \
137  DEF_CMP_NULLABLE_LHS(type, null_type, ne, !=) \
138  DEF_CMP_NULLABLE_LHS(type, null_type, lt, <) \
139  DEF_CMP_NULLABLE_LHS(type, null_type, gt, >) \
140  DEF_CMP_NULLABLE_LHS(type, null_type, le, <=) \
141  DEF_CMP_NULLABLE_LHS(type, null_type, ge, >=) \
142  DEF_CMP_NULLABLE_RHS(type, null_type, eq, ==) \
143  DEF_CMP_NULLABLE_RHS(type, null_type, ne, !=) \
144  DEF_CMP_NULLABLE_RHS(type, null_type, lt, <) \
145  DEF_CMP_NULLABLE_RHS(type, null_type, gt, >) \
146  DEF_CMP_NULLABLE_RHS(type, null_type, le, <=) \
147  DEF_CMP_NULLABLE_RHS(type, null_type, ge, >=)
148 
149 DEF_BINARY_NULLABLE_ALL_OPS(int8_t, int64_t)
150 DEF_BINARY_NULLABLE_ALL_OPS(int16_t, int64_t)
151 DEF_BINARY_NULLABLE_ALL_OPS(int32_t, int64_t)
152 DEF_BINARY_NULLABLE_ALL_OPS(int64_t, int64_t)
153 DEF_BINARY_NULLABLE_ALL_OPS(float, float)
154 DEF_BINARY_NULLABLE_ALL_OPS(double, double)
155 DEF_ARITH_NULLABLE(int8_t, int64_t, mod, %)
156 DEF_ARITH_NULLABLE(int16_t, int64_t, mod, %)
157 DEF_ARITH_NULLABLE(int32_t, int64_t, mod, %)
158 DEF_ARITH_NULLABLE(int64_t, int64_t, mod, %)
159 DEF_ARITH_NULLABLE_LHS(int8_t, int64_t, mod, %)
160 DEF_ARITH_NULLABLE_LHS(int16_t, int64_t, mod, %)
161 DEF_ARITH_NULLABLE_LHS(int32_t, int64_t, mod, %)
162 DEF_ARITH_NULLABLE_LHS(int64_t, int64_t, mod, %)
163 DEF_ARITH_NULLABLE_RHS(int8_t, int64_t, mod, %)
164 DEF_ARITH_NULLABLE_RHS(int16_t, int64_t, mod, %)
165 DEF_ARITH_NULLABLE_RHS(int32_t, int64_t, mod, %)
166 DEF_ARITH_NULLABLE_RHS(int64_t, int64_t, mod, %)
167 
168 #undef DEF_BINARY_NULLABLE_ALL_OPS
169 #undef DEF_SAFE_DIV_NULLABLE
170 #undef DEF_CMP_NULLABLE_RHS
171 #undef DEF_CMP_NULLABLE_LHS
172 #undef DEF_CMP_NULLABLE
173 #undef DEF_ARITH_NULLABLE_RHS
174 #undef DEF_ARITH_NULLABLE_LHS
175 #undef DEF_ARITH_NULLABLE
176 
177 extern "C" ALWAYS_INLINE int64_t scale_decimal_up(const int64_t operand,
178  const uint64_t scale,
179  const int64_t operand_null_val,
180  const int64_t result_null_val) {
181  return operand != operand_null_val ? operand * scale : result_null_val;
182 }
183 
184 extern "C" ALWAYS_INLINE int64_t scale_decimal_down_nullable(const int64_t operand,
185  const int64_t scale,
186  const int64_t null_val) {
187  // rounded scale down of a decimal
188  if (operand == null_val) {
189  return null_val;
190  }
191 
192  int64_t tmp = scale >> 1;
193  tmp = operand >= 0 ? operand + tmp : operand - tmp;
194  return tmp / scale;
195 }
196 
197 extern "C" ALWAYS_INLINE int64_t scale_decimal_down_not_nullable(const int64_t operand,
198  const int64_t scale,
199  const int64_t null_val) {
200  int64_t tmp = scale >> 1;
201  tmp = operand >= 0 ? operand + tmp : operand - tmp;
202  return tmp / scale;
203 }
204 
205 // Return floor(dividend / divisor).
206 // Assumes 0 < divisor.
207 extern "C" ALWAYS_INLINE int64_t floor_div_lhs(const int64_t dividend,
208  const int64_t divisor) {
209  return (dividend < 0 ? dividend - (divisor - 1) : dividend) / divisor;
210 }
211 
212 // Return floor(dividend / divisor) or NULL if dividend IS NULL.
213 // Assumes 0 < divisor.
214 extern "C" ALWAYS_INLINE int64_t floor_div_nullable_lhs(const int64_t dividend,
215  const int64_t divisor,
216  const int64_t null_val) {
217  return dividend == null_val ? null_val : floor_div_lhs(dividend, divisor);
218 }
219 
220 #define DEF_UMINUS_NULLABLE(type, null_type) \
221  extern "C" ALWAYS_INLINE type uminus_##type##_nullable(const type operand, \
222  const null_type null_val) { \
223  return operand == null_val ? null_val : -operand; \
224  }
225 
226 DEF_UMINUS_NULLABLE(int8_t, int8_t)
227 DEF_UMINUS_NULLABLE(int16_t, int16_t)
228 DEF_UMINUS_NULLABLE(int32_t, int32_t)
229 DEF_UMINUS_NULLABLE(int64_t, int64_t)
230 DEF_UMINUS_NULLABLE(float, float)
231 DEF_UMINUS_NULLABLE(double, double)
232 
233 #undef DEF_UMINUS_NULLABLE
234 
235 #define DEF_CAST_NULLABLE(from_type, to_type) \
236  extern "C" ALWAYS_INLINE to_type cast_##from_type##_to_##to_type##_nullable( \
237  const from_type operand, \
238  const from_type from_null_val, \
239  const to_type to_null_val) { \
240  return operand == from_null_val ? to_null_val : operand; \
241  }
242 
243 #define DEF_CAST_NULLABLE_BIDIR(type1, type2) \
244  DEF_CAST_NULLABLE(type1, type2) \
245  DEF_CAST_NULLABLE(type2, type1)
246 
247 DEF_CAST_NULLABLE_BIDIR(int8_t, int16_t)
248 DEF_CAST_NULLABLE_BIDIR(int8_t, int32_t)
249 DEF_CAST_NULLABLE_BIDIR(int8_t, int64_t)
250 DEF_CAST_NULLABLE_BIDIR(int16_t, int32_t)
251 DEF_CAST_NULLABLE_BIDIR(int16_t, int64_t)
252 DEF_CAST_NULLABLE_BIDIR(int32_t, int64_t)
253 DEF_CAST_NULLABLE_BIDIR(float, double)
254 DEF_CAST_NULLABLE_BIDIR(float, int8_t)
255 DEF_CAST_NULLABLE_BIDIR(float, int16_t)
256 DEF_CAST_NULLABLE_BIDIR(float, int32_t)
257 DEF_CAST_NULLABLE_BIDIR(float, int64_t)
258 DEF_CAST_NULLABLE_BIDIR(double, int8_t)
259 DEF_CAST_NULLABLE_BIDIR(double, int16_t)
260 DEF_CAST_NULLABLE_BIDIR(double, int32_t)
261 DEF_CAST_NULLABLE_BIDIR(double, int64_t)
262 DEF_CAST_NULLABLE(uint8_t, int32_t)
263 DEF_CAST_NULLABLE(uint16_t, int32_t)
264 
265 #undef DEF_CAST_NULLABLE_BIDIR
266 #undef DEF_CAST_NULLABLE
267 
268 extern "C" ALWAYS_INLINE int8_t logical_not(const int8_t operand, const int8_t null_val) {
269  return operand == null_val ? operand : (operand ? 0 : 1);
270 }
271 
272 extern "C" ALWAYS_INLINE int8_t logical_and(const int8_t lhs,
273  const int8_t rhs,
274  const int8_t null_val) {
275  if (lhs == null_val) {
276  return rhs == 0 ? rhs : null_val;
277  }
278  if (rhs == null_val) {
279  return lhs == 0 ? lhs : null_val;
280  }
281  return (lhs && rhs) ? 1 : 0;
282 }
283 
284 extern "C" ALWAYS_INLINE int8_t logical_or(const int8_t lhs,
285  const int8_t rhs,
286  const int8_t null_val) {
287  if (lhs == null_val) {
288  return rhs == 0 ? null_val : rhs;
289  }
290  if (rhs == null_val) {
291  return lhs == 0 ? null_val : lhs;
292  }
293  return (lhs || rhs) ? 1 : 0;
294 }
295 
296 // aggregator implementations
297 
298 extern "C" ALWAYS_INLINE uint64_t agg_count(uint64_t* agg, const int64_t) {
299  return (*agg)++;
300 }
301 
302 extern "C" ALWAYS_INLINE void agg_count_distinct_bitmap(int64_t* agg,
303  const int64_t val,
304  const int64_t min_val) {
305  const uint64_t bitmap_idx = val - min_val;
306  reinterpret_cast<int8_t*>(*agg)[bitmap_idx >> 3] |= (1 << (bitmap_idx & 7));
307 }
308 
309 #ifdef _MSC_VER
310 #define GPU_RT_STUB NEVER_INLINE
311 #else
312 #define GPU_RT_STUB NEVER_INLINE __attribute__((optnone))
313 #endif
314 
316  const int64_t,
317  const int64_t,
318  const int64_t,
319  const int64_t,
320  const uint64_t,
321  const uint64_t) {}
322 
323 extern "C" NEVER_INLINE void agg_approximate_count_distinct(int64_t* agg,
324  const int64_t key,
325  const uint32_t b) {
326  const uint64_t hash = MurmurHash64A(&key, sizeof(key), 0);
327  const uint32_t index = hash >> (64 - b);
328  const uint8_t rank = get_rank(hash << b, 64 - b);
329  uint8_t* M = reinterpret_cast<uint8_t*>(*agg);
330  M[index] = std::max(M[index], rank);
331 }
332 
334  const int64_t,
335  const uint32_t,
336  const int64_t,
337  const int64_t) {}
338 
339 extern "C" ALWAYS_INLINE int8_t bit_is_set(const int64_t bitset,
340  const int64_t val,
341  const int64_t min_val,
342  const int64_t max_val,
343  const int64_t null_val,
344  const int8_t null_bool_val) {
345  if (val == null_val) {
346  return null_bool_val;
347  }
348  if (val < min_val || val > max_val) {
349  return 0;
350  }
351  if (!bitset) {
352  return 0;
353  }
354  const uint64_t bitmap_idx = val - min_val;
355  return (reinterpret_cast<const int8_t*>(bitset))[bitmap_idx >> 3] &
356  (1 << (bitmap_idx & 7))
357  ? 1
358  : 0;
359 }
360 
361 extern "C" ALWAYS_INLINE int64_t agg_sum(int64_t* agg, const int64_t val) {
362  const auto old = *agg;
363  *agg += val;
364  return old;
365 }
366 
367 extern "C" ALWAYS_INLINE void agg_max(int64_t* agg, const int64_t val) {
368  *agg = std::max(*agg, val);
369 }
370 
371 extern "C" ALWAYS_INLINE void agg_min(int64_t* agg, const int64_t val) {
372  *agg = std::min(*agg, val);
373 }
374 
375 extern "C" ALWAYS_INLINE void agg_id(int64_t* agg, const int64_t val) {
376  *agg = val;
377 }
378 
379 extern "C" ALWAYS_INLINE int32_t checked_single_agg_id(int64_t* agg,
380  const int64_t val,
381  const int64_t null_val) {
382  if (val == null_val) {
383  return 0;
384  }
385 
386  if (*agg == val) {
387  return 0;
388  } else if (*agg == null_val) {
389  *agg = val;
390  return 0;
391  } else {
392  // see Execute::ERR_SINGLE_VALUE_FOUND_MULTIPLE_VALUES
393  return 15;
394  }
395 }
396 
398  const int64_t val,
399  const int64_t min_val,
400  const int64_t skip_val) {
401  if (val != skip_val) {
402  agg_count_distinct_bitmap(agg, val, min_val);
403  }
404 }
405 
407  const int64_t,
408  const int64_t,
409  const int64_t,
410  const int64_t,
411  const int64_t,
412  const uint64_t,
413  const uint64_t) {}
414 
415 extern "C" ALWAYS_INLINE uint32_t agg_count_int32(uint32_t* agg, const int32_t) {
416  return (*agg)++;
417 }
418 
419 extern "C" ALWAYS_INLINE int32_t agg_sum_int32(int32_t* agg, const int32_t val) {
420  const auto old = *agg;
421  *agg += val;
422  return old;
423 }
424 
425 #define DEF_AGG_MAX_INT(n) \
426  extern "C" ALWAYS_INLINE void agg_max_int##n(int##n##_t* agg, const int##n##_t val) { \
427  *agg = std::max(*agg, val); \
428  }
429 
430 DEF_AGG_MAX_INT(32)
431 DEF_AGG_MAX_INT(16)
433 #undef DEF_AGG_MAX_INT
434 
435 #define DEF_AGG_MIN_INT(n) \
436  extern "C" ALWAYS_INLINE void agg_min_int##n(int##n##_t* agg, const int##n##_t val) { \
437  *agg = std::min(*agg, val); \
438  }
439 
440 DEF_AGG_MIN_INT(32)
441 DEF_AGG_MIN_INT(16)
443 #undef DEF_AGG_MIN_INT
444 
445 #define DEF_AGG_ID_INT(n) \
446  extern "C" ALWAYS_INLINE void agg_id_int##n(int##n##_t* agg, const int##n##_t val) { \
447  *agg = val; \
448  }
449 
450 #define DEF_CHECKED_SINGLE_AGG_ID_INT(n) \
451  extern "C" ALWAYS_INLINE int32_t checked_single_agg_id_int##n( \
452  int##n##_t* agg, const int##n##_t val, const int##n##_t null_val) { \
453  if (val == null_val) { \
454  return 0; \
455  } \
456  if (*agg == val) { \
457  return 0; \
458  } else if (*agg == null_val) { \
459  *agg = val; \
460  return 0; \
461  } else { \
462  /* see Execute::ERR_SINGLE_VALUE_FOUND_MULTIPLE_VALUES*/ \
463  return 15; \
464  } \
465  }
466 
467 DEF_AGG_ID_INT(32)
468 DEF_AGG_ID_INT(16)
470 
474 
475 #undef DEF_AGG_ID_INT
476 #undef DEF_CHECKED_SINGLE_AGG_ID_INT
477 
478 #define DEF_WRITE_PROJECTION_INT(n) \
479  extern "C" ALWAYS_INLINE void write_projection_int##n( \
480  int8_t* slot_ptr, const int##n##_t val, const int64_t init_val) { \
481  if (val != init_val) { \
482  *reinterpret_cast<int##n##_t*>(slot_ptr) = val; \
483  } \
484  }
485 
488 #undef DEF_WRITE_PROJECTION_INT
489 
490 extern "C" ALWAYS_INLINE int64_t agg_sum_skip_val(int64_t* agg,
491  const int64_t val,
492  const int64_t skip_val) {
493  const auto old = *agg;
494  if (val != skip_val) {
495  if (old != skip_val) {
496  return agg_sum(agg, val);
497  } else {
498  *agg = val;
499  }
500  }
501  return old;
502 }
503 
504 extern "C" ALWAYS_INLINE int32_t agg_sum_int32_skip_val(int32_t* agg,
505  const int32_t val,
506  const int32_t skip_val) {
507  const auto old = *agg;
508  if (val != skip_val) {
509  if (old != skip_val) {
510  return agg_sum_int32(agg, val);
511  } else {
512  *agg = val;
513  }
514  }
515  return old;
516 }
517 
518 extern "C" ALWAYS_INLINE uint64_t agg_count_skip_val(uint64_t* agg,
519  const int64_t val,
520  const int64_t skip_val) {
521  if (val != skip_val) {
522  return agg_count(agg, val);
523  }
524  return *agg;
525 }
526 
527 extern "C" ALWAYS_INLINE uint32_t agg_count_int32_skip_val(uint32_t* agg,
528  const int32_t val,
529  const int32_t skip_val) {
530  if (val != skip_val) {
531  return agg_count_int32(agg, val);
532  }
533  return *agg;
534 }
535 
536 #define DEF_SKIP_AGG_ADD(base_agg_func) \
537  extern "C" ALWAYS_INLINE void base_agg_func##_skip_val( \
538  DATA_T* agg, const DATA_T val, const DATA_T skip_val) { \
539  if (val != skip_val) { \
540  base_agg_func(agg, val); \
541  } \
542  }
543 
544 #define DEF_SKIP_AGG(base_agg_func) \
545  extern "C" ALWAYS_INLINE void base_agg_func##_skip_val( \
546  DATA_T* agg, const DATA_T val, const DATA_T skip_val) { \
547  if (val != skip_val) { \
548  const DATA_T old_agg = *agg; \
549  if (old_agg != skip_val) { \
550  base_agg_func(agg, val); \
551  } else { \
552  *agg = val; \
553  } \
554  } \
555  }
556 
557 #define DATA_T int64_t
560 #undef DATA_T
561 
562 #define DATA_T int32_t
565 #undef DATA_T
566 
567 #define DATA_T int16_t
570 #undef DATA_T
571 
572 #define DATA_T int8_t
575 #undef DATA_T
576 
577 #undef DEF_SKIP_AGG_ADD
578 #undef DEF_SKIP_AGG
579 
580 // TODO(alex): fix signature
581 
582 extern "C" ALWAYS_INLINE uint64_t agg_count_double(uint64_t* agg, const double val) {
583  return (*agg)++;
584 }
585 
586 extern "C" ALWAYS_INLINE void agg_sum_double(int64_t* agg, const double val) {
587  const auto r = *reinterpret_cast<const double*>(agg) + val;
588  *agg = *reinterpret_cast<const int64_t*>(may_alias_ptr(&r));
589 }
590 
591 extern "C" ALWAYS_INLINE void agg_max_double(int64_t* agg, const double val) {
592  const auto r = std::max(*reinterpret_cast<const double*>(agg), val);
593  *agg = *(reinterpret_cast<const int64_t*>(may_alias_ptr(&r)));
594 }
595 
596 extern "C" ALWAYS_INLINE void agg_min_double(int64_t* agg, const double val) {
597  const auto r = std::min(*reinterpret_cast<const double*>(agg), val);
598  *agg = *(reinterpret_cast<const int64_t*>(may_alias_ptr(&r)));
599 }
600 
601 extern "C" ALWAYS_INLINE void agg_id_double(int64_t* agg, const double val) {
602  *agg = *(reinterpret_cast<const int64_t*>(may_alias_ptr(&val)));
603 }
604 
605 extern "C" ALWAYS_INLINE int32_t checked_single_agg_id_double(int64_t* agg,
606  const double val,
607  const double null_val) {
608  if (val == null_val) {
609  return 0;
610  }
611 
612  if (*agg == *(reinterpret_cast<const int64_t*>(may_alias_ptr(&val)))) {
613  return 0;
614  } else if (*agg == *(reinterpret_cast<const int64_t*>(may_alias_ptr(&null_val)))) {
615  *agg = *(reinterpret_cast<const int64_t*>(may_alias_ptr(&val)));
616  return 0;
617  } else {
618  // see Execute::ERR_SINGLE_VALUE_FOUND_MULTIPLE_VALUES
619  return 15;
620  }
621 }
622 
623 extern "C" ALWAYS_INLINE uint32_t agg_count_float(uint32_t* agg, const float val) {
624  return (*agg)++;
625 }
626 
627 extern "C" ALWAYS_INLINE void agg_sum_float(int32_t* agg, const float val) {
628  const auto r = *reinterpret_cast<const float*>(agg) + val;
629  *agg = *reinterpret_cast<const int32_t*>(may_alias_ptr(&r));
630 }
631 
632 extern "C" ALWAYS_INLINE void agg_max_float(int32_t* agg, const float val) {
633  const auto r = std::max(*reinterpret_cast<const float*>(agg), val);
634  *agg = *(reinterpret_cast<const int32_t*>(may_alias_ptr(&r)));
635 }
636 
637 extern "C" ALWAYS_INLINE void agg_min_float(int32_t* agg, const float val) {
638  const auto r = std::min(*reinterpret_cast<const float*>(agg), val);
639  *agg = *(reinterpret_cast<const int32_t*>(may_alias_ptr(&r)));
640 }
641 
642 extern "C" ALWAYS_INLINE void agg_id_float(int32_t* agg, const float val) {
643  *agg = *(reinterpret_cast<const int32_t*>(may_alias_ptr(&val)));
644 }
645 
646 extern "C" ALWAYS_INLINE int32_t checked_single_agg_id_float(int32_t* agg,
647  const float val,
648  const float null_val) {
649  if (val == null_val) {
650  return 0;
651  }
652 
653  if (*agg == *(reinterpret_cast<const int32_t*>(may_alias_ptr(&val)))) {
654  return 0;
655  } else if (*agg == *(reinterpret_cast<const int32_t*>(may_alias_ptr(&null_val)))) {
656  *agg = *(reinterpret_cast<const int32_t*>(may_alias_ptr(&val)));
657  return 0;
658  } else {
659  // see Execute::ERR_SINGLE_VALUE_FOUND_MULTIPLE_VALUES
660  return 15;
661  }
662 }
663 
664 extern "C" ALWAYS_INLINE uint64_t agg_count_double_skip_val(uint64_t* agg,
665  const double val,
666  const double skip_val) {
667  if (val != skip_val) {
668  return agg_count_double(agg, val);
669  }
670  return *agg;
671 }
672 
673 extern "C" ALWAYS_INLINE uint32_t agg_count_float_skip_val(uint32_t* agg,
674  const float val,
675  const float skip_val) {
676  if (val != skip_val) {
677  return agg_count_float(agg, val);
678  }
679  return *agg;
680 }
681 
682 #define DEF_SKIP_AGG_ADD(base_agg_func) \
683  extern "C" ALWAYS_INLINE void base_agg_func##_skip_val( \
684  ADDR_T* agg, const DATA_T val, const DATA_T skip_val) { \
685  if (val != skip_val) { \
686  base_agg_func(agg, val); \
687  } \
688  }
689 
690 #define DEF_SKIP_AGG(base_agg_func) \
691  extern "C" ALWAYS_INLINE void base_agg_func##_skip_val( \
692  ADDR_T* agg, const DATA_T val, const DATA_T skip_val) { \
693  if (val != skip_val) { \
694  const ADDR_T old_agg = *agg; \
695  if (old_agg != *reinterpret_cast<const ADDR_T*>(may_alias_ptr(&skip_val))) { \
696  base_agg_func(agg, val); \
697  } else { \
698  *agg = *reinterpret_cast<const ADDR_T*>(may_alias_ptr(&val)); \
699  } \
700  } \
701  }
702 
703 #define DATA_T double
704 #define ADDR_T int64_t
708 #undef ADDR_T
709 #undef DATA_T
710 
711 #define DATA_T float
712 #define ADDR_T int32_t
716 #undef ADDR_T
717 #undef DATA_T
718 
719 #undef DEF_SKIP_AGG_ADD
720 #undef DEF_SKIP_AGG
721 
722 extern "C" ALWAYS_INLINE int64_t decimal_floor(const int64_t x, const int64_t scale) {
723  if (x >= 0) {
724  return x / scale * scale;
725  }
726  if (!(x % scale)) {
727  return x;
728  }
729  return x / scale * scale - scale;
730 }
731 
732 extern "C" ALWAYS_INLINE int64_t decimal_ceil(const int64_t x, const int64_t scale) {
733  return decimal_floor(x, scale) + (x % scale ? scale : 0);
734 }
735 
736 // Shared memory aggregators. Should never be called,
737 // real implementations are in cuda_mapd_rt.cu.
738 #define DEF_SHARED_AGG_RET_STUBS(base_agg_func) \
739  extern "C" GPU_RT_STUB uint64_t base_agg_func##_shared(uint64_t* agg, \
740  const int64_t val) { \
741  return 0; \
742  } \
743  \
744  extern "C" GPU_RT_STUB uint64_t base_agg_func##_skip_val_shared( \
745  uint64_t* agg, const int64_t val, const int64_t skip_val) { \
746  return 0; \
747  } \
748  extern "C" GPU_RT_STUB uint32_t base_agg_func##_int32_shared(uint32_t* agg, \
749  const int32_t val) { \
750  return 0; \
751  } \
752  \
753  extern "C" GPU_RT_STUB uint32_t base_agg_func##_int32_skip_val_shared( \
754  uint32_t* agg, const int32_t val, const int32_t skip_val) { \
755  return 0; \
756  } \
757  \
758  extern "C" GPU_RT_STUB uint64_t base_agg_func##_double_shared(uint64_t* agg, \
759  const double val) { \
760  return 0; \
761  } \
762  \
763  extern "C" GPU_RT_STUB uint64_t base_agg_func##_double_skip_val_shared( \
764  uint64_t* agg, const double val, const double skip_val) { \
765  return 0; \
766  } \
767  extern "C" GPU_RT_STUB uint32_t base_agg_func##_float_shared(uint32_t* agg, \
768  const float val) { \
769  return 0; \
770  } \
771  \
772  extern "C" GPU_RT_STUB uint32_t base_agg_func##_float_skip_val_shared( \
773  uint32_t* agg, const float val, const float skip_val) { \
774  return 0; \
775  }
776 
777 #define DEF_SHARED_AGG_STUBS(base_agg_func) \
778  extern "C" GPU_RT_STUB void base_agg_func##_shared(int64_t* agg, const int64_t val) {} \
779  \
780  extern "C" GPU_RT_STUB void base_agg_func##_skip_val_shared( \
781  int64_t* agg, const int64_t val, const int64_t skip_val) {} \
782  extern "C" GPU_RT_STUB void base_agg_func##_int32_shared(int32_t* agg, \
783  const int32_t val) {} \
784  extern "C" GPU_RT_STUB void base_agg_func##_int16_shared(int16_t* agg, \
785  const int16_t val) {} \
786  extern "C" GPU_RT_STUB void base_agg_func##_int8_shared(int8_t* agg, \
787  const int8_t val) {} \
788  \
789  extern "C" GPU_RT_STUB void base_agg_func##_int32_skip_val_shared( \
790  int32_t* agg, const int32_t val, const int32_t skip_val) {} \
791  \
792  extern "C" GPU_RT_STUB void base_agg_func##_double_shared(int64_t* agg, \
793  const double val) {} \
794  \
795  extern "C" GPU_RT_STUB void base_agg_func##_double_skip_val_shared( \
796  int64_t* agg, const double val, const double skip_val) {} \
797  extern "C" GPU_RT_STUB void base_agg_func##_float_shared(int32_t* agg, \
798  const float val) {} \
799  \
800  extern "C" GPU_RT_STUB void base_agg_func##_float_skip_val_shared( \
801  int32_t* agg, const float val, const float skip_val) {}
802 
807 
808 extern "C" GPU_RT_STUB int32_t checked_single_agg_id_shared(int64_t* agg,
809  const int64_t val,
810  const int64_t null_val) {
811  return 0;
812 }
813 
814 extern "C" GPU_RT_STUB int32_t
816  const int32_t val,
817  const int32_t null_val) {
818  return 0;
819 }
820 extern "C" GPU_RT_STUB int32_t
822  const int16_t val,
823  const int16_t null_val) {
824  return 0;
825 }
826 extern "C" GPU_RT_STUB int32_t checked_single_agg_id_int8_shared(int8_t* agg,
827  const int8_t val,
828  const int8_t null_val) {
829  return 0;
830 }
831 
832 extern "C" GPU_RT_STUB int32_t
834  const double val,
835  const double null_val) {
836  return 0;
837 }
838 
839 extern "C" GPU_RT_STUB int32_t checked_single_agg_id_float_shared(int32_t* agg,
840  const float val,
841  const float null_val) {
842  return 0;
843 }
844 
845 extern "C" GPU_RT_STUB void agg_max_int16_skip_val_shared(int16_t* agg,
846  const int16_t val,
847  const int16_t skip_val) {}
848 
849 extern "C" GPU_RT_STUB void agg_max_int8_skip_val_shared(int8_t* agg,
850  const int8_t val,
851  const int8_t skip_val) {}
852 
853 extern "C" GPU_RT_STUB void agg_min_int16_skip_val_shared(int16_t* agg,
854  const int16_t val,
855  const int16_t skip_val) {}
856 
857 extern "C" GPU_RT_STUB void agg_min_int8_skip_val_shared(int8_t* agg,
858  const int8_t val,
859  const int8_t skip_val) {}
860 
861 extern "C" GPU_RT_STUB void agg_id_double_shared_slow(int64_t* agg, const double* val) {}
862 
863 extern "C" GPU_RT_STUB int64_t agg_sum_shared(int64_t* agg, const int64_t val) {
864  return 0;
865 }
866 
867 extern "C" GPU_RT_STUB int64_t agg_sum_skip_val_shared(int64_t* agg,
868  const int64_t val,
869  const int64_t skip_val) {
870  return 0;
871 }
872 extern "C" GPU_RT_STUB int32_t agg_sum_int32_shared(int32_t* agg, const int32_t val) {
873  return 0;
874 }
875 
876 extern "C" GPU_RT_STUB int32_t agg_sum_int32_skip_val_shared(int32_t* agg,
877  const int32_t val,
878  const int32_t skip_val) {
879  return 0;
880 }
881 
882 extern "C" GPU_RT_STUB void agg_sum_double_shared(int64_t* agg, const double val) {}
883 
884 extern "C" GPU_RT_STUB void agg_sum_double_skip_val_shared(int64_t* agg,
885  const double val,
886  const double skip_val) {}
887 extern "C" GPU_RT_STUB void agg_sum_float_shared(int32_t* agg, const float val) {}
888 
889 extern "C" GPU_RT_STUB void agg_sum_float_skip_val_shared(int32_t* agg,
890  const float val,
891  const float skip_val) {}
892 
893 extern "C" GPU_RT_STUB void force_sync() {}
894 
895 extern "C" GPU_RT_STUB void sync_warp() {}
896 extern "C" GPU_RT_STUB void sync_warp_protected(int64_t thread_pos, int64_t row_count) {}
897 extern "C" GPU_RT_STUB void sync_threadblock() {}
898 
899 extern "C" GPU_RT_STUB void write_back_non_grouped_agg(int64_t* input_buffer,
900  int64_t* output_buffer,
901  const int32_t num_agg_cols){};
902 // x64 stride functions
903 
904 extern "C" NEVER_INLINE int32_t pos_start_impl(int32_t* error_code) {
905  int32_t row_index_resume{0};
906  if (error_code) {
907  row_index_resume = error_code[0];
908  error_code[0] = 0;
909  }
910  return row_index_resume;
911 }
912 
913 extern "C" NEVER_INLINE int32_t group_buff_idx_impl() {
914  return pos_start_impl(nullptr);
915 }
916 
917 extern "C" NEVER_INLINE int32_t pos_step_impl() {
918  return 1;
919 }
920 
921 extern "C" GPU_RT_STUB int8_t thread_warp_idx(const int8_t warp_sz) {
922  return 0;
923 }
924 
925 extern "C" GPU_RT_STUB int64_t get_thread_index() {
926  return 0;
927 }
928 
930  return nullptr;
931 }
932 
933 extern "C" GPU_RT_STUB int64_t get_block_index() {
934  return 0;
935 }
936 
937 #undef GPU_RT_STUB
938 
939 extern "C" ALWAYS_INLINE void record_error_code(const int32_t err_code,
940  int32_t* error_codes) {
941  // NB: never override persistent error codes (with code greater than zero).
942  // On GPU, a projection query with a limit can run out of slots without it
943  // being an actual error if the limit has been hit. If a persistent error
944  // (division by zero, for example) occurs before running out of slots, we
945  // have to avoid overriding it, because there's a risk that the query would
946  // go through if we override with a potentially benign out-of-slots code.
947  if (err_code && error_codes[pos_start_impl(nullptr)] <= 0) {
948  error_codes[pos_start_impl(nullptr)] = err_code;
949  }
950 }
951 
952 extern "C" ALWAYS_INLINE int32_t get_error_code(int32_t* error_codes) {
953  return error_codes[pos_start_impl(nullptr)];
954 }
955 
956 // group by helpers
957 
958 extern "C" NEVER_INLINE const int64_t* init_shared_mem_nop(
959  const int64_t* groups_buffer,
960  const int32_t groups_buffer_size) {
961  return groups_buffer;
962 }
963 
964 extern "C" NEVER_INLINE void write_back_nop(int64_t* dest,
965  int64_t* src,
966  const int32_t sz) {
967 #ifndef _WIN32
968  // the body is not really needed, just make sure the call is not optimized away
969  assert(dest);
970 #endif
971 }
972 
973 extern "C" int64_t* init_shared_mem(const int64_t* global_groups_buffer,
974  const int32_t groups_buffer_size) {
975  return nullptr;
976 }
977 
979  int64_t* groups_buffer,
980  const int64_t* init_vals,
981  const uint32_t groups_buffer_entry_count,
982  const uint32_t key_qw_count,
983  const uint32_t agg_col_count,
984  const bool keyless,
985  const int8_t warp_size) {
986 #ifndef _WIN32
987  // the body is not really needed, just make sure the call is not optimized away
988  assert(groups_buffer);
989 #endif
990 }
991 
993  int64_t* groups_buffer,
994  const int64_t* init_vals,
995  const uint32_t groups_buffer_entry_count,
996  const uint32_t key_qw_count,
997  const uint32_t agg_col_count,
998  const bool keyless,
999  const bool blocks_share_memory,
1000  const int32_t frag_idx) {
1001 #ifndef _WIN32
1002  // the body is not really needed, just make sure the call is not optimized away
1003  assert(groups_buffer);
1004 #endif
1005 }
1006 
1008  int64_t* groups_buffer,
1009  const int64_t* init_vals,
1010  const uint32_t groups_buffer_entry_count,
1011  const uint32_t key_qw_count,
1012  const uint32_t agg_col_count,
1013  const bool keyless,
1014  const int8_t warp_size) {
1015 #ifndef _WIN32
1016  // the body is not really needed, just make sure the call is not optimized away
1017  assert(groups_buffer);
1018 #endif
1019 }
1020 
1021 template <typename T>
1022 ALWAYS_INLINE int64_t* get_matching_group_value(int64_t* groups_buffer,
1023  const uint32_t h,
1024  const T* key,
1025  const uint32_t key_count,
1026  const uint32_t row_size_quad) {
1027  auto off = h * row_size_quad;
1028  auto row_ptr = reinterpret_cast<T*>(groups_buffer + off);
1029  if (*row_ptr == get_empty_key<T>()) {
1030  memcpy(row_ptr, key, key_count * sizeof(T));
1031  auto row_ptr_i8 = reinterpret_cast<int8_t*>(row_ptr + key_count);
1032  return reinterpret_cast<int64_t*>(align_to_int64(row_ptr_i8));
1033  }
1034  if (memcmp(row_ptr, key, key_count * sizeof(T)) == 0) {
1035  auto row_ptr_i8 = reinterpret_cast<int8_t*>(row_ptr + key_count);
1036  return reinterpret_cast<int64_t*>(align_to_int64(row_ptr_i8));
1037  }
1038  return nullptr;
1039 }
1040 
1041 extern "C" ALWAYS_INLINE int64_t* get_matching_group_value(int64_t* groups_buffer,
1042  const uint32_t h,
1043  const int64_t* key,
1044  const uint32_t key_count,
1045  const uint32_t key_width,
1046  const uint32_t row_size_quad) {
1047  switch (key_width) {
1048  case 4:
1049  return get_matching_group_value(groups_buffer,
1050  h,
1051  reinterpret_cast<const int32_t*>(key),
1052  key_count,
1053  row_size_quad);
1054  case 8:
1055  return get_matching_group_value(groups_buffer, h, key, key_count, row_size_quad);
1056  default:;
1057  }
1058  return nullptr;
1059 }
1060 
1061 template <typename T>
1063  const uint32_t entry_count,
1064  const uint32_t h,
1065  const T* key,
1066  const uint32_t key_count) {
1067  auto off = h;
1068  auto key_buffer = reinterpret_cast<T*>(groups_buffer);
1069  if (key_buffer[off] == get_empty_key<T>()) {
1070  for (size_t i = 0; i < key_count; ++i) {
1071  key_buffer[off] = key[i];
1072  off += entry_count;
1073  }
1074  return h;
1075  }
1076  off = h;
1077  for (size_t i = 0; i < key_count; ++i) {
1078  if (key_buffer[off] != key[i]) {
1079  return -1;
1080  }
1081  off += entry_count;
1082  }
1083  return h;
1084 }
1085 
1086 extern "C" ALWAYS_INLINE int32_t
1088  const uint32_t entry_count,
1089  const uint32_t h,
1090  const int64_t* key,
1091  const uint32_t key_count,
1092  const uint32_t key_width) {
1093  switch (key_width) {
1094  case 4:
1095  return get_matching_group_value_columnar_slot(groups_buffer,
1096  entry_count,
1097  h,
1098  reinterpret_cast<const int32_t*>(key),
1099  key_count);
1100  case 8:
1102  groups_buffer, entry_count, h, key, key_count);
1103  default:
1104  return -1;
1105  }
1106  return -1;
1107 }
1108 
1110  int64_t* groups_buffer,
1111  const uint32_t h,
1112  const int64_t* key,
1113  const uint32_t key_qw_count,
1114  const size_t entry_count) {
1115  auto off = h;
1116  if (groups_buffer[off] == EMPTY_KEY_64) {
1117  for (size_t i = 0; i < key_qw_count; ++i) {
1118  groups_buffer[off] = key[i];
1119  off += entry_count;
1120  }
1121  return &groups_buffer[off];
1122  }
1123  off = h;
1124  for (size_t i = 0; i < key_qw_count; ++i) {
1125  if (groups_buffer[off] != key[i]) {
1126  return nullptr;
1127  }
1128  off += entry_count;
1129  }
1130  return &groups_buffer[off];
1131 }
1132 
1133 /*
1134  * For a particular hashed_index, returns the row-wise offset
1135  * to the first matching agg column in memory.
1136  * It also checks the corresponding group column, and initialize all
1137  * available keys if they are not empty (it is assumed all group columns are
1138  * 64-bit wide).
1139  *
1140  * Memory layout:
1141  *
1142  * | prepended group columns (64-bit each) | agg columns |
1143  */
1145  int64_t* groups_buffer,
1146  const uint32_t hashed_index,
1147  const int64_t* key,
1148  const uint32_t key_count,
1149  const uint32_t row_size_quad) {
1150  uint32_t off = hashed_index * row_size_quad;
1151  if (groups_buffer[off] == EMPTY_KEY_64) {
1152  for (uint32_t i = 0; i < key_count; ++i) {
1153  groups_buffer[off + i] = key[i];
1154  }
1155  }
1156  return groups_buffer + off + key_count;
1157 }
1158 
1166  int64_t* groups_buffer,
1167  const uint32_t hashed_index,
1168  const uint32_t row_size_quad) {
1169  return groups_buffer + row_size_quad * hashed_index;
1170 }
1171 
1172 /*
1173  * For a particular hashed_index, find and initialize (if necessary) all the group
1174  * columns corresponding to a key. It is assumed that all group columns are 64-bit wide.
1175  */
1177  int64_t* groups_buffer,
1178  const uint32_t hashed_index,
1179  const int64_t* key,
1180  const uint32_t key_count,
1181  const uint32_t entry_count) {
1182  if (groups_buffer[hashed_index] == EMPTY_KEY_64) {
1183  for (uint32_t i = 0; i < key_count; i++) {
1184  groups_buffer[i * entry_count + hashed_index] = key[i];
1185  }
1186  }
1187 }
1188 
1189 #include "GroupByRuntime.cpp"
1191 
1193  int64_t* groups_buffer,
1194  const int64_t key,
1195  const int64_t min_key,
1196  const int64_t /* bucket */,
1197  const uint32_t row_size_quad) {
1198  return groups_buffer + row_size_quad * (key - min_key);
1199 }
1200 
1202  int64_t* groups_buffer,
1203  const int64_t key,
1204  const int64_t min_key,
1205  const int64_t /* bucket */,
1206  const uint32_t row_size_quad,
1207  const uint8_t thread_warp_idx,
1208  const uint8_t warp_size) {
1209  return groups_buffer + row_size_quad * (warp_size * (key - min_key) + thread_warp_idx);
1210 }
1211 
1212 extern "C" ALWAYS_INLINE int8_t* extract_str_ptr(const uint64_t str_and_len) {
1213  return reinterpret_cast<int8_t*>(str_and_len & 0xffffffffffff);
1214 }
1215 
1216 extern "C" ALWAYS_INLINE int32_t extract_str_len(const uint64_t str_and_len) {
1217  return static_cast<int64_t>(str_and_len) >> 48;
1218 }
1219 
1220 extern "C" NEVER_INLINE int8_t* extract_str_ptr_noinline(const uint64_t str_and_len) {
1221  return extract_str_ptr(str_and_len);
1222 }
1223 
1224 extern "C" NEVER_INLINE int32_t extract_str_len_noinline(const uint64_t str_and_len) {
1225  return extract_str_len(str_and_len);
1226 }
1227 
1228 extern "C" ALWAYS_INLINE uint64_t string_pack(const int8_t* ptr, const int32_t len) {
1229  return (reinterpret_cast<const uint64_t>(ptr) & 0xffffffffffff) |
1230  (static_cast<const uint64_t>(len) << 48);
1231 }
1232 
1233 #ifdef __clang__
1234 #include "../Utils/StringLike.cpp"
1235 #endif
1236 
1237 #ifndef __CUDACC__
1238 #include "TopKRuntime.cpp"
1239 #endif
1240 
1241 extern "C" ALWAYS_INLINE DEVICE int32_t char_length(const char* str,
1242  const int32_t str_len) {
1243  return str_len;
1244 }
1245 
1246 extern "C" ALWAYS_INLINE DEVICE int32_t char_length_nullable(const char* str,
1247  const int32_t str_len,
1248  const int32_t int_null) {
1249  if (!str) {
1250  return int_null;
1251  }
1252  return str_len;
1253 }
1254 
1255 extern "C" ALWAYS_INLINE DEVICE int32_t key_for_string_encoded(const int32_t str_id) {
1256  return str_id;
1257 }
1258 
1259 extern "C" ALWAYS_INLINE DEVICE bool sample_ratio(const double proportion,
1260  const int64_t row_offset) {
1261  const int64_t threshold = 4294967296 * proportion;
1262  return (row_offset * 2654435761) % 4294967296 < threshold;
1263 }
1264 
1265 extern "C" ALWAYS_INLINE int64_t row_number_window_func(const int64_t output_buff,
1266  const int64_t pos) {
1267  return reinterpret_cast<const int64_t*>(output_buff)[pos];
1268 }
1269 
1270 extern "C" ALWAYS_INLINE double percent_window_func(const int64_t output_buff,
1271  const int64_t pos) {
1272  return reinterpret_cast<const double*>(output_buff)[pos];
1273 }
1274 
1275 extern "C" ALWAYS_INLINE double load_double(const int64_t* agg) {
1276  return *reinterpret_cast<const double*>(may_alias_ptr(agg));
1277 }
1278 
1279 extern "C" ALWAYS_INLINE float load_float(const int32_t* agg) {
1280  return *reinterpret_cast<const float*>(may_alias_ptr(agg));
1281 }
1282 
1283 extern "C" ALWAYS_INLINE double load_avg_int(const int64_t* sum,
1284  const int64_t* count,
1285  const double null_val) {
1286  return *count != 0 ? static_cast<double>(*sum) / *count : null_val;
1287 }
1288 
1289 extern "C" ALWAYS_INLINE double load_avg_decimal(const int64_t* sum,
1290  const int64_t* count,
1291  const double null_val,
1292  const uint32_t scale) {
1293  return *count != 0 ? (static_cast<double>(*sum) / pow(10, scale)) / *count : null_val;
1294 }
1295 
1296 extern "C" ALWAYS_INLINE double load_avg_double(const int64_t* agg,
1297  const int64_t* count,
1298  const double null_val) {
1299  return *count != 0 ? *reinterpret_cast<const double*>(may_alias_ptr(agg)) / *count
1300  : null_val;
1301 }
1302 
1303 extern "C" ALWAYS_INLINE double load_avg_float(const int32_t* agg,
1304  const int32_t* count,
1305  const double null_val) {
1306  return *count != 0 ? *reinterpret_cast<const float*>(may_alias_ptr(agg)) / *count
1307  : null_val;
1308 }
1309 
1310 extern "C" NEVER_INLINE void linear_probabilistic_count(uint8_t* bitmap,
1311  const uint32_t bitmap_bytes,
1312  const uint8_t* key_bytes,
1313  const uint32_t key_len) {
1314  const uint32_t bit_pos = MurmurHash1(key_bytes, key_len, 0) % (bitmap_bytes * 8);
1315  const uint32_t word_idx = bit_pos / 32;
1316  const uint32_t bit_idx = bit_pos % 32;
1317  reinterpret_cast<uint32_t*>(bitmap)[word_idx] |= 1 << bit_idx;
1318 }
1319 
1320 extern "C" NEVER_INLINE void query_stub_hoisted_literals(const int8_t** col_buffers,
1321  const int8_t* literals,
1322  const int64_t* num_rows,
1323  const uint64_t* frag_row_offsets,
1324  const int32_t* max_matched,
1325  const int64_t* init_agg_value,
1326  int64_t** out,
1327  uint32_t frag_idx,
1328  const int64_t* join_hash_tables,
1329  int32_t* error_code,
1330  int32_t* total_matched) {
1331 #ifndef _WIN32
1332  assert(col_buffers || literals || num_rows || frag_row_offsets || max_matched ||
1333  init_agg_value || out || frag_idx || error_code || join_hash_tables ||
1334  total_matched);
1335 #endif
1336 }
1337 
1338 extern "C" void multifrag_query_hoisted_literals(const int8_t*** col_buffers,
1339  const uint64_t* num_fragments,
1340  const int8_t* literals,
1341  const int64_t* num_rows,
1342  const uint64_t* frag_row_offsets,
1343  const int32_t* max_matched,
1344  int32_t* total_matched,
1345  const int64_t* init_agg_value,
1346  int64_t** out,
1347  int32_t* error_code,
1348  const uint32_t* num_tables_ptr,
1349  const int64_t* join_hash_tables) {
1350  for (uint32_t i = 0; i < *num_fragments; ++i) {
1351  query_stub_hoisted_literals(col_buffers ? col_buffers[i] : nullptr,
1352  literals,
1353  &num_rows[i * (*num_tables_ptr)],
1354  &frag_row_offsets[i * (*num_tables_ptr)],
1355  max_matched,
1356  init_agg_value,
1357  out,
1358  i,
1359  join_hash_tables,
1360  total_matched,
1361  error_code);
1362  }
1363 }
1364 
1365 extern "C" NEVER_INLINE void query_stub(const int8_t** col_buffers,
1366  const int64_t* num_rows,
1367  const uint64_t* frag_row_offsets,
1368  const int32_t* max_matched,
1369  const int64_t* init_agg_value,
1370  int64_t** out,
1371  uint32_t frag_idx,
1372  const int64_t* join_hash_tables,
1373  int32_t* error_code,
1374  int32_t* total_matched) {
1375 #ifndef _WIN32
1376  assert(col_buffers || num_rows || frag_row_offsets || max_matched || init_agg_value ||
1377  out || frag_idx || error_code || join_hash_tables || total_matched);
1378 #endif
1379 }
1380 
1381 extern "C" void multifrag_query(const int8_t*** col_buffers,
1382  const uint64_t* num_fragments,
1383  const int64_t* num_rows,
1384  const uint64_t* frag_row_offsets,
1385  const int32_t* max_matched,
1386  int32_t* total_matched,
1387  const int64_t* init_agg_value,
1388  int64_t** out,
1389  int32_t* error_code,
1390  const uint32_t* num_tables_ptr,
1391  const int64_t* join_hash_tables) {
1392  for (uint32_t i = 0; i < *num_fragments; ++i) {
1393  query_stub(col_buffers ? col_buffers[i] : nullptr,
1394  &num_rows[i * (*num_tables_ptr)],
1395  &frag_row_offsets[i * (*num_tables_ptr)],
1396  max_matched,
1397  init_agg_value,
1398  out,
1399  i,
1400  join_hash_tables,
1401  total_matched,
1402  error_code);
1403  }
1404 }
1405 
1407  if (check_interrupt_init(static_cast<unsigned>(INT_CHECK))) {
1408  return true;
1409  }
1410  return false;
1411 }
1412 
1413 extern "C" bool check_interrupt_init(unsigned command) {
1414  static std::atomic_bool runtime_interrupt_flag{false};
1415 
1416  if (command == static_cast<unsigned>(INT_CHECK)) {
1417  if (runtime_interrupt_flag.load()) {
1418  return true;
1419  }
1420  return false;
1421  }
1422  if (command == static_cast<unsigned>(INT_ABORT)) {
1423  runtime_interrupt_flag.store(true);
1424  return false;
1425  }
1426  if (command == static_cast<unsigned>(INT_RESET)) {
1427  runtime_interrupt_flag.store(false);
1428  return false;
1429  }
1430  return false;
1431 }
__device__ void sync_warp_protected(int64_t thread_pos, int64_t row_count)
ALWAYS_INLINE void agg_sum_float(int32_t *agg, const float val)
ALWAYS_INLINE int64_t agg_sum_skip_val(int64_t *agg, const int64_t val, const int64_t skip_val)
#define DEF_UMINUS_NULLABLE(type, null_type)
ALWAYS_INLINE void record_error_code(const int32_t err_code, int32_t *error_codes)
GPU_RT_STUB int32_t checked_single_agg_id_int32_shared(int32_t *agg, const int32_t val, const int32_t null_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)
#define DEF_CHECKED_SINGLE_AGG_ID_INT(n)
__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)
#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
#define GPU_RT_STUB
__device__ void agg_sum_float_skip_val_shared(int32_t *agg, const float val, const float skip_val)
void agg_min_int32(int32_t *agg, const int32_t val)
ALWAYS_INLINE int64_t row_number_window_func(const int64_t output_buff, const int64_t pos)
#define DEF_CAST_NULLABLE_BIDIR(type1, type2)
ALWAYS_INLINE double load_avg_float(const int32_t *agg, const int32_t *count, const double null_val)
ALWAYS_INLINE void agg_max_float(int32_t *agg, const float val)
ALWAYS_INLINE int64_t * get_group_value_fast_keyless_semiprivate(int64_t *groups_buffer, const int64_t key, const int64_t min_key, const int64_t, const uint32_t row_size_quad, const uint8_t thread_warp_idx, const uint8_t warp_size)
ALWAYS_INLINE int32_t agg_sum_int32_skip_val(int32_t *agg, const int32_t val, const int32_t 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)
ALWAYS_INLINE int64_t scale_decimal_down_not_nullable(const int64_t operand, const int64_t scale, const int64_t null_val)
__device__ int8_t thread_warp_idx(const int8_t warp_sz)
Definition: cuda_mapd_rt.cu:40
__global__ void init_group_by_buffer_gpu(int64_t *groups_buffer, const int64_t *init_vals, const uint32_t groups_buffer_entry_count, const uint32_t key_count, const uint32_t key_width, const uint32_t row_size_quad, const bool keyless, const int8_t warp_size)
ALWAYS_INLINE double load_avg_double(const int64_t *agg, const int64_t *count, const double null_val)
#define DEF_CAST_NULLABLE(from_type, to_type)
ALWAYS_INLINE int32_t checked_single_agg_id_double(int64_t *agg, const double val, const double null_val)
__device__ int64_t get_thread_index()
Definition: cuda_mapd_rt.cu:20
RUNTIME_EXPORT NEVER_INLINE DEVICE uint64_t MurmurHash64A(const void *key, int len, uint64_t seed)
Definition: MurmurHash.cpp:26
#define const
__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)
ALWAYS_INLINE double load_double(const int64_t *agg)
__device__ int32_t checked_single_agg_id_double_shared(int64_t *agg, const double val, const double null_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
tuple r
Definition: test_fsi.py:16
#define DEF_ARITH_NULLABLE_RHS(type, null_type, opname, opsym)
ALWAYS_INLINE void agg_count_distinct_bitmap_skip_val(int64_t *agg, const int64_t val, const int64_t min_val, const int64_t skip_val)
ALWAYS_INLINE int64_t scale_decimal_down_nullable(const int64_t operand, const int64_t scale, const int64_t null_val)
#define DEF_AGG_MAX_INT(n)
void multifrag_query(const int8_t ***col_buffers, const uint64_t *num_fragments, const int64_t *num_rows, const uint64_t *frag_row_offsets, const int32_t *max_matched, int32_t *total_matched, const int64_t *init_agg_value, int64_t **out, int32_t *error_code, const uint32_t *num_tables_ptr, const int64_t *join_hash_tables)
ALWAYS_INLINE int32_t extract_str_len(const uint64_t str_and_len)
__device__ int32_t checked_single_agg_id_float_shared(int32_t *agg, const float val, const float null_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)
ALWAYS_INLINE int32_t checked_single_agg_id(int64_t *agg, const int64_t val, const int64_t null_val)
ALWAYS_INLINE void set_matching_group_value_perfect_hash_columnar(int64_t *groups_buffer, const uint32_t hashed_index, const int64_t *key, const uint32_t key_count, const uint32_t entry_count)
void agg_max_int16(int16_t *agg, const int16_t val)
ALWAYS_INLINE int64_t floor_div_nullable_lhs(const int64_t dividend, const int64_t divisor, const int64_t null_val)
void agg_min_int8(int8_t *agg, const int8_t val)
__device__ int64_t agg_sum_shared(int64_t *agg, const int64_t val)
ALWAYS_INLINE int64_t * get_group_value_fast_keyless(int64_t *groups_buffer, const int64_t key, const int64_t min_key, const int64_t, const uint32_t row_size_quad)
__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)
ALWAYS_INLINE void agg_id_double(int64_t *agg, const double val)
ALWAYS_INLINE uint64_t string_pack(const int8_t *ptr, const int32_t len)
__device__ int64_t * declare_dynamic_shared_memory()
Definition: cuda_mapd_rt.cu:57
__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)
ALWAYS_INLINE void agg_sum_double(int64_t *agg, const double val)
NEVER_INLINE int8_t * extract_str_ptr_noinline(const uint64_t str_and_len)
#define DEVICE
ALWAYS_INLINE int8_t * extract_str_ptr(const uint64_t str_and_len)
#define DEF_SKIP_AGG(base_agg_func)
__device__ int64_t get_block_index()
Definition: cuda_mapd_rt.cu:24
__device__ bool check_interrupt()
#define DEF_WRITE_PROJECTION_INT(n)
ALWAYS_INLINE void agg_id_float(int32_t *agg, const float val)
ALWAYS_INLINE uint32_t agg_count_float_skip_val(uint32_t *agg, const float val, const float skip_val)
GPU_RT_STUB int32_t checked_single_agg_id_int8_shared(int8_t *agg, const int8_t val, const int8_t null_val)
ALWAYS_INLINE uint32_t agg_count_int32_skip_val(uint32_t *agg, const int32_t val, const int32_t skip_val)
__device__ int32_t agg_sum_int32_skip_val_shared(int32_t *agg, const int32_t val, const int32_t skip_val)
ALWAYS_INLINE void agg_min_double(int64_t *agg, const double val)
ALWAYS_INLINE int32_t agg_sum_int32(int32_t *agg, const int32_t val)
__device__ void linear_probabilistic_count(uint8_t *bitmap, const uint32_t bitmap_bytes, const uint8_t *key_bytes, const uint32_t key_len)
ALWAYS_INLINE DEVICE int32_t char_length(const char *str, const int32_t str_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)
#define DEF_SHARED_AGG_RET_STUBS(base_agg_func)
ALWAYS_INLINE int64_t * get_matching_group_value_perfect_hash_keyless(int64_t *groups_buffer, const uint32_t hashed_index, const uint32_t row_size_quad)
__device__ void agg_sum_double_shared(int64_t *agg, const double val)
int count
void agg_min_int16(int16_t *agg, const int16_t val)
#define DEF_ARITH_NULLABLE_LHS(type, null_type, opname, opsym)
ALWAYS_INLINE int64_t floor_div_lhs(const int64_t dividend, const int64_t divisor)
#define DEF_AGG_MIN_INT(n)
ALWAYS_INLINE void agg_max_double(int64_t *agg, const double val)
__device__ int32_t pos_start_impl(const int32_t *row_index_resume)
Definition: cuda_mapd_rt.cu:28
ALWAYS_INLINE uint64_t agg_count_double(uint64_t *agg, const double val)
void multifrag_query_hoisted_literals(const int8_t ***col_buffers, const uint64_t *num_fragments, const int8_t *literals, const int64_t *num_rows, const uint64_t *frag_row_offsets, const int32_t *max_matched, int32_t *total_matched, const int64_t *init_agg_value, int64_t **out, int32_t *error_code, const uint32_t *num_tables_ptr, const int64_t *join_hash_tables)
__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()
ALWAYS_INLINE double load_avg_decimal(const int64_t *sum, const int64_t *count, const double null_val, const uint32_t scale)
NEVER_INLINE int32_t extract_str_len_noinline(const uint64_t str_and_len)
ALWAYS_INLINE int32_t checked_single_agg_id_float(int32_t *agg, const float val, const float null_val)
NEVER_INLINE void query_stub(const int8_t **col_buffers, const int64_t *num_rows, const uint64_t *frag_row_offsets, const int32_t *max_matched, const int64_t *init_agg_value, int64_t **out, uint32_t frag_idx, const int64_t *join_hash_tables, int32_t *error_code, int32_t *total_matched)
ALWAYS_INLINE int64_t scale_decimal_up(const int64_t operand, const uint64_t scale, const int64_t operand_null_val, const int64_t result_null_val)
ALWAYS_INLINE int64_t agg_sum(int64_t *agg, const int64_t 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)
ALWAYS_INLINE void agg_min(int64_t *agg, const int64_t val)
RUNTIME_EXPORT NEVER_INLINE DEVICE uint32_t MurmurHash1(const void *key, int len, const uint32_t seed)
Definition: MurmurHash.cpp:20
ALWAYS_INLINE int64_t decimal_floor(const int64_t x, const int64_t scale)
__device__ void agg_max_int16_skip_val_shared(int16_t *agg, const int16_t val, const int16_t skip_val)
ALWAYS_INLINE int64_t * get_matching_group_value_perfect_hash(int64_t *groups_buffer, const uint32_t hashed_index, const int64_t *key, const uint32_t key_count, const uint32_t row_size_quad)
ALWAYS_INLINE DEVICE int32_t key_for_string_encoded(const int32_t str_id)
NEVER_INLINE void init_group_by_buffer_impl(int64_t *groups_buffer, const int64_t *init_vals, const uint32_t groups_buffer_entry_count, const uint32_t key_qw_count, const uint32_t agg_col_count, const bool keyless, const int8_t warp_size)
__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
void agg_max_int32(int32_t *agg, const int32_t val)
ALWAYS_INLINE int8_t logical_or(const int8_t lhs, const int8_t rhs, const int8_t null_val)
GPU_RT_STUB int32_t checked_single_agg_id_int16_shared(int16_t *agg, const int16_t val, const int16_t null_val)
#define DEF_BINARY_NULLABLE_ALL_OPS(type, null_type)
ALWAYS_INLINE int32_t get_error_code(int32_t *error_codes)
bool check_interrupt_init(unsigned command)
#define NEVER_INLINE
ALWAYS_INLINE void agg_max(int64_t *agg, const int64_t val)
#define DEF_ARITH_NULLABLE(type, null_type, opname, opsym)
ALWAYS_INLINE float load_float(const int32_t *agg)
NEVER_INLINE void query_stub_hoisted_literals(const int8_t **col_buffers, const int8_t *literals, const int64_t *num_rows, const uint64_t *frag_row_offsets, const int32_t *max_matched, const int64_t *init_agg_value, int64_t **out, uint32_t frag_idx, const int64_t *join_hash_tables, int32_t *error_code, int32_t *total_matched)
__device__ void agg_min_int16_skip_val_shared(int16_t *agg, const int16_t val, const int16_t skip_val)
__device__ void sync_threadblock()
ALWAYS_INLINE uint64_t agg_count_skip_val(uint64_t *agg, const int64_t val, const int64_t skip_val)
__device__ void agg_min_int8_skip_val_shared(int8_t *agg, const int8_t val, const int8_t skip_val)
ALWAYS_INLINE double load_avg_int(const int64_t *sum, const int64_t *count, const double null_val)
ALWAYS_INLINE DEVICE int32_t char_length_nullable(const char *str, const int32_t str_len, const int32_t int_null)
ALWAYS_INLINE DEVICE bool sample_ratio(const double proportion, const int64_t row_offset)
void agg_max_int8(int8_t *agg, const int8_t val)
#define DEF_SHARED_AGG_STUBS(base_agg_func)
ALWAYS_INLINE int8_t bit_is_set(const int64_t bitset, const int64_t val, const int64_t min_val, const int64_t max_val, const int64_t null_val, const int8_t null_bool_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__ int32_t checked_single_agg_id_shared(int64_t *agg, const int64_t val, const int64_t null_val)
#define ALWAYS_INLINE
#define DEF_AGG_ID_INT(n)
NEVER_INLINE void agg_approximate_count_distinct(int64_t *agg, const int64_t key, const uint32_t b)
ALWAYS_INLINE uint64_t agg_count_double_skip_val(uint64_t *agg, const double val, const double skip_val)
ALWAYS_INLINE void agg_id(int64_t *agg, const int64_t val)
ALWAYS_INLINE int64_t decimal_ceil(const int64_t x, const int64_t scale)
ALWAYS_INLINE void agg_count_distinct_bitmap(int64_t *agg, const int64_t val, const int64_t min_val)
FORCE_INLINE HOST DEVICE T align_to_int64(T addr)
ALWAYS_INLINE int8_t logical_not(const int8_t operand, const int8_t null_val)
ALWAYS_INLINE int8_t logical_and(const int8_t lhs, const int8_t rhs, const int8_t null_val)
ALWAYS_INLINE void agg_min_float(int32_t *agg, const float val)
__device__ void force_sync()
ALWAYS_INLINE double percent_window_func(const int64_t output_buff, const int64_t pos)
NEVER_INLINE void init_columnar_group_by_buffer_gpu(int64_t *groups_buffer, const int64_t *init_vals, const uint32_t groups_buffer_entry_count, const uint32_t key_qw_count, const uint32_t agg_col_count, const bool keyless, const bool blocks_share_memory, const int32_t frag_idx)
__device__ int32_t group_buff_idx_impl()
Definition: cuda_mapd_rt.cu:32