如何在 R future (furrr) 包中正确使用集群计划

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

我目前正在使用 furrr 来创建更有条理的模型执行。我使用 data.frame 以有序的方式将参数传递给函数,然后使用 furrr::future_map() 将函数映射到所有参数。在我的本地机器 (OSX) 上使用顺序和多核 futures 时,该函数可以完美运行。

现在,我想测试创建自己的 AWS 实例集群的代码(如图 here 所示)。

我使用链接的文章代码创建了一个函数:

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)

}

然后,我创建集群对象,然后检查是否连接到正确的实例

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

当我打印 f 时,我得到了我的一个远程实例的规格,这表明套接字已正确连接:

> 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"

但是当我 运行 我的代码使用我的集群计划时:

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)
  )

我收到以下错误:

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

我无法弄清楚引擎盖下发生了什么,我也无法 traceback() 来自我的远程机器的错误。我已经测试了与文章中示例的连接,事情似乎 运行 正确。我想知道为什么要在执行期间尝试创建 tempdir 。我在这里错过了什么?

(这也是 furrr 存储库中的一个 issue

禁用进度条,即不指定.progress = TRUE

这是因为.progress = TRUEassumes your R workers can write to the a temporary file that the main R process created。这通常只有在同一台机器上并行化时才有可能。

此错误的一个较小示例是:

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