1 | library(doParallel) |
---|

2 | # Enable command line arguments |
---|

3 | args<-commandArgs(TRUE) |
---|

4 | |
---|

5 | # use the environment variable SLURM_NTASKS_PER_NODE to set the number of cores |
---|

6 | registerDoParallel(cores=(as.integer(args[1]))) |
---|

7 | |
---|

8 | # Bootstrapping iteration example |
---|

9 | x <- iris[which(iris[,5] != "setosa"), c(1,5)] |
---|

10 | iterations <- 10000# Number of iterations to run |
---|

11 | |
---|

12 | # Parallel version of code |
---|

13 | # Note the '%dopar%' instruction |
---|

14 | part <- system.time({ |
---|

15 | r <- foreach(icount(iterations), .combine=cbind) %dopar% { |
---|

16 | ind <- sample(100, 100, replace=TRUE) |
---|

17 | result1 <- glm(x[ind,2]~x[ind,1], family=binomial(logit)) |
---|

18 | coefficients(result1) |
---|

19 | } |
---|

20 | })[3] |
---|

21 | |
---|

22 | # Shows the number of Parallel Workers to be used |
---|

23 | getDoParWorkers() |
---|

24 | # Executes the functions |
---|

25 | part |
---|