- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想知道是否有人尝试使用 Rcpp和 MAGMA使用 CPU 和 GPU 加速 R 中的线性代数运算?我试过 culatools上个月它与 Rcpp ( link ) 一起工作,但 culatools 是一种商业产品,需要花钱才能访问所有功能。
最佳答案
在使用 culatools 进行修补后,使用 Rcpp 和 MAGMA 非常简单。这是 .cpp 文件:
#include<Rcpp.h>
#include<magma.h>
using namespace Rcpp;
RcppExport SEXP gpuQR_magma(SEXP X_)
{
// Input
NumericMatrix X(X_);
// Initialize magma and cublas
magma_init();
cublasInit();
// Declare variables
int info, lwork, n_rows = X.nrow(), n_cols = X.ncol(), min_mn = min(n_rows, n_cols);
double tmp[1];
NumericVector scale(min_mn);
// Query workspace size
magma_dgeqrf(n_rows, n_cols, &(X[0]), n_rows, &(scale[0]), &(work[0]), -1, &info);
lwork = work[0];
NumericVector work(lwork);
// Run QR decomposition
magma_dgeqrf(n_rows, n_cols, &(X[0]), n_rows, &(scale[0]), &(work[0]), lwork, &info);
// Scale factor result
for(int ii = 1; ii < n_rows; ii++)
{
for(int jj = 0; jj < n_cols; jj++)
{
if(ii > jj) { X[ii + jj * n_rows] *= scale[jj]; }
}
}
// Shutdown magma and cublas
magma_finalize();
cublasShutdown();
// Output
return wrap(X);
}
可以使用以下方法将文件从 R 编译成共享库:
library(Rcpp)
PKG_LIBS <- sprintf('-Wl,-rpath,/usr/local/magma/lib -L/usr/local/magma/lib -lmagma /usr/local/magma/lib/libmagma.a -Wl,-rpath,/usr/local/cuda-5.5/lib64 %s', Rcpp:::RcppLdFlags())
PKG_CPPFLAGS <- sprintf('-DADD_ -DHAVE_CUBLAS -I/usr/local/magma/include -I/usr/local/cuda-5.5/include %s', Rcpp:::RcppCxxFlags())
Sys.setenv(PKG_LIBS = PKG_LIBS , PKG_CPPFLAGS = PKG_CPPFLAGS)
R <- file.path(R.home(component = 'bin'), 'R')
file <- '/path/gpuQR_magma.cpp'
cmd <- sprintf('%s CMD SHLIB %s', R, paste(file, collapse = ' '))
system(cmd)
现在可以在 R 中调用共享库。将结果与 R 的 qr() 进行比较,得出:
dyn.load('/path/gpuQR_magma.so')
set.seed(100)
n_row <- 3; n_col <- 3
A <- matrix(rnorm(n_row * n_col), n_row, n_col)
qr(A)$qr
[,1] [,2] [,3]
[1,] 0.5250957 -0.8666925 0.8594266
[2,] -0.2504899 -0.3878643 -0.1277838
[3,] 0.1502909 0.4742033 -0.8804247
.Call('gpuQR_magma', A)
[,1] [,2] [,3]
[1,] 0.5250957 -0.8666925 0.8594266
[2,] -0.2504899 -0.3878643 -0.1277838
[3,] 0.1502909 0.4742033 -0.8804247
以下是使用具有 960 个 CUDA 内核和 OpenBLAS 的 NVIDIA GeForce GTX 675MX GPU 进行基准测试的结果:
n_row <- 3000; n_col <- 3000
A <- matrix(rnorm(n_row * n_col), n_row, n_col)
B <- A; dim(B) <- NULL
res <- benchmark(.Call('gpuQR_magma', A), .Call('gpuQR_cula', B, n_row, n_col), qr(A), columns = c('test', 'replications', 'elapsed', 'relative'), order = 'relative')
test replications elapsed relative
2 .Call("gpuQR_cula", B, n_row, n_col) 100 18.704 1.000
1 .Call("gpuQR_magma", A) 100 70.461 3.767
3 qr(A) 100 985.411 52.685
似乎 MAGMA 与 culatools 相比有点慢(在本例中)。但是,MAGMA 会出现内存不足的情况,鉴于只有 1Gb 的 GPU 内存,我非常看重这一点。
关于r - R 中线性代数的 MAGMA 和 Rcpp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18402024/
我开始测试 Haskell 的线性代数。有没有人为此目的提供最佳包装的建议?使用 Haskell 进行基本矩阵操作的任何其他好的资源? Haskell 维基 lists several resourc
对于给定的向量,我想找到它周围的正交基, 即给定向量归一化和随机选择的正交子空间的基础。 在 Julia 中有一个方便的功能吗? 最佳答案 您正在寻找的函数名为 nullspace . julia>
我想计算 Julia 1.0 中的经典伴随 为此,我复制了 wikipedia 中作为示例给出的矩阵 julia> B = [-3 2 -5; -1 0 -2; 3 -4 1] 3×3 Array{I
我最近开始阅读 OpenGL Superbible 第 5 版,并注意到以下内容: 刚刚学习了线性代数,这对我来说似乎很奇怪。列向量的尺寸为 4x1,矩阵的尺寸为 4x4,如何将它们相乘?如果向量是行
我正在尝试修改我的线性代数模块以避免虚拟 vtable 的东西.. 尝试使用 CRTP 和表达式模板。我选择了一些基本的东西来测试整个事情,但我无法让它工作。 我有 4 个类,比如:基 CRTP 类,
我对 C++ 和 OpenCV 很陌生,但对 Matlab 比较熟悉。我有一项任务需要转移到 C++ 以加快处理速度。所以我想就图像处理问题征求您的建议。我在一个文件夹中有 10 张图片,我可以使用
我有一个数组 w (shape (3000, 100, 100)) 我想将它与另一个数组 e (shape (5, 3000)) 使得结果 k 的形状为 (5, 5, 100, 100) 和 k[:,
Tally-ho 小伙子们, 这个问题认为线性代数的艺术是数学中我无法解决的地方。所以我希望你们能帮助我 :D。 我正在尝试为一款名为《骑马与砍杀》的游戏创建单人自动踢球作弊。这个 autokicke
我是一名优秀的程序员,十分优秀!