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: /*T
 10:    Concepts: PetscLog^user-defined event profiling
 11:    Concepts: profiling^user-defined event
 12:    Concepts: PetscLog^activating/deactivating events for profiling
 13:    Concepts: profiling^activating/deactivating events
 14:    Processors: n
 15: T*/

 17: /*
 18:   Include "petscsys.h" so that we can use PETSc profiling routines.
 19: */
 20: #include <petscsys.h>
 21: #include <petscviewer.h>

 23: int main(int argc,char **argv)
 24: {
 26:   PetscMPIInt    rank;
 27:   int            i,imax=10000,icount;
 28:   PetscLogEvent  USER_EVENT,check_USER_EVENT;

 30:   PetscInitialize(&argc,&argv,NULL,help);if (ierr) return ierr;

 32:   /*
 33:      Create a new user-defined event.
 34:       - Note that PetscLogEventRegister() returns to the user a unique
 35:         integer event number, which should then be used for profiling
 36:         the event via PetscLogEventBegin() and PetscLogEventEnd().
 37:       - The user can also optionally log floating point operations
 38:         with the routine PetscLogFlops().
 39:   */
 40:   PetscLogEventRegister("User event",PETSC_VIEWER_CLASSID,&USER_EVENT);
 41:   PetscLogEventGetId("User event",&check_USER_EVENT);
 42:   if (USER_EVENT != check_USER_EVENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Event Ids do not match");

 44:   PetscLogEventBegin(USER_EVENT,0,0,0,0);
 45:   icount = 0;
 46:   for (i=0; i<imax; i++) icount++;
 47:   PetscLogFlops(imax);
 48:   PetscSleep(0.5);
 49:   PetscLogEventEnd(USER_EVENT,0,0,0,0);

 51:   /*
 52:      We disable the logging of an event.

 54:   */
 55:   PetscLogEventDeactivate(USER_EVENT);
 56:   PetscLogEventBegin(USER_EVENT,0,0,0,0);
 57:   PetscSleep(0.5);
 58:   PetscLogEventEnd(USER_EVENT,0,0,0,0);

 60:   /*
 61:      We next enable the logging of an event
 62:   */
 63:   PetscLogEventActivate(USER_EVENT);
 64:   PetscLogEventBegin(USER_EVENT,0,0,0,0);
 65:   PetscSleep(0.5);
 66:   PetscLogEventEnd(USER_EVENT,0,0,0,0);

 68:   /*
 69:      We test event logging imbalance
 70:   */
 71:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 72:   if (rank == 0) {PetscSleep(0.5);}
 73:   PetscLogEventSync(USER_EVENT,PETSC_COMM_WORLD);
 74:   PetscLogEventBegin(USER_EVENT,0,0,0,0);
 75:   MPI_Barrier(PETSC_COMM_WORLD);
 76:   PetscSleep(0.5);
 77:   PetscLogEventEnd(USER_EVENT,0,0,0,0);

 79:   PetscFinalize();
 80:   return ierr;
 81: }

 83: /*TEST

 85:    build:
 86:      requires: defined(PETSC_USE_LOG)

 88:    test:

 90: TEST*/