[Rcpp-devel] RCppParallel Example Crashing R (Cross-Post from SO)

Yixuan Qiu yixuan.qiu at cos.name
Mon Nov 24 03:15:11 CET 2014


Hello Justace,
It seems that you should write down j < end rather than j <= end.


Best,
Yixuan

2014-11-23 21:10 GMT-05:00 Justace Clutter <justacec at gmail.com>:

> 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
> http://stackoverflow.com/questions/27089888/rcppparallel-example-crashing-r
> .
>
> I have been trying to parallelize one of my Rcpp routines.  In doing so I
> have been trying to follow the [Parallel Distance Calculation](
> http://gallery.rcpp.org/articles/parallel-distance-matrix/) 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.
>
> ***Rcpp File (mytest.cpp)***
>
> ------------------------------------------------------------------------------------------------------------
>     #include <Rcpp.h>
>     // [[Rcpp::depends(RcppParallel)]]
>     #include <RcppParallel.h>
>
>     using namespace std;
>     using namespace Rcpp;
>     using namespace RcppParallel;
>
>     struct MyThing : public Worker {
>       RVector<double> _pc;
>       RVector<double> _pcsd;
>
>       MyThing(Rcpp::NumericVector _pc, Rcpp::NumericVector _pcsd) :
> _pc(_pc), _pcsd(_pcsd){}
>
>       void operator()(std::size_t begin, std::size_t end) {
>
>         for(int j = begin; j <= end; j++) {
>           _pc[j] = 1;
>     //      _pcsd[j] = 1;
>         }
>       }
>     };
>
>     // [[Rcpp::export]]
>     void calculateMyThingParallel() {
>
>       NumericVector _pc(100);
>       NumericVector _pcsd(100);
>
>       MyThing mt(_pc, _pcsd);
>
>       parallelFor(0, 100, mt);
>     }
>
> ------------------------------------------------------------------------------------------------------------
>
>
> ***R Compilation and Execution Script (mytest.R)***
>
> ------------------------------------------------------------------------------------------------------------
>     library(Rcpp)
>     library(inline)
>
>     sourceCpp('mytest.cpp')
>
>     testmything = function() {
>       calculateMyThingParallel()
>     }
>
>     if(TRUE) {
>       for(i in 1:20) {
>         testmything()
>       }
>     }
>
> ------------------------------------------------------------------------------------------------------------
>
> 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?
>
> For information I am running the following:
>
>   - Windows 7
>   - R: 3.1.2
>   - Rtools: 3.1
>   - Rcpp: 0.11.3
>   - inline: 0.3.13
>   - RStudio: 0.99.62
>
>
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>



-- 
Yixuan Qiu <yixuan.qiu at cos.name>
Department of Statistics,
Purdue University
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20141123/1b5dfdb1/attachment.html>


More information about the Rcpp-devel mailing list