保存 foreach dopar 循环的多个输出 [英] Saving multiple outputs of foreach dopar loop
问题描述
我想知道是否/如何将多个输出作为 foreach dopar
循环的一部分返回.
让我们举一个非常简单的例子.假设我想在 foreach
循环中执行 2 个操作,并且想为 i
的每个值返回或保存这两个操作的结果.>
只返回一个输出,就这么简单:
库(foreach)库(doParallel)cl <- makeCluster(3)registerDoParallel(cl)oper1 <- foreach(i=1:100000) %dopar% {我+2}
oper1
将是一个包含 100000 个元素的列表,每个元素都是对 i 的每个值进行 i+2
操作的结果.
假设现在我想分别返回或保存两个不同操作的结果,例如i+2
和 i+3
.我尝试了以下方法:
oper1 = list()oper2 <- foreach(i=1:100000) %dopar% {oper1[[i]] = i+2返回(i+3)}
希望i+2
的结果保存在列表oper1
中,第二次操作的结果i+3
> 将由 foreach
返回.但是,列表 oper1
中没有填充任何内容!在这种情况下,循环只返回 i+3
的结果.
有没有办法将两个输出返回或保存在两个单独的列表中?
不要尝试对 foreach 或任何其他并行程序包使用副作用.相反,从列表中的 foreach 循环体中返回所有值.如果您希望最终结果是两个列表的列表而不是 100,000 个列表的列表,请指定一个组合函数来转置结果:
comb <- function(x, ...) {lapply(seq_along(x),函数(i) c(x[[i]], lapply(list(...), function(y) y[[i]])))}操作 <- foreach(i=1:10, .combine='comb', .multicombine=TRUE,.init=list(list(), list())) %dopar% {列表(i+2, i+3)}oper1 <- oper[[1]]oper2 <- oper[[2]]
请注意,此组合函数需要使用 .init
参数为组合函数的第一次调用设置 x
的值.
I would like to know if/how it would be possible to return multiple outputs as part of foreach dopar
loop.
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
would be a list with 100000 elements, each element is the result of the operation i+2
for each value of i.
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)
}
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?
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]]
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屋!