我正在尝试使用与Rcpp连接的C++程序在Linux机器(CentOS)上运行R分解(LAPACKE_dgeqrf).不幸的是,我看到只有100%使用top.这也发生在Red Hat Enterprise Linux服务器上.但是,从终端(独立于R外部)启动时,C++程序(带有LAPACKE_dgeqrf)以nthreads*100%运行.我用Open编译了OpenBLAS
NO_AFFINITY=1
Run Code Online (Sandbox Code Playgroud)
并尝试过
export OPENBLAS_NUM_THREADS=4
export GOTO_NUM_THREADS=4
export OMP_NUM_THREADS=4
export OPENBLAS_MAIN_FREE=1
Run Code Online (Sandbox Code Playgroud)
什么都行不通.虽然在Mac上一切正常.并行R包中的'mcaffinity()'返回NULL.我用R配置了R.
configure 'CFLAGS=-g -O3 -Wall -pedantic' 'CXXFLAGS=-g -O3 -Wall -pedantic' 'FCFLAGS=-g -O3' 'F77FLAGS=-g -O3' '--with-system-zlib' '--enable-memory-profiling'
Run Code Online (Sandbox Code Playgroud)
我的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;
}
Run Code Online (Sandbox Code Playgroud)
我的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)
Run Code Online (Sandbox Code Playgroud)
我通过重建 R 并使用配置它找到了解决方案
../configure --enable-BLAS-shlib --enable-R-shlib --enable-memory-profiling --with-tcltk=no
Run Code Online (Sandbox Code Playgroud)
后来我只好替换libRblas.so
为相应的OpenBLAS文件libopenblas.so
。顺便说一句,我使用标准设置(即具有亲和力)构建 OpenBLAS。R 函数qr()
现在也使用所有内核和 C++ 程序。这样做的原因是,启动时 R 现在以多个线程启动(通过 验证cat /proc/pid/status
)。在不替换 的情况下libRblas.so
,R 将使用一个线程启动,然后在调用 OpenBLAS 时启动多个线程,这些线程正确固定到第一个核心。