Я извлек соответствующие биты из R-пакета gputools, чтобы выполнить декомпозицию QR на своем графическом процессоре с помощью Rcpp путем динамической загрузки общей библиотеки, которая ссылается на culatools. Все работает гладко в терминале и R.app на моем Mac. Результаты согласуются с функцией qr() в R, но проблема в том, что при выходе из R.app возникает ошибка сегментации (ошибка не происходит при использовании терминала):
*** caught segfault ***
address 0x10911b050, cause 'memory not mapped'
Я думаю, что сузил проблему до указателей «a» и «tau» в файле .c, который ссылается на culatools:
#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();
}
Я скомпилировал файл .c на своем Mac, используя:
/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);
}
Я скомпилировал файл .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
Кто-нибудь знает, как исправить ошибку сегментации?
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
, но ошибка сегментации сохраняется. - person chris   schedule 29.07.2013dlclose(lib_handle)
, и ошибка сегментации перестала возникать. Я думаю, что это и есть ответ на вопрос! - person chris   schedule 29.07.2013