get iteration number inside loop.

General OpenMP discussion

get iteration number inside loop.

Postby peroksid » Thu Jan 30, 2014 7:42 pm

Hi.

How i can get iterations number for each thread inside loop?

Something like this pseudo-code:

Code: Select all
#pragma omp parallel
{
      #pragma omp for
      for(int i = 0; i < 8; i++)
      {
              now_this_thread_working_with_iterations(); // For example: 0, 1, 2, 3 - for first thread;   4, 5, 6, 7 - for second...
      }
}
peroksid
 
Posts: 3
Joined: Wed Jan 29, 2014 7:59 am

Re: get iteration number inside loop.

Postby ftinetti » Fri Jan 31, 2014 3:11 am

Hi,

I don't know if I understood correctly, but the iteration number is that of "i"... each thread, omp_get_thread_num( ), should have different values of i.

HTH,

Fernando.
ftinetti
 
Posts: 567
Joined: Wed Feb 10, 2010 2:44 pm

Re: get iteration number inside loop.

Postby MarkB » Fri Jan 31, 2014 3:42 am

The only information you can get is to call omp_get_thread_num() on every iteration to find out which thread is executing it. There is no way of returning the set of iterations assigned to a thread. Is there a use case you have in mind?

Hope that helps,
Mark.
MarkB
 
Posts: 422
Joined: Thu Jan 08, 2009 10:12 am

Re: get iteration number inside loop.

Postby ftinetti » Fri Jan 31, 2014 4:52 am

Hi Mark,

Just a quick reminder about

There is no way of returning the set of iterations assigned to a thread


if schedule(static) is used, there is a way to know in advance the set of iterations assigned to each thread, right?

Fernando.
ftinetti
 
Posts: 567
Joined: Wed Feb 10, 2010 2:44 pm

Re: get iteration number inside loop.

Postby MarkB » Fri Jan 31, 2014 5:10 am

ftinetti wrote:if schedule(static) is used, there is a way to know in advance the set of iterations assigned to each thread, right?


Hi Fernando,

Not in general, since the way that "remainder" iterations are assigned is not specified. So, for example, if a loop has 7 iterations and there are 3 threads, then both the following assignments of iterations to threads are compliant implementations of schedule(static):

Code: Select all
Thread 0      Thread 1     Thread 2
1,2,3           4,5,6        7
1,2,3           4,5          6,7


Best wishes,
Mark.
MarkB
 
Posts: 422
Joined: Thu Jan 08, 2009 10:12 am

Re: get iteration number inside loop.

Postby ftinetti » Fri Jan 31, 2014 5:21 am

Hi Mark,

I always forgot the "note" on schedule(static)... (in page 58 of the Spec. 4.0).

Thanks again,

Fernando.
ftinetti
 
Posts: 567
Joined: Wed Feb 10, 2010 2:44 pm

Re: get iteration number inside loop.

Postby ftinetti » Fri Jan 31, 2014 5:27 am

Hi again,

Sorry to bother... it's that I think that the first case given in
Code: Select all
Thread 0      Thread 1     Thread 2
1,2,3           4,5,6        7
1,2,3           4,5          6,7

should not happen if schedule(static,omp_get_num_threads( ))
(where the number of threads is explicitly given).

Or I'm missing something...

Thanks in advance,

Fernando.
ftinetti
 
Posts: 567
Joined: Wed Feb 10, 2010 2:44 pm

Re: get iteration number inside loop.

Postby MarkB » Fri Jan 31, 2014 7:30 am

Hi Fernando,

ftinetti wrote:Sorry to bother... it's that I think that the first case given in

Code: Select all
Thread 0 Thread 1 Thread 2
1,2,3 4,5,6 7
1,2,3 4,5 6,7


should not happen if schedule(static,omp_get_num_threads( ))
(where the number of threads is explicitly given).


If the chunksize is specified as in schedule(static,3), then the spec is still not entirely clear, but the note you refer to implies that the intention is to mandate the first case (i.e there is at most one chunk with fewer than chunksize iterations) rather than the second.

In any case, if you really care which which thread executes which iterations (and I cannot think of many instances when you would), you should assign them explicitly rather than use the schedule clause!
MarkB
 
Posts: 422
Joined: Thu Jan 08, 2009 10:12 am

Re: get iteration number inside loop.

Postby peroksid » Fri Jan 31, 2014 10:58 am

MarkB wrote:Is there a use case you have in mind?


Yes, right.

Thanks for answers.

P.S.

In my case it would very useful.

Code: Select all
vector<set<int> > adj_list(N);       
vector<set<int> > adj_preproc(N);
while(ifs >> u >> v)                                       //This is are Graph, like this:                 
        {                                                          // 0 1
                adj_list[u].insert(v);                     // 0 2
                adj_list[v].insert(u);                     // 2 1
        }                                                          // 2 3
     
        typedef set<int>::const_iterator iter;

        #pragma omp parallel
        {
           #pragma omp for
           for(int i = 0; i < adj_list.size(); i++)
           {
                for(iter p = adj_list[i].begin(); p != adj_list[i].end(); p++)
                {
                        if( ...some condition...)
                        {
                                adj_preproc[i].insert(*p);     //No race condition. *p - only reading.
                        }
                        else
                        {
                                #pragma omp critical              //Because *p may be "i" owned by other thread.                                                                                 
                                adj_preproc[(*p)].insert(i);                     
                        }                                                                       
                 }                                                               
            }                                                                         
       }                                                                           
                                                                     
I could change ELSE section like this:

if( (*p) "NOT IN" set_iterrations_assigned_to_this_thread() )
{
       #pragma omp critical
      adj_preproc[(*p)].insert(i);
}
else
{
       adj_preproc[(*p)].insert(i);      //No race condtion. *p - owned by this thread;
}

peroksid
 
Posts: 3
Joined: Wed Jan 29, 2014 7:59 am

Re: get iteration number inside loop.

Postby MarkB » Fri Jan 31, 2014 11:09 am

I guess I maybe don't understand the data structure, but do you not need to protect accesses to a thread's "own" elements of adj_preproc[] in case another thread tries to modify them at the same time?
MarkB
 
Posts: 422
Joined: Thu Jan 08, 2009 10:12 am

Next

Return to Using OpenMP

Who is online

Users browsing this forum: No registered users and 9 guests

cron