[Rcpp-devel] Seg faults using Rcpp and OpenMP

Chris DuBois chris.dubois at gmail.com
Thu Feb 2 00:16:38 CET 2012


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 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20120201/70fcd9d8/attachment.html>


More information about the Rcpp-devel mailing list