seanmonstar/num_cpus

num_cpus::get() returns 1 when used alongside blas library

cake4289 opened this issue · 1 comments

cargo new example
cd example
cat << EOF >> Cargo.toml
num_cpus = "1.7"
blas = "0.18"
EOF
cat << EOF > src/lib.rs
extern crate num_cpus;
extern crate blas;

#[test]
fn test_cpus() {
    assert_eq!(num_cpus::get(), 32);
}

unsafe fn link() {
    blas::c::daxpy(0, 1.0, &[], 1, &mut [], 1);
}
EOF
cargo test

results in

running 1 test
test test_cpus ... FAILED

failures:

---- test_cpus stdout ----
        thread 'test_cpus' panicked at 'assertion failed: `(left == right)`
  left: `1`,
 right: `32`', src/lib.rs:6:4
note: Run with `RUST_BACKTRACE=1` for a backtrace

On my machine I am expecting 32 cpus. Commenting out the link function causes the test to pass.
I can honestly say that I have very little clue as to what's going on here.

I am running x86_64 linux.

Update - from https://github.com/xianyi/OpenBLAS/wiki/faq#multi-threaded :

How can I use OpenBLAS in multi-threaded applications?
If your application is already multi-threaded, it will conflict with OpenBLAS multi-threading. Thus, you must set OpenBLAS to use single thread as following.

export OPENBLAS_NUM_THREADS=1 in the environment variables. Or
Call openblas_set_num_threads(1) in the application on runtime. Or
Build OpenBLAS single thread version, e.g. make USE_THREAD=0

setting OPENBLAS_NUM_THREADS=1 fixes the issue and the test passes. Looks like an issue I should take up with the blas library.