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
// 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 super::{fanout, HistogramAlgorithmType, Tuple};
use crate::error::{ErrorKind, Result};
use numa_gpu::error::Result as NumaGpuResult;
use numa_gpu::runtime::allocator::MemAllocFn;
use numa_gpu::runtime::memory::{LaunchableMem, LaunchableMutSlice, Mem, MemLock};
use rustacuda::memory::DeviceCopy;
use std::convert::TryInto;
use std::mem;
use std::ops::{Index, IndexMut};
use std::slice::ChunksMut;

/// Convert padding bytes into padding length for the type `T`
fn padding_len<T: Sized>() -> u32 {
    crate::constants::PADDING_BYTES / mem::size_of::<T>() as u32
}

/// Partition offsets for an array of chunked partitions.
///
/// The offsets describe a `PartitionedRelation`. The offsets reference
/// partitions within an array. Optionally, each partition includes padding.
///
/// # Layout
///
/// The layout looks as follows (C = chunk, P = partition):
///
/// ```ignore
/// C0.P0 | C0.P1 | ... | C0.PN | C1.P0 | C1.P1 | ... | C1.PN | ... | CM.PN
/// ```
///
/// Note that no restrictions are placed on the data layout (i.e., offsets may
/// be numerically unordered).
///
/// # `offsets` vs. `local_offsets`
///
/// Chunked prefix sums inherently have local offsets for each chunk. In
/// contrast, contiguous histograms only have one set of "global" offsets.
///
/// The partitioning operator requires a set of local offsets for each chunk.
/// Thus, the contiguous prefix sum variants output `local_offsets` in addition
/// to the normal `offsets`.
///
/// # Invariants
///
///  - `len` must match in `PartitionedRelation`
///  - `radix_bits` must match in `GpuRadixPartitioner`
///  - `max_chunks` must equal the maximum number of chunks computed at runtime
///     (e.g., the grid size)
#[derive(Debug)]
pub struct PartitionOffsets<T: DeviceCopy> {
    pub(super) offsets: Mem<u64>,
    pub(super) local_offsets: Option<Mem<u64>>,
    data_len: Option<usize>,
    chunks: u32,
    radix_bits: u32,
    phantom_data: std::marker::PhantomData<T>,
}

impl<T: DeviceCopy> PartitionOffsets<T> {
    /// Creates a new partition offsets array.
    pub fn new(
        histogram_algorithm_type: HistogramAlgorithmType,
        max_chunks: u32,
        radix_bits: u32,
        alloc_fn: MemAllocFn<u64>,
    ) -> Self {
        let chunks: u32 = match histogram_algorithm_type {
            HistogramAlgorithmType::Chunked => max_chunks,
            HistogramAlgorithmType::Contiguous => 1,
        };

        let num_partitions = fanout(radix_bits) as usize;
        let offsets = alloc_fn(num_partitions * chunks as usize);

        let local_offsets = match histogram_algorithm_type {
            HistogramAlgorithmType::Contiguous => {
                Some(alloc_fn(num_partitions * max_chunks as usize))
            }
            _ => None,
        };

        Self {
            offsets,
            local_offsets,
            data_len: None,
            chunks,
            radix_bits,
            phantom_data: std::marker::PhantomData,
        }
    }

    /// Returns the total number of elements in the relation (excluding padding).
    ///
    /// If the prefix sum is not yet computed, then `None` is returned.
    pub fn len(&self) -> Option<usize> {
        self.data_len
    }

    /// Sets the data length of PartitionOffsets
    ///
    /// The length must be set by the prefix sum function.
    pub(super) fn set_data_len(&mut self, len: usize) {
        self.data_len = Some(len);
    }

    /// Returns the total number of elements in the relation including padding.
    ///
    /// If the prefix sum is not yet computed, then `None` is returned.
    pub(super) fn padded_len(&self) -> Option<usize> {
        let num_partitions = fanout(self.radix_bits) as usize;
        self.data_len.map(|len| {
            len + num_partitions * self.num_chunks() as usize * self.padding_len() as usize
        })
    }

    /// Returns the total number of bytes used to store the offsets.
    pub fn bytes(&self) -> usize {
        (self.offsets.len() + self.local_offsets.as_ref().map_or(0, |o| o.len()))
            * mem::size_of::<u64>()
    }

    /// Returs the number of chunks.
    pub fn num_chunks(&self) -> u32 {
        self.chunks
    }

    /// Returns an iterator over the chunks contained inside the offsets.
    ///
    /// Chunks are non-overlapping and can safely be used for parallel
    /// processing.
    pub fn chunks_mut(&mut self) -> PartitionOffsetsChunksMut<'_, T> {
        PartitionOffsetsChunksMut::new(self)
    }

    /// Returns the number of partitions.
    pub fn fanout(&self) -> u32 {
        fanout(self.radix_bits)
    }

    /// Returns the number of radix bits.
    pub fn radix_bits(&self) -> u32 {
        self.radix_bits
    }

    /// Returns the length of the requested partition.
    ///
    /// If the offsets are accessible by the CPU (i.e., in DerefMem), then the
    /// length is returned. Otherwise, the function returns an error.
    pub fn partition_len(&self, partition_id: u32) -> Result<usize> {
        let fanout = self.fanout();
        if partition_id >= fanout {
            Err(ErrorKind::InvalidArgument(
                "Invalid partition ID".to_string(),
            ))?;
        }

        let offsets: &[u64] = match (&self.offsets).try_into() {
            Ok(offsets) => offsets,
            _ => Err(ErrorKind::RuntimeError(
                "Trying to dereference device memory!".to_string(),
            ))?,
        };
        let padding_len = self.padding_len() as usize;
        let padded_len = self
            .padded_len()
            .ok_or_else(|| ErrorKind::RuntimeError("Data length not yet computed".to_string()))?;

        let len = (0..self.chunks)
            .map(|chunk_id| {
                let ofi = chunk_id as usize * fanout as usize + partition_id as usize;
                let begin = offsets[ofi] as usize;
                let end = if ofi + 1 < offsets.len() {
                    offsets[ofi + 1] as usize - padding_len
                } else {
                    padded_len
                };
                end - begin
            })
            .sum();

        Ok(len)
    }

    /// Returns the number of padding elements per partition.
    pub(super) fn padding_len(&self) -> u32 {
        padding_len::<T>()
    }
}

impl<T: DeviceCopy> MemLock for PartitionOffsets<T> {
    fn mlock(&mut self) -> NumaGpuResult<()> {
        self.offsets.mlock()?;
        if let Some(ref mut o) = self.local_offsets {
            o.mlock()?;
        }

        Ok(())
    }

    fn munlock(&mut self) -> NumaGpuResult<()> {
        self.offsets.munlock()?;
        if let Some(ref mut o) = self.local_offsets {
            o.munlock()?;
        }

        Ok(())
    }
}

/// An iterator that generates `PartitionOffsetsMutSlice`.
#[derive(Debug)]
pub struct PartitionOffsetsChunksMut<'a, T: DeviceCopy> {
    // Note: unsafe slices, must convert back to LaunchableSlice
    offsets_chunks: ChunksMut<'a, u64>,
    data_len: Option<&'a mut Option<usize>>,
    chunk_id: u32,
    chunks: u32,
    radix_bits: u32,
    phantom_data: std::marker::PhantomData<T>,
}

impl<'a, T: DeviceCopy> PartitionOffsetsChunksMut<'a, T> {
    fn new(offsets: &'a mut PartitionOffsets<T>) -> Self {
        unsafe {
            let num_partitions = fanout(offsets.radix_bits) as usize;
            let offsets_chunks = offsets
                .offsets
                .as_launchable_mut_slice()
                .as_mut_slice()
                .chunks_mut(num_partitions);

            Self {
                offsets_chunks,
                data_len: Some(&mut offsets.data_len),
                chunk_id: 0,
                chunks: offsets.chunks,
                radix_bits: offsets.radix_bits,
                phantom_data: std::marker::PhantomData,
            }
        }
    }
}

impl<'a, T: DeviceCopy> Iterator for PartitionOffsetsChunksMut<'a, T> {
    type Item = PartitionOffsetsMutSlice<'a, T>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.offsets_chunks.next().and_then(|o| {
            let chunk_id = self.chunk_id;
            self.chunk_id = self.chunk_id + 1;

            Some(PartitionOffsetsMutSlice {
                offsets: o.as_launchable_mut_slice(),
                data_len: self.data_len.take(),
                chunk_id,
                chunks: self.chunks,
                radix_bits: self.radix_bits,
                phantom_data: std::marker::PhantomData,
            })
        })
    }
}

/// A mutable slice that references the `PartitionOffsets` of one partition.
///
/// Effectively a mutable slice containing additional metadata about the chunk.
/// The purpose is to allow thread-safe writes to `PartitionOffsets`.
#[derive(Debug)]
pub struct PartitionOffsetsMutSlice<'a, T: DeviceCopy> {
    // FIXME: convert to normal slice, and check that not DevMem in chunks_mut()
    pub(super) offsets: LaunchableMutSlice<'a, u64>,
    pub(super) chunk_id: u32,
    pub(super) chunks: u32,
    pub(super) radix_bits: u32,
    data_len: Option<&'a mut Option<usize>>,
    phantom_data: std::marker::PhantomData<T>,
}

impl<'a, T: DeviceCopy> PartitionOffsetsMutSlice<'a, T> {
    /// Returns the number of padding elements per partition.
    pub(super) fn padding_len(&self) -> u32 {
        padding_len::<T>()
    }

    /// Sets the total data length of PartitionOffsets
    ///
    /// The length must be set by the prefix sum function. `set_data_len` must
    /// be called on all chunks,  as only one chunk has the mutable reference.
    pub(super) fn set_data_len(&mut self, len: usize) {
        if let Some(ref mut this_len) = self.data_len {
            **this_len = Some(len);
        }
    }
}

/// A radix-partitioned relation, optionally with padding in front of each
/// partition.
///
/// The relation supports chunking on a single GPU. E.g. in the `Chunked`
/// algorithm, there is a chunk per thread block. In this case, `chunks` should
/// equal the grid size.
///
/// # Invariants
///
///  - `len` must match in `PartitionOffsets`
///  - `radix_bits` must match in `GpuRadixPartitioner`.
///  - `max_chunks` must equal the maximum number of chunks computed at runtime
///     (e.g., the grid size).
#[derive(Debug)]
pub struct PartitionedRelation<T: DeviceCopy> {
    pub relation: Mem<T>,
    pub offsets: Mem<u64>,
    len: usize,
    chunks: u32,
    radix_bits: u32,
}

impl<T: DeviceCopy> PartitionedRelation<T> {
    /// Creates a new partitioned relation, and automatically includes the
    /// necessary padding and metadata.
    pub fn new(
        len: usize,
        histogram_algorithm_type: HistogramAlgorithmType,
        radix_bits: u32,
        max_chunks: u32,
        partition_alloc_fn: MemAllocFn<T>,
        offsets_alloc_fn: MemAllocFn<u64>,
    ) -> Self {
        let chunks: u32 = match histogram_algorithm_type {
            HistogramAlgorithmType::Chunked => max_chunks,
            HistogramAlgorithmType::Contiguous => 1,
        };

        let padding_len = padding_len::<T>();
        let num_partitions = fanout(radix_bits) as usize;
        let relation_len = len + (num_partitions * chunks as usize) * padding_len as usize;

        let relation = partition_alloc_fn(relation_len);
        let offsets = offsets_alloc_fn(num_partitions * chunks as usize);

        Self {
            relation,
            offsets,
            chunks,
            radix_bits,
            len,
        }
    }

    /// Returns the total number of elements in the relation (excluding padding).
    pub fn len(&self) -> usize {
        self.len
    }

    /// Returns the total number of elements in the relation including padding.
    pub fn padded_len(&self) -> usize {
        let num_partitions = fanout(self.radix_bits) as usize;
        self.len + num_partitions * self.num_chunks() as usize * self.padding_len() as usize
    }

    /// Returns the total number of bytes used to store the relation (including metadata).
    pub fn bytes(&self) -> usize {
        self.relation.len() * mem::size_of::<T>() + self.offsets.len() * mem::size_of::<u64>()
    }

    /// Returns the number of elements allocated in memory (excluding padding).
    ///
    /// The capacity includes unused elements, but excludes padding.
    pub fn capacity(&self) -> usize {
        let num_partitions = fanout(self.radix_bits) as usize;
        self.relation.len()
            - num_partitions * self.num_chunks() as usize * self.padding_len() as usize
    }

    /// Resizes the `PartitionedRelation` in-place so that `len` is equal to `new_len`.
    ///
    /// If `new_len` is greater than the allocated memory capacity, then the
    /// resize will abort and return `Err`.
    ///
    /// ## Post-conditions
    ///
    /// - The resize invalidates all data contained in `relation` and the `offsets`.
    /// - However, an aborted resize leaves `PartitionedRelation` intact and unmodified.
    pub fn resize(&mut self, new_len: usize) -> Result<()> {
        if new_len <= self.capacity() {
            self.len = new_len;
            Ok(())
        } else {
            Err(ErrorKind::InvalidArgument(
                "Insufficient capacity to resize to new length".to_string(),
            )
            .into())
        }
    }

    /// Returs the number of chunks.
    pub fn num_chunks(&self) -> u32 {
        self.chunks
    }

    /// Returns the number of partitions.
    pub fn fanout(&self) -> u32 {
        fanout(self.radix_bits)
    }

    /// Returns the number of radix bits.
    pub fn radix_bits(&self) -> u32 {
        self.radix_bits
    }

    /// Returns the length of the requested partition.
    ///
    /// If the offsets are accessible by the CPU (i.e., in DerefMem), then the
    /// length is returned. Otherwise, the function returns an error.
    pub fn partition_len(&self, partition_id: u32) -> Result<usize> {
        let fanout = self.fanout();
        if partition_id >= fanout {
            Err(ErrorKind::InvalidArgument(
                "Invalid partition ID".to_string(),
            ))?;
        }

        let offsets: &[u64] = match (&self.offsets).try_into() {
            Ok(offsets) => offsets,
            _ => Err(ErrorKind::RuntimeError(
                "Trying to dereference device memory!".to_string(),
            ))?,
        };
        let padding_len = self.padding_len() as usize;

        let len = (0..self.chunks)
            .map(|chunk_id| {
                let ofi = chunk_id as usize * fanout as usize + partition_id as usize;
                let begin = offsets[ofi] as usize;
                let end = if ofi + 1 < offsets.len() {
                    offsets[ofi + 1] as usize - padding_len
                } else {
                    self.padded_len()
                };
                end - begin
            })
            .sum();

        Ok(len)
    }

    /// Returns the number of padding elements per partition.
    pub fn padding_len(&self) -> u32 {
        padding_len::<T>()
    }

    /// Returns the internal representation of the relation data as a slice.
    ///
    /// This function is intended for unit testing. Use the methods provided by
    /// the `Index` trait or `chunks_mut()` instead if possible.
    ///
    /// The function is unsafe because:
    /// - the internal representation may change
    /// - padding may contain uninitialized memory.
    pub unsafe fn as_raw_relation_slice(&self) -> Result<&[T]> {
        let relation: &[_] = (&self.relation).try_into().map_err(|_| {
            ErrorKind::Msg("Tried to convert device memory into host slice".to_string())
        })?;

        Ok(relation)
    }

    /// Returns the internal representation of the relation data as a mutable slice.
    ///
    /// This function is intended for unit testing. Use the methods provided by
    /// the `Index` trait or `chunks_mut()` instead if possible.
    ///
    /// The function is unsafe because:
    /// - the internal representation may change
    /// - padding may contain uninitialized memory.
    pub unsafe fn as_raw_relation_mut_slice(&mut self) -> Result<&mut [T]> {
        let relation: &mut [_] = (&mut self.relation).try_into().map_err(|_| {
            ErrorKind::Msg("Tried to convert device memory into host slice".to_string())
        })?;

        Ok(relation)
    }
}

impl<K: DeviceCopy, V: DeviceCopy> PartitionedRelation<Tuple<K, V>> {
    /// Returns an iterator over the chunks contained inside the relation.
    ///
    /// Chunks are non-overlapping and can safely be used for parallel
    /// processing.
    pub fn chunks_mut(&mut self) -> PartitionedRelationChunksMut<'_, Tuple<K, V>> {
        PartitionedRelationChunksMut::new(self)
    }
}

/// Returns the specified chunk and partition as a subslice of the relation.
impl<T: DeviceCopy> Index<(u32, u32)> for PartitionedRelation<T> {
    type Output = [T];

    fn index(&self, (chunk_id, partition_id): (u32, u32)) -> &Self::Output {
        let fanout = self.fanout();
        if partition_id >= fanout {
            panic!("Invalid partition ID");
        }
        if chunk_id >= self.chunks {
            panic!("Invalid chunk ID");
        }

        let (offsets, relation): (&[u64], &[T]) =
            match ((&self.offsets).try_into(), (&self.relation).try_into()) {
                (Ok(offsets), Ok(relation)) => (offsets, relation),
                _ => panic!("Trying to dereference device memory!"),
            };

        let ofi = (chunk_id * fanout + partition_id) as usize;
        let begin = offsets[ofi] as usize;
        let end = if ofi + 1 < self.offsets.len() {
            offsets[ofi + 1] as usize - self.padding_len() as usize
        } else {
            self.padded_len()
        };

        &relation[begin..end]
    }
}

/// Returns the specified chunk and partition as a mutable subslice of the
/// relation.
impl<T: DeviceCopy> IndexMut<(u32, u32)> for PartitionedRelation<T> {
    fn index_mut(&mut self, (chunk_id, partition_id): (u32, u32)) -> &mut Self::Output {
        let padded_len = self.padded_len();
        let padding_len = self.padding_len();
        let offsets_len = self.offsets.len();
        let partitions = self.fanout();

        let (offsets, relation): (&mut [u64], &mut [T]) = match (
            (&mut self.offsets).try_into(),
            (&mut self.relation).try_into(),
        ) {
            (Ok(offsets), Ok(relation)) => (offsets, relation),
            _ => panic!("Trying to dereference device memory!"),
        };

        let ofi = (chunk_id * partitions + partition_id) as usize;
        let begin = offsets[ofi] as usize;
        let end = if ofi + 1 < offsets_len {
            offsets[ofi + 1] as usize - padding_len as usize
        } else {
            padded_len
        };

        &mut relation[begin..end]
    }
}

impl<T: DeviceCopy> MemLock for PartitionedRelation<T> {
    fn mlock(&mut self) -> NumaGpuResult<()> {
        self.relation.mlock()?;
        self.offsets.mlock()?;

        Ok(())
    }

    fn munlock(&mut self) -> NumaGpuResult<()> {
        self.relation.munlock()?;
        self.offsets.munlock()?;

        Ok(())
    }
}

/// An iterator that generates `PartitionedRelationMutSlice`.
#[derive(Debug)]
pub struct PartitionedRelationChunksMut<'a, T: DeviceCopy> {
    relation_remainder: Option<&'a mut [T]>,
    canonical_chunk_len: usize,
    offsets_chunks: ChunksMut<'a, u64>,
    chunks: u32,
    radix_bits: u32,
}

impl<'a, K: DeviceCopy, V: DeviceCopy> PartitionedRelationChunksMut<'a, Tuple<K, V>> {
    /// Creates a new chunk iterator for a `PartitionedRelation`.
    fn new(rel: &'a mut PartitionedRelation<Tuple<K, V>>) -> Self {
        let canonical_chunk_len =
            super::partition_input_chunk::input_chunk_size::<K>(rel.len(), rel.num_chunks())
                .unwrap()
                + rel.fanout() as usize * rel.padding_len() as usize;
        let off_chunk_size = rel.offsets.len() / rel.num_chunks() as usize;

        unsafe {
            let relation_remainder = Some(rel.relation.as_launchable_mut_slice().as_mut_slice());
            let offsets_chunks = rel
                .offsets
                .as_launchable_mut_slice()
                .as_mut_slice()
                .chunks_mut(off_chunk_size);

            Self {
                relation_remainder,
                canonical_chunk_len,
                offsets_chunks,
                chunks: rel.chunks,
                radix_bits: rel.radix_bits,
            }
        }
    }
}

impl<'a, T: DeviceCopy> Iterator for PartitionedRelationChunksMut<'a, T> {
    type Item = PartitionedRelationMutSlice<'a, T>;

    #[inline]
    fn next(&mut self) -> Option<PartitionedRelationMutSlice<'a, T>> {
        let chunk = if let Some(remainder) = self.relation_remainder.take() {
            let mid = std::cmp::min(self.canonical_chunk_len, remainder.len());
            let (c, r) = remainder.split_at_mut(mid);
            self.relation_remainder = if r.len() == 0 { None } else { Some(r) };
            Some(c)
        } else {
            None
        };

        let zipped =
            chunk.and_then(|rel| self.offsets_chunks.next().and_then(|off| Some((rel, off))));
        zipped.and_then(|(r, o)| {
            Some(PartitionedRelationMutSlice {
                relation: r.as_launchable_mut_slice(),
                offsets: o.as_launchable_mut_slice(),
                chunks: self.chunks,
                radix_bits: self.radix_bits,
            })
        })
    }
}

/// A mutable slice that references part of a `PartitionedRelation`.
///
/// Effectively a mutable slice containing additional metadata about the chunk.
/// The purpose is to allow thread-safe writes to a `PartitionedRelation`.
#[derive(Debug)]
pub struct PartitionedRelationMutSlice<'a, T> {
    // FIXME: convert to normal slice, and check that not DevMem in chunks_mut()
    pub(super) relation: LaunchableMutSlice<'a, T>,
    pub(super) offsets: LaunchableMutSlice<'a, u64>,
    pub(super) chunks: u32,
    pub(super) radix_bits: u32,
}

impl<'a, T: DeviceCopy> PartitionedRelationMutSlice<'a, T> {
    /// Returns the number of padding elements per partition.
    pub(super) fn padding_len(&self) -> u32 {
        padding_len::<T>()
    }
}