Skip to content

Commit

Permalink
Inject string comments when building up future expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikBengtsson committed Feb 9, 2025
1 parent f194097 commit dfb6848
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: future.apply
Version: 1.11.3-9004
Version: 1.11.3-9005
Title: Apply Function to Elements in Parallel using Futures
Depends:
R (>= 3.2.0),
Expand Down
13 changes: 12 additions & 1 deletion R/future_lapply.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,15 @@
#' @export
future_lapply <- local({
tmpl_expr <- bquote_compile({
"# future.apply::future_lapply(): process chunk of elements"
lapply(seq_along(...future.elements_ii), FUN = function(jj) {
...future.X_jj <- ...future.elements_ii[[jj]]
.(expr_FUN)
})
})

tmpl_expr_with_rng <- bquote_compile({
"# future.apply::future_lapply(): process chunk of elements while setting random seeds"
lapply(seq_along(...future.elements_ii), FUN = function(jj) {
...future.X_jj <- ...future.elements_ii[[jj]]
assign(".Random.seed", ...future.seeds_ii[[jj]], envir = globalenv(), inherits = FALSE)
Expand Down Expand Up @@ -196,11 +198,20 @@ future_lapply <- local({
...future.FUN <- NULL ## To please R CMD check

## Does FUN() rely on '...' being a global?
global_dotdotdot <- ("..." %in% findGlobals(FUN, dotdotdot = "return"))
## If so, make sure to *not* pass '...' to FUN()
globals_FUN <- findGlobals(FUN, dotdotdot = "return")
if (debug) {
mdebugf("- Globals in FUN(): [n=%d] %s", length(globals_FUN), paste(sQuote(globals_FUN), collapse = ", "))
}
global_dotdotdot <- ("..." %in% globals_FUN)
if (global_dotdotdot) {
## Don't pass '...' to FUN()
expr_FUN <- quote(...future.FUN(...future.X_jj))
if (debug) mdebugf(" => Will not pass '...' to FUN(): %s", deparse(expr_FUN))
} else {
## Okay to pass '...' to FUN()
expr_FUN <- quote(...future.FUN(...future.X_jj, ...))
if (debug) mdebugf(" => Will pass '...' to FUN(): %s", deparse(expr_FUN))
}

## With or without RNG?
Expand Down
17 changes: 14 additions & 3 deletions R/future_xapply.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#' @importFrom future Future nbrOfWorkers future resolve value as.FutureGlobals getGlobalsAndPackages
future_xapply <- local({
tmpl_expr_options <- bquote_compile({
"# future.apply:::future_xapply(): preserve future option"
...future.globals.maxSize.org <- getOption("future.globals.maxSize")
if (!identical(...future.globals.maxSize.org, ...future.globals.maxSize)) {
oopts <- options(future.globals.maxSize = ...future.globals.maxSize)
Expand All @@ -10,6 +11,9 @@ future_xapply <- local({
})

function(FUN, nX, chunk_args, args = NULL, MoreArgs = NULL, expr, envir = parent.frame(), future.envir, future.globals, future.packages, future.scheduling, future.chunk.size, future.stdout, future.conditions, future.seed, future.label, get_chunk, fcn_name, args_name, ..., debug) {
fcn_name <- "future_xapply"
if (debug) mdebugf("%s() ...", fcn_name)

stop_if_not(is.function(FUN))

stop_if_not(is.logical(future.stdout), length(future.stdout) == 1L)
Expand Down Expand Up @@ -96,6 +100,10 @@ future_xapply <- local({

## At this point a globals should be resolved and we should know their total size
## stop_if_not(attr(globals, "resolved"), !is.na(attr(globals, "total_size")))
if (debug) {
mdebugf(" - Globals pass to each chunk: [n=%d] %s", length(globals), commaq(names(globals)))
mstr(globals)
}

## To please R CMD check
...future.FUN <- ...future.elements_ii <- ...future.seeds_ii <-
Expand All @@ -104,7 +112,7 @@ future_xapply <- local({
globals.maxSize <- getOption("future.globals.maxSize")
globals.maxSize.default <- globals.maxSize
if (is.null(globals.maxSize.default)) globals.maxSize.default <- 500 * 1024^2

nchunks <- length(chunks)
if (debug) mdebugf("Number of futures (= number of chunks): %d", nchunks)

Expand Down Expand Up @@ -167,7 +175,7 @@ future_xapply <- local({
"...future.seeds_ii"), names(globals_args))
if (length(reserved) > 0) {
stop("Detected globals in '%s' using reserved variables names: ",
args_name, paste(sQuote(reserved), collapse = ", "))
args_name, commaq(reserved))
}
globals_args <- as.FutureGlobals(globals_args)
globals_ii <- unique(c(globals_ii, globals_args))
Expand Down Expand Up @@ -327,7 +335,10 @@ future_xapply <- local({
}

if (debug) mdebugf("Reducing values from %d chunks ... DONE", nchunks)


fcn_name <- "future_xapply"
if (debug) mdebugf("%s() ... DONE", fcn_name)

values
} ## future_xapply()
})

0 comments on commit dfb6848

Please sign in to comment.