library(SimDesign)
Design <- createDesign(N = c(10, 20, 30))
Generate <- function(condition, fixed_objects = NULL) {
dat <- with(condition, rnorm(N, 10, 5)) # distributed N(10, 5)
dat
}
Analyse <- function(condition, dat, fixed_objects = NULL) {
ret <- c(mean=mean(dat), median=median(dat)) # mean/median of sample data
ret
}
Summarise <- function(condition, results, fixed_objects = NULL){
colMeans(results)
}
if (FALSE) {
# define initial seed (do this only once to keep it constant!)
# iseed <- gen_seeds()
iseed <- 554184288
### On cluster submission, the active array ID is obtained via getArrayID(),
### and therefore should be used in real SLURM submissions
arrayID <- getArrayID(type = 'slurm')
# However, for the following example array ID is set to first row only
arrayID <- 1L
# run the simulation (results not caught on job submission, only files saved)
res <- runArraySimulation(design=Design, replications=50,
generate=Generate, analyse=Analyse,
summarise=Summarise, arrayID=arrayID,
iseed=iseed, filename='mysim') # saved as 'mysim-1.rds'
res
SimResults(res) # condition and replication count stored
dir()
SimClean('mysim-1.rds')
########################
# Same submission job as above, however split the replications over multiple
# evaluations and combine when complete
Design5 <- expandDesign(Design, 5)
Design5
# iseed <- gen_seeds()
iseed <- 554184288
# arrayID <- getArrayID(type = 'slurm')
arrayID <- 14L
# run the simulation (replications reduced per row, but same in total)
runArraySimulation(design=Design5, replications=10,
generate=Generate, analyse=Analyse,
summarise=Summarise, iseed=iseed,
filename='mylongsim', arrayID=arrayID)
res <- readRDS('mylongsim-14.rds')
res
SimResults(res) # condition and replication count stored
SimClean('mylongsim-14.rds')
###
# Emulate the arrayID distribution, storing all results in a 'sim/' folder
dir.create('sim/')
# Emulate distribution to nrow(Design5) = 15 independent job arrays
## (just used for presentation purposes on local computer)
sapply(1:nrow(Design5), \(arrayID)
runArraySimulation(design=Design5, replications=10,
generate=Generate, analyse=Analyse,
summarise=Summarise, iseed=iseed, arrayID=arrayID,
filename='condition', dirname='sim', # files: "sim/condition-#.rds"
control = list(max_time="04:00:00", max_RAM="4GB"))) |> invisible()
# If necessary, conditions above will manually terminate before
# 4 hours and 4GB of RAM are used, returning any
# successfully completed results before the HPC session times
# out (provided .slurm script specified more than 4 hours)
# list saved files
dir('sim/')
setwd('sim')
condition14 <- readRDS('condition-14.rds')
condition14
SimResults(condition14)
# aggregate simulation results into single file
final <- aggregate_simulations(files=dir())
final
SimResults(final) |> View()
setwd('..')
SimClean(dirs='sim/')
}
Run the code above in your browser using DataLab