Yes, that would explain it.<div><br></div><div>Thanks!</div><div><br></div><div>Chris<br><br><div class="gmail_quote">On Thu, Feb 2, 2012 at 8:05 AM, Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org">edd@debian.org</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im"><br>
On 1 February 2012 at 15:16, Chris DuBois wrote:<br>
| Hi all,<br>
|<br>
| I am having trouble with seg faults using OpenMP with Rcpp objects.  I have a<br>
| nested for loop where I can only parallelize the inner for loop.  Inside I<br>
| call a function that has an Rcpp::NumericVector as an argument, and this seems<br>
<br>
</div>As I recall you can't do that.  R is single-threaded, and this could call<br>
back to the R object from multiple code chunks.<br>
<br>
I have a dormant project where I tried OpenMP, and I think I kept all<br>
data-structure _for the OpenMP part_ as STL vectors etc.<br>
<br>
Dirk<br>
<div><div class="h5"><br>
| to contribute to the issue.  The number of outer loops also contributes.<br>
|<br>
| I've included an example of R code below.  The last R call gives a seg fault<br>
| when calling Rscript code.r (at least for me):<br>
| Error: segfault from C stack overflow<br>
| Execution halted<br>
| Error: C stack usage is too close to the limit<br>
| Execution halted<br>
|<br>
| Any help is much appreciated.<br>
| Chris<br>
|<br>
| -----------<br>
|<br>
| library(inline)<br>
| library(Rcpp)<br>
|<br>
| settings <- getPlugin("Rcpp")<br>
| settings$env$PKG_CXXFLAGS <- paste('-fopenmp', settings$env$PKG_CXXFLAGS)<br>
| settings$env$PKG_LIBS <- paste('-fopenmp -lgomp', settings$env$PKG_LIBS)<br>
|<br>
| fx <- cxxfunction(,"",includes=<br>
|   '<br>
| #include <iostream><br>
| using namespace std;<br>
| #include <omp.h><br>
|<br>
| double mysqrt1(double x, std::vector<double> s) {<br>
|   return sqrt(x);<br>
| }<br>
| double mysqrt2(double x, Rcpp::NumericVector s) {<br>
|   return sqrt(x);<br>
| }<br>
| double fn1(int K, int N) {<br>
|   std::vector<double> x(K);<br>
|   std::vector<double> y(N);<br>
|   for (int k = 0; k < K; k++) {<br>
|     #pragma omp parallel<br>
|     {<br>
|     #pragma omp for<br>
|       for (int i=0; i<N; i++) {<br>
|         y[i] = mysqrt1(x[i],x);<br>
|       }<br>
|     }<br>
|   }<br>
|   return 0.0;<br>
| }<br>
| double fn2(int K, int N) {<br>
|   Rcpp::NumericVector x(K);<br>
|   Rcpp::NumericVector y(N);<br>
|   for (int k = 0; k < K; k++) {<br>
|     #pragma omp parallel<br>
|     {<br>
|     #pragma omp for<br>
|       for (int i=0; i<N; i++) {<br>
|         y(i) = mysqrt2(x(i),x);<br>
|       }<br>
|     }<br>
|   }<br>
|   return 0.0;<br>
| }<br>
|<br>
| RCPP_MODULE(example){<br>
|   function( "fn1", &fn1 ) ;<br>
|   function( "fn2", &fn2 ) ;<br>
| }<br>
| ', plugin="Rcpp",settings=settings)<br>
|<br>
|<br>
| example <- Module("example",getDynLib(fx))<br>
|<br>
| example$fn1(10,10)<br>
| example$fn1(1000,10)<br>
| example$fn1(1000,10000)<br>
|<br>
| example$fn2(10,10)<br>
| example$fn2(100,10)  <br>
| example$fn2(1000,10)  # seg fault<br>
|<br>
</div></div>| ----------------------------------------------------------------------<br>
| _______________________________________________<br>
| Rcpp-devel mailing list<br>
| <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
| <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
<span class="HOEnZb"><font color="#888888">--<br>
"Outside of a dog, a book is a man's best friend. Inside of a dog, it is too<br>
dark to read." -- Groucho Marx<br>
</font></span></blockquote></div><br></div>