1use std::error::Error;
2use std::fmt;
3
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
6pub enum MatrixOperand {
7 Left,
9 Right,
11}
12
13#[derive(Clone, Copy, Debug, PartialEq, Eq)]
15pub enum ArithmeticOperation {
16 Multiply,
18 Add,
20}
21
22impl fmt::Display for ArithmeticOperation {
23 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24 match self {
25 Self::Multiply => f.write_str("multiplication"),
26 Self::Add => f.write_str("addition"),
27 }
28 }
29}
30
31impl fmt::Display for MatrixOperand {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33 match self {
34 Self::Left => f.write_str("left"),
35 Self::Right => f.write_str("right"),
36 }
37 }
38}
39
40#[derive(Clone, Debug, PartialEq, Eq)]
42#[non_exhaustive]
43pub enum SpGemmError {
44 DimensionMismatch {
46 left: (usize, usize),
48 right: (usize, usize),
50 },
51 NonCsrStorage {
53 operand: MatrixOperand,
55 },
56 IndexOverflow {
58 value: usize,
60 target: &'static str,
62 },
63 InvalidOutputStructure(String),
65 ArithmeticOverflow {
67 operation: ArithmeticOperation,
69 row: usize,
71 inner: usize,
73 column: usize,
75 },
76 OutputNnzLimitExceeded {
78 limit: usize,
80 attempted: usize,
82 },
83}
84
85impl fmt::Display for SpGemmError {
86 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87 match self {
88 Self::DimensionMismatch { left, right } => write!(
89 f,
90 "incompatible matrix dimensions: left is {}x{}, right is {}x{}",
91 left.0, left.1, right.0, right.1
92 ),
93 Self::NonCsrStorage { operand } => {
94 write!(f, "{operand} sprs operand must use CSR storage")
95 }
96 Self::IndexOverflow { value, target } => {
97 write!(f, "index {value} cannot be represented by {target}")
98 }
99 Self::InvalidOutputStructure(reason) => {
100 write!(f, "generated CSR output is invalid: {reason}")
101 }
102 Self::ArithmeticOverflow {
103 operation,
104 row,
105 inner,
106 column,
107 } => write!(
108 f,
109 "arithmetic overflow during {operation} at output ({row}, {column}) through inner index {inner}"
110 ),
111 Self::OutputNnzLimitExceeded { limit, attempted } => write!(
112 f,
113 "sparse product requires at least {attempted} output entries, exceeding limit {limit}"
114 ),
115 }
116 }
117}
118
119impl Error for SpGemmError {}