# NOT RUN {
### Example 1: Using memory measurement
tmp = makeRegistry(file.dir = NA, make.default = FALSE)
# Toy function which creates a large matrix and returns the column sums
fun = function(n, p) colMeans(matrix(runif(n*p), n, p))
# Arguments to fun:
args = CJ(n = c(1e4, 1e5), p = c(10, 50)) # like expand.grid()
print(args)
# Map function to create jobs
ids = batchMap(fun, args = args, reg = tmp)
# Set resources: enable memory measurement
res = list(measure.memory = TRUE)
# Submit jobs using the currently configured cluster functions
submitJobs(ids, resources = res, reg = tmp)
# Retrive information about memory, combine with parameters
info = ijoin(getJobStatus(reg = tmp)[, .(job.id, mem.used)], getJobPars(reg = tmp))
print(unwrap(info))
# Combine job info with results -> each job is aggregated using mean()
unwrap(ijoin(info, reduceResultsDataTable(fun = function(res) list(res = mean(res)), reg = tmp)))
### Example 2: Multicore execution on the slave
tmp = makeRegistry(file.dir = NA, make.default = FALSE)
# Function which sleeps 10 seconds, i-times
f = function(i) {
parallelMap::parallelMap(Sys.sleep, rep(10, i))
}
# Create one job with parameter i=4
ids = batchMap(f, i = 4, reg = tmp)
# Set resources: Use parallelMap in multicore mode with 4 CPUs
# batchtools internally loads the namespace of parallelMap and then
# calls parallelStart() before the job and parallelStop() right
# after the job last job in the chunk terminated.
res = list(pm.backend = "multicore", ncpus = 4)
# }
# NOT RUN {
# Submit both jobs and wait for them
submitJobs(resources = res, reg = tmp)
waitForJobs(reg = tmp)
# If successfull, the running time should be ~10s
getJobTable(reg = tmp)[, .(job.id, time.running)]
# There should also be a note in the log:
grepLogs(pattern = "parallelMap", reg = tmp)
# }
Run the code above in your browser using DataLab