Actual source code: ex3.c

  1: static char help[] = "Augmenting PETSc profiling by add events.\n\
  2: Run this program with one of the\n\
  3: following options to generate logging information:  -log, -log_view,\n\
  4: -log_all.  The PETSc routines automatically log event times and flops,\n\
  5: so this monitoring is intended solely for users to employ in application\n\
  6: codes.\n\n";

  8: /*
  9:   Include "petscsys.h" so that we can use PETSc profiling routines.
 10: */
 11: #include <petscsys.h>

 13: int main(int argc, char **argv)
 14: {
 15:   PetscMPIInt   rank;
 16:   int           i, imax = 10000, icount;
 17:   PetscLogEvent USER_EVENT, check_USER_EVENT;

 19:   PetscFunctionBeginUser;
 20:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));

 22:   /*
 23:      Create a new user-defined event.
 24:       - Note that PetscLogEventRegister() returns to the user a unique
 25:         integer event number, which should then be used for profiling
 26:         the event via PetscLogEventBegin() and PetscLogEventEnd().
 27:       - The user can also optionally log floating point operations
 28:         with the routine PetscLogFlops().
 29:   */
 30:   PetscCall(PetscLogEventRegister("User event", PETSC_VIEWER_CLASSID, &USER_EVENT));
 31:   PetscCall(PetscLogEventGetId("User event", &check_USER_EVENT));
 32:   PetscCheck(USER_EVENT == check_USER_EVENT, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Event Ids do not match");

 34:   PetscCall(PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0));
 35:   icount = 0;
 36:   for (i = 0; i < imax; i++) icount++;
 37:   (void)icount;
 38:   PetscCall(PetscLogFlops(imax));
 39:   PetscCall(PetscSleep(0.5));
 40:   PetscCall(PetscLogEventEnd(USER_EVENT, 0, 0, 0, 0));

 42:   /*
 43:      We disable the logging of an event.

 45:   */
 46:   PetscCall(PetscLogEventDeactivate(USER_EVENT));
 47:   PetscCall(PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0));
 48:   PetscCall(PetscSleep(0.5));
 49:   PetscCall(PetscLogEventEnd(USER_EVENT, 0, 0, 0, 0));

 51:   /*
 52:      We next enable the logging of an event
 53:   */
 54:   PetscCall(PetscLogEventActivate(USER_EVENT));
 55:   PetscCall(PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0));
 56:   PetscCall(PetscSleep(0.5));
 57:   PetscCall(PetscLogEventEnd(USER_EVENT, 0, 0, 0, 0));

 59:   /*
 60:      We test event logging imbalance
 61:   */
 62:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
 63:   if (rank == 0) PetscCall(PetscSleep(0.5));
 64:   PetscCall(PetscLogEventSync(USER_EVENT, PETSC_COMM_WORLD));
 65:   PetscCall(PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0));
 66:   PetscCallMPI(MPI_Barrier(PETSC_COMM_WORLD));
 67:   PetscCall(PetscSleep(0.5));
 68:   PetscCall(PetscLogEventEnd(USER_EVENT, 0, 0, 0, 0));

 70:   PetscCall(PetscFinalize());
 71:   return 0;
 72: }

 74: /*TEST

 76:    build:
 77:      requires: defined(PETSC_USE_LOG)

 79:    test:
 80:      output_file: output/empty.out

 82: TEST*/