Book Image

Mastering Parallel Programming with R

By : Simon R. Chapple, Terence Sloan, Thorsten Forster, Eilidh Troup
Book Image

Mastering Parallel Programming with R

By: Simon R. Chapple, Terence Sloan, Thorsten Forster, Eilidh Troup

Overview of this book

R is one of the most popular programming languages used in data science. Applying R to big data and complex analytic tasks requires the harnessing of scalable compute resources. Mastering Parallel Programming with R presents a comprehensive and practical treatise on how to build highly scalable and efficient algorithms in R. It will teach you a variety of parallelization techniques, from simple use of R’s built-in parallel package versions of lapply(), to high-level AWS cloud-based Hadoop and Apache Spark frameworks. It will also teach you low level scalable parallel programming using RMPI and pbdMPI for message passing, applicable to clusters and supercomputers, and how to exploit thousand-fold simple processor GPUs through ROpenCL. By the end of the book, you will understand the factors that influence parallel efficiency, including assessing code performance and implementing load balancing; pitfalls to avoid, including deadlock and numerical instability issues; how to structure your code and data for the most appropriate type of parallelism for your problem domain; and how to extract the maximum performance from your R code running on a variety of computer systems.
Table of Contents (13 chapters)

Numerical approximation


Let's have a little fun!

Question: What do you get in R if you sum 1 with successive fractions 1/2, 1/3, 1/4, and so on all the way up to 1/500000th? Well, let's take a look….

Here's some simple code that sets up the vector of fractions:

v <- 1:500000
for (i in 1:length(v))
{
    v[i] = 1/i
}
> v[1]
[1] 1
> v[2]
[1] 0.5
> v[3]
[1] 0.3333333
> v[500000]
[1] 2e-06

And now, let's explicitly sum all the elements in the vector:

suma <- 0.0
for (i in 1:length(v))
{
    suma = suma + v[i]
}
> suma
[1] 13.69958

This seems fine. So, let's take a look at what happens if we add the numbers up in reverse:

sumz <- 0.0
for (i in length(v):1)
{
    sumz = sumz + v[i]
}
> sumz
[1] 13.69958

Great, the same answer; it's all good, move along, nothing to see here….

Um, yeah, actually; let's take a closer look:

> print(suma,digits=15)
[1] 13.6995800423056
> print(sumz,digits=15)
[1] 13.6995800423055

Err- Houston…?

What happens if we try with fractions up to...