xref: /petsc/src/sys/tutorials/ex3.c (revision 5f80ce2ab25dff0f4601e710601cbbcecf323266)
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   PetscErrorCode ierr;
26   PetscMPIInt    rank;
27   int            i,imax=10000,icount;
28   PetscLogEvent  USER_EVENT,check_USER_EVENT;
29 
30   ierr = PetscInitialize(&argc,&argv,NULL,help);if (ierr) return ierr;
31 
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   CHKERRQ(PetscLogEventRegister("User event",PETSC_VIEWER_CLASSID,&USER_EVENT));
41   CHKERRQ(PetscLogEventGetId("User event",&check_USER_EVENT));
42   PetscCheckFalse(USER_EVENT != check_USER_EVENT,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Event Ids do not match");
43 
44   CHKERRQ(PetscLogEventBegin(USER_EVENT,0,0,0,0));
45   icount = 0;
46   for (i=0; i<imax; i++) icount++;
47   CHKERRQ(PetscLogFlops(imax));
48   CHKERRQ(PetscSleep(0.5));
49   CHKERRQ(PetscLogEventEnd(USER_EVENT,0,0,0,0));
50 
51   /*
52      We disable the logging of an event.
53 
54   */
55   CHKERRQ(PetscLogEventDeactivate(USER_EVENT));
56   CHKERRQ(PetscLogEventBegin(USER_EVENT,0,0,0,0));
57   CHKERRQ(PetscSleep(0.5));
58   CHKERRQ(PetscLogEventEnd(USER_EVENT,0,0,0,0));
59 
60   /*
61      We next enable the logging of an event
62   */
63   CHKERRQ(PetscLogEventActivate(USER_EVENT));
64   CHKERRQ(PetscLogEventBegin(USER_EVENT,0,0,0,0));
65   CHKERRQ(PetscSleep(0.5));
66   CHKERRQ(PetscLogEventEnd(USER_EVENT,0,0,0,0));
67 
68   /*
69      We test event logging imbalance
70   */
71   CHKERRMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank));
72   if (rank == 0) CHKERRQ(PetscSleep(0.5));
73   CHKERRQ(PetscLogEventSync(USER_EVENT,PETSC_COMM_WORLD));
74   CHKERRQ(PetscLogEventBegin(USER_EVENT,0,0,0,0));
75   CHKERRMPI(MPI_Barrier(PETSC_COMM_WORLD));
76   CHKERRQ(PetscSleep(0.5));
77   CHKERRQ(PetscLogEventEnd(USER_EVENT,0,0,0,0));
78 
79   ierr = PetscFinalize();
80   return ierr;
81 }
82 
83 /*TEST
84 
85    build:
86      requires: defined(PETSC_USE_LOG)
87 
88    test:
89 
90 TEST*/
91