# Week 9

## Lab 7

Open lab

#### Objectives

• Complete Tasks 1 and 2 for Homework 5
• Refactor your get_* and parse_* scripts

## RShiny part II

#### Learning objectives

• Understand the reactive programming model used by Shiny
• Create a better user experience by improving your code’s quality

#### Materials

Bank loan simulation app

## Parallelization

#### Learning objectives

• Benchmarking code
• Forking versus sockets
• Core functions in package parallel

#### Materials

• Slides: HTML, Rmd
• Supplementary
• Package parallel vignette by R-core
• Beyond Single-Core R slides by Jonathan Dursi
• Getting started with doMC and foreach vignette by Steve Weston

## Exercise of the week

1. Use trigonometry2 to compare parallelizing via forking and sockets. Use 1, 2, 4, 8, and 16 cores to evaluate the computation time for a simple task. What is the difference overhead cost between forking and using sockets?

2. Do you notice anything strange with result2 and result4? What is going on?
library(parallel)
library(magrittr)
detectCores()

result2 <- mclapply(1:12, FUN = function(x) rnorm(1),
mc.cores = 2, mc.set.seed = FALSE) %>%
unlist()
result2
## [1]  0.32006303  0.32006303 -1.01143576 -1.01143576  0.06824854  0.06824854
## [7]  2.09764952  2.09764952  1.30633116  1.30633116  0.92467119  0.92467119
result4 <- mclapply(1:12, FUN = function(x) rnorm(1),
mc.cores = 4, mc.set.seed = FALSE) %>%
unlist()
result4
## [1]  0.32006303  0.32006303  0.32006303  0.32006303 -1.01143576 -1.01143576
## [7]  -1.01143576 -1.01143576  0.06824854  0.06824854  0.06824854  0.06824854
Previous
Next