From edd at debian.org  Sun Feb  1 01:05:43 2015
From: edd at debian.org (Dirk Eddelbuettel)
Date: Sat, 31 Jan 2015 18:05:43 -0600
Subject: [Rd] invoking R function in C++ in parallel
In-Reply-To: <CAFLer83nzRs1cV7Cpu7VWUEDrqaSC7kjrUBbMUHzaFAp97DQGA@mail.gmail.com>
References: <CAFLer83nzRs1cV7Cpu7VWUEDrqaSC7kjrUBbMUHzaFAp97DQGA@mail.gmail.com>
Message-ID: <21709.28119.188963.170738@max.nulle.part>


On 31 January 2015 at 18:30, Zheng Da wrote:
| I'm writing an R extension in C++. In the extension, I want to invoke
| an R function directly and it works when I use Rcpp in the serial
| code. But what I really want is to invoke the R function in parallel
| with openmp. When I do so, I got segmentation fault.
| 
| I remember someone said that R isn't thread-safe. I think it's also
| understandable because an R function also has environment variables.
| 
| My question is:
| Can we make a copy of an R function (including its environment
| variables) for each thread?

Nope. And you pretty much just explained why.

You can 
 
 - either work in something like OpenMP and run in multiple threads that
   remain /completely/ shielded from R, ie no R calls, and not even R data
   types as you cannot trigger gc() calls from different threads

 - or work via, say, the parallel or Rmpi packages in multiple _processes_
   each of which could call its corresponding R interpreter (but that is
   still slower).

There are some examples for OpenMP at the Rcpp Gallery: http://gallery.rcpp.org.
You may also enjoy the RcppParallel package.  

But none of this overcomes your main hurdle: no, you cannot call R from
different threads.

| Are there any ways to invoke an R function in C/C++ in multiple threads safely?

No.  It is a design constraint.

Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org



[Rd] invoking R function in C++ in parallel
Dirk Eddelbuettel