InnerhalbNach weiteren Dokumenten suchenSupport-Ressourcen | Dieses Buch im PDF-Format herunterladen (760 KB)
Chapter 6 Automatic Scoping of VariablesDeclaring the scope attributes of variables in an OpenMP parallel region is called scoping. In general, if a variable is scoped as SHARED, all threads share a single copy of the variable. If a variable is scoped as PRIVATE, each thread has its own copy of the variable. OpenMP has a rich data environment. In addition to SHARED and PRIVATE, the scope of a variable can also be declared FIRSTPRIVATE, LASTPRIVATE, REDUCTION, or THREADPRIVATE. OpenMP requires the user to declare the scope of each variable used in a parallel region. This is a tedious and error-prone process and many find this to be the hardest part of using OpenMP to parallelize programs. The Sun Studio C, C++, and Fortran 95 compilers provide an automatic scoping feature. The compilers analyze the execution and synchronization pattern of a parallel region and determine automatically what the scope of a variable should be, based on a set of scoping rules. 6.1 The Autoscoping Data Scope ClauseThe autoscoping data scope clause is a Sun extension to the OpenMP specification. A user can specify a variable to be autoscoped by using one of the following two clauses. 6.1.1 __auto ClauseSyntax: The __auto clause on a parallel construct directs the compiler to automatically determine the scope of the named variables in the construct. (Note the two underscores before auto.) The __auto clause can appear on a PARALLEL, PARALLEL DO/for, PARALLEL SECTIONS, or on a Fortran 95 PARALLEL WORKSHARE directive. If a variable is specified on the __auto clause, then it cannot be specified in any other data scope clause. 6.1.2 default(__auto) ClauseThe default(__auto) clause on a parallel construct directs the compiler to automatically determine the scope of all variables referenced in the construct that are not explicitly scoped in any data scope clause. The default(__auto) clause can appear on a PARALLEL, PARALLEL DO/for, PARALLEL SECTIONS, or on a Fortran 95 PARALLEL WORKSHARE directive. 6.2 Autoscoping RulesUnder automatic scoping, the compiler applies the following rules to determine the scope of a variable in a parallel region. These rules do not apply to variables whose scopes are predetermined by the OpenMP specification, such as loop iteration variables of worksharing DO or FOR loops. Refer to OpenMP 3.0 Specification (section 2.9.1.1, page 78) for a complete listing of variables whose scopes are predetermined. 6.2.1 Autoscoping Rules For Scalar Variables
6.2.2 Autoscoping Rules for Arrays
6.3 General Comments About AutoscopingWhen autoscoping a variable that does not have predetermined scope, the compiler checks the use of the variable against the above rules S1–S3 in the given order if it is a scalar, and against the above rule A1 if it is an array. If a rule matches, the compiler will scope the variable according to the matching rule. If a rule does not match, the compiler tries the next rule. If the compiler is unable to find a match, the compiler gives up attempting to determine the scope of that variable and it is scoped SHARED and the binding parallel region is serialized as if an IF (.FALSE.) or if(0) clause were specified. There are two reasons why autoscoping fails. One is that the use of the variable does not match any of the rules. The other is that the source code is too complex for the compiler to do a sufficient analysis. Function calls, complicated array subscripts, memory aliasing, and user-implemented synchronizations are some typical causes. (See 6.5 Known Limitations of the Current Implementation.) Autoscoping in C and C++ applies only to basic data types: integer, floating point, and pointer. If a user specifies a structure variable or class variable to be autoscoped, the compiler will scope the variable as shared and the enclosing parallel region will be executed by a single thread. 6.4 Checking the Results of AutoscopingUse compiler commentary to check autoscoping results and to see if any parallel regions are serialized because autoscoping failed. The compiler will produce an inline commentary when compiled with the -g debug option. This generated commentary can be viewed with the er_src command, as shown below. (The er_src command is provided as part of the Sun Studio software; for more information, see the er_src(1) man page or the Sun Studio Performance Analyzer manual.) A good place to start is to compile with the -xvpara option. A warning message will be printed out if autoscoping fails, as shown below. Example 6–1 Compiling With -vpara
Compile with -vpara with f95, -xvpara with cc. (This option has not yet been implemented in CC.) Example 6–2 Using Compiler Commentary
Next, a more complicated example to illustrate how the autoscoping rules work. Example 6–3 A More Complicated Example
The function FOO() contains a parallel region, which contains a SINGLE construct, a work-sharing DO construct and a CRITICAL construct. If we ignore all the OpenMP parallel constructs, what the code in the parallel region does is:
Let's see how the compiler uses the autoscoping rules in 6.2 Autoscoping Rulesto find the appropriate scopes for the variables in the parallel region. The following variables are used in the parallel region, I, N, MM, T, W, M, X, and Y. The compiler will determine the following.
6.5 Known Limitations of the Current ImplementationHere are the known limitations to autoscoping in the current Sun Studio Fortran 95 compiler.
|
|||