xref: /petsc/src/sys/logging/plog.c (revision ffbd2f08570576d12bd1d65cc9cc8322c1a13f28)
15c6c1daeSBarry Smith /*
25c6c1daeSBarry Smith       PETSc code to log object creation and destruction and PETSc events.
35c6c1daeSBarry Smith 
45c6c1daeSBarry Smith       This provides the public API used by the rest of PETSc and by users.
55c6c1daeSBarry Smith 
65c6c1daeSBarry Smith       These routines use a private API that is not used elsewhere in PETSc and is not
75c6c1daeSBarry Smith       accessible to users. The private API is defined in logimpl.h and the utils directory.
85c6c1daeSBarry Smith 
9b665b14eSToby Isaac       ***
10b665b14eSToby Isaac 
11b665b14eSToby Isaac       This file, and only this file, is for functions that interact with the global logging state
125c6c1daeSBarry Smith */
13af0996ceSBarry Smith #include <petsc/private/logimpl.h> /*I    "petscsys.h"   I*/
1453e0a2f3SToby Isaac #include <petsc/private/loghandlerimpl.h>
155c6c1daeSBarry Smith #include <petsctime.h>
16665c2dedSJed Brown #include <petscviewer.h>
178fe3844cSJunchao Zhang #include <petscdevice.h>
188fe3844cSJunchao Zhang #include <petsc/private/deviceimpl.h>
195c6c1daeSBarry Smith 
20c708d6e3SStefano Zampini #if defined(PETSC_HAVE_THREADSAFETY)
21c708d6e3SStefano Zampini 
22c708d6e3SStefano Zampini PetscInt           petsc_log_gid = -1; /* Global threadId counter */
23c708d6e3SStefano Zampini PETSC_TLS PetscInt petsc_log_tid = -1; /* Local threadId */
24c708d6e3SStefano Zampini 
25c708d6e3SStefano Zampini /* shared variables */
26c708d6e3SStefano Zampini PetscSpinlock PetscLogSpinLock;
27c708d6e3SStefano Zampini 
282611ad71SToby Isaac PetscInt PetscLogGetTid(void)
292611ad71SToby Isaac {
302611ad71SToby Isaac   if (petsc_log_tid < 0) {
312611ad71SToby Isaac     PetscCall(PetscSpinlockLock(&PetscLogSpinLock));
322611ad71SToby Isaac     petsc_log_tid = ++petsc_log_gid;
332611ad71SToby Isaac     PetscCall(PetscSpinlockUnlock(&PetscLogSpinLock));
342611ad71SToby Isaac   }
352611ad71SToby Isaac   return petsc_log_tid;
362611ad71SToby Isaac }
372611ad71SToby Isaac 
38c708d6e3SStefano Zampini #endif
39c708d6e3SStefano Zampini 
405c6c1daeSBarry Smith /* Global counters */
415c6c1daeSBarry Smith PetscLogDouble petsc_BaseTime        = 0.0;
425c6c1daeSBarry Smith PetscLogDouble petsc_TotalFlops      = 0.0; /* The number of flops */
435c6c1daeSBarry Smith PetscLogDouble petsc_send_ct         = 0.0; /* The number of sends */
445c6c1daeSBarry Smith PetscLogDouble petsc_recv_ct         = 0.0; /* The number of receives */
455c6c1daeSBarry Smith PetscLogDouble petsc_send_len        = 0.0; /* The total length of all sent messages */
465c6c1daeSBarry Smith PetscLogDouble petsc_recv_len        = 0.0; /* The total length of all received messages */
475c6c1daeSBarry Smith PetscLogDouble petsc_isend_ct        = 0.0; /* The number of immediate sends */
485c6c1daeSBarry Smith PetscLogDouble petsc_irecv_ct        = 0.0; /* The number of immediate receives */
495c6c1daeSBarry Smith PetscLogDouble petsc_isend_len       = 0.0; /* The total length of all immediate send messages */
505c6c1daeSBarry Smith PetscLogDouble petsc_irecv_len       = 0.0; /* The total length of all immediate receive messages */
515c6c1daeSBarry Smith PetscLogDouble petsc_wait_ct         = 0.0; /* The number of waits */
525c6c1daeSBarry Smith PetscLogDouble petsc_wait_any_ct     = 0.0; /* The number of anywaits */
535c6c1daeSBarry Smith PetscLogDouble petsc_wait_all_ct     = 0.0; /* The number of waitalls */
545c6c1daeSBarry Smith PetscLogDouble petsc_sum_of_waits_ct = 0.0; /* The total number of waits */
555c6c1daeSBarry Smith PetscLogDouble petsc_allreduce_ct    = 0.0; /* The number of reductions */
565c6c1daeSBarry Smith PetscLogDouble petsc_gather_ct       = 0.0; /* The number of gathers and gathervs */
575c6c1daeSBarry Smith PetscLogDouble petsc_scatter_ct      = 0.0; /* The number of scatters and scattervs */
58c708d6e3SStefano Zampini 
59c708d6e3SStefano Zampini /* Thread Local storage */
60c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_TotalFlops_th      = 0.0;
61c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_send_ct_th         = 0.0;
62c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_recv_ct_th         = 0.0;
63c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_send_len_th        = 0.0;
64c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_recv_len_th        = 0.0;
65c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_isend_ct_th        = 0.0;
66c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_irecv_ct_th        = 0.0;
67c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_isend_len_th       = 0.0;
68c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_irecv_len_th       = 0.0;
69c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_wait_ct_th         = 0.0;
70c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_wait_any_ct_th     = 0.0;
71c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_wait_all_ct_th     = 0.0;
72c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_sum_of_waits_ct_th = 0.0;
73c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_allreduce_ct_th    = 0.0;
74c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_gather_ct_th       = 0.0;
75c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_scatter_ct_th      = 0.0;
76c708d6e3SStefano Zampini 
77bec0b493Shannah_mairs PetscLogDouble petsc_ctog_ct        = 0.0; /* The total number of CPU to GPU copies */
78bec0b493Shannah_mairs PetscLogDouble petsc_gtoc_ct        = 0.0; /* The total number of GPU to CPU copies */
79bec0b493Shannah_mairs PetscLogDouble petsc_ctog_sz        = 0.0; /* The total size of CPU to GPU copies */
80bec0b493Shannah_mairs PetscLogDouble petsc_gtoc_sz        = 0.0; /* The total size of GPU to CPU copies */
8145c4b7c1SBarry Smith PetscLogDouble petsc_ctog_ct_scalar = 0.0; /* The total number of CPU to GPU copies */
8245c4b7c1SBarry Smith PetscLogDouble petsc_gtoc_ct_scalar = 0.0; /* The total number of GPU to CPU copies */
8345c4b7c1SBarry Smith PetscLogDouble petsc_ctog_sz_scalar = 0.0; /* The total size of CPU to GPU copies */
8445c4b7c1SBarry Smith PetscLogDouble petsc_gtoc_sz_scalar = 0.0; /* The total size of GPU to CPU copies */
85958c4211Shannah_mairs PetscLogDouble petsc_gflops         = 0.0; /* The flops done on a GPU */
86958c4211Shannah_mairs PetscLogDouble petsc_gtime          = 0.0; /* The time spent on a GPU */
87c708d6e3SStefano Zampini 
88c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_ctog_ct_th        = 0.0;
89c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_gtoc_ct_th        = 0.0;
90c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_ctog_sz_th        = 0.0;
91c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_gtoc_sz_th        = 0.0;
92c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_ctog_ct_scalar_th = 0.0;
93c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_gtoc_ct_scalar_th = 0.0;
94c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_ctog_sz_scalar_th = 0.0;
95c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_gtoc_sz_scalar_th = 0.0;
96c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_gflops_th         = 0.0;
97c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_gtime_th          = 0.0;
982611ad71SToby Isaac 
992611ad71SToby Isaac PetscBool PetscLogMemory = PETSC_FALSE;
1002611ad71SToby Isaac PetscBool PetscLogSyncOn = PETSC_FALSE;
1012611ad71SToby Isaac 
1022611ad71SToby Isaac PetscBool PetscLogGpuTimeFlag = PETSC_FALSE;
1032611ad71SToby Isaac 
1042611ad71SToby Isaac PetscLogState petsc_log_state = NULL;
1052611ad71SToby Isaac 
10610dd146fSPierre Jolivet #define PETSC_LOG_HANDLER_HOT_BLANK {NULL, NULL, NULL, NULL, NULL, NULL}
1072611ad71SToby Isaac 
1082611ad71SToby Isaac PetscLogHandlerHot PetscLogHandlers[PETSC_LOG_HANDLER_MAX] = {
1092611ad71SToby Isaac   PETSC_LOG_HANDLER_HOT_BLANK,
1102611ad71SToby Isaac   PETSC_LOG_HANDLER_HOT_BLANK,
1112611ad71SToby Isaac   PETSC_LOG_HANDLER_HOT_BLANK,
1122611ad71SToby Isaac   PETSC_LOG_HANDLER_HOT_BLANK,
1132611ad71SToby Isaac };
1142611ad71SToby Isaac 
1152611ad71SToby Isaac #undef PETSC_LOG_HANDLERS_HOT_BLANK
1162611ad71SToby Isaac 
1172611ad71SToby Isaac #if defined(PETSC_USE_LOG)
1182611ad71SToby Isaac   #include <../src/sys/logging/handler/impls/default/logdefault.h>
119c708d6e3SStefano Zampini 
120c708d6e3SStefano Zampini   #if defined(PETSC_HAVE_THREADSAFETY)
121c708d6e3SStefano Zampini PetscErrorCode PetscAddLogDouble(PetscLogDouble *tot, PetscLogDouble *tot_th, PetscLogDouble tmp)
122c708d6e3SStefano Zampini {
123c708d6e3SStefano Zampini   *tot_th += tmp;
1243ba16761SJacob Faibussowitsch   PetscCall(PetscSpinlockLock(&PetscLogSpinLock));
125c708d6e3SStefano Zampini   *tot += tmp;
1263ba16761SJacob Faibussowitsch   PetscCall(PetscSpinlockUnlock(&PetscLogSpinLock));
1273ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
128c708d6e3SStefano Zampini }
129c708d6e3SStefano Zampini 
130c708d6e3SStefano Zampini PetscErrorCode PetscAddLogDoubleCnt(PetscLogDouble *cnt, PetscLogDouble *tot, PetscLogDouble *cnt_th, PetscLogDouble *tot_th, PetscLogDouble tmp)
131c708d6e3SStefano Zampini {
132c708d6e3SStefano Zampini   *cnt_th = *cnt_th + 1;
133c708d6e3SStefano Zampini   *tot_th += tmp;
1343ba16761SJacob Faibussowitsch   PetscCall(PetscSpinlockLock(&PetscLogSpinLock));
13557508eceSPierre Jolivet   *tot += (PetscLogDouble)tmp;
136c708d6e3SStefano Zampini   *cnt += *cnt + 1;
1373ba16761SJacob Faibussowitsch   PetscCall(PetscSpinlockUnlock(&PetscLogSpinLock));
1383ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
139c708d6e3SStefano Zampini }
140c708d6e3SStefano Zampini 
141bec0b493Shannah_mairs   #endif
1425c6c1daeSBarry Smith 
14353e0a2f3SToby Isaac static PetscErrorCode PetscLogTryGetHandler(PetscLogHandlerType type, PetscLogHandler *handler)
14453e0a2f3SToby Isaac {
14553e0a2f3SToby Isaac   PetscFunctionBegin;
14653e0a2f3SToby Isaac   PetscAssertPointer(handler, 2);
14753e0a2f3SToby Isaac   *handler = NULL;
14853e0a2f3SToby Isaac   for (int i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
14953e0a2f3SToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
15053e0a2f3SToby Isaac     if (h) {
15153e0a2f3SToby Isaac       PetscBool match;
15253e0a2f3SToby Isaac 
15353e0a2f3SToby Isaac       PetscCall(PetscObjectTypeCompare((PetscObject)h, type, &match));
15453e0a2f3SToby Isaac       if (match) {
15553e0a2f3SToby Isaac         *handler = PetscLogHandlers[i].handler;
15653e0a2f3SToby Isaac         PetscFunctionReturn(PETSC_SUCCESS);
15753e0a2f3SToby Isaac       }
15853e0a2f3SToby Isaac     }
15953e0a2f3SToby Isaac   }
16053e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
16153e0a2f3SToby Isaac }
16253e0a2f3SToby Isaac 
16353e0a2f3SToby Isaac /*@
164b665b14eSToby Isaac   PetscLogGetDefaultHandler - Get the default log handler if it is running.
165b665b14eSToby Isaac 
166b665b14eSToby Isaac   Not collective
167b665b14eSToby Isaac 
168b665b14eSToby Isaac   Output Parameter:
169b665b14eSToby Isaac . handler - the default `PetscLogHandler`, or `NULL` if it is not running.
170b665b14eSToby Isaac 
171b665b14eSToby Isaac   Level: developer
172b665b14eSToby Isaac 
173b665b14eSToby Isaac   Notes:
174b665b14eSToby Isaac   The default handler is started with `PetscLogDefaultBegin()`,
175b665b14eSToby Isaac   if the options flags `-log_all` or `-log_view` is given without arguments,
176b665b14eSToby Isaac   or for `-log_view :output:format` if `format` is not `ascii_xml` or `ascii_flamegraph`.
177b665b14eSToby Isaac 
178b665b14eSToby Isaac .seealso: [](ch_profiling)
179b665b14eSToby Isaac @*/
180b665b14eSToby Isaac PetscErrorCode PetscLogGetDefaultHandler(PetscLogHandler *handler)
181b665b14eSToby Isaac {
182b665b14eSToby Isaac   PetscFunctionBegin;
183294de794SToby Isaac   PetscCall(PetscLogTryGetHandler(PETSCLOGHANDLERDEFAULT, handler));
184b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
185b665b14eSToby Isaac }
186b665b14eSToby Isaac 
187b665b14eSToby Isaac static PetscErrorCode PetscLogGetHandler(PetscLogHandlerType type, PetscLogHandler *handler)
188b665b14eSToby Isaac {
189b665b14eSToby Isaac   PetscFunctionBegin;
190b665b14eSToby Isaac   PetscAssertPointer(handler, 2);
191b665b14eSToby Isaac   PetscCall(PetscLogTryGetHandler(type, handler));
1921943a749SToby Isaac   PetscCheck(*handler != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "A PetscLogHandler of type %s has not been started.", type);
193b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
194b665b14eSToby Isaac }
195b665b14eSToby Isaac 
196b665b14eSToby Isaac /*@
19753e0a2f3SToby Isaac   PetscLogGetState - Get the `PetscLogState` for PETSc's global logging, used
19853e0a2f3SToby Isaac   by all default log handlers (`PetscLogDefaultBegin()`,
19953e0a2f3SToby Isaac   `PetscLogNestedBegin()`, `PetscLogTraceBegin()`, `PetscLogMPEBegin()`,
20053e0a2f3SToby Isaac   `PetscLogPerfstubsBegin()`).
20153e0a2f3SToby Isaac 
20253e0a2f3SToby Isaac   Collective on `PETSC_COMM_WORLD`
20353e0a2f3SToby Isaac 
20453e0a2f3SToby Isaac   Output Parameter:
205b665b14eSToby Isaac . state - The `PetscLogState` changed by registrations (such as
206b665b14eSToby Isaac           `PetscLogEventRegister()`) and actions (such as `PetscLogEventBegin()` or
207b8004f34SBarry Smith           `PetscLogStagePush()`), or `NULL` if logging is not active
20853e0a2f3SToby Isaac 
20953e0a2f3SToby Isaac   Level: developer
21053e0a2f3SToby Isaac 
21153e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogState`
21253e0a2f3SToby Isaac @*/
21353e0a2f3SToby Isaac PetscErrorCode PetscLogGetState(PetscLogState *state)
21453e0a2f3SToby Isaac {
21553e0a2f3SToby Isaac   PetscFunctionBegin;
21653e0a2f3SToby Isaac   PetscAssertPointer(state, 1);
21753e0a2f3SToby Isaac   *state = petsc_log_state;
21853e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
21953e0a2f3SToby Isaac }
22053e0a2f3SToby Isaac 
22153e0a2f3SToby Isaac static PetscErrorCode PetscLogHandlerCopyToHot(PetscLogHandler h, PetscLogHandlerHot *hot)
22253e0a2f3SToby Isaac {
22353e0a2f3SToby Isaac   PetscFunctionBegin;
22453e0a2f3SToby Isaac   hot->handler       = h;
22553e0a2f3SToby Isaac   hot->eventBegin    = h->ops->eventbegin;
22653e0a2f3SToby Isaac   hot->eventEnd      = h->ops->eventend;
22753e0a2f3SToby Isaac   hot->eventSync     = h->ops->eventsync;
22853e0a2f3SToby Isaac   hot->objectCreate  = h->ops->objectcreate;
22953e0a2f3SToby Isaac   hot->objectDestroy = h->ops->objectdestroy;
23053e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
23153e0a2f3SToby Isaac }
23253e0a2f3SToby Isaac 
23353e0a2f3SToby Isaac /*@
23453e0a2f3SToby Isaac   PetscLogHandlerStart - Connect a log handler to PETSc's global logging stream and state.
23553e0a2f3SToby Isaac 
23653e0a2f3SToby Isaac   Logically collective
23753e0a2f3SToby Isaac 
23853e0a2f3SToby Isaac   Input Parameters:
23953e0a2f3SToby Isaac . h - a `PetscLogHandler`
24053e0a2f3SToby Isaac 
24153e0a2f3SToby Isaac   Level: developer
24253e0a2f3SToby Isaac 
24353e0a2f3SToby Isaac   Notes:
24453e0a2f3SToby Isaac   Users should only need this if they create their own log handlers: handlers that are started
24553e0a2f3SToby Isaac   from the command line (such as `-log_view` and `-log_trace`) or from a function like
24653e0a2f3SToby Isaac   `PetscLogNestedBegin()` will automatically be started.
24753e0a2f3SToby Isaac 
24853e0a2f3SToby Isaac   There is a limit of `PESC_LOG_HANDLER_MAX` handlers that can be active at one time.
24953e0a2f3SToby Isaac 
25053e0a2f3SToby Isaac   To disconnect a handler from the global stream call `PetscLogHandlerStop()`.
25153e0a2f3SToby Isaac 
25253e0a2f3SToby Isaac   When a log handler is started, stages that have already been pushed with `PetscLogStagePush()`,
25353e0a2f3SToby Isaac   will be pushed for the new log handler, but it will not be informed of any events that are
25453e0a2f3SToby Isaac   in progress.  It is recommended to start any user-defined log handlers immediately following
255b8004f34SBarry Smith   `PetscInitialize()`  before any user-defined stages are pushed.
25653e0a2f3SToby Isaac 
257b8004f34SBarry Smith .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogState`, `PetscLogHandlerStop()`, `PetscInitialize()`
25853e0a2f3SToby Isaac @*/
25953e0a2f3SToby Isaac PetscErrorCode PetscLogHandlerStart(PetscLogHandler h)
26053e0a2f3SToby Isaac {
26153e0a2f3SToby Isaac   PetscFunctionBegin;
26253e0a2f3SToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
26353e0a2f3SToby Isaac     if (PetscLogHandlers[i].handler == h) PetscFunctionReturn(PETSC_SUCCESS);
26453e0a2f3SToby Isaac   }
26553e0a2f3SToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
26653e0a2f3SToby Isaac     if (PetscLogHandlers[i].handler == NULL) {
26753e0a2f3SToby Isaac       PetscCall(PetscObjectReference((PetscObject)h));
26853e0a2f3SToby Isaac       PetscCall(PetscLogHandlerCopyToHot(h, &PetscLogHandlers[i]));
26953e0a2f3SToby Isaac       if (petsc_log_state) {
27053e0a2f3SToby Isaac         PetscLogStage stack_height;
27153e0a2f3SToby Isaac         PetscIntStack orig_stack, temp_stack;
27253e0a2f3SToby Isaac 
27353e0a2f3SToby Isaac         PetscCall(PetscLogHandlerSetState(h, petsc_log_state));
27453e0a2f3SToby Isaac         stack_height = petsc_log_state->stage_stack->top + 1;
27553e0a2f3SToby Isaac         PetscCall(PetscIntStackCreate(&temp_stack));
27653e0a2f3SToby Isaac         orig_stack                     = petsc_log_state->stage_stack;
27753e0a2f3SToby Isaac         petsc_log_state->stage_stack   = temp_stack;
27853e0a2f3SToby Isaac         petsc_log_state->current_stage = -1;
27953e0a2f3SToby Isaac         for (int s = 0; s < stack_height; s++) {
28053e0a2f3SToby Isaac           PetscLogStage stage = (PetscLogStage)orig_stack->stack[s];
28153e0a2f3SToby Isaac           PetscCall(PetscLogHandlerStagePush(h, stage));
28253e0a2f3SToby Isaac           PetscCall(PetscIntStackPush(temp_stack, stage));
28353e0a2f3SToby Isaac           petsc_log_state->current_stage = stage;
28453e0a2f3SToby Isaac         }
28553e0a2f3SToby Isaac         PetscCall(PetscIntStackDestroy(temp_stack));
28653e0a2f3SToby Isaac         petsc_log_state->stage_stack = orig_stack;
28753e0a2f3SToby Isaac       }
28853e0a2f3SToby Isaac       PetscFunctionReturn(PETSC_SUCCESS);
28953e0a2f3SToby Isaac     }
29053e0a2f3SToby Isaac   }
29153e0a2f3SToby Isaac   SETERRQ(PetscObjectComm((PetscObject)h), PETSC_ERR_ARG_WRONGSTATE, "%d log handlers already started, cannot start another", PETSC_LOG_HANDLER_MAX);
29253e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
29353e0a2f3SToby Isaac }
29453e0a2f3SToby Isaac 
29553e0a2f3SToby Isaac /*@
29653e0a2f3SToby Isaac   PetscLogHandlerStop - Disconnect a log handler from PETSc's global logging stream.
29753e0a2f3SToby Isaac 
29853e0a2f3SToby Isaac   Logically collective
29953e0a2f3SToby Isaac 
30053e0a2f3SToby Isaac   Input Parameters:
30153e0a2f3SToby Isaac . h - a `PetscLogHandler`
30253e0a2f3SToby Isaac 
30353e0a2f3SToby Isaac   Level: developer
30453e0a2f3SToby Isaac 
30553e0a2f3SToby Isaac   Note:
30653e0a2f3SToby Isaac   After `PetscLogHandlerStop()`, the handler can still access the global logging state
30753e0a2f3SToby Isaac   with `PetscLogHandlerGetState()`, so that it can access the registry when post-processing
30853e0a2f3SToby Isaac   (for instance, in `PetscLogHandlerView()`),
30953e0a2f3SToby Isaac 
31053e0a2f3SToby Isaac   When a log handler is stopped, the remaining stages will be popped before it is
31153e0a2f3SToby Isaac   disconnected from the log stream.
31253e0a2f3SToby Isaac 
31353e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogState`, `PetscLogHandlerStart()`
31453e0a2f3SToby Isaac @*/
31553e0a2f3SToby Isaac PetscErrorCode PetscLogHandlerStop(PetscLogHandler h)
31653e0a2f3SToby Isaac {
31753e0a2f3SToby Isaac   PetscFunctionBegin;
31853e0a2f3SToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
31953e0a2f3SToby Isaac     if (PetscLogHandlers[i].handler == h) {
32053e0a2f3SToby Isaac       if (petsc_log_state) {
32153e0a2f3SToby Isaac         PetscLogState state;
32253e0a2f3SToby Isaac         PetscLogStage stack_height;
32353e0a2f3SToby Isaac         PetscIntStack orig_stack, temp_stack;
32453e0a2f3SToby Isaac 
32553e0a2f3SToby Isaac         PetscCall(PetscLogHandlerGetState(h, &state));
32653e0a2f3SToby Isaac         PetscCheck(state == petsc_log_state, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONGSTATE, "Called PetscLogHandlerStop() for a PetscLogHander that was not started.");
32753e0a2f3SToby Isaac         stack_height = petsc_log_state->stage_stack->top + 1;
32853e0a2f3SToby Isaac         PetscCall(PetscIntStackCreate(&temp_stack));
32953e0a2f3SToby Isaac         orig_stack                   = petsc_log_state->stage_stack;
33053e0a2f3SToby Isaac         petsc_log_state->stage_stack = temp_stack;
33153e0a2f3SToby Isaac         for (int s = 0; s < stack_height; s++) {
33253e0a2f3SToby Isaac           PetscLogStage stage = (PetscLogStage)orig_stack->stack[s];
33353e0a2f3SToby Isaac 
33453e0a2f3SToby Isaac           PetscCall(PetscIntStackPush(temp_stack, stage));
33553e0a2f3SToby Isaac         }
33653e0a2f3SToby Isaac         for (int s = 0; s < stack_height; s++) {
33753e0a2f3SToby Isaac           PetscLogStage stage;
33853e0a2f3SToby Isaac           PetscBool     empty;
33953e0a2f3SToby Isaac 
34053e0a2f3SToby Isaac           PetscCall(PetscIntStackPop(temp_stack, &stage));
34153e0a2f3SToby Isaac           PetscCall(PetscIntStackEmpty(temp_stack, &empty));
34253e0a2f3SToby Isaac           if (!empty) {
34353e0a2f3SToby Isaac             PetscCall(PetscIntStackTop(temp_stack, &petsc_log_state->current_stage));
34453e0a2f3SToby Isaac           } else petsc_log_state->current_stage = -1;
34553e0a2f3SToby Isaac           PetscCall(PetscLogHandlerStagePop(h, stage));
34653e0a2f3SToby Isaac         }
34753e0a2f3SToby Isaac         PetscCall(PetscIntStackDestroy(temp_stack));
34853e0a2f3SToby Isaac         petsc_log_state->stage_stack = orig_stack;
34953e0a2f3SToby Isaac         PetscCall(PetscIntStackTop(petsc_log_state->stage_stack, &petsc_log_state->current_stage));
35053e0a2f3SToby Isaac       }
35153e0a2f3SToby Isaac       PetscCall(PetscArrayzero(&PetscLogHandlers[i], 1));
35253e0a2f3SToby Isaac       PetscCall(PetscObjectDereference((PetscObject)h));
35353e0a2f3SToby Isaac     }
35453e0a2f3SToby Isaac   }
35553e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
35653e0a2f3SToby Isaac }
3575c6c1daeSBarry Smith 
358cc4c1da9SBarry Smith /*@
3594dd65854SConnor Ward   PetscLogIsActive - Check if logging is currently in progress.
3604dd65854SConnor Ward 
3614dd65854SConnor Ward   Not Collective
3624dd65854SConnor Ward 
3634dd65854SConnor Ward   Output Parameter:
364811af0c4SBarry Smith . isActive - `PETSC_TRUE` if logging is in progress, `PETSC_FALSE` otherwise
3654dd65854SConnor Ward 
3664dd65854SConnor Ward   Level: beginner
3674dd65854SConnor Ward 
368b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogDefaultBegin()`
3694dd65854SConnor Ward @*/
370d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogIsActive(PetscBool *isActive)
371d71ae5a4SJacob Faibussowitsch {
3724dd65854SConnor Ward   PetscFunctionBegin;
373b665b14eSToby Isaac   *isActive = PETSC_FALSE;
374b665b14eSToby Isaac   if (petsc_log_state) {
375b665b14eSToby Isaac     for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
376b665b14eSToby Isaac       if (PetscLogHandlers[i].handler) {
377b665b14eSToby Isaac         *isActive = PETSC_TRUE;
378b665b14eSToby Isaac         PetscFunctionReturn(PETSC_SUCCESS);
379b665b14eSToby Isaac       }
380b665b14eSToby Isaac     }
381b665b14eSToby Isaac   }
382b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
383b665b14eSToby Isaac }
384b665b14eSToby Isaac 
385b665b14eSToby Isaac PETSC_UNUSED static PetscErrorCode PetscLogEventBeginIsActive(PetscBool *isActive)
386b665b14eSToby Isaac {
387b665b14eSToby Isaac   PetscFunctionBegin;
388b665b14eSToby Isaac   *isActive = PETSC_FALSE;
389b665b14eSToby Isaac   if (petsc_log_state) {
390b665b14eSToby Isaac     for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
391b665b14eSToby Isaac       if (PetscLogHandlers[i].eventBegin) {
392b665b14eSToby Isaac         *isActive = PETSC_TRUE;
393b665b14eSToby Isaac         PetscFunctionReturn(PETSC_SUCCESS);
394b665b14eSToby Isaac       }
395b665b14eSToby Isaac     }
396b665b14eSToby Isaac   }
397b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
398b665b14eSToby Isaac }
399b665b14eSToby Isaac 
400b665b14eSToby Isaac PETSC_UNUSED static PetscErrorCode PetscLogEventEndIsActive(PetscBool *isActive)
401b665b14eSToby Isaac {
402b665b14eSToby Isaac   PetscFunctionBegin;
403b665b14eSToby Isaac   *isActive = PETSC_FALSE;
404b665b14eSToby Isaac   if (petsc_log_state) {
405b665b14eSToby Isaac     for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
406b665b14eSToby Isaac       if (PetscLogHandlers[i].eventEnd) {
407b665b14eSToby Isaac         *isActive = PETSC_TRUE;
408b665b14eSToby Isaac         PetscFunctionReturn(PETSC_SUCCESS);
409b665b14eSToby Isaac       }
410b665b14eSToby Isaac     }
411b665b14eSToby Isaac   }
4123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4134dd65854SConnor Ward }
4144dd65854SConnor Ward 
41561cc7448SToby Isaac PETSC_INTERN PetscErrorCode PetscLogTypeBegin(PetscLogHandlerType type)
41653e0a2f3SToby Isaac {
41753e0a2f3SToby Isaac   PetscLogHandler handler;
41853e0a2f3SToby Isaac 
41953e0a2f3SToby Isaac   PetscFunctionBegin;
42053e0a2f3SToby Isaac   PetscCall(PetscLogTryGetHandler(type, &handler));
42153e0a2f3SToby Isaac   if (handler) PetscFunctionReturn(PETSC_SUCCESS);
42253e0a2f3SToby Isaac   PetscCall(PetscLogHandlerCreate(PETSC_COMM_WORLD, &handler));
42353e0a2f3SToby Isaac   PetscCall(PetscLogHandlerSetType(handler, type));
42453e0a2f3SToby Isaac   PetscCall(PetscLogHandlerStart(handler));
42553e0a2f3SToby Isaac   PetscCall(PetscLogHandlerDestroy(&handler));
42653e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
42753e0a2f3SToby Isaac }
42853e0a2f3SToby Isaac 
429cc4c1da9SBarry Smith /*@
430b665b14eSToby Isaac   PetscLogDefaultBegin - Turns on logging of objects and events using the default log handler. This logs flop
4315c6c1daeSBarry Smith   rates and object creation and should not slow programs down too much.
4325c6c1daeSBarry Smith   This routine may be called more than once.
4335c6c1daeSBarry Smith 
4348f14a041SBarry Smith   Logically Collective on `PETSC_COMM_WORLD`
4355c6c1daeSBarry Smith 
436811af0c4SBarry Smith   Options Database Key:
437a2553e36SBarry Smith . -log_view [viewertype:filename:viewerformat] - Prints summary of flop and timing information to the
438a2553e36SBarry Smith                                                  screen (for code configured with --with-log=1 (which is the default))
4395c6c1daeSBarry Smith 
44010450e9eSJacob Faibussowitsch   Example Usage:
4415c6c1daeSBarry Smith .vb
4425c6c1daeSBarry Smith       PetscInitialize(...);
443bb1d7374SBarry Smith       PetscLogDefaultBegin();
4445c6c1daeSBarry Smith        ... code ...
4455c6c1daeSBarry Smith       PetscLogView(viewer); or PetscLogDump();
4465c6c1daeSBarry Smith       PetscFinalize();
4475c6c1daeSBarry Smith .ve
4485c6c1daeSBarry Smith 
449d1f92df0SBarry Smith   Level: advanced
450d1f92df0SBarry Smith 
451811af0c4SBarry Smith   Note:
452811af0c4SBarry Smith   `PetscLogView()` or `PetscLogDump()` actually cause the printing of
4535c6c1daeSBarry Smith   the logging information.
4545c6c1daeSBarry Smith 
455b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogDump()`, `PetscLogView()`, `PetscLogTraceBegin()`
4565c6c1daeSBarry Smith @*/
457d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogDefaultBegin(void)
458d71ae5a4SJacob Faibussowitsch {
4595c6c1daeSBarry Smith   PetscFunctionBegin;
460294de794SToby Isaac   PetscCall(PetscLogTypeBegin(PETSCLOGHANDLERDEFAULT));
4613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4625c6c1daeSBarry Smith }
4635c6c1daeSBarry Smith 
4645c6c1daeSBarry Smith /*@C
465b665b14eSToby Isaac   PetscLogTraceBegin - Begins trace logging.  Every time a PETSc event
4665c6c1daeSBarry Smith   begins or ends, the event name is printed.
4675c6c1daeSBarry Smith 
468cc4c1da9SBarry Smith   Logically Collective on `PETSC_COMM_WORLD`, No Fortran Support
4695c6c1daeSBarry Smith 
4705c6c1daeSBarry Smith   Input Parameter:
4715c6c1daeSBarry Smith . file - The file to print trace in (e.g. stdout)
4725c6c1daeSBarry Smith 
4735c6c1daeSBarry Smith   Options Database Key:
474b665b14eSToby Isaac . -log_trace [filename] - Begins `PetscLogTraceBegin()`
4755c6c1daeSBarry Smith 
476d1f92df0SBarry Smith   Level: intermediate
477d1f92df0SBarry Smith 
4785c6c1daeSBarry Smith   Notes:
479811af0c4SBarry Smith   `PetscLogTraceBegin()` prints the processor number, the execution time (sec),
4805c6c1daeSBarry Smith   then "Event begin:" or "Event end:" followed by the event name.
4815c6c1daeSBarry Smith 
482811af0c4SBarry Smith   `PetscLogTraceBegin()` allows tracing of all PETSc calls, which is useful
4835c6c1daeSBarry Smith   to determine where a program is hanging without running in the
4845c6c1daeSBarry Smith   debugger.  Can be used in conjunction with the -info option.
4855c6c1daeSBarry Smith 
486b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogDump()`, `PetscLogView()`, `PetscLogDefaultBegin()`
4875c6c1daeSBarry Smith @*/
488d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogTraceBegin(FILE *file)
489d71ae5a4SJacob Faibussowitsch {
490b665b14eSToby Isaac   PetscLogHandler handler;
4914d86920dSPierre Jolivet 
4925c6c1daeSBarry Smith   PetscFunctionBegin;
493294de794SToby Isaac   PetscCall(PetscLogTryGetHandler(PETSCLOGHANDLERTRACE, &handler));
494b665b14eSToby Isaac   if (handler) PetscFunctionReturn(PETSC_SUCCESS);
495b665b14eSToby Isaac   PetscCall(PetscLogHandlerCreateTrace(PETSC_COMM_WORLD, file, &handler));
496b665b14eSToby Isaac   PetscCall(PetscLogHandlerStart(handler));
497b665b14eSToby Isaac   PetscCall(PetscLogHandlerDestroy(&handler));
498b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
499b665b14eSToby Isaac }
500a297a907SKarl Rupp 
501b665b14eSToby Isaac PETSC_INTERN PetscErrorCode PetscLogHandlerCreate_Nested(MPI_Comm, PetscLogHandler *);
502b665b14eSToby Isaac 
503cc4c1da9SBarry Smith /*@
504b665b14eSToby Isaac   PetscLogNestedBegin - Turns on nested logging of objects and events. This logs flop
505b665b14eSToby Isaac   rates and object creation and should not slow programs down too much.
506b665b14eSToby Isaac 
507cc4c1da9SBarry Smith   Logically Collective on `PETSC_COMM_WORLD`, No Fortran Support
508b665b14eSToby Isaac 
509b665b14eSToby Isaac   Options Database Keys:
510b665b14eSToby Isaac . -log_view :filename.xml:ascii_xml - Prints an XML summary of flop and timing information to the file
511b665b14eSToby Isaac 
512b665b14eSToby Isaac   Example Usage:
513b665b14eSToby Isaac .vb
514b665b14eSToby Isaac       PetscInitialize(...);
515b665b14eSToby Isaac       PetscLogNestedBegin();
516b665b14eSToby Isaac        ... code ...
517b665b14eSToby Isaac       PetscLogView(viewer);
518b665b14eSToby Isaac       PetscFinalize();
519b665b14eSToby Isaac .ve
520b665b14eSToby Isaac 
521b665b14eSToby Isaac   Level: advanced
522b665b14eSToby Isaac 
523b665b14eSToby Isaac .seealso: `PetscLogDump()`, `PetscLogView()`, `PetscLogTraceBegin()`, `PetscLogDefaultBegin()`
524b665b14eSToby Isaac @*/
525b665b14eSToby Isaac PetscErrorCode PetscLogNestedBegin(void)
526b665b14eSToby Isaac {
527b665b14eSToby Isaac   PetscFunctionBegin;
528294de794SToby Isaac   PetscCall(PetscLogTypeBegin(PETSCLOGHANDLERNESTED));
5293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5305c6c1daeSBarry Smith }
5315c6c1daeSBarry Smith 
53253e0a2f3SToby Isaac /*@C
53353e0a2f3SToby Isaac   PetscLogLegacyCallbacksBegin - Create and start a log handler from callbacks
53453e0a2f3SToby Isaac   matching the now deprecated function pointers `PetscLogPLB`, `PetscLogPLE`,
53553e0a2f3SToby Isaac   `PetscLogPHC`, `PetscLogPHD`.
53653e0a2f3SToby Isaac 
5378f14a041SBarry Smith   Logically Collective on `PETSC_COMM_WORLD`
53853e0a2f3SToby Isaac 
53953e0a2f3SToby Isaac   Input Parameters:
54053e0a2f3SToby Isaac + PetscLogPLB - A callback that will be executed by `PetscLogEventBegin()` (or `NULL`)
54153e0a2f3SToby Isaac . PetscLogPLE - A callback that will be executed by `PetscLogEventEnd()` (or `NULL`)
54253e0a2f3SToby Isaac . PetscLogPHC - A callback that will be executed by `PetscLogObjectCreate()` (or `NULL`)
54353e0a2f3SToby Isaac - PetscLogPHD - A callback that will be executed by `PetscLogObjectCreate()` (or `NULL`)
54453e0a2f3SToby Isaac 
54553e0a2f3SToby Isaac   Calling sequence of `PetscLogPLB`:
54653e0a2f3SToby Isaac + e  - a `PetscLogEvent` that is beginning
54753e0a2f3SToby Isaac . _i - deprecated, unused
54853e0a2f3SToby Isaac . o1 - a `PetscObject` associated with `e` (or `NULL`)
54953e0a2f3SToby Isaac . o2 - a `PetscObject` associated with `e` (or `NULL`)
55053e0a2f3SToby Isaac . o3 - a `PetscObject` associated with `e` (or `NULL`)
55153e0a2f3SToby Isaac - o4 - a `PetscObject` associated with `e` (or `NULL`)
55253e0a2f3SToby Isaac 
55353e0a2f3SToby Isaac   Calling sequence of `PetscLogPLE`:
55453e0a2f3SToby Isaac + e  - a `PetscLogEvent` that is beginning
55553e0a2f3SToby Isaac . _i - deprecated, unused
55653e0a2f3SToby Isaac . o1 - a `PetscObject` associated with `e` (or `NULL`)
55753e0a2f3SToby Isaac . o2 - a `PetscObject` associated with `e` (or `NULL`)
55853e0a2f3SToby Isaac . o3 - a `PetscObject` associated with `e` (or `NULL`)
55953e0a2f3SToby Isaac - o4 - a `PetscObject` associated with `e` (or `NULL`)
56053e0a2f3SToby Isaac 
56153e0a2f3SToby Isaac   Calling sequence of `PetscLogPHC`:
56253e0a2f3SToby Isaac . o - a `PetscObject` that has just been created
56353e0a2f3SToby Isaac 
56453e0a2f3SToby Isaac   Calling sequence of `PetscLogPHD`:
56553e0a2f3SToby Isaac . o - a `PetscObject` that is about to be destroyed
56653e0a2f3SToby Isaac 
56753e0a2f3SToby Isaac   Level: advanced
56853e0a2f3SToby Isaac 
56953e0a2f3SToby Isaac   Notes:
57053e0a2f3SToby Isaac   This is for transitioning from the deprecated function `PetscLogSet()` and should not be used in new code.
57153e0a2f3SToby Isaac 
57253e0a2f3SToby Isaac   This should help migrate external log handlers to use `PetscLogHandler`, but
57353e0a2f3SToby Isaac   callbacks that depend on the deprecated `PetscLogStage` datatype will have to be
57453e0a2f3SToby Isaac   updated.
57553e0a2f3SToby Isaac 
57653e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogHandlerStart()`, `PetscLogState`
57753e0a2f3SToby Isaac @*/
57853e0a2f3SToby 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))
57953e0a2f3SToby Isaac {
58053e0a2f3SToby Isaac   PetscLogHandler handler;
58153e0a2f3SToby Isaac 
58253e0a2f3SToby Isaac   PetscFunctionBegin;
58353e0a2f3SToby Isaac   PetscCall(PetscLogHandlerCreateLegacy(PETSC_COMM_WORLD, PetscLogPLB, PetscLogPLE, PetscLogPHC, PetscLogPHD, &handler));
58453e0a2f3SToby Isaac   PetscCall(PetscLogHandlerStart(handler));
58553e0a2f3SToby Isaac   PetscCall(PetscLogHandlerDestroy(&handler));
58653e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
58753e0a2f3SToby Isaac }
58853e0a2f3SToby Isaac 
5892611ad71SToby Isaac   #if defined(PETSC_HAVE_MPE)
5902611ad71SToby Isaac     #include <mpe.h>
5912611ad71SToby Isaac static PetscBool PetscBeganMPE = PETSC_FALSE;
5922611ad71SToby Isaac   #endif
5932611ad71SToby Isaac 
5942611ad71SToby Isaac /*@C
5952611ad71SToby Isaac   PetscLogMPEBegin - Turns on MPE logging of events. This creates large log files and slows the
5962611ad71SToby Isaac   program down.
5972611ad71SToby Isaac 
598cc4c1da9SBarry Smith   Collective on `PETSC_COMM_WORLD`, No Fortran Support
5992611ad71SToby Isaac 
6002611ad71SToby Isaac   Options Database Key:
6012611ad71SToby Isaac . -log_mpe - Prints extensive log information
6022611ad71SToby Isaac 
6032611ad71SToby Isaac   Level: advanced
6042611ad71SToby Isaac 
6052611ad71SToby Isaac   Note:
6062611ad71SToby Isaac   A related routine is `PetscLogDefaultBegin()` (with the options key `-log_view`), which is
6072611ad71SToby Isaac   intended for production runs since it logs only flop rates and object creation (and should
6082611ad71SToby Isaac   not significantly slow the programs).
6092611ad71SToby Isaac 
610b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogDump()`, `PetscLogDefaultBegin()`, `PetscLogEventActivate()`,
6112611ad71SToby Isaac           `PetscLogEventDeactivate()`
6122611ad71SToby Isaac @*/
6132611ad71SToby Isaac PetscErrorCode PetscLogMPEBegin(void)
6142611ad71SToby Isaac {
6152611ad71SToby Isaac   PetscFunctionBegin;
6162611ad71SToby Isaac   #if defined(PETSC_HAVE_MPE)
6172611ad71SToby Isaac   /* Do MPE initialization */
6182611ad71SToby Isaac   if (!MPE_Initialized_logging()) { /* This function exists in mpich 1.1.2 and higher */
6192611ad71SToby Isaac     PetscCall(PetscInfo(0, "Initializing MPE.\n"));
6202611ad71SToby Isaac     PetscCall(MPE_Init_log());
6212611ad71SToby Isaac 
6222611ad71SToby Isaac     PetscBeganMPE = PETSC_TRUE;
6232611ad71SToby Isaac   } else {
6242611ad71SToby Isaac     PetscCall(PetscInfo(0, "MPE already initialized. Not attempting to reinitialize.\n"));
6252611ad71SToby Isaac   }
626294de794SToby Isaac   PetscCall(PetscLogTypeBegin(PETSCLOGHANDLERMPE));
6272611ad71SToby Isaac   #else
6282611ad71SToby Isaac   SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP_SYS, "PETSc was configured without MPE support, reconfigure with --with-mpe or --download-mpe");
6292611ad71SToby Isaac   #endif
6302611ad71SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
6312611ad71SToby Isaac }
6322611ad71SToby Isaac 
63353e0a2f3SToby Isaac   #if defined(PETSC_HAVE_TAU_PERFSTUBS)
63453e0a2f3SToby Isaac     #include <../src/sys/perfstubs/timer.h>
63553e0a2f3SToby Isaac   #endif
63653e0a2f3SToby Isaac 
63753e0a2f3SToby Isaac /*@C
63853e0a2f3SToby Isaac   PetscLogPerfstubsBegin - Turns on logging of events using the perfstubs interface.
63953e0a2f3SToby Isaac 
640cc4c1da9SBarry Smith   Collective on `PETSC_COMM_WORLD`, No Fortran Support
64153e0a2f3SToby Isaac 
64253e0a2f3SToby Isaac   Options Database Key:
64353e0a2f3SToby Isaac . -log_perfstubs - use an external log handler through the perfstubs interface
64453e0a2f3SToby Isaac 
64553e0a2f3SToby Isaac   Level: advanced
64653e0a2f3SToby Isaac 
64753e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogDefaultBegin()`, `PetscLogEventActivate()`
64853e0a2f3SToby Isaac @*/
64953e0a2f3SToby Isaac PetscErrorCode PetscLogPerfstubsBegin(void)
65053e0a2f3SToby Isaac {
65153e0a2f3SToby Isaac   PetscFunctionBegin;
65253e0a2f3SToby Isaac   #if defined(PETSC_HAVE_TAU_PERFSTUBS)
653294de794SToby Isaac   PetscCall(PetscLogTypeBegin(PETSCLOGHANDLERPERFSTUBS));
65453e0a2f3SToby Isaac   #else
65553e0a2f3SToby Isaac   SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP_SYS, "PETSc was configured without perfstubs support, reconfigure with --with-tau-perfstubs");
65653e0a2f3SToby Isaac   #endif
65753e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
65853e0a2f3SToby Isaac }
65953e0a2f3SToby Isaac 
6605c6c1daeSBarry Smith /*@
661b665b14eSToby Isaac   PetscLogActions - Determines whether actions are logged for the default log handler.
6625c6c1daeSBarry Smith 
6635c6c1daeSBarry Smith   Not Collective
6645c6c1daeSBarry Smith 
6655c6c1daeSBarry Smith   Input Parameter:
666811af0c4SBarry Smith . flag - `PETSC_TRUE` if actions are to be logged
667811af0c4SBarry Smith 
668811af0c4SBarry Smith   Options Database Key:
669b665b14eSToby Isaac + -log_exclude_actions - (deprecated) Does nothing
670b665b14eSToby Isaac - -log_include_actions - Turn on action logging
6715c6c1daeSBarry Smith 
6725c6c1daeSBarry Smith   Level: intermediate
6735c6c1daeSBarry Smith 
674811af0c4SBarry Smith   Note:
675811af0c4SBarry Smith   Logging of actions continues to consume more memory as the program
6765c6c1daeSBarry Smith   runs. Long running programs should consider turning this feature off.
677aec76313SJacob Faibussowitsch 
678b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogGetDefaultHandler()`
6795c6c1daeSBarry Smith @*/
680d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogActions(PetscBool flag)
681d71ae5a4SJacob Faibussowitsch {
6825c6c1daeSBarry Smith   PetscFunctionBegin;
683dff009beSToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
684dff009beSToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
685dff009beSToby Isaac 
686dff009beSToby Isaac     if (h) PetscCall(PetscLogHandlerSetLogActions(h, flag));
687dff009beSToby Isaac   }
6883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6895c6c1daeSBarry Smith }
6905c6c1daeSBarry Smith 
6915c6c1daeSBarry Smith /*@
6925c6c1daeSBarry Smith   PetscLogObjects - Determines whether objects are logged for the graphical viewer.
6935c6c1daeSBarry Smith 
6945c6c1daeSBarry Smith   Not Collective
6955c6c1daeSBarry Smith 
6965c6c1daeSBarry Smith   Input Parameter:
697811af0c4SBarry Smith . flag - `PETSC_TRUE` if objects are to be logged
698811af0c4SBarry Smith 
699811af0c4SBarry Smith   Options Database Key:
700b665b14eSToby Isaac + -log_exclude_objects - (deprecated) Does nothing
701b665b14eSToby Isaac - -log_include_objects - Turns on object logging
7025c6c1daeSBarry Smith 
7035c6c1daeSBarry Smith   Level: intermediate
7045c6c1daeSBarry Smith 
705811af0c4SBarry Smith   Note:
706811af0c4SBarry Smith   Logging of objects continues to consume more memory as the program
7075c6c1daeSBarry Smith   runs. Long running programs should consider turning this feature off.
7085c6c1daeSBarry Smith 
709b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogGetDefaultHandler()`
7105c6c1daeSBarry Smith @*/
711d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogObjects(PetscBool flag)
712d71ae5a4SJacob Faibussowitsch {
7135c6c1daeSBarry Smith   PetscFunctionBegin;
714dff009beSToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
715dff009beSToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
716dff009beSToby Isaac 
717dff009beSToby Isaac     if (h) PetscCall(PetscLogHandlerSetLogObjects(h, flag));
718dff009beSToby Isaac   }
7193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7205c6c1daeSBarry Smith }
7215c6c1daeSBarry Smith 
7225c6c1daeSBarry Smith /*------------------------------------------------ Stage Functions --------------------------------------------------*/
723cc4c1da9SBarry Smith /*@
72474c0405dSRichard Tran Mills   PetscLogStageRegister - Attaches a character string name to a logging stage.
7255c6c1daeSBarry Smith 
7265c6c1daeSBarry Smith   Not Collective
7275c6c1daeSBarry Smith 
7285c6c1daeSBarry Smith   Input Parameter:
7295c6c1daeSBarry Smith . sname - The name to associate with that stage
7305c6c1daeSBarry Smith 
7315c6c1daeSBarry Smith   Output Parameter:
732b665b14eSToby Isaac . stage - The stage number or -1 if logging is not active (`PetscLogIsActive()`).
7335c6c1daeSBarry Smith 
7345c6c1daeSBarry Smith   Level: intermediate
7355c6c1daeSBarry Smith 
736d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStagePush()`, `PetscLogStagePop()`
7375c6c1daeSBarry Smith @*/
738d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStageRegister(const char sname[], PetscLogStage *stage)
739d71ae5a4SJacob Faibussowitsch {
740b665b14eSToby Isaac   PetscLogState state;
7415c6c1daeSBarry Smith 
7425c6c1daeSBarry Smith   PetscFunctionBegin;
743b665b14eSToby Isaac   *stage = -1;
744b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
745b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateStageRegister(state, sname, stage));
7463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7475c6c1daeSBarry Smith }
7485c6c1daeSBarry Smith 
749cc4c1da9SBarry Smith /*@
750811af0c4SBarry Smith   PetscLogStagePush - This function pushes a stage on the logging stack. Events started and stopped until `PetscLogStagePop()` will be associated with the stage
7515c6c1daeSBarry Smith 
7525c6c1daeSBarry Smith   Not Collective
7535c6c1daeSBarry Smith 
7545c6c1daeSBarry Smith   Input Parameter:
7555c6c1daeSBarry Smith . stage - The stage on which to log
7565c6c1daeSBarry Smith 
75710450e9eSJacob Faibussowitsch   Example Usage:
758811af0c4SBarry Smith   If the option -log_view is used to run the program containing the
7595c6c1daeSBarry Smith   following code, then 2 sets of summary data will be printed during
7605c6c1daeSBarry Smith   PetscFinalize().
7615c6c1daeSBarry Smith .vb
7625c6c1daeSBarry Smith       PetscInitialize(int *argc,char ***args,0,0);
7635c6c1daeSBarry Smith       [stage 0 of code]
7645c6c1daeSBarry Smith       PetscLogStagePush(1);
7655c6c1daeSBarry Smith       [stage 1 of code]
7665c6c1daeSBarry Smith       PetscLogStagePop();
7675c6c1daeSBarry Smith       PetscBarrier(...);
7685c6c1daeSBarry Smith       [more stage 0 of code]
7695c6c1daeSBarry Smith       PetscFinalize();
7705c6c1daeSBarry Smith .ve
7715c6c1daeSBarry Smith 
772d1f92df0SBarry Smith   Level: intermediate
773d1f92df0SBarry Smith 
774811af0c4SBarry Smith   Note:
775811af0c4SBarry Smith   Use `PetscLogStageRegister()` to register a stage.
7765c6c1daeSBarry Smith 
777d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStagePop()`, `PetscLogStageRegister()`, `PetscBarrier()`
7785c6c1daeSBarry Smith @*/
779d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStagePush(PetscLogStage stage)
780d71ae5a4SJacob Faibussowitsch {
781b665b14eSToby Isaac   PetscLogState state;
7825c6c1daeSBarry Smith 
7835c6c1daeSBarry Smith   PetscFunctionBegin;
784b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
785b665b14eSToby Isaac   if (!state) PetscFunctionReturn(PETSC_SUCCESS);
786b665b14eSToby Isaac   for (int i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
787b665b14eSToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
788b665b14eSToby Isaac     if (h) PetscCall(PetscLogHandlerStagePush(h, stage));
789b665b14eSToby Isaac   }
790b665b14eSToby Isaac   PetscCall(PetscLogStateStagePush(state, stage));
7913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7925c6c1daeSBarry Smith }
7935c6c1daeSBarry Smith 
794cc4c1da9SBarry Smith /*@
795811af0c4SBarry Smith   PetscLogStagePop - This function pops a stage from the logging stack that was pushed with `PetscLogStagePush()`
7965c6c1daeSBarry Smith 
7975c6c1daeSBarry Smith   Not Collective
7985c6c1daeSBarry Smith 
79910450e9eSJacob Faibussowitsch   Example Usage:
800811af0c4SBarry Smith   If the option -log_view is used to run the program containing the
8015c6c1daeSBarry Smith   following code, then 2 sets of summary data will be printed during
8025c6c1daeSBarry Smith   PetscFinalize().
8035c6c1daeSBarry Smith .vb
8045c6c1daeSBarry Smith       PetscInitialize(int *argc,char ***args,0,0);
8055c6c1daeSBarry Smith       [stage 0 of code]
8065c6c1daeSBarry Smith       PetscLogStagePush(1);
8075c6c1daeSBarry Smith       [stage 1 of code]
8085c6c1daeSBarry Smith       PetscLogStagePop();
8095c6c1daeSBarry Smith       PetscBarrier(...);
8105c6c1daeSBarry Smith       [more stage 0 of code]
8115c6c1daeSBarry Smith       PetscFinalize();
8125c6c1daeSBarry Smith .ve
8135c6c1daeSBarry Smith 
8145c6c1daeSBarry Smith   Level: intermediate
8155c6c1daeSBarry Smith 
816d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStagePush()`, `PetscLogStageRegister()`, `PetscBarrier()`
8175c6c1daeSBarry Smith @*/
818d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStagePop(void)
819d71ae5a4SJacob Faibussowitsch {
820b665b14eSToby Isaac   PetscLogState state;
821b665b14eSToby Isaac   PetscLogStage current_stage;
8225c6c1daeSBarry Smith 
8235c6c1daeSBarry Smith   PetscFunctionBegin;
824b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
825b665b14eSToby Isaac   if (!state) PetscFunctionReturn(PETSC_SUCCESS);
826b665b14eSToby Isaac   current_stage = state->current_stage;
827b665b14eSToby Isaac   PetscCall(PetscLogStateStagePop(state));
828b665b14eSToby Isaac   for (int i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
829b665b14eSToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
830b665b14eSToby Isaac     if (h) PetscCall(PetscLogHandlerStagePop(h, current_stage));
831b665b14eSToby Isaac   }
8323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8335c6c1daeSBarry Smith }
8345c6c1daeSBarry Smith 
8355c6c1daeSBarry Smith /*@
836811af0c4SBarry Smith   PetscLogStageSetActive - Sets if a stage is used for `PetscLogEventBegin()` and `PetscLogEventEnd()`.
8375c6c1daeSBarry Smith 
8385c6c1daeSBarry Smith   Not Collective
8395c6c1daeSBarry Smith 
8405c6c1daeSBarry Smith   Input Parameters:
8415c6c1daeSBarry Smith + stage    - The stage
842811af0c4SBarry Smith - isActive - The activity flag, `PETSC_TRUE` for logging, else `PETSC_FALSE` (defaults to `PETSC_TRUE`)
8435c6c1daeSBarry Smith 
8445c6c1daeSBarry Smith   Level: intermediate
8455c6c1daeSBarry Smith 
846811af0c4SBarry Smith   Note:
847811af0c4SBarry Smith   If this is set to `PETSC_FALSE` the logging acts as if the stage did not exist
848811af0c4SBarry Smith 
849d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()`
8505c6c1daeSBarry Smith @*/
851d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStageSetActive(PetscLogStage stage, PetscBool isActive)
852d71ae5a4SJacob Faibussowitsch {
853b665b14eSToby Isaac   PetscLogState state;
8545c6c1daeSBarry Smith 
8555c6c1daeSBarry Smith   PetscFunctionBegin;
856b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
857b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateStageSetActive(state, stage, isActive));
8583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8595c6c1daeSBarry Smith }
8605c6c1daeSBarry Smith 
8615c6c1daeSBarry Smith /*@
862811af0c4SBarry Smith   PetscLogStageGetActive - Checks if a stage is used for `PetscLogEventBegin()` and `PetscLogEventEnd()`.
8635c6c1daeSBarry Smith 
8645c6c1daeSBarry Smith   Not Collective
8655c6c1daeSBarry Smith 
8665c6c1daeSBarry Smith   Input Parameter:
8675c6c1daeSBarry Smith . stage - The stage
8685c6c1daeSBarry Smith 
8695c6c1daeSBarry Smith   Output Parameter:
870811af0c4SBarry Smith . isActive - The activity flag, `PETSC_TRUE` for logging, else `PETSC_FALSE` (defaults to `PETSC_TRUE`)
8715c6c1daeSBarry Smith 
8725c6c1daeSBarry Smith   Level: intermediate
8735c6c1daeSBarry Smith 
874d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()`
8755c6c1daeSBarry Smith @*/
876d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStageGetActive(PetscLogStage stage, PetscBool *isActive)
877d71ae5a4SJacob Faibussowitsch {
878b665b14eSToby Isaac   PetscLogState state;
8795c6c1daeSBarry Smith 
8805c6c1daeSBarry Smith   PetscFunctionBegin;
881b665b14eSToby Isaac   *isActive = PETSC_FALSE;
882b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
883b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateStageGetActive(state, stage, isActive));
8843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8855c6c1daeSBarry Smith }
8865c6c1daeSBarry Smith 
8875c6c1daeSBarry Smith /*@
888811af0c4SBarry Smith   PetscLogStageSetVisible - Determines stage visibility in `PetscLogView()`
8895c6c1daeSBarry Smith 
8905c6c1daeSBarry Smith   Not Collective
8915c6c1daeSBarry Smith 
8925c6c1daeSBarry Smith   Input Parameters:
8935c6c1daeSBarry Smith + stage     - The stage
894811af0c4SBarry Smith - isVisible - The visibility flag, `PETSC_TRUE` to print, else `PETSC_FALSE` (defaults to `PETSC_TRUE`)
8955c6c1daeSBarry Smith 
8965c6c1daeSBarry Smith   Level: intermediate
8975c6c1daeSBarry Smith 
898aec76313SJacob Faibussowitsch   Developer Notes:
899b665b14eSToby Isaac   Visibility only affects the default log handler in `PetscLogView()`: stages that are
900b665b14eSToby Isaac   set to invisible are suppressed from output.
901811af0c4SBarry Smith 
902b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogStageGetVisible()`, `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogView()`, `PetscLogGetDefaultHandler()`
9035c6c1daeSBarry Smith @*/
904d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStageSetVisible(PetscLogStage stage, PetscBool isVisible)
9055c6c1daeSBarry Smith 
906dff009beSToby Isaac {
9075c6c1daeSBarry Smith   PetscFunctionBegin;
908dff009beSToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
909dff009beSToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
910dff009beSToby Isaac 
911dff009beSToby Isaac     if (h) PetscCall(PetscLogHandlerStageSetVisible(h, stage, isVisible));
912dff009beSToby Isaac   }
9133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9145c6c1daeSBarry Smith }
9155c6c1daeSBarry Smith 
9165c6c1daeSBarry Smith /*@
917811af0c4SBarry Smith   PetscLogStageGetVisible - Returns stage visibility in `PetscLogView()`
9185c6c1daeSBarry Smith 
9195c6c1daeSBarry Smith   Not Collective
9205c6c1daeSBarry Smith 
9215c6c1daeSBarry Smith   Input Parameter:
9225c6c1daeSBarry Smith . stage - The stage
9235c6c1daeSBarry Smith 
9245c6c1daeSBarry Smith   Output Parameter:
925811af0c4SBarry Smith . isVisible - The visibility flag, `PETSC_TRUE` to print, else `PETSC_FALSE` (defaults to `PETSC_TRUE`)
9265c6c1daeSBarry Smith 
9275c6c1daeSBarry Smith   Level: intermediate
9285c6c1daeSBarry Smith 
929b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogStageSetVisible()`, `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogView()`, `PetscLogGetDefaultHandler()`
9305c6c1daeSBarry Smith @*/
931d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStageGetVisible(PetscLogStage stage, PetscBool *isVisible)
932d71ae5a4SJacob Faibussowitsch {
933b665b14eSToby Isaac   PetscLogHandler handler;
9345c6c1daeSBarry Smith 
9355c6c1daeSBarry Smith   PetscFunctionBegin;
936b665b14eSToby Isaac   *isVisible = PETSC_FALSE;
937294de794SToby Isaac   PetscCall(PetscLogTryGetHandler(PETSCLOGHANDLERDEFAULT, &handler));
938dff009beSToby Isaac   if (handler) { PetscCall(PetscLogHandlerStageGetVisible(handler, stage, isVisible)); }
9393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9405c6c1daeSBarry Smith }
9415c6c1daeSBarry Smith 
942cc4c1da9SBarry Smith /*@
9435c6c1daeSBarry Smith   PetscLogStageGetId - Returns the stage id when given the stage name.
9445c6c1daeSBarry Smith 
9455c6c1daeSBarry Smith   Not Collective
9465c6c1daeSBarry Smith 
9475c6c1daeSBarry Smith   Input Parameter:
9485c6c1daeSBarry Smith . name - The stage name
9495c6c1daeSBarry Smith 
9505c6c1daeSBarry Smith   Output Parameter:
9515a4a3fabSBarry Smith . stage - The stage, , or -1 if no stage with that name exists
9525c6c1daeSBarry Smith 
9535c6c1daeSBarry Smith   Level: intermediate
9545c6c1daeSBarry Smith 
955d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()`
9565c6c1daeSBarry Smith @*/
957d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStageGetId(const char name[], PetscLogStage *stage)
958d71ae5a4SJacob Faibussowitsch {
959b665b14eSToby Isaac   PetscLogState state;
9605c6c1daeSBarry Smith 
9615c6c1daeSBarry Smith   PetscFunctionBegin;
962b665b14eSToby Isaac   *stage = -1;
963b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
964b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateGetStageFromName(state, name, stage));
9653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9665c6c1daeSBarry Smith }
9675c6c1daeSBarry Smith 
968cc4c1da9SBarry Smith /*@
96953e0a2f3SToby Isaac   PetscLogStageGetName - Returns the stage name when given the stage id.
97053e0a2f3SToby Isaac 
97153e0a2f3SToby Isaac   Not Collective
97253e0a2f3SToby Isaac 
97353e0a2f3SToby Isaac   Input Parameter:
97453e0a2f3SToby Isaac . stage - The stage
97553e0a2f3SToby Isaac 
97653e0a2f3SToby Isaac   Output Parameter:
97753e0a2f3SToby Isaac . name - The stage name
97853e0a2f3SToby Isaac 
97953e0a2f3SToby Isaac   Level: intermediate
98053e0a2f3SToby Isaac 
98153e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()`
98253e0a2f3SToby Isaac @*/
983cc4c1da9SBarry Smith PetscErrorCode PetscLogStageGetName(PetscLogStage stage, const char *name[])
98453e0a2f3SToby Isaac {
98553e0a2f3SToby Isaac   PetscLogStageInfo stage_info;
98653e0a2f3SToby Isaac   PetscLogState     state;
98753e0a2f3SToby Isaac 
98853e0a2f3SToby Isaac   PetscFunctionBegin;
989b665b14eSToby Isaac   *name = NULL;
99053e0a2f3SToby Isaac   PetscCall(PetscLogGetState(&state));
991b665b14eSToby Isaac   if (!state) PetscFunctionReturn(PETSC_SUCCESS);
99253e0a2f3SToby Isaac   PetscCall(PetscLogStateStageGetInfo(state, stage, &stage_info));
99353e0a2f3SToby Isaac   *name = stage_info.name;
99453e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
99553e0a2f3SToby Isaac }
99653e0a2f3SToby Isaac 
9975c6c1daeSBarry Smith /*------------------------------------------------ Event Functions --------------------------------------------------*/
9987a101e5eSJacob Faibussowitsch 
999cc4c1da9SBarry Smith /*@
1000811af0c4SBarry Smith   PetscLogEventRegister - Registers an event name for logging operations
10015c6c1daeSBarry Smith 
10025c6c1daeSBarry Smith   Not Collective
10035c6c1daeSBarry Smith 
1004d8d19677SJose E. Roman   Input Parameters:
10055c6c1daeSBarry Smith + name    - The name associated with the event
10065c6c1daeSBarry Smith - classid - The classid associated to the class for this event, obtain either with
1007811af0c4SBarry Smith            `PetscClassIdRegister()` or use a predefined one such as `KSP_CLASSID`, `SNES_CLASSID`, the predefined ones
10085c6c1daeSBarry Smith            are only available in C code
10095c6c1daeSBarry Smith 
10105c6c1daeSBarry Smith   Output Parameter:
1011811af0c4SBarry Smith . event - The event id for use with `PetscLogEventBegin()` and `PetscLogEventEnd()`.
10125c6c1daeSBarry Smith 
101310450e9eSJacob Faibussowitsch   Example Usage:
10145c6c1daeSBarry Smith .vb
10155c6c1daeSBarry Smith       PetscLogEvent USER_EVENT;
10165c6c1daeSBarry Smith       PetscClassId classid;
10175c6c1daeSBarry Smith       PetscLogDouble user_event_flops;
10185c6c1daeSBarry Smith       PetscClassIdRegister("class name",&classid);
10195c6c1daeSBarry Smith       PetscLogEventRegister("User event name",classid,&USER_EVENT);
10205c6c1daeSBarry Smith       PetscLogEventBegin(USER_EVENT,0,0,0,0);
10215c6c1daeSBarry Smith          [code segment to monitor]
10225c6c1daeSBarry Smith          PetscLogFlops(user_event_flops);
10235c6c1daeSBarry Smith       PetscLogEventEnd(USER_EVENT,0,0,0,0);
10245c6c1daeSBarry Smith .ve
10255c6c1daeSBarry Smith 
1026d1f92df0SBarry Smith   Level: intermediate
1027d1f92df0SBarry Smith 
10285c6c1daeSBarry Smith   Notes:
10295c6c1daeSBarry Smith   PETSc automatically logs library events if the code has been
1030a2553e36SBarry Smith   configured with --with-log (which is the default) and
1031811af0c4SBarry Smith   -log_view or -log_all is specified.  `PetscLogEventRegister()` is
10325c6c1daeSBarry Smith   intended for logging user events to supplement this PETSc
10335c6c1daeSBarry Smith   information.
10345c6c1daeSBarry Smith 
1035495fc317SBarry Smith   PETSc can gather data for use with the utilities Jumpshot
10365c6c1daeSBarry Smith   (part of the MPICH distribution).  If PETSc has been compiled
10375c6c1daeSBarry Smith   with flag -DPETSC_HAVE_MPE (MPE is an additional utility within
10385c6c1daeSBarry Smith   MPICH), the user can employ another command line option, -log_mpe,
10395c6c1daeSBarry Smith   to create a logfile, "mpe.log", which can be visualized
1040495fc317SBarry Smith   Jumpshot.
10415c6c1daeSBarry Smith 
10425c6c1daeSBarry Smith   The classid is associated with each event so that classes of events
10435c6c1daeSBarry Smith   can be disabled simultaneously, such as all matrix events. The user
1044811af0c4SBarry Smith   can either use an existing classid, such as `MAT_CLASSID`, or create
10455c6c1daeSBarry Smith   their own as shown in the example.
10465c6c1daeSBarry Smith 
1047c5deb1d5SJed Brown   If an existing event with the same name exists, its event handle is
1048c5deb1d5SJed Brown   returned instead of creating a new event.
1049c5deb1d5SJed Brown 
1050d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStageRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogFlops()`,
1051db781477SPatrick Sanan           `PetscLogEventActivate()`, `PetscLogEventDeactivate()`, `PetscClassIdRegister()`
10525c6c1daeSBarry Smith @*/
1053d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventRegister(const char name[], PetscClassId classid, PetscLogEvent *event)
1054d71ae5a4SJacob Faibussowitsch {
1055b665b14eSToby Isaac   PetscLogState state;
10565c6c1daeSBarry Smith 
10575c6c1daeSBarry Smith   PetscFunctionBegin;
1058b665b14eSToby Isaac   *event = -1;
1059b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
1060b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateEventRegister(state, name, classid, event));
10613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
10625c6c1daeSBarry Smith }
10635c6c1daeSBarry Smith 
10645c6c1daeSBarry Smith /*@
1065217044c2SLisandro Dalcin   PetscLogEventSetCollective - Indicates that a particular event is collective.
1066217044c2SLisandro Dalcin 
10675aefd447SStefano Zampini   Logically Collective
1068217044c2SLisandro Dalcin 
1069d8d19677SJose E. Roman   Input Parameters:
1070217044c2SLisandro Dalcin + event      - The event id
1071*ffbd2f08SBarry Smith - collective - `PetscBool` indicating whether a particular event is collective
1072217044c2SLisandro Dalcin 
1073d1f92df0SBarry Smith   Level: developer
1074d1f92df0SBarry Smith 
1075811af0c4SBarry Smith   Notes:
1076811af0c4SBarry Smith   New events returned from `PetscLogEventRegister()` are collective by default.
1077811af0c4SBarry Smith 
1078*ffbd2f08SBarry Smith   Collective events are handled specially if the command line option `-log_sync` is used. In that case the logging saves information about
1079811af0c4SBarry Smith   two parts of the event; the time for all the MPI ranks to synchronize and then the time for the actual computation/communication
10805aefd447SStefano Zampini   to be performed. This option is useful to debug imbalance within the computations or communications.
1081217044c2SLisandro Dalcin 
1082d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogEventRegister()`
1083217044c2SLisandro Dalcin @*/
1084d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventSetCollective(PetscLogEvent event, PetscBool collective)
1085d71ae5a4SJacob Faibussowitsch {
1086b665b14eSToby Isaac   PetscLogState state;
1087217044c2SLisandro Dalcin 
1088217044c2SLisandro Dalcin   PetscFunctionBegin;
1089b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
1090b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateEventSetCollective(state, event, collective));
1091b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
1092b665b14eSToby Isaac }
1093b665b14eSToby Isaac 
1094b665b14eSToby Isaac /*
1095b665b14eSToby Isaac   PetscLogClassSetActiveAll - Activate or inactivate logging for all events associated with a PETSc object class in every stage.
1096b665b14eSToby Isaac 
1097b665b14eSToby Isaac   Not Collective
1098b665b14eSToby Isaac 
1099b665b14eSToby Isaac   Input Parameters:
1100b665b14eSToby Isaac + classid - The object class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc.
1101b665b14eSToby Isaac - isActive - if `PETSC_FALSE`, events associated with this class will not be send to log handlers.
1102b665b14eSToby Isaac 
1103b665b14eSToby Isaac   Level: developer
1104b665b14eSToby Isaac 
1105b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogEventActivate()`, `PetscLogEventActivateAll()`, `PetscLogStageSetActive()`, `PetscLogEventActivateClass()`
1106b665b14eSToby Isaac */
1107b665b14eSToby Isaac static PetscErrorCode PetscLogClassSetActiveAll(PetscClassId classid, PetscBool isActive)
1108b665b14eSToby Isaac {
1109b665b14eSToby Isaac   PetscLogState state;
1110b665b14eSToby Isaac 
1111b665b14eSToby Isaac   PetscFunctionBegin;
1112b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
1113b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateClassSetActiveAll(state, classid, isActive));
11143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1115217044c2SLisandro Dalcin }
1116217044c2SLisandro Dalcin 
1117217044c2SLisandro Dalcin /*@
1118fa2bb9feSLisandro Dalcin   PetscLogEventIncludeClass - Activates event logging for a PETSc object class in every stage.
1119fa2bb9feSLisandro Dalcin 
1120fa2bb9feSLisandro Dalcin   Not Collective
1121fa2bb9feSLisandro Dalcin 
1122fa2bb9feSLisandro Dalcin   Input Parameter:
1123811af0c4SBarry Smith . classid - The object class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc.
1124fa2bb9feSLisandro Dalcin 
1125fa2bb9feSLisandro Dalcin   Level: developer
1126fa2bb9feSLisandro Dalcin 
1127d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventActivateClass()`, `PetscLogEventDeactivateClass()`, `PetscLogEventActivate()`, `PetscLogEventDeactivate()`
1128fa2bb9feSLisandro Dalcin @*/
1129d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventIncludeClass(PetscClassId classid)
1130d71ae5a4SJacob Faibussowitsch {
1131fa2bb9feSLisandro Dalcin   PetscFunctionBegin;
1132b665b14eSToby Isaac   PetscCall(PetscLogClassSetActiveAll(classid, PETSC_TRUE));
11333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1134fa2bb9feSLisandro Dalcin }
1135fa2bb9feSLisandro Dalcin 
1136fa2bb9feSLisandro Dalcin /*@
1137fa2bb9feSLisandro Dalcin   PetscLogEventExcludeClass - Deactivates event logging for a PETSc object class in every stage.
1138fa2bb9feSLisandro Dalcin 
1139fa2bb9feSLisandro Dalcin   Not Collective
1140fa2bb9feSLisandro Dalcin 
1141fa2bb9feSLisandro Dalcin   Input Parameter:
1142811af0c4SBarry Smith . classid - The object class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc.
1143fa2bb9feSLisandro Dalcin 
1144fa2bb9feSLisandro Dalcin   Level: developer
1145fa2bb9feSLisandro Dalcin 
1146811af0c4SBarry Smith   Note:
1147811af0c4SBarry Smith   If a class is excluded then events associated with that class are not logged.
1148811af0c4SBarry Smith 
1149d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventDeactivateClass()`, `PetscLogEventActivateClass()`, `PetscLogEventDeactivate()`, `PetscLogEventActivate()`
1150fa2bb9feSLisandro Dalcin @*/
1151d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventExcludeClass(PetscClassId classid)
1152d71ae5a4SJacob Faibussowitsch {
1153b665b14eSToby Isaac   PetscFunctionBegin;
1154b665b14eSToby Isaac   PetscCall(PetscLogClassSetActiveAll(classid, PETSC_FALSE));
1155b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
1156b665b14eSToby Isaac }
1157b665b14eSToby Isaac 
1158b665b14eSToby Isaac /*
1159b665b14eSToby Isaac   PetscLogEventSetActive - Activate or inactivate logging for an event in a given stage
1160b665b14eSToby Isaac 
1161b665b14eSToby Isaac   Not Collective
1162b665b14eSToby Isaac 
1163b665b14eSToby Isaac   Input Parameters:
1164b665b14eSToby Isaac + stage - A registered `PetscLogStage` (or `PETSC_DEFAULT` for the current stage)
1165b665b14eSToby Isaac . event - A `PetscLogEvent`
1166b665b14eSToby Isaac - isActive - If `PETSC_FALSE`, activity from this event (`PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogEventSync()`) will not be sent to log handlers during this stage
1167b665b14eSToby Isaac 
1168b665b14eSToby Isaac   Usage:
1169b665b14eSToby Isaac .vb
1170b665b14eSToby Isaac       PetscLogEventSetActive(VEC_SetValues, PETSC_FALSE);
1171b665b14eSToby Isaac         [code where you do not want to log VecSetValues()]
1172b665b14eSToby Isaac       PetscLogEventSetActive(VEC_SetValues, PETSC_TRUE);
1173b665b14eSToby Isaac         [code where you do want to log VecSetValues()]
1174b665b14eSToby Isaac .ve
1175b665b14eSToby Isaac 
1176b665b14eSToby Isaac   Level: advanced
1177b665b14eSToby Isaac 
1178b665b14eSToby Isaac   Note:
1179b665b14eSToby Isaac   The event may be either a pre-defined PETSc event (found in include/petsclog.h)
1180b665b14eSToby Isaac   or an event number obtained with `PetscLogEventRegister()`.
1181b665b14eSToby Isaac 
1182b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogEventDeactivatePush()`, `PetscLogEventDeactivatePop()`
1183b665b14eSToby Isaac */
1184b665b14eSToby Isaac static PetscErrorCode PetscLogEventSetActive(PetscLogStage stage, PetscLogEvent event, PetscBool isActive)
1185b665b14eSToby Isaac {
1186b665b14eSToby Isaac   PetscLogState state;
1187fa2bb9feSLisandro Dalcin 
1188fa2bb9feSLisandro Dalcin   PetscFunctionBegin;
1189b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
1190b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateEventSetActive(state, stage, event, isActive));
11913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1192fa2bb9feSLisandro Dalcin }
1193fa2bb9feSLisandro Dalcin 
1194fa2bb9feSLisandro Dalcin /*@
11955c6c1daeSBarry Smith   PetscLogEventActivate - Indicates that a particular event should be logged.
11965c6c1daeSBarry Smith 
11975c6c1daeSBarry Smith   Not Collective
11985c6c1daeSBarry Smith 
11995c6c1daeSBarry Smith   Input Parameter:
12005c6c1daeSBarry Smith . event - The event id
12015c6c1daeSBarry Smith 
120210450e9eSJacob Faibussowitsch   Example Usage:
12035c6c1daeSBarry Smith .vb
12045c6c1daeSBarry Smith       PetscLogEventDeactivate(VEC_SetValues);
12055c6c1daeSBarry Smith         [code where you do not want to log VecSetValues()]
12065c6c1daeSBarry Smith       PetscLogEventActivate(VEC_SetValues);
12075c6c1daeSBarry Smith         [code where you do want to log VecSetValues()]
12085c6c1daeSBarry Smith .ve
12095c6c1daeSBarry Smith 
1210d1f92df0SBarry Smith   Level: advanced
1211d1f92df0SBarry Smith 
12125c6c1daeSBarry Smith   Note:
12135c6c1daeSBarry Smith   The event may be either a pre-defined PETSc event (found in include/petsclog.h)
1214811af0c4SBarry Smith   or an event number obtained with `PetscLogEventRegister()`.
12155c6c1daeSBarry Smith 
1216b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogEventDeactivate()`, `PetscLogEventDeactivatePush()`, `PetscLogEventDeactivatePop()`
12175c6c1daeSBarry Smith @*/
1218d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventActivate(PetscLogEvent event)
1219d71ae5a4SJacob Faibussowitsch {
12205c6c1daeSBarry Smith   PetscFunctionBegin;
1221b665b14eSToby Isaac   PetscCall(PetscLogEventSetActive(PETSC_DEFAULT, event, PETSC_TRUE));
12223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12235c6c1daeSBarry Smith }
12245c6c1daeSBarry Smith 
12255c6c1daeSBarry Smith /*@
12265c6c1daeSBarry Smith   PetscLogEventDeactivate - Indicates that a particular event should not be logged.
12275c6c1daeSBarry Smith 
12285c6c1daeSBarry Smith   Not Collective
12295c6c1daeSBarry Smith 
12305c6c1daeSBarry Smith   Input Parameter:
12315c6c1daeSBarry Smith . event - The event id
12325c6c1daeSBarry Smith 
123310450e9eSJacob Faibussowitsch   Example Usage:
12345c6c1daeSBarry Smith .vb
12355c6c1daeSBarry Smith       PetscLogEventDeactivate(VEC_SetValues);
12365c6c1daeSBarry Smith         [code where you do not want to log VecSetValues()]
12375c6c1daeSBarry Smith       PetscLogEventActivate(VEC_SetValues);
12385c6c1daeSBarry Smith         [code where you do want to log VecSetValues()]
12395c6c1daeSBarry Smith .ve
12405c6c1daeSBarry Smith 
1241d1f92df0SBarry Smith   Level: advanced
1242d1f92df0SBarry Smith 
12435c6c1daeSBarry Smith   Note:
12445c6c1daeSBarry Smith   The event may be either a pre-defined PETSc event (found in
1245811af0c4SBarry Smith   include/petsclog.h) or an event number obtained with `PetscLogEventRegister()`).
12465c6c1daeSBarry Smith 
1247d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventActivate()`, `PetscLogEventDeactivatePush()`, `PetscLogEventDeactivatePop()`
12485c6c1daeSBarry Smith @*/
1249d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventDeactivate(PetscLogEvent event)
1250d71ae5a4SJacob Faibussowitsch {
12515c6c1daeSBarry Smith   PetscFunctionBegin;
1252b665b14eSToby Isaac   PetscCall(PetscLogEventSetActive(PETSC_DEFAULT, event, PETSC_FALSE));
12533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12545c6c1daeSBarry Smith }
12555c6c1daeSBarry Smith 
12565c6c1daeSBarry Smith /*@
1257811af0c4SBarry Smith   PetscLogEventDeactivatePush - Indicates that a particular event should not be logged until `PetscLogEventDeactivatePop()` is called
1258c00cb57fSBarry Smith 
1259c00cb57fSBarry Smith   Not Collective
1260c00cb57fSBarry Smith 
1261c00cb57fSBarry Smith   Input Parameter:
1262c00cb57fSBarry Smith . event - The event id
1263c00cb57fSBarry Smith 
126410450e9eSJacob Faibussowitsch   Example Usage:
1265c00cb57fSBarry Smith .vb
1266c00cb57fSBarry Smith       PetscLogEventDeactivatePush(VEC_SetValues);
1267c00cb57fSBarry Smith         [code where you do not want to log VecSetValues()]
1268c00cb57fSBarry Smith       PetscLogEventDeactivatePop(VEC_SetValues);
1269c00cb57fSBarry Smith         [code where you do want to log VecSetValues()]
1270c00cb57fSBarry Smith .ve
1271c00cb57fSBarry Smith 
1272d1f92df0SBarry Smith   Level: advanced
1273d1f92df0SBarry Smith 
1274c00cb57fSBarry Smith   Note:
1275c00cb57fSBarry Smith   The event may be either a pre-defined PETSc event (found in
1276811af0c4SBarry Smith   include/petsclog.h) or an event number obtained with `PetscLogEventRegister()`).
1277c00cb57fSBarry Smith 
1278baca6076SPierre Jolivet   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 suppressing events is not helpful in their output formats.
1279b665b14eSToby Isaac 
12804b7c4d4dSPierre Jolivet .seealso: [](ch_profiling), `PetscLogEventActivate()`, `PetscLogEventDeactivate()`, `PetscLogEventDeactivatePop()`
1281c00cb57fSBarry Smith @*/
1282d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventDeactivatePush(PetscLogEvent event)
1283d71ae5a4SJacob Faibussowitsch {
1284c00cb57fSBarry Smith   PetscFunctionBegin;
1285dff009beSToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
1286dff009beSToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
1287dff009beSToby Isaac 
1288dff009beSToby Isaac     if (h) PetscCall(PetscLogHandlerEventDeactivatePush(h, PETSC_DEFAULT, event));
1289dff009beSToby Isaac   }
12903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1291c00cb57fSBarry Smith }
1292c00cb57fSBarry Smith 
1293c00cb57fSBarry Smith /*@
1294811af0c4SBarry Smith   PetscLogEventDeactivatePop - Indicates that a particular event should again be logged after the logging was turned off with `PetscLogEventDeactivatePush()`
1295c00cb57fSBarry Smith 
1296c00cb57fSBarry Smith   Not Collective
1297c00cb57fSBarry Smith 
1298c00cb57fSBarry Smith   Input Parameter:
1299c00cb57fSBarry Smith . event - The event id
1300c00cb57fSBarry Smith 
130110450e9eSJacob Faibussowitsch   Example Usage:
1302c00cb57fSBarry Smith .vb
1303c00cb57fSBarry Smith       PetscLogEventDeactivatePush(VEC_SetValues);
1304c00cb57fSBarry Smith         [code where you do not want to log VecSetValues()]
1305c00cb57fSBarry Smith       PetscLogEventDeactivatePop(VEC_SetValues);
1306c00cb57fSBarry Smith         [code where you do want to log VecSetValues()]
1307c00cb57fSBarry Smith .ve
1308c00cb57fSBarry Smith 
1309d1f92df0SBarry Smith   Level: advanced
1310d1f92df0SBarry Smith 
1311c00cb57fSBarry Smith   Note:
1312c00cb57fSBarry Smith   The event may be either a pre-defined PETSc event (found in
1313811af0c4SBarry Smith   include/petsclog.h) or an event number obtained with `PetscLogEventRegister()`).
1314c00cb57fSBarry Smith 
1315d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventActivate()`, `PetscLogEventDeactivatePush()`
1316c00cb57fSBarry Smith @*/
1317d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventDeactivatePop(PetscLogEvent event)
1318d71ae5a4SJacob Faibussowitsch {
1319c00cb57fSBarry Smith   PetscFunctionBegin;
1320dff009beSToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
1321dff009beSToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
1322dff009beSToby Isaac 
1323dff009beSToby Isaac     if (h) PetscCall(PetscLogHandlerEventDeactivatePop(h, PETSC_DEFAULT, event));
1324dff009beSToby Isaac   }
13253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1326c00cb57fSBarry Smith }
1327c00cb57fSBarry Smith 
1328c00cb57fSBarry Smith /*@
1329811af0c4SBarry Smith   PetscLogEventSetActiveAll - Turns on logging of all events
13305c6c1daeSBarry Smith 
13315c6c1daeSBarry Smith   Not Collective
13325c6c1daeSBarry Smith 
13335c6c1daeSBarry Smith   Input Parameters:
13345c6c1daeSBarry Smith + event    - The event id
13355c6c1daeSBarry Smith - isActive - The activity flag determining whether the event is logged
13365c6c1daeSBarry Smith 
13375c6c1daeSBarry Smith   Level: advanced
13385c6c1daeSBarry Smith 
1339b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogEventActivate()`, `PetscLogEventDeactivate()`
13405c6c1daeSBarry Smith @*/
1341d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventSetActiveAll(PetscLogEvent event, PetscBool isActive)
1342d71ae5a4SJacob Faibussowitsch {
1343b665b14eSToby Isaac   PetscLogState state;
13445c6c1daeSBarry Smith 
13455c6c1daeSBarry Smith   PetscFunctionBegin;
1346b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
1347b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateEventSetActiveAll(state, event, isActive));
1348b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
13495c6c1daeSBarry Smith }
1350b665b14eSToby Isaac 
1351b665b14eSToby Isaac /*
1352b665b14eSToby Isaac   PetscLogClassSetActive - Activates event logging for a PETSc object class for the current stage
1353b665b14eSToby Isaac 
1354b665b14eSToby Isaac   Not Collective
1355b665b14eSToby Isaac 
1356b665b14eSToby Isaac   Input Parameters:
1357b665b14eSToby Isaac + stage - A registered `PetscLogStage` (or `PETSC_DEFAULT` for the current stage)
1358b665b14eSToby Isaac . classid - The event class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc.
1359b665b14eSToby Isaac - isActive - If `PETSC_FALSE`, events associated with this class are not sent to log handlers.
1360b665b14eSToby Isaac 
1361b665b14eSToby Isaac   Level: developer
1362b665b14eSToby Isaac 
1363b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogEventIncludeClass()`, `PetscLogEventActivate()`, `PetscLogEventActivateAll()`, `PetscLogStageSetActive()`
1364b665b14eSToby Isaac */
1365b665b14eSToby Isaac static PetscErrorCode PetscLogClassSetActive(PetscLogStage stage, PetscClassId classid, PetscBool isActive)
1366b665b14eSToby Isaac {
1367b665b14eSToby Isaac   PetscLogState state;
1368b665b14eSToby Isaac 
1369b665b14eSToby Isaac   PetscFunctionBegin;
1370b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
1371b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateClassSetActive(state, stage, classid, isActive));
13723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13735c6c1daeSBarry Smith }
13745c6c1daeSBarry Smith 
13755c6c1daeSBarry Smith /*@
1376811af0c4SBarry Smith   PetscLogEventActivateClass - Activates event logging for a PETSc object class for the current stage
13775c6c1daeSBarry Smith 
13785c6c1daeSBarry Smith   Not Collective
13795c6c1daeSBarry Smith 
13805c6c1daeSBarry Smith   Input Parameter:
1381811af0c4SBarry Smith . classid - The event class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc.
13825c6c1daeSBarry Smith 
13835c6c1daeSBarry Smith   Level: developer
13845c6c1daeSBarry Smith 
1385d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventIncludeClass()`, `PetscLogEventExcludeClass()`, `PetscLogEventDeactivateClass()`, `PetscLogEventActivate()`, `PetscLogEventDeactivate()`
13865c6c1daeSBarry Smith @*/
1387d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventActivateClass(PetscClassId classid)
1388d71ae5a4SJacob Faibussowitsch {
13895c6c1daeSBarry Smith   PetscFunctionBegin;
1390b665b14eSToby Isaac   PetscCall(PetscLogClassSetActive(PETSC_DEFAULT, classid, PETSC_TRUE));
13913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13925c6c1daeSBarry Smith }
13935c6c1daeSBarry Smith 
13945c6c1daeSBarry Smith /*@
1395811af0c4SBarry Smith   PetscLogEventDeactivateClass - Deactivates event logging for a PETSc object class for the current stage
13965c6c1daeSBarry Smith 
13975c6c1daeSBarry Smith   Not Collective
13985c6c1daeSBarry Smith 
13995c6c1daeSBarry Smith   Input Parameter:
1400811af0c4SBarry Smith . classid - The event class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc.
14015c6c1daeSBarry Smith 
14025c6c1daeSBarry Smith   Level: developer
14035c6c1daeSBarry Smith 
1404d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventIncludeClass()`, `PetscLogEventExcludeClass()`, `PetscLogEventActivateClass()`, `PetscLogEventActivate()`, `PetscLogEventDeactivate()`
14055c6c1daeSBarry Smith @*/
1406d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventDeactivateClass(PetscClassId classid)
1407d71ae5a4SJacob Faibussowitsch {
14085c6c1daeSBarry Smith   PetscFunctionBegin;
1409b665b14eSToby Isaac   PetscCall(PetscLogClassSetActive(PETSC_DEFAULT, classid, PETSC_FALSE));
14103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
14115c6c1daeSBarry Smith }
14125c6c1daeSBarry Smith 
14135c6c1daeSBarry Smith /*MC
141462872c28SLisandro Dalcin   PetscLogEventSync - Synchronizes the beginning of a user event.
141562872c28SLisandro Dalcin 
141662872c28SLisandro Dalcin   Synopsis:
141762872c28SLisandro Dalcin   #include <petsclog.h>
1418b665b14eSToby Isaac   PetscErrorCode PetscLogEventSync(PetscLogEvent e, MPI_Comm comm)
141962872c28SLisandro Dalcin 
142062872c28SLisandro Dalcin   Collective
142162872c28SLisandro Dalcin 
142262872c28SLisandro Dalcin   Input Parameters:
1423b665b14eSToby Isaac + e    - `PetscLogEvent` obtained from `PetscLogEventRegister()`
142462872c28SLisandro Dalcin - comm - an MPI communicator
142562872c28SLisandro Dalcin 
142610450e9eSJacob Faibussowitsch   Example Usage:
142762872c28SLisandro Dalcin .vb
142862872c28SLisandro Dalcin   PetscLogEvent USER_EVENT;
142910450e9eSJacob Faibussowitsch 
143062872c28SLisandro Dalcin   PetscLogEventRegister("User event", 0, &USER_EVENT);
143162872c28SLisandro Dalcin   PetscLogEventSync(USER_EVENT, PETSC_COMM_WORLD);
143262872c28SLisandro Dalcin   PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0);
143362872c28SLisandro Dalcin   [code segment to monitor]
143462872c28SLisandro Dalcin   PetscLogEventEnd(USER_EVENT, 0, 0, 0 , 0);
143562872c28SLisandro Dalcin .ve
143662872c28SLisandro Dalcin 
1437d1f92df0SBarry Smith   Level: developer
1438d1f92df0SBarry Smith 
1439811af0c4SBarry Smith   Note:
144010450e9eSJacob Faibussowitsch   This routine should be called only if there is not a `PetscObject` available to pass to
144110450e9eSJacob Faibussowitsch   `PetscLogEventBegin()`.
144262872c28SLisandro Dalcin 
1443d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`
144462872c28SLisandro Dalcin M*/
144562872c28SLisandro Dalcin 
144662872c28SLisandro Dalcin /*MC
14475c6c1daeSBarry Smith   PetscLogEventBegin - Logs the beginning of a user event.
14485c6c1daeSBarry Smith 
14495c6c1daeSBarry Smith   Synopsis:
1450aaa7dc30SBarry Smith   #include <petsclog.h>
1451b665b14eSToby Isaac   PetscErrorCode PetscLogEventBegin(PetscLogEvent e, PetscObject o1, PetscObject o2, PetscObject o3, PetscObject o4)
14525c6c1daeSBarry Smith 
14535c6c1daeSBarry Smith   Not Collective
14545c6c1daeSBarry Smith 
14555c6c1daeSBarry Smith   Input Parameters:
1456b665b14eSToby Isaac + e  - `PetscLogEvent` obtained from `PetscLogEventRegister()`
1457baca6076SPierre Jolivet . o1 - object associated with the event, or `NULL`
1458baca6076SPierre Jolivet . o2 - object associated with the event, or `NULL`
1459baca6076SPierre Jolivet . o3 - object associated with the event, or `NULL`
1460baca6076SPierre Jolivet - o4 - object associated with the event, or `NULL`
14615c6c1daeSBarry Smith 
14625c6c1daeSBarry Smith   Fortran Synopsis:
14635c6c1daeSBarry Smith   void PetscLogEventBegin(int e, PetscErrorCode ierr)
14645c6c1daeSBarry Smith 
146510450e9eSJacob Faibussowitsch   Example Usage:
14665c6c1daeSBarry Smith .vb
14675c6c1daeSBarry Smith   PetscLogEvent USER_EVENT;
146810450e9eSJacob Faibussowitsch 
14695c6c1daeSBarry Smith   PetscLogDouble user_event_flops;
14705c6c1daeSBarry Smith   PetscLogEventRegister("User event",0, &USER_EVENT);
14715c6c1daeSBarry Smith   PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0);
14725c6c1daeSBarry Smith   [code segment to monitor]
14735c6c1daeSBarry Smith   PetscLogFlops(user_event_flops);
14745c6c1daeSBarry Smith   PetscLogEventEnd(USER_EVENT, 0, 0, 0, 0);
14755c6c1daeSBarry Smith .ve
14765c6c1daeSBarry Smith 
1477d1f92df0SBarry Smith   Level: intermediate
1478d1f92df0SBarry Smith 
1479811af0c4SBarry Smith   Developer Note:
148010450e9eSJacob Faibussowitsch   `PetscLogEventBegin()` and `PetscLogEventBegin()` return error codes instead of explicitly
148110450e9eSJacob Faibussowitsch   handling the errors that occur in the macro directly because other packages that use this
148210450e9eSJacob Faibussowitsch   macros have used them in their own functions or methods that do not return error codes and it
148310450e9eSJacob Faibussowitsch   would be disruptive to change the current behavior.
1484d0609cedSBarry Smith 
1485d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventEnd()`, `PetscLogFlops()`
14865c6c1daeSBarry Smith M*/
14875c6c1daeSBarry Smith 
14885c6c1daeSBarry Smith /*MC
14895c6c1daeSBarry Smith   PetscLogEventEnd - Log the end of a user event.
14905c6c1daeSBarry Smith 
14915c6c1daeSBarry Smith   Synopsis:
1492aaa7dc30SBarry Smith   #include <petsclog.h>
1493b665b14eSToby Isaac   PetscErrorCode PetscLogEventEnd(PetscLogEvent e, PetscObject o1, PetscObject o2, PetscObject o3, PetscObject o4)
14945c6c1daeSBarry Smith 
14955c6c1daeSBarry Smith   Not Collective
14965c6c1daeSBarry Smith 
14975c6c1daeSBarry Smith   Input Parameters:
1498b665b14eSToby Isaac + e  - `PetscLogEvent` obtained from `PetscLogEventRegister()`
1499baca6076SPierre Jolivet . o1 - object associated with the event, or `NULL`
1500baca6076SPierre Jolivet . o2 - object associated with the event, or `NULL`
1501baca6076SPierre Jolivet . o3 - object associated with the event, or `NULL`
1502baca6076SPierre Jolivet - o4 - object associated with the event, or `NULL`
15035c6c1daeSBarry Smith 
15045c6c1daeSBarry Smith   Fortran Synopsis:
15055c6c1daeSBarry Smith   void PetscLogEventEnd(int e, PetscErrorCode ierr)
15065c6c1daeSBarry Smith 
150710450e9eSJacob Faibussowitsch   Example Usage:
15085c6c1daeSBarry Smith .vb
15095c6c1daeSBarry Smith   PetscLogEvent USER_EVENT;
151010450e9eSJacob Faibussowitsch 
15115c6c1daeSBarry Smith   PetscLogDouble user_event_flops;
151210450e9eSJacob Faibussowitsch   PetscLogEventRegister("User event", 0, &USER_EVENT);
15135c6c1daeSBarry Smith   PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0);
15145c6c1daeSBarry Smith   [code segment to monitor]
15155c6c1daeSBarry Smith   PetscLogFlops(user_event_flops);
15165c6c1daeSBarry Smith   PetscLogEventEnd(USER_EVENT, 0, 0, 0, 0);
15175c6c1daeSBarry Smith .ve
15185c6c1daeSBarry Smith 
15195c6c1daeSBarry Smith   Level: intermediate
15205c6c1daeSBarry Smith 
1521d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogFlops()`
15225c6c1daeSBarry Smith M*/
15235c6c1daeSBarry Smith 
15245c6c1daeSBarry Smith /*@C
15258b08f494SToby Isaac   PetscLogStageGetPerfInfo - Return the performance information about the given stage
15268b08f494SToby Isaac 
1527cc4c1da9SBarry Smith   No Fortran Support
1528cc4c1da9SBarry Smith 
15298b08f494SToby Isaac   Input Parameters:
15308b08f494SToby Isaac . stage - The stage number or `PETSC_DETERMINE` for the current stage
15318b08f494SToby Isaac 
15328b08f494SToby Isaac   Output Parameter:
15338b08f494SToby Isaac . info - This structure is filled with the performance information
15348b08f494SToby Isaac 
15358b08f494SToby Isaac   Level: intermediate
15368b08f494SToby Isaac 
15378b08f494SToby Isaac   Notes:
15388b08f494SToby Isaac   This is a low level routine used by the logging functions in PETSc.
15398b08f494SToby Isaac 
15408b08f494SToby Isaac   A `PETSCLOGHANDLERDEFAULT` must be running for this to work, having been started either with
15415804573cSPierre Jolivet   `PetscLogDefaultBegin()` or from the command line with `-log_view`.  If it was not started,
15420ba5dad9SToby Isaac   all performance statistics in `info` will be zeroed.
15438b08f494SToby Isaac 
15448b08f494SToby Isaac .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogGetDefaultHandler()`
15458b08f494SToby Isaac @*/
15468b08f494SToby Isaac PetscErrorCode PetscLogStageGetPerfInfo(PetscLogStage stage, PetscEventPerfInfo *info)
15478b08f494SToby Isaac {
15488b08f494SToby Isaac   PetscLogHandler     handler;
15498b08f494SToby Isaac   PetscEventPerfInfo *event_info;
15508b08f494SToby Isaac 
15518b08f494SToby Isaac   PetscFunctionBegin;
15528b08f494SToby Isaac   PetscAssertPointer(info, 2);
15530ba5dad9SToby Isaac   PetscCall(PetscLogTryGetHandler(PETSCLOGHANDLERDEFAULT, &handler));
15540ba5dad9SToby Isaac   if (handler) {
15558b08f494SToby Isaac     PetscCall(PetscLogHandlerGetStagePerfInfo(handler, stage, &event_info));
15568b08f494SToby Isaac     *info = *event_info;
15570ba5dad9SToby Isaac   } else {
15580ba5dad9SToby Isaac     PetscCall(PetscInfo(NULL, "Default log handler is not running, PetscLogStageGetPerfInfo() returning zeros\n"));
15590ba5dad9SToby Isaac     PetscCall(PetscMemzero(info, sizeof(*info)));
15600ba5dad9SToby Isaac   }
15618b08f494SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
15628b08f494SToby Isaac }
15638b08f494SToby Isaac 
15648b08f494SToby Isaac /*@C
1565b665b14eSToby Isaac   PetscLogEventGetPerfInfo - Return the performance information about the given event in the given stage
1566b665b14eSToby Isaac 
1567cc4c1da9SBarry Smith   No Fortran Support
1568cc4c1da9SBarry Smith 
1569b665b14eSToby Isaac   Input Parameters:
1570b665b14eSToby Isaac + stage - The stage number or `PETSC_DETERMINE` for the current stage
1571b665b14eSToby Isaac - event - The event number
1572b665b14eSToby Isaac 
1573b665b14eSToby Isaac   Output Parameter:
1574b665b14eSToby Isaac . info - This structure is filled with the performance information
1575b665b14eSToby Isaac 
1576b665b14eSToby Isaac   Level: intermediate
1577b665b14eSToby Isaac 
1578b665b14eSToby Isaac   Note:
1579b665b14eSToby Isaac   This is a low level routine used by the logging functions in PETSc
1580b665b14eSToby Isaac 
15810ba5dad9SToby Isaac   A `PETSCLOGHANDLERDEFAULT` must be running for this to work, having been started either with
15825804573cSPierre Jolivet   `PetscLogDefaultBegin()` or from the command line with `-log_view`.  If it was not started,
15830ba5dad9SToby Isaac   all performance statistics in `info` will be zeroed.
15840ba5dad9SToby Isaac 
1585b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogGetDefaultHandler()`
1586b665b14eSToby Isaac @*/
1587b665b14eSToby Isaac PetscErrorCode PetscLogEventGetPerfInfo(PetscLogStage stage, PetscLogEvent event, PetscEventPerfInfo *info)
1588b665b14eSToby Isaac {
1589b665b14eSToby Isaac   PetscLogHandler     handler;
1590b665b14eSToby Isaac   PetscEventPerfInfo *event_info;
1591b665b14eSToby Isaac 
1592b665b14eSToby Isaac   PetscFunctionBegin;
1593b665b14eSToby Isaac   PetscAssertPointer(info, 3);
15940ba5dad9SToby Isaac   PetscCall(PetscLogTryGetHandler(PETSCLOGHANDLERDEFAULT, &handler));
15950ba5dad9SToby Isaac   if (handler) {
1596dff009beSToby Isaac     PetscCall(PetscLogHandlerGetEventPerfInfo(handler, stage, event, &event_info));
1597b665b14eSToby Isaac     *info = *event_info;
15980ba5dad9SToby Isaac   } else {
15990ba5dad9SToby Isaac     PetscCall(PetscInfo(NULL, "Default log handler is not running, PetscLogEventGetPerfInfo() returning zeros\n"));
16000ba5dad9SToby Isaac     PetscCall(PetscMemzero(info, sizeof(*info)));
16010ba5dad9SToby Isaac   }
1602b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
1603b665b14eSToby Isaac }
1604b665b14eSToby Isaac 
1605cc4c1da9SBarry Smith /*@
1606b665b14eSToby Isaac   PetscLogEventSetDof - Set the nth number of degrees of freedom of a numerical problem associated with this event
1607b665b14eSToby Isaac 
1608b665b14eSToby Isaac   Not Collective
1609b665b14eSToby Isaac 
1610b665b14eSToby Isaac   Input Parameters:
1611b665b14eSToby Isaac + event - The event id to log
1612b665b14eSToby Isaac . n     - The dof index, in [0, 8)
1613b665b14eSToby Isaac - dof   - The number of dofs
1614b665b14eSToby Isaac 
1615b665b14eSToby Isaac   Options Database Key:
1616b665b14eSToby Isaac . -log_view - Activates log summary
1617b665b14eSToby Isaac 
1618b665b14eSToby Isaac   Level: developer
1619b665b14eSToby Isaac 
1620b665b14eSToby Isaac   Note:
1621b665b14eSToby Isaac   This is to enable logging of convergence
1622b665b14eSToby Isaac 
1623b665b14eSToby Isaac .seealso: `PetscLogEventSetError()`, `PetscLogEventRegister()`, `PetscLogGetDefaultHandler()`
1624b665b14eSToby Isaac @*/
1625b665b14eSToby Isaac PetscErrorCode PetscLogEventSetDof(PetscLogEvent event, PetscInt n, PetscLogDouble dof)
1626b665b14eSToby Isaac {
1627b665b14eSToby Isaac   PetscFunctionBegin;
1628b665b14eSToby Isaac   PetscCheck(!(n < 0) && !(n > 7), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Error index %" PetscInt_FMT " is not in [0, 8)", n);
1629dff009beSToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
1630dff009beSToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
1631dff009beSToby Isaac 
1632dff009beSToby Isaac     if (h) {
1633dff009beSToby Isaac       PetscEventPerfInfo *event_info;
1634dff009beSToby Isaac 
1635dff009beSToby Isaac       PetscCall(PetscLogHandlerGetEventPerfInfo(h, PETSC_DEFAULT, event, &event_info));
1636dff009beSToby Isaac       if (event_info) event_info->dof[n] = dof;
1637dff009beSToby Isaac     }
1638b665b14eSToby Isaac   }
1639b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
1640b665b14eSToby Isaac }
1641b665b14eSToby Isaac 
1642cc4c1da9SBarry Smith /*@
1643b665b14eSToby Isaac   PetscLogEventSetError - Set the nth error associated with a numerical problem associated with this event
1644b665b14eSToby Isaac 
1645b665b14eSToby Isaac   Not Collective
1646b665b14eSToby Isaac 
1647b665b14eSToby Isaac   Input Parameters:
1648b665b14eSToby Isaac + event - The event id to log
1649b665b14eSToby Isaac . n     - The error index, in [0, 8)
1650b665b14eSToby Isaac - error - The error
1651b665b14eSToby Isaac 
1652b665b14eSToby Isaac   Options Database Key:
1653b665b14eSToby Isaac . -log_view - Activates log summary
1654b665b14eSToby Isaac 
1655b665b14eSToby Isaac   Level: developer
1656b665b14eSToby Isaac 
1657b665b14eSToby Isaac   Notes:
1658b665b14eSToby Isaac   This is to enable logging of convergence, and enable users to interpret the errors as they wish. For example,
1659b665b14eSToby Isaac   as different norms, or as errors for different fields
1660b665b14eSToby Isaac 
1661b665b14eSToby Isaac   This is a low level routine used by the logging functions in PETSc
1662b665b14eSToby Isaac 
1663b665b14eSToby Isaac .seealso: `PetscLogEventSetDof()`, `PetscLogEventRegister()`, `PetscLogGetDefaultHandler()`
1664b665b14eSToby Isaac @*/
1665b665b14eSToby Isaac PetscErrorCode PetscLogEventSetError(PetscLogEvent event, PetscInt n, PetscLogDouble error)
1666b665b14eSToby Isaac {
1667b665b14eSToby Isaac   PetscFunctionBegin;
1668b665b14eSToby Isaac   PetscCheck(!(n < 0) && !(n > 7), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Error index %" PetscInt_FMT " is not in [0, 8)", n);
1669dff009beSToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
1670dff009beSToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
1671dff009beSToby Isaac 
1672dff009beSToby Isaac     if (h) {
1673dff009beSToby Isaac       PetscEventPerfInfo *event_info;
1674dff009beSToby Isaac 
1675dff009beSToby Isaac       PetscCall(PetscLogHandlerGetEventPerfInfo(h, PETSC_DEFAULT, event, &event_info));
1676dff009beSToby Isaac       if (event_info) event_info->errors[n] = error;
1677dff009beSToby Isaac     }
1678b665b14eSToby Isaac   }
1679b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
1680b665b14eSToby Isaac }
1681b665b14eSToby Isaac 
1682cc4c1da9SBarry Smith /*@
16835c6c1daeSBarry Smith   PetscLogEventGetId - Returns the event id when given the event name.
16845c6c1daeSBarry Smith 
16855c6c1daeSBarry Smith   Not Collective
16865c6c1daeSBarry Smith 
16875c6c1daeSBarry Smith   Input Parameter:
16885c6c1daeSBarry Smith . name - The event name
16895c6c1daeSBarry Smith 
16905c6c1daeSBarry Smith   Output Parameter:
1691c5deb1d5SJed Brown . event - The event, or -1 if no event with that name exists
16925c6c1daeSBarry Smith 
16935c6c1daeSBarry Smith   Level: intermediate
16945c6c1daeSBarry Smith 
1695d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogStageGetId()`
16965c6c1daeSBarry Smith @*/
1697d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventGetId(const char name[], PetscLogEvent *event)
1698d71ae5a4SJacob Faibussowitsch {
1699b665b14eSToby Isaac   PetscLogState state;
17005c6c1daeSBarry Smith 
17015c6c1daeSBarry Smith   PetscFunctionBegin;
1702b665b14eSToby Isaac   *event = -1;
1703b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
1704b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateGetEventFromName(state, name, event));
17053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
17065c6c1daeSBarry Smith }
17075c6c1daeSBarry Smith 
1708cc4c1da9SBarry Smith /*@
170953e0a2f3SToby Isaac   PetscLogEventGetName - Returns the event name when given the event id.
171053e0a2f3SToby Isaac 
171153e0a2f3SToby Isaac   Not Collective
171253e0a2f3SToby Isaac 
171353e0a2f3SToby Isaac   Input Parameter:
171453e0a2f3SToby Isaac . event - The event
171553e0a2f3SToby Isaac 
171653e0a2f3SToby Isaac   Output Parameter:
171753e0a2f3SToby Isaac . name - The event name
171853e0a2f3SToby Isaac 
171953e0a2f3SToby Isaac   Level: intermediate
172053e0a2f3SToby Isaac 
172153e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()`
172253e0a2f3SToby Isaac @*/
1723cc4c1da9SBarry Smith PetscErrorCode PetscLogEventGetName(PetscLogEvent event, const char *name[])
172453e0a2f3SToby Isaac {
172553e0a2f3SToby Isaac   PetscLogEventInfo event_info;
172653e0a2f3SToby Isaac   PetscLogState     state;
172753e0a2f3SToby Isaac 
172853e0a2f3SToby Isaac   PetscFunctionBegin;
1729b665b14eSToby Isaac   *name = NULL;
173053e0a2f3SToby Isaac   PetscCall(PetscLogGetState(&state));
1731b665b14eSToby Isaac   if (!state) PetscFunctionReturn(PETSC_SUCCESS);
173253e0a2f3SToby Isaac   PetscCall(PetscLogStateEventGetInfo(state, event, &event_info));
173353e0a2f3SToby Isaac   *name = event_info.name;
173453e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
173553e0a2f3SToby Isaac }
173653e0a2f3SToby Isaac 
173753e0a2f3SToby Isaac /*@
173853e0a2f3SToby 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.
173953e0a2f3SToby Isaac 
174053e0a2f3SToby Isaac   Not collective
174153e0a2f3SToby Isaac 
174253e0a2f3SToby Isaac   Level: advanced
174353e0a2f3SToby Isaac 
174453e0a2f3SToby Isaac   Notes:
174553e0a2f3SToby 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()`).
174653e0a2f3SToby Isaac 
174753e0a2f3SToby Isaac   Other log handlers (such as the nested handler, `PetscLogNestedBegin()`) will ignore this function.
174853e0a2f3SToby Isaac 
1749b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogEventDeactivatePush()`, `PetscLogEventDeactivatePop()`, `PetscLogEventsResume()`, `PetscLogGetDefaultHandler()`
175053e0a2f3SToby Isaac @*/
175153e0a2f3SToby Isaac PetscErrorCode PetscLogEventsPause(void)
175253e0a2f3SToby Isaac {
175353e0a2f3SToby Isaac   PetscFunctionBegin;
1754dff009beSToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
1755dff009beSToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
1756dff009beSToby Isaac 
1757dff009beSToby Isaac     if (h) PetscCall(PetscLogHandlerEventsPause(h));
1758dff009beSToby Isaac   }
175953e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
176053e0a2f3SToby Isaac }
176153e0a2f3SToby Isaac 
176253e0a2f3SToby Isaac /*@
176353e0a2f3SToby Isaac   PetscLogEventsResume - Return logging to normal behavior after it was paused with `PetscLogEventsPause()`.
176453e0a2f3SToby Isaac 
176553e0a2f3SToby Isaac   Not collective
176653e0a2f3SToby Isaac 
176753e0a2f3SToby Isaac   Level: advanced
176853e0a2f3SToby Isaac 
1769b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogEventDeactivatePush()`, `PetscLogEventDeactivatePop()`, `PetscLogEventsPause()`, `PetscLogGetDefaultHandler()`
177053e0a2f3SToby Isaac @*/
177153e0a2f3SToby Isaac PetscErrorCode PetscLogEventsResume(void)
177253e0a2f3SToby Isaac {
177353e0a2f3SToby Isaac   PetscFunctionBegin;
1774dff009beSToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
1775dff009beSToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
1776dff009beSToby Isaac 
1777dff009beSToby Isaac     if (h) PetscCall(PetscLogHandlerEventsResume(h));
1778dff009beSToby Isaac   }
177953e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
178053e0a2f3SToby Isaac }
178153e0a2f3SToby Isaac 
17821c1ad86eSToby Isaac /*------------------------------------------------ Class Functions --------------------------------------------------*/
17831c1ad86eSToby Isaac 
17841c1ad86eSToby Isaac /*MC
17851c1ad86eSToby Isaac    PetscLogObjectCreate - Log the creation of a `PetscObject`
17861c1ad86eSToby Isaac 
17871c1ad86eSToby Isaac    Synopsis:
17881c1ad86eSToby Isaac    #include <petsclog.h>
17891c1ad86eSToby Isaac    PetscErrorCode PetscLogObjectCreate(PetscObject h)
17901c1ad86eSToby Isaac 
17911c1ad86eSToby Isaac    Not Collective
17921c1ad86eSToby Isaac 
17931c1ad86eSToby Isaac    Input Parameters:
17941c1ad86eSToby Isaac .  h - A `PetscObject`
17951c1ad86eSToby Isaac 
17961c1ad86eSToby Isaac    Level: developer
17971c1ad86eSToby Isaac 
17981c1ad86eSToby Isaac    Developer Note:
17991c1ad86eSToby Isaac      Called internally by PETSc when creating objects: users do not need to call this directly.
1800b665b14eSToby Isaac      Notification of the object creation is sent to each `PetscLogHandler` that is running.
18011c1ad86eSToby Isaac 
1802b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogObjectDestroy()`
18031c1ad86eSToby Isaac M*/
18041c1ad86eSToby Isaac 
18051c1ad86eSToby Isaac /*MC
18061c1ad86eSToby Isaac    PetscLogObjectDestroy - Logs the destruction of a `PetscObject`
18071c1ad86eSToby Isaac 
18081c1ad86eSToby Isaac    Synopsis:
18091c1ad86eSToby Isaac    #include <petsclog.h>
18101c1ad86eSToby Isaac    PetscErrorCode PetscLogObjectDestroy(PetscObject h)
18111c1ad86eSToby Isaac 
18121c1ad86eSToby Isaac    Not Collective
18131c1ad86eSToby Isaac 
18141c1ad86eSToby Isaac    Input Parameters:
18151c1ad86eSToby Isaac .  h - A `PetscObject`
18161c1ad86eSToby Isaac 
18171c1ad86eSToby Isaac    Level: developer
18181c1ad86eSToby Isaac 
18191c1ad86eSToby Isaac    Developer Note:
18201c1ad86eSToby Isaac      Called internally by PETSc when destroying objects: users do not need to call this directly.
1821b665b14eSToby Isaac      Notification of the object creation is sent to each `PetscLogHandler` that is running.
18221c1ad86eSToby Isaac 
1823b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogObjectCreate()`
18241c1ad86eSToby Isaac M*/
18251c1ad86eSToby Isaac 
1826cc4c1da9SBarry Smith /*@
182753e0a2f3SToby Isaac   PetscLogClassGetClassId - Returns the `PetscClassId` when given the class name.
182853e0a2f3SToby Isaac 
182953e0a2f3SToby Isaac   Not Collective
183053e0a2f3SToby Isaac 
183153e0a2f3SToby Isaac   Input Parameter:
183253e0a2f3SToby Isaac . name - The class name
183353e0a2f3SToby Isaac 
183453e0a2f3SToby Isaac   Output Parameter:
183553e0a2f3SToby Isaac . classid - The `PetscClassId` id, or -1 if no class with that name exists
183653e0a2f3SToby Isaac 
183753e0a2f3SToby Isaac   Level: intermediate
183853e0a2f3SToby Isaac 
183953e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogStageGetId()`
184053e0a2f3SToby Isaac @*/
184153e0a2f3SToby Isaac PetscErrorCode PetscLogClassGetClassId(const char name[], PetscClassId *classid)
184253e0a2f3SToby Isaac {
184353e0a2f3SToby Isaac   PetscLogClass     log_class;
184453e0a2f3SToby Isaac   PetscLogClassInfo class_info;
184553e0a2f3SToby Isaac   PetscLogState     state;
184653e0a2f3SToby Isaac 
184753e0a2f3SToby Isaac   PetscFunctionBegin;
1848b665b14eSToby Isaac   *classid = -1;
184953e0a2f3SToby Isaac   PetscCall(PetscLogGetState(&state));
1850b665b14eSToby Isaac   if (!state) PetscFunctionReturn(PETSC_SUCCESS);
185153e0a2f3SToby Isaac   PetscCall(PetscLogStateGetClassFromName(state, name, &log_class));
185253e0a2f3SToby Isaac   if (log_class < 0) {
185353e0a2f3SToby Isaac     *classid = -1;
185453e0a2f3SToby Isaac     PetscFunctionReturn(PETSC_SUCCESS);
185553e0a2f3SToby Isaac   }
185653e0a2f3SToby Isaac   PetscCall(PetscLogStateClassGetInfo(state, log_class, &class_info));
185753e0a2f3SToby Isaac   *classid = class_info.classid;
185853e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
185953e0a2f3SToby Isaac }
186053e0a2f3SToby Isaac 
186153e0a2f3SToby Isaac /*@C
186253e0a2f3SToby Isaac   PetscLogClassIdGetName - Returns a `PetscClassId`'s name.
186353e0a2f3SToby Isaac 
186453e0a2f3SToby Isaac   Not Collective
186553e0a2f3SToby Isaac 
186653e0a2f3SToby Isaac   Input Parameter:
186753e0a2f3SToby Isaac . classid - A `PetscClassId`
186853e0a2f3SToby Isaac 
186953e0a2f3SToby Isaac   Output Parameter:
187053e0a2f3SToby Isaac . name - The class name
187153e0a2f3SToby Isaac 
187253e0a2f3SToby Isaac   Level: intermediate
187353e0a2f3SToby Isaac 
187453e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogClassRegister()`, `PetscLogClassBegin()`, `PetscLogClassEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadClass()`
187553e0a2f3SToby Isaac @*/
187653e0a2f3SToby Isaac PetscErrorCode PetscLogClassIdGetName(PetscClassId classid, const char **name)
187753e0a2f3SToby Isaac {
187853e0a2f3SToby Isaac   PetscLogClass     log_class;
187953e0a2f3SToby Isaac   PetscLogClassInfo class_info;
188053e0a2f3SToby Isaac   PetscLogState     state;
188153e0a2f3SToby Isaac 
188253e0a2f3SToby Isaac   PetscFunctionBegin;
188353e0a2f3SToby Isaac   PetscCall(PetscLogGetState(&state));
188453e0a2f3SToby Isaac   PetscCall(PetscLogStateGetClassFromClassId(state, classid, &log_class));
188553e0a2f3SToby Isaac   PetscCall(PetscLogStateClassGetInfo(state, log_class, &class_info));
188653e0a2f3SToby Isaac   *name = class_info.name;
188753e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
188853e0a2f3SToby Isaac }
188953e0a2f3SToby Isaac 
18905c6c1daeSBarry Smith /*------------------------------------------------ Output Functions -------------------------------------------------*/
1891cc4c1da9SBarry Smith /*@
18925c6c1daeSBarry Smith   PetscLogDump - Dumps logs of objects to a file. This file is intended to
18935c6c1daeSBarry Smith   be read by bin/petscview. This program no longer exists.
18945c6c1daeSBarry Smith 
1895811af0c4SBarry Smith   Collective on `PETSC_COMM_WORLD`
18965c6c1daeSBarry Smith 
18975c6c1daeSBarry Smith   Input Parameter:
1898aec76313SJacob Faibussowitsch . sname - an optional file name
18995c6c1daeSBarry Smith 
190010450e9eSJacob Faibussowitsch   Example Usage:
19015c6c1daeSBarry Smith .vb
19025c6c1daeSBarry Smith   PetscInitialize(...);
1903b665b14eSToby Isaac   PetscLogDefaultBegin();
1904b665b14eSToby Isaac   // ... code ...
19055c6c1daeSBarry Smith   PetscLogDump(filename);
19065c6c1daeSBarry Smith   PetscFinalize();
19075c6c1daeSBarry Smith .ve
19085c6c1daeSBarry Smith 
1909d1f92df0SBarry Smith   Level: advanced
1910d1f92df0SBarry Smith 
1911811af0c4SBarry Smith   Note:
191237fdd005SBarry Smith   The default file name is Log.<rank> where <rank> is the MPI process rank. If no name is specified,
19135c6c1daeSBarry Smith   this file will be used.
19145c6c1daeSBarry Smith 
1915b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogDefaultBegin()`, `PetscLogView()`, `PetscLogGetDefaultHandler()`
19165c6c1daeSBarry Smith @*/
1917d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogDump(const char sname[])
1918d71ae5a4SJacob Faibussowitsch {
1919b665b14eSToby Isaac   PetscLogHandler handler;
19205c6c1daeSBarry Smith 
19215c6c1daeSBarry Smith   PetscFunctionBegin;
1922294de794SToby Isaac   PetscCall(PetscLogGetHandler(PETSCLOGHANDLERDEFAULT, &handler));
1923dff009beSToby Isaac   PetscCall(PetscLogHandlerDump(handler, sname));
1924b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
19255c6c1daeSBarry Smith }
1926b665b14eSToby Isaac 
1927cc4c1da9SBarry Smith /*@
1928b665b14eSToby Isaac   PetscLogMPEDump - Dumps the MPE logging info to file for later use with Jumpshot.
1929b665b14eSToby Isaac 
19308f14a041SBarry Smith   Collective on `PETSC_COMM_WORLD`
1931b665b14eSToby Isaac 
1932b665b14eSToby Isaac   Input Parameter:
1933b665b14eSToby Isaac . sname - filename for the MPE logfile
1934b665b14eSToby Isaac 
1935b665b14eSToby Isaac   Level: advanced
1936b665b14eSToby Isaac 
1937b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogDump()`, `PetscLogMPEBegin()`
1938b665b14eSToby Isaac @*/
1939b665b14eSToby Isaac PetscErrorCode PetscLogMPEDump(const char sname[])
1940b665b14eSToby Isaac {
1941b665b14eSToby Isaac   PetscFunctionBegin;
1942b665b14eSToby Isaac   #if defined(PETSC_HAVE_MPE)
1943b665b14eSToby Isaac   if (PetscBeganMPE) {
1944b665b14eSToby Isaac     char name[PETSC_MAX_PATH_LEN];
1945b665b14eSToby Isaac 
1946b665b14eSToby Isaac     PetscCall(PetscInfo(0, "Finalizing MPE.\n"));
1947b665b14eSToby Isaac     if (sname) {
1948b665b14eSToby Isaac       PetscCall(PetscStrncpy(name, sname, sizeof(name)));
19495c6c1daeSBarry Smith     } else {
1950b665b14eSToby Isaac       PetscCall(PetscGetProgramName(name, sizeof(name)));
19515c6c1daeSBarry Smith     }
1952b665b14eSToby Isaac     PetscCall(MPE_Finish_log(name));
19535c6c1daeSBarry Smith   } else {
1954b665b14eSToby Isaac     PetscCall(PetscInfo(0, "Not finalizing MPE (not started by PETSc).\n"));
19555c6c1daeSBarry Smith   }
1956c2a741eeSJunchao Zhang   #else
1957b665b14eSToby Isaac   SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP_SYS, "PETSc was configured without MPE support, reconfigure with --with-mpe or --download-mpe");
1958c2a741eeSJunchao Zhang   #endif
19593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19605c6c1daeSBarry Smith }
19615c6c1daeSBarry Smith 
1962ffeef943SBarry Smith /*@
19637d6c928cSSatish Balay   PetscLogView - Prints a summary of the logging.
19645c6c1daeSBarry Smith 
19658f14a041SBarry Smith   Collective
19665c6c1daeSBarry Smith 
19675c6c1daeSBarry Smith   Input Parameter:
1968f14045dbSBarry Smith . viewer - an ASCII viewer
19695c6c1daeSBarry Smith 
19705c6c1daeSBarry Smith   Options Database Keys:
1971bb1d7374SBarry Smith + -log_view [:filename]                    - Prints summary of log information
1972bb1d7374SBarry Smith . -log_view :filename.py:ascii_info_detail - Saves logging information from each process as a Python file
1973607d249eSBarry 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)
1974d0a29bd7SConnor 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)
1975156b51fbSBarry Smith . -log_view_memory                         - Also display memory usage in each event
1976156b51fbSBarry Smith . -log_view_gpu_time                       - Also display time in each event for GPU kernels (Note this may slow the computation)
1977811af0c4SBarry Smith . -log_all                                 - Saves a file Log.rank for each MPI rank with details of each step of the computation
1978bb1d7374SBarry Smith - -log_trace [filename]                    - Displays a trace of what each process is doing
19795c6c1daeSBarry Smith 
1980d1f92df0SBarry Smith   Level: beginner
1981d1f92df0SBarry Smith 
19825c6c1daeSBarry Smith   Notes:
1983da81f932SPierre Jolivet   It is possible to control the logging programmatically but we recommend using the options database approach whenever possible
19845c6c1daeSBarry Smith   By default the summary is printed to stdout.
19855c6c1daeSBarry Smith 
1986bb1d7374SBarry Smith   Before calling this routine you must have called either PetscLogDefaultBegin() or PetscLogNestedBegin()
1987bb1d7374SBarry Smith 
1988bb1d7374SBarry Smith   If PETSc is configured with --with-logging=0 then this functionality is not available
1989bb1d7374SBarry Smith 
1990607d249eSBarry Smith   To view the nested XML format filename.xml first copy  ${PETSC_DIR}/share/petsc/xml/performance_xml2html.xsl to the current
1991607d249eSBarry Smith   directory then open filename.xml with your browser. Specific notes for certain browsers
19921d27aa22SBarry Smith .vb
19931d27aa22SBarry Smith     Firefox and Internet explorer - simply open the file
19941d27aa22SBarry Smith     Google Chrome - you must start up Chrome with the option --allow-file-access-from-files
19951d27aa22SBarry Smith     Safari - see https://ccm.net/faq/36342-safari-how-to-enable-local-file-access
19961d27aa22SBarry Smith .ve
19971d27aa22SBarry 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
1998607d249eSBarry Smith   your browser.
19992add09c0SLisandro Dalcin   Alternatively, use the script ${PETSC_DIR}/lib/petsc/bin/petsc-performance-view to automatically open a new browser
20002add09c0SLisandro Dalcin   window and render the XML log file contents.
2001607d249eSBarry Smith 
2002bb1d7374SBarry Smith   The nested XML format was kindly donated by Koos Huijssen and Christiaan M. Klaij  MARITIME  RESEARCH  INSTITUTE  NETHERLANDS
2003bb1d7374SBarry Smith 
20041d27aa22SBarry Smith   The Flame Graph output can be visualised using either the original Flame Graph script <https://github.com/brendangregg/FlameGraph>
20051d27aa22SBarry Smith   or using speedscope <https://www.speedscope.app>.
2006d0a29bd7SConnor Ward   Old XML profiles may be converted into this format using the script ${PETSC_DIR}/lib/petsc/bin/xml2flamegraph.py.
2007d0a29bd7SConnor Ward 
2008d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogDefaultBegin()`, `PetscLogDump()`
20095c6c1daeSBarry Smith @*/
2010d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogView(PetscViewer viewer)
2011d71ae5a4SJacob Faibussowitsch {
2012f14045dbSBarry Smith   PetscBool         isascii;
2013f14045dbSBarry Smith   PetscViewerFormat format;
2014b665b14eSToby Isaac   int               stage;
2015b665b14eSToby Isaac   PetscLogState     state;
2016b665b14eSToby Isaac   PetscIntStack     temp_stack;
2017b665b14eSToby Isaac   PetscLogHandler   handler;
2018b665b14eSToby Isaac   PetscBool         is_empty;
20195c6c1daeSBarry Smith 
20205c6c1daeSBarry Smith   PetscFunctionBegin;
2021b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
202237b78d16SBarry Smith   /* Pop off any stages the user forgot to remove */
2023b665b14eSToby Isaac   PetscCall(PetscIntStackCreate(&temp_stack));
2024b665b14eSToby Isaac   PetscCall(PetscLogStateGetCurrentStage(state, &stage));
202537b78d16SBarry Smith   while (stage >= 0) {
2026b665b14eSToby Isaac     PetscCall(PetscLogStagePop());
2027b665b14eSToby Isaac     PetscCall(PetscIntStackPush(temp_stack, stage));
2028b665b14eSToby Isaac     PetscCall(PetscLogStateGetCurrentStage(state, &stage));
202937b78d16SBarry Smith   }
20309566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
203128b400f6SJacob Faibussowitsch   PetscCheck(isascii, PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Currently can only view logging to ASCII");
20329566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(viewer, &format));
2033b665b14eSToby Isaac   if (format == PETSC_VIEWER_ASCII_XML || format == PETSC_VIEWER_ASCII_FLAMEGRAPH) {
2034294de794SToby Isaac     PetscCall(PetscLogGetHandler(PETSCLOGHANDLERNESTED, &handler));
2035b665b14eSToby Isaac     PetscCall(PetscLogHandlerView(handler, viewer));
2036b665b14eSToby Isaac   } else {
2037294de794SToby Isaac     PetscCall(PetscLogGetHandler(PETSCLOGHANDLERDEFAULT, &handler));
2038b665b14eSToby Isaac     PetscCall(PetscLogHandlerView(handler, viewer));
20395c6c1daeSBarry Smith   }
2040b665b14eSToby Isaac   PetscCall(PetscIntStackEmpty(temp_stack, &is_empty));
2041b665b14eSToby Isaac   while (!is_empty) {
2042b665b14eSToby Isaac     PetscCall(PetscIntStackPop(temp_stack, &stage));
2043b665b14eSToby Isaac     PetscCall(PetscLogStagePush(stage));
2044b665b14eSToby Isaac     PetscCall(PetscIntStackEmpty(temp_stack, &is_empty));
2045b665b14eSToby Isaac   }
2046b665b14eSToby Isaac   PetscCall(PetscIntStackDestroy(temp_stack));
20473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
20485c6c1daeSBarry Smith }
20495c6c1daeSBarry Smith 
2050f14045dbSBarry Smith /*@C
2051811af0c4SBarry Smith   PetscLogViewFromOptions - Processes command line options to determine if/how a `PetscLog` is to be viewed.
2052f14045dbSBarry Smith 
2053811af0c4SBarry Smith   Collective on `PETSC_COMM_WORLD`
2054f14045dbSBarry Smith 
2055811af0c4SBarry Smith   Level: developer
2056f14045dbSBarry Smith 
2057d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogView()`
2058f14045dbSBarry Smith @*/
2059d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogViewFromOptions(void)
2060d71ae5a4SJacob Faibussowitsch {
2061ad2e3d55SToby Isaac   PetscInt          n_max = PETSC_LOG_VIEW_FROM_OPTIONS_MAX;
2062ad2e3d55SToby Isaac   PetscViewer       viewers[PETSC_LOG_VIEW_FROM_OPTIONS_MAX];
2063ad2e3d55SToby Isaac   PetscViewerFormat formats[PETSC_LOG_VIEW_FROM_OPTIONS_MAX];
2064f14045dbSBarry Smith   PetscBool         flg;
2065f14045dbSBarry Smith 
2066f14045dbSBarry Smith   PetscFunctionBegin;
2067648c30bcSBarry Smith   PetscCall(PetscOptionsCreateViewers(PETSC_COMM_WORLD, NULL, NULL, "-log_view", &n_max, viewers, formats, &flg));
2068ad2e3d55SToby Isaac   for (PetscInt i = 0; i < n_max; i++) {
2069ad2e3d55SToby Isaac     PetscCall(PetscViewerPushFormat(viewers[i], formats[i]));
2070ad2e3d55SToby Isaac     PetscCall(PetscLogView(viewers[i]));
2071ad2e3d55SToby Isaac     PetscCall(PetscViewerPopFormat(viewers[i]));
2072648c30bcSBarry Smith     PetscCall(PetscViewerDestroy(&viewers[i]));
2073f14045dbSBarry Smith   }
20743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2075f14045dbSBarry Smith }
2076f14045dbSBarry Smith 
2077b665b14eSToby Isaac PETSC_INTERN PetscErrorCode PetscLogHandlerNestedSetThreshold(PetscLogHandler, PetscLogDouble, PetscLogDouble *);
2078b665b14eSToby Isaac 
2079b665b14eSToby Isaac /*@
2080b665b14eSToby Isaac   PetscLogSetThreshold - Set the threshold time for logging the events; this is a percentage out of 100, so 1. means any event
2081b665b14eSToby Isaac   that takes 1 or more percent of the time.
2082b665b14eSToby Isaac 
20838f14a041SBarry Smith   Logically Collective on `PETSC_COMM_WORLD`
2084b665b14eSToby Isaac 
2085b665b14eSToby Isaac   Input Parameter:
2086b665b14eSToby Isaac . newThresh - the threshold to use
2087b665b14eSToby Isaac 
2088b665b14eSToby Isaac   Output Parameter:
2089b665b14eSToby Isaac . oldThresh - the previously set threshold value
2090b665b14eSToby Isaac 
2091b665b14eSToby Isaac   Options Database Keys:
2092b665b14eSToby Isaac . -log_view :filename.xml:ascii_xml - Prints an XML summary of flop and timing information to the file
2093b665b14eSToby Isaac 
2094b665b14eSToby Isaac   Example Usage:
2095b665b14eSToby Isaac .vb
2096b665b14eSToby Isaac   PetscInitialize(...);
2097b665b14eSToby Isaac   PetscLogNestedBegin();
2098b665b14eSToby Isaac   PetscLogSetThreshold(0.1,&oldthresh);
2099b665b14eSToby Isaac   // ... code ...
2100b665b14eSToby Isaac   PetscLogView(viewer);
2101b665b14eSToby Isaac   PetscFinalize();
2102b665b14eSToby Isaac .ve
2103b665b14eSToby Isaac 
2104b665b14eSToby Isaac   Level: advanced
2105b665b14eSToby Isaac 
2106b665b14eSToby Isaac   Note:
2107b665b14eSToby Isaac   This threshold is only used by the nested log handler
2108b665b14eSToby Isaac 
2109b665b14eSToby Isaac .seealso: `PetscLogDump()`, `PetscLogView()`, `PetscLogTraceBegin()`, `PetscLogDefaultBegin()`,
2110b665b14eSToby Isaac           `PetscLogNestedBegin()`
2111b665b14eSToby Isaac @*/
2112b665b14eSToby Isaac PetscErrorCode PetscLogSetThreshold(PetscLogDouble newThresh, PetscLogDouble *oldThresh)
2113b665b14eSToby Isaac {
2114b665b14eSToby Isaac   PetscLogHandler handler;
2115b665b14eSToby Isaac 
2116b665b14eSToby Isaac   PetscFunctionBegin;
2117294de794SToby Isaac   PetscCall(PetscLogTryGetHandler(PETSCLOGHANDLERNESTED, &handler));
2118b665b14eSToby Isaac   PetscCall(PetscLogHandlerNestedSetThreshold(handler, newThresh, oldThresh));
2119b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
2120b665b14eSToby Isaac }
2121b665b14eSToby Isaac 
21225c6c1daeSBarry Smith /*----------------------------------------------- Counter Functions -------------------------------------------------*/
2123cc4c1da9SBarry Smith /*@
21245c6c1daeSBarry Smith   PetscGetFlops - Returns the number of flops used on this processor
21255c6c1daeSBarry Smith   since the program began.
21265c6c1daeSBarry Smith 
21275c6c1daeSBarry Smith   Not Collective
21285c6c1daeSBarry Smith 
21295c6c1daeSBarry Smith   Output Parameter:
213010450e9eSJacob Faibussowitsch . flops - number of floating point operations
21315c6c1daeSBarry Smith 
2132d1f92df0SBarry Smith   Level: intermediate
2133d1f92df0SBarry Smith 
21345c6c1daeSBarry Smith   Notes:
21355c6c1daeSBarry Smith   A global counter logs all PETSc flop counts.  The user can use
2136811af0c4SBarry Smith   `PetscLogFlops()` to increment this counter to include flops for the
21375c6c1daeSBarry Smith   application code.
21385c6c1daeSBarry Smith 
21394b7c4d4dSPierre Jolivet   A separate counter `PetscLogGpuFlops()` logs the flops that occur on any GPU associated with this MPI rank
2140811af0c4SBarry Smith 
21414b7c4d4dSPierre Jolivet .seealso: [](ch_profiling), `PetscLogGpuFlops()`, `PetscTime()`, `PetscLogFlops()`
21425c6c1daeSBarry Smith @*/
2143d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGetFlops(PetscLogDouble *flops)
2144d71ae5a4SJacob Faibussowitsch {
21455c6c1daeSBarry Smith   PetscFunctionBegin;
21465c6c1daeSBarry Smith   *flops = petsc_TotalFlops;
21473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21485c6c1daeSBarry Smith }
21495c6c1daeSBarry Smith 
21501c1ad86eSToby Isaac /*@C
21511c1ad86eSToby Isaac   PetscLogObjectState - Record information about an object with the default log handler
21521c1ad86eSToby Isaac 
21531c1ad86eSToby Isaac   Not Collective
21541c1ad86eSToby Isaac 
21551c1ad86eSToby Isaac   Input Parameters:
21561c1ad86eSToby Isaac + obj    - the `PetscObject`
21571c1ad86eSToby Isaac . format - a printf-style format string
21581c1ad86eSToby Isaac - ...    - printf arguments to format
21591c1ad86eSToby Isaac 
21601c1ad86eSToby Isaac   Level: developer
21611c1ad86eSToby Isaac 
2162b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogObjectCreate()`, `PetscLogObjectDestroy()`, `PetscLogGetDefaultHandler()`
21631c1ad86eSToby Isaac @*/
2164d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogObjectState(PetscObject obj, const char format[], ...)
2165d71ae5a4SJacob Faibussowitsch {
21665c6c1daeSBarry Smith   PetscFunctionBegin;
2167dff009beSToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
2168dff009beSToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
2169dff009beSToby Isaac 
2170dff009beSToby Isaac     if (h) {
2171dff009beSToby Isaac       va_list Argp;
21725c6c1daeSBarry Smith       va_start(Argp, format);
2173dff009beSToby Isaac       PetscCall(PetscLogHandlerLogObjectState_Internal(h, obj, format, Argp));
21745c6c1daeSBarry Smith       va_end(Argp);
2175b665b14eSToby Isaac     }
2176dff009beSToby Isaac   }
21773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21785c6c1daeSBarry Smith }
21795c6c1daeSBarry Smith 
21805c6c1daeSBarry Smith /*MC
21815c6c1daeSBarry Smith   PetscLogFlops - Adds floating point operations to the global counter.
21825c6c1daeSBarry Smith 
21835c6c1daeSBarry Smith   Synopsis:
2184aaa7dc30SBarry Smith   #include <petsclog.h>
21855c6c1daeSBarry Smith   PetscErrorCode PetscLogFlops(PetscLogDouble f)
21865c6c1daeSBarry Smith 
21875c6c1daeSBarry Smith   Not Collective
21885c6c1daeSBarry Smith 
21895c6c1daeSBarry Smith   Input Parameter:
21905c6c1daeSBarry Smith . f - flop counter
21915c6c1daeSBarry Smith 
219210450e9eSJacob Faibussowitsch   Example Usage:
21935c6c1daeSBarry Smith .vb
21945c6c1daeSBarry Smith   PetscLogEvent USER_EVENT;
219510450e9eSJacob Faibussowitsch 
21965c6c1daeSBarry Smith   PetscLogEventRegister("User event", 0, &USER_EVENT);
21975c6c1daeSBarry Smith   PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0);
21985c6c1daeSBarry Smith   [code segment to monitor]
21995c6c1daeSBarry Smith   PetscLogFlops(user_flops)
22005c6c1daeSBarry Smith   PetscLogEventEnd(USER_EVENT, 0, 0, 0, 0);
22015c6c1daeSBarry Smith .ve
22025c6c1daeSBarry Smith 
2203d1f92df0SBarry Smith   Level: intermediate
2204d1f92df0SBarry Smith 
2205811af0c4SBarry Smith   Note:
220610450e9eSJacob Faibussowitsch    A global counter logs all PETSc flop counts. The user can use PetscLogFlops() to increment
220710450e9eSJacob Faibussowitsch    this counter to include flops for the application code.
22085c6c1daeSBarry Smith 
22094b7c4d4dSPierre Jolivet .seealso: [](ch_profiling), `PetscLogGpuFlops()`, `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscGetFlops()`
22105c6c1daeSBarry Smith M*/
22115c6c1daeSBarry Smith 
22125c6c1daeSBarry Smith /*MC
221310450e9eSJacob Faibussowitsch   PetscPreLoadBegin - Begin a segment of code that may be preloaded (run twice) to get accurate
221410450e9eSJacob Faibussowitsch   timings
22155c6c1daeSBarry Smith 
22165c6c1daeSBarry Smith   Synopsis:
2217aaa7dc30SBarry Smith   #include <petsclog.h>
22185c6c1daeSBarry Smith   void PetscPreLoadBegin(PetscBool flag, char *name);
22195c6c1daeSBarry Smith 
22205c6c1daeSBarry Smith   Not Collective
22215c6c1daeSBarry Smith 
2222d8d19677SJose E. Roman   Input Parameters:
222310450e9eSJacob Faibussowitsch + flag - `PETSC_TRUE` to run twice, `PETSC_FALSE` to run once, may be overridden with command
222410450e9eSJacob Faibussowitsch          line option `-preload true|false`
222510450e9eSJacob Faibussowitsch - name - name of first stage (lines of code timed separately with `-log_view`) to be preloaded
22265c6c1daeSBarry Smith 
222710450e9eSJacob Faibussowitsch   Example Usage:
22285c6c1daeSBarry Smith .vb
222910450e9eSJacob Faibussowitsch   PetscPreLoadBegin(PETSC_TRUE, "first stage");
223010450e9eSJacob Faibussowitsch   // lines of code
22315c6c1daeSBarry Smith   PetscPreLoadStage("second stage");
223210450e9eSJacob Faibussowitsch   // lines of code
22335c6c1daeSBarry Smith   PetscPreLoadEnd();
22345c6c1daeSBarry Smith .ve
22355c6c1daeSBarry Smith 
2236d1f92df0SBarry Smith   Level: intermediate
2237d1f92df0SBarry Smith 
2238811af0c4SBarry Smith   Note:
223995452b02SPatrick Sanan   Only works in C/C++, not Fortran
22405c6c1daeSBarry Smith 
224110450e9eSJacob Faibussowitsch   Flags available within the macro\:
224210450e9eSJacob Faibussowitsch + PetscPreLoadingUsed - `PETSC_TRUE` if we are or have done preloading
224310450e9eSJacob Faibussowitsch . PetscPreLoadingOn   - `PETSC_TRUE` if it is CURRENTLY doing preload
224410450e9eSJacob Faibussowitsch . PetscPreLoadIt      - `0` for the first computation (with preloading turned off it is only
224510450e9eSJacob Faibussowitsch                         `0`) `1`  for the second
224610450e9eSJacob Faibussowitsch - PetscPreLoadMax     - number of times it will do the computation, only one when preloading is
224710450e9eSJacob Faibussowitsch                         turned on
224810450e9eSJacob Faibussowitsch 
224910450e9eSJacob Faibussowitsch   The first two variables are available throughout the program, the second two only between the
225010450e9eSJacob Faibussowitsch   `PetscPreLoadBegin()` and `PetscPreLoadEnd()`
22515c6c1daeSBarry Smith 
2252d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()`
22535c6c1daeSBarry Smith M*/
22545c6c1daeSBarry Smith 
22555c6c1daeSBarry Smith /*MC
225610450e9eSJacob Faibussowitsch   PetscPreLoadEnd - End a segment of code that may be preloaded (run twice) to get accurate
225710450e9eSJacob Faibussowitsch   timings
22585c6c1daeSBarry Smith 
22595c6c1daeSBarry Smith   Synopsis:
2260aaa7dc30SBarry Smith   #include <petsclog.h>
22615c6c1daeSBarry Smith   void PetscPreLoadEnd(void);
22625c6c1daeSBarry Smith 
22635c6c1daeSBarry Smith   Not Collective
22645c6c1daeSBarry Smith 
226510450e9eSJacob Faibussowitsch   Example Usage:
22665c6c1daeSBarry Smith .vb
226710450e9eSJacob Faibussowitsch   PetscPreLoadBegin(PETSC_TRUE, "first stage");
226810450e9eSJacob Faibussowitsch   // lines of code
22695c6c1daeSBarry Smith   PetscPreLoadStage("second stage");
227010450e9eSJacob Faibussowitsch   // lines of code
22715c6c1daeSBarry Smith   PetscPreLoadEnd();
22725c6c1daeSBarry Smith .ve
22735c6c1daeSBarry Smith 
2274d1f92df0SBarry Smith   Level: intermediate
2275d1f92df0SBarry Smith 
2276811af0c4SBarry Smith   Note:
2277dd01b7e5SBarry Smith   Only works in C/C++ not Fortran
22785c6c1daeSBarry Smith 
2279d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadStage()`
22805c6c1daeSBarry Smith M*/
22815c6c1daeSBarry Smith 
22825c6c1daeSBarry Smith /*MC
228310450e9eSJacob Faibussowitsch   PetscPreLoadStage - Start a new segment of code to be timed separately to get accurate timings
22845c6c1daeSBarry Smith 
22855c6c1daeSBarry Smith   Synopsis:
2286aaa7dc30SBarry Smith   #include <petsclog.h>
22875c6c1daeSBarry Smith   void PetscPreLoadStage(char *name);
22885c6c1daeSBarry Smith 
22895c6c1daeSBarry Smith   Not Collective
22905c6c1daeSBarry Smith 
229110450e9eSJacob Faibussowitsch   Example Usage:
22925c6c1daeSBarry Smith .vb
229310450e9eSJacob Faibussowitsch   PetscPreLoadBegin(PETSC_TRUE,"first stage");
229410450e9eSJacob Faibussowitsch   // lines of code
22955c6c1daeSBarry Smith   PetscPreLoadStage("second stage");
229610450e9eSJacob Faibussowitsch   // lines of code
22975c6c1daeSBarry Smith   PetscPreLoadEnd();
22985c6c1daeSBarry Smith .ve
22995c6c1daeSBarry Smith 
2300d1f92df0SBarry Smith   Level: intermediate
2301d1f92df0SBarry Smith 
2302811af0c4SBarry Smith   Note:
2303dd01b7e5SBarry Smith   Only works in C/C++ not Fortran
23045c6c1daeSBarry Smith 
2305d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`
23065c6c1daeSBarry Smith M*/
23075c6c1daeSBarry Smith 
2308a4af0ceeSJacob Faibussowitsch   #if PetscDefined(HAVE_DEVICE)
2309a4af0ceeSJacob Faibussowitsch     #include <petsc/private/deviceimpl.h>
23109ffd0706SHong Zhang 
2311cc4c1da9SBarry Smith /*@
2312156b51fbSBarry Smith   PetscLogGpuTime - turn on the logging of GPU time for GPU kernels
2313156b51fbSBarry Smith 
2314811af0c4SBarry Smith   Options Database Key:
2315efa05fe8SBarry Smith . -log_view_gpu_time - provide the GPU times for all events in the `-log_view` output
2316156b51fbSBarry Smith 
2317d1f92df0SBarry Smith   Level: advanced
2318d1f92df0SBarry Smith 
2319156b51fbSBarry Smith   Notes:
232010450e9eSJacob Faibussowitsch   Turning on the timing of the GPU kernels can slow down the entire computation and should only
2321efa05fe8SBarry Smith   be used when studying the performance of individual operations on GPU such as vector operations and
232210450e9eSJacob Faibussowitsch   matrix-vector operations.
2323156b51fbSBarry Smith 
2324efa05fe8SBarry Smith   If this option is not used then times for most of the events in the `-log_view` output will be listed as Nan, indicating the times are not available
2325efa05fe8SBarry Smith 
232610450e9eSJacob Faibussowitsch   This routine should only be called once near the beginning of the program. Once it is started
232710450e9eSJacob Faibussowitsch   it cannot be turned off.
2328156b51fbSBarry Smith 
2329d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogView()`, `PetscLogGpuFlops()`, `PetscLogGpuTimeEnd()`, `PetscLogGpuTimeBegin()`
2330156b51fbSBarry Smith @*/
2331d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogGpuTime(void)
2332d71ae5a4SJacob Faibussowitsch {
2333473903fcSJunchao Zhang   PetscFunctionBegin;
2334473903fcSJunchao Zhang   PetscCheck(petsc_gtime == 0.0, PETSC_COMM_SELF, PETSC_ERR_SUP, "GPU logging has already been turned on");
2335156b51fbSBarry Smith   PetscLogGpuTimeFlag = PETSC_TRUE;
2336473903fcSJunchao Zhang   PetscFunctionReturn(PETSC_SUCCESS);
2337156b51fbSBarry Smith }
2338156b51fbSBarry Smith 
2339cc4c1da9SBarry Smith /*@
23409ffd0706SHong Zhang   PetscLogGpuTimeBegin - Start timer for device
23419ffd0706SHong Zhang 
2342d1f92df0SBarry Smith   Level: intermediate
2343d1f92df0SBarry Smith 
23449ffd0706SHong Zhang   Notes:
23457d766218SJunchao Zhang   When GPU is enabled, the timer is run on the GPU, it is a separate logging of time
234610450e9eSJacob Faibussowitsch   devoted to GPU computations (excluding kernel launch times).
2347811af0c4SBarry Smith 
23487d766218SJunchao Zhang   When GPU is not available, the timer is run on the CPU, it is a separate logging of
234910450e9eSJacob Faibussowitsch   time devoted to GPU computations (including kernel launch times).
2350811af0c4SBarry Smith 
235110450e9eSJacob Faibussowitsch   There is no need to call WaitForCUDA() or WaitForHIP() between `PetscLogGpuTimeBegin()` and
235210450e9eSJacob Faibussowitsch   `PetscLogGpuTimeEnd()`
2353811af0c4SBarry Smith 
235410450e9eSJacob Faibussowitsch   This timer should NOT include times for data transfers between the GPU and CPU, nor setup
235510450e9eSJacob Faibussowitsch   actions such as allocating space.
2356811af0c4SBarry Smith 
235710450e9eSJacob Faibussowitsch   The regular logging captures the time for data transfers and any CPU activities during the
235810450e9eSJacob Faibussowitsch   event. It is used to compute the flop rate on the GPU as it is actively engaged in running a
235910450e9eSJacob Faibussowitsch   kernel.
23609ffd0706SHong Zhang 
23619ffd0706SHong Zhang   Developer Notes:
236210450e9eSJacob Faibussowitsch   The GPU event timer captures the execution time of all the kernels launched in the default
236310450e9eSJacob Faibussowitsch   stream by the CPU between `PetscLogGpuTimeBegin()` and `PetsLogGpuTimeEnd()`.
2364811af0c4SBarry Smith 
236510450e9eSJacob Faibussowitsch   `PetscLogGpuTimeBegin()` and `PetsLogGpuTimeEnd()` insert the begin and end events into the
236610450e9eSJacob Faibussowitsch   default stream (stream 0). The device will record a time stamp for the event when it reaches
236710450e9eSJacob Faibussowitsch   that event in the stream. The function xxxEventSynchronize() is called in
236810450e9eSJacob Faibussowitsch   `PetsLogGpuTimeEnd()` to block CPU execution, but not continued GPU execution, until the
236910450e9eSJacob Faibussowitsch   timer event is recorded.
23709ffd0706SHong Zhang 
2371d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogView()`, `PetscLogGpuFlops()`, `PetscLogGpuTimeEnd()`, `PetscLogGpuTime()`
23729ffd0706SHong Zhang @*/
2373d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogGpuTimeBegin(void)
2374d71ae5a4SJacob Faibussowitsch {
2375b665b14eSToby Isaac   PetscBool isActive;
2376b665b14eSToby Isaac 
23779ffd0706SHong Zhang   PetscFunctionBegin;
2378b665b14eSToby Isaac   PetscCall(PetscLogEventBeginIsActive(&isActive));
2379b665b14eSToby Isaac   if (!isActive || !PetscLogGpuTimeFlag) PetscFunctionReturn(PETSC_SUCCESS);
23807d766218SJunchao Zhang     #if defined(PETSC_HAVE_DEVICE) && !defined(PETSC_HAVE_KOKKOS_WITHOUT_GPU)
23817d766218SJunchao Zhang   {
2382a4af0ceeSJacob Faibussowitsch     PetscDeviceContext dctx;
2383a4af0ceeSJacob Faibussowitsch 
23849566063dSJacob Faibussowitsch     PetscCall(PetscDeviceContextGetCurrentContext(&dctx));
23859566063dSJacob Faibussowitsch     PetscCall(PetscDeviceContextBeginTimer_Internal(dctx));
2386a4af0ceeSJacob Faibussowitsch   }
23877d766218SJunchao Zhang     #else
23887d766218SJunchao Zhang   PetscCall(PetscTimeSubtract(&petsc_gtime));
23897d766218SJunchao Zhang     #endif
23903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
23919ffd0706SHong Zhang }
23929ffd0706SHong Zhang 
2393cc4c1da9SBarry Smith /*@
23949ffd0706SHong Zhang   PetscLogGpuTimeEnd - Stop timer for device
23959ffd0706SHong Zhang 
23969ffd0706SHong Zhang   Level: intermediate
23979ffd0706SHong Zhang 
2398d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogView()`, `PetscLogGpuFlops()`, `PetscLogGpuTimeBegin()`
23999ffd0706SHong Zhang @*/
2400d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogGpuTimeEnd(void)
2401d71ae5a4SJacob Faibussowitsch {
2402b665b14eSToby Isaac   PetscBool isActive;
2403b665b14eSToby Isaac 
24049ffd0706SHong Zhang   PetscFunctionBegin;
2405b665b14eSToby Isaac   PetscCall(PetscLogEventEndIsActive(&isActive));
2406b665b14eSToby Isaac   if (!isActive || !PetscLogGpuTimeFlag) PetscFunctionReturn(PETSC_SUCCESS);
24077d766218SJunchao Zhang     #if defined(PETSC_HAVE_DEVICE) && !defined(PETSC_HAVE_KOKKOS_WITHOUT_GPU)
24087d766218SJunchao Zhang   {
2409a4af0ceeSJacob Faibussowitsch     PetscDeviceContext dctx;
2410a4af0ceeSJacob Faibussowitsch     PetscLogDouble     elapsed;
2411a4af0ceeSJacob Faibussowitsch 
24129566063dSJacob Faibussowitsch     PetscCall(PetscDeviceContextGetCurrentContext(&dctx));
24139566063dSJacob Faibussowitsch     PetscCall(PetscDeviceContextEndTimer_Internal(dctx, &elapsed));
2414a4af0ceeSJacob Faibussowitsch     petsc_gtime += (elapsed / 1000.0);
2415a4af0ceeSJacob Faibussowitsch   }
24167d766218SJunchao Zhang     #else
24177d766218SJunchao Zhang   PetscCall(PetscTimeAdd(&petsc_gtime));
24187d766218SJunchao Zhang     #endif
24193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24209ffd0706SHong Zhang }
2421c708d6e3SStefano Zampini 
24229ffd0706SHong Zhang   #endif /* end of PETSC_HAVE_DEVICE */
24239ffd0706SHong Zhang 
2424cb9ef012SToby Isaac #endif /* PETSC_USE_LOG*/
2425cb9ef012SToby Isaac 
2426dd01b7e5SBarry Smith /* -- Utility functions for logging from Fortran -- */
2427b665b14eSToby Isaac 
2428b665b14eSToby Isaac PETSC_EXTERN PetscErrorCode PetscASend(int count, int datatype)
2429b665b14eSToby Isaac {
2430b665b14eSToby Isaac   PetscFunctionBegin;
2431cb9ef012SToby Isaac #if PetscDefined(USE_LOG)
2432b665b14eSToby Isaac   PetscCall(PetscAddLogDouble(&petsc_send_ct, &petsc_send_ct_th, 1));
2433b665b14eSToby Isaac   #if !defined(MPIUNI_H) && !defined(PETSC_HAVE_BROKEN_RECURSIVE_MACRO)
2434b665b14eSToby Isaac   PetscCall(PetscMPITypeSize(count, MPI_Type_f2c((MPI_Fint)datatype), &petsc_send_len, &petsc_send_len_th));
2435b665b14eSToby Isaac   #endif
2436cb9ef012SToby Isaac #endif
2437b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
2438b665b14eSToby Isaac }
2439b665b14eSToby Isaac 
2440b665b14eSToby Isaac PETSC_EXTERN PetscErrorCode PetscARecv(int count, int datatype)
2441b665b14eSToby Isaac {
2442b665b14eSToby Isaac   PetscFunctionBegin;
2443cb9ef012SToby Isaac #if PetscDefined(USE_LOG)
2444b665b14eSToby Isaac   PetscCall(PetscAddLogDouble(&petsc_recv_ct, &petsc_recv_ct_th, 1));
2445b665b14eSToby Isaac   #if !defined(MPIUNI_H) && !defined(PETSC_HAVE_BROKEN_RECURSIVE_MACRO)
2446b665b14eSToby Isaac   PetscCall(PetscMPITypeSize(count, MPI_Type_f2c((MPI_Fint)datatype), &petsc_recv_len, &petsc_recv_len_th));
2447b665b14eSToby Isaac   #endif
2448cb9ef012SToby Isaac #endif
2449b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
2450b665b14eSToby Isaac }
2451b665b14eSToby Isaac 
2452b665b14eSToby Isaac PETSC_EXTERN PetscErrorCode PetscAReduce(void)
2453b665b14eSToby Isaac {
2454b665b14eSToby Isaac   PetscFunctionBegin;
2455cb9ef012SToby Isaac   if (PetscDefined(USE_LOG)) PetscCall(PetscAddLogDouble(&petsc_allreduce_ct, &petsc_allreduce_ct_th, 1));
2456b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
2457b665b14eSToby Isaac }
2458b665b14eSToby Isaac 
24595c6c1daeSBarry Smith PetscClassId PETSC_LARGEST_CLASSID = PETSC_SMALLEST_CLASSID;
24605c6c1daeSBarry Smith PetscClassId PETSC_OBJECT_CLASSID  = 0;
24615c6c1daeSBarry Smith 
24622611ad71SToby Isaac static PetscBool PetscLogInitializeCalled = PETSC_FALSE;
24632611ad71SToby Isaac 
24642611ad71SToby Isaac PETSC_INTERN PetscErrorCode PetscLogInitialize(void)
24652611ad71SToby Isaac {
24662611ad71SToby Isaac   int stage;
24672611ad71SToby Isaac 
24682611ad71SToby Isaac   PetscFunctionBegin;
24692611ad71SToby Isaac   if (PetscLogInitializeCalled) PetscFunctionReturn(PETSC_SUCCESS);
24702611ad71SToby Isaac   PetscLogInitializeCalled = PETSC_TRUE;
24712611ad71SToby Isaac   if (PetscDefined(USE_LOG)) {
24722611ad71SToby Isaac     /* Setup default logging structures */
24732611ad71SToby Isaac     PetscCall(PetscLogStateCreate(&petsc_log_state));
24742611ad71SToby Isaac     for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
24752611ad71SToby Isaac       if (PetscLogHandlers[i].handler) PetscCall(PetscLogHandlerSetState(PetscLogHandlers[i].handler, petsc_log_state));
24762611ad71SToby Isaac     }
24772611ad71SToby Isaac     PetscCall(PetscLogStateStageRegister(petsc_log_state, "Main Stage", &stage));
24782611ad71SToby Isaac     PetscCall(PetscSpinlockCreate(&PetscLogSpinLock));
24792611ad71SToby Isaac #if defined(PETSC_HAVE_THREADSAFETY)
24802611ad71SToby Isaac     petsc_log_tid = 0;
24812611ad71SToby Isaac     petsc_log_gid = 0;
24822611ad71SToby Isaac #endif
24832611ad71SToby Isaac 
24842611ad71SToby Isaac     /* All processors sync here for more consistent logging */
24852611ad71SToby Isaac     PetscCallMPI(MPI_Barrier(PETSC_COMM_WORLD));
24862611ad71SToby Isaac     PetscCall(PetscTime(&petsc_BaseTime));
24872611ad71SToby Isaac     PetscCall(PetscLogStagePush(stage));
24882611ad71SToby Isaac   }
24892611ad71SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
24902611ad71SToby Isaac }
24912611ad71SToby Isaac 
24922611ad71SToby Isaac PETSC_INTERN PetscErrorCode PetscLogFinalize(void)
24932611ad71SToby Isaac {
24942611ad71SToby Isaac   PetscFunctionBegin;
24952611ad71SToby Isaac   if (PetscDefined(USE_LOG)) {
2496b665b14eSToby Isaac     /* Resetting phase */
2497b665b14eSToby Isaac     // pop remaining stages
2498b665b14eSToby Isaac     if (petsc_log_state) {
2499b665b14eSToby Isaac       while (petsc_log_state->current_stage >= 0) { PetscCall(PetscLogStagePop()); }
2500b665b14eSToby Isaac     }
25012611ad71SToby Isaac     for (int i = 0; i < PETSC_LOG_HANDLER_MAX; i++) PetscCall(PetscLogHandlerDestroy(&PetscLogHandlers[i].handler));
25022611ad71SToby Isaac     PetscCall(PetscArrayzero(PetscLogHandlers, PETSC_LOG_HANDLER_MAX));
25032611ad71SToby Isaac     PetscCall(PetscLogStateDestroy(&petsc_log_state));
25042611ad71SToby Isaac 
25052611ad71SToby Isaac     petsc_TotalFlops         = 0.0;
25062611ad71SToby Isaac     petsc_BaseTime           = 0.0;
25072611ad71SToby Isaac     petsc_TotalFlops         = 0.0;
25082611ad71SToby Isaac     petsc_send_ct            = 0.0;
25092611ad71SToby Isaac     petsc_recv_ct            = 0.0;
25102611ad71SToby Isaac     petsc_send_len           = 0.0;
25112611ad71SToby Isaac     petsc_recv_len           = 0.0;
25122611ad71SToby Isaac     petsc_isend_ct           = 0.0;
25132611ad71SToby Isaac     petsc_irecv_ct           = 0.0;
25142611ad71SToby Isaac     petsc_isend_len          = 0.0;
25152611ad71SToby Isaac     petsc_irecv_len          = 0.0;
25162611ad71SToby Isaac     petsc_wait_ct            = 0.0;
25172611ad71SToby Isaac     petsc_wait_any_ct        = 0.0;
25182611ad71SToby Isaac     petsc_wait_all_ct        = 0.0;
25192611ad71SToby Isaac     petsc_sum_of_waits_ct    = 0.0;
25202611ad71SToby Isaac     petsc_allreduce_ct       = 0.0;
25212611ad71SToby Isaac     petsc_gather_ct          = 0.0;
25222611ad71SToby Isaac     petsc_scatter_ct         = 0.0;
25232611ad71SToby Isaac     petsc_TotalFlops_th      = 0.0;
25242611ad71SToby Isaac     petsc_send_ct_th         = 0.0;
25252611ad71SToby Isaac     petsc_recv_ct_th         = 0.0;
25262611ad71SToby Isaac     petsc_send_len_th        = 0.0;
25272611ad71SToby Isaac     petsc_recv_len_th        = 0.0;
25282611ad71SToby Isaac     petsc_isend_ct_th        = 0.0;
25292611ad71SToby Isaac     petsc_irecv_ct_th        = 0.0;
25302611ad71SToby Isaac     petsc_isend_len_th       = 0.0;
25312611ad71SToby Isaac     petsc_irecv_len_th       = 0.0;
25322611ad71SToby Isaac     petsc_wait_ct_th         = 0.0;
25332611ad71SToby Isaac     petsc_wait_any_ct_th     = 0.0;
25342611ad71SToby Isaac     petsc_wait_all_ct_th     = 0.0;
25352611ad71SToby Isaac     petsc_sum_of_waits_ct_th = 0.0;
25362611ad71SToby Isaac     petsc_allreduce_ct_th    = 0.0;
25372611ad71SToby Isaac     petsc_gather_ct_th       = 0.0;
25382611ad71SToby Isaac     petsc_scatter_ct_th      = 0.0;
25392611ad71SToby Isaac 
25402611ad71SToby Isaac     petsc_ctog_ct    = 0.0;
25412611ad71SToby Isaac     petsc_gtoc_ct    = 0.0;
25422611ad71SToby Isaac     petsc_ctog_sz    = 0.0;
25432611ad71SToby Isaac     petsc_gtoc_sz    = 0.0;
25442611ad71SToby Isaac     petsc_gflops     = 0.0;
25452611ad71SToby Isaac     petsc_gtime      = 0.0;
25462611ad71SToby Isaac     petsc_ctog_ct_th = 0.0;
25472611ad71SToby Isaac     petsc_gtoc_ct_th = 0.0;
25482611ad71SToby Isaac     petsc_ctog_sz_th = 0.0;
25492611ad71SToby Isaac     petsc_gtoc_sz_th = 0.0;
25502611ad71SToby Isaac     petsc_gflops_th  = 0.0;
25512611ad71SToby Isaac     petsc_gtime_th   = 0.0;
25522611ad71SToby Isaac   }
25532611ad71SToby Isaac   PETSC_LARGEST_CLASSID    = PETSC_SMALLEST_CLASSID;
25542611ad71SToby Isaac   PETSC_OBJECT_CLASSID     = 0;
25552611ad71SToby Isaac   PetscLogInitializeCalled = PETSC_FALSE;
25562611ad71SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
25572611ad71SToby Isaac }
25582611ad71SToby Isaac 
2559cc4c1da9SBarry Smith /*@
25605c6c1daeSBarry Smith   PetscClassIdRegister - Registers a new class name for objects and logging operations in an application code.
25615c6c1daeSBarry Smith 
25625c6c1daeSBarry Smith   Not Collective
25635c6c1daeSBarry Smith 
25645c6c1daeSBarry Smith   Input Parameter:
25655c6c1daeSBarry Smith . name - The class name
25665c6c1daeSBarry Smith 
25675c6c1daeSBarry Smith   Output Parameter:
25685c6c1daeSBarry Smith . oclass - The class id or classid
25695c6c1daeSBarry Smith 
25705c6c1daeSBarry Smith   Level: developer
25715c6c1daeSBarry Smith 
2572d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`
25735c6c1daeSBarry Smith @*/
2574d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscClassIdRegister(const char name[], PetscClassId *oclass)
2575d71ae5a4SJacob Faibussowitsch {
25765c6c1daeSBarry Smith   PetscFunctionBegin;
25775c6c1daeSBarry Smith   *oclass = ++PETSC_LARGEST_CLASSID;
25785c6c1daeSBarry Smith #if defined(PETSC_USE_LOG)
2579b665b14eSToby Isaac   {
2580b665b14eSToby Isaac     PetscLogState state;
2581b665b14eSToby Isaac     PetscLogClass logclass;
2582b665b14eSToby Isaac 
2583b665b14eSToby Isaac     PetscCall(PetscLogGetState(&state));
2584b665b14eSToby Isaac     if (state) PetscCall(PetscLogStateClassRegister(state, name, *oclass, &logclass));
2585b665b14eSToby Isaac   }
25865c6c1daeSBarry Smith #endif
25873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
25885c6c1daeSBarry Smith }
2589