ots*_*saw 5 random parallel-processing multicore r
我正在使用包中的mclapply函数multicore来进行并行处理.似乎所有子进程都开始为tempfile函数给出的临时文件生成相同的名称.即如果我有四个处理器,
library(multicore)
mclapply(1:4, function(x) tempfile())
Run Code Online (Sandbox Code Playgroud)
将给出四个完全相同的文件名.显然,我需要临时文件不同,以便子进程不会覆盖彼此的文件.当tempfile间接使用时,即调用一些调用tempfile我无法控制文件名的函数.
有没有解决的办法?R(例如foreach)的其他并行处理包有同样的问题吗?
更新:自R 2.14.1以来,这不再是一个问题.
CHANGES IN R VERSION 2.14.0 patched:
[...]
o tempfile() on a Unix-alike now takes the process ID into account.
This is needed with multicore (and as part of parallel) because
the parent and all the children share a session temporary
directory, and they can share the C random number stream used to
produce the uniaue part. Further, two children can call
tempfile() simultaneously.
Run Code Online (Sandbox Code Playgroud)
我相信multicore每个子任务都有一个单独的进程.如果这个假设是正确的,那么你应该可以使用Sys.getpid()"种子"tempfile:
tempfile(pattern=paste("foo", Sys.getpid(), sep=""))
Run Code Online (Sandbox Code Playgroud)
至少现在,我选择通过在.RprofileDaniel 使用 PID 值的建议中使用以下代码来解决这个问题。
assignInNamespace("tempfile.orig", tempfile, ns="base")
.tempfile = function(pattern="file", tmpdir=tempdir())
tempfile.orig(paste(pattern, Sys.getpid(), sep=""), tmpdir)
assignInNamespace("tempfile", .tempfile, ns="base")
Run Code Online (Sandbox Code Playgroud)
显然,对于您要分发的任何软件包来说,这都不是一个好的选择,但对于单个用户的需求来说,它是迄今为止最好的选择,因为它适用于所有情况。