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
13: #include <petscsys.h>
14: int main(int argc,char **argv)
17: PetscMPIInt rank,size;
20: Every PETSc program should begin with the PetscInitialize() routine.
21: argc, argv - These command line arguments are taken to extract the options
22: supplied to PETSc and options supplied to MPI.
23: help - When PETSc executable is invoked with the option -help,
24: it prints the various options that can be applied at
25: runtime. The user can use the "help" variable place
26: additional help messages in this printout.
31: The following MPI calls return the number of processes
32: being used and the rank of this process in the group.
38: Here we would like to print only one message that represents
39: all the processes in the group. We use PetscPrintf() with the
40: communicator PETSC_COMM_WORLD. Thus, only one message is
41: printed representing PETSC_COMM_WORLD, i.e., all the processors.
43: PetscPrintf(PETSC_COMM_WORLD,"Number of processors = %d, rank = %d\n",size,rank);
45: Here we would like to print info from each process, such that
46: output from process "n" appears after output from process "n-1".
47: To do this we use a combination of PetscSynchronizedPrintf() and
48: PetscSynchronizedFlush() with the communicator PETSC_COMM_WORLD.
49: All the processes print the message, one after another.
50: PetscSynchronizedFlush() indicates that the current process in the
51: given communicator has concluded printing, so that the next process
52: in the communicator can begin printing to the screen.
54: PetscSynchronizedPrintf(PETSC_COMM_WORLD,"[%d] Synchronized Hello World.\n",rank);
55: PetscSynchronizedPrintf(PETSC_COMM_WORLD,"[%d] Synchronized Hello World - Part II.\n",rank);
58: Here a barrier is used to separate the two states.
63: Here we simply use PetscPrintf() with the communicator PETSC_COMM_SELF
64: (where each process is considered separately). Thus, this time the
65: output from different processes does not appear in any particular order.
67: PetscPrintf(PETSC_COMM_SELF,"[%d] Jumbled Hello World\n",rank);
70: Always call PetscFinalize() before exiting a program. This routine
71: - finalizes the PETSc libraries as well as MPI
72: - provides summary and diagnostic information if certain runtime
73: options are chosen (e.g., -log_summary).
74: See the PetscFinalize() manpage for more information.
77: return 0;