从 R/Rcpp 使用的 OpenBLAS 例程仅在 Linux 中的单个内核上运行



我正在尝试使用与 Rcpp 接口的C++程序在 Linux 机器 (CentOS) 上运行 R 中的 QR 分解 (LAPACKE_dgeqrf)。 不幸的是,我只看到 100% 使用 top。这也会发生在红帽企业 Linux 服务器上。但是,C++程序(带 LAPACKE_dgeqrf)在从终端启动时以 nthreads * 100% 运行(独立于 R 之外)。我编译了OpenBLAS

NO_AFFINITY=1 

并尝试过

export OPENBLAS_NUM_THREADS=4
export GOTO_NUM_THREADS=4
export OMP_NUM_THREADS=4
export OPENBLAS_MAIN_FREE=1

什么都没用。不过,在Mac上一切正常。并行 R 包中的 'mcaffinity()' 返回 NULL。我使用

configure  'CFLAGS=-g -O3 -Wall -pedantic' 'CXXFLAGS=-g -O3 -Wall -pedantic' 'FCFLAGS=-g -O3' 'F77FLAGS=-g -O3' '--with-system-zlib' '--enable-memory-profiling'

我的C++代码:

#include <Rcpp.h>
#include <lapacke.h>
#include <cblas.h>
//[[Rcpp::export]]
Rcpp::NumericMatrix QRopenblas(Rcpp::NumericMatrix X)
{
    // Declare variables 
    int n_rows = X.nrow(), n_cols = X.ncol(), min_mn = std::min(n_rows, n_cols);
    Rcpp::NumericVector tau(min_mn);
    // Perform QR decomposition
    LAPACKE_dgeqrf(CblasColMajor, n_rows, n_cols, X.begin(), n_rows, tau.begin());
    return X;
}

我的 R 代码:

PKG_LIBS <- '/pathto/openblas/lib/libopenblas.a' 
PKG_CPPFLAGS <- '-I/pathto/openblas/include'
Sys.setenv(PKG_LIBS = PKG_LIBS , PKG_CPPFLAGS = PKG_CPPFLAGS) 
Rcpp::sourceCpp('/pathto/QRopenblas.cpp', rebuild = TRUE)
n_row <- 4000
n_col <- 4000
A <- matrix(rnorm(n_row * n_col), n_row, n_col)
res <- QRopenblas(A)

我通过重建 R 并使用

../configure --enable-BLAS-shlib --enable-R-shlib --enable-memory-profiling --with-tcltk=no

之后,我不得不用相应的 OpenBLAS 文件libopenblas.so替换libRblas.so。顺便说一句,我使用标准设置(即亲和力)构建 OpenBLAS。R 函数现在qr()使用所有内核和C++程序。这样做的原因是,在启动时,R 现在使用多个线程启动(如 cat /proc/pid/status 验证)。在不替换libRblas.so的情况下,R 用一个线程启动,然后在调用 OpenBLAS 时启动多个线程,这些线程被正确固定到第一个内核。

最新更新