Actual source code: ex3.c

petsc-3.13.6 2020-09-29
Report Typos and Errors

  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 Section 1.5 Writing Application Codes with PETSc\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*/



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

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

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

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

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

 53:   /*
 54:      We disable the logging of an event.

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

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

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

 81:   PetscFinalize();
 82:   return ierr;
 83: }



 87: /*TEST

 89:    build:
 90:      requires: define(PETSC_USE_LOG)

 92:    test:

 94: TEST*/