xref: /petsc/src/sys/logging/plog.c (revision cb9ef012bfbb6ad62d979c01e30be5766f6ea67d)
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 
10b665b14eSToby Isaac       ***
11b665b14eSToby Isaac 
12b665b14eSToby Isaac       This file, and only this file, is for functions that interact with the global logging state
135c6c1daeSBarry Smith */
14af0996ceSBarry Smith #include <petsc/private/logimpl.h> /*I    "petscsys.h"   I*/
1553e0a2f3SToby Isaac #include <petsc/private/loghandlerimpl.h>
165c6c1daeSBarry Smith #include <petsctime.h>
17665c2dedSJed Brown #include <petscviewer.h>
188fe3844cSJunchao Zhang #include <petscdevice.h>
198fe3844cSJunchao Zhang #include <petsc/private/deviceimpl.h>
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 
292611ad71SToby Isaac PetscInt PetscLogGetTid(void)
302611ad71SToby Isaac {
312611ad71SToby Isaac   if (petsc_log_tid < 0) {
322611ad71SToby Isaac     PetscCall(PetscSpinlockLock(&PetscLogSpinLock));
332611ad71SToby Isaac     petsc_log_tid = ++petsc_log_gid;
342611ad71SToby Isaac     PetscCall(PetscSpinlockUnlock(&PetscLogSpinLock));
352611ad71SToby Isaac   }
362611ad71SToby Isaac   return petsc_log_tid;
372611ad71SToby Isaac }
382611ad71SToby Isaac 
39c708d6e3SStefano Zampini #endif
40c708d6e3SStefano Zampini 
415c6c1daeSBarry Smith /* Global counters */
425c6c1daeSBarry Smith PetscLogDouble petsc_BaseTime        = 0.0;
435c6c1daeSBarry Smith PetscLogDouble petsc_TotalFlops      = 0.0; /* The number of flops */
445c6c1daeSBarry Smith PetscLogDouble petsc_send_ct         = 0.0; /* The number of sends */
455c6c1daeSBarry Smith PetscLogDouble petsc_recv_ct         = 0.0; /* The number of receives */
465c6c1daeSBarry Smith PetscLogDouble petsc_send_len        = 0.0; /* The total length of all sent messages */
475c6c1daeSBarry Smith PetscLogDouble petsc_recv_len        = 0.0; /* The total length of all received messages */
485c6c1daeSBarry Smith PetscLogDouble petsc_isend_ct        = 0.0; /* The number of immediate sends */
495c6c1daeSBarry Smith PetscLogDouble petsc_irecv_ct        = 0.0; /* The number of immediate receives */
505c6c1daeSBarry Smith PetscLogDouble petsc_isend_len       = 0.0; /* The total length of all immediate send messages */
515c6c1daeSBarry Smith PetscLogDouble petsc_irecv_len       = 0.0; /* The total length of all immediate receive messages */
525c6c1daeSBarry Smith PetscLogDouble petsc_wait_ct         = 0.0; /* The number of waits */
535c6c1daeSBarry Smith PetscLogDouble petsc_wait_any_ct     = 0.0; /* The number of anywaits */
545c6c1daeSBarry Smith PetscLogDouble petsc_wait_all_ct     = 0.0; /* The number of waitalls */
555c6c1daeSBarry Smith PetscLogDouble petsc_sum_of_waits_ct = 0.0; /* The total number of waits */
565c6c1daeSBarry Smith PetscLogDouble petsc_allreduce_ct    = 0.0; /* The number of reductions */
575c6c1daeSBarry Smith PetscLogDouble petsc_gather_ct       = 0.0; /* The number of gathers and gathervs */
585c6c1daeSBarry Smith PetscLogDouble petsc_scatter_ct      = 0.0; /* The number of scatters and scattervs */
59c708d6e3SStefano Zampini 
60c708d6e3SStefano Zampini /* Thread Local storage */
61c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_TotalFlops_th      = 0.0;
62c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_send_ct_th         = 0.0;
63c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_recv_ct_th         = 0.0;
64c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_send_len_th        = 0.0;
65c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_recv_len_th        = 0.0;
66c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_isend_ct_th        = 0.0;
67c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_irecv_ct_th        = 0.0;
68c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_isend_len_th       = 0.0;
69c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_irecv_len_th       = 0.0;
70c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_wait_ct_th         = 0.0;
71c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_wait_any_ct_th     = 0.0;
72c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_wait_all_ct_th     = 0.0;
73c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_sum_of_waits_ct_th = 0.0;
74c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_allreduce_ct_th    = 0.0;
75c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_gather_ct_th       = 0.0;
76c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_scatter_ct_th      = 0.0;
77c708d6e3SStefano Zampini 
78bec0b493Shannah_mairs PetscLogDouble petsc_ctog_ct        = 0.0; /* The total number of CPU to GPU copies */
79bec0b493Shannah_mairs PetscLogDouble petsc_gtoc_ct        = 0.0; /* The total number of GPU to CPU copies */
80bec0b493Shannah_mairs PetscLogDouble petsc_ctog_sz        = 0.0; /* The total size of CPU to GPU copies */
81bec0b493Shannah_mairs PetscLogDouble petsc_gtoc_sz        = 0.0; /* The total size of GPU to CPU copies */
8245c4b7c1SBarry Smith PetscLogDouble petsc_ctog_ct_scalar = 0.0; /* The total number of CPU to GPU copies */
8345c4b7c1SBarry Smith PetscLogDouble petsc_gtoc_ct_scalar = 0.0; /* The total number of GPU to CPU copies */
8445c4b7c1SBarry Smith PetscLogDouble petsc_ctog_sz_scalar = 0.0; /* The total size of CPU to GPU copies */
8545c4b7c1SBarry Smith PetscLogDouble petsc_gtoc_sz_scalar = 0.0; /* The total size of GPU to CPU copies */
86958c4211Shannah_mairs PetscLogDouble petsc_gflops         = 0.0; /* The flops done on a GPU */
87958c4211Shannah_mairs PetscLogDouble petsc_gtime          = 0.0; /* The time spent on a GPU */
88c708d6e3SStefano Zampini 
89c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_ctog_ct_th        = 0.0;
90c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_gtoc_ct_th        = 0.0;
91c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_ctog_sz_th        = 0.0;
92c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_gtoc_sz_th        = 0.0;
93c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_ctog_ct_scalar_th = 0.0;
94c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_gtoc_ct_scalar_th = 0.0;
95c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_ctog_sz_scalar_th = 0.0;
96c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_gtoc_sz_scalar_th = 0.0;
97c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_gflops_th         = 0.0;
98c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_gtime_th          = 0.0;
992611ad71SToby Isaac 
1002611ad71SToby Isaac PetscBool PetscLogMemory = PETSC_FALSE;
1012611ad71SToby Isaac PetscBool PetscLogSyncOn = PETSC_FALSE;
1022611ad71SToby Isaac 
1032611ad71SToby Isaac PetscBool PetscLogGpuTimeFlag = PETSC_FALSE;
1042611ad71SToby Isaac 
1052611ad71SToby Isaac PetscLogState petsc_log_state = NULL;
1062611ad71SToby Isaac 
1072611ad71SToby Isaac #define PETSC_LOG_HANDLER_HOT_BLANK \
1082611ad71SToby Isaac   { \
1092611ad71SToby Isaac     NULL, NULL, NULL, NULL, NULL, NULL \
1102611ad71SToby Isaac   }
1112611ad71SToby Isaac 
1122611ad71SToby Isaac PetscLogHandlerHot PetscLogHandlers[PETSC_LOG_HANDLER_MAX] = {
1132611ad71SToby Isaac   PETSC_LOG_HANDLER_HOT_BLANK,
1142611ad71SToby Isaac   PETSC_LOG_HANDLER_HOT_BLANK,
1152611ad71SToby Isaac   PETSC_LOG_HANDLER_HOT_BLANK,
1162611ad71SToby Isaac   PETSC_LOG_HANDLER_HOT_BLANK,
1172611ad71SToby Isaac };
1182611ad71SToby Isaac 
1192611ad71SToby Isaac #undef PETSC_LOG_HANDLERS_HOT_BLANK
1202611ad71SToby Isaac 
1212611ad71SToby Isaac #if defined(PETSC_USE_LOG)
1222611ad71SToby Isaac   #include <../src/sys/logging/handler/impls/default/logdefault.h>
123c708d6e3SStefano Zampini 
124c708d6e3SStefano Zampini   #if defined(PETSC_HAVE_THREADSAFETY)
125c708d6e3SStefano Zampini PetscErrorCode PetscAddLogDouble(PetscLogDouble *tot, PetscLogDouble *tot_th, PetscLogDouble tmp)
126c708d6e3SStefano Zampini {
127c708d6e3SStefano Zampini   *tot_th += tmp;
1283ba16761SJacob Faibussowitsch   PetscCall(PetscSpinlockLock(&PetscLogSpinLock));
129c708d6e3SStefano Zampini   *tot += tmp;
1303ba16761SJacob Faibussowitsch   PetscCall(PetscSpinlockUnlock(&PetscLogSpinLock));
1313ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
132c708d6e3SStefano Zampini }
133c708d6e3SStefano Zampini 
134c708d6e3SStefano Zampini PetscErrorCode PetscAddLogDoubleCnt(PetscLogDouble *cnt, PetscLogDouble *tot, PetscLogDouble *cnt_th, PetscLogDouble *tot_th, PetscLogDouble tmp)
135c708d6e3SStefano Zampini {
136c708d6e3SStefano Zampini   *cnt_th = *cnt_th + 1;
137c708d6e3SStefano Zampini   *tot_th += tmp;
1383ba16761SJacob Faibussowitsch   PetscCall(PetscSpinlockLock(&PetscLogSpinLock));
139c708d6e3SStefano Zampini   *tot += (PetscLogDouble)(tmp);
140c708d6e3SStefano Zampini   *cnt += *cnt + 1;
1413ba16761SJacob Faibussowitsch   PetscCall(PetscSpinlockUnlock(&PetscLogSpinLock));
1423ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
143c708d6e3SStefano Zampini }
144c708d6e3SStefano Zampini 
145bec0b493Shannah_mairs   #endif
1465c6c1daeSBarry Smith 
14753e0a2f3SToby Isaac static PetscErrorCode PetscLogTryGetHandler(PetscLogHandlerType type, PetscLogHandler *handler)
14853e0a2f3SToby Isaac {
14953e0a2f3SToby Isaac   PetscFunctionBegin;
15053e0a2f3SToby Isaac   PetscAssertPointer(handler, 2);
15153e0a2f3SToby Isaac   *handler = NULL;
15253e0a2f3SToby Isaac   for (int i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
15353e0a2f3SToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
15453e0a2f3SToby Isaac     if (h) {
15553e0a2f3SToby Isaac       PetscBool match;
15653e0a2f3SToby Isaac 
15753e0a2f3SToby Isaac       PetscCall(PetscObjectTypeCompare((PetscObject)h, type, &match));
15853e0a2f3SToby Isaac       if (match) {
15953e0a2f3SToby Isaac         *handler = PetscLogHandlers[i].handler;
16053e0a2f3SToby Isaac         PetscFunctionReturn(PETSC_SUCCESS);
16153e0a2f3SToby Isaac       }
16253e0a2f3SToby Isaac     }
16353e0a2f3SToby Isaac   }
16453e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
16553e0a2f3SToby Isaac }
16653e0a2f3SToby Isaac 
16753e0a2f3SToby Isaac /*@
168b665b14eSToby Isaac   PetscLogGetDefaultHandler - Get the default log handler if it is running.
169b665b14eSToby Isaac 
170b665b14eSToby Isaac   Not collective
171b665b14eSToby Isaac 
172b665b14eSToby Isaac   Output Parameter:
173b665b14eSToby Isaac . handler - the default `PetscLogHandler`, or `NULL` if it is not running.
174b665b14eSToby Isaac 
175b665b14eSToby Isaac   Level: developer
176b665b14eSToby Isaac 
177b665b14eSToby Isaac   Notes:
178b665b14eSToby Isaac   The default handler is started with `PetscLogDefaultBegin()`,
179b665b14eSToby Isaac   if the options flags `-log_all` or `-log_view` is given without arguments,
180b665b14eSToby Isaac   or for `-log_view :output:format` if `format` is not `ascii_xml` or `ascii_flamegraph`.
181b665b14eSToby Isaac 
182b665b14eSToby Isaac   These logging functions only affect the default log handler\:
183b665b14eSToby Isaac   `PetscLogActions()`, `PetscLogObjects()`, `PetscLogStageSetVisible()`,
184b665b14eSToby Isaac   `PetscLogDeactivatePush()`, `PetscLogDeactivatePop()`, `PetscLogEventGetPerfInfo()`,
185b665b14eSToby Isaac   `PetscLogEventSetDof()`, `PetscLogEventSetError()`, `PetscLogEventsPause()`,
186b665b14eSToby Isaac   `PetscLogEventsResume()`, `PetscLogDump()`, `PetscLogObjectState()`.
187b665b14eSToby Isaac 
188b665b14eSToby Isaac .seealso: [](ch_profiling)
189b665b14eSToby Isaac @*/
190b665b14eSToby Isaac PetscErrorCode PetscLogGetDefaultHandler(PetscLogHandler *handler)
191b665b14eSToby Isaac {
192b665b14eSToby Isaac   PetscFunctionBegin;
193b665b14eSToby Isaac   PetscCall(PetscLogTryGetHandler(PETSC_LOG_HANDLER_DEFAULT, handler));
194b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
195b665b14eSToby Isaac }
196b665b14eSToby Isaac 
197b665b14eSToby Isaac static PetscErrorCode PetscLogGetHandler(PetscLogHandlerType type, PetscLogHandler *handler)
198b665b14eSToby Isaac {
199b665b14eSToby Isaac   PetscFunctionBegin;
200b665b14eSToby Isaac   PetscAssertPointer(handler, 2);
201b665b14eSToby Isaac   PetscCall(PetscLogTryGetHandler(type, handler));
202b665b14eSToby Isaac   PetscCheck(handler != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "A PetscLogHandler of type %s has not been started.", type);
203b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
204b665b14eSToby Isaac }
205b665b14eSToby Isaac 
206b665b14eSToby Isaac /*@
20753e0a2f3SToby Isaac   PetscLogGetState - Get the `PetscLogState` for PETSc's global logging, used
20853e0a2f3SToby Isaac   by all default log handlers (`PetscLogDefaultBegin()`,
20953e0a2f3SToby Isaac   `PetscLogNestedBegin()`, `PetscLogTraceBegin()`, `PetscLogMPEBegin()`,
21053e0a2f3SToby Isaac   `PetscLogPerfstubsBegin()`).
21153e0a2f3SToby Isaac 
21253e0a2f3SToby Isaac   Collective on `PETSC_COMM_WORLD`
21353e0a2f3SToby Isaac 
21453e0a2f3SToby Isaac   Output Parameter:
215b665b14eSToby Isaac . state - The `PetscLogState` changed by registrations (such as
216b665b14eSToby Isaac           `PetscLogEventRegister()`) and actions (such as `PetscLogEventBegin()` or
217b665b14eSToby Isaac           `PetscLogStagePush()`), or NULL if logging is not active
21853e0a2f3SToby Isaac 
21953e0a2f3SToby Isaac   Level: developer
22053e0a2f3SToby Isaac 
22153e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogState`
22253e0a2f3SToby Isaac @*/
22353e0a2f3SToby Isaac PetscErrorCode PetscLogGetState(PetscLogState *state)
22453e0a2f3SToby Isaac {
22553e0a2f3SToby Isaac   PetscFunctionBegin;
22653e0a2f3SToby Isaac   PetscAssertPointer(state, 1);
22753e0a2f3SToby Isaac   *state = petsc_log_state;
22853e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
22953e0a2f3SToby Isaac }
23053e0a2f3SToby Isaac 
23153e0a2f3SToby Isaac static PetscErrorCode PetscLogHandlerCopyToHot(PetscLogHandler h, PetscLogHandlerHot *hot)
23253e0a2f3SToby Isaac {
23353e0a2f3SToby Isaac   PetscFunctionBegin;
23453e0a2f3SToby Isaac   hot->handler       = h;
23553e0a2f3SToby Isaac   hot->eventBegin    = h->ops->eventbegin;
23653e0a2f3SToby Isaac   hot->eventEnd      = h->ops->eventend;
23753e0a2f3SToby Isaac   hot->eventSync     = h->ops->eventsync;
23853e0a2f3SToby Isaac   hot->objectCreate  = h->ops->objectcreate;
23953e0a2f3SToby Isaac   hot->objectDestroy = h->ops->objectdestroy;
24053e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
24153e0a2f3SToby Isaac }
24253e0a2f3SToby Isaac 
24353e0a2f3SToby Isaac /*@
24453e0a2f3SToby Isaac   PetscLogHandlerStart - Connect a log handler to PETSc's global logging stream and state.
24553e0a2f3SToby Isaac 
24653e0a2f3SToby Isaac   Logically collective
24753e0a2f3SToby Isaac 
24853e0a2f3SToby Isaac   Input Parameters:
24953e0a2f3SToby Isaac . h - a `PetscLogHandler`
25053e0a2f3SToby Isaac 
25153e0a2f3SToby Isaac   Level: developer
25253e0a2f3SToby Isaac 
25353e0a2f3SToby Isaac   Notes:
25453e0a2f3SToby Isaac 
25553e0a2f3SToby Isaac   Users should only need this if they create their own log handlers: handlers that are started
25653e0a2f3SToby Isaac   from the command line (such as `-log_view` and `-log_trace`) or from a function like
25753e0a2f3SToby Isaac   `PetscLogNestedBegin()` will automatically be started.
25853e0a2f3SToby Isaac 
25953e0a2f3SToby Isaac   There is a limit of `PESC_LOG_HANDLER_MAX` handlers that can be active at one time.
26053e0a2f3SToby Isaac 
26153e0a2f3SToby Isaac   To disconnect a handler from the global stream call `PetscLogHandlerStop()`.
26253e0a2f3SToby Isaac 
26353e0a2f3SToby Isaac   When a log handler is started, stages that have already been pushed with `PetscLogStagePush()`,
26453e0a2f3SToby Isaac   will be pushed for the new log handler, but it will not be informed of any events that are
26553e0a2f3SToby Isaac   in progress.  It is recommended to start any user-defined log handlers immediately following
26653e0a2f3SToby Isaac   before any user-defined stages are pushed.
26753e0a2f3SToby Isaac 
26853e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogState`, `PetscLogHandlerStop()`
26953e0a2f3SToby Isaac @*/
27053e0a2f3SToby Isaac PetscErrorCode PetscLogHandlerStart(PetscLogHandler h)
27153e0a2f3SToby Isaac {
27253e0a2f3SToby Isaac   PetscFunctionBegin;
27353e0a2f3SToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
27453e0a2f3SToby Isaac     if (PetscLogHandlers[i].handler == h) PetscFunctionReturn(PETSC_SUCCESS);
27553e0a2f3SToby Isaac   }
27653e0a2f3SToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
27753e0a2f3SToby Isaac     if (PetscLogHandlers[i].handler == NULL) {
27853e0a2f3SToby Isaac       PetscCall(PetscObjectReference((PetscObject)h));
27953e0a2f3SToby Isaac       PetscCall(PetscLogHandlerCopyToHot(h, &PetscLogHandlers[i]));
28053e0a2f3SToby Isaac       if (petsc_log_state) {
28153e0a2f3SToby Isaac         PetscLogStage stack_height;
28253e0a2f3SToby Isaac         PetscIntStack orig_stack, temp_stack;
28353e0a2f3SToby Isaac 
28453e0a2f3SToby Isaac         PetscCall(PetscLogHandlerSetState(h, petsc_log_state));
28553e0a2f3SToby Isaac         stack_height = petsc_log_state->stage_stack->top + 1;
28653e0a2f3SToby Isaac         PetscCall(PetscIntStackCreate(&temp_stack));
28753e0a2f3SToby Isaac         orig_stack                     = petsc_log_state->stage_stack;
28853e0a2f3SToby Isaac         petsc_log_state->stage_stack   = temp_stack;
28953e0a2f3SToby Isaac         petsc_log_state->current_stage = -1;
29053e0a2f3SToby Isaac         for (int s = 0; s < stack_height; s++) {
29153e0a2f3SToby Isaac           PetscLogStage stage = (PetscLogStage)orig_stack->stack[s];
29253e0a2f3SToby Isaac           PetscCall(PetscLogHandlerStagePush(h, stage));
29353e0a2f3SToby Isaac           PetscCall(PetscIntStackPush(temp_stack, stage));
29453e0a2f3SToby Isaac           petsc_log_state->current_stage = stage;
29553e0a2f3SToby Isaac         }
29653e0a2f3SToby Isaac         PetscCall(PetscIntStackDestroy(temp_stack));
29753e0a2f3SToby Isaac         petsc_log_state->stage_stack = orig_stack;
29853e0a2f3SToby Isaac       }
29953e0a2f3SToby Isaac       PetscFunctionReturn(PETSC_SUCCESS);
30053e0a2f3SToby Isaac     }
30153e0a2f3SToby Isaac   }
30253e0a2f3SToby Isaac   SETERRQ(PetscObjectComm((PetscObject)h), PETSC_ERR_ARG_WRONGSTATE, "%d log handlers already started, cannot start another", PETSC_LOG_HANDLER_MAX);
30353e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
30453e0a2f3SToby Isaac }
30553e0a2f3SToby Isaac 
30653e0a2f3SToby Isaac /*@
30753e0a2f3SToby Isaac   PetscLogHandlerStop - Disconnect a log handler from PETSc's global logging stream.
30853e0a2f3SToby Isaac 
30953e0a2f3SToby Isaac   Logically collective
31053e0a2f3SToby Isaac 
31153e0a2f3SToby Isaac   Input Parameters:
31253e0a2f3SToby Isaac . h - a `PetscLogHandler`
31353e0a2f3SToby Isaac 
31453e0a2f3SToby Isaac   Level: developer
31553e0a2f3SToby Isaac 
31653e0a2f3SToby Isaac   Note:
31753e0a2f3SToby Isaac   After `PetscLogHandlerStop()`, the handler can still access the global logging state
31853e0a2f3SToby Isaac   with `PetscLogHandlerGetState()`, so that it can access the registry when post-processing
31953e0a2f3SToby Isaac   (for instance, in `PetscLogHandlerView()`),
32053e0a2f3SToby Isaac 
32153e0a2f3SToby Isaac   When a log handler is stopped, the remaining stages will be popped before it is
32253e0a2f3SToby Isaac   disconnected from the log stream.
32353e0a2f3SToby Isaac 
32453e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogState`, `PetscLogHandlerStart()`
32553e0a2f3SToby Isaac @*/
32653e0a2f3SToby Isaac PetscErrorCode PetscLogHandlerStop(PetscLogHandler h)
32753e0a2f3SToby Isaac {
32853e0a2f3SToby Isaac   PetscFunctionBegin;
32953e0a2f3SToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
33053e0a2f3SToby Isaac     if (PetscLogHandlers[i].handler == h) {
33153e0a2f3SToby Isaac       if (petsc_log_state) {
33253e0a2f3SToby Isaac         PetscLogState state;
33353e0a2f3SToby Isaac         PetscLogStage stack_height;
33453e0a2f3SToby Isaac         PetscIntStack orig_stack, temp_stack;
33553e0a2f3SToby Isaac 
33653e0a2f3SToby Isaac         PetscCall(PetscLogHandlerGetState(h, &state));
33753e0a2f3SToby Isaac         PetscCheck(state == petsc_log_state, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONGSTATE, "Called PetscLogHandlerStop() for a PetscLogHander that was not started.");
33853e0a2f3SToby Isaac         stack_height = petsc_log_state->stage_stack->top + 1;
33953e0a2f3SToby Isaac         PetscCall(PetscIntStackCreate(&temp_stack));
34053e0a2f3SToby Isaac         orig_stack                   = petsc_log_state->stage_stack;
34153e0a2f3SToby Isaac         petsc_log_state->stage_stack = temp_stack;
34253e0a2f3SToby Isaac         for (int s = 0; s < stack_height; s++) {
34353e0a2f3SToby Isaac           PetscLogStage stage = (PetscLogStage)orig_stack->stack[s];
34453e0a2f3SToby Isaac 
34553e0a2f3SToby Isaac           PetscCall(PetscIntStackPush(temp_stack, stage));
34653e0a2f3SToby Isaac         }
34753e0a2f3SToby Isaac         for (int s = 0; s < stack_height; s++) {
34853e0a2f3SToby Isaac           PetscLogStage stage;
34953e0a2f3SToby Isaac           PetscBool     empty;
35053e0a2f3SToby Isaac 
35153e0a2f3SToby Isaac           PetscCall(PetscIntStackPop(temp_stack, &stage));
35253e0a2f3SToby Isaac           PetscCall(PetscIntStackEmpty(temp_stack, &empty));
35353e0a2f3SToby Isaac           if (!empty) {
35453e0a2f3SToby Isaac             PetscCall(PetscIntStackTop(temp_stack, &petsc_log_state->current_stage));
35553e0a2f3SToby Isaac           } else petsc_log_state->current_stage = -1;
35653e0a2f3SToby Isaac           PetscCall(PetscLogHandlerStagePop(h, stage));
35753e0a2f3SToby Isaac         }
35853e0a2f3SToby Isaac         PetscCall(PetscIntStackDestroy(temp_stack));
35953e0a2f3SToby Isaac         petsc_log_state->stage_stack = orig_stack;
36053e0a2f3SToby Isaac         PetscCall(PetscIntStackTop(petsc_log_state->stage_stack, &petsc_log_state->current_stage));
36153e0a2f3SToby Isaac       }
36253e0a2f3SToby Isaac       PetscCall(PetscArrayzero(&PetscLogHandlers[i], 1));
36353e0a2f3SToby Isaac       PetscCall(PetscObjectDereference((PetscObject)h));
36453e0a2f3SToby Isaac     }
36553e0a2f3SToby Isaac   }
36653e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
36753e0a2f3SToby Isaac }
3685c6c1daeSBarry Smith 
3695c6c1daeSBarry Smith /*@C
3704dd65854SConnor Ward   PetscLogIsActive - Check if logging is currently in progress.
3714dd65854SConnor Ward 
3724dd65854SConnor Ward   Not Collective
3734dd65854SConnor Ward 
3744dd65854SConnor Ward   Output Parameter:
375811af0c4SBarry Smith . isActive - `PETSC_TRUE` if logging is in progress, `PETSC_FALSE` otherwise
3764dd65854SConnor Ward 
3774dd65854SConnor Ward   Level: beginner
3784dd65854SConnor Ward 
379b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogDefaultBegin()`
3804dd65854SConnor Ward @*/
381d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogIsActive(PetscBool *isActive)
382d71ae5a4SJacob Faibussowitsch {
3834dd65854SConnor Ward   PetscFunctionBegin;
384b665b14eSToby Isaac   *isActive = PETSC_FALSE;
385b665b14eSToby Isaac   if (petsc_log_state) {
386b665b14eSToby Isaac     for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
387b665b14eSToby Isaac       if (PetscLogHandlers[i].handler) {
388b665b14eSToby Isaac         *isActive = PETSC_TRUE;
389b665b14eSToby Isaac         PetscFunctionReturn(PETSC_SUCCESS);
390b665b14eSToby Isaac       }
391b665b14eSToby Isaac     }
392b665b14eSToby Isaac   }
393b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
394b665b14eSToby Isaac }
395b665b14eSToby Isaac 
396b665b14eSToby Isaac PETSC_UNUSED static PetscErrorCode PetscLogEventBeginIsActive(PetscBool *isActive)
397b665b14eSToby Isaac {
398b665b14eSToby Isaac   PetscFunctionBegin;
399b665b14eSToby Isaac   *isActive = PETSC_FALSE;
400b665b14eSToby Isaac   if (petsc_log_state) {
401b665b14eSToby Isaac     for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
402b665b14eSToby Isaac       if (PetscLogHandlers[i].eventBegin) {
403b665b14eSToby Isaac         *isActive = PETSC_TRUE;
404b665b14eSToby Isaac         PetscFunctionReturn(PETSC_SUCCESS);
405b665b14eSToby Isaac       }
406b665b14eSToby Isaac     }
407b665b14eSToby Isaac   }
408b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
409b665b14eSToby Isaac }
410b665b14eSToby Isaac 
411b665b14eSToby Isaac PETSC_UNUSED static PetscErrorCode PetscLogEventEndIsActive(PetscBool *isActive)
412b665b14eSToby Isaac {
413b665b14eSToby Isaac   PetscFunctionBegin;
414b665b14eSToby Isaac   *isActive = PETSC_FALSE;
415b665b14eSToby Isaac   if (petsc_log_state) {
416b665b14eSToby Isaac     for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
417b665b14eSToby Isaac       if (PetscLogHandlers[i].eventEnd) {
418b665b14eSToby Isaac         *isActive = PETSC_TRUE;
419b665b14eSToby Isaac         PetscFunctionReturn(PETSC_SUCCESS);
420b665b14eSToby Isaac       }
421b665b14eSToby Isaac     }
422b665b14eSToby Isaac   }
4233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4244dd65854SConnor Ward }
4254dd65854SConnor Ward 
42653e0a2f3SToby Isaac static PetscErrorCode PetscLogTypeBegin(PetscLogHandlerType type)
42753e0a2f3SToby Isaac {
42853e0a2f3SToby Isaac   PetscLogHandler handler;
42953e0a2f3SToby Isaac 
43053e0a2f3SToby Isaac   PetscFunctionBegin;
43153e0a2f3SToby Isaac   PetscCall(PetscLogTryGetHandler(type, &handler));
43253e0a2f3SToby Isaac   if (handler) PetscFunctionReturn(PETSC_SUCCESS);
43353e0a2f3SToby Isaac   PetscCall(PetscLogHandlerCreate(PETSC_COMM_WORLD, &handler));
43453e0a2f3SToby Isaac   PetscCall(PetscLogHandlerSetType(handler, type));
43553e0a2f3SToby Isaac   PetscCall(PetscLogHandlerStart(handler));
43653e0a2f3SToby Isaac   PetscCall(PetscLogHandlerDestroy(&handler));
43753e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
43853e0a2f3SToby Isaac }
43953e0a2f3SToby Isaac 
4404dd65854SConnor Ward /*@C
441b665b14eSToby Isaac   PetscLogDefaultBegin - Turns on logging of objects and events using the default log handler. This logs flop
4425c6c1daeSBarry Smith   rates and object creation and should not slow programs down too much.
4435c6c1daeSBarry Smith   This routine may be called more than once.
4445c6c1daeSBarry Smith 
445811af0c4SBarry Smith   Logically Collective over `PETSC_COMM_WORLD`
4465c6c1daeSBarry Smith 
447811af0c4SBarry Smith   Options Database Key:
448a2553e36SBarry Smith . -log_view [viewertype:filename:viewerformat] - Prints summary of flop and timing information to the
449a2553e36SBarry Smith                   screen (for code configured with --with-log=1 (which is the default))
4505c6c1daeSBarry Smith 
45110450e9eSJacob Faibussowitsch   Example Usage:
4525c6c1daeSBarry Smith .vb
4535c6c1daeSBarry Smith       PetscInitialize(...);
454bb1d7374SBarry Smith       PetscLogDefaultBegin();
4555c6c1daeSBarry Smith        ... code ...
4565c6c1daeSBarry Smith       PetscLogView(viewer); or PetscLogDump();
4575c6c1daeSBarry Smith       PetscFinalize();
4585c6c1daeSBarry Smith .ve
4595c6c1daeSBarry Smith 
460d1f92df0SBarry Smith   Level: advanced
461d1f92df0SBarry Smith 
462811af0c4SBarry Smith   Note:
463811af0c4SBarry Smith   `PetscLogView()` or `PetscLogDump()` actually cause the printing of
4645c6c1daeSBarry Smith   the logging information.
4655c6c1daeSBarry Smith 
466b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogDump()`, `PetscLogView()`, `PetscLogTraceBegin()`
4675c6c1daeSBarry Smith @*/
468d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogDefaultBegin(void)
469d71ae5a4SJacob Faibussowitsch {
4705c6c1daeSBarry Smith   PetscFunctionBegin;
471b665b14eSToby Isaac   PetscCall(PetscLogTypeBegin(PETSC_LOG_HANDLER_DEFAULT));
4723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4735c6c1daeSBarry Smith }
4745c6c1daeSBarry Smith 
4755c6c1daeSBarry Smith /*@C
476b665b14eSToby Isaac   PetscLogTraceBegin - Begins trace logging.  Every time a PETSc event
4775c6c1daeSBarry Smith   begins or ends, the event name is printed.
4785c6c1daeSBarry Smith 
479811af0c4SBarry Smith   Logically Collective on `PETSC_COMM_WORLD`
4805c6c1daeSBarry Smith 
4815c6c1daeSBarry Smith   Input Parameter:
4825c6c1daeSBarry Smith . file - The file to print trace in (e.g. stdout)
4835c6c1daeSBarry Smith 
4845c6c1daeSBarry Smith   Options Database Key:
485b665b14eSToby Isaac . -log_trace [filename] - Begins `PetscLogTraceBegin()`
4865c6c1daeSBarry Smith 
487d1f92df0SBarry Smith   Level: intermediate
488d1f92df0SBarry Smith 
4895c6c1daeSBarry Smith   Notes:
490811af0c4SBarry Smith   `PetscLogTraceBegin()` prints the processor number, the execution time (sec),
4915c6c1daeSBarry Smith   then "Event begin:" or "Event end:" followed by the event name.
4925c6c1daeSBarry Smith 
493811af0c4SBarry Smith   `PetscLogTraceBegin()` allows tracing of all PETSc calls, which is useful
4945c6c1daeSBarry Smith   to determine where a program is hanging without running in the
4955c6c1daeSBarry Smith   debugger.  Can be used in conjunction with the -info option.
4965c6c1daeSBarry Smith 
497b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogDump()`, `PetscLogView()`, `PetscLogDefaultBegin()`
4985c6c1daeSBarry Smith @*/
499d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogTraceBegin(FILE *file)
500d71ae5a4SJacob Faibussowitsch {
501b665b14eSToby Isaac   PetscLogHandler handler;
5025c6c1daeSBarry Smith   PetscFunctionBegin;
503b665b14eSToby Isaac   PetscCall(PetscLogTryGetHandler(PETSC_LOG_HANDLER_TRACE, &handler));
504b665b14eSToby Isaac   if (handler) PetscFunctionReturn(PETSC_SUCCESS);
505b665b14eSToby Isaac   PetscCall(PetscLogHandlerCreateTrace(PETSC_COMM_WORLD, file, &handler));
506b665b14eSToby Isaac   PetscCall(PetscLogHandlerStart(handler));
507b665b14eSToby Isaac   PetscCall(PetscLogHandlerDestroy(&handler));
508b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
509b665b14eSToby Isaac }
510a297a907SKarl Rupp 
511b665b14eSToby Isaac PETSC_INTERN PetscErrorCode PetscLogHandlerCreate_Nested(MPI_Comm, PetscLogHandler *);
512b665b14eSToby Isaac 
513b665b14eSToby Isaac /*@C
514b665b14eSToby Isaac   PetscLogNestedBegin - Turns on nested logging of objects and events. This logs flop
515b665b14eSToby Isaac   rates and object creation and should not slow programs down too much.
516b665b14eSToby Isaac 
517b665b14eSToby Isaac   Logically Collective over `PETSC_COMM_WORLD`
518b665b14eSToby Isaac 
519b665b14eSToby Isaac   Options Database Keys:
520b665b14eSToby Isaac . -log_view :filename.xml:ascii_xml - Prints an XML summary of flop and timing information to the file
521b665b14eSToby Isaac 
522b665b14eSToby Isaac   Example Usage:
523b665b14eSToby Isaac .vb
524b665b14eSToby Isaac       PetscInitialize(...);
525b665b14eSToby Isaac       PetscLogNestedBegin();
526b665b14eSToby Isaac        ... code ...
527b665b14eSToby Isaac       PetscLogView(viewer);
528b665b14eSToby Isaac       PetscFinalize();
529b665b14eSToby Isaac .ve
530b665b14eSToby Isaac 
531b665b14eSToby Isaac   Level: advanced
532b665b14eSToby Isaac 
533b665b14eSToby Isaac .seealso: `PetscLogDump()`, `PetscLogView()`, `PetscLogTraceBegin()`, `PetscLogDefaultBegin()`
534b665b14eSToby Isaac @*/
535b665b14eSToby Isaac PetscErrorCode PetscLogNestedBegin(void)
536b665b14eSToby Isaac {
537b665b14eSToby Isaac   PetscFunctionBegin;
538b665b14eSToby Isaac   PetscCall(PetscLogTypeBegin(PETSC_LOG_HANDLER_NESTED));
5393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5405c6c1daeSBarry Smith }
5415c6c1daeSBarry Smith 
54253e0a2f3SToby Isaac /*@C
54353e0a2f3SToby Isaac   PetscLogLegacyCallbacksBegin - Create and start a log handler from callbacks
54453e0a2f3SToby Isaac   matching the now deprecated function pointers `PetscLogPLB`, `PetscLogPLE`,
54553e0a2f3SToby Isaac   `PetscLogPHC`, `PetscLogPHD`.
54653e0a2f3SToby Isaac 
54753e0a2f3SToby Isaac   Logically Collective over `PETSC_COMM_WORLD`
54853e0a2f3SToby Isaac 
54953e0a2f3SToby Isaac   Input Parameters:
55053e0a2f3SToby Isaac + PetscLogPLB - A callback that will be executed by `PetscLogEventBegin()` (or `NULL`)
55153e0a2f3SToby Isaac . PetscLogPLE - A callback that will be executed by `PetscLogEventEnd()` (or `NULL`)
55253e0a2f3SToby Isaac . PetscLogPHC - A callback that will be executed by `PetscLogObjectCreate()` (or `NULL`)
55353e0a2f3SToby Isaac - PetscLogPHD - A callback that will be executed by `PetscLogObjectCreate()` (or `NULL`)
55453e0a2f3SToby Isaac 
55553e0a2f3SToby Isaac   Calling sequence of `PetscLogPLB`:
55653e0a2f3SToby Isaac + e  - a `PetscLogEvent` that is beginning
55753e0a2f3SToby Isaac . _i - deprecated, unused
55853e0a2f3SToby Isaac . o1 - a `PetscObject` associated with `e` (or `NULL`)
55953e0a2f3SToby Isaac . o2 - a `PetscObject` associated with `e` (or `NULL`)
56053e0a2f3SToby Isaac . o3 - a `PetscObject` associated with `e` (or `NULL`)
56153e0a2f3SToby Isaac - o4 - a `PetscObject` associated with `e` (or `NULL`)
56253e0a2f3SToby Isaac 
56353e0a2f3SToby Isaac   Calling sequence of `PetscLogPLE`:
56453e0a2f3SToby Isaac + e  - a `PetscLogEvent` that is beginning
56553e0a2f3SToby Isaac . _i - deprecated, unused
56653e0a2f3SToby Isaac . o1 - a `PetscObject` associated with `e` (or `NULL`)
56753e0a2f3SToby Isaac . o2 - a `PetscObject` associated with `e` (or `NULL`)
56853e0a2f3SToby Isaac . o3 - a `PetscObject` associated with `e` (or `NULL`)
56953e0a2f3SToby Isaac - o4 - a `PetscObject` associated with `e` (or `NULL`)
57053e0a2f3SToby Isaac 
57153e0a2f3SToby Isaac   Calling sequence of `PetscLogPHC`:
57253e0a2f3SToby Isaac . o - a `PetscObject` that has just been created
57353e0a2f3SToby Isaac 
57453e0a2f3SToby Isaac   Calling sequence of `PetscLogPHD`:
57553e0a2f3SToby Isaac . o - a `PetscObject` that is about to be destroyed
57653e0a2f3SToby Isaac 
57753e0a2f3SToby Isaac   Level: advanced
57853e0a2f3SToby Isaac 
57953e0a2f3SToby Isaac   Notes:
58053e0a2f3SToby Isaac   This is for transitioning from the deprecated function `PetscLogSet()` and should not be used in new code.
58153e0a2f3SToby Isaac 
58253e0a2f3SToby Isaac   This should help migrate external log handlers to use `PetscLogHandler`, but
58353e0a2f3SToby Isaac   callbacks that depend on the deprecated `PetscLogStage` datatype will have to be
58453e0a2f3SToby Isaac   updated.
58553e0a2f3SToby Isaac 
58653e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogHandlerStart()`, `PetscLogState`
58753e0a2f3SToby Isaac @*/
58853e0a2f3SToby 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))
58953e0a2f3SToby Isaac {
59053e0a2f3SToby Isaac   PetscLogHandler handler;
59153e0a2f3SToby Isaac 
59253e0a2f3SToby Isaac   PetscFunctionBegin;
59353e0a2f3SToby Isaac   PetscCall(PetscLogHandlerCreateLegacy(PETSC_COMM_WORLD, PetscLogPLB, PetscLogPLE, PetscLogPHC, PetscLogPHD, &handler));
59453e0a2f3SToby Isaac   PetscCall(PetscLogHandlerStart(handler));
59553e0a2f3SToby Isaac   PetscCall(PetscLogHandlerDestroy(&handler));
59653e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
59753e0a2f3SToby Isaac }
59853e0a2f3SToby Isaac 
5992611ad71SToby Isaac   #if defined(PETSC_HAVE_MPE)
6002611ad71SToby Isaac     #include <mpe.h>
6012611ad71SToby Isaac static PetscBool PetscBeganMPE = PETSC_FALSE;
6022611ad71SToby Isaac   #endif
6032611ad71SToby Isaac 
6042611ad71SToby Isaac /*@C
6052611ad71SToby Isaac   PetscLogMPEBegin - Turns on MPE logging of events. This creates large log files and slows the
6062611ad71SToby Isaac   program down.
6072611ad71SToby Isaac 
6082611ad71SToby Isaac   Collective over `PETSC_COMM_WORLD`
6092611ad71SToby Isaac 
6102611ad71SToby Isaac   Options Database Key:
6112611ad71SToby Isaac . -log_mpe - Prints extensive log information
6122611ad71SToby Isaac 
6132611ad71SToby Isaac   Level: advanced
6142611ad71SToby Isaac 
6152611ad71SToby Isaac   Note:
6162611ad71SToby Isaac   A related routine is `PetscLogDefaultBegin()` (with the options key `-log_view`), which is
6172611ad71SToby Isaac   intended for production runs since it logs only flop rates and object creation (and should
6182611ad71SToby Isaac   not significantly slow the programs).
6192611ad71SToby Isaac 
620b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogDump()`, `PetscLogDefaultBegin()`, `PetscLogEventActivate()`,
6212611ad71SToby Isaac           `PetscLogEventDeactivate()`
6222611ad71SToby Isaac @*/
6232611ad71SToby Isaac PetscErrorCode PetscLogMPEBegin(void)
6242611ad71SToby Isaac {
6252611ad71SToby Isaac   PetscFunctionBegin;
6262611ad71SToby Isaac   #if defined(PETSC_HAVE_MPE)
6272611ad71SToby Isaac   /* Do MPE initialization */
6282611ad71SToby Isaac   if (!MPE_Initialized_logging()) { /* This function exists in mpich 1.1.2 and higher */
6292611ad71SToby Isaac     PetscCall(PetscInfo(0, "Initializing MPE.\n"));
6302611ad71SToby Isaac     PetscCall(MPE_Init_log());
6312611ad71SToby Isaac 
6322611ad71SToby Isaac     PetscBeganMPE = PETSC_TRUE;
6332611ad71SToby Isaac   } else {
6342611ad71SToby Isaac     PetscCall(PetscInfo(0, "MPE already initialized. Not attempting to reinitialize.\n"));
6352611ad71SToby Isaac   }
636b665b14eSToby Isaac   PetscCall(PetscLogTypeBegin(PETSC_LOG_HANDLER_MPE));
6372611ad71SToby Isaac   #else
6382611ad71SToby Isaac   SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP_SYS, "PETSc was configured without MPE support, reconfigure with --with-mpe or --download-mpe");
6392611ad71SToby Isaac   #endif
6402611ad71SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
6412611ad71SToby Isaac }
6422611ad71SToby Isaac 
64353e0a2f3SToby Isaac   #if defined(PETSC_HAVE_TAU_PERFSTUBS)
64453e0a2f3SToby Isaac     #include <../src/sys/perfstubs/timer.h>
64553e0a2f3SToby Isaac   #endif
64653e0a2f3SToby Isaac 
64753e0a2f3SToby Isaac /*@C
64853e0a2f3SToby Isaac   PetscLogPerfstubsBegin - Turns on logging of events using the perfstubs interface.
64953e0a2f3SToby Isaac 
65053e0a2f3SToby Isaac   Collective over `PETSC_COMM_WORLD`
65153e0a2f3SToby Isaac 
65253e0a2f3SToby Isaac   Options Database Key:
65353e0a2f3SToby Isaac . -log_perfstubs - use an external log handler through the perfstubs interface
65453e0a2f3SToby Isaac 
65553e0a2f3SToby Isaac   Level: advanced
65653e0a2f3SToby Isaac 
65753e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogDefaultBegin()`, `PetscLogEventActivate()`
65853e0a2f3SToby Isaac @*/
65953e0a2f3SToby Isaac PetscErrorCode PetscLogPerfstubsBegin(void)
66053e0a2f3SToby Isaac {
66153e0a2f3SToby Isaac   PetscFunctionBegin;
66253e0a2f3SToby Isaac   #if defined(PETSC_HAVE_TAU_PERFSTUBS)
66353e0a2f3SToby Isaac   PetscCall(PetscLogTypeBegin(PETSC_LOG_HANDLER_PERFSTUBS));
66453e0a2f3SToby Isaac   #else
66553e0a2f3SToby Isaac   SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP_SYS, "PETSc was configured without perfstubs support, reconfigure with --with-tau-perfstubs");
66653e0a2f3SToby Isaac   #endif
66753e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
66853e0a2f3SToby Isaac }
66953e0a2f3SToby Isaac 
6705c6c1daeSBarry Smith /*@
671b665b14eSToby Isaac   PetscLogActions - Determines whether actions are logged for the default log handler.
6725c6c1daeSBarry Smith 
6735c6c1daeSBarry Smith   Not Collective
6745c6c1daeSBarry Smith 
6755c6c1daeSBarry Smith   Input Parameter:
676811af0c4SBarry Smith . flag - `PETSC_TRUE` if actions are to be logged
677811af0c4SBarry Smith 
678811af0c4SBarry Smith   Options Database Key:
679b665b14eSToby Isaac + -log_exclude_actions - (deprecated) Does nothing
680b665b14eSToby Isaac - -log_include_actions - Turn on action logging
6815c6c1daeSBarry Smith 
6825c6c1daeSBarry Smith   Level: intermediate
6835c6c1daeSBarry Smith 
684811af0c4SBarry Smith   Note:
685811af0c4SBarry Smith   Logging of actions continues to consume more memory as the program
6865c6c1daeSBarry Smith   runs. Long running programs should consider turning this feature off.
687aec76313SJacob Faibussowitsch 
688b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogGetDefaultHandler()`
6895c6c1daeSBarry Smith @*/
690d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogActions(PetscBool flag)
691d71ae5a4SJacob Faibussowitsch {
692b665b14eSToby Isaac   PetscLogHandler handler;
693b665b14eSToby Isaac 
6945c6c1daeSBarry Smith   PetscFunctionBegin;
695b665b14eSToby Isaac   PetscCall(PetscLogTryGetHandler(PETSC_LOG_HANDLER_DEFAULT, &handler));
696b665b14eSToby Isaac   if (handler) PetscCall(PetscLogHandlerDefaultSetLogActions(handler, flag));
6973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6985c6c1daeSBarry Smith }
6995c6c1daeSBarry Smith 
7005c6c1daeSBarry Smith /*@
7015c6c1daeSBarry Smith   PetscLogObjects - Determines whether objects are logged for the graphical viewer.
7025c6c1daeSBarry Smith 
7035c6c1daeSBarry Smith   Not Collective
7045c6c1daeSBarry Smith 
7055c6c1daeSBarry Smith   Input Parameter:
706811af0c4SBarry Smith . flag - `PETSC_TRUE` if objects are to be logged
707811af0c4SBarry Smith 
708811af0c4SBarry Smith   Options Database Key:
709b665b14eSToby Isaac + -log_exclude_objects - (deprecated) Does nothing
710b665b14eSToby Isaac - -log_include_objects - Turns on object logging
7115c6c1daeSBarry Smith 
7125c6c1daeSBarry Smith   Level: intermediate
7135c6c1daeSBarry Smith 
714811af0c4SBarry Smith   Note:
715811af0c4SBarry Smith   Logging of objects continues to consume more memory as the program
7165c6c1daeSBarry Smith   runs. Long running programs should consider turning this feature off.
7175c6c1daeSBarry Smith 
718b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogGetDefaultHandler()`
7195c6c1daeSBarry Smith @*/
720d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogObjects(PetscBool flag)
721d71ae5a4SJacob Faibussowitsch {
722b665b14eSToby Isaac   PetscLogHandler handler;
723b665b14eSToby Isaac 
7245c6c1daeSBarry Smith   PetscFunctionBegin;
725b665b14eSToby Isaac   PetscCall(PetscLogTryGetHandler(PETSC_LOG_HANDLER_DEFAULT, &handler));
726b665b14eSToby Isaac   if (handler) PetscCall(PetscLogHandlerDefaultSetLogObjects(handler, flag));
7273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7285c6c1daeSBarry Smith }
7295c6c1daeSBarry Smith 
7305c6c1daeSBarry Smith /*------------------------------------------------ Stage Functions --------------------------------------------------*/
7315c6c1daeSBarry Smith /*@C
73274c0405dSRichard Tran Mills   PetscLogStageRegister - Attaches a character string name to a logging stage.
7335c6c1daeSBarry Smith 
7345c6c1daeSBarry Smith   Not Collective
7355c6c1daeSBarry Smith 
7365c6c1daeSBarry Smith   Input Parameter:
7375c6c1daeSBarry Smith . sname - The name to associate with that stage
7385c6c1daeSBarry Smith 
7395c6c1daeSBarry Smith   Output Parameter:
740b665b14eSToby Isaac . stage - The stage number or -1 if logging is not active (`PetscLogIsActive()`).
7415c6c1daeSBarry Smith 
7425c6c1daeSBarry Smith   Level: intermediate
7435c6c1daeSBarry Smith 
744d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStagePush()`, `PetscLogStagePop()`
7455c6c1daeSBarry Smith @*/
746d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStageRegister(const char sname[], PetscLogStage *stage)
747d71ae5a4SJacob Faibussowitsch {
748b665b14eSToby Isaac   PetscLogState state;
7495c6c1daeSBarry Smith 
7505c6c1daeSBarry Smith   PetscFunctionBegin;
751b665b14eSToby Isaac   *stage = -1;
752b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
753b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateStageRegister(state, sname, stage));
7543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7555c6c1daeSBarry Smith }
7565c6c1daeSBarry Smith 
7575c6c1daeSBarry Smith /*@C
758811af0c4SBarry Smith   PetscLogStagePush - This function pushes a stage on the logging stack. Events started and stopped until `PetscLogStagePop()` will be associated with the stage
7595c6c1daeSBarry Smith 
7605c6c1daeSBarry Smith   Not Collective
7615c6c1daeSBarry Smith 
7625c6c1daeSBarry Smith   Input Parameter:
7635c6c1daeSBarry Smith . stage - The stage on which to log
7645c6c1daeSBarry Smith 
76510450e9eSJacob Faibussowitsch   Example Usage:
766811af0c4SBarry Smith   If the option -log_view is used to run the program containing the
7675c6c1daeSBarry Smith   following code, then 2 sets of summary data will be printed during
7685c6c1daeSBarry Smith   PetscFinalize().
7695c6c1daeSBarry Smith .vb
7705c6c1daeSBarry Smith       PetscInitialize(int *argc,char ***args,0,0);
7715c6c1daeSBarry Smith       [stage 0 of code]
7725c6c1daeSBarry Smith       PetscLogStagePush(1);
7735c6c1daeSBarry Smith       [stage 1 of code]
7745c6c1daeSBarry Smith       PetscLogStagePop();
7755c6c1daeSBarry Smith       PetscBarrier(...);
7765c6c1daeSBarry Smith       [more stage 0 of code]
7775c6c1daeSBarry Smith       PetscFinalize();
7785c6c1daeSBarry Smith .ve
7795c6c1daeSBarry Smith 
780d1f92df0SBarry Smith   Level: intermediate
781d1f92df0SBarry Smith 
782811af0c4SBarry Smith   Note:
783811af0c4SBarry Smith   Use `PetscLogStageRegister()` to register a stage.
7845c6c1daeSBarry Smith 
785d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStagePop()`, `PetscLogStageRegister()`, `PetscBarrier()`
7865c6c1daeSBarry Smith @*/
787d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStagePush(PetscLogStage stage)
788d71ae5a4SJacob Faibussowitsch {
789b665b14eSToby Isaac   PetscLogState state;
7905c6c1daeSBarry Smith 
7915c6c1daeSBarry Smith   PetscFunctionBegin;
792b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
793b665b14eSToby Isaac   if (!state) PetscFunctionReturn(PETSC_SUCCESS);
794b665b14eSToby Isaac   for (int i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
795b665b14eSToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
796b665b14eSToby Isaac     if (h) PetscCall(PetscLogHandlerStagePush(h, stage));
797b665b14eSToby Isaac   }
798b665b14eSToby Isaac   PetscCall(PetscLogStateStagePush(state, stage));
7993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8005c6c1daeSBarry Smith }
8015c6c1daeSBarry Smith 
8025c6c1daeSBarry Smith /*@C
803811af0c4SBarry Smith   PetscLogStagePop - This function pops a stage from the logging stack that was pushed with `PetscLogStagePush()`
8045c6c1daeSBarry Smith 
8055c6c1daeSBarry Smith   Not Collective
8065c6c1daeSBarry Smith 
80710450e9eSJacob Faibussowitsch   Example Usage:
808811af0c4SBarry Smith   If the option -log_view is used to run the program containing the
8095c6c1daeSBarry Smith   following code, then 2 sets of summary data will be printed during
8105c6c1daeSBarry Smith   PetscFinalize().
8115c6c1daeSBarry Smith .vb
8125c6c1daeSBarry Smith       PetscInitialize(int *argc,char ***args,0,0);
8135c6c1daeSBarry Smith       [stage 0 of code]
8145c6c1daeSBarry Smith       PetscLogStagePush(1);
8155c6c1daeSBarry Smith       [stage 1 of code]
8165c6c1daeSBarry Smith       PetscLogStagePop();
8175c6c1daeSBarry Smith       PetscBarrier(...);
8185c6c1daeSBarry Smith       [more stage 0 of code]
8195c6c1daeSBarry Smith       PetscFinalize();
8205c6c1daeSBarry Smith .ve
8215c6c1daeSBarry Smith 
8225c6c1daeSBarry Smith   Level: intermediate
8235c6c1daeSBarry Smith 
824d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStagePush()`, `PetscLogStageRegister()`, `PetscBarrier()`
8255c6c1daeSBarry Smith @*/
826d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStagePop(void)
827d71ae5a4SJacob Faibussowitsch {
828b665b14eSToby Isaac   PetscLogState state;
829b665b14eSToby Isaac   PetscLogStage current_stage;
8305c6c1daeSBarry Smith 
8315c6c1daeSBarry Smith   PetscFunctionBegin;
832b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
833b665b14eSToby Isaac   if (!state) PetscFunctionReturn(PETSC_SUCCESS);
834b665b14eSToby Isaac   current_stage = state->current_stage;
835b665b14eSToby Isaac   PetscCall(PetscLogStateStagePop(state));
836b665b14eSToby Isaac   for (int i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
837b665b14eSToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
838b665b14eSToby Isaac     if (h) PetscCall(PetscLogHandlerStagePop(h, current_stage));
839b665b14eSToby Isaac   }
8403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8415c6c1daeSBarry Smith }
8425c6c1daeSBarry Smith 
8435c6c1daeSBarry Smith /*@
844811af0c4SBarry Smith   PetscLogStageSetActive - Sets if a stage is used for `PetscLogEventBegin()` and `PetscLogEventEnd()`.
8455c6c1daeSBarry Smith 
8465c6c1daeSBarry Smith   Not Collective
8475c6c1daeSBarry Smith 
8485c6c1daeSBarry Smith   Input Parameters:
8495c6c1daeSBarry Smith + stage    - The stage
850811af0c4SBarry Smith - isActive - The activity flag, `PETSC_TRUE` for logging, else `PETSC_FALSE` (defaults to `PETSC_TRUE`)
8515c6c1daeSBarry Smith 
8525c6c1daeSBarry Smith   Level: intermediate
8535c6c1daeSBarry Smith 
854811af0c4SBarry Smith   Note:
855811af0c4SBarry Smith   If this is set to `PETSC_FALSE` the logging acts as if the stage did not exist
856811af0c4SBarry Smith 
857d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()`
8585c6c1daeSBarry Smith @*/
859d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStageSetActive(PetscLogStage stage, PetscBool isActive)
860d71ae5a4SJacob Faibussowitsch {
861b665b14eSToby Isaac   PetscLogState state;
8625c6c1daeSBarry Smith 
8635c6c1daeSBarry Smith   PetscFunctionBegin;
864b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
865b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateStageSetActive(state, stage, isActive));
8663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8675c6c1daeSBarry Smith }
8685c6c1daeSBarry Smith 
8695c6c1daeSBarry Smith /*@
870811af0c4SBarry Smith   PetscLogStageGetActive - Checks if a stage is used for `PetscLogEventBegin()` and `PetscLogEventEnd()`.
8715c6c1daeSBarry Smith 
8725c6c1daeSBarry Smith   Not Collective
8735c6c1daeSBarry Smith 
8745c6c1daeSBarry Smith   Input Parameter:
8755c6c1daeSBarry Smith . stage - The stage
8765c6c1daeSBarry Smith 
8775c6c1daeSBarry Smith   Output Parameter:
878811af0c4SBarry Smith . isActive - The activity flag, `PETSC_TRUE` for logging, else `PETSC_FALSE` (defaults to `PETSC_TRUE`)
8795c6c1daeSBarry Smith 
8805c6c1daeSBarry Smith   Level: intermediate
8815c6c1daeSBarry Smith 
882d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()`
8835c6c1daeSBarry Smith @*/
884d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStageGetActive(PetscLogStage stage, PetscBool *isActive)
885d71ae5a4SJacob Faibussowitsch {
886b665b14eSToby Isaac   PetscLogState state;
8875c6c1daeSBarry Smith 
8885c6c1daeSBarry Smith   PetscFunctionBegin;
889b665b14eSToby Isaac   *isActive = PETSC_FALSE;
890b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
891b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateStageGetActive(state, stage, isActive));
8923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8935c6c1daeSBarry Smith }
8945c6c1daeSBarry Smith 
8955c6c1daeSBarry Smith /*@
896811af0c4SBarry Smith   PetscLogStageSetVisible - Determines stage visibility in `PetscLogView()`
8975c6c1daeSBarry Smith 
8985c6c1daeSBarry Smith   Not Collective
8995c6c1daeSBarry Smith 
9005c6c1daeSBarry Smith   Input Parameters:
9015c6c1daeSBarry Smith + stage     - The stage
902811af0c4SBarry Smith - isVisible - The visibility flag, `PETSC_TRUE` to print, else `PETSC_FALSE` (defaults to `PETSC_TRUE`)
9035c6c1daeSBarry Smith 
9045c6c1daeSBarry Smith   Level: intermediate
9055c6c1daeSBarry Smith 
906aec76313SJacob Faibussowitsch   Developer Notes:
907b665b14eSToby Isaac   Visibility only affects the default log handler in `PetscLogView()`: stages that are
908b665b14eSToby Isaac   set to invisible are suppressed from output.
909811af0c4SBarry Smith 
910b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogStageGetVisible()`, `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogView()`, `PetscLogGetDefaultHandler()`
9115c6c1daeSBarry Smith @*/
912d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStageSetVisible(PetscLogStage stage, PetscBool isVisible)
913d71ae5a4SJacob Faibussowitsch {
914b665b14eSToby Isaac   PetscLogHandler handler;
9155c6c1daeSBarry Smith 
9165c6c1daeSBarry Smith   PetscFunctionBegin;
917b665b14eSToby Isaac   PetscCall(PetscLogTryGetHandler(PETSC_LOG_HANDLER_DEFAULT, &handler));
918b665b14eSToby Isaac   if (handler) { PetscCall(PetscLogHandlerDefaultStageSetVisible(handler, stage, isVisible)); }
9193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9205c6c1daeSBarry Smith }
9215c6c1daeSBarry Smith 
9225c6c1daeSBarry Smith /*@
923811af0c4SBarry Smith   PetscLogStageGetVisible - Returns stage visibility in `PetscLogView()`
9245c6c1daeSBarry Smith 
9255c6c1daeSBarry Smith   Not Collective
9265c6c1daeSBarry Smith 
9275c6c1daeSBarry Smith   Input Parameter:
9285c6c1daeSBarry Smith . stage - The stage
9295c6c1daeSBarry Smith 
9305c6c1daeSBarry Smith   Output Parameter:
931811af0c4SBarry Smith . isVisible - The visibility flag, `PETSC_TRUE` to print, else `PETSC_FALSE` (defaults to `PETSC_TRUE`)
9325c6c1daeSBarry Smith 
9335c6c1daeSBarry Smith   Level: intermediate
9345c6c1daeSBarry Smith 
935b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogStageSetVisible()`, `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogView()`, `PetscLogGetDefaultHandler()`
9365c6c1daeSBarry Smith @*/
937d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStageGetVisible(PetscLogStage stage, PetscBool *isVisible)
938d71ae5a4SJacob Faibussowitsch {
939b665b14eSToby Isaac   PetscLogHandler handler;
9405c6c1daeSBarry Smith 
9415c6c1daeSBarry Smith   PetscFunctionBegin;
942b665b14eSToby Isaac   *isVisible = PETSC_FALSE;
943b665b14eSToby Isaac   PetscCall(PetscLogTryGetHandler(PETSC_LOG_HANDLER_DEFAULT, &handler));
944b665b14eSToby Isaac   if (handler) { PetscCall(PetscLogHandlerDefaultStageGetVisible(handler, stage, isVisible)); }
9453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9465c6c1daeSBarry Smith }
9475c6c1daeSBarry Smith 
9485c6c1daeSBarry Smith /*@C
9495c6c1daeSBarry Smith   PetscLogStageGetId - Returns the stage id when given the stage name.
9505c6c1daeSBarry Smith 
9515c6c1daeSBarry Smith   Not Collective
9525c6c1daeSBarry Smith 
9535c6c1daeSBarry Smith   Input Parameter:
9545c6c1daeSBarry Smith . name - The stage name
9555c6c1daeSBarry Smith 
9565c6c1daeSBarry Smith   Output Parameter:
9575a4a3fabSBarry Smith . stage - The stage, , or -1 if no stage with that name exists
9585c6c1daeSBarry Smith 
9595c6c1daeSBarry Smith   Level: intermediate
9605c6c1daeSBarry Smith 
961d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()`
9625c6c1daeSBarry Smith @*/
963d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStageGetId(const char name[], PetscLogStage *stage)
964d71ae5a4SJacob Faibussowitsch {
965b665b14eSToby Isaac   PetscLogState state;
9665c6c1daeSBarry Smith 
9675c6c1daeSBarry Smith   PetscFunctionBegin;
968b665b14eSToby Isaac   *stage = -1;
969b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
970b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateGetStageFromName(state, name, stage));
9713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9725c6c1daeSBarry Smith }
9735c6c1daeSBarry Smith 
97453e0a2f3SToby Isaac /*@C
97553e0a2f3SToby Isaac   PetscLogStageGetName - Returns the stage name when given the stage id.
97653e0a2f3SToby Isaac 
97753e0a2f3SToby Isaac   Not Collective
97853e0a2f3SToby Isaac 
97953e0a2f3SToby Isaac   Input Parameter:
98053e0a2f3SToby Isaac . stage - The stage
98153e0a2f3SToby Isaac 
98253e0a2f3SToby Isaac   Output Parameter:
98353e0a2f3SToby Isaac . name - The stage name
98453e0a2f3SToby Isaac 
98553e0a2f3SToby Isaac   Level: intermediate
98653e0a2f3SToby Isaac 
98753e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()`
98853e0a2f3SToby Isaac @*/
98953e0a2f3SToby Isaac PetscErrorCode PetscLogStageGetName(PetscLogStage stage, const char **name)
99053e0a2f3SToby Isaac {
99153e0a2f3SToby Isaac   PetscLogStageInfo stage_info;
99253e0a2f3SToby Isaac   PetscLogState     state;
99353e0a2f3SToby Isaac 
99453e0a2f3SToby Isaac   PetscFunctionBegin;
995b665b14eSToby Isaac   *name = NULL;
99653e0a2f3SToby Isaac   PetscCall(PetscLogGetState(&state));
997b665b14eSToby Isaac   if (!state) PetscFunctionReturn(PETSC_SUCCESS);
99853e0a2f3SToby Isaac   PetscCall(PetscLogStateStageGetInfo(state, stage, &stage_info));
99953e0a2f3SToby Isaac   *name = stage_info.name;
100053e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
100153e0a2f3SToby Isaac }
100253e0a2f3SToby Isaac 
10035c6c1daeSBarry Smith /*------------------------------------------------ Event Functions --------------------------------------------------*/
10047a101e5eSJacob Faibussowitsch 
10055c6c1daeSBarry Smith /*@C
1006811af0c4SBarry Smith   PetscLogEventRegister - Registers an event name for logging operations
10075c6c1daeSBarry Smith 
10085c6c1daeSBarry Smith   Not Collective
10095c6c1daeSBarry Smith 
1010d8d19677SJose E. Roman   Input Parameters:
10115c6c1daeSBarry Smith + name    - The name associated with the event
10125c6c1daeSBarry Smith - classid - The classid associated to the class for this event, obtain either with
1013811af0c4SBarry Smith            `PetscClassIdRegister()` or use a predefined one such as `KSP_CLASSID`, `SNES_CLASSID`, the predefined ones
10145c6c1daeSBarry Smith            are only available in C code
10155c6c1daeSBarry Smith 
10165c6c1daeSBarry Smith   Output Parameter:
1017811af0c4SBarry Smith . event - The event id for use with `PetscLogEventBegin()` and `PetscLogEventEnd()`.
10185c6c1daeSBarry Smith 
101910450e9eSJacob Faibussowitsch   Example Usage:
10205c6c1daeSBarry Smith .vb
10215c6c1daeSBarry Smith       PetscLogEvent USER_EVENT;
10225c6c1daeSBarry Smith       PetscClassId classid;
10235c6c1daeSBarry Smith       PetscLogDouble user_event_flops;
10245c6c1daeSBarry Smith       PetscClassIdRegister("class name",&classid);
10255c6c1daeSBarry Smith       PetscLogEventRegister("User event name",classid,&USER_EVENT);
10265c6c1daeSBarry Smith       PetscLogEventBegin(USER_EVENT,0,0,0,0);
10275c6c1daeSBarry Smith          [code segment to monitor]
10285c6c1daeSBarry Smith          PetscLogFlops(user_event_flops);
10295c6c1daeSBarry Smith       PetscLogEventEnd(USER_EVENT,0,0,0,0);
10305c6c1daeSBarry Smith .ve
10315c6c1daeSBarry Smith 
1032d1f92df0SBarry Smith   Level: intermediate
1033d1f92df0SBarry Smith 
10345c6c1daeSBarry Smith   Notes:
10355c6c1daeSBarry Smith   PETSc automatically logs library events if the code has been
1036a2553e36SBarry Smith   configured with --with-log (which is the default) and
1037811af0c4SBarry Smith   -log_view or -log_all is specified.  `PetscLogEventRegister()` is
10385c6c1daeSBarry Smith   intended for logging user events to supplement this PETSc
10395c6c1daeSBarry Smith   information.
10405c6c1daeSBarry Smith 
1041495fc317SBarry Smith   PETSc can gather data for use with the utilities Jumpshot
10425c6c1daeSBarry Smith   (part of the MPICH distribution).  If PETSc has been compiled
10435c6c1daeSBarry Smith   with flag -DPETSC_HAVE_MPE (MPE is an additional utility within
10445c6c1daeSBarry Smith   MPICH), the user can employ another command line option, -log_mpe,
10455c6c1daeSBarry Smith   to create a logfile, "mpe.log", which can be visualized
1046495fc317SBarry Smith   Jumpshot.
10475c6c1daeSBarry Smith 
10485c6c1daeSBarry Smith   The classid is associated with each event so that classes of events
10495c6c1daeSBarry Smith   can be disabled simultaneously, such as all matrix events. The user
1050811af0c4SBarry Smith   can either use an existing classid, such as `MAT_CLASSID`, or create
10515c6c1daeSBarry Smith   their own as shown in the example.
10525c6c1daeSBarry Smith 
1053c5deb1d5SJed Brown   If an existing event with the same name exists, its event handle is
1054c5deb1d5SJed Brown   returned instead of creating a new event.
1055c5deb1d5SJed Brown 
1056d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStageRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogFlops()`,
1057db781477SPatrick Sanan           `PetscLogEventActivate()`, `PetscLogEventDeactivate()`, `PetscClassIdRegister()`
10585c6c1daeSBarry Smith @*/
1059d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventRegister(const char name[], PetscClassId classid, PetscLogEvent *event)
1060d71ae5a4SJacob Faibussowitsch {
1061b665b14eSToby Isaac   PetscLogState state;
10625c6c1daeSBarry Smith 
10635c6c1daeSBarry Smith   PetscFunctionBegin;
1064b665b14eSToby Isaac   *event = -1;
1065b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
1066b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateEventRegister(state, name, classid, event));
10673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
10685c6c1daeSBarry Smith }
10695c6c1daeSBarry Smith 
10705c6c1daeSBarry Smith /*@
1071217044c2SLisandro Dalcin   PetscLogEventSetCollective - Indicates that a particular event is collective.
1072217044c2SLisandro Dalcin 
1073217044c2SLisandro Dalcin   Not Collective
1074217044c2SLisandro Dalcin 
1075d8d19677SJose E. Roman   Input Parameters:
1076217044c2SLisandro Dalcin + event      - The event id
1077d5b43468SJose E. Roman - collective - Boolean flag indicating whether a particular event is collective
1078217044c2SLisandro Dalcin 
1079d1f92df0SBarry Smith   Level: developer
1080d1f92df0SBarry Smith 
1081811af0c4SBarry Smith   Notes:
1082811af0c4SBarry Smith   New events returned from `PetscLogEventRegister()` are collective by default.
1083811af0c4SBarry Smith 
1084811af0c4SBarry Smith   Collective events are handled specially if the -log_sync is used. In that case the logging saves information about
1085811af0c4SBarry Smith   two parts of the event; the time for all the MPI ranks to synchronize and then the time for the actual computation/communication
1086811af0c4SBarry Smith   to be performed. This option is useful to debug imbalance within the computations or communications
1087217044c2SLisandro Dalcin 
1088d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogEventRegister()`
1089217044c2SLisandro Dalcin @*/
1090d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventSetCollective(PetscLogEvent event, PetscBool collective)
1091d71ae5a4SJacob Faibussowitsch {
1092b665b14eSToby Isaac   PetscLogState state;
1093217044c2SLisandro Dalcin 
1094217044c2SLisandro Dalcin   PetscFunctionBegin;
1095b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
1096b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateEventSetCollective(state, event, collective));
1097b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
1098b665b14eSToby Isaac }
1099b665b14eSToby Isaac 
1100b665b14eSToby Isaac /*
1101b665b14eSToby Isaac   PetscLogClassSetActiveAll - Activate or inactivate logging for all events associated with a PETSc object class in every stage.
1102b665b14eSToby Isaac 
1103b665b14eSToby Isaac   Not Collective
1104b665b14eSToby Isaac 
1105b665b14eSToby Isaac   Input Parameters:
1106b665b14eSToby Isaac + classid - The object class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc.
1107b665b14eSToby Isaac - isActive - if `PETSC_FALSE`, events associated with this class will not be send to log handlers.
1108b665b14eSToby Isaac 
1109b665b14eSToby Isaac   Level: developer
1110b665b14eSToby Isaac 
1111b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogEventActivate()`, `PetscLogEventActivateAll()`, `PetscLogStageSetActive()`, `PetscLogEventActivateClass()`
1112b665b14eSToby Isaac */
1113b665b14eSToby Isaac static PetscErrorCode PetscLogClassSetActiveAll(PetscClassId classid, PetscBool isActive)
1114b665b14eSToby Isaac {
1115b665b14eSToby Isaac   PetscLogState state;
1116b665b14eSToby Isaac 
1117b665b14eSToby Isaac   PetscFunctionBegin;
1118b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
1119b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateClassSetActiveAll(state, classid, isActive));
11203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1121217044c2SLisandro Dalcin }
1122217044c2SLisandro Dalcin 
1123217044c2SLisandro Dalcin /*@
1124fa2bb9feSLisandro Dalcin   PetscLogEventIncludeClass - Activates event logging for a PETSc object class in every stage.
1125fa2bb9feSLisandro Dalcin 
1126fa2bb9feSLisandro Dalcin   Not Collective
1127fa2bb9feSLisandro Dalcin 
1128fa2bb9feSLisandro Dalcin   Input Parameter:
1129811af0c4SBarry Smith . classid - The object class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc.
1130fa2bb9feSLisandro Dalcin 
1131fa2bb9feSLisandro Dalcin   Level: developer
1132fa2bb9feSLisandro Dalcin 
1133d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventActivateClass()`, `PetscLogEventDeactivateClass()`, `PetscLogEventActivate()`, `PetscLogEventDeactivate()`
1134fa2bb9feSLisandro Dalcin @*/
1135d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventIncludeClass(PetscClassId classid)
1136d71ae5a4SJacob Faibussowitsch {
1137fa2bb9feSLisandro Dalcin   PetscFunctionBegin;
1138b665b14eSToby Isaac   PetscCall(PetscLogClassSetActiveAll(classid, PETSC_TRUE));
11393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1140fa2bb9feSLisandro Dalcin }
1141fa2bb9feSLisandro Dalcin 
1142fa2bb9feSLisandro Dalcin /*@
1143fa2bb9feSLisandro Dalcin   PetscLogEventExcludeClass - Deactivates event logging for a PETSc object class in every stage.
1144fa2bb9feSLisandro Dalcin 
1145fa2bb9feSLisandro Dalcin   Not Collective
1146fa2bb9feSLisandro Dalcin 
1147fa2bb9feSLisandro Dalcin   Input Parameter:
1148811af0c4SBarry Smith . classid - The object class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc.
1149fa2bb9feSLisandro Dalcin 
1150fa2bb9feSLisandro Dalcin   Level: developer
1151fa2bb9feSLisandro Dalcin 
1152811af0c4SBarry Smith   Note:
1153811af0c4SBarry Smith   If a class is excluded then events associated with that class are not logged.
1154811af0c4SBarry Smith 
1155d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventDeactivateClass()`, `PetscLogEventActivateClass()`, `PetscLogEventDeactivate()`, `PetscLogEventActivate()`
1156fa2bb9feSLisandro Dalcin @*/
1157d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventExcludeClass(PetscClassId classid)
1158d71ae5a4SJacob Faibussowitsch {
1159b665b14eSToby Isaac   PetscFunctionBegin;
1160b665b14eSToby Isaac   PetscCall(PetscLogClassSetActiveAll(classid, PETSC_FALSE));
1161b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
1162b665b14eSToby Isaac }
1163b665b14eSToby Isaac 
1164b665b14eSToby Isaac /*
1165b665b14eSToby Isaac   PetscLogEventSetActive - Activate or inactivate logging for an event in a given stage
1166b665b14eSToby Isaac 
1167b665b14eSToby Isaac   Not Collective
1168b665b14eSToby Isaac 
1169b665b14eSToby Isaac   Input Parameters:
1170b665b14eSToby Isaac + stage - A registered `PetscLogStage` (or `PETSC_DEFAULT` for the current stage)
1171b665b14eSToby Isaac . event - A `PetscLogEvent`
1172b665b14eSToby Isaac - isActive - If `PETSC_FALSE`, activity from this event (`PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogEventSync()`) will not be sent to log handlers during this stage
1173b665b14eSToby Isaac 
1174b665b14eSToby Isaac   Usage:
1175b665b14eSToby Isaac .vb
1176b665b14eSToby Isaac       PetscLogEventSetActive(VEC_SetValues, PETSC_FALSE);
1177b665b14eSToby Isaac         [code where you do not want to log VecSetValues()]
1178b665b14eSToby Isaac       PetscLogEventSetActive(VEC_SetValues, PETSC_TRUE);
1179b665b14eSToby Isaac         [code where you do want to log VecSetValues()]
1180b665b14eSToby Isaac .ve
1181b665b14eSToby Isaac 
1182b665b14eSToby Isaac   Level: advanced
1183b665b14eSToby Isaac 
1184b665b14eSToby Isaac   Note:
1185b665b14eSToby Isaac   The event may be either a pre-defined PETSc event (found in include/petsclog.h)
1186b665b14eSToby Isaac   or an event number obtained with `PetscLogEventRegister()`.
1187b665b14eSToby Isaac 
1188b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogEventDeactivatePush()`, `PetscLogEventDeactivatePop()`
1189b665b14eSToby Isaac */
1190b665b14eSToby Isaac static PetscErrorCode PetscLogEventSetActive(PetscLogStage stage, PetscLogEvent event, PetscBool isActive)
1191b665b14eSToby Isaac {
1192b665b14eSToby Isaac   PetscLogState state;
1193fa2bb9feSLisandro Dalcin 
1194fa2bb9feSLisandro Dalcin   PetscFunctionBegin;
1195b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
1196b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateEventSetActive(state, stage, event, isActive));
11973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1198fa2bb9feSLisandro Dalcin }
1199fa2bb9feSLisandro Dalcin 
1200fa2bb9feSLisandro Dalcin /*@
12015c6c1daeSBarry Smith   PetscLogEventActivate - Indicates that a particular event should be logged.
12025c6c1daeSBarry Smith 
12035c6c1daeSBarry Smith   Not Collective
12045c6c1daeSBarry Smith 
12055c6c1daeSBarry Smith   Input Parameter:
12065c6c1daeSBarry Smith . event - The event id
12075c6c1daeSBarry Smith 
120810450e9eSJacob Faibussowitsch   Example Usage:
12095c6c1daeSBarry Smith .vb
12105c6c1daeSBarry Smith       PetscLogEventDeactivate(VEC_SetValues);
12115c6c1daeSBarry Smith         [code where you do not want to log VecSetValues()]
12125c6c1daeSBarry Smith       PetscLogEventActivate(VEC_SetValues);
12135c6c1daeSBarry Smith         [code where you do want to log VecSetValues()]
12145c6c1daeSBarry Smith .ve
12155c6c1daeSBarry Smith 
1216d1f92df0SBarry Smith   Level: advanced
1217d1f92df0SBarry Smith 
12185c6c1daeSBarry Smith   Note:
12195c6c1daeSBarry Smith   The event may be either a pre-defined PETSc event (found in include/petsclog.h)
1220811af0c4SBarry Smith   or an event number obtained with `PetscLogEventRegister()`.
12215c6c1daeSBarry Smith 
1222b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogEventDeactivate()`, `PetscLogEventDeactivatePush()`, `PetscLogEventDeactivatePop()`
12235c6c1daeSBarry Smith @*/
1224d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventActivate(PetscLogEvent event)
1225d71ae5a4SJacob Faibussowitsch {
12265c6c1daeSBarry Smith   PetscFunctionBegin;
1227b665b14eSToby Isaac   PetscCall(PetscLogEventSetActive(PETSC_DEFAULT, event, PETSC_TRUE));
12283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12295c6c1daeSBarry Smith }
12305c6c1daeSBarry Smith 
12315c6c1daeSBarry Smith /*@
12325c6c1daeSBarry Smith   PetscLogEventDeactivate - Indicates that a particular event should not be logged.
12335c6c1daeSBarry Smith 
12345c6c1daeSBarry Smith   Not Collective
12355c6c1daeSBarry Smith 
12365c6c1daeSBarry Smith   Input Parameter:
12375c6c1daeSBarry Smith . event - The event id
12385c6c1daeSBarry Smith 
123910450e9eSJacob Faibussowitsch   Example Usage:
12405c6c1daeSBarry Smith .vb
12415c6c1daeSBarry Smith       PetscLogEventDeactivate(VEC_SetValues);
12425c6c1daeSBarry Smith         [code where you do not want to log VecSetValues()]
12435c6c1daeSBarry Smith       PetscLogEventActivate(VEC_SetValues);
12445c6c1daeSBarry Smith         [code where you do want to log VecSetValues()]
12455c6c1daeSBarry Smith .ve
12465c6c1daeSBarry Smith 
1247d1f92df0SBarry Smith   Level: advanced
1248d1f92df0SBarry Smith 
12495c6c1daeSBarry Smith   Note:
12505c6c1daeSBarry 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()`).
12525c6c1daeSBarry Smith 
1253d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventActivate()`, `PetscLogEventDeactivatePush()`, `PetscLogEventDeactivatePop()`
12545c6c1daeSBarry Smith @*/
1255d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventDeactivate(PetscLogEvent event)
1256d71ae5a4SJacob Faibussowitsch {
12575c6c1daeSBarry Smith   PetscFunctionBegin;
1258b665b14eSToby Isaac   PetscCall(PetscLogEventSetActive(PETSC_DEFAULT, event, PETSC_FALSE));
12593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12605c6c1daeSBarry Smith }
12615c6c1daeSBarry Smith 
12625c6c1daeSBarry Smith /*@
1263811af0c4SBarry Smith   PetscLogEventDeactivatePush - Indicates that a particular event should not be logged until `PetscLogEventDeactivatePop()` is called
1264c00cb57fSBarry Smith 
1265c00cb57fSBarry Smith   Not Collective
1266c00cb57fSBarry Smith 
1267c00cb57fSBarry Smith   Input Parameter:
1268c00cb57fSBarry Smith . event - The event id
1269c00cb57fSBarry Smith 
127010450e9eSJacob Faibussowitsch   Example Usage:
1271c00cb57fSBarry Smith .vb
1272c00cb57fSBarry Smith       PetscLogEventDeactivatePush(VEC_SetValues);
1273c00cb57fSBarry Smith         [code where you do not want to log VecSetValues()]
1274c00cb57fSBarry Smith       PetscLogEventDeactivatePop(VEC_SetValues);
1275c00cb57fSBarry Smith         [code where you do want to log VecSetValues()]
1276c00cb57fSBarry Smith .ve
1277c00cb57fSBarry Smith 
1278d1f92df0SBarry Smith   Level: advanced
1279d1f92df0SBarry Smith 
1280c00cb57fSBarry Smith   Note:
1281c00cb57fSBarry Smith   The event may be either a pre-defined PETSc event (found in
1282811af0c4SBarry Smith   include/petsclog.h) or an event number obtained with `PetscLogEventRegister()`).
1283c00cb57fSBarry Smith 
1284b665b14eSToby Isaac   PETSc's default log handler (`PetscLogDefaultBegin()`) respects this function because it can make the output of `PetscLogView()` easier to interpret, but other handlers (such as the nested handler, `PetscLogNestedBegin()`) ignore it because surpressing events is not helpful in their output formats.
1285b665b14eSToby Isaac 
1286b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogEventActivate()`, `PetscLogEVentDeactivate()`, `PetscLogEventDeactivatePop()`
1287c00cb57fSBarry Smith @*/
1288d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventDeactivatePush(PetscLogEvent event)
1289d71ae5a4SJacob Faibussowitsch {
1290b665b14eSToby Isaac   PetscLogHandler handler;
1291c00cb57fSBarry Smith 
1292c00cb57fSBarry Smith   PetscFunctionBegin;
1293b665b14eSToby Isaac   PetscCall(PetscLogTryGetHandler(PETSC_LOG_HANDLER_DEFAULT, &handler));
1294b665b14eSToby Isaac   if (handler) { PetscCall(PetscLogHandlerDefaultDeactivatePush(handler, PETSC_DEFAULT, event)); }
12953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1296c00cb57fSBarry Smith }
1297c00cb57fSBarry Smith 
1298c00cb57fSBarry Smith /*@
1299811af0c4SBarry Smith   PetscLogEventDeactivatePop - Indicates that a particular event should again be logged after the logging was turned off with `PetscLogEventDeactivatePush()`
1300c00cb57fSBarry Smith 
1301c00cb57fSBarry Smith   Not Collective
1302c00cb57fSBarry Smith 
1303c00cb57fSBarry Smith   Input Parameter:
1304c00cb57fSBarry Smith . event - The event id
1305c00cb57fSBarry Smith 
130610450e9eSJacob Faibussowitsch   Example Usage:
1307c00cb57fSBarry Smith .vb
1308c00cb57fSBarry Smith       PetscLogEventDeactivatePush(VEC_SetValues);
1309c00cb57fSBarry Smith         [code where you do not want to log VecSetValues()]
1310c00cb57fSBarry Smith       PetscLogEventDeactivatePop(VEC_SetValues);
1311c00cb57fSBarry Smith         [code where you do want to log VecSetValues()]
1312c00cb57fSBarry Smith .ve
1313c00cb57fSBarry Smith 
1314d1f92df0SBarry Smith   Level: advanced
1315d1f92df0SBarry Smith 
1316c00cb57fSBarry Smith   Note:
1317c00cb57fSBarry Smith   The event may be either a pre-defined PETSc event (found in
1318811af0c4SBarry Smith   include/petsclog.h) or an event number obtained with `PetscLogEventRegister()`).
1319c00cb57fSBarry Smith 
1320d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventActivate()`, `PetscLogEventDeactivatePush()`
1321c00cb57fSBarry Smith @*/
1322d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventDeactivatePop(PetscLogEvent event)
1323d71ae5a4SJacob Faibussowitsch {
1324b665b14eSToby Isaac   PetscLogHandler handler;
1325c00cb57fSBarry Smith 
1326c00cb57fSBarry Smith   PetscFunctionBegin;
1327b665b14eSToby Isaac   PetscCall(PetscLogTryGetHandler(PETSC_LOG_HANDLER_DEFAULT, &handler));
1328b665b14eSToby Isaac   if (handler) { PetscCall(PetscLogHandlerDefaultDeactivatePop(handler, PETSC_DEFAULT, event)); }
13293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1330c00cb57fSBarry Smith }
1331c00cb57fSBarry Smith 
1332c00cb57fSBarry Smith /*@
1333811af0c4SBarry Smith   PetscLogEventSetActiveAll - Turns on logging of all events
13345c6c1daeSBarry Smith 
13355c6c1daeSBarry Smith   Not Collective
13365c6c1daeSBarry Smith 
13375c6c1daeSBarry Smith   Input Parameters:
13385c6c1daeSBarry Smith + event    - The event id
13395c6c1daeSBarry Smith - isActive - The activity flag determining whether the event is logged
13405c6c1daeSBarry Smith 
13415c6c1daeSBarry Smith   Level: advanced
13425c6c1daeSBarry Smith 
1343b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogEventActivate()`, `PetscLogEventDeactivate()`
13445c6c1daeSBarry Smith @*/
1345d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventSetActiveAll(PetscLogEvent event, PetscBool isActive)
1346d71ae5a4SJacob Faibussowitsch {
1347b665b14eSToby Isaac   PetscLogState state;
13485c6c1daeSBarry Smith 
13495c6c1daeSBarry Smith   PetscFunctionBegin;
1350b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
1351b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateEventSetActiveAll(state, event, isActive));
1352b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
13535c6c1daeSBarry Smith }
1354b665b14eSToby Isaac 
1355b665b14eSToby Isaac /*
1356b665b14eSToby Isaac   PetscLogClassSetActive - Activates event logging for a PETSc object class for the current stage
1357b665b14eSToby Isaac 
1358b665b14eSToby Isaac   Not Collective
1359b665b14eSToby Isaac 
1360b665b14eSToby Isaac   Input Parameters:
1361b665b14eSToby Isaac + stage - A registered `PetscLogStage` (or `PETSC_DEFAULT` for the current stage)
1362b665b14eSToby Isaac . classid - The event class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc.
1363b665b14eSToby Isaac - isActive - If `PETSC_FALSE`, events associated with this class are not sent to log handlers.
1364b665b14eSToby Isaac 
1365b665b14eSToby Isaac   Level: developer
1366b665b14eSToby Isaac 
1367b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogEventIncludeClass()`, `PetscLogEventActivate()`, `PetscLogEventActivateAll()`, `PetscLogStageSetActive()`
1368b665b14eSToby Isaac */
1369b665b14eSToby Isaac static PetscErrorCode PetscLogClassSetActive(PetscLogStage stage, PetscClassId classid, PetscBool isActive)
1370b665b14eSToby Isaac {
1371b665b14eSToby Isaac   PetscLogState state;
1372b665b14eSToby Isaac 
1373b665b14eSToby Isaac   PetscFunctionBegin;
1374b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
1375b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateClassSetActive(state, stage, classid, isActive));
13763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13775c6c1daeSBarry Smith }
13785c6c1daeSBarry Smith 
13795c6c1daeSBarry Smith /*@
1380811af0c4SBarry Smith   PetscLogEventActivateClass - Activates event logging for a PETSc object class for the current stage
13815c6c1daeSBarry Smith 
13825c6c1daeSBarry Smith   Not Collective
13835c6c1daeSBarry Smith 
13845c6c1daeSBarry Smith   Input Parameter:
1385811af0c4SBarry Smith . classid - The event class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc.
13865c6c1daeSBarry Smith 
13875c6c1daeSBarry Smith   Level: developer
13885c6c1daeSBarry Smith 
1389d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventIncludeClass()`, `PetscLogEventExcludeClass()`, `PetscLogEventDeactivateClass()`, `PetscLogEventActivate()`, `PetscLogEventDeactivate()`
13905c6c1daeSBarry Smith @*/
1391d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventActivateClass(PetscClassId classid)
1392d71ae5a4SJacob Faibussowitsch {
13935c6c1daeSBarry Smith   PetscFunctionBegin;
1394b665b14eSToby Isaac   PetscCall(PetscLogClassSetActive(PETSC_DEFAULT, classid, PETSC_TRUE));
13953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13965c6c1daeSBarry Smith }
13975c6c1daeSBarry Smith 
13985c6c1daeSBarry Smith /*@
1399811af0c4SBarry Smith   PetscLogEventDeactivateClass - Deactivates event logging for a PETSc object class for the current stage
14005c6c1daeSBarry Smith 
14015c6c1daeSBarry Smith   Not Collective
14025c6c1daeSBarry Smith 
14035c6c1daeSBarry Smith   Input Parameter:
1404811af0c4SBarry Smith . classid - The event class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc.
14055c6c1daeSBarry Smith 
14065c6c1daeSBarry Smith   Level: developer
14075c6c1daeSBarry Smith 
1408d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventIncludeClass()`, `PetscLogEventExcludeClass()`, `PetscLogEventActivateClass()`, `PetscLogEventActivate()`, `PetscLogEventDeactivate()`
14095c6c1daeSBarry Smith @*/
1410d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventDeactivateClass(PetscClassId classid)
1411d71ae5a4SJacob Faibussowitsch {
14125c6c1daeSBarry Smith   PetscFunctionBegin;
1413b665b14eSToby Isaac   PetscCall(PetscLogClassSetActive(PETSC_DEFAULT, classid, PETSC_FALSE));
14143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
14155c6c1daeSBarry Smith }
14165c6c1daeSBarry Smith 
14175c6c1daeSBarry Smith /*MC
141862872c28SLisandro Dalcin   PetscLogEventSync - Synchronizes the beginning of a user event.
141962872c28SLisandro Dalcin 
142062872c28SLisandro Dalcin   Synopsis:
142162872c28SLisandro Dalcin   #include <petsclog.h>
1422b665b14eSToby Isaac   PetscErrorCode PetscLogEventSync(PetscLogEvent e, MPI_Comm comm)
142362872c28SLisandro Dalcin 
142462872c28SLisandro Dalcin   Collective
142562872c28SLisandro Dalcin 
142662872c28SLisandro Dalcin   Input Parameters:
1427b665b14eSToby Isaac + e    - `PetscLogEvent` obtained from `PetscLogEventRegister()`
142862872c28SLisandro Dalcin - comm - an MPI communicator
142962872c28SLisandro Dalcin 
143010450e9eSJacob Faibussowitsch   Example Usage:
143162872c28SLisandro Dalcin .vb
143262872c28SLisandro Dalcin   PetscLogEvent USER_EVENT;
143310450e9eSJacob Faibussowitsch 
143462872c28SLisandro Dalcin   PetscLogEventRegister("User event", 0, &USER_EVENT);
143562872c28SLisandro Dalcin   PetscLogEventSync(USER_EVENT, PETSC_COMM_WORLD);
143662872c28SLisandro Dalcin   PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0);
143762872c28SLisandro Dalcin   [code segment to monitor]
143862872c28SLisandro Dalcin   PetscLogEventEnd(USER_EVENT, 0, 0, 0 , 0);
143962872c28SLisandro Dalcin .ve
144062872c28SLisandro Dalcin 
1441d1f92df0SBarry Smith   Level: developer
1442d1f92df0SBarry Smith 
1443811af0c4SBarry Smith   Note:
144410450e9eSJacob Faibussowitsch   This routine should be called only if there is not a `PetscObject` available to pass to
144510450e9eSJacob Faibussowitsch   `PetscLogEventBegin()`.
144662872c28SLisandro Dalcin 
1447d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`
144862872c28SLisandro Dalcin M*/
144962872c28SLisandro Dalcin 
145062872c28SLisandro Dalcin /*MC
14515c6c1daeSBarry Smith   PetscLogEventBegin - Logs the beginning of a user event.
14525c6c1daeSBarry Smith 
14535c6c1daeSBarry Smith   Synopsis:
1454aaa7dc30SBarry Smith   #include <petsclog.h>
1455b665b14eSToby Isaac   PetscErrorCode PetscLogEventBegin(PetscLogEvent e, PetscObject o1, PetscObject o2, PetscObject o3, PetscObject o4)
14565c6c1daeSBarry Smith 
14575c6c1daeSBarry Smith   Not Collective
14585c6c1daeSBarry Smith 
14595c6c1daeSBarry Smith   Input Parameters:
1460b665b14eSToby Isaac + e  - `PetscLogEvent` obtained from `PetscLogEventRegister()`
1461b665b14eSToby Isaac . o1 - object assocated with the event, or NULL
1462b665b14eSToby Isaac . o2 - object assocated with the event, or NULL
1463b665b14eSToby Isaac . o3 - object assocated with the event, or NULL
1464b665b14eSToby Isaac - o4 - object assocated with the event, or NULL
14655c6c1daeSBarry Smith 
14665c6c1daeSBarry Smith   Fortran Synopsis:
14675c6c1daeSBarry Smith   void PetscLogEventBegin(int e, PetscErrorCode ierr)
14685c6c1daeSBarry Smith 
146910450e9eSJacob Faibussowitsch   Example Usage:
14705c6c1daeSBarry Smith .vb
14715c6c1daeSBarry Smith   PetscLogEvent USER_EVENT;
147210450e9eSJacob Faibussowitsch 
14735c6c1daeSBarry Smith   PetscLogDouble user_event_flops;
14745c6c1daeSBarry Smith   PetscLogEventRegister("User event",0, &USER_EVENT);
14755c6c1daeSBarry Smith   PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0);
14765c6c1daeSBarry Smith   [code segment to monitor]
14775c6c1daeSBarry Smith   PetscLogFlops(user_event_flops);
14785c6c1daeSBarry Smith   PetscLogEventEnd(USER_EVENT, 0, 0, 0, 0);
14795c6c1daeSBarry Smith .ve
14805c6c1daeSBarry Smith 
1481d1f92df0SBarry Smith   Level: intermediate
1482d1f92df0SBarry Smith 
1483811af0c4SBarry Smith   Developer Note:
148410450e9eSJacob Faibussowitsch   `PetscLogEventBegin()` and `PetscLogEventBegin()` return error codes instead of explicitly
148510450e9eSJacob Faibussowitsch   handling the errors that occur in the macro directly because other packages that use this
148610450e9eSJacob Faibussowitsch   macros have used them in their own functions or methods that do not return error codes and it
148710450e9eSJacob Faibussowitsch   would be disruptive to change the current behavior.
1488d0609cedSBarry Smith 
1489d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventEnd()`, `PetscLogFlops()`
14905c6c1daeSBarry Smith M*/
14915c6c1daeSBarry Smith 
14925c6c1daeSBarry Smith /*MC
14935c6c1daeSBarry Smith   PetscLogEventEnd - Log the end of a user event.
14945c6c1daeSBarry Smith 
14955c6c1daeSBarry Smith   Synopsis:
1496aaa7dc30SBarry Smith   #include <petsclog.h>
1497b665b14eSToby Isaac   PetscErrorCode PetscLogEventEnd(PetscLogEvent e, PetscObject o1, PetscObject o2, PetscObject o3, PetscObject o4)
14985c6c1daeSBarry Smith 
14995c6c1daeSBarry Smith   Not Collective
15005c6c1daeSBarry Smith 
15015c6c1daeSBarry Smith   Input Parameters:
1502b665b14eSToby Isaac + e  - `PetscLogEvent` obtained from `PetscLogEventRegister()`
1503b665b14eSToby Isaac . o1 - object assocated with the event, or NULL
1504b665b14eSToby Isaac . o2 - object assocated with the event, or NULL
1505b665b14eSToby Isaac . o3 - object assocated with the event, or NULL
1506b665b14eSToby Isaac - o4 - object assocated with the event, or NULL
15075c6c1daeSBarry Smith 
15085c6c1daeSBarry Smith   Fortran Synopsis:
15095c6c1daeSBarry Smith   void PetscLogEventEnd(int e, PetscErrorCode ierr)
15105c6c1daeSBarry Smith 
151110450e9eSJacob Faibussowitsch   Example Usage:
15125c6c1daeSBarry Smith .vb
15135c6c1daeSBarry Smith   PetscLogEvent USER_EVENT;
151410450e9eSJacob Faibussowitsch 
15155c6c1daeSBarry Smith   PetscLogDouble user_event_flops;
151610450e9eSJacob Faibussowitsch   PetscLogEventRegister("User event", 0, &USER_EVENT);
15175c6c1daeSBarry Smith   PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0);
15185c6c1daeSBarry Smith   [code segment to monitor]
15195c6c1daeSBarry Smith   PetscLogFlops(user_event_flops);
15205c6c1daeSBarry Smith   PetscLogEventEnd(USER_EVENT, 0, 0, 0, 0);
15215c6c1daeSBarry Smith .ve
15225c6c1daeSBarry Smith 
15235c6c1daeSBarry Smith   Level: intermediate
15245c6c1daeSBarry Smith 
1525d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogFlops()`
15265c6c1daeSBarry Smith M*/
15275c6c1daeSBarry Smith 
15285c6c1daeSBarry Smith /*@C
1529b665b14eSToby Isaac   PetscLogEventGetPerfInfo - Return the performance information about the given event in the given stage
1530b665b14eSToby Isaac 
1531b665b14eSToby Isaac   Input Parameters:
1532b665b14eSToby Isaac + stage - The stage number or `PETSC_DETERMINE` for the current stage
1533b665b14eSToby Isaac - event - The event number
1534b665b14eSToby Isaac 
1535b665b14eSToby Isaac   Output Parameter:
1536b665b14eSToby Isaac . info - This structure is filled with the performance information
1537b665b14eSToby Isaac 
1538b665b14eSToby Isaac   Level: intermediate
1539b665b14eSToby Isaac 
1540b665b14eSToby Isaac   Note:
1541b665b14eSToby Isaac   This is a low level routine used by the logging functions in PETSc
1542b665b14eSToby Isaac 
1543b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogGetDefaultHandler()`
1544b665b14eSToby Isaac @*/
1545b665b14eSToby Isaac PetscErrorCode PetscLogEventGetPerfInfo(PetscLogStage stage, PetscLogEvent event, PetscEventPerfInfo *info)
1546b665b14eSToby Isaac {
1547b665b14eSToby Isaac   PetscLogHandler     handler;
1548b665b14eSToby Isaac   PetscEventPerfInfo *event_info;
1549b665b14eSToby Isaac 
1550b665b14eSToby Isaac   PetscFunctionBegin;
1551b665b14eSToby Isaac   PetscAssertPointer(info, 3);
1552b665b14eSToby Isaac   PetscCall(PetscLogGetHandler(PETSC_LOG_HANDLER_DEFAULT, &handler));
1553b665b14eSToby Isaac   PetscCall(PetscLogHandlerDefaultGetEventPerfInfo(handler, stage, event, &event_info));
1554b665b14eSToby Isaac   *info = *event_info;
1555b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
1556b665b14eSToby Isaac }
1557b665b14eSToby Isaac 
1558b665b14eSToby Isaac /*@C
1559b665b14eSToby Isaac   PetscLogEventSetDof - Set the nth number of degrees of freedom of a numerical problem associated with this event
1560b665b14eSToby Isaac 
1561b665b14eSToby Isaac   Not Collective
1562b665b14eSToby Isaac 
1563b665b14eSToby Isaac   Input Parameters:
1564b665b14eSToby Isaac + event - The event id to log
1565b665b14eSToby Isaac . n     - The dof index, in [0, 8)
1566b665b14eSToby Isaac - dof   - The number of dofs
1567b665b14eSToby Isaac 
1568b665b14eSToby Isaac   Options Database Key:
1569b665b14eSToby Isaac . -log_view - Activates log summary
1570b665b14eSToby Isaac 
1571b665b14eSToby Isaac   Level: developer
1572b665b14eSToby Isaac 
1573b665b14eSToby Isaac   Note:
1574b665b14eSToby Isaac   This is to enable logging of convergence
1575b665b14eSToby Isaac 
1576b665b14eSToby Isaac .seealso: `PetscLogEventSetError()`, `PetscLogEventRegister()`, `PetscLogGetDefaultHandler()`
1577b665b14eSToby Isaac @*/
1578b665b14eSToby Isaac PetscErrorCode PetscLogEventSetDof(PetscLogEvent event, PetscInt n, PetscLogDouble dof)
1579b665b14eSToby Isaac {
1580b665b14eSToby Isaac   PetscLogHandler     handler;
1581b665b14eSToby Isaac   PetscEventPerfInfo *event_info;
1582b665b14eSToby Isaac 
1583b665b14eSToby Isaac   PetscFunctionBegin;
1584b665b14eSToby Isaac   PetscCheck(!(n < 0) && !(n > 7), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Error index %" PetscInt_FMT " is not in [0, 8)", n);
1585b665b14eSToby Isaac   PetscCall(PetscLogTryGetHandler(PETSC_LOG_HANDLER_DEFAULT, &handler));
1586b665b14eSToby Isaac   if (handler) {
1587b665b14eSToby Isaac     PetscCall(PetscLogHandlerDefaultGetEventPerfInfo(handler, PETSC_DEFAULT, event, &event_info));
1588b665b14eSToby Isaac     event_info->dof[n] = dof;
1589b665b14eSToby Isaac   }
1590b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
1591b665b14eSToby Isaac }
1592b665b14eSToby Isaac 
1593b665b14eSToby Isaac /*@C
1594b665b14eSToby Isaac   PetscLogEventSetError - Set the nth error associated with a numerical problem associated with this event
1595b665b14eSToby Isaac 
1596b665b14eSToby Isaac   Not Collective
1597b665b14eSToby Isaac 
1598b665b14eSToby Isaac   Input Parameters:
1599b665b14eSToby Isaac + event - The event id to log
1600b665b14eSToby Isaac . n     - The error index, in [0, 8)
1601b665b14eSToby Isaac - error - The error
1602b665b14eSToby Isaac 
1603b665b14eSToby Isaac   Options Database Key:
1604b665b14eSToby Isaac . -log_view - Activates log summary
1605b665b14eSToby Isaac 
1606b665b14eSToby Isaac   Level: developer
1607b665b14eSToby Isaac 
1608b665b14eSToby Isaac   Notes:
1609b665b14eSToby Isaac   This is to enable logging of convergence, and enable users to interpret the errors as they wish. For example,
1610b665b14eSToby Isaac   as different norms, or as errors for different fields
1611b665b14eSToby Isaac 
1612b665b14eSToby Isaac   This is a low level routine used by the logging functions in PETSc
1613b665b14eSToby Isaac 
1614b665b14eSToby Isaac .seealso: `PetscLogEventSetDof()`, `PetscLogEventRegister()`, `PetscLogGetDefaultHandler()`
1615b665b14eSToby Isaac @*/
1616b665b14eSToby Isaac PetscErrorCode PetscLogEventSetError(PetscLogEvent event, PetscInt n, PetscLogDouble error)
1617b665b14eSToby Isaac {
1618b665b14eSToby Isaac   PetscLogHandler     handler;
1619b665b14eSToby Isaac   PetscEventPerfInfo *event_info;
1620b665b14eSToby Isaac 
1621b665b14eSToby Isaac   PetscFunctionBegin;
1622b665b14eSToby Isaac   PetscCheck(!(n < 0) && !(n > 7), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Error index %" PetscInt_FMT " is not in [0, 8)", n);
1623b665b14eSToby Isaac   PetscCall(PetscLogTryGetHandler(PETSC_LOG_HANDLER_DEFAULT, &handler));
1624b665b14eSToby Isaac   if (handler) {
1625b665b14eSToby Isaac     PetscCall(PetscLogHandlerDefaultGetEventPerfInfo(handler, PETSC_DEFAULT, event, &event_info));
1626b665b14eSToby Isaac     event_info->errors[n] = error;
1627b665b14eSToby Isaac   }
1628b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
1629b665b14eSToby Isaac }
1630b665b14eSToby Isaac 
1631b665b14eSToby Isaac /*@C
16325c6c1daeSBarry Smith   PetscLogEventGetId - Returns the event id when given the event name.
16335c6c1daeSBarry Smith 
16345c6c1daeSBarry Smith   Not Collective
16355c6c1daeSBarry Smith 
16365c6c1daeSBarry Smith   Input Parameter:
16375c6c1daeSBarry Smith . name - The event name
16385c6c1daeSBarry Smith 
16395c6c1daeSBarry Smith   Output Parameter:
1640c5deb1d5SJed Brown . event - The event, or -1 if no event with that name exists
16415c6c1daeSBarry Smith 
16425c6c1daeSBarry Smith   Level: intermediate
16435c6c1daeSBarry Smith 
1644d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogStageGetId()`
16455c6c1daeSBarry Smith @*/
1646d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventGetId(const char name[], PetscLogEvent *event)
1647d71ae5a4SJacob Faibussowitsch {
1648b665b14eSToby Isaac   PetscLogState state;
16495c6c1daeSBarry Smith 
16505c6c1daeSBarry Smith   PetscFunctionBegin;
1651b665b14eSToby Isaac   *event = -1;
1652b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
1653b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateGetEventFromName(state, name, event));
16543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
16555c6c1daeSBarry Smith }
16565c6c1daeSBarry Smith 
165753e0a2f3SToby Isaac /*@C
165853e0a2f3SToby Isaac   PetscLogEventGetName - Returns the event name when given the event id.
165953e0a2f3SToby Isaac 
166053e0a2f3SToby Isaac   Not Collective
166153e0a2f3SToby Isaac 
166253e0a2f3SToby Isaac   Input Parameter:
166353e0a2f3SToby Isaac . event - The event
166453e0a2f3SToby Isaac 
166553e0a2f3SToby Isaac   Output Parameter:
166653e0a2f3SToby Isaac . name - The event name
166753e0a2f3SToby Isaac 
166853e0a2f3SToby Isaac   Level: intermediate
166953e0a2f3SToby Isaac 
167053e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()`
167153e0a2f3SToby Isaac @*/
167253e0a2f3SToby Isaac PetscErrorCode PetscLogEventGetName(PetscLogEvent event, const char **name)
167353e0a2f3SToby Isaac {
167453e0a2f3SToby Isaac   PetscLogEventInfo event_info;
167553e0a2f3SToby Isaac   PetscLogState     state;
167653e0a2f3SToby Isaac 
167753e0a2f3SToby Isaac   PetscFunctionBegin;
1678b665b14eSToby Isaac   *name = NULL;
167953e0a2f3SToby Isaac   PetscCall(PetscLogGetState(&state));
1680b665b14eSToby Isaac   if (!state) PetscFunctionReturn(PETSC_SUCCESS);
168153e0a2f3SToby Isaac   PetscCall(PetscLogStateEventGetInfo(state, event, &event_info));
168253e0a2f3SToby Isaac   *name = event_info.name;
168353e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
168453e0a2f3SToby Isaac }
168553e0a2f3SToby Isaac 
168653e0a2f3SToby Isaac /*@
168753e0a2f3SToby 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.
168853e0a2f3SToby Isaac 
168953e0a2f3SToby Isaac   Not collective
169053e0a2f3SToby Isaac 
169153e0a2f3SToby Isaac   Level: advanced
169253e0a2f3SToby Isaac 
169353e0a2f3SToby Isaac   Notes:
169453e0a2f3SToby 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()`).
169553e0a2f3SToby Isaac 
169653e0a2f3SToby Isaac   Other log handlers (such as the nested handler, `PetscLogNestedBegin()`) will ignore this function.
169753e0a2f3SToby Isaac 
1698b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogEventDeactivatePush()`, `PetscLogEventDeactivatePop()`, `PetscLogEventsResume()`, `PetscLogGetDefaultHandler()`
169953e0a2f3SToby Isaac @*/
170053e0a2f3SToby Isaac PetscErrorCode PetscLogEventsPause(void)
170153e0a2f3SToby Isaac {
170253e0a2f3SToby Isaac   PetscLogHandler handler;
170353e0a2f3SToby Isaac 
170453e0a2f3SToby Isaac   PetscFunctionBegin;
170553e0a2f3SToby Isaac   PetscCall(PetscLogTryGetHandler(PETSC_LOG_HANDLER_DEFAULT, &handler));
170653e0a2f3SToby Isaac   if (handler) PetscCall(PetscLogHandlerDefaultEventsPause(handler));
170753e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
170853e0a2f3SToby Isaac }
170953e0a2f3SToby Isaac 
171053e0a2f3SToby Isaac /*@
171153e0a2f3SToby Isaac   PetscLogEventsResume - Return logging to normal behavior after it was paused with `PetscLogEventsPause()`.
171253e0a2f3SToby Isaac 
171353e0a2f3SToby Isaac   Not collective
171453e0a2f3SToby Isaac 
171553e0a2f3SToby Isaac   Level: advanced
171653e0a2f3SToby Isaac 
1717b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogEventDeactivatePush()`, `PetscLogEventDeactivatePop()`, `PetscLogEventsPause()`, `PetscLogGetDefaultHandler()`
171853e0a2f3SToby Isaac @*/
171953e0a2f3SToby Isaac PetscErrorCode PetscLogEventsResume(void)
172053e0a2f3SToby Isaac {
172153e0a2f3SToby Isaac   PetscLogHandler handler;
172253e0a2f3SToby Isaac 
172353e0a2f3SToby Isaac   PetscFunctionBegin;
172453e0a2f3SToby Isaac   PetscCall(PetscLogTryGetHandler(PETSC_LOG_HANDLER_DEFAULT, &handler));
172553e0a2f3SToby Isaac   if (handler) PetscCall(PetscLogHandlerDefaultEventsResume(handler));
172653e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
172753e0a2f3SToby Isaac }
172853e0a2f3SToby Isaac 
17291c1ad86eSToby Isaac /*------------------------------------------------ Class Functions --------------------------------------------------*/
17301c1ad86eSToby Isaac 
17311c1ad86eSToby Isaac /*MC
17321c1ad86eSToby Isaac    PetscLogObjectCreate - Log the creation of a `PetscObject`
17331c1ad86eSToby Isaac 
17341c1ad86eSToby Isaac    Synopsis:
17351c1ad86eSToby Isaac    #include <petsclog.h>
17361c1ad86eSToby Isaac    PetscErrorCode PetscLogObjectCreate(PetscObject h)
17371c1ad86eSToby Isaac 
17381c1ad86eSToby Isaac    Not Collective
17391c1ad86eSToby Isaac 
17401c1ad86eSToby Isaac    Input Parameters:
17411c1ad86eSToby Isaac .  h - A `PetscObject`
17421c1ad86eSToby Isaac 
17431c1ad86eSToby Isaac    Level: developer
17441c1ad86eSToby Isaac 
17451c1ad86eSToby Isaac    Developer Note:
17461c1ad86eSToby Isaac      Called internally by PETSc when creating objects: users do not need to call this directly.
1747b665b14eSToby Isaac      Notification of the object creation is sent to each `PetscLogHandler` that is running.
17481c1ad86eSToby Isaac 
1749b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogObjectDestroy()`
17501c1ad86eSToby Isaac M*/
17511c1ad86eSToby Isaac 
17521c1ad86eSToby Isaac /*MC
17531c1ad86eSToby Isaac    PetscLogObjectDestroy - Logs the destruction of a `PetscObject`
17541c1ad86eSToby Isaac 
17551c1ad86eSToby Isaac    Synopsis:
17561c1ad86eSToby Isaac    #include <petsclog.h>
17571c1ad86eSToby Isaac    PetscErrorCode PetscLogObjectDestroy(PetscObject h)
17581c1ad86eSToby Isaac 
17591c1ad86eSToby Isaac    Not Collective
17601c1ad86eSToby Isaac 
17611c1ad86eSToby Isaac    Input Parameters:
17621c1ad86eSToby Isaac .  h - A `PetscObject`
17631c1ad86eSToby Isaac 
17641c1ad86eSToby Isaac    Level: developer
17651c1ad86eSToby Isaac 
17661c1ad86eSToby Isaac    Developer Note:
17671c1ad86eSToby Isaac      Called internally by PETSc when destroying objects: users do not need to call this directly.
1768b665b14eSToby Isaac      Notification of the object creation is sent to each `PetscLogHandler` that is running.
17691c1ad86eSToby Isaac 
1770b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogObjectCreate()`
17711c1ad86eSToby Isaac M*/
17721c1ad86eSToby Isaac 
177353e0a2f3SToby Isaac /*@C
177453e0a2f3SToby Isaac   PetscLogClassGetClassId - Returns the `PetscClassId` when given the class name.
177553e0a2f3SToby Isaac 
177653e0a2f3SToby Isaac   Not Collective
177753e0a2f3SToby Isaac 
177853e0a2f3SToby Isaac   Input Parameter:
177953e0a2f3SToby Isaac . name - The class name
178053e0a2f3SToby Isaac 
178153e0a2f3SToby Isaac   Output Parameter:
178253e0a2f3SToby Isaac . classid - The `PetscClassId` id, or -1 if no class with that name exists
178353e0a2f3SToby Isaac 
178453e0a2f3SToby Isaac   Level: intermediate
178553e0a2f3SToby Isaac 
178653e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogStageGetId()`
178753e0a2f3SToby Isaac @*/
178853e0a2f3SToby Isaac PetscErrorCode PetscLogClassGetClassId(const char name[], PetscClassId *classid)
178953e0a2f3SToby Isaac {
179053e0a2f3SToby Isaac   PetscLogClass     log_class;
179153e0a2f3SToby Isaac   PetscLogClassInfo class_info;
179253e0a2f3SToby Isaac   PetscLogState     state;
179353e0a2f3SToby Isaac 
179453e0a2f3SToby Isaac   PetscFunctionBegin;
1795b665b14eSToby Isaac   *classid = -1;
179653e0a2f3SToby Isaac   PetscCall(PetscLogGetState(&state));
1797b665b14eSToby Isaac   if (!state) PetscFunctionReturn(PETSC_SUCCESS);
179853e0a2f3SToby Isaac   PetscCall(PetscLogStateGetClassFromName(state, name, &log_class));
179953e0a2f3SToby Isaac   if (log_class < 0) {
180053e0a2f3SToby Isaac     *classid = -1;
180153e0a2f3SToby Isaac     PetscFunctionReturn(PETSC_SUCCESS);
180253e0a2f3SToby Isaac   }
180353e0a2f3SToby Isaac   PetscCall(PetscLogStateClassGetInfo(state, log_class, &class_info));
180453e0a2f3SToby Isaac   *classid = class_info.classid;
180553e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
180653e0a2f3SToby Isaac }
180753e0a2f3SToby Isaac 
180853e0a2f3SToby Isaac /*@C
180953e0a2f3SToby Isaac   PetscLogClassIdGetName - Returns a `PetscClassId`'s name.
181053e0a2f3SToby Isaac 
181153e0a2f3SToby Isaac   Not Collective
181253e0a2f3SToby Isaac 
181353e0a2f3SToby Isaac   Input Parameter:
181453e0a2f3SToby Isaac . classid - A `PetscClassId`
181553e0a2f3SToby Isaac 
181653e0a2f3SToby Isaac   Output Parameter:
181753e0a2f3SToby Isaac . name - The class name
181853e0a2f3SToby Isaac 
181953e0a2f3SToby Isaac   Level: intermediate
182053e0a2f3SToby Isaac 
182153e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogClassRegister()`, `PetscLogClassBegin()`, `PetscLogClassEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadClass()`
182253e0a2f3SToby Isaac @*/
182353e0a2f3SToby Isaac PetscErrorCode PetscLogClassIdGetName(PetscClassId classid, const char **name)
182453e0a2f3SToby Isaac {
182553e0a2f3SToby Isaac   PetscLogClass     log_class;
182653e0a2f3SToby Isaac   PetscLogClassInfo class_info;
182753e0a2f3SToby Isaac   PetscLogState     state;
182853e0a2f3SToby Isaac 
182953e0a2f3SToby Isaac   PetscFunctionBegin;
183053e0a2f3SToby Isaac   PetscCall(PetscLogGetState(&state));
183153e0a2f3SToby Isaac   PetscCall(PetscLogStateGetClassFromClassId(state, classid, &log_class));
183253e0a2f3SToby Isaac   PetscCall(PetscLogStateClassGetInfo(state, log_class, &class_info));
183353e0a2f3SToby Isaac   *name = class_info.name;
183453e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
183553e0a2f3SToby Isaac }
183653e0a2f3SToby Isaac 
18375c6c1daeSBarry Smith /*------------------------------------------------ Output Functions -------------------------------------------------*/
18385c6c1daeSBarry Smith /*@C
18395c6c1daeSBarry Smith   PetscLogDump - Dumps logs of objects to a file. This file is intended to
18405c6c1daeSBarry Smith   be read by bin/petscview. This program no longer exists.
18415c6c1daeSBarry Smith 
1842811af0c4SBarry Smith   Collective on `PETSC_COMM_WORLD`
18435c6c1daeSBarry Smith 
18445c6c1daeSBarry Smith   Input Parameter:
1845aec76313SJacob Faibussowitsch . sname - an optional file name
18465c6c1daeSBarry Smith 
184710450e9eSJacob Faibussowitsch   Example Usage:
18485c6c1daeSBarry Smith .vb
18495c6c1daeSBarry Smith   PetscInitialize(...);
1850b665b14eSToby Isaac   PetscLogDefaultBegin();
1851b665b14eSToby Isaac   // ... code ...
18525c6c1daeSBarry Smith   PetscLogDump(filename);
18535c6c1daeSBarry Smith   PetscFinalize();
18545c6c1daeSBarry Smith .ve
18555c6c1daeSBarry Smith 
1856d1f92df0SBarry Smith   Level: advanced
1857d1f92df0SBarry Smith 
1858811af0c4SBarry Smith   Note:
185937fdd005SBarry Smith   The default file name is Log.<rank> where <rank> is the MPI process rank. If no name is specified,
18605c6c1daeSBarry Smith   this file will be used.
18615c6c1daeSBarry Smith 
1862b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogDefaultBegin()`, `PetscLogView()`, `PetscLogGetDefaultHandler()`
18635c6c1daeSBarry Smith @*/
1864d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogDump(const char sname[])
1865d71ae5a4SJacob Faibussowitsch {
1866b665b14eSToby Isaac   PetscLogHandler handler;
18675c6c1daeSBarry Smith 
18685c6c1daeSBarry Smith   PetscFunctionBegin;
1869b665b14eSToby Isaac   PetscCall(PetscLogGetHandler(PETSC_LOG_HANDLER_DEFAULT, &handler));
1870b665b14eSToby Isaac   PetscCall(PetscLogHandlerDump_Default(handler, sname));
1871b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
18725c6c1daeSBarry Smith }
1873b665b14eSToby Isaac 
1874b665b14eSToby Isaac /*@C
1875b665b14eSToby Isaac   PetscLogMPEDump - Dumps the MPE logging info to file for later use with Jumpshot.
1876b665b14eSToby Isaac 
1877b665b14eSToby Isaac   Collective over `PETSC_COMM_WORLD`
1878b665b14eSToby Isaac 
1879b665b14eSToby Isaac   Input Parameter:
1880b665b14eSToby Isaac . sname - filename for the MPE logfile
1881b665b14eSToby Isaac 
1882b665b14eSToby Isaac   Level: advanced
1883b665b14eSToby Isaac 
1884b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogDump()`, `PetscLogMPEBegin()`
1885b665b14eSToby Isaac @*/
1886b665b14eSToby Isaac PetscErrorCode PetscLogMPEDump(const char sname[])
1887b665b14eSToby Isaac {
1888b665b14eSToby Isaac   PetscFunctionBegin;
1889b665b14eSToby Isaac   #if defined(PETSC_HAVE_MPE)
1890b665b14eSToby Isaac   if (PetscBeganMPE) {
1891b665b14eSToby Isaac     char name[PETSC_MAX_PATH_LEN];
1892b665b14eSToby Isaac 
1893b665b14eSToby Isaac     PetscCall(PetscInfo(0, "Finalizing MPE.\n"));
1894b665b14eSToby Isaac     if (sname) {
1895b665b14eSToby Isaac       PetscCall(PetscStrncpy(name, sname, sizeof(name)));
18965c6c1daeSBarry Smith     } else {
1897b665b14eSToby Isaac       PetscCall(PetscGetProgramName(name, sizeof(name)));
18985c6c1daeSBarry Smith     }
1899b665b14eSToby Isaac     PetscCall(MPE_Finish_log(name));
19005c6c1daeSBarry Smith   } else {
1901b665b14eSToby Isaac     PetscCall(PetscInfo(0, "Not finalizing MPE (not started by PETSc).\n"));
19025c6c1daeSBarry Smith   }
1903c2a741eeSJunchao Zhang   #else
1904b665b14eSToby Isaac   SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP_SYS, "PETSc was configured without MPE support, reconfigure with --with-mpe or --download-mpe");
1905c2a741eeSJunchao Zhang   #endif
19063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19075c6c1daeSBarry Smith }
19085c6c1daeSBarry Smith 
19097d6c928cSSatish Balay /*@C
19107d6c928cSSatish Balay   PetscLogView - Prints a summary of the logging.
19115c6c1daeSBarry Smith 
19125c6c1daeSBarry Smith   Collective over MPI_Comm
19135c6c1daeSBarry Smith 
19145c6c1daeSBarry Smith   Input Parameter:
1915f14045dbSBarry Smith . viewer - an ASCII viewer
19165c6c1daeSBarry Smith 
19175c6c1daeSBarry Smith   Options Database Keys:
1918bb1d7374SBarry Smith + -log_view [:filename]                    - Prints summary of log information
1919bb1d7374SBarry Smith . -log_view :filename.py:ascii_info_detail - Saves logging information from each process as a Python file
1920607d249eSBarry 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)
1921d0a29bd7SConnor 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)
1922156b51fbSBarry Smith . -log_view_memory                         - Also display memory usage in each event
1923156b51fbSBarry Smith . -log_view_gpu_time                       - Also display time in each event for GPU kernels (Note this may slow the computation)
1924811af0c4SBarry Smith . -log_all                                 - Saves a file Log.rank for each MPI rank with details of each step of the computation
1925bb1d7374SBarry Smith - -log_trace [filename]                    - Displays a trace of what each process is doing
19265c6c1daeSBarry Smith 
1927d1f92df0SBarry Smith   Level: beginner
1928d1f92df0SBarry Smith 
19295c6c1daeSBarry Smith   Notes:
1930da81f932SPierre Jolivet   It is possible to control the logging programmatically but we recommend using the options database approach whenever possible
19315c6c1daeSBarry Smith   By default the summary is printed to stdout.
19325c6c1daeSBarry Smith 
1933bb1d7374SBarry Smith   Before calling this routine you must have called either PetscLogDefaultBegin() or PetscLogNestedBegin()
1934bb1d7374SBarry Smith 
1935bb1d7374SBarry Smith   If PETSc is configured with --with-logging=0 then this functionality is not available
1936bb1d7374SBarry Smith 
1937607d249eSBarry Smith   To view the nested XML format filename.xml first copy  ${PETSC_DIR}/share/petsc/xml/performance_xml2html.xsl to the current
1938607d249eSBarry Smith   directory then open filename.xml with your browser. Specific notes for certain browsers
1939607d249eSBarry Smith $    Firefox and Internet explorer - simply open the file
1940607d249eSBarry Smith $    Google Chrome - you must start up Chrome with the option --allow-file-access-from-files
1941a8d69d7bSBarry Smith $    Safari - see https://ccm.net/faq/36342-safari-how-to-enable-local-file-access
1942607d249eSBarry 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
1943607d249eSBarry Smith   your browser.
19442add09c0SLisandro Dalcin   Alternatively, use the script ${PETSC_DIR}/lib/petsc/bin/petsc-performance-view to automatically open a new browser
19452add09c0SLisandro Dalcin   window and render the XML log file contents.
1946607d249eSBarry Smith 
1947bb1d7374SBarry Smith   The nested XML format was kindly donated by Koos Huijssen and Christiaan M. Klaij  MARITIME  RESEARCH  INSTITUTE  NETHERLANDS
1948bb1d7374SBarry Smith 
1949d0a29bd7SConnor Ward   The Flame Graph output can be visualised using either the original Flame Graph script (https://github.com/brendangregg/FlameGraph)
1950d0a29bd7SConnor Ward   or using speedscope (https://www.speedscope.app).
1951d0a29bd7SConnor Ward   Old XML profiles may be converted into this format using the script ${PETSC_DIR}/lib/petsc/bin/xml2flamegraph.py.
1952d0a29bd7SConnor Ward 
1953d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogDefaultBegin()`, `PetscLogDump()`
19545c6c1daeSBarry Smith @*/
1955d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogView(PetscViewer viewer)
1956d71ae5a4SJacob Faibussowitsch {
1957f14045dbSBarry Smith   PetscBool         isascii;
1958f14045dbSBarry Smith   PetscViewerFormat format;
1959b665b14eSToby Isaac   int               stage;
1960b665b14eSToby Isaac   PetscLogState     state;
1961b665b14eSToby Isaac   PetscIntStack     temp_stack;
1962b665b14eSToby Isaac   PetscLogHandler   handler;
1963b665b14eSToby Isaac   PetscBool         is_empty;
19645c6c1daeSBarry Smith 
19655c6c1daeSBarry Smith   PetscFunctionBegin;
1966b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
196737b78d16SBarry Smith   /* Pop off any stages the user forgot to remove */
1968b665b14eSToby Isaac   PetscCall(PetscIntStackCreate(&temp_stack));
1969b665b14eSToby Isaac   PetscCall(PetscLogStateGetCurrentStage(state, &stage));
197037b78d16SBarry Smith   while (stage >= 0) {
1971b665b14eSToby Isaac     PetscCall(PetscLogStagePop());
1972b665b14eSToby Isaac     PetscCall(PetscIntStackPush(temp_stack, stage));
1973b665b14eSToby Isaac     PetscCall(PetscLogStateGetCurrentStage(state, &stage));
197437b78d16SBarry Smith   }
19759566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
197628b400f6SJacob Faibussowitsch   PetscCheck(isascii, PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Currently can only view logging to ASCII");
19779566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(viewer, &format));
1978b665b14eSToby Isaac   if (format == PETSC_VIEWER_ASCII_XML || format == PETSC_VIEWER_ASCII_FLAMEGRAPH) {
1979b665b14eSToby Isaac     PetscCall(PetscLogGetHandler(PETSC_LOG_HANDLER_NESTED, &handler));
1980b665b14eSToby Isaac     PetscCall(PetscLogHandlerView(handler, viewer));
1981b665b14eSToby Isaac   } else {
1982b665b14eSToby Isaac     PetscCall(PetscLogGetHandler(PETSC_LOG_HANDLER_DEFAULT, &handler));
1983b665b14eSToby Isaac     PetscCall(PetscLogHandlerView(handler, viewer));
19845c6c1daeSBarry Smith   }
1985b665b14eSToby Isaac   PetscCall(PetscIntStackEmpty(temp_stack, &is_empty));
1986b665b14eSToby Isaac   while (!is_empty) {
1987b665b14eSToby Isaac     PetscCall(PetscIntStackPop(temp_stack, &stage));
1988b665b14eSToby Isaac     PetscCall(PetscLogStagePush(stage));
1989b665b14eSToby Isaac     PetscCall(PetscIntStackEmpty(temp_stack, &is_empty));
1990b665b14eSToby Isaac   }
1991b665b14eSToby Isaac   PetscCall(PetscIntStackDestroy(temp_stack));
19923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19935c6c1daeSBarry Smith }
19945c6c1daeSBarry Smith 
1995f14045dbSBarry Smith /*@C
1996811af0c4SBarry Smith   PetscLogViewFromOptions - Processes command line options to determine if/how a `PetscLog` is to be viewed.
1997f14045dbSBarry Smith 
1998811af0c4SBarry Smith   Collective on `PETSC_COMM_WORLD`
1999f14045dbSBarry Smith 
2000811af0c4SBarry Smith   Level: developer
2001f14045dbSBarry Smith 
2002d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogView()`
2003f14045dbSBarry Smith @*/
2004d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogViewFromOptions(void)
2005d71ae5a4SJacob Faibussowitsch {
2006ad2e3d55SToby Isaac   PetscInt          n_max = PETSC_LOG_VIEW_FROM_OPTIONS_MAX;
2007ad2e3d55SToby Isaac   PetscViewer       viewers[PETSC_LOG_VIEW_FROM_OPTIONS_MAX];
2008ad2e3d55SToby Isaac   PetscViewerFormat formats[PETSC_LOG_VIEW_FROM_OPTIONS_MAX];
2009f14045dbSBarry Smith   PetscBool         flg;
2010f14045dbSBarry Smith 
2011f14045dbSBarry Smith   PetscFunctionBegin;
2012ad2e3d55SToby Isaac   PetscCall(PetscOptionsGetViewers(PETSC_COMM_WORLD, NULL, NULL, "-log_view", &n_max, viewers, formats, &flg));
2013ad2e3d55SToby Isaac   for (PetscInt i = 0; i < n_max; i++) {
2014ad2e3d55SToby Isaac     PetscCall(PetscViewerPushFormat(viewers[i], formats[i]));
2015ad2e3d55SToby Isaac     PetscCall(PetscLogView(viewers[i]));
2016ad2e3d55SToby Isaac     PetscCall(PetscViewerPopFormat(viewers[i]));
2017ad2e3d55SToby Isaac     PetscCall(PetscViewerDestroy(&(viewers[i])));
2018f14045dbSBarry Smith   }
20193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2020f14045dbSBarry Smith }
2021f14045dbSBarry Smith 
2022b665b14eSToby Isaac PETSC_INTERN PetscErrorCode PetscLogHandlerNestedSetThreshold(PetscLogHandler, PetscLogDouble, PetscLogDouble *);
2023b665b14eSToby Isaac 
2024b665b14eSToby Isaac /*@
2025b665b14eSToby Isaac   PetscLogSetThreshold - Set the threshold time for logging the events; this is a percentage out of 100, so 1. means any event
2026b665b14eSToby Isaac   that takes 1 or more percent of the time.
2027b665b14eSToby Isaac 
2028b665b14eSToby Isaac   Logically Collective over `PETSC_COMM_WORLD`
2029b665b14eSToby Isaac 
2030b665b14eSToby Isaac   Input Parameter:
2031b665b14eSToby Isaac . newThresh - the threshold to use
2032b665b14eSToby Isaac 
2033b665b14eSToby Isaac   Output Parameter:
2034b665b14eSToby Isaac . oldThresh - the previously set threshold value
2035b665b14eSToby Isaac 
2036b665b14eSToby Isaac   Options Database Keys:
2037b665b14eSToby Isaac . -log_view :filename.xml:ascii_xml - Prints an XML summary of flop and timing information to the file
2038b665b14eSToby Isaac 
2039b665b14eSToby Isaac   Example Usage:
2040b665b14eSToby Isaac .vb
2041b665b14eSToby Isaac   PetscInitialize(...);
2042b665b14eSToby Isaac   PetscLogNestedBegin();
2043b665b14eSToby Isaac   PetscLogSetThreshold(0.1,&oldthresh);
2044b665b14eSToby Isaac   // ... code ...
2045b665b14eSToby Isaac   PetscLogView(viewer);
2046b665b14eSToby Isaac   PetscFinalize();
2047b665b14eSToby Isaac .ve
2048b665b14eSToby Isaac 
2049b665b14eSToby Isaac   Level: advanced
2050b665b14eSToby Isaac 
2051b665b14eSToby Isaac   Note:
2052b665b14eSToby Isaac   This threshold is only used by the nested log handler
2053b665b14eSToby Isaac 
2054b665b14eSToby Isaac .seealso: `PetscLogDump()`, `PetscLogView()`, `PetscLogTraceBegin()`, `PetscLogDefaultBegin()`,
2055b665b14eSToby Isaac           `PetscLogNestedBegin()`
2056b665b14eSToby Isaac @*/
2057b665b14eSToby Isaac PetscErrorCode PetscLogSetThreshold(PetscLogDouble newThresh, PetscLogDouble *oldThresh)
2058b665b14eSToby Isaac {
2059b665b14eSToby Isaac   PetscLogHandler handler;
2060b665b14eSToby Isaac 
2061b665b14eSToby Isaac   PetscFunctionBegin;
2062b665b14eSToby Isaac   PetscCall(PetscLogTryGetHandler(PETSC_LOG_HANDLER_NESTED, &handler));
2063b665b14eSToby Isaac   PetscCall(PetscLogHandlerNestedSetThreshold(handler, newThresh, oldThresh));
2064b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
2065b665b14eSToby Isaac }
2066b665b14eSToby Isaac 
20675c6c1daeSBarry Smith /*----------------------------------------------- Counter Functions -------------------------------------------------*/
20685c6c1daeSBarry Smith /*@C
20695c6c1daeSBarry Smith   PetscGetFlops - Returns the number of flops used on this processor
20705c6c1daeSBarry Smith   since the program began.
20715c6c1daeSBarry Smith 
20725c6c1daeSBarry Smith   Not Collective
20735c6c1daeSBarry Smith 
20745c6c1daeSBarry Smith   Output Parameter:
207510450e9eSJacob Faibussowitsch . flops - number of floating point operations
20765c6c1daeSBarry Smith 
2077d1f92df0SBarry Smith   Level: intermediate
2078d1f92df0SBarry Smith 
20795c6c1daeSBarry Smith   Notes:
20805c6c1daeSBarry Smith   A global counter logs all PETSc flop counts.  The user can use
2081811af0c4SBarry Smith   `PetscLogFlops()` to increment this counter to include flops for the
20825c6c1daeSBarry Smith   application code.
20835c6c1daeSBarry Smith 
2084811af0c4SBarry Smith   A separate counter `PetscLogGPUFlops()` logs the flops that occur on any GPU associated with this MPI rank
2085811af0c4SBarry Smith 
2086d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogGPUFlops()`, `PetscTime()`, `PetscLogFlops()`
20875c6c1daeSBarry Smith @*/
2088d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGetFlops(PetscLogDouble *flops)
2089d71ae5a4SJacob Faibussowitsch {
20905c6c1daeSBarry Smith   PetscFunctionBegin;
20915c6c1daeSBarry Smith   *flops = petsc_TotalFlops;
20923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
20935c6c1daeSBarry Smith }
20945c6c1daeSBarry Smith 
20951c1ad86eSToby Isaac /*@C
20961c1ad86eSToby Isaac   PetscLogObjectState - Record information about an object with the default log handler
20971c1ad86eSToby Isaac 
20981c1ad86eSToby Isaac   Not Collective
20991c1ad86eSToby Isaac 
21001c1ad86eSToby Isaac   Input Parameters:
21011c1ad86eSToby Isaac + obj    - the `PetscObject`
21021c1ad86eSToby Isaac . format - a printf-style format string
21031c1ad86eSToby Isaac - ...    - printf arguments to format
21041c1ad86eSToby Isaac 
21051c1ad86eSToby Isaac   Level: developer
21061c1ad86eSToby Isaac 
2107b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogObjectCreate()`, `PetscLogObjectDestroy()`, `PetscLogGetDefaultHandler()`
21081c1ad86eSToby Isaac @*/
2109d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogObjectState(PetscObject obj, const char format[], ...)
2110d71ae5a4SJacob Faibussowitsch {
2111b665b14eSToby Isaac   PetscLogHandler handler;
21125c6c1daeSBarry Smith   va_list         Argp;
21135c6c1daeSBarry Smith 
21145c6c1daeSBarry Smith   PetscFunctionBegin;
2115b665b14eSToby Isaac   PetscCall(PetscLogTryGetHandler(PETSC_LOG_HANDLER_DEFAULT, &handler));
2116b665b14eSToby Isaac   if (handler) {
21175c6c1daeSBarry Smith     va_start(Argp, format);
2118b665b14eSToby Isaac     PetscCall(PetscLogHandlerDefaultLogObjectState(handler, obj, format, Argp));
21195c6c1daeSBarry Smith     va_end(Argp);
2120b665b14eSToby Isaac   }
21213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21225c6c1daeSBarry Smith }
21235c6c1daeSBarry Smith 
21245c6c1daeSBarry Smith /*MC
21255c6c1daeSBarry Smith   PetscLogFlops - Adds floating point operations to the global counter.
21265c6c1daeSBarry Smith 
21275c6c1daeSBarry Smith   Synopsis:
2128aaa7dc30SBarry Smith   #include <petsclog.h>
21295c6c1daeSBarry Smith   PetscErrorCode PetscLogFlops(PetscLogDouble f)
21305c6c1daeSBarry Smith 
21315c6c1daeSBarry Smith   Not Collective
21325c6c1daeSBarry Smith 
21335c6c1daeSBarry Smith   Input Parameter:
21345c6c1daeSBarry Smith . f - flop counter
21355c6c1daeSBarry Smith 
213610450e9eSJacob Faibussowitsch   Example Usage:
21375c6c1daeSBarry Smith .vb
21385c6c1daeSBarry Smith   PetscLogEvent USER_EVENT;
213910450e9eSJacob Faibussowitsch 
21405c6c1daeSBarry Smith   PetscLogEventRegister("User event", 0, &USER_EVENT);
21415c6c1daeSBarry Smith   PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0);
21425c6c1daeSBarry Smith   [code segment to monitor]
21435c6c1daeSBarry Smith   PetscLogFlops(user_flops)
21445c6c1daeSBarry Smith   PetscLogEventEnd(USER_EVENT, 0, 0, 0, 0);
21455c6c1daeSBarry Smith .ve
21465c6c1daeSBarry Smith 
2147d1f92df0SBarry Smith   Level: intermediate
2148d1f92df0SBarry Smith 
2149811af0c4SBarry Smith   Note:
215010450e9eSJacob Faibussowitsch    A global counter logs all PETSc flop counts. The user can use PetscLogFlops() to increment
215110450e9eSJacob Faibussowitsch    this counter to include flops for the application code.
21525c6c1daeSBarry Smith 
2153d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogGPUFlops()`, `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscGetFlops()`
21545c6c1daeSBarry Smith M*/
21555c6c1daeSBarry Smith 
21565c6c1daeSBarry Smith /*MC
215710450e9eSJacob Faibussowitsch   PetscPreLoadBegin - Begin a segment of code that may be preloaded (run twice) to get accurate
215810450e9eSJacob Faibussowitsch   timings
21595c6c1daeSBarry Smith 
21605c6c1daeSBarry Smith   Synopsis:
2161aaa7dc30SBarry Smith   #include <petsclog.h>
21625c6c1daeSBarry Smith   void PetscPreLoadBegin(PetscBool flag, char *name);
21635c6c1daeSBarry Smith 
21645c6c1daeSBarry Smith   Not Collective
21655c6c1daeSBarry Smith 
2166d8d19677SJose E. Roman   Input Parameters:
216710450e9eSJacob Faibussowitsch + flag - `PETSC_TRUE` to run twice, `PETSC_FALSE` to run once, may be overridden with command
216810450e9eSJacob Faibussowitsch          line option `-preload true|false`
216910450e9eSJacob Faibussowitsch - name - name of first stage (lines of code timed separately with `-log_view`) to be preloaded
21705c6c1daeSBarry Smith 
217110450e9eSJacob Faibussowitsch   Example Usage:
21725c6c1daeSBarry Smith .vb
217310450e9eSJacob Faibussowitsch   PetscPreLoadBegin(PETSC_TRUE, "first stage");
217410450e9eSJacob Faibussowitsch   // lines of code
21755c6c1daeSBarry Smith   PetscPreLoadStage("second stage");
217610450e9eSJacob Faibussowitsch   // lines of code
21775c6c1daeSBarry Smith   PetscPreLoadEnd();
21785c6c1daeSBarry Smith .ve
21795c6c1daeSBarry Smith 
2180d1f92df0SBarry Smith   Level: intermediate
2181d1f92df0SBarry Smith 
2182811af0c4SBarry Smith   Note:
218395452b02SPatrick Sanan   Only works in C/C++, not Fortran
21845c6c1daeSBarry Smith 
218510450e9eSJacob Faibussowitsch   Flags available within the macro\:
218610450e9eSJacob Faibussowitsch + PetscPreLoadingUsed - `PETSC_TRUE` if we are or have done preloading
218710450e9eSJacob Faibussowitsch . PetscPreLoadingOn   - `PETSC_TRUE` if it is CURRENTLY doing preload
218810450e9eSJacob Faibussowitsch . PetscPreLoadIt      - `0` for the first computation (with preloading turned off it is only
218910450e9eSJacob Faibussowitsch                         `0`) `1`  for the second
219010450e9eSJacob Faibussowitsch - PetscPreLoadMax     - number of times it will do the computation, only one when preloading is
219110450e9eSJacob Faibussowitsch                         turned on
219210450e9eSJacob Faibussowitsch 
219310450e9eSJacob Faibussowitsch   The first two variables are available throughout the program, the second two only between the
219410450e9eSJacob Faibussowitsch   `PetscPreLoadBegin()` and `PetscPreLoadEnd()`
21955c6c1daeSBarry Smith 
2196d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()`
21975c6c1daeSBarry Smith M*/
21985c6c1daeSBarry Smith 
21995c6c1daeSBarry Smith /*MC
220010450e9eSJacob Faibussowitsch   PetscPreLoadEnd - End a segment of code that may be preloaded (run twice) to get accurate
220110450e9eSJacob Faibussowitsch   timings
22025c6c1daeSBarry Smith 
22035c6c1daeSBarry Smith   Synopsis:
2204aaa7dc30SBarry Smith   #include <petsclog.h>
22055c6c1daeSBarry Smith   void PetscPreLoadEnd(void);
22065c6c1daeSBarry Smith 
22075c6c1daeSBarry Smith   Not Collective
22085c6c1daeSBarry Smith 
220910450e9eSJacob Faibussowitsch   Example Usage:
22105c6c1daeSBarry Smith .vb
221110450e9eSJacob Faibussowitsch   PetscPreLoadBegin(PETSC_TRUE, "first stage");
221210450e9eSJacob Faibussowitsch   // lines of code
22135c6c1daeSBarry Smith   PetscPreLoadStage("second stage");
221410450e9eSJacob Faibussowitsch   // lines of code
22155c6c1daeSBarry Smith   PetscPreLoadEnd();
22165c6c1daeSBarry Smith .ve
22175c6c1daeSBarry Smith 
2218d1f92df0SBarry Smith   Level: intermediate
2219d1f92df0SBarry Smith 
2220811af0c4SBarry Smith   Note:
2221811af0c4SBarry Smith   Only works in C/C++ not fortran
22225c6c1daeSBarry Smith 
2223d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadStage()`
22245c6c1daeSBarry Smith M*/
22255c6c1daeSBarry Smith 
22265c6c1daeSBarry Smith /*MC
222710450e9eSJacob Faibussowitsch   PetscPreLoadStage - Start a new segment of code to be timed separately to get accurate timings
22285c6c1daeSBarry Smith 
22295c6c1daeSBarry Smith   Synopsis:
2230aaa7dc30SBarry Smith   #include <petsclog.h>
22315c6c1daeSBarry Smith   void PetscPreLoadStage(char *name);
22325c6c1daeSBarry Smith 
22335c6c1daeSBarry Smith   Not Collective
22345c6c1daeSBarry Smith 
223510450e9eSJacob Faibussowitsch   Example Usage:
22365c6c1daeSBarry Smith .vb
223710450e9eSJacob Faibussowitsch   PetscPreLoadBegin(PETSC_TRUE,"first stage");
223810450e9eSJacob Faibussowitsch   // lines of code
22395c6c1daeSBarry Smith   PetscPreLoadStage("second stage");
224010450e9eSJacob Faibussowitsch   // lines of code
22415c6c1daeSBarry Smith   PetscPreLoadEnd();
22425c6c1daeSBarry Smith .ve
22435c6c1daeSBarry Smith 
2244d1f92df0SBarry Smith   Level: intermediate
2245d1f92df0SBarry Smith 
2246811af0c4SBarry Smith   Note:
2247811af0c4SBarry Smith   Only works in C/C++ not fortran
22485c6c1daeSBarry Smith 
2249d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`
22505c6c1daeSBarry Smith M*/
22515c6c1daeSBarry Smith 
2252a4af0ceeSJacob Faibussowitsch   #if PetscDefined(HAVE_DEVICE)
2253a4af0ceeSJacob Faibussowitsch     #include <petsc/private/deviceimpl.h>
22549ffd0706SHong Zhang 
2255156b51fbSBarry Smith /*
2256156b51fbSBarry Smith    This cannot be called by users between PetscInitialize() and PetscFinalize() at any random location in the code
2257156b51fbSBarry Smith    because it will result in timing results that cannot be interpreted.
2258156b51fbSBarry Smith */
2259d71ae5a4SJacob Faibussowitsch static PetscErrorCode PetscLogGpuTime_Off(void)
2260d71ae5a4SJacob Faibussowitsch {
2261156b51fbSBarry Smith   PetscLogGpuTimeFlag = PETSC_FALSE;
22623ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
2263156b51fbSBarry Smith }
2264156b51fbSBarry Smith 
2265156b51fbSBarry Smith /*@C
2266156b51fbSBarry Smith   PetscLogGpuTime - turn on the logging of GPU time for GPU kernels
2267156b51fbSBarry Smith 
2268811af0c4SBarry Smith   Options Database Key:
226910450e9eSJacob Faibussowitsch . -log_view_gpu_time - provide the GPU times in the `-log_view` output
2270156b51fbSBarry Smith 
2271d1f92df0SBarry Smith   Level: advanced
2272d1f92df0SBarry Smith 
2273156b51fbSBarry Smith   Notes:
227410450e9eSJacob Faibussowitsch   Turning on the timing of the GPU kernels can slow down the entire computation and should only
227510450e9eSJacob Faibussowitsch   be used when studying the performance of operations on GPU such as vector operations and
227610450e9eSJacob Faibussowitsch   matrix-vector operations.
2277156b51fbSBarry Smith 
227810450e9eSJacob Faibussowitsch   This routine should only be called once near the beginning of the program. Once it is started
227910450e9eSJacob Faibussowitsch   it cannot be turned off.
2280156b51fbSBarry Smith 
2281d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogView()`, `PetscLogGpuFlops()`, `PetscLogGpuTimeEnd()`, `PetscLogGpuTimeBegin()`
2282156b51fbSBarry Smith @*/
2283d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogGpuTime(void)
2284d71ae5a4SJacob Faibussowitsch {
2285156b51fbSBarry Smith   if (!PetscLogGpuTimeFlag) PetscCall(PetscRegisterFinalize(PetscLogGpuTime_Off));
2286156b51fbSBarry Smith   PetscLogGpuTimeFlag = PETSC_TRUE;
22873ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
2288156b51fbSBarry Smith }
2289156b51fbSBarry Smith 
22909ffd0706SHong Zhang /*@C
22919ffd0706SHong Zhang   PetscLogGpuTimeBegin - Start timer for device
22929ffd0706SHong Zhang 
2293d1f92df0SBarry Smith   Level: intermediate
2294d1f92df0SBarry Smith 
22959ffd0706SHong Zhang   Notes:
229610450e9eSJacob Faibussowitsch   When CUDA or HIP is enabled, the timer is run on the GPU, it is a separate logging of time
229710450e9eSJacob Faibussowitsch   devoted to GPU computations (excluding kernel launch times).
2298811af0c4SBarry Smith 
229910450e9eSJacob Faibussowitsch   When CUDA or HIP is not available, the timer is run on the CPU, it is a separate logging of
230010450e9eSJacob Faibussowitsch   time devoted to GPU computations (including kernel launch times).
2301811af0c4SBarry Smith 
230210450e9eSJacob Faibussowitsch   There is no need to call WaitForCUDA() or WaitForHIP() between `PetscLogGpuTimeBegin()` and
230310450e9eSJacob Faibussowitsch   `PetscLogGpuTimeEnd()`
2304811af0c4SBarry Smith 
230510450e9eSJacob Faibussowitsch   This timer should NOT include times for data transfers between the GPU and CPU, nor setup
230610450e9eSJacob Faibussowitsch   actions such as allocating space.
2307811af0c4SBarry Smith 
230810450e9eSJacob Faibussowitsch   The regular logging captures the time for data transfers and any CPU activities during the
230910450e9eSJacob Faibussowitsch   event. It is used to compute the flop rate on the GPU as it is actively engaged in running a
231010450e9eSJacob Faibussowitsch   kernel.
23119ffd0706SHong Zhang 
23129ffd0706SHong Zhang   Developer Notes:
231310450e9eSJacob Faibussowitsch   The GPU event timer captures the execution time of all the kernels launched in the default
231410450e9eSJacob Faibussowitsch   stream by the CPU between `PetscLogGpuTimeBegin()` and `PetsLogGpuTimeEnd()`.
2315811af0c4SBarry Smith 
231610450e9eSJacob Faibussowitsch   `PetscLogGpuTimeBegin()` and `PetsLogGpuTimeEnd()` insert the begin and end events into the
231710450e9eSJacob Faibussowitsch   default stream (stream 0). The device will record a time stamp for the event when it reaches
231810450e9eSJacob Faibussowitsch   that event in the stream. The function xxxEventSynchronize() is called in
231910450e9eSJacob Faibussowitsch   `PetsLogGpuTimeEnd()` to block CPU execution, but not continued GPU execution, until the
232010450e9eSJacob Faibussowitsch   timer event is recorded.
23219ffd0706SHong Zhang 
2322d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogView()`, `PetscLogGpuFlops()`, `PetscLogGpuTimeEnd()`, `PetscLogGpuTime()`
23239ffd0706SHong Zhang @*/
2324d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogGpuTimeBegin(void)
2325d71ae5a4SJacob Faibussowitsch {
2326b665b14eSToby Isaac   PetscBool isActive;
2327b665b14eSToby Isaac 
23289ffd0706SHong Zhang   PetscFunctionBegin;
2329b665b14eSToby Isaac   PetscCall(PetscLogEventBeginIsActive(&isActive));
2330b665b14eSToby Isaac   if (!isActive || !PetscLogGpuTimeFlag) PetscFunctionReturn(PETSC_SUCCESS);
2331744d70b0SJunchao Zhang   if (PetscDefined(HAVE_DEVICE)) {
2332a4af0ceeSJacob Faibussowitsch     PetscDeviceContext dctx;
2333a4af0ceeSJacob Faibussowitsch 
23349566063dSJacob Faibussowitsch     PetscCall(PetscDeviceContextGetCurrentContext(&dctx));
23359566063dSJacob Faibussowitsch     PetscCall(PetscDeviceContextBeginTimer_Internal(dctx));
2336a4af0ceeSJacob Faibussowitsch   } else {
23379566063dSJacob Faibussowitsch     PetscCall(PetscTimeSubtract(&petsc_gtime));
2338a4af0ceeSJacob Faibussowitsch   }
23393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
23409ffd0706SHong Zhang }
23419ffd0706SHong Zhang 
23429ffd0706SHong Zhang /*@C
23439ffd0706SHong Zhang   PetscLogGpuTimeEnd - Stop timer for device
23449ffd0706SHong Zhang 
23459ffd0706SHong Zhang   Level: intermediate
23469ffd0706SHong Zhang 
2347d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogView()`, `PetscLogGpuFlops()`, `PetscLogGpuTimeBegin()`
23489ffd0706SHong Zhang @*/
2349d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogGpuTimeEnd(void)
2350d71ae5a4SJacob Faibussowitsch {
2351b665b14eSToby Isaac   PetscBool isActive;
2352b665b14eSToby Isaac 
23539ffd0706SHong Zhang   PetscFunctionBegin;
2354b665b14eSToby Isaac   PetscCall(PetscLogEventEndIsActive(&isActive));
2355b665b14eSToby Isaac   if (!isActive || !PetscLogGpuTimeFlag) PetscFunctionReturn(PETSC_SUCCESS);
2356744d70b0SJunchao Zhang   if (PetscDefined(HAVE_DEVICE)) {
2357a4af0ceeSJacob Faibussowitsch     PetscDeviceContext dctx;
2358a4af0ceeSJacob Faibussowitsch     PetscLogDouble     elapsed;
2359a4af0ceeSJacob Faibussowitsch 
23609566063dSJacob Faibussowitsch     PetscCall(PetscDeviceContextGetCurrentContext(&dctx));
23619566063dSJacob Faibussowitsch     PetscCall(PetscDeviceContextEndTimer_Internal(dctx, &elapsed));
2362a4af0ceeSJacob Faibussowitsch     petsc_gtime += (elapsed / 1000.0);
2363a4af0ceeSJacob Faibussowitsch   } else {
23649566063dSJacob Faibussowitsch     PetscCall(PetscTimeAdd(&petsc_gtime));
2365a4af0ceeSJacob Faibussowitsch   }
23663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
23679ffd0706SHong Zhang }
2368c708d6e3SStefano Zampini 
23699ffd0706SHong Zhang   #endif /* end of PETSC_HAVE_DEVICE */
23709ffd0706SHong Zhang 
2371*cb9ef012SToby Isaac #endif /* PETSC_USE_LOG*/
2372*cb9ef012SToby Isaac 
2373b665b14eSToby Isaac /* -- Utility functions for logging from fortran -- */
2374b665b14eSToby Isaac 
2375b665b14eSToby Isaac PETSC_EXTERN PetscErrorCode PetscASend(int count, int datatype)
2376b665b14eSToby Isaac {
2377b665b14eSToby Isaac   PetscFunctionBegin;
2378*cb9ef012SToby Isaac #if PetscDefined(USE_LOG)
2379b665b14eSToby Isaac   PetscCall(PetscAddLogDouble(&petsc_send_ct, &petsc_send_ct_th, 1));
2380b665b14eSToby Isaac   #if !defined(MPIUNI_H) && !defined(PETSC_HAVE_BROKEN_RECURSIVE_MACRO)
2381b665b14eSToby Isaac   PetscCall(PetscMPITypeSize(count, MPI_Type_f2c((MPI_Fint)datatype), &petsc_send_len, &petsc_send_len_th));
2382b665b14eSToby Isaac   #endif
2383*cb9ef012SToby Isaac #endif
2384b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
2385b665b14eSToby Isaac }
2386b665b14eSToby Isaac 
2387b665b14eSToby Isaac PETSC_EXTERN PetscErrorCode PetscARecv(int count, int datatype)
2388b665b14eSToby Isaac {
2389b665b14eSToby Isaac   PetscFunctionBegin;
2390*cb9ef012SToby Isaac #if PetscDefined(USE_LOG)
2391b665b14eSToby Isaac   PetscCall(PetscAddLogDouble(&petsc_recv_ct, &petsc_recv_ct_th, 1));
2392b665b14eSToby Isaac   #if !defined(MPIUNI_H) && !defined(PETSC_HAVE_BROKEN_RECURSIVE_MACRO)
2393b665b14eSToby Isaac   PetscCall(PetscMPITypeSize(count, MPI_Type_f2c((MPI_Fint)datatype), &petsc_recv_len, &petsc_recv_len_th));
2394b665b14eSToby Isaac   #endif
2395*cb9ef012SToby Isaac #endif
2396b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
2397b665b14eSToby Isaac }
2398b665b14eSToby Isaac 
2399b665b14eSToby Isaac PETSC_EXTERN PetscErrorCode PetscAReduce(void)
2400b665b14eSToby Isaac {
2401b665b14eSToby Isaac   PetscFunctionBegin;
2402*cb9ef012SToby Isaac   if (PetscDefined(USE_LOG)) PetscCall(PetscAddLogDouble(&petsc_allreduce_ct, &petsc_allreduce_ct_th, 1));
2403b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
2404b665b14eSToby Isaac }
2405b665b14eSToby Isaac 
24065c6c1daeSBarry Smith PetscClassId PETSC_LARGEST_CLASSID = PETSC_SMALLEST_CLASSID;
24075c6c1daeSBarry Smith PetscClassId PETSC_OBJECT_CLASSID  = 0;
24085c6c1daeSBarry Smith 
24092611ad71SToby Isaac static PetscBool PetscLogInitializeCalled = PETSC_FALSE;
24102611ad71SToby Isaac 
24112611ad71SToby Isaac PETSC_INTERN PetscErrorCode PetscLogInitialize(void)
24122611ad71SToby Isaac {
24132611ad71SToby Isaac   int stage;
24142611ad71SToby Isaac 
24152611ad71SToby Isaac   PetscFunctionBegin;
24162611ad71SToby Isaac   if (PetscLogInitializeCalled) PetscFunctionReturn(PETSC_SUCCESS);
24172611ad71SToby Isaac   PetscLogInitializeCalled = PETSC_TRUE;
24182611ad71SToby Isaac   if (PetscDefined(USE_LOG)) {
24192611ad71SToby Isaac     /* Setup default logging structures */
24202611ad71SToby Isaac     PetscCall(PetscLogStateCreate(&petsc_log_state));
24212611ad71SToby Isaac     for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
24222611ad71SToby Isaac       if (PetscLogHandlers[i].handler) PetscCall(PetscLogHandlerSetState(PetscLogHandlers[i].handler, petsc_log_state));
24232611ad71SToby Isaac     }
24242611ad71SToby Isaac     PetscCall(PetscLogStateStageRegister(petsc_log_state, "Main Stage", &stage));
24252611ad71SToby Isaac     PetscCall(PetscSpinlockCreate(&PetscLogSpinLock));
24262611ad71SToby Isaac #if defined(PETSC_HAVE_THREADSAFETY)
24272611ad71SToby Isaac     petsc_log_tid = 0;
24282611ad71SToby Isaac     petsc_log_gid = 0;
24292611ad71SToby Isaac #endif
24302611ad71SToby Isaac 
24312611ad71SToby Isaac     /* All processors sync here for more consistent logging */
24322611ad71SToby Isaac     PetscCallMPI(MPI_Barrier(PETSC_COMM_WORLD));
24332611ad71SToby Isaac     PetscCall(PetscTime(&petsc_BaseTime));
24342611ad71SToby Isaac     PetscCall(PetscLogStagePush(stage));
24352611ad71SToby Isaac   }
24362611ad71SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
24372611ad71SToby Isaac }
24382611ad71SToby Isaac 
24392611ad71SToby Isaac PETSC_INTERN PetscErrorCode PetscLogFinalize(void)
24402611ad71SToby Isaac {
24412611ad71SToby Isaac   PetscFunctionBegin;
24422611ad71SToby Isaac   if (PetscDefined(USE_LOG)) {
2443b665b14eSToby Isaac     /* Resetting phase */
2444b665b14eSToby Isaac     // pop remaining stages
2445b665b14eSToby Isaac     if (petsc_log_state) {
2446b665b14eSToby Isaac       while (petsc_log_state->current_stage >= 0) { PetscCall(PetscLogStagePop()); }
2447b665b14eSToby Isaac     }
24482611ad71SToby Isaac     for (int i = 0; i < PETSC_LOG_HANDLER_MAX; i++) PetscCall(PetscLogHandlerDestroy(&PetscLogHandlers[i].handler));
24492611ad71SToby Isaac     PetscCall(PetscArrayzero(PetscLogHandlers, PETSC_LOG_HANDLER_MAX));
24502611ad71SToby Isaac     PetscCall(PetscLogStateDestroy(&petsc_log_state));
24512611ad71SToby Isaac 
24522611ad71SToby Isaac     petsc_TotalFlops         = 0.0;
24532611ad71SToby Isaac     petsc_BaseTime           = 0.0;
24542611ad71SToby Isaac     petsc_TotalFlops         = 0.0;
24552611ad71SToby Isaac     petsc_send_ct            = 0.0;
24562611ad71SToby Isaac     petsc_recv_ct            = 0.0;
24572611ad71SToby Isaac     petsc_send_len           = 0.0;
24582611ad71SToby Isaac     petsc_recv_len           = 0.0;
24592611ad71SToby Isaac     petsc_isend_ct           = 0.0;
24602611ad71SToby Isaac     petsc_irecv_ct           = 0.0;
24612611ad71SToby Isaac     petsc_isend_len          = 0.0;
24622611ad71SToby Isaac     petsc_irecv_len          = 0.0;
24632611ad71SToby Isaac     petsc_wait_ct            = 0.0;
24642611ad71SToby Isaac     petsc_wait_any_ct        = 0.0;
24652611ad71SToby Isaac     petsc_wait_all_ct        = 0.0;
24662611ad71SToby Isaac     petsc_sum_of_waits_ct    = 0.0;
24672611ad71SToby Isaac     petsc_allreduce_ct       = 0.0;
24682611ad71SToby Isaac     petsc_gather_ct          = 0.0;
24692611ad71SToby Isaac     petsc_scatter_ct         = 0.0;
24702611ad71SToby Isaac     petsc_TotalFlops_th      = 0.0;
24712611ad71SToby Isaac     petsc_send_ct_th         = 0.0;
24722611ad71SToby Isaac     petsc_recv_ct_th         = 0.0;
24732611ad71SToby Isaac     petsc_send_len_th        = 0.0;
24742611ad71SToby Isaac     petsc_recv_len_th        = 0.0;
24752611ad71SToby Isaac     petsc_isend_ct_th        = 0.0;
24762611ad71SToby Isaac     petsc_irecv_ct_th        = 0.0;
24772611ad71SToby Isaac     petsc_isend_len_th       = 0.0;
24782611ad71SToby Isaac     petsc_irecv_len_th       = 0.0;
24792611ad71SToby Isaac     petsc_wait_ct_th         = 0.0;
24802611ad71SToby Isaac     petsc_wait_any_ct_th     = 0.0;
24812611ad71SToby Isaac     petsc_wait_all_ct_th     = 0.0;
24822611ad71SToby Isaac     petsc_sum_of_waits_ct_th = 0.0;
24832611ad71SToby Isaac     petsc_allreduce_ct_th    = 0.0;
24842611ad71SToby Isaac     petsc_gather_ct_th       = 0.0;
24852611ad71SToby Isaac     petsc_scatter_ct_th      = 0.0;
24862611ad71SToby Isaac 
24872611ad71SToby Isaac     petsc_ctog_ct    = 0.0;
24882611ad71SToby Isaac     petsc_gtoc_ct    = 0.0;
24892611ad71SToby Isaac     petsc_ctog_sz    = 0.0;
24902611ad71SToby Isaac     petsc_gtoc_sz    = 0.0;
24912611ad71SToby Isaac     petsc_gflops     = 0.0;
24922611ad71SToby Isaac     petsc_gtime      = 0.0;
24932611ad71SToby Isaac     petsc_ctog_ct_th = 0.0;
24942611ad71SToby Isaac     petsc_gtoc_ct_th = 0.0;
24952611ad71SToby Isaac     petsc_ctog_sz_th = 0.0;
24962611ad71SToby Isaac     petsc_gtoc_sz_th = 0.0;
24972611ad71SToby Isaac     petsc_gflops_th  = 0.0;
24982611ad71SToby Isaac     petsc_gtime_th   = 0.0;
24992611ad71SToby Isaac   }
25002611ad71SToby Isaac   PETSC_LARGEST_CLASSID    = PETSC_SMALLEST_CLASSID;
25012611ad71SToby Isaac   PETSC_OBJECT_CLASSID     = 0;
25022611ad71SToby Isaac   PetscLogInitializeCalled = PETSC_FALSE;
25032611ad71SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
25042611ad71SToby Isaac }
25052611ad71SToby Isaac 
25065c6c1daeSBarry Smith /*@C
25075c6c1daeSBarry Smith   PetscClassIdRegister - Registers a new class name for objects and logging operations in an application code.
25085c6c1daeSBarry Smith 
25095c6c1daeSBarry Smith   Not Collective
25105c6c1daeSBarry Smith 
25115c6c1daeSBarry Smith   Input Parameter:
25125c6c1daeSBarry Smith . name - The class name
25135c6c1daeSBarry Smith 
25145c6c1daeSBarry Smith   Output Parameter:
25155c6c1daeSBarry Smith . oclass - The class id or classid
25165c6c1daeSBarry Smith 
25175c6c1daeSBarry Smith   Level: developer
25185c6c1daeSBarry Smith 
2519d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`
25205c6c1daeSBarry Smith @*/
2521d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscClassIdRegister(const char name[], PetscClassId *oclass)
2522d71ae5a4SJacob Faibussowitsch {
25235c6c1daeSBarry Smith   PetscFunctionBegin;
25245c6c1daeSBarry Smith   *oclass = ++PETSC_LARGEST_CLASSID;
25255c6c1daeSBarry Smith #if defined(PETSC_USE_LOG)
2526b665b14eSToby Isaac   {
2527b665b14eSToby Isaac     PetscLogState state;
2528b665b14eSToby Isaac     PetscLogClass logclass;
2529b665b14eSToby Isaac 
2530b665b14eSToby Isaac     PetscCall(PetscLogGetState(&state));
2531b665b14eSToby Isaac     if (state) PetscCall(PetscLogStateClassRegister(state, name, *oclass, &logclass));
2532b665b14eSToby Isaac   }
25335c6c1daeSBarry Smith #endif
25343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
25355c6c1daeSBarry Smith }
2536