OpenMP Tasks by example

General OpenMP discussion

OpenMP Tasks by example

Postby vegihat » Thu Feb 16, 2012 8:14 am

Hello folks.

Code: Select all
#include <omp.h>
#include <stdio.h>
#define N 4
int main(){

        #pragma omp parallel num_threads(2)
        {
                #pragma omp single
                {
                        int i;
                        printf("I'm boss and my id is %d\n",omp_get_thread_num());
                        for(i=0;i<N;i++)
                        {
                                #pragma omp task untied
                                {
                                        printf("hello from %d\n",omp_get_thread_num());
                                }
                        }
                }
        }
}




i try to understand how tasks of OpenMP work.I wrote the above code waiting to see something like

Code: Select all
[advpar08@xanthos]$ ./a.out
I'm boss and my id is 0
hello from 1
hello from 1
hello from 1
hello from 1


but the real output is

Code: Select all
[advpar08@xanthos]$ ./a.out
I'm boss and my id is 0
hello from 0
hello from 0
hello from 0
hello from 0


[1] why is it happen?
[2] how could it print the output that i want.

Thanks in advance.
vegihat
 
Posts: 2
Joined: Thu Feb 16, 2012 8:01 am

Re: OpenMP Tasks by example

Postby MarkB » Fri Feb 17, 2012 2:57 am

There is no guarantee as to which thread will execute the single region, nor as to which threads will execute the tasks, so both the output you observe and that you would like to see are equally valid. Your example generates a small number of short tasks: if you increase the number of tasks, or the length of time they take to execute, I expect you will observe both threads executing tasks.
MarkB
 
Posts: 447
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh

Re: OpenMP Tasks by example

Postby vegihat » Fri Feb 17, 2012 5:42 am

I thought that untied tasks are not executed by the same thread that created it. But this is wrong,right?

-Thank you for your answer
vegihat
 
Posts: 2
Joined: Thu Feb 16, 2012 8:01 am

Re: OpenMP Tasks by example

Postby MarkB » Fri Feb 17, 2012 9:33 am

Untied tasks may be suspended and then resumed by a different thread to the one that started executing them.
They may be executed by the same thread that created them, or by a different one (or ones).
MarkB
 
Posts: 447
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh


Return to Using OpenMP

Who is online

Users browsing this forum: Google [Bot] and 5 guests

cron