1c4762a1bSJed Brown static char help[] = "Synchronized printing.\n\n";
2c4762a1bSJed Brown
3c4762a1bSJed Brown #include <petscsys.h>
main(int argc,char ** argv)4*d71ae5a4SJacob Faibussowitsch int main(int argc, char **argv)
5*d71ae5a4SJacob Faibussowitsch {
6c4762a1bSJed Brown PetscMPIInt rank, size;
7c4762a1bSJed Brown
8c4762a1bSJed Brown /*
9c4762a1bSJed Brown Every PETSc program should begin with the PetscInitialize() routine.
10c4762a1bSJed Brown argc, argv - These command line arguments are taken to extract the options
11c4762a1bSJed Brown supplied to PETSc and options supplied to MPI.
12c4762a1bSJed Brown help - When PETSc executable is invoked with the option -help,
13c4762a1bSJed Brown it prints the various options that can be applied at
14c87e60e8SJose E. Roman runtime. The user can use the "help" variable to place
15c4762a1bSJed Brown additional help messages in this printout.
16c4762a1bSJed Brown */
17327415f7SBarry Smith PetscFunctionBeginUser;
189566063dSJacob Faibussowitsch PetscCall(PetscInitialize(&argc, &argv, NULL, help));
19c4762a1bSJed Brown
20c4762a1bSJed Brown /*
21c4762a1bSJed Brown The following MPI calls return the number of processes
22c4762a1bSJed Brown being used and the rank of this process in the group.
23c4762a1bSJed Brown */
249566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
259566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
26c4762a1bSJed Brown
27c4762a1bSJed Brown /*
28c4762a1bSJed Brown Here we would like to print only one message that represents
29c4762a1bSJed Brown all the processes in the group. We use PetscPrintf() with the
30c4762a1bSJed Brown communicator PETSC_COMM_WORLD. Thus, only one message is
31c4762a1bSJed Brown printed representing PETSC_COMM_WORLD, i.e., all the processors.
32c4762a1bSJed Brown */
339566063dSJacob Faibussowitsch PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Number of processors = %d, rank = %d\n", size, rank));
34c4762a1bSJed Brown /*
35c4762a1bSJed Brown Here we would like to print info from each process, such that
36c4762a1bSJed Brown output from process "n" appears after output from process "n-1".
37c4762a1bSJed Brown To do this we use a combination of PetscSynchronizedPrintf() and
38c4762a1bSJed Brown PetscSynchronizedFlush() with the communicator PETSC_COMM_WORLD.
39c4762a1bSJed Brown All the processes print the message, one after another.
40c4762a1bSJed Brown PetscSynchronizedFlush() indicates that the current process in the
41c4762a1bSJed Brown given communicator has concluded printing, so that the next process
42c4762a1bSJed Brown in the communicator can begin printing to the screen.
43c4762a1bSJed Brown */
449566063dSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(PETSC_COMM_WORLD, "[%d] Synchronized Hello World.\n", rank));
459566063dSJacob Faibussowitsch PetscCall(PetscSynchronizedPrintf(PETSC_COMM_WORLD, "[%d] Synchronized Hello World - Part II.\n", rank));
469566063dSJacob Faibussowitsch PetscCall(PetscSynchronizedFlush(PETSC_COMM_WORLD, PETSC_STDOUT));
47c4762a1bSJed Brown /*
48c4762a1bSJed Brown Here a barrier is used to separate the two states.
49c4762a1bSJed Brown */
509566063dSJacob Faibussowitsch PetscCallMPI(MPI_Barrier(PETSC_COMM_WORLD));
51c4762a1bSJed Brown
52c4762a1bSJed Brown /*
53c4762a1bSJed Brown Here we simply use PetscPrintf() with the communicator PETSC_COMM_SELF
54c4762a1bSJed Brown (where each process is considered separately). Thus, this time the
55c4762a1bSJed Brown output from different processes does not appear in any particular order.
56c4762a1bSJed Brown */
579566063dSJacob Faibussowitsch PetscCall(PetscPrintf(PETSC_COMM_SELF, "[%d] Jumbled Hello World\n", rank));
58c4762a1bSJed Brown
59c4762a1bSJed Brown /*
60c4762a1bSJed Brown Always call PetscFinalize() before exiting a program. This routine
61c4762a1bSJed Brown - finalizes the PETSc libraries as well as MPI
62c4762a1bSJed Brown - provides summary and diagnostic information if certain runtime
63c4762a1bSJed Brown options are chosen (e.g., -log_view).
64c4762a1bSJed Brown See the PetscFinalize() manpage for more information.
65c4762a1bSJed Brown */
669566063dSJacob Faibussowitsch PetscCall(PetscFinalize());
67b122ec5aSJacob Faibussowitsch return 0;
68c4762a1bSJed Brown }
69c4762a1bSJed Brown
70c4762a1bSJed Brown /*TEST
71c4762a1bSJed Brown
72c4762a1bSJed Brown test:
73c4762a1bSJed Brown
74c4762a1bSJed Brown TEST*/
75