Intel® C++ Compiler
Support and discussions for creating C++ code that runs on platforms based on Intel® processors.
7646 Discussions

Setting the number of threads in a shared library

ale3
Beginner
146 Views

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
146 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>

Reply