Skip to content

Commit

Permalink
Merge pull request #255 from TahminaMojumder/BayesianSetupUpdateParallel
Browse files Browse the repository at this point in the history
Updated help file for BayesianSetupGenerateParallel.R
  • Loading branch information
florianhartig committed Nov 8, 2023
2 parents eef2476 + 6575b1c commit 1436e76
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 20 deletions.
16 changes: 5 additions & 11 deletions BayesianTools/R/BayesianSetupGenerateParallel.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
#' Factory to generate a parallel executor of an existing function
#'
#' @author Florian Hartig
#' @param fun function to be changed to parallel execution
#' @param parallel should a parallel R cluster be used or not. If set to T, cores will be detected automatically and n-1 of the available n cores of the machine will be used. Alternatively, you can set the number of cores used by hand
#' @param parallelOptions list containing three lists. First "packages" determines the R packages necessary to run the likelihood function. Second "variables" the objects in the global environment needed to run the likelihood function and third "dlls" the DLLs needed to run the likelihood function (see Details).
#' @note Can also be used to make functions compatible with library sensitivity
#' @details For parallelization, option T means that an automatic parallelization via R is attempted, or "external", in which case it is assumed that the likelihood is already parallelized. In this case it needs to accept a matrix with parameters as columns.
#' Further you can specify the packages, objects and DLLs that are exported to the cluster.
#' By default a copy of your workspace is exported. However, depending on your workspace this can be very inefficient.
#'
#' Alternatively you can specify the environments and packages in the likelihood function (e.g. BayesianTools::VSEM() instead of VSEM()).
#' @param parallel should a parallel R cluster be used? If set to T, the operating system will automatically detect the available cores and n-1 of the available n cores will be used. Alternatively, you can manually set the number of cores to be used
#' @param parallelOptions a list containing three lists. \itemize{\item First, "packages": determines the R packages required to run the likelihood function. \item Second, "variables": the objects in the global environment needed to run the likelihood function. \item Third, "dlls": the DLLs needed to run the likelihood function (see Details).}
#' @note can be used to make functions compatible with library sensitivity
#' @details For parallelization, if option T is selected, an automatic parallelization is tried via R. Alternatively, "external" can be selected on the assumption that the likelihood has already been parallelized. In the latter case, a matrix with parameters as columns must be accepted. You can also specify which packages, objects and DLLs are exported to the cluster. By default, a copy of your workspace is exported, but depending on your workspace, this can be inefficient. As an alternative, you can specify the environments and packages in the likelihood function (e.g. BayesianTools::VSEM() instead of VSEM()).
#' @export
#' @example /inst/examples/generateParallelExecuter.R

#'
generateParallelExecuter <- function(fun, parallel = F, parallelOptions = list(variables = "all", packages = "all", dlls = NULL)){

if (parallel == F){
Expand All @@ -27,7 +22,6 @@ generateParallelExecuter <- function(fun, parallel = F, parallelOptions = list(v
#library(foreach)
#library(iterators)
# library(parallel)

if (parallel == T | parallel == "auto"){
cores <- parallel::detectCores() - 1
} else if (is.numeric(parallel)){
Expand Down
1 change: 1 addition & 0 deletions BayesianTools/R/classBayesianSetup.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

#' Creates a standardized collection of prior, likelihood and posterior functions, including error checks etc.
#' @author Florian Hartig, Tankred Ott
#' @param likelihood log likelihood density function
Expand Down
12 changes: 4 additions & 8 deletions BayesianTools/man/generateParallelExecuter.Rd

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

2 changes: 1 addition & 1 deletion BayesianTools/vignettes/BayesianTools.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ In the first case, you want to parallelize n internal (not total chains) on n co

```{r, eval = FALSE}
## n = Number of cores
n=2
n = 2
x <- c(1:10)
likelihood <- function(param) return(sum(dnorm(x, mean = param, log = T)))
bayesianSetup <- createBayesianSetup(likelihood, parallel = n, lower = -5, upper = 5)
Expand Down

0 comments on commit 1436e76

Please sign in to comment.