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
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
//! Distributed preconditioner helpers and wrappers.
//!
//! Local preconditioners operate purely on the data owned by the current rank.
//! This module exposes a small trait for global wrappers and the distributed
//! vector helper that carries only the local slice.

mod coarse;
mod native_plan;
#[cfg(all(not(feature = "complex"), feature = "mpi"))]
mod strict_mode;

pub use coarse::{DistCoarseRepartition, DistCoarseSolverRoute, DistCoarseStrategy};
pub use native_plan::{
    DistLocalApplyMode, DistRouteDecision, DistRouteDecisionReason, DistRouteDecisionReport,
    DistRouteFallbackReason, DistRoutePolicy, DistRoutePolicyBudget, DistRouteResolveInput,
    DistRouteSelection, resolve_dist_route, validate_dist_route_policy_budget,
};
#[cfg(all(not(feature = "complex"), feature = "mpi"))]
pub use strict_mode::validate_dist_builder_strict_mode;

use crate::algebra::scalar::S as ScalarAlias;
use crate::error::KError;
use crate::parallel::UniverseComm;
use crate::preconditioner::PcSide;
#[cfg(all(not(feature = "complex"), feature = "mpi"))]
use crate::preconditioner::Preconditioner;
#[cfg(all(not(feature = "complex"), feature = "mpi"))]
use crate::preconditioner::asm::{AsmBlockSolver, AsmInnerPc, AsmMode, DistributedAsm, Weighting};
use crate::preconditioner::ilu::IluConfig;
use crate::utils::conditioning::ConditioningOptions;
use std::str::FromStr;
#[cfg(all(not(feature = "complex"), feature = "mpi"))]
use std::sync::Mutex;

#[cfg(all(not(feature = "complex"), feature = "mpi"))]
use crate::algebra::scalar::S;
#[cfg(all(not(feature = "complex"), feature = "mpi"))]
use crate::matrix::DistCsrOp;
#[cfg(all(not(feature = "complex"), feature = "mpi"))]
use crate::matrix::op::LinOp;

/// Global distributed preconditioner modes exposed to CLI.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum GlobalPcKind {
    /// No MPI-level preconditioning.
    None,
    /// Block-Jacobi wrapping a local ILU-like solver.
    BlockJacobi,
    /// Additive Schwarz.
    Asm,
    /// Restricted additive Schwarz (RAS).
    Ras,
}

impl FromStr for GlobalPcKind {
    type Err = KError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "none" => Ok(GlobalPcKind::None),
            "block-jacobi" | "blockjacobi" | "block_jacobi" => Ok(GlobalPcKind::BlockJacobi),
            "asm" => Ok(GlobalPcKind::Asm),
            "ras" => Ok(GlobalPcKind::Ras),
            other => Err(KError::InvalidInput(format!(
                "Unknown pc_global value: {other}"
            ))),
        }
    }
}

/// Local block preconditioner family used within block-Jacobi.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LocalPcKind {
    Ilu,
    Ilut,
    Ilutp,
    Jacobi,
    Sor,
    Chebyshev,
    Fsai,
    Spai,
}

/// Capability metadata used to negotiate distributed-native compatibility for
/// block-Jacobi local preconditioner builders.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct LocalPcBuildCapabilities {
    pub native_local_apply: bool,
}

impl FromStr for LocalPcKind {
    type Err = KError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "ilu" => Ok(LocalPcKind::Ilu),
            "ilut" => Ok(LocalPcKind::Ilut),
            "ilutp" => Ok(LocalPcKind::Ilutp),
            "jacobi" => Ok(LocalPcKind::Jacobi),
            "sor" => Ok(LocalPcKind::Sor),
            "chebyshev" | "cheby" => Ok(LocalPcKind::Chebyshev),
            "fsai" => Ok(LocalPcKind::Fsai),
            "spai" | "approxinv" => Ok(LocalPcKind::Spai),
            other => Err(KError::InvalidInput(format!(
                "Unknown pc_local value: {other}"
            ))),
        }
    }
}

impl LocalPcKind {
    /// Capabilities advertised by each local preconditioner when used inside
    /// distributed block-Jacobi wrappers.
    pub const fn build_capabilities(self) -> LocalPcBuildCapabilities {
        match self {
            Self::Ilu
            | Self::Ilut
            | Self::Ilutp
            | Self::Jacobi
            | Self::Sor
            | Self::Chebyshev
            | Self::Fsai => LocalPcBuildCapabilities {
                native_local_apply: true,
            },
            Self::Spai => LocalPcBuildCapabilities {
                native_local_apply: true,
            },
        }
    }
}

/// Parsed MPI-specific PC options.
///
/// Default policy favors DistCsr-native distributed kernels whenever
/// communicator and operator constraints are satisfied. Adapter routes are
/// treated as explicit fallback paths via `pc_dist_route=adapted`.
#[derive(Clone, Debug)]
pub struct MpiPcOptions {
    pub global_pc: GlobalPcKind,
    pub local_pc: LocalPcKind,
    pub ilu_config: IluConfig,
    pub conditioning: ConditioningOptions,
    pub ilut_fill: usize,
    pub ilut_drop_tol: f64,
    pub ilut_perm_tol: f64,
    pub ilutp_max_fill: usize,
    pub ilutp_drop_tol: f64,
    pub ilutp_perm_tol: f64,
    pub local_apply_mode: DistLocalApplyMode,
    pub route_policy: DistRoutePolicy,
    pub route_policy_budget: DistRoutePolicyBudget,
}

impl Default for MpiPcOptions {
    fn default() -> Self {
        Self {
            global_pc: GlobalPcKind::None,
            local_pc: LocalPcKind::Ilu,
            ilu_config: IluConfig::default(),
            conditioning: ConditioningOptions::default(),
            ilut_fill: 10,
            ilut_drop_tol: 1e-4,
            ilut_perm_tol: 0.1,
            ilutp_max_fill: 10,
            ilutp_drop_tol: 1e-4,
            ilutp_perm_tol: 0.1,
            local_apply_mode: DistLocalApplyMode::NativeLocalHalo,
            route_policy: DistRoutePolicy::Native,
            route_policy_budget: DistRoutePolicyBudget::default(),
        }
    }
}

/// Distributed preconditioners expose an MPI-friendly apply API.
pub trait DistributedPreconditioner: Send + Sync {
    type Scalar;

    /// Apply the distributed preconditioner to a global vector.
    fn apply_global(&self, side: PcSide, x: &mut DistVecS<'_, Self::Scalar>) -> Result<(), KError>;
}

/// Simple distributed vector carrying only the owned local slice.
#[derive(Debug)]
pub struct DistVecS<'a, S> {
    comm: UniverseComm,
    row_offset: usize,
    global_len: usize,
    local: DistVecLocal<'a, S>,
    scratch: DistVecScratch<'a, S>,
}

#[derive(Debug)]
enum DistVecLocal<'a, S> {
    Owned(Vec<S>),
    Borrowed(&'a mut [S]),
}

#[derive(Debug)]
enum DistVecScratch<'a, S> {
    Owned(Vec<S>),
    Borrowed(&'a mut Vec<S>),
}

/// Backward-compatible distributed vector alias on top of the crate scalar `S`.
pub type DistVec<'a> = DistVecS<'a, ScalarAlias>;

impl<S: Copy + Default> DistVecS<'_, S> {
    /// Construct a distributed vector for the current rank.
    pub fn new(comm: UniverseComm, row_offset: usize, global_len: usize, local: Vec<S>) -> Self {
        Self::with_scratch(comm, row_offset, global_len, local, Vec::new())
    }

    /// Construct a distributed vector with reusable owned work buffers.
    pub fn with_scratch(
        comm: UniverseComm,
        row_offset: usize,
        global_len: usize,
        local: Vec<S>,
        scratch: Vec<S>,
    ) -> Self {
        if row_offset > global_len {
            panic!("row_offset ({row_offset}) must be < global_len ({global_len})");
        }
        if row_offset + local.len() > global_len {
            panic!(
                "local slice length ({}) at offset {} exceeds global length {}",
                local.len(),
                row_offset,
                global_len
            );
        }
        Self {
            comm,
            row_offset,
            global_len,
            local: DistVecLocal::Owned(local),
            scratch: DistVecScratch::Owned(scratch),
        }
    }

    /// Construct a distributed vector that writes directly into a borrowed local slice.
    pub fn from_local_slice<'a>(
        comm: UniverseComm,
        row_offset: usize,
        global_len: usize,
        local: &'a mut [S],
        scratch: &'a mut Vec<S>,
    ) -> DistVecS<'a, S> {
        if row_offset > global_len {
            panic!("row_offset ({row_offset}) must be < global_len ({global_len})");
        }
        if row_offset + local.len() > global_len {
            panic!(
                "local slice length ({}) at offset {} exceeds global length {}",
                local.len(),
                row_offset,
                global_len
            );
        }
        DistVecS {
            comm,
            row_offset,
            global_len,
            local: DistVecLocal::Borrowed(local),
            scratch: DistVecScratch::Borrowed(scratch),
        }
    }

    /// Communicator owning this vector.
    pub fn comm(&self) -> &UniverseComm {
        &self.comm
    }

    /// Global length of the distributed vector.
    pub fn global_len(&self) -> usize {
        self.global_len
    }

    /// Row offset of the local slice within the global vector.
    pub fn row_offset(&self) -> usize {
        self.row_offset
    }

    /// Local slice owned by this rank.
    pub fn local_view(&self) -> &[S] {
        match &self.local {
            DistVecLocal::Owned(local) => local,
            DistVecLocal::Borrowed(local) => local,
        }
    }

    /// Mutable local slice owned by this rank.
    pub fn local_view_mut(&mut self) -> &mut [S] {
        match &mut self.local {
            DistVecLocal::Owned(local) => local,
            DistVecLocal::Borrowed(local) => local,
        }
    }

    /// Number of entries owned by this rank.
    pub fn local_len(&self) -> usize {
        self.local_view().len()
    }

    /// Return a mutable scratch slice sized for local operations.
    pub fn scratch_mut(&mut self) -> &mut [S] {
        let local_len = self.local_len();
        let scratch = match &mut self.scratch {
            DistVecScratch::Owned(scratch) => scratch,
            DistVecScratch::Borrowed(scratch) => scratch,
        };
        if scratch.len() != local_len {
            scratch.resize(local_len, S::default());
        }
        scratch.as_mut_slice()
    }

    /// Current scratch slice.
    pub fn scratch_view(&self) -> &[S] {
        match &self.scratch {
            DistVecScratch::Owned(scratch) => scratch,
            DistVecScratch::Borrowed(scratch) => scratch,
        }
    }

    /// Copy local data into the reusable scratch buffer.
    pub fn copy_local_to_scratch(&mut self) {
        let local_len = self.local_len();
        let local_ptr = self.local_view().as_ptr();
        let scratch = self.scratch_mut();
        debug_assert_eq!(scratch.len(), local_len);
        // SAFETY: local and scratch refer to independent buffers managed by this type.
        unsafe {
            std::ptr::copy_nonoverlapping(local_ptr, scratch.as_mut_ptr(), local_len);
        }
    }

    /// Provide immutable scratch input and mutable local output slices.
    pub fn with_scratch_input_local_output<R>(&mut self, f: impl FnOnce(&[S], &mut [S]) -> R) -> R {
        self.copy_local_to_scratch();
        let len = self.local_len();
        let in_ptr = self.scratch_mut().as_ptr();
        let out_ptr = self.local_view_mut().as_mut_ptr();
        // SAFETY: scratch and local are distinct storage owned by separate fields.
        unsafe {
            let x_local = std::slice::from_raw_parts(in_ptr, len);
            let y_local = std::slice::from_raw_parts_mut(out_ptr, len);
            f(x_local, y_local)
        }
    }
}

/// Builder configuration for distributed preconditioner adapters.
#[cfg(all(not(feature = "complex"), feature = "mpi"))]
#[derive(Clone, Debug)]
pub enum DistPcBuilder {
    BlockJacobi {
        opts: MpiPcOptions,
    },
    Asm {
        overlap: usize,
        subdomain_hint: Option<usize>,
        block_solver: AsmBlockSolver,
        inner_pc: AsmInnerPc,
        weighting: Weighting,
        coarse_strategy: DistCoarseStrategy,
        local_apply_mode: DistLocalApplyMode,
    },
    Ras {
        overlap: usize,
        subdomain_hint: Option<usize>,
        block_solver: AsmBlockSolver,
        inner_pc: AsmInnerPc,
        weighting: Weighting,
        coarse_strategy: DistCoarseStrategy,
        local_apply_mode: DistLocalApplyMode,
    },
}

#[cfg(all(not(feature = "complex"), feature = "mpi"))]
#[derive(Debug)]
struct DistAsmPc {
    inner: DistributedAsm,
}

#[cfg(all(not(feature = "complex"), feature = "mpi"))]
impl DistAsmPc {
    fn new(mut inner: DistributedAsm, dist_op: &DistCsrOp) -> Result<Self, KError> {
        inner.setup(dist_op)?;
        Ok(Self { inner })
    }
}

#[cfg(all(not(feature = "complex"), feature = "mpi"))]
impl DistributedPreconditioner for DistAsmPc {
    type Scalar = f64;

    fn apply_global(&self, side: PcSide, x: &mut DistVecS<'_, f64>) -> Result<(), KError> {
        x.with_scratch_input_local_output(|x_local, y_local| {
            self.inner.apply(side, x_local, y_local)
        })?;
        Ok(())
    }
}

/// Adapter bridging a `DistributedPreconditioner` to the `Preconditioner` trait.
#[cfg(all(not(feature = "complex"), feature = "mpi"))]
pub struct DistPcAdapter {
    comm: UniverseComm,
    row_offset: usize,
    global_len: usize,
    local_len: usize,
    inner: Box<dyn DistributedPreconditioner<Scalar = f64>>,
    builder: DistPcBuilder,
    workspace: Mutex<Vec<f64>>,
}

#[cfg(all(not(feature = "complex"), feature = "mpi"))]
impl DistPcAdapter {
    pub fn build(dist_op: &DistCsrOp, builder: DistPcBuilder) -> Result<Self, KError> {
        let inner = build_dist_pc(dist_op, &builder)?;
        Ok(Self::new(dist_op, inner, builder))
    }

    fn new(
        dist_op: &DistCsrOp,
        inner: Box<dyn DistributedPreconditioner<Scalar = f64>>,
        builder: DistPcBuilder,
    ) -> Self {
        let comm = dist_op.comm();
        let row_offset = dist_op.local_row_offset();
        let global_len = dist_op.n_global;
        let local_len = dist_op.local_nrows();
        Self {
            comm,
            row_offset,
            global_len,
            local_len,
            inner,
            builder,
            workspace: Mutex::new(Vec::new()),
        }
    }

    fn rebuild(&mut self, dist_op: &DistCsrOp) -> Result<(), KError> {
        self.inner = build_dist_pc(dist_op, &self.builder)?;
        self.comm = dist_op.comm();
        self.row_offset = dist_op.local_row_offset();
        self.global_len = dist_op.n_global;
        self.local_len = dist_op.local_nrows();
        Ok(())
    }
}

#[cfg(all(not(feature = "complex"), feature = "mpi"))]
impl Preconditioner for DistPcAdapter {
    fn dims(&self) -> (usize, usize) {
        (self.local_len, self.local_len)
    }

    fn setup(&mut self, op: &dyn crate::matrix::op::LinOp<S = S>) -> Result<(), KError> {
        let dist_op = op
            .as_any()
            .downcast_ref::<DistCsrOp>()
            .ok_or_else(|| KError::InvalidInput("distributed PC requires a DistCsrOp".into()))?;
        self.rebuild(dist_op)
    }

    fn apply(&self, side: PcSide, x: &[S], y: &mut [S]) -> Result<(), KError> {
        if x.len() != self.local_len || y.len() != self.local_len {
            return Err(KError::InvalidInput(
                "distributed PC apply length mismatch".into(),
            ));
        }
        y.copy_from_slice(x);
        let mut workspace = self
            .workspace
            .lock()
            .expect("distributed PC workspace mutex poisoned");
        let mut dist_vec = DistVecS::<f64>::from_local_slice(
            self.comm.clone(),
            self.row_offset,
            self.global_len,
            y,
            &mut workspace,
        );
        self.inner.apply_global(side, &mut dist_vec)?;
        Ok(())
    }

    fn apply_mut(&mut self, side: PcSide, x: &[S], y: &mut [S]) -> Result<(), KError> {
        self.apply(side, x, y)
    }

    fn distributed_support(&self) -> crate::preconditioner::PcDistributedSupport {
        crate::preconditioner::PcDistributedSupport::Distributed
    }
}

#[cfg(all(not(feature = "complex"), feature = "mpi"))]
fn build_dist_pc(
    dist_op: &DistCsrOp,
    builder: &DistPcBuilder,
) -> Result<Box<dyn DistributedPreconditioner<Scalar = f64>>, KError> {
    validate_dist_builder_strict_mode(dist_op, builder)?;
    match builder {
        DistPcBuilder::BlockJacobi { opts } => {
            #[cfg(feature = "backend-faer")]
            {
                let pc = build_block_jacobi_pc(dist_op, opts)?.ok_or_else(|| {
                    KError::InvalidInput("block-Jacobi PC not constructed".into())
                })?;
                Ok(pc)
            }
            #[cfg(not(feature = "backend-faer"))]
            {
                let _ = dist_op;
                let _ = opts;
                Err(KError::Unsupported(
                    "block-Jacobi distributed PC requires backend-faer".into(),
                ))
            }
        }
        DistPcBuilder::Asm {
            overlap,
            subdomain_hint,
            block_solver,
            inner_pc,
            weighting,
            coarse_strategy,
            local_apply_mode: _,
        } => {
            let asm = DistributedAsm::new(
                *overlap,
                *subdomain_hint,
                *block_solver,
                *inner_pc,
                AsmMode::ASM,
                *weighting,
                *coarse_strategy,
            );
            Ok(Box::new(DistAsmPc::new(asm, dist_op)?))
        }
        DistPcBuilder::Ras {
            overlap,
            subdomain_hint,
            block_solver,
            inner_pc,
            weighting,
            coarse_strategy,
            local_apply_mode: _,
        } => {
            let asm = DistributedAsm::new(
                *overlap,
                *subdomain_hint,
                *block_solver,
                *inner_pc,
                AsmMode::RAS,
                *weighting,
                *coarse_strategy,
            );
            Ok(Box::new(DistAsmPc::new(asm, dist_op)?))
        }
    }
}

#[cfg(feature = "backend-faer")]
pub mod block_jacobi_ilu;

#[cfg(feature = "backend-faer")]
pub use block_jacobi_ilu::BlockJacobiLocalPc;

#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
pub use block_jacobi_ilu::{
    build_block_jacobi_ilu_pc, build_block_jacobi_ilut_pc, build_block_jacobi_ilutp_pc,
    build_block_jacobi_pc,
};

#[cfg(test)]
mod tests {
    use super::{DistVec, DistVecS, DistributedPreconditioner};
    use crate::algebra::scalar::S;
    use crate::parallel::UniverseComm;
    use crate::preconditioner::PcSide;

    struct CopyPc;

    impl DistributedPreconditioner for CopyPc {
        type Scalar = S;

        fn apply_global(
            &self,
            _side: PcSide,
            x: &mut DistVecS<'_, Self::Scalar>,
        ) -> Result<(), crate::error::KError> {
            x.with_scratch_input_local_output(|xin, yout| yout.copy_from_slice(xin));
            Ok(())
        }
    }

    #[test]
    fn dist_vec_alias_matches_scalar_alias() {
        let comm = UniverseComm::NoComm(crate::parallel::NoComm);
        let mut local = vec![S::default(); 3];
        let mut scratch = vec![S::default(); 3];
        let x: DistVec<'_> = DistVecS::from_local_slice(comm, 0, 3, &mut local, &mut scratch);
        assert_eq!(x.local_len(), 3);
    }

    #[test]
    fn dist_vec_with_scratch_roundtrip_for_current_scalar() {
        let comm = UniverseComm::NoComm(crate::parallel::NoComm);
        let mut x = DistVec::new(comm, 0, 4, vec![S::default(); 4]);
        let pc = CopyPc;
        pc.apply_global(PcSide::Left, &mut x).expect("copy apply");
        assert_eq!(x.local_len(), 4);
        assert_eq!(x.scratch_view().len(), 4);
    }

    #[cfg(feature = "complex")]
    #[test]
    fn dist_vec_complex_transport_preserves_values() {
        let comm = UniverseComm::NoComm(crate::parallel::NoComm);
        let mut local = vec![
            num_complex::Complex64::new(1.0, -2.0),
            num_complex::Complex64::new(-0.5, 3.0),
        ];
        let mut scratch = Vec::new();
        let mut x = DistVecS::from_local_slice(comm, 0, 2, &mut local, &mut scratch);
        x.with_scratch_input_local_output(|xin, yout| {
            yout.copy_from_slice(xin);
        });
        assert_eq!(x.local_view()[0], num_complex::Complex64::new(1.0, -2.0));
        assert_eq!(x.local_view()[1], num_complex::Complex64::new(-0.5, 3.0));
    }
}