[Rd] Set the number of threads using openmp with .C

From: Eduardo García <edugarpor_at_gmail.com>
Date: Sat, 10 Jul 2010 19:31:03 +0200


Hi everybody! Could somebody help me with the following?

I'm trying to run a simple Hello World code in openmp using .C function. The C code i have is:

 #include <omp.h>
 #include <stdio.h>
 #include <R.h>

 void hello_omp(int *n) {
   int th_id, nthreads;
   omp_set_num_threads(*n);
   #pragma omp parallel private(th_id)
   {

     th_id = omp_get_thread_num();
     Rprintf("Hello World from thread %d\n", th_id);
     #pragma omp barrier
     if ( th_id == 0 ) {
       nthreads = omp_get_num_threads();
       Rprintf("There are %d threads\n",nthreads);
     }

   }
}

Where n is the number of threads that i want.

I compite it with "R CMD SHLIB hello_openmp_R.c -fopenmp" and when I try to run it in R using:

dyn.load("hello_openmp_R.so")

hello_omp=function(n){.C("hello_omp",as.integer(n))}
hello_omp(3)
hello_omp(2)

Only 1 thread is been used, instead of 3 and 2:

> hello_omp(3)

Hello World from thread 0
There are 1 threads
[[1]]
[1] 3

> hello_omp(2)

Hello World from thread 0
There are 1 threads
[[1]]
[1] 2

I also tried to set OMP_NUM_THREADS=3 in the Konsole with "export OMP_NUM_THREADS=3", in the .c file directory, but it seems that R don't recognize it when calls .C

I am using R version 2.10.1 in Ubuntu 9.10 - Karmic Koala, but i'm newbie in Linux.

Thanks a lot in advance for your help !* *

        [[alternative HTML version deleted]]



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Sat 10 Jul 2010 - 17:35:39 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 10 Jul 2010 - 18:30:14 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive