xref: /petsc/src/sys/tutorials/ex3.c (revision d71ae5a4db6382e7f06317b8d368875286fe9008)
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   PetscFunctionBeginUser;
22   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
23 
24   /*
25      Create a new user-defined event.
26       - Note that PetscLogEventRegister() returns to the user a unique
27         integer event number, which should then be used for profiling
28         the event via PetscLogEventBegin() and PetscLogEventEnd().
29       - The user can also optionally log floating point operations
30         with the routine PetscLogFlops().
31   */
32   PetscCall(PetscLogEventRegister("User event", PETSC_VIEWER_CLASSID, &USER_EVENT));
33   PetscCall(PetscLogEventGetId("User event", &check_USER_EVENT));
34   PetscCheck(USER_EVENT == check_USER_EVENT, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Event Ids do not match");
35 
36   PetscCall(PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0));
37   icount = 0;
38   for (i = 0; i < imax; i++) icount++;
39   PetscCall(PetscLogFlops(imax));
40   PetscCall(PetscSleep(0.5));
41   PetscCall(PetscLogEventEnd(USER_EVENT, 0, 0, 0, 0));
42 
43   /*
44      We disable the logging of an event.
45 
46   */
47   PetscCall(PetscLogEventDeactivate(USER_EVENT));
48   PetscCall(PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0));
49   PetscCall(PetscSleep(0.5));
50   PetscCall(PetscLogEventEnd(USER_EVENT, 0, 0, 0, 0));
51 
52   /*
53      We next enable the logging of an event
54   */
55   PetscCall(PetscLogEventActivate(USER_EVENT));
56   PetscCall(PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0));
57   PetscCall(PetscSleep(0.5));
58   PetscCall(PetscLogEventEnd(USER_EVENT, 0, 0, 0, 0));
59 
60   /*
61      We test event logging imbalance
62   */
63   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
64   if (rank == 0) PetscCall(PetscSleep(0.5));
65   PetscCall(PetscLogEventSync(USER_EVENT, PETSC_COMM_WORLD));
66   PetscCall(PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0));
67   PetscCallMPI(MPI_Barrier(PETSC_COMM_WORLD));
68   PetscCall(PetscSleep(0.5));
69   PetscCall(PetscLogEventEnd(USER_EVENT, 0, 0, 0, 0));
70 
71   PetscCall(PetscFinalize());
72   return 0;
73 }
74 
75 /*TEST
76 
77    build:
78      requires: defined(PETSC_USE_LOG)
79 
80    test:
81 
82 TEST*/
83