JavaScript is required to for searching.
Skip Navigation Links
Exit Print View
Oracle Solaris Studio 12.2: OpenMP API User's Guide
search filter icon
search icon

Document Information

Preface

1.  Introducing the OpenMP API

2.  Compiling and Running OpenMP Programs

3.  Implementation-Defined Behaviors

4.  Nested Parallelism

5.  Tasking

6.  Automatic Scoping of Variables

7.  Scope Checking

7.1 Using the Scope Checking Feature

7.2 Restrictions

8.  Performance Considerations

A.  Placement of Clauses on Directives

B.  Converting to OpenMP

Index

7.1 Using the Scope Checking Feature

To enable scope checking, the OpenMP program should be compiled with the -xvpara and -xopenmp options, and at optimization level -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, suggestions for the correct data sharing attribute clause to use.

For example:

Example 7-1 Scope Checking

% 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

A more complicated example:

Example 7-2 source2

% 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

The above artifical example shows some typical errors of scoping 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. Since PRIVATE variables do not have an initial value, the reference on line 11 to a may read some garbage value. The compiler points out this problem, and suggests that the programmer consider 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 loop. But this is not the case in the above example; i is not referenced at all after the loop. The compiler issues a warning and suggests that the programmer scope i as PRIVATE. Using PRIVATE instead of LASTPRIVATE can lead to better performance.

  4. The programmer does not explicitly specify a data-sharing attribute for variable b. According to page 79, lines 27-28 of the OpenMP Specification 3.0, 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.