Skip to content

Commit

Permalink
fix parallel tests to avoid comparing run time with serial execution
Browse files Browse the repository at this point in the history
- revert sim_num to 100 to simulate 100 sets of test data (see tests/testthat/fixtures/simulate-integration-test-data.R)
- remove code that compares parallel run time with serial execution, focusing on ensuring that parallel runs work correctly and produce consistent results (see tests/testthat/test-parallel-with-snowfall-with-wrappers.R)
- delete parallel tests that don't use the wrapper functions
- TODO: Once the wrapper functions for preparing FIMS inputs are fully developed, add a standalone test (CI-only) for checking the speed of parallel runs
  • Loading branch information
Bai-Li-NOAA authored and kellijohnson-NOAA committed Oct 22, 2024
1 parent 60bf7b0 commit 4f77263
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 99 deletions.
Binary file modified tests/testthat/fixtures/integration_test_data.RData
Binary file not shown.
4 changes: 2 additions & 2 deletions tests/testthat/fixtures/simulate-integration-test-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ maindir <- tempdir()
model_input <- ASSAMC::save_initial_input()

# Configure the input parameters for the simulation
sim_num <- 150
sim_num <- 100
sim_input <- ASSAMC::save_initial_input(
base_case = TRUE,
input_list = model_input,
Expand Down Expand Up @@ -45,4 +45,4 @@ for (i in 1:sim_num) {

save(om_input_list, om_output_list, em_input_list,
file = test_path("fixtures", "integration_test_data.RData")
)
)
58 changes: 36 additions & 22 deletions tests/testthat/test-parallel-with-snowfall-with-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@

# Run FIMS in serial and parallel
# This test demonstrates how to run the FIMS model in both serial and parallel
# modes. The test compares the execution time and results of running the model
# in serial versus parallel. The parallel execution uses the {snowfall} package
# to parallelize the tasks across multiple CPU cores
# modes. The parallel execution uses {snowfall} to parallelize the tasks across
# multiple CPU cores.

# Load the model comparison operating model data from the fixtures folder
load(test_path("fixtures", "integration_test_data.RData"))

sim_num <- 10

# Run the FIMS model in serial and record the execution time
estimation_results_serial <- vector(mode = "list", length = length(om_input_list))
estimation_results_serial <- vector(mode = "list", length = sim_num)

start_time_serial <- Sys.time()
for (i in 1:length(om_input_list)) {
for (i in 1:sim_num) {
estimation_results_serial[[i]] <- setup_and_run_FIMS_with_wrappers(
iter_id = i,
om_input_list = om_input_list,
Expand All @@ -25,37 +25,51 @@ for (i in 1:length(om_input_list)) {
estimation_mode = TRUE
)
}
end_time_serial <- Sys.time()
estimation_time_serial <- end_time_serial - start_time_serial

test_that("Run FIMS in parallel using {snowfall}", {
core_num <- parallel::detectCores() - 1
core_num <- 2
snowfall::sfInit(parallel = TRUE, cpus = core_num)
start_time_parallel <- Sys.time()

results_parallel <- snowfall::sfLapply(
1:length(om_input_list),
1:sim_num,
setup_and_run_FIMS_with_wrappers,
om_input_list,
om_output_list,
em_input_list,
TRUE
)

end_time_parallel <- Sys.time()

time_parallel <- end_time_parallel - start_time_parallel

snowfall::sfStop()

# Compare execution times: verify that the execution time of the parallel run
# is less than the serial run.
expect_lt(object = time_parallel, expected = estimation_time_serial)
# Comparison of results:
# Verify that SSB values from both runs are equivalent.
expect_setequal(
purrr::map(
results_parallel,
\(x) x@estimates[x@estimates$name == "SSB", "value"]
),
purrr::map(
estimation_results_serial,
\(x) x@estimates[x@estimates$name == "SSB", "value"]
)
)

# Compare parameters in results:
# Verify that the results from both runs are equivalent.
# Verify that parameter values from both runs are equivalent.
expect_setequal(
purrr::map(results_parallel, \(x) x@estimates[["value"]]),
purrr::map(estimation_results_serial, \(x) x@estimates[["value"]])
purrr::map(
results_parallel,
\(x) x@estimates[x@estimates$name == "p", "value"]
),
purrr::map(
estimation_results_serial,
\(x) x@estimates[x@estimates$name == "p", "value"]
)
)

# Verify that total NLL values from both runs are equivalent.
expect_equal(
purrr::map(results_parallel, \(x) x@report[["jnll"]]),
purrr::map(estimation_results_serial, \(x) x@report[["jnll"]])
)

})
75 changes: 0 additions & 75 deletions tests/testthat/test-parallel-with-snowfall-without-wrappers.R

This file was deleted.

0 comments on commit 4f77263

Please sign in to comment.