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