[Rcpp-devel] Conversion of Rcpp::Vector Rcpp::List and Rcpp::Matrix to std objects - OpenMP

Simon Zehnder szehnder at uni-bonn.de
Sat Jun 1 14:05:44 CEST 2013


Hi Asis,

I'll write between the lines here:

On Jun 1, 2013, at 1:45 PM, Asis Hallab <asis.hallab at gmail.com> wrote:

> Dear Dirk and Simon,
> 
> thank you very much for your help.
> 
> I have made a few experiments and am very puzzled. Maybe I could
> bother you with advise on this?
> 
> The experiments on a
> #pragma omp parallel for
> 
> 1) Use private directive on all Rcpp-Objects that are only accessed to
> be read from, and a shared statement on the Rcpp List that collects
> the results. -> segfault

First, you do usually not need a private clause on objects you read from, but only objects you want to manipulate inside a thread. 
Second, I do not know how an Rcpp-List is embedded into memory, but it could be, that dependent on this memory structure,
one thread wants to write into a block, where another one is already writing. I would do the following: Try first using a matrix instead
of a list, here memory structure is more clear. Second, also try Armadillo objects instead of NumericMatrix. It could give a difference. 
For more input on the memory structure let us hope on a reply from the Rcpp-Devels.

> 2) Make all private -> runs fine, but returns an empty result list

That is clear, you let each thread now create its own result list and the result list outside the loop remains at the state you left it before starting
the loop. The list MUST be shared().

> 3) Use a std::map instead of the Rcpp List to collect the results ->
> endless loop telling me "****snapping into wrong generation"

Again, could be the same thing as with the RcppList - memory structure. You should be able to find a lot of about 
OpenMP and generic Lists (or more generally STL-containers) in the web, as C++ Gurus are much more into this than statistical programmers.

> 4) No private, nor shared directives and the above Rcpp List
> collecting the results works fine on small test data (3 loops), but on
> real live test data ( 1000 loops ) gives the above "****snapping into
> wrong generation"

Same as above. in the case of only 3 loops, the possibility for several threads writing into the same memory should be very low.
> 
> Do you have any idea, what I could do?
> I am still learning C++ - Maybe I should use references instead of
> Objects? But aren't SEXP pointers anyway?

I wouldn't do this, as references make things here just more complicated and it should work with objects (The pragma directive usually calls for private objects the appropriate 
copy constructor). Maybe you post the code here, and I have a look on it. 

One last question from my side: Did you use nested loops? Using this can sometimes result in undefined behavior. I had such cases before. 
> 
> Kind regards!

Hope it helps

Best

Simon


P.S. If I do not answer in the next hours, it is because I have to give some exams....I'll be back later.




More information about the Rcpp-devel mailing list