2016-10-19 86 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

相關問題