In R 3.0.2 on Linux 3.12.0, I am using the system()
function to execute a number of tasks. The desired effect is for each of these tasks to run as they would if I had executed them on the command-line via Rscript outside of R system()
.
However, when executing them inside R via system()
, each task is tied to the same single CPU from the master R process.
In other words:
When launched via RScript directly from a bash shell, outside of R, each task runs on its own core as possible (this is desired)
When launched inside R via system()
, each task runs on the same single core. There is no multicore sharing. If I have 100 tasks, they are all stuck on one core.
I cannot figure out how to spawn a process inside of R so that each process will use its own core.
I am using a simple test to consume CPU cycles so I can measure the effect using top
/htop
:
dd if=/dev/urandom bs=32k count=1000 | bzip2 -9 >> /dev/null
When this simple test is launched outside of R multiple times, each iteration gets its own core. But when I launch it inside of R:
system("dd if=/dev/urandom bs=32k count=2000 | bzip2 -9 >> /dev/null", ignore.stdout=TRUE,ignore.stderr=TRUE,wait=FALSE)
They are all stuck on a single core.
Here is a visualization after running 4 simultaneous/concurrent iterations of system()
.
Please help me, I need to be able to tell R to launch new tasks, with each of them running in their own core.
UPDATE DEC 4 2013:
I tried a test in Python using this:
import thread
thread.start_new_thread(os.system,("/bin/dd if=/dev/urandom of=/dev/null bs=32k count=2000",))
I repeated the new thread several times, and as expected everything worked (multiple cores used, one per thread).
So I think install the rPython
package in R, and try the same from within R:
python.exec("import thread")
python.exec("thread.start_new_thread(os.system,('/bin/dd if=/dev/urandom of=/dev/null bs=32k count=2000',))")
Unfortunately, once again it was limited to a single core even after repeated calls. Why is it that everything launched is limited to a single core when executed from R?
parallel
package. You find here more explanations. – MortieGNU parallel
on your system? Or perhaps if you are running 4 processes you could try usingxargs
in your launch script with theP - 4
'4 maxprocs' option to try and force parallel execution?? – DanleyGNU parallel
the bash utility doesn't run them in parallel then fine - I'm sorry I couldn't help. – Danley