Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OMP For parallel thread ID hello world

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();
}
like image 360
Oliver Avatar asked Mar 05 '15 20:03

Oliver


People also ask

How do I find my OMP thread ID?

EffectThe omp_get_thread_num routine returns the thread number of the calling thread, within the team that is executing the parallel region to which the routine region binds. The thread number is an integer between 0 and one less than the value returned by omp_get_num_threads, inclusive.

What is the difference between OMP for and OMP parallel for?

#pragma omp parallel spawns a group of threads, while #pragma omp for divides loop iterations between the spawned threads.

What does #pragma OMP parallel for do?

Purpose. The omp parallel directive explicitly instructs the compiler to parallelize the chosen block of code.

What is #pragma OMP?

Purpose. The omp for directive instructs the compiler to distribute loop iterations within the team of threads that encounters this work-sharing construct.


1 Answers

#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
like image 160
Adam Avatar answered Sep 28 '22 14:09

Adam