Skip to content

Instantly share code, notes, and snippets.

@xhdong-umd
Last active July 14, 2017 23:33
Show Gist options
  • Save xhdong-umd/473b82c25720fe86d1a4a491b9b55c6e to your computer and use it in GitHub Desktop.
Save xhdong-umd/473b82c25720fe86d1a4a491b9b55c6e to your computer and use it in GitHub Desktop.
test parallel
---
title: "test parallel"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
if (!require("pacman")) install.packages("pacman")
pacman::p_load(ctmm, data.table, parallel)
data("buffalo")
# hyperthread may not always available, like in VM
cores <- detectCores(logical = FALSE)
cores_logical <- detectCores()
population <- 10
subset_size <- 300 # make sure population * subset_size < total size
# prepare data and function
animal_1 <- buffalo[[1]]
# create a chunks. note random sample is not good since we are deal with a time series movement
chunks <- lapply(1:population, function(i) {
dt <- data.table(data.frame(animal_1[(1 + subset_size * (i - 1)):(
subset_size * i), ]))
dt[, timestamp := as.character(timestamp)]
as.telemetry(dt)
})
test <- function(x) {
guessed <- ctmm.guess(x, interactive = FALSE)
ctmm.select(x, CTMM = guessed)
}
```
## intro
This is used to test parallel options in various platform.
The time needed for processing one task, all task in serial mode.
```{r baseline}
system.time(test(chunks[[1]]))
system.time(lapply(chunks, test))
```
### mclapply
doesn't work in windows
```{r mclapply, eval=FALSE}
# don't need explict cluster setup, also no export
invisible(lapply(cores:cores_logical, function(cores) {
print(cores)
print(system.time(mclapply(chunks, test, mc.cores = cores)))
}))
```
### socket cluster
```{r sock cluster}
invisible(lapply(c(cores, cores + 1, cores_logical, 10), function(cores) {
cl <- makeCluster(cores)
print(cores)
print(system.time({
clusterEvalQ(cl, library(ctmm))
# this don't need to be exported, since it's in parameter
# clusterExport(cl, c("test"))
parLapplyLB(cl, chunks, test)}))
stopCluster(cl)
}))
```
### fork cluster
not work in windows
```{r fork cluster, eval=FALSE}
invisible(lapply(c(cores, cores + 1, cores_logical, 10), function(cores) {
cl <- makeCluster(cores, type = "FORK")
print(cores)
print(system.time({
clusterEvalQ(cl, library(ctmm))
parLapplyLB(cl, chunks, test)}))
stopCluster(cl)
}))
```
## auto detection
```{r}
os <- Sys.info()[['sysname']]
cores <- detectCores(logical = FALSE)
cores_logical <- detectCores()
cat(os, "with", cores, "physical cores,", cores_logical, "threads", "detected\n")
if (os == "Windows") {
} else {
}
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment