Date: 2022-03-31T04:46:17+00:00

Actual source code: ex4.c

      1: static char help[] = "Introductory example that illustrates running PETSc on a subset of processes.\n\n";

      3: /*T
      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 with PetscCall() before PETSc is initialized
      9:    and after PETSc is finalized. This is because the PETSc macro PetscCall() will not work in those circumstances.
     10: T*/
     11: #include <petscsys.h>

     13: int main(int argc, char *argv[])
     14: {
     16:   PetscMPIInt    rank, size;

     18:   /* We must call MPI_Init() first, making us, not PETSc, responsible for MPI */
     19:   MPI_Init(&argc, &argv);if (ierr) return ierr;
     20: #if defined(PETSC_HAVE_ELEMENTAL)
     21:   PetscElementalInitializePackage();if (ierr) return ierr;
     22: #endif
     23:   /* We can now change the communicator universe for PETSc */
     24:   MPI_Comm_rank(MPI_COMM_WORLD, &rank);if (ierr) return ierr;
     25:   MPI_Comm_split(MPI_COMM_WORLD, rank%2, 0, &PETSC_COMM_WORLD);if (ierr) return ierr;

     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:   PetscInitialize(&argc, &argv, (char*) 0, help);

     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:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
     43:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

     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:   PetscPrintf(PETSC_COMM_WORLD,"Number of processors = %d, rank = %d\n", size, rank);

     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:   PetscFinalize();if (ierr) return ierr;
     61:   MPI_Comm_free(&PETSC_COMM_WORLD);if (ierr) return 0;
     62: #if defined(PETSC_HAVE_ELEMENTAL)
     63:   PetscElementalFinalizePackage();if (ierr) return ierr;
     64: #endif
     65:   /* Since we initialized MPI, we must call MPI_Finalize() */
     66:   MPI_Finalize();
     67:   return ierr;
     68: }

     70: /*TEST

     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

     78: TEST*/