Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.noexport not working as expected #34

Open
mitokic opened this issue May 5, 2022 · 2 comments
Open

.noexport not working as expected #34

mitokic opened this issue May 5, 2022 · 2 comments

Comments

@mitokic
Copy link

mitokic commented May 5, 2022

Hi there,

The .noexport value within the foreach function doesn't seem to work correctly when both the function to loop over and the call to foreach itself live within various functions. Please see the example below.

library(dplyr)
library(foreach)
library(timetk)

# function to call foreach and run in parallel
submit_par <- function(iterator, fn) {
  
  cl <- parallel::makeCluster(3)
  doParallel::registerDoParallel(cl)
  
  temp <- foreach::foreach(i = iterator, 
                           .combine = 'rbind',
                           .packages = c("dplyr"),
                           .export = NULL, 
                           .errorhandling = "stop", 
                           .verbose = FALSE, 
                           .inorder = FALSE, 
                           .multicombine = TRUE, 
                           .noexport = c("combos")
  ) %dopar% {fn(i)}
  
  parallel::stopCluster(cl)
  
  return(temp)
}

# main function that takes some data and calls foreach
outer_fn <- function(input_tbl) {
  
  data <- input_tbl %>%
    dplyr::filter(date > "2010-01-01")
  
  combos <- unique(data$id)
  
  par_fn <- function(i) {
    
    df <- data %>%
      dplyr::filter(id == i)
    
    return(exists("combos"))
  }
  
  output <- submit_par(combos, par_fn)
  
  return(output)
}

# call to function with some example data
outer_fn(timetk::m4_monthly)

Here is what is returned from the outer_fn call

         [,1]
result.1 TRUE
result.2 TRUE
result.3 TRUE
result.4 TRUE

I'm expecting to see all FALSE values, since the object "combos" was specified in the ".noexport" argument so it shouldn't be exported to the cluster. But it turns out it is being exported. Is there anything I need to change with the above functions to ensure certain objects are not exported? I've played around with removing objects from specific environments within the "outer_fn" environment before calling foreach but that turns into a slippery slope if those objects need to be used in any way after the foreach call.

Thanks for your help! I'm trying to migrate my CRAN package (finnts) to run on spark using sparklyr. And there is a limit to the amount of data that can be serialized when running foreach in spark. So it's necessary I can easily remove specific objects from getting exported to compute clusters (either through doparallel or sparklyr).

@bwlewis
Copy link

bwlewis commented May 7, 2022 via email

@bwlewis
Copy link

bwlewis commented Oct 11, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants