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