Skip to main content

set_blas_backend

Function set_blas_backend 

Source
pub unsafe fn set_blas_backend(
    dgemm: unsafe extern "C" fn(*const i8, *const i8, *const i32, *const i32, *const i32, *const f64, *const f64, *const i32, *const f64, *const i32, *const f64, *mut f64, *const i32),
    zgemm: unsafe extern "C" fn(*const i8, *const i8, *const i32, *const i32, *const i32, *const Complex<f64>, *const Complex<f64>, *const i32, *const Complex<f64>, *const i32, *const Complex<f64>, *mut Complex<f64>, *const i32),
)
Expand description

Set the process-wide default to an LP64 BLAS.

§Safety

The function pointers must be valid Fortran-convention LP64 dgemm_ and zgemm_ implementations that stay valid for the rest of the process.

§Example

use num_complex::Complex;
use sparse_ir::Matrix;
use sparse_ir::gemm::{clear_blas_backend, get_backend_info, matmul, set_blas_backend};

// `my_dgemm` and `my_zgemm` are the LP64 Fortran BLAS `dgemm_` and `zgemm_`
// to use, e.g. from OpenBLAS. (This example defines naive stand-ins in
// hidden lines so that it runs without a BLAS library.)
unsafe {
    set_blas_backend(my_dgemm, my_zgemm);
}
assert_eq!(get_backend_info(), ("External BLAS (LP64)", true, false));

// Calls without an explicit backend handle now go through the injected BLAS
// Column-major data: [[1, 2], [3, 4]] and [[5, 6], [7, 8]]
let a = Matrix::<f64>::from_vec_col_major([2, 2], vec![1.0, 3.0, 2.0, 4.0]).unwrap();
let b = Matrix::<f64>::from_vec_col_major([2, 2], vec![5.0, 7.0, 6.0, 8.0]).unwrap();
let c = matmul(None, &a, &b).unwrap();
assert_eq!(c.host_data().unwrap(), &[19.0, 43.0, 22.0, 50.0]); // [[19, 22], [43, 50]]

// Complex matrices use `zgemm`: (i A) B = i (A B)
let to_complex = |m: &Matrix<f64>, imaginary: bool| {
    let data = m.host_data().unwrap().iter().map(|&x| {
        if imaginary { Complex::new(0.0, x) } else { Complex::new(x, 0.0) }
    });
    Matrix::from_vec_col_major([2, 2], data.collect()).unwrap()
};
let ic = matmul(None, &to_complex(&a, true), &to_complex(&b, false)).unwrap();
assert_eq!(ic.host_data().unwrap(), to_complex(&c, true).host_data().unwrap());

clear_blas_backend(); // back to the pure-Rust Faer backend
assert!(!get_backend_info().1);