xref: /petsc/src/sys/tutorials/ex1.c (revision 6a98f8dc3f2c9149905a87dc2e9d0fedaf64e09a)
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 
11 
12 #include <petscsys.h>
13 int main(int argc,char **argv)
14 {
15   PetscErrorCode ierr;
16   PetscMPIInt    rank,size;
17 
18   /*
19     Every PETSc routine 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 place
25                  additional help messages in this printout.
26   */
27   ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
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   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
34   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
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 representng PETSC_COMM_WORLD, i.e., all the processors.
41   */
42   ierr = PetscPrintf(PETSC_COMM_WORLD,"Number of processors = %d, rank = %d\n",size,rank);CHKERRQ(ierr);
43 
44   /*
45     Here a barrier is used to separate the two program states.
46   */
47   ierr = MPI_Barrier(PETSC_COMM_WORLD);CHKERRQ(ierr);
48 
49   /*
50     Here we simply use PetscPrintf() with the communicator PETSC_COMM_SELF,
51     where each process is considered separately and prints independently
52     to the screen.  Thus, the output from different processes does not
53     appear in any particular order.
54   */
55 
56   ierr = PetscPrintf(PETSC_COMM_SELF,"[%d] Jumbled Hello World\n",rank);CHKERRQ(ierr);
57 
58   /*
59      Always call PetscFinalize() before exiting a program.  This routine
60        - finalizes the PETSc libraries as well as MPI
61        - provides summary and diagnostic information if certain runtime
62          options are chosen (e.g., -log_view).  See PetscFinalize()
63      manpage for more information.
64   */
65   ierr = PetscFinalize();
66   return ierr;
67 }
68 
69 
70 /*TEST
71 
72    test:
73 
74 TEST*/
75