Skip to main content

ringkernel_graph/
lib.rs

1//! GPU-accelerated graph algorithm primitives for RingKernel.
2//!
3//! This crate provides high-performance graph algorithms optimized for
4//! parallel execution on GPUs. It includes:
5//!
6//! - **CSR Matrix**: Compressed Sparse Row format for efficient graph storage
7//! - **BFS**: Parallel breadth-first search with multiple source support
8//! - **SCC**: Strongly connected components via forward-backward algorithm
9//! - **Union-Find**: Parallel disjoint set data structure
10//! - **SpMV**: Sparse matrix-vector multiplication
11//!
12//! # Example
13//!
14//! ```ignore
15//! use ringkernel_graph::{CsrMatrix, bfs_parallel, NodeId};
16//!
17//! // Build adjacency list: 0 -> 1 -> 2
18//! let matrix = CsrMatrix::from_edges(3, &[(0, 1), (1, 2)]);
19//!
20//! // Run BFS from node 0
21//! let distances = bfs_parallel(&matrix, &[NodeId(0)]).await?;
22//! assert_eq!(distances[0].0, 0);  // Distance to self
23//! assert_eq!(distances[1].0, 1);  // Distance to node 1
24//! assert_eq!(distances[2].0, 2);  // Distance to node 2
25//! ```
26
27pub mod algorithms;
28pub mod models;
29
30/// GPU-accelerated implementations (requires `cuda` feature).
31#[cfg(feature = "cuda")]
32pub mod gpu;
33
34// Re-export main types
35pub use algorithms::bfs::{bfs_parallel, bfs_sequential, BfsConfig};
36pub use algorithms::scc::{scc_kosaraju, scc_tarjan, SccConfig};
37pub use algorithms::spmv::{spmv, spmv_parallel, SpmvConfig};
38pub use algorithms::union_find::{union_find_parallel, union_find_sequential, UnionFind};
39pub use models::csr::{CsrMatrix, CsrMatrixBuilder};
40pub use models::node::{ComponentId, Distance, NodeId};
41
42/// Graph algorithm error types.
43#[derive(Debug, thiserror::Error)]
44pub enum GraphError {
45    /// Invalid node ID.
46    #[error("Invalid node ID: {0}")]
47    InvalidNodeId(u64),
48
49    /// Matrix dimension mismatch.
50    #[error("Matrix dimension mismatch: expected {expected}, got {actual}")]
51    DimensionMismatch { expected: usize, actual: usize },
52
53    /// Empty graph.
54    #[error("Empty graph")]
55    EmptyGraph,
56
57    /// Invalid CSR format.
58    #[error("Invalid CSR format: {0}")]
59    InvalidCsr(String),
60
61    /// Algorithm error.
62    #[error("Algorithm error: {0}")]
63    AlgorithmError(String),
64}
65
66/// Result type for graph operations.
67pub type Result<T> = std::result::Result<T, GraphError>;