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
// Copyright 2021-2022 Clemens Lutz
//
// 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.

use crate::error::{ErrorKind, Result};
use crate::measurement::harness::RadixJoinPoint;
use cstr::cstr;
use data_store::join_data::JoinData;
use datagen::relation::KeyAttribute;
use itertools::izip;
use numa_gpu::error::Result as NumaGpuResult;
use numa_gpu::runtime::allocator::{Allocator, DerefMemType, MemType};
use numa_gpu::runtime::cpu_affinity::CpuAffinity;
use numa_gpu::runtime::cuda_wrapper;
use numa_gpu::runtime::hw_info::NvidiaDriverInfo;
use numa_gpu::runtime::linux_wrapper;
use numa_gpu::runtime::memory::*;
use numa_gpu::runtime::numa::PageType;
use numa_gpu::runtime::nvtx::Range;
use numa_gpu::utils::DeviceType;
use rustacuda::context::{CacheConfig, CurrentContext, SharedMemoryConfig};
use rustacuda::event::{Event, EventFlags};
use rustacuda::function::{BlockSize, GridSize};
use rustacuda::memory::{CopyDestination, DeviceBuffer, DeviceCopy};
use rustacuda::stream::{Stream, StreamFlags, StreamWaitEventFlags};
use sql_ops::join::{cuda_radix_join, no_partitioning_join, HashingScheme};
use sql_ops::partition::cpu_radix_partition::{
    CpuHistogramAlgorithm, CpuRadixPartitionAlgorithm, CpuRadixPartitionable, CpuRadixPartitioner,
};
use sql_ops::partition::gpu_radix_partition::{
    GpuHistogramAlgorithm, GpuRadixPartitionAlgorithm, GpuRadixPartitionable, GpuRadixPartitioner,
};
use sql_ops::partition::{
    PartitionOffsets, PartitionedRelation, RadixBits, RadixPartitionInputChunkable, RadixPass,
    Tuple,
};
use std::cmp;
use std::convert::TryInto;
use std::iter;
use std::mem;
use std::sync::Arc;
use std::time::Instant;

/// GPU memory to leave free when allocating the Triton join's cache
///
/// Getting the amount of free GPU memory and allocating the memory is
/// *not atomic*. Sometimes the allocation spuriously fails.
///
/// Instead of allocating every last byte of GPU memory, leave some slack space.
const GPU_MEM_SLACK_BYTES: usize = 32 * 1024 * 1024;

/// GPU memory to leave free between the partitioned relations
///
/// The cached partitioned relations are allocated individually. The CUDA seems
/// to have some external fragementation, presumably due to page alignment of
/// allocations.
///
/// The overhead needs to be taken into account when calculating the maximum
/// number of partitioned relations that can be cached in GPU memory.
///
/// Note that this is a quick-fix. A better implementation would allocate the
/// 2nd pass partitioned relations in one or more arrays instead of one-by-one.
/// However, a single array wouldn't be sufficient, as not all relations can be
/// cached.
const GPU_MEM_RELATION_SLACK_BYTES: usize = 2 * 1024 * 1024;

// Helper struct that stores state of 2nd partitioning passes. Memory
// allocations occur asynchronously in parallel to partitioning.
//
// FIXME: refactor the StreamState into an seperate file, and deduplicate
struct StreamState<T: DeviceCopy> {
    stream: Stream,
    event: Event,
    radix_prnr_2nd: GpuRadixPartitioner,
    radix_join: cuda_radix_join::CudaRadixJoin,
    cached_inner_key: Mem<T>,
    cached_inner_pay: Mem<T>,
    cached_outer_key: Mem<T>,
    cached_outer_pay: Mem<T>,
    inner_rel_partition_offsets_2nd: PartitionOffsets<Tuple<T, T>>,
    outer_rel_partition_offsets_2nd: PartitionOffsets<Tuple<T, T>>,
    inner_rel_partitions_2nd: PartitionedRelation<Tuple<T, T>>,
    outer_rel_partitions_2nd: PartitionedRelation<Tuple<T, T>>,
    join_task_assignments: Mem<u32>,
    join_result_sums: DeviceBuffer<i64>,
}

impl<T: DeviceCopy> MemLock for StreamState<T> {
    fn mlock(&mut self) -> NumaGpuResult<()> {
        self.cached_inner_key.mlock()?;
        self.cached_inner_pay.mlock()?;
        self.cached_outer_key.mlock()?;
        self.cached_outer_pay.mlock()?;
        self.inner_rel_partition_offsets_2nd.mlock()?;
        self.outer_rel_partition_offsets_2nd.mlock()?;
        self.inner_rel_partitions_2nd.mlock()?;
        self.outer_rel_partitions_2nd.mlock()?;
        self.join_task_assignments.mlock()?;

        Ok(())
    }

    fn munlock(&mut self) -> NumaGpuResult<()> {
        unimplemented!();
    }
}

pub fn cpu_partitioned_radix_join<T>(
    data: &mut JoinData<T>,
    hashing_scheme: HashingScheme,
    histogram_algorithm_fst: DeviceType<CpuHistogramAlgorithm, GpuHistogramAlgorithm>,
    histogram_algorithm_snd: DeviceType<CpuHistogramAlgorithm, GpuHistogramAlgorithm>,
    partition_algorithm_fst: DeviceType<CpuRadixPartitionAlgorithm, GpuRadixPartitionAlgorithm>,
    partition_algorithm_snd: DeviceType<CpuRadixPartitionAlgorithm, GpuRadixPartitionAlgorithm>,
    radix_bits: &RadixBits,
    dmem_buffer_bytes: usize,
    max_partitions_cache_bytes: Option<usize>,
    threads: usize,
    cpu_affinity: CpuAffinity,
    partitions_mem_type: MemType,
    stream_state_mem_type: MemType,
    _page_type: PageType,
    _partition_dim: (&GridSize, &BlockSize),
    join_dim: (&GridSize, &BlockSize),
) -> Result<(i64, RadixJoinPoint)>
where
    T: Default
        + Clone
        + DeviceCopy
        + Sync
        + Send
        + CpuRadixPartitionable
        + GpuRadixPartitionable
        + KeyAttribute
        + no_partitioning_join::CudaHashJoinable
        + no_partitioning_join::CpuHashJoinable
        + cuda_radix_join::CudaRadixJoinable,
{
    const NUM_STREAMS: usize = 2;

    // Precondition checks
    let histogram_algorithm_snd = histogram_algorithm_snd.gpu().ok_or_else(|| {
        ErrorKind::InvalidArgument("Only GPU prefix sum is supported in 2nd pass".to_string())
    })?;
    let partition_algorithm_fst = partition_algorithm_fst.cpu().ok_or_else(|| {
        ErrorKind::InvalidArgument("Only CPU partitioning is supported in 1st pass".to_string())
    })?;
    let partition_algorithm_snd = partition_algorithm_snd.gpu().ok_or_else(|| {
        ErrorKind::InvalidArgument("Only GPU partitioning is supported in 2nd pass".to_string())
    })?;

    CurrentContext::set_cache_config(CacheConfig::PreferShared)?;
    CurrentContext::set_shared_memory_config(SharedMemoryConfig::FourByteBankSize)?;

    let boxed_cpu_affinity = Arc::new(cpu_affinity);
    let thread_pool = rayon::ThreadPoolBuilder::new()
        .num_threads(threads)
        .start_handler(move |tid| {
            boxed_cpu_affinity
                .clone()
                .set_affinity(tid as u16)
                .expect("Couldn't set CPU core affinity")
        })
        .build()?;

    let partitions_malloc_timer = Instant::now();

    let stream_grid_size = &join_dim.0;
    let stream_block_size = &join_dim.1;
    let max_chunks_1st = threads as u32;
    let max_chunks_2nd = stream_grid_size.x;
    let join_result_sums_len = (stream_grid_size.x * stream_block_size.x) as usize;

    let mut radix_prnrs: Vec<_> = (0..threads)
        .map(|_| {
            Ok(CpuRadixPartitioner::new(
                histogram_algorithm_fst.cpu().ok_or_else(|| {
                    ErrorKind::InvalidArgument("Only CPU prefix sum is supported".to_string())
                })?,
                partition_algorithm_fst,
                radix_bits.pass_radix_bits(RadixPass::First).unwrap(),
                DerefMemType::AlignedSysMem {
                    align_bytes: sql_ops::CPU_CACHE_LINE_SIZE as usize,
                },
            ))
        })
        .collect::<Result<_>>()?;

    let mut inner_rel_partitions = PartitionedRelation::new(
        data.build_relation_key.len(),
        histogram_algorithm_fst.either(|cpu| cpu.into(), |gpu| gpu.into()),
        radix_bits.pass_radix_bits(RadixPass::First).unwrap(),
        max_chunks_1st,
        Allocator::mem_alloc_fn(partitions_mem_type.clone()),
        Allocator::mem_alloc_fn(partitions_mem_type.clone()),
    );

    let mut outer_rel_partitions = PartitionedRelation::new(
        data.probe_relation_key.len(),
        histogram_algorithm_fst.either(|cpu| cpu.into(), |gpu| gpu.into()),
        radix_bits.pass_radix_bits(RadixPass::First).unwrap(),
        max_chunks_1st,
        Allocator::mem_alloc_fn(partitions_mem_type.clone()),
        Allocator::mem_alloc_fn(partitions_mem_type.clone()),
    );

    let mut inner_rel_partition_offsets = PartitionOffsets::new(
        histogram_algorithm_fst.either(|cpu| cpu.into(), |gpu| gpu.into()),
        max_chunks_1st,
        radix_bits.pass_radix_bits(RadixPass::First).unwrap(),
        Allocator::mem_alloc_fn(partitions_mem_type.clone()),
    );

    let mut outer_rel_partition_offsets = PartitionOffsets::new(
        histogram_algorithm_fst.either(|cpu| cpu.into(), |gpu| gpu.into()),
        max_chunks_1st,
        radix_bits.pass_radix_bits(RadixPass::First).unwrap(),
        Allocator::mem_alloc_fn(partitions_mem_type.clone()),
    );

    inner_rel_partitions.mlock()?;
    outer_rel_partitions.mlock()?;
    inner_rel_partition_offsets.mlock()?;
    outer_rel_partition_offsets.mlock()?;

    let partitions_malloc_time = partitions_malloc_timer.elapsed();

    let prefix_sum_range = Range::new(cstr!("phase_prefix_sum"));
    let prefix_sum_timer = Instant::now();

    let inner_key_slice: &[T] = (&data.build_relation_key).try_into().map_err(|_| {
        ErrorKind::RuntimeError("Failed to run CPU prefix sum on device memory".into())
    })?;
    let inner_pay_slice: &[T] = (&data.build_relation_payload).try_into().map_err(|_| {
        ErrorKind::RuntimeError("Failed to run CPU prefix sum on device memory".into())
    })?;
    let inner_key_chunks = inner_key_slice.input_chunks::<T>(max_chunks_1st)?;
    let inner_pay_chunks = inner_pay_slice.input_chunks::<T>(max_chunks_1st)?;

    let outer_key_slice: &[T] = (&data.probe_relation_key).try_into().map_err(|_| {
        ErrorKind::RuntimeError("Failed to run CPU prefix sum on device memory".into())
    })?;
    let outer_pay_slice: &[T] = (&data.probe_relation_payload).try_into().map_err(|_| {
        ErrorKind::RuntimeError("Failed to run CPU prefix sum on device memory".into())
    })?;
    let outer_key_chunks = outer_key_slice.input_chunks::<T>(max_chunks_1st)?;
    let outer_pay_chunks = outer_pay_slice.input_chunks::<T>(max_chunks_1st)?;

    // Prefix sum on inner relation
    thread_pool.scope(|s| {
        for (radix_prnr, key_chunk, offsets_chunk) in izip!(
            radix_prnrs.iter_mut(),
            inner_key_chunks.into_iter(),
            inner_rel_partition_offsets.chunks_mut()
        ) {
            s.spawn(move |_| {
                radix_prnr
                    .prefix_sum(key_chunk, offsets_chunk)
                    .expect("Failed to run prefix sum");
            })
        }
    });

    // Prefix sum on outer relation
    thread_pool.scope(|s| {
        for (radix_prnr, key_chunk, offsets_chunk) in izip!(
            radix_prnrs.iter_mut(),
            outer_key_chunks.into_iter(),
            outer_rel_partition_offsets.chunks_mut()
        ) {
            s.spawn(move |_| {
                radix_prnr
                    .prefix_sum(key_chunk, offsets_chunk)
                    .expect("Failed to run prefix sum");
            })
        }
    });

    let prefix_sum_time = prefix_sum_timer.elapsed().as_nanos() as f64;
    prefix_sum_range.end();

    let state_malloc_timer = Instant::now();

    let max_inner_partition_len =
        (0..inner_rel_partitions.fanout()).try_fold(0, |max, partition_id| {
            inner_rel_partition_offsets
                .partition_len(partition_id)
                .map(|len| cmp::max(max, len))
        })?;
    let max_outer_partition_len =
        (0..outer_rel_partitions.fanout()).try_fold(0, |max, partition_id| {
            outer_rel_partition_offsets
                .partition_len(partition_id)
                .map(|len| cmp::max(max, len))
        })?;

    // Memory allocations occur asynchronously in parallel to partitioning
    let mut stream_states = iter::repeat_with(|| {
        let stream = Stream::new(StreamFlags::NON_BLOCKING, None)?;
        let mut join_result_sums = unsafe { DeviceBuffer::uninitialized(join_result_sums_len)? };
        cuda_wrapper::memset_async(join_result_sums.as_launchable_mut_slice(), 0, &stream)?;

        Ok(StreamState {
            stream,
            event: Event::new(EventFlags::DEFAULT)?,
            radix_prnr_2nd: GpuRadixPartitioner::new(
                histogram_algorithm_snd,
                partition_algorithm_snd,
                radix_bits.clone(),
                stream_grid_size,
                stream_block_size,
                dmem_buffer_bytes,
            )?,
            radix_join: cuda_radix_join::CudaRadixJoin::new(
                RadixPass::Second,
                radix_bits.clone(),
                hashing_scheme,
                stream_grid_size,
                stream_block_size,
            )?,
            cached_inner_key: Allocator::alloc_mem(
                stream_state_mem_type.clone(),
                max_inner_partition_len,
            ),
            cached_inner_pay: Allocator::alloc_mem(
                stream_state_mem_type.clone(),
                max_inner_partition_len,
            ),
            cached_outer_key: Allocator::alloc_mem(
                stream_state_mem_type.clone(),
                max_outer_partition_len,
            ),
            cached_outer_pay: Allocator::alloc_mem(
                stream_state_mem_type.clone(),
                max_outer_partition_len,
            ),
            inner_rel_partition_offsets_2nd: PartitionOffsets::new(
                histogram_algorithm_snd.into(),
                max_chunks_2nd,
                radix_bits.pass_radix_bits(RadixPass::Second).unwrap(),
                Allocator::mem_alloc_fn(stream_state_mem_type.clone()),
            ),
            outer_rel_partition_offsets_2nd: PartitionOffsets::new(
                histogram_algorithm_snd.into(),
                max_chunks_2nd,
                radix_bits.pass_radix_bits(RadixPass::Second).unwrap(),
                Allocator::mem_alloc_fn(stream_state_mem_type.clone()),
            ),
            inner_rel_partitions_2nd: PartitionedRelation::new(
                max_inner_partition_len,
                histogram_algorithm_snd.into(),
                radix_bits.pass_radix_bits(RadixPass::Second).unwrap(),
                max_chunks_2nd,
                Allocator::mem_alloc_fn(stream_state_mem_type.clone()),
                Allocator::mem_alloc_fn(stream_state_mem_type.clone()),
            ),
            outer_rel_partitions_2nd: PartitionedRelation::new(
                max_outer_partition_len,
                histogram_algorithm_snd.into(),
                radix_bits.pass_radix_bits(RadixPass::Second).unwrap(),
                max_chunks_2nd,
                Allocator::mem_alloc_fn(stream_state_mem_type.clone()),
                Allocator::mem_alloc_fn(stream_state_mem_type.clone()),
            ),
            join_task_assignments: Allocator::alloc_mem(
                stream_state_mem_type.clone(),
                join_dim.0.x as usize + 1,
            ),
            join_result_sums,
        })
    })
    .take(NUM_STREAMS)
    .collect::<Result<Vec<_>>>()?;

    // Populate the pages with mlock(); ideally this would be taken care of by a
    // NUMA-aware malloc implementation
    stream_states
        .iter_mut()
        .try_for_each(|state| state.mlock())?;

    // Get the amount of free GPU memory. The free memory is then used to cache
    // the partitioned relations in GPU memory.
    //
    // In this case, we are trying to capture:
    //  - CUDA context uses several hundred MB memory
    //  - radix_prnr state (e.g., HSSWWC and prefix sum)
    //  - StreamState for all streams
    //
    // Note that CudaMemInfo over-reports how much memory is free. The Linux
    // kernel seems to give a more accurate report (on IBM AC922 with CUDA 10.2).
    let free = CurrentContext::get_device()?
        .numa_node()
        .and_then(|cache_node| {
            linux_wrapper::numa_mem_info(cache_node)
                .map(|linux_wrapper::NumaMemInfo { free, .. }| free)
        })
        .or_else(|_| {
            eprintln!(
            "Warning: Couldn't get free GPU memory from Linux, falling back to CUDA get_mem_info. \
            This method is less accurate and may lead to out-of-memory failures."
            );
            cuda_wrapper::mem_info().map(|cuda_wrapper::CudaMemInfo { free, .. }| free)
        })?;

    let free = free - GPU_MEM_SLACK_BYTES;
    let cache_bytes = max_partitions_cache_bytes.map_or(free, |bytes| {
        if bytes > free {
            eprintln!(
                "Warning: Partitions cache size too large, reducing to maximum available memory"
            );
        }
        cmp::min(bytes, free)
    });

    // Allocate 2nd pass inner relation partitions for overlapping CPU
    // partitioning with transfer and GPU partitioning. The partitions result
    // from the 2nd pass on the GPU.
    let num_cached_inner_partitions = cmp::min(
        radix_bits.pass_fanout(RadixPass::First).unwrap(),
        // FIXME: use the exact length instead of the maximum essentially compute the inclusive
        // prefix sum, and take the maximum that fits into the cache. Or even better: make an
        // actual schedule using the knapsack algorithm
        (cache_bytes
            / (stream_states[0].inner_rel_partitions_2nd.bytes() + GPU_MEM_RELATION_SLACK_BYTES))
            as u32,
    );
    let mut cached_inner_partitions = (0..num_cached_inner_partitions)
        .map(|_| {
            let mut p = PartitionedRelation::<Tuple<T, T>>::new(
                max_inner_partition_len,
                histogram_algorithm_snd.into(),
                radix_bits.pass_radix_bits(RadixPass::Second).unwrap(),
                max_chunks_2nd,
                Allocator::mem_alloc_fn(stream_state_mem_type.clone()),
                Allocator::mem_alloc_fn(stream_state_mem_type.clone()),
            );
            p.mlock()?;
            Ok(p)
        })
        .collect::<Result<Vec<_>>>()?;
    let cached_build_tuples = cached_inner_partitions.iter().map(|p| p.padded_len()).sum();

    let state_malloc_time = state_malloc_timer.elapsed();

    let partition_range = Range::new(cstr!("phase_partition"));
    let partition_timer = Instant::now();

    // Partition inner relation
    let inner_key_chunks = inner_key_slice.input_chunks::<T>(max_chunks_1st)?;
    thread_pool.scope(|s| {
        for (radix_prnr, key_chunk, pay_chunk, offsets_chunk, partitioned_chunk) in izip!(
            radix_prnrs.iter_mut(),
            inner_key_chunks.into_iter(),
            inner_pay_chunks.into_iter(),
            inner_rel_partition_offsets.chunks_mut(),
            inner_rel_partitions.chunks_mut()
        ) {
            s.spawn(move |_| {
                radix_prnr
                    .partition(key_chunk, pay_chunk, offsets_chunk, partitioned_chunk)
                    .expect("Failed to partition the data");
            })
        }
    });

    // Enqueue GPU kernels that copy and 2nd pass partition the inner relation
    // to the cache
    for (partition_id, stream_id, inner_rel_partitions_2nd) in izip!(
        0..num_cached_inner_partitions,
        (0..stream_states.len()).cycle(),
        cached_inner_partitions.iter_mut()
    ) {
        let StreamState {
            stream,
            event,
            radix_prnr_2nd,
            cached_inner_key,
            cached_inner_pay,
            inner_rel_partition_offsets_2nd,
            ..
        } = stream_states
            .get_mut(stream_id)
            .ok_or_else(|| ErrorKind::RuntimeError("Failed to get stream state".into()))?;

        let old_event = mem::replace(event, Event::new(EventFlags::DEFAULT)?);
        stream.wait_event(old_event, StreamWaitEventFlags::DEFAULT)?;

        let inner_partition_len = inner_rel_partitions.partition_len(partition_id)?;
        inner_rel_partitions_2nd.resize(inner_partition_len)?;

        let cached_inner_key_slice = unsafe {
            &mut cached_inner_key.as_launchable_mut_slice().as_mut_slice()[0..inner_partition_len]
        };
        let cached_inner_pay_slice = unsafe {
            &mut cached_inner_pay.as_launchable_mut_slice().as_mut_slice()[0..inner_partition_len]
        };

        radix_prnr_2nd.prefix_sum_and_transform(
            RadixPass::Second,
            partition_id,
            &inner_rel_partitions,
            cached_inner_key_slice.as_launchable_mut_slice(),
            cached_inner_pay_slice.as_launchable_mut_slice(),
            inner_rel_partition_offsets_2nd,
            stream,
        )?;

        radix_prnr_2nd.partition(
            RadixPass::Second,
            cached_inner_key_slice.as_launchable_slice(),
            cached_inner_pay_slice.as_launchable_slice(),
            inner_rel_partition_offsets_2nd,
            inner_rel_partitions_2nd,
            stream,
        )?;

        event.record(stream)?;
    }

    // Partition outer relation on the CPU in parallel to the transfer and 2nd
    // pass of the inner relation on the GPU.
    let outer_key_chunks = outer_key_slice.input_chunks::<T>(max_chunks_1st).unwrap();
    thread_pool.scope(|s| {
        for (radix_prnr, key_chunk, pay_chunk, offsets_chunk, partitioned_chunk) in izip!(
            radix_prnrs.iter_mut(),
            outer_key_chunks.into_iter(),
            outer_pay_chunks.into_iter(),
            outer_rel_partition_offsets.chunks_mut(),
            outer_rel_partitions.chunks_mut()
        ) {
            s.spawn(move |_| {
                radix_prnr
                    .partition(key_chunk, pay_chunk, offsets_chunk, partitioned_chunk)
                    .expect("Failed to partition the data");
            })
        }
    });

    let partition_time = partition_timer.elapsed();
    partition_range.end();

    let join_timer = Instant::now();

    let join_range = Range::new(cstr!("phase_join"));
    let join_start_event = Event::new(EventFlags::DEFAULT)?;
    stream_states
        .iter()
        .take(1)
        .try_for_each(|StreamState { stream, .. }| join_start_event.record(stream))?;

    // Note: Outer relation partitions must be enqueued on the same stream as
    // their respective inner relation partition. Thus, need to match up the
    // stream_state cycle.
    for (partition_id, stream_id, inner_rel_partitions_2nd) in izip!(
        0..num_cached_inner_partitions,
        (0..stream_states.len()).cycle(),
        cached_inner_partitions.iter_mut()
    ) {
        let StreamState {
            stream,
            event,
            radix_prnr_2nd,
            radix_join,
            cached_outer_key,
            cached_outer_pay,
            outer_rel_partition_offsets_2nd,
            outer_rel_partitions_2nd,
            join_task_assignments,
            join_result_sums,
            ..
        } = stream_states
            .get_mut(stream_id)
            .ok_or_else(|| ErrorKind::RuntimeError("Failed to get stream state".into()))?;

        let old_event = mem::replace(event, Event::new(EventFlags::DEFAULT)?);
        stream.wait_event(old_event, StreamWaitEventFlags::DEFAULT)?;

        let inner_partition_len = inner_rel_partitions.partition_len(partition_id)?;
        let outer_partition_len = outer_rel_partitions.partition_len(partition_id)?;

        inner_rel_partitions_2nd.resize(inner_partition_len)?;
        outer_rel_partitions_2nd.resize(outer_partition_len)?;

        let cached_outer_key_slice = unsafe {
            &mut cached_outer_key.as_launchable_mut_slice().as_mut_slice()[0..outer_partition_len]
        };
        let cached_outer_pay_slice = unsafe {
            &mut cached_outer_pay.as_launchable_mut_slice().as_mut_slice()[0..outer_partition_len]
        };

        radix_prnr_2nd.prefix_sum_and_transform(
            RadixPass::Second,
            partition_id,
            &outer_rel_partitions,
            cached_outer_key_slice.as_launchable_mut_slice(),
            cached_outer_pay_slice.as_launchable_mut_slice(),
            outer_rel_partition_offsets_2nd,
            stream,
        )?;

        radix_prnr_2nd.partition(
            RadixPass::Second,
            cached_outer_key_slice.as_launchable_slice(),
            cached_outer_pay_slice.as_launchable_slice(),
            outer_rel_partition_offsets_2nd,
            outer_rel_partitions_2nd,
            stream,
        )?;

        radix_join.join(
            &inner_rel_partitions_2nd,
            &outer_rel_partitions_2nd,
            &mut join_result_sums.as_launchable_mut_slice(),
            &mut join_task_assignments.as_launchable_mut_slice(),
            stream,
        )?;

        event.record(stream)?;
    }

    for (partition_id, stream_id) in izip!(
        (0..radix_bits.pass_fanout(RadixPass::First).unwrap())
            .skip(num_cached_inner_partitions as usize),
        // Skip the first N stream_states to balance the queues and avoid stragglers
        (0..stream_states.len())
            .cycle()
            .skip(num_cached_inner_partitions as usize)
    ) {
        let StreamState {
            stream,
            event,
            radix_prnr_2nd,
            radix_join,
            cached_inner_key,
            cached_inner_pay,
            cached_outer_key,
            cached_outer_pay,
            inner_rel_partition_offsets_2nd,
            outer_rel_partition_offsets_2nd,
            inner_rel_partitions_2nd,
            outer_rel_partitions_2nd,
            join_task_assignments,
            join_result_sums,
        } = stream_states
            .get_mut(stream_id)
            .ok_or_else(|| ErrorKind::RuntimeError("Failed to get stream state".into()))?;

        let old_event = mem::replace(event, Event::new(EventFlags::DEFAULT)?);
        stream.wait_event(old_event, StreamWaitEventFlags::DEFAULT)?;

        let inner_partition_len = inner_rel_partitions.partition_len(partition_id)?;
        let outer_partition_len = outer_rel_partitions.partition_len(partition_id)?;

        inner_rel_partitions_2nd.resize(inner_partition_len)?;
        outer_rel_partitions_2nd.resize(outer_partition_len)?;

        let cached_inner_key_slice = unsafe {
            &mut cached_inner_key.as_launchable_mut_slice().as_mut_slice()[0..inner_partition_len]
        };
        let cached_inner_pay_slice = unsafe {
            &mut cached_inner_pay.as_launchable_mut_slice().as_mut_slice()[0..inner_partition_len]
        };
        let cached_outer_key_slice = unsafe {
            &mut cached_outer_key.as_launchable_mut_slice().as_mut_slice()[0..outer_partition_len]
        };
        let cached_outer_pay_slice = unsafe {
            &mut cached_outer_pay.as_launchable_mut_slice().as_mut_slice()[0..outer_partition_len]
        };

        radix_prnr_2nd.prefix_sum_and_transform(
            RadixPass::Second,
            partition_id,
            &inner_rel_partitions,
            cached_inner_key_slice.as_launchable_mut_slice(),
            cached_inner_pay_slice.as_launchable_mut_slice(),
            inner_rel_partition_offsets_2nd,
            stream,
        )?;

        radix_prnr_2nd.prefix_sum_and_transform(
            RadixPass::Second,
            partition_id,
            &outer_rel_partitions,
            cached_outer_key_slice.as_launchable_mut_slice(),
            cached_outer_pay_slice.as_launchable_mut_slice(),
            outer_rel_partition_offsets_2nd,
            stream,
        )?;

        radix_prnr_2nd.partition(
            RadixPass::Second,
            cached_inner_key_slice.as_launchable_slice(),
            cached_inner_pay_slice.as_launchable_slice(),
            inner_rel_partition_offsets_2nd,
            inner_rel_partitions_2nd,
            stream,
        )?;

        radix_prnr_2nd.partition(
            RadixPass::Second,
            cached_outer_key_slice.as_launchable_slice(),
            cached_outer_pay_slice.as_launchable_slice(),
            outer_rel_partition_offsets_2nd,
            outer_rel_partitions_2nd,
            stream,
        )?;

        radix_join.join(
            &inner_rel_partitions_2nd,
            &outer_rel_partitions_2nd,
            &mut join_result_sums.as_launchable_mut_slice(),
            &mut join_task_assignments.as_launchable_mut_slice(),
            stream,
        )?;

        event.record(stream)?;
    }

    stream_states
        .iter()
        .try_for_each::<_, Result<_>>(|StreamState { stream, .. }| {
            stream.synchronize()?;
            Ok(())
        })?;

    let join_time = join_timer.elapsed();
    join_range.end();

    let mut result_sums_host = vec![0; join_result_sums_len * NUM_STREAMS];
    stream_states
        .into_iter()
        .zip(result_sums_host.chunks_mut(join_result_sums_len))
        .map(
            |(
                StreamState {
                    join_result_sums, ..
                },
                host_sums,
            )| {
                join_result_sums.copy_to(host_sums)?;
                Ok(())
            },
        )
        .collect::<Result<()>>()?;

    let sum = result_sums_host.iter().sum();

    let data_point = RadixJoinPoint {
        prefix_sum_ns: Some(prefix_sum_time),
        partition_ns: Some(partition_time.as_nanos() as f64),
        join_ns: Some(join_time.as_nanos() as f64),
        partitions_malloc_ns: Some(partitions_malloc_time.as_nanos() as f64),
        state_malloc_ns: Some(state_malloc_time.as_nanos() as f64),
        cached_build_tuples: Some(cached_build_tuples),
        cached_probe_tuples: None,
    };

    Ok((sum, data_point))
}