Actual source code: ex4.c
1: static char help = "Introductory example that illustrates running PETSc on a subset of processes.\n\n";
4: Concepts: introduction to PETSc;
5: Concepts: process^subset set PETSC_COMM_WORLD
6: Processors: 2
8: Note that this example is not checking the error codes from the MPI calls before and after PETSc is initialized.
9: This is because the PETSc macro CHKERRQ() will not work in those circumstances. You should also check the
10: MPI codes in this situation with your own code.
12: #include <petscsys.h>
14: int main(int argc, char *argv)
17: PetscMPIInt rank, size;
19: /* We must call MPI_Init() first, making us, not PETSc, responsible for MPI */
20: MPI_Init(&argc, &argv);if (ierr) return ierr;
22: /* We can now change the communicator universe for PETSc */
23: MPI_Comm_rank(MPI_COMM_WORLD, &rank);if (ierr) return ierr;
24: MPI_Comm_split(MPI_COMM_WORLD, rank%2, 0, &PETSC_COMM_WORLD);if (ierr) return ierr;
27: Every PETSc routine should begin with the PetscInitialize() routine.
28: argc, argv - These command line arguments are taken to extract the options
29: supplied to PETSc and options supplied to MPI.
30: help - When PETSc executable is invoked with the option -help,
31: it prints the various options that can be applied at
32: runtime. The user can use the "help" variable place
33: additional help messages in this printout.
35: PetscInitialize(&argc, &argv, (char*) 0, help);if (ierr) return ierr;
38: The following MPI calls return the number of processes
39: being used and the rank of this process in the group.
45: Here we would like to print only one message that represents
46: all the processes in the group. We use PetscPrintf() with the
47: communicator PETSC_COMM_WORLD. Thus, only one message is
48: printed representng PETSC_COMM_WORLD, i.e., all the processors.
50: PetscPrintf(PETSC_COMM_WORLD,"Number of processors = %d, rank = %d\n", size, rank);
53: Always call PetscFinalize() before exiting a program. This routine
54: - finalizes the PETSc libraries as well as MPI
55: - provides summary and diagnostic information if certain runtime
56: options are chosen (e.g., -log_summary). See PetscFinalize()
57: manpage for more information.
59: PetscFinalize();if (ierr) return ierr;
61: MPI_Comm_free(&PETSC_COMM_WORLD);if (ierr) return ierr;
62: /* Since we initialized MPI, we must call MPI_Finalize() */
64: return ierr;