xref: /petsc/src/sys/tutorials/ex2.c (revision 9566063d113dddea24716c546802770db7481bc0)
1 
2 static char help[] = "Synchronized printing.\n\n";
3 
4 /*T
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
11 T*/
12 
13 #include <petscsys.h>
14 int main(int argc,char **argv)
15 {
16   PetscMPIInt    rank,size;
17 
18   /*
19     Every PETSc program should begin with the PetscInitialize() routine.
20     argc, argv - These command line arguments are taken to extract the options
21                  supplied to PETSc and options supplied to MPI.
22     help       - When PETSc executable is invoked with the option -help,
23                  it prints the various options that can be applied at
24                  runtime.  The user can use the "help" variable to place
25                  additional help messages in this printout.
26   */
27   PetscCall(PetscInitialize(&argc,&argv,NULL,help));
28 
29   /*
30      The following MPI calls return the number of processes
31      being used and the rank of this process in the group.
32    */
33   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD,&size));
34   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
35 
36   /*
37      Here we would like to print only one message that represents
38      all the processes in the group.  We use PetscPrintf() with the
39      communicator PETSC_COMM_WORLD.  Thus, only one message is
40      printed representing PETSC_COMM_WORLD, i.e., all the processors.
41   */
42   PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Number of processors = %d, rank = %d\n",size,rank));
43   /*
44      Here we would like to print info from each process, such that
45      output from process "n" appears after output from process "n-1".
46      To do this we use a combination of PetscSynchronizedPrintf() and
47      PetscSynchronizedFlush() with the communicator PETSC_COMM_WORLD.
48      All the processes print the message, one after another.
49      PetscSynchronizedFlush() indicates that the current process in the
50      given communicator has concluded printing, so that the next process
51      in the communicator can begin printing to the screen.
52      */
53   PetscCall(PetscSynchronizedPrintf(PETSC_COMM_WORLD,"[%d] Synchronized Hello World.\n",rank));
54   PetscCall(PetscSynchronizedPrintf(PETSC_COMM_WORLD,"[%d] Synchronized Hello World - Part II.\n",rank));
55   PetscCall(PetscSynchronizedFlush(PETSC_COMM_WORLD,PETSC_STDOUT));
56   /*
57     Here a barrier is used to separate the two states.
58   */
59   PetscCallMPI(MPI_Barrier(PETSC_COMM_WORLD));
60 
61   /*
62     Here we simply use PetscPrintf() with the communicator PETSC_COMM_SELF
63     (where each process is considered separately).  Thus, this time the
64     output from different processes does not appear in any particular order.
65   */
66   PetscCall(PetscPrintf(PETSC_COMM_SELF,"[%d] Jumbled Hello World\n",rank));
67 
68   /*
69      Always call PetscFinalize() before exiting a program.  This routine
70        - finalizes the PETSc libraries as well as MPI
71        - provides summary and diagnostic information if certain runtime
72          options are chosen (e.g., -log_view).
73      See the PetscFinalize() manpage for more information.
74   */
75   PetscCall(PetscFinalize());
76   return 0;
77 }
78 
79 /*TEST
80 
81    test:
82 
83 TEST*/
84