Skip to content

Commit

Permalink
Style code and devtools::document()
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick-Eagles committed Oct 10, 2023
1 parent 0a43ebb commit 48b364f
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 25 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export(job_info)
export(job_loop)
export(job_report)
export(job_single)
export(partition_info)
export(with_wd)
import(dplyr)
import(glue)
Expand Down
2 changes: 1 addition & 1 deletion R/job_info.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @param user A `character(1)` vector specifying the username of the jobs to
#' query. Set NULL to return info about all users' jobs.
#' @param partition A `character(1)` vector specifying the partition of the jobs
#' to query. Set NULL to return info about jobs of all paritions.
#' to query. Set NULL to return info about jobs of all partitions.
#'
#' @return A tibble with job information about currently running jobs.
#' @export
Expand Down
5 changes: 2 additions & 3 deletions R/job_loop.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@
#' name = "bsp2_test_array"
#' )
#'
job_loop <- function(
loops, name, create_shell = FALSE, partition = "shared", memory = "10G",
cores = 1L, email = "ALL", logdir = "logs", task_num = NULL, tc = 20) {
job_loop <- function(loops, name, create_shell = FALSE, partition = "shared", memory = "10G",
cores = 1L, email = "ALL", logdir = "logs", task_num = NULL, tc = 20) {
## Check that the loops are correctly defined
if (!is.list(loops)) {
stop("'loops' should be a named list.", call. = FALSE)
Expand Down
9 changes: 5 additions & 4 deletions R/job_single.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@
#' ## An array job
#' job_single("jhpce_job_array", task_num = 20, create_logdir = FALSE)
#'
job_single <- function(name, create_shell = FALSE, partition = "shared", memory = "10G",
cores = 1L, email = "ALL", logdir = "logs", task_num = NULL, tc = 20,
command = 'Rscript -e "options(width = 120); sessioninfo::session_info()"',
create_logdir = TRUE) {
job_single <- function(
name, create_shell = FALSE, partition = "shared", memory = "10G",
cores = 1L, email = "ALL", logdir = "logs", task_num = NULL, tc = 20,
command = 'Rscript -e "options(width = 120); sessioninfo::session_info()"',
create_logdir = TRUE) {
## Remove any spaces
name <- gsub(" ", "_", name)

Expand Down
29 changes: 14 additions & 15 deletions R/partition_info.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
#' # Print summary of CPU availability for the 'shared' partition
#' print(
#' sprintf(
#' 'The %s partition has %i CPUs free (%.1f%% of total)',
#' "The %s partition has %i CPUs free (%.1f%% of total)",
#' part_df$partition,
#' part_df$free_cpus,
#' 100 * part_df$prop_free_cpus
#' )
#' )
#' }
partition_info <- function(partition = "shared", all_nodes = FALSE) {
command = paste(
command <- paste(
# Grab info about CPUs and memory with 'sinfo'
"sinfo -N -O 'PartitionName,NodeList:50,FreeMem,AllocMem,Memory,StateLong,CPUsState'",
# Properly delimit columns by "|"
Expand All @@ -44,17 +44,17 @@ partition_info <- function(partition = "shared", all_nodes = FALSE) {
sep = " | "
)

part_df = read.csv(text = system(command, intern = TRUE), sep = "|") |>
part_df <- read.csv(text = system(command, intern = TRUE), sep = "|") |>
as_tibble()

# Subset by partition if not null
if(!is.null(partition)) {
part_df = part_df |> filter(PARTITION == partition)
if (!is.null(partition)) {
part_df <- part_df |> filter(PARTITION == partition)
}

colnames(part_df) = tolower(colnames(part_df))
colnames(part_df) <- tolower(colnames(part_df))

part_df = part_df |>
part_df <- part_df |>
# Use more informative and consistent column names
rename(
node = nodelist,
Expand All @@ -65,27 +65,26 @@ partition_info <- function(partition = "shared", all_nodes = FALSE) {
mutate(
state = as.factor(state),
# Convert from MB to GB to match other slurmjobs functions
across(matches('.*_mem_gb$'), ~ .x / 1000)
across(matches(".*_mem_gb$"), ~ .x / 1000)
)

# When 'all_nodes' is FALSE, sum across all nodes within a partition to
# provide summary statistics
if (! all_nodes) {
part_df = part_df |>
if (!all_nodes) {
part_df <- part_df |>
group_by(partition) |>
# Note free resources don't count hypothetically free resources (i.e. CPUs
# or memory that aren't being used, but aren't available for use now).
# 'total*' columns do include these hypothetical resources
summarize(
free_cpus = sum(inactive_cpus[state %in% c('mixed', 'idle')]),
free_cpus = sum(inactive_cpus[state %in% c("mixed", "idle")]),
total_cpus = sum(total_cpus),
prop_free_cpus = free_cpus / total_cpus,
free_mem_gb = sum(free_mem_gb[state %in% c('mixed', 'idle')]),
free_mem_gb = sum(free_mem_gb[state %in% c("mixed", "idle")]),
total_mem_gb = sum(total_mem_gb),
prop_free_mem_gb = free_mem_gb / total_mem_gb
)
}

return (part_df)
return(part_df)
}

2 changes: 1 addition & 1 deletion man/job_info.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions man/partition_info.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/test-array_submit.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ run_test <- function(shell_creation_fun, ...) {
final <- readLines(paste0(script_name, ".sh"))
}
)

return(list("original" = original, "final" = final))
}

Expand Down

0 comments on commit 48b364f

Please sign in to comment.