如何使用clusterApply多次并行运行自己的函数

时间:2014-07-18 19:00:19

标签: r snow

我尝试运行自己的功能" EMGMM(y,startmy = 0)"不同参数并行:

require(snow)
library(parallel)

tasks = list(
    job1 = function(y) EMGMM(y=y, startmy = 0),
    job2 = function(y) EMGMM(y=y, startmy = 1),
    job3 = function(y) EMGMM(y=y, startmy = 2)
)

cl = makeCluster( length(tasks) )
clusterExport(cl, list("EMGMM"))

out = clusterApply(cl, tasks, ????)
stopCluster(cl)

但我现在不必打电话给#34; clusterApply"。 y para在每个作业中都是相同的,startmy是我算法的起始段。

1 个答案:

答案 0 :(得分:0)

谢谢:) y是多维随机变量的矩阵

这对我来说很好(它可能不是最好的解决方案):

require(snow)
library(parallel)

tasks = list(
    job1 = function(y) EMGMM(y=y, startmy = 0),
    job2 = function(y) EMGMM(y=y, startmy = 1),
    job3 = function(y) EMGMM(y=y, startmy = 2)
)

cl = makeCluster( length(tasks) )
clusterExport(cl, ls())
clusterExport(cl, "dmvnorm") #needed in EMGMM()

out = clusterApply(cl, tasks, function(f) f(y))

stopCluster(cl)