Date: 2022-08-02T03:13:28+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: #include <petscsys.h>

      5: int main(int argc, char *argv[])
      6: {
      8:   PetscMPIInt    rank, size;

     10:   /* We must call MPI_Init() first, making us, not PETSc, responsible for MPI */
     11:   MPI_Init(&argc, &argv);if (ierr) return ierr;
     12: #if defined(PETSC_HAVE_ELEMENTAL)
     13:   PetscElementalInitializePackage();if (ierr) return ierr;
     14: #endif
     15:   /* We can now change the communicator universe for PETSc */
     16:   MPI_Comm_rank(MPI_COMM_WORLD, &rank);if (ierr) return ierr;
     17:   MPI_Comm_split(MPI_COMM_WORLD, rank%2, 0, &PETSC_COMM_WORLD);if (ierr) return ierr;

     19:   /*
     20:     Every PETSc routine should begin with the PetscInitialize() routine.
     21:     argc, argv - These command line arguments are taken to extract the options
     22:                  supplied to PETSc and options supplied to MPI.
     23:     help       - When PETSc executable is invoked with the option -help,
     24:                  it prints the various options that can be applied at
     25:                  runtime.  The user can use the "help" variable place
     26:                  additional help messages in this printout.
     27:   */
     28:   PetscInitialize(&argc, &argv, (char*) 0, help);

     30:   /*
     31:      The following MPI calls return the number of processes
     32:      being used and the rank of this process in the group.
     33:    */
     34:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
     35:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

     37:   /*
     38:      Here we would like to print only one message that represents
     39:      all the processes in the group.  We use PetscPrintf() with the
     40:      communicator PETSC_COMM_WORLD.  Thus, only one message is
     41:      printed representng PETSC_COMM_WORLD, i.e., all the processors.
     42:   */
     43:   PetscPrintf(PETSC_COMM_WORLD,"Number of processors = %d, rank = %d\n", size, rank);

     45:   /*
     46:      Always call PetscFinalize() before exiting a program.  This routine
     47:        - finalizes the PETSc libraries as well as MPI
     48:        - provides summary and diagnostic information if certain runtime
     49:          options are chosen (e.g., -log_view).  See PetscFinalize()
     50:      manpage for more information.
     51:   */
     52:   PetscFinalize();if (ierr) return ierr;
     53:   MPI_Comm_free(&PETSC_COMM_WORLD);if (ierr) return 0;
     54: #if defined(PETSC_HAVE_ELEMENTAL)
     55:   PetscElementalFinalizePackage();if (ierr) return ierr;
     56: #endif
     57:   /* Since we initialized MPI, we must call MPI_Finalize() */
     58:   MPI_Finalize();
     59:   return ierr;
     60: }

     62: /*TEST

     64:    test:
     65:       nsize: 5
     66:       args: -options_left no
     67:       filter: sort -b | grep -v saws_port_auto_selectcd
     68:       filter_output: sort -b

     70: TEST*/