|
|
HP C/HP-UX Programmer's Guide: HP 9000 Computers > Chapter 8 Threads and Parallel
ProcessingGetting Started with Parallelizing C Programs |
|
Here are some basis tasks to help you get started with parallelizing C programs. The +Oparallel option causes the compiler to transform eligible loops for parallel execution on multiprocessor machines. The following command lines compile (without linking) three source files: x.c, y.c, and z.c. The files x.c and y.c are compiled for parallel execution. The file z.c is compiled for serial execution, even though its object file will be linked with x.o and y.o. cc +O3 +Oparallel -c x.c y.c cc +O3 -c z.c The following command line links the three object files, producing the executable file para_prog: cc +O3 +Oparallel -o para_prog x.o y.o z.o As this command line implies, if you link and compile separately, you must use cc, not ld. The command line to link must also include the +Oparallel and +O3 options in order to link in the right startup files and runtime support. Use the MP_NUMBER_OF_THREADS environment variable to set the number of processors that are to execute your program in parallel. If you do not set this variable, it defaults to the number of processors on the executing machine. From the C shell, the following command sets MP_NUMBER_OF_THREADS to indicate that programs compiled for parallel execution can execute on two processors: setenv MP_NUMBER_OF_THREADS 2 If you use the Korn shell, the command is: export MP_NUMBER_OF_THREADS=2 Use the MP_IDLE_THREADS_WAIT environment variable to determine how threads wait. Idle threads can be suspended or can spin-wait. This variable takes an integer value n. For n less than 0, the threads spin-wait. For n equal to or greater than 0, the threads spin-wait for n milliseconds before being suspended. By default, idle threads spin-wait briefly after creation or a join. They then suspend themselves if they receive no work. Pthreads (POSIX threads) refers to the Pthreads library of thread-management routines. For information on Pthread routines see the pthread(3t) man page. To use the Pthread routines, your program must include the <pthreads.h> header file and the Pthreads library must be explicitly linked to your program. For example: % cc -D_POSIX_C_SOURCE+199506L prog.c -lpthread The -D_POSIX_C_SOURCE=199506L string specifies the appropriate POSIX revision level. In this case, the level is 199506L. Profiling a program that has been compiled for parallel execution is performed in much the same way as it is for non-parallel programs:
The differences are:
|
|