简体   繁体   English

在R中并行运行功能

[英]Running functions in parallel in R

I have two functions fun1 and fun2 . 我有两个功能fun1fun2 I would like to run them in parallel in my R script. 我想在我的R脚本中并行运行它们。 When they complete I'd like to use their output in further processing sequentially. 他们完成后,我想在后续处理中使用它们的输出。 I guess this means that I need to wait for them to complete. 我想这意味着我需要等待它们完成。

I would very much appreciate any tips or pointers. 我将不胜感激任何提示或指示。

Thank You. 谢谢。

The parallel packages for R are mostly designed to run the same function or expression in parallel with possibly different data. R的并行程序包主要设计为与可能不同的数据并行运行相同的函数或表达式。 They are not designed out of the box to run different functions in a simple way. 它们不是开箱即用的,不能以简单的方式运行不同的功能。

It would be nice to have a function like DAGapply that you could specify a Directed Acyclic Graph (what pieces depend on which other pieces) and all the pieces that could be run in parallel would be, then as the initial pieces returned, the pieces whose dependencies have all completed would then be submitted to run in parallel, but I don't see that functionality anywhere yet. 最好有一个DAGapply之类的函数,您可以指定一个有向非循环图(哪些块取决于其他块),并且所有可以并行运行的块都是,然后作为初始块返回,依赖项全部完成后,将被提交以并行运行,但是我在任何地方都看不到该功能。 I have thought of writing it myself and can see the main pieces to call (but they are non-exported and could change at any time) but have not found the time yet. 我已经考虑过自己编写它,并且可以看到要调用的主要部分(但是它们是未导出的,并且可以随时更改),但尚未找到时间。

A work around that you could use now would be to write your own function that take a single argument and if that argument is 1 then it calls your first function and if the argument in 2 it calls your second function. 您现在可以使用的解决方法是编写一个带有单个参数的函数,如果该参数为1,则它将调用您的第一个函数,如果2中的参数将调用您的第二个函数。 This new function could then be passed to a parallel function (such as parLapply in the parallel package) and it would in turn run your functions in parallel and wait for both to finish. 然后可以将此新函数传递给并行函数(例如并行程序包中的parLapply),然后它将并行运行您的函数,并等待两个函数完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM