Channels ▼


OpenMP: A Portable Solution for Threading

Work-Sharing Sections

The work-sharing sections construct directs the OpenMP compiler and runtime to distribute the identified sections of your application among threads in the team created for the parallel region. The following example uses work-sharing for loops and work-sharing sections together within a single parallel region. In this case, the overhead of forking or resuming threads for parallel sections is eliminated.

#pragma omp parallel
    #pragma omp for
    for ( k = 0; k < m; k++ ) {
        x = fn1(k) + fn2(k);

    #pragma omp sections private(y, z)
        #pragma omp section
            { y = sectionA(x); fn7(y); }
        #pragma omp section
            { z = sectionB(x); fn8(z); }

Here, OpenMP first creates several threads. Then, the iterations of the loop are divided among the threads. Once the loop is finished, the sections are divided among the threads so that each section is executed exactly once, but in parallel with the other sections. If the program contains more sections than threads, the remaining sections get scheduled as threads finish their previous sections. Unlike loop scheduling, the schedule clause is not defined for sections. Therefore, OpenMP is in complete control of how, when, and in what order threads are scheduled to execute the sections. You can still control which variables are shared or private, using the private and reduction clauses in the same fashion as the loop construct.

Performance-oriented Programming

OpenMP provides a set of important pragmas and runtime functions that enable thread synchronization and related actions to facilitate correct parallel programming. Using these pragmas and runtime functions effectively with minimum overhead and thread waiting time is extremely important for achieving optimal performance from your applications.

Barriers are a form of synchronization method that OpenMP employs to synchronize threads. Threads will wait at a barrier until all the threads in the parallel region have reached the same point. You have been using implied barriers without realizing it in the work-sharing for and work sharing sections constructs. At the end of the parallel, for, sections, and single constructs, an implicit barrier is generated by the compiler or invoked in the runtime library. The barrier causes execution to wait for all threads to finish the work of the loop, sections, or region before any go on to execute additional work. This barrier can be removed with the nowait clause, as shown in the following code sample.

#pragma omp parallel
    #pragma omp for nowait
    for ( k = 0; k < m; k++ ) {
        fn10(k); fn20(k);

    #pragma omp sections private(y, z)
        #pragma omp section
            { y = sectionD(); fn70(y); }
        #pragma omp section
            { z = sectionC(); fn80(z); }

In this example, since data is not dependent between the first work sharing for loop and the second work-sharing sections code block, the threads that process the first work-sharing for loop continue immediately to the second work-sharing sections without waiting for all threads to finish the first loop. Depending upon your situation, this behavior may be beneficial, because it can make full use of available resources and reduce the amount of time that threads are idle. The nowait clause can also be used with the work-sharing sections construct and single construct to remove its implicit barrier at the end of the code block.

Adding an explicit barrier is also supported by OpenMP as shown in the following example through the barrier pragma.

#pragma omp parallel shared(x, y, z) num_threads(2)
    int tid = omp_get_thread_num();
    if (tid == 0) {
        y = fn70(tid);
    else {
        z = fn80(tid);

#pragma omp barrier

#pragma omp for
    for ( k = 0; k < 100; k++ ) {
        x[k] = y + z + fn10(k) + fn20(k);

In this example, the OpenMP code is to be executed by two threads; one thread writes the result to the variable y, and another thread writes the result to the variable z. Both y and z are read in the work-sharing for loop, hence, two flow dependencies exist. In order to obey the data dependence constraints in the code for correct threading, you need to add an explicit barrier pragma right before the work-sharing for loop to guarantee that the value of both y and z are ready for read. In real applications, the barrier pragma is especially useful when all threads need to finish a task before any more work can be completed, as would be the case, for example, when updating a graphics frame buffer before displaying its contents.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.