Skip to content

Commit 49af615

Browse files
committed
init starting to pull out helper functions from misc project areas
0 parents  commit 49af615

10 files changed

+808
-0
lines changed

.Rbuildignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
^slurmtools\.Rproj$
2+
^\.Rproj\.user$
3+
^LICENSE\.md$

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.Rproj.user

DESCRIPTION

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Package: slurmtools
2+
Title: slurm tooling
3+
Version: 0.0.0.9000
4+
Authors@R:
5+
person("Devin", "Pastoor", , "[email protected]", role = c("aut", "cre"))
6+
Description: What the package does (one paragraph).
7+
License: GPL (>= 3)
8+
Encoding: UTF-8
9+
Roxygen: list(markdown = TRUE)
10+
RoxygenNote: 7.2.3
11+
Imports:
12+
brio,
13+
fs,
14+
processx,
15+
rlang,
16+
whisker

LICENSE.md

+595
Large diffs are not rendered by default.

NAMESPACE

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Generated by roxygen2: do not edit by hand
2+
3+
export(get_slurm_partitions)
4+
export(submit_nonmem_model)

R/get-partitions.R

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# this will take the raw string and return the partitions such that the default one will be first
2+
# and will have the asterisk removed
3+
process_slurm_partitions <- function(ps){
4+
partitions <- strsplit(ps, "\n")[[1]]
5+
all_partitions <- gsub("'", "", partitions)
6+
is_default_partition <- grepl('\\*$', x = all_partitions, )
7+
default_partition <- gsub(x = all_partitions[is_default_partition], pattern = "\\*$", replacement = "")
8+
# delete the default partition then lets put it at the beginning so it can be selected that way
9+
all_partitions <- all_partitions[-is_default_partition]
10+
c(default_partition, all_partitions)
11+
}
12+
13+
#' get slurm partitions for the given cluster
14+
#' @export
15+
get_slurm_partitions <- function() {
16+
sinfobin <- Sys.which("sinfo")
17+
if (!nzchar(sinfobin)) {
18+
rlang::abort("could not find sinfo binary")
19+
}
20+
res <- processx::run(sinfobin, c("--format='%P'", "--noheader"), )
21+
if (res$status != 0) {
22+
stderrout <- sprintf("failed to get partition info with info: \n\n stdout: %s\n\n stderr: %s", res$stdout, res$stderr)
23+
rlang::abort(stderrout)
24+
}
25+
# this will be a line separated list of partitions with an extra * for the default partition
26+
# for example it'll look something like
27+
# "'cpu2mem4gb*'\n'cpu4mem32gb'\n'cpu8mem64gb'\n'cpu2mem8gb'\n'cpu4mem16gb'\n'cpu16mem128gb'\n'cpu8mem32gb'\n'cpu16mem64gb'\n'cpu32mem128gb'\n"
28+
process_slurm_partitions(res$stdout)
29+
}
30+

R/submit-model.R

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#' submit a nonmem model to slurm in parallel
2+
#' @param .mod a path to a model or a bbi nonmem model object
3+
#' @param partition name of the partition to submit the model
4+
#' @param ncpu number of cpus to run the model against
5+
#' @param overwrite whether to overwrite existing model results
6+
#' @param dry_run return the command that would have been invoked, without invoking
7+
#' @param ... arguments to pass to processx::run
8+
#' @param slurm_job_template_path path to slurm job template
9+
#' @param submission_root directory to track job submission scripts and output
10+
#' @param bbi_config_path path to bbi config file
11+
#' @export
12+
submit_nonmem_model <-
13+
function(.mod,
14+
partition = getOption('slurmtools.partitions'),
15+
ncpu = 4,
16+
overwrite = FALSE,
17+
dry_run = FALSE,
18+
...,
19+
slurm_job_template_path = getOption('slurmtools.slurm_job_template_path'),
20+
submission_root = getOption('slurmtools.submission_root'),
21+
bbi_config_path = getOption('slurmtools.bbi_config_path'),
22+
slurm_template_opts = list()) {
23+
if (is.null(partition)) {
24+
rlang::abort("no partition selected")
25+
}
26+
partition <- match.arg(partition)
27+
if (!inherits(.mod, "bbi_nonmem_model") &&
28+
!fs::file_exists(.mod)) {
29+
stop(
30+
"please provide a bbi_nonmem_model created via read_model/new_model, or a path to the model file"
31+
)
32+
}
33+
if (!inherits(.mod, "bbi_nonmem_model")) {
34+
# its a file path that exists so lets convert that into the structure bbi
35+
# provides for now
36+
.mod <- list(absolute_model_path = fs::path_abs(.mod))
37+
}
38+
parallel <- if (ncpu > 1) {
39+
TRUE
40+
} else {
41+
FALSE
42+
}
43+
44+
if (!fs::is_absolute_path(bbi_config_path)) {
45+
rlang::abort(sprintf("bbi_config_path must be absolute, not %s", bbi_config_path))
46+
}
47+
if (!fs::file_exists(slurm_job_template_path)) {
48+
rlang::abort(sprintf("slurm job template path not valid: `%s`", slurm_job_template_path))
49+
}
50+
if (overwrite && fs::dir_exists(.mod$absolute_model_path)) {
51+
fs::dir_delete(.mod$absolute_model_path)
52+
}
53+
template_script <-
54+
withr::with_dir(dirname(.mod$absolute_model_path), {
55+
tmpl <- brio::read_file(slurm_job_template_path)
56+
whisker::whisker.render(
57+
tmpl,
58+
list(
59+
partition = partition,
60+
parallel = parallel,
61+
ncpu = ncpu,
62+
job_name = sprintf("nonmem-run-%s", basename(.mod$absolute_model_path)),
63+
bbi_exe_path = Sys.which("bbi"),
64+
bbi_config_path = bbi_config_path,
65+
model_path = .mod$absolute_model_path
66+
)
67+
)
68+
})
69+
script_file_path <-
70+
file.path(submission_root, sprintf("%s.sh", basename(.mod$absolute_model_path)))
71+
if (!dry_run) {
72+
if (!fs::dir_exists(submission_root)) {
73+
fs::dir_create(submission_root)
74+
}
75+
brio::write_file(template_script, script_file_path)
76+
fs::file_chmod(script_file_path, "0755")
77+
}
78+
cmd <- list(cmd = "sbatch", args = script_file_path, template_script = template_script, partition = partition)
79+
if (dry_run) {
80+
return(cmd)
81+
}
82+
withr::with_dir(submission_root, {
83+
processx::run(cmd$cmd, cmd$args, ...)
84+
})
85+
}

man/get_slurm_partitions.Rd

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/submit_nonmem_model.Rd

+41
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

slurmtools.Rproj

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Version: 1.0
2+
3+
RestoreWorkspace: No
4+
SaveWorkspace: No
5+
AlwaysSaveHistory: Default
6+
7+
EnableCodeIndexing: Yes
8+
UseSpacesForTab: Yes
9+
NumSpacesForTab: 2
10+
Encoding: UTF-8
11+
12+
RnwWeave: Sweave
13+
LaTeX: pdfLaTeX
14+
15+
AutoAppendNewline: Yes
16+
StripTrailingWhitespace: Yes
17+
LineEndingConversion: Posix
18+
19+
BuildType: Package
20+
PackageUseDevtools: Yes
21+
PackageInstallArgs: --no-multiarch --with-keep.source
22+
PackageRoxygenize: rd,collate,namespace

0 commit comments

Comments
 (0)