I have the following C/C++ code using OpenMP:
int nProcessors=omp_get_max_threads();
if(argv[4]!=NULL){
printf("argv[4]: %s\n",argv[4]);
nProcessors=atoi(argv[4]);
printf("nProcessors: %d\n",nProcessors);
}
omp_set_num_threads(nProcessors);
printf("omp_get_num_threads(): %d\n",omp_get_num_threads());
exit(0);
As you can see, I'm trying to set the number of processors to use based on an argument passed on the command line.
However, I'm getting the following output:
argv[4]: 2 //OK
nProcessors: 2 //OK
omp_get_num_threads(): 1 //WTF?!
Why isn't omp_get_num_threads()
returning 2?!!!
As has been pointed out, I'm calling omp_get_num_threads()
in a serial region, hence the function returns 1
.
However, I have the following parallel code:
#pragma omp parallel for private(i,j,tid,_hash) firstprivate(firstTime) reduction(+:nChunksDetected)
for(i=0;i<fileLen-CHUNKSIZE;i++){
tid=omp_get_thread_num();
printf("%d\n",tid);
int nThreads=omp_get_num_threads();
printf("%d\n",nThreads);
...
which outputs:
0 //tid
1 //nThreads - this should be 2!
0
1
0
1
0
1
...