Actual source code: ex2.c

petsc-3.3-p7 2013-05-11
  2: /* Program usage:  mpiexec ex2 [-help] [all PETSc options] */

  4: static char help[] = "Synchronized printing.\n\n";

  6: /*T
  7:    Concepts: introduction to PETSc;
  8:    Concepts: printing^synchronized
  9:    Concepts: printing^in parallel
 10:    Concepts: printf^synchronized
 11:    Concepts: printf^in parallel
 12:    Processors: n
 13: T*/
 14: 
 15: #include <petscsys.h>
 16: int main(int argc,char **argv)
 17: {
 19:   PetscMPIInt    rank,size;

 21:   /*
 22:     Every PETSc program should begin with the PetscInitialize() routine.
 23:     argc, argv - These command line arguments are taken to extract the options
 24:                  supplied to PETSc and options supplied to MPI.
 25:     help       - When PETSc executable is invoked with the option -help, 
 26:                  it prints the various options that can be applied at 
 27:                  runtime.  The user can use the "help" variable place
 28:                  additional help messages in this printout.
 29:   */
 30:   PetscInitialize(&argc,&argv,PETSC_NULL,help);

 32:   /* 
 33:      The following MPI calls return the number of processes
 34:      being used and the rank of this process in the group.
 35:    */
 36:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 37:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

 39:   /* 
 40:      Here we would like to print only one message that represents
 41:      all the processes in the group.  We use PetscPrintf() with the 
 42:      communicator PETSC_COMM_WORLD.  Thus, only one message is
 43:      printed representing PETSC_COMM_WORLD, i.e., all the processors.
 44:   */
 45:   PetscPrintf(PETSC_COMM_WORLD,"Number of processors = %d, rank = %d\n",size,rank);
 46:   /* 
 47:      Here we would like to print info from each process, such that
 48:      output from process "n" appears after output from process "n-1".
 49:      To do this we use a combination of PetscSynchronizedPrintf() and
 50:      PetscSynchronizedFlush() with the communicator PETSC_COMM_WORLD.
 51:      All the processes print the message, one after another. 
 52:      PetscSynchronizedFlush() indicates that the current process in the
 53:      given communicator has concluded printing, so that the next process
 54:      in the communicator can begin printing to the screen.
 55:      */
 56:   PetscSynchronizedPrintf(PETSC_COMM_WORLD,"[%d] Synchronized Hello World.\n",rank);
 57:   PetscSynchronizedPrintf(PETSC_COMM_WORLD,"[%d] Synchronized Hello World - Part II.\n",rank);
 58:   PetscSynchronizedFlush(PETSC_COMM_WORLD);
 59:   /*
 60:     Here a barrier is used to separate the two states.
 61:   */
 62:   MPI_Barrier(PETSC_COMM_WORLD);

 64:   /*
 65:     Here we simply use PetscPrintf() with the communicator PETSC_COMM_SELF
 66:     (where each process is considered separately).  Thus, this time the
 67:     output from different processes does not appear in any particular order.
 68:   */
 69:   PetscPrintf(PETSC_COMM_SELF,"[%d] Jumbled Hello World\n",rank);

 71:   /*
 72:      Always call PetscFinalize() before exiting a program.  This routine
 73:        - finalizes the PETSc libraries as well as MPI
 74:        - provides summary and diagnostic information if certain runtime
 75:          options are chosen (e.g., -log_summary).  
 76:      See the PetscFinalize() manpage for more information.
 77:   */
 78:   PetscFinalize();
 79:   return 0;
 80: }