在R中并行化自己的程序包 [英] parallelize Own Package in R

查看:208
本文介绍了在R中并行化自己的程序包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照其他文章的建议,我在R中编写了自己的程序包,以并行化使用Rcpp编写的函数.我可以加载该程序包,并且一切正常,但是当我使用optimParallel时,会收到消息:

As recommended in other posts I wrote my own package in R to parallelize functions I wrote with Rcpp. I can load the package and everything works, but when I'm using optimParallel, I get the message:

checkForRemoteErrors(val)中的错误: 3个节点产生错误;第一个错误:找不到对象"_EffES_profileLLcpp"

Error in checkForRemoteErrors(val) : 3 nodes produced errors; first error: object '_EffES_profileLLcpp' not found

这是我在做什么:

library(optimParallel)
library(EffES) # EffES is my own package

cl <- makeCluster(detectCores()-1)
clusterEvalQ(cl, library(EffES))
clusterEvalQ(cl, library(optimParallel))
setDefaultCluster(cl = cl)

w.es <- optimParallel(par=rep(0.001,3), profileLLcpp, y=y.test, x=x.test, lower = rep(0.001,3), method = "L-BFGS-B")$par

Error in checkForRemoteErrors(val) : 
  3 nodes produced errors; first error: object '_EffES_profileLLcpp' not found

我做错了什么?

推荐答案

该问题已在optimParallel 0.7-4版本中解决

该版本在CRAN上可用: https://CRAN.R-project.org/package = optimParallel

此帖子中所述, optimParallel()需要一些技巧,以便对参数名称没有限制可以通过...参数传递.当前,这意味着必须在.GlobalEnv中定义传递给optimParallel()的函数,以便正确地找到已编译的代码.

As detailed in this post optimParallel() needs to trick a bit in order to have no restrictions on the argument names that can be passed through the ... argument. Currently, this implies that the function passed to optimParallel() has to be defined in the .GlobalEnv in order to find compiled code properly.

因此,一种解决方法可能是在.GlobalEnv中定义功能:

Hence, a workaround could be to define the function in the .GlobalEnv:

library(optimParallel)
library(EffES)                          # EffES is your own package
cl <- makeCluster(detectCores()-1)
clusterEvalQ(cl, library(EffES))
setDefaultCluster(cl=cl)

f <- function(par, y, x) {
    profileLLcpp(par=par, x=x, y=y)
}
optimParallel(par=rep(0.001,3), f=f, y=y.test, x=x.test, 
              lower = rep(0.001,3), method = "L-BFGS-B")$par

欢迎改进optimParallel()代码的建议.我在这里打开了一个相应的问题.

Suggestions to improve the code of optimParallel() are welcome. I opened a corresponding question here.

这篇关于在R中并行化自己的程序包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆