Community
cancel
Showing results for 
Search instead for 
Did you mean: 
ale3
Beginner
47 Views

Setting the number of threads in a shared library

I'm having troubles using more than one thread in a OpenMP parallel region in a shared library.

Let's say that I have a function foo in my library foo.so

void foo(){

  int NT = omp_get_max_threads();
  fprintf(stderr,"Max num TH= %d\n", omp_get_max_threads());
  omp_set_num_threads(NT);
  omp_set_dynamic(0);*/
  #pragma omp parallel for schedule(dynamic) num_threads(NT)
  for ( i = 0; i < N; i++ ) {

    fprintf(stdout,"Using %d threads\n", omp_get_num_threads());

  }

}

Calling foo from another program linking foo.so, and usign  e.g. export OMP_NUM_THREADS=4, gives:

>> Max num TH=4

>>Using 1 thread

 

Am I doing something clearly wrong?

 

0 Kudos
1 Reply
Judith_W_Intel
Employee
47 Views

 

I tried to reproduce the problem and it worked for me.

Please tell us exactly what you did and we can try to help. The code you've shown above is incomplete.

thanks

Judy

This is what I did:

sptel15-145> cat foo.c
#include <stdio.h>

#define N 12

void foo(){
  int i;
  int NT = omp_get_max_threads();
   fprintf(stderr,"Max num TH= %d\n", omp_get_max_threads());
   omp_set_num_threads(NT);
   omp_set_dynamic(0);
   #pragma omp parallel for schedule(dynamic) num_threads(NT)
   for ( i = 0; i < N; i++ ) {

    fprintf(stdout,"Using %d threads\n", omp_get_num_threads());

  }

}

sptel15-146> cat main.c
extern void foo();

int main() {
   foo();
   return 0;
}
sptel15-147> icc -c -fopenmp -fPIC foo.c
sptel15-148> icc -shared -o libfoo.so foo.o
sptel15-149> setenv LD_LIBRARY_PATH .:$LD_LIBRARY_PATH
sptel15-150> icc -fopenmp -L. main.c -lfoo
sptel15-151> ./a.out
Max num TH= 4
Using 4 threads
Using 4 threads
Using 4 threads
Using 4 threads
Using 4 threads
Using 4 threads
Using 4 threads
Using 4 threads
Using 4 threads
Using 4 threads
Using 4 threads
Using 4 threads
sptel15-152>