Skip to content

Commit

Permalink
Now all future_nnn() functions set a label on each future [#15]
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikBengtsson committed Dec 29, 2019
1 parent 1b407b9 commit 0d4afaa
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 34 deletions.
6 changes: 6 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ Package: future.apply

Version: 1.3.0-9000 [2019-12-28]

NEW FEATURES:

* Now all future_nnn() functions set a label on each future that reflects the
name of the future_nnn() function and the index of the chunk. The format
can be controlled by argument 'future.label'.

BUG FIXES:

* future_lapply(X) and future_mapply(FUN, X) would drop 'names' argument of
Expand Down
4 changes: 2 additions & 2 deletions R/future_Map.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#' GPL (>= 2) with 'The R Core Team' as the copyright holder.
#'
#' @rdname future_mapply
future_Map <- function(f, ...) {
future_Map <- function(f, ..., future.label = "future_Map-%d") {
f <- match.fun(f)
future_mapply(FUN = f, ..., SIMPLIFY = FALSE)
future_mapply(FUN = f, ..., SIMPLIFY = FALSE, future.label = future.label)
}
5 changes: 2 additions & 3 deletions R/future_apply.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
#' @example incl/future_apply.R
#'
#' @export
future_apply <- function(X, MARGIN, FUN, ...)
{
future_apply <- function(X, MARGIN, FUN, ..., future.label = "future_apply-%d") {
FUN <- match.fun(FUN)

## Ensure that X is an array object
Expand Down Expand Up @@ -86,7 +85,7 @@ future_apply <- function(X, MARGIN, FUN, ...)
} else
newX <- lapply(1L:d2, FUN = function(i)
array(newX[,i], dim = d.call, dimnames = dn.call))
ans <- future_lapply(newX, FUN = FUN, ...)
ans <- future_lapply(newX, FUN = FUN, ..., future.label = future.label)

## answer dims and dimnames

Expand Down
4 changes: 2 additions & 2 deletions R/future_by.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ future_by.data.frame <- function(data, INDICES, FUN, ..., simplify = TRUE) {



future_by_internal <- function(data, INDICES, FUN, ..., simplify = TRUE, .SUBSETTER, .CALL, .INDICES.NAME) {
future_by_internal <- function(data, INDICES, FUN, ..., simplify = TRUE, .SUBSETTER, .CALL, .INDICES.NAME, future.label = "future_by-%d") {
FUN <- if (!is.null(FUN)) match.fun(FUN)
stop_if_not(is.function(.SUBSETTER))

Expand Down Expand Up @@ -108,7 +108,7 @@ future_by_internal <- function(data, INDICES, FUN, ..., simplify = TRUE, .SUBSET
group <- NULL ## Not needed anymore

grouped_data <- lapply(X = ans[index], FUN = .SUBSETTER)
ans <- future_lapply(X = grouped_data, FUN = FUN, ...)
ans <- future_lapply(X = grouped_data, FUN = FUN, ..., future.label = future.label)
grouped_data <- NULL ## Not needed anymore

ansmat <- array({
Expand Down
4 changes: 2 additions & 2 deletions R/future_eapply.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
#'
#' @rdname future_lapply
#' @export
future_eapply <- function(env, FUN, ..., all.names = FALSE, USE.NAMES = TRUE) {
future_eapply <- function(env, FUN, ..., all.names = FALSE, USE.NAMES = TRUE, future.label = "future_eapply-%d") {
names <- ls(envir = env, all.names = all.names, sorted = FALSE)
X <- mget(names, envir = env, inherits = FALSE)
if (!USE.NAMES) names(X) <- NULL
future_lapply(X = X, FUN = FUN, ...)
future_lapply(X = X, FUN = FUN, ..., future.label = future.label)
}
29 changes: 25 additions & 4 deletions R/future_lapply.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
#' If `Inf`, then all elements are processed in a single future.
#' If `NULL`, then argument `future.scheduling` is used.
#'
#' @param future.label If a character string, then each future is assigned
#' a label `sprintf(future.label, chunk_idx)`. If TRUE, then the
#' same as `future.label = "future_lapply-%d"`. If FALSE, no labels
#' are assigned.
#'
#' @return
#' For `future_lapply()`, a list with same length and names as `X`.
#' See [base::lapply()] for details.
Expand Down Expand Up @@ -134,7 +139,7 @@
#' @importFrom future future resolve values as.FutureGlobals nbrOfWorkers getGlobalsAndPackages FutureError
#' @importFrom utils head str
#' @export
future_lapply <- function(X, FUN, ..., future.stdout = TRUE, future.conditions = NULL, future.globals = TRUE, future.packages = NULL, future.lazy = FALSE, future.seed = FALSE, future.scheduling = 1.0, future.chunk.size = NULL) {
future_lapply <- function(X, FUN, ..., future.stdout = TRUE, future.conditions = NULL, future.globals = TRUE, future.packages = NULL, future.lazy = FALSE, future.seed = FALSE, future.scheduling = 1.0, future.chunk.size = NULL, future.label = "future_lapply-%d") {
stop_if_not(is.function(FUN))

stop_if_not(is.logical(future.stdout), length(future.stdout) == 1L)
Expand All @@ -149,6 +154,9 @@ future_lapply <- function(X, FUN, ..., future.stdout = TRUE, future.conditions =
stop_if_not(length(future.scheduling) == 1L, !is.na(future.scheduling),
is.numeric(future.scheduling) || is.logical(future.scheduling))

stop_if_not(length(future.label) == 1L, !is.na(future.label),
is.logical(future.label) || is.character(future.label))

## Coerce to as.list()?
if (!is.vector(X) || is.object(X)) X <- as.list(X)

Expand Down Expand Up @@ -247,6 +255,17 @@ future_lapply <- function(X, FUN, ..., future.stdout = TRUE, future.conditions =
nchunks <- length(chunks)
fs <- vector("list", length = nchunks)
if (debug) mdebugf("Number of futures (= number of chunks): %d", nchunks)

## Create labels?
if (isTRUE(future.label)) {
future.label <- "future_lapply-%d"
}
if (is.character(future.label)) {
labels <- sprintf(future.label, seq_len(nchunks))
stopifnot(length(labels) == nchunks)
} else {
labels <- NULL
}

if (debug) mdebugf("Launching %d futures (chunks) ...", nchunks)
for (ii in seq_along(chunks)) {
Expand Down Expand Up @@ -304,7 +323,7 @@ future_lapply <- function(X, FUN, ..., future.stdout = TRUE, future.conditions =
if (debug) mdebugf(" - Adjusted option 'future.globals.maxSize': %g -> %d * %g = %g (bytes)", globals.maxSize.default, length(chunk), globals.maxSize.default, getOption("future.globals.maxSize"))
on.exit(options(future.globals.maxSize = globals.maxSize), add = TRUE)
}

## Using RNG seeds or not?
if (is.null(seeds)) {
if (debug) mdebug(" - seeds: <none>")
Expand All @@ -323,7 +342,8 @@ future_lapply <- function(X, FUN, ..., future.stdout = TRUE, future.conditions =
conditions = future.conditions,
globals = globals_ii, packages = packages_ii,
seed = future.seed,
lazy = future.lazy)
lazy = future.lazy,
label = labels[[ii]])
} else {
if (debug) mdebugf(" - seeds: [%d] <seeds>", length(chunk))
globals_ii[["...future.seeds_ii"]] <- seeds[chunk]
Expand All @@ -343,7 +363,8 @@ future_lapply <- function(X, FUN, ..., future.stdout = TRUE, future.conditions =
conditions = future.conditions,
globals = globals_ii, packages = packages_ii,
seed = NULL, ## As seed=FALSE but without the RNG check
lazy = future.lazy)
lazy = future.lazy,
label = labels[[ii]])
}

## Not needed anymore
Expand Down
24 changes: 20 additions & 4 deletions R/future_mapply.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
#' @importFrom future Future future resolve values as.FutureGlobals nbrOfWorkers getGlobalsAndPackages FutureError
#' @importFrom utils head str
#' @export
future_mapply <- function(FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE, USE.NAMES = TRUE, future.stdout = TRUE, future.conditions = NULL, future.globals = TRUE, future.packages = NULL, future.lazy = FALSE, future.seed = FALSE, future.scheduling = 1.0, future.chunk.size = NULL) {
future_mapply <- function(FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE, USE.NAMES = TRUE, future.stdout = TRUE, future.conditions = NULL, future.globals = TRUE, future.packages = NULL, future.lazy = FALSE, future.seed = FALSE, future.scheduling = 1.0, future.chunk.size = NULL, future.label = "future_mapply-%d") {
FUN <- match.fun(FUN)
stop_if_not(is.function(FUN))

Expand Down Expand Up @@ -92,6 +92,9 @@ future_mapply <- function(FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE, USE.NAMES
stop_if_not(length(future.scheduling) == 1L, !is.na(future.scheduling),
is.numeric(future.scheduling) || is.logical(future.scheduling))

stop_if_not(length(future.label) == 1L, !is.na(future.label),
is.logical(future.label) || is.character(future.label))

debug <- getOption("future.debug", FALSE)

if (debug) mdebug("future_mapply() ...")
Expand Down Expand Up @@ -182,7 +185,18 @@ future_mapply <- function(FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE, USE.NAMES
nchunks <- length(chunks)
fs <- vector("list", length = nchunks)
if (debug) mdebugf("Number of futures (= number of chunks): %d", nchunks)


## Create labels?
if (isTRUE(future.label)) {
future.label <- "future_mapply-%d"
}
if (is.character(future.label)) {
labels <- sprintf(future.label, seq_len(nchunks))
stopifnot(length(labels) == nchunks)
} else {
labels <- NULL
}

if (debug) mdebugf("Launching %d futures (chunks) ...", nchunks)
for (ii in seq_along(chunks)) {
chunk <- chunks[[ii]]
Expand Down Expand Up @@ -256,7 +270,8 @@ future_mapply <- function(FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE, USE.NAMES
conditions = future.conditions,
globals = globals_ii, packages = packages_ii,
seed = future.seed,
lazy = future.lazy)
lazy = future.lazy,
label = labels[ii])
} else {
if (debug) mdebugf(" - seeds: [%d] <seeds>", length(chunk))
globals_ii[["...future.seeds_ii"]] <- seeds[chunk]
Expand All @@ -277,7 +292,8 @@ future_mapply <- function(FUN, ..., MoreArgs = NULL, SIMPLIFY = TRUE, USE.NAMES
conditions = future.conditions,
globals = globals_ii, packages = packages_ii,
seed = NULL, ## As seed=FALSE but without the RNG check
lazy = future.lazy)
lazy = future.lazy,
label = labels[ii])
}

## Not needed anymore
Expand Down
5 changes: 3 additions & 2 deletions R/future_replicate.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
#'
#' @rdname future_lapply
future_replicate <- function(n, expr, simplify = "array",
future.seed = TRUE, ...)
future.seed = TRUE, ...,
future.label = "future_replicate-%d")
future_sapply(X = integer(n),
FUN = eval.parent(substitute(function(...)expr)),
simplify = simplify,
future.seed = future.seed, ...)
future.seed = future.seed, ..., future.label = future.label)
4 changes: 2 additions & 2 deletions R/future_sapply.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
#' 'The R Core Team' as the copyright holder.
#'
#' @rdname future_lapply
future_sapply <- function(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE) {
answer <- future_lapply(X = X, FUN = FUN, ...)
future_sapply <- function(X, FUN, ..., simplify = TRUE, USE.NAMES = TRUE, future.label = "future_sapply-%d") {
answer <- future_lapply(X = X, FUN = FUN, ..., future.label = future.label)
if (USE.NAMES && is.character(X) && is.null(names(answer)))
names(answer) <- X

Expand Down
5 changes: 3 additions & 2 deletions R/future_tapply.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
#' @rdname future_lapply
#' @export
future_tapply <- function(X, INDEX, FUN = NULL, ...,
default = NA, simplify = TRUE) {
default = NA, simplify = TRUE,
future.label = "future_tapply-%d") {
FUN <- if (!is.null(FUN))
match.fun(FUN)
if (!is.list(INDEX))
Expand Down Expand Up @@ -49,7 +50,7 @@ future_tapply <- function(X, INDEX, FUN = NULL, ...,
ans <- split(X, f = group)
names(ans) <- NULL
index <- as.logical(lengths(ans))
ans <- future_lapply(X = ans[index], FUN = FUN, ...)
ans <- future_lapply(X = ans[index], FUN = FUN, ..., future.label = future.label)

ansmat <- array({
if (simplify && all(lengths(ans) == 1L)) {
Expand Down
4 changes: 2 additions & 2 deletions R/future_vapply.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#' @export
#'
#' @rdname future_lapply
future_vapply <- function(X, FUN, FUN.VALUE, ..., USE.NAMES = TRUE) {
future_vapply <- function(X, FUN, FUN.VALUE, ..., USE.NAMES = TRUE, future.label = "future_vapply-%d") {
## Coerce to as.list()?
if (!is.vector(X) || is.object(X)) X <- as.list(X)

Expand Down Expand Up @@ -44,7 +44,7 @@ future_vapply <- function(X, FUN, FUN.VALUE, ..., USE.NAMES = TRUE) {
}
stop_if_not(all(dim(value) == dim), typeof(value) %in% valid_types)
value
}, ...)
}, ..., future.label = future.label)

if (!is.null(dim)) {
dim_res <- c(dim, n)
Expand Down
7 changes: 6 additions & 1 deletion man/future_apply.Rd

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

54 changes: 48 additions & 6 deletions man/future_lapply.Rd

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

10 changes: 8 additions & 2 deletions man/future_mapply.Rd

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

0 comments on commit 0d4afaa

Please sign in to comment.