xref: /petsc/src/sys/tutorials/ex4.c (revision 030f984af8d8bb4c203755d35bded3c05b3d83ce)
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 #if defined(PETSC_HAVE_ELEMENTAL)
21   ierr = PetscElementalInitializePackage();if (ierr) return ierr;
22 #endif
23   /* We can now change the communicator universe for PETSc */
24   ierr = MPI_Comm_rank(MPI_COMM_WORLD, &rank);if (ierr) return ierr;
25   ierr = MPI_Comm_split(MPI_COMM_WORLD, rank%2, 0, &PETSC_COMM_WORLD);if (ierr) return ierr;
26 
27   /*
28     Every PETSc routine should begin with the PetscInitialize() routine.
29     argc, argv - These command line arguments are taken to extract the options
30                  supplied to PETSc and options supplied to MPI.
31     help       - When PETSc executable is invoked with the option -help,
32                  it prints the various options that can be applied at
33                  runtime.  The user can use the "help" variable place
34                  additional help messages in this printout.
35   */
36   ierr = PetscInitialize(&argc, &argv, (char*) 0, help);if (ierr) return ierr;
37 
38   /*
39      The following MPI calls return the number of processes
40      being used and the rank of this process in the group.
41    */
42   ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRMPI(ierr);
43   ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRMPI(ierr);
44 
45   /*
46      Here we would like to print only one message that represents
47      all the processes in the group.  We use PetscPrintf() with the
48      communicator PETSC_COMM_WORLD.  Thus, only one message is
49      printed representng PETSC_COMM_WORLD, i.e., all the processors.
50   */
51   ierr = PetscPrintf(PETSC_COMM_WORLD,"Number of processors = %d, rank = %d\n", size, rank);CHKERRQ(ierr);
52 
53   /*
54      Always call PetscFinalize() before exiting a program.  This routine
55        - finalizes the PETSc libraries as well as MPI
56        - provides summary and diagnostic information if certain runtime
57          options are chosen (e.g., -log_view).  See PetscFinalize()
58      manpage for more information.
59   */
60   ierr = PetscFinalize();if (ierr) return ierr;
61   ierr = MPI_Comm_free(&PETSC_COMM_WORLD);if (ierr) return ierr;
62 #if defined(PETSC_HAVE_ELEMENTAL)
63   ierr = PetscElementalFinalizePackage();if (ierr) return ierr;
64 #endif
65   /* Since we initialized MPI, we must call MPI_Finalize() */
66   ierr = MPI_Finalize();
67   return ierr;
68 }
69 
70 /*TEST
71 
72    test:
73       nsize: 5
74       args: -options_left no
75       filter: sort -b | grep -v saws_port_auto_selectcd
76       filter_output: sort -b
77 
78 TEST*/
79