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
//! Operation and format selectors for the CUDA sparse merge kernels.
/// Element-wise operation of a sparse merge.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SparseMergeOp {
/// `a + b` over the union of both patterns.
Add,
/// `a - b` over the union of both patterns.
Sub,
/// `a * b` over the intersection of both patterns.
Mul,
/// `a / b` over the intersection of both patterns.
Div,
}
impl SparseMergeOp {
/// The operation name inside the kernel names: `add`, `sub`, `mul`, `div`.
pub fn kernel_prefix(&self) -> &'static str {
match self {
SparseMergeOp::Add => "add",
SparseMergeOp::Sub => "sub",
SparseMergeOp::Mul => "mul",
SparseMergeOp::Div => "div",
}
}
/// Whether the output pattern is the union of both patterns (add, sub).
pub fn is_union(&self) -> bool {
matches!(self, SparseMergeOp::Add | SparseMergeOp::Sub)
}
/// Whether the output pattern is the intersection of both patterns (mul, div).
pub fn is_intersection(&self) -> bool {
!self.is_union()
}
}
/// Sparse storage format of a merge operand.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SparseFormat {
/// Row pointers + column indices.
Csr,
/// Column pointers + row indices.
Csc,
/// Row indices + column indices.
Coo,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn union_and_intersection_split_the_ops() {
assert!(SparseMergeOp::Add.is_union());
assert!(SparseMergeOp::Sub.is_union());
assert!(SparseMergeOp::Mul.is_intersection());
assert!(SparseMergeOp::Div.is_intersection());
assert_eq!(SparseMergeOp::Div.kernel_prefix(), "div");
}
}