在函数内使用parLapply和clusterExport [英] Using parLapply and clusterExport inside a function

查看:131
本文介绍了在函数内使用parLapply和clusterExport的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里问了一个相关的问题,并且响应效果很好:使用parallel的parLapply:无法访问并行代码中的变量

I asked a related question here and the response worked well: using parallel's parLapply: unable to access variables within parallel code

问题是,当我尝试在函数内部使用答案时,它将无法正常工作,因为我认为默认环境为clusterExport.我已经阅读了小插图并查看了帮助文件,但知识库的知识却非常有限.我使用parLapply的方式我希望它的行为与lapply相似,但似乎没有.

The problem is when I try to use the answer inside of the function it won't work as I think it has to the default environment of clusterExport. I've read the vignette and looked at the help file but am approaching this with a very limited knowledge base. The way I used parLapply I expected it to behave similar to lapply but it doesn't appear to.

这是我的尝试:

par.test <- function(text.var, gc.rate=10){ 
    ntv <- length(text.var)
    require(parallel)
    pos <-  function(i) {
        paste(sapply(strsplit(tolower(i), " "), nchar), collapse=" | ")
    }
    cl <- makeCluster(mc <- getOption("cl.cores", 4))
    clusterExport(cl=cl, varlist=c("text.var", "ntv", "gc.rate", "pos"))
    parLapply(cl, seq_len(ntv), function(i) {
            x <- pos(text.var[i])
            if (i%%gc.rate==0) gc()
            return(x)
        }
    )
}

par.test(rep("I like cake and ice cream so much!", 20))

#gives this error message
> par.test(rep("I like cake and ice cream so much!", 20))
Error in get(name, envir = envir) : object 'text.var' not found

推荐答案

默认情况下,clusterExport.GlobalEnv中查找在varlist中命名的要导出的对象.如果您的对象不在.GlobalEnv中,则必须告诉clusterExport它可以在哪个环境中找到这些对象.

By default clusterExport looks in the .GlobalEnv for objects to export that are named in varlist. If your objects are not in the .GlobalEnv, you must tell clusterExport in which environment it can find those objects.

您可以将clusterExport更改为以下内容(我没有测试过,但是您说的是注释中的内容)

You can change your clusterExport to the following (which I didn't test, but you said works in the comments)

clusterExport(cl=cl, varlist=c("text.var", "ntv", "gc.rate", "pos"), envir=environment())

这样,它将在函数的环境中查找要导出的对象.

This way, it will look in the function's environment for the objects to export.

这篇关于在函数内使用parLapply和clusterExport的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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