xref: /petsc/src/sys/logging/plog.c (revision 2611ad710242b3c70d66651ef7e40f9450d305e2)
15c6c1daeSBarry Smith 
25c6c1daeSBarry Smith /*
35c6c1daeSBarry Smith       PETSc code to log object creation and destruction and PETSc events.
45c6c1daeSBarry Smith 
55c6c1daeSBarry Smith       This provides the public API used by the rest of PETSc and by users.
65c6c1daeSBarry Smith 
75c6c1daeSBarry Smith       These routines use a private API that is not used elsewhere in PETSc and is not
85c6c1daeSBarry Smith       accessible to users. The private API is defined in logimpl.h and the utils directory.
95c6c1daeSBarry Smith 
105c6c1daeSBarry Smith */
11af0996ceSBarry Smith #include <petsc/private/logimpl.h> /*I    "petscsys.h"   I*/
1253e0a2f3SToby Isaac #include <petsc/private/loghandlerimpl.h>
135c6c1daeSBarry Smith #include <petsctime.h>
14665c2dedSJed Brown #include <petscviewer.h>
158fe3844cSJunchao Zhang #include <petscdevice.h>
168fe3844cSJunchao Zhang #include <petsc/private/deviceimpl.h>
17a0c7f9aaSSamuel Khuvis #if defined(PETSC_HAVE_TAU_PERFSTUBS)
18a0c7f9aaSSamuel Khuvis   #include <../src/sys/perfstubs/timer.h>
19a0c7f9aaSSamuel Khuvis #endif
205c6c1daeSBarry Smith 
21c708d6e3SStefano Zampini #if defined(PETSC_HAVE_THREADSAFETY)
22c708d6e3SStefano Zampini 
23c708d6e3SStefano Zampini PetscInt           petsc_log_gid = -1; /* Global threadId counter */
24c708d6e3SStefano Zampini PETSC_TLS PetscInt petsc_log_tid = -1; /* Local threadId */
25c708d6e3SStefano Zampini 
26c708d6e3SStefano Zampini /* shared variables */
27c708d6e3SStefano Zampini PetscSpinlock  PetscLogSpinLock;
28c708d6e3SStefano Zampini PetscHMapEvent eventInfoMap_th = NULL;
29c708d6e3SStefano Zampini 
30*2611ad71SToby Isaac PetscInt PetscLogGetTid(void)
31*2611ad71SToby Isaac {
32*2611ad71SToby Isaac   if (petsc_log_tid < 0) {
33*2611ad71SToby Isaac     PetscCall(PetscSpinlockLock(&PetscLogSpinLock));
34*2611ad71SToby Isaac     petsc_log_tid = ++petsc_log_gid;
35*2611ad71SToby Isaac     PetscCall(PetscSpinlockUnlock(&PetscLogSpinLock));
36*2611ad71SToby Isaac   }
37*2611ad71SToby Isaac   return petsc_log_tid;
38*2611ad71SToby Isaac }
39*2611ad71SToby Isaac 
40c708d6e3SStefano Zampini #endif
41c708d6e3SStefano Zampini 
42*2611ad71SToby Isaac PetscLogEvent PETSC_LARGEST_EVENT = PETSC_EVENT;
4353e0a2f3SToby Isaac 
445c6c1daeSBarry Smith /* Global counters */
455c6c1daeSBarry Smith PetscLogDouble petsc_BaseTime        = 0.0;
465c6c1daeSBarry Smith PetscLogDouble petsc_TotalFlops      = 0.0; /* The number of flops */
475c6c1daeSBarry Smith PetscLogDouble petsc_send_ct         = 0.0; /* The number of sends */
485c6c1daeSBarry Smith PetscLogDouble petsc_recv_ct         = 0.0; /* The number of receives */
495c6c1daeSBarry Smith PetscLogDouble petsc_send_len        = 0.0; /* The total length of all sent messages */
505c6c1daeSBarry Smith PetscLogDouble petsc_recv_len        = 0.0; /* The total length of all received messages */
515c6c1daeSBarry Smith PetscLogDouble petsc_isend_ct        = 0.0; /* The number of immediate sends */
525c6c1daeSBarry Smith PetscLogDouble petsc_irecv_ct        = 0.0; /* The number of immediate receives */
535c6c1daeSBarry Smith PetscLogDouble petsc_isend_len       = 0.0; /* The total length of all immediate send messages */
545c6c1daeSBarry Smith PetscLogDouble petsc_irecv_len       = 0.0; /* The total length of all immediate receive messages */
555c6c1daeSBarry Smith PetscLogDouble petsc_wait_ct         = 0.0; /* The number of waits */
565c6c1daeSBarry Smith PetscLogDouble petsc_wait_any_ct     = 0.0; /* The number of anywaits */
575c6c1daeSBarry Smith PetscLogDouble petsc_wait_all_ct     = 0.0; /* The number of waitalls */
585c6c1daeSBarry Smith PetscLogDouble petsc_sum_of_waits_ct = 0.0; /* The total number of waits */
595c6c1daeSBarry Smith PetscLogDouble petsc_allreduce_ct    = 0.0; /* The number of reductions */
605c6c1daeSBarry Smith PetscLogDouble petsc_gather_ct       = 0.0; /* The number of gathers and gathervs */
615c6c1daeSBarry Smith PetscLogDouble petsc_scatter_ct      = 0.0; /* The number of scatters and scattervs */
62c708d6e3SStefano Zampini 
63c708d6e3SStefano Zampini /* Thread Local storage */
64c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_TotalFlops_th      = 0.0;
65c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_send_ct_th         = 0.0;
66c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_recv_ct_th         = 0.0;
67c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_send_len_th        = 0.0;
68c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_recv_len_th        = 0.0;
69c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_isend_ct_th        = 0.0;
70c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_irecv_ct_th        = 0.0;
71c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_isend_len_th       = 0.0;
72c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_irecv_len_th       = 0.0;
73c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_wait_ct_th         = 0.0;
74c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_wait_any_ct_th     = 0.0;
75c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_wait_all_ct_th     = 0.0;
76c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_sum_of_waits_ct_th = 0.0;
77c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_allreduce_ct_th    = 0.0;
78c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_gather_ct_th       = 0.0;
79c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_scatter_ct_th      = 0.0;
80c708d6e3SStefano Zampini 
81bec0b493Shannah_mairs PetscLogDouble petsc_ctog_ct        = 0.0; /* The total number of CPU to GPU copies */
82bec0b493Shannah_mairs PetscLogDouble petsc_gtoc_ct        = 0.0; /* The total number of GPU to CPU copies */
83bec0b493Shannah_mairs PetscLogDouble petsc_ctog_sz        = 0.0; /* The total size of CPU to GPU copies */
84bec0b493Shannah_mairs PetscLogDouble petsc_gtoc_sz        = 0.0; /* The total size of GPU to CPU copies */
8545c4b7c1SBarry Smith PetscLogDouble petsc_ctog_ct_scalar = 0.0; /* The total number of CPU to GPU copies */
8645c4b7c1SBarry Smith PetscLogDouble petsc_gtoc_ct_scalar = 0.0; /* The total number of GPU to CPU copies */
8745c4b7c1SBarry Smith PetscLogDouble petsc_ctog_sz_scalar = 0.0; /* The total size of CPU to GPU copies */
8845c4b7c1SBarry Smith PetscLogDouble petsc_gtoc_sz_scalar = 0.0; /* The total size of GPU to CPU copies */
89958c4211Shannah_mairs PetscLogDouble petsc_gflops         = 0.0; /* The flops done on a GPU */
90958c4211Shannah_mairs PetscLogDouble petsc_gtime          = 0.0; /* The time spent on a GPU */
91c708d6e3SStefano Zampini 
92c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_ctog_ct_th        = 0.0;
93c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_gtoc_ct_th        = 0.0;
94c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_ctog_sz_th        = 0.0;
95c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_gtoc_sz_th        = 0.0;
96c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_ctog_ct_scalar_th = 0.0;
97c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_gtoc_ct_scalar_th = 0.0;
98c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_ctog_sz_scalar_th = 0.0;
99c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_gtoc_sz_scalar_th = 0.0;
100c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_gflops_th         = 0.0;
101c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_gtime_th          = 0.0;
102*2611ad71SToby Isaac 
103*2611ad71SToby Isaac PetscBool PetscLogMemory = PETSC_FALSE;
104*2611ad71SToby Isaac PetscBool PetscLogSyncOn = PETSC_FALSE;
105*2611ad71SToby Isaac 
106*2611ad71SToby Isaac PetscBool PetscLogGpuTimeFlag = PETSC_FALSE;
107*2611ad71SToby Isaac 
108*2611ad71SToby Isaac PetscLogState petsc_log_state = NULL;
109*2611ad71SToby Isaac 
110*2611ad71SToby Isaac #define PETSC_LOG_HANDLER_HOT_BLANK \
111*2611ad71SToby Isaac   { \
112*2611ad71SToby Isaac     NULL, NULL, NULL, NULL, NULL, NULL \
113*2611ad71SToby Isaac   }
114*2611ad71SToby Isaac 
115*2611ad71SToby Isaac PetscLogHandlerHot PetscLogHandlers[PETSC_LOG_HANDLER_MAX] = {
116*2611ad71SToby Isaac   PETSC_LOG_HANDLER_HOT_BLANK,
117*2611ad71SToby Isaac   PETSC_LOG_HANDLER_HOT_BLANK,
118*2611ad71SToby Isaac   PETSC_LOG_HANDLER_HOT_BLANK,
119*2611ad71SToby Isaac   PETSC_LOG_HANDLER_HOT_BLANK,
120*2611ad71SToby Isaac };
121*2611ad71SToby Isaac 
122*2611ad71SToby Isaac #undef PETSC_LOG_HANDLERS_HOT_BLANK
123*2611ad71SToby Isaac 
124*2611ad71SToby Isaac #if defined(PETSC_USE_LOG)
125*2611ad71SToby Isaac   #include <petscmachineinfo.h>
126*2611ad71SToby Isaac   #include <petscconfiginfo.h>
127*2611ad71SToby Isaac 
128*2611ad71SToby Isaac /* used in the MPI_XXX() count macros in petsclog.h */
129*2611ad71SToby Isaac 
130*2611ad71SToby Isaac /* Action and object logging variables */
131*2611ad71SToby Isaac Action   *petsc_actions    = NULL;
132*2611ad71SToby Isaac Object   *petsc_objects    = NULL;
133*2611ad71SToby Isaac PetscBool petsc_logActions = PETSC_FALSE;
134*2611ad71SToby Isaac PetscBool petsc_logObjects = PETSC_FALSE;
135*2611ad71SToby Isaac int       petsc_numActions = 0, petsc_maxActions = 100;
136*2611ad71SToby Isaac int       petsc_numObjects = 0, petsc_maxObjects = 100;
137*2611ad71SToby Isaac int       petsc_numObjectsDestroyed = 0;
138*2611ad71SToby Isaac 
139*2611ad71SToby Isaac   #include <../src/sys/logging/handler/impls/default/logdefault.h>
140c708d6e3SStefano Zampini 
141c708d6e3SStefano Zampini   #if defined(PETSC_HAVE_THREADSAFETY)
142c708d6e3SStefano Zampini PetscErrorCode PetscAddLogDouble(PetscLogDouble *tot, PetscLogDouble *tot_th, PetscLogDouble tmp)
143c708d6e3SStefano Zampini {
144c708d6e3SStefano Zampini   *tot_th += tmp;
1453ba16761SJacob Faibussowitsch   PetscCall(PetscSpinlockLock(&PetscLogSpinLock));
146c708d6e3SStefano Zampini   *tot += tmp;
1473ba16761SJacob Faibussowitsch   PetscCall(PetscSpinlockUnlock(&PetscLogSpinLock));
1483ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
149c708d6e3SStefano Zampini }
150c708d6e3SStefano Zampini 
151c708d6e3SStefano Zampini PetscErrorCode PetscAddLogDoubleCnt(PetscLogDouble *cnt, PetscLogDouble *tot, PetscLogDouble *cnt_th, PetscLogDouble *tot_th, PetscLogDouble tmp)
152c708d6e3SStefano Zampini {
153c708d6e3SStefano Zampini   *cnt_th = *cnt_th + 1;
154c708d6e3SStefano Zampini   *tot_th += tmp;
1553ba16761SJacob Faibussowitsch   PetscCall(PetscSpinlockLock(&PetscLogSpinLock));
156c708d6e3SStefano Zampini   *tot += (PetscLogDouble)(tmp);
157c708d6e3SStefano Zampini   *cnt += *cnt + 1;
1583ba16761SJacob Faibussowitsch   PetscCall(PetscSpinlockUnlock(&PetscLogSpinLock));
1593ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
160c708d6e3SStefano Zampini }
161c708d6e3SStefano Zampini 
162bec0b493Shannah_mairs   #endif
1635c6c1daeSBarry Smith 
16453e0a2f3SToby Isaac static PetscErrorCode PetscLogTryGetHandler(PetscLogHandlerType type, PetscLogHandler *handler)
16553e0a2f3SToby Isaac {
16653e0a2f3SToby Isaac   PetscFunctionBegin;
16753e0a2f3SToby Isaac   PetscAssertPointer(handler, 2);
16853e0a2f3SToby Isaac   *handler = NULL;
16953e0a2f3SToby Isaac   for (int i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
17053e0a2f3SToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
17153e0a2f3SToby Isaac     if (h) {
17253e0a2f3SToby Isaac       PetscBool match;
17353e0a2f3SToby Isaac 
17453e0a2f3SToby Isaac       PetscCall(PetscObjectTypeCompare((PetscObject)h, type, &match));
17553e0a2f3SToby Isaac       if (match) {
17653e0a2f3SToby Isaac         *handler = PetscLogHandlers[i].handler;
17753e0a2f3SToby Isaac         PetscFunctionReturn(PETSC_SUCCESS);
17853e0a2f3SToby Isaac       }
17953e0a2f3SToby Isaac     }
18053e0a2f3SToby Isaac   }
18153e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
18253e0a2f3SToby Isaac }
18353e0a2f3SToby Isaac 
18453e0a2f3SToby Isaac /*@
18553e0a2f3SToby Isaac   PetscLogGetState - Get the `PetscLogState` for PETSc's global logging, used
18653e0a2f3SToby Isaac   by all default log handlers (`PetscLogDefaultBegin()`,
18753e0a2f3SToby Isaac   `PetscLogNestedBegin()`, `PetscLogTraceBegin()`, `PetscLogMPEBegin()`,
18853e0a2f3SToby Isaac   `PetscLogPerfstubsBegin()`).
18953e0a2f3SToby Isaac 
19053e0a2f3SToby Isaac   Collective on `PETSC_COMM_WORLD`
19153e0a2f3SToby Isaac 
19253e0a2f3SToby Isaac   Output Parameter:
19353e0a2f3SToby Isaac . state - The `PetscLogState` changed by registrations (such as `PetscLogEventRegister()`) and actions (such as `PetscLogEventBegin()` or `PetscLogStagePush()`)
19453e0a2f3SToby Isaac 
19553e0a2f3SToby Isaac   Level: developer
19653e0a2f3SToby Isaac 
19753e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogState`
19853e0a2f3SToby Isaac @*/
19953e0a2f3SToby Isaac PetscErrorCode PetscLogGetState(PetscLogState *state)
20053e0a2f3SToby Isaac {
20153e0a2f3SToby Isaac   PetscFunctionBegin;
20253e0a2f3SToby Isaac   PetscAssertPointer(state, 1);
20353e0a2f3SToby Isaac   if (!petsc_log_state) {
20453e0a2f3SToby Isaac     fprintf(stderr, "PETSC ERROR: Logging has not been enabled.\nYou might have forgotten to call PetscInitialize().\n");
20553e0a2f3SToby Isaac     PETSCABORT(MPI_COMM_WORLD, PETSC_ERR_SUP);
20653e0a2f3SToby Isaac   }
20753e0a2f3SToby Isaac   *state = petsc_log_state;
20853e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
20953e0a2f3SToby Isaac }
21053e0a2f3SToby Isaac 
2115c6c1daeSBarry Smith /* Logging functions */
2120298fd71SBarry Smith PetscErrorCode (*PetscLogPHC)(PetscObject)                                                            = NULL;
2130298fd71SBarry Smith PetscErrorCode (*PetscLogPHD)(PetscObject)                                                            = NULL;
2140298fd71SBarry Smith PetscErrorCode (*PetscLogPLB)(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject) = NULL;
2150298fd71SBarry Smith PetscErrorCode (*PetscLogPLE)(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject) = NULL;
2165c6c1daeSBarry Smith 
21753e0a2f3SToby Isaac static PetscErrorCode PetscLogHandlerCopyToHot(PetscLogHandler h, PetscLogHandlerHot *hot)
21853e0a2f3SToby Isaac {
21953e0a2f3SToby Isaac   PetscFunctionBegin;
22053e0a2f3SToby Isaac   hot->handler       = h;
22153e0a2f3SToby Isaac   hot->eventBegin    = h->ops->eventbegin;
22253e0a2f3SToby Isaac   hot->eventEnd      = h->ops->eventend;
22353e0a2f3SToby Isaac   hot->eventSync     = h->ops->eventsync;
22453e0a2f3SToby Isaac   hot->objectCreate  = h->ops->objectcreate;
22553e0a2f3SToby Isaac   hot->objectDestroy = h->ops->objectdestroy;
22653e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
22753e0a2f3SToby Isaac }
22853e0a2f3SToby Isaac 
22953e0a2f3SToby Isaac /*@
23053e0a2f3SToby Isaac   PetscLogHandlerStart - Connect a log handler to PETSc's global logging stream and state.
23153e0a2f3SToby Isaac 
23253e0a2f3SToby Isaac   Logically collective
23353e0a2f3SToby Isaac 
23453e0a2f3SToby Isaac   Input Parameters:
23553e0a2f3SToby Isaac . h - a `PetscLogHandler`
23653e0a2f3SToby Isaac 
23753e0a2f3SToby Isaac   Level: developer
23853e0a2f3SToby Isaac 
23953e0a2f3SToby Isaac   Notes:
24053e0a2f3SToby Isaac 
24153e0a2f3SToby Isaac   Users should only need this if they create their own log handlers: handlers that are started
24253e0a2f3SToby Isaac   from the command line (such as `-log_view` and `-log_trace`) or from a function like
24353e0a2f3SToby Isaac   `PetscLogNestedBegin()` will automatically be started.
24453e0a2f3SToby Isaac 
24553e0a2f3SToby Isaac   There is a limit of `PESC_LOG_HANDLER_MAX` handlers that can be active at one time.
24653e0a2f3SToby Isaac 
24753e0a2f3SToby Isaac   To disconnect a handler from the global stream call `PetscLogHandlerStop()`.
24853e0a2f3SToby Isaac 
24953e0a2f3SToby Isaac   When a log handler is started, stages that have already been pushed with `PetscLogStagePush()`,
25053e0a2f3SToby Isaac   will be pushed for the new log handler, but it will not be informed of any events that are
25153e0a2f3SToby Isaac   in progress.  It is recommended to start any user-defined log handlers immediately following
25253e0a2f3SToby Isaac   before any user-defined stages are pushed.
25353e0a2f3SToby Isaac 
25453e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogState`, `PetscLogHandlerStop()`
25553e0a2f3SToby Isaac @*/
25653e0a2f3SToby Isaac PetscErrorCode PetscLogHandlerStart(PetscLogHandler h)
25753e0a2f3SToby Isaac {
25853e0a2f3SToby Isaac   PetscFunctionBegin;
25953e0a2f3SToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
26053e0a2f3SToby Isaac     if (PetscLogHandlers[i].handler == h) PetscFunctionReturn(PETSC_SUCCESS);
26153e0a2f3SToby Isaac   }
26253e0a2f3SToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
26353e0a2f3SToby Isaac     if (PetscLogHandlers[i].handler == NULL) {
26453e0a2f3SToby Isaac       PetscCall(PetscObjectReference((PetscObject)h));
26553e0a2f3SToby Isaac       PetscCall(PetscLogHandlerCopyToHot(h, &PetscLogHandlers[i]));
26653e0a2f3SToby Isaac       if (petsc_log_state) {
26753e0a2f3SToby Isaac         PetscLogStage stack_height;
26853e0a2f3SToby Isaac         PetscIntStack orig_stack, temp_stack;
26953e0a2f3SToby Isaac 
27053e0a2f3SToby Isaac         PetscCall(PetscLogHandlerSetState(h, petsc_log_state));
27153e0a2f3SToby Isaac         stack_height = petsc_log_state->stage_stack->top + 1;
27253e0a2f3SToby Isaac         PetscCall(PetscIntStackCreate(&temp_stack));
27353e0a2f3SToby Isaac         orig_stack                     = petsc_log_state->stage_stack;
27453e0a2f3SToby Isaac         petsc_log_state->stage_stack   = temp_stack;
27553e0a2f3SToby Isaac         petsc_log_state->current_stage = -1;
27653e0a2f3SToby Isaac         for (int s = 0; s < stack_height; s++) {
27753e0a2f3SToby Isaac           PetscLogStage stage = (PetscLogStage)orig_stack->stack[s];
27853e0a2f3SToby Isaac           PetscCall(PetscLogHandlerStagePush(h, stage));
27953e0a2f3SToby Isaac           PetscCall(PetscIntStackPush(temp_stack, stage));
28053e0a2f3SToby Isaac           petsc_log_state->current_stage = stage;
28153e0a2f3SToby Isaac         }
28253e0a2f3SToby Isaac         PetscCall(PetscIntStackDestroy(temp_stack));
28353e0a2f3SToby Isaac         petsc_log_state->stage_stack = orig_stack;
28453e0a2f3SToby Isaac       }
28553e0a2f3SToby Isaac       PetscFunctionReturn(PETSC_SUCCESS);
28653e0a2f3SToby Isaac     }
28753e0a2f3SToby Isaac   }
28853e0a2f3SToby Isaac   SETERRQ(PetscObjectComm((PetscObject)h), PETSC_ERR_ARG_WRONGSTATE, "%d log handlers already started, cannot start another", PETSC_LOG_HANDLER_MAX);
28953e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
29053e0a2f3SToby Isaac }
29153e0a2f3SToby Isaac 
29253e0a2f3SToby Isaac /*@
29353e0a2f3SToby Isaac   PetscLogHandlerStop - Disconnect a log handler from PETSc's global logging stream.
29453e0a2f3SToby Isaac 
29553e0a2f3SToby Isaac   Logically collective
29653e0a2f3SToby Isaac 
29753e0a2f3SToby Isaac   Input Parameters:
29853e0a2f3SToby Isaac . h - a `PetscLogHandler`
29953e0a2f3SToby Isaac 
30053e0a2f3SToby Isaac   Level: developer
30153e0a2f3SToby Isaac 
30253e0a2f3SToby Isaac   Note:
30353e0a2f3SToby Isaac   After `PetscLogHandlerStop()`, the handler can still access the global logging state
30453e0a2f3SToby Isaac   with `PetscLogHandlerGetState()`, so that it can access the registry when post-processing
30553e0a2f3SToby Isaac   (for instance, in `PetscLogHandlerView()`),
30653e0a2f3SToby Isaac 
30753e0a2f3SToby Isaac   When a log handler is stopped, the remaining stages will be popped before it is
30853e0a2f3SToby Isaac   disconnected from the log stream.
30953e0a2f3SToby Isaac 
31053e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogState`, `PetscLogHandlerStart()`
31153e0a2f3SToby Isaac @*/
31253e0a2f3SToby Isaac PetscErrorCode PetscLogHandlerStop(PetscLogHandler h)
31353e0a2f3SToby Isaac {
31453e0a2f3SToby Isaac   PetscFunctionBegin;
31553e0a2f3SToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
31653e0a2f3SToby Isaac     if (PetscLogHandlers[i].handler == h) {
31753e0a2f3SToby Isaac       if (petsc_log_state) {
31853e0a2f3SToby Isaac         PetscLogState state;
31953e0a2f3SToby Isaac         PetscLogStage stack_height;
32053e0a2f3SToby Isaac         PetscIntStack orig_stack, temp_stack;
32153e0a2f3SToby Isaac 
32253e0a2f3SToby Isaac         PetscCall(PetscLogHandlerGetState(h, &state));
32353e0a2f3SToby Isaac         PetscCheck(state == petsc_log_state, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONGSTATE, "Called PetscLogHandlerStop() for a PetscLogHander that was not started.");
32453e0a2f3SToby Isaac         stack_height = petsc_log_state->stage_stack->top + 1;
32553e0a2f3SToby Isaac         PetscCall(PetscIntStackCreate(&temp_stack));
32653e0a2f3SToby Isaac         orig_stack                   = petsc_log_state->stage_stack;
32753e0a2f3SToby Isaac         petsc_log_state->stage_stack = temp_stack;
32853e0a2f3SToby Isaac         for (int s = 0; s < stack_height; s++) {
32953e0a2f3SToby Isaac           PetscLogStage stage = (PetscLogStage)orig_stack->stack[s];
33053e0a2f3SToby Isaac 
33153e0a2f3SToby Isaac           PetscCall(PetscIntStackPush(temp_stack, stage));
33253e0a2f3SToby Isaac         }
33353e0a2f3SToby Isaac         for (int s = 0; s < stack_height; s++) {
33453e0a2f3SToby Isaac           PetscLogStage stage;
33553e0a2f3SToby Isaac           PetscBool     empty;
33653e0a2f3SToby Isaac 
33753e0a2f3SToby Isaac           PetscCall(PetscIntStackPop(temp_stack, &stage));
33853e0a2f3SToby Isaac           PetscCall(PetscIntStackEmpty(temp_stack, &empty));
33953e0a2f3SToby Isaac           if (!empty) {
34053e0a2f3SToby Isaac             PetscCall(PetscIntStackTop(temp_stack, &petsc_log_state->current_stage));
34153e0a2f3SToby Isaac           } else petsc_log_state->current_stage = -1;
34253e0a2f3SToby Isaac           PetscCall(PetscLogHandlerStagePop(h, stage));
34353e0a2f3SToby Isaac         }
34453e0a2f3SToby Isaac         PetscCall(PetscIntStackDestroy(temp_stack));
34553e0a2f3SToby Isaac         petsc_log_state->stage_stack = orig_stack;
34653e0a2f3SToby Isaac         PetscCall(PetscIntStackTop(petsc_log_state->stage_stack, &petsc_log_state->current_stage));
34753e0a2f3SToby Isaac       }
34853e0a2f3SToby Isaac       PetscCall(PetscArrayzero(&PetscLogHandlers[i], 1));
34953e0a2f3SToby Isaac       PetscCall(PetscObjectDereference((PetscObject)h));
35053e0a2f3SToby Isaac     }
35153e0a2f3SToby Isaac   }
35253e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
35353e0a2f3SToby Isaac }
3545c6c1daeSBarry Smith /*@C
3555c6c1daeSBarry Smith   PetscLogSet - Sets the logging functions called at the beginning and ending of every event.
3565c6c1daeSBarry Smith 
3575c6c1daeSBarry Smith   Not Collective
3585c6c1daeSBarry Smith 
3595c6c1daeSBarry Smith   Input Parameters:
3605c6c1daeSBarry Smith + b - The function called at beginning of event
3615c6c1daeSBarry Smith - e - The function called at end of event
3625c6c1daeSBarry Smith 
3635c6c1daeSBarry Smith   Level: developer
3645c6c1daeSBarry Smith 
365aec76313SJacob Faibussowitsch   Developer Notes:
366811af0c4SBarry Smith   The default loggers are `PetscLogEventBeginDefault()` and `PetscLogEventEndDefault()`.
367811af0c4SBarry Smith 
368d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogDump()`, `PetscLogDefaultBegin()`, `PetscLogAllBegin()`, `PetscLogTraceBegin()`, `PetscLogEventBeginDefault()`, `PetscLogEventEndDefault()`
3695c6c1daeSBarry Smith @*/
370d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogSet(PetscErrorCode (*b)(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject), PetscErrorCode (*e)(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject))
371d71ae5a4SJacob Faibussowitsch {
3725c6c1daeSBarry Smith   PetscFunctionBegin;
3735c6c1daeSBarry Smith   PetscLogPLB = b;
3745c6c1daeSBarry Smith   PetscLogPLE = e;
3753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3765c6c1daeSBarry Smith }
3775c6c1daeSBarry Smith 
3785c6c1daeSBarry Smith /*@C
3794dd65854SConnor Ward   PetscLogIsActive - Check if logging is currently in progress.
3804dd65854SConnor Ward 
3814dd65854SConnor Ward   Not Collective
3824dd65854SConnor Ward 
3834dd65854SConnor Ward   Output Parameter:
384811af0c4SBarry Smith . isActive - `PETSC_TRUE` if logging is in progress, `PETSC_FALSE` otherwise
3854dd65854SConnor Ward 
3864dd65854SConnor Ward   Level: beginner
3874dd65854SConnor Ward 
388d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogDefaultBegin()`, `PetscLogAllBegin()`, `PetscLogSet()`
3894dd65854SConnor Ward @*/
390d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogIsActive(PetscBool *isActive)
391d71ae5a4SJacob Faibussowitsch {
3924dd65854SConnor Ward   PetscFunctionBegin;
3934dd65854SConnor Ward   *isActive = (PetscLogPLB && PetscLogPLE) ? PETSC_TRUE : PETSC_FALSE;
3943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3954dd65854SConnor Ward }
3964dd65854SConnor Ward 
39753e0a2f3SToby Isaac static PetscErrorCode PetscLogTypeBegin(PetscLogHandlerType type)
39853e0a2f3SToby Isaac {
39953e0a2f3SToby Isaac   PetscLogHandler handler;
40053e0a2f3SToby Isaac 
40153e0a2f3SToby Isaac   PetscFunctionBegin;
40253e0a2f3SToby Isaac   PetscCall(PetscLogTryGetHandler(type, &handler));
40353e0a2f3SToby Isaac   if (handler) PetscFunctionReturn(PETSC_SUCCESS);
40453e0a2f3SToby Isaac   PetscCall(PetscLogHandlerCreate(PETSC_COMM_WORLD, &handler));
40553e0a2f3SToby Isaac   PetscCall(PetscLogHandlerSetType(handler, type));
40653e0a2f3SToby Isaac   PetscCall(PetscLogHandlerStart(handler));
40753e0a2f3SToby Isaac   PetscCall(PetscLogHandlerDestroy(&handler));
40853e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
40953e0a2f3SToby Isaac }
41053e0a2f3SToby Isaac 
4114dd65854SConnor Ward /*@C
412811af0c4SBarry Smith   PetscLogDefaultBegin - Turns on logging of objects and events using the default logging functions `PetscLogEventBeginDefault()` and `PetscLogEventEndDefault()`. This logs flop
4135c6c1daeSBarry Smith   rates and object creation and should not slow programs down too much.
4145c6c1daeSBarry Smith   This routine may be called more than once.
4155c6c1daeSBarry Smith 
416811af0c4SBarry Smith   Logically Collective over `PETSC_COMM_WORLD`
4175c6c1daeSBarry Smith 
418811af0c4SBarry Smith   Options Database Key:
419a2553e36SBarry Smith . -log_view [viewertype:filename:viewerformat] - Prints summary of flop and timing information to the
420a2553e36SBarry Smith                   screen (for code configured with --with-log=1 (which is the default))
4215c6c1daeSBarry Smith 
42210450e9eSJacob Faibussowitsch   Example Usage:
4235c6c1daeSBarry Smith .vb
4245c6c1daeSBarry Smith       PetscInitialize(...);
425bb1d7374SBarry Smith       PetscLogDefaultBegin();
4265c6c1daeSBarry Smith        ... code ...
4275c6c1daeSBarry Smith       PetscLogView(viewer); or PetscLogDump();
4285c6c1daeSBarry Smith       PetscFinalize();
4295c6c1daeSBarry Smith .ve
4305c6c1daeSBarry Smith 
431d1f92df0SBarry Smith   Level: advanced
432d1f92df0SBarry Smith 
433811af0c4SBarry Smith   Note:
434811af0c4SBarry Smith   `PetscLogView()` or `PetscLogDump()` actually cause the printing of
4355c6c1daeSBarry Smith   the logging information.
4365c6c1daeSBarry Smith 
437d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogDump()`, `PetscLogAllBegin()`, `PetscLogView()`, `PetscLogTraceBegin()`
4385c6c1daeSBarry Smith @*/
439d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogDefaultBegin(void)
440d71ae5a4SJacob Faibussowitsch {
4415c6c1daeSBarry Smith   PetscFunctionBegin;
4429566063dSJacob Faibussowitsch   PetscCall(PetscLogSet(PetscLogEventBeginDefault, PetscLogEventEndDefault));
4433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4445c6c1daeSBarry Smith }
4455c6c1daeSBarry Smith 
4465c6c1daeSBarry Smith /*@C
4475c6c1daeSBarry Smith   PetscLogAllBegin - Turns on extensive logging of objects and events. Logs
4485c6c1daeSBarry Smith   all events. This creates large log files and slows the program down.
4495c6c1daeSBarry Smith 
450811af0c4SBarry Smith   Logically Collective on `PETSC_COMM_WORLD`
4515c6c1daeSBarry Smith 
452811af0c4SBarry Smith   Options Database Key:
453a2553e36SBarry Smith . -log_all - Prints extensive log information
4545c6c1daeSBarry Smith 
45510450e9eSJacob Faibussowitsch   Example Usage:
4565c6c1daeSBarry Smith .vb
4575c6c1daeSBarry Smith      PetscInitialize(...);
4585c6c1daeSBarry Smith      PetscLogAllBegin();
4595c6c1daeSBarry Smith      ... code ...
4605c6c1daeSBarry Smith      PetscLogDump(filename);
4615c6c1daeSBarry Smith      PetscFinalize();
4625c6c1daeSBarry Smith .ve
4635c6c1daeSBarry Smith 
464d1f92df0SBarry Smith   Level: advanced
465d1f92df0SBarry Smith 
466811af0c4SBarry Smith   Note:
467811af0c4SBarry Smith   A related routine is `PetscLogDefaultBegin()` (with the options key -log_view), which is
4685c6c1daeSBarry Smith   intended for production runs since it logs only flop rates and object
4695c6c1daeSBarry Smith   creation (and shouldn't significantly slow the programs).
4705c6c1daeSBarry Smith 
471d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogDump()`, `PetscLogDefaultBegin()`, `PetscLogTraceBegin()`
4725c6c1daeSBarry Smith @*/
473d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogAllBegin(void)
474d71ae5a4SJacob Faibussowitsch {
4755c6c1daeSBarry Smith   PetscFunctionBegin;
4769566063dSJacob Faibussowitsch   PetscCall(PetscLogSet(PetscLogEventBeginComplete, PetscLogEventEndComplete));
4773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4785c6c1daeSBarry Smith }
4795c6c1daeSBarry Smith 
480956f8c0dSBarry Smith /*@C
4815c6c1daeSBarry Smith   PetscLogTraceBegin - Activates trace logging.  Every time a PETSc event
4825c6c1daeSBarry Smith   begins or ends, the event name is printed.
4835c6c1daeSBarry Smith 
484811af0c4SBarry Smith   Logically Collective on `PETSC_COMM_WORLD`
4855c6c1daeSBarry Smith 
4865c6c1daeSBarry Smith   Input Parameter:
4875c6c1daeSBarry Smith . file - The file to print trace in (e.g. stdout)
4885c6c1daeSBarry Smith 
4895c6c1daeSBarry Smith   Options Database Key:
490811af0c4SBarry Smith . -log_trace [filename] - Activates `PetscLogTraceBegin()`
4915c6c1daeSBarry Smith 
492d1f92df0SBarry Smith   Level: intermediate
493d1f92df0SBarry Smith 
4945c6c1daeSBarry Smith   Notes:
495811af0c4SBarry Smith   `PetscLogTraceBegin()` prints the processor number, the execution time (sec),
4965c6c1daeSBarry Smith   then "Event begin:" or "Event end:" followed by the event name.
4975c6c1daeSBarry Smith 
498811af0c4SBarry Smith   `PetscLogTraceBegin()` allows tracing of all PETSc calls, which is useful
4995c6c1daeSBarry Smith   to determine where a program is hanging without running in the
5005c6c1daeSBarry Smith   debugger.  Can be used in conjunction with the -info option.
5015c6c1daeSBarry Smith 
502d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogDump()`, `PetscLogAllBegin()`, `PetscLogView()`, `PetscLogDefaultBegin()`
5035c6c1daeSBarry Smith @*/
504d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogTraceBegin(FILE *file)
505d71ae5a4SJacob Faibussowitsch {
5065c6c1daeSBarry Smith   PetscFunctionBegin;
5075c6c1daeSBarry Smith   petsc_tracefile = file;
508a297a907SKarl Rupp 
5099566063dSJacob Faibussowitsch   PetscCall(PetscLogSet(PetscLogEventBeginTrace, PetscLogEventEndTrace));
5103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5115c6c1daeSBarry Smith }
5125c6c1daeSBarry Smith 
51353e0a2f3SToby Isaac /*@C
51453e0a2f3SToby Isaac   PetscLogLegacyCallbacksBegin - Create and start a log handler from callbacks
51553e0a2f3SToby Isaac   matching the now deprecated function pointers `PetscLogPLB`, `PetscLogPLE`,
51653e0a2f3SToby Isaac   `PetscLogPHC`, `PetscLogPHD`.
51753e0a2f3SToby Isaac 
51853e0a2f3SToby Isaac   Logically Collective over `PETSC_COMM_WORLD`
51953e0a2f3SToby Isaac 
52053e0a2f3SToby Isaac   Input Parameters:
52153e0a2f3SToby Isaac + PetscLogPLB - A callback that will be executed by `PetscLogEventBegin()` (or `NULL`)
52253e0a2f3SToby Isaac . PetscLogPLE - A callback that will be executed by `PetscLogEventEnd()` (or `NULL`)
52353e0a2f3SToby Isaac . PetscLogPHC - A callback that will be executed by `PetscLogObjectCreate()` (or `NULL`)
52453e0a2f3SToby Isaac - PetscLogPHD - A callback that will be executed by `PetscLogObjectCreate()` (or `NULL`)
52553e0a2f3SToby Isaac 
52653e0a2f3SToby Isaac   Calling sequence of `PetscLogPLB`:
52753e0a2f3SToby Isaac + e  - a `PetscLogEvent` that is beginning
52853e0a2f3SToby Isaac . _i - deprecated, unused
52953e0a2f3SToby Isaac . o1 - a `PetscObject` associated with `e` (or `NULL`)
53053e0a2f3SToby Isaac . o2 - a `PetscObject` associated with `e` (or `NULL`)
53153e0a2f3SToby Isaac . o3 - a `PetscObject` associated with `e` (or `NULL`)
53253e0a2f3SToby Isaac - o4 - a `PetscObject` associated with `e` (or `NULL`)
53353e0a2f3SToby Isaac 
53453e0a2f3SToby Isaac   Calling sequence of `PetscLogPLE`:
53553e0a2f3SToby Isaac + e  - a `PetscLogEvent` that is beginning
53653e0a2f3SToby Isaac . _i - deprecated, unused
53753e0a2f3SToby Isaac . o1 - a `PetscObject` associated with `e` (or `NULL`)
53853e0a2f3SToby Isaac . o2 - a `PetscObject` associated with `e` (or `NULL`)
53953e0a2f3SToby Isaac . o3 - a `PetscObject` associated with `e` (or `NULL`)
54053e0a2f3SToby Isaac - o4 - a `PetscObject` associated with `e` (or `NULL`)
54153e0a2f3SToby Isaac 
54253e0a2f3SToby Isaac   Calling sequence of `PetscLogPHC`:
54353e0a2f3SToby Isaac . o - a `PetscObject` that has just been created
54453e0a2f3SToby Isaac 
54553e0a2f3SToby Isaac   Calling sequence of `PetscLogPHD`:
54653e0a2f3SToby Isaac . o - a `PetscObject` that is about to be destroyed
54753e0a2f3SToby Isaac 
54853e0a2f3SToby Isaac   Level: advanced
54953e0a2f3SToby Isaac 
55053e0a2f3SToby Isaac   Notes:
55153e0a2f3SToby Isaac   This is for transitioning from the deprecated function `PetscLogSet()` and should not be used in new code.
55253e0a2f3SToby Isaac 
55353e0a2f3SToby Isaac   This should help migrate external log handlers to use `PetscLogHandler`, but
55453e0a2f3SToby Isaac   callbacks that depend on the deprecated `PetscLogStage` datatype will have to be
55553e0a2f3SToby Isaac   updated.
55653e0a2f3SToby Isaac 
55753e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogHandlerStart()`, `PetscLogState`
55853e0a2f3SToby Isaac @*/
55953e0a2f3SToby Isaac PetscErrorCode PetscLogLegacyCallbacksBegin(PetscErrorCode (*PetscLogPLB)(PetscLogEvent e, int _i, PetscObject o1, PetscObject o2, PetscObject o3, PetscObject o4), PetscErrorCode (*PetscLogPLE)(PetscLogEvent e, int _i, PetscObject o1, PetscObject o2, PetscObject o3, PetscObject o4), PetscErrorCode (*PetscLogPHC)(PetscObject o), PetscErrorCode (*PetscLogPHD)(PetscObject o))
56053e0a2f3SToby Isaac {
56153e0a2f3SToby Isaac   PetscLogHandler handler;
56253e0a2f3SToby Isaac 
56353e0a2f3SToby Isaac   PetscFunctionBegin;
56453e0a2f3SToby Isaac   PetscCall(PetscLogHandlerCreateLegacy(PETSC_COMM_WORLD, PetscLogPLB, PetscLogPLE, PetscLogPHC, PetscLogPHD, &handler));
56553e0a2f3SToby Isaac   PetscCall(PetscLogHandlerStart(handler));
56653e0a2f3SToby Isaac   PetscCall(PetscLogHandlerDestroy(&handler));
56753e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
56853e0a2f3SToby Isaac }
56953e0a2f3SToby Isaac 
570*2611ad71SToby Isaac   #if defined(PETSC_HAVE_MPE)
571*2611ad71SToby Isaac     #include <mpe.h>
572*2611ad71SToby Isaac 
573*2611ad71SToby Isaac PETSC_INTERN PetscErrorCode PetscLogHandlerCreate_MPE(MPI_Comm, PetscLogHandler *);
574*2611ad71SToby Isaac 
575*2611ad71SToby Isaac static PetscBool PetscBeganMPE = PETSC_FALSE;
576*2611ad71SToby Isaac 
577*2611ad71SToby Isaac PETSC_INTERN PetscErrorCode PetscLogEventBeginMPE(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject);
578*2611ad71SToby Isaac PETSC_INTERN PetscErrorCode PetscLogEventEndMPE(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject);
579*2611ad71SToby Isaac   #endif
580*2611ad71SToby Isaac 
581*2611ad71SToby Isaac /*@C
582*2611ad71SToby Isaac   PetscLogMPEBegin - Turns on MPE logging of events. This creates large log files and slows the
583*2611ad71SToby Isaac   program down.
584*2611ad71SToby Isaac 
585*2611ad71SToby Isaac   Collective over `PETSC_COMM_WORLD`
586*2611ad71SToby Isaac 
587*2611ad71SToby Isaac   Options Database Key:
588*2611ad71SToby Isaac . -log_mpe - Prints extensive log information
589*2611ad71SToby Isaac 
590*2611ad71SToby Isaac   Level: advanced
591*2611ad71SToby Isaac 
592*2611ad71SToby Isaac   Note:
593*2611ad71SToby Isaac   A related routine is `PetscLogDefaultBegin()` (with the options key `-log_view`), which is
594*2611ad71SToby Isaac   intended for production runs since it logs only flop rates and object creation (and should
595*2611ad71SToby Isaac   not significantly slow the programs).
596*2611ad71SToby Isaac 
597*2611ad71SToby Isaac .seealso: [](ch_profiling), `PetscLogDump()`, `PetscLogDefaultBegin()`, `PetscLogAllBegin()`, `PetscLogEventActivate()`,
598*2611ad71SToby Isaac           `PetscLogEventDeactivate()`
599*2611ad71SToby Isaac @*/
600*2611ad71SToby Isaac PetscErrorCode PetscLogMPEBegin(void)
601*2611ad71SToby Isaac {
602*2611ad71SToby Isaac   PetscFunctionBegin;
603*2611ad71SToby Isaac   #if defined(PETSC_HAVE_MPE)
604*2611ad71SToby Isaac   /* Do MPE initialization */
605*2611ad71SToby Isaac   if (!MPE_Initialized_logging()) { /* This function exists in mpich 1.1.2 and higher */
606*2611ad71SToby Isaac     PetscCall(PetscInfo(0, "Initializing MPE.\n"));
607*2611ad71SToby Isaac     PetscCall(MPE_Init_log());
608*2611ad71SToby Isaac 
609*2611ad71SToby Isaac     PetscBeganMPE = PETSC_TRUE;
610*2611ad71SToby Isaac   } else {
611*2611ad71SToby Isaac     PetscCall(PetscInfo(0, "MPE already initialized. Not attempting to reinitialize.\n"));
612*2611ad71SToby Isaac   }
613*2611ad71SToby Isaac   PetscCall(PetscLogSet(PetscLogEventBeginMPE, PetscLogEventEndMPE));
614*2611ad71SToby Isaac   #else
615*2611ad71SToby Isaac   SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP_SYS, "PETSc was configured without MPE support, reconfigure with --with-mpe or --download-mpe");
616*2611ad71SToby Isaac   #endif
617*2611ad71SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
618*2611ad71SToby Isaac }
619*2611ad71SToby Isaac 
62053e0a2f3SToby Isaac   #if defined(PETSC_HAVE_TAU_PERFSTUBS)
62153e0a2f3SToby Isaac     #include <../src/sys/perfstubs/timer.h>
62253e0a2f3SToby Isaac   #endif
62353e0a2f3SToby Isaac 
62453e0a2f3SToby Isaac /*@C
62553e0a2f3SToby Isaac   PetscLogPerfstubsBegin - Turns on logging of events using the perfstubs interface.
62653e0a2f3SToby Isaac 
62753e0a2f3SToby Isaac   Collective over `PETSC_COMM_WORLD`
62853e0a2f3SToby Isaac 
62953e0a2f3SToby Isaac   Options Database Key:
63053e0a2f3SToby Isaac . -log_perfstubs - use an external log handler through the perfstubs interface
63153e0a2f3SToby Isaac 
63253e0a2f3SToby Isaac   Level: advanced
63353e0a2f3SToby Isaac 
63453e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogDefaultBegin()`, `PetscLogEventActivate()`
63553e0a2f3SToby Isaac @*/
63653e0a2f3SToby Isaac PetscErrorCode PetscLogPerfstubsBegin(void)
63753e0a2f3SToby Isaac {
63853e0a2f3SToby Isaac   PetscFunctionBegin;
63953e0a2f3SToby Isaac   #if defined(PETSC_HAVE_TAU_PERFSTUBS)
64053e0a2f3SToby Isaac   PetscCall(PetscLogTypeBegin(PETSC_LOG_HANDLER_PERFSTUBS));
64153e0a2f3SToby Isaac   #else
64253e0a2f3SToby Isaac   SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP_SYS, "PETSc was configured without perfstubs support, reconfigure with --with-tau-perfstubs");
64353e0a2f3SToby Isaac   #endif
64453e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
64553e0a2f3SToby Isaac }
64653e0a2f3SToby Isaac 
6475c6c1daeSBarry Smith /*@
6485c6c1daeSBarry Smith   PetscLogActions - Determines whether actions are logged for the graphical viewer.
6495c6c1daeSBarry Smith 
6505c6c1daeSBarry Smith   Not Collective
6515c6c1daeSBarry Smith 
6525c6c1daeSBarry Smith   Input Parameter:
653811af0c4SBarry Smith . flag - `PETSC_TRUE` if actions are to be logged
654811af0c4SBarry Smith 
655811af0c4SBarry Smith   Options Database Key:
656811af0c4SBarry Smith . -log_exclude_actions - Turns off actions logging
6575c6c1daeSBarry Smith 
6585c6c1daeSBarry Smith   Level: intermediate
6595c6c1daeSBarry Smith 
660811af0c4SBarry Smith   Note:
661811af0c4SBarry Smith   Logging of actions continues to consume more memory as the program
6625c6c1daeSBarry Smith   runs. Long running programs should consider turning this feature off.
663aec76313SJacob Faibussowitsch 
664d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStagePush()`, `PetscLogStagePop()`
6655c6c1daeSBarry Smith @*/
666d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogActions(PetscBool flag)
667d71ae5a4SJacob Faibussowitsch {
6685c6c1daeSBarry Smith   PetscFunctionBegin;
6695c6c1daeSBarry Smith   petsc_logActions = flag;
6703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6715c6c1daeSBarry Smith }
6725c6c1daeSBarry Smith 
6735c6c1daeSBarry Smith /*@
6745c6c1daeSBarry Smith   PetscLogObjects - Determines whether objects are logged for the graphical viewer.
6755c6c1daeSBarry Smith 
6765c6c1daeSBarry Smith   Not Collective
6775c6c1daeSBarry Smith 
6785c6c1daeSBarry Smith   Input Parameter:
679811af0c4SBarry Smith . flag - `PETSC_TRUE` if objects are to be logged
680811af0c4SBarry Smith 
681811af0c4SBarry Smith   Options Database Key:
682811af0c4SBarry Smith . -log_exclude_objects - Turns off objects logging
6835c6c1daeSBarry Smith 
6845c6c1daeSBarry Smith   Level: intermediate
6855c6c1daeSBarry Smith 
686811af0c4SBarry Smith   Note:
687811af0c4SBarry Smith   Logging of objects continues to consume more memory as the program
6885c6c1daeSBarry Smith   runs. Long running programs should consider turning this feature off.
6895c6c1daeSBarry Smith 
690d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStagePush()`, `PetscLogStagePop()`
6915c6c1daeSBarry Smith @*/
692d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogObjects(PetscBool flag)
693d71ae5a4SJacob Faibussowitsch {
6945c6c1daeSBarry Smith   PetscFunctionBegin;
6955c6c1daeSBarry Smith   petsc_logObjects = flag;
6963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6975c6c1daeSBarry Smith }
6985c6c1daeSBarry Smith 
6995c6c1daeSBarry Smith /*------------------------------------------------ Stage Functions --------------------------------------------------*/
7005c6c1daeSBarry Smith /*@C
70174c0405dSRichard Tran Mills   PetscLogStageRegister - Attaches a character string name to a logging stage.
7025c6c1daeSBarry Smith 
7035c6c1daeSBarry Smith   Not Collective
7045c6c1daeSBarry Smith 
7055c6c1daeSBarry Smith   Input Parameter:
7065c6c1daeSBarry Smith . sname - The name to associate with that stage
7075c6c1daeSBarry Smith 
7085c6c1daeSBarry Smith   Output Parameter:
7095c6c1daeSBarry Smith . stage - The stage number
7105c6c1daeSBarry Smith 
7115c6c1daeSBarry Smith   Level: intermediate
7125c6c1daeSBarry Smith 
713d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStagePush()`, `PetscLogStagePop()`
7145c6c1daeSBarry Smith @*/
715d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStageRegister(const char sname[], PetscLogStage *stage)
716d71ae5a4SJacob Faibussowitsch {
7175c6c1daeSBarry Smith   PetscStageLog stageLog;
7185c6c1daeSBarry Smith   PetscLogEvent event;
7195c6c1daeSBarry Smith 
7205c6c1daeSBarry Smith   PetscFunctionBegin;
7219566063dSJacob Faibussowitsch   PetscCall(PetscLogGetStageLog(&stageLog));
7229566063dSJacob Faibussowitsch   PetscCall(PetscStageLogRegister(stageLog, sname, stage));
7235c6c1daeSBarry Smith   /* Copy events already changed in the main stage, this sucks */
7249566063dSJacob Faibussowitsch   PetscCall(PetscEventPerfLogEnsureSize(stageLog->stageInfo[*stage].eventLog, stageLog->eventLog->numEvents));
72548a46eb9SPierre Jolivet   for (event = 0; event < stageLog->eventLog->numEvents; event++) PetscCall(PetscEventPerfInfoCopy(&stageLog->stageInfo[0].eventLog->eventInfo[event], &stageLog->stageInfo[*stage].eventLog->eventInfo[event]));
7269566063dSJacob Faibussowitsch   PetscCall(PetscClassPerfLogEnsureSize(stageLog->stageInfo[*stage].classLog, stageLog->classLog->numClasses));
727a0c7f9aaSSamuel Khuvis   #if defined(PETSC_HAVE_TAU_PERFSTUBS)
728a0c7f9aaSSamuel Khuvis   if (perfstubs_initialized == PERFSTUBS_SUCCESS) PetscStackCallExternalVoid("ps_timer_create_", stageLog->stageInfo[*stage].timer = ps_timer_create_(sname));
729a0c7f9aaSSamuel Khuvis   #endif
7303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7315c6c1daeSBarry Smith }
7325c6c1daeSBarry Smith 
7335c6c1daeSBarry Smith /*@C
734811af0c4SBarry Smith   PetscLogStagePush - This function pushes a stage on the logging stack. Events started and stopped until `PetscLogStagePop()` will be associated with the stage
7355c6c1daeSBarry Smith 
7365c6c1daeSBarry Smith   Not Collective
7375c6c1daeSBarry Smith 
7385c6c1daeSBarry Smith   Input Parameter:
7395c6c1daeSBarry Smith . stage - The stage on which to log
7405c6c1daeSBarry Smith 
74110450e9eSJacob Faibussowitsch   Example Usage:
742811af0c4SBarry Smith   If the option -log_view is used to run the program containing the
7435c6c1daeSBarry Smith   following code, then 2 sets of summary data will be printed during
7445c6c1daeSBarry Smith   PetscFinalize().
7455c6c1daeSBarry Smith .vb
7465c6c1daeSBarry Smith       PetscInitialize(int *argc,char ***args,0,0);
7475c6c1daeSBarry Smith       [stage 0 of code]
7485c6c1daeSBarry Smith       PetscLogStagePush(1);
7495c6c1daeSBarry Smith       [stage 1 of code]
7505c6c1daeSBarry Smith       PetscLogStagePop();
7515c6c1daeSBarry Smith       PetscBarrier(...);
7525c6c1daeSBarry Smith       [more stage 0 of code]
7535c6c1daeSBarry Smith       PetscFinalize();
7545c6c1daeSBarry Smith .ve
7555c6c1daeSBarry Smith 
756d1f92df0SBarry Smith   Level: intermediate
757d1f92df0SBarry Smith 
758811af0c4SBarry Smith   Note:
759811af0c4SBarry Smith   Use `PetscLogStageRegister()` to register a stage.
7605c6c1daeSBarry Smith 
761d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStagePop()`, `PetscLogStageRegister()`, `PetscBarrier()`
7625c6c1daeSBarry Smith @*/
763d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStagePush(PetscLogStage stage)
764d71ae5a4SJacob Faibussowitsch {
7655c6c1daeSBarry Smith   PetscStageLog stageLog;
7665c6c1daeSBarry Smith 
7675c6c1daeSBarry Smith   PetscFunctionBegin;
7689566063dSJacob Faibussowitsch   PetscCall(PetscLogGetStageLog(&stageLog));
7699566063dSJacob Faibussowitsch   PetscCall(PetscStageLogPush(stageLog, stage));
770a0c7f9aaSSamuel Khuvis   #if defined(PETSC_HAVE_TAU_PERFSTUBS)
771a0c7f9aaSSamuel Khuvis   if (perfstubs_initialized == PERFSTUBS_SUCCESS && stageLog->stageInfo[stage].timer != NULL) PetscStackCallExternalVoid("ps_timer_start_", ps_timer_start_(stageLog->stageInfo[stage].timer));
772a0c7f9aaSSamuel Khuvis   #endif
7733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7745c6c1daeSBarry Smith }
7755c6c1daeSBarry Smith 
7765c6c1daeSBarry Smith /*@C
777811af0c4SBarry Smith   PetscLogStagePop - This function pops a stage from the logging stack that was pushed with `PetscLogStagePush()`
7785c6c1daeSBarry Smith 
7795c6c1daeSBarry Smith   Not Collective
7805c6c1daeSBarry Smith 
78110450e9eSJacob Faibussowitsch   Example Usage:
782811af0c4SBarry Smith   If the option -log_view is used to run the program containing the
7835c6c1daeSBarry Smith   following code, then 2 sets of summary data will be printed during
7845c6c1daeSBarry Smith   PetscFinalize().
7855c6c1daeSBarry Smith .vb
7865c6c1daeSBarry Smith       PetscInitialize(int *argc,char ***args,0,0);
7875c6c1daeSBarry Smith       [stage 0 of code]
7885c6c1daeSBarry Smith       PetscLogStagePush(1);
7895c6c1daeSBarry Smith       [stage 1 of code]
7905c6c1daeSBarry Smith       PetscLogStagePop();
7915c6c1daeSBarry Smith       PetscBarrier(...);
7925c6c1daeSBarry Smith       [more stage 0 of code]
7935c6c1daeSBarry Smith       PetscFinalize();
7945c6c1daeSBarry Smith .ve
7955c6c1daeSBarry Smith 
7965c6c1daeSBarry Smith   Level: intermediate
7975c6c1daeSBarry Smith 
798d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStagePush()`, `PetscLogStageRegister()`, `PetscBarrier()`
7995c6c1daeSBarry Smith @*/
800d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStagePop(void)
801d71ae5a4SJacob Faibussowitsch {
8025c6c1daeSBarry Smith   PetscStageLog stageLog;
8035c6c1daeSBarry Smith 
8045c6c1daeSBarry Smith   PetscFunctionBegin;
8059566063dSJacob Faibussowitsch   PetscCall(PetscLogGetStageLog(&stageLog));
806a0c7f9aaSSamuel Khuvis   #if defined(PETSC_HAVE_TAU_PERFSTUBS)
807a0c7f9aaSSamuel Khuvis   if (perfstubs_initialized == PERFSTUBS_SUCCESS && stageLog->stageInfo[stageLog->curStage].timer != NULL) PetscStackCallExternalVoid("ps_timer_stop_", ps_timer_stop_(stageLog->stageInfo[stageLog->curStage].timer));
808a0c7f9aaSSamuel Khuvis   #endif
8099566063dSJacob Faibussowitsch   PetscCall(PetscStageLogPop(stageLog));
8103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8115c6c1daeSBarry Smith }
8125c6c1daeSBarry Smith 
8135c6c1daeSBarry Smith /*@
814811af0c4SBarry Smith   PetscLogStageSetActive - Sets if a stage is used for `PetscLogEventBegin()` and `PetscLogEventEnd()`.
8155c6c1daeSBarry Smith 
8165c6c1daeSBarry Smith   Not Collective
8175c6c1daeSBarry Smith 
8185c6c1daeSBarry Smith   Input Parameters:
8195c6c1daeSBarry Smith + stage    - The stage
820811af0c4SBarry Smith - isActive - The activity flag, `PETSC_TRUE` for logging, else `PETSC_FALSE` (defaults to `PETSC_TRUE`)
8215c6c1daeSBarry Smith 
8225c6c1daeSBarry Smith   Level: intermediate
8235c6c1daeSBarry Smith 
824811af0c4SBarry Smith   Note:
825811af0c4SBarry Smith   If this is set to `PETSC_FALSE` the logging acts as if the stage did not exist
826811af0c4SBarry Smith 
827d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()`
8285c6c1daeSBarry Smith @*/
829d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStageSetActive(PetscLogStage stage, PetscBool isActive)
830d71ae5a4SJacob Faibussowitsch {
8315c6c1daeSBarry Smith   PetscStageLog stageLog;
8325c6c1daeSBarry Smith 
8335c6c1daeSBarry Smith   PetscFunctionBegin;
8349566063dSJacob Faibussowitsch   PetscCall(PetscLogGetStageLog(&stageLog));
8359566063dSJacob Faibussowitsch   PetscCall(PetscStageLogSetActive(stageLog, stage, isActive));
8363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8375c6c1daeSBarry Smith }
8385c6c1daeSBarry Smith 
8395c6c1daeSBarry Smith /*@
840811af0c4SBarry Smith   PetscLogStageGetActive - Checks if a stage is used for `PetscLogEventBegin()` and `PetscLogEventEnd()`.
8415c6c1daeSBarry Smith 
8425c6c1daeSBarry Smith   Not Collective
8435c6c1daeSBarry Smith 
8445c6c1daeSBarry Smith   Input Parameter:
8455c6c1daeSBarry Smith . stage - The stage
8465c6c1daeSBarry Smith 
8475c6c1daeSBarry Smith   Output Parameter:
848811af0c4SBarry Smith . isActive - The activity flag, `PETSC_TRUE` for logging, else `PETSC_FALSE` (defaults to `PETSC_TRUE`)
8495c6c1daeSBarry Smith 
8505c6c1daeSBarry Smith   Level: intermediate
8515c6c1daeSBarry Smith 
852d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()`
8535c6c1daeSBarry Smith @*/
854d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStageGetActive(PetscLogStage stage, PetscBool *isActive)
855d71ae5a4SJacob Faibussowitsch {
8565c6c1daeSBarry Smith   PetscStageLog stageLog;
8575c6c1daeSBarry Smith 
8585c6c1daeSBarry Smith   PetscFunctionBegin;
8599566063dSJacob Faibussowitsch   PetscCall(PetscLogGetStageLog(&stageLog));
8609566063dSJacob Faibussowitsch   PetscCall(PetscStageLogGetActive(stageLog, stage, isActive));
8613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8625c6c1daeSBarry Smith }
8635c6c1daeSBarry Smith 
8645c6c1daeSBarry Smith /*@
865811af0c4SBarry Smith   PetscLogStageSetVisible - Determines stage visibility in `PetscLogView()`
8665c6c1daeSBarry Smith 
8675c6c1daeSBarry Smith   Not Collective
8685c6c1daeSBarry Smith 
8695c6c1daeSBarry Smith   Input Parameters:
8705c6c1daeSBarry Smith + stage     - The stage
871811af0c4SBarry Smith - isVisible - The visibility flag, `PETSC_TRUE` to print, else `PETSC_FALSE` (defaults to `PETSC_TRUE`)
8725c6c1daeSBarry Smith 
8735c6c1daeSBarry Smith   Level: intermediate
8745c6c1daeSBarry Smith 
875aec76313SJacob Faibussowitsch   Developer Notes:
876811af0c4SBarry Smith   What does visible mean, needs to be documented.
877811af0c4SBarry Smith 
878d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogView()`
8795c6c1daeSBarry Smith @*/
880d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStageSetVisible(PetscLogStage stage, PetscBool isVisible)
881d71ae5a4SJacob Faibussowitsch {
8825c6c1daeSBarry Smith   PetscStageLog stageLog;
8835c6c1daeSBarry Smith 
8845c6c1daeSBarry Smith   PetscFunctionBegin;
8859566063dSJacob Faibussowitsch   PetscCall(PetscLogGetStageLog(&stageLog));
8869566063dSJacob Faibussowitsch   PetscCall(PetscStageLogSetVisible(stageLog, stage, isVisible));
8873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8885c6c1daeSBarry Smith }
8895c6c1daeSBarry Smith 
8905c6c1daeSBarry Smith /*@
891811af0c4SBarry Smith   PetscLogStageGetVisible - Returns stage visibility in `PetscLogView()`
8925c6c1daeSBarry Smith 
8935c6c1daeSBarry Smith   Not Collective
8945c6c1daeSBarry Smith 
8955c6c1daeSBarry Smith   Input Parameter:
8965c6c1daeSBarry Smith . stage - The stage
8975c6c1daeSBarry Smith 
8985c6c1daeSBarry Smith   Output Parameter:
899811af0c4SBarry Smith . isVisible - The visibility flag, `PETSC_TRUE` to print, else `PETSC_FALSE` (defaults to `PETSC_TRUE`)
9005c6c1daeSBarry Smith 
9015c6c1daeSBarry Smith   Level: intermediate
9025c6c1daeSBarry Smith 
903d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogView()`
9045c6c1daeSBarry Smith @*/
905d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStageGetVisible(PetscLogStage stage, PetscBool *isVisible)
906d71ae5a4SJacob Faibussowitsch {
9075c6c1daeSBarry Smith   PetscStageLog stageLog;
9085c6c1daeSBarry Smith 
9095c6c1daeSBarry Smith   PetscFunctionBegin;
9109566063dSJacob Faibussowitsch   PetscCall(PetscLogGetStageLog(&stageLog));
9119566063dSJacob Faibussowitsch   PetscCall(PetscStageLogGetVisible(stageLog, stage, isVisible));
9123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9135c6c1daeSBarry Smith }
9145c6c1daeSBarry Smith 
9155c6c1daeSBarry Smith /*@C
9165c6c1daeSBarry Smith   PetscLogStageGetId - Returns the stage id when given the stage name.
9175c6c1daeSBarry Smith 
9185c6c1daeSBarry Smith   Not Collective
9195c6c1daeSBarry Smith 
9205c6c1daeSBarry Smith   Input Parameter:
9215c6c1daeSBarry Smith . name - The stage name
9225c6c1daeSBarry Smith 
9235c6c1daeSBarry Smith   Output Parameter:
9245a4a3fabSBarry Smith . stage - The stage, , or -1 if no stage with that name exists
9255c6c1daeSBarry Smith 
9265c6c1daeSBarry Smith   Level: intermediate
9275c6c1daeSBarry Smith 
928d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()`
9295c6c1daeSBarry Smith @*/
930d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStageGetId(const char name[], PetscLogStage *stage)
931d71ae5a4SJacob Faibussowitsch {
9325c6c1daeSBarry Smith   PetscStageLog stageLog;
9335c6c1daeSBarry Smith 
9345c6c1daeSBarry Smith   PetscFunctionBegin;
9359566063dSJacob Faibussowitsch   PetscCall(PetscLogGetStageLog(&stageLog));
9369566063dSJacob Faibussowitsch   PetscCall(PetscStageLogGetStage(stageLog, name, stage));
9373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9385c6c1daeSBarry Smith }
9395c6c1daeSBarry Smith 
94053e0a2f3SToby Isaac /*@C
94153e0a2f3SToby Isaac   PetscLogStageGetName - Returns the stage name when given the stage id.
94253e0a2f3SToby Isaac 
94353e0a2f3SToby Isaac   Not Collective
94453e0a2f3SToby Isaac 
94553e0a2f3SToby Isaac   Input Parameter:
94653e0a2f3SToby Isaac . stage - The stage
94753e0a2f3SToby Isaac 
94853e0a2f3SToby Isaac   Output Parameter:
94953e0a2f3SToby Isaac . name - The stage name
95053e0a2f3SToby Isaac 
95153e0a2f3SToby Isaac   Level: intermediate
95253e0a2f3SToby Isaac 
95353e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()`
95453e0a2f3SToby Isaac @*/
95553e0a2f3SToby Isaac PetscErrorCode PetscLogStageGetName(PetscLogStage stage, const char **name)
95653e0a2f3SToby Isaac {
95753e0a2f3SToby Isaac   PetscLogStageInfo stage_info;
95853e0a2f3SToby Isaac   PetscLogState     state;
95953e0a2f3SToby Isaac 
96053e0a2f3SToby Isaac   PetscFunctionBegin;
96153e0a2f3SToby Isaac   PetscCall(PetscLogGetState(&state));
96253e0a2f3SToby Isaac   PetscCall(PetscLogStateStageGetInfo(state, stage, &stage_info));
96353e0a2f3SToby Isaac   *name = stage_info.name;
96453e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
96553e0a2f3SToby Isaac }
96653e0a2f3SToby Isaac 
9675c6c1daeSBarry Smith /*------------------------------------------------ Event Functions --------------------------------------------------*/
9687a101e5eSJacob Faibussowitsch 
9695c6c1daeSBarry Smith /*@C
970811af0c4SBarry Smith   PetscLogEventRegister - Registers an event name for logging operations
9715c6c1daeSBarry Smith 
9725c6c1daeSBarry Smith   Not Collective
9735c6c1daeSBarry Smith 
974d8d19677SJose E. Roman   Input Parameters:
9755c6c1daeSBarry Smith + name    - The name associated with the event
9765c6c1daeSBarry Smith - classid - The classid associated to the class for this event, obtain either with
977811af0c4SBarry Smith            `PetscClassIdRegister()` or use a predefined one such as `KSP_CLASSID`, `SNES_CLASSID`, the predefined ones
9785c6c1daeSBarry Smith            are only available in C code
9795c6c1daeSBarry Smith 
9805c6c1daeSBarry Smith   Output Parameter:
981811af0c4SBarry Smith . event - The event id for use with `PetscLogEventBegin()` and `PetscLogEventEnd()`.
9825c6c1daeSBarry Smith 
98310450e9eSJacob Faibussowitsch   Example Usage:
9845c6c1daeSBarry Smith .vb
9855c6c1daeSBarry Smith       PetscLogEvent USER_EVENT;
9865c6c1daeSBarry Smith       PetscClassId classid;
9875c6c1daeSBarry Smith       PetscLogDouble user_event_flops;
9885c6c1daeSBarry Smith       PetscClassIdRegister("class name",&classid);
9895c6c1daeSBarry Smith       PetscLogEventRegister("User event name",classid,&USER_EVENT);
9905c6c1daeSBarry Smith       PetscLogEventBegin(USER_EVENT,0,0,0,0);
9915c6c1daeSBarry Smith          [code segment to monitor]
9925c6c1daeSBarry Smith          PetscLogFlops(user_event_flops);
9935c6c1daeSBarry Smith       PetscLogEventEnd(USER_EVENT,0,0,0,0);
9945c6c1daeSBarry Smith .ve
9955c6c1daeSBarry Smith 
996d1f92df0SBarry Smith   Level: intermediate
997d1f92df0SBarry Smith 
9985c6c1daeSBarry Smith   Notes:
9995c6c1daeSBarry Smith   PETSc automatically logs library events if the code has been
1000a2553e36SBarry Smith   configured with --with-log (which is the default) and
1001811af0c4SBarry Smith   -log_view or -log_all is specified.  `PetscLogEventRegister()` is
10025c6c1daeSBarry Smith   intended for logging user events to supplement this PETSc
10035c6c1daeSBarry Smith   information.
10045c6c1daeSBarry Smith 
1005495fc317SBarry Smith   PETSc can gather data for use with the utilities Jumpshot
10065c6c1daeSBarry Smith   (part of the MPICH distribution).  If PETSc has been compiled
10075c6c1daeSBarry Smith   with flag -DPETSC_HAVE_MPE (MPE is an additional utility within
10085c6c1daeSBarry Smith   MPICH), the user can employ another command line option, -log_mpe,
10095c6c1daeSBarry Smith   to create a logfile, "mpe.log", which can be visualized
1010495fc317SBarry Smith   Jumpshot.
10115c6c1daeSBarry Smith 
10125c6c1daeSBarry Smith   The classid is associated with each event so that classes of events
10135c6c1daeSBarry Smith   can be disabled simultaneously, such as all matrix events. The user
1014811af0c4SBarry Smith   can either use an existing classid, such as `MAT_CLASSID`, or create
10155c6c1daeSBarry Smith   their own as shown in the example.
10165c6c1daeSBarry Smith 
1017c5deb1d5SJed Brown   If an existing event with the same name exists, its event handle is
1018c5deb1d5SJed Brown   returned instead of creating a new event.
1019c5deb1d5SJed Brown 
1020d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStageRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogFlops()`,
1021db781477SPatrick Sanan           `PetscLogEventActivate()`, `PetscLogEventDeactivate()`, `PetscClassIdRegister()`
10225c6c1daeSBarry Smith @*/
1023d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventRegister(const char name[], PetscClassId classid, PetscLogEvent *event)
1024d71ae5a4SJacob Faibussowitsch {
10255c6c1daeSBarry Smith   PetscStageLog stageLog;
10265c6c1daeSBarry Smith   int           stage;
10275c6c1daeSBarry Smith 
10285c6c1daeSBarry Smith   PetscFunctionBegin;
10295c6c1daeSBarry Smith   *event = PETSC_DECIDE;
10309566063dSJacob Faibussowitsch   PetscCall(PetscLogGetStageLog(&stageLog));
10319566063dSJacob Faibussowitsch   PetscCall(PetscEventRegLogGetEvent(stageLog->eventLog, name, event));
10323ba16761SJacob Faibussowitsch   if (*event > 0) PetscFunctionReturn(PETSC_SUCCESS);
10339566063dSJacob Faibussowitsch   PetscCall(PetscEventRegLogRegister(stageLog->eventLog, name, classid, event));
10345c6c1daeSBarry Smith   for (stage = 0; stage < stageLog->numStages; stage++) {
10359566063dSJacob Faibussowitsch     PetscCall(PetscEventPerfLogEnsureSize(stageLog->stageInfo[stage].eventLog, stageLog->eventLog->numEvents));
10369566063dSJacob Faibussowitsch     PetscCall(PetscClassPerfLogEnsureSize(stageLog->stageInfo[stage].classLog, stageLog->classLog->numClasses));
10375c6c1daeSBarry Smith   }
10383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
10395c6c1daeSBarry Smith }
10405c6c1daeSBarry Smith 
10415c6c1daeSBarry Smith /*@
1042217044c2SLisandro Dalcin   PetscLogEventSetCollective - Indicates that a particular event is collective.
1043217044c2SLisandro Dalcin 
1044217044c2SLisandro Dalcin   Not Collective
1045217044c2SLisandro Dalcin 
1046d8d19677SJose E. Roman   Input Parameters:
1047217044c2SLisandro Dalcin + event      - The event id
1048d5b43468SJose E. Roman - collective - Boolean flag indicating whether a particular event is collective
1049217044c2SLisandro Dalcin 
1050d1f92df0SBarry Smith   Level: developer
1051d1f92df0SBarry Smith 
1052811af0c4SBarry Smith   Notes:
1053811af0c4SBarry Smith   New events returned from `PetscLogEventRegister()` are collective by default.
1054811af0c4SBarry Smith 
1055811af0c4SBarry Smith   Collective events are handled specially if the -log_sync is used. In that case the logging saves information about
1056811af0c4SBarry Smith   two parts of the event; the time for all the MPI ranks to synchronize and then the time for the actual computation/communication
1057811af0c4SBarry Smith   to be performed. This option is useful to debug imbalance within the computations or communications
1058217044c2SLisandro Dalcin 
1059d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogEventRegister()`
1060217044c2SLisandro Dalcin @*/
1061d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventSetCollective(PetscLogEvent event, PetscBool collective)
1062d71ae5a4SJacob Faibussowitsch {
1063217044c2SLisandro Dalcin   PetscStageLog    stageLog;
1064217044c2SLisandro Dalcin   PetscEventRegLog eventRegLog;
1065217044c2SLisandro Dalcin 
1066217044c2SLisandro Dalcin   PetscFunctionBegin;
10679566063dSJacob Faibussowitsch   PetscCall(PetscLogGetStageLog(&stageLog));
10689566063dSJacob Faibussowitsch   PetscCall(PetscStageLogGetEventRegLog(stageLog, &eventRegLog));
1069cc73adaaSBarry Smith   PetscCheck(event >= 0 && event <= eventRegLog->numEvents, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Invalid event id");
1070217044c2SLisandro Dalcin   eventRegLog->eventInfo[event].collective = collective;
10713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1072217044c2SLisandro Dalcin }
1073217044c2SLisandro Dalcin 
1074217044c2SLisandro Dalcin /*@
1075fa2bb9feSLisandro Dalcin   PetscLogEventIncludeClass - Activates event logging for a PETSc object class in every stage.
1076fa2bb9feSLisandro Dalcin 
1077fa2bb9feSLisandro Dalcin   Not Collective
1078fa2bb9feSLisandro Dalcin 
1079fa2bb9feSLisandro Dalcin   Input Parameter:
1080811af0c4SBarry Smith . classid - The object class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc.
1081fa2bb9feSLisandro Dalcin 
1082fa2bb9feSLisandro Dalcin   Level: developer
1083fa2bb9feSLisandro Dalcin 
1084d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventActivateClass()`, `PetscLogEventDeactivateClass()`, `PetscLogEventActivate()`, `PetscLogEventDeactivate()`
1085fa2bb9feSLisandro Dalcin @*/
1086d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventIncludeClass(PetscClassId classid)
1087d71ae5a4SJacob Faibussowitsch {
1088fa2bb9feSLisandro Dalcin   PetscStageLog stageLog;
1089fa2bb9feSLisandro Dalcin   int           stage;
1090fa2bb9feSLisandro Dalcin 
1091fa2bb9feSLisandro Dalcin   PetscFunctionBegin;
10929566063dSJacob Faibussowitsch   PetscCall(PetscLogGetStageLog(&stageLog));
109348a46eb9SPierre Jolivet   for (stage = 0; stage < stageLog->numStages; stage++) PetscCall(PetscEventPerfLogActivateClass(stageLog->stageInfo[stage].eventLog, stageLog->eventLog, classid));
10943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1095fa2bb9feSLisandro Dalcin }
1096fa2bb9feSLisandro Dalcin 
1097fa2bb9feSLisandro Dalcin /*@
1098fa2bb9feSLisandro Dalcin   PetscLogEventExcludeClass - Deactivates event logging for a PETSc object class in every stage.
1099fa2bb9feSLisandro Dalcin 
1100fa2bb9feSLisandro Dalcin   Not Collective
1101fa2bb9feSLisandro Dalcin 
1102fa2bb9feSLisandro Dalcin   Input Parameter:
1103811af0c4SBarry Smith . classid - The object class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc.
1104fa2bb9feSLisandro Dalcin 
1105fa2bb9feSLisandro Dalcin   Level: developer
1106fa2bb9feSLisandro Dalcin 
1107811af0c4SBarry Smith   Note:
1108811af0c4SBarry Smith   If a class is excluded then events associated with that class are not logged.
1109811af0c4SBarry Smith 
1110d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventDeactivateClass()`, `PetscLogEventActivateClass()`, `PetscLogEventDeactivate()`, `PetscLogEventActivate()`
1111fa2bb9feSLisandro Dalcin @*/
1112d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventExcludeClass(PetscClassId classid)
1113d71ae5a4SJacob Faibussowitsch {
1114fa2bb9feSLisandro Dalcin   PetscStageLog stageLog;
1115fa2bb9feSLisandro Dalcin   int           stage;
1116fa2bb9feSLisandro Dalcin 
1117fa2bb9feSLisandro Dalcin   PetscFunctionBegin;
11189566063dSJacob Faibussowitsch   PetscCall(PetscLogGetStageLog(&stageLog));
111948a46eb9SPierre Jolivet   for (stage = 0; stage < stageLog->numStages; stage++) PetscCall(PetscEventPerfLogDeactivateClass(stageLog->stageInfo[stage].eventLog, stageLog->eventLog, classid));
11203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1121fa2bb9feSLisandro Dalcin }
1122fa2bb9feSLisandro Dalcin 
1123fa2bb9feSLisandro Dalcin /*@
11245c6c1daeSBarry Smith   PetscLogEventActivate - Indicates that a particular event should be logged.
11255c6c1daeSBarry Smith 
11265c6c1daeSBarry Smith   Not Collective
11275c6c1daeSBarry Smith 
11285c6c1daeSBarry Smith   Input Parameter:
11295c6c1daeSBarry Smith . event - The event id
11305c6c1daeSBarry Smith 
113110450e9eSJacob Faibussowitsch   Example Usage:
11325c6c1daeSBarry Smith .vb
11335c6c1daeSBarry Smith       PetscLogEventDeactivate(VEC_SetValues);
11345c6c1daeSBarry Smith         [code where you do not want to log VecSetValues()]
11355c6c1daeSBarry Smith       PetscLogEventActivate(VEC_SetValues);
11365c6c1daeSBarry Smith         [code where you do want to log VecSetValues()]
11375c6c1daeSBarry Smith .ve
11385c6c1daeSBarry Smith 
1139d1f92df0SBarry Smith   Level: advanced
1140d1f92df0SBarry Smith 
11415c6c1daeSBarry Smith   Note:
11425c6c1daeSBarry Smith   The event may be either a pre-defined PETSc event (found in include/petsclog.h)
1143811af0c4SBarry Smith   or an event number obtained with `PetscLogEventRegister()`.
11445c6c1daeSBarry Smith 
1145d1f92df0SBarry Smith .seealso: [](ch_profiling), `PlogEventDeactivate()`, `PlogEventDeactivatePush()`, `PetscLogEventDeactivatePop()`
11465c6c1daeSBarry Smith @*/
1147d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventActivate(PetscLogEvent event)
1148d71ae5a4SJacob Faibussowitsch {
11495c6c1daeSBarry Smith   PetscStageLog stageLog;
11505c6c1daeSBarry Smith   int           stage;
11515c6c1daeSBarry Smith 
11525c6c1daeSBarry Smith   PetscFunctionBegin;
11539566063dSJacob Faibussowitsch   PetscCall(PetscLogGetStageLog(&stageLog));
11549566063dSJacob Faibussowitsch   PetscCall(PetscStageLogGetCurrent(stageLog, &stage));
11559566063dSJacob Faibussowitsch   PetscCall(PetscEventPerfLogActivate(stageLog->stageInfo[stage].eventLog, event));
11563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11575c6c1daeSBarry Smith }
11585c6c1daeSBarry Smith 
11595c6c1daeSBarry Smith /*@
11605c6c1daeSBarry Smith   PetscLogEventDeactivate - Indicates that a particular event should not be logged.
11615c6c1daeSBarry Smith 
11625c6c1daeSBarry Smith   Not Collective
11635c6c1daeSBarry Smith 
11645c6c1daeSBarry Smith   Input Parameter:
11655c6c1daeSBarry Smith . event - The event id
11665c6c1daeSBarry Smith 
116710450e9eSJacob Faibussowitsch   Example Usage:
11685c6c1daeSBarry Smith .vb
11695c6c1daeSBarry Smith       PetscLogEventDeactivate(VEC_SetValues);
11705c6c1daeSBarry Smith         [code where you do not want to log VecSetValues()]
11715c6c1daeSBarry Smith       PetscLogEventActivate(VEC_SetValues);
11725c6c1daeSBarry Smith         [code where you do want to log VecSetValues()]
11735c6c1daeSBarry Smith .ve
11745c6c1daeSBarry Smith 
1175d1f92df0SBarry Smith   Level: advanced
1176d1f92df0SBarry Smith 
11775c6c1daeSBarry Smith   Note:
11785c6c1daeSBarry Smith   The event may be either a pre-defined PETSc event (found in
1179811af0c4SBarry Smith   include/petsclog.h) or an event number obtained with `PetscLogEventRegister()`).
11805c6c1daeSBarry Smith 
1181d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventActivate()`, `PetscLogEventDeactivatePush()`, `PetscLogEventDeactivatePop()`
11825c6c1daeSBarry Smith @*/
1183d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventDeactivate(PetscLogEvent event)
1184d71ae5a4SJacob Faibussowitsch {
11855c6c1daeSBarry Smith   PetscStageLog stageLog;
11865c6c1daeSBarry Smith   int           stage;
11875c6c1daeSBarry Smith 
11885c6c1daeSBarry Smith   PetscFunctionBegin;
11899566063dSJacob Faibussowitsch   PetscCall(PetscLogGetStageLog(&stageLog));
11909566063dSJacob Faibussowitsch   PetscCall(PetscStageLogGetCurrent(stageLog, &stage));
11919566063dSJacob Faibussowitsch   PetscCall(PetscEventPerfLogDeactivate(stageLog->stageInfo[stage].eventLog, event));
11923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
11935c6c1daeSBarry Smith }
11945c6c1daeSBarry Smith 
11955c6c1daeSBarry Smith /*@
1196811af0c4SBarry Smith   PetscLogEventDeactivatePush - Indicates that a particular event should not be logged until `PetscLogEventDeactivatePop()` is called
1197c00cb57fSBarry Smith 
1198c00cb57fSBarry Smith   Not Collective
1199c00cb57fSBarry Smith 
1200c00cb57fSBarry Smith   Input Parameter:
1201c00cb57fSBarry Smith . event - The event id
1202c00cb57fSBarry Smith 
120310450e9eSJacob Faibussowitsch   Example Usage:
1204c00cb57fSBarry Smith .vb
1205c00cb57fSBarry Smith       PetscLogEventDeactivatePush(VEC_SetValues);
1206c00cb57fSBarry Smith         [code where you do not want to log VecSetValues()]
1207c00cb57fSBarry Smith       PetscLogEventDeactivatePop(VEC_SetValues);
1208c00cb57fSBarry Smith         [code where you do want to log VecSetValues()]
1209c00cb57fSBarry Smith .ve
1210c00cb57fSBarry Smith 
1211d1f92df0SBarry Smith   Level: advanced
1212d1f92df0SBarry Smith 
1213c00cb57fSBarry Smith   Note:
1214c00cb57fSBarry Smith   The event may be either a pre-defined PETSc event (found in
1215811af0c4SBarry Smith   include/petsclog.h) or an event number obtained with `PetscLogEventRegister()`).
1216c00cb57fSBarry Smith 
1217d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventActivate()`, `PetscLogEventDeactivatePop()`, `PetscLogEventDeactivate()`
1218c00cb57fSBarry Smith @*/
1219d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventDeactivatePush(PetscLogEvent event)
1220d71ae5a4SJacob Faibussowitsch {
1221c00cb57fSBarry Smith   PetscStageLog stageLog;
1222c00cb57fSBarry Smith   int           stage;
1223c00cb57fSBarry Smith 
1224c00cb57fSBarry Smith   PetscFunctionBegin;
12259566063dSJacob Faibussowitsch   PetscCall(PetscLogGetStageLog(&stageLog));
12269566063dSJacob Faibussowitsch   PetscCall(PetscStageLogGetCurrent(stageLog, &stage));
12279566063dSJacob Faibussowitsch   PetscCall(PetscEventPerfLogDeactivatePush(stageLog->stageInfo[stage].eventLog, event));
12283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1229c00cb57fSBarry Smith }
1230c00cb57fSBarry Smith 
1231c00cb57fSBarry Smith /*@
1232811af0c4SBarry Smith   PetscLogEventDeactivatePop - Indicates that a particular event should again be logged after the logging was turned off with `PetscLogEventDeactivatePush()`
1233c00cb57fSBarry Smith 
1234c00cb57fSBarry Smith   Not Collective
1235c00cb57fSBarry Smith 
1236c00cb57fSBarry Smith   Input Parameter:
1237c00cb57fSBarry Smith . event - The event id
1238c00cb57fSBarry Smith 
123910450e9eSJacob Faibussowitsch   Example Usage:
1240c00cb57fSBarry Smith .vb
1241c00cb57fSBarry Smith       PetscLogEventDeactivatePush(VEC_SetValues);
1242c00cb57fSBarry Smith         [code where you do not want to log VecSetValues()]
1243c00cb57fSBarry Smith       PetscLogEventDeactivatePop(VEC_SetValues);
1244c00cb57fSBarry Smith         [code where you do want to log VecSetValues()]
1245c00cb57fSBarry Smith .ve
1246c00cb57fSBarry Smith 
1247d1f92df0SBarry Smith   Level: advanced
1248d1f92df0SBarry Smith 
1249c00cb57fSBarry Smith   Note:
1250c00cb57fSBarry Smith   The event may be either a pre-defined PETSc event (found in
1251811af0c4SBarry Smith   include/petsclog.h) or an event number obtained with `PetscLogEventRegister()`).
1252c00cb57fSBarry Smith 
1253d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventActivate()`, `PetscLogEventDeactivatePush()`
1254c00cb57fSBarry Smith @*/
1255d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventDeactivatePop(PetscLogEvent event)
1256d71ae5a4SJacob Faibussowitsch {
1257c00cb57fSBarry Smith   PetscStageLog stageLog;
1258c00cb57fSBarry Smith   int           stage;
1259c00cb57fSBarry Smith 
1260c00cb57fSBarry Smith   PetscFunctionBegin;
12619566063dSJacob Faibussowitsch   PetscCall(PetscLogGetStageLog(&stageLog));
12629566063dSJacob Faibussowitsch   PetscCall(PetscStageLogGetCurrent(stageLog, &stage));
12639566063dSJacob Faibussowitsch   PetscCall(PetscEventPerfLogDeactivatePop(stageLog->stageInfo[stage].eventLog, event));
12643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1265c00cb57fSBarry Smith }
1266c00cb57fSBarry Smith 
1267c00cb57fSBarry Smith /*@
1268811af0c4SBarry Smith   PetscLogEventSetActiveAll - Turns on logging of all events
12695c6c1daeSBarry Smith 
12705c6c1daeSBarry Smith   Not Collective
12715c6c1daeSBarry Smith 
12725c6c1daeSBarry Smith   Input Parameters:
12735c6c1daeSBarry Smith + event    - The event id
12745c6c1daeSBarry Smith - isActive - The activity flag determining whether the event is logged
12755c6c1daeSBarry Smith 
12765c6c1daeSBarry Smith   Level: advanced
12775c6c1daeSBarry Smith 
1278d1f92df0SBarry Smith .seealso: [](ch_profiling), `PlogEventActivate()`, `PlogEventDeactivate()`
12795c6c1daeSBarry Smith @*/
1280d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventSetActiveAll(PetscLogEvent event, PetscBool isActive)
1281d71ae5a4SJacob Faibussowitsch {
12825c6c1daeSBarry Smith   PetscStageLog stageLog;
12835c6c1daeSBarry Smith   int           stage;
12845c6c1daeSBarry Smith 
12855c6c1daeSBarry Smith   PetscFunctionBegin;
12869566063dSJacob Faibussowitsch   PetscCall(PetscLogGetStageLog(&stageLog));
12875c6c1daeSBarry Smith   for (stage = 0; stage < stageLog->numStages; stage++) {
12885c6c1daeSBarry Smith     if (isActive) {
12899566063dSJacob Faibussowitsch       PetscCall(PetscEventPerfLogActivate(stageLog->stageInfo[stage].eventLog, event));
12905c6c1daeSBarry Smith     } else {
12919566063dSJacob Faibussowitsch       PetscCall(PetscEventPerfLogDeactivate(stageLog->stageInfo[stage].eventLog, event));
12925c6c1daeSBarry Smith     }
12935c6c1daeSBarry Smith   }
12943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12955c6c1daeSBarry Smith }
12965c6c1daeSBarry Smith 
12975c6c1daeSBarry Smith /*@
1298811af0c4SBarry Smith   PetscLogEventActivateClass - Activates event logging for a PETSc object class for the current stage
12995c6c1daeSBarry Smith 
13005c6c1daeSBarry Smith   Not Collective
13015c6c1daeSBarry Smith 
13025c6c1daeSBarry Smith   Input Parameter:
1303811af0c4SBarry Smith . classid - The event class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc.
13045c6c1daeSBarry Smith 
13055c6c1daeSBarry Smith   Level: developer
13065c6c1daeSBarry Smith 
1307d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventIncludeClass()`, `PetscLogEventExcludeClass()`, `PetscLogEventDeactivateClass()`, `PetscLogEventActivate()`, `PetscLogEventDeactivate()`
13085c6c1daeSBarry Smith @*/
1309d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventActivateClass(PetscClassId classid)
1310d71ae5a4SJacob Faibussowitsch {
13115c6c1daeSBarry Smith   PetscStageLog stageLog;
13125c6c1daeSBarry Smith   int           stage;
13135c6c1daeSBarry Smith 
13145c6c1daeSBarry Smith   PetscFunctionBegin;
13159566063dSJacob Faibussowitsch   PetscCall(PetscLogGetStageLog(&stageLog));
13169566063dSJacob Faibussowitsch   PetscCall(PetscStageLogGetCurrent(stageLog, &stage));
13179566063dSJacob Faibussowitsch   PetscCall(PetscEventPerfLogActivateClass(stageLog->stageInfo[stage].eventLog, stageLog->eventLog, classid));
13183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13195c6c1daeSBarry Smith }
13205c6c1daeSBarry Smith 
13215c6c1daeSBarry Smith /*@
1322811af0c4SBarry Smith   PetscLogEventDeactivateClass - Deactivates event logging for a PETSc object class for the current stage
13235c6c1daeSBarry Smith 
13245c6c1daeSBarry Smith   Not Collective
13255c6c1daeSBarry Smith 
13265c6c1daeSBarry Smith   Input Parameter:
1327811af0c4SBarry Smith . classid - The event class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc.
13285c6c1daeSBarry Smith 
13295c6c1daeSBarry Smith   Level: developer
13305c6c1daeSBarry Smith 
1331d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventIncludeClass()`, `PetscLogEventExcludeClass()`, `PetscLogEventActivateClass()`, `PetscLogEventActivate()`, `PetscLogEventDeactivate()`
13325c6c1daeSBarry Smith @*/
1333d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventDeactivateClass(PetscClassId classid)
1334d71ae5a4SJacob Faibussowitsch {
13355c6c1daeSBarry Smith   PetscStageLog stageLog;
13365c6c1daeSBarry Smith   int           stage;
13375c6c1daeSBarry Smith 
13385c6c1daeSBarry Smith   PetscFunctionBegin;
13399566063dSJacob Faibussowitsch   PetscCall(PetscLogGetStageLog(&stageLog));
13409566063dSJacob Faibussowitsch   PetscCall(PetscStageLogGetCurrent(stageLog, &stage));
13419566063dSJacob Faibussowitsch   PetscCall(PetscEventPerfLogDeactivateClass(stageLog->stageInfo[stage].eventLog, stageLog->eventLog, classid));
13423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13435c6c1daeSBarry Smith }
13445c6c1daeSBarry Smith 
13455c6c1daeSBarry Smith /*MC
134662872c28SLisandro Dalcin   PetscLogEventSync - Synchronizes the beginning of a user event.
134762872c28SLisandro Dalcin 
134862872c28SLisandro Dalcin   Synopsis:
134962872c28SLisandro Dalcin   #include <petsclog.h>
135062872c28SLisandro Dalcin   PetscErrorCode PetscLogEventSync(int e, MPI_Comm comm)
135162872c28SLisandro Dalcin 
135262872c28SLisandro Dalcin   Collective
135362872c28SLisandro Dalcin 
135462872c28SLisandro Dalcin   Input Parameters:
135562872c28SLisandro Dalcin + e    - integer associated with the event obtained from PetscLogEventRegister()
135662872c28SLisandro Dalcin - comm - an MPI communicator
135762872c28SLisandro Dalcin 
135810450e9eSJacob Faibussowitsch   Example Usage:
135962872c28SLisandro Dalcin .vb
136062872c28SLisandro Dalcin   PetscLogEvent USER_EVENT;
136110450e9eSJacob Faibussowitsch 
136262872c28SLisandro Dalcin   PetscLogEventRegister("User event", 0, &USER_EVENT);
136362872c28SLisandro Dalcin   PetscLogEventSync(USER_EVENT, PETSC_COMM_WORLD);
136462872c28SLisandro Dalcin   PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0);
136562872c28SLisandro Dalcin   [code segment to monitor]
136662872c28SLisandro Dalcin   PetscLogEventEnd(USER_EVENT, 0, 0, 0 , 0);
136762872c28SLisandro Dalcin .ve
136862872c28SLisandro Dalcin 
1369d1f92df0SBarry Smith   Level: developer
1370d1f92df0SBarry Smith 
1371811af0c4SBarry Smith   Note:
137210450e9eSJacob Faibussowitsch   This routine should be called only if there is not a `PetscObject` available to pass to
137310450e9eSJacob Faibussowitsch   `PetscLogEventBegin()`.
137462872c28SLisandro Dalcin 
1375d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`
137662872c28SLisandro Dalcin M*/
137762872c28SLisandro Dalcin 
137862872c28SLisandro Dalcin /*MC
13795c6c1daeSBarry Smith   PetscLogEventBegin - Logs the beginning of a user event.
13805c6c1daeSBarry Smith 
13815c6c1daeSBarry Smith   Synopsis:
1382aaa7dc30SBarry Smith   #include <petsclog.h>
1383f2ba6396SBarry Smith   PetscErrorCode PetscLogEventBegin(int e, PetscObject o1, PetscObject o2, PetscObject o3, PetscObject o4)
13845c6c1daeSBarry Smith 
13855c6c1daeSBarry Smith   Not Collective
13865c6c1daeSBarry Smith 
13875c6c1daeSBarry Smith   Input Parameters:
13885c6c1daeSBarry Smith + e - integer associated with the event obtained from PetscLogEventRegister()
13895c6c1daeSBarry Smith - o1,o2,o3,o4 - objects associated with the event, or 0
13905c6c1daeSBarry Smith 
13915c6c1daeSBarry Smith   Fortran Synopsis:
13925c6c1daeSBarry Smith   void PetscLogEventBegin(int e, PetscErrorCode ierr)
13935c6c1daeSBarry Smith 
139410450e9eSJacob Faibussowitsch   Example Usage:
13955c6c1daeSBarry Smith .vb
13965c6c1daeSBarry Smith   PetscLogEvent USER_EVENT;
139710450e9eSJacob Faibussowitsch 
13985c6c1daeSBarry Smith   PetscLogDouble user_event_flops;
13995c6c1daeSBarry Smith   PetscLogEventRegister("User event",0, &USER_EVENT);
14005c6c1daeSBarry Smith   PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0);
14015c6c1daeSBarry Smith   [code segment to monitor]
14025c6c1daeSBarry Smith   PetscLogFlops(user_event_flops);
14035c6c1daeSBarry Smith   PetscLogEventEnd(USER_EVENT, 0, 0, 0, 0);
14045c6c1daeSBarry Smith .ve
14055c6c1daeSBarry Smith 
1406d1f92df0SBarry Smith   Level: intermediate
1407d1f92df0SBarry Smith 
1408811af0c4SBarry Smith   Developer Note:
140910450e9eSJacob Faibussowitsch   `PetscLogEventBegin()` and `PetscLogEventBegin()` return error codes instead of explicitly
141010450e9eSJacob Faibussowitsch   handling the errors that occur in the macro directly because other packages that use this
141110450e9eSJacob Faibussowitsch   macros have used them in their own functions or methods that do not return error codes and it
141210450e9eSJacob Faibussowitsch   would be disruptive to change the current behavior.
1413d0609cedSBarry Smith 
1414d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventEnd()`, `PetscLogFlops()`
14155c6c1daeSBarry Smith M*/
14165c6c1daeSBarry Smith 
14175c6c1daeSBarry Smith /*MC
14185c6c1daeSBarry Smith   PetscLogEventEnd - Log the end of a user event.
14195c6c1daeSBarry Smith 
14205c6c1daeSBarry Smith   Synopsis:
1421aaa7dc30SBarry Smith   #include <petsclog.h>
1422f2ba6396SBarry Smith   PetscErrorCode PetscLogEventEnd(int e, PetscObject o1, PetscObject o2, PetscObject o3, PetscObject o4)
14235c6c1daeSBarry Smith 
14245c6c1daeSBarry Smith   Not Collective
14255c6c1daeSBarry Smith 
14265c6c1daeSBarry Smith   Input Parameters:
14275c6c1daeSBarry Smith + e - integer associated with the event obtained with PetscLogEventRegister()
14285c6c1daeSBarry Smith - o1,o2,o3,o4 - objects associated with the event, or 0
14295c6c1daeSBarry Smith 
14305c6c1daeSBarry Smith   Fortran Synopsis:
14315c6c1daeSBarry Smith   void PetscLogEventEnd(int e, PetscErrorCode ierr)
14325c6c1daeSBarry Smith 
143310450e9eSJacob Faibussowitsch   Example Usage:
14345c6c1daeSBarry Smith .vb
14355c6c1daeSBarry Smith   PetscLogEvent USER_EVENT;
143610450e9eSJacob Faibussowitsch 
14375c6c1daeSBarry Smith   PetscLogDouble user_event_flops;
143810450e9eSJacob Faibussowitsch   PetscLogEventRegister("User event", 0, &USER_EVENT);
14395c6c1daeSBarry Smith   PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0);
14405c6c1daeSBarry Smith   [code segment to monitor]
14415c6c1daeSBarry Smith   PetscLogFlops(user_event_flops);
14425c6c1daeSBarry Smith   PetscLogEventEnd(USER_EVENT, 0, 0, 0, 0);
14435c6c1daeSBarry Smith .ve
14445c6c1daeSBarry Smith 
14455c6c1daeSBarry Smith   Level: intermediate
14465c6c1daeSBarry Smith 
1447d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogFlops()`
14485c6c1daeSBarry Smith M*/
14495c6c1daeSBarry Smith 
14505c6c1daeSBarry Smith /*@C
14515c6c1daeSBarry Smith   PetscLogEventGetId - Returns the event id when given the event name.
14525c6c1daeSBarry Smith 
14535c6c1daeSBarry Smith   Not Collective
14545c6c1daeSBarry Smith 
14555c6c1daeSBarry Smith   Input Parameter:
14565c6c1daeSBarry Smith . name - The event name
14575c6c1daeSBarry Smith 
14585c6c1daeSBarry Smith   Output Parameter:
1459c5deb1d5SJed Brown . event - The event, or -1 if no event with that name exists
14605c6c1daeSBarry Smith 
14615c6c1daeSBarry Smith   Level: intermediate
14625c6c1daeSBarry Smith 
1463d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogStageGetId()`
14645c6c1daeSBarry Smith @*/
1465d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventGetId(const char name[], PetscLogEvent *event)
1466d71ae5a4SJacob Faibussowitsch {
14675c6c1daeSBarry Smith   PetscStageLog stageLog;
14685c6c1daeSBarry Smith 
14695c6c1daeSBarry Smith   PetscFunctionBegin;
14709566063dSJacob Faibussowitsch   PetscCall(PetscLogGetStageLog(&stageLog));
14719566063dSJacob Faibussowitsch   PetscCall(PetscEventRegLogGetEvent(stageLog->eventLog, name, event));
14723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
14735c6c1daeSBarry Smith }
14745c6c1daeSBarry Smith 
147553e0a2f3SToby Isaac /*@C
147653e0a2f3SToby Isaac   PetscLogEventGetName - Returns the event name when given the event id.
147753e0a2f3SToby Isaac 
147853e0a2f3SToby Isaac   Not Collective
147953e0a2f3SToby Isaac 
148053e0a2f3SToby Isaac   Input Parameter:
148153e0a2f3SToby Isaac . event - The event
148253e0a2f3SToby Isaac 
148353e0a2f3SToby Isaac   Output Parameter:
148453e0a2f3SToby Isaac . name  - The event name
148553e0a2f3SToby Isaac 
148653e0a2f3SToby Isaac   Level: intermediate
148753e0a2f3SToby Isaac 
148853e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()`
148953e0a2f3SToby Isaac @*/
149053e0a2f3SToby Isaac PetscErrorCode PetscLogEventGetName(PetscLogEvent event, const char **name)
149153e0a2f3SToby Isaac {
149253e0a2f3SToby Isaac   PetscLogEventInfo event_info;
149353e0a2f3SToby Isaac   PetscLogState     state;
149453e0a2f3SToby Isaac 
149553e0a2f3SToby Isaac   PetscFunctionBegin;
149653e0a2f3SToby Isaac   PetscCall(PetscLogGetState(&state));
149753e0a2f3SToby Isaac   PetscCall(PetscLogStateEventGetInfo(state, event, &event_info));
149853e0a2f3SToby Isaac   *name = event_info.name;
149953e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
150053e0a2f3SToby Isaac }
150153e0a2f3SToby Isaac 
150253e0a2f3SToby Isaac /*@
150353e0a2f3SToby Isaac   PetscLogEventsPause - Put event logging into "paused" mode: timers and counters for in-progress events are paused, and any events that happen before logging is resumed with `PetscLogEventsResume()` are logged in the "Main Stage" of execution.
150453e0a2f3SToby Isaac 
150553e0a2f3SToby Isaac   Not collective
150653e0a2f3SToby Isaac 
150753e0a2f3SToby Isaac   Level: advanced
150853e0a2f3SToby Isaac 
150953e0a2f3SToby Isaac   Notes:
151053e0a2f3SToby Isaac   When an external library or runtime has is initialized it can involve lots of setup time that skews the statistics of any unrelated running events: this function is intended to isolate such calls in the default log summary (`PetscLogDefaultBegin()`, `PetscLogView()`).
151153e0a2f3SToby Isaac 
151253e0a2f3SToby Isaac   Other log handlers (such as the nested handler, `PetscLogNestedBegin()`) will ignore this function.
151353e0a2f3SToby Isaac 
151453e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogEventDeactivatePush()`, `PetscLogEventDeactivatePop()`, `PetscLogEventsResume()`
151553e0a2f3SToby Isaac @*/
151653e0a2f3SToby Isaac PetscErrorCode PetscLogEventsPause(void)
151753e0a2f3SToby Isaac {
151853e0a2f3SToby Isaac   PetscLogHandler handler;
151953e0a2f3SToby Isaac 
152053e0a2f3SToby Isaac   PetscFunctionBegin;
152153e0a2f3SToby Isaac   PetscCall(PetscLogTryGetHandler(PETSC_LOG_HANDLER_DEFAULT, &handler));
152253e0a2f3SToby Isaac   if (handler) PetscCall(PetscLogHandlerDefaultEventsPause(handler));
152353e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
152453e0a2f3SToby Isaac }
152553e0a2f3SToby Isaac 
152653e0a2f3SToby Isaac /*@
152753e0a2f3SToby Isaac   PetscLogEventsResume - Return logging to normal behavior after it was paused with `PetscLogEventsPause()`.
152853e0a2f3SToby Isaac 
152953e0a2f3SToby Isaac   Not collective
153053e0a2f3SToby Isaac 
153153e0a2f3SToby Isaac   Level: advanced
153253e0a2f3SToby Isaac 
153353e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogEventDeactivatePush()`, `PetscLogEventDeactivatePop()`, `PetscLogEventsPause()`
153453e0a2f3SToby Isaac @*/
153553e0a2f3SToby Isaac PetscErrorCode PetscLogEventsResume(void)
153653e0a2f3SToby Isaac {
153753e0a2f3SToby Isaac   PetscLogHandler handler;
153853e0a2f3SToby Isaac 
153953e0a2f3SToby Isaac   PetscFunctionBegin;
154053e0a2f3SToby Isaac   PetscCall(PetscLogTryGetHandler(PETSC_LOG_HANDLER_DEFAULT, &handler));
154153e0a2f3SToby Isaac   if (handler) PetscCall(PetscLogHandlerDefaultEventsResume(handler));
154253e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
154353e0a2f3SToby Isaac }
154453e0a2f3SToby Isaac 
1545d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogPushCurrentEvent_Internal(PetscLogEvent event)
1546d71ae5a4SJacob Faibussowitsch {
15477a101e5eSJacob Faibussowitsch   PetscFunctionBegin;
1548c708d6e3SStefano Zampini   if (!PetscDefined(HAVE_THREADSAFETY)) PetscCall(PetscIntStackPush(current_log_event_stack, event));
15493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
15507a101e5eSJacob Faibussowitsch }
15517a101e5eSJacob Faibussowitsch 
1552d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogPopCurrentEvent_Internal(void)
1553d71ae5a4SJacob Faibussowitsch {
15547a101e5eSJacob Faibussowitsch   PetscFunctionBegin;
1555c708d6e3SStefano Zampini   if (!PetscDefined(HAVE_THREADSAFETY)) PetscCall(PetscIntStackPop(current_log_event_stack, NULL));
15563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
15577a101e5eSJacob Faibussowitsch }
15587a101e5eSJacob Faibussowitsch 
1559d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogGetCurrentEvent_Internal(PetscLogEvent *event)
1560d71ae5a4SJacob Faibussowitsch {
15617a101e5eSJacob Faibussowitsch   PetscBool empty;
15627a101e5eSJacob Faibussowitsch 
15637a101e5eSJacob Faibussowitsch   PetscFunctionBegin;
15644f572ea9SToby Isaac   PetscAssertPointer(event, 1);
15657a101e5eSJacob Faibussowitsch   *event = PETSC_DECIDE;
15667a101e5eSJacob Faibussowitsch   PetscCall(PetscIntStackEmpty(current_log_event_stack, &empty));
15677a101e5eSJacob Faibussowitsch   if (!empty) PetscCall(PetscIntStackTop(current_log_event_stack, event));
15683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
15697a101e5eSJacob Faibussowitsch }
15707a101e5eSJacob Faibussowitsch 
1571d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventPause_Internal(PetscLogEvent event)
1572d71ae5a4SJacob Faibussowitsch {
15737a101e5eSJacob Faibussowitsch   PetscFunctionBegin;
15747a101e5eSJacob Faibussowitsch   if (event != PETSC_DECIDE) PetscCall(PetscLogEventEnd(event, NULL, NULL, NULL, NULL));
15753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
15767a101e5eSJacob Faibussowitsch }
15777a101e5eSJacob Faibussowitsch 
1578d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventResume_Internal(PetscLogEvent event)
1579d71ae5a4SJacob Faibussowitsch {
15807a101e5eSJacob Faibussowitsch   PetscStageLog     stageLog;
15817a101e5eSJacob Faibussowitsch   PetscEventPerfLog eventLog;
15827a101e5eSJacob Faibussowitsch   int               stage;
15837a101e5eSJacob Faibussowitsch 
15847a101e5eSJacob Faibussowitsch   PetscFunctionBegin;
15853ba16761SJacob Faibussowitsch   if (event == PETSC_DECIDE) PetscFunctionReturn(PETSC_SUCCESS);
15867a101e5eSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(event, NULL, NULL, NULL, NULL));
15877a101e5eSJacob Faibussowitsch   PetscCall(PetscLogGetStageLog(&stageLog));
15887a101e5eSJacob Faibussowitsch   PetscCall(PetscStageLogGetCurrent(stageLog, &stage));
15897a101e5eSJacob Faibussowitsch   PetscCall(PetscStageLogGetEventPerfLog(stageLog, stage, &eventLog));
15907a101e5eSJacob Faibussowitsch   eventLog->eventInfo[event].count--;
15913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
15927a101e5eSJacob Faibussowitsch }
15937a101e5eSJacob Faibussowitsch 
15941c1ad86eSToby Isaac /*------------------------------------------------ Class Functions --------------------------------------------------*/
15951c1ad86eSToby Isaac 
15961c1ad86eSToby Isaac /*MC
15971c1ad86eSToby Isaac    PetscLogObjectCreate - Log the creation of a `PetscObject`
15981c1ad86eSToby Isaac 
15991c1ad86eSToby Isaac    Synopsis:
16001c1ad86eSToby Isaac    #include <petsclog.h>
16011c1ad86eSToby Isaac    PetscErrorCode PetscLogObjectCreate(PetscObject h)
16021c1ad86eSToby Isaac 
16031c1ad86eSToby Isaac    Not Collective
16041c1ad86eSToby Isaac 
16051c1ad86eSToby Isaac    Input Parameters:
16061c1ad86eSToby Isaac .  h - A `PetscObject`
16071c1ad86eSToby Isaac 
16081c1ad86eSToby Isaac    Level: developer
16091c1ad86eSToby Isaac 
16101c1ad86eSToby Isaac    Developer Note:
16111c1ad86eSToby Isaac      Called internally by PETSc when creating objects: users do not need to call this directly.
16121c1ad86eSToby Isaac 
16131c1ad86eSToby Isaac .seealso: [](ch_profiling), `PetscLogObjectDestroy()`
16141c1ad86eSToby Isaac M*/
16151c1ad86eSToby Isaac 
16161c1ad86eSToby Isaac /*MC
16171c1ad86eSToby Isaac    PetscLogObjectDestroy - Logs the destruction of a `PetscObject`
16181c1ad86eSToby Isaac 
16191c1ad86eSToby Isaac    Synopsis:
16201c1ad86eSToby Isaac    #include <petsclog.h>
16211c1ad86eSToby Isaac    PetscErrorCode PetscLogObjectDestroy(PetscObject h)
16221c1ad86eSToby Isaac 
16231c1ad86eSToby Isaac    Not Collective
16241c1ad86eSToby Isaac 
16251c1ad86eSToby Isaac    Input Parameters:
16261c1ad86eSToby Isaac .  h - A `PetscObject`
16271c1ad86eSToby Isaac 
16281c1ad86eSToby Isaac    Level: developer
16291c1ad86eSToby Isaac 
16301c1ad86eSToby Isaac    Developer Note:
16311c1ad86eSToby Isaac      Called internally by PETSc when destroying objects: users do not need to call this directly.
16321c1ad86eSToby Isaac 
16331c1ad86eSToby Isaac .seealso: [](ch_profiling), `PetscLogObjectCreate()`
16341c1ad86eSToby Isaac M*/
16351c1ad86eSToby Isaac 
163653e0a2f3SToby Isaac /*@C
163753e0a2f3SToby Isaac   PetscLogClassGetClassId - Returns the `PetscClassId` when given the class name.
163853e0a2f3SToby Isaac 
163953e0a2f3SToby Isaac   Not Collective
164053e0a2f3SToby Isaac 
164153e0a2f3SToby Isaac   Input Parameter:
164253e0a2f3SToby Isaac . name - The class name
164353e0a2f3SToby Isaac 
164453e0a2f3SToby Isaac   Output Parameter:
164553e0a2f3SToby Isaac . classid - The `PetscClassId` id, or -1 if no class with that name exists
164653e0a2f3SToby Isaac 
164753e0a2f3SToby Isaac   Level: intermediate
164853e0a2f3SToby Isaac 
164953e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogStageGetId()`
165053e0a2f3SToby Isaac @*/
165153e0a2f3SToby Isaac PetscErrorCode PetscLogClassGetClassId(const char name[], PetscClassId *classid)
165253e0a2f3SToby Isaac {
165353e0a2f3SToby Isaac   PetscLogClass     log_class;
165453e0a2f3SToby Isaac   PetscLogClassInfo class_info;
165553e0a2f3SToby Isaac   PetscLogState     state;
165653e0a2f3SToby Isaac 
165753e0a2f3SToby Isaac   PetscFunctionBegin;
165853e0a2f3SToby Isaac   PetscCall(PetscLogGetState(&state));
165953e0a2f3SToby Isaac   PetscCall(PetscLogStateGetClassFromName(state, name, &log_class));
166053e0a2f3SToby Isaac   if (log_class < 0) {
166153e0a2f3SToby Isaac     *classid = -1;
166253e0a2f3SToby Isaac     PetscFunctionReturn(PETSC_SUCCESS);
166353e0a2f3SToby Isaac   }
166453e0a2f3SToby Isaac   PetscCall(PetscLogStateClassGetInfo(state, log_class, &class_info));
166553e0a2f3SToby Isaac   *classid = class_info.classid;
166653e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
166753e0a2f3SToby Isaac }
166853e0a2f3SToby Isaac 
166953e0a2f3SToby Isaac /*@C
167053e0a2f3SToby Isaac   PetscLogClassIdGetName - Returns a `PetscClassId`'s name.
167153e0a2f3SToby Isaac 
167253e0a2f3SToby Isaac   Not Collective
167353e0a2f3SToby Isaac 
167453e0a2f3SToby Isaac   Input Parameter:
167553e0a2f3SToby Isaac . classid - A `PetscClassId`
167653e0a2f3SToby Isaac 
167753e0a2f3SToby Isaac   Output Parameter:
167853e0a2f3SToby Isaac . name - The class name
167953e0a2f3SToby Isaac 
168053e0a2f3SToby Isaac   Level: intermediate
168153e0a2f3SToby Isaac 
168253e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogClassRegister()`, `PetscLogClassBegin()`, `PetscLogClassEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadClass()`
168353e0a2f3SToby Isaac @*/
168453e0a2f3SToby Isaac PetscErrorCode PetscLogClassIdGetName(PetscClassId classid, const char **name)
168553e0a2f3SToby Isaac {
168653e0a2f3SToby Isaac   PetscLogClass     log_class;
168753e0a2f3SToby Isaac   PetscLogClassInfo class_info;
168853e0a2f3SToby Isaac   PetscLogState     state;
168953e0a2f3SToby Isaac 
169053e0a2f3SToby Isaac   PetscFunctionBegin;
169153e0a2f3SToby Isaac   PetscCall(PetscLogGetState(&state));
169253e0a2f3SToby Isaac   PetscCall(PetscLogStateGetClassFromClassId(state, classid, &log_class));
169353e0a2f3SToby Isaac   PetscCall(PetscLogStateClassGetInfo(state, log_class, &class_info));
169453e0a2f3SToby Isaac   *name = class_info.name;
169553e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
169653e0a2f3SToby Isaac }
169753e0a2f3SToby Isaac 
16985c6c1daeSBarry Smith /*------------------------------------------------ Output Functions -------------------------------------------------*/
16995c6c1daeSBarry Smith /*@C
17005c6c1daeSBarry Smith   PetscLogDump - Dumps logs of objects to a file. This file is intended to
17015c6c1daeSBarry Smith   be read by bin/petscview. This program no longer exists.
17025c6c1daeSBarry Smith 
1703811af0c4SBarry Smith   Collective on `PETSC_COMM_WORLD`
17045c6c1daeSBarry Smith 
17055c6c1daeSBarry Smith   Input Parameter:
1706aec76313SJacob Faibussowitsch . sname - an optional file name
17075c6c1daeSBarry Smith 
170810450e9eSJacob Faibussowitsch   Example Usage:
17095c6c1daeSBarry Smith .vb
17105c6c1daeSBarry Smith   PetscInitialize(...);
1711bb1d7374SBarry Smith   PetscLogDefaultBegin(); or PetscLogAllBegin();
17125c6c1daeSBarry Smith   ... code ...
17135c6c1daeSBarry Smith   PetscLogDump(filename);
17145c6c1daeSBarry Smith   PetscFinalize();
17155c6c1daeSBarry Smith .ve
17165c6c1daeSBarry Smith 
1717d1f92df0SBarry Smith   Level: advanced
1718d1f92df0SBarry Smith 
1719811af0c4SBarry Smith   Note:
172037fdd005SBarry Smith   The default file name is Log.<rank> where <rank> is the MPI process rank. If no name is specified,
17215c6c1daeSBarry Smith   this file will be used.
17225c6c1daeSBarry Smith 
1723d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogDefaultBegin()`, `PetscLogAllBegin()`, `PetscLogView()`
17245c6c1daeSBarry Smith @*/
1725d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogDump(const char sname[])
1726d71ae5a4SJacob Faibussowitsch {
17275c6c1daeSBarry Smith   PetscStageLog       stageLog;
17285c6c1daeSBarry Smith   PetscEventPerfInfo *eventInfo;
17295c6c1daeSBarry Smith   FILE               *fd;
17305c6c1daeSBarry Smith   char                file[PETSC_MAX_PATH_LEN], fname[PETSC_MAX_PATH_LEN];
17315c6c1daeSBarry Smith   PetscLogDouble      flops, _TotalTime;
17325c6c1daeSBarry Smith   PetscMPIInt         rank;
17335c6c1daeSBarry Smith   int                 action, object, curStage;
17345c6c1daeSBarry Smith   PetscLogEvent       event;
17355c6c1daeSBarry Smith 
17365c6c1daeSBarry Smith   PetscFunctionBegin;
17375c6c1daeSBarry Smith   /* Calculate the total elapsed time */
17383ba16761SJacob Faibussowitsch   PetscCall(PetscTime(&_TotalTime));
17395c6c1daeSBarry Smith   _TotalTime -= petsc_BaseTime;
17405c6c1daeSBarry Smith   /* Open log file */
17419566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
1742a364092eSJacob Faibussowitsch   PetscCall(PetscSNPrintf(file, PETSC_STATIC_ARRAY_LENGTH(file), "%s.%d", sname && sname[0] ? sname : "Log", rank));
17439566063dSJacob Faibussowitsch   PetscCall(PetscFixFilename(file, fname));
17449566063dSJacob Faibussowitsch   PetscCall(PetscFOpen(PETSC_COMM_WORLD, fname, "w", &fd));
174508401ef6SPierre Jolivet   PetscCheck(!(rank == 0) || !(!fd), PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Cannot open file: %s", fname);
17465c6c1daeSBarry Smith   /* Output totals */
17479566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(PETSC_COMM_WORLD, fd, "Total Flop %14e %16.8e\n", petsc_TotalFlops, _TotalTime));
17489566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(PETSC_COMM_WORLD, fd, "Clock Resolution %g\n", 0.0));
17495c6c1daeSBarry Smith   /* Output actions */
17505c6c1daeSBarry Smith   if (petsc_logActions) {
17519566063dSJacob Faibussowitsch     PetscCall(PetscFPrintf(PETSC_COMM_WORLD, fd, "Actions accomplished %d\n", petsc_numActions));
17525c6c1daeSBarry Smith     for (action = 0; action < petsc_numActions; action++) {
17539371c9d4SSatish Balay       PetscCall(PetscFPrintf(PETSC_COMM_WORLD, fd, "%g %d %d %d %d %d %d %g %g %g\n", petsc_actions[action].time, petsc_actions[action].action, (int)petsc_actions[action].event, (int)petsc_actions[action].classid, petsc_actions[action].id1,
1754d0609cedSBarry Smith                              petsc_actions[action].id2, petsc_actions[action].id3, petsc_actions[action].flops, petsc_actions[action].mem, petsc_actions[action].maxmem));
17555c6c1daeSBarry Smith     }
17565c6c1daeSBarry Smith   }
17575c6c1daeSBarry Smith   /* Output objects */
17585c6c1daeSBarry Smith   if (petsc_logObjects) {
17599566063dSJacob Faibussowitsch     PetscCall(PetscFPrintf(PETSC_COMM_WORLD, fd, "Objects created %d destroyed %d\n", petsc_numObjects, petsc_numObjectsDestroyed));
17605c6c1daeSBarry Smith     for (object = 0; object < petsc_numObjects; object++) {
17619566063dSJacob Faibussowitsch       PetscCall(PetscFPrintf(PETSC_COMM_WORLD, fd, "Parent ID: %d Memory: %d\n", petsc_objects[object].parent, (int)petsc_objects[object].mem));
17625c6c1daeSBarry Smith       if (!petsc_objects[object].name[0]) {
17639566063dSJacob Faibussowitsch         PetscCall(PetscFPrintf(PETSC_COMM_WORLD, fd, "No Name\n"));
17645c6c1daeSBarry Smith       } else {
17659566063dSJacob Faibussowitsch         PetscCall(PetscFPrintf(PETSC_COMM_WORLD, fd, "Name: %s\n", petsc_objects[object].name));
17665c6c1daeSBarry Smith       }
17675c6c1daeSBarry Smith       if (petsc_objects[object].info[0] != 0) {
17689566063dSJacob Faibussowitsch         PetscCall(PetscFPrintf(PETSC_COMM_WORLD, fd, "No Info\n"));
17695c6c1daeSBarry Smith       } else {
17709566063dSJacob Faibussowitsch         PetscCall(PetscFPrintf(PETSC_COMM_WORLD, fd, "Info: %s\n", petsc_objects[object].info));
17715c6c1daeSBarry Smith       }
17725c6c1daeSBarry Smith     }
17735c6c1daeSBarry Smith   }
17745c6c1daeSBarry Smith   /* Output events */
17759566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(PETSC_COMM_WORLD, fd, "Event log:\n"));
17769566063dSJacob Faibussowitsch   PetscCall(PetscLogGetStageLog(&stageLog));
17779566063dSJacob Faibussowitsch   PetscCall(PetscIntStackTop(stageLog->stack, &curStage));
17785c6c1daeSBarry Smith   eventInfo = stageLog->stageInfo[curStage].eventLog->eventInfo;
17795c6c1daeSBarry Smith   for (event = 0; event < stageLog->stageInfo[curStage].eventLog->numEvents; event++) {
1780a297a907SKarl Rupp     if (eventInfo[event].time != 0.0) flops = eventInfo[event].flops / eventInfo[event].time;
1781a297a907SKarl Rupp     else flops = 0.0;
1782d0609cedSBarry Smith     PetscCall(PetscFPrintf(PETSC_COMM_WORLD, fd, "%d %16d %16g %16g %16g\n", event, eventInfo[event].count, eventInfo[event].flops, eventInfo[event].time, flops));
17835c6c1daeSBarry Smith   }
17849566063dSJacob Faibussowitsch   PetscCall(PetscFClose(PETSC_COMM_WORLD, fd));
17853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
17865c6c1daeSBarry Smith }
17875c6c1daeSBarry Smith 
1788f14045dbSBarry Smith /*
1789f14045dbSBarry Smith   PetscLogView_Detailed - Each process prints the times for its own events
1790f14045dbSBarry Smith 
1791f14045dbSBarry Smith */
179210450e9eSJacob Faibussowitsch static PetscErrorCode PetscLogView_Detailed(PetscViewer viewer)
1793d71ae5a4SJacob Faibussowitsch {
17942dff6485SMatthew G. Knepley   PetscStageLog       stageLog;
1795217044c2SLisandro Dalcin   PetscEventPerfInfo *eventInfo = NULL, *stageInfo = NULL;
1796217044c2SLisandro Dalcin   PetscLogDouble      locTotalTime, numRed, maxMem;
17972dff6485SMatthew G. Knepley   int                 numStages, numEvents, stage, event;
1798217044c2SLisandro Dalcin   MPI_Comm            comm = PetscObjectComm((PetscObject)viewer);
17992dff6485SMatthew G. Knepley   PetscMPIInt         rank, size;
1800f14045dbSBarry Smith 
1801f14045dbSBarry Smith   PetscFunctionBegin;
18029566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
18039566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
18042dff6485SMatthew G. Knepley   /* Must preserve reduction count before we go on */
18052dff6485SMatthew G. Knepley   numRed = petsc_allreduce_ct + petsc_gather_ct + petsc_scatter_ct;
18062d1ec343SBarry Smith   /* Get the total elapsed time */
18073ba16761SJacob Faibussowitsch   PetscCall(PetscTime(&locTotalTime));
18089371c9d4SSatish Balay   locTotalTime -= petsc_BaseTime;
18099566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "size = %d\n", size));
18109566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "LocalTimes = {}\n"));
18119566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "LocalMessages = {}\n"));
18129566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "LocalMessageLens = {}\n"));
18139566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "LocalReductions = {}\n"));
18149566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "LocalFlop = {}\n"));
18159566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "LocalObjects = {}\n"));
18169566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "LocalMemory = {}\n"));
18179566063dSJacob Faibussowitsch   PetscCall(PetscLogGetStageLog(&stageLog));
18189566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(&stageLog->numStages, &numStages, 1, MPI_INT, MPI_MAX, comm));
18199566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPrintf(viewer, "Stages = {}\n"));
18202d1ec343SBarry Smith   for (stage = 0; stage < numStages; stage++) {
18219566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "Stages[\"%s\"] = {}\n", stageLog->stageInfo[stage].name));
18229566063dSJacob Faibussowitsch     PetscCall(PetscViewerASCIIPrintf(viewer, "Stages[\"%s\"][\"summary\"] = {}\n", stageLog->stageInfo[stage].name));
18239566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce(&stageLog->stageInfo[stage].eventLog->numEvents, &numEvents, 1, MPI_INT, MPI_MAX, comm));
182448a46eb9SPierre Jolivet     for (event = 0; event < numEvents; event++) PetscCall(PetscViewerASCIIPrintf(viewer, "Stages[\"%s\"][\"%s\"] = {}\n", stageLog->stageInfo[stage].name, stageLog->eventLog->eventInfo[event].name));
18252d1ec343SBarry Smith   }
18269566063dSJacob Faibussowitsch   PetscCall(PetscMallocGetMaximumUsage(&maxMem));
18279566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPushSynchronized(viewer));
18289566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "LocalTimes[%d] = %g\n", rank, locTotalTime));
18299566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "LocalMessages[%d] = %g\n", rank, (petsc_irecv_ct + petsc_isend_ct + petsc_recv_ct + petsc_send_ct)));
18309566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "LocalMessageLens[%d] = %g\n", rank, (petsc_irecv_len + petsc_isend_len + petsc_recv_len + petsc_send_len)));
18319566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "LocalReductions[%d] = %g\n", rank, numRed));
18329566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "LocalFlop[%d] = %g\n", rank, petsc_TotalFlops));
18339566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "LocalObjects[%d] = %d\n", rank, petsc_numObjects));
18349566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "LocalMemory[%d] = %g\n", rank, maxMem));
18359566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(viewer));
18362d1ec343SBarry Smith   for (stage = 0; stage < numStages; stage++) {
1837217044c2SLisandro Dalcin     stageInfo = &stageLog->stageInfo[stage].perfInfo;
18389371c9d4SSatish Balay     PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "Stages[\"%s\"][\"summary\"][%d] = {\"time\" : %g, \"numMessages\" : %g, \"messageLength\" : %g, \"numReductions\" : %g, \"flop\" : %g}\n", stageLog->stageInfo[stage].name, rank, stageInfo->time,
18399371c9d4SSatish Balay                                                  stageInfo->numMessages, stageInfo->messageLength, stageInfo->numReductions, stageInfo->flops));
18409566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce(&stageLog->stageInfo[stage].eventLog->numEvents, &numEvents, 1, MPI_INT, MPI_MAX, comm));
18412d1ec343SBarry Smith     for (event = 0; event < numEvents; event++) {
1842217044c2SLisandro Dalcin       eventInfo = &stageLog->stageInfo[stage].eventLog->eventInfo[event];
1843d0609cedSBarry Smith       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "Stages[\"%s\"][\"%s\"][%d] = {\"count\" : %d, \"time\" : %g, \"syncTime\" : %g, \"numMessages\" : %g, \"messageLength\" : %g, \"numReductions\" : %g, \"flop\" : %g",
18449371c9d4SSatish Balay                                                    stageLog->stageInfo[stage].name, stageLog->eventLog->eventInfo[event].name, rank, eventInfo->count, eventInfo->time, eventInfo->syncTime, eventInfo->numMessages, eventInfo->messageLength, eventInfo->numReductions,
18459371c9d4SSatish Balay                                                    eventInfo->flops));
1846891e75beSMatthew G. Knepley       if (eventInfo->dof[0] >= 0.) {
1847891e75beSMatthew G. Knepley         PetscInt d, e;
18485d68e14cSMatthew G. Knepley 
18499566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ", \"dof\" : ["));
1850891e75beSMatthew G. Knepley         for (d = 0; d < 8; ++d) {
18519566063dSJacob Faibussowitsch           if (d > 0) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ", "));
18529566063dSJacob Faibussowitsch           PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%g", eventInfo->dof[d]));
1853891e75beSMatthew G. Knepley         }
18549566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "]"));
18559566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ", \"error\" : ["));
18565d68e14cSMatthew G. Knepley         for (e = 0; e < 8; ++e) {
18579566063dSJacob Faibussowitsch           if (e > 0) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ", "));
18589566063dSJacob Faibussowitsch           PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%g", eventInfo->errors[e]));
18595d68e14cSMatthew G. Knepley         }
18609566063dSJacob Faibussowitsch         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "]"));
18615d68e14cSMatthew G. Knepley       }
18629566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "}\n"));
18632d1ec343SBarry Smith     }
18642d1ec343SBarry Smith   }
18659566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(viewer));
18669566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPopSynchronized(viewer));
18673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1868f14045dbSBarry Smith }
1869f14045dbSBarry Smith 
187038144912Sdeepblu2718 /*
187138144912Sdeepblu2718   PetscLogView_CSV - Each process prints the times for its own events in Comma-Separated Value Format
187238144912Sdeepblu2718 */
187310450e9eSJacob Faibussowitsch static PetscErrorCode PetscLogView_CSV(PetscViewer viewer)
1874d71ae5a4SJacob Faibussowitsch {
187538144912Sdeepblu2718   PetscStageLog       stageLog;
1876669c5be0SSatish Balay   PetscEventPerfInfo *eventInfo = NULL;
187738144912Sdeepblu2718   PetscLogDouble      locTotalTime, maxMem;
187838144912Sdeepblu2718   int                 numStages, numEvents, stage, event;
187938144912Sdeepblu2718   MPI_Comm            comm = PetscObjectComm((PetscObject)viewer);
188038144912Sdeepblu2718   PetscMPIInt         rank, size;
188138144912Sdeepblu2718 
188238144912Sdeepblu2718   PetscFunctionBegin;
18839566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
18849566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
188538144912Sdeepblu2718   /* Must preserve reduction count before we go on */
188638144912Sdeepblu2718   /* Get the total elapsed time */
18873ba16761SJacob Faibussowitsch   PetscCall(PetscTime(&locTotalTime));
18889371c9d4SSatish Balay   locTotalTime -= petsc_BaseTime;
18899566063dSJacob Faibussowitsch   PetscCall(PetscLogGetStageLog(&stageLog));
18909566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(&stageLog->numStages, &numStages, 1, MPI_INT, MPI_MAX, comm));
18919566063dSJacob Faibussowitsch   PetscCall(PetscMallocGetMaximumUsage(&maxMem));
18929566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPushSynchronized(viewer));
1893d0609cedSBarry Smith   PetscCall(PetscViewerASCIIPrintf(viewer, "Stage Name,Event Name,Rank,Count,Time,Num Messages,Message Length,Num Reductions,FLOP,dof0,dof1,dof2,dof3,dof4,dof5,dof6,dof7,e0,e1,e2,e3,e4,e5,e6,e7,%d\n", size));
18949566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(viewer));
189538144912Sdeepblu2718   for (stage = 0; stage < numStages; stage++) {
18969e29573dSMatthew G. Knepley     PetscEventPerfInfo *stageInfo = &stageLog->stageInfo[stage].perfInfo;
18979e29573dSMatthew G. Knepley 
18989371c9d4SSatish Balay     PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%s,summary,%d,1,%g,%g,%g,%g,%g\n", stageLog->stageInfo[stage].name, rank, stageInfo->time, stageInfo->numMessages, stageInfo->messageLength, stageInfo->numReductions, stageInfo->flops));
18999566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce(&stageLog->stageInfo[stage].eventLog->numEvents, &numEvents, 1, MPI_INT, MPI_MAX, comm));
190038144912Sdeepblu2718     for (event = 0; event < numEvents; event++) {
190138144912Sdeepblu2718       eventInfo = &stageLog->stageInfo[stage].eventLog->eventInfo[event];
19029371c9d4SSatish Balay       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%s,%s,%d,%d,%g,%g,%g,%g,%g", stageLog->stageInfo[stage].name, stageLog->eventLog->eventInfo[event].name, rank, eventInfo->count, eventInfo->time, eventInfo->numMessages, eventInfo->messageLength,
19039371c9d4SSatish Balay                                                    eventInfo->numReductions, eventInfo->flops));
190438144912Sdeepblu2718       if (eventInfo->dof[0] >= 0.) {
190538144912Sdeepblu2718         PetscInt d, e;
190638144912Sdeepblu2718 
190748a46eb9SPierre Jolivet         for (d = 0; d < 8; ++d) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ",%g", eventInfo->dof[d]));
190848a46eb9SPierre Jolivet         for (e = 0; e < 8; ++e) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ",%g", eventInfo->errors[e]));
190938144912Sdeepblu2718       }
19109566063dSJacob Faibussowitsch       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n"));
191138144912Sdeepblu2718     }
191238144912Sdeepblu2718   }
19139566063dSJacob Faibussowitsch   PetscCall(PetscViewerFlush(viewer));
19149566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIPopSynchronized(viewer));
19153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
191638144912Sdeepblu2718 }
191738144912Sdeepblu2718 
1918d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscLogViewWarnSync(MPI_Comm comm, FILE *fd)
1919d71ae5a4SJacob Faibussowitsch {
1920217044c2SLisandro Dalcin   PetscFunctionBegin;
19213ba16761SJacob Faibussowitsch   if (!PetscLogSyncOn) PetscFunctionReturn(PETSC_SUCCESS);
19229566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "\n\n"));
19239566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "      ##########################################################\n"));
19249566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "      #                                                        #\n"));
19259566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "      #                       WARNING!!!                       #\n"));
19269566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "      #                                                        #\n"));
19279566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "      #   This program was run with logging synchronization.   #\n"));
19289566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "      #   This option provides more meaningful imbalance       #\n"));
19299566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "      #   figures at the expense of slowing things down and    #\n"));
19309566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "      #   providing a distorted view of the overall runtime.   #\n"));
19319566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "      #                                                        #\n"));
19329566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "      ##########################################################\n\n\n"));
19333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1934217044c2SLisandro Dalcin }
1935217044c2SLisandro Dalcin 
1936d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscLogViewWarnDebugging(MPI_Comm comm, FILE *fd)
1937d71ae5a4SJacob Faibussowitsch {
1938f4091ad2SBarry Smith   PetscFunctionBegin;
193976bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
19409566063dSJacob Faibussowitsch     PetscCall(PetscFPrintf(comm, fd, "\n\n"));
19419566063dSJacob Faibussowitsch     PetscCall(PetscFPrintf(comm, fd, "      ##########################################################\n"));
19429566063dSJacob Faibussowitsch     PetscCall(PetscFPrintf(comm, fd, "      #                                                        #\n"));
19439566063dSJacob Faibussowitsch     PetscCall(PetscFPrintf(comm, fd, "      #                       WARNING!!!                       #\n"));
19449566063dSJacob Faibussowitsch     PetscCall(PetscFPrintf(comm, fd, "      #                                                        #\n"));
19459566063dSJacob Faibussowitsch     PetscCall(PetscFPrintf(comm, fd, "      #   This code was compiled with a debugging option.      #\n"));
19469566063dSJacob Faibussowitsch     PetscCall(PetscFPrintf(comm, fd, "      #   To get timing results run ./configure                #\n"));
19479566063dSJacob Faibussowitsch     PetscCall(PetscFPrintf(comm, fd, "      #   using --with-debugging=no, the performance will      #\n"));
19489566063dSJacob Faibussowitsch     PetscCall(PetscFPrintf(comm, fd, "      #   be generally two or three times faster.              #\n"));
19499566063dSJacob Faibussowitsch     PetscCall(PetscFPrintf(comm, fd, "      #                                                        #\n"));
19509566063dSJacob Faibussowitsch     PetscCall(PetscFPrintf(comm, fd, "      ##########################################################\n\n\n"));
195176bd3646SJed Brown   }
19523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1953f4091ad2SBarry Smith }
1954f4091ad2SBarry Smith 
1955d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscLogViewWarnNoGpuAwareMpi(MPI_Comm comm, FILE *fd)
1956d71ae5a4SJacob Faibussowitsch {
1957009ab46cSJunchao Zhang   #if defined(PETSC_HAVE_DEVICE)
1958009ab46cSJunchao Zhang   PetscMPIInt size;
19598fe3844cSJunchao Zhang   PetscBool   deviceInitialized = PETSC_FALSE;
1960c2a741eeSJunchao Zhang 
1961c2a741eeSJunchao Zhang   PetscFunctionBegin;
19629566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
19638fe3844cSJunchao Zhang   for (int i = PETSC_DEVICE_HOST + 1; i < PETSC_DEVICE_MAX; ++i) {
19648fe3844cSJunchao Zhang     const PetscDeviceType dtype = PetscDeviceTypeCast(i);
19658fe3844cSJunchao Zhang     if (PetscDeviceInitialized(dtype)) { /* a non-host device was initialized */
19668fe3844cSJunchao Zhang       deviceInitialized = PETSC_TRUE;
19678fe3844cSJunchao Zhang       break;
19688fe3844cSJunchao Zhang     }
19698fe3844cSJunchao Zhang   }
19708fe3844cSJunchao Zhang   /* the last condition says petsc is configured with device but it is a pure CPU run, so don't print misleading warnings */
19713ba16761SJacob Faibussowitsch   if (use_gpu_aware_mpi || size == 1 || !deviceInitialized) PetscFunctionReturn(PETSC_SUCCESS);
19729566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "\n\n"));
19739566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "      ##########################################################\n"));
19749566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "      #                                                        #\n"));
19759566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "      #                       WARNING!!!                       #\n"));
19769566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "      #                                                        #\n"));
19779566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "      #   This code was compiled with GPU support and you've   #\n"));
19789566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "      #   created PETSc/GPU objects, but you intentionally     #\n"));
19799566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "      #   used -use_gpu_aware_mpi 0, requiring PETSc to copy   #\n"));
19809566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "      #   additional data between the GPU and CPU. To obtain   #\n"));
19819566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "      #   meaningful timing results on multi-rank runs, use    #\n"));
19829566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "      #   GPU-aware MPI instead.                               #\n"));
19839566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "      #                                                        #\n"));
19849566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "      ##########################################################\n\n\n"));
19853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1986c2a741eeSJunchao Zhang   #else
198710450e9eSJacob Faibussowitsch   (void)comm;
198810450e9eSJacob Faibussowitsch   (void)fd;
19893ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
1990c2a741eeSJunchao Zhang   #endif
1991c2a741eeSJunchao Zhang }
1992c2a741eeSJunchao Zhang 
1993d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscLogViewWarnGpuTime(MPI_Comm comm, FILE *fd)
1994d71ae5a4SJacob Faibussowitsch {
1995156b51fbSBarry Smith   #if defined(PETSC_HAVE_DEVICE)
1996*2611ad71SToby Isaac 
1997156b51fbSBarry Smith   PetscFunctionBegin;
19983ba16761SJacob Faibussowitsch   if (!PetscLogGpuTimeFlag || petsc_gflops == 0) PetscFunctionReturn(PETSC_SUCCESS);
1999156b51fbSBarry Smith   PetscCall(PetscFPrintf(comm, fd, "\n\n"));
2000156b51fbSBarry Smith   PetscCall(PetscFPrintf(comm, fd, "      ##########################################################\n"));
2001156b51fbSBarry Smith   PetscCall(PetscFPrintf(comm, fd, "      #                                                        #\n"));
2002156b51fbSBarry Smith   PetscCall(PetscFPrintf(comm, fd, "      #                       WARNING!!!                       #\n"));
2003156b51fbSBarry Smith   PetscCall(PetscFPrintf(comm, fd, "      #                                                        #\n"));
2004156b51fbSBarry Smith   PetscCall(PetscFPrintf(comm, fd, "      #   This code was run with -log_view_gpu_time            #\n"));
2005156b51fbSBarry Smith   PetscCall(PetscFPrintf(comm, fd, "      #   This provides accurate timing within the GPU kernels #\n"));
2006156b51fbSBarry Smith   PetscCall(PetscFPrintf(comm, fd, "      #   but can slow down the entire computation by a        #\n"));
2007156b51fbSBarry Smith   PetscCall(PetscFPrintf(comm, fd, "      #   measurable amount. For fastest runs we recommend     #\n"));
2008156b51fbSBarry Smith   PetscCall(PetscFPrintf(comm, fd, "      #   not using this option.                               #\n"));
2009156b51fbSBarry Smith   PetscCall(PetscFPrintf(comm, fd, "      #                                                        #\n"));
2010156b51fbSBarry Smith   PetscCall(PetscFPrintf(comm, fd, "      ##########################################################\n\n\n"));
20113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2012156b51fbSBarry Smith   #else
201310450e9eSJacob Faibussowitsch   (void)comm;
201410450e9eSJacob Faibussowitsch   (void)fd;
20153ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
2016156b51fbSBarry Smith   #endif
2017156b51fbSBarry Smith }
2018156b51fbSBarry Smith 
201910450e9eSJacob Faibussowitsch static PetscErrorCode PetscLogView_Default(PetscViewer viewer)
2020d71ae5a4SJacob Faibussowitsch {
20215c6c1daeSBarry Smith   FILE               *fd;
20225c6c1daeSBarry Smith   PetscLogDouble      zero = 0.0;
20235c6c1daeSBarry Smith   PetscStageLog       stageLog;
20240298fd71SBarry Smith   PetscStageInfo     *stageInfo = NULL;
20250298fd71SBarry Smith   PetscEventPerfInfo *eventInfo = NULL;
20265c6c1daeSBarry Smith   PetscClassPerfInfo *classInfo;
20275c6c1daeSBarry Smith   char                arch[128], hostname[128], username[128], pname[PETSC_MAX_PATH_LEN], date[128];
20285c6c1daeSBarry Smith   const char         *name;
20295c6c1daeSBarry Smith   PetscLogDouble      locTotalTime, TotalTime, TotalFlops;
20305c6c1daeSBarry Smith   PetscLogDouble      numMessages, messageLength, avgMessLen, numReductions;
20315c6c1daeSBarry Smith   PetscLogDouble      stageTime, flops, flopr, mem, mess, messLen, red;
20325c6c1daeSBarry Smith   PetscLogDouble      fracTime, fracFlops, fracMessages, fracLength, fracReductions, fracMess, fracMessLen, fracRed;
20335c6c1daeSBarry Smith   PetscLogDouble      fracStageTime, fracStageFlops, fracStageMess, fracStageMessLen, fracStageRed;
20345c6c1daeSBarry Smith   PetscLogDouble      min, max, tot, ratio, avg, x, y;
2035e3ed9ee7SBarry Smith   PetscLogDouble      minf, maxf, totf, ratf, mint, maxt, tott, ratt, ratC, totm, totml, totr, mal, malmax, emalmax;
2036d9c66bfbSJunchao Zhang   #if defined(PETSC_HAVE_DEVICE)
2037156b51fbSBarry Smith   PetscLogEvent  KSP_Solve, SNES_Solve, TS_Step, TAO_Solve; /* These need to be fixed to be some events registered with certain objects */
2038958c4211Shannah_mairs   PetscLogDouble cct, gct, csz, gsz, gmaxt, gflops, gflopr, fracgflops;
2039bec0b493Shannah_mairs   #endif
2040217044c2SLisandro Dalcin   PetscMPIInt   minC, maxC;
20415c6c1daeSBarry Smith   PetscMPIInt   size, rank;
20425c6c1daeSBarry Smith   PetscBool    *localStageUsed, *stageUsed;
20435c6c1daeSBarry Smith   PetscBool    *localStageVisible, *stageVisible;
20445c6c1daeSBarry Smith   int           numStages, localNumEvents, numEvents;
204537b78d16SBarry Smith   int           stage, oclass;
20465c6c1daeSBarry Smith   PetscLogEvent event;
20475c6c1daeSBarry Smith   char          version[256];
20485c6c1daeSBarry Smith   MPI_Comm      comm;
2049156b51fbSBarry Smith   #if defined(PETSC_HAVE_DEVICE)
2050156b51fbSBarry Smith   PetscLogEvent eventid;
2051156b51fbSBarry Smith   PetscInt64    nas = 0x7FF0000000000002;
2052156b51fbSBarry Smith   #endif
20535c6c1daeSBarry Smith 
20545c6c1daeSBarry Smith   PetscFunctionBegin;
2055156b51fbSBarry Smith   PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
20569566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm));
20579566063dSJacob Faibussowitsch   PetscCall(PetscViewerASCIIGetPointer(viewer, &fd));
20589566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
20599566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
20605c6c1daeSBarry Smith   /* Get the total elapsed time */
20613ba16761SJacob Faibussowitsch   PetscCall(PetscTime(&locTotalTime));
20629371c9d4SSatish Balay   locTotalTime -= petsc_BaseTime;
20635c6c1daeSBarry Smith 
2064faad7b01SPierre Jolivet   PetscCall(PetscFPrintf(comm, fd, "****************************************************************************************************************************************************************\n"));
20659566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "***                                WIDEN YOUR WINDOW TO 160 CHARACTERS.  Use 'enscript -r -fCourier9' to print this document                                 ***\n"));
20669566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "****************************************************************************************************************************************************************\n"));
2067faad7b01SPierre Jolivet   PetscCall(PetscFPrintf(comm, fd, "\n------------------------------------------------------------------ PETSc Performance Summary: ------------------------------------------------------------------\n\n"));
20689566063dSJacob Faibussowitsch   PetscCall(PetscLogViewWarnSync(comm, fd));
20699566063dSJacob Faibussowitsch   PetscCall(PetscLogViewWarnDebugging(comm, fd));
20709566063dSJacob Faibussowitsch   PetscCall(PetscLogViewWarnNoGpuAwareMpi(comm, fd));
2071156b51fbSBarry Smith   PetscCall(PetscLogViewWarnGpuTime(comm, fd));
20729566063dSJacob Faibussowitsch   PetscCall(PetscGetArchType(arch, sizeof(arch)));
20739566063dSJacob Faibussowitsch   PetscCall(PetscGetHostName(hostname, sizeof(hostname)));
20749566063dSJacob Faibussowitsch   PetscCall(PetscGetUserName(username, sizeof(username)));
20759566063dSJacob Faibussowitsch   PetscCall(PetscGetProgramName(pname, sizeof(pname)));
20769566063dSJacob Faibussowitsch   PetscCall(PetscGetDate(date, sizeof(date)));
20779566063dSJacob Faibussowitsch   PetscCall(PetscGetVersion(version, sizeof(version)));
20785c6c1daeSBarry Smith   if (size == 1) {
20799566063dSJacob Faibussowitsch     PetscCall(PetscFPrintf(comm, fd, "%s on a %s named %s with %d processor, by %s %s\n", pname, arch, hostname, size, username, date));
20805c6c1daeSBarry Smith   } else {
20819566063dSJacob Faibussowitsch     PetscCall(PetscFPrintf(comm, fd, "%s on a %s named %s with %d processors, by %s %s\n", pname, arch, hostname, size, username, date));
20825c6c1daeSBarry Smith   }
2083f90b075cSBarry Smith   #if defined(PETSC_HAVE_OPENMP)
20849566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "Using %" PetscInt_FMT " OpenMP threads\n", PetscNumOMPThreads));
2085f90b075cSBarry Smith   #endif
20869566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "Using %s\n", version));
20875c6c1daeSBarry Smith 
20885c6c1daeSBarry Smith   /* Must preserve reduction count before we go on */
20895c6c1daeSBarry Smith   red = petsc_allreduce_ct + petsc_gather_ct + petsc_scatter_ct;
20905c6c1daeSBarry Smith 
20915c6c1daeSBarry Smith   /* Calculate summary information */
20929566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "\n                         Max       Max/Min     Avg       Total\n"));
20935c6c1daeSBarry Smith   /*   Time */
2094712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&locTotalTime, &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm));
2095712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&locTotalTime, &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm));
2096712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&locTotalTime, &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2097217044c2SLisandro Dalcin   avg = tot / ((PetscLogDouble)size);
20989371c9d4SSatish Balay   if (min != 0.0) ratio = max / min;
20999371c9d4SSatish Balay   else ratio = 0.0;
21009566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "Time (sec):           %5.3e   %7.3f   %5.3e\n", max, ratio, avg));
21015c6c1daeSBarry Smith   TotalTime = tot;
21025c6c1daeSBarry Smith   /*   Objects */
21035c6c1daeSBarry Smith   avg = (PetscLogDouble)petsc_numObjects;
2104712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&avg, &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm));
2105712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&avg, &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm));
2106712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&avg, &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2107217044c2SLisandro Dalcin   avg = tot / ((PetscLogDouble)size);
21089371c9d4SSatish Balay   if (min != 0.0) ratio = max / min;
21099371c9d4SSatish Balay   else ratio = 0.0;
21109566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "Objects:              %5.3e   %7.3f   %5.3e\n", max, ratio, avg));
21115c6c1daeSBarry Smith   /*   Flops */
2112712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&petsc_TotalFlops, &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm));
2113712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&petsc_TotalFlops, &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm));
2114712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&petsc_TotalFlops, &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2115217044c2SLisandro Dalcin   avg = tot / ((PetscLogDouble)size);
21169371c9d4SSatish Balay   if (min != 0.0) ratio = max / min;
21179371c9d4SSatish Balay   else ratio = 0.0;
21189566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "Flops:                %5.3e   %7.3f   %5.3e  %5.3e\n", max, ratio, avg, tot));
21195c6c1daeSBarry Smith   TotalFlops = tot;
21205c6c1daeSBarry Smith   /*   Flops/sec -- Must talk to Barry here */
21219371c9d4SSatish Balay   if (locTotalTime != 0.0) flops = petsc_TotalFlops / locTotalTime;
21229371c9d4SSatish Balay   else flops = 0.0;
2123712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&flops, &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm));
2124712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&flops, &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm));
2125712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&flops, &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2126217044c2SLisandro Dalcin   avg = tot / ((PetscLogDouble)size);
21279371c9d4SSatish Balay   if (min != 0.0) ratio = max / min;
21289371c9d4SSatish Balay   else ratio = 0.0;
21299566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "Flops/sec:            %5.3e   %7.3f   %5.3e  %5.3e\n", max, ratio, avg, tot));
21305c6c1daeSBarry Smith   /*   Memory */
21319566063dSJacob Faibussowitsch   PetscCall(PetscMallocGetMaximumUsage(&mem));
21325c6c1daeSBarry Smith   if (mem > 0.0) {
2133712fec58SPierre Jolivet     PetscCall(MPIU_Allreduce(&mem, &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm));
2134712fec58SPierre Jolivet     PetscCall(MPIU_Allreduce(&mem, &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm));
2135712fec58SPierre Jolivet     PetscCall(MPIU_Allreduce(&mem, &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2136217044c2SLisandro Dalcin     avg = tot / ((PetscLogDouble)size);
21379371c9d4SSatish Balay     if (min != 0.0) ratio = max / min;
21389371c9d4SSatish Balay     else ratio = 0.0;
21399566063dSJacob Faibussowitsch     PetscCall(PetscFPrintf(comm, fd, "Memory (bytes):       %5.3e   %7.3f   %5.3e  %5.3e\n", max, ratio, avg, tot));
21405c6c1daeSBarry Smith   }
21415c6c1daeSBarry Smith   /*   Messages */
21425c6c1daeSBarry Smith   mess = 0.5 * (petsc_irecv_ct + petsc_isend_ct + petsc_recv_ct + petsc_send_ct);
2143712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&mess, &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm));
2144712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&mess, &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm));
2145712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&mess, &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2146217044c2SLisandro Dalcin   avg = tot / ((PetscLogDouble)size);
21479371c9d4SSatish Balay   if (min != 0.0) ratio = max / min;
21489371c9d4SSatish Balay   else ratio = 0.0;
21499566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "MPI Msg Count:        %5.3e   %7.3f   %5.3e  %5.3e\n", max, ratio, avg, tot));
21505c6c1daeSBarry Smith   numMessages = tot;
21515c6c1daeSBarry Smith   /*   Message Lengths */
21525c6c1daeSBarry Smith   mess = 0.5 * (petsc_irecv_len + petsc_isend_len + petsc_recv_len + petsc_send_len);
2153712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&mess, &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm));
2154712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&mess, &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm));
2155712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&mess, &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
21569371c9d4SSatish Balay   if (numMessages != 0) avg = tot / numMessages;
21579371c9d4SSatish Balay   else avg = 0.0;
21589371c9d4SSatish Balay   if (min != 0.0) ratio = max / min;
21599371c9d4SSatish Balay   else ratio = 0.0;
21609566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "MPI Msg Len (bytes):  %5.3e   %7.3f   %5.3e  %5.3e\n", max, ratio, avg, tot));
21615c6c1daeSBarry Smith   messageLength = tot;
21625c6c1daeSBarry Smith   /*   Reductions */
2163712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&red, &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm));
2164712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&red, &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm));
2165712fec58SPierre Jolivet   PetscCall(MPIU_Allreduce(&red, &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
21669371c9d4SSatish Balay   if (min != 0.0) ratio = max / min;
21679371c9d4SSatish Balay   else ratio = 0.0;
21689566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "MPI Reductions:       %5.3e   %7.3f\n", max, ratio));
21695c6c1daeSBarry Smith   numReductions = red; /* wrong because uses count from process zero */
21709566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "\nFlop counting convention: 1 flop = 1 real number operation of type (multiply/divide/add/subtract)\n"));
21719566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "                            e.g., VecAXPY() for real vectors of length N --> 2N flops\n"));
21729566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "                            and VecAXPY() for complex vectors of length N --> 8N flops\n"));
21735c6c1daeSBarry Smith 
21745c6c1daeSBarry Smith   /* Get total number of stages --
21755c6c1daeSBarry Smith        Currently, a single processor can register more stages than another, but stages must all be registered in order.
21765c6c1daeSBarry Smith        We can removed this requirement if necessary by having a global stage numbering and indirection on the stage ID.
21775c6c1daeSBarry Smith        This seems best accomplished by assoicating a communicator with each stage.
21785c6c1daeSBarry Smith   */
21799566063dSJacob Faibussowitsch   PetscCall(PetscLogGetStageLog(&stageLog));
21809566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Allreduce(&stageLog->numStages, &numStages, 1, MPI_INT, MPI_MAX, comm));
21819566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numStages, &localStageUsed));
21829566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numStages, &stageUsed));
21839566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numStages, &localStageVisible));
21849566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(numStages, &stageVisible));
21855c6c1daeSBarry Smith   if (numStages > 0) {
21865c6c1daeSBarry Smith     stageInfo = stageLog->stageInfo;
21875c6c1daeSBarry Smith     for (stage = 0; stage < numStages; stage++) {
21885c6c1daeSBarry Smith       if (stage < stageLog->numStages) {
21895c6c1daeSBarry Smith         localStageUsed[stage]    = stageInfo[stage].used;
21905c6c1daeSBarry Smith         localStageVisible[stage] = stageInfo[stage].perfInfo.visible;
21915c6c1daeSBarry Smith       } else {
21925c6c1daeSBarry Smith         localStageUsed[stage]    = PETSC_FALSE;
21935c6c1daeSBarry Smith         localStageVisible[stage] = PETSC_TRUE;
21945c6c1daeSBarry Smith       }
21955c6c1daeSBarry Smith     }
2196712fec58SPierre Jolivet     PetscCall(MPIU_Allreduce(localStageUsed, stageUsed, numStages, MPIU_BOOL, MPI_LOR, comm));
2197712fec58SPierre Jolivet     PetscCall(MPIU_Allreduce(localStageVisible, stageVisible, numStages, MPIU_BOOL, MPI_LAND, comm));
21985c6c1daeSBarry Smith     for (stage = 0; stage < numStages; stage++) {
21995c6c1daeSBarry Smith       if (stageUsed[stage]) {
22009566063dSJacob Faibussowitsch         PetscCall(PetscFPrintf(comm, fd, "\nSummary of Stages:   ----- Time ------  ----- Flop ------  --- Messages ---  -- Message Lengths --  -- Reductions --\n"));
22019566063dSJacob Faibussowitsch         PetscCall(PetscFPrintf(comm, fd, "                        Avg     %%Total     Avg     %%Total    Count   %%Total     Avg         %%Total    Count   %%Total\n"));
22025c6c1daeSBarry Smith         break;
22035c6c1daeSBarry Smith       }
22045c6c1daeSBarry Smith     }
22055c6c1daeSBarry Smith     for (stage = 0; stage < numStages; stage++) {
22065c6c1daeSBarry Smith       if (!stageUsed[stage]) continue;
2207820f2d46SBarry Smith       /* CANNOT use MPI_Allreduce() since it might fail the line number check */
22085c6c1daeSBarry Smith       if (localStageUsed[stage]) {
2209712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&stageInfo[stage].perfInfo.time, &stageTime, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2210712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&stageInfo[stage].perfInfo.flops, &flops, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2211712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&stageInfo[stage].perfInfo.numMessages, &mess, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2212712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&stageInfo[stage].perfInfo.messageLength, &messLen, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2213712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&stageInfo[stage].perfInfo.numReductions, &red, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
22145c6c1daeSBarry Smith         name = stageInfo[stage].name;
22155c6c1daeSBarry Smith       } else {
2216712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&zero, &stageTime, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2217712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&zero, &flops, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2218712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&zero, &mess, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2219712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&zero, &messLen, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2220712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&zero, &red, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
22215c6c1daeSBarry Smith         name = "";
22225c6c1daeSBarry Smith       }
22239371c9d4SSatish Balay       mess *= 0.5;
22249371c9d4SSatish Balay       messLen *= 0.5;
22259371c9d4SSatish Balay       red /= size;
22269371c9d4SSatish Balay       if (TotalTime != 0.0) fracTime = stageTime / TotalTime;
22279371c9d4SSatish Balay       else fracTime = 0.0;
22289371c9d4SSatish Balay       if (TotalFlops != 0.0) fracFlops = flops / TotalFlops;
22299371c9d4SSatish Balay       else fracFlops = 0.0;
22305c6c1daeSBarry Smith       /* Talk to Barry if (stageTime     != 0.0) flops          = (size*flops)/stageTime; else flops          = 0.0; */
22319371c9d4SSatish Balay       if (numMessages != 0.0) fracMessages = mess / numMessages;
22329371c9d4SSatish Balay       else fracMessages = 0.0;
22339371c9d4SSatish Balay       if (mess != 0.0) avgMessLen = messLen / mess;
22349371c9d4SSatish Balay       else avgMessLen = 0.0;
22359371c9d4SSatish Balay       if (messageLength != 0.0) fracLength = messLen / messageLength;
22369371c9d4SSatish Balay       else fracLength = 0.0;
22379371c9d4SSatish Balay       if (numReductions != 0.0) fracReductions = red / numReductions;
22389371c9d4SSatish Balay       else fracReductions = 0.0;
22399371c9d4SSatish Balay       PetscCall(PetscFPrintf(comm, fd, "%2d: %15s: %6.4e %5.1f%%  %6.4e %5.1f%%  %5.3e %5.1f%%  %5.3e      %5.1f%%  %5.3e %5.1f%%\n", stage, name, stageTime / size, 100.0 * fracTime, flops, 100.0 * fracFlops, mess, 100.0 * fracMessages, avgMessLen, 100.0 * fracLength, red, 100.0 * fracReductions));
22405c6c1daeSBarry Smith     }
22415c6c1daeSBarry Smith   }
22425c6c1daeSBarry Smith 
22439566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "\n------------------------------------------------------------------------------------------------------------------------\n"));
22449566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "See the 'Profiling' chapter of the users' manual for details on interpreting output.\n"));
22459566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "Phase summary info:\n"));
22469566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "   Count: number of times phase was executed\n"));
22479566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "   Time and Flop: Max - maximum over all processors\n"));
22489566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "                  Ratio - ratio of maximum to minimum over all processors\n"));
22499566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "   Mess: number of messages sent\n"));
22509566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "   AvgLen: average message length (bytes)\n"));
22519566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "   Reduct: number of global reductions\n"));
22529566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "   Global: entire computation\n"));
22539566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "   Stage: stages of a computation. Set stages with PetscLogStagePush() and PetscLogStagePop().\n"));
22549566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "      %%T - percent time in this phase         %%F - percent flop in this phase\n"));
22559566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "      %%M - percent messages in this phase     %%L - percent message lengths in this phase\n"));
22569566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "      %%R - percent reductions in this phase\n"));
22579566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "   Total Mflop/s: 10e-6 * (sum of flop over all processors)/(max time over all processors)\n"));
2258e3ed9ee7SBarry Smith   if (PetscLogMemory) {
225992d1ba04SBarry Smith     PetscCall(PetscFPrintf(comm, fd, "   Memory usage is summed over all MPI processes, it is given in mega-bytes\n"));
226054aabf2dSBarry Smith     PetscCall(PetscFPrintf(comm, fd, "   Malloc Mbytes: Memory allocated and kept during event (sum over all calls to event). May be negative\n"));
226154aabf2dSBarry Smith     PetscCall(PetscFPrintf(comm, fd, "   EMalloc Mbytes: extra memory allocated during event and then freed (maximum over all calls to events). Never negative\n"));
226254aabf2dSBarry Smith     PetscCall(PetscFPrintf(comm, fd, "   MMalloc Mbytes: Increase in high water mark of allocated memory (sum over all calls to event). Never negative\n"));
22639566063dSJacob Faibussowitsch     PetscCall(PetscFPrintf(comm, fd, "   RMI Mbytes: Increase in resident memory (sum over all calls to event)\n"));
2264e3ed9ee7SBarry Smith   }
2265d9c66bfbSJunchao Zhang   #if defined(PETSC_HAVE_DEVICE)
22669566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "   GPU Mflop/s: 10e-6 * (sum of flop on GPU over all processors)/(max GPU time over all processors)\n"));
22679566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "   CpuToGpu Count: total number of CPU to GPU copies per processor\n"));
22689566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "   CpuToGpu Size (Mbytes): 10e-6 * (total size of CPU to GPU copies per processor)\n"));
22699566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "   GpuToCpu Count: total number of GPU to CPU copies per processor\n"));
22709566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "   GpuToCpu Size (Mbytes): 10e-6 * (total size of GPU to CPU copies per processor)\n"));
22719566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "   GPU %%F: percent flops on GPU in this event\n"));
2272bec0b493Shannah_mairs   #endif
22739566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "------------------------------------------------------------------------------------------------------------------------\n"));
22745c6c1daeSBarry Smith 
22759566063dSJacob Faibussowitsch   PetscCall(PetscLogViewWarnDebugging(comm, fd));
22765c6c1daeSBarry Smith 
22775c6c1daeSBarry Smith   /* Report events */
22789566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "Event                Count      Time (sec)     Flop                              --- Global ---  --- Stage ----  Total"));
227948a46eb9SPierre Jolivet   if (PetscLogMemory) PetscCall(PetscFPrintf(comm, fd, "  Malloc EMalloc MMalloc RMI"));
2280d9c66bfbSJunchao Zhang   #if defined(PETSC_HAVE_DEVICE)
22819566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "   GPU    - CpuToGpu -   - GpuToCpu - GPU"));
2282bec0b493Shannah_mairs   #endif
22839566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "\n"));
22849566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "                   Max Ratio  Max     Ratio   Max  Ratio  Mess   AvgLen  Reduct  %%T %%F %%M %%L %%R  %%T %%F %%M %%L %%R Mflop/s"));
228548a46eb9SPierre Jolivet   if (PetscLogMemory) PetscCall(PetscFPrintf(comm, fd, " Mbytes Mbytes Mbytes Mbytes"));
2286d9c66bfbSJunchao Zhang   #if defined(PETSC_HAVE_DEVICE)
22879566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, " Mflop/s Count   Size   Count   Size  %%F"));
2288bec0b493Shannah_mairs   #endif
22899566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "\n"));
22909566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "------------------------------------------------------------------------------------------------------------------------"));
229148a46eb9SPierre Jolivet   if (PetscLogMemory) PetscCall(PetscFPrintf(comm, fd, "-----------------------------"));
2292d9c66bfbSJunchao Zhang   #if defined(PETSC_HAVE_DEVICE)
22939566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "---------------------------------------"));
22944863603aSSatish Balay   #endif
22959566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "\n"));
22965c6c1daeSBarry Smith 
2297156b51fbSBarry Smith   #if defined(PETSC_HAVE_DEVICE)
2298156b51fbSBarry Smith   /* this indirect way of accessing these values is needed when PETSc is build with multiple libraries since the symbols are not in libpetscsys */
2299156b51fbSBarry Smith   PetscCall(PetscEventRegLogGetEvent(stageLog->eventLog, "TAOSolve", &TAO_Solve));
2300156b51fbSBarry Smith   PetscCall(PetscEventRegLogGetEvent(stageLog->eventLog, "TSStep", &TS_Step));
2301156b51fbSBarry Smith   PetscCall(PetscEventRegLogGetEvent(stageLog->eventLog, "SNESSolve", &SNES_Solve));
2302156b51fbSBarry Smith   PetscCall(PetscEventRegLogGetEvent(stageLog->eventLog, "KSPSolve", &KSP_Solve));
2303156b51fbSBarry Smith   #endif
2304156b51fbSBarry Smith 
23055c6c1daeSBarry Smith   /* Problem: The stage name will not show up unless the stage executed on proc 1 */
23065c6c1daeSBarry Smith   for (stage = 0; stage < numStages; stage++) {
23075c6c1daeSBarry Smith     if (!stageVisible[stage]) continue;
2308820f2d46SBarry Smith     /* CANNOT use MPI_Allreduce() since it might fail the line number check */
23095c6c1daeSBarry Smith     if (localStageUsed[stage]) {
23109566063dSJacob Faibussowitsch       PetscCall(PetscFPrintf(comm, fd, "\n--- Event Stage %d: %s\n\n", stage, stageInfo[stage].name));
2311712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&stageInfo[stage].perfInfo.time, &stageTime, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2312712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&stageInfo[stage].perfInfo.flops, &flops, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2313712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&stageInfo[stage].perfInfo.numMessages, &mess, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2314712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&stageInfo[stage].perfInfo.messageLength, &messLen, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2315712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&stageInfo[stage].perfInfo.numReductions, &red, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
23165c6c1daeSBarry Smith     } else {
23179566063dSJacob Faibussowitsch       PetscCall(PetscFPrintf(comm, fd, "\n--- Event Stage %d: Unknown\n\n", stage));
2318712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&zero, &stageTime, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2319712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&zero, &flops, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2320712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&zero, &mess, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2321712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&zero, &messLen, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2322712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(&zero, &red, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
23235c6c1daeSBarry Smith     }
23249371c9d4SSatish Balay     mess *= 0.5;
23259371c9d4SSatish Balay     messLen *= 0.5;
23269371c9d4SSatish Balay     red /= size;
23275c6c1daeSBarry Smith 
23285c6c1daeSBarry Smith     /* Get total number of events in this stage --
23295c6c1daeSBarry Smith        Currently, a single processor can register more events than another, but events must all be registered in order,
23305c6c1daeSBarry Smith        just like stages. We can removed this requirement if necessary by having a global event numbering and indirection
2331217044c2SLisandro Dalcin        on the event ID. This seems best accomplished by associating a communicator with each stage.
23325c6c1daeSBarry Smith 
23335c6c1daeSBarry Smith        Problem: If the event did not happen on proc 1, its name will not be available.
23345c6c1daeSBarry Smith        Problem: Event visibility is not implemented
23355c6c1daeSBarry Smith     */
23365c6c1daeSBarry Smith     if (localStageUsed[stage]) {
23375c6c1daeSBarry Smith       eventInfo      = stageLog->stageInfo[stage].eventLog->eventInfo;
23385c6c1daeSBarry Smith       localNumEvents = stageLog->stageInfo[stage].eventLog->numEvents;
2339a297a907SKarl Rupp     } else localNumEvents = 0;
23409566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce(&localNumEvents, &numEvents, 1, MPI_INT, MPI_MAX, comm));
23415c6c1daeSBarry Smith     for (event = 0; event < numEvents; event++) {
2342820f2d46SBarry Smith       /* CANNOT use MPI_Allreduce() since it might fail the line number check */
23435c6c1daeSBarry Smith       if (localStageUsed[stage] && (event < stageLog->stageInfo[stage].eventLog->numEvents) && (eventInfo[event].depth == 0)) {
23449371c9d4SSatish Balay         if ((eventInfo[event].count > 0) && (eventInfo[event].time > 0.0)) flopr = eventInfo[event].flops;
23459371c9d4SSatish Balay         else flopr = 0.0;
2346712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&flopr, &minf, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm));
2347712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&flopr, &maxf, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm));
2348712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&eventInfo[event].flops, &totf, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2349712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&eventInfo[event].time, &mint, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm));
2350712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&eventInfo[event].time, &maxt, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm));
2351712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&eventInfo[event].time, &tott, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2352712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&eventInfo[event].numMessages, &totm, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2353712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&eventInfo[event].messageLength, &totml, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2354712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&eventInfo[event].numReductions, &totr, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
23559566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Allreduce(&eventInfo[event].count, &minC, 1, MPI_INT, MPI_MIN, comm));
23569566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Allreduce(&eventInfo[event].count, &maxC, 1, MPI_INT, MPI_MAX, comm));
2357e3ed9ee7SBarry Smith         if (PetscLogMemory) {
2358712fec58SPierre Jolivet           PetscCall(MPIU_Allreduce(&eventInfo[event].memIncrease, &mem, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2359712fec58SPierre Jolivet           PetscCall(MPIU_Allreduce(&eventInfo[event].mallocSpace, &mal, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2360712fec58SPierre Jolivet           PetscCall(MPIU_Allreduce(&eventInfo[event].mallocIncrease, &malmax, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2361712fec58SPierre Jolivet           PetscCall(MPIU_Allreduce(&eventInfo[event].mallocIncreaseEvent, &emalmax, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2362e3ed9ee7SBarry Smith         }
2363d9c66bfbSJunchao Zhang   #if defined(PETSC_HAVE_DEVICE)
2364712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&eventInfo[event].CpuToGpuCount, &cct, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2365712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&eventInfo[event].GpuToCpuCount, &gct, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2366712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&eventInfo[event].CpuToGpuSize, &csz, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2367712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&eventInfo[event].GpuToCpuSize, &gsz, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2368712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&eventInfo[event].GpuFlops, &gflops, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2369712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&eventInfo[event].GpuTime, &gmaxt, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm));
2370bec0b493Shannah_mairs   #endif
23715c6c1daeSBarry Smith         name = stageLog->eventLog->eventInfo[event].name;
23725c6c1daeSBarry Smith       } else {
23733ba16761SJacob Faibussowitsch         int ierr = 0;
23743ba16761SJacob Faibussowitsch 
23755c6c1daeSBarry Smith         flopr = 0.0;
2376712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&flopr, &minf, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm));
2377712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&flopr, &maxf, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm));
2378712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&zero, &totf, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2379712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&zero, &mint, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm));
2380712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&zero, &maxt, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm));
2381712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&zero, &tott, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2382712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&zero, &totm, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2383712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&zero, &totml, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2384712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&zero, &totr, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
23859566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Allreduce(&ierr, &minC, 1, MPI_INT, MPI_MIN, comm));
23869566063dSJacob Faibussowitsch         PetscCallMPI(MPI_Allreduce(&ierr, &maxC, 1, MPI_INT, MPI_MAX, comm));
2387e3ed9ee7SBarry Smith         if (PetscLogMemory) {
2388712fec58SPierre Jolivet           PetscCall(MPIU_Allreduce(&zero, &mem, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2389712fec58SPierre Jolivet           PetscCall(MPIU_Allreduce(&zero, &mal, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2390712fec58SPierre Jolivet           PetscCall(MPIU_Allreduce(&zero, &malmax, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2391712fec58SPierre Jolivet           PetscCall(MPIU_Allreduce(&zero, &emalmax, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2392e3ed9ee7SBarry Smith         }
2393d9c66bfbSJunchao Zhang   #if defined(PETSC_HAVE_DEVICE)
2394712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&zero, &cct, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2395712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&zero, &gct, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2396712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&zero, &csz, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2397712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&zero, &gsz, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2398712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&zero, &gflops, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
2399712fec58SPierre Jolivet         PetscCall(MPIU_Allreduce(&zero, &gmaxt, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm));
2400bec0b493Shannah_mairs   #endif
24015c6c1daeSBarry Smith         name = "";
24025c6c1daeSBarry Smith       }
24035c6c1daeSBarry Smith       if (mint < 0.0) {
2404d0609cedSBarry Smith         PetscCall(PetscFPrintf(comm, fd, "WARNING!!! Minimum time %g over all processors for %s is negative! This happens\n on some machines whose times cannot handle too rapid calls.!\n artificially changing minimum to zero.\n", mint, name));
24055c6c1daeSBarry Smith         mint = 0;
24065c6c1daeSBarry Smith       }
240708401ef6SPierre Jolivet       PetscCheck(minf >= 0.0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Minimum flop %g over all processors for %s is negative! Not possible!", minf, name);
2408156b51fbSBarry Smith   /* Put NaN into the time for all events that may not be time accurately since they may happen asynchronously on the GPU */
2409156b51fbSBarry Smith   #if defined(PETSC_HAVE_DEVICE)
2410156b51fbSBarry Smith       if (!PetscLogGpuTimeFlag && petsc_gflops > 0) {
2411156b51fbSBarry Smith         memcpy(&gmaxt, &nas, sizeof(PetscLogDouble));
2412156b51fbSBarry Smith         PetscCall(PetscEventRegLogGetEvent(stageLog->eventLog, name, &eventid));
2413156b51fbSBarry Smith         if (eventid != SNES_Solve && eventid != KSP_Solve && eventid != TS_Step && eventid != TAO_Solve) {
2414156b51fbSBarry Smith           memcpy(&mint, &nas, sizeof(PetscLogDouble));
2415156b51fbSBarry Smith           memcpy(&maxt, &nas, sizeof(PetscLogDouble));
2416156b51fbSBarry Smith         }
2417156b51fbSBarry Smith       }
2418156b51fbSBarry Smith   #endif
24199371c9d4SSatish Balay       totm *= 0.5;
24209371c9d4SSatish Balay       totml *= 0.5;
24219371c9d4SSatish Balay       totr /= size;
24225c6c1daeSBarry Smith 
2423217044c2SLisandro Dalcin       if (maxC != 0) {
24249371c9d4SSatish Balay         if (minC != 0) ratC = ((PetscLogDouble)maxC) / minC;
24259371c9d4SSatish Balay         else ratC = 0.0;
24269371c9d4SSatish Balay         if (mint != 0.0) ratt = maxt / mint;
24279371c9d4SSatish Balay         else ratt = 0.0;
24289371c9d4SSatish Balay         if (minf != 0.0) ratf = maxf / minf;
24299371c9d4SSatish Balay         else ratf = 0.0;
24309371c9d4SSatish Balay         if (TotalTime != 0.0) fracTime = tott / TotalTime;
24319371c9d4SSatish Balay         else fracTime = 0.0;
24329371c9d4SSatish Balay         if (TotalFlops != 0.0) fracFlops = totf / TotalFlops;
24339371c9d4SSatish Balay         else fracFlops = 0.0;
24349371c9d4SSatish Balay         if (stageTime != 0.0) fracStageTime = tott / stageTime;
24359371c9d4SSatish Balay         else fracStageTime = 0.0;
24369371c9d4SSatish Balay         if (flops != 0.0) fracStageFlops = totf / flops;
24379371c9d4SSatish Balay         else fracStageFlops = 0.0;
24389371c9d4SSatish Balay         if (numMessages != 0.0) fracMess = totm / numMessages;
24399371c9d4SSatish Balay         else fracMess = 0.0;
24409371c9d4SSatish Balay         if (messageLength != 0.0) fracMessLen = totml / messageLength;
24419371c9d4SSatish Balay         else fracMessLen = 0.0;
24429371c9d4SSatish Balay         if (numReductions != 0.0) fracRed = totr / numReductions;
24439371c9d4SSatish Balay         else fracRed = 0.0;
24449371c9d4SSatish Balay         if (mess != 0.0) fracStageMess = totm / mess;
24459371c9d4SSatish Balay         else fracStageMess = 0.0;
24469371c9d4SSatish Balay         if (messLen != 0.0) fracStageMessLen = totml / messLen;
24479371c9d4SSatish Balay         else fracStageMessLen = 0.0;
24489371c9d4SSatish Balay         if (red != 0.0) fracStageRed = totr / red;
24499371c9d4SSatish Balay         else fracStageRed = 0.0;
24509371c9d4SSatish Balay         if (totm != 0.0) totml /= totm;
24519371c9d4SSatish Balay         else totml = 0.0;
24529371c9d4SSatish Balay         if (maxt != 0.0) flopr = totf / maxt;
24539371c9d4SSatish Balay         else flopr = 0.0;
245468a21331SBarry Smith         if (fracStageTime > 1.0 || fracStageFlops > 1.0 || fracStageMess > 1.0 || fracStageMessLen > 1.0 || fracStageRed > 1.0)
2455a76edefcSRicardo Jesus           PetscCall(PetscFPrintf(comm, fd, "%-16s %7d %3.1f %5.4e %3.1f %3.2e %3.1f %2.1e %2.1e %2.1e %2.0f %2.0f %2.0f %2.0f %2.0f Multiple stages %5.0f", name, maxC, ratC, maxt, ratt, maxf, ratf, totm, totml, totr, 100.0 * fracTime, 100.0 * fracFlops, 100.0 * fracMess, 100.0 * fracMessLen, 100.0 * fracRed, PetscAbs(flopr) / 1.0e6));
245668a21331SBarry Smith         else
2457a76edefcSRicardo Jesus           PetscCall(PetscFPrintf(comm, fd, "%-16s %7d %3.1f %5.4e %3.1f %3.2e %3.1f %2.1e %2.1e %2.1e %2.0f %2.0f %2.0f %2.0f %2.0f %3.0f %2.0f %2.0f %2.0f %2.0f %5.0f", name, maxC, ratC, maxt, ratt, maxf, ratf, totm, totml, totr, 100.0 * fracTime, 100.0 * fracFlops, 100.0 * fracMess, 100.0 * fracMessLen, 100.0 * fracRed, 100.0 * fracStageTime, 100.0 * fracStageFlops, 100.0 * fracStageMess, 100.0 * fracStageMessLen, 100.0 * fracStageRed, PetscAbs(flopr) / 1.0e6));
245848a46eb9SPierre Jolivet         if (PetscLogMemory) PetscCall(PetscFPrintf(comm, fd, " %5.0f   %5.0f   %5.0f   %5.0f", mal / 1.0e6, emalmax / 1.0e6, malmax / 1.0e6, mem / 1.0e6));
2459d9c66bfbSJunchao Zhang   #if defined(PETSC_HAVE_DEVICE)
24609371c9d4SSatish Balay         if (totf != 0.0) fracgflops = gflops / totf;
24619371c9d4SSatish Balay         else fracgflops = 0.0;
24629371c9d4SSatish Balay         if (gmaxt != 0.0) gflopr = gflops / gmaxt;
24639371c9d4SSatish Balay         else gflopr = 0.0;
2464a76edefcSRicardo Jesus         PetscCall(PetscFPrintf(comm, fd, "   %5.0f   %4.0f %3.2e %4.0f %3.2e % 2.0f", PetscAbs(gflopr) / 1.0e6, cct / size, csz / (1.0e6 * size), gct / size, gsz / (1.0e6 * size), 100.0 * fracgflops));
2465bec0b493Shannah_mairs   #endif
24669566063dSJacob Faibussowitsch         PetscCall(PetscFPrintf(comm, fd, "\n"));
24675c6c1daeSBarry Smith       }
24685c6c1daeSBarry Smith     }
24695c6c1daeSBarry Smith   }
24705c6c1daeSBarry Smith 
24715c6c1daeSBarry Smith   /* Memory usage and object creation */
24729566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "------------------------------------------------------------------------------------------------------------------------"));
247348a46eb9SPierre Jolivet   if (PetscLogMemory) PetscCall(PetscFPrintf(comm, fd, "-----------------------------"));
2474d9c66bfbSJunchao Zhang   #if defined(PETSC_HAVE_DEVICE)
24759566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "---------------------------------------"));
24764863603aSSatish Balay   #endif
24779566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "\n"));
24789566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "\n"));
24795c6c1daeSBarry Smith 
24805c6c1daeSBarry Smith   /* Right now, only stages on the first processor are reported here, meaning only objects associated with
24815c6c1daeSBarry Smith      the global communicator, or MPI_COMM_SELF for proc 1. We really should report global stats and then
24825c6c1daeSBarry Smith      stats for stages local to processor sets.
24835c6c1daeSBarry Smith   */
24845c6c1daeSBarry Smith   /* We should figure out the longest object name here (now 20 characters) */
24854851f57dSBarry Smith   PetscCall(PetscFPrintf(comm, fd, "Object Type          Creations   Destructions. Reports information only for process 0.\n"));
24865c6c1daeSBarry Smith   for (stage = 0; stage < numStages; stage++) {
24875c6c1daeSBarry Smith     if (localStageUsed[stage]) {
24885c6c1daeSBarry Smith       classInfo = stageLog->stageInfo[stage].classLog->classInfo;
24899566063dSJacob Faibussowitsch       PetscCall(PetscFPrintf(comm, fd, "\n--- Event Stage %d: %s\n\n", stage, stageInfo[stage].name));
24905c6c1daeSBarry Smith       for (oclass = 0; oclass < stageLog->stageInfo[stage].classLog->numClasses; oclass++) {
24915c6c1daeSBarry Smith         if ((classInfo[oclass].creations > 0) || (classInfo[oclass].destructions > 0)) {
24924851f57dSBarry Smith           PetscCall(PetscFPrintf(comm, fd, "%20s %5d          %5d\n", stageLog->classLog->classInfo[oclass].name, classInfo[oclass].creations, classInfo[oclass].destructions));
24935c6c1daeSBarry Smith         }
24945c6c1daeSBarry Smith       }
24955c6c1daeSBarry Smith     } else {
2496cf019ec6SStefano Zampini       if (!localStageVisible[stage]) continue;
24979566063dSJacob Faibussowitsch       PetscCall(PetscFPrintf(comm, fd, "\n--- Event Stage %d: Unknown\n\n", stage));
24985c6c1daeSBarry Smith     }
24995c6c1daeSBarry Smith   }
25005c6c1daeSBarry Smith 
25019566063dSJacob Faibussowitsch   PetscCall(PetscFree(localStageUsed));
25029566063dSJacob Faibussowitsch   PetscCall(PetscFree(stageUsed));
25039566063dSJacob Faibussowitsch   PetscCall(PetscFree(localStageVisible));
25049566063dSJacob Faibussowitsch   PetscCall(PetscFree(stageVisible));
25055c6c1daeSBarry Smith 
25065c6c1daeSBarry Smith   /* Information unrelated to this particular run */
25079566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "========================================================================================================================\n"));
25083ba16761SJacob Faibussowitsch   PetscCall(PetscTime(&y));
25093ba16761SJacob Faibussowitsch   PetscCall(PetscTime(&x));
25103ba16761SJacob Faibussowitsch   PetscCall(PetscTime(&y));
25113ba16761SJacob Faibussowitsch   PetscCall(PetscTime(&y));
25123ba16761SJacob Faibussowitsch   PetscCall(PetscTime(&y));
25133ba16761SJacob Faibussowitsch   PetscCall(PetscTime(&y));
25143ba16761SJacob Faibussowitsch   PetscCall(PetscTime(&y));
25153ba16761SJacob Faibussowitsch   PetscCall(PetscTime(&y));
25163ba16761SJacob Faibussowitsch   PetscCall(PetscTime(&y));
25173ba16761SJacob Faibussowitsch   PetscCall(PetscTime(&y));
25183ba16761SJacob Faibussowitsch   PetscCall(PetscTime(&y));
25193ba16761SJacob Faibussowitsch   PetscCall(PetscTime(&y));
25209566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "Average time to get PetscTime(): %g\n", (y - x) / 10.0));
25215c6c1daeSBarry Smith   /* MPI information */
25225c6c1daeSBarry Smith   if (size > 1) {
25235c6c1daeSBarry Smith     MPI_Status  status;
25245c6c1daeSBarry Smith     PetscMPIInt tag;
25255c6c1daeSBarry Smith     MPI_Comm    newcomm;
25265c6c1daeSBarry Smith 
25279566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Barrier(comm));
25283ba16761SJacob Faibussowitsch     PetscCall(PetscTime(&x));
25299566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Barrier(comm));
25309566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Barrier(comm));
25319566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Barrier(comm));
25329566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Barrier(comm));
25339566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Barrier(comm));
25343ba16761SJacob Faibussowitsch     PetscCall(PetscTime(&y));
25359566063dSJacob Faibussowitsch     PetscCall(PetscFPrintf(comm, fd, "Average time for MPI_Barrier(): %g\n", (y - x) / 5.0));
25369566063dSJacob Faibussowitsch     PetscCall(PetscCommDuplicate(comm, &newcomm, &tag));
25379566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Barrier(comm));
25385c6c1daeSBarry Smith     if (rank) {
25399566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Recv(NULL, 0, MPI_INT, rank - 1, tag, newcomm, &status));
25409566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Send(NULL, 0, MPI_INT, (rank + 1) % size, tag, newcomm));
25415c6c1daeSBarry Smith     } else {
25423ba16761SJacob Faibussowitsch       PetscCall(PetscTime(&x));
25439566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Send(NULL, 0, MPI_INT, 1, tag, newcomm));
25449566063dSJacob Faibussowitsch       PetscCallMPI(MPI_Recv(NULL, 0, MPI_INT, size - 1, tag, newcomm, &status));
25453ba16761SJacob Faibussowitsch       PetscCall(PetscTime(&y));
25469566063dSJacob Faibussowitsch       PetscCall(PetscFPrintf(comm, fd, "Average time for zero size MPI_Send(): %g\n", (y - x) / size));
25475c6c1daeSBarry Smith     }
25489566063dSJacob Faibussowitsch     PetscCall(PetscCommDestroy(&newcomm));
25495c6c1daeSBarry Smith   }
25509566063dSJacob Faibussowitsch   PetscCall(PetscOptionsView(NULL, viewer));
25515c6c1daeSBarry Smith 
25525c6c1daeSBarry Smith   /* Machine and compile information */
25535c6c1daeSBarry Smith   #if defined(PETSC_USE_FORTRAN_KERNELS)
25549566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "Compiled with FORTRAN kernels\n"));
25555c6c1daeSBarry Smith   #else
25569566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "Compiled without FORTRAN kernels\n"));
25575c6c1daeSBarry Smith   #endif
2558217044c2SLisandro Dalcin   #if defined(PETSC_USE_64BIT_INDICES)
25597de69702SBarry Smith   PetscCall(PetscFPrintf(comm, fd, "Compiled with 64-bit PetscInt\n"));
2560217044c2SLisandro Dalcin   #elif defined(PETSC_USE___FLOAT128)
25617de69702SBarry Smith   PetscCall(PetscFPrintf(comm, fd, "Compiled with 32-bit PetscInt\n"));
2562217044c2SLisandro Dalcin   #endif
25635c6c1daeSBarry Smith   #if defined(PETSC_USE_REAL_SINGLE)
25649566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "Compiled with single precision PetscScalar and PetscReal\n"));
25655f27b2e0SBarry Smith   #elif defined(PETSC_USE___FLOAT128)
25669566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "Compiled with 128 bit precision PetscScalar and PetscReal\n"));
25675c6c1daeSBarry Smith   #endif
25685c6c1daeSBarry Smith   #if defined(PETSC_USE_REAL_MAT_SINGLE)
25699566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "Compiled with single precision matrices\n"));
25705c6c1daeSBarry Smith   #else
25719566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "Compiled with full precision matrices (default)\n"));
25725c6c1daeSBarry Smith   #endif
25739371c9d4SSatish Balay   PetscCall(PetscFPrintf(comm, fd, "sizeof(short) %d sizeof(int) %d sizeof(long) %d sizeof(void*) %d sizeof(PetscScalar) %d sizeof(PetscInt) %d\n", (int)sizeof(short), (int)sizeof(int), (int)sizeof(long), (int)sizeof(void *), (int)sizeof(PetscScalar), (int)sizeof(PetscInt)));
25745c6c1daeSBarry Smith 
25759566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "Configure options: %s", petscconfigureoptions));
25769566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "%s", petscmachineinfo));
25779566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "%s", petsccompilerinfo));
25789566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "%s", petsccompilerflagsinfo));
25799566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "%s", petsclinkerinfo));
25805c6c1daeSBarry Smith 
25815c6c1daeSBarry Smith   /* Cleanup */
25829566063dSJacob Faibussowitsch   PetscCall(PetscFPrintf(comm, fd, "\n"));
25839566063dSJacob Faibussowitsch   PetscCall(PetscLogViewWarnNoGpuAwareMpi(comm, fd));
25849566063dSJacob Faibussowitsch   PetscCall(PetscLogViewWarnDebugging(comm, fd));
2585156b51fbSBarry Smith   PetscCall(PetscFPTrapPop());
25863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
25875c6c1daeSBarry Smith }
25885c6c1daeSBarry Smith 
25897d6c928cSSatish Balay /*@C
25907d6c928cSSatish Balay   PetscLogView - Prints a summary of the logging.
25915c6c1daeSBarry Smith 
25925c6c1daeSBarry Smith   Collective over MPI_Comm
25935c6c1daeSBarry Smith 
25945c6c1daeSBarry Smith   Input Parameter:
2595f14045dbSBarry Smith . viewer - an ASCII viewer
25965c6c1daeSBarry Smith 
25975c6c1daeSBarry Smith   Options Database Keys:
2598bb1d7374SBarry Smith + -log_view [:filename]                    - Prints summary of log information
2599bb1d7374SBarry Smith . -log_view :filename.py:ascii_info_detail - Saves logging information from each process as a Python file
2600607d249eSBarry Smith . -log_view :filename.xml:ascii_xml        - Saves a summary of the logging information in a nested format (see below for how to view it)
2601d0a29bd7SConnor Ward . -log_view :filename.txt:ascii_flamegraph - Saves logging information in a format suitable for visualising as a Flame Graph (see below for how to view it)
2602156b51fbSBarry Smith . -log_view_memory                         - Also display memory usage in each event
2603156b51fbSBarry Smith . -log_view_gpu_time                       - Also display time in each event for GPU kernels (Note this may slow the computation)
2604811af0c4SBarry Smith . -log_all                                 - Saves a file Log.rank for each MPI rank with details of each step of the computation
2605bb1d7374SBarry Smith - -log_trace [filename]                    - Displays a trace of what each process is doing
26065c6c1daeSBarry Smith 
2607d1f92df0SBarry Smith   Level: beginner
2608d1f92df0SBarry Smith 
26095c6c1daeSBarry Smith   Notes:
2610da81f932SPierre Jolivet   It is possible to control the logging programmatically but we recommend using the options database approach whenever possible
26115c6c1daeSBarry Smith   By default the summary is printed to stdout.
26125c6c1daeSBarry Smith 
2613bb1d7374SBarry Smith   Before calling this routine you must have called either PetscLogDefaultBegin() or PetscLogNestedBegin()
2614bb1d7374SBarry Smith 
2615bb1d7374SBarry Smith   If PETSc is configured with --with-logging=0 then this functionality is not available
2616bb1d7374SBarry Smith 
2617607d249eSBarry Smith   To view the nested XML format filename.xml first copy  ${PETSC_DIR}/share/petsc/xml/performance_xml2html.xsl to the current
2618607d249eSBarry Smith   directory then open filename.xml with your browser. Specific notes for certain browsers
2619607d249eSBarry Smith $    Firefox and Internet explorer - simply open the file
2620607d249eSBarry Smith $    Google Chrome - you must start up Chrome with the option --allow-file-access-from-files
2621a8d69d7bSBarry Smith $    Safari - see https://ccm.net/faq/36342-safari-how-to-enable-local-file-access
2622607d249eSBarry Smith   or one can use the package http://xmlsoft.org/XSLT/xsltproc2.html to translate the xml file to html and then open it with
2623607d249eSBarry Smith   your browser.
26242add09c0SLisandro Dalcin   Alternatively, use the script ${PETSC_DIR}/lib/petsc/bin/petsc-performance-view to automatically open a new browser
26252add09c0SLisandro Dalcin   window and render the XML log file contents.
2626607d249eSBarry Smith 
2627bb1d7374SBarry Smith   The nested XML format was kindly donated by Koos Huijssen and Christiaan M. Klaij  MARITIME  RESEARCH  INSTITUTE  NETHERLANDS
2628bb1d7374SBarry Smith 
2629d0a29bd7SConnor Ward   The Flame Graph output can be visualised using either the original Flame Graph script (https://github.com/brendangregg/FlameGraph)
2630d0a29bd7SConnor Ward   or using speedscope (https://www.speedscope.app).
2631d0a29bd7SConnor Ward   Old XML profiles may be converted into this format using the script ${PETSC_DIR}/lib/petsc/bin/xml2flamegraph.py.
2632d0a29bd7SConnor Ward 
2633d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogDefaultBegin()`, `PetscLogDump()`
26345c6c1daeSBarry Smith @*/
2635d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogView(PetscViewer viewer)
2636d71ae5a4SJacob Faibussowitsch {
2637f14045dbSBarry Smith   PetscBool         isascii;
2638f14045dbSBarry Smith   PetscViewerFormat format;
263937b78d16SBarry Smith   int               stage, lastStage;
264037b78d16SBarry Smith   PetscStageLog     stageLog;
26415c6c1daeSBarry Smith 
26425c6c1daeSBarry Smith   PetscFunctionBegin;
264328b400f6SJacob Faibussowitsch   PetscCheck(PetscLogPLB, PETSC_COMM_SELF, PETSC_ERR_SUP, "Must use -log_view or PetscLogDefaultBegin() before calling this routine");
264437b78d16SBarry Smith   /* Pop off any stages the user forgot to remove */
264537b78d16SBarry Smith   lastStage = 0;
26469566063dSJacob Faibussowitsch   PetscCall(PetscLogGetStageLog(&stageLog));
26479566063dSJacob Faibussowitsch   PetscCall(PetscStageLogGetCurrent(stageLog, &stage));
264837b78d16SBarry Smith   while (stage >= 0) {
264937b78d16SBarry Smith     lastStage = stage;
26509566063dSJacob Faibussowitsch     PetscCall(PetscStageLogPop(stageLog));
26519566063dSJacob Faibussowitsch     PetscCall(PetscStageLogGetCurrent(stageLog, &stage));
265237b78d16SBarry Smith   }
26539566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
265428b400f6SJacob Faibussowitsch   PetscCheck(isascii, PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Currently can only view logging to ASCII");
26559566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(viewer, &format));
2656f14045dbSBarry Smith   if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO) {
26579566063dSJacob Faibussowitsch     PetscCall(PetscLogView_Default(viewer));
2658f14045dbSBarry Smith   } else if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
26599566063dSJacob Faibussowitsch     PetscCall(PetscLogView_Detailed(viewer));
266038144912Sdeepblu2718   } else if (format == PETSC_VIEWER_ASCII_CSV) {
26619566063dSJacob Faibussowitsch     PetscCall(PetscLogView_CSV(viewer));
2662bb1d7374SBarry Smith   } else if (format == PETSC_VIEWER_ASCII_XML) {
26639566063dSJacob Faibussowitsch     PetscCall(PetscLogView_Nested(viewer));
2664d0a29bd7SConnor Ward   } else if (format == PETSC_VIEWER_ASCII_FLAMEGRAPH) {
26659566063dSJacob Faibussowitsch     PetscCall(PetscLogView_Flamegraph(viewer));
26665c6c1daeSBarry Smith   }
26679566063dSJacob Faibussowitsch   PetscCall(PetscStageLogPush(stageLog, lastStage));
26683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
26695c6c1daeSBarry Smith }
26705c6c1daeSBarry Smith 
2671f14045dbSBarry Smith /*@C
2672811af0c4SBarry Smith   PetscLogViewFromOptions - Processes command line options to determine if/how a `PetscLog` is to be viewed.
2673f14045dbSBarry Smith 
2674811af0c4SBarry Smith   Collective on `PETSC_COMM_WORLD`
2675f14045dbSBarry Smith 
2676811af0c4SBarry Smith   Level: developer
2677f14045dbSBarry Smith 
2678d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogView()`
2679f14045dbSBarry Smith @*/
2680d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogViewFromOptions(void)
2681d71ae5a4SJacob Faibussowitsch {
2682f14045dbSBarry Smith   PetscViewer       viewer;
2683f14045dbSBarry Smith   PetscBool         flg;
2684f14045dbSBarry Smith   PetscViewerFormat format;
2685f14045dbSBarry Smith 
2686f14045dbSBarry Smith   PetscFunctionBegin;
26879566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetViewer(PETSC_COMM_WORLD, NULL, NULL, "-log_view", &viewer, &format, &flg));
2688f14045dbSBarry Smith   if (flg) {
26899566063dSJacob Faibussowitsch     PetscCall(PetscViewerPushFormat(viewer, format));
26909566063dSJacob Faibussowitsch     PetscCall(PetscLogView(viewer));
26919566063dSJacob Faibussowitsch     PetscCall(PetscViewerPopFormat(viewer));
26929566063dSJacob Faibussowitsch     PetscCall(PetscViewerDestroy(&viewer));
2693f14045dbSBarry Smith   }
26943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2695f14045dbSBarry Smith }
2696f14045dbSBarry Smith 
26975c6c1daeSBarry Smith /*----------------------------------------------- Counter Functions -------------------------------------------------*/
26985c6c1daeSBarry Smith /*@C
26995c6c1daeSBarry Smith   PetscGetFlops - Returns the number of flops used on this processor
27005c6c1daeSBarry Smith   since the program began.
27015c6c1daeSBarry Smith 
27025c6c1daeSBarry Smith   Not Collective
27035c6c1daeSBarry Smith 
27045c6c1daeSBarry Smith   Output Parameter:
270510450e9eSJacob Faibussowitsch . flops - number of floating point operations
27065c6c1daeSBarry Smith 
2707d1f92df0SBarry Smith   Level: intermediate
2708d1f92df0SBarry Smith 
27095c6c1daeSBarry Smith   Notes:
27105c6c1daeSBarry Smith   A global counter logs all PETSc flop counts.  The user can use
2711811af0c4SBarry Smith   `PetscLogFlops()` to increment this counter to include flops for the
27125c6c1daeSBarry Smith   application code.
27135c6c1daeSBarry Smith 
2714811af0c4SBarry Smith   A separate counter `PetscLogGPUFlops()` logs the flops that occur on any GPU associated with this MPI rank
2715811af0c4SBarry Smith 
2716d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogGPUFlops()`, `PetscTime()`, `PetscLogFlops()`
27175c6c1daeSBarry Smith @*/
2718d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGetFlops(PetscLogDouble *flops)
2719d71ae5a4SJacob Faibussowitsch {
27205c6c1daeSBarry Smith   PetscFunctionBegin;
27215c6c1daeSBarry Smith   *flops = petsc_TotalFlops;
27223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27235c6c1daeSBarry Smith }
27245c6c1daeSBarry Smith 
27251c1ad86eSToby Isaac /*@C
27261c1ad86eSToby Isaac   PetscLogObjectState - Record information about an object with the default log handler
27271c1ad86eSToby Isaac 
27281c1ad86eSToby Isaac   Not Collective
27291c1ad86eSToby Isaac 
27301c1ad86eSToby Isaac   Input Parameters:
27311c1ad86eSToby Isaac + obj    - the `PetscObject`
27321c1ad86eSToby Isaac . format - a printf-style format string
27331c1ad86eSToby Isaac - ...    - printf arguments to format
27341c1ad86eSToby Isaac 
27351c1ad86eSToby Isaac   Level: developer
27361c1ad86eSToby Isaac 
27371c1ad86eSToby Isaac .seealso: [](ch_profiling), `PetscLogObjectCreate()`, `PetscLogObjectDestroy()`
27381c1ad86eSToby Isaac @*/
2739d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogObjectState(PetscObject obj, const char format[], ...)
2740d71ae5a4SJacob Faibussowitsch {
27415c6c1daeSBarry Smith   size_t  fullLength;
27425c6c1daeSBarry Smith   va_list Argp;
27435c6c1daeSBarry Smith 
27445c6c1daeSBarry Smith   PetscFunctionBegin;
27453ba16761SJacob Faibussowitsch   if (!petsc_logObjects) PetscFunctionReturn(PETSC_SUCCESS);
27465c6c1daeSBarry Smith   va_start(Argp, format);
27479566063dSJacob Faibussowitsch   PetscCall(PetscVSNPrintf(petsc_objects[obj->id].info, 64, format, &fullLength, Argp));
27485c6c1daeSBarry Smith   va_end(Argp);
27493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27505c6c1daeSBarry Smith }
27515c6c1daeSBarry Smith 
27525c6c1daeSBarry Smith /*MC
27535c6c1daeSBarry Smith   PetscLogFlops - Adds floating point operations to the global counter.
27545c6c1daeSBarry Smith 
27555c6c1daeSBarry Smith   Synopsis:
2756aaa7dc30SBarry Smith   #include <petsclog.h>
27575c6c1daeSBarry Smith   PetscErrorCode PetscLogFlops(PetscLogDouble f)
27585c6c1daeSBarry Smith 
27595c6c1daeSBarry Smith   Not Collective
27605c6c1daeSBarry Smith 
27615c6c1daeSBarry Smith   Input Parameter:
27625c6c1daeSBarry Smith . f - flop counter
27635c6c1daeSBarry Smith 
276410450e9eSJacob Faibussowitsch   Example Usage:
27655c6c1daeSBarry Smith .vb
27665c6c1daeSBarry Smith   PetscLogEvent USER_EVENT;
276710450e9eSJacob Faibussowitsch 
27685c6c1daeSBarry Smith   PetscLogEventRegister("User event", 0, &USER_EVENT);
27695c6c1daeSBarry Smith   PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0);
27705c6c1daeSBarry Smith   [code segment to monitor]
27715c6c1daeSBarry Smith   PetscLogFlops(user_flops)
27725c6c1daeSBarry Smith   PetscLogEventEnd(USER_EVENT, 0, 0, 0, 0);
27735c6c1daeSBarry Smith .ve
27745c6c1daeSBarry Smith 
2775d1f92df0SBarry Smith   Level: intermediate
2776d1f92df0SBarry Smith 
2777811af0c4SBarry Smith   Note:
277810450e9eSJacob Faibussowitsch    A global counter logs all PETSc flop counts. The user can use PetscLogFlops() to increment
277910450e9eSJacob Faibussowitsch    this counter to include flops for the application code.
27805c6c1daeSBarry Smith 
2781d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogGPUFlops()`, `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscGetFlops()`
27825c6c1daeSBarry Smith M*/
27835c6c1daeSBarry Smith 
27845c6c1daeSBarry Smith /*MC
278510450e9eSJacob Faibussowitsch   PetscPreLoadBegin - Begin a segment of code that may be preloaded (run twice) to get accurate
278610450e9eSJacob Faibussowitsch   timings
27875c6c1daeSBarry Smith 
27885c6c1daeSBarry Smith   Synopsis:
2789aaa7dc30SBarry Smith   #include <petsclog.h>
27905c6c1daeSBarry Smith   void PetscPreLoadBegin(PetscBool flag, char *name);
27915c6c1daeSBarry Smith 
27925c6c1daeSBarry Smith   Not Collective
27935c6c1daeSBarry Smith 
2794d8d19677SJose E. Roman   Input Parameters:
279510450e9eSJacob Faibussowitsch + flag - `PETSC_TRUE` to run twice, `PETSC_FALSE` to run once, may be overridden with command
279610450e9eSJacob Faibussowitsch          line option `-preload true|false`
279710450e9eSJacob Faibussowitsch - name - name of first stage (lines of code timed separately with `-log_view`) to be preloaded
27985c6c1daeSBarry Smith 
279910450e9eSJacob Faibussowitsch   Example Usage:
28005c6c1daeSBarry Smith .vb
280110450e9eSJacob Faibussowitsch   PetscPreLoadBegin(PETSC_TRUE, "first stage");
280210450e9eSJacob Faibussowitsch   // lines of code
28035c6c1daeSBarry Smith   PetscPreLoadStage("second stage");
280410450e9eSJacob Faibussowitsch   // lines of code
28055c6c1daeSBarry Smith   PetscPreLoadEnd();
28065c6c1daeSBarry Smith .ve
28075c6c1daeSBarry Smith 
2808d1f92df0SBarry Smith   Level: intermediate
2809d1f92df0SBarry Smith 
2810811af0c4SBarry Smith   Note:
281195452b02SPatrick Sanan   Only works in C/C++, not Fortran
28125c6c1daeSBarry Smith 
281310450e9eSJacob Faibussowitsch   Flags available within the macro\:
281410450e9eSJacob Faibussowitsch + PetscPreLoadingUsed - `PETSC_TRUE` if we are or have done preloading
281510450e9eSJacob Faibussowitsch . PetscPreLoadingOn   - `PETSC_TRUE` if it is CURRENTLY doing preload
281610450e9eSJacob Faibussowitsch . PetscPreLoadIt      - `0` for the first computation (with preloading turned off it is only
281710450e9eSJacob Faibussowitsch                         `0`) `1`  for the second
281810450e9eSJacob Faibussowitsch - PetscPreLoadMax     - number of times it will do the computation, only one when preloading is
281910450e9eSJacob Faibussowitsch                         turned on
282010450e9eSJacob Faibussowitsch 
282110450e9eSJacob Faibussowitsch   The first two variables are available throughout the program, the second two only between the
282210450e9eSJacob Faibussowitsch   `PetscPreLoadBegin()` and `PetscPreLoadEnd()`
28235c6c1daeSBarry Smith 
2824d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()`
28255c6c1daeSBarry Smith M*/
28265c6c1daeSBarry Smith 
28275c6c1daeSBarry Smith /*MC
282810450e9eSJacob Faibussowitsch   PetscPreLoadEnd - End a segment of code that may be preloaded (run twice) to get accurate
282910450e9eSJacob Faibussowitsch   timings
28305c6c1daeSBarry Smith 
28315c6c1daeSBarry Smith   Synopsis:
2832aaa7dc30SBarry Smith   #include <petsclog.h>
28335c6c1daeSBarry Smith   void PetscPreLoadEnd(void);
28345c6c1daeSBarry Smith 
28355c6c1daeSBarry Smith   Not Collective
28365c6c1daeSBarry Smith 
283710450e9eSJacob Faibussowitsch   Example Usage:
28385c6c1daeSBarry Smith .vb
283910450e9eSJacob Faibussowitsch   PetscPreLoadBegin(PETSC_TRUE, "first stage");
284010450e9eSJacob Faibussowitsch   // lines of code
28415c6c1daeSBarry Smith   PetscPreLoadStage("second stage");
284210450e9eSJacob Faibussowitsch   // lines of code
28435c6c1daeSBarry Smith   PetscPreLoadEnd();
28445c6c1daeSBarry Smith .ve
28455c6c1daeSBarry Smith 
2846d1f92df0SBarry Smith   Level: intermediate
2847d1f92df0SBarry Smith 
2848811af0c4SBarry Smith   Note:
2849811af0c4SBarry Smith   Only works in C/C++ not fortran
28505c6c1daeSBarry Smith 
2851d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadStage()`
28525c6c1daeSBarry Smith M*/
28535c6c1daeSBarry Smith 
28545c6c1daeSBarry Smith /*MC
285510450e9eSJacob Faibussowitsch   PetscPreLoadStage - Start a new segment of code to be timed separately to get accurate timings
28565c6c1daeSBarry Smith 
28575c6c1daeSBarry Smith   Synopsis:
2858aaa7dc30SBarry Smith   #include <petsclog.h>
28595c6c1daeSBarry Smith   void PetscPreLoadStage(char *name);
28605c6c1daeSBarry Smith 
28615c6c1daeSBarry Smith   Not Collective
28625c6c1daeSBarry Smith 
286310450e9eSJacob Faibussowitsch   Example Usage:
28645c6c1daeSBarry Smith .vb
286510450e9eSJacob Faibussowitsch   PetscPreLoadBegin(PETSC_TRUE,"first stage");
286610450e9eSJacob Faibussowitsch   // lines of code
28675c6c1daeSBarry Smith   PetscPreLoadStage("second stage");
286810450e9eSJacob Faibussowitsch   // lines of code
28695c6c1daeSBarry Smith   PetscPreLoadEnd();
28705c6c1daeSBarry Smith .ve
28715c6c1daeSBarry Smith 
2872d1f92df0SBarry Smith   Level: intermediate
2873d1f92df0SBarry Smith 
2874811af0c4SBarry Smith   Note:
2875811af0c4SBarry Smith   Only works in C/C++ not fortran
28765c6c1daeSBarry Smith 
2877d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`
28785c6c1daeSBarry Smith M*/
28795c6c1daeSBarry Smith 
2880a4af0ceeSJacob Faibussowitsch   #if PetscDefined(HAVE_DEVICE)
2881a4af0ceeSJacob Faibussowitsch     #include <petsc/private/deviceimpl.h>
28829ffd0706SHong Zhang 
2883156b51fbSBarry Smith /*
2884156b51fbSBarry Smith    This cannot be called by users between PetscInitialize() and PetscFinalize() at any random location in the code
2885156b51fbSBarry Smith    because it will result in timing results that cannot be interpreted.
2886156b51fbSBarry Smith */
2887d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscLogGpuTime_Off(void)
2888d71ae5a4SJacob Faibussowitsch {
2889156b51fbSBarry Smith   PetscLogGpuTimeFlag = PETSC_FALSE;
28903ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
2891156b51fbSBarry Smith }
2892156b51fbSBarry Smith 
2893156b51fbSBarry Smith /*@C
2894156b51fbSBarry Smith   PetscLogGpuTime - turn on the logging of GPU time for GPU kernels
2895156b51fbSBarry Smith 
2896811af0c4SBarry Smith   Options Database Key:
289710450e9eSJacob Faibussowitsch . -log_view_gpu_time - provide the GPU times in the `-log_view` output
2898156b51fbSBarry Smith 
2899d1f92df0SBarry Smith   Level: advanced
2900d1f92df0SBarry Smith 
2901156b51fbSBarry Smith   Notes:
290210450e9eSJacob Faibussowitsch   Turning on the timing of the GPU kernels can slow down the entire computation and should only
290310450e9eSJacob Faibussowitsch   be used when studying the performance of operations on GPU such as vector operations and
290410450e9eSJacob Faibussowitsch   matrix-vector operations.
2905156b51fbSBarry Smith 
290610450e9eSJacob Faibussowitsch   This routine should only be called once near the beginning of the program. Once it is started
290710450e9eSJacob Faibussowitsch   it cannot be turned off.
2908156b51fbSBarry Smith 
2909d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogView()`, `PetscLogGpuFlops()`, `PetscLogGpuTimeEnd()`, `PetscLogGpuTimeBegin()`
2910156b51fbSBarry Smith @*/
2911d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogGpuTime(void)
2912d71ae5a4SJacob Faibussowitsch {
2913156b51fbSBarry Smith   if (!PetscLogGpuTimeFlag) PetscCall(PetscRegisterFinalize(PetscLogGpuTime_Off));
2914156b51fbSBarry Smith   PetscLogGpuTimeFlag = PETSC_TRUE;
29153ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
2916156b51fbSBarry Smith }
2917156b51fbSBarry Smith 
29189ffd0706SHong Zhang /*@C
29199ffd0706SHong Zhang   PetscLogGpuTimeBegin - Start timer for device
29209ffd0706SHong Zhang 
2921d1f92df0SBarry Smith   Level: intermediate
2922d1f92df0SBarry Smith 
29239ffd0706SHong Zhang   Notes:
292410450e9eSJacob Faibussowitsch   When CUDA or HIP is enabled, the timer is run on the GPU, it is a separate logging of time
292510450e9eSJacob Faibussowitsch   devoted to GPU computations (excluding kernel launch times).
2926811af0c4SBarry Smith 
292710450e9eSJacob Faibussowitsch   When CUDA or HIP is not available, the timer is run on the CPU, it is a separate logging of
292810450e9eSJacob Faibussowitsch   time devoted to GPU computations (including kernel launch times).
2929811af0c4SBarry Smith 
293010450e9eSJacob Faibussowitsch   There is no need to call WaitForCUDA() or WaitForHIP() between `PetscLogGpuTimeBegin()` and
293110450e9eSJacob Faibussowitsch   `PetscLogGpuTimeEnd()`
2932811af0c4SBarry Smith 
293310450e9eSJacob Faibussowitsch   This timer should NOT include times for data transfers between the GPU and CPU, nor setup
293410450e9eSJacob Faibussowitsch   actions such as allocating space.
2935811af0c4SBarry Smith 
293610450e9eSJacob Faibussowitsch   The regular logging captures the time for data transfers and any CPU activities during the
293710450e9eSJacob Faibussowitsch   event. It is used to compute the flop rate on the GPU as it is actively engaged in running a
293810450e9eSJacob Faibussowitsch   kernel.
29399ffd0706SHong Zhang 
29409ffd0706SHong Zhang   Developer Notes:
294110450e9eSJacob Faibussowitsch   The GPU event timer captures the execution time of all the kernels launched in the default
294210450e9eSJacob Faibussowitsch   stream by the CPU between `PetscLogGpuTimeBegin()` and `PetsLogGpuTimeEnd()`.
2943811af0c4SBarry Smith 
294410450e9eSJacob Faibussowitsch   `PetscLogGpuTimeBegin()` and `PetsLogGpuTimeEnd()` insert the begin and end events into the
294510450e9eSJacob Faibussowitsch   default stream (stream 0). The device will record a time stamp for the event when it reaches
294610450e9eSJacob Faibussowitsch   that event in the stream. The function xxxEventSynchronize() is called in
294710450e9eSJacob Faibussowitsch   `PetsLogGpuTimeEnd()` to block CPU execution, but not continued GPU execution, until the
294810450e9eSJacob Faibussowitsch   timer event is recorded.
29499ffd0706SHong Zhang 
2950d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogView()`, `PetscLogGpuFlops()`, `PetscLogGpuTimeEnd()`, `PetscLogGpuTime()`
29519ffd0706SHong Zhang @*/
2952d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogGpuTimeBegin(void)
2953d71ae5a4SJacob Faibussowitsch {
29549ffd0706SHong Zhang   PetscFunctionBegin;
29553ba16761SJacob Faibussowitsch   if (!PetscLogPLB || !PetscLogGpuTimeFlag) PetscFunctionReturn(PETSC_SUCCESS);
2956744d70b0SJunchao Zhang   if (PetscDefined(HAVE_DEVICE)) {
2957a4af0ceeSJacob Faibussowitsch     PetscDeviceContext dctx;
2958a4af0ceeSJacob Faibussowitsch 
29599566063dSJacob Faibussowitsch     PetscCall(PetscDeviceContextGetCurrentContext(&dctx));
29609566063dSJacob Faibussowitsch     PetscCall(PetscDeviceContextBeginTimer_Internal(dctx));
2961a4af0ceeSJacob Faibussowitsch   } else {
29629566063dSJacob Faibussowitsch     PetscCall(PetscTimeSubtract(&petsc_gtime));
2963a4af0ceeSJacob Faibussowitsch   }
29643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
29659ffd0706SHong Zhang }
29669ffd0706SHong Zhang 
29679ffd0706SHong Zhang /*@C
29689ffd0706SHong Zhang   PetscLogGpuTimeEnd - Stop timer for device
29699ffd0706SHong Zhang 
29709ffd0706SHong Zhang   Level: intermediate
29719ffd0706SHong Zhang 
2972d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogView()`, `PetscLogGpuFlops()`, `PetscLogGpuTimeBegin()`
29739ffd0706SHong Zhang @*/
2974d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogGpuTimeEnd(void)
2975d71ae5a4SJacob Faibussowitsch {
29769ffd0706SHong Zhang   PetscFunctionBegin;
29773ba16761SJacob Faibussowitsch   if (!PetscLogPLE || !PetscLogGpuTimeFlag) PetscFunctionReturn(PETSC_SUCCESS);
2978744d70b0SJunchao Zhang   if (PetscDefined(HAVE_DEVICE)) {
2979a4af0ceeSJacob Faibussowitsch     PetscDeviceContext dctx;
2980a4af0ceeSJacob Faibussowitsch     PetscLogDouble     elapsed;
2981a4af0ceeSJacob Faibussowitsch 
29829566063dSJacob Faibussowitsch     PetscCall(PetscDeviceContextGetCurrentContext(&dctx));
29839566063dSJacob Faibussowitsch     PetscCall(PetscDeviceContextEndTimer_Internal(dctx, &elapsed));
2984a4af0ceeSJacob Faibussowitsch     petsc_gtime += (elapsed / 1000.0);
2985a4af0ceeSJacob Faibussowitsch   } else {
29869566063dSJacob Faibussowitsch     PetscCall(PetscTimeAdd(&petsc_gtime));
2987a4af0ceeSJacob Faibussowitsch   }
29883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
29899ffd0706SHong Zhang }
2990c708d6e3SStefano Zampini 
29919ffd0706SHong Zhang   #endif /* end of PETSC_HAVE_DEVICE */
29929ffd0706SHong Zhang 
29935c6c1daeSBarry Smith #endif /* PETSC_USE_LOG*/
29945c6c1daeSBarry Smith 
29955c6c1daeSBarry Smith PetscClassId PETSC_LARGEST_CLASSID = PETSC_SMALLEST_CLASSID;
29965c6c1daeSBarry Smith PetscClassId PETSC_OBJECT_CLASSID  = 0;
29975c6c1daeSBarry Smith 
2998*2611ad71SToby Isaac /* Logging functions */
2999*2611ad71SToby Isaac PetscErrorCode (*PetscLogPHC)(PetscObject)                                                            = NULL;
3000*2611ad71SToby Isaac PetscErrorCode (*PetscLogPHD)(PetscObject)                                                            = NULL;
3001*2611ad71SToby Isaac PetscErrorCode (*PetscLogPLB)(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject) = NULL;
3002*2611ad71SToby Isaac PetscErrorCode (*PetscLogPLE)(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject) = NULL;
3003*2611ad71SToby Isaac 
3004*2611ad71SToby Isaac /* Tracing event logging variables */
3005*2611ad71SToby Isaac FILE            *petsc_tracefile          = NULL;
3006*2611ad71SToby Isaac int              petsc_tracelevel         = 0;
3007*2611ad71SToby Isaac const char      *petsc_traceblanks        = "                                                                                                    ";
3008*2611ad71SToby Isaac char             petsc_tracespace[128]    = " ";
3009*2611ad71SToby Isaac PetscLogDouble   petsc_tracetime          = 0.0;
3010*2611ad71SToby Isaac static PetscBool PetscLogInitializeCalled = PETSC_FALSE;
3011*2611ad71SToby Isaac 
3012*2611ad71SToby Isaac static PetscIntStack current_log_event_stack = NULL;
3013*2611ad71SToby Isaac 
3014*2611ad71SToby Isaac PETSC_INTERN PetscErrorCode PetscLogInitialize(void)
3015*2611ad71SToby Isaac {
3016*2611ad71SToby Isaac   int       stage;
3017*2611ad71SToby Isaac   PetscBool opt;
3018*2611ad71SToby Isaac 
3019*2611ad71SToby Isaac   PetscFunctionBegin;
3020*2611ad71SToby Isaac   if (PetscLogInitializeCalled) PetscFunctionReturn(PETSC_SUCCESS);
3021*2611ad71SToby Isaac   PetscLogInitializeCalled = PETSC_TRUE;
3022*2611ad71SToby Isaac   if (PetscDefined(USE_LOG)) {
3023*2611ad71SToby Isaac     PetscCall(PetscIntStackCreate(&current_log_event_stack));
3024*2611ad71SToby Isaac     PetscCall(PetscOptionsHasName(NULL, NULL, "-log_exclude_actions", &opt));
3025*2611ad71SToby Isaac     if (opt) petsc_logActions = PETSC_FALSE;
3026*2611ad71SToby Isaac     PetscCall(PetscOptionsHasName(NULL, NULL, "-log_exclude_objects", &opt));
3027*2611ad71SToby Isaac     if (opt) petsc_logObjects = PETSC_FALSE;
3028*2611ad71SToby Isaac     if (petsc_logActions) PetscCall(PetscMalloc1(petsc_maxActions, &petsc_actions));
3029*2611ad71SToby Isaac     if (petsc_logObjects) PetscCall(PetscMalloc1(petsc_maxObjects, &petsc_objects));
3030*2611ad71SToby Isaac     PetscLogPHC = PetscLogObjCreateDefault;
3031*2611ad71SToby Isaac     PetscLogPHD = PetscLogObjDestroyDefault;
3032*2611ad71SToby Isaac     /* Setup default logging structures */
3033*2611ad71SToby Isaac     PetscCall(PetscLogStateCreate(&petsc_log_state));
3034*2611ad71SToby Isaac     for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
3035*2611ad71SToby Isaac       if (PetscLogHandlers[i].handler) PetscCall(PetscLogHandlerSetState(PetscLogHandlers[i].handler, petsc_log_state));
3036*2611ad71SToby Isaac     }
3037*2611ad71SToby Isaac     PetscCall(PetscLogStateStageRegister(petsc_log_state, "Main Stage", &stage));
3038*2611ad71SToby Isaac     PetscCall(PetscStageLogCreate(&petsc_stageLog));
3039*2611ad71SToby Isaac     PetscCall(PetscStageLogRegister(petsc_stageLog, "Main Stage", &stage));
3040*2611ad71SToby Isaac 
3041*2611ad71SToby Isaac     PetscCall(PetscSpinlockCreate(&PetscLogSpinLock));
3042*2611ad71SToby Isaac #if defined(PETSC_HAVE_THREADSAFETY)
3043*2611ad71SToby Isaac     petsc_log_tid = 0;
3044*2611ad71SToby Isaac     petsc_log_gid = 0;
3045*2611ad71SToby Isaac     PetscCall(PetscHMapEventCreate(&eventInfoMap_th));
3046*2611ad71SToby Isaac #endif
3047*2611ad71SToby Isaac 
3048*2611ad71SToby Isaac     /* All processors sync here for more consistent logging */
3049*2611ad71SToby Isaac     PetscCallMPI(MPI_Barrier(PETSC_COMM_WORLD));
3050*2611ad71SToby Isaac     PetscCall(PetscTime(&petsc_BaseTime));
3051*2611ad71SToby Isaac     PetscCall(PetscLogStagePush(stage));
3052*2611ad71SToby Isaac   #if defined(PETSC_HAVE_TAU_PERFSTUBS)
3053*2611ad71SToby Isaac     PetscStackCallExternalVoid("ps_initialize_", ps_initialize_());
3054*2611ad71SToby Isaac   #endif
3055*2611ad71SToby Isaac   }
3056*2611ad71SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
3057*2611ad71SToby Isaac }
3058*2611ad71SToby Isaac 
3059*2611ad71SToby Isaac PETSC_INTERN PetscErrorCode PetscLogFinalize(void)
3060*2611ad71SToby Isaac {
3061*2611ad71SToby Isaac   PetscStageLog stageLog;
3062*2611ad71SToby Isaac 
3063*2611ad71SToby Isaac   PetscFunctionBegin;
3064*2611ad71SToby Isaac   if (PetscDefined(USE_LOG)) {
3065*2611ad71SToby Isaac     for (int i = 0; i < PETSC_LOG_HANDLER_MAX; i++) PetscCall(PetscLogHandlerDestroy(&PetscLogHandlers[i].handler));
3066*2611ad71SToby Isaac     PetscCall(PetscArrayzero(PetscLogHandlers, PETSC_LOG_HANDLER_MAX));
3067*2611ad71SToby Isaac     PetscCall(PetscLogStateDestroy(&petsc_log_state));
3068*2611ad71SToby Isaac   #if defined(PETSC_HAVE_THREADSAFETY)
3069*2611ad71SToby Isaac     if (eventInfoMap_th) {
3070*2611ad71SToby Isaac       PetscEventPerfInfo **array;
3071*2611ad71SToby Isaac       PetscInt             n, off = 0;
3072*2611ad71SToby Isaac 
3073*2611ad71SToby Isaac       PetscCall(PetscHMapEventGetSize(eventInfoMap_th, &n));
3074*2611ad71SToby Isaac       PetscCall(PetscMalloc1(n, &array));
3075*2611ad71SToby Isaac       PetscCall(PetscHMapEventGetVals(eventInfoMap_th, &off, array));
3076*2611ad71SToby Isaac       for (PetscInt i = 0; i < n; i++) PetscCall(PetscFree(array[i]));
3077*2611ad71SToby Isaac       PetscCall(PetscFree(array));
3078*2611ad71SToby Isaac       PetscCall(PetscHMapEventDestroy(&eventInfoMap_th));
3079*2611ad71SToby Isaac     }
3080*2611ad71SToby Isaac   #endif
3081*2611ad71SToby Isaac     PetscCall(PetscFree(petsc_actions));
3082*2611ad71SToby Isaac     PetscCall(PetscFree(petsc_objects));
3083*2611ad71SToby Isaac     PetscCall(PetscLogNestedEnd());
3084*2611ad71SToby Isaac     PetscCall(PetscLogSet(NULL, NULL));
3085*2611ad71SToby Isaac 
3086*2611ad71SToby Isaac     /* Resetting phase */
3087*2611ad71SToby Isaac     PetscCall(PetscLogGetStageLog(&stageLog));
3088*2611ad71SToby Isaac     PetscCall(PetscStageLogDestroy(stageLog));
3089*2611ad71SToby Isaac     PetscCall(PetscIntStackDestroy(current_log_event_stack));
3090*2611ad71SToby Isaac     current_log_event_stack = NULL;
3091*2611ad71SToby Isaac 
3092*2611ad71SToby Isaac     petsc_TotalFlops          = 0.0;
3093*2611ad71SToby Isaac     petsc_numActions          = 0;
3094*2611ad71SToby Isaac     petsc_numObjects          = 0;
3095*2611ad71SToby Isaac     petsc_numObjectsDestroyed = 0;
3096*2611ad71SToby Isaac     petsc_maxActions          = 100;
3097*2611ad71SToby Isaac     petsc_maxObjects          = 100;
3098*2611ad71SToby Isaac     petsc_actions             = NULL;
3099*2611ad71SToby Isaac     petsc_objects             = NULL;
3100*2611ad71SToby Isaac     petsc_logActions          = PETSC_FALSE;
3101*2611ad71SToby Isaac     petsc_logObjects          = PETSC_FALSE;
3102*2611ad71SToby Isaac     petsc_BaseTime            = 0.0;
3103*2611ad71SToby Isaac     petsc_TotalFlops          = 0.0;
3104*2611ad71SToby Isaac     petsc_send_ct             = 0.0;
3105*2611ad71SToby Isaac     petsc_recv_ct             = 0.0;
3106*2611ad71SToby Isaac     petsc_send_len            = 0.0;
3107*2611ad71SToby Isaac     petsc_recv_len            = 0.0;
3108*2611ad71SToby Isaac     petsc_isend_ct            = 0.0;
3109*2611ad71SToby Isaac     petsc_irecv_ct            = 0.0;
3110*2611ad71SToby Isaac     petsc_isend_len           = 0.0;
3111*2611ad71SToby Isaac     petsc_irecv_len           = 0.0;
3112*2611ad71SToby Isaac     petsc_wait_ct             = 0.0;
3113*2611ad71SToby Isaac     petsc_wait_any_ct         = 0.0;
3114*2611ad71SToby Isaac     petsc_wait_all_ct         = 0.0;
3115*2611ad71SToby Isaac     petsc_sum_of_waits_ct     = 0.0;
3116*2611ad71SToby Isaac     petsc_allreduce_ct        = 0.0;
3117*2611ad71SToby Isaac     petsc_gather_ct           = 0.0;
3118*2611ad71SToby Isaac     petsc_scatter_ct          = 0.0;
3119*2611ad71SToby Isaac     petsc_TotalFlops_th       = 0.0;
3120*2611ad71SToby Isaac     petsc_send_ct_th          = 0.0;
3121*2611ad71SToby Isaac     petsc_recv_ct_th          = 0.0;
3122*2611ad71SToby Isaac     petsc_send_len_th         = 0.0;
3123*2611ad71SToby Isaac     petsc_recv_len_th         = 0.0;
3124*2611ad71SToby Isaac     petsc_isend_ct_th         = 0.0;
3125*2611ad71SToby Isaac     petsc_irecv_ct_th         = 0.0;
3126*2611ad71SToby Isaac     petsc_isend_len_th        = 0.0;
3127*2611ad71SToby Isaac     petsc_irecv_len_th        = 0.0;
3128*2611ad71SToby Isaac     petsc_wait_ct_th          = 0.0;
3129*2611ad71SToby Isaac     petsc_wait_any_ct_th      = 0.0;
3130*2611ad71SToby Isaac     petsc_wait_all_ct_th      = 0.0;
3131*2611ad71SToby Isaac     petsc_sum_of_waits_ct_th  = 0.0;
3132*2611ad71SToby Isaac     petsc_allreduce_ct_th     = 0.0;
3133*2611ad71SToby Isaac     petsc_gather_ct_th        = 0.0;
3134*2611ad71SToby Isaac     petsc_scatter_ct_th       = 0.0;
3135*2611ad71SToby Isaac 
3136*2611ad71SToby Isaac     petsc_ctog_ct    = 0.0;
3137*2611ad71SToby Isaac     petsc_gtoc_ct    = 0.0;
3138*2611ad71SToby Isaac     petsc_ctog_sz    = 0.0;
3139*2611ad71SToby Isaac     petsc_gtoc_sz    = 0.0;
3140*2611ad71SToby Isaac     petsc_gflops     = 0.0;
3141*2611ad71SToby Isaac     petsc_gtime      = 0.0;
3142*2611ad71SToby Isaac     petsc_ctog_ct_th = 0.0;
3143*2611ad71SToby Isaac     petsc_gtoc_ct_th = 0.0;
3144*2611ad71SToby Isaac     petsc_ctog_sz_th = 0.0;
3145*2611ad71SToby Isaac     petsc_gtoc_sz_th = 0.0;
3146*2611ad71SToby Isaac     petsc_gflops_th  = 0.0;
3147*2611ad71SToby Isaac     petsc_gtime_th   = 0.0;
3148*2611ad71SToby Isaac 
3149*2611ad71SToby Isaac     PETSC_LARGEST_EVENT      = PETSC_EVENT;
3150*2611ad71SToby Isaac     PetscLogPHC              = NULL;
3151*2611ad71SToby Isaac     PetscLogPHD              = NULL;
3152*2611ad71SToby Isaac     petsc_tracefile          = NULL;
3153*2611ad71SToby Isaac     petsc_tracelevel         = 0;
3154*2611ad71SToby Isaac     petsc_traceblanks        = "                                                                                                    ";
3155*2611ad71SToby Isaac     petsc_tracespace[0]      = ' ';
3156*2611ad71SToby Isaac     petsc_tracespace[1]      = 0;
3157*2611ad71SToby Isaac     petsc_tracetime          = 0.0;
3158*2611ad71SToby Isaac     petsc_stageLog           = NULL;
3159*2611ad71SToby Isaac   }
3160*2611ad71SToby Isaac   PETSC_LARGEST_CLASSID    = PETSC_SMALLEST_CLASSID;
3161*2611ad71SToby Isaac   PETSC_OBJECT_CLASSID     = 0;
3162*2611ad71SToby Isaac   PetscLogInitializeCalled = PETSC_FALSE;
3163*2611ad71SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
3164*2611ad71SToby Isaac }
3165*2611ad71SToby Isaac 
31665c6c1daeSBarry Smith /*@C
31675c6c1daeSBarry Smith   PetscClassIdRegister - Registers a new class name for objects and logging operations in an application code.
31685c6c1daeSBarry Smith 
31695c6c1daeSBarry Smith   Not Collective
31705c6c1daeSBarry Smith 
31715c6c1daeSBarry Smith   Input Parameter:
31725c6c1daeSBarry Smith . name - The class name
31735c6c1daeSBarry Smith 
31745c6c1daeSBarry Smith   Output Parameter:
31755c6c1daeSBarry Smith . oclass - The class id or classid
31765c6c1daeSBarry Smith 
31775c6c1daeSBarry Smith   Level: developer
31785c6c1daeSBarry Smith 
3179d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`
31805c6c1daeSBarry Smith @*/
3181d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscClassIdRegister(const char name[], PetscClassId *oclass)
3182d71ae5a4SJacob Faibussowitsch {
31835c6c1daeSBarry Smith #if defined(PETSC_USE_LOG)
31845c6c1daeSBarry Smith   PetscStageLog stageLog;
31855c6c1daeSBarry Smith   PetscInt      stage;
31865c6c1daeSBarry Smith #endif
31875c6c1daeSBarry Smith 
31885c6c1daeSBarry Smith   PetscFunctionBegin;
31895c6c1daeSBarry Smith   *oclass = ++PETSC_LARGEST_CLASSID;
31905c6c1daeSBarry Smith #if defined(PETSC_USE_LOG)
31919566063dSJacob Faibussowitsch   PetscCall(PetscLogGetStageLog(&stageLog));
31929566063dSJacob Faibussowitsch   PetscCall(PetscClassRegLogRegister(stageLog->classLog, name, *oclass));
319348a46eb9SPierre Jolivet   for (stage = 0; stage < stageLog->numStages; stage++) PetscCall(PetscClassPerfLogEnsureSize(stageLog->stageInfo[stage].classLog, stageLog->classLog->numClasses));
31945c6c1daeSBarry Smith #endif
31953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31965c6c1daeSBarry Smith }
3197210b5426SBarry Smith 
3198210b5426SBarry Smith #if defined(PETSC_USE_LOG) && defined(PETSC_HAVE_MPE)
3199210b5426SBarry Smith   #include <mpe.h>
3200210b5426SBarry Smith 
3201210b5426SBarry Smith /*@C
3202495fc317SBarry Smith   PetscLogMPEDump - Dumps the MPE logging info to file for later use with Jumpshot.
3203210b5426SBarry Smith 
320410450e9eSJacob Faibussowitsch   Input Parameter:
320510450e9eSJacob Faibussowitsch . sname - The filename to dump to
320610450e9eSJacob Faibussowitsch 
3207811af0c4SBarry Smith   Collective over `PETSC_COMM_WORLD`
3208210b5426SBarry Smith 
3209210b5426SBarry Smith   Level: advanced
3210210b5426SBarry Smith 
3211d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogDump()`, `PetscLogAllBegin()`, `PetscLogMPEBegin()`
3212210b5426SBarry Smith @*/
3213d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogMPEDump(const char sname[])
3214d71ae5a4SJacob Faibussowitsch {
3215210b5426SBarry Smith   char name[PETSC_MAX_PATH_LEN];
3216210b5426SBarry Smith 
3217210b5426SBarry Smith   PetscFunctionBegin;
3218210b5426SBarry Smith   if (PetscBeganMPE) {
32199566063dSJacob Faibussowitsch     PetscCall(PetscInfo(0, "Finalizing MPE.\n"));
3220210b5426SBarry Smith     if (sname) {
3221c6a7a370SJeremy L Thompson       PetscCall(PetscStrncpy(name, sname, sizeof(name)));
3222210b5426SBarry Smith     } else {
32239566063dSJacob Faibussowitsch       PetscCall(PetscGetProgramName(name, sizeof(name)));
3224210b5426SBarry Smith     }
32259566063dSJacob Faibussowitsch     PetscCall(MPE_Finish_log(name));
3226210b5426SBarry Smith   } else {
32279566063dSJacob Faibussowitsch     PetscCall(PetscInfo(0, "Not finalizing MPE (not started by PETSc).\n"));
3228210b5426SBarry Smith   }
32293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3230210b5426SBarry Smith }
3231210b5426SBarry Smith 
3232210b5426SBarry Smith   #define PETSC_RGB_COLORS_MAX 39
32339371c9d4SSatish Balay static const char *PetscLogMPERGBColors[PETSC_RGB_COLORS_MAX] = {"OliveDrab:      ", "BlueViolet:     ", "CadetBlue:      ", "CornflowerBlue: ", "DarkGoldenrod:  ", "DarkGreen:      ", "DarkKhaki:      ", "DarkOliveGreen: ",
32349371c9d4SSatish Balay                                                                  "DarkOrange:     ", "DarkOrchid:     ", "DarkSeaGreen:   ", "DarkSlateGray:  ", "DarkTurquoise:  ", "DeepPink:       ", "DarkKhaki:      ", "DimGray:        ",
32359371c9d4SSatish Balay                                                                  "DodgerBlue:     ", "GreenYellow:    ", "HotPink:        ", "IndianRed:      ", "LavenderBlush:  ", "LawnGreen:      ", "LemonChiffon:   ", "LightCoral:     ",
32369371c9d4SSatish Balay                                                                  "LightCyan:      ", "LightPink:      ", "LightSalmon:    ", "LightSlateGray: ", "LightYellow:    ", "LimeGreen:      ", "MediumPurple:   ", "MediumSeaGreen: ",
32379371c9d4SSatish Balay                                                                  "MediumSlateBlue:", "MidnightBlue:   ", "MintCream:      ", "MistyRose:      ", "NavajoWhite:    ", "NavyBlue:       ", "OliveDrab:      "};
3238210b5426SBarry Smith 
3239210b5426SBarry Smith /*@C
3240811af0c4SBarry Smith   PetscLogMPEGetRGBColor - This routine returns a rgb color useable with `PetscLogEventRegister()`
3241210b5426SBarry Smith 
3242210b5426SBarry Smith   Not collective. Maybe it should be?
3243210b5426SBarry Smith 
32447a7aea1fSJed Brown   Output Parameter:
3245210b5426SBarry Smith . str - character string representing the color
3246210b5426SBarry Smith 
3247210b5426SBarry Smith   Level: developer
3248210b5426SBarry Smith 
3249d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`
3250210b5426SBarry Smith @*/
3251d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogMPEGetRGBColor(const char *str[])
3252d71ae5a4SJacob Faibussowitsch {
3253210b5426SBarry Smith   static int idx = 0;
3254210b5426SBarry Smith 
3255210b5426SBarry Smith   PetscFunctionBegin;
3256210b5426SBarry Smith   *str = PetscLogMPERGBColors[idx];
3257210b5426SBarry Smith   idx  = (idx + 1) % PETSC_RGB_COLORS_MAX;
32583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3259210b5426SBarry Smith }
3260210b5426SBarry Smith 
3261210b5426SBarry Smith #endif /* PETSC_USE_LOG && PETSC_HAVE_MPE */
3262