Actual source code: ex3.c


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

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

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

 21:   PetscFunctionBeginUser;
 22:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));

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

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

 44:   /*
 45:      We disable the logging of an event.

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

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

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

 72:   PetscCall(PetscFinalize());
 73:   return 0;
 74: }

 76: /*TEST

 78:    build:
 79:      requires: defined(PETSC_USE_LOG)

 81:    test:

 83: TEST*/