Contained WithinFind More DocumentationFeatured Support Resources | Descargar este libro en PDF (723 KB)
Chapter 2 Compiling and Running OpenMP ProgramsThis chapter describes compiler and runtime options affecting programs that utilize the OpenMP API. To run a parallelized program in a multithreaded environment, you must set the OMP_NUM_THREADS environment variable prior to program execution. This tells the runtime system the maximum number of threads the program can create. The default is 1. In general, set OMP_NUM_THREADS to a value no larger than the number of available virtual processors on the target platform. Set OMP_DYNAMIC to FALSE to use the number of threads specified by OMP_NUM_THREADS. The latest information regarding Sun Studio compilers and OpenMP can be found on the Sun Developer Network portal, http://developers.sun.com/sunstudio 2.1 Compiler Options To UseTo enable explicit parallelization with OpenMP directives, compile your program with the cc, CC, or f95 option flag -xopenmp. This flag can take an optional keyword argument. (The f95 compiler accepts both -xopenmp and -openmp as synonyms.) The -xopenmp flag accepts the following keyword sub-options.
Additional Notes:
2.2 Fortran 95 OpenMP ValidationYou can obtain a static, interprocedural validation of a Fortran 95 program’s OpenMP directives by using the f95 compiler’s global program checking feature. Enable OpenMP checking by compiling with the -XlistMP flag. (Diagnostic messages from -XlistMP appear in a separate file created with the name of the source file and a .lst extension). The compiler will diagnose the following violations and parallelization inhibitors:
For example, compiling a source file ord.f with -XlistMP produces a diagnostic file ord.lst:
In this example, the ORDERED directive in subroutine WORK receives a diagnostic that refers to the second DO directive because it lacks an ORDERED clause. 2.3 OpenMP Environment VariablesThe OpenMP specification define four environment variables that control the execution of OpenMP programs. These are summarized in the following table. Table 2–1 OpenMP Environment Variables
Additional multiprocessing environment variables affect execution of OpenMP programs and are not part of the OpenMP specifications. These are summarized in the following table. Table 2–2 Multiprocessing Environment Variables
2.4 Processor Binding on SolarisWith processor binding, the programmer instructs the Solaris Operating System that a thread in the program should run on the same processor throughout the execution of the program. (This feature is not available on Linux.) Processor binding, when used along with static scheduling, benefits applications that exhibit a certain data reuse pattern where data accessed by a thread in a parallel or worksharing region will be in the local cache from a previous invocation of a parallel or worksharing region. From the hardware point of view, a computer system is composed of one or more physical processors. From the Operating System point of view, each of these physical processors maps to one or more virtual processors onto which threads in a program can be run. If n virtual processors are available, then n threads can be scheduled to run at the same time. Depending on the system, a virtual processor may be a processor, a core, etc. For example, each UltraSPARC IV physical processor has two cores; from the Solaris OS point of view, each of these cores is a virtual processor onto which a thread can be scheduled to run. The UltraSPARC T1 physical processor, on the other hand, has eight cores, and each core can run four simultaneous processing threads; from the Solaris OS point of view, there are 32 virtual processors onto which threads can be scheduled to run. On the Solaris Operating System, the number of virtual processors can be determined by using the psrinfo(1M) command. When the operating system binds threads to processors, they are in effect bound to specific virtual processors, not physical processors. When running under the Solaris OS, set the SUNW_MP_PROCBIND environment variable to bind threads in an OpenMP program to specific virtual processors. The value specified for SUNW_MP_PROCBIND can be one of the following:
Note that the non-negative integers referred to above denote logical identifiers (IDs). Logical IDs may be different from virtual processor IDs. The difference will be explained below. Virtual Processor IDs: Each virtual processor in a system has a unique processor ID. You can use the Solaris OS psrinfo(1M) command to display information about the processors in a system, including their processor IDs. Moreover, you can use the prtdiag(1M) command to display system configuration and diagnostic information. You can use psrinfo -pv to list all physical processors in the system and the virtual processors that are associated with each physical processor. Virtual processor IDs may be sequential or there may be gaps in the IDs. For example, on a Sun Fire 4810 with 8 UltraSPARC IV processors (16 cores), the virtual processor IDs may be: 0, 1, 2, 3, 8, 9, 10, 11, 512, 513, 514, 515, 520, 521, 522, 523. Logical IDs: As mentioned above, the non-negative integers specified for SUNW_MP_PROCBIND are logical IDs. Logical IDs are consecutive integers that start with 0. If the number of virtual processors available in the system is n, then their logical IDs are 0, 1, ..., n-1, in the order presented by psrinfo(1M). The following Korn shell script can be used to display the mapping from virtual processor IDs to logical IDs.
On systems where a single physical processor maps to several virtual processors, it may be useful to know which logical IDs correspond to virtual processors that belong to the same physical processor. The following Korn shell script can be used with later Solaris releases to display this information.
Interpreting the Value Specified for SUNW_MP_PROCBIND: If the value specified for SUNW_MP_PROCBIND is a non-negative integer, then that integer denotes the starting logical ID of the virtual processor to which threads should be bound. Threads will be bound to virtual processors in a round-robin fashion, starting with the processor with the specified logical ID, and wrapping around to the processor with logical ID 0, after binding to the processor with logical ID n-1.If the value specified for SUNW_MP_PROCBIND is a list of two or more non-negative integers, then threads will be bound in a round-robin fashion to virtual processors with the specified logical IDs. Processors with logical IDs other than those specified will not be used. If the value specified for SUNW_MP_PROCBIND is two non-negative integers separated by a minus ("-"), then threads will be bound in a round-robin fashion to virtual processors in the range that begins with the first logical ID and ends with the second logical ID. Processors with logical IDs other than those included in the range will not be used. If the value specified for SUNW_MP_PROCBIND does not conform to one of the forms described above, or if an invalid logical ID is given, then an error message will be emitted and execution of the program will terminate. Note that the number of threads created by the microtasking library, libmtsk, depends on environment variables, API calls in the user’s program, and the num_threads clause. SUNW_MP_PROCBIND specifies the logical IDs of virtual processors to which the threads should be bound. Threads will be bound to that set of processors in a round-robin fashion. If the number of threads used in the program is less than the number of logical IDs specified by SUNW_MP_PROCBIND, then some virtual processors will not be used by the program. If the number of threads is greater than the number of logical IDs specified by SUNW_MP_PROCBIND, them some virtual processors will have more than one thread bound to them. 2.5 Stacks and Stack SizesThe executing program maintains a main stack for the initial thread executing the program, as well as distinct stacks for each slave thread. Stacks are temporary memory address spaces used to hold arguments and automatic variables during invocation of a subprogram or function reference. In general, the default main stack size is 8 megabytes. Compiling Fortran programs with the f95 -stackvar option forces the allocation of local variables and arrays on the stack as if they were automatic variables. Use of -stackvar with OpenMP programs is implied with explicitly parallelized programs because it improves the optimizer’s ability to parallelize calls in loops. (See the Fortran User’s Guide for a discussion of the -stackvar flag.) However, this may lead to stack overflow if not enough memory is allocated for the stack. Use the limit C-shell command, or the ulimit ksh/sh command, to display or set the size of the main stack. Each slave thread of an OpenMP program has its own thread stack. This stack mimics the initial (or main) thread stack but is unique to the thread. The thread’s PRIVATE arrays and variables (local to the thread) are allocated on the thread stack. The default size is 4 megabytes on 32-bit SPARC V8 and x86 platforms, and 8 megabytes on 64-bit SPARC V9 and x86 platforms. The size of the helper thread stack is set with the STACKSIZE environment variable.
Finding the best stack size might have to be determined by trial and error. If the stack size is too small for a thread to run it may cause silent data corruption in neighboring threads, or segmentation faults. If you are unsure about stack overflows, compile your Fortran, C, or C++ programs with the -xcheck=stkovf compiler option to force a segmentation fault on stack overflow. This stops the program before any data corruption can occur. (Note: The -xcheck=stkovf compiler option is available only on SPARC systems). 2.6 Checking OpenMP Programs With the Thread AnalyzerYou can check your OpenMP program for data races and deadlocks by using the Sun Studio Thread Analyzer tool. Refer to the Thread Analyzer manual and the tha(1) man page for details. |
||||||||||||||||||||||||||||||||||