Go to main content
Oracle® Developer Studio 12.5: OpenMP API User's Guide

Exit Print View

Updated: July 2016
 
 

7.2 Using the Scope Checking Feature

To enable scope checking, compile the OpenMP program with the -xvpara and -xopenmp options. The optimization level should be -xO3 or higher. Scope checking does not work if the program is compiled with just -xopenmp=noopt. If the optimization level is less than -xO3, the compiler will issue a warning message and will not do any scope checking.

During scope checking, the compiler will check all OpenMP constructs. If the scoping of some variables causes problems, the compiler will issue warning messages, and, in some cases, provide suggestions for the correct data-sharing attribute clauses to use. For example, warning messages are issued if the compiler detects the following situations:

  • Loops are parallelized using OpenMP directives when there are data dependencies between different loop iterations

  • OpenMP data-sharing attribute clauses can can be problematic if, for example, you specify a variable to be shared in a parallel region when accesses to the variable in the parallel region might cause data race, or you specify a variable to be private in a parallel region when the value assigned to the variable in the parallel region is used after the parallel region.

The following example illustrates scope checking.

Example 24  Scope Checking With –xvpara
% cat t.c

#include <omp.h>
#include <string.h>

int main()
{
  int g[100], b, i;

  memset(g, 0, sizeof(int)*100);

  #pragma omp parallel for shared(b)
  for (i = 0; i < 100; i++)
  {
    b += g[i];
  }

  return 0;
}

% cc -xopenmp -xO3 -xvpara source1.c
"source1.c", line 10: Warning: inappropriate scoping
         variable 'b' may be scoped inappropriately as 'shared'
         . write at line 13 and write at line 13 may cause data race

"source1.c", line 10: Warning: inappropriate scoping
         variable 'b' may be scoped inappropriately as 'shared'
         . write at line 13 and read at line 13 may cause data race

The compiler will not do scope checking if the optimization level is less than -xO3.

% cc -xopenmp=noopt -xvpara source1.c
 "source1.c", line 10: Warning: Scope checking under vpara compiler 
option is supported with optimization level -xO3 or higher.
 Compile with a higher optimization level to enable this feature

The following example illustrates how potential scoping errors are reported.

Example 25  Scoping Errors Example
% cat source2.c

#include <omp.h>

int main()
{
  int g[100];
  int r=0, a=1, b, i;

  #pragma omp parallel for private(a) lastprivate(i) reduction(+:r)
  for (i = 0; i < 100; i++)
  {
    g[i] = a;
    b = b + g[i];
    r = r * g[i];
  }

  a = b;
  return 0;
}

% cc -xopenmp -xO3 -xvpara source2.c
"source2.c", line 8: Warning: inappropriate scoping
        variable 'r' may be scoped inappropriately as 'reduction'
        . reference at line 13 may not be a reduction of the specified type

"source2.c", line 8: Warning: inappropriate scoping
        variable 'a' may be scoped inappropriately as 'private'
        . read at line 11 may be undefined
        . consider 'firstprivate'

"source2.c", line 8: Warning: inappropriate scoping
        variable 'i' may be scoped inappropriately as 'lastprivate'
        . value defined inside the parallel construct is not used outside
        . consider 'private'

"source2.c", line 8: Warning: inappropriate scoping
        variable 'b' may be scoped inappropriately as 'shared'
        . write at line 12 and write at line 12 may cause data race

"source2.c", line 8: Warning: inappropriate scoping
        variable 'b' may be scoped inappropriately as 'shared'
        . write at line 12 and read at line 12 may cause data race

This example shows some typical errors that scope checking can detect.

  1. r is specified as a reduction variable whose operation is +, but actually the operation should be *.

  2. a is explicitly scoped as private. Because private variables do not have an initial value, the reference on line 11 to a could read undefined values. The compiler points out this problem and suggests scoping a as firstprivate.

  3. Variable i is the loop index variable. In some cases, the programmer may wish to specify it to be LASTPRIVATE if the value of the loop index is used after the parallel for loop. However, in the above example, i is not referenced at all after the loop. The compiler issues a warning and suggests scoping i as private. Using private instead of lastprivate can lead to better performance.

  4. No data-sharing attribute for variable b was explicitly specified. According to the OpenMP specification, b will be implicitly scoped as shared. However, scoping b as shared will cause a data race. The correct data-sharing attribute of b should be reduction.