[R-sig-hpc] OpenMP problem

Norm Matloff matloff at cs.ucdavis.edu
Thu Sep 17 05:49:11 CEST 2009


I somehow can't get more than one thread in interfacing R to a C
function that uses OpenMP.

Here's a simple example:

   #include <R.h>
   
   void tiny()
   {  
      omp_set_num_threads(2);
      #pragma omp parallel  
      {  int me = omp_get_thread_num();
         printf("number of threads is %d\n",omp_get_num_threads());
         printf("I am thread %d\n",me);
      }
   }

I compile via

   $ R CMD SHLIB TinyR.c -fopenmp

But I only get one thread, rather than the two I asked for.

If I set the OS environment variable OMP_NUM_THREADS to 2, then start up
R (and confirm the environment by Sys.getenv("OMP_NUM_THREADS")), it
still gives me only one thread.

If I run it outside R, e.g. as

   void dowork()
   {
      omp_set_num_threads(4);
      #pragma omp parallel  
      {  int nth = omp_get_num_threads(),  
             me = omp_get_thread_num();  
         printf("I am thread %d\n",me);  
      }
   }
   
   int main(int argc, char **argv)
   {  int i;
      dowork();  
   }

it works fine (get four threads here), either compiling with GCC or the
Omni OpenMP compiler (the latter being my preferred OpenMP compiler).

I'm doing all this on a dual core machine running Ubuntu 9.04.

Any suggestions?

Norm Matloff



More information about the R-sig-hpc mailing list