Skip to content

Commit 2695231

Browse files
author
Samuel Jenness
committed
Update netsim_hpc for checkpointing
* Allow a cp.save.int of NULL to avoid creating/saving CP data * Internal reorganization of code for better clarity
1 parent 84a641a commit 2695231

File tree

1 file changed

+65
-48
lines changed

1 file changed

+65
-48
lines changed

R/netsim_hpc.R

Lines changed: 65 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111
#' @param param Model parameters, as an object of class \code{param.net}.
1212
#' @param init Initial conditions, as an object of class \code{init.net}.
1313
#' @param control Control settings, as an object of class \code{control.net}.
14-
#' @param cp.save.int Checkpointing save interval.
14+
#' @param cp.save.int Check-pointing save interval, which is used to specify how
15+
#' often intermediate data should be saved out to disk. When a job has been
16+
#' check-pointed, it will resume automatically at the last saved time
17+
#' step stored on disk. If set to \code{NULL}, then no intermediate data
18+
#' storage will occur.
1519
#' @param save.min Argument passed to \code{\link{savesim}}.
1620
#' @param save.max Argument passed to \code{\link{savesim}}.
1721
#' @param compress Matches the \code{compress} argument for the \code{\link{save}}
@@ -34,7 +38,8 @@
3438
#' \code{\link{check_cp}} function. If CP data are available, a
3539
#' checkpointed model will be run, else a new model will be run.
3640
#' \item Create a checkpoint directory if one does not exist at
37-
#' "data/sim<simno>".
41+
#' "data/sim<simno>". This and the related checkpointing functions will
42+
#' not occur if \code{cp.save.int} is set to \code{NULL}.
3843
#' \item Sets the checkpoint save interval at the number of time steps specified
3944
#' in \code{cp.save.int}.
4045
#' \item Resets the initialize module function to \code{\link{initialize_cp}}
@@ -45,7 +50,8 @@
4550
#' \code{\link{savesim}}.
4651
#' \item Remove any files in the "verb/" subdirectory, which is typically
4752
#' used to store incremental model tracking text files.
48-
#' \item Remove the checkpointed data and file directory created in step 1.
53+
#' \item Remove the checkpointed data and file directory created in step 1, if
54+
#' it exists.
4955
#' }
5056
#'
5157
#' The \code{x} argument must specify a \strong{file name} in a character string,
@@ -57,7 +63,7 @@
5763
#'
5864
#' @export
5965
netsim_hpc <- function(x, param, init, control,
60-
cp.save.int = 100,
66+
cp.save.int = NULL,
6167
save.min = TRUE,
6268
save.max = FALSE,
6369
compress = TRUE,
@@ -82,65 +88,72 @@ netsim_hpc <- function(x, param, init, control,
8288
x <- cpDir
8389
}
8490

85-
# Creates CP directory
91+
# New simulations ---------------------------------------------------------
92+
8693
if (type == "new") {
87-
dirname <- paste0("data/sim", control$simno)
88-
if (file.exists("data/") == FALSE) {
89-
dir.create("data/")
94+
95+
if (verbose == TRUE) {
96+
cat("\nSTARTING Simulation ", control$simno, sep = "")
9097
}
91-
if (file.exists(dirname) == FALSE) {
92-
dir.create(dirname)
98+
99+
# Set CP save interval if missing
100+
if (is.null(control$save.int) & !is.null(cp.save.int)) {
101+
if (verbose == TRUE) {
102+
cat("\nSetting save.int on control settings at", cp.save.int, "time steps ... ")
103+
}
104+
control$save.int <- cp.save.int
93105
}
94-
}
95-
96-
if (type == "new" & verbose == TRUE) {
97-
cat("\nSTARTING Simulation ", control$simno, sep = "")
98-
}
99-
100-
# Set CP save interval if missing
101-
if (is.null(control$save.int)) {
102-
if (verbose == TRUE) {
103-
cat("\nSetting save.int on control settings at", cp.save.int, "time steps ... ")
106+
107+
# Store save CP on control settings
108+
if (is.null(control$savedata.FUN) & !is.null(control$save.int)) {
109+
control$savedata.FUN <- save_cpdata
110+
control$bi.mods <- c(control$bi.mods, "savedata.FUN")
104111
}
105-
control$save.int <- cp.save.int
112+
113+
# Creates CP directory
114+
if (!is.null(control$save.int)) {
115+
dirname <- paste0("data/sim", control$simno)
116+
if (file.exists("data/") == FALSE) {
117+
dir.create("data/")
118+
}
119+
if (file.exists(dirname) == FALSE) {
120+
dir.create(dirname)
121+
}
122+
}
123+
124+
# Run a new simulation
125+
if (type == "new") {
126+
load(x)
127+
if ("sim" %in% ls()) {
128+
assign("est", sim)
129+
}
130+
if (verbose == TRUE) {
131+
cat("\nRunning new simulation from", class(est), "object ...")
132+
}
133+
sim <- netsim(est, param, init, control)
134+
}
135+
106136
}
137+
107138

108-
# Store save CP on control settings
109-
if (is.null(control$savedata.FUN)) {
110-
control$savedata.FUN <- save_cpdata
111-
control$bi.mods <- c(control$bi.mods, "savedata.FUN")
112-
}
139+
# CP resumed simulations --------------------------------------------------
113140

114-
# Replace initialization module if CP
115141
if (type == "cp") {
142+
143+
# Replace initialization module
116144
control$initialize.FUN <- initialize_cp
117145
control$skip.check <- TRUE
118-
}
119-
120-
# Run a new simulation
121-
if (type == "new") {
122-
load(x)
123-
if ("sim" %in% ls()) {
124-
assign("est", sim)
125-
}
126-
if (verbose == TRUE) {
127-
cat("\nRunning new simulation from", class(est), "object ...")
128-
}
129-
sim <- netsim(est, param, init, control)
130-
}
131-
132-
# Run a checkpointed simulation
133-
if (type == "cp") {
146+
134147
if (verbose == TRUE) {
135148
cat("\nRestarting simulation from checkpoint data ...")
136149
}
137-
150+
138151
nsims <- control$nsims
139152
ncores <- ifelse(nsims == 1, 1, min(parallel::detectCores(), control$ncores))
140-
153+
141154
cluster.size <- min(nsims, ncores)
142155
doParallel::registerDoParallel(cluster.size)
143-
156+
144157
xfn <- x
145158
i <- NULL # just to pass R CMD Check
146159
out <- foreach(i = 1:nsims) %dopar% {
@@ -157,15 +170,18 @@ netsim_hpc <- function(x, param, init, control,
157170
}
158171
netsim(x, param, init, control)
159172
}
160-
173+
161174
all <- out[[1]]
162175
for (j in 2:length(out)) {
163176
all <- merge(all, out[[j]], param.error = FALSE)
164177
}
165178
sim <- all
166-
179+
167180
}
168181

182+
183+
# Post-Processing ---------------------------------------------------------
184+
169185
# Save completed simulation data
170186
if (verbose == TRUE) {
171187
cat("\nSaving simulation data ...")
@@ -199,4 +215,5 @@ netsim_hpc <- function(x, param, init, control,
199215
if (save.min == FALSE & save.max == FALSE) {
200216
return(sim)
201217
}
218+
202219
}

0 commit comments

Comments
 (0)