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
// Copyright 2019-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.

//! A collection of partitioning operators.

use crate::error::{Error, ErrorKind};
use rustacuda::memory::DeviceCopy;
use std::convert::TryFrom;

pub mod cpu_radix_partition;
pub mod gpu_radix_partition;
mod partition_input_chunk;
mod partitioned_relation;

// Export structs
pub use partition_input_chunk::{RadixPartitionInputChunk, RadixPartitionInputChunkable};
pub use partitioned_relation::{
    PartitionOffsets, PartitionOffsetsChunksMut, PartitionOffsetsMutSlice, PartitionedRelation,
    PartitionedRelationChunksMut, PartitionedRelationMutSlice,
};

/// Histogram algorithm type
#[derive(Copy, Clone, Debug)]
pub enum HistogramAlgorithmType {
    /// A class of algorithms that split partitions over multiple chunks.
    ///
    /// Typically, each chunk is the result of a thread.
    Chunked,

    /// A class of algorithms that output contiguous partitions.
    ///
    /// The result is one single, contiguous sequence of partitions.  Thus, in multi-threaded
    /// implementations, threads cooperate to make the output contiguous.
    Contiguous,
}

/// Compute the fanout (i.e., the number of partitions) from the number of radix
/// bits.
fn fanout(radix_bits: u32) -> u32 {
    1 << radix_bits
}

/// A radix pass
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum RadixPass {
    First = 0,
    Second,
    Third,
}

/// The number of radix bits per pass.
#[derive(Copy, Clone, Debug)]
pub struct RadixBits {
    pass_bits: [Option<u32>; 3],
}

impl RadixBits {
    /// Returns a new `RadixBits` object
    pub fn new(first_bits: Option<u32>, second_bits: Option<u32>, third_bits: Option<u32>) -> Self {
        let pass_bits = [first_bits, second_bits, third_bits];

        Self { pass_bits }
    }

    /// Total number of radix bits.
    pub fn radix_bits(&self) -> u32 {
        self.pass_bits
            .iter()
            .fold(0_u32, |sum, item| sum + item.unwrap_or(0))
    }

    /// Total fanout.
    pub fn fanout(&self) -> u32 {
        fanout(self.radix_bits())
    }

    /// Number of radix bits per pass.
    pub fn pass_radix_bits(&self, pass: RadixPass) -> Option<u32> {
        self.pass_bits[pass as usize]
    }

    /// Fanout per pass.
    pub fn pass_fanout(&self, pass: RadixPass) -> Option<u32> {
        let radix_bits = self.pass_bits[pass as usize];
        radix_bits.map(|bits| fanout(bits))
    }

    /// Number of radix bits by all earlier passes.
    ///
    /// For example, the first pass has 6 radix bits and the second pass has 6
    /// radix bits. Then `pass_ignore_bits(First)` equals 0, and
    /// `pass_ignore_bits(Second)` equals 6.
    pub fn pass_ignore_bits(&self, pass: RadixPass) -> u32 {
        self.pass_bits
            .iter()
            .take(pass as usize)
            .fold(0_u32, |sum, item| sum + item.unwrap_or(0))
    }
}

impl From<u32> for RadixBits {
    fn from(first_bits: u32) -> Self {
        Self::new(Some(first_bits), None, None)
    }
}

impl TryFrom<&[u32]> for RadixBits {
    type Error = Error;

    fn try_from(v: &[u32]) -> Result<Self, Self::Error> {
        if v.len() == 0 || v.len() > 3 {
            Err(ErrorKind::InvalidArgument(
                "At least one and at most three sets of radix bits required".to_string(),
            ))?;
        }

        Ok(Self::new(
            v.get(0).copied(),
            v.get(1).copied(),
            v.get(2).copied(),
        ))
    }
}

/// A key-value tuple.
///
/// The partitioned relation is stored as a collection of `Tuple<K, V>`.
///
/// Note that the struct's layout must be kept in sync with its counterpart in
/// C/C++.
#[derive(Copy, Clone, Default, Debug, Eq, PartialEq)]
#[repr(C)]
pub struct Tuple<Key: Sized, Value: Sized> {
    pub key: Key,
    pub value: Value,
}

unsafe impl<K, V> DeviceCopy for Tuple<K, V>
where
    K: DeviceCopy,
    V: DeviceCopy,
{
}