-
Notifications
You must be signed in to change notification settings - Fork 89
/
static_map.cuh
2132 lines (2001 loc) · 91.4 KB
/
static_map.cuh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2020-2024, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <cuco/cuda_stream_ref.hpp>
#include <cuco/detail/__config>
#include <cuco/detail/open_addressing/open_addressing_impl.cuh>
#include <cuco/detail/static_map_kernels.cuh>
#include <cuco/hash_functions.cuh>
#include <cuco/pair.cuh>
#include <cuco/sentinel.cuh>
#include <cuco/static_map_ref.cuh>
#include <cuco/utility/allocator.hpp>
#include <cuco/utility/cuda_thread_scope.cuh>
#include <cuco/utility/traits.hpp>
#include <thrust/functional.h>
#include <cuda/std/atomic>
#if defined(CUCO_HAS_CUDA_BARRIER)
#include <cuda/barrier>
#endif
#include <cstddef>
#include <memory>
#include <utility>
namespace cuco {
/**
* @brief A GPU-accelerated, unordered, associative container of key-value pairs with unique keys.
*
* The `static_map` supports two types of operations:
* - Host-side "bulk" operations
* - Device-side "singular" operations
*
* The host-side bulk operations include `insert`, `contains`, etc. These APIs should be used when
* there are a large number of keys to modify or lookup. For example, given a range of keys
* specified by device-accessible iterators, the bulk `insert` function will insert all keys into
* the map.
*
* The singular device-side operations allow individual threads (or cooperative groups) to perform
* independent modify or lookup operations from device code. These operations are accessed through
* non-owning, trivially copyable reference types (or "ref"). User can combine any arbitrary
* operators (see options in `include/cuco/operator.hpp`) when creating the ref. Concurrent modify
* and lookup will be supported if both kinds of operators are specified during the ref
* construction.
*
* @note Allows constant time concurrent modify or lookup operations from threads in device code.
* @note cuCollections data structures always place the slot keys on the left-hand side when
* invoking the key comparison predicate, i.e., `pred(slot_key, query_key)`. Order-sensitive
* `KeyEqual` should be used with caution.
* @note `ProbingScheme::cg_size` indicates how many threads are used to handle one independent
* device operation. `cg_size == 1` uses the scalar (or non-CG) code paths.
*
* @throw If the size of the given key type is larger than 8 bytes
* @throw If the size of the given payload type is larger than 8 bytes
* @throw If the size of the given slot type is larger than 16 bytes
* @throw If the given key type doesn't have unique object representations, i.e.,
* `cuco::bitwise_comparable_v<Key> == false`
* @throw If the given mapped type doesn't have unique object representations, i.e.,
* `cuco::bitwise_comparable_v<T> == false`
* @throw If the probing scheme type is not inherited from `cuco::detail::probing_scheme_base`
*
* @tparam Key Type used for keys. Requires `cuco::is_bitwise_comparable_v<Key>`
* @tparam T Type of the mapped values
* @tparam Extent Data structure size type
* @tparam Scope The scope in which operations will be performed by individual threads.
* @tparam KeyEqual Binary callable type used to compare two keys for equality
* @tparam ProbingScheme Probing scheme (see `include/cuco/probing_scheme.cuh` for choices)
* @tparam Allocator Type of allocator used for device storage
* @tparam Storage Slot window storage type
*/
template <class Key,
class T,
class Extent = cuco::extent<std::size_t>,
cuda::thread_scope Scope = cuda::thread_scope_device,
class KeyEqual = thrust::equal_to<Key>,
class ProbingScheme = cuco::linear_probing<4, // CG size
cuco::default_hash_function<Key>>,
class Allocator = cuco::cuda_allocator<cuco::pair<Key, T>>,
class Storage = cuco::storage<1>>
class static_map {
static_assert(sizeof(Key) <= 8, "Container does not support key types larger than 8 bytes.");
static_assert(sizeof(T) <= 8, "Container does not support payload types larger than 8 bytes.");
static_assert(cuco::is_bitwise_comparable_v<T>,
"Mapped type must have unique object representations or have been explicitly "
"declared as safe for bitwise comparison via specialization of "
"cuco::is_bitwise_comparable_v<T>.");
using impl_type = detail::open_addressing_impl<Key,
cuco::pair<Key, T>,
Extent,
Scope,
KeyEqual,
ProbingScheme,
Allocator,
Storage>;
public:
static constexpr auto cg_size = impl_type::cg_size; ///< CG size used for probing
static constexpr auto window_size = impl_type::window_size; ///< Window size used for probing
static constexpr auto thread_scope = impl_type::thread_scope; ///< CUDA thread scope
using key_type = typename impl_type::key_type; ///< Key type
using value_type = typename impl_type::value_type; ///< Key-value pair type
using extent_type = typename impl_type::extent_type; ///< Extent type
using size_type = typename impl_type::size_type; ///< Size type
using key_equal = typename impl_type::key_equal; ///< Key equality comparator type
using allocator_type = typename impl_type::allocator_type; ///< Allocator type
/// Non-owning window storage ref type
using storage_ref_type = typename impl_type::storage_ref_type;
using probing_scheme_type = typename impl_type::probing_scheme_type; ///< Probing scheme type
using mapped_type = T; ///< Payload type
template <typename... Operators>
using ref_type = cuco::static_map_ref<key_type,
mapped_type,
thread_scope,
key_equal,
probing_scheme_type,
storage_ref_type,
Operators...>; ///< Non-owning container ref type
static_map(static_map const&) = delete;
static_map& operator=(static_map const&) = delete;
static_map(static_map&&) = default; ///< Move constructor
/**
* @brief Replaces the contents of the container with another container.
*
* @return Reference of the current map object
*/
static_map& operator=(static_map&&) = default;
~static_map() = default;
/**
* @brief Constructs a statically-sized map with the specified initial capacity, sentinel values
* and CUDA stream
*
* The actual map capacity depends on the given `capacity`, the probing scheme, CG size, and the
* window size and it is computed via the `make_window_extent` factory. Insert operations will not
* automatically grow the map. Attempting to insert more unique keys than the capacity of the map
* results in undefined behavior.
*
* @note Any `*_sentinel`s are reserved and behavior is undefined when attempting to insert
* this sentinel value.
* @note This constructor doesn't synchronize the given stream.
*
* @param capacity The requested lower-bound map size
* @param empty_key_sentinel The reserved key value for empty slots
* @param empty_value_sentinel The reserved mapped value for empty slots
* @param pred Key equality binary predicate
* @param probing_scheme Probing scheme
* @param scope The scope in which operations will be performed
* @param storage Kind of storage to use
* @param alloc Allocator used for allocating device storage
* @param stream CUDA stream used to initialize the map
*/
constexpr static_map(Extent capacity,
empty_key<Key> empty_key_sentinel,
empty_value<T> empty_value_sentinel,
KeyEqual const& pred = {},
ProbingScheme const& probing_scheme = {},
cuda_thread_scope<Scope> scope = {},
Storage storage = {},
Allocator const& alloc = {},
cuda_stream_ref stream = {});
/**
* @brief Constructs a statically-sized map with the number of elements to insert `n`, the desired
* load factor, etc
*
* @note This constructor helps users create a map based on the number of elements to insert and
* the desired load factor without manually computing the desired capacity. The actual map
* capacity will be a size no smaller than `ceil(n / desired_load_factor)`. It's determined by
* multiple factors including the given `n`, the desired load factor, the probing scheme, the CG
* size, and the window size and is computed via the `make_window_extent` factory.
* @note Insert operations will not automatically grow the container.
* @note Attempting to insert more unique keys than the capacity of the container results in
* undefined behavior.
* @note Any `*_sentinel`s are reserved and behavior is undefined when attempting to insert
* this sentinel value.
* @note This constructor doesn't synchronize the given stream.
* @note This overload will convert compile-time extents to runtime constants which might lead to
* performance regressions.
*
* @throw If the desired occupancy is no bigger than zero
* @throw If the desired occupancy is no smaller than one
*
* @param n The number of elements to insert
* @param desired_load_factor The desired load factor of the container, e.g., 0.5 implies a 50%
* load factor
* @param empty_key_sentinel The reserved key value for empty slots
* @param empty_value_sentinel The reserved mapped value for empty slots
* @param pred Key equality binary predicate
* @param probing_scheme Probing scheme
* @param scope The scope in which operations will be performed
* @param storage Kind of storage to use
* @param alloc Allocator used for allocating device storage
* @param stream CUDA stream used to initialize the map
*/
constexpr static_map(Extent n,
double desired_load_factor,
empty_key<Key> empty_key_sentinel,
empty_value<T> empty_value_sentinel,
KeyEqual const& pred = {},
ProbingScheme const& probing_scheme = {},
cuda_thread_scope<Scope> scope = {},
Storage storage = {},
Allocator const& alloc = {},
cuda_stream_ref stream = {});
/**
* @brief Constructs a statically-sized map with the specified initial capacity, sentinel values
* and CUDA stream.
*
* The actual map capacity depends on the given `capacity`, the probing scheme, CG size, and the
* window size and it is computed via the `make_window_extent` factory. Insert operations will not
* automatically grow the map. Attempting to insert more unique keys than the capacity of the map
* results in undefined behavior.
*
* @note Any `*_sentinel`s are reserved and behavior is undefined when attempting to insert
* this sentinel value.
* @note If a non-default CUDA stream is provided, the caller is responsible for synchronizing the
* stream before the object is first used.
*
* @param capacity The requested lower-bound map size
* @param empty_key_sentinel The reserved key value for empty slots
* @param empty_value_sentinel The reserved mapped value for empty slots
* @param erased_key_sentinel The reserved key to denote erased slots
* @param pred Key equality binary predicate
* @param probing_scheme Probing scheme
* @param scope The scope in which operations will be performed
* @param storage Kind of storage to use
* @param alloc Allocator used for allocating device storage
* @param stream CUDA stream used to initialize the map
*/
constexpr static_map(Extent capacity,
empty_key<Key> empty_key_sentinel,
empty_value<T> empty_value_sentinel,
erased_key<Key> erased_key_sentinel,
KeyEqual const& pred = {},
ProbingScheme const& probing_scheme = {},
cuda_thread_scope<Scope> scope = {},
Storage storage = {},
Allocator const& alloc = {},
cuda_stream_ref stream = {});
/**
* @brief Erases all elements from the container. After this call, `size()` returns zero.
* Invalidates any references, pointers, or iterators referring to contained elements.
*
* @param stream CUDA stream this operation is executed in
*/
void clear(cuda_stream_ref stream = {}) noexcept;
/**
* @brief Asynchronously erases all elements from the container. After this call, `size()` returns
* zero. Invalidates any references, pointers, or iterators referring to contained elements.
*
* @param stream CUDA stream this operation is executed in
*/
void clear_async(cuda_stream_ref stream = {}) noexcept;
/**
* @brief Inserts all keys in the range `[first, last)` and returns the number of successful
* insertions.
*
* @note This function synchronizes the given stream. For asynchronous execution use
* `insert_async`.
*
* @tparam InputIt Device accessible random access input iterator where
* <tt>std::is_convertible<std::iterator_traits<InputIt>::value_type,
* static_map<K, V>::value_type></tt> is `true`
*
* @param first Beginning of the sequence of keys
* @param last End of the sequence of keys
* @param stream CUDA stream used for insert
*
* @return Number of successful insertions
*/
template <typename InputIt>
size_type insert(InputIt first, InputIt last, cuda_stream_ref stream = {});
/**
* @brief Asynchronously inserts all keys in the range `[first, last)`.
*
* @tparam InputIt Device accessible random access input iterator where
* <tt>std::is_convertible<std::iterator_traits<InputIt>::value_type,
* static_map<K, V>::value_type></tt> is `true`
*
* @param first Beginning of the sequence of keys
* @param last End of the sequence of keys
* @param stream CUDA stream used for insert
*/
template <typename InputIt>
void insert_async(InputIt first, InputIt last, cuda_stream_ref stream = {}) noexcept;
/**
* @brief Inserts keys in the range `[first, last)` if `pred` of the corresponding stencil returns
* true.
*
* @note The key `*(first + i)` is inserted if `pred( *(stencil + i) )` returns true.
* @note This function synchronizes the given stream and returns the number of successful
* insertions. For asynchronous execution use `insert_if_async`.
*
* @tparam InputIt Device accessible random access iterator whose `value_type` is
* convertible to the container's `value_type`
* @tparam StencilIt Device accessible random access iterator whose value_type is
* convertible to Predicate's argument type
* @tparam Predicate Unary predicate callable whose return type must be convertible to `bool` and
* argument type is convertible from <tt>std::iterator_traits<StencilIt>::value_type</tt>
*
* @param first Beginning of the sequence of key/value pairs
* @param last End of the sequence of key/value pairs
* @param stencil Beginning of the stencil sequence
* @param pred Predicate to test on every element in the range `[stencil, stencil +
* std::distance(first, last))`
* @param stream CUDA stream used for the operation
*
* @return Number of successful insertions
*/
template <typename InputIt, typename StencilIt, typename Predicate>
size_type insert_if(
InputIt first, InputIt last, StencilIt stencil, Predicate pred, cuda_stream_ref stream = {});
/**
* @brief Asynchronously inserts keys in the range `[first, last)` if `pred` of the corresponding
* stencil returns true.
*
* @note The key `*(first + i)` is inserted if `pred( *(stencil + i) )` returns true.
*
* @tparam InputIt Device accessible random access iterator whose `value_type` is
* convertible to the container's `value_type`
* @tparam StencilIt Device accessible random access iterator whose value_type is
* convertible to Predicate's argument type
* @tparam Predicate Unary predicate callable whose return type must be convertible to `bool` and
* argument type is convertible from <tt>std::iterator_traits<StencilIt>::value_type</tt>
*
* @param first Beginning of the sequence of key/value pairs
* @param last End of the sequence of key/value pairs
* @param stencil Beginning of the stencil sequence
* @param pred Predicate to test on every element in the range `[stencil, stencil +
* std::distance(first, last))`
* @param stream CUDA stream used for the operation
*/
template <typename InputIt, typename StencilIt, typename Predicate>
void insert_if_async(InputIt first,
InputIt last,
StencilIt stencil,
Predicate pred,
cuda_stream_ref stream = {}) noexcept;
/**
* @brief For any key-value pair `{k, v}` in the range `[first, last)`, if a key equivalent to `k`
* already exists in the container, assigns `v` to the mapped_type corresponding to the key `k`.
* If the key does not exist, inserts the pair as if by insert.
*
* @note This function synchronizes the given stream. For asynchronous execution use
* `insert_or_assign_async`.
* @note If multiple pairs in `[first, last)` compare equal, it is unspecified which pair is
* inserted or assigned.
*
* @tparam InputIt Device accessible random access input iterator where
* <tt>std::is_convertible<std::iterator_traits<InputIt>::value_type,
* static_map<K, V>::value_type></tt> is `true`
*
* @param first Beginning of the sequence of keys
* @param last End of the sequence of keys
* @param stream CUDA stream used for insert
*/
template <typename InputIt>
void insert_or_assign(InputIt first, InputIt last, cuda_stream_ref stream = {}) noexcept;
/**
* @brief For any key-value pair `{k, v}` in the range `[first, last)`, if a key equivalent to `k`
* already exists in the container, assigns `v` to the mapped_type corresponding to the key `k`.
* If the key does not exist, inserts the pair as if by insert.
*
* @note If multiple pairs in `[first, last)` compare equal, it is unspecified which pair is
* inserted or assigned.
*
* @tparam InputIt Device accessible random access input iterator where
* <tt>std::is_convertible<std::iterator_traits<InputIt>::value_type,
* static_map<K, V>::value_type></tt> is `true`
*
* @param first Beginning of the sequence of keys
* @param last End of the sequence of keys
* @param stream CUDA stream used for insert
*/
template <typename InputIt>
void insert_or_assign_async(InputIt first, InputIt last, cuda_stream_ref stream = {}) noexcept;
/**
* @brief Erases keys in the range `[first, last)`.
*
* @note For each key `k` in `[first, last)`, if contains(k) returns true, removes `k` and it's
* associated value from the map. Else, no effect.
*
* @note This function synchronizes `stream`.
*
* @note Side-effects:
* - `contains(k) == false`
* - `find(k) == end()`
* - `insert({k,v}) == true`
* - `size()` is reduced by the total number of erased keys
*
* @tparam InputIt Device accessible input iterator whose `value_type` is
* convertible to the map's `key_type`
*
* @param first Beginning of the sequence of keys
* @param last End of the sequence of keys
* @param stream Stream used for executing the kernels
*
* @throw std::runtime_error if a unique erased key sentinel value was not
* provided at construction
*/
template <typename InputIt>
void erase(InputIt first, InputIt last, cuda_stream_ref stream = {});
/**
* @brief Asynchronously erases keys in the range `[first, last)`.
*
* @note For each key `k` in `[first, last)`, if contains(k) returns true, removes `k` and it's
* associated value from the map. Else, no effect.
*
* @note Side-effects:
* - `contains(k) == false`
* - `find(k) == end()`
* - `insert({k,v}) == true`
* - `size()` is reduced by the total number of erased keys
*
* @tparam InputIt Device accessible input iterator whose `value_type` is
* convertible to the map's `key_type`
*
* @param first Beginning of the sequence of keys
* @param last End of the sequence of keys
* @param stream Stream used for executing the kernels
*
* @throw std::runtime_error if a unique erased key sentinel value was not
* provided at construction
*/
template <typename InputIt>
void erase_async(InputIt first, InputIt last, cuda_stream_ref stream = {});
/**
* @brief Indicates whether the keys in the range `[first, last)` are contained in the map.
*
* @note This function synchronizes the given stream. For asynchronous execution use
* `contains_async`.
*
* @tparam InputIt Device accessible input iterator
* @tparam OutputIt Device accessible output iterator assignable from `bool`
*
* @param first Beginning of the sequence of keys
* @param last End of the sequence of keys
* @param output_begin Beginning of the sequence of booleans for the presence of each key
* @param stream Stream used for executing the kernels
*/
template <typename InputIt, typename OutputIt>
void contains(InputIt first,
InputIt last,
OutputIt output_begin,
cuda_stream_ref stream = {}) const;
/**
* @brief Asynchronously indicates whether the keys in the range `[first, last)` are contained in
* the map.
*
* @tparam InputIt Device accessible input iterator
* @tparam OutputIt Device accessible output iterator assignable from `bool`
*
* @param first Beginning of the sequence of keys
* @param last End of the sequence of keys
* @param output_begin Beginning of the sequence of booleans for the presence of each key
* @param stream Stream used for executing the kernels
*/
template <typename InputIt, typename OutputIt>
void contains_async(InputIt first,
InputIt last,
OutputIt output_begin,
cuda_stream_ref stream = {}) const noexcept;
/**
* @brief Indicates whether the keys in the range `[first, last)` are contained in the map if
* `pred` of the corresponding stencil returns true.
*
* @note If `pred( *(stencil + i) )` is true, stores `true` or `false` to `(output_begin + i)`
* indicating if the key `*(first + i)` is present in the map. If `pred( *(stencil + i) )` is
* false, stores false to `(output_begin + i)`.
* @note This function synchronizes the given stream. For asynchronous execution use
* `contains_if_async`.
*
* @tparam InputIt Device accessible input iterator
* @tparam StencilIt Device accessible random access iterator whose value_type is
* convertible to Predicate's argument type
* @tparam Predicate Unary predicate callable whose return type must be convertible to `bool` and
* argument type is convertible from <tt>std::iterator_traits<StencilIt>::value_type</tt>
* @tparam OutputIt Device accessible output iterator assignable from `bool`
*
* @param first Beginning of the sequence of keys
* @param last End of the sequence of keys
* @param stencil Beginning of the stencil sequence
* @param pred Predicate to test on every element in the range `[stencil, stencil +
* std::distance(first, last))`
* @param output_begin Beginning of the sequence of booleans for the presence of each key
* @param stream Stream used for executing the kernels
*/
template <typename InputIt, typename StencilIt, typename Predicate, typename OutputIt>
void contains_if(InputIt first,
InputIt last,
StencilIt stencil,
Predicate pred,
OutputIt output_begin,
cuda_stream_ref stream = {}) const;
/**
* @brief Asynchronously indicates whether the keys in the range `[first, last)` are contained in
* the map if `pred` of the corresponding stencil returns true.
*
* @note If `pred( *(stencil + i) )` is true, stores `true` or `false` to `(output_begin + i)`
* indicating if the key `*(first + i)` is present in the map. If `pred( *(stencil + i) )` is
* false, stores false to `(output_begin + i)`.
*
* @tparam InputIt Device accessible input iterator
* @tparam StencilIt Device accessible random access iterator whose value_type is
* convertible to Predicate's argument type
* @tparam Predicate Unary predicate callable whose return type must be convertible to `bool` and
* argument type is convertible from <tt>std::iterator_traits<StencilIt>::value_type</tt>
* @tparam OutputIt Device accessible output iterator assignable from `bool`
*
* @param first Beginning of the sequence of keys
* @param last End of the sequence of keys
* @param stencil Beginning of the stencil sequence
* @param pred Predicate to test on every element in the range `[stencil, stencil +
* std::distance(first, last))`
* @param output_begin Beginning of the sequence of booleans for the presence of each key
* @param stream Stream used for executing the kernels
*/
template <typename InputIt, typename StencilIt, typename Predicate, typename OutputIt>
void contains_if_async(InputIt first,
InputIt last,
StencilIt stencil,
Predicate pred,
OutputIt output_begin,
cuda_stream_ref stream = {}) const noexcept;
/**
* @brief For all keys in the range `[first, last)`, finds a payload with its key equivalent to
* the query key.
*
* @note This function synchronizes the given stream. For asynchronous execution use `find_async`.
* @note If the key `*(first + i)` has a matched `element` in the map, copies the payload of
* `element` to
* `(output_begin + i)`. Else, copies the empty value sentinel.
*
* @tparam InputIt Device accessible input iterator
* @tparam OutputIt Device accessible output iterator assignable from the map's `mapped_type`
*
* @param first Beginning of the sequence of keys
* @param last End of the sequence of keys
* @param output_begin Beginning of the sequence of payloads retrieved for each key
* @param stream Stream used for executing the kernels
*/
template <typename InputIt, typename OutputIt>
void find(InputIt first, InputIt last, OutputIt output_begin, cuda_stream_ref stream = {}) const;
/**
* @brief For all keys in the range `[first, last)`, asynchronously finds a payload with its key
* equivalent to the query key.
*
* @note If the key `*(first + i)` has a matched `element` in the map, copies the payload of
* `element` to
* `(output_begin + i)`. Else, copies the empty value sentinel.
*
* @tparam InputIt Device accessible input iterator
* @tparam OutputIt Device accessible output iterator assignable from the map's `mapped_type`
*
* @param first Beginning of the sequence of keys
* @param last End of the sequence of keys
* @param output_begin Beginning of the sequence of payloads retrieved for each key
* @param stream Stream used for executing the kernels
*/
template <typename InputIt, typename OutputIt>
void find_async(InputIt first,
InputIt last,
OutputIt output_begin,
cuda_stream_ref stream = {}) const;
/**
* @brief Retrieves all of the keys and their associated values.
*
* @note This API synchronizes the given stream.
* @note The order in which keys are returned is implementation defined and not guaranteed to be
* consistent between subsequent calls to `retrieve_all`.
* @note Behavior is undefined if the range beginning at `keys_out` or `values_out` is smaller
* than the return value of `size()`.
*
* @tparam KeyOut Device accessible random access output iterator whose `value_type` is
* convertible from `key_type`.
* @tparam ValueOut Device accesible random access output iterator whose `value_type` is
* convertible from `mapped_type`.
*
* @param keys_out Beginning output iterator for keys
* @param values_out Beginning output iterator for associated values
* @param stream CUDA stream used for this operation
*
* @return Pair of iterators indicating the last elements in the output
*/
template <typename KeyOut, typename ValueOut>
std::pair<KeyOut, ValueOut> retrieve_all(KeyOut keys_out,
ValueOut values_out,
cuda_stream_ref stream = {}) const;
/**
* @brief Regenerates the container.
*
* @note This function synchronizes the given stream. For asynchronous execution use
* `rehash_async`.
*
* @param stream CUDA stream used for this operation
*/
void rehash(cuda_stream_ref stream = {});
/**
* @brief Reserves at least the specified number of slots and regenerates the container
*
* @note Changes the number of slots to a value that is not less than `capacity`, then
* rehashes the container, i.e. puts the elements into appropriate slots considering
* that the total number of slots has changed.
*
* @note This function synchronizes the given stream. For asynchronous execution use
* `rehash_async`.
*
* @note Behavior is undefined if the desired `capacity` is insufficient to store all of the
* contained elements.
*
* @note This function is not available if the conatiner's `extent_type` is static.
*
* @param capacity New capacity of the container
* @param stream CUDA stream used for this operation
*/
void rehash(size_type capacity, cuda_stream_ref stream = {});
/**
* @brief Asynchronously regenerates the container.
*
* @param stream CUDA stream used for this operation
*/
void rehash_async(cuda_stream_ref stream = {});
/**
* @brief Asynchronously reserves at least the specified number of slots and regenerates the
* container
*
* @note Changes the number of slots to a value that is not less than `capacity`, then
* rehashes the container, i.e. puts the elements into appropriate slots considering
* that the total number of slots has changed.
*
* @note Behavior is undefined if the desired `capacity` is insufficient to store all of the
* contained elements.
*
* @note This function is not available if the conatiner's `extent_type` is static.
*
* @param capacity New capacity of the container
* @param stream CUDA stream used for this operation
*/
void rehash_async(size_type capacity, cuda_stream_ref stream = {});
/**
* @brief Gets the number of elements in the container.
*
* @note This function synchronizes the given stream.
*
* @param stream CUDA stream used to get the number of inserted elements
* @return The number of elements in the container
*/
[[nodiscard]] size_type size(cuda_stream_ref stream = {}) const noexcept;
/**
* @brief Gets the maximum number of elements the hash map can hold.
*
* @return The maximum number of elements the hash map can hold
*/
[[nodiscard]] constexpr auto capacity() const noexcept;
/**
* @brief Gets the sentinel value used to represent an empty key slot.
*
* @return The sentinel value used to represent an empty key slot
*/
[[nodiscard]] constexpr key_type empty_key_sentinel() const noexcept;
/**
* @brief Gets the sentinel value used to represent an empty value slot.
*
* @return The sentinel value used to represent an empty value slot
*/
[[nodiscard]] constexpr mapped_type empty_value_sentinel() const noexcept;
/**
* @brief Gets the sentinel value used to represent an erased key slot.
*
* @return The sentinel value used to represent an erased key slot
*/
[[nodiscard]] constexpr key_type erased_key_sentinel() const noexcept;
/**
* @brief Get device ref with operators.
*
* @tparam Operators Set of `cuco::op` to be provided by the ref
*
* @param ops List of operators, e.g., `cuco::insert`
*
* @return Device ref of the current `static_map` object
*/
template <typename... Operators>
[[nodiscard]] auto ref(Operators... ops) const noexcept;
private:
std::unique_ptr<impl_type> impl_; ///< Static map implementation
mapped_type empty_value_sentinel_; ///< Sentinel value that indicates an empty payload
};
template <typename Key, typename Value, cuda::thread_scope Scope, typename Allocator>
class dynamic_map;
namespace legacy {
/**
* @brief A GPU-accelerated, unordered, associative container of key-value
* pairs with unique keys.
*
* Allows constant time concurrent inserts or concurrent find operations from threads in device
* code. Concurrent insert and find are supported only if the pair type is packable (see
* `cuco::detail::is_packable` constexpr).
*
* Current limitations:
* - Requires keys and values that where `cuco::is_bitwise_comparable_v<T>` is true
* - Comparisons against the "sentinel" values will always be done with bitwise comparisons.
* - Capacity is fixed and will not grow automatically
* - Requires the user to specify sentinel values for both key and mapped value to indicate empty
* slots
* - Conditionally support concurrent insert and find operations
*
* The `static_map` supports two types of operations:
* - Host-side "bulk" operations
* - Device-side "singular" operations
*
* The host-side bulk operations include `insert`, `erase`, `find`, and `contains`. These
* APIs should be used when there are a large number of keys to insert, erase or lookup
* in the map. For example, given a range of keys specified by device-accessible
* iterators, the bulk `insert` function will insert all keys into the map. Note that in order
* for a `static_map` instance to support `erase`, the user must provide an `erased_key_sentinel`
* which is distinct from the `empty_key_sentinel` at construction. If `erase` is called on a
* `static_map` which was not constructed in this way, a runtime error will be generated.
*
* The singular device-side operations allow individual threads to perform
* independent insert or find/contains operations from device code. These
* operations are accessed through non-owning, trivially copyable "view" types:
* `device_view` and `device_mutable_view`. The `device_view` class is an
* immutable view that allows only non-modifying operations such as `find` or
* `contains`. The `device_mutable_view` class only allows `insert` and `erase` operations.
* The two types are separate to prevent erroneous concurrent insert/erase/find
* operations. Note that the device-side `erase` may only be called if the corresponding
* `device_mutable_view` was constructed with a user-provided `erased_key_sentinel`. It is
* up to the user to ensure this condition is met.
*
* Example:
* \code{.cpp}
* int empty_key_sentinel = -1;
* int empty_value_sentinel = -1;
* int erased_key_sentinel = -2;
*
* // Constructs a map with 100,000 slots using -1 and -1 as the empty key/value
* // sentinels. The supplied erased key sentinel of -2 must be a different value from the empty
* // key sentinel. If erase functionality is not needed, you may elect to not supply an erased
* // key sentinel to the constructor. Note the capacity is chosen knowing we will insert 50,000
* keys,
* // for an load factor of 50%.
* static_map<int, int> m{100'000, empty_key_sentinel, empty_value_sentinel, erased_value_sentinel};
*
* // Create a sequence of pairs {{0,0}, {1,1}, ... {i,i}}
* thrust::device_vector<thrust::pair<int,int>> pairs(50,000);
* thrust::transform(thrust::make_counting_iterator(0),
* thrust::make_counting_iterator(pairs.size()),
* pairs.begin(),
* []__device__(auto i){ return thrust::make_pair(i,i); };
*
*
* // Inserts all pairs into the map
* m.insert(pairs.begin(), pairs.end());
*
* // Get a `device_view` and passes it to a kernel where threads may perform
* // `find/contains` lookups
* kernel<<<...>>>(m.get_device_view());
* \endcode
*
*
* @tparam Key Arithmetic type used for key
* @tparam Value Type of the mapped values
* @tparam Scope The scope in which insert/find operations will be performed by
* individual threads.
* @tparam Allocator Type of allocator used for device storage
*/
template <typename Key,
typename Value,
cuda::thread_scope Scope = cuda::thread_scope_device,
typename Allocator = cuco::cuda_allocator<char>>
class static_map {
static_assert(
cuco::is_bitwise_comparable_v<Key>,
"Key type must have unique object representations or have been explicitly declared as safe for "
"bitwise comparison via specialization of cuco::is_bitwise_comparable_v<Key>.");
static_assert(cuco::is_bitwise_comparable_v<Value>,
"Value type must have unique object representations or have been explicitly "
"declared as safe for bitwise comparison via specialization of "
"cuco::is_bitwise_comparable_v<Value>.");
friend class dynamic_map<Key, Value, Scope, Allocator>; ///< Dynamic map as friend class
public:
using value_type = cuco::pair<Key, Value>; ///< Type of key/value pairs
using key_type = Key; ///< Key type
using mapped_type = Value; ///< Type of mapped values
using atomic_key_type = cuda::atomic<key_type, Scope>; ///< Type of atomic keys
using atomic_mapped_type = cuda::atomic<mapped_type, Scope>; ///< Type of atomic mapped values
using pair_atomic_type =
cuco::pair<atomic_key_type,
atomic_mapped_type>; ///< Pair type of atomic key and atomic mapped value
using slot_type = pair_atomic_type; ///< Type of hash map slots
using atomic_ctr_type = cuda::atomic<std::size_t, Scope>; ///< Atomic counter type
using allocator_type = Allocator; ///< Allocator type
using slot_allocator_type = typename std::allocator_traits<Allocator>::template rebind_alloc<
pair_atomic_type>; ///< Type of the allocator to (de)allocate slots
using counter_allocator_type = typename std::allocator_traits<Allocator>::template rebind_alloc<
atomic_ctr_type>; ///< Type of the allocator to (de)allocate atomic counters
#if !defined(CUCO_HAS_INDEPENDENT_THREADS)
static_assert(atomic_key_type::is_always_lock_free,
"A key type larger than 8B is supported for only sm_70 and up.");
static_assert(atomic_mapped_type::is_always_lock_free,
"A value type larger than 8B is supported for only sm_70 and up.");
#endif
static_map(static_map const&) = delete;
static_map(static_map&&) = delete;
static_map& operator=(static_map const&) = delete;
static_map& operator=(static_map&&) = delete;
/**
* @brief Indicates if concurrent insert/find is supported for the key/value types.
*
* @return Boolean indicating if concurrent insert/find is supported.
*/
__host__ __device__ static constexpr bool supports_concurrent_insert_find() noexcept
{
return cuco::detail::is_packable<value_type>();
}
/**
* @brief Constructs a statically sized map with the specified number of slots
* and sentinel values.
*
* The capacity of the map is fixed. Insert operations will not automatically
* grow the map. Attempting to insert equal to or more unique keys than the capacity
* of the map results in undefined behavior (there should be at least one empty slot).
*
* Performance begins to degrade significantly beyond a load factor of ~70%.
* For best performance, choose a capacity that will keep the load factor
* below 70%. E.g., if inserting `N` unique keys, choose a capacity of
* `N * (1/0.7)`.
*
* The `empty_key_sentinel` and `empty_value_sentinel` values are reserved and
* undefined behavior results from attempting to insert any key/value pair
* that contains either.
*
* @param capacity The total number of slots in the map
* @param empty_key_sentinel The reserved key value for empty slots
* @param empty_value_sentinel The reserved mapped value for empty slots
* @param alloc Allocator used for allocating device storage
* @param stream Stream used for executing the kernels
*/
static_map(std::size_t capacity,
empty_key<Key> empty_key_sentinel,
empty_value<Value> empty_value_sentinel,
Allocator const& alloc = Allocator{},
cudaStream_t stream = 0);
/**
* @brief Constructs a fixed-size map with erase capability.
* empty_key_sentinel and erased_key_sentinel must be different values.
*
* @throw std::runtime error if the empty key sentinel and erased key sentinel
* are the same value
*
* @param capacity The total number of slots in the map
* @param empty_key_sentinel The reserved key value for empty slots
* @param empty_value_sentinel The reserved mapped value for empty slots
* @param erased_key_sentinel The reserved value to denote erased slots
* @param alloc Allocator used for allocating device storage
* @param stream Stream used for executing the kernels
*/
static_map(std::size_t capacity,
empty_key<Key> empty_key_sentinel,
empty_value<Value> empty_value_sentinel,
erased_key<Key> erased_key_sentinel,
Allocator const& alloc = Allocator{},
cudaStream_t stream = 0);
/**
* @brief Destroys the map and frees its contents.
*
*/
~static_map();
/**
* @brief Inserts all key/value pairs in the range `[first, last)`.
*
* This function synchronizes `stream`.
*
* If multiple keys in `[first, last)` compare equal, it is unspecified which
* element is inserted.
*
* @tparam InputIt Device accessible input iterator whose `value_type` is
* convertible to the map's `value_type`
* @tparam Hash Unary callable type
* @tparam KeyEqual Binary callable type
* @param first Beginning of the sequence of key/value pairs
* @param last End of the sequence of key/value pairs
* @param hash The unary function to apply to hash each key
* @param key_equal The binary function to compare two keys for equality
* @param stream Stream used for executing the kernels
*/
template <typename InputIt,
typename Hash = cuco::default_hash_function<key_type>,
typename KeyEqual = thrust::equal_to<key_type>>
void insert(InputIt first,
InputIt last,
Hash hash = Hash{},
KeyEqual key_equal = KeyEqual{},
cudaStream_t stream = 0);
/**
* @brief Inserts key/value pairs in the range `[first, last)` if `pred`
* of the corresponding stencil returns true.
*
* The key/value pair `*(first + i)` is inserted if `pred( *(stencil + i) )` returns true.
*
* @tparam InputIt Device accessible random access iterator whose `value_type` is
* convertible to the map's `value_type`
* @tparam StencilIt Device accessible random access iterator whose value_type is
* convertible to Predicate's argument type
* @tparam Predicate Unary predicate callable whose return type must be convertible to `bool` and
* argument type is convertible from <tt>std::iterator_traits<StencilIt>::value_type</tt>
* @tparam Hash Unary callable type
* @tparam KeyEqual Binary callable type
* @param first Beginning of the sequence of key/value pairs
* @param last End of the sequence of key/value pairs
* @param stencil Beginning of the stencil sequence
* @param pred Predicate to test on every element in the range `[stencil, stencil +
* std::distance(first, last))`
* @param hash The unary function to hash each key
* @param key_equal The binary function to compare two keys for equality
* @param stream CUDA stream used for insert
*/
template <typename InputIt,
typename StencilIt,
typename Predicate,
typename Hash = cuco::default_hash_function<key_type>,
typename KeyEqual = thrust::equal_to<key_type>>
void insert_if(InputIt first,
InputIt last,
StencilIt stencil,
Predicate pred,
Hash hash = Hash{},
KeyEqual key_equal = KeyEqual{},
cudaStream_t stream = 0);
/**