Skip to content

Commit

Permalink
Prevent memory leaks in growR_run_loop
Browse files Browse the repository at this point in the history
  • Loading branch information
kuadrat committed Aug 21, 2024
1 parent 5eec56f commit 484a9df
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: growR
Type: Package
Version: 1.3.0.9005
Version: 1.3.0.9006
Date: 2024-05-23
Authors@R: person(
given = "Kevin",
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* PhenologicalAutocut
* Multithreading for `growR_run_loop` with the `parallel` package.
* New parameter for irrigation.
* `growR_run_loop` allows to suppress returning the simulation objects. This
is useful to prevent overkill memory usage.

## Changed

Expand Down
29 changes: 23 additions & 6 deletions R/run.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ parallel_error_message = paste0("The following R packages are required in ",
#' @param modvege_environments A list of [ModvegeEnvironment] instances.
#' @param output_dir string; name of directory to which output files are to
#' be written. If `output_dir == ""` (default), no files are written.
#' @param return_results boolean Whether or not simulation results are to be
#' kept in memory and returned at function completion. Can be disabled to
#' safe RAM, especially if results are anyways written to file (which is
#' the case if `output_dir` is not empty).
#' @param independent boolean; If `TRUE` (default) the simulation for each
#' year starts with the same initial conditions, as specified in the
#' parameters of the modvege_environments. If `FALSE`, initial conditions
Expand All @@ -37,7 +41,8 @@ parallel_error_message = paste0("The following R packages are required in ",
#' @export
#'
growR_run_loop = function(modvege_environments, output_dir = "",
independent = TRUE, n_threads = 1) {
return_results = TRUE, independent = TRUE,
n_threads = 1) {
# Set up parallelisation
if (n_threads > 1) {
# Check if suggested packages are present
Expand All @@ -61,7 +66,7 @@ growR_run_loop = function(modvege_environments, output_dir = "",
if (n_threads > 1) {
# Multi-thread run
results = parallel::mclapply(modvege_environments, run_loop_parallel,
output_dir, independent,
output_dir, return_results, independent,
mc.cores = n_threads)
} else {
# Single-thread run
Expand All @@ -72,8 +77,11 @@ growR_run_loop = function(modvege_environments, output_dir = "",
level = INFO)
logger(sprintf(" run name: `%s`.", run_environment$run_name),
level = INFO)
results[[run]] = run_loop_core(run_environment, run, n_runs, output_dir,
independent)
result = run_loop_core(run_environment, run, n_runs, output_dir,
return_results, independent)
if (return_results) {
results[[run]] = result
}
} # End of loop over runs
}
logger("All runs completed.", level = INFO)
Expand Down Expand Up @@ -118,6 +126,10 @@ run_loop_parallel = function(run_environment, ...) {
#' @param n_runs integer Total number of runs to carry out.
#' @param output_dir str Path to directory where output files will be written
#' to.
#' @param return_results boolean Whether or not simulation results are to be
#' kept in memory and returned at function completion. Can be disabled to
#' safe RAM, especially if results are anyways written to file (which is
#' the case if `output_dir` is not empty).
#' @param independent boolean Whether or not simulations of subsequent years
#' are treated as independent.
#' @return A list of [ModvegeSite] instances containing the simulation
Expand All @@ -128,7 +140,7 @@ run_loop_parallel = function(run_environment, ...) {
#' @keywords internal
#'
run_loop_core = function(run_environment, run, n_runs, output_dir,
independent) {
return_results, independent) {
results_for_env = list()
modvege = ModvegeSite$new(run_environment$parameters,
site_name = run_environment$site_name,
Expand Down Expand Up @@ -172,6 +184,11 @@ run_loop_core = function(run_environment, run, n_runs, output_dir,
}
}
} # End of loop over simulation years
return(results_for_env)
# Avoid RAM filling by not returning anything.
if (return_results) {
return(results_for_env)
} else {
return(list())
}
}

0 comments on commit 484a9df

Please sign in to comment.