[Rcpp-devel] Seg faults using Rcpp and OpenMP

Dirk Eddelbuettel edd at debian.org
Thu Feb 2 17:05:34 CET 2012


On 1 February 2012 at 15:16, Chris DuBois wrote:
| Hi all,
| 
| I am having trouble with seg faults using OpenMP with Rcpp objects.  I have a
| nested for loop where I can only parallelize the inner for loop.  Inside I
| call a function that has an Rcpp::NumericVector as an argument, and this seems

As I recall you can't do that.  R is single-threaded, and this could call
back to the R object from multiple code chunks.

I have a dormant project where I tried OpenMP, and I think I kept all
data-structure _for the OpenMP part_ as STL vectors etc.

Dirk

| to contribute to the issue.  The number of outer loops also contributes.
| 
| I've included an example of R code below.  The last R call gives a seg fault
| when calling Rscript code.r (at least for me):
| Error: segfault from C stack overflow
| Execution halted
| Error: C stack usage is too close to the limit
| Execution halted
| 
| Any help is much appreciated.
| Chris
| 
| -----------
| 
| library(inline)
| library(Rcpp)
| 
| settings <- getPlugin("Rcpp")
| settings$env$PKG_CXXFLAGS <- paste('-fopenmp', settings$env$PKG_CXXFLAGS)
| settings$env$PKG_LIBS <- paste('-fopenmp -lgomp', settings$env$PKG_LIBS)
| 
| fx <- cxxfunction(,"",includes=
|   '
| #include <iostream>
| using namespace std;
| #include <omp.h>
| 
| double mysqrt1(double x, std::vector<double> s) {
|   return sqrt(x);
| }
| double mysqrt2(double x, Rcpp::NumericVector s) {
|   return sqrt(x);
| }
| double fn1(int K, int N) {
|   std::vector<double> x(K);
|   std::vector<double> y(N);
|   for (int k = 0; k < K; k++) {
|     #pragma omp parallel
|     {
|     #pragma omp for
|       for (int i=0; i<N; i++) {
|         y[i] = mysqrt1(x[i],x);
|       }
|     }
|   }
|   return 0.0;
| }
| double fn2(int K, int N) {
|   Rcpp::NumericVector x(K);
|   Rcpp::NumericVector y(N);
|   for (int k = 0; k < K; k++) {
|     #pragma omp parallel
|     {
|     #pragma omp for
|       for (int i=0; i<N; i++) {
|         y(i) = mysqrt2(x(i),x);
|       }
|     }
|   }
|   return 0.0;
| }
| 
| RCPP_MODULE(example){
|   function( "fn1", &fn1 ) ;
|   function( "fn2", &fn2 ) ;
| }
| ', plugin="Rcpp",settings=settings)
| 
| 
| example <- Module("example",getDynLib(fx))
| 
| example$fn1(10,10)
| example$fn1(1000,10)
| example$fn1(1000,10000)
| 
| example$fn2(10,10)
| example$fn2(100,10)  
| example$fn2(1000,10)  # seg fault
| 
| ----------------------------------------------------------------------
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
-- 
"Outside of a dog, a book is a man's best friend. Inside of a dog, it is too
dark to read." -- Groucho Marx


More information about the Rcpp-devel mailing list