保存foreach dopar循环的多个输出 [英] Saving multiple outputs of foreach dopar loop
问题描述
我想知道是否可以/如何作为foreach dopar
循环的一部分返回多个输出.
I would like to know if/how it would be possible to return multiple outputs as part of foreach dopar
loop.
让我们举一个非常简单的例子.假设我想在foreach
循环中进行2次操作,并且想为i
的每个值返回或保存这两项操作的结果.
Let's take a very simplistic example. Let's suppose I would like to do 2 operations as part of the foreach
loop, and would like to return or save the results of both operations for each value of i
.
仅返回一个输出,就很简单:
For only one output to return, it would be as simple as:
library(foreach)
library(doParallel)
cl <- makeCluster(3)
registerDoParallel(cl)
oper1 <- foreach(i=1:100000) %dopar% {
i+2
}
oper1
将是一个包含100000个元素的列表,每个元素都是针对每个i值的操作i+2
的结果.
oper1
would be a list with 100000 elements, each element is the result of the operation i+2
for each value of i.
假设现在我想分别返回或保存两个不同操作的结果,例如i+2
和i+3
.我尝试了以下方法:
Suppose now I would like to return or save the results of two different operations separately, e.g. i+2
and i+3
. I tried the following:
oper1 = list()
oper2 <- foreach(i=1:100000) %dopar% {
oper1[[i]] = i+2
return(i+3)
}
希望i+2
的结果将保存在列表oper1
中,并且第二个操作i+3
的结果将由foreach
返回.但是,列表oper1
中没有任何内容!在这种情况下,只有i+3
的结果会从循环中返回.
hoping that the results of i+2
will be saved in the list oper1
, and that the results of the second operation i+3
will be returned by foreach
. However, nothing gets populated in the list oper1
! In this case, only the result of i+3
gets returned from the loop.
有没有办法将两个输出都返回或保存在两个单独的列表中?
Is there any way of returning or saving both outputs in two separate lists?
推荐答案
不要尝试将副作用与foreach或任何其他并行程序包一起使用.而是从列表中的foreach循环的主体返回所有值.如果希望最终结果是两个列表的列表,而不是100,000个列表的列表,则指定一个组合函数以对结果进行转置:
Don't try to use side-effects with foreach or any other parallel program package. Instead, return all of the values from the body of the foreach loop in a list. If you want your final result to be a list of two lists rather than a list of 100,000 lists, then specify a combine function that transposes the results:
comb <- function(x, ...) {
lapply(seq_along(x),
function(i) c(x[[i]], lapply(list(...), function(y) y[[i]])))
}
oper <- foreach(i=1:10, .combine='comb', .multicombine=TRUE,
.init=list(list(), list())) %dopar% {
list(i+2, i+3)
}
oper1 <- oper[[1]]
oper2 <- oper[[2]]
请注意,此合并功能要求使用.init
参数为第一次调用合并功能设置x
的值.
Note that this combine function requires the use of the .init
argument to set the value of x
for the first invocation of the combine function.
这篇关于保存foreach dopar循环的多个输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!