multithreading with R?
Solution 1:
You are confused.
The R (and before it, S) internals are single-threaded, and will almost surely remain single-threaded. As I understand it, Duncan Temple Lang's PhD work was about overcoming this, and if he can't do it...
That said, there are pockets of multi-threadedness:
First off, whenever you make external calls, and with proper locking, you can go multi-threaded. That is what the BLAS libraries MKL, Goto/Open BLAS, Atlas (if built multithreaded), ... all offer. Revo R "merely" ships with (Intel's) MKL as Intel happens to be a key Revo investor
If you are careful about what you do, you can use OpenMP (a compiler extension for multi-threading). This started with Luke Tierney's work on pnmath and pnmath0 (which used to be experimental / external packages) and has since been coming into R itself, slowly but surely.
Next, in a multicore world, and on the right operating system, you can always
fork()
. That is what package multicore pioneered and which package parallel now carries on.Last but not least there is the network / RPC route with MPI used by packages like Rmpi, snow, parallel, ... and covered in HPC introductions.
Solution 2:
Renjin
is an JVM based implementation of the interpreter. They claim that:
Unlike GNU R, Renjin is multithreaded and will run happily in a Platform-as-a-Service environment such as Google Appengine, AWS Elastic Beanstalk, Heroku or Microsoft Azure.
#resource
http://www.bedatadriven.com/products/renjin.html
Still, the actual R packages we would call from R may not be thread safe.
See Jep
documentation explaining this issue from standpoint of calling CPython
from Java/Scala
.
https://github.com/ninia/jep/wiki/How-Jep-Works#threading-complications
Due to complications and limitations of JNI, a thread that creates a Jep instance must be reused for all method calls to that Jep instance. Jep will enforce this and throw exceptions mentioning invalid thread access. (In the future we hope to simplify or provide utilities for thread management).
More than one Jep instance should not be run on the same thread at the same time. While this is technically allowed, it can potentially mess up the thread state and lead to deadlock in the Python interpreter. This will probably be changed to throw an exception if encountered in the future.
So, there seems to be hope with Renjin
but actual binary (C/C++, etc) packages used need to be verified for thread safety.
There are other R implementations
https://dynamicecology.wordpress.com/2014/01/14/r-isnt-just-r-anymore/