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