8

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?

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Eines He
  • 187
  • 1
  • 2
  • 8
  • 3
    How do you know that it runs only one thread? – Antoine Apr 18 '12 at 06:25
  • Yes I checked in the test() by printing out the omp_get_thread_num(). – Eines He Apr 18 '12 at 17:19
  • Did you check `omp_get_max_threads()` to see if openmp things that it can use only one thread for some reason? – Grizzly Apr 18 '12 at 17:27
  • Thanks, I checked omp_get_max_threads() in the test() and it is 4. But still every test() runs on thread number 0. – Eines He Apr 18 '12 at 17:49
  • Is it possible that the compiler optimizes out all of the loops as they don't do anything, and then each thread finishes quicker than the loop creating them can run as they don't do anything... so there is only ever one thread running. Just a theory, probably wrong :) – jcoder May 14 '13 at 16:36
  • Try `#pragma omp parallel for schedule(static) num_threads(4)`. Static scheduling is the de facto default but not necessarily the default. – Z boson Aug 04 '14 at 07:29

5 Answers5

7

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.

Tudor
  • 61,523
  • 12
  • 102
  • 142
  • Thanks and sorry, that was a typo. In the code I had it num_threads(4) but still have no luck – Eines He Apr 19 '12 at 15:38
  • @Eines He: Then it's very strange, because I pasted your code, compiled it and ran it and it worked on all 4 cores. – Tudor Apr 19 '12 at 15:39
  • How do you set up your environment? I think the code is quite basic and could not possibly go wrong. I am using g++ 4.6.1 and libomp1. I think the openmp is definitely working because I have no problem in checking the number of cpus ...etc. It must be some where I didn't set the environment correctly. – Eines He Apr 20 '12 at 04:59
  • Hmm... I tested this on Windows with cl.exe. I'll test it on my Linux environment ASAP and let you know. – Tudor Apr 20 '12 at 07:49
  • Ok, so I tested on ubuntu with g++ 4.5.2 and it also works fine there. – Tudor Apr 20 '12 at 09:26
3

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

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
hamed246
  • 31
  • 1
1

first choose project _> properties -> c/c++ -> language -> open mp support -> choose yes and then you will find above conformance mode (make it no )

abdelhamed
  • 11
  • 1
0

use the function omp_set_num_threads(4) before calling the omp parallel section.

also, how do you determine the number of threads ?? embed your printfs in a critical section just to make sure everything is getting printed.

prathmesh.kallurkar
  • 5,468
  • 8
  • 39
  • 50
0

I encountered the very same situation on my ubuntu desktop when I extends numpy module with C code. openmp only ran with one thread. I happened to remove libopenblas-base and install libatlas-base-dev.(to deal with numpy installation problem) Then multi-threading openmp came back:)

I have tested it on a ubuntu server with 64 cores and it works just as my desktop! I think this is because libopenblas conflicts with libraries like atlas.

GBY
  • 1,090
  • 1
  • 10
  • 13