|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright The Lance Authors |
| 3 | + |
| 4 | +//! # GraphBLAS Descriptors |
| 5 | +//! |
| 6 | +//! Descriptors control the behaviour of GraphBLAS operations, such as whether |
| 7 | +//! to transpose inputs, complement masks, or replace outputs. |
| 8 | +
|
| 9 | +use std::fmt; |
| 10 | + |
| 11 | +/// Descriptor field identifiers. |
| 12 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 13 | +pub enum DescField { |
| 14 | + /// Controls the output container. |
| 15 | + Output, |
| 16 | + /// Controls the mask. |
| 17 | + Mask, |
| 18 | + /// Controls the first input. |
| 19 | + Input0, |
| 20 | + /// Controls the second input. |
| 21 | + Input1, |
| 22 | +} |
| 23 | + |
| 24 | +/// Descriptor value for a given field. |
| 25 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 26 | +pub enum DescValue { |
| 27 | + /// Default behaviour (no modification). |
| 28 | + Default, |
| 29 | + /// Transpose the matrix operand. |
| 30 | + Transpose, |
| 31 | + /// Complement the mask (use structural complement). |
| 32 | + Complement, |
| 33 | + /// Replace mode: clear the output before writing results. |
| 34 | + Replace, |
| 35 | + /// Structure-only mask: ignore values, use pattern only. |
| 36 | + Structure, |
| 37 | +} |
| 38 | + |
| 39 | +/// A descriptor that controls GraphBLAS operation behaviour. |
| 40 | +/// |
| 41 | +/// Each field can be set independently. Unset fields default to [`DescValue::Default`]. |
| 42 | +#[derive(Clone, PartialEq, Eq)] |
| 43 | +pub struct Descriptor { |
| 44 | + /// Output field setting. |
| 45 | + pub output: DescValue, |
| 46 | + /// Mask field setting. |
| 47 | + pub mask: DescValue, |
| 48 | + /// First input field setting. |
| 49 | + pub inp0: DescValue, |
| 50 | + /// Second input field setting. |
| 51 | + pub inp1: DescValue, |
| 52 | +} |
| 53 | + |
| 54 | +impl Default for Descriptor { |
| 55 | + fn default() -> Self { |
| 56 | + Self { |
| 57 | + output: DescValue::Default, |
| 58 | + mask: DescValue::Default, |
| 59 | + inp0: DescValue::Default, |
| 60 | + inp1: DescValue::Default, |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl fmt::Debug for Descriptor { |
| 66 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 67 | + f.debug_struct("Descriptor") |
| 68 | + .field("output", &self.output) |
| 69 | + .field("mask", &self.mask) |
| 70 | + .field("inp0", &self.inp0) |
| 71 | + .field("inp1", &self.inp1) |
| 72 | + .finish() |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl Descriptor { |
| 77 | + /// Create a new default descriptor. |
| 78 | + pub fn new() -> Self { |
| 79 | + Self::default() |
| 80 | + } |
| 81 | + |
| 82 | + /// Set a descriptor field. |
| 83 | + pub fn set(&mut self, field: DescField, value: DescValue) -> &mut Self { |
| 84 | + match field { |
| 85 | + DescField::Output => self.output = value, |
| 86 | + DescField::Mask => self.mask = value, |
| 87 | + DescField::Input0 => self.inp0 = value, |
| 88 | + DescField::Input1 => self.inp1 = value, |
| 89 | + } |
| 90 | + self |
| 91 | + } |
| 92 | + |
| 93 | + /// Get the value of a descriptor field. |
| 94 | + pub fn get(&self, field: DescField) -> DescValue { |
| 95 | + match field { |
| 96 | + DescField::Output => self.output, |
| 97 | + DescField::Mask => self.mask, |
| 98 | + DescField::Input0 => self.inp0, |
| 99 | + DescField::Input1 => self.inp1, |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /// Whether the first input should be transposed. |
| 104 | + pub fn transpose_input0(&self) -> bool { |
| 105 | + self.inp0 == DescValue::Transpose |
| 106 | + } |
| 107 | + |
| 108 | + /// Whether the second input should be transposed. |
| 109 | + pub fn transpose_input1(&self) -> bool { |
| 110 | + self.inp1 == DescValue::Transpose |
| 111 | + } |
| 112 | + |
| 113 | + /// Whether the mask should be complemented. |
| 114 | + pub fn complement_mask(&self) -> bool { |
| 115 | + self.mask == DescValue::Complement |
| 116 | + } |
| 117 | + |
| 118 | + /// Whether the output should be replaced (cleared before writing). |
| 119 | + pub fn replace_output(&self) -> bool { |
| 120 | + self.output == DescValue::Replace |
| 121 | + } |
| 122 | + |
| 123 | + /// Whether the mask is structure-only. |
| 124 | + pub fn structure_mask(&self) -> bool { |
| 125 | + self.mask == DescValue::Structure |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +/// Pre-built descriptor constants. |
| 130 | +#[allow(non_snake_case)] |
| 131 | +pub mod GrBDesc { |
| 132 | + use super::*; |
| 133 | + |
| 134 | + /// Default descriptor — no modifications. |
| 135 | + pub fn default() -> Descriptor { |
| 136 | + Descriptor::default() |
| 137 | + } |
| 138 | + |
| 139 | + /// Transpose the first input. |
| 140 | + pub fn t0() -> Descriptor { |
| 141 | + let mut d = Descriptor::default(); |
| 142 | + d.set(DescField::Input0, DescValue::Transpose); |
| 143 | + d |
| 144 | + } |
| 145 | + |
| 146 | + /// Transpose the second input. |
| 147 | + pub fn t1() -> Descriptor { |
| 148 | + let mut d = Descriptor::default(); |
| 149 | + d.set(DescField::Input1, DescValue::Transpose); |
| 150 | + d |
| 151 | + } |
| 152 | + |
| 153 | + /// Transpose both inputs. |
| 154 | + pub fn t0t1() -> Descriptor { |
| 155 | + let mut d = Descriptor::default(); |
| 156 | + d.set(DescField::Input0, DescValue::Transpose); |
| 157 | + d.set(DescField::Input1, DescValue::Transpose); |
| 158 | + d |
| 159 | + } |
| 160 | + |
| 161 | + /// Complement the mask. |
| 162 | + pub fn comp() -> Descriptor { |
| 163 | + let mut d = Descriptor::default(); |
| 164 | + d.set(DescField::Mask, DescValue::Complement); |
| 165 | + d |
| 166 | + } |
| 167 | + |
| 168 | + /// Replace the output. |
| 169 | + pub fn replace() -> Descriptor { |
| 170 | + let mut d = Descriptor::default(); |
| 171 | + d.set(DescField::Output, DescValue::Replace); |
| 172 | + d |
| 173 | + } |
| 174 | + |
| 175 | + /// Replace the output and complement the mask. |
| 176 | + pub fn replace_comp() -> Descriptor { |
| 177 | + let mut d = Descriptor::default(); |
| 178 | + d.set(DescField::Output, DescValue::Replace); |
| 179 | + d.set(DescField::Mask, DescValue::Complement); |
| 180 | + d |
| 181 | + } |
| 182 | + |
| 183 | + /// Structure-only mask. |
| 184 | + pub fn structure() -> Descriptor { |
| 185 | + let mut d = Descriptor::default(); |
| 186 | + d.set(DescField::Mask, DescValue::Structure); |
| 187 | + d |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +#[cfg(test)] |
| 192 | +mod tests { |
| 193 | + use super::*; |
| 194 | + |
| 195 | + #[test] |
| 196 | + fn test_default_descriptor() { |
| 197 | + let d = Descriptor::default(); |
| 198 | + assert_eq!(d.get(DescField::Output), DescValue::Default); |
| 199 | + assert_eq!(d.get(DescField::Mask), DescValue::Default); |
| 200 | + assert_eq!(d.get(DescField::Input0), DescValue::Default); |
| 201 | + assert_eq!(d.get(DescField::Input1), DescValue::Default); |
| 202 | + assert!(!d.transpose_input0()); |
| 203 | + assert!(!d.transpose_input1()); |
| 204 | + assert!(!d.complement_mask()); |
| 205 | + assert!(!d.replace_output()); |
| 206 | + } |
| 207 | + |
| 208 | + #[test] |
| 209 | + fn test_set_fields() { |
| 210 | + let mut d = Descriptor::new(); |
| 211 | + d.set(DescField::Input0, DescValue::Transpose); |
| 212 | + assert!(d.transpose_input0()); |
| 213 | + assert!(!d.transpose_input1()); |
| 214 | + |
| 215 | + d.set(DescField::Mask, DescValue::Complement); |
| 216 | + assert!(d.complement_mask()); |
| 217 | + } |
| 218 | + |
| 219 | + #[test] |
| 220 | + fn test_presets() { |
| 221 | + let t0 = GrBDesc::t0(); |
| 222 | + assert!(t0.transpose_input0()); |
| 223 | + assert!(!t0.transpose_input1()); |
| 224 | + |
| 225 | + let t1 = GrBDesc::t1(); |
| 226 | + assert!(!t1.transpose_input0()); |
| 227 | + assert!(t1.transpose_input1()); |
| 228 | + |
| 229 | + let t0t1 = GrBDesc::t0t1(); |
| 230 | + assert!(t0t1.transpose_input0()); |
| 231 | + assert!(t0t1.transpose_input1()); |
| 232 | + |
| 233 | + let comp = GrBDesc::comp(); |
| 234 | + assert!(comp.complement_mask()); |
| 235 | + |
| 236 | + let repl = GrBDesc::replace(); |
| 237 | + assert!(repl.replace_output()); |
| 238 | + |
| 239 | + let rc = GrBDesc::replace_comp(); |
| 240 | + assert!(rc.replace_output()); |
| 241 | + assert!(rc.complement_mask()); |
| 242 | + |
| 243 | + let s = GrBDesc::structure(); |
| 244 | + assert!(s.structure_mask()); |
| 245 | + } |
| 246 | + |
| 247 | + #[test] |
| 248 | + fn test_debug_format() { |
| 249 | + let d = GrBDesc::t0(); |
| 250 | + let s = format!("{:?}", d); |
| 251 | + assert!(s.contains("Transpose")); |
| 252 | + } |
| 253 | +} |
0 commit comments