R sys.child中的system.time和parallel包为0 [英] system.time and parallel package in R sys.child is 0
问题描述
我想在R中使用system.time来获取多核函数上的总CPU时间.问题在于,system.time显然无法捕获并行程序包所产生的子进程所花费的CPU时间.
I would like to use system.time in R to get the total CPU time on a multicore function. The problem is that system.time does obviously not capture CPU time spend by the child processes spawned by the parallel package.
library(doParallel)
cl <- makeCluster(2)
registerDoParalllel(2)
timings <- system.time(foreach(i = 1:2) %do% rnorm(1e8))
时间然后看起来像这样
> timings
user system elapsed
16.883 5.731 22.899
计时加起来.现在,如果我使用并行处理:
The timings add up. Now if I use parallel processing:
timings <- system.time(foreach(i = 1:2) %dopar% rnorm(1e8))
> timings
user system elapsed
2.445 3.410 20.347
用户和系统时间仅捕获主进程.专门查看时间[4]和[5],可以发现user.child和sys.child时间均为0.
The user and system time are only capturing the master process. Specifically looking at the timings[4] and [5] shows me that the user.child and sys.child times are 0.
在并行处理中,如何测量R中的CPU总时间?
What do I have to do to measure total CPU time in R on parallel processing?
注意:将集群启动代码移入system.time调用没有任何作用.
Note: Moving the cluster startup code into the system.time call did not make a difference.
> sessionInfo()
R version 3.3.2 (2016-10-31)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 8 (jessie)
other attached packages:
[1] doParallel_1.0.10 iterators_1.0.8 foreach_1.4.3
推荐答案
@ chinsoon12为我指明了正确的方向.仅当通过registerDoParallel创建集群时,才会填充user.child和sys.child.
@chinsoon12 pointed me in the right direction. user.child and sys.child are only populated when the cluster is created by registerDoParallel, e.g.
registerDoParalllel(cores = 2)
timings <- system.time(foreach(i = 1:2) %dopar% rnorm(1e8))
user.self sys.self elapsed user.child sys.child
timings 0.429 1.978 19.378 9.818 1.386
这就是为什么它与doMC开箱即用的原因,我没有通过cl变量手动启动和停止集群.
This is why it worked out of the box with doMC where I did not manually start and stop the cluster via the cl variable.
这篇关于R sys.child中的system.time和parallel包为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!