onemkl 0.5.2

Safe, idiomatic Rust wrapper over Intel oneAPI Math Kernel Library (oneMKL).
Documentation
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
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Nonlinear optimization — trust-region nonlinear least-squares
//! (TRNLS) and bound-constrained variant (TRNLSPBC).
//!
//! Solves problems of the form
//!
//! ```text
//! minimize  ½ ‖F(x)‖²    subject to  l ≤ x ≤ u  (optional)
//! ```
//!
//! where `F: ℝⁿ → ℝᵐ` is supplied by the caller along with its
//! Jacobian. The wrappers run oneMKL's reverse-communication loop
//! internally, calling the user's closures when MKL requests a
//! residual or Jacobian evaluation.
//!
//! Currently double-precision only.

use core::ffi::{c_int, c_void};
use core::ptr;
use core::slice;
use std::any::Any;
use std::panic::{AssertUnwindSafe, catch_unwind, resume_unwind};
use std::sync::Mutex;

use onemkl_sys as sys;

use crate::error::{Error, Result};

/// `m * n` with overflow reported as an error (a wrapped product would let an
/// undersized buffer pass the length check on 32-bit targets).
fn checked_len(m: usize, n: usize) -> Result<usize> {
    m.checked_mul(n).ok_or(Error::DimensionOverflow)
}

/// Tunable options for the trust-region NLLS solver.
#[derive(Debug, Clone, Copy)]
pub struct TrnlsOptions {
    /// Maximum number of outer iterations.
    pub max_iterations: i32,
    /// Maximum number of trial step refinements per iteration.
    pub max_trial_steps: i32,
    /// Initial trust region size.
    pub initial_trust_region: f64,
    /// Stopping tolerances `eps[0..=5]`. Each defaults to a reasonable
    /// value if left at `0.0`. See the oneMKL reference for the
    /// individual meanings.
    pub eps: [f64; 6],
}

impl Default for TrnlsOptions {
    fn default() -> Self {
        Self {
            max_iterations: 1000,
            max_trial_steps: 100,
            initial_trust_region: 100.0,
            // 1e-5 is the value used in oneMKL's bundled examples;
            // tightening below ~1e-7 typically trips TR_INVALID_OPTION.
            eps: [1.0e-5; 6],
        }
    }
}

/// Outcome of a successful TRNLS solve.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TrnlsResult {
    /// Number of outer iterations performed.
    pub iterations: i32,
    /// Stopping criterion code (`st_cr` from `dtrnlsp_get`).
    /// Mirrors oneMKL's six-valued enumeration.
    pub stopping_criterion: i32,
    /// `‖F(x₀)‖₂` — Euclidean norm of the residual at the initial
    /// guess.
    pub initial_residual_norm: f64,
    /// `‖F(x*)‖₂` — Euclidean norm of the residual at the returned
    /// solution.
    pub final_residual_norm: f64,
}

/// Unconstrained trust-region NLLS solver.
///
/// `x` (length `n`) is the initial guess on entry and the optimum on
/// return. `m` is the number of residual equations.
/// `residual(x, f)` writes `F(x)` into `f` (length `m`).
/// `jacobian(x, j)` writes the Jacobian into `j` (length `m * n`,
/// column-major).
pub fn solve_trnls<R, J>(
    n: usize,
    m: usize,
    x: &mut [f64],
    opts: TrnlsOptions,
    mut residual: R,
    mut jacobian: J,
) -> Result<TrnlsResult>
where
    R: FnMut(&[f64], &mut [f64]),
    J: FnMut(&[f64], &mut [f64]),
{
    if x.len() != n {
        return Err(Error::InvalidArgument("x must have length n"));
    }
    let n_i: c_int = n.try_into().map_err(|_| Error::DimensionOverflow)?;
    let m_i: c_int = m.try_into().map_err(|_| Error::DimensionOverflow)?;

    let mut handle: sys::_TRNSP_HANDLE_t = ptr::null_mut();
    let status = unsafe {
        sys::dtrnlsp_init(
            &mut handle,
            &n_i,
            &m_i,
            x.as_mut_ptr(),
            opts.eps.as_ptr(),
            &opts.max_iterations,
            &opts.max_trial_steps,
            &opts.initial_trust_region,
        )
    };
    if status != sys::TR_SUCCESS as c_int {
        return Err(Error::LapackComputationFailure { info: status });
    }

    let mut fvec = vec![0.0_f64; m];
    let mut fjac = vec![0.0_f64; checked_len(m, n)?];
    let mut rci: c_int = 0;

    let result = (|| -> Result<TrnlsResult> {
        loop {
            let status = unsafe {
                sys::dtrnlsp_solve(&mut handle, fvec.as_mut_ptr(), fjac.as_mut_ptr(), &mut rci)
            };
            if status != sys::TR_SUCCESS as c_int {
                return Err(Error::LapackComputationFailure { info: status });
            }
            match rci {
                -6..=-1 => break,
                0 => break,
                1 => residual(x, &mut fvec),
                2 => jacobian(x, &mut fjac),
                other => {
                    return Err(Error::LapackComputationFailure { info: other });
                }
            }
        }

        let mut iters: c_int = 0;
        let mut st_cr: c_int = 0;
        let mut r1: f64 = 0.0;
        let mut r2: f64 = 0.0;
        let status =
            unsafe { sys::dtrnlsp_get(&mut handle, &mut iters, &mut st_cr, &mut r1, &mut r2) };
        if status != sys::TR_SUCCESS as c_int {
            return Err(Error::LapackComputationFailure { info: status });
        }
        Ok(TrnlsResult {
            iterations: iters,
            stopping_criterion: st_cr,
            initial_residual_norm: r1,
            final_residual_norm: r2,
        })
    })();

    unsafe {
        let _ = sys::dtrnlsp_delete(&mut handle);
    }
    result
}

/// Element-wise bounds `lower[i] ≤ x[i] ≤ upper[i]` for
/// [`solve_trnls_bounded`]; both slices must have length `n`.
#[derive(Debug, Clone, Copy)]
pub struct TrnlsBounds<'a> {
    /// Lower bounds, length `n`.
    pub lower: &'a [f64],
    /// Upper bounds, length `n`.
    pub upper: &'a [f64],
}

/// Bound-constrained trust-region NLLS solver. Adds element-wise
/// lower / upper bound enforcement: `lower[i] ≤ x[i] ≤ upper[i]`.
pub fn solve_trnls_bounded<R, J>(
    n: usize,
    m: usize,
    x: &mut [f64],
    bounds: TrnlsBounds<'_>,
    opts: TrnlsOptions,
    mut residual: R,
    mut jacobian: J,
) -> Result<TrnlsResult>
where
    R: FnMut(&[f64], &mut [f64]),
    J: FnMut(&[f64], &mut [f64]),
{
    let TrnlsBounds { lower, upper } = bounds;
    if x.len() != n || lower.len() != n || upper.len() != n {
        return Err(Error::InvalidArgument(
            "x, lower, and upper must each have length n",
        ));
    }
    let n_i: c_int = n.try_into().map_err(|_| Error::DimensionOverflow)?;
    let m_i: c_int = m.try_into().map_err(|_| Error::DimensionOverflow)?;

    let mut handle: sys::_TRNSPBC_HANDLE_t = ptr::null_mut();
    let status = unsafe {
        sys::dtrnlspbc_init(
            &mut handle,
            &n_i,
            &m_i,
            x.as_mut_ptr(),
            lower.as_ptr(),
            upper.as_ptr(),
            opts.eps.as_ptr(),
            &opts.max_iterations,
            &opts.max_trial_steps,
            &opts.initial_trust_region,
        )
    };
    if status != sys::TR_SUCCESS as c_int {
        return Err(Error::LapackComputationFailure { info: status });
    }

    let mut fvec = vec![0.0_f64; m];
    let mut fjac = vec![0.0_f64; checked_len(m, n)?];
    let mut rci: c_int = 0;

    let result = (|| -> Result<TrnlsResult> {
        loop {
            let status = unsafe {
                sys::dtrnlspbc_solve(&mut handle, fvec.as_mut_ptr(), fjac.as_mut_ptr(), &mut rci)
            };
            if status != sys::TR_SUCCESS as c_int {
                return Err(Error::LapackComputationFailure { info: status });
            }
            match rci {
                -6..=-1 => break,
                0 => break,
                1 => residual(x, &mut fvec),
                2 => jacobian(x, &mut fjac),
                other => {
                    return Err(Error::LapackComputationFailure { info: other });
                }
            }
        }

        let mut iters: c_int = 0;
        let mut st_cr: c_int = 0;
        let mut r1: f64 = 0.0;
        let mut r2: f64 = 0.0;
        let status =
            unsafe { sys::dtrnlspbc_get(&mut handle, &mut iters, &mut st_cr, &mut r1, &mut r2) };
        if status != sys::TR_SUCCESS as c_int {
            return Err(Error::LapackComputationFailure { info: status });
        }
        Ok(TrnlsResult {
            iterations: iters,
            stopping_criterion: st_cr,
            initial_residual_norm: r1,
            final_residual_norm: r2,
        })
    })();

    unsafe {
        let _ = sys::dtrnlspbc_delete(&mut handle);
    }
    result
}

/// Numerical Jacobian via forward differences. `residual(x, f)` is
/// called repeatedly with perturbed `x`; the resulting `m × n`
/// (column-major) Jacobian is written into `jac`.
///
/// `step` is the perturbation step size; pass `1e-6` for a typical
/// double-precision balance between truncation and round-off error.
pub fn numerical_jacobian<R>(
    n: usize,
    m: usize,
    x: &[f64],
    step: f64,
    jac: &mut [f64],
    mut residual: R,
) -> Result<()>
where
    R: FnMut(&[f64], &mut [f64]),
{
    if x.len() != n {
        return Err(Error::InvalidArgument("x must have length n"));
    }
    if jac.len() < checked_len(m, n)? {
        return Err(Error::InvalidArgument("jac buffer is smaller than m * n"));
    }
    let n_i: c_int = n.try_into().map_err(|_| Error::DimensionOverflow)?;
    let m_i: c_int = m.try_into().map_err(|_| Error::DimensionOverflow)?;

    // MKL's RCI loop perturbs `x` in place and the closure reads it between
    // calls, so MKL must get an owned, mutable buffer (writing through a
    // pointer derived from the shared `&[f64]` would be undefined behaviour).
    let mut xv = x.to_vec();
    let mut handle = JacobiHandle(ptr::null_mut());
    let status = unsafe {
        sys::djacobi_init(
            &mut handle.0,
            &n_i,
            &m_i,
            xv.as_mut_ptr(),
            jac.as_mut_ptr(),
            &step,
        )
    };
    if status != sys::TR_SUCCESS as c_int {
        return Err(Error::LapackComputationFailure { info: status });
    }

    // Working buffers MKL will manage internally; we provide the
    // residual closure via the RCI loop.
    let mut f1 = vec![0.0_f64; m];
    let mut f2 = vec![0.0_f64; m];
    let mut rci: c_int = 0;

    // `handle` is an RAII guard: a panic in `residual` unwinds through here and
    // still frees the MKL handle.
    let result = (|| -> Result<()> {
        loop {
            let status = unsafe {
                sys::djacobi_solve(&mut handle.0, f1.as_mut_ptr(), f2.as_mut_ptr(), &mut rci)
            };
            if status != sys::TR_SUCCESS as c_int {
                return Err(Error::LapackComputationFailure { info: status });
            }
            match rci {
                0 => break,
                1 => {
                    // Need F(x) at the current sample — MKL has
                    // already adjusted x internally; we call the
                    // user's residual on whatever they passed.
                    residual(&xv, &mut f1);
                }
                2 => {
                    residual(&xv, &mut f2);
                }
                other if other < 0 => {
                    return Err(Error::LapackComputationFailure { info: other });
                }
                _ => {}
            }
        }
        Ok(())
    })();

    drop(handle);
    result
}

/// Owns an initialised `djacobi` RCI handle; `djacobi_delete` runs on drop,
/// including during unwinding.
struct JacobiHandle(sys::_JACOBIMATRIX_HANDLE_t);

impl Drop for JacobiHandle {
    fn drop(&mut self) {
        if !self.0.is_null() {
            unsafe {
                let _ = sys::djacobi_delete(&mut self.0);
            }
        }
    }
}

/// Function-pointer type for the direct-callback form of `djacobi`.
/// MKL invokes this once per Jacobian column, passing pointers to
/// dimensions, the perturbed `x`, and the output residual.
pub type DjacobiCallback = unsafe extern "C" fn(
    n: *mut core::ffi::c_int,
    m: *mut core::ffi::c_int,
    x: *mut f64,
    f: *mut f64,
);

/// Compute a numerical Jacobian using oneMKL's direct-callback form
/// (`djacobi`). Unlike [`numerical_jacobian`], which drives MKL's
/// reverse-communication interface from a Rust closure, this variant
/// accepts a raw `extern "C"` function pointer that MKL calls
/// internally — useful when the residual is implemented in C / C++ /
/// Fortran or already exposed as an FFI-compatible function.
///
/// `jac` must be at least `m * n` elements (column-major). `step` is
/// the perturbation size for forward-difference Jacobian estimation.
///
/// # Safety
///
/// `fcn` must remain valid for the duration of this call and must
/// not unwind across the FFI boundary.
pub unsafe fn djacobi_with_callback(
    n: usize,
    m: usize,
    x: &[f64],
    step: f64,
    jac: &mut [f64],
    fcn: DjacobiCallback,
) -> Result<()> {
    if x.len() != n {
        return Err(Error::InvalidArgument("x must have length n"));
    }
    if jac.len() < checked_len(m, n)? {
        return Err(Error::InvalidArgument("jac buffer is smaller than m * n"));
    }
    let n_i: c_int = n.try_into().map_err(|_| Error::DimensionOverflow)?;
    let m_i: c_int = m.try_into().map_err(|_| Error::DimensionOverflow)?;
    // MKL temporarily perturbs each element of `x` in place, so it gets an
    // owned buffer (writing through a pointer derived from the shared `&[f64]`
    // would be undefined behaviour).
    let mut xv = x.to_vec();
    let mut step_mut = step;
    let status = unsafe {
        sys::djacobi(
            Some(fcn),
            &n_i,
            &m_i,
            jac.as_mut_ptr(),
            xv.as_mut_ptr(),
            &mut step_mut,
        )
    };
    if status != sys::TR_SUCCESS as c_int {
        return Err(Error::LapackComputationFailure { info: status });
    }
    Ok(())
}

struct JacobixState<'a> {
    n: usize,
    m: usize,
    f: &'a mut dyn FnMut(&[f64], &mut [f64]),
    panic: Option<Box<dyn Any + Send>>,
}

unsafe extern "C" fn jacobix_trampoline(
    _m: *mut c_int,
    _n: *mut c_int,
    x: *mut f64,
    f: *mut f64,
    user: *mut c_void,
) {
    // SAFETY: `user` is the `Mutex<JacobixState>` that
    // `numerical_jacobian_central` passed to `djacobix` and keeps alive for the
    // whole call. Only a shared reference is formed; the mutex serialises
    // callbacks, so a threaded MKL layer cannot alias the `FnMut` state.
    let cell = unsafe { &*(user as *const Mutex<JacobixState<'_>>) };
    let mut guard = cell.lock().unwrap_or_else(|e| e.into_inner());
    let st = &mut *guard;
    if st.panic.is_some() {
        return;
    }
    // The dimensions come from our own state, not from MKL's (Fortran-order)
    // arguments. MKL passes `x` of length `n` and `f` of length `m`.
    let xs = unsafe { slice::from_raw_parts(x, st.n) };
    // `f` is an output buffer MKL may not have initialised: zero it through the
    // raw pointer before forming a `&mut [f64]` over it.
    unsafe { ptr::write_bytes(f, 0, st.m) };
    let fs = unsafe { slice::from_raw_parts_mut(f, st.m) };
    if let Err(p) = catch_unwind(AssertUnwindSafe(|| (st.f)(xs, fs))) {
        st.panic = Some(p);
    }
}

/// Numerical Jacobian of `residual: ℝⁿ → ℝᵐ` by central differences
/// (`djacobix`). `residual(x, f)` writes `F(x)` into `f` (length `m`) and
/// is called with perturbed copies of `x` (length `n`). The `m × n`
/// Jacobian is written into `jac` column-major (column `j` = ∂F/∂xⱼ);
/// measured in `tests/optim_jacobix.rs`.
///
/// `eps` is the requested precision of the Jacobian (`1e-6` is typical).
/// A panic in `residual` is caught inside the callback and re-raised here
/// once MKL has returned, never unwound through MKL. `residual` must be
/// `Send` and is serialised by a mutex, because a threaded oneMKL layer may
/// call back from worker threads.
pub fn numerical_jacobian_central<R>(
    n: usize,
    m: usize,
    x: &[f64],
    eps: f64,
    jac: &mut [f64],
    mut residual: R,
) -> Result<()>
where
    R: FnMut(&[f64], &mut [f64]) + Send,
{
    if x.len() != n {
        return Err(Error::InvalidArgument("x must have length n"));
    }
    if jac.len() < checked_len(m, n)? {
        return Err(Error::InvalidArgument("jac buffer is smaller than m * n"));
    }
    if !(eps.is_finite() && eps > 0.0) {
        return Err(Error::InvalidArgument("eps must be finite and positive"));
    }
    let n_i: c_int = n.try_into().map_err(|_| Error::DimensionOverflow)?;
    let m_i: c_int = m.try_into().map_err(|_| Error::DimensionOverflow)?;

    // MKL perturbs and restores `x` in place, so give it an owned copy.
    let mut xv = x.to_vec();
    let mut eps_mut = eps;
    let state = Mutex::new(JacobixState {
        n,
        m,
        f: &mut residual,
        panic: None,
    });
    let status = unsafe {
        sys::djacobix(
            Some(jacobix_trampoline),
            &n_i,
            &m_i,
            jac.as_mut_ptr(),
            xv.as_mut_ptr(),
            &mut eps_mut,
            &state as *const Mutex<JacobixState<'_>> as *mut c_void,
        )
    };
    let panic = state
        .into_inner()
        .unwrap_or_else(|e| e.into_inner())
        .panic
        .take();
    if let Some(p) = panic {
        resume_unwind(p);
    }
    if status != sys::TR_SUCCESS as c_int {
        return Err(Error::LapackComputationFailure { info: status });
    }
    Ok(())
}

/// Next action requested by a [`TrnlsSession`].
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum TrnlsStep {
    /// Evaluate `F(x)` at [`TrnlsSession::x`] into [`TrnlsSession::residual_mut`], then call `step` again.
    NeedResidual,
    /// Evaluate the Jacobian at [`TrnlsSession::x`] into [`TrnlsSession::jacobian_mut`]
    /// (column-major `m x n`), then call `step` again.
    NeedJacobian,
    /// The solver stopped; the optimum is in [`TrnlsSession::x`].
    Done(TrnlsResult),
}

/// Result of [`TrnlsSession::check`] (`dtrnlsp_check`): the raw six-entry
/// `info` array. All zero means the Jacobian, residual and tolerances are
/// valid; `info[0]` names the first invalid object and the others locate the
/// offending entries (see the oneMKL reference).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TrnlsCheck {
    /// Raw `info` array returned by `dtrnlsp_check`.
    pub info: [i32; 6],
}

impl TrnlsCheck {
    /// `true` when every `info` entry is zero.
    #[must_use]
    pub fn is_valid(&self) -> bool {
        self.info.iter().all(|&i| i == 0)
    }
}

/// Step-by-step driver of the unconstrained trust-region solver: the caller
/// owns the reverse-communication loop (unlike [`solve_trnls`], which runs it
/// with closures). The session owns `x`, the residual and the Jacobian
/// buffers, since MKL keeps pointers to `x` for the life of the handle.
pub struct TrnlsSession {
    /// Boxed so the handle's address survives moves of the session.
    handle: Box<sys::_TRNSP_HANDLE_t>,
    n: usize,
    m: usize,
    x: Vec<f64>,
    fvec: Vec<f64>,
    fjac: Vec<f64>,
    opts: Box<TrnlsOptions>,
    /// Reverse-communication state: MKL reads it back on the next call, so it must persist.
    rci: c_int,
    done: Option<TrnlsResult>,
}

impl TrnlsSession {
    /// Start a session with `n` variables, `m` residuals and initial guess `x0`.
    pub fn new(n: usize, m: usize, x0: &[f64], opts: TrnlsOptions) -> Result<Self> {
        if x0.len() != n {
            return Err(Error::InvalidArgument("x0 must have length n"));
        }
        let n_i: c_int = n.try_into().map_err(|_| Error::DimensionOverflow)?;
        let m_i: c_int = m.try_into().map_err(|_| Error::DimensionOverflow)?;
        let mut s = Self {
            handle: Box::new(ptr::null_mut()),
            n,
            m,
            x: x0.to_vec(),
            fvec: vec![0.0; m],
            fjac: vec![0.0; checked_len(m, n)?],
            opts: Box::new(opts),
            rci: 0,
            done: None,
        };
        let status = unsafe {
            sys::dtrnlsp_init(
                &mut *s.handle,
                &n_i,
                &m_i,
                s.x.as_mut_ptr(),
                s.opts.eps.as_ptr(),
                &s.opts.max_iterations,
                &s.opts.max_trial_steps,
                &s.opts.initial_trust_region,
            )
        };
        if status != sys::TR_SUCCESS as c_int {
            // No handle was created: Drop must not delete it.
            *s.handle = ptr::null_mut();
            return Err(Error::LapackComputationFailure { info: status });
        }
        Ok(s)
    }

    /// Current iterate; evaluate residual / Jacobian here.
    #[must_use]
    pub fn x(&self) -> &[f64] {
        &self.x
    }

    /// Residual buffer (length `m`) to fill on [`TrnlsStep::NeedResidual`].
    pub fn residual_mut(&mut self) -> &mut [f64] {
        &mut self.fvec
    }

    /// Jacobian buffer (column-major `m x n`) to fill on [`TrnlsStep::NeedJacobian`].
    pub fn jacobian_mut(&mut self) -> &mut [f64] {
        &mut self.fjac
    }

    /// Validate the Jacobian, residual and tolerances currently in the
    /// session's buffers (`dtrnlsp_check`). Call after filling both buffers.
    pub fn check(&mut self) -> Result<TrnlsCheck> {
        let n_i = self.n as c_int;
        let m_i = self.m as c_int;
        let mut info = [0 as c_int; 6];
        let status = unsafe {
            sys::dtrnlsp_check(
                &mut *self.handle,
                &n_i,
                &m_i,
                self.fjac.as_ptr(),
                self.fvec.as_ptr(),
                self.opts.eps.as_ptr(),
                info.as_mut_ptr(),
            )
        };
        if status != sys::TR_SUCCESS as c_int {
            return Err(Error::LapackComputationFailure { info: status });
        }
        Ok(TrnlsCheck { info })
    }

    /// Advance the solver one reverse-communication step. After `Done`,
    /// further calls return the same result.
    pub fn step(&mut self) -> Result<TrnlsStep> {
        if let Some(r) = self.done {
            return Ok(TrnlsStep::Done(r));
        }
        let status = unsafe {
            sys::dtrnlsp_solve(
                &mut *self.handle,
                self.fvec.as_mut_ptr(),
                self.fjac.as_mut_ptr(),
                &mut self.rci,
            )
        };
        if status != sys::TR_SUCCESS as c_int {
            return Err(Error::LapackComputationFailure { info: status });
        }
        match self.rci {
            1 => Ok(TrnlsStep::NeedResidual),
            2 => Ok(TrnlsStep::NeedJacobian),
            -6..=0 => {
                let (mut iters, mut st_cr, mut r1, mut r2) =
                    (0 as c_int, 0 as c_int, 0.0_f64, 0.0_f64);
                let status = unsafe {
                    sys::dtrnlsp_get(&mut *self.handle, &mut iters, &mut st_cr, &mut r1, &mut r2)
                };
                if status != sys::TR_SUCCESS as c_int {
                    return Err(Error::LapackComputationFailure { info: status });
                }
                let r = TrnlsResult {
                    iterations: iters,
                    stopping_criterion: st_cr,
                    initial_residual_norm: r1,
                    final_residual_norm: r2,
                };
                self.done = Some(r);
                Ok(TrnlsStep::Done(r))
            }
            other => Err(Error::LapackComputationFailure { info: other }),
        }
    }
}

impl Drop for TrnlsSession {
    fn drop(&mut self) {
        if !(*self.handle).is_null() {
            unsafe {
                let _ = sys::dtrnlsp_delete(&mut *self.handle);
            }
        }
    }
}

/// Step-by-step driver of the bound-constrained trust-region solver
/// (`dtrnlspbc_*`); the bounded counterpart of [`TrnlsSession`]. The session
/// owns `x`, the bounds and the residual / Jacobian buffers.
pub struct TrnlsBoundedSession {
    /// Boxed so the handle's address survives moves of the session.
    handle: Box<sys::_TRNSPBC_HANDLE_t>,
    n: usize,
    m: usize,
    x: Vec<f64>,
    lower: Vec<f64>,
    upper: Vec<f64>,
    fvec: Vec<f64>,
    fjac: Vec<f64>,
    opts: Box<TrnlsOptions>,
    /// Reverse-communication state: MKL reads it back on the next call.
    rci: c_int,
    done: Option<TrnlsResult>,
}

impl TrnlsBoundedSession {
    /// Start a session with `n` variables, `m` residuals, initial guess `x0`
    /// and element-wise bounds `lower[i] <= x[i] <= upper[i]`.
    pub fn new(
        n: usize,
        m: usize,
        x0: &[f64],
        lower: &[f64],
        upper: &[f64],
        opts: TrnlsOptions,
    ) -> Result<Self> {
        if x0.len() != n || lower.len() != n || upper.len() != n {
            return Err(Error::InvalidArgument(
                "x0, lower, and upper must each have length n",
            ));
        }
        let n_i: c_int = n.try_into().map_err(|_| Error::DimensionOverflow)?;
        let m_i: c_int = m.try_into().map_err(|_| Error::DimensionOverflow)?;
        let mut s = Self {
            handle: Box::new(ptr::null_mut()),
            n,
            m,
            x: x0.to_vec(),
            lower: lower.to_vec(),
            upper: upper.to_vec(),
            fvec: vec![0.0; m],
            fjac: vec![0.0; checked_len(m, n)?],
            opts: Box::new(opts),
            rci: 0,
            done: None,
        };
        let status = unsafe {
            sys::dtrnlspbc_init(
                &mut *s.handle,
                &n_i,
                &m_i,
                s.x.as_mut_ptr(),
                s.lower.as_ptr(),
                s.upper.as_ptr(),
                s.opts.eps.as_ptr(),
                &s.opts.max_iterations,
                &s.opts.max_trial_steps,
                &s.opts.initial_trust_region,
            )
        };
        if status != sys::TR_SUCCESS as c_int {
            *s.handle = ptr::null_mut();
            return Err(Error::LapackComputationFailure { info: status });
        }
        Ok(s)
    }

    /// Current iterate; evaluate residual / Jacobian here.
    #[must_use]
    pub fn x(&self) -> &[f64] {
        &self.x
    }

    /// Residual buffer (length `m`) to fill on [`TrnlsStep::NeedResidual`].
    pub fn residual_mut(&mut self) -> &mut [f64] {
        &mut self.fvec
    }

    /// Jacobian buffer (column-major `m x n`) to fill on [`TrnlsStep::NeedJacobian`].
    pub fn jacobian_mut(&mut self) -> &mut [f64] {
        &mut self.fjac
    }

    /// Validate the buffers and bounds (`dtrnlspbc_check`); see [`TrnlsCheck`].
    pub fn check(&mut self) -> Result<TrnlsCheck> {
        let n_i = self.n as c_int;
        let m_i = self.m as c_int;
        let mut info = [0 as c_int; 6];
        let status = unsafe {
            sys::dtrnlspbc_check(
                &mut *self.handle,
                &n_i,
                &m_i,
                self.fjac.as_ptr(),
                self.fvec.as_ptr(),
                self.lower.as_ptr(),
                self.upper.as_ptr(),
                self.opts.eps.as_ptr(),
                info.as_mut_ptr(),
            )
        };
        if status != sys::TR_SUCCESS as c_int {
            return Err(Error::LapackComputationFailure { info: status });
        }
        Ok(TrnlsCheck { info })
    }

    /// Advance the solver one reverse-communication step. After `Done`,
    /// further calls return the same result.
    pub fn step(&mut self) -> Result<TrnlsStep> {
        if let Some(r) = self.done {
            return Ok(TrnlsStep::Done(r));
        }
        let status = unsafe {
            sys::dtrnlspbc_solve(
                &mut *self.handle,
                self.fvec.as_mut_ptr(),
                self.fjac.as_mut_ptr(),
                &mut self.rci,
            )
        };
        if status != sys::TR_SUCCESS as c_int {
            return Err(Error::LapackComputationFailure { info: status });
        }
        match self.rci {
            1 => Ok(TrnlsStep::NeedResidual),
            2 => Ok(TrnlsStep::NeedJacobian),
            -6..=0 => {
                let (mut iters, mut st_cr, mut r1, mut r2) =
                    (0 as c_int, 0 as c_int, 0.0_f64, 0.0_f64);
                let status = unsafe {
                    sys::dtrnlspbc_get(&mut *self.handle, &mut iters, &mut st_cr, &mut r1, &mut r2)
                };
                if status != sys::TR_SUCCESS as c_int {
                    return Err(Error::LapackComputationFailure { info: status });
                }
                let r = TrnlsResult {
                    iterations: iters,
                    stopping_criterion: st_cr,
                    initial_residual_norm: r1,
                    final_residual_norm: r2,
                };
                self.done = Some(r);
                Ok(TrnlsStep::Done(r))
            }
            other => Err(Error::LapackComputationFailure { info: other }),
        }
    }
}

impl Drop for TrnlsBoundedSession {
    fn drop(&mut self) {
        if !(*self.handle).is_null() {
            unsafe {
                let _ = sys::dtrnlspbc_delete(&mut *self.handle);
            }
        }
    }
}