Utilizing multiple cores in R

[This article was first published on Recipes, scripts and genomics, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

There are a couple of options in R, if you want to utilize multiple cores on your machine. These days my favorite is doMC package, which depends on foreach and multicore packages.

in the section below squareroot for each number is calculated in parallel. Check the vignette for more complicated example. In practice, if you need to iterate through a large data structure and there is no escape from that, this package makes things considerably faster depending on how many cores you have access to in your machine.

> library(doMC)
> registerDoMC() 
> foreach(i = 1:3) %dopar% sqrt(i)
 
[[1]] 
[1] 1
[[2]] 
[1] 1.414214 
[[3]] 
[1] 1.732051


you can also choose how the resulting data structure is combined

> library(doMC) 
> registerDoMC() 
> foreach(i = 1:3,.combine="rbind") %dopar% sqrt(i)
 
             [,1]
result.1 1.000000
result.2 1.414214
result.3 1.732051 

To leave a comment for the author, please follow the link and comment on their blog: Recipes, scripts and genomics.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)