oxiblas-ndarray
ndarray integration for OxiBLAS
Overview
oxiblas-ndarray provides integration between OxiBLAS and the ndarray crate (0.17), enabling you to use OxiBLAS BLAS/LAPACK operations on ndarray arrays. The API is a set of free functions, re-exported through oxiblas_ndarray::prelude, that take and return Array1 / Array2 values.
Features
- Conversions between
ndarray::Array2andoxiblas_matrix::Mat: the owned conversions always copy, while the view conversions (ArrayView2toMatRef) borrow the array's buffer without copying - BLAS operations on ndarray arrays: Level 1 (dot, nrm2, asum, axpy, scal), Level 2 (gemv), Level 3 (gemm), matrix norms and helpers, with
Complex32/Complex64variants - LAPACK decompositions (LU, QR, SVD, Cholesky, symmetric, Hermitian and general eigenvalue decompositions, Schur), solvers, inverse, pseudo-inverse, determinant, rank, condition number and randomized SVD
- Parallel GEMM with Rayon (
parallelfeature) and sparse CSR/CSC conversions, SpMV and a CG solver (sparsefeature) - Type-safe API: result structs (
LuResult,QrResult,SvdResult, ...) and aLapackErrortype for the LAPACK wrappers - Compatible with existing ndarray code
Installation
[]
= "0.2"
# With parallel GEMM (Rayon)
= { = "0.2", = ["parallel"] }
# With sparse (CSR/CSC) integration
= { = "0.2", = ["sparse"] }
Usage
BLAS Operations
use ;
use *;
let a = array!;
let b = array!;
// Matrix multiplication using OxiBLAS
let c = matmul;
assert_eq!;
// C = alpha * A * B + beta * C
let mut c2 = zeros;
gemm_ndarray;
assert_eq!;
// Matrix-vector product and dot product
let x = array!;
let y = matvec; // [4.0, 10.0]
let d = dot_ndarray; // 4.0
Decompositions
use ;
use *;
Linear Solvers
use array;
use *;
Conversions
use ;
use *;
// ndarray -> oxiblas (copies into a new Mat)
let nd = array!;
let ox = array2_to_mat;
// oxiblas -> ndarray (copies)
let back: = mat_to_array2;
assert_eq!;
// Zero-copy view of a column-major array (None for layouts that cannot be borrowed)
let col_major: = zeros_f;
let view = col_major.view;
let mat_ref = array_view_to_mat_ref;
assert!;
// A row-major array is borrowed as a transposed MatRef; the flag says so
let row_major = zeros;
let view = row_major.view;
let = array_view_to_mat_ref_or_transposed.expect;
assert!;
Parallel and Sparse
// Requires the `parallel` and `sparse` features
use array;
use *;
Performance
The BLAS and LAPACK wrappers delegate to OxiBLAS's optimized SIMD kernels:
- GEMM: runs
oxiblas-blasGEMM; see Performance in the root README for measured figures - Copying wrappers - The BLAS/LAPACK convenience functions copy their inputs into
Mat; only the view conversions are zero-copy - Column-major -
matmulandmatmul_parreturn column-major arrays (matmul_creturns row-major); input layout is handled by the copy
API Reference
All of the items below are available through oxiblas_ndarray::prelude::* unless noted.
BLAS (blas)
| Level | Functions |
|---|---|
| Level 1 | dot_ndarray, dot_view, nrm2_ndarray, asum_ndarray, axpy_ndarray, scal_ndarray; complex: dotc_c64_ndarray, dotu_c64_ndarray, nrm2_c64_ndarray, asum_c64_ndarray, axpy_c64_ndarray, scal_c64_ndarray and the _c32_ counterparts |
| Level 2 | gemv_ndarray (takes a Transpose), matvec, matvec_t |
| Level 3 | gemm_ndarray(alpha, a, b, beta, c), matmul, matmul_c, matmul_into; with parallel: gemm_par_ndarray, matmul_par |
| Norms | frobenius_norm, norm_1, norm_inf, norm_max (the last three for f64), complex *_c32 / *_c64 versions |
| Helpers | eye, eye_f, trace, transpose, conj_transpose_c32, conj_transpose_c64, eye_c32, eye_c64, trace_c32, trace_c64 |
LAPACK (lapack)
| Area | Functions and results |
|---|---|
| Decompositions | lu_ndarray (LuResult { l, u, perm } with solve, det), qr_ndarray (QrResult { q, r }), svd_ndarray / svd_truncated (SvdResult { u, s, vt }), cholesky_ndarray (CholeskyResult { l } with solve, det), schur_ndarray (SchurResult) |
| Eigenvalues | eig_symmetric / eigvals_symmetric (SymEvdResult { eigenvalues, eigenvectors }), eig_ndarray / eigvals_ndarray (general matrices, Eigenvalue values) |
| Randomized | rsvd_ndarray, rsvd_power_ndarray (RandomizedSvdResult { u, s, v }), low_rank_approx_ndarray |
| Solvers | solve_ndarray, solve_multiple_ndarray, lstsq_ndarray, tridiag_solve_ndarray, tridiag_solve_spd_ndarray, tridiag_solve_multiple_ndarray |
| Matrix functions | inv_ndarray, pinv_ndarray, det_ndarray, cond_ndarray, rank_ndarray |
Complex (lapack module only) |
svd_complex_ndarray, qr_complex_ndarray, cholesky_hermitian_ndarray, eig_hermitian_ndarray |
| Errors | LapackError (Singular, NotPositiveDefinite, DimensionMismatch, NotConverged, Other) and LapackResult<T> |
Conversions (conversions)
| Kind | Functions |
|---|---|
| Copying | array2_to_mat, array2_into_mat, mat_to_array2, mat_to_array2_c, mat_ref_to_array2, and ArrayD versions (arrayd_to_mat, mat_to_arrayd, arrayd_to_array2, array2_to_arrayd, ...) |
| Zero-copy views | array_view_to_mat_ref, array_view_to_mat_ref_or_transposed, array_view_mut_to_mat_mut, and ArrayViewD versions; they return None for layouts that cannot be borrowed (not contiguous along one axis, or a negative stride) |
| Layout helpers | zeros_f, filled_f, is_column_major, is_row_major, to_column_major, array1_to_vec, slice_to_array1 |
Sparse (sparse, feature sparse)
array2_to_csr, array2_to_csc (and *_with_tolerance), csr_to_array2, csc_to_array2, spmv_ndarray, spmv_full_ndarray, sparse_solve_ndarray, sparse_solve_ndarray_with_options and SparseNdarrayError.
Feature Flags
| Feature | Description | Default |
|---|---|---|
parallel |
Rayon-parallel GEMM (gemm_par_ndarray, matmul_par) via oxiblas-blas/parallel and oxiblas-core/parallel |
|
sparse |
CSR/CSC conversions, SpMV and a CG solver via oxiblas-sparse |
The default features enable std support in oxiblas-core and oxiblas-matrix.
Benchmarks
Comparison with ndarray-linalg
| Feature | oxiblas-ndarray | ndarray-linalg |
|---|---|---|
| Pure Rust | ✓ | |
| No C dependencies (library and its normal dependencies) | ✓ | |
| BLAS/LAPACK | ✓ | ✓ |
| OpenBLAS backend | ✓ | |
| Intel MKL backend | ✓ | |
| Custom backend | ✓ (OxiBLAS) | |
| Performance | See Performance | Depends on the backend |
| Cross-compilation | Easy | Difficult |
Related Crates
ndarray- N-dimensional arraysoxiblas- Core BLAS/LAPACK implementationoxiblas-matrix- Matrix types
License
Licensed under the Apache License, Version 2.0. See LICENSE for details.