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