7

I'm trying to get started with using basic OpenMP functionality in C. My basic understanding of 'omp parallel for' leads me to believe the following should distribute the following iterations of the loop between threads and should execute concurrently. The output I am getting is as follows. Code Below. Is there something subtle I am missing in my hello world example?

Hello World from omp thread 0 Hello World from omp thread 0 Hello World from omp thread 0 Hello World from omp thread 0 Hello World from omp thread 0 Hello World from omp thread 0 etc..

 int HelloFunc()
{
    int i;
    int numthreads = 8;
#pragma omp parallel for default(none) num_threads(numthreads) private(i)
    for (i = 0; i < 100; i++)
    {
        int tid = omp_get_thread_num();
        printf("Hello world from omp thread %d\n", tid);
    }
    return -1;
}

int main()
{
    int result = HelloFunc();
}
Oliver
  • 651
  • 2
  • 9
  • 17
  • 1
    Your computer is likely only using one thread to run this program. OMP doesn't force it to run with multiple threads, it just tells the compiler that it can and sets up the necessary environment to make it happen. – randomusername Mar 05 '15 at 20:52
  • I think I can make that an answer if you post it as one since it answers my question. There exist no ways to force this behavior I would gather from your answer? – Oliver Mar 05 '15 at 20:56
  • 2
    You can request that OpenMP use a specific number of threads by setting the `OMP_NUM_THREADS` environment variable. The maximum number of threads can be queried from inside the code with `omp_get_max_threads()` – halfflat Mar 05 '15 at 20:59

2 Answers2

9
#include <omp.h>
#include <stdio.h>


 int HelloFunc()
{
    int i;
    int numthreads = 8;
#pragma omp parallel for default(none) num_threads(numthreads) private(i)
    for (i = 0; i < 100; i++)
    {
        int tid = omp_get_thread_num();
        printf("Hello world from omp thread %d\n", tid);
    }
    return -1;
}

int main()
{
    HelloFunc();

  return 0;
}

then compile :

gcc t.c -fopenmp -Wall

and run :

./a.out

output :

Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 7
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 3
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 0
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 6
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 5
Hello world from omp thread 2
Hello world from omp thread 2
Hello world from omp thread 4
Hello world from omp thread 4
Hello world from omp thread 4
Hello world from omp thread 4
Hello world from omp thread 2
Hello world from omp thread 1
Hello world from omp thread 4
Hello world from omp thread 2
Hello world from omp thread 1
Hello world from omp thread 4
Hello world from omp thread 2
Hello world from omp thread 2
Hello world from omp thread 4
Hello world from omp thread 1
Hello world from omp thread 1
Hello world from omp thread 1
Hello world from omp thread 1
Hello world from omp thread 1
Hello world from omp thread 1
Hello world from omp thread 1
Hello world from omp thread 2
Hello world from omp thread 4
Hello world from omp thread 1
Hello world from omp thread 2
Hello world from omp thread 4
Hello world from omp thread 1
Hello world from omp thread 1
Hello world from omp thread 4
Hello world from omp thread 2
Hello world from omp thread 1
Hello world from omp thread 4
Hello world from omp thread 4
Hello world from omp thread 2
Hello world from omp thread 2
Hello world from omp thread 2
Hello world from omp thread 2
Adam
  • 1,254
  • 12
  • 25
  • 4
    This is fine as an educational example to show that things work as the OP expected when there are multiple threads. However for *real* codes it's generally a bad idea to force the number of threads explicitly in the code, because at some point you'll buy a new machine, or someone else will want to use it, and then you'll spend a day or two wondering why it doesn't run faster despite having twice as many cores! – Jim Cownie Mar 09 '15 at 09:30
  • @Jim Cownie. Ok. Can you show the code for that? TIA – Adam Sep 27 '18 at 09:41
  • 1
    I think `private(i)` is redundant here. `i` is private by default. – gonidelis Sep 20 '20 at 16:03
2

Your computer is likely only using one thread to run this program. OMP doesn't force it to run with multiple threads, it just tells the compiler that it can and sets up the necessary environment to make it happen.

There is no way to force OMP to do something in more threads than it would otherwise do. And you wouldn't want to since OMP automatically sets everything up to make run the fastest it can.

randomusername
  • 7,927
  • 23
  • 50