Justace Clutter
Justace Clutter

Reputation: 2186

RCppParallel Programming Error 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 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 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:

Upvotes: 2

Views: 585

Answers (1)

Justace Clutter
Justace Clutter

Reputation: 2186

After cross-posting this question on the rcpp-devel list, a user responded and infomed me that my loop over j in the operator() method should go between begin <= j < end and not begin <= j <= end which is what I had.

I made that change and sure nuff, everything seems to be working right now.

seems like overextending ones reach past allocated memory spaces still results in unintended consequences...

Upvotes: 2

Related Questions