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
#![cfg(feature = "backend-faer")]

//! Incomplete LU factorization with threshold dropping plus partial pivoting (ILUTP).
//!
//! ILUTP is typically used for nonsymmetric, possibly indefinite problems (e.g., Navier–Stokes)
//! where simple ILU0/ILUT may fail due to unstable pivots. This implementation combines ILUT-style
//! dropping with partial pivoting in `U`.
//! It uses:
//! - a dense working matrix during factorization,
//! - explicit row permutations recorded in `row_perm`,
//! - threshold-based dropping controlled by `drop_tol` and `max_fill`,
//! - pivot tolerance `perm_tol` to steer stability.
//! The factors remain real (`f64`), but complex solvers reuse them via the `KPreconditioner`
//! bridge (`BridgeScratch`).
//!
//! # Real vs complex
//! Factorization is always performed in real arithmetic (`f64`), and complex Krylov solvers depend
//! on the [`KPreconditioner`] bridge that copies into real scratch buffers, applies ILUTP, and
//! copies back.
//!
//! For non-pivoting ILUT on `faer::Mat<f64>`, prefer [`Ilu`] with [`IluType::ILUT`].
//!
//! The algorithm follows Saad's *Iterative Methods for Sparse Linear Systems* with stability
//! safeguards tailored for pragmatic use.
//! See `examples/convection_diffusion_ilutp.rs` for a convection–diffusion demonstration.

#[cfg(feature = "complex")]
use crate::algebra::bridge::{BridgeScratch, copy_real_into_scalar, copy_scalar_to_real_in};
#[allow(unused_imports)]
use crate::algebra::prelude::*;
use crate::error::KError;
#[cfg(feature = "complex")]
use crate::ops::kpc::KPreconditioner;
use crate::preconditioner::{LocalPreconditioner, legacy::Preconditioner};
use crate::preconditioner::ilu_csr::{ReorderingKind, ReorderingOptions};
use crate::utils::permutation::{Permutation, amd_from_adj, rcm_from_adj};
use crate::utils::conditioning::{apply_dense_transforms, ConditioningOptions};
use crate::utils::metrics::{Counters, SolveTimer};
use faer::Mat;
use std::cmp::Ordering;
use std::sync::Mutex;
use std::time::Instant;

/// ILUTP preconditioner with threshold control and partial pivoting.
///
/// This preconditioner is particularly effective for nonsymmetric, indefinite
/// matrices arising from discretized Navier-Stokes equations.
/// It implements [`LocalPreconditioner`] and is designed to stay on the local rank
/// so that MPI routines can wrap it without additional communication.
#[derive(Debug)]
pub struct Ilutp {
    /// Lower triangular factor
    l_factor: Mat<f64>,
    /// Upper triangular factor
    u_factor: Mat<f64>,
    /// Row permutation vector
    row_perm: Vec<usize>,
    /// Maximum fill-in per row
    max_fill: usize,
    /// Drop tolerance for small elements
    drop_tol: f64,
    /// Pivot tolerance (threshold for pivoting)
    perm_tol: f64,
    workspace: IlutpWorkspace,
    /// Time spent in the most recent setup
    setup_time: f64,
    /// Solve timing counters
    solve_ctrs: Counters,
    /// Optional conditioning transforms applied before factorization.
    conditioning: ConditioningOptions,
    /// Reordering configuration for local factorization.
    reordering: ReorderingOptions,
    /// Permutation applied before factorization.
    perm: Permutation,
}

#[derive(Debug, Clone)]
pub struct IlutpStats {
    pub setup_time: f64,
    pub solve_time: f64,
    pub solve_count: usize,
}

#[derive(Debug)]
struct IlutpWorkspace {
    buf: Mutex<Vec<f64>>,
    size: usize,
}

impl IlutpWorkspace {
    fn new() -> Self {
        Self {
            buf: Mutex::new(Vec::new()),
            size: 0,
        }
    }

    fn ensure_size(&mut self, n: usize) {
        if n > self.size {
            let mut guard = self.buf.lock().unwrap();
            guard.resize(n, 0.0);
            self.size = n;
        }
    }

    #[inline]
    fn borrow_buf(&self, n: usize) -> std::sync::MutexGuard<'_, Vec<f64>> {
        debug_assert!(self.size >= n, "workspace not sized via setup()");
        self.buf.lock().unwrap()
    }
}

fn apply_reordering(
    matrix: &Mat<f64>,
    reordering: ReorderingOptions,
) -> Result<(Mat<f64>, Permutation), KError> {
    let n = matrix.nrows();
    if n != matrix.ncols() {
        return Err(KError::InvalidInput(
            "Matrix must be square for ILUTP".to_string(),
        ));
    }
    if !reordering.symmetric {
        return Err(KError::Unsupported(
            "Ilutp only supports symmetric reordering",
        ));
    }

    let perm = match reordering.kind {
        ReorderingKind::None => Permutation::identity(n),
        ReorderingKind::Rcm => {
            let mut adj = dense_adj_from_matrix(matrix);
            rcm_from_adj(&mut adj)
        }
        ReorderingKind::Amd => {
            let mut adj = dense_adj_from_matrix(matrix);
            amd_from_adj(&mut adj)
        }
    };

    if matches!(reordering.kind, ReorderingKind::None) {
        Ok((matrix.clone(), perm))
    } else {
        Ok((permute_dense_symmetric(matrix, &perm), perm))
    }
}

fn dense_adj_from_matrix(matrix: &Mat<f64>) -> Vec<Vec<usize>> {
    let n = matrix.nrows();
    let mut adj = vec![Vec::new(); n];
    for i in 0..n {
        for j in (i + 1)..n {
            if matrix[(i, j)] != 0.0 || matrix[(j, i)] != 0.0 {
                adj[i].push(j);
                adj[j].push(i);
            }
        }
    }
    adj
}

fn permute_dense_symmetric(matrix: &Mat<f64>, perm: &Permutation) -> Mat<f64> {
    let n = matrix.nrows();
    let mut result = Mat::zeros(n, n);
    for i in 0..n {
        let old_i = perm.p[i];
        for j in 0..n {
            let old_j = perm.p[j];
            result[(i, j)] = matrix[(old_i, old_j)];
        }
    }
    result
}

impl Ilutp {
    /// Create a new ILUTP preconditioner with default parameters.
    pub fn new() -> Self {
        Self {
            l_factor: Mat::zeros(0, 0),
            u_factor: Mat::zeros(0, 0),
            row_perm: Vec::new(),
            max_fill: 10,
            drop_tol: 1e-4,
            perm_tol: 0.1,
            workspace: IlutpWorkspace::new(),
            setup_time: 0.0,
            solve_ctrs: Counters::new(),
            conditioning: ConditioningOptions::default(),
            reordering: ReorderingOptions::default(),
            perm: Permutation::identity(0),
        }
    }

    /// Create a new ILUTP preconditioner with custom parameters.
    pub fn with_params(max_fill: usize, drop_tol: f64, perm_tol: f64) -> Self {
        Self {
            l_factor: Mat::zeros(0, 0),
            u_factor: Mat::zeros(0, 0),
            row_perm: Vec::new(),
            max_fill,
            drop_tol,
            perm_tol,
            workspace: IlutpWorkspace::new(),
            setup_time: 0.0,
            solve_ctrs: Counters::new(),
            conditioning: ConditioningOptions::default(),
            reordering: ReorderingOptions::default(),
            perm: Permutation::identity(0),
        }
    }

    pub fn set_conditioning(&mut self, conditioning: ConditioningOptions) {
        self.conditioning = conditioning;
    }

    pub fn set_reordering(&mut self, reordering: ReorderingOptions) {
        self.reordering = reordering;
    }

    /// Set the maximum fill-in per row.
    pub fn set_max_fill(&mut self, max_fill: usize) {
        self.max_fill = max_fill;
    }

    /// Set the drop tolerance.
    pub fn set_drop_tolerance(&mut self, drop_tol: f64) {
        self.drop_tol = drop_tol;
    }

    /// Set the pivot tolerance.
    pub fn set_pivot_tolerance(&mut self, perm_tol: f64) {
        self.perm_tol = perm_tol;
    }

    /// Compute ILUTP factorization with simplified algorithm.
    fn compute_factorization(&mut self, matrix: &Mat<f64>) -> Result<(), KError> {
        let n = matrix.nrows();
        if n != matrix.ncols() {
            return Err(KError::InvalidInput(
                "Matrix must be square for ILUTP".to_string(),
            ));
        }

        // Initialize working matrix as a copy
        let mut work_matrix = matrix.clone();

        // Initialize permutation as identity
        self.row_perm = (0..n).collect();

        // Initialize L and U factors
        let mut l_factor = Mat::zeros(n, n);
        let mut u_factor = Mat::zeros(n, n);

        // Set diagonal of L to 1
        for i in 0..n {
            l_factor[(i, i)] = 1.0;
        }

        // Simplified ILUTP factorization
        for k in 0..n {
            // Find pivot row (simplified pivoting with zero avoidance)
            let mut pivot_row = k;
            let mut max_val = work_matrix[(k, k)].abs();

            // Look for a better pivot if current is too small
            if max_val < 1e-12 {
                for i in (k + 1)..n {
                    let val = work_matrix[(i, k)].abs();
                    if val > max_val {
                        max_val = val;
                        pivot_row = i;
                    }
                }
            } else {
                // Standard partial pivoting
                for i in (k + 1)..n {
                    let val = work_matrix[(i, k)].abs();
                    if val > max_val && val > self.perm_tol * max_val {
                        max_val = val;
                        pivot_row = i;
                    }
                }
            }

            // Swap rows if needed
            if pivot_row != k {
                self.row_perm.swap(k, pivot_row);
                for j in 0..n {
                    let temp = work_matrix[(k, j)];
                    work_matrix[(k, j)] = work_matrix[(pivot_row, j)];
                    work_matrix[(pivot_row, j)] = temp;
                }
            }

            let pivot = work_matrix[(k, k)];
            if pivot.abs() < 1e-12 {
                // TODO: consider reusing `preconditioner::pivot` handling (e.g., `PivotPolicy`)
                // for consistent stabilization instead of this ad-hoc regularization.
                // Handle near-zero pivot by regularization
                work_matrix[(k, k)] = if pivot >= 0.0 { 1e-12 } else { -1e-12 };
                eprintln!(
                    "Warning: Near-zero pivot at row {} regularized from {} to {}",
                    k,
                    pivot,
                    work_matrix[(k, k)]
                );
            }

            // Store U factor
            for j in k..n {
                u_factor[(k, j)] = work_matrix[(k, j)];
            }

            // Elimination with threshold dropping
            for i in (k + 1)..n {
                if work_matrix[(i, k)].abs() > self.drop_tol {
                    let multiplier = work_matrix[(i, k)] / pivot;
                    l_factor[(i, k)] = multiplier;

                    // Collect row elements for fill-in control
                    let mut row_elements = Vec::new();
                    for j in (k + 1)..n {
                        let new_val = work_matrix[(i, j)] - multiplier * work_matrix[(k, j)];
                        if new_val.abs() > self.drop_tol {
                            row_elements.push((j, new_val));
                        }
                    }

                    // Apply fill-in control
                    if row_elements.len() > self.max_fill {
                        row_elements.sort_by(|a, b| {
                            b.1.abs().partial_cmp(&a.1.abs()).unwrap_or(Ordering::Equal)
                        });
                        row_elements.truncate(self.max_fill);
                    }

                    // Clear row and set selected elements
                    for j in (k + 1)..n {
                        work_matrix[(i, j)] = 0.0;
                    }
                    for (j, val) in row_elements {
                        work_matrix[(i, j)] = val;
                    }
                }
            }
        }

        self.l_factor = l_factor;
        self.u_factor = u_factor;
        self.workspace.ensure_size(n);
        Ok(())
    }

    /// Forward substitution: solve Ly = Pb
    fn forward_solve(&self, b: &[f64], y: &mut [f64]) -> Result<(), KError> {
        let n = self.l_factor.nrows();

        // Apply row permutation
        for i in 0..n {
            y[i] = b[self.row_perm[i]];
        }

        // Forward substitution: Ly = Pb
        for i in 0..n {
            for j in 0..i {
                y[i] -= self.l_factor[(i, j)] * y[j];
            }
            // L has unit diagonal, so no division needed
        }

        Ok(())
    }

    /// Backward substitution: solve Ux = y
    fn backward_solve(&self, y: &[f64], x: &mut [f64]) -> Result<(), KError> {
        let n = self.u_factor.nrows();
        x.copy_from_slice(y);

        // Backward substitution: Ux = y
        for i in (0..n).rev() {
            for j in (i + 1)..n {
                x[i] -= self.u_factor[(i, j)] * x[j];
            }
            let diag = self.u_factor[(i, i)];
            if diag.abs() < 1e-14 {
                return Err(KError::ZeroPivot(i));
            }
            x[i] /= diag;
        }

        Ok(())
    }

    fn apply_slice(&self, input: &[f64], output: &mut [f64]) -> Result<(), KError> {
        let n = input.len();
        let expected = self.l_factor.nrows();
        if output.len() != n || expected != n {
            return Err(KError::InvalidInput(format!(
                "Ilutp::apply dimension mismatch: input.len()={}, output.len()={}, n={}",
                n,
                output.len(),
                expected
            )));
        }

        let _timer = SolveTimer::start(&self.solve_ctrs);
        if matches!(self.reordering.kind, ReorderingKind::None) {
            let mut temp_guard = self.workspace.borrow_buf(n);
            let temp = &mut temp_guard[..n];
            self.forward_solve(input, temp)?;
            self.backward_solve(temp, output)?;
        } else {
            let mut perm_input = vec![0.0; n];
            self.perm.apply_vec(input, &mut perm_input);
            let mut temp_guard = self.workspace.borrow_buf(n);
            let temp = &mut temp_guard[..n];
            self.forward_solve(&perm_input, temp)?;
            let mut perm_output = vec![0.0; n];
            self.backward_solve(temp, &mut perm_output)?;
            self.perm.apply_vec_t(&perm_output, output);
        }
        Ok(())
    }
}

impl Default for Ilutp {
    fn default() -> Self {
        Self::new()
    }
}

impl Preconditioner<Mat<f64>, Vec<f64>> for Ilutp {
    /// Setup the ILUTP preconditioner by computing the factorization.
    fn setup(&mut self, matrix: &Mat<f64>) -> Result<(), KError> {
        let start = Instant::now();
        self.solve_ctrs = Counters::new();
        let mut conditioned = None;
        let matrix = if self.conditioning.is_active() {
            let mut local = matrix.clone();
            apply_dense_transforms("ILUTP", &mut local, &self.conditioning)?;
            conditioned = Some(local);
            conditioned.as_ref().unwrap()
        } else {
            matrix
        };
        let (matrix, perm) = apply_reordering(matrix, self.reordering.clone())?;
        self.perm = perm;
        self.compute_factorization(&matrix)?;
        self.setup_time = start.elapsed().as_secs_f64();
        Ok(())
    }

    /// Apply the ILUTP preconditioner: solve M⁻¹x = b where M ≈ A.
    fn apply(
        &self,
        _side: crate::preconditioner::PcSide,
        input: &Vec<f64>,
        output: &mut Vec<f64>,
    ) -> Result<(), KError> {
        self.apply_slice(input, output)
    }
}

impl LocalPreconditioner<f64> for Ilutp {
    fn dims(&self) -> (usize, usize) {
        let n = self.l_factor.nrows();
        (n, n)
    }

    fn apply_local(&self, x: &[f64], y: &mut [f64]) -> Result<(), KError> {
        let (n, _) = LocalPreconditioner::<f64>::dims(self);
        debug_assert_eq!(x.len(), n);
        debug_assert_eq!(y.len(), n);
        self.apply_slice(x, y)
    }
}

impl Ilutp {
    pub fn get_stats(&self) -> IlutpStats {
        let (total_ns, count, _) = self.solve_ctrs.snapshot();
        IlutpStats {
            setup_time: self.setup_time,
            solve_time: if count == 0 {
                0.0
            } else {
                total_ns as f64 / count as f64 / 1e9
            },
            solve_count: count as usize,
        }
    }
}

#[cfg(test)]
impl Ilutp {
    pub fn test_max_fill(&self) -> usize {
        self.max_fill
    }

    pub fn test_drop_tolerance(&self) -> f64 {
        self.drop_tol
    }

    pub fn test_perm_tolerance(&self) -> f64 {
        self.perm_tol
    }
}

#[cfg(feature = "complex")]
impl KPreconditioner for Ilutp {
    type Scalar = S;

    #[inline]
    fn dims(&self) -> (usize, usize) {
        let n = self.l_factor.nrows();
        (n, n)
    }

    fn apply_s(
        &self,
        _side: crate::preconditioner::PcSide,
        x: &[S],
        y: &mut [S],
        scratch: &mut BridgeScratch,
    ) -> Result<(), KError> {
        let (rows, cols) = LocalPreconditioner::<f64>::dims(self);
        let n = x.len();
        if x.len() != y.len() || rows != n || cols != n {
            return Err(KError::InvalidInput(format!(
                "Ilutp::apply_s dimension mismatch: expected {}x{}, got x.len()={} y.len()={}",
                rows,
                cols,
                x.len(),
                y.len()
            )));
        }
        scratch.with_pair(n, |xr, yr| {
            copy_scalar_to_real_in(x, xr);
            self.apply_slice(xr, yr)?;
            copy_real_into_scalar(yr, y);
            Ok(())
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use faer::Mat;

    #[test]
    fn test_ilutp_basic() {
        let mut ilutp = Ilutp::new();

        // Test matrix: simple 3x3 diagonally dominant
        let mut matrix = Mat::zeros(3, 3);
        matrix[(0, 0)] = 4.0;
        matrix[(0, 1)] = -1.0;
        matrix[(1, 0)] = -1.0;
        matrix[(1, 1)] = 4.0;
        matrix[(1, 2)] = -1.0;
        matrix[(2, 1)] = -1.0;
        matrix[(2, 2)] = 4.0;

        // Setup should succeed
        assert!(ilutp.setup(&matrix).is_ok());

        // Test application
        let input = vec![1.0, 2.0, 3.0];
        let mut output = vec![0.0; 3];
        assert!(
            ilutp
                .apply(crate::preconditioner::PcSide::Left, &input, &mut output)
                .is_ok()
        );
    }

    #[test]
    fn test_ilutp_with_custom_params() {
        let mut ilutp = Ilutp::with_params(5, 1e-6, 0.01);

        // Test parameter setting
        ilutp.set_max_fill(15);
        ilutp.set_drop_tolerance(1e-8);
        ilutp.set_pivot_tolerance(0.1);

        assert_eq!(ilutp.max_fill, 15);
        assert_eq!(ilutp.drop_tol, 1e-8);
        assert_eq!(ilutp.perm_tol, 0.1);
    }

    #[cfg(feature = "complex")]
    #[test]
    fn apply_s_matches_real_path() {
        use crate::algebra::bridge::BridgeScratch;
        use crate::algebra::prelude::*;
        use crate::ops::kpc::KPreconditioner;

        let mut ilutp = Ilutp::with_params(4, 1e-6, 0.05);

        let mut matrix = Mat::zeros(2, 2);
        matrix[(0, 0)] = 5.0;
        matrix[(0, 1)] = -1.0;
        matrix[(1, 0)] = -2.0;
        matrix[(1, 1)] = 6.0;
        ilutp.setup(&matrix).unwrap();

        let rhs_real = vec![3.0f64, -1.0];
        let mut out_real = vec![0.0; rhs_real.len()];
        ilutp
            .apply(
                crate::preconditioner::PcSide::Left,
                &rhs_real,
                &mut out_real,
            )
            .expect("ilutp real apply");

        let rhs_s: Vec<S> = rhs_real.iter().copied().map(S::from_real).collect();
        let mut out_s = vec![S::zero(); rhs_s.len()];
        let mut scratch = BridgeScratch::default();
        ilutp
            .apply_s(
                crate::preconditioner::PcSide::Left,
                &rhs_s,
                &mut out_s,
                &mut scratch,
            )
            .expect("ilutp apply_s");

        for (ys, yr) in out_s.iter().zip(out_real.iter()) {
            assert!((ys.real() - yr).abs() < 1e-10);
        }
    }
}