r - 并行处理和临时文件

标签 r random parallel-processing multicore

我正在使用 mclapply multicore 中的函数包做并行处理。似乎所有启动的子进程都为 tempfile 给出的临时文件生成相同的名称。功能。即如果我有四个处理器,

library(multicore)
mclapply(1:4, function(x) tempfile())

将给出四个完全相同的文件名。显然,我需要临时文件不同,以便子进程不会覆盖彼此的文件。使用时 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.

最佳答案

我相信 multicore为每个子任务分拆一个单独的进程。如果这个假设是正确的,那么你应该可以使用 Sys.getpid() “播种”临时文件:

tempfile(pattern=paste("foo", Sys.getpid(), sep=""))

关于r - 并行处理和临时文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5262332/

相关文章:

algorithm - 使用有偏随机数生成器的无偏随机数生成器

javascript - 在 JavaScript Math.Floor((Math.Random() * 1) + 0) 中得到 1 的概率

r - Foreach循环无法找到对象

r - 有条件地将列中的子字符串值替换为其他列的子字符串

c# - 从加权列表中选择一个随机项目

r - 显示没有给定字符 (#) 的所有向量

C# 和 SQL 服务器 : deadlock in parallel transactions

algorithm - 在固定的时间内找到元素的索引 O(1)

r - 如何在R中对具有相同名称的向量元素求和?

r - saveRDS 膨胀对象的大小