2016-10-19 50 views

回答

1

应该有几个选择。下面是从Writing R Extensions, Section 1.2.1.1

Packages are not standard-alone programs, and an R process could 
contain more than one OpenMP-enabled package as well as other components 
(for example, an optimized BLAS) making use of OpenMP. So careful 
consideration needs to be given to resource usage. OpenMP works with 
parallel regions, and for most implementations the default is to use as 
many threads as 'CPUs' for such regions. Parallel regions can be 
nested, although it is common to use only a single thread below the 
first level. The correctness of the detected number of 'CPUs' and the 
assumption that the R process is entitled to use them all are both 
dubious assumptions. The best way to limit resources is to limit the 
overall number of threads available to OpenMP in the R process: this can 
be done via environment variable 'OMP_THREAD_LIMIT', where 
implemented.(4) Alternatively, the number of threads per region can be 
limited by the environment variable 'OMP_NUM_THREADS' or API call 
'omp_set_num_threads', or, better, for the regions in your code as part 
of their specification. E.g. R uses 
    #pragma omp parallel for num_threads(nthreads) ... 
That way you only control your own code and not that of other OpenMP 
users. 

我最喜欢的工具相关的部分是包控制这样的:RhpcBLASctl。下面是它的描述:

控制上 'BLAS' 线程的数目(又名 'GotoBLAS', 'ACML' 和 'MKL')。并可能控制'OpenMP'中的线程数量。如果可行的话,获得 许多逻辑核心和物理核心。

毕竟您需要控制并行会话的数量以及分配给每个并行线程的BLAS核心的数量。有一个原因,并行程序包的默认值为每个会话2个线程...

所有这些应该基本上独立于您正在运行的Linux或Unix的风格。那么,除了OS X当然(仍然!!)不给你OpenMP的事实。

而且你可以从doMC和朋友那里控制的非常外层。

+0

看起来很有前途,谢谢我会深入研究它 – statquant

2

您可以使用registerDoMC(见该文档here

registerDoMC(cores=<some number>) 

另一种选择是运行将R脚本之前使用ulimit命令:

ulimit -u <some number> 

限制R将能够产生的进程数量。

如果要限制多个R进程同时使用的CPU总数,则需要使用cgroupscpusets并将R进程附加到cgroup或cpuset。然后它们将被限制在cgroup或cpuset中定义的物理CPUS上。 cgroups允许更多的控制(例如也是内存),但是设置更复杂。

+0

感谢您的编辑,我会尽快查看 – statquant

相关问题