none
Help on very basic OpenMP program RRS feed

  • Question

  • Hello,

    I have a hello world program that uses OpenMP, and I am unable to understand
    it's behavior.

    The code is:

    #include <iostream>
    #include <omp.h>

    using namespace std;

    int main ()
    {
         cout << "Max openMP threads = " << omp_get_max_threads () << endl;

        #pragma omp parallel
        {
            int num = omp_get_thread_num ();
            cout << "Hello from thread " << num << endl;
        }

        cout << endl << "Number of threads " << omp_get_num_threads ();
    }

    Very basic program.

    My machine is a HyperThreaded P4, and omp_get_max_threads () returns 2.

    I would now expect that the parallel code segment will be run on two
    separate threads. But it is run on only 1.

    The program output is:

    "Max OpenMP threads = 2
    Hello from thread 0

    Number of threads 1"

    So, the parallel segment is run on only 1 thread. Can someone explain this
    behavior, and how I can fix it?

    Thanks.
    Saturday, September 17, 2005 10:30 AM

Answers

  • The "number of threads" out side of a work sharing construct is always 1.  If you use the following two lines inside your parallel region, it should output 2.

    #pragma omp master
    cout << endl << "Number of threads " << omp_get_num_threads ();

    Even so the program obviously only ran on 1 thread.  Did you use the -openmp compiler switch?

    -Chuck

    Tuesday, September 20, 2005 1:13 AM

All replies

  • The "number of threads" out side of a work sharing construct is always 1.  If you use the following two lines inside your parallel region, it should output 2.

    #pragma omp master
    cout << endl << "Number of threads " << omp_get_num_threads ();

    Even so the program obviously only ran on 1 thread.  Did you use the -openmp compiler switch?

    -Chuck

    Tuesday, September 20, 2005 1:13 AM
  • Chuck,

    That was indeed the error.

    I had not enabled the /openmp switch.

    Thanks,
    Hari


    Thursday, September 22, 2005 12:08 AM