Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my R uses all CPU cores when running functions like step()?

My R often shows using more than 100% CPU in "top", does it mean it's using more than 1 core? As I understand, R by default uses 1 core of CPU unless when using certain parallel computing packages. But I am just using step() function. It's Dell T410 + Ubuntu Server 14.04 + R 3.3.2.

Is it R 3.3.2 or Dell Server or Ubuntu Server 14.04 that's helping? Or is it just a bug of "top"?

top - 17:42:39 up 11:09,  2 users,  load average: 16.00, 16.01, 15.98
Tasks: 282 total,   3 running, 279 sleeping,   0 stopped,   0 zombie
%Cpu(s): 14.9 us, 85.1 sy,  0.0 ni,  0.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:  24668964 total, 23472468 used,  1196496 free,   229884 buffers
KiB Swap: 25145340 total,       60 used, 25145280 free.  1117020 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                             
17704 can       20   0 21.495g 0.020t  13016 R  **1540** 87.1   4458:52 rsession                            
17748 can       20   0   26632   1780   1172 S   0.7  0.0   0:50.62 top                                 
 2528 can       20   0  105660   2276   1260 S   0.3  0.0   0:00.01 sshd   
like image 959
MacChuck Avatar asked Nov 26 '16 22:11

MacChuck


1 Answers

R often appears to be using more than one core even when your code is technically single-threaded. This often happens because R is switching between different processors too quickly to notice. I created the code below as an example. When I run it on my Windows 10 machine I see two processors hard at work.

library(microbenchmark)

pb <- txtProgressBar(min = 0, max = 100, style = 3)

for(i in 1:100) {
  microbenchmark(rnorm(10000), runif(10000), rpois(10000, 1))
  setTxtProgressBar(pb, i)
}
close(pb)

If you're interested in learning more about the situations in which a computer will try to stick with the same logical processor, look up "Processor Affinity."

like image 134
Andrew Brēza Avatar answered Sep 20 '22 00:09

Andrew Brēza