OmniSciDB  21ac014ffc
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
HashJoinRuntime.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "HashJoinRuntime.h"
18 
24 #include "Shared/shard_key.h"
25 #ifdef __CUDACC__
29 #else
30 #include "Logger/Logger.h"
31 
33 #include "Shared/likely.h"
36 
37 #include <future>
38 #endif
39 
40 #if HAVE_CUDA
41 #include <thrust/scan.h>
42 #endif
43 #include "Shared/funcannotations.h"
44 
45 #include <cmath>
46 #include <numeric>
47 
48 #ifndef __CUDACC__
49 namespace {
50 
71 inline int64_t translate_str_id_to_outer_dict(const int64_t elem,
72  const int64_t min_elem,
73  const int64_t max_elem,
74  const void* sd_inner_proxy,
75  const void* sd_outer_proxy) {
76  CHECK(sd_outer_proxy);
77  const auto sd_inner_dict_proxy =
78  static_cast<const StringDictionaryProxy*>(sd_inner_proxy);
79  const auto sd_outer_dict_proxy =
80  static_cast<const StringDictionaryProxy*>(sd_outer_proxy);
81  const auto elem_str = sd_inner_dict_proxy->getString(elem);
82  const auto outer_id = sd_outer_dict_proxy->getIdOfString(elem_str);
83  if (outer_id > max_elem || outer_id < min_elem) {
85  }
86  return outer_id;
87 }
88 
89 } // namespace
90 #endif
91 
92 DEVICE void SUFFIX(init_hash_join_buff)(int32_t* groups_buffer,
93  const int64_t hash_entry_count,
94  const int32_t invalid_slot_val,
95  const int32_t cpu_thread_idx,
96  const int32_t cpu_thread_count) {
97 #ifdef __CUDACC__
98  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
99  int32_t step = blockDim.x * gridDim.x;
100 #else
101  int32_t start = cpu_thread_idx;
102  int32_t step = cpu_thread_count;
103 #endif
104  for (int64_t i = start; i < hash_entry_count; i += step) {
105  groups_buffer[i] = invalid_slot_val;
106  }
107 }
108 
109 #ifdef __CUDACC__
110 #define mapd_cas(address, compare, val) atomicCAS(address, compare, val)
111 #elif defined(_MSC_VER)
112 #define mapd_cas(address, compare, val) \
113  InterlockedCompareExchange(reinterpret_cast<volatile long*>(address), \
114  static_cast<long>(val), \
115  static_cast<long>(compare))
116 #else
117 #define mapd_cas(address, compare, val) __sync_val_compare_and_swap(address, compare, val)
118 #endif
119 
120 template <typename HASHTABLE_FILLING_FUNC>
121 DEVICE auto fill_hash_join_buff_impl(int32_t* buff,
122  const int32_t invalid_slot_val,
123  const JoinColumn join_column,
124  const JoinColumnTypeInfo type_info,
125  const void* sd_inner_proxy,
126  const void* sd_outer_proxy,
127  const int32_t cpu_thread_idx,
128  const int32_t cpu_thread_count,
129  HASHTABLE_FILLING_FUNC filling_func) {
130 #ifdef __CUDACC__
131  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
132  int32_t step = blockDim.x * gridDim.x;
133 #else
134  int32_t start = cpu_thread_idx;
135  int32_t step = cpu_thread_count;
136 #endif
137  JoinColumnTyped col{&join_column, &type_info};
138  for (auto item : col.slice(start, step)) {
139  const size_t index = item.index;
140  int64_t elem = item.element;
141  if (elem == type_info.null_val) {
142  if (type_info.uses_bw_eq) {
143  elem = type_info.translated_null_val;
144  } else {
145  continue;
146  }
147  }
148 #ifndef __CUDACC__
149  if (sd_inner_proxy &&
150  (!type_info.uses_bw_eq || elem != type_info.translated_null_val)) {
151  const auto outer_id = translate_str_id_to_outer_dict(
152  elem, type_info.min_val, type_info.max_val, sd_inner_proxy, sd_outer_proxy);
153  if (outer_id == StringDictionary::INVALID_STR_ID) {
154  continue;
155  }
156  elem = outer_id;
157  }
158  CHECK_GE(elem, type_info.min_val)
159  << "Element " << elem << " less than min val " << type_info.min_val;
160 #endif
161  if (filling_func(elem, index)) {
162  return -1;
163  }
164  }
165  return 0;
166 };
167 
169  const int32_t invalid_slot_val,
170  const bool for_semi_join,
171  const JoinColumn join_column,
172  const JoinColumnTypeInfo type_info,
173  const void* sd_inner_proxy,
174  const void* sd_outer_proxy,
175  const int32_t cpu_thread_idx,
176  const int32_t cpu_thread_count,
177  const int64_t bucket_normalization) {
178  auto filling_func = for_semi_join ? SUFFIX(fill_hashtable_for_semi_join)
180  auto hashtable_filling_func = [&](auto elem, size_t index) {
181  auto entry_ptr = SUFFIX(get_bucketized_hash_slot)(
182  buff, elem, type_info.min_val, bucket_normalization);
183  return filling_func(index, entry_ptr, invalid_slot_val);
184  };
185 
186  return fill_hash_join_buff_impl(buff,
187  invalid_slot_val,
188  join_column,
189  type_info,
190  sd_inner_proxy,
191  sd_outer_proxy,
192  cpu_thread_idx,
193  cpu_thread_count,
194  hashtable_filling_func);
195 }
196 
197 DEVICE int SUFFIX(fill_hash_join_buff)(int32_t* buff,
198  const int32_t invalid_slot_val,
199  const bool for_semi_join,
200  const JoinColumn join_column,
201  const JoinColumnTypeInfo type_info,
202  const void* sd_inner_proxy,
203  const void* sd_outer_proxy,
204  const int32_t cpu_thread_idx,
205  const int32_t cpu_thread_count) {
206  auto filling_func = for_semi_join ? SUFFIX(fill_hashtable_for_semi_join)
208  auto hashtable_filling_func = [&](auto elem, size_t index) {
209  auto entry_ptr = SUFFIX(get_hash_slot)(buff, elem, type_info.min_val);
210  return filling_func(index, entry_ptr, invalid_slot_val);
211  };
212 
213  return fill_hash_join_buff_impl(buff,
214  invalid_slot_val,
215  join_column,
216  type_info,
217  sd_inner_proxy,
218  sd_outer_proxy,
219  cpu_thread_idx,
220  cpu_thread_count,
221  hashtable_filling_func);
222 }
223 
224 template <typename HASHTABLE_FILLING_FUNC>
226  const int32_t invalid_slot_val,
227  const JoinColumn join_column,
228  const JoinColumnTypeInfo type_info,
229  const ShardInfo shard_info,
230  const void* sd_inner_proxy,
231  const void* sd_outer_proxy,
232  const int32_t cpu_thread_idx,
233  const int32_t cpu_thread_count,
234  HASHTABLE_FILLING_FUNC filling_func) {
235 #ifdef __CUDACC__
236  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
237  int32_t step = blockDim.x * gridDim.x;
238 #else
239  int32_t start = cpu_thread_idx;
240  int32_t step = cpu_thread_count;
241 #endif
242  JoinColumnTyped col{&join_column, &type_info};
243  for (auto item : col.slice(start, step)) {
244  const size_t index = item.index;
245  int64_t elem = item.element;
246  size_t shard = SHARD_FOR_KEY(elem, shard_info.num_shards);
247  if (shard != shard_info.shard) {
248  continue;
249  }
250  if (elem == type_info.null_val) {
251  if (type_info.uses_bw_eq) {
252  elem = type_info.translated_null_val;
253  } else {
254  continue;
255  }
256  }
257 #ifndef __CUDACC__
258  if (sd_inner_proxy &&
259  (!type_info.uses_bw_eq || elem != type_info.translated_null_val)) {
260  const auto outer_id = translate_str_id_to_outer_dict(
261  elem, type_info.min_val, type_info.max_val, sd_inner_proxy, sd_outer_proxy);
262  if (outer_id == StringDictionary::INVALID_STR_ID) {
263  continue;
264  }
265  elem = outer_id;
266  }
267  CHECK_GE(elem, type_info.min_val)
268  << "Element " << elem << " less than min val " << type_info.min_val;
269 #endif
270  if (filling_func(elem, shard, index)) {
271  return -1;
272  }
273  }
274  return 0;
275 }
276 
278  int32_t* buff,
279  const int32_t invalid_slot_val,
280  const bool for_semi_join,
281  const JoinColumn join_column,
282  const JoinColumnTypeInfo type_info,
283  const ShardInfo shard_info,
284  const void* sd_inner_proxy,
285  const void* sd_outer_proxy,
286  const int32_t cpu_thread_idx,
287  const int32_t cpu_thread_count,
288  const int64_t bucket_normalization) {
289  auto filling_func = for_semi_join ? SUFFIX(fill_hashtable_for_semi_join)
291  auto hashtable_filling_func = [&](auto elem, auto shard, size_t index) {
292  auto entry_ptr =
294  elem,
295  type_info.min_val,
296  shard_info.entry_count_per_shard,
297  shard,
298  shard_info.num_shards,
299  shard_info.device_count,
300  bucket_normalization);
301  return filling_func(index, entry_ptr, invalid_slot_val);
302  };
303 
305  invalid_slot_val,
306  join_column,
307  type_info,
308  shard_info,
309  sd_inner_proxy,
310  sd_outer_proxy,
311  cpu_thread_idx,
312  cpu_thread_count,
313  hashtable_filling_func);
314 }
315 
317  const int32_t invalid_slot_val,
318  const bool for_semi_join,
319  const JoinColumn join_column,
320  const JoinColumnTypeInfo type_info,
321  const ShardInfo shard_info,
322  const void* sd_inner_proxy,
323  const void* sd_outer_proxy,
324  const int32_t cpu_thread_idx,
325  const int32_t cpu_thread_count) {
326  auto filling_func = for_semi_join ? SUFFIX(fill_hashtable_for_semi_join)
328  auto hashtable_filling_func = [&](auto elem, auto shard, size_t index) {
329  auto entry_ptr = SUFFIX(get_hash_slot_sharded_opt)(buff,
330  elem,
331  type_info.min_val,
332  shard_info.entry_count_per_shard,
333  shard,
334  shard_info.num_shards,
335  shard_info.device_count);
336  return filling_func(index, entry_ptr, invalid_slot_val);
337  };
338 
340  invalid_slot_val,
341  join_column,
342  type_info,
343  shard_info,
344  sd_inner_proxy,
345  sd_outer_proxy,
346  cpu_thread_idx,
347  cpu_thread_count,
348  hashtable_filling_func);
349 }
350 
351 template <typename T>
353  const int64_t entry_count,
354  const size_t key_component_count,
355  const bool with_val_slot,
356  const int32_t invalid_slot_val,
357  const int32_t cpu_thread_idx,
358  const int32_t cpu_thread_count) {
359 #ifdef __CUDACC__
360  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
361  int32_t step = blockDim.x * gridDim.x;
362 #else
363  int32_t start = cpu_thread_idx;
364  int32_t step = cpu_thread_count;
365 #endif
366  auto hash_entry_size = (key_component_count + (with_val_slot ? 1 : 0)) * sizeof(T);
367  const T empty_key = SUFFIX(get_invalid_key)<T>();
368  for (int64_t h = start; h < entry_count; h += step) {
369  int64_t off = h * hash_entry_size;
370  auto row_ptr = reinterpret_cast<T*>(hash_buff + off);
371  for (size_t i = 0; i < key_component_count; ++i) {
372  row_ptr[i] = empty_key;
373  }
374  if (with_val_slot) {
375  row_ptr[key_component_count] = invalid_slot_val;
376  }
377  }
378 }
379 
380 #ifdef __CUDACC__
381 template <typename T>
382 __device__ T* get_matching_baseline_hash_slot_at(int8_t* hash_buff,
383  const uint32_t h,
384  const T* key,
385  const size_t key_component_count,
386  const int64_t hash_entry_size) {
387  uint32_t off = h * hash_entry_size;
388  auto row_ptr = reinterpret_cast<T*>(hash_buff + off);
389  const T empty_key = SUFFIX(get_invalid_key)<T>();
390  {
391  const T old = atomicCAS(row_ptr, empty_key, *key);
392  if (empty_key == old && key_component_count > 1) {
393  for (int64_t i = 1; i <= key_component_count - 1; ++i) {
394  atomicExch(row_ptr + i, key[i]);
395  }
396  }
397  }
398  if (key_component_count > 1) {
399  while (atomicAdd(row_ptr + key_component_count - 1, 0) == empty_key) {
400  // spin until the winning thread has finished writing the entire key and the init
401  // value
402  }
403  }
404  bool match = true;
405  for (uint32_t i = 0; i < key_component_count; ++i) {
406  if (row_ptr[i] != key[i]) {
407  match = false;
408  break;
409  }
410  }
411 
412  if (match) {
413  return reinterpret_cast<T*>(row_ptr + key_component_count);
414  }
415  return nullptr;
416 }
417 #else
418 
419 #ifdef _MSC_VER
420 #define cas_cst(ptr, expected, desired) \
421  (InterlockedCompareExchangePointer(reinterpret_cast<void* volatile*>(ptr), \
422  reinterpret_cast<void*>(&desired), \
423  expected) == expected)
424 #define store_cst(ptr, val) \
425  InterlockedExchangePointer(reinterpret_cast<void* volatile*>(ptr), \
426  reinterpret_cast<void*>(val))
427 #define load_cst(ptr) \
428  InterlockedCompareExchange(reinterpret_cast<volatile long*>(ptr), 0, 0)
429 #else
430 #define cas_cst(ptr, expected, desired) \
431  __atomic_compare_exchange_n( \
432  ptr, expected, desired, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)
433 #define store_cst(ptr, val) __atomic_store_n(ptr, val, __ATOMIC_SEQ_CST)
434 #define load_cst(ptr) __atomic_load_n(ptr, __ATOMIC_SEQ_CST)
435 #endif
436 
437 template <typename T>
439  const uint32_t h,
440  const T* key,
441  const size_t key_component_count,
442  const int64_t hash_entry_size) {
443  uint32_t off = h * hash_entry_size;
444  auto row_ptr = reinterpret_cast<T*>(hash_buff + off);
445  T empty_key = SUFFIX(get_invalid_key)<T>();
446  T write_pending = SUFFIX(get_invalid_key)<T>() - 1;
447  if (UNLIKELY(*key == write_pending)) {
448  // Address the singularity case where the first column contains the pending
449  // write special value. Should never happen, but avoid doing wrong things.
450  return nullptr;
451  }
452  const bool success = cas_cst(row_ptr, &empty_key, write_pending);
453  if (success) {
454  if (key_component_count > 1) {
455  memcpy(row_ptr + 1, key + 1, (key_component_count - 1) * sizeof(T));
456  }
457  store_cst(row_ptr, *key);
458  return reinterpret_cast<T*>(row_ptr + key_component_count);
459  }
460  while (load_cst(row_ptr) == write_pending) {
461  // spin until the winning thread has finished writing the entire key
462  }
463  for (size_t i = 0; i < key_component_count; ++i) {
464  if (load_cst(row_ptr + i) != key[i]) {
465  return nullptr;
466  }
467  }
468  return reinterpret_cast<T*>(row_ptr + key_component_count);
469 }
470 
471 #undef load_cst
472 #undef store_cst
473 #undef cas_cst
474 
475 #endif // __CUDACC__
476 
477 template <typename T>
478 DEVICE int write_baseline_hash_slot(const int32_t val,
479  int8_t* hash_buff,
480  const int64_t entry_count,
481  const T* key,
482  const size_t key_component_count,
483  const bool with_val_slot,
484  const int32_t invalid_slot_val,
485  const size_t key_size_in_bytes,
486  const size_t hash_entry_size) {
487  const uint32_t h = MurmurHash1Impl(key, key_size_in_bytes, 0) % entry_count;
488  T* matching_group = get_matching_baseline_hash_slot_at(
489  hash_buff, h, key, key_component_count, hash_entry_size);
490  if (!matching_group) {
491  uint32_t h_probe = (h + 1) % entry_count;
492  while (h_probe != h) {
493  matching_group = get_matching_baseline_hash_slot_at(
494  hash_buff, h_probe, key, key_component_count, hash_entry_size);
495  if (matching_group) {
496  break;
497  }
498  h_probe = (h_probe + 1) % entry_count;
499  }
500  }
501  if (!matching_group) {
502  return -2;
503  }
504  if (!with_val_slot) {
505  return 0;
506  }
507  if (mapd_cas(matching_group, invalid_slot_val, val) != invalid_slot_val) {
508  return -1;
509  }
510  return 0;
511 }
512 
513 template <typename T>
515  int8_t* hash_buff,
516  const int64_t entry_count,
517  const T* key,
518  const size_t key_component_count,
519  const bool with_val_slot,
520  const int32_t invalid_slot_val,
521  const size_t key_size_in_bytes,
522  const size_t hash_entry_size) {
523  const uint32_t h = MurmurHash1Impl(key, key_size_in_bytes, 0) % entry_count;
524  T* matching_group = get_matching_baseline_hash_slot_at(
525  hash_buff, h, key, key_component_count, hash_entry_size);
526  if (!matching_group) {
527  uint32_t h_probe = (h + 1) % entry_count;
528  while (h_probe != h) {
529  matching_group = get_matching_baseline_hash_slot_at(
530  hash_buff, h_probe, key, key_component_count, hash_entry_size);
531  if (matching_group) {
532  break;
533  }
534  h_probe = (h_probe + 1) % entry_count;
535  }
536  }
537  if (!matching_group) {
538  return -2;
539  }
540  if (!with_val_slot) {
541  return 0;
542  }
543  mapd_cas(matching_group, invalid_slot_val, val);
544  return 0;
545 }
546 
547 template <typename T, typename FILL_HANDLER>
549  const int64_t entry_count,
550  const int32_t invalid_slot_val,
551  const bool for_semi_join,
552  const size_t key_component_count,
553  const bool with_val_slot,
554  const FILL_HANDLER* f,
555  const int64_t num_elems,
556  const int32_t cpu_thread_idx,
557  const int32_t cpu_thread_count) {
558 #ifdef __CUDACC__
559  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
560  int32_t step = blockDim.x * gridDim.x;
561 #else
562  int32_t start = cpu_thread_idx;
563  int32_t step = cpu_thread_count;
564 #endif
565 
566  T key_scratch_buff[g_maximum_conditions_to_coalesce];
567  const size_t key_size_in_bytes = key_component_count * sizeof(T);
568  const size_t hash_entry_size =
569  (key_component_count + (with_val_slot ? 1 : 0)) * sizeof(T);
570  auto key_buff_handler = [hash_buff,
571  entry_count,
572  with_val_slot,
573  invalid_slot_val,
574  key_size_in_bytes,
575  hash_entry_size,
576  &for_semi_join](const int64_t entry_idx,
577  const T* key_scratch_buffer,
578  const size_t key_component_count) {
579  if (for_semi_join) {
580  return write_baseline_hash_slot_for_semi_join<T>(entry_idx,
581  hash_buff,
582  entry_count,
583  key_scratch_buffer,
584  key_component_count,
585  with_val_slot,
586  invalid_slot_val,
587  key_size_in_bytes,
588  hash_entry_size);
589  } else {
590  return write_baseline_hash_slot<T>(entry_idx,
591  hash_buff,
592  entry_count,
593  key_scratch_buffer,
594  key_component_count,
595  with_val_slot,
596  invalid_slot_val,
597  key_size_in_bytes,
598  hash_entry_size);
599  }
600  };
601 
602  JoinColumnTuple cols(
603  f->get_number_of_columns(), f->get_join_columns(), f->get_join_column_type_infos());
604  for (auto& it : cols.slice(start, step)) {
605  const auto err = (*f)(it.join_column_iterators, key_scratch_buff, key_buff_handler);
606  if (err) {
607  return err;
608  }
609  }
610  return 0;
611 }
612 
613 #undef mapd_cas
614 
615 #ifdef __CUDACC__
616 #define mapd_add(address, val) atomicAdd(address, val)
617 #elif defined(_MSC_VER)
618 #define mapd_add(address, val) \
619  InterlockedExchangeAdd(reinterpret_cast<volatile long*>(address), \
620  static_cast<long>(val))
621 #else
622 #define mapd_add(address, val) __sync_fetch_and_add(address, val)
623 #endif
624 
625 template <typename SLOT_SELECTOR>
626 DEVICE void count_matches_impl(int32_t* count_buff,
627  const int32_t invalid_slot_val,
628  const JoinColumn join_column,
629  const JoinColumnTypeInfo type_info
630 #ifndef __CUDACC__
631  ,
632  const void* sd_inner_proxy,
633  const void* sd_outer_proxy,
634  const int32_t cpu_thread_idx,
635  const int32_t cpu_thread_count
636 #endif
637  ,
638  SLOT_SELECTOR slot_selector) {
639 #ifdef __CUDACC__
640  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
641  int32_t step = blockDim.x * gridDim.x;
642 #else
643  int32_t start = cpu_thread_idx;
644  int32_t step = cpu_thread_count;
645 #endif
646  JoinColumnTyped col{&join_column, &type_info};
647  for (auto item : col.slice(start, step)) {
648  int64_t elem = item.element;
649  if (elem == type_info.null_val) {
650  if (type_info.uses_bw_eq) {
651  elem = type_info.translated_null_val;
652  } else {
653  continue;
654  }
655  }
656 #ifndef __CUDACC__
657  if (sd_inner_proxy &&
658  (!type_info.uses_bw_eq || elem != type_info.translated_null_val)) {
659  const auto outer_id = translate_str_id_to_outer_dict(
660  elem, type_info.min_val, type_info.max_val, sd_inner_proxy, sd_outer_proxy);
661  if (outer_id == StringDictionary::INVALID_STR_ID) {
662  continue;
663  }
664  elem = outer_id;
665  }
666  CHECK_GE(elem, type_info.min_val)
667  << "Element " << elem << " less than min val " << type_info.min_val;
668 #endif
669  auto* entry_ptr = slot_selector(count_buff, elem);
670  mapd_add(entry_ptr, int32_t(1));
671  }
672 }
673 
674 GLOBAL void SUFFIX(count_matches)(int32_t* count_buff,
675  const int32_t invalid_slot_val,
676  const JoinColumn join_column,
677  const JoinColumnTypeInfo type_info
678 #ifndef __CUDACC__
679  ,
680  const void* sd_inner_proxy,
681  const void* sd_outer_proxy,
682  const int32_t cpu_thread_idx,
683  const int32_t cpu_thread_count
684 #endif
685 ) {
686  auto slot_sel = [&type_info](auto count_buff, auto elem) {
687  return SUFFIX(get_hash_slot)(count_buff, elem, type_info.min_val);
688  };
689  count_matches_impl(count_buff,
690  invalid_slot_val,
691  join_column,
692  type_info
693 #ifndef __CUDACC__
694  ,
695  sd_inner_proxy,
696  sd_outer_proxy,
697  cpu_thread_idx,
698  cpu_thread_count
699 #endif
700  ,
701  slot_sel);
702 }
703 
704 GLOBAL void SUFFIX(count_matches_bucketized)(int32_t* count_buff,
705  const int32_t invalid_slot_val,
706  const JoinColumn join_column,
707  const JoinColumnTypeInfo type_info
708 #ifndef __CUDACC__
709  ,
710  const void* sd_inner_proxy,
711  const void* sd_outer_proxy,
712  const int32_t cpu_thread_idx,
713  const int32_t cpu_thread_count
714 #endif
715  ,
716  const int64_t bucket_normalization) {
717  auto slot_sel = [bucket_normalization, &type_info](auto count_buff, auto elem) {
719  count_buff, elem, type_info.min_val, bucket_normalization);
720  };
721  count_matches_impl(count_buff,
722  invalid_slot_val,
723  join_column,
724  type_info
725 #ifndef __CUDACC__
726  ,
727  sd_inner_proxy,
728  sd_outer_proxy,
729  cpu_thread_idx,
730  cpu_thread_count
731 #endif
732  ,
733  slot_sel);
734 }
735 
736 GLOBAL void SUFFIX(count_matches_sharded)(int32_t* count_buff,
737  const int32_t invalid_slot_val,
738  const JoinColumn join_column,
739  const JoinColumnTypeInfo type_info,
740  const ShardInfo shard_info
741 #ifndef __CUDACC__
742  ,
743  const void* sd_inner_proxy,
744  const void* sd_outer_proxy,
745  const int32_t cpu_thread_idx,
746  const int32_t cpu_thread_count
747 #endif
748 ) {
749 #ifdef __CUDACC__
750  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
751  int32_t step = blockDim.x * gridDim.x;
752 #else
753  int32_t start = cpu_thread_idx;
754  int32_t step = cpu_thread_count;
755 #endif
756  JoinColumnTyped col{&join_column, &type_info};
757  for (auto item : col.slice(start, step)) {
758  int64_t elem = item.element;
759  if (elem == type_info.null_val) {
760  if (type_info.uses_bw_eq) {
761  elem = type_info.translated_null_val;
762  } else {
763  continue;
764  }
765  }
766 #ifndef __CUDACC__
767  if (sd_inner_proxy &&
768  (!type_info.uses_bw_eq || elem != type_info.translated_null_val)) {
769  const auto outer_id = translate_str_id_to_outer_dict(
770  elem, type_info.min_val, type_info.max_val, sd_inner_proxy, sd_outer_proxy);
771  if (outer_id == StringDictionary::INVALID_STR_ID) {
772  continue;
773  }
774  elem = outer_id;
775  }
776  CHECK_GE(elem, type_info.min_val)
777  << "Element " << elem << " less than min val " << type_info.min_val;
778 #endif
779  int32_t* entry_ptr = SUFFIX(get_hash_slot_sharded)(count_buff,
780  elem,
781  type_info.min_val,
782  shard_info.entry_count_per_shard,
783  shard_info.num_shards,
784  shard_info.device_count);
785  mapd_add(entry_ptr, int32_t(1));
786  }
787 }
788 
789 template <typename T>
791  const T* key,
792  const size_t key_component_count,
793  const T* composite_key_dict,
794  const int64_t entry_count,
795  const size_t key_size_in_bytes) {
796  const uint32_t h = MurmurHash1Impl(key, key_size_in_bytes, 0) % entry_count;
797  uint32_t off = h * key_component_count;
798  if (keys_are_equal(&composite_key_dict[off], key, key_component_count)) {
799  return &composite_key_dict[off];
800  }
801  uint32_t h_probe = (h + 1) % entry_count;
802  while (h_probe != h) {
803  off = h_probe * key_component_count;
804  if (keys_are_equal(&composite_key_dict[off], key, key_component_count)) {
805  return &composite_key_dict[off];
806  }
807  h_probe = (h_probe + 1) % entry_count;
808  }
809 #ifndef __CUDACC__
810  CHECK(false);
811 #else
812  assert(false);
813 #endif
814  return nullptr;
815 }
816 
817 template <typename T, typename KEY_HANDLER>
818 GLOBAL void SUFFIX(count_matches_baseline)(int32_t* count_buff,
819  const T* composite_key_dict,
820  const int64_t entry_count,
821  const KEY_HANDLER* f,
822  const int64_t num_elems
823 #ifndef __CUDACC__
824  ,
825  const int32_t cpu_thread_idx,
826  const int32_t cpu_thread_count
827 #endif
828 ) {
829 #ifdef __CUDACC__
830  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
831  int32_t step = blockDim.x * gridDim.x;
832 #else
833  int32_t start = cpu_thread_idx;
834  int32_t step = cpu_thread_count;
835 #endif
836 #ifdef __CUDACC__
837  assert(composite_key_dict);
838 #endif
839  T key_scratch_buff[g_maximum_conditions_to_coalesce];
840  const size_t key_size_in_bytes = f->get_key_component_count() * sizeof(T);
841  auto key_buff_handler = [composite_key_dict,
842  entry_count,
843  count_buff,
844  key_size_in_bytes](const int64_t row_entry_idx,
845  const T* key_scratch_buff,
846  const size_t key_component_count) {
847  const auto matching_group =
849  key_component_count,
850  composite_key_dict,
851  entry_count,
852  key_size_in_bytes);
853  const auto entry_idx = (matching_group - composite_key_dict) / key_component_count;
854  mapd_add(&count_buff[entry_idx], int32_t(1));
855  return 0;
856  };
857 
858  JoinColumnTuple cols(
859  f->get_number_of_columns(), f->get_join_columns(), f->get_join_column_type_infos());
860  for (auto& it : cols.slice(start, step)) {
861  (*f)(it.join_column_iterators, key_scratch_buff, key_buff_handler);
862  }
863 }
864 
865 template <typename SLOT_SELECTOR>
866 DEVICE void fill_row_ids_impl(int32_t* buff,
867  const int64_t hash_entry_count,
868  const int32_t invalid_slot_val,
869  const JoinColumn join_column,
870  const JoinColumnTypeInfo type_info
871 #ifndef __CUDACC__
872  ,
873  const void* sd_inner_proxy,
874  const void* sd_outer_proxy,
875  const int32_t cpu_thread_idx,
876  const int32_t cpu_thread_count
877 #endif
878  ,
879  SLOT_SELECTOR slot_selector) {
880  int32_t* pos_buff = buff;
881  int32_t* count_buff = buff + hash_entry_count;
882  int32_t* id_buff = count_buff + hash_entry_count;
883 
884 #ifdef __CUDACC__
885  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
886  int32_t step = blockDim.x * gridDim.x;
887 #else
888  int32_t start = cpu_thread_idx;
889  int32_t step = cpu_thread_count;
890 #endif
891  JoinColumnTyped col{&join_column, &type_info};
892  for (auto item : col.slice(start, step)) {
893  const size_t index = item.index;
894  int64_t elem = item.element;
895  if (elem == type_info.null_val) {
896  if (type_info.uses_bw_eq) {
897  elem = type_info.translated_null_val;
898  } else {
899  continue;
900  }
901  }
902 #ifndef __CUDACC__
903  if (sd_inner_proxy &&
904  (!type_info.uses_bw_eq || elem != type_info.translated_null_val)) {
905  const auto outer_id = translate_str_id_to_outer_dict(
906  elem, type_info.min_val, type_info.max_val, sd_inner_proxy, sd_outer_proxy);
907  if (outer_id == StringDictionary::INVALID_STR_ID) {
908  continue;
909  }
910  elem = outer_id;
911  }
912  CHECK_GE(elem, type_info.min_val)
913  << "Element " << elem << " less than min val " << type_info.min_val;
914 #endif
915  auto pos_ptr = slot_selector(pos_buff, elem);
916 #ifndef __CUDACC__
917  CHECK_NE(*pos_ptr, invalid_slot_val);
918 #endif
919  const auto bin_idx = pos_ptr - pos_buff;
920  const auto id_buff_idx = mapd_add(count_buff + bin_idx, 1) + *pos_ptr;
921  id_buff[id_buff_idx] = static_cast<int32_t>(index);
922  }
923 }
924 
925 GLOBAL void SUFFIX(fill_row_ids)(int32_t* buff,
926  const int64_t hash_entry_count,
927  const int32_t invalid_slot_val,
928  const JoinColumn join_column,
929  const JoinColumnTypeInfo type_info
930 #ifndef __CUDACC__
931  ,
932  const void* sd_inner_proxy,
933  const void* sd_outer_proxy,
934  const int32_t cpu_thread_idx,
935  const int32_t cpu_thread_count
936 #endif
937 ) {
938  auto slot_sel = [&type_info](auto pos_buff, auto elem) {
939  return SUFFIX(get_hash_slot)(pos_buff, elem, type_info.min_val);
940  };
941 
942  fill_row_ids_impl(buff,
943  hash_entry_count,
944  invalid_slot_val,
945  join_column,
946  type_info
947 #ifndef __CUDACC__
948  ,
949  sd_inner_proxy,
950  sd_outer_proxy,
951  cpu_thread_idx,
952  cpu_thread_count
953 #endif
954  ,
955  slot_sel);
956 }
957 
959  const int64_t hash_entry_count,
960  const int32_t invalid_slot_val,
961  const JoinColumn join_column,
962  const JoinColumnTypeInfo type_info
963 #ifndef __CUDACC__
964  ,
965  const void* sd_inner_proxy,
966  const void* sd_outer_proxy,
967  const int32_t cpu_thread_idx,
968  const int32_t cpu_thread_count
969 #endif
970  ,
971  const int64_t bucket_normalization) {
972  auto slot_sel = [&type_info, bucket_normalization](auto pos_buff, auto elem) {
974  pos_buff, elem, type_info.min_val, bucket_normalization);
975  };
976  fill_row_ids_impl(buff,
977  hash_entry_count,
978  invalid_slot_val,
979  join_column,
980  type_info
981 #ifndef __CUDACC__
982  ,
983  sd_inner_proxy,
984  sd_outer_proxy,
985  cpu_thread_idx,
986  cpu_thread_count
987 #endif
988  ,
989  slot_sel);
990 }
991 
992 template <typename SLOT_SELECTOR>
994  const int64_t hash_entry_count,
995  const int32_t invalid_slot_val,
996  const JoinColumn join_column,
997  const JoinColumnTypeInfo type_info,
998  const ShardInfo shard_info
999 #ifndef __CUDACC__
1000  ,
1001  const void* sd_inner_proxy,
1002  const void* sd_outer_proxy,
1003  const int32_t cpu_thread_idx,
1004  const int32_t cpu_thread_count
1005 #endif
1006  ,
1007  SLOT_SELECTOR slot_selector) {
1008 
1009  int32_t* pos_buff = buff;
1010  int32_t* count_buff = buff + hash_entry_count;
1011  int32_t* id_buff = count_buff + hash_entry_count;
1012 
1013 #ifdef __CUDACC__
1014  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
1015  int32_t step = blockDim.x * gridDim.x;
1016 #else
1017  int32_t start = cpu_thread_idx;
1018  int32_t step = cpu_thread_count;
1019 #endif
1020  JoinColumnTyped col{&join_column, &type_info};
1021  for (auto item : col.slice(start, step)) {
1022  const size_t index = item.index;
1023  int64_t elem = item.element;
1024  if (elem == type_info.null_val) {
1025  if (type_info.uses_bw_eq) {
1026  elem = type_info.translated_null_val;
1027  } else {
1028  continue;
1029  }
1030  }
1031 #ifndef __CUDACC__
1032  if (sd_inner_proxy &&
1033  (!type_info.uses_bw_eq || elem != type_info.translated_null_val)) {
1034  const auto outer_id = translate_str_id_to_outer_dict(
1035  elem, type_info.min_val, type_info.max_val, sd_inner_proxy, sd_outer_proxy);
1036  if (outer_id == StringDictionary::INVALID_STR_ID) {
1037  continue;
1038  }
1039  elem = outer_id;
1040  }
1041  CHECK_GE(elem, type_info.min_val)
1042  << "Element " << elem << " less than min val " << type_info.min_val;
1043 #endif
1044  auto* pos_ptr = slot_selector(pos_buff, elem);
1045 #ifndef __CUDACC__
1046  CHECK_NE(*pos_ptr, invalid_slot_val);
1047 #endif
1048  const auto bin_idx = pos_ptr - pos_buff;
1049  const auto id_buff_idx = mapd_add(count_buff + bin_idx, 1) + *pos_ptr;
1050  id_buff[id_buff_idx] = static_cast<int32_t>(index);
1051  }
1052 }
1053 
1054 GLOBAL void SUFFIX(fill_row_ids_sharded)(int32_t* buff,
1055  const int64_t hash_entry_count,
1056  const int32_t invalid_slot_val,
1057  const JoinColumn join_column,
1058  const JoinColumnTypeInfo type_info,
1059  const ShardInfo shard_info
1060 #ifndef __CUDACC__
1061  ,
1062  const void* sd_inner_proxy,
1063  const void* sd_outer_proxy,
1064  const int32_t cpu_thread_idx,
1065  const int32_t cpu_thread_count
1066 #endif
1067 ) {
1068  auto slot_sel = [&type_info, &shard_info](auto pos_buff, auto elem) {
1069  return SUFFIX(get_hash_slot_sharded)(pos_buff,
1070  elem,
1071  type_info.min_val,
1072  shard_info.entry_count_per_shard,
1073  shard_info.num_shards,
1074  shard_info.device_count);
1075  };
1076 
1077  fill_row_ids_impl(buff,
1078  hash_entry_count,
1079  invalid_slot_val,
1080  join_column,
1081  type_info
1082 #ifndef __CUDACC__
1083  ,
1084  sd_inner_proxy,
1085  sd_outer_proxy,
1086  cpu_thread_idx,
1087  cpu_thread_count
1088 #endif
1089  ,
1090  slot_sel);
1091 }
1092 
1094  const int64_t hash_entry_count,
1095  const int32_t invalid_slot_val,
1096  const JoinColumn join_column,
1097  const JoinColumnTypeInfo type_info,
1098  const ShardInfo shard_info
1099 #ifndef __CUDACC__
1100  ,
1101  const void* sd_inner_proxy,
1102  const void* sd_outer_proxy,
1103  const int32_t cpu_thread_idx,
1104  const int32_t cpu_thread_count
1105 #endif
1106  ,
1107  const int64_t bucket_normalization) {
1108  auto slot_sel = [&shard_info, &type_info, bucket_normalization](auto pos_buff,
1109  auto elem) {
1110  return SUFFIX(get_bucketized_hash_slot_sharded)(pos_buff,
1111  elem,
1112  type_info.min_val,
1113  shard_info.entry_count_per_shard,
1114  shard_info.num_shards,
1115  shard_info.device_count,
1116  bucket_normalization);
1117  };
1118 
1119  fill_row_ids_impl(buff,
1120  hash_entry_count,
1121  invalid_slot_val,
1122  join_column,
1123  type_info
1124 #ifndef __CUDACC__
1125  ,
1126  sd_inner_proxy,
1127  sd_outer_proxy,
1128  cpu_thread_idx,
1129  cpu_thread_count
1130 #endif
1131  ,
1132  slot_sel);
1133 }
1134 
1135 template <typename T, typename KEY_HANDLER>
1137  const T* composite_key_dict,
1138  const int64_t hash_entry_count,
1139  const int32_t invalid_slot_val,
1140  const KEY_HANDLER* f,
1141  const int64_t num_elems
1142 #ifndef __CUDACC__
1143  ,
1144  const int32_t cpu_thread_idx,
1145  const int32_t cpu_thread_count
1146 #endif
1147 ) {
1148  int32_t* pos_buff = buff;
1149  int32_t* count_buff = buff + hash_entry_count;
1150  int32_t* id_buff = count_buff + hash_entry_count;
1151 #ifdef __CUDACC__
1152  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
1153  int32_t step = blockDim.x * gridDim.x;
1154 #else
1155  int32_t start = cpu_thread_idx;
1156  int32_t step = cpu_thread_count;
1157 #endif
1158 
1159  T key_scratch_buff[g_maximum_conditions_to_coalesce];
1160 #ifdef __CUDACC__
1161  assert(composite_key_dict);
1162 #endif
1163  const size_t key_size_in_bytes = f->get_key_component_count() * sizeof(T);
1164  auto key_buff_handler = [composite_key_dict,
1165  hash_entry_count,
1166  pos_buff,
1167  invalid_slot_val,
1168  count_buff,
1169  id_buff,
1170  key_size_in_bytes](const int64_t row_index,
1171  const T* key_scratch_buff,
1172  const size_t key_component_count) {
1173  const T* matching_group =
1175  key_component_count,
1176  composite_key_dict,
1177  hash_entry_count,
1178  key_size_in_bytes);
1179  const auto entry_idx = (matching_group - composite_key_dict) / key_component_count;
1180  int32_t* pos_ptr = pos_buff + entry_idx;
1181 #ifndef __CUDACC__
1182  CHECK_NE(*pos_ptr, invalid_slot_val);
1183 #endif
1184  const auto bin_idx = pos_ptr - pos_buff;
1185  const auto id_buff_idx = mapd_add(count_buff + bin_idx, 1) + *pos_ptr;
1186  id_buff[id_buff_idx] = static_cast<int32_t>(row_index);
1187  return 0;
1188  };
1189 
1190  JoinColumnTuple cols(
1191  f->get_number_of_columns(), f->get_join_columns(), f->get_join_column_type_infos());
1192  for (auto& it : cols.slice(start, step)) {
1193  (*f)(it.join_column_iterators, key_scratch_buff, key_buff_handler);
1194  }
1195  return;
1196 }
1197 
1198 #undef mapd_add
1199 
1200 template <typename KEY_HANDLER>
1202  int32_t* row_count_buffer,
1203  const uint32_t b,
1204  const int64_t num_elems,
1205  const KEY_HANDLER* f
1206 #ifndef __CUDACC__
1207  ,
1208  const int32_t cpu_thread_idx,
1209  const int32_t cpu_thread_count
1210 #endif
1211 ) {
1212 #ifdef __CUDACC__
1213  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
1214  int32_t step = blockDim.x * gridDim.x;
1215 #else
1216  int32_t start = cpu_thread_idx;
1217  int32_t step = cpu_thread_count;
1218 #endif
1219 
1220  auto key_buff_handler = [b, hll_buffer, row_count_buffer](
1221  const int64_t entry_idx,
1222  const int64_t* key_scratch_buff,
1223  const size_t key_component_count) {
1224  if (row_count_buffer) {
1225  row_count_buffer[entry_idx] += 1;
1226  }
1227 
1228  const uint64_t hash =
1229  MurmurHash64AImpl(key_scratch_buff, key_component_count * sizeof(int64_t), 0);
1230  const uint32_t index = hash >> (64 - b);
1231  const auto rank = get_rank(hash << b, 64 - b);
1232 #ifdef __CUDACC__
1233  atomicMax(reinterpret_cast<int32_t*>(hll_buffer) + index, rank);
1234 #else
1235  hll_buffer[index] = std::max(hll_buffer[index], rank);
1236 #endif
1237 
1238  return 0;
1239  };
1240 
1241  int64_t key_scratch_buff[g_maximum_conditions_to_coalesce];
1242 
1243  JoinColumnTuple cols(
1244  f->get_number_of_columns(), f->get_join_columns(), f->get_join_column_type_infos());
1245  for (auto& it : cols.slice(start, step)) {
1246  (*f)(it.join_column_iterators, key_scratch_buff, key_buff_handler);
1247  }
1248 }
1249 
1250 #ifdef __CUDACC__
1251 namespace {
1252 // TODO(adb): put these in a header file so they are not duplicated between here and
1253 // cuda_mapd_rt.cu
1254 __device__ double atomicMin(double* address, double val) {
1255  unsigned long long int* address_as_ull = (unsigned long long int*)address;
1256  unsigned long long int old = *address_as_ull, assumed;
1257 
1258  do {
1259  assumed = old;
1260  old = atomicCAS(address_as_ull,
1261  assumed,
1262  __double_as_longlong(min(val, __longlong_as_double(assumed))));
1263  } while (assumed != old);
1264 
1265  return __longlong_as_double(old);
1266 }
1267 } // namespace
1268 #endif
1269 
1270 template <size_t N>
1271 GLOBAL void SUFFIX(compute_bucket_sizes_impl)(double* bucket_sizes_for_thread,
1272  const JoinColumn* join_column,
1273  const JoinColumnTypeInfo* type_info,
1274  const double* bucket_size_thresholds
1275 #ifndef __CUDACC__
1276  ,
1277  const int32_t cpu_thread_idx,
1278  const int32_t cpu_thread_count
1279 #endif
1280 ) {
1281 #ifdef __CUDACC__
1282  int32_t start = threadIdx.x + blockDim.x * blockIdx.x;
1283  int32_t step = blockDim.x * gridDim.x;
1284 #else
1285  int32_t start = cpu_thread_idx;
1286  int32_t step = cpu_thread_count;
1287 #endif
1288  JoinColumnIterator it(join_column, type_info, start, step);
1289  for (; it; ++it) {
1290  // We expect the bounds column to be (min, max) e.g. (x_min, y_min, x_max, y_max)
1291  double bounds[2 * N];
1292  for (size_t j = 0; j < 2 * N; j++) {
1293  bounds[j] = SUFFIX(fixed_width_double_decode_noinline)(it.ptr(), j);
1294  }
1295 
1296  for (size_t j = 0; j < N; j++) {
1297  const auto diff = bounds[j + N] - bounds[j];
1298 #ifdef __CUDACC__
1299  if (diff > bucket_size_thresholds[j]) {
1300  atomicMin(&bucket_sizes_for_thread[j], diff);
1301  }
1302 #else
1303  if (diff < bucket_size_thresholds[j] && diff > bucket_sizes_for_thread[j]) {
1304  bucket_sizes_for_thread[j] = diff;
1305  }
1306 #endif
1307  }
1308  }
1309 }
1310 
1311 #ifndef __CUDACC__
1312 
1313 template <typename InputIterator, typename OutputIterator>
1314 void inclusive_scan(InputIterator first,
1315  InputIterator last,
1316  OutputIterator out,
1317  const size_t thread_count) {
1318  using ElementType = typename InputIterator::value_type;
1319  using OffsetType = typename InputIterator::difference_type;
1320  const OffsetType elem_count = last - first;
1321  if (elem_count < 10000 || thread_count <= 1) {
1322  ElementType sum = 0;
1323  for (auto iter = first; iter != last; ++iter, ++out) {
1324  *out = sum += *iter;
1325  }
1326  return;
1327  }
1328 
1329  const OffsetType step = (elem_count + thread_count - 1) / thread_count;
1330  OffsetType start_off = 0;
1331  OffsetType end_off = std::min(step, elem_count);
1332  std::vector<ElementType> partial_sums(thread_count);
1333  std::vector<std::future<void>> counter_threads;
1334  for (size_t thread_idx = 0; thread_idx < thread_count; ++thread_idx,
1335  start_off = std::min(start_off + step, elem_count),
1336  end_off = std::min(start_off + step, elem_count)) {
1337  counter_threads.push_back(std::async(
1338  std::launch::async,
1339  [first, out](
1340  ElementType& partial_sum, const OffsetType start, const OffsetType end) {
1341  ElementType sum = 0;
1342  for (auto in_iter = first + start, out_iter = out + start;
1343  in_iter != (first + end);
1344  ++in_iter, ++out_iter) {
1345  *out_iter = sum += *in_iter;
1346  }
1347  partial_sum = sum;
1348  },
1349  std::ref(partial_sums[thread_idx]),
1350  start_off,
1351  end_off));
1352  }
1353  for (auto& child : counter_threads) {
1354  child.get();
1355  }
1356 
1357  ElementType sum = 0;
1358  for (auto& s : partial_sums) {
1359  s += sum;
1360  sum = s;
1361  }
1362 
1363  counter_threads.clear();
1364  start_off = std::min(step, elem_count);
1365  end_off = std::min(start_off + step, elem_count);
1366  for (size_t thread_idx = 0; thread_idx < thread_count - 1; ++thread_idx,
1367  start_off = std::min(start_off + step, elem_count),
1368  end_off = std::min(start_off + step, elem_count)) {
1369  counter_threads.push_back(std::async(
1370  std::launch::async,
1371  [out](const ElementType prev_sum, const OffsetType start, const OffsetType end) {
1372  for (auto iter = out + start; iter != (out + end); ++iter) {
1373  *iter += prev_sum;
1374  }
1375  },
1376  partial_sums[thread_idx],
1377  start_off,
1378  end_off));
1379  }
1380  for (auto& child : counter_threads) {
1381  child.get();
1382  }
1383 }
1384 
1385 template <typename COUNT_MATCHES_LAUNCH_FUNCTOR, typename FILL_ROW_IDS_LAUNCH_FUNCTOR>
1387  const int64_t hash_entry_count,
1388  const int32_t invalid_slot_val,
1389  const JoinColumn& join_column,
1390  const JoinColumnTypeInfo& type_info,
1391  const void* sd_inner_proxy,
1392  const void* sd_outer_proxy,
1393  const unsigned cpu_thread_count,
1394  COUNT_MATCHES_LAUNCH_FUNCTOR count_matches_func,
1395  FILL_ROW_IDS_LAUNCH_FUNCTOR fill_row_ids_func) {
1396  int32_t* pos_buff = buff;
1397  int32_t* count_buff = buff + hash_entry_count;
1398  memset(count_buff, 0, hash_entry_count * sizeof(int32_t));
1399  std::vector<std::future<void>> counter_threads;
1400  for (unsigned cpu_thread_idx = 0; cpu_thread_idx < cpu_thread_count; ++cpu_thread_idx) {
1401  counter_threads.push_back(std::async(
1402  std::launch::async, count_matches_func, cpu_thread_idx, cpu_thread_count));
1403  }
1404 
1405  for (auto& child : counter_threads) {
1406  child.get();
1407  }
1408 
1409  std::vector<int32_t> count_copy(hash_entry_count, 0);
1410  CHECK_GT(hash_entry_count, int64_t(0));
1411  memcpy(count_copy.data() + 1, count_buff, (hash_entry_count - 1) * sizeof(int32_t));
1412 #if HAVE_CUDA
1413  thrust::inclusive_scan(count_copy.begin(), count_copy.end(), count_copy.begin());
1414 #else
1416  count_copy.begin(), count_copy.end(), count_copy.begin(), cpu_thread_count);
1417 #endif
1418  std::vector<std::future<void>> pos_threads;
1419  for (size_t cpu_thread_idx = 0; cpu_thread_idx < cpu_thread_count; ++cpu_thread_idx) {
1420  pos_threads.push_back(std::async(
1421  std::launch::async,
1422  [&](size_t thread_idx) {
1423  for (int64_t i = thread_idx; i < hash_entry_count; i += cpu_thread_count) {
1424  if (count_buff[i]) {
1425  pos_buff[i] = count_copy[i];
1426  }
1427  }
1428  },
1429  cpu_thread_idx));
1430  }
1431  for (auto& child : pos_threads) {
1432  child.get();
1433  }
1434 
1435  memset(count_buff, 0, hash_entry_count * sizeof(int32_t));
1436  std::vector<std::future<void>> rowid_threads;
1437  for (size_t cpu_thread_idx = 0; cpu_thread_idx < cpu_thread_count; ++cpu_thread_idx) {
1438  rowid_threads.push_back(std::async(
1439  std::launch::async, fill_row_ids_func, cpu_thread_idx, cpu_thread_count));
1440  }
1441 
1442  for (auto& child : rowid_threads) {
1443  child.get();
1444  }
1445 }
1446 
1447 void fill_one_to_many_hash_table(int32_t* buff,
1448  const HashEntryInfo hash_entry_info,
1449  const int32_t invalid_slot_val,
1450  const JoinColumn& join_column,
1451  const JoinColumnTypeInfo& type_info,
1452  const void* sd_inner_proxy,
1453  const void* sd_outer_proxy,
1454  const unsigned cpu_thread_count) {
1455  auto launch_count_matches = [count_buff = buff + hash_entry_info.hash_entry_count,
1456  invalid_slot_val,
1457  &join_column,
1458  &type_info,
1459  sd_inner_proxy,
1460  sd_outer_proxy](auto cpu_thread_idx,
1461  auto cpu_thread_count) {
1463  (count_buff,
1464  invalid_slot_val,
1465  join_column,
1466  type_info,
1467  sd_inner_proxy,
1468  sd_outer_proxy,
1469  cpu_thread_idx,
1470  cpu_thread_count);
1471  };
1472  auto launch_fill_row_ids = [hash_entry_count = hash_entry_info.hash_entry_count,
1473  buff,
1474  invalid_slot_val,
1475  &join_column,
1476  &type_info,
1477  sd_inner_proxy,
1478  sd_outer_proxy](auto cpu_thread_idx,
1479  auto cpu_thread_count) {
1481  (buff,
1482  hash_entry_count,
1483  invalid_slot_val,
1484  join_column,
1485  type_info,
1486  sd_inner_proxy,
1487  sd_outer_proxy,
1488  cpu_thread_idx,
1489  cpu_thread_count);
1490  };
1491 
1493  hash_entry_info.hash_entry_count,
1494  invalid_slot_val,
1495  join_column,
1496  type_info,
1497  sd_inner_proxy,
1498  sd_outer_proxy,
1499  cpu_thread_count,
1500  launch_count_matches,
1501  launch_fill_row_ids);
1502 }
1503 
1505  const HashEntryInfo hash_entry_info,
1506  const int32_t invalid_slot_val,
1507  const JoinColumn& join_column,
1508  const JoinColumnTypeInfo& type_info,
1509  const void* sd_inner_proxy,
1510  const void* sd_outer_proxy,
1511  const unsigned cpu_thread_count) {
1512  auto bucket_normalization = hash_entry_info.bucket_normalization;
1513  auto hash_entry_count = hash_entry_info.getNormalizedHashEntryCount();
1514  auto launch_count_matches = [bucket_normalization,
1515  count_buff = buff + hash_entry_count,
1516  invalid_slot_val,
1517  &join_column,
1518  &type_info,
1519  sd_inner_proxy,
1520  sd_outer_proxy](auto cpu_thread_idx,
1521  auto cpu_thread_count) {
1523  (count_buff,
1524  invalid_slot_val,
1525  join_column,
1526  type_info,
1527  sd_inner_proxy,
1528  sd_outer_proxy,
1529  cpu_thread_idx,
1530  cpu_thread_count,
1531  bucket_normalization);
1532  };
1533  auto launch_fill_row_ids = [bucket_normalization,
1534  hash_entry_count,
1535  buff,
1536  invalid_slot_val,
1537  &join_column,
1538  &type_info,
1539  sd_inner_proxy,
1540  sd_outer_proxy](auto cpu_thread_idx,
1541  auto cpu_thread_count) {
1543  (buff,
1544  hash_entry_count,
1545  invalid_slot_val,
1546  join_column,
1547  type_info,
1548  sd_inner_proxy,
1549  sd_outer_proxy,
1550  cpu_thread_idx,
1551  cpu_thread_count,
1552  bucket_normalization);
1553  };
1554 
1556  hash_entry_count,
1557  invalid_slot_val,
1558  join_column,
1559  type_info,
1560  sd_inner_proxy,
1561  sd_outer_proxy,
1562  cpu_thread_count,
1563  launch_count_matches,
1564  launch_fill_row_ids);
1565 }
1566 
1567 template <typename COUNT_MATCHES_LAUNCH_FUNCTOR, typename FILL_ROW_IDS_LAUNCH_FUNCTOR>
1569  int32_t* buff,
1570  const int64_t hash_entry_count,
1571  const int32_t invalid_slot_val,
1572  const JoinColumn& join_column,
1573  const JoinColumnTypeInfo& type_info,
1574  const ShardInfo& shard_info,
1575  const void* sd_inner_proxy,
1576  const void* sd_outer_proxy,
1577  const unsigned cpu_thread_count,
1578  COUNT_MATCHES_LAUNCH_FUNCTOR count_matches_launcher,
1579  FILL_ROW_IDS_LAUNCH_FUNCTOR fill_row_ids_launcher) {
1580  int32_t* pos_buff = buff;
1581  int32_t* count_buff = buff + hash_entry_count;
1582  memset(count_buff, 0, hash_entry_count * sizeof(int32_t));
1583  std::vector<std::future<void>> counter_threads;
1584  for (size_t cpu_thread_idx = 0; cpu_thread_idx < cpu_thread_count; ++cpu_thread_idx) {
1585  counter_threads.push_back(std::async(
1586  std::launch::async, count_matches_launcher, cpu_thread_idx, cpu_thread_count));
1587  }
1588 
1589  for (auto& child : counter_threads) {
1590  child.get();
1591  }
1592 
1593  std::vector<int32_t> count_copy(hash_entry_count, 0);
1594  CHECK_GT(hash_entry_count, int64_t(0));
1595  memcpy(&count_copy[1], count_buff, (hash_entry_count - 1) * sizeof(int32_t));
1597  count_copy.begin(), count_copy.end(), count_copy.begin(), cpu_thread_count);
1598  std::vector<std::future<void>> pos_threads;
1599  for (size_t cpu_thread_idx = 0; cpu_thread_idx < cpu_thread_count; ++cpu_thread_idx) {
1600  pos_threads.push_back(std::async(
1601  std::launch::async,
1602  [&](const unsigned thread_idx) {
1603  for (int64_t i = thread_idx; i < hash_entry_count; i += cpu_thread_count) {
1604  if (count_buff[i]) {
1605  pos_buff[i] = count_copy[i];
1606  }
1607  }
1608  },
1609  cpu_thread_idx));
1610  }
1611  for (auto& child : pos_threads) {
1612  child.get();
1613  }
1614 
1615  memset(count_buff, 0, hash_entry_count * sizeof(int32_t));
1616  std::vector<std::future<void>> rowid_threads;
1617  for (size_t cpu_thread_idx = 0; cpu_thread_idx < cpu_thread_count; ++cpu_thread_idx) {
1618  rowid_threads.push_back(std::async(
1619  std::launch::async, fill_row_ids_launcher, cpu_thread_idx, cpu_thread_count));
1620  }
1621 
1622  for (auto& child : rowid_threads) {
1623  child.get();
1624  }
1625 }
1626 
1628  const int64_t hash_entry_count,
1629  const int32_t invalid_slot_val,
1630  const JoinColumn& join_column,
1631  const JoinColumnTypeInfo& type_info,
1632  const ShardInfo& shard_info,
1633  const void* sd_inner_proxy,
1634  const void* sd_outer_proxy,
1635  const unsigned cpu_thread_count) {
1636  auto launch_count_matches = [count_buff = buff + hash_entry_count,
1637  invalid_slot_val,
1638  &join_column,
1639  &type_info,
1640  &shard_info
1641 #ifndef __CUDACC__
1642  ,
1643  sd_inner_proxy,
1644  sd_outer_proxy
1645 #endif
1646  ](auto cpu_thread_idx, auto cpu_thread_count) {
1647  return SUFFIX(count_matches_sharded)(count_buff,
1648  invalid_slot_val,
1649  join_column,
1650  type_info,
1651  shard_info
1652 #ifndef __CUDACC__
1653  ,
1654  sd_inner_proxy,
1655  sd_outer_proxy,
1656  cpu_thread_idx,
1657  cpu_thread_count
1658 #endif
1659  );
1660  };
1661 
1662  auto launch_fill_row_ids = [buff,
1663  hash_entry_count,
1664  invalid_slot_val,
1665  &join_column,
1666  &type_info,
1667  &shard_info
1668 #ifndef __CUDACC__
1669  ,
1670  sd_inner_proxy,
1671  sd_outer_proxy
1672 #endif
1673  ](auto cpu_thread_idx, auto cpu_thread_count) {
1674  return SUFFIX(fill_row_ids_sharded)(buff,
1675  hash_entry_count,
1676  invalid_slot_val,
1677  join_column,
1678  type_info,
1679  shard_info
1680 #ifndef __CUDACC__
1681  ,
1682  sd_inner_proxy,
1683  sd_outer_proxy,
1684  cpu_thread_idx,
1685  cpu_thread_count);
1686 #endif
1687  };
1688 
1690  hash_entry_count,
1691  invalid_slot_val,
1692  join_column,
1693  type_info,
1694  shard_info
1695 #ifndef __CUDACC__
1696  ,
1697  sd_inner_proxy,
1698  sd_outer_proxy,
1699  cpu_thread_count
1700 #endif
1701  ,
1702  launch_count_matches,
1703  launch_fill_row_ids);
1704 }
1705 
1706 void init_baseline_hash_join_buff_32(int8_t* hash_join_buff,
1707  const int64_t entry_count,
1708  const size_t key_component_count,
1709  const bool with_val_slot,
1710  const int32_t invalid_slot_val,
1711  const int32_t cpu_thread_idx,
1712  const int32_t cpu_thread_count) {
1713  init_baseline_hash_join_buff<int32_t>(hash_join_buff,
1714  entry_count,
1715  key_component_count,
1716  with_val_slot,
1717  invalid_slot_val,
1718  cpu_thread_idx,
1719  cpu_thread_count);
1720 }
1721 
1722 void init_baseline_hash_join_buff_64(int8_t* hash_join_buff,
1723  const int64_t entry_count,
1724  const size_t key_component_count,
1725  const bool with_val_slot,
1726  const int32_t invalid_slot_val,
1727  const int32_t cpu_thread_idx,
1728  const int32_t cpu_thread_count) {
1729  init_baseline_hash_join_buff<int64_t>(hash_join_buff,
1730  entry_count,
1731  key_component_count,
1732  with_val_slot,
1733  invalid_slot_val,
1734  cpu_thread_idx,
1735  cpu_thread_count);
1736 }
1737 
1738 int fill_baseline_hash_join_buff_32(int8_t* hash_buff,
1739  const int64_t entry_count,
1740  const int32_t invalid_slot_val,
1741  const bool for_semi_join,
1742  const size_t key_component_count,
1743  const bool with_val_slot,
1744  const GenericKeyHandler* key_handler,
1745  const int64_t num_elems,
1746  const int32_t cpu_thread_idx,
1747  const int32_t cpu_thread_count) {
1748  return fill_baseline_hash_join_buff<int32_t>(hash_buff,
1749  entry_count,
1750  invalid_slot_val,
1751  for_semi_join,
1752  key_component_count,
1753  with_val_slot,
1754  key_handler,
1755  num_elems,
1756  cpu_thread_idx,
1757  cpu_thread_count);
1758 }
1759 
1761  const int64_t entry_count,
1762  const int32_t invalid_slot_val,
1763  const size_t key_component_count,
1764  const bool with_val_slot,
1765  const OverlapsKeyHandler* key_handler,
1766  const int64_t num_elems,
1767  const int32_t cpu_thread_idx,
1768  const int32_t cpu_thread_count) {
1769  return fill_baseline_hash_join_buff<int32_t>(hash_buff,
1770  entry_count,
1771  invalid_slot_val,
1772  false,
1773  key_component_count,
1774  with_val_slot,
1775  key_handler,
1776  num_elems,
1777  cpu_thread_idx,
1778  cpu_thread_count);
1779 }
1780 
1781 int fill_baseline_hash_join_buff_64(int8_t* hash_buff,
1782  const int64_t entry_count,
1783  const int32_t invalid_slot_val,
1784  const bool for_semi_join,
1785  const size_t key_component_count,
1786  const bool with_val_slot,
1787  const GenericKeyHandler* key_handler,
1788  const int64_t num_elems,
1789  const int32_t cpu_thread_idx,
1790  const int32_t cpu_thread_count) {
1791  return fill_baseline_hash_join_buff<int64_t>(hash_buff,
1792  entry_count,
1793  invalid_slot_val,
1794  for_semi_join,
1795  key_component_count,
1796  with_val_slot,
1797  key_handler,
1798  num_elems,
1799  cpu_thread_idx,
1800  cpu_thread_count);
1801 }
1802 
1804  const int64_t entry_count,
1805  const int32_t invalid_slot_val,
1806  const size_t key_component_count,
1807  const bool with_val_slot,
1808  const OverlapsKeyHandler* key_handler,
1809  const int64_t num_elems,
1810  const int32_t cpu_thread_idx,
1811  const int32_t cpu_thread_count) {
1812  return fill_baseline_hash_join_buff<int64_t>(hash_buff,
1813  entry_count,
1814  invalid_slot_val,
1815  false,
1816  key_component_count,
1817  with_val_slot,
1818  key_handler,
1819  num_elems,
1820  cpu_thread_idx,
1821  cpu_thread_count);
1822 }
1823 
1824 template <typename T>
1826  int32_t* buff,
1827  const T* composite_key_dict,
1828  const int64_t hash_entry_count,
1829  const int32_t invalid_slot_val,
1830  const size_t key_component_count,
1831  const std::vector<JoinColumn>& join_column_per_key,
1832  const std::vector<JoinColumnTypeInfo>& type_info_per_key,
1833  const std::vector<JoinBucketInfo>& join_buckets_per_key,
1834  const std::vector<const void*>& sd_inner_proxy_per_key,
1835  const std::vector<const void*>& sd_outer_proxy_per_key,
1836  const size_t cpu_thread_count) {
1837  int32_t* pos_buff = buff;
1838  int32_t* count_buff = buff + hash_entry_count;
1839  memset(count_buff, 0, hash_entry_count * sizeof(int32_t));
1840  std::vector<std::future<void>> counter_threads;
1841  for (size_t cpu_thread_idx = 0; cpu_thread_idx < cpu_thread_count; ++cpu_thread_idx) {
1842  if (join_buckets_per_key.size() > 0) {
1843  counter_threads.push_back(std::async(
1844  std::launch::async,
1845  [count_buff,
1846  composite_key_dict,
1847  &hash_entry_count,
1848  &join_buckets_per_key,
1849  &join_column_per_key,
1850  cpu_thread_idx,
1851  cpu_thread_count] {
1852  const auto key_handler = OverlapsKeyHandler(
1853  join_buckets_per_key[0].inverse_bucket_sizes_for_dimension.size(),
1854  &join_column_per_key[0],
1855  join_buckets_per_key[0].inverse_bucket_sizes_for_dimension.data());
1856  count_matches_baseline(count_buff,
1857  composite_key_dict,
1858  hash_entry_count,
1859  &key_handler,
1860  join_column_per_key[0].num_elems,
1861  cpu_thread_idx,
1862  cpu_thread_count);
1863  }));
1864  } else {
1865  counter_threads.push_back(std::async(
1866  std::launch::async,
1867  [count_buff,
1868  composite_key_dict,
1869  &key_component_count,
1870  &hash_entry_count,
1871  &join_column_per_key,
1872  &type_info_per_key,
1873  &sd_inner_proxy_per_key,
1874  &sd_outer_proxy_per_key,
1875  cpu_thread_idx,
1876  cpu_thread_count] {
1877  const auto key_handler = GenericKeyHandler(key_component_count,
1878  true,
1879  &join_column_per_key[0],
1880  &type_info_per_key[0],
1881  &sd_inner_proxy_per_key[0],
1882  &sd_outer_proxy_per_key[0]);
1883  count_matches_baseline(count_buff,
1884  composite_key_dict,
1885  hash_entry_count,
1886  &key_handler,
1887  join_column_per_key[0].num_elems,
1888  cpu_thread_idx,
1889  cpu_thread_count);
1890  }));
1891  }
1892  }
1893 
1894  for (auto& child : counter_threads) {
1895  child.get();
1896  }
1897 
1898  std::vector<int32_t> count_copy(hash_entry_count, 0);
1899  CHECK_GT(hash_entry_count, int64_t(0));
1900  memcpy(&count_copy[1], count_buff, (hash_entry_count - 1) * sizeof(int32_t));
1902  count_copy.begin(), count_copy.end(), count_copy.begin(), cpu_thread_count);
1903  std::vector<std::future<void>> pos_threads;
1904  for (size_t cpu_thread_idx = 0; cpu_thread_idx < cpu_thread_count; ++cpu_thread_idx) {
1905  pos_threads.push_back(std::async(
1906  std::launch::async,
1907  [&](const int thread_idx) {
1908  for (int64_t i = thread_idx; i < hash_entry_count; i += cpu_thread_count) {
1909  if (count_buff[i]) {
1910  pos_buff[i] = count_copy[i];
1911  }
1912  }
1913  },
1914  cpu_thread_idx));
1915  }
1916  for (auto& child : pos_threads) {
1917  child.get();
1918  }
1919 
1920  memset(count_buff, 0, hash_entry_count * sizeof(int32_t));
1921  std::vector<std::future<void>> rowid_threads;
1922  for (size_t cpu_thread_idx = 0; cpu_thread_idx < cpu_thread_count; ++cpu_thread_idx) {
1923  if (join_buckets_per_key.size() > 0) {
1924  rowid_threads.push_back(std::async(
1925  std::launch::async,
1926  [buff,
1927  composite_key_dict,
1928  hash_entry_count,
1929  invalid_slot_val,
1930  &join_column_per_key,
1931  &join_buckets_per_key,
1932  cpu_thread_idx,
1933  cpu_thread_count] {
1934  const auto key_handler = OverlapsKeyHandler(
1935  join_buckets_per_key[0].inverse_bucket_sizes_for_dimension.size(),
1936  &join_column_per_key[0],
1937  join_buckets_per_key[0].inverse_bucket_sizes_for_dimension.data());
1939  (buff,
1940  composite_key_dict,
1941  hash_entry_count,
1942  invalid_slot_val,
1943  &key_handler,
1944  join_column_per_key[0].num_elems,
1945  cpu_thread_idx,
1946  cpu_thread_count);
1947  }));
1948  } else {
1949  rowid_threads.push_back(std::async(std::launch::async,
1950  [buff,
1951  composite_key_dict,
1952  hash_entry_count,
1953  invalid_slot_val,
1954  key_component_count,
1955  &join_column_per_key,
1956  &type_info_per_key,
1957  &sd_inner_proxy_per_key,
1958  &sd_outer_proxy_per_key,
1959  cpu_thread_idx,
1960  cpu_thread_count] {
1961  const auto key_handler = GenericKeyHandler(
1962  key_component_count,
1963  true,
1964  &join_column_per_key[0],
1965  &type_info_per_key[0],
1966  &sd_inner_proxy_per_key[0],
1967  &sd_outer_proxy_per_key[0]);
1969  (buff,
1970  composite_key_dict,
1971  hash_entry_count,
1972  invalid_slot_val,
1973  &key_handler,
1974  join_column_per_key[0].num_elems,
1975  cpu_thread_idx,
1976  cpu_thread_count);
1977  }));
1978  }
1979  }
1980 
1981  for (auto& child : rowid_threads) {
1982  child.get();
1983  }
1984 }
1985 
1987  int32_t* buff,
1988  const int32_t* composite_key_dict,
1989  const int64_t hash_entry_count,
1990  const int32_t invalid_slot_val,
1991  const size_t key_component_count,
1992  const std::vector<JoinColumn>& join_column_per_key,
1993  const std::vector<JoinColumnTypeInfo>& type_info_per_key,
1994  const std::vector<JoinBucketInfo>& join_bucket_info,
1995  const std::vector<const void*>& sd_inner_proxy_per_key,
1996  const std::vector<const void*>& sd_outer_proxy_per_key,
1997  const int32_t cpu_thread_count) {
1998  fill_one_to_many_baseline_hash_table<int32_t>(buff,
1999  composite_key_dict,
2000  hash_entry_count,
2001  invalid_slot_val,
2002  key_component_count,
2003  join_column_per_key,
2004  type_info_per_key,
2005  join_bucket_info,
2006  sd_inner_proxy_per_key,
2007  sd_outer_proxy_per_key,
2008  cpu_thread_count);
2009 }
2010 
2012  int32_t* buff,
2013  const int64_t* composite_key_dict,
2014  const int64_t hash_entry_count,
2015  const int32_t invalid_slot_val,
2016  const size_t key_component_count,
2017  const std::vector<JoinColumn>& join_column_per_key,
2018  const std::vector<JoinColumnTypeInfo>& type_info_per_key,
2019  const std::vector<JoinBucketInfo>& join_bucket_info,
2020  const std::vector<const void*>& sd_inner_proxy_per_key,
2021  const std::vector<const void*>& sd_outer_proxy_per_key,
2022  const int32_t cpu_thread_count) {
2023  fill_one_to_many_baseline_hash_table<int64_t>(buff,
2024  composite_key_dict,
2025  hash_entry_count,
2026  invalid_slot_val,
2027  key_component_count,
2028  join_column_per_key,
2029  type_info_per_key,
2030  join_bucket_info,
2031  sd_inner_proxy_per_key,
2032  sd_outer_proxy_per_key,
2033  cpu_thread_count);
2034 }
2035 
2036 void approximate_distinct_tuples(uint8_t* hll_buffer_all_cpus,
2037  const uint32_t b,
2038  const size_t padded_size_bytes,
2039  const std::vector<JoinColumn>& join_column_per_key,
2040  const std::vector<JoinColumnTypeInfo>& type_info_per_key,
2041  const int thread_count) {
2042  CHECK_EQ(join_column_per_key.size(), type_info_per_key.size());
2043  CHECK(!join_column_per_key.empty());
2044 
2045  std::vector<std::future<void>> approx_distinct_threads;
2046  for (int thread_idx = 0; thread_idx < thread_count; ++thread_idx) {
2047  approx_distinct_threads.push_back(std::async(
2048  std::launch::async,
2049  [&join_column_per_key,
2050  &type_info_per_key,
2051  b,
2052  hll_buffer_all_cpus,
2053  padded_size_bytes,
2054  thread_idx,
2055  thread_count] {
2056  auto hll_buffer = hll_buffer_all_cpus + thread_idx * padded_size_bytes;
2057 
2058  const auto key_handler = GenericKeyHandler(join_column_per_key.size(),
2059  false,
2060  &join_column_per_key[0],
2061  &type_info_per_key[0],
2062  nullptr,
2063  nullptr);
2065  nullptr,
2066  b,
2067  join_column_per_key[0].num_elems,
2068  &key_handler,
2069  thread_idx,
2070  thread_count);
2071  }));
2072  }
2073  for (auto& child : approx_distinct_threads) {
2074  child.get();
2075  }
2076 }
2077 
2079  uint8_t* hll_buffer_all_cpus,
2080  std::vector<int32_t>& row_counts,
2081  const uint32_t b,
2082  const size_t padded_size_bytes,
2083  const std::vector<JoinColumn>& join_column_per_key,
2084  const std::vector<JoinColumnTypeInfo>& type_info_per_key,
2085  const std::vector<JoinBucketInfo>& join_buckets_per_key,
2086  const int thread_count) {
2087  CHECK_EQ(join_column_per_key.size(), join_buckets_per_key.size());
2088  CHECK_EQ(join_column_per_key.size(), type_info_per_key.size());
2089  CHECK(!join_column_per_key.empty());
2090 
2091  std::vector<std::future<void>> approx_distinct_threads;
2092  for (int thread_idx = 0; thread_idx < thread_count; ++thread_idx) {
2093  approx_distinct_threads.push_back(std::async(
2094  std::launch::async,
2095  [&join_column_per_key,
2096  &join_buckets_per_key,
2097  &row_counts,
2098  b,
2099  hll_buffer_all_cpus,
2100  padded_size_bytes,
2101  thread_idx,
2102  thread_count] {
2103  auto hll_buffer = hll_buffer_all_cpus + thread_idx * padded_size_bytes;
2104 
2105  const auto key_handler = OverlapsKeyHandler(
2106  join_buckets_per_key[0].inverse_bucket_sizes_for_dimension.size(),
2107  &join_column_per_key[0],
2108  join_buckets_per_key[0].inverse_bucket_sizes_for_dimension.data());
2110  row_counts.data(),
2111  b,
2112  join_column_per_key[0].num_elems,
2113  &key_handler,
2114  thread_idx,
2115  thread_count);
2116  }));
2117  }
2118  for (auto& child : approx_distinct_threads) {
2119  child.get();
2120  }
2121 
2123  row_counts.begin(), row_counts.end(), row_counts.begin(), thread_count);
2124 }
2125 
2126 void compute_bucket_sizes_on_cpu(std::vector<double>& bucket_sizes_for_dimension,
2127  const JoinColumn& join_column,
2128  const JoinColumnTypeInfo& type_info,
2129  const std::vector<double>& bucket_size_thresholds,
2130  const int thread_count) {
2131  std::vector<std::vector<double>> bucket_sizes_for_threads;
2132  for (int thread_idx = 0; thread_idx < thread_count; ++thread_idx) {
2133  bucket_sizes_for_threads.emplace_back(bucket_sizes_for_dimension.size(), 0.0);
2134  }
2135  std::vector<std::future<void>> threads;
2136  for (int thread_idx = 0; thread_idx < thread_count; ++thread_idx) {
2137  threads.push_back(std::async(std::launch::async,
2138  compute_bucket_sizes_impl<2>,
2139  bucket_sizes_for_threads[thread_idx].data(),
2140  &join_column,
2141  &type_info,
2142  bucket_size_thresholds.data(),
2143  thread_idx,
2144  thread_count));
2145  }
2146  for (auto& child : threads) {
2147  child.get();
2148  }
2149 
2150  for (int thread_idx = 0; thread_idx < thread_count; ++thread_idx) {
2151  for (size_t i = 0; i < bucket_sizes_for_dimension.size(); i++) {
2152  if (bucket_sizes_for_threads[thread_idx][i] > bucket_sizes_for_dimension[i]) {
2153  bucket_sizes_for_dimension[i] = bucket_sizes_for_threads[thread_idx][i];
2154  }
2155  }
2156  }
2157 }
2158 
2159 #endif // ifndef __CUDACC__
GLOBAL void SUFFIX() count_matches_bucketized(int32_t *count_buff, const int32_t invalid_slot_val, const JoinColumn join_column, const JoinColumnTypeInfo type_info, const void *sd_inner_proxy, const void *sd_outer_proxy, const int32_t cpu_thread_idx, const int32_t cpu_thread_count, const int64_t bucket_normalization)
const size_t num_shards
DEVICE int fill_hash_join_buff_sharded_impl(int32_t *buff, const int32_t invalid_slot_val, const JoinColumn join_column, const JoinColumnTypeInfo type_info, const ShardInfo shard_info, const void *sd_inner_proxy, const void *sd_outer_proxy, const int32_t cpu_thread_idx, const int32_t cpu_thread_count, HASHTABLE_FILLING_FUNC filling_func)
GLOBAL void SUFFIX() fill_row_ids_sharded(int32_t *buff, const int64_t hash_entry_count, const int32_t invalid_slot_val, const JoinColumn join_column, const JoinColumnTypeInfo type_info, const ShardInfo shard_info, const void *sd_inner_proxy, const void *sd_outer_proxy, const int32_t cpu_thread_idx, const int32_t cpu_thread_count)
#define CHECK_EQ(x, y)
Definition: Logger.h:214
T * get_matching_baseline_hash_slot_at(int8_t *hash_buff, const uint32_t h, const T *key, const size_t key_component_count, const int64_t hash_entry_size)
bool keys_are_equal(const T *key1, const T *key2, const size_t key_component_count)
DEVICE auto fill_hash_join_buff_impl(int32_t *buff, const int32_t invalid_slot_val, const JoinColumn join_column, const JoinColumnTypeInfo type_info, const void *sd_inner_proxy, const void *sd_outer_proxy, const int32_t cpu_thread_idx, const int32_t cpu_thread_count, HASHTABLE_FILLING_FUNC filling_func)
void fill_one_to_many_hash_table_sharded(int32_t *buff, const int64_t hash_entry_count, const int32_t invalid_slot_val, const JoinColumn &join_column, const JoinColumnTypeInfo &type_info, const ShardInfo &shard_info, const void *sd_inner_proxy, const void *sd_outer_proxy, const unsigned cpu_thread_count)
void init_baseline_hash_join_buff_32(int8_t *hash_join_buff, const int64_t entry_count, const size_t key_component_count, const bool with_val_slot, const int32_t invalid_slot_val, const int32_t cpu_thread_idx, const int32_t cpu_thread_count)
void fill_one_to_many_baseline_hash_table_64(int32_t *buff, const int64_t *composite_key_dict, const int64_t hash_entry_count, const int32_t invalid_slot_val, const size_t key_component_count, const std::vector< JoinColumn > &join_column_per_key, const std::vector< JoinColumnTypeInfo > &type_info_per_key, const std::vector< JoinBucketInfo > &join_bucket_info, const std::vector< const void * > &sd_inner_proxy_per_key, const std::vector< const void * > &sd_outer_proxy_per_key, const int32_t cpu_thread_count)
ALWAYS_INLINE DEVICE int32_t *SUFFIX() get_bucketized_hash_slot(int32_t *buff, const int64_t key, const int64_t min_key, const int64_t bucket_normalization)
Definition: JoinHashImpl.h:66
FORCE_INLINE uint8_t get_rank(uint64_t x, uint32_t b)
void fill_one_to_many_baseline_hash_table(int32_t *buff, const T *composite_key_dict, const int64_t hash_entry_count, const int32_t invalid_slot_val, const size_t key_component_count, const std::vector< JoinColumn > &join_column_per_key, const std::vector< JoinColumnTypeInfo > &type_info_per_key, const std::vector< JoinBucketInfo > &join_buckets_per_key, const std::vector< const void * > &sd_inner_proxy_per_key, const std::vector< const void * > &sd_outer_proxy_per_key, const size_t cpu_thread_count)
ALWAYS_INLINE DEVICE int32_t *SUFFIX() get_bucketized_hash_slot_sharded_opt(int32_t *buff, const int64_t key, const int64_t min_key, const uint32_t entry_count_per_shard, const uint32_t shard, const uint32_t num_shards, const uint32_t device_count, const int64_t bucket_normalization)
Definition: JoinHashImpl.h:109
__device__ double atomicMin(double *address, double val)
#define GLOBAL
#define CHECK_GE(x, y)
Definition: Logger.h:219
#define SUFFIX(name)
GLOBAL void SUFFIX() fill_row_ids_baseline(int32_t *buff, const T *composite_key_dict, const int64_t hash_entry_count, const int32_t invalid_slot_val, const KEY_HANDLER *f, const int64_t num_elems, const int32_t cpu_thread_idx, const int32_t cpu_thread_count)
std::string getString(int32_t string_id) const
int64_t translate_str_id_to_outer_dict(const int64_t elem, const int64_t min_elem, const int64_t max_elem, const void *sd_inner_proxy, const void *sd_outer_proxy)
ALWAYS_INLINE DEVICE int SUFFIX() fill_hashtable_for_semi_join(size_t idx, int32_t *entry_ptr, const int32_t invalid_slot_val)
Definition: JoinHashImpl.h:54
const int64_t null_val
void init_baseline_hash_join_buff_64(int8_t *hash_join_buff, const int64_t entry_count, const size_t key_component_count, const bool with_val_slot, const int32_t invalid_slot_val, const int32_t cpu_thread_idx, const int32_t cpu_thread_count)
GLOBAL void SUFFIX() count_matches_sharded(int32_t *count_buff, const int32_t invalid_slot_val, const JoinColumn join_column, const JoinColumnTypeInfo type_info, const ShardInfo shard_info, const void *sd_inner_proxy, const void *sd_outer_proxy, const int32_t cpu_thread_idx, const int32_t cpu_thread_count)
void inclusive_scan(InputIterator first, InputIterator last, OutputIterator out, const size_t thread_count)
DEVICE void SUFFIX() init_baseline_hash_join_buff(int8_t *hash_buff, const int64_t entry_count, const size_t key_component_count, const bool with_val_slot, const int32_t invalid_slot_val, const int32_t cpu_thread_idx, const int32_t cpu_thread_count)
void fill_one_to_many_hash_table_sharded_impl(int32_t *buff, const int64_t hash_entry_count, const int32_t invalid_slot_val, const JoinColumn &join_column, const JoinColumnTypeInfo &type_info, const ShardInfo &shard_info, const void *sd_inner_proxy, const void *sd_outer_proxy, const unsigned cpu_thread_count, COUNT_MATCHES_LAUNCH_FUNCTOR count_matches_launcher, FILL_ROW_IDS_LAUNCH_FUNCTOR fill_row_ids_launcher)
#define CHECK_GT(x, y)
Definition: Logger.h:218
#define load_cst(ptr)
FORCE_INLINE DEVICE uint32_t MurmurHash1Impl(const void *key, int len, const uint32_t seed)
Definition: MurmurHash1Inl.h:6
#define DEVICE
const int64_t translated_null_val
ALWAYS_INLINE DEVICE int32_t *SUFFIX() get_hash_slot(int32_t *buff, const int64_t key, const int64_t min_key)
Definition: JoinHashImpl.h:74
void fill_one_to_many_hash_table_bucketized(int32_t *buff, const HashEntryInfo hash_entry_info, const int32_t invalid_slot_val, const JoinColumn &join_column, const JoinColumnTypeInfo &type_info, const void *sd_inner_proxy, const void *sd_outer_proxy, const unsigned cpu_thread_count)
void approximate_distinct_tuples(uint8_t *hll_buffer_all_cpus, const uint32_t b, const size_t padded_size_bytes, const std::vector< JoinColumn > &join_column_per_key, const std::vector< JoinColumnTypeInfo > &type_info_per_key, const int thread_count)
DEVICE NEVER_INLINE const T *SUFFIX() get_matching_baseline_hash_slot_readonly(const T *key, const size_t key_component_count, const T *composite_key_dict, const int64_t entry_count, const size_t key_size_in_bytes)
ALWAYS_INLINE DEVICE int32_t *SUFFIX() get_bucketized_hash_slot_sharded(int32_t *buff, const int64_t key, const int64_t min_key, const uint32_t entry_count_per_shard, const uint32_t num_shards, const uint32_t device_count, const int64_t bucket_normalization)
Definition: JoinHashImpl.h:80
static constexpr int32_t INVALID_STR_ID
Iterates over the rows of a JoinColumn across multiple fragments/chunks.
DEVICE T SUFFIX() get_invalid_key()
#define CHECK_NE(x, y)
Definition: Logger.h:215
#define mapd_cas(address, compare, val)
void compute_bucket_sizes_on_cpu(std::vector< double > &bucket_sizes_for_dimension, const JoinColumn &join_column, const JoinColumnTypeInfo &type_info, const std::vector< double > &bucket_size_thresholds, const int thread_count)
void fill_one_to_many_hash_table_impl(int32_t *buff, const int64_t hash_entry_count, const int32_t invalid_slot_val, const JoinColumn &join_column, const JoinColumnTypeInfo &type_info, const void *sd_inner_proxy, const void *sd_outer_proxy, const unsigned cpu_thread_count, COUNT_MATCHES_LAUNCH_FUNCTOR count_matches_func, FILL_ROW_IDS_LAUNCH_FUNCTOR fill_row_ids_func)
DEVICE void partial_sum(ARGS &&...args)
Definition: gpu_enabled.h:87
int64_t bucket_normalization
#define cas_cst(ptr, expected, desired)
GLOBAL void SUFFIX() approximate_distinct_tuples_impl(uint8_t *hll_buffer, int32_t *row_count_buffer, const uint32_t b, const int64_t num_elems, const KEY_HANDLER *f, const int32_t cpu_thread_idx, const int32_t cpu_thread_count)
int fill_baseline_hash_join_buff_64(int8_t *hash_buff, const int64_t entry_count, const int32_t invalid_slot_val, const bool for_semi_join, const size_t key_component_count, const bool with_val_slot, const GenericKeyHandler *key_handler, const int64_t num_elems, const int32_t cpu_thread_idx, const int32_t cpu_thread_count)
const size_t shard
size_t hash_entry_count
const int64_t max_val
#define UNLIKELY(x)
Definition: likely.h:25
int fill_baseline_hash_join_buff(int8_t *hash_buff, const size_t entry_count, const int32_t invalid_slot_val, const bool for_semi_join, const size_t key_component_count, const bool with_val_slot, const KEY_HANDLER *key_handler, const size_t num_elems, const int32_t cpu_thread_idx, const int32_t cpu_thread_count)
void fill_one_to_many_baseline_hash_table_32(int32_t *buff, const int32_t *composite_key_dict, const int64_t hash_entry_count, const int32_t invalid_slot_val, const size_t key_component_count, const std::vector< JoinColumn > &join_column_per_key, const std::vector< JoinColumnTypeInfo > &type_info_per_key, const std::vector< JoinBucketInfo > &join_bucket_info, const std::vector< const void * > &sd_inner_proxy_per_key, const std::vector< const void * > &sd_outer_proxy_per_key, const int32_t cpu_thread_count)
void approximate_distinct_tuples_overlaps(uint8_t *hll_buffer_all_cpus, std::vector< int32_t > &row_counts, const uint32_t b, const size_t padded_size_bytes, const std::vector< JoinColumn > &join_column_per_key, const std::vector< JoinColumnTypeInfo > &type_info_per_key, const std::vector< JoinBucketInfo > &join_buckets_per_key, const int thread_count)
void fill_one_to_many_hash_table(int32_t *buff, const HashEntryInfo hash_entry_info, const int32_t invalid_slot_val, const JoinColumn &join_column, const JoinColumnTypeInfo &type_info, const void *sd_inner_proxy, const void *sd_outer_proxy, const unsigned cpu_thread_count)
GLOBAL void SUFFIX() count_matches(int32_t *count_buff, const int32_t invalid_slot_val, const JoinColumn join_column, const JoinColumnTypeInfo type_info, const void *sd_inner_proxy, const void *sd_outer_proxy, const int32_t cpu_thread_idx, const int32_t cpu_thread_count)
ALWAYS_INLINE DEVICE int32_t *SUFFIX() get_hash_slot_sharded_opt(int32_t *buff, const int64_t key, const int64_t min_key, const uint32_t entry_count_per_shard, const uint32_t shard, const uint32_t num_shards, const uint32_t device_count)
Definition: JoinHashImpl.h:124
DEVICE int SUFFIX() fill_hash_join_buff_sharded_bucketized(int32_t *buff, const int32_t invalid_slot_val, const bool for_semi_join, const JoinColumn join_column, const JoinColumnTypeInfo type_info, const ShardInfo shard_info, const void *sd_inner_proxy, const void *sd_outer_proxy, const int32_t cpu_thread_idx, const int32_t cpu_thread_count, const int64_t bucket_normalization)
int overlaps_fill_baseline_hash_join_buff_64(int8_t *hash_buff, const int64_t entry_count, const int32_t invalid_slot_val, const size_t key_component_count, const bool with_val_slot, const OverlapsKeyHandler *key_handler, const int64_t num_elems, const int32_t cpu_thread_idx, const int32_t cpu_thread_count)
GLOBAL void SUFFIX() fill_row_ids_bucketized(int32_t *buff, const int64_t hash_entry_count, const int32_t invalid_slot_val, const JoinColumn join_column, const JoinColumnTypeInfo type_info, const void *sd_inner_proxy, const void *sd_outer_proxy, const int32_t cpu_thread_idx, const int32_t cpu_thread_count, const int64_t bucket_normalization)
DEVICE void fill_row_ids_sharded_impl(int32_t *buff, const int64_t hash_entry_count, const int32_t invalid_slot_val, const JoinColumn join_column, const JoinColumnTypeInfo type_info, const ShardInfo shard_info, const void *sd_inner_proxy, const void *sd_outer_proxy, const int32_t cpu_thread_idx, const int32_t cpu_thread_count, SLOT_SELECTOR slot_selector)
GLOBAL void SUFFIX() fill_row_ids(int32_t *buff, const int64_t hash_entry_count, const int32_t invalid_slot_val, const JoinColumn join_column, const JoinColumnTypeInfo type_info, const void *sd_inner_proxy, const void *sd_outer_proxy, const int32_t cpu_thread_idx, const int32_t cpu_thread_count)
DEVICE void SUFFIX() init_hash_join_buff(int32_t *groups_buffer, const int64_t hash_entry_count, const int32_t invalid_slot_val, const int32_t cpu_thread_idx, const int32_t cpu_thread_count)
const int64_t min_val
ALWAYS_INLINE DEVICE int32_t *SUFFIX() get_hash_slot_sharded(int32_t *buff, const int64_t key, const int64_t min_key, const uint32_t entry_count_per_shard, const uint32_t num_shards, const uint32_t device_count)
Definition: JoinHashImpl.h:95
DEVICE int write_baseline_hash_slot(const int32_t val, int8_t *hash_buff, const int64_t entry_count, const T *key, const size_t key_component_count, const bool with_val_slot, const int32_t invalid_slot_val, const size_t key_size_in_bytes, const size_t hash_entry_size)
int overlaps_fill_baseline_hash_join_buff_32(int8_t *hash_buff, const int64_t entry_count, const int32_t invalid_slot_val, const size_t key_component_count, const bool with_val_slot, const OverlapsKeyHandler *key_handler, const int64_t num_elems, const int32_t cpu_thread_idx, const int32_t cpu_thread_count)
DEVICE void count_matches_impl(int32_t *count_buff, const int32_t invalid_slot_val, const JoinColumn join_column, const JoinColumnTypeInfo type_info, const void *sd_inner_proxy, const void *sd_outer_proxy, const int32_t cpu_thread_idx, const int32_t cpu_thread_count, SLOT_SELECTOR slot_selector)
#define NEVER_INLINE
size_t getNormalizedHashEntryCount() const
#define store_cst(ptr, val)
#define CHECK(condition)
Definition: Logger.h:206
ALWAYS_INLINE DEVICE int SUFFIX() fill_one_to_one_hashtable(size_t idx, int32_t *entry_ptr, const int32_t invalid_slot_val)
Definition: JoinHashImpl.h:44
DEVICE NEVER_INLINE double SUFFIX() fixed_width_double_decode_noinline(const int8_t *byte_stream, const int64_t pos)
Definition: DecodersImpl.h:134
char * f
DEVICE int write_baseline_hash_slot_for_semi_join(const int32_t val, int8_t *hash_buff, const int64_t entry_count, const T *key, const size_t key_component_count, const bool with_val_slot, const int32_t invalid_slot_val, const size_t key_size_in_bytes, const size_t hash_entry_size)
DEVICE int SUFFIX() fill_hash_join_buff_sharded(int32_t *buff, const int32_t invalid_slot_val, const bool for_semi_join, const JoinColumn join_column, const JoinColumnTypeInfo type_info, const ShardInfo shard_info, const void *sd_inner_proxy, const void *sd_outer_proxy, const int32_t cpu_thread_idx, const int32_t cpu_thread_count)
__device__ double atomicMax(double *address, double val)
DEVICE FORCE_INLINE const int8_t * ptr() const
#define mapd_add(address, val)
GLOBAL void SUFFIX() count_matches_baseline(int32_t *count_buff, const T *composite_key_dict, const int64_t entry_count, const KEY_HANDLER *f, const int64_t num_elems, const int32_t cpu_thread_idx, const int32_t cpu_thread_count)
int fill_baseline_hash_join_buff_32(int8_t *hash_buff, const int64_t entry_count, const int32_t invalid_slot_val, const bool for_semi_join, const size_t key_component_count, const bool with_val_slot, const GenericKeyHandler *key_handler, const int64_t num_elems, const int32_t cpu_thread_idx, const int32_t cpu_thread_count)
GLOBAL void SUFFIX() fill_row_ids_sharded_bucketized(int32_t *buff, const int64_t hash_entry_count, const int32_t invalid_slot_val, const JoinColumn join_column, const JoinColumnTypeInfo type_info, const ShardInfo shard_info, const void *sd_inner_proxy, const void *sd_outer_proxy, const int32_t cpu_thread_idx, const int32_t cpu_thread_count, const int64_t bucket_normalization)
GLOBAL void SUFFIX() compute_bucket_sizes_impl(double *bucket_sizes_for_thread, const JoinColumn *join_column, const JoinColumnTypeInfo *type_info, const double *bucket_size_thresholds, const int32_t cpu_thread_idx, const int32_t cpu_thread_count)
const size_t g_maximum_conditions_to_coalesce
#define SHARD_FOR_KEY(key, num_shards)
Definition: shard_key.h:20
FORCE_INLINE DEVICE uint64_t MurmurHash64AImpl(const void *key, int len, uint64_t seed)
DEVICE int SUFFIX() fill_hash_join_buff_bucketized(int32_t *buff, const int32_t invalid_slot_val, const bool for_semi_join, const JoinColumn join_column, const JoinColumnTypeInfo type_info, const void *sd_inner_proxy, const void *sd_outer_proxy, const int32_t cpu_thread_idx, const int32_t cpu_thread_count, const int64_t bucket_normalization)
DEVICE int SUFFIX() fill_hash_join_buff(int32_t *buff, const int32_t invalid_slot_val, const bool for_semi_join, const JoinColumn join_column, const JoinColumnTypeInfo type_info, const void *sd_inner_proxy, const void *sd_outer_proxy, const int32_t cpu_thread_idx, const int32_t cpu_thread_count)
DEVICE void fill_row_ids_impl(int32_t *buff, const int64_t hash_entry_count, const int32_t invalid_slot_val, const JoinColumn join_column, const JoinColumnTypeInfo type_info, const void *sd_inner_proxy, const void *sd_outer_proxy, const int32_t cpu_thread_idx, const int32_t cpu_thread_count, SLOT_SELECTOR slot_selector)