From feb47aba498b411d4c9d2bf726fb5d86e7091508 Mon Sep 17 00:00:00 2001 From: Dongchen Zhang Date: Thu, 13 Jul 2023 15:20:33 -0400 Subject: [PATCH] Export the GEF.Block.Nimble code, and use the future_map function instead of the foreach function for the parallelization. --- modules/assim.sequential/NAMESPACE | 2 +- .../assim.sequential/R/Analysis_sda_block.R | 21 +++++-------------- modules/assim.sequential/R/Nimble_codes.R | 1 + 3 files changed, 7 insertions(+), 17 deletions(-) diff --git a/modules/assim.sequential/NAMESPACE b/modules/assim.sequential/NAMESPACE index c352ba52faa..9e0ba94474a 100644 --- a/modules/assim.sequential/NAMESPACE +++ b/modules/assim.sequential/NAMESPACE @@ -9,6 +9,7 @@ export(Create_Site_PFT_CSV) export(EnKF) export(EnKF.MultiSite) export(GEF) +export(GEF.Block.Nimble) export(GEF.MultiSite) export(GEF.MultiSite.Nimble) export(Local.support) @@ -59,7 +60,6 @@ import(furrr) import(lubridate) import(nimble) importFrom(dplyr,"%>%") -importFrom(foreach,"%dopar%") importFrom(lubridate,"%m+%") importFrom(magrittr,"%>%") importFrom(rlang,.data) diff --git a/modules/assim.sequential/R/Analysis_sda_block.R b/modules/assim.sequential/R/Analysis_sda_block.R index b24ff3881b8..24633954953 100644 --- a/modules/assim.sequential/R/Analysis_sda_block.R +++ b/modules/assim.sequential/R/Analysis_sda_block.R @@ -15,7 +15,6 @@ ##' @description This function provides the block-based MCMC sampling approach. ##' ##' @return It returns the `build.block.xy` object and the analysis results. -##' @importFrom foreach %dopar% ##' @importFrom dplyr %>% analysis_sda_block <- function (settings, block.list.all, X, obs.mean, obs.cov, t, nt, MCMC.args) { #convert from vector values to block lists. @@ -51,26 +50,16 @@ analysis_sda_block <- function (settings, block.list.all, X, obs.mean, obs.cov, }) #parallel for loop over each block. - #initialize parallel settings. - cores <- parallel::detectCores() - cl <- parallel::makeCluster(cores) - doSNOW::registerDoSNOW(cl) - - #progress bar - pb <- utils::txtProgressBar(min=1, max=length(block.list.all[[t]]), style=3) - progress <- function(n) utils::setTxtProgressBar(pb, n) - opts <- list(progress=progress) PEcAn.logger::logger.info(paste0("Running MCMC ", "for ", length(block.list.all[[t]]), " blocks")) - - if ("try-error" %in% class(try(block.list.all[[t]] <- foreach::foreach(block = block.list.all[[t]], .packages="Kendall", .options.snow=opts) %dopar% { - library(nimble) - return(MCMC_block_function(block)) - }))) { + if ("try-error" %in% class(try(block.list.all[[t]] <- furrr::future_map(block.list.all[[t]], MCMC_block_function, .progress = T)))) { PEcAn.logger::logger.error("Something wrong within the MCMC_block_function function.") } + PEcAn.logger::logger.info("Completed!") #convert from block lists to vector values. - V <- block.2.vector(block.list.all[[t]], X, H) + if ("try-error" %in% class(try(V <- block.2.vector(block.list.all[[t]], X, H)))) { + PEcAn.logger::logger.error("Something wrong within the block.2.vector function.") + } #return values return(list(block.list.all = block.list.all, diff --git a/modules/assim.sequential/R/Nimble_codes.R b/modules/assim.sequential/R/Nimble_codes.R index f33f8ac2835..4b183647a16 100644 --- a/modules/assim.sequential/R/Nimble_codes.R +++ b/modules/assim.sequential/R/Nimble_codes.R @@ -223,6 +223,7 @@ GEF.MultiSite.Nimble <- nimbleCode({ #GEF.Block.Nimble--This does the block-based SDA ------------------------------------- #' block-based TWEnF #' @format TBD +#' @export GEF.Block.Nimble <- nimbleCode({ #I think the blocked nimble has to be implemented and used instead of a long vector sampling. #1) due to the convergence of X.mod.