xref: /petsc/src/sys/tutorials/ex4.c (revision 080f0011773203ad8803837596fa1e3f25ef47ae) !
1 static char help[] = "Introductory example that illustrates running PETSc on a subset of processes.\n\n";
2 
3 /*T
4    Concepts: introduction to PETSc;
5    Concepts: process^subset set PETSC_COMM_WORLD
6    Processors: 2
7 
8    Note that this example is not checking the error codes from the MPI calls with CHKERRQ() before PETSc is initialized
9    and after PETSc is finalized. This is because the PETSc macro CHKERRQ() will not work in those circumstances.
10 T*/
11  #include <petscsys.h>
12 
13 int main(int argc, char *argv[])
14 {
15   PetscErrorCode ierr;
16   PetscMPIInt    rank, size;
17 
18   /* We must call MPI_Init() first, making us, not PETSc, responsible for MPI */
19   ierr = MPI_Init(&argc, &argv);if (ierr) return ierr;
20 
21   /* We can now change the communicator universe for PETSc */
22   ierr = MPI_Comm_rank(MPI_COMM_WORLD, &rank);if (ierr) return ierr;
23   ierr = MPI_Comm_split(MPI_COMM_WORLD, rank%2, 0, &PETSC_COMM_WORLD);if (ierr) return ierr;
24 
25   /*
26     Every PETSc routine should begin with the PetscInitialize() routine.
27     argc, argv - These command line arguments are taken to extract the options
28                  supplied to PETSc and options supplied to MPI.
29     help       - When PETSc executable is invoked with the option -help,
30                  it prints the various options that can be applied at
31                  runtime.  The user can use the "help" variable place
32                  additional help messages in this printout.
33   */
34   ierr = PetscInitialize(&argc, &argv, (char*) 0, help);if (ierr) return ierr;
35 
36   /*
37      The following MPI calls return the number of processes
38      being used and the rank of this process in the group.
39    */
40   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr);
41   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
42 
43   /*
44      Here we would like to print only one message that represents
45      all the processes in the group.  We use PetscPrintf() with the
46      communicator PETSC_COMM_WORLD.  Thus, only one message is
47      printed representng PETSC_COMM_WORLD, i.e., all the processors.
48   */
49   ierr = PetscPrintf(PETSC_COMM_WORLD,"Number of processors = %d, rank = %d\n", size, rank);CHKERRQ(ierr);
50 
51   /*
52      Always call PetscFinalize() before exiting a program.  This routine
53        - finalizes the PETSc libraries as well as MPI
54        - provides summary and diagnostic information if certain runtime
55          options are chosen (e.g., -log_view).  See PetscFinalize()
56      manpage for more information.
57   */
58   ierr = PetscFinalize();if (ierr) return ierr;
59 
60   ierr = MPI_Comm_free(&PETSC_COMM_WORLD);if (ierr) return ierr;
61   /* Since we initialized MPI, we must call MPI_Finalize() */
62   ierr = MPI_Finalize();
63   return ierr;
64 }
65 
66 
67 /*TEST
68 
69    test:
70       nsize: 5
71       args: -options_left no
72       filter: sort -b | grep -v saws_port_auto_selectcd
73       filter_output: sort -b
74 
75 TEST*/
76