Skip to content

Commit

Permalink
Parallelise forecast() across series
Browse files Browse the repository at this point in the history
Ref: #268
  • Loading branch information
mitchelloharawild committed Sep 28, 2020
1 parent f4c96de commit 907db8b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
9 changes: 6 additions & 3 deletions R/forecast.R
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,12 @@ forecast.mdl_df <- function(object, new_data = NULL, h = NULL,

#' @export
forecast.lst_mdl <- function(object, new_data = NULL, key_data, ...){
map2(object,
new_data %||% rep(list(NULL), length.out = length(object)),
forecast, ...)
mapply_maybe_parallel(
.f = forecast,
object,
new_data %||% rep(list(NULL), length.out = length(object)),
MoreArgs = dots_list(...)
)
}

#' @rdname forecast
Expand Down
22 changes: 22 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,26 @@ flatten_with_names <- function (x, sep = "_") {
if(!is.null(names(x))) set_names(x, paste(nm, names(x), sep = sep)) else x
})
flatten(unname(map(x, flatten_with_names, sep = sep)))
}

mapply_maybe_parallel <- function (.f, ..., MoreArgs = list(), SIMPLIFY = FALSE) {
if(is_attached("package:future")){
require_package("future.apply")

future.apply::future_mapply(
FUN = .f,
...,
MoreArgs = MoreArgs,
SIMPLIFY = SIMPLIFY,
future.globals = FALSE
)
}
else{
mapply(
FUN = .f,
...,
MoreArgs = static_args,
SIMPLIFY = SIMPLIFY
)
}
}

0 comments on commit 907db8b

Please sign in to comment.