kryst 4.0.3

Krylov subspace and preconditioned iterative solvers for dense and sparse linear systems, with shared and distributed memory parallelism.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
use crate::algebra::prelude::*;
use crate::context::ksp_context::Workspace;
use crate::error::KError;
use crate::matrix::op::LinOp;
use crate::ops::klinop::KLinOp;
use crate::ops::kpc::KPreconditioner;
use crate::ops::wrap::{as_s_op, as_s_pc_mut};
use crate::parallel::UniverseComm;
use crate::preconditioner::{PcSide, Preconditioner};
use crate::solver::common::{ReductCtx, call_monitors, recompute_true_residual_norm_s};
use crate::solver::{LinearSolver, MonitorCallback};
use crate::utils::convergence::{
    ConvergedReason, GcrCounters, ReductionModel, SolveStats, SolverCounters,
};
use std::any::Any;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum GcrOrthog {
    Classical,
    Modified,
}

/// Pipelined flexible GCR with dedicated GCR recurrence/storage.
pub struct PipeGcrSolver {
    pub rtol: f64,
    pub atol: f64,
    pub dtol: f64,
    pub maxits: usize,
    pub restart: usize,
    pub orthog: GcrOrthog,
}

impl PipeGcrSolver {
    pub fn new(restart: usize, rtol: f64, maxits: usize) -> Self {
        Self {
            rtol,
            atol: 1e-12,
            dtol: 1e3,
            maxits,
            restart: restart.max(1),
            orthog: GcrOrthog::Classical,
        }
    }

    pub fn set_restart(&mut self, restart: usize) {
        self.restart = restart.max(1);
    }

    pub fn set_orthog(&mut self, orthog: GcrOrthog) {
        self.orthog = orthog;
    }

    fn reduction_model(&self) -> ReductionModel {
        let per_iter = match self.orthog {
            GcrOrthog::Classical => 2.0,
            GcrOrthog::Modified => 2.0,
        };
        ReductionModel {
            variant: "pipegcr",
            startup: 2,
            per_iteration: per_iter,
            tail: 1,
        }
    }

    #[allow(clippy::too_many_arguments)]
    pub fn solve_k<A>(
        &mut self,
        a: &A,
        mut pc: Option<&mut dyn KPreconditioner<Scalar = S>>,
        b: &[S],
        x: &mut [S],
        pc_side: PcSide,
        comm: &UniverseComm,
        monitors: Option<&[Box<MonitorCallback<R>>]>,
        work: Option<&mut Workspace>,
    ) -> Result<SolveStats<R>, KError>
    where
        A: KLinOp<Scalar = S> + ?Sized,
    {
        let (m, n) = a.dims();
        if m != n {
            return Err(KError::InvalidInput(
                "PipeGCR requires a square operator".to_string(),
            ));
        }
        if b.len() != n || x.len() != n {
            return Err(KError::InvalidInput(
                "PipeGCR: vector size mismatch".to_string(),
            ));
        }

        let pc_apply_side = match pc_side {
            PcSide::Right => PcSide::Right,
            PcSide::Left | PcSide::Symmetric => PcSide::Right,
        };

        let mut owned_ws;
        let ws = if let Some(w) = work {
            w
        } else {
            owned_ws = Workspace::new(n);
            &mut owned_ws
        };
        ws.tmp1.resize(n, S::zero());
        ws.tmp2.resize(n, S::zero());
        let red = ReductCtx::new(comm, Some(&*ws));
        let mut v = vec![S::zero(); n];
        let mut true_res_buf = vec![S::zero(); n];

        a.matvec_s(x, &mut ws.tmp1[..n], &mut ws.bridge);
        for i in 0..n {
            ws.tmp1[i] = b[i] - ws.tmp1[i];
        }

        let mut norms = [R::zero(); 2];
        red.norm2_many_into(&[&ws.tmp1[..n], b], &mut norms);
        let bnorm = norms[1].max(1e-32);
        let mut rnorm = norms[0];
        let mons = monitors.unwrap_or(&[]);

        let mut sync_count = 1usize;
        if call_monitors(mons, 0, rnorm, sync_count) {
            let counters = SolverCounters {
                num_global_reductions: sync_count,
                ..SolverCounters::default()
            };
            return Ok(SolveStats::new(0, rnorm, ConvergedReason::StoppedByMonitor)
                .with_counters(counters)
                .with_reduction_model(self.reduction_model())
                .with_gcr_counters(GcrCounters {
                    basis_updates: 0,
                    sync_count,
                    restart_count: 0,
                    restarted: false,
                }));
        }

        let threshold = self.atol.max(self.rtol * bnorm);
        if rnorm <= threshold {
            let reason = if rnorm <= self.atol {
                ConvergedReason::ConvergedAtol
            } else {
                ConvergedReason::ConvergedRtol
            };
            let counters = SolverCounters {
                num_global_reductions: sync_count,
                ..SolverCounters::default()
            };
            return Ok(SolveStats::new(0, rnorm, reason)
                .with_counters(counters)
                .with_reduction_model(self.reduction_model())
                .with_gcr_counters(GcrCounters {
                    basis_updates: 0,
                    sync_count,
                    restart_count: 0,
                    restarted: false,
                }));
        }

        let mut p_basis: Vec<Vec<S>> = Vec::with_capacity(self.restart);
        let mut ap_basis: Vec<Vec<S>> = Vec::with_capacity(self.restart);
        let mut iters = 0usize;
        let mut basis_updates = 0usize;
        let mut restart_count = 0usize;

        while iters < self.maxits {
            if !p_basis.is_empty() {
                p_basis.clear();
                ap_basis.clear();
                restart_count += 1;
            }

            let cycle = self.restart.min(self.maxits - iters);
            for _ in 0..cycle {
                // u = M^{-1} r (or r when no PC)
                if let Some(pc_ref) = pc.as_deref_mut() {
                    pc_ref.apply_mut_s(
                        pc_apply_side,
                        &ws.tmp1[..n],
                        &mut ws.tmp2[..n],
                        &mut ws.bridge,
                    )?;
                } else {
                    ws.tmp2[..n].copy_from_slice(&ws.tmp1[..n]);
                }

                // v = A u
                a.matvec_s(&ws.tmp2[..n], &mut v[..n], &mut ws.bridge);

                // Orthogonalize (u,v) against prior Ap basis
                match self.orthog {
                    GcrOrthog::Classical => {
                        if !ap_basis.is_empty() {
                            let mut pairs: Vec<(&[S], &[S])> = Vec::with_capacity(ap_basis.len());
                            for api in &ap_basis {
                                pairs.push((&api[..], &v[..n]));
                            }
                            let mut numer = vec![S::zero(); ap_basis.len()];
                            red.dot_many_into(&pairs, &mut numer);
                            sync_count += 1;

                            let mut denom_pairs: Vec<(&[S], &[S])> =
                                Vec::with_capacity(ap_basis.len());
                            for api in &ap_basis {
                                denom_pairs.push((&api[..], &api[..]));
                            }
                            let mut denoms = vec![S::zero(); ap_basis.len()];
                            red.dot_many_into(&denom_pairs, &mut denoms);
                            sync_count += 1;

                            for i in 0..ap_basis.len() {
                                let denom = denoms[i];
                                if denom.abs() <= 1e-30 {
                                    continue;
                                }
                                let beta = numer[i] / denom;
                                for k in 0..n {
                                    ws.tmp2[k] -= beta * p_basis[i][k];
                                    v[k] -= beta * ap_basis[i][k];
                                }
                            }
                        }
                    }
                    GcrOrthog::Modified => {
                        for i in 0..ap_basis.len() {
                            let pair = [
                                (&ap_basis[i][..], &v[..n]),
                                (&ap_basis[i][..], &ap_basis[i][..]),
                            ];
                            let mut vals = [S::zero(); 2];
                            red.dot_many_into(&pair, &mut vals);
                            sync_count += 1;
                            if vals[1].abs() <= 1e-30 {
                                continue;
                            }
                            let beta = vals[0] / vals[1];
                            for k in 0..n {
                                ws.tmp2[k] -= beta * p_basis[i][k];
                                v[k] -= beta * ap_basis[i][k];
                            }
                        }
                    }
                }

                let pair = [(&ws.tmp1[..n], &v[..n]), (&v[..n], &v[..n])];
                let mut vals = [S::zero(); 2];
                red.dot_many_into(&pair, &mut vals);
                sync_count += 1;
                if vals[1].abs() <= 1e-30 {
                    let counters = SolverCounters {
                        num_global_reductions: sync_count,
                        ..SolverCounters::default()
                    };
                    return Ok(
                        SolveStats::new(iters, rnorm, ConvergedReason::DivergedBreakdown)
                            .with_counters(counters)
                            .with_reduction_model(self.reduction_model())
                            .with_gcr_counters(GcrCounters {
                                basis_updates,
                                sync_count,
                                restart_count,
                                restarted: restart_count > 0,
                            }),
                    );
                }
                let alpha = vals[0] / vals[1];

                for i in 0..n {
                    x[i] += alpha * ws.tmp2[i];
                    ws.tmp1[i] -= alpha * v[i];
                }

                p_basis.push(ws.tmp2[..n].to_vec());
                ap_basis.push(v[..n].to_vec());
                basis_updates += 1;
                iters += 1;

                rnorm = red.norm2(&ws.tmp1[..n]);
                sync_count += 1;
                if call_monitors(mons, iters, rnorm, sync_count) {
                    let counters = SolverCounters {
                        num_global_reductions: sync_count,
                        ..SolverCounters::default()
                    };
                    return Ok(
                        SolveStats::new(iters, rnorm, ConvergedReason::StoppedByMonitor)
                            .with_counters(counters)
                            .with_reduction_model(self.reduction_model())
                            .with_gcr_counters(GcrCounters {
                                basis_updates,
                                sync_count,
                                restart_count,
                                restarted: restart_count > 0,
                            }),
                    );
                }

                if rnorm <= threshold {
                    let true_res = recompute_true_residual_norm_s(
                        a,
                        b,
                        x,
                        comm,
                        red.engine(),
                        &mut true_res_buf[..n],
                        &mut ws.bridge,
                    );
                    let reason = if true_res <= self.atol {
                        ConvergedReason::ConvergedAtol
                    } else {
                        ConvergedReason::ConvergedRtol
                    };
                    let counters = SolverCounters {
                        num_global_reductions: sync_count,
                        ..SolverCounters::default()
                    };
                    return Ok(SolveStats::new(iters, true_res, reason)
                        .with_counters(counters)
                        .with_reduction_model(self.reduction_model())
                        .with_gcr_counters(GcrCounters {
                            basis_updates,
                            sync_count,
                            restart_count,
                            restarted: restart_count > 0,
                        }));
                }

                if rnorm >= self.dtol * bnorm {
                    let counters = SolverCounters {
                        num_global_reductions: sync_count,
                        ..SolverCounters::default()
                    };
                    return Ok(SolveStats::new(iters, rnorm, ConvergedReason::DivergedDtol)
                        .with_counters(counters)
                        .with_reduction_model(self.reduction_model())
                        .with_gcr_counters(GcrCounters {
                            basis_updates,
                            sync_count,
                            restart_count,
                            restarted: restart_count > 0,
                        }));
                }
            }
        }

        let final_res = recompute_true_residual_norm_s(
            a,
            b,
            x,
            comm,
            red.engine(),
            &mut true_res_buf[..n],
            &mut ws.bridge,
        );
        let counters = SolverCounters {
            num_global_reductions: sync_count,
            ..SolverCounters::default()
        };
        Ok(
            SolveStats::new(iters, final_res, ConvergedReason::DivergedMaxIts)
                .with_counters(counters)
                .with_reduction_model(self.reduction_model())
                .with_gcr_counters(GcrCounters {
                    basis_updates,
                    sync_count,
                    restart_count,
                    restarted: restart_count > 0,
                }),
        )
    }

    #[cfg(not(feature = "complex"))]
    #[allow(clippy::too_many_arguments)]
    pub fn solve_f64(
        &mut self,
        a: &dyn LinOp<S = f64>,
        pc: Option<&mut dyn Preconditioner>,
        b: &[f64],
        x: &mut [f64],
        pc_side: PcSide,
        comm: &UniverseComm,
        monitors: Option<&[Box<MonitorCallback<f64>>]>,
        work: Option<&mut Workspace>,
    ) -> Result<SolveStats<f64>, KError> {
        let (m, n) = a.dims();
        if m != n {
            return Err(KError::InvalidInput(
                "PipeGCR requires a square operator".to_string(),
            ));
        }
        if b.len() != n || x.len() != n {
            return Err(KError::InvalidInput(
                "PipeGCR: vector size mismatch".to_string(),
            ));
        }

        let a_s = as_s_op(a);
        match pc {
            Some(pc_ref) => {
                let mut pc_s = as_s_pc_mut(pc_ref);
                self.solve_k(&a_s, Some(&mut pc_s), b, x, pc_side, comm, monitors, work)
            }
            None => self.solve_k(&a_s, None, b, x, pc_side, comm, monitors, work),
        }
    }
}

impl LinearSolver for PipeGcrSolver {
    type Error = KError;

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }

    fn setup_workspace(&mut self, _work: &mut Workspace) {}

    fn solve(
        &mut self,
        a: &dyn LinOp<S = f64>,
        pc: Option<&mut dyn Preconditioner>,
        b: &[f64],
        x: &mut [f64],
        pc_side: PcSide,
        comm: &UniverseComm,
        monitors: Option<&[Box<MonitorCallback<f64>>]>,
        work: Option<&mut Workspace>,
    ) -> Result<SolveStats<f64>, Self::Error> {
        #[cfg(not(feature = "complex"))]
        {
            self.solve_f64(a, pc, b, x, pc_side, comm, monitors, work)
        }
        #[cfg(feature = "complex")]
        {
            let _ = (a, pc, b, x, pc_side, comm, monitors, work);
            Err(KError::Unsupported(
                "PipeGCR real-valued LinearSolver bridge is unavailable when complex is enabled"
                    .into(),
            ))
        }
    }
}