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@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to