有没有办法限制运行的R进程数

时间:2016-10-19 15:31:37

标签: r ulimit domc setrlimit

我使用的是使用包doMC的{​​{1}}。它发生了(好几次),当我在调试时(在控制台中)它横向移动并且fork-bombed

R是否有setrlimit()系统调用? 在pyhton中,我会使用resource.RLIMIT_NPROC

理想情况下,我想将运行的R进程数量限制为数字

编辑:操作系统是linux CentOS 6

2 个答案:

答案 0 :(得分:3)

应该有几种选择。以下是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'上的线程数量。 (Aka' GotoBLAS',' ACML'和   ' MKL&#39)。并且可以控制' OpenMP'中的线程数量。得到   如果可行,可以使用许多逻辑核心和物理核心。

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

所有这一切都应该在很大程度上独立于您运行的Linux或Unix的风格。好吧,除了OS X当然(仍然!!)没有给你OpenMP。

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

答案 1 :(得分:2)

您可以使用registerDoMC(请参阅文档here

registerDoMC(cores=<some number>)

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

ulimit -u <some number>

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

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