Skip to content

Commit 50e4ff9

Browse files
committed
Convergence_Plot: Refactor to use Set_parallel function for cluster management
1 parent 1d5eeda commit 50e4ff9

File tree

7 files changed

+52
-108
lines changed

7 files changed

+52
-108
lines changed

NAMESPACE

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ export(SaveSessionInfo)
129129
export(Scale_0_1)
130130
export(ScrapLinks)
131131
export(ScriptLocation)
132-
export(SetChainName)
133132
export(Set_parallel)
134133
export(Solve2)
135134
export(Solve2vect)

R/General_Set_parallel.R

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
#' Set up or stop parallel processing plan
66
#'
77
#' Configures parallel processing with `future::plan()` or stops an existing
8-
#' plan. On Windows, it uses `snow::makeSOCKcluster()` with `future::cluster`;
9-
#' on Linux/macOS, it uses `future::multicore`. When stopping, it resets to
10-
#' sequential mode.
8+
#' plan. When stopping, it resets to sequential mode.
119
#'
1210
#' @param NCores Integer. Number of cores to use. If `NULL`, defaults to
1311
#' sequential mode.
@@ -43,19 +41,29 @@ Set_parallel <- function(
4341
NCores = 1L, Stop = FALSE, Cat = TRUE, Level = 0L, Future_maxSize = 8) {
4442

4543
# Validate NCores input
46-
NCores <- ifelse(is.null(NCores) || NCores < 1, 1L, as.integer(NCores))
44+
NCores <- ifelse(
45+
(is.null(NCores) || NCores < 1),
46+
1L, as.integer(NCores))
47+
48+
# NCores can not be more than the available cores
49+
NCores <- ifelse(
50+
NCores > parallelly::availableCores(),
51+
{
52+
warning(
53+
paste0(
54+
"`NCores` > number of available cores. ",
55+
"It was reset to the number of available cores",
56+
parallelly::availableCores()),
57+
call. = FALSE)
58+
parallelly::availableCores()
59+
},
60+
NCores)
4761

4862
if (Stop) {
4963
if (Cat) {
5064
IASDT.R::CatTime("Stopping parallel processing", Level = Level)
5165
}
5266

53-
if (.Platform$OS.type == "windows" &&
54-
exists("c1", envir = parent.frame())) {
55-
try(snow::stopCluster(get("c1", envir = parent.frame())), silent = TRUE)
56-
rm("c1", envir = parent.frame())
57-
}
58-
5967
# Stop any running future plan and reset to sequential
6068
future::plan("future::sequential", gc = TRUE)
6169

@@ -75,17 +83,7 @@ Set_parallel <- function(
7583
.local_envir = parent.frame())
7684

7785
if (NCores > 1) {
78-
79-
if (.Platform$OS.type == "windows") {
80-
# Windows: Use cluster (SOCK) since multicore is unavailable
81-
c1 <- snow::makeSOCKcluster(NCores)
82-
# assigns to the calling function's environment
83-
assign("c1", c1, envir = parent.frame())
84-
future::plan("future::cluster", workers = c1)
85-
} else {
86-
# Linux/Mac: Use multicore for better performance
87-
future::plan("future::multicore", workers = NCores)
88-
}
86+
future::plan("future::multisession", workers = NCores)
8987
} else {
9088
future::plan("future::sequential", gc = TRUE)
9189
}

R/Mod_Convergence_Plot.R

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -468,12 +468,14 @@ Convergence_Plot <- function(
468468

469469
FileConv_Beta <- IASDT.R::Path(Path_Convergence, "Convergence_Beta.RData")
470470

471-
472471
if (file.exists(FileConv_Beta)) {
473472

474473
IASDT.R::CatTime("Loading plotting data", Level = 1)
475474
PlotObj_Beta <- IASDT.R::LoadAs(FileConv_Beta)
476475

476+
rm(Obj_Beta, envir = environment())
477+
invisible(gc())
478+
477479
} else {
478480

479481
HTML1 <- "<span style='color:blue;'><b>"
@@ -497,6 +499,7 @@ Convergence_Plot <- function(
497499
HTML1 <- "<span style='color:blue;'><b>"
498500
HTML2 <- "</b></span><span style='color:grey;'>"
499501
HTML3 <- "</span>"
502+
HTML4 <- "&nbsp;&nbsp;&nbsp;&mdash;&nbsp;&nbsp;&nbsp;"
500503
VarsDesc <- tibble::tribble(
501504
~Variable, ~VarDesc,
502505
"bio1", "annual mean temperature",
@@ -525,9 +528,9 @@ Convergence_Plot <- function(
525528
"{HTML1}{stringr::str_to_sentence(Variable)}\\
526529
{HTML2} [{VarDesc}]{HTML3} - {Term}"),
527530
VarDesc = stringr::str_replace(
528-
VarDesc, " - L$", "&nbsp&nbsp&mdash&nbsp&nbspLinear"),
531+
VarDesc, " - L$", paste0(HTML4, "Linear")),
529532
VarDesc = stringr::str_replace(
530-
VarDesc, " - Q$", "&nbsp&nbsp&mdash&nbsp&nbspQuadratic"),
533+
VarDesc, " - Q$", paste0(HTML4, "Quadratic")),
531534
Variable = paste0(Variable, "_", Term),
532535
Term = NULL) %>%
533536
dplyr::bind_rows(LinearTerms)
@@ -629,9 +632,6 @@ Convergence_Plot <- function(
629632

630633
# Prepare working on parallel
631634
IASDT.R::Set_parallel(NCores = min(NCores, nrow(Beta_DF)), Level = 2)
632-
if (.Platform$OS.type == "windows") {
633-
on.exit(try(snow::stopCluster("c1"), silent = TRUE), add = TRUE)
634-
}
635635
on.exit(future::plan("future::sequential", gc = TRUE), add = TRUE)
636636

637637
# # |||||||||||||||||||||||||||||||||||||||||||||||||||||||| ##
@@ -773,7 +773,7 @@ Convergence_Plot <- function(
773773
# # |||||||||||||||||||||||||||||||||||||||||||||||||||||||| ##
774774

775775
# Stopping cluster
776-
IASDT.R::Set_parallel(Stop = TRUE, Cat = TRUE, Level = 2)
776+
IASDT.R::Set_parallel(Stop = TRUE, Level = 2)
777777

778778
rm(Beta_DF, BetaNames, envir = environment())
779779
invisible(gc())
@@ -803,17 +803,15 @@ Convergence_Plot <- function(
803803
# Prepare working on parallel
804804
IASDT.R::Set_parallel(
805805
NCores = min(NCores, nrow(BetaTracePlots_ByVar)), Level = 2)
806-
807-
if (.Platform$OS.type == "windows") {
808-
on.exit(try(snow::stopCluster("c1"), silent = TRUE), add = TRUE)
809-
}
810806
on.exit(future::plan("future::sequential", gc = TRUE), add = TRUE)
811807

812808
# # |||||||||||||||||||||||||||||||||||||||||||||||||||||||| ##
813809

814810
IASDT.R::CatTime("Save plots", Level = 2)
811+
VarNames <- BetaTracePlots_ByVar$Variable
812+
815813
BetaTracePlots_ByVar0 <- future.apply::future_lapply(
816-
X = BetaTracePlots_ByVar$Variable,
814+
X = VarNames,
817815
FUN = function(x) {
818816

819817
VarDesc <- BetaTracePlots_ByVar %>%
@@ -903,16 +901,16 @@ Convergence_Plot <- function(
903901
future.seed = TRUE,
904902
future.globals = c("BetaTracePlots_ByVar", "NRC", "Path_Convergence"),
905903
future.packages = c(
906-
"dplyr", "ggplot2", "magrittr", "purrr", "IASDT.R", "ggtext",
907-
"tibble", "tidyr", "cowplot", "grid"))
904+
"tidyr", "dplyr", "ggplot2", "purrr", "ggtext",
905+
"tibble", "cowplot", "grDevices", "IASDT.R"))
908906

909907
rm(BetaTracePlots_ByVar0, BetaTracePlots_ByVar, envir = environment())
910908
invisible(gc())
911909

912910
# # |||||||||||||||||||||||||||||||||||||||||||||||||||||||| ##
913911

914912
# Stopping cluster
915-
IASDT.R::Set_parallel(Stop = TRUE, Cat = TRUE, Level = 2)
913+
IASDT.R::Set_parallel(Stop = TRUE, Level = 2)
916914

917915
# # ..................................................................... ###
918916

@@ -933,10 +931,6 @@ Convergence_Plot <- function(
933931
# Prepare working on parallel
934932
IASDT.R::Set_parallel(
935933
NCores = min(NCores, nrow(BetaTracePlots_BySp)), Level = 2)
936-
937-
if (.Platform$OS.type == "windows") {
938-
on.exit(try(snow::stopCluster("c1"), silent = TRUE), add = TRUE)
939-
}
940934
on.exit(future::plan("future::sequential", gc = TRUE), add = TRUE)
941935

942936
# # |||||||||||||||||||||||||||||||||||||||||||||||||||||||| ##
@@ -1018,17 +1012,17 @@ Convergence_Plot <- function(
10181012
rm(PlotTitle, envir = environment())
10191013
return(invisible(NULL))
10201014
},
1021-
future.scheduling = 1, future.seed = TRUE,
1015+
future.seed = TRUE,
10221016
future.globals = c(
10231017
"MarginType", "BetaTracePlots_BySp", "Path_Convergence_BySp", "Beta_NRC"),
10241018
future.packages = c(
10251019
"dplyr", "coda", "ggplot2", "ggExtra", "ggtext", "IASDT.R",
1026-
"stringr", "gtools", "cowplot", "purrr"))
1020+
"stringr", "gtools", "cowplot", "purrr", "grDevices"))
10271021

10281022
# # |||||||||||||||||||||||||||||||||||||||||||||||||||||||| ##
10291023

10301024
# Stopping cluster
1031-
IASDT.R::Set_parallel(Stop = TRUE, Cat = TRUE, Level = 2)
1025+
IASDT.R::Set_parallel(Stop = TRUE, Level = 2)
10321026

10331027
rm(BetaTracePlots_BySp0, envir = environment())
10341028

R/Mod_Prep4HPC.R

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,18 +1300,28 @@ Mod_Prep4HPC <- function(
13001300

13011301
IASDT.R::CatTime("Save data to disk")
13021302

1303+
SetChainName <- function(Obj, Chain) {
1304+
if (is.null(Obj) || is.null(Chain)) {
1305+
stop("Obj and Chain cannot be empty", call. = FALSE)
1306+
}
1307+
Obj %>%
1308+
unlist() %>%
1309+
as.vector() %>%
1310+
stats::setNames(paste0("Chain", unlist(Chain)))
1311+
}
1312+
13031313
Model_Info <- Model_Info %>%
13041314
tidyr::nest(
13051315
Post_Path = Post_Path, Path_ModProg = Path_ModProg,
13061316
Chain = Chain, Command_HPC = Command_HPC, Command_WS = Command_WS,
13071317
Post_Missing = Post_Missing) %>%
13081318
dplyr::mutate(
1309-
Post_Path = purrr::map2(Post_Path, Chain, IASDT.R::SetChainName),
1310-
Chain = purrr::map2(Chain, Chain, IASDT.R::SetChainName),
1311-
Command_HPC = purrr::map2(Command_HPC, Chain, IASDT.R::SetChainName),
1312-
Command_WS = purrr::map2(Command_WS, Chain, IASDT.R::SetChainName),
1313-
Path_ModProg = purrr::map2(Path_ModProg, Chain, IASDT.R::SetChainName),
1314-
Post_Missing = purrr::map2(Post_Missing, Chain, IASDT.R::SetChainName),
1319+
Post_Path = purrr::map2(Post_Path, Chain, SetChainName),
1320+
Chain = purrr::map2(Chain, Chain, SetChainName),
1321+
Command_HPC = purrr::map2(Command_HPC, Chain, SetChainName),
1322+
Command_WS = purrr::map2(Command_WS, Chain, SetChainName),
1323+
Path_ModProg = purrr::map2(Path_ModProg, Chain, SetChainName),
1324+
Post_Missing = purrr::map2(Post_Missing, Chain, SetChainName),
13151325
Post_Aligned = NA)
13161326

13171327
save(Model_Info, file = Path_ModelDT)

R/Mod_SetChainName.R

Lines changed: 0 additions & 30 deletions
This file was deleted.

_pkgdown.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ reference:
193193
- AddMissingCols
194194
- subtitle: Other functions
195195
contents:
196-
- SetChainName
196+
- Set_parallel
197197
- CurrOS
198198
- System
199199
- CheckURL

man/SetChainName.Rd

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)