gpt4 book ai didi

r - 从 R/Rcpp 使用的 OpenBLAS 例程仅在 linux 的单个内核上运行

转载 作者:行者123 更新时间:2023-12-04 12:47:48 28 4
gpt4 key购买 nike

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

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

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

关于r - 从 R/Rcpp 使用的 OpenBLAS 例程仅在 linux 的单个内核上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21730547/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com