<div>Hi all,</div><div><br></div><div>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.</div>
<div><br></div><div>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):</div><div><div>Error: segfault from C stack overflow</div><div>Execution halted</div>
<div>Error: C stack usage is too close to the limit</div><div>Execution halted</div></div><div><br></div><div>Any help is much appreciated.</div><div>Chris</div><div><br></div><div>-----------</div><div><br></div><div>library(inline)</div>
<div>library(Rcpp)</div><div><br></div><div>settings <- getPlugin("Rcpp")</div><div>settings$env$PKG_CXXFLAGS <- paste('-fopenmp', settings$env$PKG_CXXFLAGS)</div><div>settings$env$PKG_LIBS <- paste('-fopenmp -lgomp', settings$env$PKG_LIBS)</div>
<div><br></div><div>fx <- cxxfunction(,"",includes=</div><div> '</div><div>#include <iostream></div><div>using namespace std;</div><div>#include <omp.h></div><div><br></div><div>double mysqrt1(double x, std::vector<double> s) {</div>
<div> return sqrt(x);</div><div>}</div><div>double mysqrt2(double x, Rcpp::NumericVector s) {</div><div> return sqrt(x);</div><div>}</div><div>double fn1(int K, int N) {</div><div> std::vector<double> x(K);</div>
<div> std::vector<double> y(N);</div><div> for (int k = 0; k < K; k++) {</div><div> #pragma omp parallel</div><div> {</div><div> #pragma omp for</div><div> for (int i=0; i<N; i++) {</div><div>
y[i] = mysqrt1(x[i],x);</div>
<div> }</div><div> }</div><div> }</div><div> return 0.0;</div><div>}</div><div>double fn2(int K, int N) {</div><div> Rcpp::NumericVector x(K);</div><div> Rcpp::NumericVector y(N);</div><div> for (int k = 0; k < K; k++) {</div>
<div> #pragma omp parallel</div><div> {</div><div> #pragma omp for</div><div> for (int i=0; i<N; i++) {</div><div> y(i) = mysqrt2(x(i),x);</div><div> }</div><div> }</div><div> }</div><div>
return 0.0;</div><div>}</div><div><br></div><div>RCPP_MODULE(example){</div><div> function( "fn1", &fn1 ) ;</div><div> function( "fn2", &fn2 ) ;</div><div>}</div><div>', plugin="Rcpp",settings=settings)</div>
<div><br></div><div><br></div><div>example <- Module("example",getDynLib(fx))</div><div><br></div><div>example$fn1(10,10)</div><div>example$fn1(1000,10)</div><div>example$fn1(1000,10000)</div><div><br></div>
<div>
example$fn2(10,10)</div><div>example$fn2(100,10) </div><div>example$fn2(1000,10) # seg fault</div>