How can I get R to use more CPU usage?

僤鯓⒐⒋嵵緔 提交于 2019-12-01 20:52:22

问题


I noticed that R doesn't use all of my CPU, and I want to increase that tremendously (upwards to 100%). I don't want it to just parallelize a few functions; I want R to use more of my CPU resources. I am trying to run a pure IP set packing program using the lp() function. Currently, I run windows and I have 4 cores on my computer.

I have tried to experiment with snow, doParallel, and foreach (though I do not know what I am doing with them really).

In my code I have this...

library(foreach)
library(doParallel)
library(snowfall)

cl <- makeCluster(4)
registerDoParallel(cl)

sfInit(parallel = TRUE, cpus = 4)


#code that is taking a while to run but does not involve simulations/iterations

lp (......, all.int = TRUE)

sfStop()

R gets stuck and runs lp() for a very long time. My CPU is around 25%, but how can I increase that?


回答1:


If you are trying to run 4 different LPs in parallel, here's how to do it in snowfall.

sfInit(parallel=TRUE, cpus=4)
sfSource(code.R) #if you have your function in a separate file
sfExport(list=c("variable1","variable2",
            "functionname1")) #export your variables and function to cluster
results<-sfClusterApplyLB(parameters, functionname) #this starts the function on the clusters

E.g. The function in the sfClusterApply could contain your LP.

Otherwise see comments in regard to your question




回答2:


Posting this as an answer because there's not enough space in a comment.
This is not an answer directly towards your question but more to the performance.


R uses slow statistical libraries by default which also can only use single core by default. Improved libraries are OPENBLAS/ATLAS. These however, can be a pain to install.
Personally I eventually got it working using this guide.

I ended up using Revolution R open(RRO) + MKL which has both improved BLAS libraries and multi-cpu support. It is an alternative R distribution which is supposed to have up to 20x the speed of regular R (I cannot confirm this, but it is alot faster).

Furthermore, you could check the CRAN HPC packages to see if there is any improved packages which support the lp function.

There is also packages to explore multi cpu usage.
This answer by Gavin, as well as @user3293236's answer above show several possibilities for packages allowing multi CPU usage.



来源:https://stackoverflow.com/questions/31900708/how-can-i-get-r-to-use-more-cpu-usage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!