Rust-Rust blas: rust-blas — BLAS bindings


MIT licensed

Rust bindings and wrappers for BLAS (Basic Linear Algebra Subprograms).


RBLAS wraps each external call in a trait with the same name (but capitalized). This trait contains a single static method, of the same name. These traits are generic over the four main types of numbers BLAS supports: f32, f64, Complex32, and Complex64.

For example the functions cblas_saxpy, cblas_daxpy, cblas_caxypy, and cblas_zaxpy are called with the function Axpy::axpy.

Additionally, RBLAS introduces a few traits to shorten calls to these BLAS functions: Vector for types that implement vector-like characteristics and Matrix for types that implement matrix-like characteristics. The Vector trait is already implemented by Vec and [] types.



By default, the library links with blas dynamically. To link to an alternate implementation, like OpenBLAS, use the environment variable CARGO_BLAS. If you've already built the bindings, you may need to clean and build again.

export CARGO_BLAS=openblas


extern crate rblas;

use rblas::Dot;

fn main() {
    let x = vec![1.0, -2.0, 3.0, 4.0];
    let y = [1.0, 1.0, 1.0, 1.0, 7.0];

    let d = Dot::dot(&x, &y[..x.len()]);
    assert_eq!(d, 6.0);

Sugared Example (Soon to be Deprecated)

extern crate rblas as blas;
use blas::math::Mat;
use blas::{Matrix, Vector};
use blas::math::Marker::T;

fn main() {
    let x = vec![1.0, 2.0];
    let xr = &x as &Vector<_>;
    let i = mat![1.0, 0.0; 0.0, 1.0];
    let ir = &i as &Matrix<_>;

    assert!(xr + &x == 2.0 * xr);
    assert!(ir * xr == x);

    let dot = (xr ^ T) * xr;
    assert!(dot == 5.0);


  • Link error In function `rblas::matrix::ops::f32.Gemm::gemm': rblas-0.0.11/src/matrix/ undefined reference to `cblas_sgemm'
    Link error In function `rblas::matrix::ops::f32.Gemm::gemm': rblas-0.0.11/src/matrix/ undefined reference to `cblas_sgemm'

    Apr 30, 2016


    I am on Fedora 23, with blas and blas-devel 3.5.0 installed. I get the following error message when trying to run tests in Leaf using cargo.

    If I switch to openblas, the link error disapears

  • Link errors when building/testing
    Link errors when building/testing

    May 3, 2016

    I'm on Gentoo and have the sci-libs/cblas-reference and virtual/cblas packages installed. When I run cargo test I get a couple of pages of errors "undefined reference to `cblas_...'"

    I eventually figured out that I need to build with CARGO_BLAS=cblas to make it work.

  • Default to openblas
    Default to openblas

    Jan 31, 2017

    See tickets #20 and #21

  • VectorOperations not implemented for slices
    VectorOperations not implemented for slices

    Feb 3, 2017

    It would be useful to have VectorOperations implemented for &[f64] etc.

  • Chore rename rust blas
    Chore rename rust blas

    Oct 18, 2019

  • Release

    Oct 18, 2019

  • Use only feature `complex` from num
    Use only feature `complex` from num

    Dec 17, 2015

    This way we don't have to compile rand, rustc-serialize and some unused parts of num.

  • Use of usize instead of i32 for dimensions/lengths?
    Use of usize instead of i32 for dimensions/lengths?

    Jul 22, 2015

    I was curious as to whether there was any reason for using i32 instead of usize for various 'size' parameters? Are negative integers ever returned? And what if (hypothetically speaking) a Vector on a 64-bit machine has a length exceeding the maximum i32 integer?

    I'll have a go at implementing this change, but it will unfortunately break quite a lot of function (though easily fixable, and better late than never).

  • Implemented simple matrix type with 2D indexing
    Implemented simple matrix type with 2D indexing

    Feb 7, 2015

    I have added a simple matrix type that wraps a vector and implements your Matrix trait. It allows use like let A = Mat::::new(4,4); A[0][1] = 1f64; ... and since A[i] yields a slice rows can also be used as a vector. This is some of my first rust code ever so I hope it's not too bad.

  • implement trait std::ops::IndexMut for rblas::math::mat::Mat ?
    implement trait std::ops::IndexMut for rblas::math::mat::Mat ?

    Jan 15, 2016


    i’m considering using rblas to speed up some machine learning code i’m working on. i’ve done some benchmarks. unsurprisingly rblas is 4-10 times faster than the same operations in nalgebra.

    i need to fill some rblas::math::mat::Mat with data flexibly and programmatically.

    unfortunately i've not found a nice way to do so. maybe i’m missing something obvious ?

    it would be great if rblas::math::mat::Mat implemented std::ops::IndexMut. is there a specific reason why it doesn't at the moment or is this just an oversight ?

    i’d be happy to implement it and do a pull request !

    let me know.

    best, max

  • Remove concat_ident! for module expressions, enable building on stable
    Remove concat_ident! for module expressions, enable building on stable

    Dec 1, 2015

    Enables rust-blas to build on Rust stable.

    This sorts the function prototypes in the FFI by group, and changes the prefix! and similar macros to using the new modules instead of concat_indent!.

    The future of concat_ident! is uncertain:

  • configurable cblas library?
    configurable cblas library?

    Jun 9, 2015

    Hi, thanks for writing this wrapper @mikkyang! It seems very intuitive and lacks the extra cruft found in some other ports.

    As it stands, I was unable to use this library. In my system "libblas" links to the (fortran?) blas library which has a different interface than the cblas library. Note that the blas functions dont have the "cblas_" prefix. The linker couldn't find the functions I wanted to use.

    In order to get your code working, I had to link against "cblas" instead. You'll notice I linked against openblas instead of cblas in this pull request. There may be other implementations of blas that users of this port may want to use too. I don't suggest merging this pull, or making a new pull linking to "cblas" instead of "openblas". I think a better solution is to let the user decide which library (with the cblas interface) they want to link to. I'm thinking some sort of ENV variable used in the macros, and if the env-var is not defined, use "cblas" as default.

    Thanks again for the port! I'm very new to rust, but if you want this change and you're not feeling up to making this change, I'd be willing to give it a shot. Also, I'm curious if your projects work if you link to "cblas" instead of "blas". I think "cblas" is the proper library to be linking to.