4

我正在尝试自学如何使用 Snowfall 包,当我尝试调用第二个函数的函数时遇到了以下问题(这是我最终想要实现的简化用例)。

我目前有:

library (snowfall)
f1 <- function(n) { return (n-1) }
f2 <- function(n) { return (f1(n)^2) }
# initialize cluster
sfInit (parallel=TRUE , cpus=4)
# parallel computing
result <- sfLapply(1:10, f2)
# stop cluster
sfStop ()

但我收到错误消息:

Error in checkForRemoteErrors(val) :
  4 nodes produced errors; first error: could not find function "f1"

但是,如果我然后运行 ​​lapply(1:10, f2) 我会收到以下输出:

lapply(1:10, f2)
[[1]]
[1] 0

[[2]]
[1] 1

[[3]]
[1] 4

[[4]]
[1] 9

[[5]]
[1] 16

[[6]]
[1] 25

[[7]]
[1] 36

[[8]]
[1] 49

[[9]]
[1] 64

[[10]]
[1] 81

我最终想用降雪来实现一个多维最小化问题的并行搜索程序,所以肯定需要能够从主并行函数调用函数。

有人能帮忙吗?

4

2 回答 2

4

You need to export the f1 function to the workers using the sfExport function between sfInit and sfLapply:

sfExport('f1')

This is the snowfall equivalent to the snow clusterExport function.

To export multiple variables, you can either use multiple arguments or the list argument:

sfExport('f1', 'x')
sfExport(list=c('f1', 'x'))

To export all variables in your global environment, use sfExportAll:

sfExportAll()
于 2013-09-09T18:58:50.980 回答
0

I do not know the specifics of the snowfall package, but I do know that when doing parallel computing in R, you need to assume that each core you pass information to is a brand-new R instance. Meaning, regardless of what you pass through your host prior to registering clusters, there is just the vanilla instance of R on your clones.

It looks like there is a call sfExportAll() which will put all of your global variables onto the clone instances. I would try it out, but I can't run snowfall on my Windows machine.

于 2013-09-09T18:58:12.073 回答