我从gputools R包中提取了相关的位,通过动态加载链接到culatools的共享库,使用Rcpp在我的GPU上运行QR分解。终端和我的Mac电脑上的R.app都运行得很顺利。结果与R的qr()函数一致,但问题是退出R.app时会发生分段错误(在使用终端时不会发生错误):
*** caught segfault ***
address 0x10911b050, cause 'memory not mapped'我想我把问题缩小到链接到culatools的.c文件中的指针'a‘和’τ‘。
#include<cula.h>
void gpuQR(const int *m, const int *n, float *a, const int *lda, float *tau)
{
culaInitialize();
culaSgeqrf(m[0], n[0], a, lda[0], tau);
culaShutdown();
}我在Mac上编译了.c文件,使用:
/usr/local/cuda/bin/nvcc -gencode arch=compute_10,code=sm_10 -gencode arch=compute_11,code=sm_11 -gencode arch=compute_12,code=sm_12 -gencode arch=compute_13,code=sm_13 -gencode arch=compute_20,code=sm_20 -c -I. -I/usr/local/cula/include -m64 -Xcompiler -fPIC gpuQR.c -o gpuQR.o
/usr/local/cuda/bin/nvcc -gencode arch=compute_10,code=sm_10 -gencode arch=compute_11,code=sm_11 -gencode arch=compute_12,code=sm_12 -gencode arch=compute_13,code=sm_13 -gencode arch=compute_20,code=sm_20 -shared -m64 -Xlinker -rpath,/usr/local/cula/lib64 -L/usr/local/cula/lib64 -lcula_core -lcula_lapack -lcublas -o gpuQR.so gpuQR.o我编写了一个.cpp文件,该文件使用Rcpp并动态加载共享库gpuQR.so:
#include <Rcpp.h>
#include <dlfcn.h>
using namespace Rcpp;
using namespace std;
typedef void (*func)(int*, int*, float*, int*, float*);
RcppExport SEXP gpuQR_Rcpp(SEXP x_, SEXP n_rows_, SEXP n_cols_)
{
vector<float> x = as<vector<float> >(x_);
int n_rows = as<int>(n_rows_);
int n_cols = as<int>(n_cols_);
vector<float> scale(n_cols);
void* lib_handle = dlopen("path/gpuQR.so", RTLD_LAZY);
if (!lib_handle)
{
Rcout << dlerror() << endl;
} else {
func gpuQR = (func) dlsym(lib_handle, "gpuQR");
gpuQR(&n_rows, &n_cols, &(x[0]), &n_rows, &(scale[0]));
}
dlclose(lib_handle);
for(int ii = 1; ii < n_rows; ii++)
{
for(int jj = 0; jj < n_cols; jj++)
{
if(ii > jj) { y[ii + jj * n_rows] *= scale[jj]; }
}
}
return wrap(x);
}我在R中编译了.cpp文件,使用:
library(Rcpp)
PKG_LIBS <- sprintf('%s $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)', Rcpp:::RcppLdFlags())
PKG_CPPFLAGS <- sprintf('%s', Rcpp:::RcppCxxFlags())
Sys.setenv(PKG_LIBS = PKG_LIBS , PKG_CPPFLAGS = PKG_CPPFLAGS)
R <- file.path(R.home(component = 'bin'), 'R')
file <- 'path/gpuQR_Rcpp.cpp'
cmd <- sprintf('%s CMD SHLIB %s', R, paste(file, collapse = ' '))
system(cmd)并做了一个例子:
dyn.load('path/gpuQR_Rcpp.so')
set.seed(100)
A <- matrix(rnorm(9), 3, 3)
n_row <- nrow(A)
n_col <- ncol(A)
res <- .Call('gpuQR_Rcpp', c(A), n_row, n_col)
matrix(res, n_row, n_col)
[,1] [,2] [,3]
[1,] 0.5250958 -0.8666927 0.8594266
[2,] -0.2504899 -0.3878644 -0.1277837
[3,] 0.1502908 0.4742033 -0.8804248
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有谁知道如何修复分割错误吗?
发布于 2013-07-30 01:38:07
这个问题通过删除
dlclose(lib_handle);来自.cpp文件。这将产生以下结果:
#include <Rcpp.h>
#include <dlfcn.h>
using namespace Rcpp;
using namespace std;
typedef void (*func)(int*, int*, float*, int*, float*);
RcppExport SEXP gpuQR_Rcpp(SEXP x_, SEXP n_rows_, SEXP n_cols_)
{
vector<float> x = as<vector<float> >(x_);
int n_rows = as<int>(n_rows_);
int n_cols = as<int>(n_cols_);
vector<float> scale(n_cols);
void* lib_handle = dlopen("path/gpuQR.so", RTLD_LAZY);
if (!lib_handle)
{
Rcout << dlerror() << endl;
} else {
func gpuQR = (func) dlsym(lib_handle, "gpuQR");
gpuQR(&n_rows, &n_cols, &(x[0]), &n_rows, &(scale[0]));
}
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]; }
}
}
return wrap(x);
}可以使用以下方法在R中编译.cpp文件:
library(Rcpp)
PKG_LIBS <- sprintf('%s $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)', Rcpp:::RcppLdFlags())
PKG_CPPFLAGS <- sprintf('%s', Rcpp:::RcppCxxFlags())
Sys.setenv(PKG_LIBS = PKG_LIBS , PKG_CPPFLAGS = PKG_CPPFLAGS)
R <- file.path(R.home(component = 'bin'), 'R')
file <- 'path/gpuQR_Rcpp.cpp'
cmd <- sprintf('%s CMD SHLIB %s', R, paste(file, collapse = ' '))
system(cmd)链接到culatools的实际.c文件是:
#include<cula.h>
void gpuQR(const int *m, const int *n, float *a, const int *lda, float *tau)
{
culaInitialize();
culaSgeqrf(m[0], n[0], a, lda[0], tau);
culaShutdown();
}它可以使用以下方法进行编译:
gcc -c -I/usr/local/cula/include gpuQR.c
gcc -shared -Wl,-rpath,/usr/local/cula/lib64 -L/usr/local/cula/lib64 -lcula_lapack -o gpuQR.so gpuQR.o然后,QR分解可以在R中执行,使用:
dyn.load('path/gpuQR_Rcpp.so')
set.seed(100)
n_row <- 3
n_col <- 3
A <- matrix(rnorm(n_row * n_col), n_row, n_col)
res <- .Call('gpuQR_Rcpp', c(A), n_row, n_col)
matrix(res, n_row, n_col)
[,1] [,2] [,3]
[1,] 0.5250958 -0.8666927 0.8594266
[2,] -0.2504899 -0.3878644 -0.1277837
[3,] 0.1502908 0.4742033 -0.8804248
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以下是使用带有16个CUDA核心的NVIDIA GeForce 9400 M GPU的基准测试结果:
n_row <- 1000; n_col <- 1000
A <- matrix(rnorm(n_row * n_col), n_row, n_col)
B <- A; dim(B) <- NULL
res <- benchmark(.Call('gpuQR_Rcpp', B, n_row, n_col), qr(A), columns = c('test', 'replications', 'elapsed', 'relative'), order = 'relative')
test replications elapsed relative
1 .Call("gpuQR_Rcpp", B, n_row, n_col) 100 38.037 1.000
2 qr(A) 100 152.575 4.011发布于 2013-07-31 15:48:10
实际上,不需要动态加载链接到culatools的共享库。我最初考虑过这一点,但没有使用编译的.cpp文件。无论如何,新的.cpp文件是:
#include<Rcpp.h>
#include<cula.h>
using namespace Rcpp;
using namespace std;
RcppExport SEXP gpuQR_Rcpp(SEXP x_, SEXP n_rows_, SEXP n_cols_)
{
vector<float> x = as<vector<float> >(x_);
int n_rows = as<int>(n_rows_);
int n_cols = as<int>(n_cols_);
vector<float> scale(n_cols);
culaInitialize();
culaSgeqrf(n_rows, n_cols, &(x[0]), n_rows, &(scale[0]));
culaShutdown();
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]; }
}
}
return wrap(x);
}使用以下方法编译.cpp文件:
library(Rcpp)
PKG_LIBS <- sprintf('-Wl,-rpath,/usr/local/cula/lib64 -L/usr/local/cula/lib64 -lcula_lapack %s $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)', Rcpp:::RcppLdFlags())
PKG_CPPFLAGS <- sprintf('-I/usr/local/cula/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_inc.cpp'
cmd <- sprintf('%s CMD SHLIB %s', R, paste(file, collapse = ' '))
system(cmd)在那里我设置了适合培养的路径。整个过程不会运行得更快,但是不再需要编译连接到culatools的共享库并动态加载它。
我认为这是gputools R包的一个很好的替代方案,可以用C++扩展R并在GPU上执行线性代数操作。
https://stackoverflow.com/questions/17925733
复制相似问题