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>
 12: #include <petscviewer.h>

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

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

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

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

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

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

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

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

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

 75: /*TEST

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

 80:    test:

 82: TEST*/