From 48b364f7f3e95c58d6c189f2ede7dfc2f0f9969a Mon Sep 17 00:00:00 2001 From: Nick-Eagles Date: Tue, 10 Oct 2023 11:24:41 -0400 Subject: [PATCH] Style code and devtools::document() --- NAMESPACE | 1 + R/job_info.R | 2 +- R/job_loop.R | 5 ++-- R/job_single.R | 9 +++--- R/partition_info.R | 29 ++++++++++---------- man/job_info.Rd | 2 +- man/partition_info.Rd | 44 ++++++++++++++++++++++++++++++ tests/testthat/test-array_submit.R | 2 +- 8 files changed, 69 insertions(+), 25 deletions(-) create mode 100644 man/partition_info.Rd diff --git a/NAMESPACE b/NAMESPACE index b9c7dbd..4a04f0e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) diff --git a/R/job_info.R b/R/job_info.R index 78669bd..e4b7ff5 100644 --- a/R/job_info.R +++ b/R/job_info.R @@ -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 diff --git a/R/job_loop.R b/R/job_loop.R index 81b85a8..7323862 100644 --- a/R/job_loop.R +++ b/R/job_loop.R @@ -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) diff --git a/R/job_single.R b/R/job_single.R index d1552e4..73aa796 100644 --- a/R/job_single.R +++ b/R/job_single.R @@ -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) diff --git a/R/partition_info.R b/R/partition_info.R index ce705aa..21b6d52 100644 --- a/R/partition_info.R +++ b/R/partition_info.R @@ -24,7 +24,7 @@ #' # 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 @@ -32,7 +32,7 @@ #' ) #' } 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 "|" @@ -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, @@ -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) } - \ No newline at end of file diff --git a/man/job_info.Rd b/man/job_info.Rd index 486ee53..17ad2d7 100644 --- a/man/job_info.Rd +++ b/man/job_info.Rd @@ -11,7 +11,7 @@ job_info(user = Sys.getenv("USER"), partition = "shared") query. Set NULL to return info about all users' jobs.} \item{partition}{A \code{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.} } \value{ A tibble with job information about currently running jobs. diff --git a/man/partition_info.Rd b/man/partition_info.Rd new file mode 100644 index 0000000..e02a6c7 --- /dev/null +++ b/man/partition_info.Rd @@ -0,0 +1,44 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/partition_info.R +\name{partition_info} +\alias{partition_info} +\title{Return a tibble containing information about partitions.} +\usage{ +partition_info(partition = "shared", all_nodes = FALSE) +} +\arguments{ +\item{partition}{A \code{character(1)} vector specifying the partition to show +information about. Set NULL to return info about all partitions.} + +\item{all_nodes}{A \code{logical(1)} vector. When TRUE, return one row per node. +When FALSE, summarize information over nodes and return one row per +partition.} +} +\value{ +A tibble with memory and CPU information about partitions. +} +\description{ +This function imports the output of 'sinfo' into tibble, focusing on +available vs. total CPUs and memory by either node or partition (depending +on \code{all_nodes}). +} +\examples{ + +# Must be run in a SLURM environment where a 'shared' partition exists +if (system("which sbatch") == 0) { + part_df <- partition_info(partition = "shared", all_nodes = FALSE) + + # Print summary of CPU availability for the 'shared' partition + print( + sprintf( + "The \%s partition has \%i CPUs free (\%.1f\%\% of total)", + part_df$partition, + part_df$free_cpus, + 100 * part_df$prop_free_cpus + ) + ) +} +} +\author{ +Nicholas J. Eagles +} diff --git a/tests/testthat/test-array_submit.R b/tests/testthat/test-array_submit.R index 8534062..d56c0fe 100644 --- a/tests/testthat/test-array_submit.R +++ b/tests/testthat/test-array_submit.R @@ -41,7 +41,7 @@ run_test <- function(shell_creation_fun, ...) { final <- readLines(paste0(script_name, ".sh")) } ) - + return(list("original" = original, "final" = final)) }