How-to go parallel in R – basics + tips

Don’t waist another second, start parallelizing your computations today! The image is CC by  Smudge 9000
Don’t waist another second, start parallelizing your computations today! The image is CC by Smudge 9000

Today is a good day to start parallelizing your code. I’ve been using the parallel package since its integration with R (v. 2.14.0) and its much easier than it at first seems. In this post I’ll go through the basics for implementing parallel computations in R, cover a few common pitfalls, and give tips on how to avoid them.

The common motivation behind parallel computing is that something is taking too long time. For me that means any computation that takes more than 3 minutes – this because parallelization is incredibly simple and most tasks that take time are embarrassingly parallel. Here are a few common tasks that fit the description:

  • Bootstrapping
  • Cross-validation
  • Multivariate Imputation by Chained Equations (MICE)
  • Fitting multiple regression models

Learning lapply is key

One thing I regret is not learning earlier lapply. The function is beautiful in its simplicity: It takes one parameter (a vector/list), feeds that variable into the function, and returns a list:

You can feed it additional values by adding named parameters:

The tasks are embarrassingly parallel as the elements are calculated independently, i.e. second element is independent of the result from the first element. After learning to code using lapply you will find that parallelizing your code is a breeze.

The parallel package

The parallel package is basically about doing the above in parallel. The main difference is that we need to start with setting up a cluster, a collection of “workers” that will be doing the job. A good number of clusters is the numbers of available cores – 1. I’ve found that using all 8 cores on my machine will prevent me from doing anything else (the computers comes to a standstill until the R task has finished). I therefore always set up the cluster as follows:

Now we just call the parallel version of lapply, parLapply:

Once we are done we need to close the cluster so that resources such as memory are returned to the operating system.

Variable scope

On Mac/Linux you have the option of using makeCluster(no_core, type="FORK") that automatically contains all environment variables (more details on this below). On Windows you have to use the Parallel Socket Cluster (PSOCK) that starts out with only the base packages loaded (note that PSOCK is default on all systems). You should therefore always specify exactly what variables and libraries that you need for the parallel function to work, e.g. the following fails:

While this passes:

Note that you need the clusterExport(cl, "base") in order for the function to see the base variable. If you are using some special packages you will similarly need to load those through clusterEvalQ, e.g. I often use the rms package and I therefore use clusterEvalQ(cl, library(rms)). Note that any changes to the variable after clusterExport are ignored:

Using parSapply

Sometimes we only want to return a simple value and directly get it processed as a vector/matrix. The lapply version that does this is called sapply, thus it is hardly surprising that its parallel version is parSapply:

Matrix output with names (this is why we need the as.character):

The foreach package

The idea behind the foreach package is to create ‘a hybrid of the standard for loop and lapply function’ and its ease of use has made it rather popular. The set-up is slightly different, you need “register” the cluster as below:

Note that you can change the last two lines to:

But then you need to remember to instead of stopCluster() at the end do:

The foreach function can be viewed as being a more controlled version of the parSapply that allows combining the results into a suitable format. By specifying the .combine argument we can choose how to combine our results, below is a vector, matrix, and a list example:

Note that the last is the default and can be achieved without any tweaking, just foreach(exponent = 2:4) %dopar%. In the example it is worth noting the .multicombine argument that is needed to avoid a nested list. The nesting occurs due to the sequential .combine function calls, i.e. list(list(result.1, result.2), result.3):

Variable scope

The variable scope constraints are slightly different for the foreach package. Variable within the same local environment are by default available:

While variables from a parent environment will not be available, i.e. the following will throw an error:

A nice feature is that you can use the .export option instead of the clusterExport. Note that as it is part of the parallel call it will have the latest version of the variable, i.e. the following change in “base” will work:

Similarly you can load packages with the .packages option, e.g. .packages = c("rms", "mice"). I strongly recommend always exporting the variables you need as it limits issues that arise when encapsulating the code within functions.

Fork or sock?

I do most of my analyses on Windows and have therefore gotten used to the PSOCK system. For those of you on other systems you should be aware of some important differences between the two main alternatives:

FORK: "to divide in branches and go separate ways"
Systems: Unix/Mac (not Windows)
Environment: Link all

PSOCK: Parallel Socket Cluster
Systems: All (including Windows)
Environment: Empty

Memory handling

Unless you are using multiple computers or Windows or planning on sharing your code with someone using a Windows machine, you should try to use FORK (I use capitalized due to the makeCluster type argument). It is leaner on the memory usage by linking to the same address space. Below you can see that the memory address space for variables exported to PSOCK are not the same as the original:

While they are for FORK clusters:

This can save a lot of time during setup and also memory. Interestingly, you do not need to worry about variable corruption:


Debugging is especially hard when working in a parallelized environment. You cannot simply call browser/cat/print in order to find out what the issue is.

The tryCatchlist approach

Using stop() for debugging without modification is generally a bad idea; while you will receive the error message, there is a large chance that you have forgotten about that stop(), and it gets evoked once you have run your software for a day or two. It is annoying to throw away all the previous successful computations just because one failed (yup, this is default behavior of all the above functions). You should therefore try to catch errors and return a text explaining the setting that caused the error:

This is also why I like lists, the .combine may look appealing but it is easy to manually apply and if you have function that crashes when one of the element is not of the expected type you will loose all your data. Here is a simple example of how to call rbind on a lapply output:

Creating a common output file

Since we can’t have a console per worker we can set a shared file. I would say that this is a “last resort” solution:

As you can see due to a race between first and the second node the output is a little garbled and therefore in my opinion less useful than returning a custom statement.

Creating a node-specific file

A perhaps slightly more appealing alternative is to a have a node-specific file. This could potentially be interesting when you have a dataset that is causing some issues and you want to have a closer look at that data set:

A tip is to combine this with your tryCatchlist approach. Thereby you can extract any data that is not suitable for a simple message (e.g. a large data.frame), load that, and debug it without parallel. If the x is too long for a file name I suggest that you use digest as described below for the cache function.

The partools package

There is an interesting package partools that has a dbs() function that may be worth looking into (unless your on a Windows machine). It allows coupling terminals per process and debugging through them.


I strongly recommend implementing some caching when doing large computations. There may be a multitude of reasons to why you need to exit a computation and it would be a pity to waist all that valuable time. There is a package for caching, R.cache, but I’ve found it easier to write the function myself. All you need is the built-in digest package. By feeding the data + the function that you are using to the digest() you get an unique key, if that key matches your previous calculation there is no need for re-running that particular section. Here is a function with caching:

The when running the code it is pretty obvious that the Sys.sleep is not invoked the second time around:

Load balancing

Balancing so that the cores have similar weight load and don’t fight for memory resources is core for a successful parallelization scheme.

Work load

Note that the parLapply and foreach are wrapper functions. This means that they are not directly doing the processing the parallel code, but rely on other functions for this. In the parLapply the function is defined as:

Note the splitList(X, length(cl)). This will split the tasks into even portions and send them onto the workers. If you have many of those cached or there is a big computational difference between the tasks you risk ending up with only one cluster actually working while the others are inactive. To avoid this you should when caching try to remove those that are cached from the X or try to mix everything into an even workload. E.g. if we want to find optimal number of neurons in a neural network we may want to change:


Memory load

Running large datasets in parallel can quickly get you into trouble. If you run out of memory the system will either crash or run incredibly slow. The former happens to me on Linux systems while the latter is quite common on Windows systems. You should therefore always monitor your parallelization to make sure that you aren’t too close to the memory ceiling.

Using FORKs is an important tool for handling memory ceilings. As they link to the original variable address the fork will not require any time for exporting variables or take up any additional space when using these. The impact on performance can be significant (my system has 16Gb of memory and eight cores):

FORKs can also make your able to run code in parallel that otherwise crashes:

Although, it won’t save you from yourself 😀 as you can see below when we create an intermediate variable that takes up storage space:

Memory tips

  • Frequently use rm() in order to avoid having unused variables around
  • Frequently call the garbage collector gc(). Although this should be implemented automatically in R, I’ve found that while it may releases the memory locally it may not return it to the operating system (OS). This makes sense when running at a single instance as this is an time expensive procedure but if you have multiple processes this may not be a good strategy. Each process needs to get their memory from the OS and it is therefore vital that each process returns memory once they no longer need it.
  • Although it is often better to parallelize at a large scale due to initialization costs it may in memory situations be better to parallelize at a small scale, i.e. in subroutines.
  • I sometimes run code in parallel, cache the results, and once I reach the limit I change to sequential.
  • You can also manually limit the number of cores, using all the cores is of no use if the memory isn’t large enough. A simple way to think of it is: memory.limit()/memory.size() = max cores

Other tips

  • A general core detector function that I often use is:

  • Never use set.seed(), use clusterSetRNGStream() instead, to set the cluster seed if you want reproducible results
  • If you have a Nvidia GPU-card, you can get huge gains from micro-parallelization through the gputools package (Warning though, the installation can be rather difficult…).
  • When using mice in parallel remember to use ibind() for combining the imputations.

Flattr this!

This entry was posted in R, Tutorial and tagged , . Bookmark the permalink.

15 Responses to How-to go parallel in R – basics + tips

  1. Markus Senn says:

    Hi Max,

    Thanks again for your presentation at the Meetup.

    Just to let you know: you convinced me. I started using parallel in R and I’m liking it! This blog post is really helpful as a reference.


  2. Shaun Ryan says:


    I have 4 cores. I set it to 3 but it still used all 4 (I can send the screen shot if no one believes me). I looked at the Windows Task Manager and can clearly see all 4 cores spiking due to the R call.
    Am I misunderstanding something?


    • Shaun Ryan says:

      having looked into this further. Altering the core number has some effect but it doesn’t look like a clean affinity partitioning. I think it’s because they’re logical cores maybe and not physical cores and therefore there might be some lower level affinity masking going on to decide where to run higher parallel stuff. If I switch it to 1 it definitely only runs on 1 core and it’s comparatively very slow (I tuned up the work load a little than in your example)

  3. jn says:

    Excellent post! Thank you for your great work.

  4. Paul Johnson says:

    This is very helpful. I’ve written notes like this for classes, but never as good as this set. Way to go.

  5. Jessica says:

    I am a relatively novice R user, and absolute novice at any sort of parallel computing. I am desperately trying to figure out how to use an extant r package (specifically I’m using LQMM) in parallel mode. I see lots of examples where you can write your own functions, but I have yet to find an example of combining parallel and other packages. Any advice?

    • Max Gordon says:

      Unfortunately it is difficult to go parallel without changing the core code – you will most likely need to change the source and adapt those parts suitable for parallel execution. I would recommend avoiding this path if your new to R.

      PS. Sorry for the late answer.

  6. Sérgio Casca says:

    Great post! I’ve just started using parallel computing in R and your post has been really helpful.


  7. Juanmi says:

    Hello, very didactic and useful post. Thank you!!!

  8. Great stuff. One extra tip is that when you makeCluster(), if you specify outfile=”” you will be able to see the printed output from parallel workers without needing a separate text file – it will go right to the console. There is an example here:

  9. Pingback: Parallel Programming In R | loneharoon

  10. Pingback: R with Parallel Computing from User Perspectives – ParallelR

  11. Pingback: Better Confidence Intervals for Quantiles – Mubashir Qasim

  12. Pingback: How to make R use all processors? - ExceptionsHub

Leave a Reply to jn Cancel reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.