-
Notifications
You must be signed in to change notification settings - Fork 234
/
start_model_runs.R
341 lines (299 loc) · 10.7 KB
/
start_model_runs.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
##-------------------------------------------------------------------------------
## Copyright (c) 2012 University of Illinois, NCSA. All rights reserved. This
## program and the accompanying materials are made available under the terms of
## the University of Illinois/NCSA Open Source License which accompanies this
## distribution, and is available at
## http://opensource.ncsa.illinois.edu/license.html
##-------------------------------------------------------------------------------
#' Start selected ecosystem model runs within PEcAn workflow
#'
#' @param settings pecan settings object
#' @param write (logical) Whether or not to write to the database. Default TRUE.
#' @param stop.on.error Throw error if _any_ of the runs fails. Default TRUE.
#' @export
#' @examples
#' \dontrun{
#' start_model_runs(settings)
#' }
#' @author Shawn Serbin, Rob Kooper, David LeBauer, Alexey Shiklomanov
#'
start_model_runs <- function(settings, write = TRUE, stop.on.error = TRUE) {
run_file <- file.path(settings$rundir, "runs.txt")
# check if runs need to be done
if (!file.exists(file.path(settings$rundir, "runs.txt"))) {
PEcAn.logger::logger.warn(
"runs.txt not found, assuming no runs need to be done")
return()
}
run_list <- readLines(con = run_file)
nruns <- length(run_list)
if (nruns == 0) {
PEcAn.logger::logger.warn(
"runs.txt found, but is empty. Assuming no runs need to be done")
return()
}
model <- settings$model$type
PEcAn.logger::logger.info(
"-------------------------------------------------------------------")
PEcAn.logger::logger.info(paste(" Starting model runs", model))
PEcAn.logger::logger.info(
"-------------------------------------------------------------------")
is_local <- PEcAn.remote::is.localhost(settings$host)
is_qsub <- !is.null(settings$host$qsub)
is_rabbitmq <- !is.null(settings$host$rabbitmq)
is_modellauncher <- !is.null(settings$host$modellauncher)
# Check if Njobmax tag exists in seetings
if (is_modellauncher){
if (!is.null(settings$host$modellauncher$Njobmax)){
Njobmax <- settings$host$modellauncher$Njobmax
} else {
Njobmax <- nruns
}
compt_run <- 0
compt_run_modellauncher <- 1
job_modellauncher <- list()
}
# loop through runs and either call start run, or launch job on remote machine
jobids <- list()
## setup progressbar
pb <- utils::txtProgressBar(min = 0, max = nruns, style = 3)
pbi <- 0
# create database connection
if (write) {
dbcon <- PEcAn.DB::db.open(settings$database$bety)
on.exit(PEcAn.DB::db.close(dbcon), add = TRUE)
} else {
dbcon <- NULL
}
# launcher folder
jobfile <- NULL
firstrun <- NULL
# launch each of the jobs
for (run in run_list) {
run_id_string <- format(run, scientific = FALSE)
# write start time to database
PEcAn.DB::stamp_started(con = dbcon, run = run)
# if running on a remote cluster, create folders and copy any data
# to remote host
if (!is_local) {
PEcAn.remote::remote.execute.cmd(
host = settings$host,
cmd = "mkdir",
args = c(
"-p",
file.path(settings$host$outdir, run_id_string)))
PEcAn.remote::remote.copy.to(
host = settings$host,
src = file.path(settings$rundir, run_id_string),
dst = settings$host$rundir,
delete = TRUE)
}
# check to see if we use the model launcer
if (is_rabbitmq) {
run_id_string <- format(run, scientific = FALSE)
folder <- file.path(settings$rundir, run_id_string)
out <- PEcAn.remote::start_rabbitmq(
folder, settings$host$rabbitmq$uri, settings$host$rabbitmq$queue)
PEcAn.logger::logger.debug("JOB.SH submit status:", out)
jobids[run] <- folder
} else if (is_modellauncher) {
# set up launcher script if we use modellauncher
if (is.null(firstrun)) {
firstrun <- run
jobfile <- PEcAn.remote::setup_modellauncher(
run = run,
rundir = settings$rundir,
host_rundir = settings$host$rundir,
mpirun = settings$host$modellauncher$mpirun,
binary = settings$host$modellauncher$binary)
job_modellauncher[compt_run_modellauncher] <- run
compt_run_modellauncher <- compt_run_modellauncher+1
}
writeLines(
c(file.path(settings$host$rundir, run_id_string)),
con = jobfile)
pbi <- pbi + 1
} else if (is_qsub) {
out <- PEcAn.remote::start_qsub(
run = run,
qsub_string = settings$host$qsub,
rundir = settings$rundir,
host = settings$host,
host_rundir = settings$host$rundir,
host_outdir = settings$host$outdir,
stdout_log = "stdout.log",
stderr_log = "stderr.log",
job_script = "job.sh")
PEcAn.logger::logger.debug("JOB.SH submit status:", out)
jobids[run] <- PEcAn.remote::qsub_get_jobid(
out = out[length(out)],
qsub.jobid = settings$host$qsub.jobid,
stop.on.error = stop.on.error)
} else {
# if qsub option is not invoked. just start model runs in serial.
out <- PEcAn.remote::start_serial(
run = run,
host = settings$host,
rundir = settings$rundir,
host_rundir = settings$host$rundir,
job_script = "job.sh")
# check output to see if an error occurred during the model run
PEcAn.remote::check_model_run(out = out, stop.on.error = stop.on.error)
if (!is_local) {
# copy data back to local
PEcAn.remote::remote.copy.from(
host = settings$host,
src = file.path(settings$host$outdir, run_id_string),
dst = settings$modeloutdir)
}
# write finished time to database
PEcAn.DB::stamp_finished(con = dbcon, run = run)
pbi <- pbi + 1
utils::setTxtProgressBar(pb, pbi)
}
# Check if compt_run has reached Njobmax
if (is_modellauncher){
compt_run <- compt_run + 1
if (compt_run == Njobmax){
close(jobfile)
firstrun <- NULL
compt_run <- 0
jobfile <- NULL
}
}
} # end loop over runs
close(pb)
# need to actually launch the model launcher
if (is_modellauncher) {
# Only close if not already closed
if (compt_run != 0){
close(jobfile)
}
if (!is_local) {
for (run in run_list){
if (run %in% job_modellauncher) {
# copy launcher and joblist
PEcAn.remote::remote.copy.to(
host = settings$host,
src = file.path(settings$rundir, format(run, scientific = FALSE)),
dst = settings$host$rundir,
delete = TRUE)
}
}
}
if (is_qsub) {
for (run in run_list){
if (run %in% job_modellauncher) {
out <- PEcAn.remote::start_qsub(
run = run,
qsub_string = settings$host$qsub,
rundir = settings$rundir,
host = settings$host,
host_rundir = settings$host$rundir,
host_outdir = settings$host$outdir,
stdout_log = "launcher.out.log",
stderr_log = "launcher.err.log",
job_script = "launcher.sh",
qsub_extra = settings$host$modellauncher$qsub)
}
# HACK: Code below gets 'run' from names(jobids) so need an entry for
# each run. But when using modellauncher all runs have the same jobid
jobids[run] <- sub(settings$host$qsub.jobid, "\\1", out[length(out)])
}
} else {
out <- PEcAn.remote::start_serial(
run = run,
host = settings$host,
rundir = settings$rundir,
host_rundir = settings$host$rundir,
job_script = "launcher.sh")
# check output to see if an error occurred during the model run
PEcAn.remote::check_model_run(out = out, stop.on.error = TRUE)
# write finished time to database
for (run in run_list) {
PEcAn.DB::stamp_finished(con = dbcon, run = run)
}
utils::setTxtProgressBar(pb, pbi)
}
}
# wait for all jobs to finish
if (length(jobids) > 0) {
PEcAn.logger::logger.debug(
"Waiting for the following jobs:",
unlist(jobids, use.names = FALSE))
}
while (length(jobids) > 0) {
Sys.sleep(10)
for (run in names(jobids)) {
run_id_string <- format(run, scientific = FALSE)
# check to see if job is done
job_finished <- FALSE
if (is_rabbitmq) {
job_finished <- file.exists(file.path(jobids[run], "rabbitmq.out"))
} else if (is_qsub) {
job_finished <- PEcAn.remote::qsub_run_finished(
run = jobids[run],
host = settings$host,
qstat = settings$host$qstat)
}
if (job_finished) {
# Copy data back to local
if (!is_local) {
PEcAn.remote::remote.copy.from(
host = settings$host,
src = file.path(settings$host$outdir, run_id_string),
dst = settings$modeloutdir)
}
# TODO check output log
if (is_rabbitmq) {
data <- readLines(file.path(jobids[run], "rabbitmq.out"))
if (data[-1] == "ERROR") {
msg <- paste("Run", run, "has an ERROR executing")
if (stop.on.error) {
PEcAn.logger::logger.severe(msg)
} else {
PEcAn.logger::logger.error(msg)
}
}
}
# Write finish time to database
if (is_modellauncher) {
for (x in run_list) {
PEcAn.DB::stamp_finished(con = dbcon, run = x)
}
} else {
PEcAn.DB::stamp_finished(con = dbcon, run = run)
}
# move progress bar
if (!is_modellauncher) {
pbi <- pbi + 1
}
utils::setTxtProgressBar(pb, pbi)
# remove job
if (is_modellauncher) {
for (x in run_list) {
jobids[x] <- NULL
}
} else {
jobids[run] <- NULL
}
} # End job finished
} # end loop over runs
} # end while loop checking runs
} # start_model_runs
#' @describeIn start_model_runs
#'
#' A lightweight wrapper around `start_model_runs` that takes `write` from
#' `settings` instead of as a separate argument.
#'
#' @export
runModule_start_model_runs <- function(settings, stop.on.error=TRUE) {
if (PEcAn.settings::is.MultiSettings(settings) ||
PEcAn.settings::is.Settings(settings)) {
write <- settings$database$bety$write
return(start_model_runs(settings, write, stop.on.error))
} else {
PEcAn.logger::logger.severe(
"runModule_start_model_runs only works with Settings or MultiSettings")
}
} # runModule_start_model_runs