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