并行使用大量排列:组合 iterpc 和 foreach
Using large numbers of permutations in parallel: combining iterpc and foreach
Iterpc 从同一点开始每个循环。这产生了一个有趣但令人沮丧的问题,如下所示:
####Load Packages:
library("doParallel")
library("foreach")
library("iterpc")
####Define variables:
n<-2
precision<-0.1
support<-matrix(seq(0+precision,1-precision,by=precision), ncol=1)
nodes<-2 #preparing for multicore.
cl<-makeCluster(nodes)
####Prep iterations
I<-iterpc(table(support),n, ordered=TRUE,replace=FALSE)
steps<-((factorial(length(support)) / factorial(length(support)-n)))/n
####Run loop to get the combined values:
registerDoParallel(cl)
support_n<-foreach(m=1:n,.packages="iterpc", .combine='cbind') %dopar% {
t(getnext(I,steps))
} #????
哪个returns
support_n
我希望这会 运行 每个集合并行,将一半的排列分配给每个节点。但是,它只进行了前半部分的排列……两次。 ([1] 等于 [37]。)如何得到 return 所有排列并将它们并行组合?
假设会有任意多的排列,因此内存管理和速度很重要。
之前的研究:All possible permutations for large n
经过进一步调查,我相信以下命令确实是并行执行的。
registerDoParallel(cl)
system.time(
support_n<-foreach(a=getnext(I,d=(2*steps)),.combine='cbind') %dopar% a
)
support_n<-t(support_n)
感谢您的帮助。
仅供会像我一样通过搜索"foreach iterpc R"来到这里的任何人。
您标记为已接受答案的方法与
并没有太大区别
result <- foreach(a=1:10) %dopar% {
a
}
因为 a=getnext(I,d=(2*steps))
将简单地 return 第一个 2*steps
组合,然后 foreach
包将并行迭代这些组合。
当您有非常多的组合 return 由 iterpc
编辑(它是为它构建的)时,您实际上不能使用这种方法。
在那种情况下,我认为唯一可以做的就是在 iterpc
对象上编写迭代器包装器。
# register parallel backend
library(doParallel)
registerDoParallel(cores = 3)
#create iterpc object
library(iterpc)
combinations <- iterpc(4,2)
library(iterators)
iterpc_iterator <- function(iterpc_object, iteration_length) {
# one's own function of nextElement() because iterpc
# returns NULL on finished iteration on subsequent getnext() invocation
# but not 'StopIteration'
nextEl <- function() {
if (iteration_length > 0)
iteration_length <<- iteration_length - 1
else
stop('StopIteration')
getnext(iterpc_object)
}
obj <- list(nextElem=nextEl)
class(obj) <- c('irep', 'abstractiter', 'iter')
obj
}
it <- iterpc_iterator(combinations, getlength(combinations))
library(foreach)
result <- foreach(i=it) %dopar% {
i
}
您可以简单地使用 iterpc::iter_wrapper。
您示例中的相关行:
support_n <-foreach(a = iter_wrapper(I), .combine='cbind') %dopar% a
Iterpc 从同一点开始每个循环。这产生了一个有趣但令人沮丧的问题,如下所示:
####Load Packages:
library("doParallel")
library("foreach")
library("iterpc")
####Define variables:
n<-2
precision<-0.1
support<-matrix(seq(0+precision,1-precision,by=precision), ncol=1)
nodes<-2 #preparing for multicore.
cl<-makeCluster(nodes)
####Prep iterations
I<-iterpc(table(support),n, ordered=TRUE,replace=FALSE)
steps<-((factorial(length(support)) / factorial(length(support)-n)))/n
####Run loop to get the combined values:
registerDoParallel(cl)
support_n<-foreach(m=1:n,.packages="iterpc", .combine='cbind') %dopar% {
t(getnext(I,steps))
} #????
哪个returns
support_n
我希望这会 运行 每个集合并行,将一半的排列分配给每个节点。但是,它只进行了前半部分的排列……两次。 ([1] 等于 [37]。)如何得到 return 所有排列并将它们并行组合?
假设会有任意多的排列,因此内存管理和速度很重要。
之前的研究:All possible permutations for large n
经过进一步调查,我相信以下命令确实是并行执行的。
registerDoParallel(cl)
system.time(
support_n<-foreach(a=getnext(I,d=(2*steps)),.combine='cbind') %dopar% a
)
support_n<-t(support_n)
感谢您的帮助。
仅供会像我一样通过搜索"foreach iterpc R"来到这里的任何人。 您标记为已接受答案的方法与
并没有太大区别result <- foreach(a=1:10) %dopar% {
a
}
因为 a=getnext(I,d=(2*steps))
将简单地 return 第一个 2*steps
组合,然后 foreach
包将并行迭代这些组合。
当您有非常多的组合 return 由 iterpc
编辑(它是为它构建的)时,您实际上不能使用这种方法。
在那种情况下,我认为唯一可以做的就是在 iterpc
对象上编写迭代器包装器。
# register parallel backend
library(doParallel)
registerDoParallel(cores = 3)
#create iterpc object
library(iterpc)
combinations <- iterpc(4,2)
library(iterators)
iterpc_iterator <- function(iterpc_object, iteration_length) {
# one's own function of nextElement() because iterpc
# returns NULL on finished iteration on subsequent getnext() invocation
# but not 'StopIteration'
nextEl <- function() {
if (iteration_length > 0)
iteration_length <<- iteration_length - 1
else
stop('StopIteration')
getnext(iterpc_object)
}
obj <- list(nextElem=nextEl)
class(obj) <- c('irep', 'abstractiter', 'iter')
obj
}
it <- iterpc_iterator(combinations, getlength(combinations))
library(foreach)
result <- foreach(i=it) %dopar% {
i
}
您可以简单地使用 iterpc::iter_wrapper。 您示例中的相关行:
support_n <-foreach(a = iter_wrapper(I), .combine='cbind') %dopar% a