Actual source code: ex2.c
1: static char help = "Synchronized printing.\n\n";
3: #include <petscsys.h>
4: int main(int argc, char **argv)
6: PetscMPIInt rank, size;
9: Every PETSc program should begin with the PetscInitialize() routine.
10: argc, argv - These command line arguments are taken to extract the options
11: supplied to PETSc and options supplied to MPI.
12: help - When PETSc executable is invoked with the option -help,
13: it prints the various options that can be applied at
14: runtime. The user can use the "help" variable to place
15: additional help messages in this printout.
18: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
21: The following MPI calls return the number of processes
22: being used and the rank of this process in the group.
24: PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
25: PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
28: Here we would like to print only one message that represents
29: all the processes in the group. We use PetscPrintf() with the
30: communicator PETSC_COMM_WORLD. Thus, only one message is
31: printed representing PETSC_COMM_WORLD, i.e., all the processors.
33: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Number of processors = %d, rank = %d\n", size, rank));
35: Here we would like to print info from each process, such that
36: output from process "n" appears after output from process "n-1".
37: To do this we use a combination of PetscSynchronizedPrintf() and
38: PetscSynchronizedFlush() with the communicator PETSC_COMM_WORLD.
39: All the processes print the message, one after another.
40: PetscSynchronizedFlush() indicates that the current process in the
41: given communicator has concluded printing, so that the next process
42: in the communicator can begin printing to the screen.
44: PetscCall(PetscSynchronizedPrintf(PETSC_COMM_WORLD, "[%d] Synchronized Hello World.\n", rank));
45: PetscCall(PetscSynchronizedPrintf(PETSC_COMM_WORLD, "[%d] Synchronized Hello World - Part II.\n", rank));
46: PetscCall(PetscSynchronizedFlush(PETSC_COMM_WORLD, PETSC_STDOUT));
48: Here a barrier is used to separate the two states.
53: Here we simply use PetscPrintf() with the communicator PETSC_COMM_SELF
54: (where each process is considered separately). Thus, this time the
55: output from different processes does not appear in any particular order.
57: PetscCall(PetscPrintf(PETSC_COMM_SELF, "[%d] Jumbled Hello World\n", rank));
60: Always call PetscFinalize() before exiting a program. This routine
61: - finalizes the PETSc libraries as well as MPI
62: - provides summary and diagnostic information if certain runtime
63: options are chosen (e.g., -log_view).
64: See the PetscFinalize() manpage for more information.
67: return 0;