<div dir="ltr"><div><div><div>Hello Justace,<br></div>It seems that you should write down j < end rather than j <= end.<br><br><br></div>Best,<br></div>Yixuan<br></div><div class="gmail_extra"><br><div class="gmail_quote">2014-11-23 21:10 GMT-05:00 Justace Clutter <span dir="ltr"><<a href="mailto:justacec@gmail.com" target="_blank">justacec@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>I posted the following to SO earlier this morning and it was suggested that I repost this question to the rcpp-devel list for exposure.  For reference, the original posting is at <a href="http://stackoverflow.com/questions/27089888/rcppparallel-example-crashing-r" target="_blank">http://stackoverflow.com/questions/27089888/rcppparallel-example-crashing-r</a>.</div><div><br></div><div>I have been trying to parallelize one of my Rcpp routines.  In doing so I have been trying to follow the [Parallel Distance Calculation](<a href="http://gallery.rcpp.org/articles/parallel-distance-matrix/" target="_blank">http://gallery.rcpp.org/articles/parallel-distance-matrix/</a>) example from jjalaire.  Unfortunately, once I got everything coded up and started to play around, my R session would crash.  Sometimes after the first execution, sometimes after the third.  To be honest, it was a crap shoot as to when R would crash when I ran the routine.  So, I have paired down my code to a small reproducible example to play with.</div><div><br></div><div><b><font size="4">**Rcpp File (mytest.cpp)**</font></b></div><div>------------------------------------------------------------------------------------------------------------</div><div>    #include <Rcpp.h></div><div>    // [[Rcpp::depends(RcppParallel)]]</div><div>    #include <RcppParallel.h></div><div>    </div><div>    using namespace std;</div><div>    using namespace Rcpp;</div><div>    using namespace RcppParallel;</div><div>    </div><div>    struct MyThing : public Worker {</div><div>      RVector<double> _pc;</div><div>      RVector<double> _pcsd;</div><div>      </div><div>      MyThing(Rcpp::NumericVector _pc, Rcpp::NumericVector _pcsd) : _pc(_pc), _pcsd(_pcsd){}</div><div>      </div><div>      void operator()(std::size_t begin, std::size_t end) {</div><div>        </div><div>        for(int j = begin; j <= end; j++) {      </div><div>          _pc[j] = 1;</div><div>    //      _pcsd[j] = 1;</div><div>        }</div><div>      }    </div><div>    };</div><div>    </div><div>    // [[Rcpp::export]]</div><div>    void calculateMyThingParallel() {</div><div>    </div><div>      NumericVector _pc(100);</div><div>      NumericVector _pcsd(100);</div><div>      </div><div>      MyThing mt(_pc, _pcsd);</div><div>      </div><div>      parallelFor(0, 100, mt);</div><div>    }</div><div><div>------------------------------------------------------------------------------------------------------------</div></div><div><br></div><div><br></div><div><font size="4"><b>**R Compilation and Execution Script (mytest.R)**</b></font></div><div><div>------------------------------------------------------------------------------------------------------------</div></div><div>    library(Rcpp)<br></div><div>    library(inline)</div><div>    </div><div>    sourceCpp('mytest.cpp')</div><div>    </div><div>    testmything = function() {</div><div>      calculateMyThingParallel()</div><div>    }</div><div>    </div><div>    if(TRUE) {</div><div>      for(i in 1:20) {</div><div>        testmything()</div><div>      }</div><div>    }</div><div><div>------------------------------------------------------------------------------------------------------------</div></div><div><br></div><div>The error seems to be directly related to my setting of the _pc and _pcsd variables in the `operator()` method.  If I take those out things dramatically improve.  Based on the Parallel Distance Calculation example, I am not sure what it is that I have done wrong here.  I was under the impression that RVector<type> was thread safe.  Although that is my impression, I know this is an issue with threads somehow.  Can anybody help me to understand why the above code randomly crashes my R sessions?</div><div><br></div><div>For information I am running the following:</div><div>  </div><div>  - Windows 7</div><div>  - R: 3.1.2</div><div>  - Rtools: 3.1</div><div>  - Rcpp: 0.11.3</div><div>  - inline: 0.3.13</div><div>  - RStudio: 0.99.62</div><div><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></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature">Yixuan Qiu <<a href="mailto:yixuan.qiu@cos.name" target="_blank">yixuan.qiu@cos.name</a>><br>Department of Statistics,<br>Purdue University<br></div>
</div>