get iteration number inside loop.

General OpenMP discussion

Re: get iteration number inside loop.

Postby peroksid » Sat Feb 01, 2014 5:16 am

MarkB wrote: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?

No. Trick in iterations == vectors ID. If OpenMP always give unique iterators for each thread, so this thread safety allways - adj_preproc[i].insert(*p);

But this - adj_preproc[*p].insert(i) no. But if we do #pragma omp critical - all will be OK.

If you are interested in why that, see attach.

And another question, if i replace

Code: Select all
#pragma omp critical                                                                                     

Code: Select all

it will be little faster?
thread.png (79.48 KiB) Viewed 200 times
Posts: 3
Joined: Wed Jan 29, 2014 7:59 am

Re: get iteration number inside loop.

Postby MarkB » Mon Feb 03, 2014 4:04 am

OK, I see. In this case it is probably best not to use a for construct, but to explicitly compute lower and upper bounds for each thread for the parallel loop, based on the number of threads and the thread ID. Then you have a simple test to know whether an iterator value "belongs" to the executing thread or not.

As to whether locks are faster than critical, I would expect maybe a little, since critical may involve executing some extra instructions, but in most implementations the underlying mutex lock/unlock mechanism is likely the same.
Posts: 408
Joined: Thu Jan 08, 2009 10:12 am


Return to Using OpenMP

Who is online

Users browsing this forum: Yahoo [Bot] and 6 guests