Date: 2026-06-01T23:13:45+00:00

Actual source code: ex1.c

      1: static char help[] = "Introductory example that illustrates printing.\n\n";

      3: #include <petscsys.h>
      4: int main(int argc, char **argv)
      5: {
      6:   PetscMPIInt rank, size;

      8:   /*
      9:     Every PETSc program should begin with the PetscInitialize() routine.
     10:     argc, argv - These command line arguments are taken to extract the options
     11:                  supplied to PETSc and options supplied to MPI.
     12:     help       - When PETSc executable is invoked with the option -help,
     13:                  it prints the various options that can be applied at
     14:                  runtime.  The user can use the "help" variable to place
     15:                  additional help messages in this printout.
     16:   */
     17:   PetscFunctionBeginUser;
     18:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));

     20:   /*
     21:      The following MPI calls return the number of processes
     22:      being used and the rank of this process in the group.
     23:    */
     24:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
     25:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));

     27:   /*
     28:      Here we would like to print only one message that represents
     29:      all the processes in the group.  We use PetscPrintf() with the
     30:      communicator PETSC_COMM_WORLD.  Thus, only one message is
     31:      printed representng PETSC_COMM_WORLD, i.e., all the processors.
     32:   */
     33:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Number of processors = %d, rank = %d\n", size, rank));

     35:   /*
     36:     Here a barrier is used to separate the two program states.
     37:   */
     38:   PetscCallMPI(MPI_Barrier(PETSC_COMM_WORLD));

     40:   /*
     41:     Here we simply use PetscPrintf() with the communicator PETSC_COMM_SELF,
     42:     where each process is considered separately and prints independently
     43:     to the screen.  Thus, the output from different processes does not
     44:     appear in any particular order.
     45:   */

     47:   PetscCall(PetscPrintf(PETSC_COMM_SELF, "[%d] Jumbled Hello World\n", rank));

     49:   /*
     50:      Always call PetscFinalize() before exiting a program.  This routine
     51:        - finalizes the PETSc libraries as well as MPI
     52:        - provides summary and diagnostic information if certain runtime
     53:          options are chosen (e.g., -log_view).  See PetscFinalize()
     54:      manpage for more information.
     55:   */
     56:   PetscCall(PetscFinalize());
     57:   return 0;
     58: }

     60: /*TEST

     62:    test:

     64: TEST*/