use super::super::{CudaClient, CudaRuntime};
use super::indices::{check_merge_dtype, indices_to_i32, indices_to_i64};
use crate::algorithm::sparse::validate_sparse_operands;
use crate::dtype::{DType, Element};
use crate::error::{Error, Result};
use crate::runtime::cuda::kernels::{
CompressedPtrs, SparseFormat, SparseMergeOp, exclusive_scan_i32_gpu,
launch_compressed_merge_compute, launch_compressed_merge_count,
};
use crate::tensor::Tensor;
pub(super) type SparseParts<'a> = (
&'a Tensor<CudaRuntime>,
&'a Tensor<CudaRuntime>,
&'a Tensor<CudaRuntime>,
);
pub(super) type SparseTriple = (
Tensor<CudaRuntime>,
Tensor<CudaRuntime>,
Tensor<CudaRuntime>,
);
impl CudaClient {
pub(super) fn compressed_merge<T: Element>(
&self,
format: SparseFormat,
op: SparseMergeOp,
a: SparseParts<'_>,
b: SparseParts<'_>,
n: usize,
) -> Result<SparseTriple> {
let name = merge_name(format, op);
let dtype = a.2.dtype();
check_merge_dtype(dtype, name)?;
validate_sparse_operands::<T>(&[], &[dtype, b.2.dtype()])?;
if op == SparseMergeOp::Div && b.2.numel() == 0 && a.2.numel() > 0 {
return Err(Error::Internal(format!(
"{name}: B stores no entries, so each of the {} stored A entries divides by zero",
a.2.numel()
)));
}
let device = a.2.device();
let a_ptrs = indices_to_i32(self, a.0, name)?;
let a_indices = indices_to_i32(self, a.1, name)?;
let a_values = a.2.contiguous()?;
let b_ptrs = indices_to_i32(self, b.0, name)?;
let b_indices = indices_to_i32(self, b.1, name)?;
let b_values = b.2.contiguous()?;
let a_dev = CompressedPtrs {
ptrs: a_ptrs.ptr(),
indices: a_indices.ptr(),
values: a_values.ptr(),
};
let b_dev = CompressedPtrs {
ptrs: b_ptrs.ptr(),
indices: b_indices.ptr(),
values: b_values.ptr(),
};
let counts = Tensor::<CudaRuntime>::zeros(&[n], DType::I32, device)?;
if n > 0 {
unsafe {
launch_compressed_merge_count(
&self.context,
&self.stream,
self.device.index,
format,
op,
&a_dev,
&b_dev,
counts.ptr(),
n,
)?;
}
}
let (out_ptrs, total) = if n == 0 {
(Tensor::<CudaRuntime>::zeros(&[1], DType::I32, device)?, 0)
} else {
unsafe {
exclusive_scan_i32_gpu(
&self.context,
&self.stream,
self.device.index,
device,
&counts,
)?
}
};
let out_indices = Tensor::<CudaRuntime>::zeros(&[total], DType::I32, device)?;
let out_values = Tensor::<CudaRuntime>::zeros(&[total], dtype, device)?;
if total > 0 {
unsafe {
launch_compressed_merge_compute(
&self.context,
&self.stream,
self.device.index,
format,
op,
dtype,
&a_dev,
&b_dev,
out_ptrs.ptr(),
out_indices.ptr(),
out_values.ptr(),
n,
)?;
}
}
Ok((
indices_to_i64(self, &out_ptrs)?,
indices_to_i64(self, &out_indices)?,
out_values,
))
}
pub(super) fn coo_merge<T: Element>(
&self,
op: SparseMergeOp,
a: SparseParts<'_>,
b: SparseParts<'_>,
shape: [usize; 2],
) -> Result<SparseTriple> {
let (a_ptrs, a_cols, a_values) = self.coo_to_csr_impl::<T>(a.0, a.1, a.2, shape)?;
let (b_ptrs, b_cols, b_values) = self.coo_to_csr_impl::<T>(b.0, b.1, b.2, shape)?;
let (ptrs, cols, values) = self.compressed_merge::<T>(
SparseFormat::Csr,
op,
(&a_ptrs, &a_cols, &a_values),
(&b_ptrs, &b_cols, &b_values),
shape[0],
)?;
self.csr_to_coo_impl::<T>(&ptrs, &cols, &values, shape)
}
}
fn merge_name(format: SparseFormat, op: SparseMergeOp) -> &'static str {
match (format, op) {
(SparseFormat::Csr, SparseMergeOp::Add) => "add_csr (CUDA)",
(SparseFormat::Csr, SparseMergeOp::Sub) => "sub_csr (CUDA)",
(SparseFormat::Csr, SparseMergeOp::Mul) => "mul_csr (CUDA)",
(SparseFormat::Csr, SparseMergeOp::Div) => "div_csr (CUDA)",
(SparseFormat::Csc, SparseMergeOp::Add) => "add_csc (CUDA)",
(SparseFormat::Csc, SparseMergeOp::Sub) => "sub_csc (CUDA)",
(SparseFormat::Csc, SparseMergeOp::Mul) => "mul_csc (CUDA)",
(SparseFormat::Csc, SparseMergeOp::Div) => "div_csc (CUDA)",
(SparseFormat::Coo, _) => "sparse merge (CUDA)",
}
}