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
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
#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
use crate::algebra::scalar::{KrystScalar, S};
use crate::error::KError;
#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
use crate::matrix::dist::halo::HaloPlan;
#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
use crate::matrix::dist_csr::DistCsrOp;
#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
use crate::matrix::op::LinOp;
use crate::parallel::{Comm, UniverseComm};
#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
use crate::preconditioner::approxinv_csr::{ApproxInvKind, ApproxInvParams, FsaiCsr, SpaiCsr};
#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
use crate::preconditioner::chebyshev::ChebyshevPc;
#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
use crate::preconditioner::ilu::{Ilu, IluConfig};
#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
use crate::preconditioner::ilut::RowFilterPreconditioner;
#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
use crate::preconditioner::ilutp::Ilutp;
#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
use crate::preconditioner::jacobi::Jacobi;
#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
use crate::preconditioner::legacy::Preconditioner as LegacyPreconditioner;
#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
use crate::preconditioner::sor::{MatSorType, SorPc};
use crate::preconditioner::{LocalPreconditioner, PcSide, Preconditioner as ObjPreconditioner};
use crate::utils::conditioning::ConditioningOptions;
#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
use std::collections::BTreeMap;
#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
use std::sync::Arc;

#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
use crate::matrix::op::CsrOp;

use super::{DistLocalApplyMode, DistVecS, DistributedPreconditioner};
#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
use super::{GlobalPcKind, LocalPcKind, MpiPcOptions};

#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
#[derive(Clone)]
struct NativeCouplingPlan {
    halo: Arc<HaloPlan>,
    remote_entries_by_row: Vec<Vec<(usize, f64)>>,
    diag_inv: Vec<f64>,
    omega: f64,
    coarse_weight: f64,
}

#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
#[derive(Clone, Copy, Debug)]
enum NativePlanBuildReason {
    ReusedOperatorHalo,
    BuiltDedicatedHalo,
    IncompatibleHaloIndex,
}

#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
impl NativePlanBuildReason {
    fn code(self) -> &'static str {
        match self {
            Self::ReusedOperatorHalo => "reused_operator_halo",
            Self::BuiltDedicatedHalo => "built_dedicated_halo",
            Self::IncompatibleHaloIndex => "incompatible_halo_index",
        }
    }
}

#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
impl NativeCouplingPlan {
    fn from_dist_op(
        dist_op: &DistCsrOp,
        omega: f64,
        local_apply_mode: DistLocalApplyMode,
    ) -> Result<Self, KError> {
        let local = dist_op.local_matrix();
        let row_start = dist_op.local_row_offset();
        let row_end = row_start + dist_op.local_nrows();
        let part = dist_op.row_partition();
        let rank = dist_op.comm().rank();

        let mut recv_map: BTreeMap<usize, Vec<usize>> = BTreeMap::new();
        let mut remote_entries_by_row = vec![Vec::new(); local.nrows()];
        let mut diag_inv = vec![1.0; local.nrows()];

        for row in 0..local.nrows() {
            let mut diag = None;
            for idx in local.row_ptr()[row]..local.row_ptr()[row + 1] {
                let gcol = local.col_idx()[idx];
                if gcol == row_start + row {
                    diag = Some(local.values()[idx]);
                    continue;
                }
                if gcol < row_start || gcol >= row_end {
                    let owner = owner_of(gcol, part.as_ref());
                    if owner == rank {
                        continue;
                    }
                    recv_map.entry(owner).or_default().push(gcol);
                    remote_entries_by_row[row].push((gcol, local.values()[idx]));
                }
            }
            if let Some(d) = diag
                && d.abs() > 1e-14
            {
                diag_inv[row] = 1.0 / d;
            }
        }

        for cols in recv_map.values_mut() {
            cols.sort_unstable();
            cols.dedup();
        }

        let (halo, reason) = Self::build_halo_plan(dist_op, &recv_map, row_start, row_end, part)?;
        log::debug!("native coupling halo plan reason_code={}", reason.code());

        Ok(Self {
            halo: Arc::new(halo),
            remote_entries_by_row,
            diag_inv,
            omega,
            coarse_weight: if matches!(local_apply_mode, DistLocalApplyMode::NativeHybrid) {
                0.1
            } else {
                0.0
            },
        })
    }

    fn build_halo_plan(
        dist_op: &DistCsrOp,
        recv_map: &BTreeMap<usize, Vec<usize>>,
        row_start: usize,
        row_end: usize,
        part: Arc<Vec<usize>>,
    ) -> Result<(HaloPlan, NativePlanBuildReason), KError> {
        let op_index = dist_op.halo_index();
        if Self::halo_index_compatible(op_index.as_ref(), recv_map) {
            return Ok((
                HaloPlan::from_shared_index(op_index),
                NativePlanBuildReason::ReusedOperatorHalo,
            ));
        }

        let reason = NativePlanBuildReason::IncompatibleHaloIndex;
        log::debug!(
            "native coupling halo reuse skipped reason_code={}",
            reason.code()
        );

        Ok((
            HaloPlan::new(dist_op.comm(), part, row_start, row_end, recv_map.clone())?,
            NativePlanBuildReason::BuiltDedicatedHalo,
        ))
    }

    fn halo_index_compatible(
        op_index: &crate::matrix::dist::halo::HaloIndexPlan,
        recv_map: &BTreeMap<usize, Vec<usize>>,
    ) -> bool {
        if op_index.recv_map.len() != recv_map.len() {
            return false;
        }
        recv_map.iter().all(|(nbr, cols)| {
            if let Some(op_cols) = op_index.recv_map.get(nbr) {
                let mut lhs = cols.clone();
                let mut rhs = op_cols.clone();
                lhs.sort_unstable();
                lhs.dedup();
                rhs.sort_unstable();
                rhs.dedup();
                lhs == rhs
            } else {
                false
            }
        })
    }

    fn apply_remote_correction(&self, x_local: &[f64], y_local: &mut [f64]) {
        if self.halo.index.n_ghost == 0 {
            return;
        }
        let req = self.halo.post_halo(x_local);
        self.halo.complete_halo(req);
        let ghost = self.halo.ghost_slice_ref();
        for (row, entries) in self.remote_entries_by_row.iter().enumerate() {
            if entries.is_empty() {
                continue;
            }
            let mut remote_sum = 0.0;
            for &(gcol, value) in entries {
                if let Some(&gidx) = self.halo.index.ghost_index_of.get(&gcol) {
                    remote_sum += value * ghost[gidx];
                } else {
                    log::error!(
                        "native coupling halo mismatch reason_code=missing_ghost_column gcol={gcol}"
                    );
                }
            }
            y_local[row] -= self.omega * self.diag_inv[row] * remote_sum;
        }

        if self.coarse_weight != 0.0 && !x_local.is_empty() {
            let comm = &self.halo.index.comm;
            let local_sum: f64 = x_local.iter().copied().sum();
            let global_sum = comm.all_reduce_f64(local_sum);
            let global_n = comm.all_reduce_f64(x_local.len() as f64).max(1.0);
            let coarse_avg = global_sum / global_n;
            for yi in y_local.iter_mut() {
                *yi += self.coarse_weight * coarse_avg;
            }
        }
    }
}

#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
fn maybe_native_plan(
    dist_op: &DistCsrOp,
    local_apply_mode: DistLocalApplyMode,
    supports_native: bool,
    local_pc_name: &str,
) -> Result<Option<NativeCouplingPlan>, KError> {
    if !local_apply_mode.is_distributed_native() {
        return Ok(None);
    }
    if !supports_native {
        if local_apply_mode.requires_native() {
            return Err(KError::InvalidInput(format!(
                "pc_dist_local_apply=strict requested but pc_local={local_pc_name} only supports wrapped_local mode"
            )));
        }
        log::warn!(
            "Distributed native route unavailable for pc_local={local_pc_name}; falling back to wrapped_local compatibility adapter"
        );
        return Ok(None);
    }
    Ok(Some(NativeCouplingPlan::from_dist_op(
        dist_op,
        1.0,
        local_apply_mode,
    )?))
}

#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
fn strict_native_unavailable(local_pc_name: &str) -> KError {
    KError::InvalidInput(format!(
        "pc_dist_local_apply=strict requested but pc_local={local_pc_name} only supports wrapped_local mode"
    ))
}

#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
fn build_obj_block_pc<L>(
    dist_op: &DistCsrOp,
    mut local_pc: L,
    local_apply_mode: DistLocalApplyMode,
    supports_native: bool,
    local_pc_name: &str,
) -> Result<BlockJacobiObjPc, KError>
where
    L: ObjPreconditioner + 'static,
{
    let local_square = dist_op.local_square_block();
    let local = Arc::new(local_square.as_csr().clone());
    let op = CsrOp::new(local);
    ObjPreconditioner::setup(&mut local_pc, &op)?;
    let native_plan = if supports_native {
        maybe_native_plan(dist_op, local_apply_mode, true, local_pc_name)?
    } else if local_apply_mode.requires_native() {
        return Err(strict_native_unavailable(local_pc_name));
    } else {
        None
    };
    Ok(BlockJacobiObjPc::new(
        dist_op.comm(),
        Box::new(local_pc),
        dist_op.local_row_offset(),
        dist_op.local_nrows(),
        local_apply_mode,
        native_plan,
    ))
}

#[cfg(all(feature = "mpi", feature = "backend-faer", not(feature = "complex")))]
fn assemble_global_input(
    comm: &UniverseComm,
    x_local: &[f64],
    global_len: usize,
) -> Result<Vec<f64>, KError> {
    use mpi::traits::*;
    let mut out = vec![0.0; global_len];
    match comm {
        UniverseComm::Mpi(comm_impl) => {
            let local_len = x_local.len() as i32;
            let mut lengths = vec![0i32; comm_impl.size];
            comm_impl
                .world
                .all_gather_into(&local_len, &mut lengths[..]);
            let max_len = lengths.iter().copied().max().unwrap_or(0) as usize;
            let mut padded = vec![0.0; max_len];
            padded[..x_local.len()].copy_from_slice(x_local);
            let mut gathered = vec![0.0; max_len * comm_impl.size];
            if max_len > 0 {
                comm_impl
                    .world
                    .all_gather_into(&padded[..], &mut gathered[..]);
            }
            let mut offset = 0usize;
            for (r, &len_i32) in lengths.iter().enumerate() {
                let len = len_i32.max(0) as usize;
                if offset + len > global_len {
                    return Err(KError::InvalidInput(
                        "replicated apply allgatherv exceeded global length".into(),
                    ));
                }
                let start = r * max_len;
                out[offset..offset + len].copy_from_slice(&gathered[start..start + len]);
                offset += len;
            }
            if offset != global_len {
                return Err(KError::InvalidInput(format!(
                    "replicated apply expected global_len={global_len}, assembled={offset}"
                )));
            }
            Ok(out)
        }
        _ => {
            if x_local.len() != global_len {
                return Err(KError::InvalidInput(
                    "replicated apply requires mpi communicator with distributed layout".into(),
                ));
            }
            out.copy_from_slice(x_local);
            Ok(out)
        }
    }
}

fn owner_of(gcol: usize, row_part: &[usize]) -> usize {
    let mut lo = 0usize;
    let mut hi = row_part.len().saturating_sub(2);
    while lo <= hi {
        let mid = (lo + hi) / 2;
        if gcol < row_part[mid + 1] {
            if gcol >= row_part[mid] {
                return mid;
            }
            if mid == 0 {
                break;
            }
            hi = mid - 1;
        } else {
            lo = mid + 1;
        }
    }
    lo
}

/// Distributed block-Jacobi wrapper around a local ILU-like preconditioner.
///
/// Each rank usually applies the local preconditioner to its owned slice.
///
/// Optional replicated-global mode (opt-in via `pc_dist_local_apply=replicated_global`)
/// first assembles the full input vector on every rank via Allgatherv-style communication,
/// applies a replicated factorization, then extracts each rank's owned segment. This improves
/// semantic correctness for replicated factors at the cost of O(N) memory/rank and O(N)
/// communicated scalars per apply.
pub struct BlockJacobiLocalPc<LPC>
where
    LPC: LocalPreconditioner<f64>,
{
    comm: UniverseComm,
    local_pc: LPC,
    row_offset: usize,
    n_local: usize,
    local_apply_mode: DistLocalApplyMode,
    #[cfg(all(feature = "backend-faer", not(feature = "complex")))]
    native_plan: Option<NativeCouplingPlan>,
}

impl<LPC> BlockJacobiLocalPc<LPC>
where
    LPC: LocalPreconditioner<f64>,
{
    /// Construct a new distributed block-Jacobi preconditioner.
    fn new(
        comm: UniverseComm,
        local_pc: LPC,
        row_offset: usize,
        local_apply_mode: DistLocalApplyMode,
        #[cfg(all(feature = "backend-faer", not(feature = "complex")))] native_plan: Option<
            NativeCouplingPlan,
        >,
    ) -> Self {
        let (n_local, _) = local_pc.dims();
        Self {
            comm,
            local_pc,
            row_offset,
            n_local,
            local_apply_mode,
            #[cfg(all(feature = "backend-faer", not(feature = "complex")))]
            native_plan,
        }
    }

    /// Communicator used by this preconditioner.
    pub fn comm(&self) -> &UniverseComm {
        &self.comm
    }

    /// Global row offset for this block.
    pub fn row_offset(&self) -> usize {
        self.row_offset
    }

    /// Number of rows owned locally.
    pub fn n_local(&self) -> usize {
        self.n_local
    }
}

impl<LPC> DistributedPreconditioner for BlockJacobiLocalPc<LPC>
where
    LPC: LocalPreconditioner<f64>,
{
    type Scalar = f64;

    fn apply_global(&self, side: PcSide, x: &mut DistVecS<'_, f64>) -> Result<(), KError> {
        debug_assert_eq!(x.row_offset(), self.row_offset);
        debug_assert_eq!(x.local_len(), self.n_local);
        if self.n_local == 0 {
            return Ok(());
        }

        debug_assert!(matches!(side, PcSide::Left));
        let x_global_len = x.global_len();
        x.with_scratch_input_local_output(|x_local, y_local| {
            #[cfg(all(feature = "mpi", feature = "backend-faer", not(feature = "complex")))]
            if self.local_apply_mode.is_replicated_global()
                && self.native_plan.is_none()
                && self.n_local != x_global_len
            {
                let x_global = assemble_global_input(&self.comm, x_local, x_global_len)?;
                let mut y_global = vec![0.0; x_global_len];
                self.local_pc.apply_local(&x_global, &mut y_global)?;
                let start = self.row_offset;
                let end = start + self.n_local;
                y_local.copy_from_slice(&y_global[start..end]);
                return Ok::<(), KError>(());
            }
            self.local_pc.apply_local(x_local, y_local)?;
            #[cfg(all(feature = "backend-faer", not(feature = "complex")))]
            if let Some(plan) = &self.native_plan {
                plan.apply_remote_correction(x_local, y_local);
            }
            Ok::<(), KError>(())
        })?;
        Ok(())
    }
}

#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
pub struct BlockJacobiObjPc {
    comm: UniverseComm,
    local_pc: Box<dyn ObjPreconditioner>,
    row_offset: usize,
    n_local: usize,
    local_apply_mode: DistLocalApplyMode,
    native_plan: Option<NativeCouplingPlan>,
}

#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
impl BlockJacobiObjPc {
    fn new(
        comm: UniverseComm,
        local_pc: Box<dyn ObjPreconditioner>,
        row_offset: usize,
        n_local: usize,
        local_apply_mode: DistLocalApplyMode,
        native_plan: Option<NativeCouplingPlan>,
    ) -> Self {
        Self {
            comm,
            local_pc,
            row_offset,
            n_local,
            local_apply_mode,
            native_plan,
        }
    }
}

#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
impl DistributedPreconditioner for BlockJacobiObjPc {
    type Scalar = f64;

    fn apply_global(&self, side: PcSide, x: &mut DistVecS<'_, f64>) -> Result<(), KError> {
        let _ = &self.comm;
        debug_assert_eq!(x.row_offset(), self.row_offset);
        debug_assert_eq!(x.local_len(), self.n_local);
        if self.n_local == 0 {
            return Ok(());
        }
        let x_global_len = x.global_len();
        x.with_scratch_input_local_output(|x_local, y_local| {
            #[cfg(feature = "mpi")]
            if self.local_apply_mode.is_replicated_global()
                && self.native_plan.is_none()
                && self.n_local != x_global_len
            {
                let x_global = assemble_global_input(&self.comm, x_local, x_global_len)?;
                let mut y_global = vec![0.0; x_global_len];
                self.local_pc.apply(side, &x_global, &mut y_global)?;
                let start = self.row_offset;
                let end = start + self.n_local;
                y_local.copy_from_slice(&y_global[start..end]);
                return Ok::<(), KError>(());
            }
            self.local_pc.apply(side, x_local, y_local)?;
            if let Some(plan) = &self.native_plan {
                plan.apply_remote_correction(x_local, y_local);
            }
            Ok::<(), KError>(())
        })?;
        Ok(())
    }
}

#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
pub fn build_block_jacobi_ilu_pc(
    dist_op: &DistCsrOp,
    config: &IluConfig,
    local_apply_mode: DistLocalApplyMode,
    supports_native: bool,
) -> Result<BlockJacobiLocalPc<Ilu>, KError> {
    let mut ilu = Ilu::new_with_config(config.clone())?;
    let local = dist_op.local_block_dense();
    LegacyPreconditioner::setup(&mut ilu, &local)?;
    Ok(BlockJacobiLocalPc::new(
        dist_op.comm(),
        ilu,
        dist_op.local_row_offset(),
        local_apply_mode,
        maybe_native_plan(dist_op, local_apply_mode, supports_native, "ilu")?,
    ))
}

#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
pub fn build_block_jacobi_ilut_pc(
    dist_op: &DistCsrOp,
    fill: usize,
    drop_tol: f64,
    conditioning: ConditioningOptions,
    local_apply_mode: DistLocalApplyMode,
    supports_native: bool,
) -> Result<BlockJacobiLocalPc<RowFilterPreconditioner>, KError> {
    let mut pc = RowFilterPreconditioner::new(fill, S::from_real(drop_tol));
    pc.set_conditioning(conditioning);
    let local = dist_op.local_block_dense();
    LegacyPreconditioner::setup(&mut pc, &local)?;
    Ok(BlockJacobiLocalPc::new(
        dist_op.comm(),
        pc,
        dist_op.local_row_offset(),
        local_apply_mode,
        maybe_native_plan(dist_op, local_apply_mode, supports_native, "ilut")?,
    ))
}

#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
pub fn build_block_jacobi_ilutp_pc(
    dist_op: &DistCsrOp,
    max_fill: usize,
    drop_tol: f64,
    perm_tol: f64,
    conditioning: ConditioningOptions,
    local_apply_mode: DistLocalApplyMode,
    supports_native: bool,
) -> Result<BlockJacobiLocalPc<Ilutp>, KError> {
    let mut pc = Ilutp::with_params(max_fill, drop_tol, perm_tol);
    pc.set_conditioning(conditioning);
    let local = dist_op.local_block_dense();
    LegacyPreconditioner::setup(&mut pc, &local)?;
    Ok(BlockJacobiLocalPc::new(
        dist_op.comm(),
        pc,
        dist_op.local_row_offset(),
        local_apply_mode,
        maybe_native_plan(dist_op, local_apply_mode, supports_native, "ilutp")?,
    ))
}

#[cfg(all(feature = "backend-faer", not(feature = "complex")))]
pub fn build_block_jacobi_pc(
    dist_op: &DistCsrOp,
    opts: &MpiPcOptions,
) -> Result<Option<Box<dyn DistributedPreconditioner<Scalar = f64>>>, KError> {
    match opts.global_pc {
        GlobalPcKind::None => Ok(None),
        GlobalPcKind::BlockJacobi => {
            let local_caps = opts.local_pc.build_capabilities();
            let wrapper: Box<dyn DistributedPreconditioner<Scalar = f64>> = match opts.local_pc {
                LocalPcKind::Ilu => Box::new(build_block_jacobi_ilu_pc(
                    dist_op,
                    &opts.ilu_config,
                    opts.local_apply_mode,
                    local_caps.native_local_apply,
                )?),
                LocalPcKind::Ilut => Box::new(build_block_jacobi_ilut_pc(
                    dist_op,
                    opts.ilut_fill,
                    opts.ilut_drop_tol,
                    opts.conditioning.clone(),
                    opts.local_apply_mode,
                    local_caps.native_local_apply,
                )?),
                LocalPcKind::Ilutp => Box::new(build_block_jacobi_ilutp_pc(
                    dist_op,
                    opts.ilutp_max_fill,
                    opts.ilutp_drop_tol,
                    opts.ilutp_perm_tol,
                    opts.conditioning.clone(),
                    opts.local_apply_mode,
                    local_caps.native_local_apply,
                )?),
                LocalPcKind::Sor => Box::new(build_obj_block_pc(
                    dist_op,
                    SorPc::new(1.0, 1, MatSorType::SYMMETRIC_SWEEP, 0.0),
                    opts.local_apply_mode,
                    local_caps.native_local_apply,
                    "sor",
                )?),
                LocalPcKind::Chebyshev => Box::new(build_obj_block_pc(
                    dist_op,
                    ChebyshevPc::new(2, 1e-2, 1.0),
                    opts.local_apply_mode,
                    local_caps.native_local_apply,
                    "chebyshev",
                )?),
                LocalPcKind::Jacobi => {
                    let mut jacobi = Jacobi::new();
                    let local_square = dist_op.local_square_block();
                    let local = Arc::new(local_square.as_csr().clone());
                    let op = CsrOp::new(local);
                    ObjPreconditioner::setup(&mut jacobi, &op)?;
                    Box::new(BlockJacobiLocalPc::new(
                        dist_op.comm(),
                        jacobi,
                        dist_op.local_row_offset(),
                        opts.local_apply_mode,
                        maybe_native_plan(
                            dist_op,
                            opts.local_apply_mode,
                            local_caps.native_local_apply,
                            "jacobi",
                        )?,
                    ))
                }
                LocalPcKind::Fsai => Box::new(build_obj_block_pc(
                    dist_op,
                    FsaiCsr::new_with_params(ApproxInvParams {
                        kind: ApproxInvKind::FSAI,
                        ..ApproxInvParams::default()
                    }),
                    opts.local_apply_mode,
                    local_caps.native_local_apply,
                    "fsai",
                )?),
                LocalPcKind::Spai => Box::new(build_obj_block_pc(
                    dist_op,
                    SpaiCsr::new_with_params(ApproxInvParams {
                        kind: ApproxInvKind::SPAI,
                        ..ApproxInvParams::default()
                    }),
                    opts.local_apply_mode,
                    local_caps.native_local_apply,
                    "spai",
                )?),
            };
            Ok(Some(wrapper))
        }
        GlobalPcKind::Asm | GlobalPcKind::Ras => Err(KError::NotImplemented(
            "block-Jacobi builder does not apply to ASM/RAS distributed preconditioners".into(),
        )),
    }
}

// Builder helpers and distributed wrappers are defined below.