我提取的相关位从gputoolsř -package到使用RCPP通过动态加载链接到culatools一个共享库我的GPU上运行的QR分解。在我的Mac上,所有内容都可以在终端和R.app中顺利运行。该结果与- [R的QR()功能,但问题是,分割故障发生在离开R.app(使用终端时,不会发生错误):RCPP和CULA:分段错误
*** caught segfault ***
address 0x10911b050, cause 'memory not mapped'
我想我缩小了问题的指针“a”和“头”在链接到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();
}
我编译使用我的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
我写使用RCPP和动态加载共享库gpuQR.so .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]));
}
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);
}
我使用编译cpp文件中- [R:
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
有没有人有一个想法如何解决分段故障?
你为什么使用nvcc编译你的代码?如果它是普通的C代码,则可以(也可能应该)使用主机C编译器。 nvcc默认使用C++主机编译路由。这可能不是你想要的。 – talonmies
我刚刚使用了* gputools R *包使用的内容。实际上,* Nvidia * [文档](http://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc)对此做了如下说明:“... nvcc模仿GNU编译器gcc的行为:它接受一系列常规编译器选项,例如定义宏和include /库路径,以及指导编译过程。所有非CUDA编译步骤都被转发到nvcc支持的通用C编译器,...“ – chris
是的,我完全理解这一点。但请注意,“通用编译器”是C++编译器,而不是C编译器。所以你的C代码可以用C++编译器编译。大多数时候它并不重要,但有时它确实如此。这可能是其中的一种...... – talonmies