Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multithreading with R?

Tags:

r

Reading the R-project website, there are some (unclear) references to multithreading with R, but it is unclear how the base product and CRAN libraries are compiled.

Revolution Analytics offers multithreaded base(?) download for Windows and Redhat.

Would some of the other Linux distributions also include multithreaded R (and packages)?

like image 488
gliptak Avatar asked May 31 '12 13:05

gliptak


People also ask

Is Rstudio single-threaded?

Since the R interpreter is single-threaded, one must not check for user interruptions or print to the R console from multiple threads. One can, however, synchronize with R from the main thread. The R package RcppThread (current version 1.0.

What is multithreading vs multiprocessing?

By formal definition, multithreading refers to the ability of a processor to execute multiple threads concurrently, where each thread runs a process. Whereas multiprocessing refers to the ability of a system to run multiple processors concurrently, where each processor can run one or more threads.

Is multithreading possible in python?

Python doesn't support multi-threading because Python on the Cpython interpreter does not support true multi-core execution via multithreading. However, Python does have a threading library. The GIL does not prevent threading.

Is foreach multithreaded Java?

forEach() and parallel foreach() is the multithreading feature given in the parallel forEach(). This is way faster that foreach() and stream.


2 Answers

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.

like image 83
Dirk Eddelbuettel Avatar answered Sep 21 '22 16:09

Dirk Eddelbuettel


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/

like image 23
SemanticBeeng Avatar answered Sep 23 '22 16:09

SemanticBeeng