forked from MariekeDirk/ML_project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParallel.Rmd
More file actions
74 lines (58 loc) · 1.72 KB
/
Parallel.Rmd
File metadata and controls
74 lines (58 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
---
title: "Parallel"
author: "Eva Kleingeld"
date: "August 12, 2016"
output: pdf_document
---
This script contains the code to build/run a model in parallel AND top build/run a model in parallel on multiple computers.
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r }
#library(doParallel)
library(parallel)
library(doParallel)
library(foreach) #loop parallel
# foreach () %doPar%
cl<-makeCluster(4)
registerDoParallel(cl)
registerDoSEQ() ######### What does this function do?
#example
system.time(
t <-foreach(i=1:100000,combine=cbind) %dopar% { sqrt(i) }
)[3]
stopCluster(cl)
CloseC
## Normal run is faster?
system.time(
for (i in c(1:100000)) {
t[i] <- sqrt(i) })
library(doParallel)
library(foreach)
cl<-makeCluster(6)
registerDoParallel(cl)
#Meerdere computers
user <- 'Eva'
primary <- 'pc150169.knmi.nl' #number of your computer .knmi.nl
machineAddresses <- list(
list(host=primary,user=user,
ncore=4),
list(host= 'pc150395.knmi.nl',user=user,
ncore=1) #list all the computers you want to use with the number of cores
)
spec <- lapply(machineAddresses,
function(machine) {
rep(list(list(host=machine$host,
user=machine$user)),
machine$ncore)
})
spec <- unlist(spec,recursive=FALSE)
parallelCluster <- parallel::makeCluster(type='PSOCK',
master=primary,
spec=spec)
print(parallelCluster)
clusterEvalQ(parallelCluster, library(doParallel)) #To check wether packages are available
registerDoParallel(parallelCluster)
#Here room for some calculations with a foreach loop
stopCluster(parallelCluster)
```