kryst 3.2.1

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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
#[allow(unused_imports)]
use crate::algebra::blas::{dot_conj, nrm2};
use crate::solver::MonitorCallback;
use crate::solver::common::call_monitors;
use crate::algebra::bridge::BridgeScratch;
#[allow(unused_imports)]
use crate::algebra::prelude::*;
use crate::context::ksp_context::Workspace;
use crate::error::KError;
use crate::matrix::op::LinOp;
use crate::matrix::op_bridge::matvec_s;
use crate::parallel::{Comm, UniverseComm};
use crate::preconditioner::bridge::apply_pc_s;
use crate::preconditioner::{PcSide, Preconditioner};
#[cfg(feature = "complex")]
use crate::reduction::Packet;
use crate::reduction::{CommDeterministic, DotEngine, ReductionOptions, ReproMode};
use crate::solver::LinearSolver;
#[cfg(feature = "complex")]
use crate::solver::common::dot_result_to_real;
use crate::solver::common::{dot1_async_s, nrm2_async_s};
use crate::utils::convergence::{ConvergedReason, Convergence, SolveStats, SolverCounters};
use crate::utils::reduction::{AllreduceHandle, AllreduceOps, ReductOptions};
use smallvec::SmallVec;
use std::any::Any;

#[derive(Debug, Clone, Copy)]
pub enum CgNormType {
    /// Monitor the preconditioned residual `sqrt(ráµ€z)` (default)
    Preconditioned,
    /// Monitor the unpreconditioned residual `||r||â‚‚`
    Unpreconditioned,
    /// Monitor the "natural" norm of the preconditioned residual vector
    /// `||z||â‚‚`, matching PETSc's `-ksp_norm_type natural` semantics.
    Natural,
    /// Do not compute or report a residual norm
    None,
}

/// Supported PCG algorithmic variants.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PcgVariant {
    /// Classic Hestenes-Stiefel style PCG.
    Classic,
    /// Pipelined CG with asynchronous reductions.
    Pipelined {
        /// Residual replacement interval (0 disables replacement).
        replace_every: usize,
    },
}

/// Default replacement interval for pipelined CG.
///
pub const PCG_PIPELINED_DEFAULT_REPLACE_EVERY: usize = 50;

pub struct PcgSolver {
    pub(crate) conv: Convergence,
    norm_type: CgNormType,
    reduction: ReductionOptions,
    true_residual_monitor: Option<Box<MonitorCallback<f64>>>,
    /// Whether the initial guess in `x` should be treated as nonzero.
    ///
    /// PETSc zeroes the initial guess by default unless told otherwise via
    /// `KSPSetInitialGuessNonzero`. We follow the same policy; when this flag is
    /// `false` and the provided `x` is exactly the zero vector, the solver
    /// skips the initial matvec and assumes a zero guess.
    initial_guess_nonzero: bool,
    variant: PcgVariant,
    async_reduction: ReductOptions,
}

struct ClassicWorkspace<'a> {
    r: &'a mut [S],
    z: &'a mut [S],
    p: &'a mut [S],
    ap: &'a mut [S],
    scratch: &'a mut BridgeScratch,
}

impl<'a> ClassicWorkspace<'a> {
    fn acquire(work: &'a mut Workspace, n: usize) -> Self {
        if work.tmp1.len() != n {
            work.tmp1.resize(n, S::zero());
        }
        if work.tmp2.len() != n {
            work.tmp2.resize(n, S::zero());
        }
        while work.q_s.len() < 2 {
            work.q_s.push(Vec::new());
        }
        for buf in &mut work.q_s[..2] {
            if buf.len() != n {
                buf.resize(n, S::zero());
            }
        }
        let (pbuf, rest) = work.q_s.split_at_mut(1);
        let (apbuf, _) = rest.split_at_mut(1);
        Self {
            r: &mut work.tmp1[..n],
            z: &mut work.tmp2[..n],
            p: &mut pbuf[0][..n],
            ap: &mut apbuf[0][..n],
            scratch: &mut work.bridge,
        }
    }
}

impl PcgSolver {
    pub fn new(rtol: f64, maxits: usize) -> Self {
        Self {
            conv: Convergence {
                rtol,
                atol: 1e-50,
                dtol: 1e5,
                max_iters: maxits,
            },
            norm_type: CgNormType::Preconditioned,
            reduction: ReductionOptions::default(),
            true_residual_monitor: None,
            initial_guess_nonzero: false,
            variant: PcgVariant::Classic,
            async_reduction: ReductOptions::default(),
        }
    }

    /// Optional runtime update of solver tolerances
    pub fn set_tolerances(&mut self, rtol: f64, atol: f64, dtol: f64, maxits: usize) {
        self.conv.rtol = rtol;
        self.conv.atol = atol;
        self.conv.dtol = dtol;
        self.conv.max_iters = maxits;
    }

    pub fn with_norm(mut self, norm_type: CgNormType) -> Self {
        self.norm_type = norm_type;
        self
    }

    /// Enable a more reproducible (but slightly slower) local dot product using
    /// Kahan summation. When combined with a deterministic MPI reduction this
    /// yields bitwise-identical results across runs.
    pub fn with_reproducible_dot(mut self, f: bool) -> Self {
        self.reduction.mode = if f {
            ReproMode::Deterministic
        } else {
            ReproMode::Fast
        };
        self
    }

    /// Install a monitor that receives the true residual norm `||b - A x||â‚‚`
    /// at each iteration. This uses the already available residual and is
    /// intended for debugging.
    pub fn with_true_residual_monitor(mut self, m: Box<MonitorCallback<f64>>) -> Self {
        self.true_residual_monitor = Some(m);
        self
    }

    #[must_use = "with_variant returns an updated solver; assign it before continuing"]
    pub fn with_variant(mut self, variant: PcgVariant) -> Self {
        self.variant = variant;
        self
    }

    pub fn set_variant(&mut self, variant: PcgVariant) {
        self.variant = variant;
    }

    pub fn variant(&self) -> PcgVariant {
        self.variant
    }

    pub fn set_async_reduction_options(&mut self, opt: ReductOptions) {
        self.async_reduction = opt;
    }

    fn async_options(&self) -> ReductOptions {
        let mut opt = self.async_reduction.clone();
        opt.mode = self.reduction.mode;
        opt
    }

    /// Indicate whether the supplied initial guess is nonzero.
    ///
    /// By default `x` is assumed to be zero, which avoids an extra matvec on
    /// entry. Calling this with `true` forces the solver to compute the initial
    /// residual `b - A x` even if `x` happens to be the zero vector.
    pub fn with_nonzero_guess(mut self, f: bool) -> Self {
        self.initial_guess_nonzero = f;
        self
    }

    /// Set the nonzero initial guess flag after construction.
    pub fn set_nonzero_guess(&mut self, f: bool) {
        self.initial_guess_nonzero = f;
    }

    /// Toggle reproducible local dot products after construction.
    pub fn set_reproducible_dot(&mut self, f: bool) {
        self.reduction.mode = if f {
            ReproMode::Deterministic
        } else {
            ReproMode::Fast
        };
    }

    /// Set or clear the true residual monitor after construction.
    pub fn set_true_residual_monitor(&mut self, m: Option<Box<MonitorCallback<f64>>>) {
        self.true_residual_monitor = m;
    }

    #[inline]
    fn dot<C: Comm + CommDeterministic>(&self, u: &[f64], v: &[f64], comm: &C) -> f64 {
        let engine = DotEngine {
            opts: self.reduction,
        };
        engine.dot(u, v, comm)
    }

    #[inline]
    fn dot_scalar<C: Comm + CommDeterministic>(&self, u: &[S], v: &[S], comm: &C) -> R {
        #[cfg(not(feature = "complex"))]
        {
            let ur: &[f64] = unsafe { &*(u as *const [S] as *const [f64]) };
            let vr: &[f64] = unsafe { &*(v as *const [S] as *const [f64]) };
            self.dot(ur, vr, comm)
        }

        #[cfg(feature = "complex")]
        {
            let local = crate::algebra::blas::dot_conj(u, v);
            if matches!(self.reduction.mode, ReproMode::Fast) {
                return dot_result_to_real(comm.allreduce_sum_scalar(local));
            }

            let packet = Packet::<2> {
                v: [local.real(), local.imag()],
            };
            let reduced = comm.allreduce_det(&packet, self.reduction.mode);
            reduced.v[0]
        }
    }

    #[inline]
    fn dot_scalar_many<C: Comm + CommDeterministic>(
        &self,
        pairs: &[(&[S], &[S])],
        comm: &C,
        out: &mut [R],
    ) {
        assert_eq!(pairs.len(), out.len());
        if pairs.is_empty() {
            return;
        }

        #[cfg(not(feature = "complex"))]
        {
            let engine = DotEngine {
                opts: self.reduction,
            };
            let mut real_pairs: SmallVec<[(&[f64], &[f64]); 8]> =
                SmallVec::with_capacity(pairs.len());
            for &(u, v) in pairs {
                let ur: &[f64] = unsafe { &*(u as *const [S] as *const [f64]) };
                let vr: &[f64] = unsafe { &*(v as *const [S] as *const [f64]) };
                real_pairs.push((ur, vr));
            }
            engine.dot_many_into(real_pairs.as_slice(), out, comm);
        }

        #[cfg(feature = "complex")]
        {
            for ((u, v), slot) in pairs.iter().zip(out.iter_mut()) {
                let local = crate::algebra::blas::dot_conj(u, v);
                if matches!(self.reduction.mode, ReproMode::Fast) {
                    *slot = dot_result_to_real(comm.allreduce_sum_scalar(local));
                } else {
                    let packet = Packet::<2> {
                        v: [local.real(), local.imag()],
                    };
                    let reduced = comm.allreduce_det(&packet, self.reduction.mode);
                    *slot = reduced.v[0];
                }
            }
        }
    }

    #[inline]
    fn nrm2_scalar<C: Comm + CommDeterministic>(&self, u: &[S], comm: &C) -> R {
        let val = self.dot_scalar(u, u, comm);
        val.abs().sqrt()
    }

    #[inline]
    fn ensure_norm<C: Comm + CommDeterministic>(
        &self,
        vec: &[S],
        comm: &C,
        cache: &mut Option<R>,
    ) -> R {
        if let Some(val) = *cache {
            val
        } else {
            let val = self.nrm2_scalar(vec, comm);
            *cache = Some(val);
            val
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn solve_classic_scalar<C: Comm + CommDeterministic>(
        &mut self,
        a: &dyn LinOp<S = f64>,
        pc: Option<&dyn Preconditioner>,
        b: &[S],
        x: &mut [S],
        comm: &C,
        monitors: &[Box<MonitorCallback<f64>>],
        work: &mut Workspace,
    ) -> Result<SolveStats<f64>, KError> {
        let n = b.len();
        let mut buffers = ClassicWorkspace::acquire(work, n);
        let ClassicWorkspace {
            r,
            z,
            p,
            ap,
            scratch,
        } = &mut buffers;
        let (r, z, p, ap, scratch) = (&mut **r, &mut **z, &mut **p, &mut **ap, &mut **scratch);

        let zero_guess =
            !self.initial_guess_nonzero && x.iter().all(|&xi| xi.abs() <= R::default());
        if zero_guess {
            r.copy_from_slice(b);
        } else {
            matvec_s(a, x, &mut ap[..], scratch);
            for i in 0..n {
                r[i] = b[i] - ap[i];
            }
        }

        if let Some(pc) = pc {
            apply_pc_s(pc, PcSide::Left, r, &mut z[..], scratch)?;
        } else {
            z.copy_from_slice(r);
        }

        let need_r_norm_res = matches!(
            self.norm_type,
            CgNormType::Unpreconditioned | CgNormType::None
        );
        let need_z_norm_res = matches!(self.norm_type, CgNormType::Natural);
        let need_r_norm_monitor = self.true_residual_monitor.is_some();

        let mut initial_pairs: SmallVec<[(&[S], &[S]); 3]> = SmallVec::new();
        initial_pairs.push((&r[..], &z[..]));
        if need_r_norm_res || need_r_norm_monitor {
            initial_pairs.push((&r[..], &r[..]));
        }
        if need_z_norm_res {
            initial_pairs.push((&z[..], &z[..]));
        }
        let mut reductions: SmallVec<[R; 3]> = SmallVec::new();
        reductions.resize(initial_pairs.len(), R::zero());
        self.dot_scalar_many(initial_pairs.as_slice(), comm, reductions.as_mut_slice());

        let mut idx = 0;
        let mut rho = reductions[idx];
        idx += 1;
        if rho <= R::default() || !rho.is_finite() {
            return Err(KError::IndefinitePreconditioner);
        }

        let mut cached_r_norm = if need_r_norm_res || need_r_norm_monitor {
            let value = reductions[idx];
            idx += 1;
            Some(
                if value < R::default() {
                    R::default()
                } else {
                    value
                }
                .sqrt(),
            )
        } else {
            None
        };
        let cached_z_norm = if need_z_norm_res {
            let value = reductions[idx];
            Some(
                if value < R::default() {
                    R::default()
                } else {
                    value
                }
                .sqrt(),
            )
        } else {
            None
        };
        let mut rho_prev = rho;

        drop(initial_pairs);
        drop(reductions);

        let mut res = match self.norm_type {
            CgNormType::Preconditioned => rho.abs().sqrt(),
            CgNormType::Unpreconditioned => cached_r_norm.unwrap(),
            CgNormType::Natural => cached_z_norm.unwrap(),
            CgNormType::None => cached_r_norm.unwrap(),
        };
        let res0 = res;

        if call_monitors(monitors, 0, res, 0) {
            return Ok(SolveStats::new(0, res, ConvergedReason::StoppedByMonitor));
        }
        if let Some(m) = &self.true_residual_monitor {
            let value = self.ensure_norm(r, comm, &mut cached_r_norm);
            m(0, value, 0);
        }

        p.copy_from_slice(z);

        let (reason0, mut stats0) = self.conv.check(res, res0, 0);
        if !matches!(reason0, ConvergedReason::Continued) {
            stats0.final_residual = self.ensure_norm(r, comm, &mut cached_r_norm);
            return Ok(stats0);
        }

        for k in 1..=self.conv.max_iters {
            if k > 1 {
                let beta = rho / rho_prev;
                let beta_s = S::from_real(beta);
                for i in 0..n {
                    p[i] = z[i] + beta_s * p[i];
                }
            }

            matvec_s(a, p, &mut ap[..], scratch);
            let p_ap = self.dot_scalar(p, ap, comm);
            if !p_ap.is_finite() || p_ap <= R::default() {
                return Err(KError::IndefiniteMatrix);
            }

            let alpha = rho / p_ap;
            let alpha_s = S::from_real(alpha);
            for i in 0..n {
                x[i] += alpha_s * p[i];
                r[i] -= alpha_s * ap[i];
            }

            if let Some(pc) = pc {
                apply_pc_s(pc, PcSide::Left, r, &mut z[..], scratch)?;
            } else {
                z.copy_from_slice(r);
            }

            let need_r_norm_res = matches!(
                self.norm_type,
                CgNormType::Unpreconditioned | CgNormType::None
            );
            let need_z_norm_res = matches!(self.norm_type, CgNormType::Natural);
            let need_r_norm_monitor = self.true_residual_monitor.is_some();

            let mut dot_pairs: SmallVec<[(&[S], &[S]); 3]> = SmallVec::new();
            dot_pairs.push((&r[..], &z[..]));
            if need_r_norm_res || need_r_norm_monitor {
                dot_pairs.push((&r[..], &r[..]));
            }
            if need_z_norm_res {
                dot_pairs.push((&z[..], &z[..]));
            }
            let mut dot_results: SmallVec<[R; 3]> = SmallVec::new();
            dot_results.resize(dot_pairs.len(), R::default());
            self.dot_scalar_many(dot_pairs.as_slice(), comm, dot_results.as_mut_slice());

            let mut idx = 0;
            let mut rho_new = dot_results[idx];
            idx += 1;
            if !rho_new.is_finite() || rho_new < R::default() {
                return Err(KError::IndefinitePreconditioner);
            }
            if rho_new < 1e-300 {
                rho_new = R::default();
            }

            let mut r_norm = if need_r_norm_res || need_r_norm_monitor {
                let value = dot_results[idx];
                idx += 1;
                Some(
                    if value < R::default() {
                        R::default()
                    } else {
                        value
                    }
                    .sqrt(),
                )
            } else {
                None
            };
            let mut z_norm = if need_z_norm_res {
                let value = dot_results[idx];
                Some(
                    if value < R::default() {
                        R::default()
                    } else {
                        value
                    }
                    .sqrt(),
                )
            } else {
                None
            };

            drop(dot_pairs);
            drop(dot_results);

            match self.norm_type {
                CgNormType::Preconditioned => {
                    res = rho_new.abs().sqrt();
                }
                CgNormType::Unpreconditioned => {
                    res = r_norm.unwrap();
                }
                CgNormType::Natural => {
                    res = z_norm.unwrap();
                }
                CgNormType::None => {}
            }

            if call_monitors(monitors, k, res, 0) {
                return Ok(SolveStats::new(k, res, ConvergedReason::StoppedByMonitor));
            }
            if let Some(m) = &self.true_residual_monitor {
                let value = self.ensure_norm(r, comm, &mut r_norm);
                m(k, value, 0);
            }

            let res_check = match self.norm_type {
                CgNormType::Preconditioned => rho_new.abs().sqrt(),
                CgNormType::Unpreconditioned => self.ensure_norm(r, comm, &mut r_norm),
                CgNormType::Natural => self.ensure_norm(z, comm, &mut z_norm),
                CgNormType::None => self.ensure_norm(r, comm, &mut r_norm),
            };
            let (reason, mut stats) = self.conv.check(res_check, res0, k);
            if !matches!(reason, ConvergedReason::Continued) {
                stats.final_residual = self.ensure_norm(r, comm, &mut r_norm);
                return Ok(stats);
            }

            rho_prev = rho;
            rho = rho_new;
            cached_r_norm = r_norm;
        }

        let final_res = self.ensure_norm(r, comm, &mut cached_r_norm);
        Ok(SolveStats::new(
            self.conv.max_iters,
            final_res,
            ConvergedReason::DivergedMaxIts,
        ))
    }

    #[allow(clippy::too_many_arguments)]
    fn solve_pipelined_scalar<C: Comm + CommDeterministic + AllreduceOps>(
        &mut self,
        a: &dyn LinOp<S = f64>,
        pc: Option<&dyn Preconditioner>,
        b: &[S],
        x: &mut [S],
        pc_side: PcSide,
        comm: &C,
        monitors: &[Box<MonitorCallback<f64>>],
        work: &mut Workspace,
    ) -> Result<SolveStats<f64>, KError> {
        if pc_side != PcSide::Left {
            return Err(KError::InvalidInput(
                "Pipelined PCG requires left preconditioning with HPD M; choose PcSide::Left or use MINRES (Hermitian) / GMRES (general) instead".into(),
            ));
        }

        let n = b.len();
        if x.len() != n {
            return Err(KError::InvalidInput("dimension mismatch: x,b".into()));
        }

        let mut counters = SolverCounters::default();
        let mut buffers = ClassicWorkspace::acquire(work, n);
        let ClassicWorkspace {
            r,
            z,
            p,
            ap,
            scratch,
        } = &mut buffers;

        let zero_guess =
            !self.initial_guess_nonzero && x.iter().all(|&xi| xi.abs() <= R::default());
        if zero_guess {
            r.copy_from_slice(b);
        } else {
            matvec_s(a, x, &mut ap[..], scratch);
            for i in 0..n {
                r[i] = b[i] - ap[i];
            }
        }

        if let Some(pc) = pc {
            apply_pc_s(pc, PcSide::Left, r, &mut z[..], scratch)?;
        } else {
            z.copy_from_slice(r);
        }

        p.copy_from_slice(z);

        let mut opt = self.async_options();
        if opt.max_inflight == 0 {
            opt.max_inflight = 1;
        }

        let (h_rho0, _) = dot1_async_s(comm, r, z, &opt)?;
        let rho0 = {
            counters.num_global_reductions += 1;
            <C as AllreduceOps>::wait_pair(h_rho0).0
        };
        if !rho0.is_finite() || rho0 < R::default() {
            return Err(KError::IndefinitePreconditioner);
        }

        let (h_rnorm0, _) = nrm2_async_s(comm, r, &opt);
        let rnorm0_sq = {
            counters.num_global_reductions += 1;
            <C as AllreduceOps>::wait_pair(h_rnorm0).0
        };
        let rnorm0 = rnorm0_sq.sqrt();
        if rnorm0 == R::default() {
            return Ok(
                SolveStats::new(0, R::default(), ConvergedReason::ConvergedRtol)
                    .with_counters(counters),
            );
        }

        let _ = match self.norm_type {
            CgNormType::Preconditioned => rho0.sqrt(),
            CgNormType::Unpreconditioned | CgNormType::None => rnorm0,
            CgNormType::Natural => {
                let (h_z, _) = nrm2_async_s(comm, z, &opt);
                counters.num_global_reductions += 1;
                <C as AllreduceOps>::wait_pair(h_z).0.sqrt()
            }
        };

        let actual_res0 = self.nrm2_scalar(r, comm);
        counters.num_global_reductions += 1;
        if call_monitors(monitors, 0, actual_res0, counters.num_global_reductions) {
            return Ok(
                SolveStats::new(0, actual_res0, ConvergedReason::StoppedByMonitor)
                    .with_counters(counters),
            );
        }
        if let Some(m) = &self.true_residual_monitor {
            m(0, actual_res0, 0);
        }

        let (reason0, mut stats0) = self.conv.check(actual_res0, rnorm0, 0);
        if !matches!(reason0, ConvergedReason::Continued) {
            stats0.final_residual = actual_res0;
            counters.residual_replacements = 0;
            stats0.counters = counters;
            return Ok(stats0);
        }

        let replace_every = match self.variant {
            PcgVariant::Pipelined { replace_every } => replace_every,
            PcgVariant::Classic => 0,
        };

        let mut rho_curr = rho0;
        let mut rho_prev = rho0;
        let mut pending_rho: Option<AllreduceHandle<(R, R)>> = None;
        let mut iterations = 0usize;
        let mut residual_replacements = 0usize;
        let mut force_restart = false;

        'solve: loop {
            while iterations < self.conv.max_iters {
                if iterations > 0 {
                    let handle = pending_rho
                        .take()
                        .expect("pipelined PCG pending rho handle");
                    rho_curr = {
                        counters.num_global_reductions += 1;
                        <C as AllreduceOps>::wait_pair(handle).0
                    };
                    if !rho_curr.is_finite() || rho_curr < R::default() {
                        return Err(KError::IndefinitePreconditioner);
                    }

                    let _ = match self.norm_type {
                        CgNormType::Preconditioned => rho_curr.sqrt(),
                        CgNormType::Unpreconditioned | CgNormType::None => {
                            let (h_nr, _) = nrm2_async_s(comm, r, &opt);
                            counters.num_global_reductions += 1;
                            <C as AllreduceOps>::wait_pair(h_nr).0.sqrt()
                        }
                        CgNormType::Natural => {
                            let (h_z, _) = nrm2_async_s(comm, z, &opt);
                            counters.num_global_reductions += 1;
                            <C as AllreduceOps>::wait_pair(h_z).0.sqrt()
                        }
                    };

                    let actual_res = self.nrm2_scalar(r, comm);
                    counters.num_global_reductions += 1;

                    if call_monitors(
                        monitors,
                        iterations,
                        actual_res,
                        counters.num_global_reductions,
                    ) {
                        return Ok(
                            SolveStats::new(
                                iterations,
                                actual_res,
                                ConvergedReason::StoppedByMonitor,
                            )
                            .with_counters(counters),
                        );
                    }
                    if let Some(m) = &self.true_residual_monitor {
                        m(iterations, actual_res, 0);
                    }

                    let (reason, mut stats) = self.conv.check(actual_res, rnorm0, iterations);
                    if !matches!(reason, ConvergedReason::Continued) {
                        stats.final_residual = actual_res;
                        let tol = self.conv.atol.max(self.conv.rtol * rnorm0);
                        if actual_res > tol {
                            matvec_s(a, x, &mut ap[..], scratch);
                            for i in 0..n {
                                r[i] = b[i] - ap[i];
                            }
                            if let Some(pc) = pc {
                                apply_pc_s(pc, PcSide::Left, r, &mut z[..], scratch)?;
                            } else {
                                z.copy_from_slice(r);
                            }
                            residual_replacements += 1;
                            p.copy_from_slice(z);
                            rho_prev = R::default();

                            rho_curr = self.dot_scalar(r, z, comm);
                            counters.num_global_reductions += 1;
                            if !rho_curr.is_finite() || rho_curr < R::default() {
                                return Err(KError::IndefinitePreconditioner);
                            }

                            let _ = match self.norm_type {
                                CgNormType::Preconditioned => rho_curr.sqrt(),
                                CgNormType::Unpreconditioned | CgNormType::None => {
                                    counters.num_global_reductions += 1;
                                    self.nrm2_scalar(r, comm)
                                }
                                CgNormType::Natural => {
                                    counters.num_global_reductions += 1;
                                    self.nrm2_scalar(z, comm)
                                }
                            };

                            let (h_rho_next, _) = dot1_async_s(comm, r, z, &opt)?;
                            pending_rho = Some(h_rho_next);
                            counters.residual_replacements = residual_replacements;
                            continue;
                        }

                        counters.residual_replacements = residual_replacements;
                        stats.counters = counters;
                        return Ok(stats);
                    }
                }

                if iterations >= self.conv.max_iters {
                    break;
                }

                if iterations > 0 {
                    let beta = if rho_prev == R::default() {
                        R::default()
                    } else {
                        rho_curr / rho_prev
                    };
                    let beta_s = S::from_real(beta);
                    for i in 0..n {
                        p[i] = z[i] + beta_s * p[i];
                    }
                }

                matvec_s(a, p, &mut ap[..], scratch);

                #[cfg(not(feature = "complex"))]
                let pp_ap_local: R = p
                    .iter()
                    .zip(ap.iter())
                    .fold(R::default(), |acc, (&pi, &api)| acc + pi * api);

                #[cfg(feature = "complex")]
                let pp_ap_local: R = dot_result_to_real(dot_conj(p, ap));

                let (h_ppap, _) = comm.allreduce2_async(pp_ap_local, R::default(), &opt)?;
                let pp_ap = {
                    counters.num_global_reductions += 1;
                    <C as AllreduceOps>::wait_pair(h_ppap).0
                };
                if !pp_ap.is_finite() {
                    return Err(KError::IndefiniteMatrix);
                }
                if pp_ap.abs() <= f64::EPSILON {
                    return Ok(SolveStats::new(
                        iterations,
                        self.nrm2_scalar(r, comm),
                        ConvergedReason::ConvergedHappyBreakdown,
                    )
                    .with_counters({
                        counters.residual_replacements = residual_replacements;
                        counters
                    }));
                }

                let alpha = rho_curr / pp_ap;
                let alpha_s = S::from_real(alpha);
                for i in 0..n {
                    x[i] += alpha_s * p[i];
                    r[i] -= alpha_s * ap[i];
                }

                if let Some(pc) = pc {
                    apply_pc_s(pc, PcSide::Left, r, &mut z[..], scratch)?;
                } else {
                    z.copy_from_slice(r);
                }

                if replace_every > 0 && ((iterations + 1) % replace_every == 0) {
                    matvec_s(a, x, &mut ap[..], scratch);
                    for i in 0..n {
                        r[i] = b[i] - ap[i];
                    }
                    if let Some(pc) = pc {
                        apply_pc_s(pc, PcSide::Left, r, &mut z[..], scratch)?;
                    } else {
                        z.copy_from_slice(r);
                    }
                    residual_replacements += 1;
                    p.copy_from_slice(z);
                    force_restart = true;
                }

                let (h_rho_next, _) = dot1_async_s(comm, r, z, &opt)?;
                pending_rho = Some(h_rho_next);

                if force_restart {
                    rho_prev = R::default();
                    force_restart = false;
                } else {
                    rho_prev = rho_curr;
                }
                iterations += 1;
            }

            if let Some(handle) = pending_rho.take() {
                counters.num_global_reductions += 1;
                rho_curr = <C as AllreduceOps>::wait_pair(handle).0;
                if !rho_curr.is_finite() || rho_curr < R::default() {
                    return Err(KError::IndefinitePreconditioner);
                }

                let _ = match self.norm_type {
                    CgNormType::Preconditioned => rho_curr.sqrt(),
                    CgNormType::Unpreconditioned | CgNormType::None => {
                        let (h_nr, _) = nrm2_async_s(comm, r, &opt);
                        counters.num_global_reductions += 1;
                        <C as AllreduceOps>::wait_pair(h_nr).0.sqrt()
                    }
                    CgNormType::Natural => {
                        let (h_z, _) = nrm2_async_s(comm, z, &opt);
                        counters.num_global_reductions += 1;
                        <C as AllreduceOps>::wait_pair(h_z).0.sqrt()
                    }
                };

                let actual_res = self.nrm2_scalar(r, comm);
                counters.num_global_reductions += 1;

                if call_monitors(
                    monitors,
                    iterations,
                    actual_res,
                    counters.num_global_reductions,
                ) {
                    return Ok(
                        SolveStats::new(
                            iterations,
                            actual_res,
                            ConvergedReason::StoppedByMonitor,
                        )
                        .with_counters(counters),
                    );
                }
                if let Some(m) = &self.true_residual_monitor {
                    m(iterations, actual_res, 0);
                }

                let (reason, mut stats) = self.conv.check(actual_res, rnorm0, iterations);
                if !matches!(reason, ConvergedReason::Continued) {
                    stats.final_residual = actual_res;
                    let tol = self.conv.atol.max(self.conv.rtol * rnorm0);
                    if actual_res > tol {
                        if iterations >= self.conv.max_iters {
                            counters.residual_replacements = residual_replacements;
                            break 'solve;
                        }

                        matvec_s(a, x, &mut ap[..], scratch);
                        for i in 0..n {
                            r[i] = b[i] - ap[i];
                        }
                        if let Some(pc) = pc {
                            apply_pc_s(pc, PcSide::Left, r, &mut z[..], scratch)?;
                        } else {
                            z.copy_from_slice(r);
                        }
                        residual_replacements += 1;
                        p.copy_from_slice(z);
                        rho_prev = R::default();

                        rho_curr = self.dot_scalar(r, z, comm);
                        counters.num_global_reductions += 1;
                        if !rho_curr.is_finite() || rho_curr < R::default() {
                            return Err(KError::IndefinitePreconditioner);
                        }

                        let _ = match self.norm_type {
                            CgNormType::Preconditioned => rho_curr.sqrt(),
                            CgNormType::Unpreconditioned | CgNormType::None => {
                                counters.num_global_reductions += 1;
                                self.nrm2_scalar(r, comm)
                            }
                            CgNormType::Natural => {
                                counters.num_global_reductions += 1;
                                self.nrm2_scalar(z, comm)
                            }
                        };

                        let (h_rho_next, _) = dot1_async_s(comm, r, z, &opt)?;
                        pending_rho = Some(h_rho_next);
                        if iterations < self.conv.max_iters {
                            force_restart = true;
                        }
                        counters.residual_replacements = residual_replacements;
                        continue 'solve;
                    }

                    counters.residual_replacements = residual_replacements;
                    stats.counters = counters;
                    return Ok(stats);
                }
            }

            break 'solve;
        }

        counters.residual_replacements = residual_replacements;
        let final_res = self.nrm2_scalar(r, comm);
        Ok(
            SolveStats::new(iterations, final_res, ConvergedReason::DivergedMaxIts)
                .with_counters(counters),
        )
    }

    #[allow(clippy::too_many_arguments)]
    pub fn solve_with_comm<C: Comm + CommDeterministic + AllreduceOps>(
        &mut self,
        a: &dyn LinOp<S = f64>,
        pc: Option<&mut dyn Preconditioner>,
        b: &[f64],
        x: &mut [f64],
        pc_side: PcSide,
        comm: &C,
        monitors: Option<&[Box<MonitorCallback<f64>>]>,
        work: Option<&mut Workspace>,
    ) -> Result<SolveStats<f64>, KError> {
        self.solve_impl(a, pc, b, x, pc_side, comm, monitors, work)
    }

    #[allow(clippy::too_many_arguments)]
    fn solve_impl<C: Comm + CommDeterministic + AllreduceOps>(
        &mut self,
        a: &dyn LinOp<S = f64>,
        pc: Option<&mut dyn Preconditioner>,
        b: &[f64],
        x: &mut [f64],
        pc_side: PcSide,
        comm: &C,
        monitors: Option<&[Box<MonitorCallback<f64>>]>,
        work: Option<&mut Workspace>,
    ) -> Result<SolveStats<f64>, KError> {
        match self.variant {
            PcgVariant::Classic => self.solve_classic(a, pc, b, x, pc_side, comm, monitors, work),
            PcgVariant::Pipelined { .. } => {
                self.solve_pipelined(a, pc, b, x, pc_side, comm, monitors, work)
            }
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn solve_classic<C: Comm + CommDeterministic>(
        &mut self,
        a: &dyn LinOp<S = f64>,
        pc: Option<&mut dyn Preconditioner>,
        b: &[f64],
        x: &mut [f64],
        pc_side: PcSide,
        comm: &C,
        monitors: Option<&[Box<MonitorCallback<f64>>]>,
        work: Option<&mut Workspace>,
    ) -> Result<SolveStats<f64>, KError> {
        let pc_ref = pc.as_deref();
        if pc_side != PcSide::Left {
            return Err(KError::InvalidInput(
                "CG/PCG requires left preconditioning with HPD M; choose PcSide::Left or use MINRES (Hermitian) / GMRES (general) instead".into(),
            ));
        }

        let n = b.len();
        if x.len() != n {
            return Err(KError::InvalidInput("dimension mismatch: x,b".into()));
        }

        let monitors = monitors.unwrap_or(&[]);

        let mut owned_workspace;
        let work = match work {
            Some(ws) => ws,
            None => {
                owned_workspace = Workspace::new(n);
                &mut owned_workspace
            }
        };

        #[cfg(not(feature = "complex"))]
        let b_slice: &[S] = unsafe { &*(b as *const [f64] as *const [S]) };
        #[cfg(not(feature = "complex"))]
        let x_slice: &mut [S] = unsafe { &mut *(x as *mut [f64] as *mut [S]) };

        #[cfg(feature = "complex")]
        let b_owned: Vec<S> = b.iter().copied().map(S::from_real).collect();
        #[cfg(feature = "complex")]
        let mut x_owned: Vec<S> = x.iter().copied().map(S::from_real).collect();
        #[cfg(feature = "complex")]
        let b_slice: &[S] = &b_owned;
        #[cfg(feature = "complex")]
        let x_slice: &mut [S] = &mut x_owned;

        let stats = self.solve_classic_scalar(a, pc_ref, b_slice, x_slice, comm, monitors, work)?;

        #[cfg(feature = "complex")]
        {
            for (dst, src) in x.iter_mut().zip(x_slice.iter()) {
                *dst = src.real();
            }
        }

        Ok(stats)
    }

    #[allow(clippy::too_many_arguments)]
    fn solve_pipelined<C: Comm + CommDeterministic + AllreduceOps>(
        &mut self,
        a: &dyn LinOp<S = f64>,
        pc: Option<&mut dyn Preconditioner>,
        b: &[f64],
        x: &mut [f64],
        pc_side: PcSide,
        comm: &C,
        monitors: Option<&[Box<MonitorCallback<f64>>]>,
        work: Option<&mut Workspace>,
    ) -> Result<SolveStats<f64>, KError> {
        let pc_ref = pc.as_deref();
        if pc_side != PcSide::Left {
            return Err(KError::InvalidInput(
                "Pipelined PCG requires left preconditioning with HPD M; choose PcSide::Left or use MINRES (Hermitian) / GMRES (general) instead".into(),
            ));
        }

        let n = b.len();
        if x.len() != n {
            return Err(KError::InvalidInput("dimension mismatch: x,b".into()));
        }

        let monitors = monitors.unwrap_or(&[]);

        let mut owned_workspace;
        let work = match work {
            Some(ws) => ws,
            None => {
                owned_workspace = Workspace::new(n);
                &mut owned_workspace
            }
        };

        #[cfg(not(feature = "complex"))]
        let b_slice: &[S] = unsafe { &*(b as *const [f64] as *const [S]) };
        #[cfg(not(feature = "complex"))]
        let x_slice: &mut [S] = unsafe { &mut *(x as *mut [f64] as *mut [S]) };

        #[cfg(feature = "complex")]
        let b_owned: Vec<S> = b.iter().copied().map(S::from_real).collect();
        #[cfg(feature = "complex")]
        let mut x_owned: Vec<S> = x.iter().copied().map(S::from_real).collect();
        #[cfg(feature = "complex")]
        let b_slice: &[S] = &b_owned;
        #[cfg(feature = "complex")]
        let x_slice: &mut [S] = &mut x_owned;

        let stats = self
            .solve_pipelined_scalar(a, pc_ref, b_slice, x_slice, pc_side, comm, monitors, work)?;

        #[cfg(feature = "complex")]
        {
            for (dst, src) in x.iter_mut().zip(x_slice.iter()) {
                *dst = src.real();
            }
        }

        Ok(stats)
    }
}

impl LinearSolver for PcgSolver {
    type Error = KError;

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

    fn setup_workspace(&mut self, work: &mut Workspace) {
        if work.q.len() < 2 {
            work.q.resize(2, Vec::new());
        }
    }

    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> {
        self.solve_impl(a, pc, b, x, pc_side, comm, monitors, work)
    }
}