Specialized loops

General OpenMP discussion

Specialized loops

Postby djvp » Fri Jul 19, 2013 12:07 pm

I am new to the community so please be patient with me if I ask dumb questions. I plan to use OpenMP in several different locations in the code. At times I want OpenMP to use as many threads as it thinks it wants to use on open ended loops and then there are other times I have a highly specialized parallel scheme where I know exactly the number of threads I want. It is this latter case where I have a question.

I have a small loop of not more than three passes where each pass is highly CPU intensive and no index cross talk is required. I have learned that the main thread takes on a pass so I at most need two more threads. What is the most efficient way to tell OpenMP what I want?

I have experimented a little but not really knowing what I am doing and how it may affect my other uses of OpenMP. I would appreciate a knowledgeable review.

Code: Select all
//#pragma omp parallel num_threads(2), default(shared)
//#pragma omp for private(i), schedule(dynamic, 1)
#pragma omp parallel for
  for(i=0; i<notMoreThan3; ++i)

Posts: 1
Joined: Fri Jul 19, 2013 11:26 am

Re: Specialized loops

Postby MarkB » Mon Jul 22, 2013 2:32 am

Hi there,

The code you posted will work OK. If the parallel region uses more than 3 threads, then the extra threads will idle in the barrier at the end of the parallel region, so don't do any harm, unless you really want/need the extra threads to go to sleep for this period.

You can use the num_threads clause to specify exactly the number of threads you require, though note that this number includes the master thread (i.e. num_threads(2) will only create one additional thread). The num_threads clause does not affect the number of threads used for subsequent parallel regions.

If the number of loop iterations does not exceed the number of threads, then using the dynamic schedule is of no benefit, since each thread will still only get one

Hope that helps,
Posts: 746
Joined: Thu Jan 08, 2009 10:12 am
Location: EPCC, University of Edinburgh

Return to Using OpenMP

Who is online

Users browsing this forum: No registered users and 3 guests