Skip to content

Commit

Permalink
Set the default number of cores to use as 2 unless otherwise specified
Browse files Browse the repository at this point in the history
  • Loading branch information
shwilks committed Aug 30, 2023
1 parent 4e09543 commit 794df31
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 12 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,6 @@

# Racmacs 1.2.5
* Allow for shorthand description of shapes e.g. "C" for "CIRCLE" in map .ace files.

# Racmacs 1.2.6
* Set the default number of cores to use as 2 unless otherwise specified in either RacOptimizer.options, or by setting the global option 'RacOptimizer.num_cores' with e.g. `options(RacOptimizer.num_cores = parallel::detectCores())`.
16 changes: 13 additions & 3 deletions R/map_optimize.R
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ make.acmap <- function(
#' @param max_line_search_trials The maximum number of trials for the line search (before giving up).
#' @param min_step The minimum step of the line search.
#' @param max_step The maximum step of the line search.
#' @param num_cores The number of cores to run in parallel
#' @param num_cores The number of cores to run in parallel when running optimizations
#' @param report_progress Should progress be reported
#' @param ignore_disconnected Should the check for disconnected points be skipped
#' @param progress_bar_length Progress bar length when progress is reported
Expand All @@ -270,7 +270,7 @@ RacOptimizer.options <- function(
max_line_search_trials = 50,
min_step = 1e-20,
max_step = 1e20,
num_cores = parallel::detectCores(),
num_cores = getOption("RacOptimizer.num_cores"),
report_progress = NULL,
ignore_disconnected = FALSE,
progress_bar_length = options()$width
Expand All @@ -281,9 +281,19 @@ RacOptimizer.options <- function(
check.logical(ignore_disconnected)
check.string(method)
check.numeric(maxit)
check.numeric(num_cores)
check.numeric(progress_bar_length)
if (!is.null(report_progress)) check.logical(report_progress)
if (!is.null(num_cores)) check.integer(num_cores)

# Set default number of cores to 2
if (is.null(num_cores)) {
rlang::warn(
message = "Number of parallel cores to use for optimization was not specified, so using default of 2. You can set the number of cores to use explicitly by passing it as an argument to the optimizer function, or globally by setting the option 'RacOptimizer.num_cores', e.g. by adding the line `options(RacOptimizer.num_cores = parallel::detectCores())` to the top of your script.",
.frequency = "regularly",
.frequency_id = "RacOptimizer_num_cores_check"
)
num_cores <- 2
}

# This is a hack to attempt to see if messages are currently suppressed
if (is.null(report_progress)) {
Expand Down
4 changes: 2 additions & 2 deletions man/RacOptimizer.options.Rd

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

14 changes: 8 additions & 6 deletions src/ac_move_trapped_points.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ arma::mat check_ag_trapped_points(
const AcOptimization &optimization,
const arma::mat &tabledists,
const arma::imat &titertypes,
const double &grid_spacing
const double &grid_spacing,
AcOptimizerOptions options
){

// Variables
Expand All @@ -28,7 +29,7 @@ arma::mat check_ag_trapped_points(
trapped_ag_improved_coords.fill(arma::datum::nan);

// Check trapped antigens
#pragma omp parallel for schedule(dynamic)
#pragma omp parallel for schedule(dynamic) num_threads(options.num_cores)
for(int ag=0; ag<num_ags; ag++){

// Do a grid search
Expand Down Expand Up @@ -62,7 +63,8 @@ arma::mat check_sr_trapped_points(
const AcOptimization &optimization,
const arma::mat &tabledists,
const arma::imat &titertypes,
const double &grid_spacing
const double &grid_spacing,
AcOptimizerOptions options
){

// Variables
Expand All @@ -75,7 +77,7 @@ arma::mat check_sr_trapped_points(
trapped_sr_improved_coords.fill(arma::datum::nan);

// Check trapped sera
#pragma omp parallel for schedule(dynamic)
#pragma omp parallel for schedule(dynamic) num_threads(options.num_cores)
for(int sr=0; sr<num_sr; sr++){

// Do a grid search
Expand Down Expand Up @@ -134,8 +136,8 @@ AcOptimization ac_move_trapped_points(
arma::mat sr_coords = optimization.get_sr_base_coords();

// Check for any improved coordinates
arma::mat ag_trapped_improved_coords = check_ag_trapped_points(optimization, tabledists, titertypes, grid_spacing);
arma::mat sr_trapped_improved_coords = check_sr_trapped_points(optimization, tabledists, titertypes, grid_spacing);
arma::mat ag_trapped_improved_coords = check_ag_trapped_points(optimization, tabledists, titertypes, grid_spacing, options);
arma::mat sr_trapped_improved_coords = check_sr_trapped_points(optimization, tabledists, titertypes, grid_spacing, options);

// Get any improved indices
arma::uvec ag_trapped_coord_indices = arma::find_finite(ag_trapped_improved_coords);
Expand Down
2 changes: 1 addition & 1 deletion src/ac_optim_map_stress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ void ac_relaxOptimizations(
}

// Run and return optimization results
#pragma omp parallel for schedule(dynamic)
#pragma omp parallel for schedule(dynamic) num_threads(options.num_cores)
for (int i=0; i<num_optimizations; i++) {

// Run the optimization
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-diagnostics_bootstrap.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ library(testthat)
context("Bootstrapping maps")

# Set variables
options(RacOptimizer.num_cores = 2)
num_bs_repeats <- 200
map <- read.acmap(test_path("../testdata/testmap_h3subset.ace"))

Expand Down

0 comments on commit 794df31

Please sign in to comment.