Actual source code: ex2.c
2: static char help = "Synchronized printing.\n\n";
5: Concepts: petsc^introduction
6: Concepts: printing^synchronized
7: Concepts: printing^in parallel
8: Concepts: printf^synchronized
9: Concepts: printf^in parallel
10: Processors: n
15: #include <petscsys.h>
16: int main(int argc,char **argv)
19: PetscMPIInt rank,size;
22: Every PETSc program should begin with the PetscInitialize() routine.
23: argc, argv - These command line arguments are taken to extract the options
24: supplied to PETSc and options supplied to MPI.
25: help - When PETSc executable is invoked with the option -help,
26: it prints the various options that can be applied at
27: runtime. The user can use the "help" variable place
28: additional help messages in this printout.
30: PetscInitialize(&argc,&argv,NULL,help);if (ierr) return ierr;
33: The following MPI calls return the number of processes
34: being used and the rank of this process in the group.
40: Here we would like to print only one message that represents
41: all the processes in the group. We use PetscPrintf() with the
42: communicator PETSC_COMM_WORLD. Thus, only one message is
43: printed representing PETSC_COMM_WORLD, i.e., all the processors.
45: PetscPrintf(PETSC_COMM_WORLD,"Number of processors = %d, rank = %d\n",size,rank);
47: Here we would like to print info from each process, such that
48: output from process "n" appears after output from process "n-1".
49: To do this we use a combination of PetscSynchronizedPrintf() and
50: PetscSynchronizedFlush() with the communicator PETSC_COMM_WORLD.
51: All the processes print the message, one after another.
52: PetscSynchronizedFlush() indicates that the current process in the
53: given communicator has concluded printing, so that the next process
54: in the communicator can begin printing to the screen.
56: PetscSynchronizedPrintf(PETSC_COMM_WORLD,"[%d] Synchronized Hello World.\n",rank);
57: PetscSynchronizedPrintf(PETSC_COMM_WORLD,"[%d] Synchronized Hello World - Part II.\n",rank);
60: Here a barrier is used to separate the two states.
65: Here we simply use PetscPrintf() with the communicator PETSC_COMM_SELF
66: (where each process is considered separately). Thus, this time the
67: output from different processes does not appear in any particular order.
69: PetscPrintf(PETSC_COMM_SELF,"[%d] Jumbled Hello World\n",rank);
72: Always call PetscFinalize() before exiting a program. This routine
73: - finalizes the PETSc libraries as well as MPI
74: - provides summary and diagnostic information if certain runtime
75: options are chosen (e.g., -log_view).
76: See the PetscFinalize() manpage for more information.
79: return ierr;