xref: /petsc/src/sys/tutorials/ex3.c (revision 5becb6a3a7fe6fe66ca978502f4679a4b24b29fc)
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 /*
10   Include "petscsys.h" so that we can use PETSc profiling routines.
11 */
12 #include <petscsys.h>
13 #include <petscviewer.h>
14 
15 int main(int argc,char **argv)
16 {
17   PetscMPIInt    rank;
18   int            i,imax=10000,icount;
19   PetscLogEvent  USER_EVENT,check_USER_EVENT;
20 
21   PetscCall(PetscInitialize(&argc,&argv,NULL,help));
22 
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   PetscCall(PetscLogEventRegister("User event",PETSC_VIEWER_CLASSID,&USER_EVENT));
32   PetscCall(PetscLogEventGetId("User event",&check_USER_EVENT));
33   PetscCheck(USER_EVENT == check_USER_EVENT,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Event Ids do not match");
34 
35   PetscCall(PetscLogEventBegin(USER_EVENT,0,0,0,0));
36   icount = 0;
37   for (i=0; i<imax; i++) icount++;
38   PetscCall(PetscLogFlops(imax));
39   PetscCall(PetscSleep(0.5));
40   PetscCall(PetscLogEventEnd(USER_EVENT,0,0,0,0));
41 
42   /*
43      We disable the logging of an event.
44 
45   */
46   PetscCall(PetscLogEventDeactivate(USER_EVENT));
47   PetscCall(PetscLogEventBegin(USER_EVENT,0,0,0,0));
48   PetscCall(PetscSleep(0.5));
49   PetscCall(PetscLogEventEnd(USER_EVENT,0,0,0,0));
50 
51   /*
52      We next enable the logging of an event
53   */
54   PetscCall(PetscLogEventActivate(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 test event logging imbalance
61   */
62   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
63   if (rank == 0) PetscCall(PetscSleep(0.5));
64   PetscCall(PetscLogEventSync(USER_EVENT,PETSC_COMM_WORLD));
65   PetscCall(PetscLogEventBegin(USER_EVENT,0,0,0,0));
66   PetscCallMPI(MPI_Barrier(PETSC_COMM_WORLD));
67   PetscCall(PetscSleep(0.5));
68   PetscCall(PetscLogEventEnd(USER_EVENT,0,0,0,0));
69 
70   PetscCall(PetscFinalize());
71   return 0;
72 }
73 
74 /*TEST
75 
76    build:
77      requires: defined(PETSC_USE_LOG)
78 
79    test:
80 
81 TEST*/
82