kom*_*an_ 4 parallel-processing r
我一直在互联网上搜索,试图了解并行处理。
他们似乎都假设我有某种循环函数在运行,例如在 N 个核心之间划分并随后组合的数据集的每第 N 行,并且我指向许多并行化apply()函数。
(警告,下面丑陋的代码)
我的情况是我已经在表格上
tempJob <- myFunction(filepath, string.arg1, string.arg2)
Run Code Online (Sandbox Code Playgroud)
其中路径是文件位置,字符串参数是对数据进行排序的各种方式。
我目前的工作流程只是积累了很多
tempjob1 <- myFunction(args)
tempjob2 <- myFunction(other args)
...
tempjobN <- myFunction(some other args here)
# Make a list of all temporary outputs in the global environment
temp.list <- lapply(ls(pattern = "temp"), get)
# Stack them all
df <- rbindlist(temp.list)
# Remove all variables from workspace matching "temp"
rm(list=ls(pattern="temp"))
Run Code Online (Sandbox Code Playgroud)
这些作业是完全独立的,原则上可以在 8 个独立的 R 实例中运行(尽管我猜这会很麻烦)。我如何将前 8 个作业分成 8 个内核,每当一个内核完成其工作并将处理过的数据集返回到全局环境时,它就会简单地执行下一个作业。
使用future包(我是作者),您可以通过对代码进行微小修改来实现您想要的 -对要异步运行的代码使用“未来”分配%<-%而不是常规分配<-。
library("future")
plan(multiprocess)
tempjob1 %<-% myFunction(args)
tempjob2 %<-% myFunction(other args)
...
tempjobN %<-% myFunction(some other args here)
temp.list <- lapply(ls(pattern = "temp"), get)
Run Code Online (Sandbox Code Playgroud)