scirs2_sparse/parallel_amg/
types.rs1use crate::csr::CsrMatrix;
7
8#[derive(Debug, Clone)]
10pub struct ParallelAmgConfig {
11 pub n_threads: usize,
13 pub strength_threshold: f64,
15 pub coarsening: CoarsenMethod,
17 pub max_levels: usize,
19 pub min_coarse_size: usize,
21 pub max_coarsening_ratio: f64,
23 pub omega: f64,
25}
26
27impl Default for ParallelAmgConfig {
28 fn default() -> Self {
29 Self {
30 n_threads: 1,
31 strength_threshold: 0.25,
32 coarsening: CoarsenMethod::ParallelRS,
33 max_levels: 10,
34 min_coarse_size: 4,
35 max_coarsening_ratio: 0.85,
36 omega: 4.0 / 3.0,
37 }
38 }
39}
40
41#[non_exhaustive]
43#[derive(Debug, Clone, Copy, PartialEq, Eq)]
44pub enum CoarsenMethod {
45 ParallelRS,
47 ParallelSA,
49 PMIS,
51 CLJP,
53}
54
55#[derive(Debug, Clone)]
57pub struct CoarseningResult {
58 pub c_nodes: Vec<usize>,
60 pub f_nodes: Vec<usize>,
62 pub cf_splitting: Vec<u8>,
64}
65
66impl CoarseningResult {
67 pub fn from_splitting(cf_splitting: Vec<u8>) -> Self {
69 let n = cf_splitting.len();
70 let mut c_nodes = Vec::new();
71 let mut f_nodes = Vec::new();
72 for i in 0..n {
73 if cf_splitting[i] == 1 {
74 c_nodes.push(i);
75 } else {
76 f_nodes.push(i);
77 }
78 }
79 Self {
80 c_nodes,
81 f_nodes,
82 cf_splitting,
83 }
84 }
85
86 pub fn n(&self) -> usize {
88 self.cf_splitting.len()
89 }
90
91 pub fn n_coarse(&self) -> usize {
93 self.c_nodes.len()
94 }
95
96 pub fn n_fine(&self) -> usize {
98 self.f_nodes.len()
99 }
100
101 pub fn coarsening_ratio(&self) -> f64 {
103 let n = self.n();
104 if n == 0 {
105 return 0.0;
106 }
107 self.n_coarse() as f64 / n as f64
108 }
109}
110
111#[derive(Debug, Clone)]
113pub struct ParallelAmgLevel {
114 pub a: CsrMatrix<f64>,
116 pub p: CsrMatrix<f64>,
118 pub r: CsrMatrix<f64>,
120 pub n_fine: usize,
122 pub n_coarse: usize,
124}
125
126impl ParallelAmgLevel {
127 pub fn new(
129 a: CsrMatrix<f64>,
130 p: CsrMatrix<f64>,
131 r: CsrMatrix<f64>,
132 n_fine: usize,
133 n_coarse: usize,
134 ) -> Self {
135 Self {
136 a,
137 p,
138 r,
139 n_fine,
140 n_coarse,
141 }
142 }
143}
144
145#[derive(Debug, Clone)]
147pub struct ParallelAmgHierarchy {
148 pub levels: Vec<ParallelAmgLevel>,
150 pub n_levels: usize,
152 pub coarsest_a: CsrMatrix<f64>,
154}
155
156impl ParallelAmgHierarchy {
157 pub fn new(levels: Vec<ParallelAmgLevel>, coarsest_a: CsrMatrix<f64>) -> Self {
159 let n_levels = levels.len() + 1; Self {
161 levels,
162 n_levels,
163 coarsest_a,
164 }
165 }
166
167 pub fn fine_size(&self) -> usize {
169 if self.levels.is_empty() {
170 self.coarsest_a.shape().0
171 } else {
172 self.levels[0].n_fine
173 }
174 }
175
176 pub fn coarse_size(&self) -> usize {
178 self.coarsest_a.shape().0
179 }
180}