R包并行:如何从一个节点打印输出? [英] R package parallel: how to print the output from one node?

查看:83
本文介绍了R包并行:如何从一个节点打印输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用R包并行中的parSapply函数. 我对该函数的调用类似于:

I am using the function parSapply from the R package parallel. My call to this function is something like:

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,迭代是在同一节点上执行的.因此,我尝试添加条件:

The parallelized function prints a message to follow the process. This is very useful to estimate the time required by the function. With this code, the printed message for each node are stocken in a txt file (output.txt), but there are melted since all nodes generate messages in a same time. Therefore, to have printed messages that are readable in my txt file, I would like to follow the process for only one node. I was thinking that for mtmp in {1,4,7,10} the iterations was performed on a same node. So, I tried to add the condition:

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

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

but, the messages are again melted, which indicates that calls for mtmp in {1,4,7,10} are not performed on a same node.

因此,我的问题是:如何将一个节点(仅此一个节点)的所有已打印消息保存在txt文件中?另外,我希望该节点能够处理对并行化函数的大量调用.

Therefore, my question is: how can I save in my txt file all the printed messages of one node and only of this one ? In addition, I would like this node is the one which handles the larger number of calls to my parallelized function.

非常感谢您的帮助,

Vincent

推荐答案

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

Divert the output of each process to a separate file:

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包并行:如何从一个节点打印输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆