How do I get node or process id when using doParal

2020-06-18 10:23发布

问题:

I am running a parallel process on a 12 node cluster.

And was wondering if there is a way to get the node-id or node-number or node-name during a foreach call?

Something like this:

foreach(i = 1:12, .combine=c) %dopar% {node.name()}

This will be helpful in processing the files.

回答1:

The foreach package doesn't provide any support for a node id or node name, but R has the "sys.info" function, so you could use:

foreach(i = 1:12, .combine=c) %dopar% {
  Sys.info()[['nodename']]
}

To create a unique worker id, you can combine the node name with the process id of the worker:

foreach(i = 1:12, .combine=c) %dopar% {
  paste(Sys.info()[['nodename']], Sys.getpid(), sep='-')
}


回答2:

After a lot of trial and error, I found the following to work:

foreach(i = 1:12, .combine=c) %dopar% {
  Sys.getpid()
}

This gives a unique process ID for each of the nodes, which can be used as the node id.