rparallel-processingr-futurefurrr

How can I correctly use the cluster plan in the R future (furrr) package


I am currently using furrr to create a more organized execution of my model. I use a data.frame to pass parameters to a function in a orderly way, and then using the furrr::future_map() to map a function across all the parameters. The function works flawlessly when using the sequential and multicore futures on my local Machine (OSX).

Now, I want to test my code creating my own cluster of AWS instances (just as shown here).

I created a function using the linked article code:

make_cluster_ec2  <- function(public_ip){
  ssh_private_key_file  <-  Sys.getenv('PEM_PATH')
  github_pac  <-  Sys.getenv('PAC')

  cl_multi <- future::makeClusterPSOCK(
  workers = public_ip,
  user = "ubuntu",
  rshopts = c(
    "-o", "StrictHostKeyChecking=no",
    "-o", "IdentitiesOnly=yes",
    "-i", ssh_private_key_file
  ),
  rscript_args = c(
    "-e", shQuote("local({p <- Sys.getenv('R_LIBS_USER'); dir.create(p, recursive = TRUE, showWarnings = FALSE); .libPaths(p)})"),
    "-e", shQuote("install.packages('devtools')"),
    "-e", shQuote(glue::glue("devtools::install_github('user/repo', auth_token = '{github_pac}')"))
  ),
  dryrun = FALSE)

  return(cl_multi)

}

Then, I create the cluster object and then check that is connected to the right instance

public_ids <- c('public_ip_1', 'public_ip_2')
cls <- make_cluster_ec2(public_ids)
f <- future(Sys.info())

And when I print f I get the specs of one of my remote instances, which indicates the socket is correctly connected:

> value(f)
                                      sysname
                                      "Linux"
                                      release
                            "4.15.0-1037-aws"
                                      version
"#39-Ubuntu SMP Tue Apr 16 08:09:09 UTC 2019"
                                     nodename
                           "ip-xxx-xx-xx-xxx"
                                      machine
                                     "x86_64"
                                        login
                                     "ubuntu"
                                         user
                                     "ubuntu"
                               effective_user
                                     "ubuntu"

But when I run my code using my cluster plan:

plan(list(tweak(cluster, workers = cls), multisession))
  parameter_df %>%
  mutate(model_traj =  furrr::future_pmap(list('lat' = latitude,
                               'lon' = longitude,
                               'height' = stack_height,
                               'name_source' = facility_name,
                               'id_source' = facility_id,
                               'duration' = duration,
                               'days' = seq_dates,
                               'daily_hours' = daily_hours,
                               'direction' = 'forward',
                               'met_type' = 'reanalysis',
                               'met_dir' = here::here('met'),
                               'exec_dir' = here::here("Hysplit4/exec"),
                               'cred'= list(creds)),
                           dirtywind::hysplit_trajectory,
                           .progress = TRUE)
  )

I get the following error:

Error in file(temp_file, "a") : cannot open the connection
In addition: Warning message:
In file(temp_file, "a") :
  cannot open file '/var/folders/rc/rbmg32js2qlf4d7cd4ts6x6h0000gn/T//RtmpPvdbV3/filecf23390c093.txt': No such file or directory

I can not figure out what is happening under the hood, and I can not traceback() the error either from my remote machines. I have test the connection with the examples in the article and things seem to run correctly. I am wondering why is trying to create a tempdir during the execution. What am I missing here?

(This is also an issue in the furrr repo)


Solution

  • Disable the progress bar, i.e. don't specify .progress = TRUE.

    This is because .progress = TRUE assumes your R workers can write to a temporary file that the main R process created. This is typically only possible when you parallelize on the same machine.

    A smaller example of this error is:

    library(future)
    ## Set up a cluster with one worker running on another machine
    cl <- makeClusterPSOCK(workers = "node2")
    plan(cluster, workers = cl)
    
    y <- furrr::future_map(1:2, identity, .progress = FALSE)
    str(y)
    ## List of 2
    ##  $ : int 1
    ##  $ : int 2
    
    y <- furrr::future_map(1:2, identity, .progress = TRUE)
    ## Error in file(temp_file, "a") : cannot open the connection
    ## In addition: Warning message:
    ## In file(temp_file, "a") :
    ##   cannot open file '/tmp/henrik/Rtmp1HkyJ8/file4c4b864a028ac.txt': No such file or directory
    

    If you want to get near-live progress updates, you can instead use the progressr package. It works with the Futureverse, including furrr. See https://progressr.futureverse.org/articles/progressr-intro.html#future_map---parallel-purrrmap for an example.

    EDIT 2023-07-06: Mention progressr.