gpt4 book ai didi

R包并行: how to print the output from one node?

转载 作者:行者123 更新时间:2023-12-02 09:21:37 25 4
gpt4 key购买 nike

我正在使用 R 包 parallel 中的函数 parSapply。我对这个函数的调用是这样的:

cl <- makeCluster(3, type="PSOCK",outfile="output.txt")
m<-10
parSapply(cl,as.list(1:m), FUN=function(mtmp){
comp<-0
for (ii in 1:10){
print(ii)
comp<-comp+rnorm(1)
}
return(comp)
})

并行函数打印一条消息以跟进流程。这对于估计函数所需的时间非常有用。使用此代码,每个节点的打印消息都存储在一个 txt 文件 (output.txt) 中,但由于所有节点同时生成消息,因此会被融化。因此,要打印在我的 txt 文件中可读的消息,我只想遵循一个节点的过程。我在想,对于 {1,4,7,10} 中的 mtmp,迭代是在同一个节点上执行的。所以,我尝试添加条件:

if(mtmp%%3==1){print(ii)}

但是,消息再次融化,这表明在 {1,4,7,10} 中对 mtmp 的调用不是在同一节点上执行的。

因此,我的问题是:如何在我的 txt 文件中保存一个节点的所有打印消息,并且只保存这个节点的消息?此外,我希望这个节点能够处理对我的并行化函数的大量调用。

非常感谢您的帮助,

文森特

最佳答案

将每个进程的输出转移到一个单独的文件中:

library(parallel)
cl <- makeCluster(3, type="PSOCK")
#divert to different files:
clusterEvalQ(cl, sink(paste0("E:/temp/output", Sys.getpid(), ".txt")))

m<-10

parSapply(cl,as.list(1:m), FUN=function(mtmp){
comp<-0
for (ii in 1:10){
print(ii)
comp<-comp+rnorm(1)
}
return(comp)
})

stopCluster(cl)

关于R包并行: how to print the output from one node?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42249857/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com