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:   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:   PetscLogEventRegister("User event",PETSC_VIEWER_CLASSID,&USER_EVENT);
 32:   PetscLogEventGetId("User event",&check_USER_EVENT);

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

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

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

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

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

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

 74: /*TEST

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

 79:    test:

 81: TEST*/