Problem with : OMP_NUM_THREADS

Hello ,

I have a program that use Open Mp Pragma with C, and put this test part in it



int nthreads, tid,maxth;



/* Fork a team of threads giving them their own copies of variables */

#pragma omp parallel private(tid)

{

/* Obtain thread number /
omp_set_num_threads(4);
tid = omp_get_thread_num();
maxth=omp_get_max_threads();
printf(“Hello World from thread = %d\n”, tid);



/
Only master thread does this */



// #pragma omp master

if(tid==0)

{

nthreads = omp_get_num_threads();

printf(“Number of threads = %d\n”, nthreads);
printf(“Heloo max = %d\n”, maxth);
}

}


and he display to me

Hello World from thread = 0
Number of threads = 1
Heloo max = 3

that false because i have server with four procesor !!!
and i try to export : OMP_NUM_THREADS, and get the same thing, the problem is that Number of threads is always one = 1, no parrallel execution

Any help would be deeply appreciated…
Thank’s

The results you get are correct. Remember that setting the number of threads with omp_set_num_threads () only effects the next parallel region, so nothing changes for the current parallel region (and therefore that statement in your code has no effect).

On the other hand, when you do set the environment variable OMP_NUM_THREADS, you should see a difference and if I do that and compile your program with the Portland Compiler, I do see a difference. Or did you maybe forget to compile with -mp? If you leave out that switch, I get the same results as you. So make sure to use it.

Hope that helps,
best regards,
Michael


Michael Suess
My blog on parallel programming

/******************************/
thank’s but it’s doesn’t resolve the probleme
remember if that i dont use -mp it not display the s_max and the messages, else i use export and i get the same result…
is there any configuration of servet on other
that’s the r
pgcc -B -02 -mp -c -o …



pgcc -02 -mp *.o -o exec -lm

Hi forest_gump,

Besides calling omp_set_num_threads before the parallel section, you also need to use brackets to define the start and end of the section.

Fo example:

% cat omptest.c
#include <omp.h>

int main() {

  int nthreads, tid, maxth;

  /* Fork a team of threads giving them their own copies of variables */

  omp_set_num_threads(4);
#pragma omp parallel private(tid)
 {    
  /* Obtain thread number */
  //omp_set_num_threads(4);
  tid = omp_get_thread_num();
  maxth=omp_get_max_threads();
  printf("Hello World from thread = %d\n", tid);

  /* Only master thread does this */

  // #pragma omp master

  if(tid==0)
    {
      nthreads = omp_get_num_threads();
      printf("Number of threads = %d\n", nthreads);
      printf("Heloo max = %d\n", maxth);
     }
 }
  exit(0);
}

% pgcc -V6.2-3 -mp omptest.c -o omp.out
% omp.out
Hello World from thread = 0
Number of threads = 4
Heloo max = 4
Hello World from thread = 1
Hello World from thread = 3
Hello World from thread = 2

Hope this helps,
Mat

thank’s but i try this exemple just now and it work, but my main program do the same "that mean 1 threads whith 3thread maximun "
O yeah this programme use ifdef PRRALLELE, to differ Parrallel from sequetien declaration,
i think that a compilation may heave an effct that 's why i give make file format


pgcc -B -mp -O2 -c -o n.o n.c
pgcc -B -mp -O2 -c -o an.o an.c
pgcc -B -mp -O2 -c -o ad.o ad.c
pgcc -B -mp -O2 -c -o tn.o tn.c
pgcc -B -mp -O2 -c -o o.o o.c
…ant other in the same way


pgcc -B -mp *.o (all the o extension) -o exec -lm

oh yeah thank and thank you

Hi forest_gump,

Please make sure that you added the brackets around your parallel sections in your source. Also, the “-D” flag can be used to pass the preprocessor define flags.

Note in the following example, I’m assuming you meant “#ifdef PARALLEL” not “#ifdef PRRALLELE”.

pgcc -B -DPARALLEL -mp -O2 -c -o n.o n.c
pgcc -B -DPARALLEL -mp -O2 -c -o an.o an.c
pgcc -B -DPARALLEL -mp -O2 -c -o ad.o ad.c
pgcc -B -DPARALLEL -mp -O2 -c -o tn.o tn.c
pgcc -B -DPARALLEL -mp -O2 -c -o o.o o.c
  • Mat

Thank’s mat, but the bracket is added and i am sur , oh yeah it’s PARRALLE with E, i dont develop this code but i debug it" PARAALLELE this not a problem it’s work",

the probleme is that omp_set_num_threads() have not any effect in my program but i other program it’s work “test program”!!!, that mean number thread it’s always one but threads_ max= 3 or 4 according to exported, and i dont uderstand why ???

mat, Michael Suess , thank’s ;-)

OKAY it’s WOOOOOOOOOOOOOOORK

thank’s it a bracket and declaration of the VARs in commen.h

, oh yeah now i have 4 thread that execute in the processor how to make each thread in one process