xref: /petsc/src/sys/tutorials/ex3.c (revision 08401ef684002a709c6d3db98a0c9f54a8bcf1ec)
1 
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";
8 
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*/
16 
17 /*
18   Include "petscsys.h" so that we can use PETSc profiling routines.
19 */
20 #include <petscsys.h>
21 #include <petscviewer.h>
22 
23 int main(int argc,char **argv)
24 {
25   PetscMPIInt    rank;
26   int            i,imax=10000,icount;
27   PetscLogEvent  USER_EVENT,check_USER_EVENT;
28 
29   PetscCall(PetscInitialize(&argc,&argv,NULL,help));
30 
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   PetscCall(PetscLogEventRegister("User event",PETSC_VIEWER_CLASSID,&USER_EVENT));
40   PetscCall(PetscLogEventGetId("User event",&check_USER_EVENT));
41   PetscCheck(USER_EVENT == check_USER_EVENT,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Event Ids do not match");
42 
43   PetscCall(PetscLogEventBegin(USER_EVENT,0,0,0,0));
44   icount = 0;
45   for (i=0; i<imax; i++) icount++;
46   PetscCall(PetscLogFlops(imax));
47   PetscCall(PetscSleep(0.5));
48   PetscCall(PetscLogEventEnd(USER_EVENT,0,0,0,0));
49 
50   /*
51      We disable the logging of an event.
52 
53   */
54   PetscCall(PetscLogEventDeactivate(USER_EVENT));
55   PetscCall(PetscLogEventBegin(USER_EVENT,0,0,0,0));
56   PetscCall(PetscSleep(0.5));
57   PetscCall(PetscLogEventEnd(USER_EVENT,0,0,0,0));
58 
59   /*
60      We next enable the logging of an event
61   */
62   PetscCall(PetscLogEventActivate(USER_EVENT));
63   PetscCall(PetscLogEventBegin(USER_EVENT,0,0,0,0));
64   PetscCall(PetscSleep(0.5));
65   PetscCall(PetscLogEventEnd(USER_EVENT,0,0,0,0));
66 
67   /*
68      We test event logging imbalance
69   */
70   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
71   if (rank == 0) PetscCall(PetscSleep(0.5));
72   PetscCall(PetscLogEventSync(USER_EVENT,PETSC_COMM_WORLD));
73   PetscCall(PetscLogEventBegin(USER_EVENT,0,0,0,0));
74   PetscCallMPI(MPI_Barrier(PETSC_COMM_WORLD));
75   PetscCall(PetscSleep(0.5));
76   PetscCall(PetscLogEventEnd(USER_EVENT,0,0,0,0));
77 
78   PetscCall(PetscFinalize());
79   return 0;
80 }
81 
82 /*TEST
83 
84    build:
85      requires: defined(PETSC_USE_LOG)
86 
87    test:
88 
89 TEST*/
90