Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple OpenCL example in R with R code?

Tags:

r

opencl

Is it possible to use OpenCL but with R code? I still don't have a good understanding of OpenCL and GPU programming. For example, suppose I have the following R code:

aaa <- function(x) mean(rnorm(1000000))

sapply(1:10, aaa)

I like that I can kind of use mclapply as a dropin replacement for lapply. Is there a way to do that for OpenCL? Or to use OpenCL as a backend for mclapply? I'm guessing this is not possible because I have not been able to find an example, so I have two questions:

  1. Is this possible and if so can you give a complete example using my function aaa above?
  2. If this is not possible, can you please explain why? I do not know much about GPU programming. I view GPU just like CPUs, so why cannot I run R code in parallel?
like image 357
Xu Wang Avatar asked Jul 19 '15 01:07

Xu Wang


2 Answers

I would start by looking at the High Performance Computing CRAN task view, in particular the Parallel computing: GPUs section.

There are a number of packages listed there which take advantage of GPGPU for specific tasks that lend themselves to massive parallelisation (e.g. gputools, HiPLARM). Most of these use NVIDIA's own CUDA rather than OpenCL.

There is also a more generic OpenCL package, but it requires you to learn how to write OpenCL code yourself, and merely provides an interface to that code from R.

like image 154
Nick Kennedy Avatar answered Sep 20 '22 15:09

Nick Kennedy


It isn't possible because GPUs work differently than CPUs which means you can't give them the same instructions that you'd give a CPU.

Nvidia puts on a good show with this video of describing the difference between CPU and GPU processing. Essentially the difference is that GPUs typically have, by orders of magnitude, more cores than CPUs.

Your example is one that can be extended to GPU code because it is highly parallel.

Here's some code to create random numbers (although they aren't normally distributed) http://cas.ee.ic.ac.uk/people/dt10/research/rngs-gpu-mwc64x.html

Once you create the random numbers you could break them into chunks and then sum each of the chunks in parallel and then add the sums of the chunks to get the overall sum Is it possible to run the sum computation in parallel in OpenCL?

I realize that your code would make the random number vector and its sum in serial and parallel that operation 10 times but with GPU processing, having a mere 10 tasks isn't very efficient since you'd leave so many cores idle.

like image 23
Dean MacGregor Avatar answered Sep 20 '22 15:09

Dean MacGregor