Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macOS & accelerate BLAS for matmul #1197

Closed
unconverged opened this issue Aug 19, 2022 · 3 comments
Closed

macOS & accelerate BLAS for matmul #1197

unconverged opened this issue Aug 19, 2022 · 3 comments

Comments

@unconverged
Copy link

unconverged commented Aug 19, 2022

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

@matthagan15
Copy link

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

@rappoport
Copy link

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"

@matthagan15
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants