ndarray icon indicating copy to clipboard operation
ndarray copied to clipboard

macOS & accelerate BLAS for matmul

Open unconverged opened this issue 3 years ago • 3 comments
trafficstars

Hi! I'm trying to experiment with ndarray & BLAS on macOS. Since I'm using macOS, I'd like to use the Accelerate framework, a built-in BLAS. This is my code:

extern crate ndarray;

use ndarray::arr2;

fn main() {

    let a = arr2(&[[1., 2.], [3., 4.]]);
    let b = arr2(&[[1., 2.], [3., 4.]]);

    let c = a.dot(&b);
}

and these are my dependencies:

[dependencies]
ndarray = { version = "0.15.0", features = ["blas"] }
blas-src = { version = "0.8", features = ["accelerate"] }

However, the application fails to compile producing the following errors:

= note: Undefined symbols for architecture x86_64:
            "_cblas_sgemm", referenced from:
                ndarray::linalg::impl_linalg::mat_mul_impl::h3695191879e5c795 in untitled-87ed52038fcda5fd.3ydzrgohodau95nr.rcgu.o
            "_cblas_dgemm", referenced from:
                ndarray::linalg::impl_linalg::mat_mul_impl::h3695191879e5c795 in untitled-87ed52038fcda5fd.3ydzrgohodau95nr.rcgu.o
            "_cblas_cgemm", referenced from:
                ndarray::linalg::impl_linalg::mat_mul_impl::h3695191879e5c795 in untitled-87ed52038fcda5fd.3ydzrgohodau95nr.rcgu.o
            "_cblas_zgemm", referenced from:
                ndarray::linalg::impl_linalg::mat_mul_impl::h3695191879e5c795 in untitled-87ed52038fcda5fd.3ydzrgohodau95nr.rcgu.o
          ld: symbol(s) not found for architecture x86_64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

So it appears ndarray does not support Accelerate as a backend. Is it true, or did I miss something?

UPD: I've just checked openblas, and I have the same problem (exactly the same error message) using the following config:

[dependencies]
ndarray = { version = "0.15.0", features = ["blas"] }
blas-src = { version = "0.8", features = ["openblas"] }
openblas-src = { version = "0.10", features = ["cblas"] }

I'm using macOS

unconverged avatar Aug 19 '22 18:08 unconverged

Hi I'm having the exact same issue, I'm trying to write a matrix exponentiation routine for some simulations (I don't want to use numpy or c++) and this issue keeps popping up for me. I've tried that Cargo.toml and having the following in the build.rs fn main() {println!("cargo:rustc-link-search=/opt/homebrew/opt/openblas/lib");} and it still cannot seem to find my installed openblas lib (aka I still have the "_cblas_zgemm" reference issues). Kinda stuck for how to get around this

matthagan15 avatar Sep 24 '22 16:09 matthagan15

I had a similar problem with Accelerate and, as it turned out, the framework had to be added to the rustc invocation for the binary since they are all linked dynamically. As a workaround, running cargo --config 'build.rustflags="-l framework=Accelerate"' build -v worked for me. It seems clunky so I'm hoping there is a better way. But for now, if you need to use it regularly, you could add the rust flags to the global config (~/.cargo/config):

[build]
rustflags = "-l framework=Accelerate"

rappoport avatar Sep 29 '22 04:09 rappoport

Appreciate the help! I actually posted this same issue on reddit and someone suggested adding cargo:rustc-link-lib=framework=Accelerate to a println! in a build.rs file, which worked for me.

matthagan15 avatar Sep 29 '22 16:09 matthagan15