OpenMP® Forum

Discussion on the OpenMP specification run by the OpenMP ARB. OpenMP and the OpenMP logo are registered trademarks of the OpenMP Architecture Review Board in the United States and other countries. All rights reserved.

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.

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 iteration.