What sort of problems benefit from parallel computing

Writing Efficient R Code

Colin Gillespie

Jumping Rivers & Newcastle University

Cooking

An extra hand

Too many cooks

Too many cooks spoiling a broth

Writing Efficient R Code

Running in parallel

  • Not every analysis can make use of multiple cores
    • Many statistical algorithms can only use a single core

So where can parallel computing help?

Writing Efficient R Code

Monte-Carlo simulations

for(i in 1:8)
    sims[i] <- monte_carlo()
# User defined function
combine_simulations(sims)
  • 8 core machine
    • One simulation per core
    • Combine the results at the end
  • Embarrassingly parallel
Writing Efficient R Code

Not everything runs in parallel

x <- 1:8
for(i in 2:8)
    x[i] <- x[i-1]
x[8] = x[7] = ... x[2] = x[1] = 1
  • Can we run this in parallel?
    • NO
      • But order of evaluation in parallel computing can't be predicted
      • We'll get the wrong answer, since x[3]may get evaluated before x[2]
Writing Efficient R Code

Rule of thumb

Can the loop be run forward and backwards?

for(i in 1:8)
    sim[i] <- monte_carlo_simulation()

for(i in 8:1)
    sim[i] <- monte_carlo_simulation()
  • Both loops give the same result
  • So we can run the loops in parallel
Writing Efficient R Code

Rule of thumb

Can the loop be run forward and backwards?

x <- 1:8
for(i in 2:8)
    x[i] <- x[i-1]
for(i in 8:2)
    x[i] <- x[i-1]
  • The loops give different answers
    • The first: x[8] = x[7] = ... = 1
    • The second: x[8] = x[7] = 7
  • Can't use parallel computing

Remember: If you can run your loop in reverse, you can probably use parallel computing.

Writing Efficient R Code

Let's practice!

Writing Efficient R Code

Preparing Video For Download...