multicore and gputools are two R packages that allow for some limited "quick & dirty" parallel processing from within R. Using both packages I was able to exploit my Macbook's dual core processor and built in GPU to reduce run times for certain computationally intensive tasks. This is a brief wiki on how go about setting them up.
There are various packages out there, most notably snow
and it's wrapper snowfall,
that enable feature-rich parallel computing in R. In contrast we will only focus on the relatively simple multicore
package which essentially makes use of multiple cores through a single
function mclapply.
This function is a parallelized version of lapply.
Getting started with multicore
is quite simple, just install the package as usual. However,
there is no Windows binary version, so if you want to use the package
in Windows you have to build from source on a PC, something I cannot help you with.
Calling mclapply is easy. Consider the following example of calculating the distance matrix
of 2^10 vectors each of length 2^10.
> set.seed(5446)
> p <- 20
> X <- matrix(rnorm(2^p),ncol = 2^(p/2))
>
> lfun <- function(i){
+ d <- dist(X)
+ }
>
> system.time(res <- lapply(1:4,lfun))
user system elapsed
80.062 0.199 81.204
> system.time(mcres <- mclapply(1:4,lfun))
user system elapsed
57.896 0.294 61.507
As you can see, computing time is reduced by roughly 25% for this
particular task. It goes without saying there are a few more
options in mclapply, but in general this is how it works with multicore. Consult the package documentation for further details.
The R package gputools provides several functions for fast computation on GPUs, most of them are wrappers for CUBLAS. One can drastically improve R computation times using the GPU optimized functions in gputools. Alternitavely, one can work directly in C++ utilizing the CUBLAS libraries and NVIDIA's CUDA toolkit. Go here for lots of information on CUDA and NVIDIA's tools for GPU computing.
Installed gcc C compiler and toolchain via Xcode. Installing Xcode installs gcc and toolchain automatically. If you are running Lion (OS 10.7.x), Xcode 4 is available as a free download from the Apple's app store. Since I have Leopard (OS 10.6.8) I had to first register on Apple's developer site, then was able to access a free download of Xcode 3. Xcode is large (>14 Gb) but once downloaded and installed, I was able to verify the installation of gcc using the command /usr/bin/gcc --help in the terminal.
library(gputools)
set.seed(5446)
p <- 20
X <- matrix(rnorm(2^p),ncol = 2^(p/2))
system.time(d <- dist(X))
system.time(d <- gpuDist(X))
> system.time(d <- dist(X))
user system elapsed
23.432 0.032 23.529
> system.time(gpud <- gpuDist(X))
user system elapsed
1.255 0.040 2.737
> max(abs(c(d) - c(gpud)))
[1] 9.710027e-06