views:

391

answers:

2
void foo(void) {
    #pragma omp parallel
    {
        #pragma omp single
            {fprintf(stderr, "first part\n"); }
        #pragma omp barrier   
        #pragma omp single
           { fprintf(stderr, "second part\n"); }
    }
}
void bar (void) {
    #pragma omp parallel
    {
        #pragma  omp sections
        {
            fprintf(stderr, "first part\n"); 
             #pragma  omp section
             fprintf(stderr, "second part\n"); 
         }
    }
}

Q1-are foo() and bar() equivalent?

+5  A: 

They are not equivalent.

foo() will execute the two fprintf (parts) sequentially. bar() may execute them in parallel.

For an exhaustive reference you may refer to the IBM compiler documentation.

Essentially, in the case of foo(), a single worker thread handles first part, then completes and resynchronizes with the master thread (the barrier is, by the way, implicit at the end of a single construct.) A new worker thread then handles the second part, completes, and resynchronizes with the master thread.

In the case of bar(), a separate worker thread handles each section (the first section, first part, is an implicit section.) The worker threads complete work in parallel.

#pragma omp parallel

The omp parallel directive explicitly instructs the compiler to parallelize the chosen block of code. When a parallel region is encountered, a logical team of threads is formed. Each thread in the team executes all statements within a parallel region except for work-sharing constructs. Work within work-sharing constructs is distributed among the threads in a team.

Loop iterations must be independent before the loop can be parallelized. An implied barrier exists at the end of a parallelized statement block.

Nested parallel regions are always serialized.

#pragma omp single

The omp single directive identifies a section of code that must be run by a single available thread.

An implied barrier exists at the end of a parallelized statement block unless the nowait clause is specified.

#pragma omp section, #pragma omp sections

The omp sections directive distributes work among threads bound to a defined parallel region.

The omp section directive is optional for the first program code segment inside the omp sections directive. Following segments must be preceded by an omp section directive. All omp section directives must appear within the lexical construct of the program source code segment associated with the omp sections directive.

When program execution reaches a omp sections directive, program segments defined by the following omp section directive are distributed for parallel execution among available threads. A barrier is implicitly defined at the end of the larger program region associated with the omp sections directive unless the nowait clause is specified.

Cheers, V.

vladr
+2  A: 

Here's a bit of what I remember about OpenMP, with help from this resource.

  • SECTION directives "specify parallel execution for arbitrary blocks of sequential code. Each SECTION is executed once by a thread in the team."

  • SINGLE specifies a block of code "where exactly one thread is allowed to execute the code; threads not chosen to execute this section ignore the code."

  • A BARRIER directive forces all threads to meet at that point, thus producing a block until all threads have caught up.

Scottie T