-
Notifications
You must be signed in to change notification settings - Fork 3
/
gpu_versioned_blink_tree.hpp
1720 lines (1559 loc) · 72.4 KB
/
gpu_versioned_blink_tree.hpp
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 2022 The Regents of the University of California, Davis
*
* 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
#define _CG_ABI_EXPERIMENTAL
#include <cooperative_groups.h>
#include <cuda_runtime.h>
#include <stdio.h>
#include <btree_kernels.hpp>
#include <cstddef>
#include <cstdint>
#include <fstream>
#include <ios>
#include <iostream>
#include <node.hpp>
#include <pair_type.hpp>
#include <queue>
#include <sstream>
#include <stdexcept>
#include <type_traits>
#include <versioned_node.hpp>
#include <device_bump_allocator.hpp>
#include <memory_reclaimer.hpp>
#include <slab_alloc.hpp>
//#define DEBUG_LOCKS
//#define DEBUG_STRUCTURE
#ifdef DEBUG_STRUCTURE
#define DEBUG_STRUCTURE_PRINT(fmt, ...) \
if (tile.thread_rank() == 0) { \
do { printf(fmt, __VA_ARGS__); } while (0); \
}
#else
#define DEBUG_STRUCTURE_PRINT(fmt, ...)
#endif
#define DEBUG_STRUCTURE_PRINT1(fmt, ...) \
if (tile.thread_rank() == 0) { \
do { printf(fmt, __VA_ARGS__); } while (0); \
}
#define DEBUG_THREAD_TO_TILE ((threadIdx.x + blockIdx.x * blockDim.x) / branching_factor)
#ifdef DEBUG_LOCKS
#define debug_print_tile(tile, fmt, ...) \
if (tile.thread_rank() == 0) { \
do { printf(fmt, __VA_ARGS__); } while (0); \
}
#else
#define debug_print_tile(fmt, ...)
#endif
namespace GpuBTree {
template <typename Key,
typename Value,
int B = 16,
typename Allocator = device_bump_allocator<node_type<Key, Value, B>>>
struct gpu_versioned_btree {
using size_type = uint32_t;
using key_type = Key;
using value_type = Value;
using pair_type = pair_type<Key, Value>;
static auto constexpr branching_factor = B;
using allocator_type = Allocator;
using device_allocator_context_type = device_allocator_context<allocator_type>;
static constexpr key_type invalid_key = std::numeric_limits<key_type>::max();
static constexpr value_type invalid_value = std::numeric_limits<key_type>::max();
// Host-side APIs
gpu_versioned_btree()
: allocator_{}, host_reclaimer_{reclaimer_blocks_count_, reclaimer_buffer_size_} {
static_assert(sizeof(Key) == sizeof(Value),
"Size of key must be the same as the size of the value");
allocate();
}
gpu_versioned_btree(const gpu_versioned_btree& other)
: snapshot_index_(other.snapshot_index_)
, root_index_(other.root_index_)
, d_snapshot_index_(other.d_snapshot_index_)
, h_btree_(other.h_btree_)
, h_node_count_(other.h_node_count_)
, d_root_index_(other.d_root_index_)
, allocator_(other.allocator_)
, host_reclaimer_(other.host_reclaimer_) {}
~gpu_versioned_btree() {}
void insert(const Key* keys,
const Value* values,
const size_type num_keys,
cudaStream_t stream = 0,
bool in_place = true) {
int block_size = 256;
int num_blocks = (num_keys + block_size - 1) / block_size;
if (in_place) {
kernels::insert_in_place_kernel<<<num_blocks, block_size, 0, stream>>>(
keys, values, num_keys, *this);
} else {
block_size = reclaimer_block_size_;
std::size_t required_shmem = 0;
int num_blocks_per_sm;
cudaOccupancyMaxActiveBlocksPerMultiprocessor(
&num_blocks_per_sm,
kernels::insert_out_of_place_kernel<
Key,
Value,
size_type,
typename std::remove_reference<decltype(*this)>::type>,
block_size,
static_cast<std::size_t>(required_shmem) * sizeof(uint32_t));
int device_id = 0;
cudaDeviceProp device_prop;
cudaGetDeviceProperties(&device_prop, device_id);
auto sms_count = device_prop.multiProcessorCount;
num_blocks = num_blocks_per_sm * sms_count;
assert(num_blocks <= reclaimer_blocks_count_);
kernels::insert_out_of_place_kernel<<<num_blocks,
block_size,
required_shmem * sizeof(uint32_t),
stream>>>(keys, values, num_keys, *this);
}
}
void find(const Key* keys,
Value* values,
const size_type num_keys,
cudaStream_t stream = 0,
bool concurrent = false) const {
const uint32_t block_size = 512;
const uint32_t num_blocks = (num_keys + block_size - 1) / block_size;
kernels::find_kernel<<<num_blocks, block_size, 0, stream>>>(
keys, values, num_keys, *this, concurrent);
}
void find(const Key* keys,
Value* values,
const size_type num_keys,
size_type timestamp,
cudaStream_t stream = 0,
bool concurrent = false) const {
const uint32_t block_size = 512;
const uint32_t num_blocks = (num_keys + block_size - 1) / block_size;
kernels::find_kernel<<<num_blocks, block_size, 0, stream>>>(
keys, values, num_keys, *this, timestamp, concurrent);
}
void erase(const Key* keys,
const size_type num_keys,
cudaStream_t stream = 0,
bool concurrent = false) {
const uint32_t block_size = 512;
const uint32_t num_blocks = (num_keys + block_size - 1) / block_size;
kernels::erase_kernel<<<num_blocks, block_size, 0, stream>>>(keys, num_keys, *this, concurrent);
}
// [lower_bound, upper_bound)
void range_query(const Key* lower_bound,
const Key* upper_bound,
pair_type* result,
size_type* counts,
const size_type average_range_length,
const size_type num_keys,
cudaStream_t stream = 0,
bool concurrent = false) {
const uint32_t block_size = 512;
const uint32_t num_blocks = (num_keys + block_size - 1) / block_size;
kernels::range_query_kernel<<<num_blocks, block_size, 0, stream>>>(lower_bound,
upper_bound,
result,
average_range_length,
num_keys,
*this,
counts,
concurrent);
}
// [lower_bound, upper_bound)
void range_query(const Key* lower_bound,
const Key* upper_bound,
pair_type* result,
size_type* counts,
const size_type average_range_length,
const size_type num_keys,
size_type timestamp,
cudaStream_t stream = 0,
bool concurrent = false) {
const uint32_t block_size = 512;
const uint32_t num_blocks = (num_keys + block_size - 1) / block_size;
kernels::range_query_kernel<<<num_blocks, block_size, 0, stream>>>(lower_bound,
upper_bound,
result,
average_range_length,
num_keys,
*this,
counts,
timestamp,
concurrent);
}
void concurrent_find_erase(const Key* find_keys,
Value* find_results,
const size_type num_finds,
const Key* erase_keys,
const size_type num_erasures,
cudaStream_t stream = 0) {
int block_size = reclaimer_block_size_;
auto required_shmem = smr::DEBR_device<reclaimer_max_ptrs_count_>::compute_shmem_requirements();
// num_blocks is hardware specific
int num_blocks_per_sm;
cudaOccupancyMaxActiveBlocksPerMultiprocessor(
&num_blocks_per_sm,
kernels::concurrent_find_erase_kernel_versioned<
Key,
Value,
size_type,
typename std::remove_reference<decltype(*this)>::type>,
block_size,
static_cast<std::size_t>(required_shmem) * sizeof(uint32_t));
int device_id = 0;
cudaDeviceProp device_prop;
cudaGetDeviceProperties(&device_prop, device_id);
auto sms_count = device_prop.multiProcessorCount;
const uint32_t num_blocks = num_blocks_per_sm * sms_count;
kernels::concurrent_find_erase_kernel_versioned<<<num_blocks,
block_size,
required_shmem * sizeof(uint32_t),
stream>>>(
find_keys, find_results, num_finds, erase_keys, num_erasures, *this);
}
void concurrent_insert_range(const Key* keys,
const Value* values,
const size_type num_insertion,
const Key* lower_bound,
const Key* upper_bound,
const size_type num_ranges,
pair_type* result,
const size_type average_range_length,
cudaStream_t stream = 0) {
int block_size = reclaimer_block_size_;
auto required_shmem = smr::DEBR_device<reclaimer_max_ptrs_count_>::compute_shmem_requirements();
int num_blocks_per_sm;
cudaOccupancyMaxActiveBlocksPerMultiprocessor(
&num_blocks_per_sm,
kernels::concurrent_insert_range_kernel<
Key,
Value,
pair_type,
size_type,
typename std::remove_reference<decltype(*this)>::type>,
block_size,
static_cast<std::size_t>(required_shmem) * sizeof(uint32_t));
int device_id = 0;
cudaDeviceProp device_prop;
cudaGetDeviceProperties(&device_prop, device_id);
auto sms_count = device_prop.multiProcessorCount;
const uint32_t num_blocks = num_blocks_per_sm * sms_count;
assert(num_blocks <= reclaimer_blocks_count_);
kernels::concurrent_insert_range_kernel<<<num_blocks,
block_size,
required_shmem * sizeof(uint32_t),
stream>>>(keys,
values,
num_insertion,
lower_bound,
upper_bound,
num_ranges,
result,
average_range_length,
*this);
}
// takes a snapshot and returns the old snapshot index
size_type take_snapshot(const cudaStream_t stream = 0) {
kernels::take_snapshot_kernel<<<1, 1, 0, stream>>>(*this);
size_type current_ts;
cuda_try(cudaMemcpy(¤t_ts, d_snapshot_index_, sizeof(size_type), cudaMemcpyDeviceToHost));
return current_ts - 1;
}
// Device-side APIs
template <typename tile_type, typename DeviceAllocator>
DEVICE_QUALIFIER bool cooperative_insert_in_place(const Key& key,
const Value& value,
const tile_type& tile,
DeviceAllocator& allocator) {
return cooperative_insert_versioned_in_place(key, value, tile, allocator);
}
template <typename tile_type, typename DeviceAllocator, typename DeviceReclaimer>
DEVICE_QUALIFIER bool cooperative_insert_out_of_place(const Key& key,
const Value& value,
const tile_type& tile,
DeviceAllocator& allocator,
DeviceReclaimer& reclaimer) {
return cooperative_insert_versioned_out_of_place(key, value, tile, allocator, reclaimer);
}
template <typename tile_type, typename DeviceAllocator, typename DeviceReclaimer>
DEVICE_QUALIFIER bool cooperative_insert(const Key& key,
const Value& value,
const tile_type& tile,
DeviceAllocator& allocator,
DeviceReclaimer& reclaimer) {
return cooperative_insert_versioned_out_of_place(key, value, tile, allocator, reclaimer);
}
// takes a snapshot and returns the old snapshot index
template <typename tile_type>
DEVICE_QUALIFIER size_type take_snapshot(const tile_type& tile) {
static constexpr int elected_lane = 0;
size_type cur_ts;
cuda_memory<uint32_t>::atomic_thread_fence();
if (tile.thread_rank() == elected_lane) {
cur_ts = get_current_version();
cur_ts = atomicCAS(d_snapshot_index_, cur_ts, cur_ts + 1);
}
auto snapshot_index = tile.shfl(cur_ts, elected_lane);
return snapshot_index;
}
template <typename tile_type, typename DeviceAllocator>
DEVICE_QUALIFIER Value cooperative_find(const Key& key,
const tile_type& tile,
DeviceAllocator& allocator,
bool concurrent = false) {
size_type ts = invalid_timestamp_;
return cooperative_find(key, tile, allocator, ts, concurrent);
}
template <typename tile_type, typename DeviceAllocator>
DEVICE_QUALIFIER bool cooperative_erase(const Key& key,
const tile_type& tile,
DeviceAllocator& allocator,
bool concurrent = false) {
using node_type = btree_versioned_node<pair_type, tile_type, branching_factor>;
auto current_node_index = *d_root_index_;
while (true) {
node_type current_node = node_type(
reinterpret_cast<pair_type*>(allocator.address(allocator_, current_node_index)), tile);
if (concurrent) {
current_node.load(cuda_memory_order::memory_order_relaxed);
current_node.init(invalid_timestamp_, d_snapshot_index_);
traverse_side_links_init(current_node, current_node_index, key, tile, allocator);
} else {
current_node.load();
}
bool is_leaf = current_node.is_leaf();
bool is_locked = current_node.is_locked();
if (is_leaf) {
current_node.lock();
current_node.load(cuda_memory_order::memory_order_relaxed);
if (concurrent) {
traverse_side_links_with_locks_init(
current_node, current_node_index, key, tile, allocator);
}
bool success = current_node.erase(key);
if (success) { current_node.store(cuda_memory_order::memory_order_relaxed); }
current_node.unlock();
return success;
} else {
current_node_index = current_node.find_next(key);
}
}
return false;
}
// We always delete from the latest version
// This overload assume concurrent operations
template <typename tile_type, typename DeviceAllocator, typename DeviceReclaimer>
DEVICE_QUALIFIER bool cooperative_erase(const Key& key,
const tile_type& tile,
DeviceAllocator& allocator,
DeviceReclaimer& reclaimer) {
using node_type = btree_versioned_node<pair_type, tile_type, branching_factor>;
auto current_node_index = *d_root_index_;
while (true) {
node_type current_node = node_type(
reinterpret_cast<pair_type*>(allocator.address(allocator_, current_node_index)), tile);
current_node.load(cuda_memory_order::memory_order_relaxed);
current_node.init(invalid_timestamp_, d_snapshot_index_);
traverse_side_links_init(current_node, current_node_index, key, tile, allocator);
bool is_leaf = current_node.is_leaf();
if (is_leaf) {
current_node.lock();
current_node.load(cuda_memory_order::memory_order_relaxed);
current_node.init(invalid_timestamp_, d_snapshot_index_);
traverse_side_links_with_locks_init(current_node, current_node_index, key, tile, allocator);
auto old_node_index = allocator.allocate(allocator_, 1, tile);
current_node.store_unlocked_copy_at(
reinterpret_cast<pair_type*>(allocator.address(allocator_, old_node_index)));
bool success = current_node.erase(key);
// we should only create a copy if we succeed, but for now we always copy and reclaim
// later. Ideally, we shouldn't lock if the key doesn't exist
if (success) {
current_node.set_version_ptr_data(invalid_timestamp_, old_node_index);
__threadfence(); // make sure copy is stored
current_node.store(cuda_memory_order::memory_order_relaxed);
}
current_node.unlock();
if (success) { current_node.init(invalid_timestamp_, d_snapshot_index_); }
reclaimer.retire(old_node_index, tile, allocator, allocator_);
return success;
} else {
current_node_index = current_node.find_next(key);
}
}
return false;
}
template <typename tile_type, typename DeviceAllocator>
DEVICE_QUALIFIER Value cooperative_find(const Key& key,
const tile_type& tile,
DeviceAllocator& allocator,
const size_type& timestamp,
bool concurrent = false) {
auto value = invalid_value;
using node_type = btree_versioned_node<pair_type, tile_type, branching_factor>;
auto current_node_index = *d_root_index_;
while (true) {
node_type current_node = node_type(
reinterpret_cast<pair_type*>(allocator.address(allocator_, current_node_index)), tile);
if (concurrent) {
current_node.load(cuda_memory_order::memory_order_relaxed);
current_node.init(invalid_timestamp_, d_snapshot_index_);
traverse_side_links_init(current_node, current_node_index, key, tile, allocator);
traverse_version_list(current_node, current_node_index, timestamp, tile, allocator);
} else {
current_node.load();
traverse_version_list(current_node,
current_node_index,
timestamp,
tile,
allocator,
cuda_memory_order::memory_order_weak);
}
bool is_leaf = current_node.is_leaf();
if (is_leaf) {
value = current_node.get_key_value_from_node(key);
return value;
} else {
current_node_index = current_node.find_next(key);
}
}
return value;
}
// An overload for concurrent find running alongside updates
// todo use this and not the one above
template <typename tile_type, typename DeviceAllocator>
DEVICE_QUALIFIER Value concurrent_cooperative_find(const Key& key,
const tile_type& tile,
DeviceAllocator& allocator,
const size_type& timestamp) {
auto value = invalid_value;
using node_type = btree_versioned_node<pair_type, tile_type, branching_factor>;
auto current_node_index = *d_root_index_;
while (true) {
node_type current_node = node_type(
reinterpret_cast<pair_type*>(allocator.address(allocator_, current_node_index)), tile);
current_node.load(cuda_memory_order::memory_order_relaxed);
traverse_version_list(current_node, timestamp, tile, allocator);
bool is_leaf = current_node.is_leaf();
bool is_locked = current_node.is_locked();
if (is_leaf) {
value = current_node.get_key_value_from_node(key);
return value;
} else {
current_node_index = current_node.find_next(key);
}
}
return value;
}
// Range query that includes [lower_bound, upper_bound)
template <typename tile_type, typename DeviceAllocator>
DEVICE_QUALIFIER size_type concurrent_cooperative_range_query(const Key& lower_bound,
const Key& upper_bound,
const tile_type& tile,
DeviceAllocator& allocator,
const size_type& timestamp,
pair_type* buffer = nullptr) {
using node_type = btree_versioned_node<pair_type, tile_type, branching_factor>;
auto current_node_index = *d_root_index_;
size_type count = 0;
while (true) {
node_type current_node = node_type(
reinterpret_cast<pair_type*>(allocator.address(allocator_, current_node_index)), tile);
current_node.load(cuda_memory_order::memory_order_relaxed);
auto init_res = current_node.init(invalid_timestamp_, d_snapshot_index_);
traverse_side_links_init(current_node, current_node_index, lower_bound, tile, allocator);
traverse_version_list(current_node, current_node_index, timestamp, tile, allocator);
DEBUG_STRUCTURE_PRINT("%i init RQ node %u result [%i,%u]\n",
DEBUG_THREAD_TO_TILE,
current_node_index,
init_res.success,
init_res.cur_ts)
bool is_leaf = current_node.is_leaf();
if (is_leaf) {
bool keep_traversing = true;
do {
if (buffer != nullptr) {
count += current_node.get_in_range(lower_bound, upper_bound, buffer + count);
} else {
count += current_node.get_in_range(lower_bound, upper_bound, nullptr);
}
keep_traversing = upper_bound > current_node.get_high_key();
if (keep_traversing) {
current_node_index = current_node.get_sibling_index();
current_node = node_type(
reinterpret_cast<pair_type*>(allocator.address(allocator_, current_node_index)),
tile);
current_node.load(cuda_memory_order::memory_order_relaxed);
init_res = current_node.init(invalid_timestamp_, d_snapshot_index_);
DEBUG_STRUCTURE_PRINT("%i init RQ traversal %u result [%i,%u]\n",
DEBUG_THREAD_TO_TILE,
current_node_index,
init_res.success,
init_res.cur_ts)
traverse_version_list(current_node, current_node_index, timestamp, tile, allocator);
}
} while (keep_traversing);
return count;
} else {
current_node_index = current_node.find_next(lower_bound);
}
}
return count;
}
template <typename tile_type, typename DeviceAllocator, typename MemoryReclaimer>
DEVICE_QUALIFIER bool cooperative_insert_versioned_out_of_place(const Key& key,
const Value& value,
const tile_type& tile,
DeviceAllocator& allocator,
MemoryReclaimer& reclaimer) {
using node_type = btree_versioned_node<pair_type, tile_type, branching_factor>;
auto root_index = *d_root_index_;
auto current_node_index = root_index;
auto parent_index = root_index;
bool keep_going = true;
bool link_traversed = false;
do {
auto current_node = node_type(
reinterpret_cast<pair_type*>(allocator.address(allocator_, current_node_index)), tile);
current_node.load(cuda_memory_order::memory_order_relaxed);
auto init_res = current_node.init(invalid_timestamp_, d_snapshot_index_);
DEBUG_STRUCTURE_PRINT("%i init node %u result [%i,%u]\n",
DEBUG_THREAD_TO_TILE,
current_node_index,
init_res.success,
init_res.cur_ts)
// if we restarted from root, we reset the traversal
link_traversed = current_node_index == root_index ? false : link_traversed;
// Traversing side-links
link_traversed |=
traverse_side_links_init(current_node, current_node_index, key, tile, allocator);
bool is_leaf = current_node.is_leaf();
if (is_leaf) {
if (current_node.try_lock()) {
current_node.load(cuda_memory_order::memory_order_relaxed);
current_node.init(invalid_timestamp_, d_snapshot_index_);
bool parent_unknown =
current_node_index == parent_index && current_node_index != root_index;
bool traversal_required = key >= current_node.get_high_key();
// if the parent is unknown we will not proceed
if (parent_unknown && traversal_required) {
current_node.unlock();
current_node_index = root_index;
parent_index = root_index;
continue;
}
is_leaf = current_node.is_leaf();
// if the node is not a leaf anymore, we don't need the lock
if (!is_leaf) { current_node.unlock(); }
// traversal while holding the lock
while (key >= current_node.get_high_key()) {
if (is_leaf) { current_node.unlock(); }
current_node_index = current_node.get_sibling_index();
current_node = node_type(
reinterpret_cast<pair_type*>(allocator.address(allocator_, current_node_index)),
tile);
if (is_leaf) { current_node.lock(); }
current_node.load(cuda_memory_order::memory_order_relaxed);
current_node.init(invalid_timestamp_, d_snapshot_index_);
is_leaf = current_node.is_leaf();
// if the node is not a leaf anymore, we don't need the lock
if (!is_leaf) { current_node.unlock(); }
link_traversed = true;
}
} else {
current_node_index = parent_index;
continue;
}
}
// make sure that if the node is full, we know the parent
// we only know the parent if we didn't do side-traversal
bool is_full = current_node.is_full();
if (is_full && link_traversed) {
if (is_leaf) {
current_node.unlock();
current_node_index = root_index;
parent_index = root_index;
continue;
}
}
// if is full, and not leaf we need to acquire the lock
if (is_full && !is_leaf) {
if (current_node.try_lock()) {
current_node.load(cuda_memory_order::memory_order_relaxed);
current_node.init(invalid_timestamp_, d_snapshot_index_);
is_full = current_node.is_full();
if (is_full) {
bool traversal_required = key >= current_node.get_high_key();
// if we traverse, parent will change so we will restart
if (traversal_required) {
current_node.unlock();
current_node_index = root_index;
parent_index = root_index;
continue;
}
} else {
current_node.unlock();
// Traversing side-links
link_traversed |=
traverse_side_links_init(current_node, current_node_index, key, tile, allocator);
}
} else {
current_node_index = parent_index;
continue;
}
}
is_full = current_node.is_full();
// if the node full after we restarted we can't proceed
if (is_full && (current_node_index != root_index) && (current_node_index == parent_index)) {
current_node.unlock();
current_node_index = root_index;
parent_index = root_index;
continue;
}
// splitting an intermediate node
if (is_full && (current_node_index != root_index)) {
auto parent_node = node_type(
reinterpret_cast<pair_type*>(allocator.address(allocator_, parent_index)), tile);
parent_node.lock();
parent_node.load(cuda_memory_order::memory_order_relaxed);
parent_node.init(invalid_timestamp_, d_snapshot_index_);
bool parent_is_full = parent_node.is_full();
// make sure parent is not full
if (parent_is_full) {
current_node.unlock();
parent_node.unlock();
current_node_index = root_index;
parent_index = root_index;
continue;
}
// make sure parent is correct parent
auto parent_is_correct = parent_node.ptr_is_in_node(current_node_index);
if (!parent_is_correct) {
current_node.unlock();
parent_node.unlock();
current_node_index = root_index;
parent_index = root_index;
continue;
}
// now it is safe to split
size_type old_parent_index = allocator.allocate(allocator_, 1, tile);
size_type old_node_index = allocator.allocate(allocator_, 1, tile);
size_type sibling_index = allocator.allocate(allocator_, 1, tile);
parent_node.store_unlocked_copy_at(
reinterpret_cast<pair_type*>(allocator.address(allocator_, old_parent_index)));
current_node.store_unlocked_copy_at(
reinterpret_cast<pair_type*>(allocator.address(allocator_, old_node_index)));
auto go_right = current_node.key_is_in_upperhalf(key);
auto split_result = current_node.split(
sibling_index,
parent_index,
reinterpret_cast<pair_type*>(allocator.address(allocator_, sibling_index)),
reinterpret_cast<pair_type*>(allocator.address(allocator_, parent_index)),
true);
// Splitting node and sibling get the same timestamp
auto split_ts = current_node.get_version_number();
split_result.parent.set_version_ptr_data(split_ts, old_parent_index);
split_result.sibling.set_version_ptr_data(split_ts, invalid_value);
current_node.set_version_ptr_data(split_ts, old_node_index);
split_result.sibling.store(cuda_memory_order::memory_order_relaxed);
__threadfence();
current_node.store(cuda_memory_order::memory_order_relaxed);
__threadfence();
split_result.parent.store(cuda_memory_order::memory_order_relaxed);
split_result.parent.unlock();
if (go_right) {
current_node_index = sibling_index;
current_node.unlock();
current_node = split_result.sibling;
} else {
split_result.sibling.unlock();
}
is_leaf = current_node.is_leaf();
if (!is_leaf) { current_node.unlock(); }
// retire the two old nodes
reclaimer.retire(old_parent_index, tile, allocator, allocator_);
reclaimer.retire(old_node_index, tile, allocator, allocator_);
} else if (is_full) {
auto sibling_index0 = allocator.allocate(allocator_, 1, tile);
auto sibling_index1 = allocator.allocate(allocator_, 1, tile);
auto old_node_index = allocator.allocate(allocator_, 1, tile);
auto split_ts = get_current_version();
current_node.store_unlocked_copy_at(
reinterpret_cast<pair_type*>(allocator.address(allocator_, old_node_index)));
auto two_siblings =
current_node.split_as_root(sibling_index0, // left node
sibling_index1, // left right
reinterpret_cast<pair_type*>(allocator.address(
allocator_, sibling_index0)), // left ptr
reinterpret_cast<pair_type*>(allocator.address(
allocator_, sibling_index1)), // right ptr
true); // children_are_locked
// set the timestamps
current_node.set_version_ptr_data(split_ts, old_node_index);
two_siblings.right.set_version_ptr_data(split_ts, invalid_value);
two_siblings.left.set_version_ptr_data(split_ts, invalid_value);
two_siblings.right.store(cuda_memory_order::memory_order_relaxed);
__threadfence();
two_siblings.left.store(cuda_memory_order::memory_order_relaxed);
__threadfence();
current_node.store(cuda_memory_order::memory_order_relaxed); // root is still locked
current_node.unlock();
// go right or left?
current_node_index = current_node.find_next(key);
if (current_node_index == sibling_index0) { // go left
two_siblings.right.unlock();
current_node = two_siblings.left;
} else { // go right
two_siblings.left.unlock();
current_node = two_siblings.right;
}
parent_index = root_index;
is_leaf = current_node.is_leaf();
if (!is_leaf) { current_node.unlock(); }
// retire the old root
reclaimer.retire(old_node_index, tile, allocator, allocator_);
}
// traversal and insertion
is_leaf = current_node.is_leaf();
if (is_leaf) {
// copy into a new node
auto new_node_index = allocator.allocate(allocator_, 1, tile);
current_node.store_unlocked_copy_at(
reinterpret_cast<pair_type*>(allocator.address(allocator_, new_node_index)));
__threadfence();
// now do the in-place update
current_node.insert(key, value);
current_node.set_version_ptr_data(invalid_timestamp_, new_node_index);
current_node.store(cuda_memory_order::memory_order_relaxed);
current_node.unlock();
current_node.init(invalid_timestamp_, d_snapshot_index_);
reclaimer.retire(new_node_index, tile, allocator, allocator_);
return true;
} else { // traverse
parent_index = link_traversed ? root_index : current_node_index;
current_node_index = current_node.find_next(key);
}
} while (keep_going);
return true;
}
template <typename tile_type, typename DeviceAllocator>
DEVICE_QUALIFIER bool cooperative_insert_versioned_in_place(const Key& key,
const Value& value,
const tile_type& tile,
DeviceAllocator& allocator) {
using node_type = btree_versioned_node<pair_type, tile_type, branching_factor>;
auto root_index = *d_root_index_;
auto current_node_index = root_index;
auto parent_index = root_index;
bool keep_going = true;
bool link_traversed = false;
auto current_timestamp = get_current_version();
do {
auto current_node = node_type(
reinterpret_cast<pair_type*>(allocator.address(allocator_, current_node_index)), tile);
current_node.load(cuda_memory_order::memory_order_relaxed);
// if we restarted from root, we reset the traversal
link_traversed = current_node_index == root_index ? false : link_traversed;
// Traversing side-links
link_traversed |= traverse_side_links(current_node, current_node_index, key, tile, allocator);
bool is_leaf = current_node.is_leaf();
if (is_leaf) {
if (current_node.try_lock()) {
current_node.load(cuda_memory_order::memory_order_relaxed);
bool parent_unknown =
current_node_index == parent_index && current_node_index != root_index;
bool traversal_required = key >= current_node.get_high_key();
// if the parent is unknown we will not proceed
if (parent_unknown && traversal_required) {
current_node.unlock();
current_node_index = root_index;
parent_index = root_index;
continue;
}
is_leaf = current_node.is_leaf();
// if the node is not a leaf anymore, we don't need the lock
if (!is_leaf) { current_node.unlock(); }
// traversal while holding the lock
while (key >= current_node.get_high_key()) {
if (is_leaf) { current_node.unlock(); }
current_node_index = current_node.get_sibling_index();
current_node = node_type(
reinterpret_cast<pair_type*>(allocator.address(allocator_, current_node_index)),
tile);
if (is_leaf) { current_node.lock(); }
current_node.load(cuda_memory_order::memory_order_relaxed);
is_leaf = current_node.is_leaf();
// if the node is not a leaf anymore, we don't need the lock
if (!is_leaf) { current_node.unlock(); }
link_traversed = true;
}
} else {
current_node_index = parent_index;
continue;
}
}
// make sure that if the node is full, we know the parent
// we only know the parent if we didn't do side-traversal
bool is_full = current_node.is_full();
if (is_full && link_traversed) {
if (is_leaf) {
current_node.unlock();
current_node_index = root_index;
parent_index = root_index;
continue;
}
}
// if is full, and not leaf we need to acquire the lock
if (is_full && !is_leaf) {
if (current_node.try_lock()) {
current_node.load(cuda_memory_order::memory_order_relaxed);
is_full = current_node.is_full();
if (is_full) {
bool traversal_required = key >= current_node.get_high_key();
// if we traverse, parent will change so we will restart
if (traversal_required) {
current_node.unlock();
current_node_index = root_index;
parent_index = root_index;
continue;
}
} else {
current_node.unlock();
// Traversing side-links
link_traversed |=
traverse_side_links(current_node, current_node_index, key, tile, allocator);
}
} else {
current_node_index = parent_index;
continue;
}
}
is_full = current_node.is_full();
// if the node full after we restarted we can't proceed
if (is_full && (current_node_index != root_index) && (current_node_index == parent_index)) {
current_node.unlock();
current_node_index = root_index;
parent_index = root_index;
continue;
}
// splitting an intermediate node
if (is_full && (current_node_index != root_index)) {
auto parent_node = node_type(
reinterpret_cast<pair_type*>(allocator.address(allocator_, parent_index)), tile);
parent_node.lock();
parent_node.load(cuda_memory_order::memory_order_relaxed);
bool parent_is_full = parent_node.is_full();
// make sure parent is not full
if (parent_is_full) {
current_node.unlock();
parent_node.unlock();
current_node_index = root_index;
parent_index = root_index;
continue;
}
// make sure parent is correct parent
auto parent_is_correct = parent_node.ptr_is_in_node(current_node_index);
if (!parent_is_correct) {
current_node.unlock();
parent_node.unlock();
current_node_index = root_index;
parent_index = root_index;
continue;
}
// now it is safe to split
size_type old_parent_index, old_node_index;
auto parent_node_timestamp = parent_node.get_version_number();
auto current_node_timestamp = current_node.get_version_number();
if (current_timestamp != parent_node_timestamp) {
old_parent_index = allocator.allocate(allocator_, 1, tile);
parent_node.store_unlocked_copy_at(
reinterpret_cast<pair_type*>(allocator.address(allocator_, old_parent_index)));
}
if (current_timestamp != current_node_timestamp) {
old_node_index = allocator.allocate(allocator_, 1, tile);
current_node.store_unlocked_copy_at(
reinterpret_cast<pair_type*>(allocator.address(allocator_, old_node_index)));
}
auto go_right = current_node.key_is_in_upperhalf(key);
size_type sibling_index = allocator.allocate(allocator_, 1, tile);
auto split_result = current_node.split(
sibling_index,
parent_index,
reinterpret_cast<pair_type*>(allocator.address(allocator_, sibling_index)),
reinterpret_cast<pair_type*>(allocator.address(allocator_, parent_index)),
true);
if (current_timestamp != parent_node_timestamp) {
split_result.parent.set_version_ptr_data(current_timestamp, old_parent_index);
}
if (current_timestamp != current_node_timestamp) {
split_result.sibling.set_version_ptr_data(current_timestamp, old_node_index);
current_node.set_version_ptr_data(current_timestamp, old_node_index);
}
split_result.sibling.store(cuda_memory_order::memory_order_relaxed);
__threadfence();
current_node.store(cuda_memory_order::memory_order_relaxed);
__threadfence();
split_result.parent.store(cuda_memory_order::memory_order_relaxed);
split_result.parent.unlock();
if (go_right) {
current_node_index = sibling_index;