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: {
25: PetscMPIInt rank;
26: int i,imax=10000,icount;
27: PetscLogEvent USER_EVENT,check_USER_EVENT;
29: PetscInitialize(&argc,&argv,NULL,help);
31: /*
32: Create a new user-defined event.
33: - Note that PetscLogEventRegister() returns to the user a unique
34: integer event number, which should then be used for profiling
35: the event via PetscLogEventBegin() and PetscLogEventEnd().
36: - The user can also optionally log floating point operations
37: with the routine PetscLogFlops().
38: */
39: PetscLogEventRegister("User event",PETSC_VIEWER_CLASSID,&USER_EVENT);
40: PetscLogEventGetId("User event",&check_USER_EVENT);
43: PetscLogEventBegin(USER_EVENT,0,0,0,0);
44: icount = 0;
45: for (i=0; i<imax; i++) icount++;
46: PetscLogFlops(imax);
47: PetscSleep(0.5);
48: PetscLogEventEnd(USER_EVENT,0,0,0,0);
50: /*
51: We disable the logging of an event.
53: */
54: PetscLogEventDeactivate(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 next enable the logging of an event
61: */
62: PetscLogEventActivate(USER_EVENT);
63: PetscLogEventBegin(USER_EVENT,0,0,0,0);
64: PetscSleep(0.5);
65: PetscLogEventEnd(USER_EVENT,0,0,0,0);
67: /*
68: We test event logging imbalance
69: */
70: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
71: if (rank == 0) PetscSleep(0.5);
72: PetscLogEventSync(USER_EVENT,PETSC_COMM_WORLD);
73: PetscLogEventBegin(USER_EVENT,0,0,0,0);
74: MPI_Barrier(PETSC_COMM_WORLD);
75: PetscSleep(0.5);
76: PetscLogEventEnd(USER_EVENT,0,0,0,0);
78: PetscFinalize();
79: return 0;
80: }
82: /*TEST
84: build:
85: requires: defined(PETSC_USE_LOG)
87: test:
89: TEST*/