use cudarc::driver::safe::CudaContext;
use std::sync::Arc;
use super::super::sparse_strategy::{SparseFormat, SparseMergeOp};
use super::helpers::{launch_count_kernel, launch_csc_compute_kernel, launch_csr_compute_kernel};
use crate::dtype::DType;
use crate::error::{Error, Result};
use crate::runtime::cuda::GuardedStream;
#[derive(Debug, Clone, Copy)]
pub struct CompressedPtrs {
pub ptrs: u64,
pub indices: u64,
pub values: u64,
}
fn count_kernel(format: SparseFormat, op: SparseMergeOp) -> Result<&'static str> {
match (format, op.is_union()) {
(SparseFormat::Csr, true) => Ok("csr_merge_count"),
(SparseFormat::Csr, false) => Ok("csr_mul_count"),
(SparseFormat::Csc, true) => Ok("csc_merge_count"),
(SparseFormat::Csc, false) => Ok("csc_intersect_count"),
(SparseFormat::Coo, _) => Err(Error::Internal(
"sparse merge kernels take CSR or CSC operands, not COO".to_string(),
)),
}
}
pub unsafe fn launch_compressed_merge_count(
context: &Arc<CudaContext>,
stream: &GuardedStream,
device_index: usize,
format: SparseFormat,
op: SparseMergeOp,
a: &CompressedPtrs,
b: &CompressedPtrs,
counts: u64,
n: usize,
) -> Result<()> {
let kernel = count_kernel(format, op)?;
unsafe {
launch_count_kernel(
context,
stream,
device_index,
kernel,
a.ptrs,
a.indices,
b.ptrs,
b.indices,
counts,
n,
kernel,
)
}
}
pub unsafe fn launch_compressed_merge_compute(
context: &Arc<CudaContext>,
stream: &GuardedStream,
device_index: usize,
format: SparseFormat,
op: SparseMergeOp,
dtype: DType,
a: &CompressedPtrs,
b: &CompressedPtrs,
out_ptrs: u64,
out_indices: u64,
out_values: u64,
n: usize,
) -> Result<()> {
let base = match format {
SparseFormat::Csr => format!("csr_{}_compute", op.kernel_prefix()),
SparseFormat::Csc => format!("csc_{}_compute", op.kernel_prefix()),
SparseFormat::Coo => {
return Err(Error::Internal(
"sparse merge kernels take CSR or CSC operands, not COO".to_string(),
));
}
};
let launch = if format == SparseFormat::Csr {
launch_csr_compute_kernel
} else {
launch_csc_compute_kernel
};
unsafe {
launch(
context,
stream,
device_index,
&base,
dtype,
a.ptrs,
a.indices,
a.values,
b.ptrs,
b.indices,
b.values,
out_ptrs,
out_indices,
out_values,
n,
&base,
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn count_kernels_match_the_cu_names() {
assert_eq!(
count_kernel(SparseFormat::Csr, SparseMergeOp::Sub).unwrap(),
"csr_merge_count"
);
assert_eq!(
count_kernel(SparseFormat::Csr, SparseMergeOp::Div).unwrap(),
"csr_mul_count"
);
assert_eq!(
count_kernel(SparseFormat::Csc, SparseMergeOp::Add).unwrap(),
"csc_merge_count"
);
assert_eq!(
count_kernel(SparseFormat::Csc, SparseMergeOp::Mul).unwrap(),
"csc_intersect_count"
);
assert!(count_kernel(SparseFormat::Coo, SparseMergeOp::Add).is_err());
}
}