<div dir="ltr">I would say that was it... Thanks for catching that. Sometimes the smallest thing can get you. I have updated the SO question with the answer.<div><br></div><div>Now that I got this working I am seeing great speed improvements:</div><div><br></div><div>b1 = R native code</div><div>b2 = Rcpp non parallel</div><div>b3 = Rcpp parallel</div><div><br></div><div><div> test replications elapsed relative user.self sys.self user.child sys.child</div><div>1 b1() 10 1760.65 78.776 1753.76 0.56 NA NA</div><div>2 b2() 10 128.78 5.762 128.38 0.05 NA NA</div><div>3 b3() 10 22.35 1.000 170.68 0.06 NA NA</div></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Nov 23, 2014 at 9:15 PM, Yixuan Qiu <span dir="ltr"><<a href="mailto:yixuan.qiu@cos.name" target="_blank">yixuan.qiu@cos.name</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><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><div class="h5"><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></div></div>_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">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><span class="HOEnZb"><font color="#888888"><br></font></span></blockquote></div><span class="HOEnZb"><font color="#888888"><br><br clear="all"><br>-- <br><div>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>
</font></span></div>
</blockquote></div><br></div>