[Rcpp-devel] R limiting the number of child threads

Yan Zhou zhouyan at me.com
Mon Feb 4 18:47:22 CET 2013


Hi all, 

My problem is not strictly related to Rcpp. However I hope someone with expertise in both C++ and R can help.

I have a C++ library for parallel implementation of Sequential Monte Carlo algorithms. It can use some different programming models, OpenMP, Intel TBB, Intel Cilk, C++11 <thread>, and others. Everything worked fine so far.

However, when I try to integrate a program with R using Rcpp, I exported a few R functions from the C++ source, each of them uses a different backend as listed above. For example

// [[Rcpp::export]]
Rcpp::List rcpp_func_omp (...)
{
    do_some_work_with_omp();
    // get_results
    // create and return the list
}

// [[Rcpp::export]]
Rcpp::List rcpp_func_tbb (...)
{
    do_some_work_with_tbb();
    // get_results
    // create and return the list
}

The same source was also compiled into standalone C++ program, for example
// standalon_omp.cpp
int main ()
{
    do_some_work_with_omp();
    // get results
    // output to a file
}

// standalon_tbb.cpp
int main ()
{
    do_some_work_with_tbb();
    // get results
    // output to a file
}

Basically the standalone C++ program and the R function exported using Rcpp are exactly the same, except how they handle some output. I build all the C++ programs using all the listed parallel backend and an R package whose shared library has all the rcpp_func_omp, rcpp_func_cilk, etc. Everything runs fine. But there is a problem that appears that the R master process does not allow me to spawn some threads.

The C++ standalone programs all works fine, all of them use 8 threads (4 cores, 8 hyperthreads on my computer)

However, when run those R functions within an R session, the OpenMP, Intel Cilk version appears to utilize all 8 threads (and in htop there shows 8 R process). But the TBB and C++11 <thread> version only use two threads. I tried to manually specify the number of threads. However, it appears that at least with my poor man's C++11 <thread> parallelization  there are indeed 8 threads being created, but from htop I can only see one master R process and it is use 200% CPU (which means it is use only two hardware threads).

It appears to me that the R master process is limiting itself to use at most 2 hardware threads in this case. That is, even I spawned 8 threads, they are all running on two cores (or perhaps more likely two hyperthreads within one core).

I also tried to isolate the implementations. That is to have shared library only have one version, say Intel TBB without others and it only links to TBB. But the same problem persists.

Is there some reason that all child thread of the R process will run on the same core that R is running? I don't think so, since R itself has a parallel package and OpenMP etc has been used with R for a long time. If anyone can suggest any possible explanation to my problem, that will be very appreciated.

Both R, the package, the standalone programs are build with the same compiler, flags, etc. Specifically, icpc -std=c++11 --gxx_name=g++-4,.7 -openmp

Hopefully I have stated the question clear enough.

Best,

Yan Zhou
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20130204/27039f1a/attachment.html>


More information about the Rcpp-devel mailing list