Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why OpenMP program runs only in one thread

I just tried OpenMP with a simple c program

test() {
   for(int i=0;i<100000000;i++);
}
main() {
    printf("Num of CPU: %d\n", omp_get_num_procs());
    #pragma omp parallel for num_threads(4)
    for(int i=0;i<100;i++) test();
}

Compiled with g++ -fopenmp. It can correctly print out that I have 4 CPUs, but all test functions are running at thread 0.

I tried to modify the OMP_NUM_THREADS. But it has no effect also.

I had everything the same as the online examples but why wouldn't I get it to work?

like image 691
Eines He Avatar asked Apr 18 '12 06:04

Eines He


2 Answers

Your problem is here:

#pragma omp parallel for num_thread(4) <---

The correct clause is num_threads(4), not num_thread(4). Incorrect openmp pragmas are ignored and so you ended up with a sequential program. :)

I'm surprised you didn't get a compiler warning, because I did.

like image 191
Tudor Avatar answered Sep 22 '22 16:09

Tudor


I had this problem in visual studio and finally I understood that I had forgotten to enable Open MP support in visual studio. It didn't give me any error but executed the program just for one thread

like image 41
hamed246 Avatar answered Sep 23 '22 16:09

hamed246