xref: /petsc/src/sys/logging/plog.c (revision 5268dc8a1a0f032710726b8d0a0dac288ca10d3a)
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 */
87*5268dc8aSHong Zhang PetscLogDouble petsc_genergy        = 0.0; /* The energy (estimated with power*gtime) consumed on a GPU */
88*5268dc8aSHong Zhang PetscLogDouble petsc_genergy_meter  = 0.0; /* Readings from the energy meter on a GPU */
89c708d6e3SStefano Zampini 
90c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_ctog_ct_th        = 0.0;
91c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_gtoc_ct_th        = 0.0;
92c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_ctog_sz_th        = 0.0;
93c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_gtoc_sz_th        = 0.0;
94c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_ctog_ct_scalar_th = 0.0;
95c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_gtoc_ct_scalar_th = 0.0;
96c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_ctog_sz_scalar_th = 0.0;
97c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_gtoc_sz_scalar_th = 0.0;
98c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_gflops_th         = 0.0;
99c708d6e3SStefano Zampini PETSC_TLS PetscLogDouble petsc_gtime_th          = 0.0;
1002611ad71SToby Isaac 
1012611ad71SToby Isaac PetscBool PetscLogMemory = PETSC_FALSE;
1022611ad71SToby Isaac PetscBool PetscLogSyncOn = PETSC_FALSE;
1032611ad71SToby Isaac 
1042611ad71SToby Isaac PetscBool PetscLogGpuTimeFlag        = PETSC_FALSE;
105*5268dc8aSHong Zhang PetscBool PetscLogGpuEnergyFlag      = PETSC_FALSE;
106*5268dc8aSHong Zhang PetscBool PetscLogGpuEnergyMeterFlag = PETSC_FALSE;
1072611ad71SToby Isaac 
1080bc1c2e8SToby Isaac PetscInt PetscLogNumViewersCreated   = 0;
1090bc1c2e8SToby Isaac PetscInt PetscLogNumViewersDestroyed = 0;
1100bc1c2e8SToby Isaac 
1112611ad71SToby Isaac PetscLogState petsc_log_state = NULL;
1122611ad71SToby Isaac 
11310dd146fSPierre Jolivet #define PETSC_LOG_HANDLER_HOT_BLANK {NULL, NULL, NULL, NULL, NULL, NULL}
1142611ad71SToby Isaac 
1152611ad71SToby Isaac PetscLogHandlerHot PetscLogHandlers[PETSC_LOG_HANDLER_MAX] = {
1162611ad71SToby Isaac   PETSC_LOG_HANDLER_HOT_BLANK,
1172611ad71SToby Isaac   PETSC_LOG_HANDLER_HOT_BLANK,
1182611ad71SToby Isaac   PETSC_LOG_HANDLER_HOT_BLANK,
1192611ad71SToby Isaac   PETSC_LOG_HANDLER_HOT_BLANK,
1202611ad71SToby Isaac };
1212611ad71SToby Isaac 
1222611ad71SToby Isaac #undef PETSC_LOG_HANDLERS_HOT_BLANK
1232611ad71SToby Isaac 
1242611ad71SToby Isaac #if defined(PETSC_USE_LOG)
1252611ad71SToby Isaac   #include <../src/sys/logging/handler/impls/default/logdefault.h>
126c708d6e3SStefano Zampini 
127c708d6e3SStefano Zampini   #if defined(PETSC_HAVE_THREADSAFETY)
128c708d6e3SStefano Zampini PetscErrorCode PetscAddLogDouble(PetscLogDouble *tot, PetscLogDouble *tot_th, PetscLogDouble tmp)
129c708d6e3SStefano Zampini {
130c708d6e3SStefano Zampini   *tot_th += tmp;
1313ba16761SJacob Faibussowitsch   PetscCall(PetscSpinlockLock(&PetscLogSpinLock));
132c708d6e3SStefano Zampini   *tot += tmp;
1333ba16761SJacob Faibussowitsch   PetscCall(PetscSpinlockUnlock(&PetscLogSpinLock));
1343ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
135c708d6e3SStefano Zampini }
136c708d6e3SStefano Zampini 
137c708d6e3SStefano Zampini PetscErrorCode PetscAddLogDoubleCnt(PetscLogDouble *cnt, PetscLogDouble *tot, PetscLogDouble *cnt_th, PetscLogDouble *tot_th, PetscLogDouble tmp)
138c708d6e3SStefano Zampini {
139c708d6e3SStefano Zampini   *cnt_th = *cnt_th + 1;
140c708d6e3SStefano Zampini   *tot_th += tmp;
1413ba16761SJacob Faibussowitsch   PetscCall(PetscSpinlockLock(&PetscLogSpinLock));
14257508eceSPierre Jolivet   *tot += (PetscLogDouble)tmp;
143c708d6e3SStefano Zampini   *cnt += *cnt + 1;
1443ba16761SJacob Faibussowitsch   PetscCall(PetscSpinlockUnlock(&PetscLogSpinLock));
1453ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
146c708d6e3SStefano Zampini }
147c708d6e3SStefano Zampini 
148bec0b493Shannah_mairs   #endif
1495c6c1daeSBarry Smith 
15053e0a2f3SToby Isaac static PetscErrorCode PetscLogTryGetHandler(PetscLogHandlerType type, PetscLogHandler *handler)
15153e0a2f3SToby Isaac {
15253e0a2f3SToby Isaac   PetscFunctionBegin;
15353e0a2f3SToby Isaac   PetscAssertPointer(handler, 2);
15453e0a2f3SToby Isaac   *handler = NULL;
15553e0a2f3SToby Isaac   for (int i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
15653e0a2f3SToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
15753e0a2f3SToby Isaac     if (h) {
15853e0a2f3SToby Isaac       PetscBool match;
15953e0a2f3SToby Isaac 
16053e0a2f3SToby Isaac       PetscCall(PetscObjectTypeCompare((PetscObject)h, type, &match));
16153e0a2f3SToby Isaac       if (match) {
16253e0a2f3SToby Isaac         *handler = PetscLogHandlers[i].handler;
16353e0a2f3SToby Isaac         PetscFunctionReturn(PETSC_SUCCESS);
16453e0a2f3SToby Isaac       }
16553e0a2f3SToby Isaac     }
16653e0a2f3SToby Isaac   }
16753e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
16853e0a2f3SToby Isaac }
16953e0a2f3SToby Isaac 
17053e0a2f3SToby Isaac /*@
171b665b14eSToby Isaac   PetscLogGetDefaultHandler - Get the default log handler if it is running.
172b665b14eSToby Isaac 
173b665b14eSToby Isaac   Not collective
174b665b14eSToby Isaac 
175b665b14eSToby Isaac   Output Parameter:
176b665b14eSToby Isaac . handler - the default `PetscLogHandler`, or `NULL` if it is not running.
177b665b14eSToby Isaac 
178b665b14eSToby Isaac   Level: developer
179b665b14eSToby Isaac 
180b665b14eSToby Isaac   Notes:
181b665b14eSToby Isaac   The default handler is started with `PetscLogDefaultBegin()`,
182b665b14eSToby Isaac   if the options flags `-log_all` or `-log_view` is given without arguments,
183b665b14eSToby Isaac   or for `-log_view :output:format` if `format` is not `ascii_xml` or `ascii_flamegraph`.
184b665b14eSToby Isaac 
185b665b14eSToby Isaac .seealso: [](ch_profiling)
186b665b14eSToby Isaac @*/
187b665b14eSToby Isaac PetscErrorCode PetscLogGetDefaultHandler(PetscLogHandler *handler)
188b665b14eSToby Isaac {
189b665b14eSToby Isaac   PetscFunctionBegin;
190294de794SToby Isaac   PetscCall(PetscLogTryGetHandler(PETSCLOGHANDLERDEFAULT, handler));
191b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
192b665b14eSToby Isaac }
193b665b14eSToby Isaac 
194b665b14eSToby Isaac static PetscErrorCode PetscLogGetHandler(PetscLogHandlerType type, PetscLogHandler *handler)
195b665b14eSToby Isaac {
196b665b14eSToby Isaac   PetscFunctionBegin;
197b665b14eSToby Isaac   PetscAssertPointer(handler, 2);
198b665b14eSToby Isaac   PetscCall(PetscLogTryGetHandler(type, handler));
1991943a749SToby Isaac   PetscCheck(*handler != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "A PetscLogHandler of type %s has not been started.", type);
200b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
201b665b14eSToby Isaac }
202b665b14eSToby Isaac 
203b665b14eSToby Isaac /*@
20453e0a2f3SToby Isaac   PetscLogGetState - Get the `PetscLogState` for PETSc's global logging, used
20553e0a2f3SToby Isaac   by all default log handlers (`PetscLogDefaultBegin()`,
20653e0a2f3SToby Isaac   `PetscLogNestedBegin()`, `PetscLogTraceBegin()`, `PetscLogMPEBegin()`,
20753e0a2f3SToby Isaac   `PetscLogPerfstubsBegin()`).
20853e0a2f3SToby Isaac 
20953e0a2f3SToby Isaac   Collective on `PETSC_COMM_WORLD`
21053e0a2f3SToby Isaac 
21153e0a2f3SToby Isaac   Output Parameter:
212b665b14eSToby Isaac . state - The `PetscLogState` changed by registrations (such as
213b665b14eSToby Isaac           `PetscLogEventRegister()`) and actions (such as `PetscLogEventBegin()` or
214b8004f34SBarry Smith           `PetscLogStagePush()`), or `NULL` if logging is not active
21553e0a2f3SToby Isaac 
21653e0a2f3SToby Isaac   Level: developer
21753e0a2f3SToby Isaac 
21853e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogState`
21953e0a2f3SToby Isaac @*/
22053e0a2f3SToby Isaac PetscErrorCode PetscLogGetState(PetscLogState *state)
22153e0a2f3SToby Isaac {
22253e0a2f3SToby Isaac   PetscFunctionBegin;
22353e0a2f3SToby Isaac   PetscAssertPointer(state, 1);
22453e0a2f3SToby Isaac   *state = petsc_log_state;
22553e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
22653e0a2f3SToby Isaac }
22753e0a2f3SToby Isaac 
22853e0a2f3SToby Isaac static PetscErrorCode PetscLogHandlerCopyToHot(PetscLogHandler h, PetscLogHandlerHot *hot)
22953e0a2f3SToby Isaac {
23053e0a2f3SToby Isaac   PetscFunctionBegin;
23153e0a2f3SToby Isaac   hot->handler       = h;
23253e0a2f3SToby Isaac   hot->eventBegin    = h->ops->eventbegin;
23353e0a2f3SToby Isaac   hot->eventEnd      = h->ops->eventend;
23453e0a2f3SToby Isaac   hot->eventSync     = h->ops->eventsync;
23553e0a2f3SToby Isaac   hot->objectCreate  = h->ops->objectcreate;
23653e0a2f3SToby Isaac   hot->objectDestroy = h->ops->objectdestroy;
23753e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
23853e0a2f3SToby Isaac }
23953e0a2f3SToby Isaac 
24053e0a2f3SToby Isaac /*@
24153e0a2f3SToby Isaac   PetscLogHandlerStart - Connect a log handler to PETSc's global logging stream and state.
24253e0a2f3SToby Isaac 
24353e0a2f3SToby Isaac   Logically collective
24453e0a2f3SToby Isaac 
24553e0a2f3SToby Isaac   Input Parameters:
24653e0a2f3SToby Isaac . h - a `PetscLogHandler`
24753e0a2f3SToby Isaac 
24853e0a2f3SToby Isaac   Level: developer
24953e0a2f3SToby Isaac 
25053e0a2f3SToby Isaac   Notes:
25153e0a2f3SToby Isaac   Users should only need this if they create their own log handlers: handlers that are started
25253e0a2f3SToby Isaac   from the command line (such as `-log_view` and `-log_trace`) or from a function like
25353e0a2f3SToby Isaac   `PetscLogNestedBegin()` will automatically be started.
25453e0a2f3SToby Isaac 
25553e0a2f3SToby Isaac   There is a limit of `PESC_LOG_HANDLER_MAX` handlers that can be active at one time.
25653e0a2f3SToby Isaac 
25753e0a2f3SToby Isaac   To disconnect a handler from the global stream call `PetscLogHandlerStop()`.
25853e0a2f3SToby Isaac 
25953e0a2f3SToby Isaac   When a log handler is started, stages that have already been pushed with `PetscLogStagePush()`,
26053e0a2f3SToby Isaac   will be pushed for the new log handler, but it will not be informed of any events that are
26153e0a2f3SToby Isaac   in progress.  It is recommended to start any user-defined log handlers immediately following
262b8004f34SBarry Smith   `PetscInitialize()`  before any user-defined stages are pushed.
26353e0a2f3SToby Isaac 
264b8004f34SBarry Smith .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogState`, `PetscLogHandlerStop()`, `PetscInitialize()`
26553e0a2f3SToby Isaac @*/
26653e0a2f3SToby Isaac PetscErrorCode PetscLogHandlerStart(PetscLogHandler h)
26753e0a2f3SToby Isaac {
26853e0a2f3SToby Isaac   PetscFunctionBegin;
26953e0a2f3SToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
27053e0a2f3SToby Isaac     if (PetscLogHandlers[i].handler == h) PetscFunctionReturn(PETSC_SUCCESS);
27153e0a2f3SToby Isaac   }
27253e0a2f3SToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
27353e0a2f3SToby Isaac     if (PetscLogHandlers[i].handler == NULL) {
27453e0a2f3SToby Isaac       PetscCall(PetscObjectReference((PetscObject)h));
27553e0a2f3SToby Isaac       PetscCall(PetscLogHandlerCopyToHot(h, &PetscLogHandlers[i]));
27653e0a2f3SToby Isaac       if (petsc_log_state) {
27753e0a2f3SToby Isaac         PetscLogStage stack_height;
27853e0a2f3SToby Isaac         PetscIntStack orig_stack, temp_stack;
27953e0a2f3SToby Isaac 
28053e0a2f3SToby Isaac         PetscCall(PetscLogHandlerSetState(h, petsc_log_state));
28153e0a2f3SToby Isaac         stack_height = petsc_log_state->stage_stack->top + 1;
28253e0a2f3SToby Isaac         PetscCall(PetscIntStackCreate(&temp_stack));
28353e0a2f3SToby Isaac         orig_stack                     = petsc_log_state->stage_stack;
28453e0a2f3SToby Isaac         petsc_log_state->stage_stack   = temp_stack;
28553e0a2f3SToby Isaac         petsc_log_state->current_stage = -1;
28653e0a2f3SToby Isaac         for (int s = 0; s < stack_height; s++) {
287835f2295SStefano Zampini           PetscLogStage stage = orig_stack->stack[s];
28853e0a2f3SToby Isaac           PetscCall(PetscLogHandlerStagePush(h, stage));
28953e0a2f3SToby Isaac           PetscCall(PetscIntStackPush(temp_stack, stage));
29053e0a2f3SToby Isaac           petsc_log_state->current_stage = stage;
29153e0a2f3SToby Isaac         }
29253e0a2f3SToby Isaac         PetscCall(PetscIntStackDestroy(temp_stack));
29353e0a2f3SToby Isaac         petsc_log_state->stage_stack = orig_stack;
29453e0a2f3SToby Isaac       }
29553e0a2f3SToby Isaac       PetscFunctionReturn(PETSC_SUCCESS);
29653e0a2f3SToby Isaac     }
29753e0a2f3SToby Isaac   }
29853e0a2f3SToby Isaac   SETERRQ(PetscObjectComm((PetscObject)h), PETSC_ERR_ARG_WRONGSTATE, "%d log handlers already started, cannot start another", PETSC_LOG_HANDLER_MAX);
29953e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
30053e0a2f3SToby Isaac }
30153e0a2f3SToby Isaac 
30253e0a2f3SToby Isaac /*@
30353e0a2f3SToby Isaac   PetscLogHandlerStop - Disconnect a log handler from PETSc's global logging stream.
30453e0a2f3SToby Isaac 
30553e0a2f3SToby Isaac   Logically collective
30653e0a2f3SToby Isaac 
30753e0a2f3SToby Isaac   Input Parameters:
30853e0a2f3SToby Isaac . h - a `PetscLogHandler`
30953e0a2f3SToby Isaac 
31053e0a2f3SToby Isaac   Level: developer
31153e0a2f3SToby Isaac 
31253e0a2f3SToby Isaac   Note:
31353e0a2f3SToby Isaac   After `PetscLogHandlerStop()`, the handler can still access the global logging state
31453e0a2f3SToby Isaac   with `PetscLogHandlerGetState()`, so that it can access the registry when post-processing
31553e0a2f3SToby Isaac   (for instance, in `PetscLogHandlerView()`),
31653e0a2f3SToby Isaac 
31753e0a2f3SToby Isaac   When a log handler is stopped, the remaining stages will be popped before it is
31853e0a2f3SToby Isaac   disconnected from the log stream.
31953e0a2f3SToby Isaac 
32053e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogState`, `PetscLogHandlerStart()`
32153e0a2f3SToby Isaac @*/
32253e0a2f3SToby Isaac PetscErrorCode PetscLogHandlerStop(PetscLogHandler h)
32353e0a2f3SToby Isaac {
32453e0a2f3SToby Isaac   PetscFunctionBegin;
32553e0a2f3SToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
32653e0a2f3SToby Isaac     if (PetscLogHandlers[i].handler == h) {
32753e0a2f3SToby Isaac       if (petsc_log_state) {
32853e0a2f3SToby Isaac         PetscLogState state;
32953e0a2f3SToby Isaac         PetscLogStage stack_height;
33053e0a2f3SToby Isaac         PetscIntStack orig_stack, temp_stack;
33153e0a2f3SToby Isaac 
33253e0a2f3SToby Isaac         PetscCall(PetscLogHandlerGetState(h, &state));
33353e0a2f3SToby Isaac         PetscCheck(state == petsc_log_state, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONGSTATE, "Called PetscLogHandlerStop() for a PetscLogHander that was not started.");
33453e0a2f3SToby Isaac         stack_height = petsc_log_state->stage_stack->top + 1;
33553e0a2f3SToby Isaac         PetscCall(PetscIntStackCreate(&temp_stack));
33653e0a2f3SToby Isaac         orig_stack                   = petsc_log_state->stage_stack;
33753e0a2f3SToby Isaac         petsc_log_state->stage_stack = temp_stack;
33853e0a2f3SToby Isaac         for (int s = 0; s < stack_height; s++) {
339835f2295SStefano Zampini           PetscLogStage stage = orig_stack->stack[s];
34053e0a2f3SToby Isaac 
34153e0a2f3SToby Isaac           PetscCall(PetscIntStackPush(temp_stack, stage));
34253e0a2f3SToby Isaac         }
34353e0a2f3SToby Isaac         for (int s = 0; s < stack_height; s++) {
34453e0a2f3SToby Isaac           PetscLogStage stage;
34553e0a2f3SToby Isaac           PetscBool     empty;
34653e0a2f3SToby Isaac 
34753e0a2f3SToby Isaac           PetscCall(PetscIntStackPop(temp_stack, &stage));
34853e0a2f3SToby Isaac           PetscCall(PetscIntStackEmpty(temp_stack, &empty));
349ac530a7eSPierre Jolivet           if (!empty) PetscCall(PetscIntStackTop(temp_stack, &petsc_log_state->current_stage));
350ac530a7eSPierre Jolivet           else petsc_log_state->current_stage = -1;
35153e0a2f3SToby Isaac           PetscCall(PetscLogHandlerStagePop(h, stage));
35253e0a2f3SToby Isaac         }
35353e0a2f3SToby Isaac         PetscCall(PetscIntStackDestroy(temp_stack));
35453e0a2f3SToby Isaac         petsc_log_state->stage_stack = orig_stack;
35553e0a2f3SToby Isaac         PetscCall(PetscIntStackTop(petsc_log_state->stage_stack, &petsc_log_state->current_stage));
35653e0a2f3SToby Isaac       }
35753e0a2f3SToby Isaac       PetscCall(PetscArrayzero(&PetscLogHandlers[i], 1));
35853e0a2f3SToby Isaac       PetscCall(PetscObjectDereference((PetscObject)h));
35953e0a2f3SToby Isaac     }
36053e0a2f3SToby Isaac   }
36153e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
36253e0a2f3SToby Isaac }
3635c6c1daeSBarry Smith 
364cc4c1da9SBarry Smith /*@
3650b4b7b1cSBarry Smith   PetscLogIsActive - Check if logging (profiling) is currently in progress.
3664dd65854SConnor Ward 
3674dd65854SConnor Ward   Not Collective
3684dd65854SConnor Ward 
3694dd65854SConnor Ward   Output Parameter:
370811af0c4SBarry Smith . isActive - `PETSC_TRUE` if logging is in progress, `PETSC_FALSE` otherwise
3714dd65854SConnor Ward 
3724dd65854SConnor Ward   Level: beginner
3734dd65854SConnor Ward 
374b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogDefaultBegin()`
3754dd65854SConnor Ward @*/
376d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogIsActive(PetscBool *isActive)
377d71ae5a4SJacob Faibussowitsch {
3784dd65854SConnor Ward   PetscFunctionBegin;
379b665b14eSToby Isaac   *isActive = PETSC_FALSE;
380b665b14eSToby Isaac   if (petsc_log_state) {
381b665b14eSToby Isaac     for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
382b665b14eSToby Isaac       if (PetscLogHandlers[i].handler) {
383b665b14eSToby Isaac         *isActive = PETSC_TRUE;
384b665b14eSToby Isaac         PetscFunctionReturn(PETSC_SUCCESS);
385b665b14eSToby Isaac       }
386b665b14eSToby Isaac     }
387b665b14eSToby Isaac   }
388b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
389b665b14eSToby Isaac }
390b665b14eSToby Isaac 
391b665b14eSToby Isaac PETSC_UNUSED static PetscErrorCode PetscLogEventBeginIsActive(PetscBool *isActive)
392b665b14eSToby Isaac {
393b665b14eSToby Isaac   PetscFunctionBegin;
394b665b14eSToby Isaac   *isActive = PETSC_FALSE;
395b665b14eSToby Isaac   if (petsc_log_state) {
396b665b14eSToby Isaac     for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
397b665b14eSToby Isaac       if (PetscLogHandlers[i].eventBegin) {
398b665b14eSToby Isaac         *isActive = PETSC_TRUE;
399b665b14eSToby Isaac         PetscFunctionReturn(PETSC_SUCCESS);
400b665b14eSToby Isaac       }
401b665b14eSToby Isaac     }
402b665b14eSToby Isaac   }
403b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
404b665b14eSToby Isaac }
405b665b14eSToby Isaac 
406b665b14eSToby Isaac PETSC_UNUSED static PetscErrorCode PetscLogEventEndIsActive(PetscBool *isActive)
407b665b14eSToby Isaac {
408b665b14eSToby Isaac   PetscFunctionBegin;
409b665b14eSToby Isaac   *isActive = PETSC_FALSE;
410b665b14eSToby Isaac   if (petsc_log_state) {
411b665b14eSToby Isaac     for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
412b665b14eSToby Isaac       if (PetscLogHandlers[i].eventEnd) {
413b665b14eSToby Isaac         *isActive = PETSC_TRUE;
414b665b14eSToby Isaac         PetscFunctionReturn(PETSC_SUCCESS);
415b665b14eSToby Isaac       }
416b665b14eSToby Isaac     }
417b665b14eSToby Isaac   }
4183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4194dd65854SConnor Ward }
4204dd65854SConnor Ward 
42161cc7448SToby Isaac PETSC_INTERN PetscErrorCode PetscLogTypeBegin(PetscLogHandlerType type)
42253e0a2f3SToby Isaac {
42353e0a2f3SToby Isaac   PetscLogHandler handler;
42453e0a2f3SToby Isaac 
42553e0a2f3SToby Isaac   PetscFunctionBegin;
42653e0a2f3SToby Isaac   PetscCall(PetscLogTryGetHandler(type, &handler));
42753e0a2f3SToby Isaac   if (handler) PetscFunctionReturn(PETSC_SUCCESS);
42853e0a2f3SToby Isaac   PetscCall(PetscLogHandlerCreate(PETSC_COMM_WORLD, &handler));
42953e0a2f3SToby Isaac   PetscCall(PetscLogHandlerSetType(handler, type));
43053e0a2f3SToby Isaac   PetscCall(PetscLogHandlerStart(handler));
43153e0a2f3SToby Isaac   PetscCall(PetscLogHandlerDestroy(&handler));
43253e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
43353e0a2f3SToby Isaac }
43453e0a2f3SToby Isaac 
435cc4c1da9SBarry Smith /*@
4360b4b7b1cSBarry Smith   PetscLogDefaultBegin - Turns on logging (profiling) of PETSc code using the default log handler (profiler). This logs time, flop
4370b4b7b1cSBarry Smith   rates, and object creation and should not slow programs down too much.
4385c6c1daeSBarry Smith 
4398f14a041SBarry Smith   Logically Collective on `PETSC_COMM_WORLD`
4405c6c1daeSBarry Smith 
441811af0c4SBarry Smith   Options Database Key:
4420b4b7b1cSBarry Smith . -log_view [viewertype:filename:viewerformat] - Prints summary of flop and timing (profiling) information to the
4430b4b7b1cSBarry Smith                                                  screen (for PETSc configured with `--with-log=1` (which is the default)).
4440b4b7b1cSBarry Smith                                                  This option must be provided before `PetscInitialize()`.
4455c6c1daeSBarry Smith 
44610450e9eSJacob Faibussowitsch   Example Usage:
4475c6c1daeSBarry Smith .vb
4485c6c1daeSBarry Smith       PetscInitialize(...);
449bb1d7374SBarry Smith       PetscLogDefaultBegin();
4505c6c1daeSBarry Smith        ... code ...
4515c6c1daeSBarry Smith       PetscLogView(viewer); or PetscLogDump();
4525c6c1daeSBarry Smith       PetscFinalize();
4535c6c1daeSBarry Smith .ve
4545c6c1daeSBarry Smith 
455d1f92df0SBarry Smith   Level: advanced
456d1f92df0SBarry Smith 
4570b4b7b1cSBarry Smith   Notes:
458811af0c4SBarry Smith   `PetscLogView()` or `PetscLogDump()` actually cause the printing of
4595c6c1daeSBarry Smith   the logging information.
4605c6c1daeSBarry Smith 
4610b4b7b1cSBarry Smith   This routine may be called more than once.
4620b4b7b1cSBarry Smith 
4630b4b7b1cSBarry Smith   To provide the `-log_view` option in your source code you must call  PetscCall(PetscOptionsSetValue(NULL, "-log_view", NULL));
4640b4b7b1cSBarry Smith   before you call `PetscInitialize()`
4650b4b7b1cSBarry Smith 
466b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogDump()`, `PetscLogView()`, `PetscLogTraceBegin()`
4675c6c1daeSBarry Smith @*/
468d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogDefaultBegin(void)
469d71ae5a4SJacob Faibussowitsch {
4705c6c1daeSBarry Smith   PetscFunctionBegin;
471294de794SToby Isaac   PetscCall(PetscLogTypeBegin(PETSCLOGHANDLERDEFAULT));
4723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4735c6c1daeSBarry Smith }
4745c6c1daeSBarry Smith 
4755c6c1daeSBarry Smith /*@C
476b665b14eSToby Isaac   PetscLogTraceBegin - Begins trace logging.  Every time a PETSc event
4775c6c1daeSBarry Smith   begins or ends, the event name is printed.
4785c6c1daeSBarry Smith 
479cc4c1da9SBarry Smith   Logically Collective on `PETSC_COMM_WORLD`, No Fortran Support
4805c6c1daeSBarry Smith 
4815c6c1daeSBarry Smith   Input Parameter:
4825c6c1daeSBarry Smith . file - The file to print trace in (e.g. stdout)
4835c6c1daeSBarry Smith 
4845c6c1daeSBarry Smith   Options Database Key:
485b665b14eSToby Isaac . -log_trace [filename] - Begins `PetscLogTraceBegin()`
4865c6c1daeSBarry Smith 
487d1f92df0SBarry Smith   Level: intermediate
488d1f92df0SBarry Smith 
4895c6c1daeSBarry Smith   Notes:
490811af0c4SBarry Smith   `PetscLogTraceBegin()` prints the processor number, the execution time (sec),
4915c6c1daeSBarry Smith   then "Event begin:" or "Event end:" followed by the event name.
4925c6c1daeSBarry Smith 
493811af0c4SBarry Smith   `PetscLogTraceBegin()` allows tracing of all PETSc calls, which is useful
4945c6c1daeSBarry Smith   to determine where a program is hanging without running in the
4955c6c1daeSBarry Smith   debugger.  Can be used in conjunction with the -info option.
4965c6c1daeSBarry Smith 
497b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogDump()`, `PetscLogView()`, `PetscLogDefaultBegin()`
4985c6c1daeSBarry Smith @*/
499d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogTraceBegin(FILE *file)
500d71ae5a4SJacob Faibussowitsch {
501b665b14eSToby Isaac   PetscLogHandler handler;
5024d86920dSPierre Jolivet 
5035c6c1daeSBarry Smith   PetscFunctionBegin;
504294de794SToby Isaac   PetscCall(PetscLogTryGetHandler(PETSCLOGHANDLERTRACE, &handler));
505b665b14eSToby Isaac   if (handler) PetscFunctionReturn(PETSC_SUCCESS);
506b665b14eSToby Isaac   PetscCall(PetscLogHandlerCreateTrace(PETSC_COMM_WORLD, file, &handler));
507b665b14eSToby Isaac   PetscCall(PetscLogHandlerStart(handler));
508b665b14eSToby Isaac   PetscCall(PetscLogHandlerDestroy(&handler));
509b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
510b665b14eSToby Isaac }
511a297a907SKarl Rupp 
512b665b14eSToby Isaac PETSC_INTERN PetscErrorCode PetscLogHandlerCreate_Nested(MPI_Comm, PetscLogHandler *);
513b665b14eSToby Isaac 
514cc4c1da9SBarry Smith /*@
515b665b14eSToby Isaac   PetscLogNestedBegin - Turns on nested logging of objects and events. This logs flop
516b665b14eSToby Isaac   rates and object creation and should not slow programs down too much.
517b665b14eSToby Isaac 
518cc4c1da9SBarry Smith   Logically Collective on `PETSC_COMM_WORLD`, No Fortran Support
519b665b14eSToby Isaac 
520b665b14eSToby Isaac   Options Database Keys:
521b665b14eSToby Isaac . -log_view :filename.xml:ascii_xml - Prints an XML summary of flop and timing information to the file
522b665b14eSToby Isaac 
523b665b14eSToby Isaac   Example Usage:
524b665b14eSToby Isaac .vb
525b665b14eSToby Isaac       PetscInitialize(...);
526b665b14eSToby Isaac       PetscLogNestedBegin();
527b665b14eSToby Isaac        ... code ...
528b665b14eSToby Isaac       PetscLogView(viewer);
529b665b14eSToby Isaac       PetscFinalize();
530b665b14eSToby Isaac .ve
531b665b14eSToby Isaac 
532b665b14eSToby Isaac   Level: advanced
533b665b14eSToby Isaac 
534b665b14eSToby Isaac .seealso: `PetscLogDump()`, `PetscLogView()`, `PetscLogTraceBegin()`, `PetscLogDefaultBegin()`
535b665b14eSToby Isaac @*/
536b665b14eSToby Isaac PetscErrorCode PetscLogNestedBegin(void)
537b665b14eSToby Isaac {
538b665b14eSToby Isaac   PetscFunctionBegin;
539294de794SToby Isaac   PetscCall(PetscLogTypeBegin(PETSCLOGHANDLERNESTED));
5403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5415c6c1daeSBarry Smith }
5425c6c1daeSBarry Smith 
54353e0a2f3SToby Isaac /*@C
54453e0a2f3SToby Isaac   PetscLogLegacyCallbacksBegin - Create and start a log handler from callbacks
54553e0a2f3SToby Isaac   matching the now deprecated function pointers `PetscLogPLB`, `PetscLogPLE`,
54653e0a2f3SToby Isaac   `PetscLogPHC`, `PetscLogPHD`.
54753e0a2f3SToby Isaac 
5488f14a041SBarry Smith   Logically Collective on `PETSC_COMM_WORLD`
54953e0a2f3SToby Isaac 
55053e0a2f3SToby Isaac   Input Parameters:
55153e0a2f3SToby Isaac + PetscLogPLB - A callback that will be executed by `PetscLogEventBegin()` (or `NULL`)
55253e0a2f3SToby Isaac . PetscLogPLE - A callback that will be executed by `PetscLogEventEnd()` (or `NULL`)
55353e0a2f3SToby Isaac . PetscLogPHC - A callback that will be executed by `PetscLogObjectCreate()` (or `NULL`)
55453e0a2f3SToby Isaac - PetscLogPHD - A callback that will be executed by `PetscLogObjectCreate()` (or `NULL`)
55553e0a2f3SToby Isaac 
55653e0a2f3SToby Isaac   Calling sequence of `PetscLogPLB`:
55753e0a2f3SToby Isaac + e  - a `PetscLogEvent` that is beginning
55853e0a2f3SToby Isaac . _i - deprecated, unused
55953e0a2f3SToby Isaac . o1 - a `PetscObject` associated with `e` (or `NULL`)
56053e0a2f3SToby Isaac . o2 - a `PetscObject` associated with `e` (or `NULL`)
56153e0a2f3SToby Isaac . o3 - a `PetscObject` associated with `e` (or `NULL`)
56253e0a2f3SToby Isaac - o4 - a `PetscObject` associated with `e` (or `NULL`)
56353e0a2f3SToby Isaac 
56453e0a2f3SToby Isaac   Calling sequence of `PetscLogPLE`:
56553e0a2f3SToby Isaac + e  - a `PetscLogEvent` that is beginning
56653e0a2f3SToby Isaac . _i - deprecated, unused
56753e0a2f3SToby Isaac . o1 - a `PetscObject` associated with `e` (or `NULL`)
56853e0a2f3SToby Isaac . o2 - a `PetscObject` associated with `e` (or `NULL`)
56953e0a2f3SToby Isaac . o3 - a `PetscObject` associated with `e` (or `NULL`)
57053e0a2f3SToby Isaac - o4 - a `PetscObject` associated with `e` (or `NULL`)
57153e0a2f3SToby Isaac 
57253e0a2f3SToby Isaac   Calling sequence of `PetscLogPHC`:
57353e0a2f3SToby Isaac . o - a `PetscObject` that has just been created
57453e0a2f3SToby Isaac 
57553e0a2f3SToby Isaac   Calling sequence of `PetscLogPHD`:
57653e0a2f3SToby Isaac . o - a `PetscObject` that is about to be destroyed
57753e0a2f3SToby Isaac 
57853e0a2f3SToby Isaac   Level: advanced
57953e0a2f3SToby Isaac 
58053e0a2f3SToby Isaac   Notes:
58153e0a2f3SToby Isaac   This is for transitioning from the deprecated function `PetscLogSet()` and should not be used in new code.
58253e0a2f3SToby Isaac 
58353e0a2f3SToby Isaac   This should help migrate external log handlers to use `PetscLogHandler`, but
58453e0a2f3SToby Isaac   callbacks that depend on the deprecated `PetscLogStage` datatype will have to be
58553e0a2f3SToby Isaac   updated.
58653e0a2f3SToby Isaac 
58753e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogHandlerStart()`, `PetscLogState`
58853e0a2f3SToby Isaac @*/
58953e0a2f3SToby 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))
59053e0a2f3SToby Isaac {
59153e0a2f3SToby Isaac   PetscLogHandler handler;
59253e0a2f3SToby Isaac 
59353e0a2f3SToby Isaac   PetscFunctionBegin;
59453e0a2f3SToby Isaac   PetscCall(PetscLogHandlerCreateLegacy(PETSC_COMM_WORLD, PetscLogPLB, PetscLogPLE, PetscLogPHC, PetscLogPHD, &handler));
59553e0a2f3SToby Isaac   PetscCall(PetscLogHandlerStart(handler));
59653e0a2f3SToby Isaac   PetscCall(PetscLogHandlerDestroy(&handler));
59753e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
59853e0a2f3SToby Isaac }
59953e0a2f3SToby Isaac 
6002611ad71SToby Isaac   #if defined(PETSC_HAVE_MPE)
6012611ad71SToby Isaac     #include <mpe.h>
6022611ad71SToby Isaac static PetscBool PetscBeganMPE = PETSC_FALSE;
6032611ad71SToby Isaac   #endif
6042611ad71SToby Isaac 
6052611ad71SToby Isaac /*@C
6062611ad71SToby Isaac   PetscLogMPEBegin - Turns on MPE logging of events. This creates large log files and slows the
6072611ad71SToby Isaac   program down.
6082611ad71SToby Isaac 
609cc4c1da9SBarry Smith   Collective on `PETSC_COMM_WORLD`, No Fortran Support
6102611ad71SToby Isaac 
6112611ad71SToby Isaac   Options Database Key:
6122611ad71SToby Isaac . -log_mpe - Prints extensive log information
6132611ad71SToby Isaac 
6142611ad71SToby Isaac   Level: advanced
6152611ad71SToby Isaac 
6162611ad71SToby Isaac   Note:
6172611ad71SToby Isaac   A related routine is `PetscLogDefaultBegin()` (with the options key `-log_view`), which is
6182611ad71SToby Isaac   intended for production runs since it logs only flop rates and object creation (and should
6192611ad71SToby Isaac   not significantly slow the programs).
6202611ad71SToby Isaac 
621b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogDump()`, `PetscLogDefaultBegin()`, `PetscLogEventActivate()`,
6222611ad71SToby Isaac           `PetscLogEventDeactivate()`
6232611ad71SToby Isaac @*/
6242611ad71SToby Isaac PetscErrorCode PetscLogMPEBegin(void)
6252611ad71SToby Isaac {
6262611ad71SToby Isaac   PetscFunctionBegin;
6272611ad71SToby Isaac   #if defined(PETSC_HAVE_MPE)
6282611ad71SToby Isaac   /* Do MPE initialization */
6292611ad71SToby Isaac   if (!MPE_Initialized_logging()) { /* This function exists in mpich 1.1.2 and higher */
6302611ad71SToby Isaac     PetscCall(PetscInfo(0, "Initializing MPE.\n"));
6312611ad71SToby Isaac     PetscCall(MPE_Init_log());
6322611ad71SToby Isaac 
6332611ad71SToby Isaac     PetscBeganMPE = PETSC_TRUE;
6342611ad71SToby Isaac   } else {
6352611ad71SToby Isaac     PetscCall(PetscInfo(0, "MPE already initialized. Not attempting to reinitialize.\n"));
6362611ad71SToby Isaac   }
637294de794SToby Isaac   PetscCall(PetscLogTypeBegin(PETSCLOGHANDLERMPE));
6382611ad71SToby Isaac   #else
6392611ad71SToby Isaac   SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP_SYS, "PETSc was configured without MPE support, reconfigure with --with-mpe or --download-mpe");
6402611ad71SToby Isaac   #endif
6412611ad71SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
6422611ad71SToby Isaac }
6432611ad71SToby Isaac 
64453e0a2f3SToby Isaac   #if defined(PETSC_HAVE_TAU_PERFSTUBS)
64553e0a2f3SToby Isaac     #include <../src/sys/perfstubs/timer.h>
64653e0a2f3SToby Isaac   #endif
64753e0a2f3SToby Isaac 
64853e0a2f3SToby Isaac /*@C
64953e0a2f3SToby Isaac   PetscLogPerfstubsBegin - Turns on logging of events using the perfstubs interface.
65053e0a2f3SToby Isaac 
651cc4c1da9SBarry Smith   Collective on `PETSC_COMM_WORLD`, No Fortran Support
65253e0a2f3SToby Isaac 
65353e0a2f3SToby Isaac   Options Database Key:
65453e0a2f3SToby Isaac . -log_perfstubs - use an external log handler through the perfstubs interface
65553e0a2f3SToby Isaac 
65653e0a2f3SToby Isaac   Level: advanced
65753e0a2f3SToby Isaac 
65853e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogDefaultBegin()`, `PetscLogEventActivate()`
65953e0a2f3SToby Isaac @*/
66053e0a2f3SToby Isaac PetscErrorCode PetscLogPerfstubsBegin(void)
66153e0a2f3SToby Isaac {
66253e0a2f3SToby Isaac   PetscFunctionBegin;
66353e0a2f3SToby Isaac   #if defined(PETSC_HAVE_TAU_PERFSTUBS)
664294de794SToby Isaac   PetscCall(PetscLogTypeBegin(PETSCLOGHANDLERPERFSTUBS));
66553e0a2f3SToby Isaac   #else
66653e0a2f3SToby Isaac   SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP_SYS, "PETSc was configured without perfstubs support, reconfigure with --with-tau-perfstubs");
66753e0a2f3SToby Isaac   #endif
66853e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
66953e0a2f3SToby Isaac }
67053e0a2f3SToby Isaac 
6715c6c1daeSBarry Smith /*@
672b665b14eSToby Isaac   PetscLogActions - Determines whether actions are logged for the default log handler.
6735c6c1daeSBarry Smith 
6745c6c1daeSBarry Smith   Not Collective
6755c6c1daeSBarry Smith 
6765c6c1daeSBarry Smith   Input Parameter:
677811af0c4SBarry Smith . flag - `PETSC_TRUE` if actions are to be logged
678811af0c4SBarry Smith 
679811af0c4SBarry Smith   Options Database Key:
680b665b14eSToby Isaac + -log_exclude_actions - (deprecated) Does nothing
681b665b14eSToby Isaac - -log_include_actions - Turn on action logging
6825c6c1daeSBarry Smith 
6835c6c1daeSBarry Smith   Level: intermediate
6845c6c1daeSBarry Smith 
685811af0c4SBarry Smith   Note:
686811af0c4SBarry Smith   Logging of actions continues to consume more memory as the program
6875c6c1daeSBarry Smith   runs. Long running programs should consider turning this feature off.
688aec76313SJacob Faibussowitsch 
689b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogGetDefaultHandler()`
6905c6c1daeSBarry Smith @*/
691d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogActions(PetscBool flag)
692d71ae5a4SJacob Faibussowitsch {
6935c6c1daeSBarry Smith   PetscFunctionBegin;
694dff009beSToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
695dff009beSToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
696dff009beSToby Isaac 
697dff009beSToby Isaac     if (h) PetscCall(PetscLogHandlerSetLogActions(h, flag));
698dff009beSToby Isaac   }
6993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7005c6c1daeSBarry Smith }
7015c6c1daeSBarry Smith 
7025c6c1daeSBarry Smith /*@
7035c6c1daeSBarry Smith   PetscLogObjects - Determines whether objects are logged for the graphical viewer.
7045c6c1daeSBarry Smith 
7055c6c1daeSBarry Smith   Not Collective
7065c6c1daeSBarry Smith 
7075c6c1daeSBarry Smith   Input Parameter:
708811af0c4SBarry Smith . flag - `PETSC_TRUE` if objects are to be logged
709811af0c4SBarry Smith 
710811af0c4SBarry Smith   Options Database Key:
711b665b14eSToby Isaac + -log_exclude_objects - (deprecated) Does nothing
712b665b14eSToby Isaac - -log_include_objects - Turns on object logging
7135c6c1daeSBarry Smith 
7145c6c1daeSBarry Smith   Level: intermediate
7155c6c1daeSBarry Smith 
716811af0c4SBarry Smith   Note:
717811af0c4SBarry Smith   Logging of objects continues to consume more memory as the program
7185c6c1daeSBarry Smith   runs. Long running programs should consider turning this feature off.
7195c6c1daeSBarry Smith 
720b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogGetDefaultHandler()`
7215c6c1daeSBarry Smith @*/
722d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogObjects(PetscBool flag)
723d71ae5a4SJacob Faibussowitsch {
7245c6c1daeSBarry Smith   PetscFunctionBegin;
725dff009beSToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
726dff009beSToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
727dff009beSToby Isaac 
728dff009beSToby Isaac     if (h) PetscCall(PetscLogHandlerSetLogObjects(h, flag));
729dff009beSToby Isaac   }
7303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7315c6c1daeSBarry Smith }
7325c6c1daeSBarry Smith 
7335c6c1daeSBarry Smith /*------------------------------------------------ Stage Functions --------------------------------------------------*/
734cc4c1da9SBarry Smith /*@
73574c0405dSRichard Tran Mills   PetscLogStageRegister - Attaches a character string name to a logging stage.
7365c6c1daeSBarry Smith 
7375c6c1daeSBarry Smith   Not Collective
7385c6c1daeSBarry Smith 
7395c6c1daeSBarry Smith   Input Parameter:
7405c6c1daeSBarry Smith . sname - The name to associate with that stage
7415c6c1daeSBarry Smith 
7425c6c1daeSBarry Smith   Output Parameter:
743b665b14eSToby Isaac . stage - The stage number or -1 if logging is not active (`PetscLogIsActive()`).
7445c6c1daeSBarry Smith 
7455c6c1daeSBarry Smith   Level: intermediate
7465c6c1daeSBarry Smith 
747d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStagePush()`, `PetscLogStagePop()`
7485c6c1daeSBarry Smith @*/
749d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStageRegister(const char sname[], PetscLogStage *stage)
750d71ae5a4SJacob Faibussowitsch {
751b665b14eSToby Isaac   PetscLogState state;
7525c6c1daeSBarry Smith 
7535c6c1daeSBarry Smith   PetscFunctionBegin;
754b665b14eSToby Isaac   *stage = -1;
755b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
756b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateStageRegister(state, sname, stage));
7573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7585c6c1daeSBarry Smith }
7595c6c1daeSBarry Smith 
760cc4c1da9SBarry Smith /*@
761811af0c4SBarry Smith   PetscLogStagePush - This function pushes a stage on the logging stack. Events started and stopped until `PetscLogStagePop()` will be associated with the stage
7625c6c1daeSBarry Smith 
7635c6c1daeSBarry Smith   Not Collective
7645c6c1daeSBarry Smith 
7655c6c1daeSBarry Smith   Input Parameter:
7665c6c1daeSBarry Smith . stage - The stage on which to log
7675c6c1daeSBarry Smith 
76810450e9eSJacob Faibussowitsch   Example Usage:
769811af0c4SBarry Smith   If the option -log_view is used to run the program containing the
7705c6c1daeSBarry Smith   following code, then 2 sets of summary data will be printed during
7715c6c1daeSBarry Smith   PetscFinalize().
7725c6c1daeSBarry Smith .vb
7735c6c1daeSBarry Smith       PetscInitialize(int *argc,char ***args,0,0);
7745c6c1daeSBarry Smith       [stage 0 of code]
7755c6c1daeSBarry Smith       PetscLogStagePush(1);
7765c6c1daeSBarry Smith       [stage 1 of code]
7775c6c1daeSBarry Smith       PetscLogStagePop();
7785c6c1daeSBarry Smith       PetscBarrier(...);
7795c6c1daeSBarry Smith       [more stage 0 of code]
7805c6c1daeSBarry Smith       PetscFinalize();
7815c6c1daeSBarry Smith .ve
7825c6c1daeSBarry Smith 
783d1f92df0SBarry Smith   Level: intermediate
784d1f92df0SBarry Smith 
785811af0c4SBarry Smith   Note:
786811af0c4SBarry Smith   Use `PetscLogStageRegister()` to register a stage.
7875c6c1daeSBarry Smith 
788d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStagePop()`, `PetscLogStageRegister()`, `PetscBarrier()`
7895c6c1daeSBarry Smith @*/
790d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStagePush(PetscLogStage stage)
791d71ae5a4SJacob Faibussowitsch {
792b665b14eSToby Isaac   PetscLogState state;
7935c6c1daeSBarry Smith 
7945c6c1daeSBarry Smith   PetscFunctionBegin;
795b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
796b665b14eSToby Isaac   if (!state) PetscFunctionReturn(PETSC_SUCCESS);
797b665b14eSToby Isaac   for (int i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
798b665b14eSToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
799b665b14eSToby Isaac     if (h) PetscCall(PetscLogHandlerStagePush(h, stage));
800b665b14eSToby Isaac   }
801b665b14eSToby Isaac   PetscCall(PetscLogStateStagePush(state, stage));
8023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8035c6c1daeSBarry Smith }
8045c6c1daeSBarry Smith 
805cc4c1da9SBarry Smith /*@
806811af0c4SBarry Smith   PetscLogStagePop - This function pops a stage from the logging stack that was pushed with `PetscLogStagePush()`
8075c6c1daeSBarry Smith 
8085c6c1daeSBarry Smith   Not Collective
8095c6c1daeSBarry Smith 
81010450e9eSJacob Faibussowitsch   Example Usage:
811811af0c4SBarry Smith   If the option -log_view is used to run the program containing the
8125c6c1daeSBarry Smith   following code, then 2 sets of summary data will be printed during
8135c6c1daeSBarry Smith   PetscFinalize().
8145c6c1daeSBarry Smith .vb
8155c6c1daeSBarry Smith       PetscInitialize(int *argc,char ***args,0,0);
8165c6c1daeSBarry Smith       [stage 0 of code]
8175c6c1daeSBarry Smith       PetscLogStagePush(1);
8185c6c1daeSBarry Smith       [stage 1 of code]
8195c6c1daeSBarry Smith       PetscLogStagePop();
8205c6c1daeSBarry Smith       PetscBarrier(...);
8215c6c1daeSBarry Smith       [more stage 0 of code]
8225c6c1daeSBarry Smith       PetscFinalize();
8235c6c1daeSBarry Smith .ve
8245c6c1daeSBarry Smith 
8255c6c1daeSBarry Smith   Level: intermediate
8265c6c1daeSBarry Smith 
827d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStagePush()`, `PetscLogStageRegister()`, `PetscBarrier()`
8285c6c1daeSBarry Smith @*/
829d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStagePop(void)
830d71ae5a4SJacob Faibussowitsch {
831b665b14eSToby Isaac   PetscLogState state;
832b665b14eSToby Isaac   PetscLogStage current_stage;
8335c6c1daeSBarry Smith 
8345c6c1daeSBarry Smith   PetscFunctionBegin;
835b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
836b665b14eSToby Isaac   if (!state) PetscFunctionReturn(PETSC_SUCCESS);
837b665b14eSToby Isaac   current_stage = state->current_stage;
838b665b14eSToby Isaac   PetscCall(PetscLogStateStagePop(state));
839b665b14eSToby Isaac   for (int i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
840b665b14eSToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
841b665b14eSToby Isaac     if (h) PetscCall(PetscLogHandlerStagePop(h, current_stage));
842b665b14eSToby Isaac   }
8433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8445c6c1daeSBarry Smith }
8455c6c1daeSBarry Smith 
8465c6c1daeSBarry Smith /*@
847811af0c4SBarry Smith   PetscLogStageSetActive - Sets if a stage is used for `PetscLogEventBegin()` and `PetscLogEventEnd()`.
8485c6c1daeSBarry Smith 
8495c6c1daeSBarry Smith   Not Collective
8505c6c1daeSBarry Smith 
8515c6c1daeSBarry Smith   Input Parameters:
8525c6c1daeSBarry Smith + stage    - The stage
853811af0c4SBarry Smith - isActive - The activity flag, `PETSC_TRUE` for logging, else `PETSC_FALSE` (defaults to `PETSC_TRUE`)
8545c6c1daeSBarry Smith 
8555c6c1daeSBarry Smith   Level: intermediate
8565c6c1daeSBarry Smith 
857811af0c4SBarry Smith   Note:
858811af0c4SBarry Smith   If this is set to `PETSC_FALSE` the logging acts as if the stage did not exist
859811af0c4SBarry Smith 
860d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()`
8615c6c1daeSBarry Smith @*/
862d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStageSetActive(PetscLogStage stage, PetscBool isActive)
863d71ae5a4SJacob Faibussowitsch {
864b665b14eSToby Isaac   PetscLogState state;
8655c6c1daeSBarry Smith 
8665c6c1daeSBarry Smith   PetscFunctionBegin;
867b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
868b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateStageSetActive(state, stage, isActive));
8693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8705c6c1daeSBarry Smith }
8715c6c1daeSBarry Smith 
8725c6c1daeSBarry Smith /*@
873811af0c4SBarry Smith   PetscLogStageGetActive - Checks if a stage is used for `PetscLogEventBegin()` and `PetscLogEventEnd()`.
8745c6c1daeSBarry Smith 
8755c6c1daeSBarry Smith   Not Collective
8765c6c1daeSBarry Smith 
8775c6c1daeSBarry Smith   Input Parameter:
8785c6c1daeSBarry Smith . stage - The stage
8795c6c1daeSBarry Smith 
8805c6c1daeSBarry Smith   Output Parameter:
881811af0c4SBarry Smith . isActive - The activity flag, `PETSC_TRUE` for logging, else `PETSC_FALSE` (defaults to `PETSC_TRUE`)
8825c6c1daeSBarry Smith 
8835c6c1daeSBarry Smith   Level: intermediate
8845c6c1daeSBarry Smith 
885d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()`
8865c6c1daeSBarry Smith @*/
887d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStageGetActive(PetscLogStage stage, PetscBool *isActive)
888d71ae5a4SJacob Faibussowitsch {
889b665b14eSToby Isaac   PetscLogState state;
8905c6c1daeSBarry Smith 
8915c6c1daeSBarry Smith   PetscFunctionBegin;
892b665b14eSToby Isaac   *isActive = PETSC_FALSE;
893b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
894b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateStageGetActive(state, stage, isActive));
8953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
8965c6c1daeSBarry Smith }
8975c6c1daeSBarry Smith 
8985c6c1daeSBarry Smith /*@
899811af0c4SBarry Smith   PetscLogStageSetVisible - Determines stage visibility in `PetscLogView()`
9005c6c1daeSBarry Smith 
9015c6c1daeSBarry Smith   Not Collective
9025c6c1daeSBarry Smith 
9035c6c1daeSBarry Smith   Input Parameters:
9045c6c1daeSBarry Smith + stage     - The stage
905811af0c4SBarry Smith - isVisible - The visibility flag, `PETSC_TRUE` to print, else `PETSC_FALSE` (defaults to `PETSC_TRUE`)
9065c6c1daeSBarry Smith 
9075c6c1daeSBarry Smith   Level: intermediate
9085c6c1daeSBarry Smith 
909aec76313SJacob Faibussowitsch   Developer Notes:
910b665b14eSToby Isaac   Visibility only affects the default log handler in `PetscLogView()`: stages that are
911b665b14eSToby Isaac   set to invisible are suppressed from output.
912811af0c4SBarry Smith 
913b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogStageGetVisible()`, `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogView()`, `PetscLogGetDefaultHandler()`
9145c6c1daeSBarry Smith @*/
915d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStageSetVisible(PetscLogStage stage, PetscBool isVisible)
9165c6c1daeSBarry Smith 
917dff009beSToby Isaac {
9185c6c1daeSBarry Smith   PetscFunctionBegin;
919dff009beSToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
920dff009beSToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
921dff009beSToby Isaac 
922dff009beSToby Isaac     if (h) PetscCall(PetscLogHandlerStageSetVisible(h, stage, isVisible));
923dff009beSToby Isaac   }
9243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9255c6c1daeSBarry Smith }
9265c6c1daeSBarry Smith 
9275c6c1daeSBarry Smith /*@
928811af0c4SBarry Smith   PetscLogStageGetVisible - Returns stage visibility in `PetscLogView()`
9295c6c1daeSBarry Smith 
9305c6c1daeSBarry Smith   Not Collective
9315c6c1daeSBarry Smith 
9325c6c1daeSBarry Smith   Input Parameter:
9335c6c1daeSBarry Smith . stage - The stage
9345c6c1daeSBarry Smith 
9355c6c1daeSBarry Smith   Output Parameter:
936811af0c4SBarry Smith . isVisible - The visibility flag, `PETSC_TRUE` to print, else `PETSC_FALSE` (defaults to `PETSC_TRUE`)
9375c6c1daeSBarry Smith 
9385c6c1daeSBarry Smith   Level: intermediate
9395c6c1daeSBarry Smith 
940b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogStageSetVisible()`, `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogView()`, `PetscLogGetDefaultHandler()`
9415c6c1daeSBarry Smith @*/
942d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStageGetVisible(PetscLogStage stage, PetscBool *isVisible)
943d71ae5a4SJacob Faibussowitsch {
944b665b14eSToby Isaac   PetscLogHandler handler;
9455c6c1daeSBarry Smith 
9465c6c1daeSBarry Smith   PetscFunctionBegin;
947b665b14eSToby Isaac   *isVisible = PETSC_FALSE;
948294de794SToby Isaac   PetscCall(PetscLogTryGetHandler(PETSCLOGHANDLERDEFAULT, &handler));
9493a7d0413SPierre Jolivet   if (handler) PetscCall(PetscLogHandlerStageGetVisible(handler, stage, isVisible));
9503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9515c6c1daeSBarry Smith }
9525c6c1daeSBarry Smith 
953cc4c1da9SBarry Smith /*@
9545c6c1daeSBarry Smith   PetscLogStageGetId - Returns the stage id when given the stage name.
9555c6c1daeSBarry Smith 
9565c6c1daeSBarry Smith   Not Collective
9575c6c1daeSBarry Smith 
9585c6c1daeSBarry Smith   Input Parameter:
9595c6c1daeSBarry Smith . name - The stage name
9605c6c1daeSBarry Smith 
9615c6c1daeSBarry Smith   Output Parameter:
9625a4a3fabSBarry Smith . stage - The stage, , or -1 if no stage with that name exists
9635c6c1daeSBarry Smith 
9645c6c1daeSBarry Smith   Level: intermediate
9655c6c1daeSBarry Smith 
966d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()`
9675c6c1daeSBarry Smith @*/
968d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogStageGetId(const char name[], PetscLogStage *stage)
969d71ae5a4SJacob Faibussowitsch {
970b665b14eSToby Isaac   PetscLogState state;
9715c6c1daeSBarry Smith 
9725c6c1daeSBarry Smith   PetscFunctionBegin;
973b665b14eSToby Isaac   *stage = -1;
974b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
975b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateGetStageFromName(state, name, stage));
9763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9775c6c1daeSBarry Smith }
9785c6c1daeSBarry Smith 
979cc4c1da9SBarry Smith /*@
98053e0a2f3SToby Isaac   PetscLogStageGetName - Returns the stage name when given the stage id.
98153e0a2f3SToby Isaac 
98253e0a2f3SToby Isaac   Not Collective
98353e0a2f3SToby Isaac 
98453e0a2f3SToby Isaac   Input Parameter:
98553e0a2f3SToby Isaac . stage - The stage
98653e0a2f3SToby Isaac 
98753e0a2f3SToby Isaac   Output Parameter:
98853e0a2f3SToby Isaac . name - The stage name
98953e0a2f3SToby Isaac 
99053e0a2f3SToby Isaac   Level: intermediate
99153e0a2f3SToby Isaac 
99253e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()`
99353e0a2f3SToby Isaac @*/
994cc4c1da9SBarry Smith PetscErrorCode PetscLogStageGetName(PetscLogStage stage, const char *name[])
99553e0a2f3SToby Isaac {
99653e0a2f3SToby Isaac   PetscLogStageInfo stage_info;
99753e0a2f3SToby Isaac   PetscLogState     state;
99853e0a2f3SToby Isaac 
99953e0a2f3SToby Isaac   PetscFunctionBegin;
1000b665b14eSToby Isaac   *name = NULL;
100153e0a2f3SToby Isaac   PetscCall(PetscLogGetState(&state));
1002b665b14eSToby Isaac   if (!state) PetscFunctionReturn(PETSC_SUCCESS);
100353e0a2f3SToby Isaac   PetscCall(PetscLogStateStageGetInfo(state, stage, &stage_info));
100453e0a2f3SToby Isaac   *name = stage_info.name;
100553e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
100653e0a2f3SToby Isaac }
100753e0a2f3SToby Isaac 
10085c6c1daeSBarry Smith /*------------------------------------------------ Event Functions --------------------------------------------------*/
10097a101e5eSJacob Faibussowitsch 
1010cc4c1da9SBarry Smith /*@
1011811af0c4SBarry Smith   PetscLogEventRegister - Registers an event name for logging operations
10125c6c1daeSBarry Smith 
10135c6c1daeSBarry Smith   Not Collective
10145c6c1daeSBarry Smith 
1015d8d19677SJose E. Roman   Input Parameters:
10165c6c1daeSBarry Smith + name    - The name associated with the event
10175c6c1daeSBarry Smith - classid - The classid associated to the class for this event, obtain either with
1018811af0c4SBarry Smith            `PetscClassIdRegister()` or use a predefined one such as `KSP_CLASSID`, `SNES_CLASSID`, the predefined ones
10195c6c1daeSBarry Smith            are only available in C code
10205c6c1daeSBarry Smith 
10215c6c1daeSBarry Smith   Output Parameter:
1022811af0c4SBarry Smith . event - The event id for use with `PetscLogEventBegin()` and `PetscLogEventEnd()`.
10235c6c1daeSBarry Smith 
102410450e9eSJacob Faibussowitsch   Example Usage:
10255c6c1daeSBarry Smith .vb
10265c6c1daeSBarry Smith       PetscLogEvent USER_EVENT;
10275c6c1daeSBarry Smith       PetscClassId classid;
10285c6c1daeSBarry Smith       PetscLogDouble user_event_flops;
10295c6c1daeSBarry Smith       PetscClassIdRegister("class name",&classid);
10305c6c1daeSBarry Smith       PetscLogEventRegister("User event name",classid,&USER_EVENT);
10315c6c1daeSBarry Smith       PetscLogEventBegin(USER_EVENT,0,0,0,0);
10325c6c1daeSBarry Smith          [code segment to monitor]
10335c6c1daeSBarry Smith          PetscLogFlops(user_event_flops);
10345c6c1daeSBarry Smith       PetscLogEventEnd(USER_EVENT,0,0,0,0);
10355c6c1daeSBarry Smith .ve
10365c6c1daeSBarry Smith 
1037d1f92df0SBarry Smith   Level: intermediate
1038d1f92df0SBarry Smith 
10395c6c1daeSBarry Smith   Notes:
10405c6c1daeSBarry Smith   PETSc automatically logs library events if the code has been
1041a2553e36SBarry Smith   configured with --with-log (which is the default) and
1042811af0c4SBarry Smith   -log_view or -log_all is specified.  `PetscLogEventRegister()` is
10435c6c1daeSBarry Smith   intended for logging user events to supplement this PETSc
10445c6c1daeSBarry Smith   information.
10455c6c1daeSBarry Smith 
1046495fc317SBarry Smith   PETSc can gather data for use with the utilities Jumpshot
10475c6c1daeSBarry Smith   (part of the MPICH distribution).  If PETSc has been compiled
10485c6c1daeSBarry Smith   with flag -DPETSC_HAVE_MPE (MPE is an additional utility within
10495c6c1daeSBarry Smith   MPICH), the user can employ another command line option, -log_mpe,
10505c6c1daeSBarry Smith   to create a logfile, "mpe.log", which can be visualized
1051495fc317SBarry Smith   Jumpshot.
10525c6c1daeSBarry Smith 
10535c6c1daeSBarry Smith   The classid is associated with each event so that classes of events
10545c6c1daeSBarry Smith   can be disabled simultaneously, such as all matrix events. The user
1055811af0c4SBarry Smith   can either use an existing classid, such as `MAT_CLASSID`, or create
10565c6c1daeSBarry Smith   their own as shown in the example.
10575c6c1daeSBarry Smith 
1058c5deb1d5SJed Brown   If an existing event with the same name exists, its event handle is
1059c5deb1d5SJed Brown   returned instead of creating a new event.
1060c5deb1d5SJed Brown 
1061d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogStageRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogFlops()`,
1062db781477SPatrick Sanan           `PetscLogEventActivate()`, `PetscLogEventDeactivate()`, `PetscClassIdRegister()`
10635c6c1daeSBarry Smith @*/
1064d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventRegister(const char name[], PetscClassId classid, PetscLogEvent *event)
1065d71ae5a4SJacob Faibussowitsch {
1066b665b14eSToby Isaac   PetscLogState state;
10675c6c1daeSBarry Smith 
10685c6c1daeSBarry Smith   PetscFunctionBegin;
1069b665b14eSToby Isaac   *event = -1;
1070b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
1071b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateEventRegister(state, name, classid, event));
10723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
10735c6c1daeSBarry Smith }
10745c6c1daeSBarry Smith 
10755c6c1daeSBarry Smith /*@
1076217044c2SLisandro Dalcin   PetscLogEventSetCollective - Indicates that a particular event is collective.
1077217044c2SLisandro Dalcin 
10785aefd447SStefano Zampini   Logically Collective
1079217044c2SLisandro Dalcin 
1080d8d19677SJose E. Roman   Input Parameters:
1081217044c2SLisandro Dalcin + event      - The event id
1082ffbd2f08SBarry Smith - collective - `PetscBool` indicating whether a particular event is collective
1083217044c2SLisandro Dalcin 
1084d1f92df0SBarry Smith   Level: developer
1085d1f92df0SBarry Smith 
1086811af0c4SBarry Smith   Notes:
1087811af0c4SBarry Smith   New events returned from `PetscLogEventRegister()` are collective by default.
1088811af0c4SBarry Smith 
1089ffbd2f08SBarry Smith   Collective events are handled specially if the command line option `-log_sync` is used. In that case the logging saves information about
1090811af0c4SBarry Smith   two parts of the event; the time for all the MPI ranks to synchronize and then the time for the actual computation/communication
10915aefd447SStefano Zampini   to be performed. This option is useful to debug imbalance within the computations or communications.
1092217044c2SLisandro Dalcin 
1093d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogEventRegister()`
1094217044c2SLisandro Dalcin @*/
1095d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventSetCollective(PetscLogEvent event, PetscBool collective)
1096d71ae5a4SJacob Faibussowitsch {
1097b665b14eSToby Isaac   PetscLogState state;
1098217044c2SLisandro Dalcin 
1099217044c2SLisandro Dalcin   PetscFunctionBegin;
1100b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
1101b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateEventSetCollective(state, event, collective));
1102b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
1103b665b14eSToby Isaac }
1104b665b14eSToby Isaac 
1105b665b14eSToby Isaac /*
1106b665b14eSToby Isaac   PetscLogClassSetActiveAll - Activate or inactivate logging for all events associated with a PETSc object class in every stage.
1107b665b14eSToby Isaac 
1108b665b14eSToby Isaac   Not Collective
1109b665b14eSToby Isaac 
1110b665b14eSToby Isaac   Input Parameters:
1111b665b14eSToby Isaac + classid - The object class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc.
1112b665b14eSToby Isaac - isActive - if `PETSC_FALSE`, events associated with this class will not be send to log handlers.
1113b665b14eSToby Isaac 
1114b665b14eSToby Isaac   Level: developer
1115b665b14eSToby Isaac 
1116b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogEventActivate()`, `PetscLogEventActivateAll()`, `PetscLogStageSetActive()`, `PetscLogEventActivateClass()`
1117b665b14eSToby Isaac */
1118b665b14eSToby Isaac static PetscErrorCode PetscLogClassSetActiveAll(PetscClassId classid, PetscBool isActive)
1119b665b14eSToby Isaac {
1120b665b14eSToby Isaac   PetscLogState state;
1121b665b14eSToby Isaac 
1122b665b14eSToby Isaac   PetscFunctionBegin;
1123b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
1124b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateClassSetActiveAll(state, classid, isActive));
11253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1126217044c2SLisandro Dalcin }
1127217044c2SLisandro Dalcin 
1128217044c2SLisandro Dalcin /*@
1129fa2bb9feSLisandro Dalcin   PetscLogEventIncludeClass - Activates event logging for a PETSc object class in every stage.
1130fa2bb9feSLisandro Dalcin 
1131fa2bb9feSLisandro Dalcin   Not Collective
1132fa2bb9feSLisandro Dalcin 
1133fa2bb9feSLisandro Dalcin   Input Parameter:
1134811af0c4SBarry Smith . classid - The object class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc.
1135fa2bb9feSLisandro Dalcin 
1136fa2bb9feSLisandro Dalcin   Level: developer
1137fa2bb9feSLisandro Dalcin 
1138d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventActivateClass()`, `PetscLogEventDeactivateClass()`, `PetscLogEventActivate()`, `PetscLogEventDeactivate()`
1139fa2bb9feSLisandro Dalcin @*/
1140d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventIncludeClass(PetscClassId classid)
1141d71ae5a4SJacob Faibussowitsch {
1142fa2bb9feSLisandro Dalcin   PetscFunctionBegin;
1143b665b14eSToby Isaac   PetscCall(PetscLogClassSetActiveAll(classid, PETSC_TRUE));
11443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1145fa2bb9feSLisandro Dalcin }
1146fa2bb9feSLisandro Dalcin 
1147fa2bb9feSLisandro Dalcin /*@
1148fa2bb9feSLisandro Dalcin   PetscLogEventExcludeClass - Deactivates event logging for a PETSc object class in every stage.
1149fa2bb9feSLisandro Dalcin 
1150fa2bb9feSLisandro Dalcin   Not Collective
1151fa2bb9feSLisandro Dalcin 
1152fa2bb9feSLisandro Dalcin   Input Parameter:
1153811af0c4SBarry Smith . classid - The object class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc.
1154fa2bb9feSLisandro Dalcin 
1155fa2bb9feSLisandro Dalcin   Level: developer
1156fa2bb9feSLisandro Dalcin 
1157811af0c4SBarry Smith   Note:
1158811af0c4SBarry Smith   If a class is excluded then events associated with that class are not logged.
1159811af0c4SBarry Smith 
1160d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventDeactivateClass()`, `PetscLogEventActivateClass()`, `PetscLogEventDeactivate()`, `PetscLogEventActivate()`
1161fa2bb9feSLisandro Dalcin @*/
1162d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventExcludeClass(PetscClassId classid)
1163d71ae5a4SJacob Faibussowitsch {
1164b665b14eSToby Isaac   PetscFunctionBegin;
1165b665b14eSToby Isaac   PetscCall(PetscLogClassSetActiveAll(classid, PETSC_FALSE));
1166b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
1167b665b14eSToby Isaac }
1168b665b14eSToby Isaac 
1169b665b14eSToby Isaac /*
1170b665b14eSToby Isaac   PetscLogEventSetActive - Activate or inactivate logging for an event in a given stage
1171b665b14eSToby Isaac 
1172b665b14eSToby Isaac   Not Collective
1173b665b14eSToby Isaac 
1174b665b14eSToby Isaac   Input Parameters:
1175b665b14eSToby Isaac + stage - A registered `PetscLogStage` (or `PETSC_DEFAULT` for the current stage)
1176b665b14eSToby Isaac . event - A `PetscLogEvent`
1177b665b14eSToby Isaac - isActive - If `PETSC_FALSE`, activity from this event (`PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogEventSync()`) will not be sent to log handlers during this stage
1178b665b14eSToby Isaac 
1179b665b14eSToby Isaac   Usage:
1180b665b14eSToby Isaac .vb
1181b665b14eSToby Isaac       PetscLogEventSetActive(VEC_SetValues, PETSC_FALSE);
1182b665b14eSToby Isaac         [code where you do not want to log VecSetValues()]
1183b665b14eSToby Isaac       PetscLogEventSetActive(VEC_SetValues, PETSC_TRUE);
1184b665b14eSToby Isaac         [code where you do want to log VecSetValues()]
1185b665b14eSToby Isaac .ve
1186b665b14eSToby Isaac 
1187b665b14eSToby Isaac   Level: advanced
1188b665b14eSToby Isaac 
1189b665b14eSToby Isaac   Note:
1190b665b14eSToby Isaac   The event may be either a pre-defined PETSc event (found in include/petsclog.h)
1191b665b14eSToby Isaac   or an event number obtained with `PetscLogEventRegister()`.
1192b665b14eSToby Isaac 
1193b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogEventDeactivatePush()`, `PetscLogEventDeactivatePop()`
1194b665b14eSToby Isaac */
1195b665b14eSToby Isaac static PetscErrorCode PetscLogEventSetActive(PetscLogStage stage, PetscLogEvent event, PetscBool isActive)
1196b665b14eSToby Isaac {
1197b665b14eSToby Isaac   PetscLogState state;
1198fa2bb9feSLisandro Dalcin 
1199fa2bb9feSLisandro Dalcin   PetscFunctionBegin;
1200b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
1201b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateEventSetActive(state, stage, event, isActive));
12023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1203fa2bb9feSLisandro Dalcin }
1204fa2bb9feSLisandro Dalcin 
1205fa2bb9feSLisandro Dalcin /*@
12065c6c1daeSBarry Smith   PetscLogEventActivate - Indicates that a particular event should be logged.
12075c6c1daeSBarry Smith 
12085c6c1daeSBarry Smith   Not Collective
12095c6c1daeSBarry Smith 
12105c6c1daeSBarry Smith   Input Parameter:
12115c6c1daeSBarry Smith . event - The event id
12125c6c1daeSBarry Smith 
121310450e9eSJacob Faibussowitsch   Example Usage:
12145c6c1daeSBarry Smith .vb
12155c6c1daeSBarry Smith       PetscLogEventDeactivate(VEC_SetValues);
12165c6c1daeSBarry Smith         [code where you do not want to log VecSetValues()]
12175c6c1daeSBarry Smith       PetscLogEventActivate(VEC_SetValues);
12185c6c1daeSBarry Smith         [code where you do want to log VecSetValues()]
12195c6c1daeSBarry Smith .ve
12205c6c1daeSBarry Smith 
1221d1f92df0SBarry Smith   Level: advanced
1222d1f92df0SBarry Smith 
12235c6c1daeSBarry Smith   Note:
12245c6c1daeSBarry Smith   The event may be either a pre-defined PETSc event (found in include/petsclog.h)
1225811af0c4SBarry Smith   or an event number obtained with `PetscLogEventRegister()`.
12265c6c1daeSBarry Smith 
1227b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogEventDeactivate()`, `PetscLogEventDeactivatePush()`, `PetscLogEventDeactivatePop()`
12285c6c1daeSBarry Smith @*/
1229d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventActivate(PetscLogEvent event)
1230d71ae5a4SJacob Faibussowitsch {
12315c6c1daeSBarry Smith   PetscFunctionBegin;
1232b665b14eSToby Isaac   PetscCall(PetscLogEventSetActive(PETSC_DEFAULT, event, PETSC_TRUE));
12333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12345c6c1daeSBarry Smith }
12355c6c1daeSBarry Smith 
12365c6c1daeSBarry Smith /*@
12375c6c1daeSBarry Smith   PetscLogEventDeactivate - Indicates that a particular event should not be logged.
12385c6c1daeSBarry Smith 
12395c6c1daeSBarry Smith   Not Collective
12405c6c1daeSBarry Smith 
12415c6c1daeSBarry Smith   Input Parameter:
12425c6c1daeSBarry Smith . event - The event id
12435c6c1daeSBarry Smith 
124410450e9eSJacob Faibussowitsch   Example Usage:
12455c6c1daeSBarry Smith .vb
12465c6c1daeSBarry Smith       PetscLogEventDeactivate(VEC_SetValues);
12475c6c1daeSBarry Smith         [code where you do not want to log VecSetValues()]
12485c6c1daeSBarry Smith       PetscLogEventActivate(VEC_SetValues);
12495c6c1daeSBarry Smith         [code where you do want to log VecSetValues()]
12505c6c1daeSBarry Smith .ve
12515c6c1daeSBarry Smith 
1252d1f92df0SBarry Smith   Level: advanced
1253d1f92df0SBarry Smith 
12545c6c1daeSBarry Smith   Note:
12555c6c1daeSBarry Smith   The event may be either a pre-defined PETSc event (found in
1256811af0c4SBarry Smith   include/petsclog.h) or an event number obtained with `PetscLogEventRegister()`).
12575c6c1daeSBarry Smith 
1258d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventActivate()`, `PetscLogEventDeactivatePush()`, `PetscLogEventDeactivatePop()`
12595c6c1daeSBarry Smith @*/
1260d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventDeactivate(PetscLogEvent event)
1261d71ae5a4SJacob Faibussowitsch {
12625c6c1daeSBarry Smith   PetscFunctionBegin;
1263b665b14eSToby Isaac   PetscCall(PetscLogEventSetActive(PETSC_DEFAULT, event, PETSC_FALSE));
12643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12655c6c1daeSBarry Smith }
12665c6c1daeSBarry Smith 
12675c6c1daeSBarry Smith /*@
1268811af0c4SBarry Smith   PetscLogEventDeactivatePush - Indicates that a particular event should not be logged until `PetscLogEventDeactivatePop()` is called
1269c00cb57fSBarry Smith 
1270c00cb57fSBarry Smith   Not Collective
1271c00cb57fSBarry Smith 
1272c00cb57fSBarry Smith   Input Parameter:
1273c00cb57fSBarry Smith . event - The event id
1274c00cb57fSBarry Smith 
127510450e9eSJacob Faibussowitsch   Example Usage:
1276c00cb57fSBarry Smith .vb
1277c00cb57fSBarry Smith       PetscLogEventDeactivatePush(VEC_SetValues);
1278c00cb57fSBarry Smith         [code where you do not want to log VecSetValues()]
1279c00cb57fSBarry Smith       PetscLogEventDeactivatePop(VEC_SetValues);
1280c00cb57fSBarry Smith         [code where you do want to log VecSetValues()]
1281c00cb57fSBarry Smith .ve
1282c00cb57fSBarry Smith 
1283d1f92df0SBarry Smith   Level: advanced
1284d1f92df0SBarry Smith 
1285c00cb57fSBarry Smith   Note:
1286c00cb57fSBarry Smith   The event may be either a pre-defined PETSc event (found in
1287811af0c4SBarry Smith   include/petsclog.h) or an event number obtained with `PetscLogEventRegister()`).
1288c00cb57fSBarry Smith 
1289baca6076SPierre 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.
1290b665b14eSToby Isaac 
12914b7c4d4dSPierre Jolivet .seealso: [](ch_profiling), `PetscLogEventActivate()`, `PetscLogEventDeactivate()`, `PetscLogEventDeactivatePop()`
1292c00cb57fSBarry Smith @*/
1293d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventDeactivatePush(PetscLogEvent event)
1294d71ae5a4SJacob Faibussowitsch {
1295c00cb57fSBarry Smith   PetscFunctionBegin;
1296dff009beSToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
1297dff009beSToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
1298dff009beSToby Isaac 
1299dff009beSToby Isaac     if (h) PetscCall(PetscLogHandlerEventDeactivatePush(h, PETSC_DEFAULT, event));
1300dff009beSToby Isaac   }
13013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1302c00cb57fSBarry Smith }
1303c00cb57fSBarry Smith 
1304c00cb57fSBarry Smith /*@
1305811af0c4SBarry Smith   PetscLogEventDeactivatePop - Indicates that a particular event should again be logged after the logging was turned off with `PetscLogEventDeactivatePush()`
1306c00cb57fSBarry Smith 
1307c00cb57fSBarry Smith   Not Collective
1308c00cb57fSBarry Smith 
1309c00cb57fSBarry Smith   Input Parameter:
1310c00cb57fSBarry Smith . event - The event id
1311c00cb57fSBarry Smith 
131210450e9eSJacob Faibussowitsch   Example Usage:
1313c00cb57fSBarry Smith .vb
1314c00cb57fSBarry Smith       PetscLogEventDeactivatePush(VEC_SetValues);
1315c00cb57fSBarry Smith         [code where you do not want to log VecSetValues()]
1316c00cb57fSBarry Smith       PetscLogEventDeactivatePop(VEC_SetValues);
1317c00cb57fSBarry Smith         [code where you do want to log VecSetValues()]
1318c00cb57fSBarry Smith .ve
1319c00cb57fSBarry Smith 
1320d1f92df0SBarry Smith   Level: advanced
1321d1f92df0SBarry Smith 
1322c00cb57fSBarry Smith   Note:
1323c00cb57fSBarry Smith   The event may be either a pre-defined PETSc event (found in
1324811af0c4SBarry Smith   include/petsclog.h) or an event number obtained with `PetscLogEventRegister()`).
1325c00cb57fSBarry Smith 
1326d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventActivate()`, `PetscLogEventDeactivatePush()`
1327c00cb57fSBarry Smith @*/
1328d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventDeactivatePop(PetscLogEvent event)
1329d71ae5a4SJacob Faibussowitsch {
1330c00cb57fSBarry Smith   PetscFunctionBegin;
1331dff009beSToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
1332dff009beSToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
1333dff009beSToby Isaac 
1334dff009beSToby Isaac     if (h) PetscCall(PetscLogHandlerEventDeactivatePop(h, PETSC_DEFAULT, event));
1335dff009beSToby Isaac   }
13363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1337c00cb57fSBarry Smith }
1338c00cb57fSBarry Smith 
1339c00cb57fSBarry Smith /*@
1340811af0c4SBarry Smith   PetscLogEventSetActiveAll - Turns on logging of all events
13415c6c1daeSBarry Smith 
13425c6c1daeSBarry Smith   Not Collective
13435c6c1daeSBarry Smith 
13445c6c1daeSBarry Smith   Input Parameters:
13455c6c1daeSBarry Smith + event    - The event id
13465c6c1daeSBarry Smith - isActive - The activity flag determining whether the event is logged
13475c6c1daeSBarry Smith 
13485c6c1daeSBarry Smith   Level: advanced
13495c6c1daeSBarry Smith 
1350b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogEventActivate()`, `PetscLogEventDeactivate()`
13515c6c1daeSBarry Smith @*/
1352d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventSetActiveAll(PetscLogEvent event, PetscBool isActive)
1353d71ae5a4SJacob Faibussowitsch {
1354b665b14eSToby Isaac   PetscLogState state;
13555c6c1daeSBarry Smith 
13565c6c1daeSBarry Smith   PetscFunctionBegin;
1357b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
1358b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateEventSetActiveAll(state, event, isActive));
1359b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
13605c6c1daeSBarry Smith }
1361b665b14eSToby Isaac 
1362b665b14eSToby Isaac /*
1363b665b14eSToby Isaac   PetscLogClassSetActive - Activates event logging for a PETSc object class for the current stage
1364b665b14eSToby Isaac 
1365b665b14eSToby Isaac   Not Collective
1366b665b14eSToby Isaac 
1367b665b14eSToby Isaac   Input Parameters:
1368b665b14eSToby Isaac + stage - A registered `PetscLogStage` (or `PETSC_DEFAULT` for the current stage)
1369b665b14eSToby Isaac . classid - The event class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc.
1370b665b14eSToby Isaac - isActive - If `PETSC_FALSE`, events associated with this class are not sent to log handlers.
1371b665b14eSToby Isaac 
1372b665b14eSToby Isaac   Level: developer
1373b665b14eSToby Isaac 
1374b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogEventIncludeClass()`, `PetscLogEventActivate()`, `PetscLogEventActivateAll()`, `PetscLogStageSetActive()`
1375b665b14eSToby Isaac */
1376b665b14eSToby Isaac static PetscErrorCode PetscLogClassSetActive(PetscLogStage stage, PetscClassId classid, PetscBool isActive)
1377b665b14eSToby Isaac {
1378b665b14eSToby Isaac   PetscLogState state;
1379b665b14eSToby Isaac 
1380b665b14eSToby Isaac   PetscFunctionBegin;
1381b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
1382b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateClassSetActive(state, stage, classid, isActive));
13833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13845c6c1daeSBarry Smith }
13855c6c1daeSBarry Smith 
13865c6c1daeSBarry Smith /*@
1387811af0c4SBarry Smith   PetscLogEventActivateClass - Activates event logging for a PETSc object class for the current stage
13885c6c1daeSBarry Smith 
13895c6c1daeSBarry Smith   Not Collective
13905c6c1daeSBarry Smith 
13915c6c1daeSBarry Smith   Input Parameter:
1392811af0c4SBarry Smith . classid - The event class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc.
13935c6c1daeSBarry Smith 
13945c6c1daeSBarry Smith   Level: developer
13955c6c1daeSBarry Smith 
1396d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventIncludeClass()`, `PetscLogEventExcludeClass()`, `PetscLogEventDeactivateClass()`, `PetscLogEventActivate()`, `PetscLogEventDeactivate()`
13975c6c1daeSBarry Smith @*/
1398d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventActivateClass(PetscClassId classid)
1399d71ae5a4SJacob Faibussowitsch {
14005c6c1daeSBarry Smith   PetscFunctionBegin;
1401b665b14eSToby Isaac   PetscCall(PetscLogClassSetActive(PETSC_DEFAULT, classid, PETSC_TRUE));
14023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
14035c6c1daeSBarry Smith }
14045c6c1daeSBarry Smith 
14055c6c1daeSBarry Smith /*@
1406811af0c4SBarry Smith   PetscLogEventDeactivateClass - Deactivates event logging for a PETSc object class for the current stage
14075c6c1daeSBarry Smith 
14085c6c1daeSBarry Smith   Not Collective
14095c6c1daeSBarry Smith 
14105c6c1daeSBarry Smith   Input Parameter:
1411811af0c4SBarry Smith . classid - The event class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc.
14125c6c1daeSBarry Smith 
14135c6c1daeSBarry Smith   Level: developer
14145c6c1daeSBarry Smith 
1415d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventIncludeClass()`, `PetscLogEventExcludeClass()`, `PetscLogEventActivateClass()`, `PetscLogEventActivate()`, `PetscLogEventDeactivate()`
14165c6c1daeSBarry Smith @*/
1417d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventDeactivateClass(PetscClassId classid)
1418d71ae5a4SJacob Faibussowitsch {
14195c6c1daeSBarry Smith   PetscFunctionBegin;
1420b665b14eSToby Isaac   PetscCall(PetscLogClassSetActive(PETSC_DEFAULT, classid, PETSC_FALSE));
14213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
14225c6c1daeSBarry Smith }
14235c6c1daeSBarry Smith 
14245c6c1daeSBarry Smith /*MC
142562872c28SLisandro Dalcin   PetscLogEventSync - Synchronizes the beginning of a user event.
142662872c28SLisandro Dalcin 
142762872c28SLisandro Dalcin   Synopsis:
142862872c28SLisandro Dalcin   #include <petsclog.h>
1429b665b14eSToby Isaac   PetscErrorCode PetscLogEventSync(PetscLogEvent e, MPI_Comm comm)
143062872c28SLisandro Dalcin 
143162872c28SLisandro Dalcin   Collective
143262872c28SLisandro Dalcin 
143362872c28SLisandro Dalcin   Input Parameters:
1434b665b14eSToby Isaac + e    - `PetscLogEvent` obtained from `PetscLogEventRegister()`
143562872c28SLisandro Dalcin - comm - an MPI communicator
143662872c28SLisandro Dalcin 
143710450e9eSJacob Faibussowitsch   Example Usage:
143862872c28SLisandro Dalcin .vb
143962872c28SLisandro Dalcin   PetscLogEvent USER_EVENT;
144010450e9eSJacob Faibussowitsch 
144162872c28SLisandro Dalcin   PetscLogEventRegister("User event", 0, &USER_EVENT);
144262872c28SLisandro Dalcin   PetscLogEventSync(USER_EVENT, PETSC_COMM_WORLD);
144362872c28SLisandro Dalcin   PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0);
144462872c28SLisandro Dalcin   [code segment to monitor]
144562872c28SLisandro Dalcin   PetscLogEventEnd(USER_EVENT, 0, 0, 0 , 0);
144662872c28SLisandro Dalcin .ve
144762872c28SLisandro Dalcin 
1448d1f92df0SBarry Smith   Level: developer
1449d1f92df0SBarry Smith 
1450811af0c4SBarry Smith   Note:
145110450e9eSJacob Faibussowitsch   This routine should be called only if there is not a `PetscObject` available to pass to
145210450e9eSJacob Faibussowitsch   `PetscLogEventBegin()`.
145362872c28SLisandro Dalcin 
1454d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`
145562872c28SLisandro Dalcin M*/
145662872c28SLisandro Dalcin 
145762872c28SLisandro Dalcin /*MC
14585c6c1daeSBarry Smith   PetscLogEventBegin - Logs the beginning of a user event.
14595c6c1daeSBarry Smith 
14605c6c1daeSBarry Smith   Synopsis:
1461aaa7dc30SBarry Smith   #include <petsclog.h>
1462b665b14eSToby Isaac   PetscErrorCode PetscLogEventBegin(PetscLogEvent e, PetscObject o1, PetscObject o2, PetscObject o3, PetscObject o4)
14635c6c1daeSBarry Smith 
14645c6c1daeSBarry Smith   Not Collective
14655c6c1daeSBarry Smith 
14665c6c1daeSBarry Smith   Input Parameters:
1467b665b14eSToby Isaac + e  - `PetscLogEvent` obtained from `PetscLogEventRegister()`
1468baca6076SPierre Jolivet . o1 - object associated with the event, or `NULL`
1469baca6076SPierre Jolivet . o2 - object associated with the event, or `NULL`
1470baca6076SPierre Jolivet . o3 - object associated with the event, or `NULL`
1471baca6076SPierre Jolivet - o4 - object associated with the event, or `NULL`
14725c6c1daeSBarry Smith 
14735c6c1daeSBarry Smith   Fortran Synopsis:
14745c6c1daeSBarry Smith   void PetscLogEventBegin(int e, PetscErrorCode ierr)
14755c6c1daeSBarry Smith 
147610450e9eSJacob Faibussowitsch   Example Usage:
14775c6c1daeSBarry Smith .vb
14785c6c1daeSBarry Smith   PetscLogEvent USER_EVENT;
147910450e9eSJacob Faibussowitsch 
14805c6c1daeSBarry Smith   PetscLogDouble user_event_flops;
14815c6c1daeSBarry Smith   PetscLogEventRegister("User event",0, &USER_EVENT);
14825c6c1daeSBarry Smith   PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0);
14835c6c1daeSBarry Smith   [code segment to monitor]
14845c6c1daeSBarry Smith   PetscLogFlops(user_event_flops);
14855c6c1daeSBarry Smith   PetscLogEventEnd(USER_EVENT, 0, 0, 0, 0);
14865c6c1daeSBarry Smith .ve
14875c6c1daeSBarry Smith 
1488d1f92df0SBarry Smith   Level: intermediate
1489d1f92df0SBarry Smith 
1490811af0c4SBarry Smith   Developer Note:
149110450e9eSJacob Faibussowitsch   `PetscLogEventBegin()` and `PetscLogEventBegin()` return error codes instead of explicitly
149210450e9eSJacob Faibussowitsch   handling the errors that occur in the macro directly because other packages that use this
149310450e9eSJacob Faibussowitsch   macros have used them in their own functions or methods that do not return error codes and it
149410450e9eSJacob Faibussowitsch   would be disruptive to change the current behavior.
1495d0609cedSBarry Smith 
1496d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventEnd()`, `PetscLogFlops()`
14975c6c1daeSBarry Smith M*/
14985c6c1daeSBarry Smith 
14995c6c1daeSBarry Smith /*MC
15005c6c1daeSBarry Smith   PetscLogEventEnd - Log the end of a user event.
15015c6c1daeSBarry Smith 
15025c6c1daeSBarry Smith   Synopsis:
1503aaa7dc30SBarry Smith   #include <petsclog.h>
1504b665b14eSToby Isaac   PetscErrorCode PetscLogEventEnd(PetscLogEvent e, PetscObject o1, PetscObject o2, PetscObject o3, PetscObject o4)
15055c6c1daeSBarry Smith 
15065c6c1daeSBarry Smith   Not Collective
15075c6c1daeSBarry Smith 
15085c6c1daeSBarry Smith   Input Parameters:
1509b665b14eSToby Isaac + e  - `PetscLogEvent` obtained from `PetscLogEventRegister()`
1510baca6076SPierre Jolivet . o1 - object associated with the event, or `NULL`
1511baca6076SPierre Jolivet . o2 - object associated with the event, or `NULL`
1512baca6076SPierre Jolivet . o3 - object associated with the event, or `NULL`
1513baca6076SPierre Jolivet - o4 - object associated with the event, or `NULL`
15145c6c1daeSBarry Smith 
15155c6c1daeSBarry Smith   Fortran Synopsis:
15165c6c1daeSBarry Smith   void PetscLogEventEnd(int e, PetscErrorCode ierr)
15175c6c1daeSBarry Smith 
151810450e9eSJacob Faibussowitsch   Example Usage:
15195c6c1daeSBarry Smith .vb
15205c6c1daeSBarry Smith   PetscLogEvent USER_EVENT;
152110450e9eSJacob Faibussowitsch 
15225c6c1daeSBarry Smith   PetscLogDouble user_event_flops;
152310450e9eSJacob Faibussowitsch   PetscLogEventRegister("User event", 0, &USER_EVENT);
15245c6c1daeSBarry Smith   PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0);
15255c6c1daeSBarry Smith   [code segment to monitor]
15265c6c1daeSBarry Smith   PetscLogFlops(user_event_flops);
15275c6c1daeSBarry Smith   PetscLogEventEnd(USER_EVENT, 0, 0, 0, 0);
15285c6c1daeSBarry Smith .ve
15295c6c1daeSBarry Smith 
15305c6c1daeSBarry Smith   Level: intermediate
15315c6c1daeSBarry Smith 
1532d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogFlops()`
15335c6c1daeSBarry Smith M*/
15345c6c1daeSBarry Smith 
15355c6c1daeSBarry Smith /*@C
15368b08f494SToby Isaac   PetscLogStageGetPerfInfo - Return the performance information about the given stage
15378b08f494SToby Isaac 
1538cc4c1da9SBarry Smith   No Fortran Support
1539cc4c1da9SBarry Smith 
15408b08f494SToby Isaac   Input Parameters:
15418b08f494SToby Isaac . stage - The stage number or `PETSC_DETERMINE` for the current stage
15428b08f494SToby Isaac 
15438b08f494SToby Isaac   Output Parameter:
15448b08f494SToby Isaac . info - This structure is filled with the performance information
15458b08f494SToby Isaac 
15468b08f494SToby Isaac   Level: intermediate
15478b08f494SToby Isaac 
15488b08f494SToby Isaac   Notes:
15498b08f494SToby Isaac   This is a low level routine used by the logging functions in PETSc.
15508b08f494SToby Isaac 
15518b08f494SToby Isaac   A `PETSCLOGHANDLERDEFAULT` must be running for this to work, having been started either with
15525804573cSPierre Jolivet   `PetscLogDefaultBegin()` or from the command line with `-log_view`.  If it was not started,
15530ba5dad9SToby Isaac   all performance statistics in `info` will be zeroed.
15548b08f494SToby Isaac 
15558b08f494SToby Isaac .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogGetDefaultHandler()`
15568b08f494SToby Isaac @*/
15578b08f494SToby Isaac PetscErrorCode PetscLogStageGetPerfInfo(PetscLogStage stage, PetscEventPerfInfo *info)
15588b08f494SToby Isaac {
15598b08f494SToby Isaac   PetscLogHandler     handler;
15608b08f494SToby Isaac   PetscEventPerfInfo *event_info;
15618b08f494SToby Isaac 
15628b08f494SToby Isaac   PetscFunctionBegin;
15638b08f494SToby Isaac   PetscAssertPointer(info, 2);
15640ba5dad9SToby Isaac   PetscCall(PetscLogTryGetHandler(PETSCLOGHANDLERDEFAULT, &handler));
15650ba5dad9SToby Isaac   if (handler) {
15668b08f494SToby Isaac     PetscCall(PetscLogHandlerGetStagePerfInfo(handler, stage, &event_info));
15678b08f494SToby Isaac     *info = *event_info;
15680ba5dad9SToby Isaac   } else {
15690ba5dad9SToby Isaac     PetscCall(PetscInfo(NULL, "Default log handler is not running, PetscLogStageGetPerfInfo() returning zeros\n"));
15700ba5dad9SToby Isaac     PetscCall(PetscMemzero(info, sizeof(*info)));
15710ba5dad9SToby Isaac   }
15728b08f494SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
15738b08f494SToby Isaac }
15748b08f494SToby Isaac 
15758b08f494SToby Isaac /*@C
1576b665b14eSToby Isaac   PetscLogEventGetPerfInfo - Return the performance information about the given event in the given stage
1577b665b14eSToby Isaac 
1578cc4c1da9SBarry Smith   No Fortran Support
1579cc4c1da9SBarry Smith 
1580b665b14eSToby Isaac   Input Parameters:
1581b665b14eSToby Isaac + stage - The stage number or `PETSC_DETERMINE` for the current stage
1582b665b14eSToby Isaac - event - The event number
1583b665b14eSToby Isaac 
1584b665b14eSToby Isaac   Output Parameter:
1585b665b14eSToby Isaac . info - This structure is filled with the performance information
1586b665b14eSToby Isaac 
1587b665b14eSToby Isaac   Level: intermediate
1588b665b14eSToby Isaac 
1589b665b14eSToby Isaac   Note:
1590b665b14eSToby Isaac   This is a low level routine used by the logging functions in PETSc
1591b665b14eSToby Isaac 
15920ba5dad9SToby Isaac   A `PETSCLOGHANDLERDEFAULT` must be running for this to work, having been started either with
15935804573cSPierre Jolivet   `PetscLogDefaultBegin()` or from the command line with `-log_view`.  If it was not started,
15940ba5dad9SToby Isaac   all performance statistics in `info` will be zeroed.
15950ba5dad9SToby Isaac 
1596b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogGetDefaultHandler()`
1597b665b14eSToby Isaac @*/
1598b665b14eSToby Isaac PetscErrorCode PetscLogEventGetPerfInfo(PetscLogStage stage, PetscLogEvent event, PetscEventPerfInfo *info)
1599b665b14eSToby Isaac {
1600b665b14eSToby Isaac   PetscLogHandler     handler;
1601b665b14eSToby Isaac   PetscEventPerfInfo *event_info;
1602b665b14eSToby Isaac 
1603b665b14eSToby Isaac   PetscFunctionBegin;
1604b665b14eSToby Isaac   PetscAssertPointer(info, 3);
16050ba5dad9SToby Isaac   PetscCall(PetscLogTryGetHandler(PETSCLOGHANDLERDEFAULT, &handler));
16060ba5dad9SToby Isaac   if (handler) {
1607dff009beSToby Isaac     PetscCall(PetscLogHandlerGetEventPerfInfo(handler, stage, event, &event_info));
1608b665b14eSToby Isaac     *info = *event_info;
16090ba5dad9SToby Isaac   } else {
16100ba5dad9SToby Isaac     PetscCall(PetscInfo(NULL, "Default log handler is not running, PetscLogEventGetPerfInfo() returning zeros\n"));
16110ba5dad9SToby Isaac     PetscCall(PetscMemzero(info, sizeof(*info)));
16120ba5dad9SToby Isaac   }
1613b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
1614b665b14eSToby Isaac }
1615b665b14eSToby Isaac 
1616cc4c1da9SBarry Smith /*@
1617b665b14eSToby Isaac   PetscLogEventSetDof - Set the nth number of degrees of freedom of a numerical problem associated with this event
1618b665b14eSToby Isaac 
1619b665b14eSToby Isaac   Not Collective
1620b665b14eSToby Isaac 
1621b665b14eSToby Isaac   Input Parameters:
1622b665b14eSToby Isaac + event - The event id to log
1623b665b14eSToby Isaac . n     - The dof index, in [0, 8)
1624b665b14eSToby Isaac - dof   - The number of dofs
1625b665b14eSToby Isaac 
1626b665b14eSToby Isaac   Options Database Key:
1627b665b14eSToby Isaac . -log_view - Activates log summary
1628b665b14eSToby Isaac 
1629b665b14eSToby Isaac   Level: developer
1630b665b14eSToby Isaac 
1631b665b14eSToby Isaac   Note:
1632b665b14eSToby Isaac   This is to enable logging of convergence
1633b665b14eSToby Isaac 
1634b665b14eSToby Isaac .seealso: `PetscLogEventSetError()`, `PetscLogEventRegister()`, `PetscLogGetDefaultHandler()`
1635b665b14eSToby Isaac @*/
1636b665b14eSToby Isaac PetscErrorCode PetscLogEventSetDof(PetscLogEvent event, PetscInt n, PetscLogDouble dof)
1637b665b14eSToby Isaac {
1638b665b14eSToby Isaac   PetscFunctionBegin;
1639b665b14eSToby Isaac   PetscCheck(!(n < 0) && !(n > 7), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Error index %" PetscInt_FMT " is not in [0, 8)", n);
1640dff009beSToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
1641dff009beSToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
1642dff009beSToby Isaac 
1643dff009beSToby Isaac     if (h) {
1644dff009beSToby Isaac       PetscEventPerfInfo *event_info;
1645dff009beSToby Isaac 
1646dff009beSToby Isaac       PetscCall(PetscLogHandlerGetEventPerfInfo(h, PETSC_DEFAULT, event, &event_info));
1647dff009beSToby Isaac       if (event_info) event_info->dof[n] = dof;
1648dff009beSToby Isaac     }
1649b665b14eSToby Isaac   }
1650b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
1651b665b14eSToby Isaac }
1652b665b14eSToby Isaac 
1653cc4c1da9SBarry Smith /*@
1654b665b14eSToby Isaac   PetscLogEventSetError - Set the nth error associated with a numerical problem associated with this event
1655b665b14eSToby Isaac 
1656b665b14eSToby Isaac   Not Collective
1657b665b14eSToby Isaac 
1658b665b14eSToby Isaac   Input Parameters:
1659b665b14eSToby Isaac + event - The event id to log
1660b665b14eSToby Isaac . n     - The error index, in [0, 8)
1661b665b14eSToby Isaac - error - The error
1662b665b14eSToby Isaac 
1663b665b14eSToby Isaac   Options Database Key:
1664b665b14eSToby Isaac . -log_view - Activates log summary
1665b665b14eSToby Isaac 
1666b665b14eSToby Isaac   Level: developer
1667b665b14eSToby Isaac 
1668b665b14eSToby Isaac   Notes:
1669b665b14eSToby Isaac   This is to enable logging of convergence, and enable users to interpret the errors as they wish. For example,
1670b665b14eSToby Isaac   as different norms, or as errors for different fields
1671b665b14eSToby Isaac 
1672b665b14eSToby Isaac   This is a low level routine used by the logging functions in PETSc
1673b665b14eSToby Isaac 
1674b665b14eSToby Isaac .seealso: `PetscLogEventSetDof()`, `PetscLogEventRegister()`, `PetscLogGetDefaultHandler()`
1675b665b14eSToby Isaac @*/
1676b665b14eSToby Isaac PetscErrorCode PetscLogEventSetError(PetscLogEvent event, PetscInt n, PetscLogDouble error)
1677b665b14eSToby Isaac {
1678b665b14eSToby Isaac   PetscFunctionBegin;
1679b665b14eSToby Isaac   PetscCheck(!(n < 0) && !(n > 7), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Error index %" PetscInt_FMT " is not in [0, 8)", n);
1680dff009beSToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
1681dff009beSToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
1682dff009beSToby Isaac 
1683dff009beSToby Isaac     if (h) {
1684dff009beSToby Isaac       PetscEventPerfInfo *event_info;
1685dff009beSToby Isaac 
1686dff009beSToby Isaac       PetscCall(PetscLogHandlerGetEventPerfInfo(h, PETSC_DEFAULT, event, &event_info));
1687dff009beSToby Isaac       if (event_info) event_info->errors[n] = error;
1688dff009beSToby Isaac     }
1689b665b14eSToby Isaac   }
1690b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
1691b665b14eSToby Isaac }
1692b665b14eSToby Isaac 
1693cc4c1da9SBarry Smith /*@
16945c6c1daeSBarry Smith   PetscLogEventGetId - Returns the event id when given the event name.
16955c6c1daeSBarry Smith 
16965c6c1daeSBarry Smith   Not Collective
16975c6c1daeSBarry Smith 
16985c6c1daeSBarry Smith   Input Parameter:
16995c6c1daeSBarry Smith . name - The event name
17005c6c1daeSBarry Smith 
17015c6c1daeSBarry Smith   Output Parameter:
1702c5deb1d5SJed Brown . event - The event, or -1 if no event with that name exists
17035c6c1daeSBarry Smith 
17045c6c1daeSBarry Smith   Level: intermediate
17055c6c1daeSBarry Smith 
1706d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogStageGetId()`
17075c6c1daeSBarry Smith @*/
1708d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogEventGetId(const char name[], PetscLogEvent *event)
1709d71ae5a4SJacob Faibussowitsch {
1710b665b14eSToby Isaac   PetscLogState state;
17115c6c1daeSBarry Smith 
17125c6c1daeSBarry Smith   PetscFunctionBegin;
1713b665b14eSToby Isaac   *event = -1;
1714b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
1715b665b14eSToby Isaac   if (state) PetscCall(PetscLogStateGetEventFromName(state, name, event));
17163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
17175c6c1daeSBarry Smith }
17185c6c1daeSBarry Smith 
1719cc4c1da9SBarry Smith /*@
172053e0a2f3SToby Isaac   PetscLogEventGetName - Returns the event name when given the event id.
172153e0a2f3SToby Isaac 
172253e0a2f3SToby Isaac   Not Collective
172353e0a2f3SToby Isaac 
172453e0a2f3SToby Isaac   Input Parameter:
172553e0a2f3SToby Isaac . event - The event
172653e0a2f3SToby Isaac 
172753e0a2f3SToby Isaac   Output Parameter:
172853e0a2f3SToby Isaac . name - The event name
172953e0a2f3SToby Isaac 
173053e0a2f3SToby Isaac   Level: intermediate
173153e0a2f3SToby Isaac 
173253e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()`
173353e0a2f3SToby Isaac @*/
1734cc4c1da9SBarry Smith PetscErrorCode PetscLogEventGetName(PetscLogEvent event, const char *name[])
173553e0a2f3SToby Isaac {
173653e0a2f3SToby Isaac   PetscLogEventInfo event_info;
173753e0a2f3SToby Isaac   PetscLogState     state;
173853e0a2f3SToby Isaac 
173953e0a2f3SToby Isaac   PetscFunctionBegin;
1740b665b14eSToby Isaac   *name = NULL;
174153e0a2f3SToby Isaac   PetscCall(PetscLogGetState(&state));
1742b665b14eSToby Isaac   if (!state) PetscFunctionReturn(PETSC_SUCCESS);
174353e0a2f3SToby Isaac   PetscCall(PetscLogStateEventGetInfo(state, event, &event_info));
174453e0a2f3SToby Isaac   *name = event_info.name;
174553e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
174653e0a2f3SToby Isaac }
174753e0a2f3SToby Isaac 
174853e0a2f3SToby Isaac /*@
174953e0a2f3SToby 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.
175053e0a2f3SToby Isaac 
175153e0a2f3SToby Isaac   Not collective
175253e0a2f3SToby Isaac 
175353e0a2f3SToby Isaac   Level: advanced
175453e0a2f3SToby Isaac 
175553e0a2f3SToby Isaac   Notes:
175653e0a2f3SToby 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()`).
175753e0a2f3SToby Isaac 
175853e0a2f3SToby Isaac   Other log handlers (such as the nested handler, `PetscLogNestedBegin()`) will ignore this function.
175953e0a2f3SToby Isaac 
1760b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogEventDeactivatePush()`, `PetscLogEventDeactivatePop()`, `PetscLogEventsResume()`, `PetscLogGetDefaultHandler()`
176153e0a2f3SToby Isaac @*/
176253e0a2f3SToby Isaac PetscErrorCode PetscLogEventsPause(void)
176353e0a2f3SToby Isaac {
176453e0a2f3SToby Isaac   PetscFunctionBegin;
1765dff009beSToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
1766dff009beSToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
1767dff009beSToby Isaac 
1768dff009beSToby Isaac     if (h) PetscCall(PetscLogHandlerEventsPause(h));
1769dff009beSToby Isaac   }
177053e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
177153e0a2f3SToby Isaac }
177253e0a2f3SToby Isaac 
177353e0a2f3SToby Isaac /*@
177453e0a2f3SToby Isaac   PetscLogEventsResume - Return logging to normal behavior after it was paused with `PetscLogEventsPause()`.
177553e0a2f3SToby Isaac 
177653e0a2f3SToby Isaac   Not collective
177753e0a2f3SToby Isaac 
177853e0a2f3SToby Isaac   Level: advanced
177953e0a2f3SToby Isaac 
1780b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogEventDeactivatePush()`, `PetscLogEventDeactivatePop()`, `PetscLogEventsPause()`, `PetscLogGetDefaultHandler()`
178153e0a2f3SToby Isaac @*/
178253e0a2f3SToby Isaac PetscErrorCode PetscLogEventsResume(void)
178353e0a2f3SToby Isaac {
178453e0a2f3SToby Isaac   PetscFunctionBegin;
1785dff009beSToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
1786dff009beSToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
1787dff009beSToby Isaac 
1788dff009beSToby Isaac     if (h) PetscCall(PetscLogHandlerEventsResume(h));
1789dff009beSToby Isaac   }
179053e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
179153e0a2f3SToby Isaac }
179253e0a2f3SToby Isaac 
17931c1ad86eSToby Isaac /*------------------------------------------------ Class Functions --------------------------------------------------*/
17941c1ad86eSToby Isaac 
17951c1ad86eSToby Isaac /*MC
17961c1ad86eSToby Isaac    PetscLogObjectCreate - Log the creation of a `PetscObject`
17971c1ad86eSToby Isaac 
17981c1ad86eSToby Isaac    Synopsis:
17991c1ad86eSToby Isaac    #include <petsclog.h>
18001c1ad86eSToby Isaac    PetscErrorCode PetscLogObjectCreate(PetscObject h)
18011c1ad86eSToby Isaac 
18021c1ad86eSToby Isaac    Not Collective
18031c1ad86eSToby Isaac 
18041c1ad86eSToby Isaac    Input Parameters:
18051c1ad86eSToby Isaac .  h - A `PetscObject`
18061c1ad86eSToby Isaac 
18071c1ad86eSToby Isaac    Level: developer
18081c1ad86eSToby Isaac 
18091c1ad86eSToby Isaac    Developer Note:
18101c1ad86eSToby Isaac      Called internally by PETSc when creating objects: users do not need to call this directly.
1811b665b14eSToby Isaac      Notification of the object creation is sent to each `PetscLogHandler` that is running.
18121c1ad86eSToby Isaac 
1813b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogObjectDestroy()`
18141c1ad86eSToby Isaac M*/
18151c1ad86eSToby Isaac 
18161c1ad86eSToby Isaac /*MC
18171c1ad86eSToby Isaac    PetscLogObjectDestroy - Logs the destruction of a `PetscObject`
18181c1ad86eSToby Isaac 
18191c1ad86eSToby Isaac    Synopsis:
18201c1ad86eSToby Isaac    #include <petsclog.h>
18211c1ad86eSToby Isaac    PetscErrorCode PetscLogObjectDestroy(PetscObject h)
18221c1ad86eSToby Isaac 
18231c1ad86eSToby Isaac    Not Collective
18241c1ad86eSToby Isaac 
18251c1ad86eSToby Isaac    Input Parameters:
18261c1ad86eSToby Isaac .  h - A `PetscObject`
18271c1ad86eSToby Isaac 
18281c1ad86eSToby Isaac    Level: developer
18291c1ad86eSToby Isaac 
18301c1ad86eSToby Isaac    Developer Note:
18311c1ad86eSToby Isaac      Called internally by PETSc when destroying objects: users do not need to call this directly.
1832b665b14eSToby Isaac      Notification of the object creation is sent to each `PetscLogHandler` that is running.
18331c1ad86eSToby Isaac 
1834b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogObjectCreate()`
18351c1ad86eSToby Isaac M*/
18361c1ad86eSToby Isaac 
1837cc4c1da9SBarry Smith /*@
183853e0a2f3SToby Isaac   PetscLogClassGetClassId - Returns the `PetscClassId` when given the class name.
183953e0a2f3SToby Isaac 
184053e0a2f3SToby Isaac   Not Collective
184153e0a2f3SToby Isaac 
184253e0a2f3SToby Isaac   Input Parameter:
184353e0a2f3SToby Isaac . name - The class name
184453e0a2f3SToby Isaac 
184553e0a2f3SToby Isaac   Output Parameter:
184653e0a2f3SToby Isaac . classid - The `PetscClassId` id, or -1 if no class with that name exists
184753e0a2f3SToby Isaac 
184853e0a2f3SToby Isaac   Level: intermediate
184953e0a2f3SToby Isaac 
185053e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogStageGetId()`
185153e0a2f3SToby Isaac @*/
185253e0a2f3SToby Isaac PetscErrorCode PetscLogClassGetClassId(const char name[], PetscClassId *classid)
185353e0a2f3SToby Isaac {
185453e0a2f3SToby Isaac   PetscLogClass     log_class;
185553e0a2f3SToby Isaac   PetscLogClassInfo class_info;
185653e0a2f3SToby Isaac   PetscLogState     state;
185753e0a2f3SToby Isaac 
185853e0a2f3SToby Isaac   PetscFunctionBegin;
1859b665b14eSToby Isaac   *classid = -1;
186053e0a2f3SToby Isaac   PetscCall(PetscLogGetState(&state));
1861b665b14eSToby Isaac   if (!state) PetscFunctionReturn(PETSC_SUCCESS);
186253e0a2f3SToby Isaac   PetscCall(PetscLogStateGetClassFromName(state, name, &log_class));
186353e0a2f3SToby Isaac   if (log_class < 0) {
186453e0a2f3SToby Isaac     *classid = -1;
186553e0a2f3SToby Isaac     PetscFunctionReturn(PETSC_SUCCESS);
186653e0a2f3SToby Isaac   }
186753e0a2f3SToby Isaac   PetscCall(PetscLogStateClassGetInfo(state, log_class, &class_info));
186853e0a2f3SToby Isaac   *classid = class_info.classid;
186953e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
187053e0a2f3SToby Isaac }
187153e0a2f3SToby Isaac 
187253e0a2f3SToby Isaac /*@C
187353e0a2f3SToby Isaac   PetscLogClassIdGetName - Returns a `PetscClassId`'s name.
187453e0a2f3SToby Isaac 
187553e0a2f3SToby Isaac   Not Collective
187653e0a2f3SToby Isaac 
187753e0a2f3SToby Isaac   Input Parameter:
187853e0a2f3SToby Isaac . classid - A `PetscClassId`
187953e0a2f3SToby Isaac 
188053e0a2f3SToby Isaac   Output Parameter:
188153e0a2f3SToby Isaac . name - The class name
188253e0a2f3SToby Isaac 
188353e0a2f3SToby Isaac   Level: intermediate
188453e0a2f3SToby Isaac 
188553e0a2f3SToby Isaac .seealso: [](ch_profiling), `PetscLogClassRegister()`, `PetscLogClassBegin()`, `PetscLogClassEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadClass()`
188653e0a2f3SToby Isaac @*/
188753e0a2f3SToby Isaac PetscErrorCode PetscLogClassIdGetName(PetscClassId classid, const char **name)
188853e0a2f3SToby Isaac {
188953e0a2f3SToby Isaac   PetscLogClass     log_class;
189053e0a2f3SToby Isaac   PetscLogClassInfo class_info;
189153e0a2f3SToby Isaac   PetscLogState     state;
189253e0a2f3SToby Isaac 
189353e0a2f3SToby Isaac   PetscFunctionBegin;
189453e0a2f3SToby Isaac   PetscCall(PetscLogGetState(&state));
189553e0a2f3SToby Isaac   PetscCall(PetscLogStateGetClassFromClassId(state, classid, &log_class));
189653e0a2f3SToby Isaac   PetscCall(PetscLogStateClassGetInfo(state, log_class, &class_info));
189753e0a2f3SToby Isaac   *name = class_info.name;
189853e0a2f3SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
189953e0a2f3SToby Isaac }
190053e0a2f3SToby Isaac 
19015c6c1daeSBarry Smith /*------------------------------------------------ Output Functions -------------------------------------------------*/
1902cc4c1da9SBarry Smith /*@
19035c6c1daeSBarry Smith   PetscLogDump - Dumps logs of objects to a file. This file is intended to
19045c6c1daeSBarry Smith   be read by bin/petscview. This program no longer exists.
19055c6c1daeSBarry Smith 
1906811af0c4SBarry Smith   Collective on `PETSC_COMM_WORLD`
19075c6c1daeSBarry Smith 
19085c6c1daeSBarry Smith   Input Parameter:
1909aec76313SJacob Faibussowitsch . sname - an optional file name
19105c6c1daeSBarry Smith 
191110450e9eSJacob Faibussowitsch   Example Usage:
19125c6c1daeSBarry Smith .vb
19135c6c1daeSBarry Smith   PetscInitialize(...);
1914b665b14eSToby Isaac   PetscLogDefaultBegin();
1915b665b14eSToby Isaac   // ... code ...
19165c6c1daeSBarry Smith   PetscLogDump(filename);
19175c6c1daeSBarry Smith   PetscFinalize();
19185c6c1daeSBarry Smith .ve
19195c6c1daeSBarry Smith 
1920d1f92df0SBarry Smith   Level: advanced
1921d1f92df0SBarry Smith 
1922811af0c4SBarry Smith   Note:
192337fdd005SBarry Smith   The default file name is Log.<rank> where <rank> is the MPI process rank. If no name is specified,
19245c6c1daeSBarry Smith   this file will be used.
19255c6c1daeSBarry Smith 
1926b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogDefaultBegin()`, `PetscLogView()`, `PetscLogGetDefaultHandler()`
19275c6c1daeSBarry Smith @*/
1928d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogDump(const char sname[])
1929d71ae5a4SJacob Faibussowitsch {
1930b665b14eSToby Isaac   PetscLogHandler handler;
19315c6c1daeSBarry Smith 
19325c6c1daeSBarry Smith   PetscFunctionBegin;
1933294de794SToby Isaac   PetscCall(PetscLogGetHandler(PETSCLOGHANDLERDEFAULT, &handler));
1934dff009beSToby Isaac   PetscCall(PetscLogHandlerDump(handler, sname));
1935b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
19365c6c1daeSBarry Smith }
1937b665b14eSToby Isaac 
1938cc4c1da9SBarry Smith /*@
1939b665b14eSToby Isaac   PetscLogMPEDump - Dumps the MPE logging info to file for later use with Jumpshot.
1940b665b14eSToby Isaac 
19418f14a041SBarry Smith   Collective on `PETSC_COMM_WORLD`
1942b665b14eSToby Isaac 
1943b665b14eSToby Isaac   Input Parameter:
1944b665b14eSToby Isaac . sname - filename for the MPE logfile
1945b665b14eSToby Isaac 
1946b665b14eSToby Isaac   Level: advanced
1947b665b14eSToby Isaac 
1948b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogDump()`, `PetscLogMPEBegin()`
1949b665b14eSToby Isaac @*/
1950b665b14eSToby Isaac PetscErrorCode PetscLogMPEDump(const char sname[])
1951b665b14eSToby Isaac {
1952b665b14eSToby Isaac   PetscFunctionBegin;
1953b665b14eSToby Isaac   #if defined(PETSC_HAVE_MPE)
1954b665b14eSToby Isaac   if (PetscBeganMPE) {
1955b665b14eSToby Isaac     char name[PETSC_MAX_PATH_LEN];
1956b665b14eSToby Isaac 
1957b665b14eSToby Isaac     PetscCall(PetscInfo(0, "Finalizing MPE.\n"));
1958b665b14eSToby Isaac     if (sname) {
1959b665b14eSToby Isaac       PetscCall(PetscStrncpy(name, sname, sizeof(name)));
19605c6c1daeSBarry Smith     } else {
1961b665b14eSToby Isaac       PetscCall(PetscGetProgramName(name, sizeof(name)));
19625c6c1daeSBarry Smith     }
1963b665b14eSToby Isaac     PetscCall(MPE_Finish_log(name));
19645c6c1daeSBarry Smith   } else {
1965b665b14eSToby Isaac     PetscCall(PetscInfo(0, "Not finalizing MPE (not started by PETSc).\n"));
19665c6c1daeSBarry Smith   }
1967c2a741eeSJunchao Zhang   #else
1968b665b14eSToby Isaac   SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP_SYS, "PETSc was configured without MPE support, reconfigure with --with-mpe or --download-mpe");
1969c2a741eeSJunchao Zhang   #endif
19703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19715c6c1daeSBarry Smith }
19725c6c1daeSBarry Smith 
1973ffeef943SBarry Smith /*@
19747d6c928cSSatish Balay   PetscLogView - Prints a summary of the logging.
19755c6c1daeSBarry Smith 
19768f14a041SBarry Smith   Collective
19775c6c1daeSBarry Smith 
19785c6c1daeSBarry Smith   Input Parameter:
1979f14045dbSBarry Smith . viewer - an ASCII viewer
19805c6c1daeSBarry Smith 
19815c6c1daeSBarry Smith   Options Database Keys:
1982bb1d7374SBarry Smith + -log_view [:filename]                    - Prints summary of log information
1983bb1d7374SBarry Smith . -log_view :filename.py:ascii_info_detail - Saves logging information from each process as a Python file
1984607d249eSBarry 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)
1985d0a29bd7SConnor 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)
1986156b51fbSBarry Smith . -log_view_memory                         - Also display memory usage in each event
1987156b51fbSBarry Smith . -log_view_gpu_time                       - Also display time in each event for GPU kernels (Note this may slow the computation)
1988*5268dc8aSHong Zhang . -log_view_gpu_energy                     - Also display energy (estimated with power*gtime) in Joules for GPU kernels
1989*5268dc8aSHong Zhang . -log_view_gpu_energy_meter               - [Experimental] Also display energy (readings from energy meters) in Joules for GPU kernels. This option is ignored if -log_view_gpu_energy is provided.
1990811af0c4SBarry Smith . -log_all                                 - Saves a file Log.rank for each MPI rank with details of each step of the computation
1991bb1d7374SBarry Smith - -log_trace [filename]                    - Displays a trace of what each process is doing
19925c6c1daeSBarry Smith 
1993d1f92df0SBarry Smith   Level: beginner
1994d1f92df0SBarry Smith 
19955c6c1daeSBarry Smith   Notes:
1996da81f932SPierre Jolivet   It is possible to control the logging programmatically but we recommend using the options database approach whenever possible
19975c6c1daeSBarry Smith   By default the summary is printed to stdout.
19985c6c1daeSBarry Smith 
1999bb1d7374SBarry Smith   Before calling this routine you must have called either PetscLogDefaultBegin() or PetscLogNestedBegin()
2000bb1d7374SBarry Smith 
2001bb1d7374SBarry Smith   If PETSc is configured with --with-logging=0 then this functionality is not available
2002bb1d7374SBarry Smith 
2003607d249eSBarry Smith   To view the nested XML format filename.xml first copy  ${PETSC_DIR}/share/petsc/xml/performance_xml2html.xsl to the current
2004607d249eSBarry Smith   directory then open filename.xml with your browser. Specific notes for certain browsers
20051d27aa22SBarry Smith .vb
20061d27aa22SBarry Smith     Firefox and Internet explorer - simply open the file
20071d27aa22SBarry Smith     Google Chrome - you must start up Chrome with the option --allow-file-access-from-files
20081d27aa22SBarry Smith     Safari - see https://ccm.net/faq/36342-safari-how-to-enable-local-file-access
20091d27aa22SBarry Smith .ve
20101d27aa22SBarry 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
2011607d249eSBarry Smith   your browser.
20122add09c0SLisandro Dalcin   Alternatively, use the script ${PETSC_DIR}/lib/petsc/bin/petsc-performance-view to automatically open a new browser
20132add09c0SLisandro Dalcin   window and render the XML log file contents.
2014607d249eSBarry Smith 
2015bb1d7374SBarry Smith   The nested XML format was kindly donated by Koos Huijssen and Christiaan M. Klaij  MARITIME  RESEARCH  INSTITUTE  NETHERLANDS
2016bb1d7374SBarry Smith 
20171d27aa22SBarry Smith   The Flame Graph output can be visualised using either the original Flame Graph script <https://github.com/brendangregg/FlameGraph>
20181d27aa22SBarry Smith   or using speedscope <https://www.speedscope.app>.
2019d0a29bd7SConnor Ward   Old XML profiles may be converted into this format using the script ${PETSC_DIR}/lib/petsc/bin/xml2flamegraph.py.
2020d0a29bd7SConnor Ward 
2021d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogDefaultBegin()`, `PetscLogDump()`
20225c6c1daeSBarry Smith @*/
2023d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogView(PetscViewer viewer)
2024d71ae5a4SJacob Faibussowitsch {
2025f14045dbSBarry Smith   PetscBool         isascii;
2026f14045dbSBarry Smith   PetscViewerFormat format;
2027b665b14eSToby Isaac   int               stage;
2028b665b14eSToby Isaac   PetscLogState     state;
2029b665b14eSToby Isaac   PetscIntStack     temp_stack;
2030b665b14eSToby Isaac   PetscLogHandler   handler;
2031b665b14eSToby Isaac   PetscBool         is_empty;
20325c6c1daeSBarry Smith 
20335c6c1daeSBarry Smith   PetscFunctionBegin;
2034b665b14eSToby Isaac   PetscCall(PetscLogGetState(&state));
203537b78d16SBarry Smith   /* Pop off any stages the user forgot to remove */
2036b665b14eSToby Isaac   PetscCall(PetscIntStackCreate(&temp_stack));
2037b665b14eSToby Isaac   PetscCall(PetscLogStateGetCurrentStage(state, &stage));
203837b78d16SBarry Smith   while (stage >= 0) {
2039b665b14eSToby Isaac     PetscCall(PetscLogStagePop());
2040b665b14eSToby Isaac     PetscCall(PetscIntStackPush(temp_stack, stage));
2041b665b14eSToby Isaac     PetscCall(PetscLogStateGetCurrentStage(state, &stage));
204237b78d16SBarry Smith   }
20439566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
204428b400f6SJacob Faibussowitsch   PetscCheck(isascii, PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Currently can only view logging to ASCII");
20459566063dSJacob Faibussowitsch   PetscCall(PetscViewerGetFormat(viewer, &format));
2046b665b14eSToby Isaac   if (format == PETSC_VIEWER_ASCII_XML || format == PETSC_VIEWER_ASCII_FLAMEGRAPH) {
2047294de794SToby Isaac     PetscCall(PetscLogGetHandler(PETSCLOGHANDLERNESTED, &handler));
2048b665b14eSToby Isaac     PetscCall(PetscLogHandlerView(handler, viewer));
2049b665b14eSToby Isaac   } else {
2050294de794SToby Isaac     PetscCall(PetscLogGetHandler(PETSCLOGHANDLERDEFAULT, &handler));
2051b665b14eSToby Isaac     PetscCall(PetscLogHandlerView(handler, viewer));
20525c6c1daeSBarry Smith   }
2053b665b14eSToby Isaac   PetscCall(PetscIntStackEmpty(temp_stack, &is_empty));
2054b665b14eSToby Isaac   while (!is_empty) {
2055b665b14eSToby Isaac     PetscCall(PetscIntStackPop(temp_stack, &stage));
2056b665b14eSToby Isaac     PetscCall(PetscLogStagePush(stage));
2057b665b14eSToby Isaac     PetscCall(PetscIntStackEmpty(temp_stack, &is_empty));
2058b665b14eSToby Isaac   }
2059b665b14eSToby Isaac   PetscCall(PetscIntStackDestroy(temp_stack));
20603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
20615c6c1daeSBarry Smith }
20625c6c1daeSBarry Smith 
2063f14045dbSBarry Smith /*@C
2064811af0c4SBarry Smith   PetscLogViewFromOptions - Processes command line options to determine if/how a `PetscLog` is to be viewed.
2065f14045dbSBarry Smith 
2066811af0c4SBarry Smith   Collective on `PETSC_COMM_WORLD`
2067f14045dbSBarry Smith 
2068811af0c4SBarry Smith   Level: developer
2069f14045dbSBarry Smith 
2070d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogView()`
2071f14045dbSBarry Smith @*/
2072d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogViewFromOptions(void)
2073d71ae5a4SJacob Faibussowitsch {
2074ad2e3d55SToby Isaac   PetscInt          n_max = PETSC_LOG_VIEW_FROM_OPTIONS_MAX;
2075ad2e3d55SToby Isaac   PetscViewer       viewers[PETSC_LOG_VIEW_FROM_OPTIONS_MAX];
2076ad2e3d55SToby Isaac   PetscViewerFormat formats[PETSC_LOG_VIEW_FROM_OPTIONS_MAX];
2077f14045dbSBarry Smith   PetscBool         flg;
2078f14045dbSBarry Smith 
2079f14045dbSBarry Smith   PetscFunctionBegin;
2080648c30bcSBarry Smith   PetscCall(PetscOptionsCreateViewers(PETSC_COMM_WORLD, NULL, NULL, "-log_view", &n_max, viewers, formats, &flg));
20810bc1c2e8SToby Isaac   /*
20820bc1c2e8SToby Isaac      PetscLogHandlerView_Default_Info() wants to be sure that the only objects still around are these viewers, so keep track of how many there are
20830bc1c2e8SToby Isaac    */
20840bc1c2e8SToby Isaac   PetscLogNumViewersCreated = n_max;
2085ad2e3d55SToby Isaac   for (PetscInt i = 0; i < n_max; i++) {
20860bc1c2e8SToby Isaac     PetscInt refct;
20870bc1c2e8SToby Isaac 
2088ad2e3d55SToby Isaac     PetscCall(PetscViewerPushFormat(viewers[i], formats[i]));
2089ad2e3d55SToby Isaac     PetscCall(PetscLogView(viewers[i]));
2090ad2e3d55SToby Isaac     PetscCall(PetscViewerPopFormat(viewers[i]));
20910bc1c2e8SToby Isaac     PetscCall(PetscObjectGetReference((PetscObject)viewers[i], &refct));
2092648c30bcSBarry Smith     PetscCall(PetscViewerDestroy(&viewers[i]));
20930bc1c2e8SToby Isaac     if (refct == 1) PetscLogNumViewersDestroyed++;
2094f14045dbSBarry Smith   }
2095a0449d98SPierre Jolivet   PetscLogNumViewersDestroyed = 0;
20963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2097f14045dbSBarry Smith }
2098f14045dbSBarry Smith 
2099b665b14eSToby Isaac PETSC_INTERN PetscErrorCode PetscLogHandlerNestedSetThreshold(PetscLogHandler, PetscLogDouble, PetscLogDouble *);
2100b665b14eSToby Isaac 
2101b665b14eSToby Isaac /*@
2102b665b14eSToby Isaac   PetscLogSetThreshold - Set the threshold time for logging the events; this is a percentage out of 100, so 1. means any event
2103b665b14eSToby Isaac   that takes 1 or more percent of the time.
2104b665b14eSToby Isaac 
21058f14a041SBarry Smith   Logically Collective on `PETSC_COMM_WORLD`
2106b665b14eSToby Isaac 
2107b665b14eSToby Isaac   Input Parameter:
2108b665b14eSToby Isaac . newThresh - the threshold to use
2109b665b14eSToby Isaac 
2110b665b14eSToby Isaac   Output Parameter:
2111b665b14eSToby Isaac . oldThresh - the previously set threshold value
2112b665b14eSToby Isaac 
2113b665b14eSToby Isaac   Options Database Keys:
2114b665b14eSToby Isaac . -log_view :filename.xml:ascii_xml - Prints an XML summary of flop and timing information to the file
2115b665b14eSToby Isaac 
2116b665b14eSToby Isaac   Example Usage:
2117b665b14eSToby Isaac .vb
2118b665b14eSToby Isaac   PetscInitialize(...);
2119b665b14eSToby Isaac   PetscLogNestedBegin();
2120b665b14eSToby Isaac   PetscLogSetThreshold(0.1,&oldthresh);
2121b665b14eSToby Isaac   // ... code ...
2122b665b14eSToby Isaac   PetscLogView(viewer);
2123b665b14eSToby Isaac   PetscFinalize();
2124b665b14eSToby Isaac .ve
2125b665b14eSToby Isaac 
2126b665b14eSToby Isaac   Level: advanced
2127b665b14eSToby Isaac 
2128b665b14eSToby Isaac   Note:
2129b665b14eSToby Isaac   This threshold is only used by the nested log handler
2130b665b14eSToby Isaac 
2131b665b14eSToby Isaac .seealso: `PetscLogDump()`, `PetscLogView()`, `PetscLogTraceBegin()`, `PetscLogDefaultBegin()`,
2132b665b14eSToby Isaac           `PetscLogNestedBegin()`
2133b665b14eSToby Isaac @*/
2134b665b14eSToby Isaac PetscErrorCode PetscLogSetThreshold(PetscLogDouble newThresh, PetscLogDouble *oldThresh)
2135b665b14eSToby Isaac {
2136b665b14eSToby Isaac   PetscLogHandler handler;
2137b665b14eSToby Isaac 
2138b665b14eSToby Isaac   PetscFunctionBegin;
2139294de794SToby Isaac   PetscCall(PetscLogTryGetHandler(PETSCLOGHANDLERNESTED, &handler));
2140b665b14eSToby Isaac   PetscCall(PetscLogHandlerNestedSetThreshold(handler, newThresh, oldThresh));
2141b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
2142b665b14eSToby Isaac }
2143b665b14eSToby Isaac 
21445c6c1daeSBarry Smith /*----------------------------------------------- Counter Functions -------------------------------------------------*/
2145cc4c1da9SBarry Smith /*@
21465c6c1daeSBarry Smith   PetscGetFlops - Returns the number of flops used on this processor
21475c6c1daeSBarry Smith   since the program began.
21485c6c1daeSBarry Smith 
21495c6c1daeSBarry Smith   Not Collective
21505c6c1daeSBarry Smith 
21515c6c1daeSBarry Smith   Output Parameter:
215210450e9eSJacob Faibussowitsch . flops - number of floating point operations
21535c6c1daeSBarry Smith 
2154d1f92df0SBarry Smith   Level: intermediate
2155d1f92df0SBarry Smith 
21565c6c1daeSBarry Smith   Notes:
21575c6c1daeSBarry Smith   A global counter logs all PETSc flop counts.  The user can use
2158811af0c4SBarry Smith   `PetscLogFlops()` to increment this counter to include flops for the
21595c6c1daeSBarry Smith   application code.
21605c6c1daeSBarry Smith 
21614b7c4d4dSPierre Jolivet   A separate counter `PetscLogGpuFlops()` logs the flops that occur on any GPU associated with this MPI rank
2162811af0c4SBarry Smith 
21634b7c4d4dSPierre Jolivet .seealso: [](ch_profiling), `PetscLogGpuFlops()`, `PetscTime()`, `PetscLogFlops()`
21645c6c1daeSBarry Smith @*/
2165d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscGetFlops(PetscLogDouble *flops)
2166d71ae5a4SJacob Faibussowitsch {
21675c6c1daeSBarry Smith   PetscFunctionBegin;
21685c6c1daeSBarry Smith   *flops = petsc_TotalFlops;
21693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21705c6c1daeSBarry Smith }
21715c6c1daeSBarry Smith 
21721c1ad86eSToby Isaac /*@C
21731c1ad86eSToby Isaac   PetscLogObjectState - Record information about an object with the default log handler
21741c1ad86eSToby Isaac 
21751c1ad86eSToby Isaac   Not Collective
21761c1ad86eSToby Isaac 
21771c1ad86eSToby Isaac   Input Parameters:
21781c1ad86eSToby Isaac + obj    - the `PetscObject`
21791c1ad86eSToby Isaac . format - a printf-style format string
21801c1ad86eSToby Isaac - ...    - printf arguments to format
21811c1ad86eSToby Isaac 
21821c1ad86eSToby Isaac   Level: developer
21831c1ad86eSToby Isaac 
2184b665b14eSToby Isaac .seealso: [](ch_profiling), `PetscLogObjectCreate()`, `PetscLogObjectDestroy()`, `PetscLogGetDefaultHandler()`
21851c1ad86eSToby Isaac @*/
2186d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogObjectState(PetscObject obj, const char format[], ...)
2187d71ae5a4SJacob Faibussowitsch {
21885c6c1daeSBarry Smith   PetscFunctionBegin;
2189dff009beSToby Isaac   for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
2190dff009beSToby Isaac     PetscLogHandler h = PetscLogHandlers[i].handler;
2191dff009beSToby Isaac 
2192dff009beSToby Isaac     if (h) {
2193dff009beSToby Isaac       va_list Argp;
21945c6c1daeSBarry Smith       va_start(Argp, format);
2195dff009beSToby Isaac       PetscCall(PetscLogHandlerLogObjectState_Internal(h, obj, format, Argp));
21965c6c1daeSBarry Smith       va_end(Argp);
2197b665b14eSToby Isaac     }
2198dff009beSToby Isaac   }
21993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
22005c6c1daeSBarry Smith }
22015c6c1daeSBarry Smith 
22025c6c1daeSBarry Smith /*MC
22035c6c1daeSBarry Smith   PetscLogFlops - Adds floating point operations to the global counter.
22045c6c1daeSBarry Smith 
22055c6c1daeSBarry Smith   Synopsis:
2206aaa7dc30SBarry Smith   #include <petsclog.h>
22075c6c1daeSBarry Smith   PetscErrorCode PetscLogFlops(PetscLogDouble f)
22085c6c1daeSBarry Smith 
22095c6c1daeSBarry Smith   Not Collective
22105c6c1daeSBarry Smith 
22115c6c1daeSBarry Smith   Input Parameter:
22125c6c1daeSBarry Smith . f - flop counter
22135c6c1daeSBarry Smith 
221410450e9eSJacob Faibussowitsch   Example Usage:
22155c6c1daeSBarry Smith .vb
22165c6c1daeSBarry Smith   PetscLogEvent USER_EVENT;
221710450e9eSJacob Faibussowitsch 
22185c6c1daeSBarry Smith   PetscLogEventRegister("User event", 0, &USER_EVENT);
22195c6c1daeSBarry Smith   PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0);
22205c6c1daeSBarry Smith   [code segment to monitor]
22215c6c1daeSBarry Smith   PetscLogFlops(user_flops)
22225c6c1daeSBarry Smith   PetscLogEventEnd(USER_EVENT, 0, 0, 0, 0);
22235c6c1daeSBarry Smith .ve
22245c6c1daeSBarry Smith 
2225d1f92df0SBarry Smith   Level: intermediate
2226d1f92df0SBarry Smith 
2227811af0c4SBarry Smith   Note:
222810450e9eSJacob Faibussowitsch    A global counter logs all PETSc flop counts. The user can use PetscLogFlops() to increment
222910450e9eSJacob Faibussowitsch    this counter to include flops for the application code.
22305c6c1daeSBarry Smith 
22314b7c4d4dSPierre Jolivet .seealso: [](ch_profiling), `PetscLogGpuFlops()`, `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscGetFlops()`
22325c6c1daeSBarry Smith M*/
22335c6c1daeSBarry Smith 
22345c6c1daeSBarry Smith /*MC
223510450e9eSJacob Faibussowitsch   PetscPreLoadBegin - Begin a segment of code that may be preloaded (run twice) to get accurate
223610450e9eSJacob Faibussowitsch   timings
22375c6c1daeSBarry Smith 
22385c6c1daeSBarry Smith   Synopsis:
2239aaa7dc30SBarry Smith   #include <petsclog.h>
22405c6c1daeSBarry Smith   void PetscPreLoadBegin(PetscBool flag, char *name);
22415c6c1daeSBarry Smith 
22425c6c1daeSBarry Smith   Not Collective
22435c6c1daeSBarry Smith 
2244d8d19677SJose E. Roman   Input Parameters:
224510450e9eSJacob Faibussowitsch + flag - `PETSC_TRUE` to run twice, `PETSC_FALSE` to run once, may be overridden with command
224610450e9eSJacob Faibussowitsch          line option `-preload true|false`
224710450e9eSJacob Faibussowitsch - name - name of first stage (lines of code timed separately with `-log_view`) to be preloaded
22485c6c1daeSBarry Smith 
224910450e9eSJacob Faibussowitsch   Example Usage:
22505c6c1daeSBarry Smith .vb
225110450e9eSJacob Faibussowitsch   PetscPreLoadBegin(PETSC_TRUE, "first stage");
225210450e9eSJacob Faibussowitsch   // lines of code
22535c6c1daeSBarry Smith   PetscPreLoadStage("second stage");
225410450e9eSJacob Faibussowitsch   // lines of code
22555c6c1daeSBarry Smith   PetscPreLoadEnd();
22565c6c1daeSBarry Smith .ve
22575c6c1daeSBarry Smith 
2258d1f92df0SBarry Smith   Level: intermediate
2259d1f92df0SBarry Smith 
2260811af0c4SBarry Smith   Note:
226195452b02SPatrick Sanan   Only works in C/C++, not Fortran
22625c6c1daeSBarry Smith 
226310450e9eSJacob Faibussowitsch   Flags available within the macro\:
226410450e9eSJacob Faibussowitsch + PetscPreLoadingUsed - `PETSC_TRUE` if we are or have done preloading
226510450e9eSJacob Faibussowitsch . PetscPreLoadingOn   - `PETSC_TRUE` if it is CURRENTLY doing preload
226610450e9eSJacob Faibussowitsch . PetscPreLoadIt      - `0` for the first computation (with preloading turned off it is only
226710450e9eSJacob Faibussowitsch                         `0`) `1`  for the second
226810450e9eSJacob Faibussowitsch - PetscPreLoadMax     - number of times it will do the computation, only one when preloading is
226910450e9eSJacob Faibussowitsch                         turned on
227010450e9eSJacob Faibussowitsch 
227110450e9eSJacob Faibussowitsch   The first two variables are available throughout the program, the second two only between the
227210450e9eSJacob Faibussowitsch   `PetscPreLoadBegin()` and `PetscPreLoadEnd()`
22735c6c1daeSBarry Smith 
2274d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()`
22755c6c1daeSBarry Smith M*/
22765c6c1daeSBarry Smith 
22775c6c1daeSBarry Smith /*MC
227810450e9eSJacob Faibussowitsch   PetscPreLoadEnd - End a segment of code that may be preloaded (run twice) to get accurate
227910450e9eSJacob Faibussowitsch   timings
22805c6c1daeSBarry Smith 
22815c6c1daeSBarry Smith   Synopsis:
2282aaa7dc30SBarry Smith   #include <petsclog.h>
22835c6c1daeSBarry Smith   void PetscPreLoadEnd(void);
22845c6c1daeSBarry Smith 
22855c6c1daeSBarry Smith   Not Collective
22865c6c1daeSBarry Smith 
228710450e9eSJacob Faibussowitsch   Example Usage:
22885c6c1daeSBarry Smith .vb
228910450e9eSJacob Faibussowitsch   PetscPreLoadBegin(PETSC_TRUE, "first stage");
229010450e9eSJacob Faibussowitsch   // lines of code
22915c6c1daeSBarry Smith   PetscPreLoadStage("second stage");
229210450e9eSJacob Faibussowitsch   // lines of code
22935c6c1daeSBarry Smith   PetscPreLoadEnd();
22945c6c1daeSBarry Smith .ve
22955c6c1daeSBarry Smith 
2296d1f92df0SBarry Smith   Level: intermediate
2297d1f92df0SBarry Smith 
2298811af0c4SBarry Smith   Note:
2299dd01b7e5SBarry Smith   Only works in C/C++ not Fortran
23005c6c1daeSBarry Smith 
2301d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadStage()`
23025c6c1daeSBarry Smith M*/
23035c6c1daeSBarry Smith 
23045c6c1daeSBarry Smith /*MC
230510450e9eSJacob Faibussowitsch   PetscPreLoadStage - Start a new segment of code to be timed separately to get accurate timings
23065c6c1daeSBarry Smith 
23075c6c1daeSBarry Smith   Synopsis:
2308aaa7dc30SBarry Smith   #include <petsclog.h>
23095c6c1daeSBarry Smith   void PetscPreLoadStage(char *name);
23105c6c1daeSBarry Smith 
23115c6c1daeSBarry Smith   Not Collective
23125c6c1daeSBarry Smith 
231310450e9eSJacob Faibussowitsch   Example Usage:
23145c6c1daeSBarry Smith .vb
231510450e9eSJacob Faibussowitsch   PetscPreLoadBegin(PETSC_TRUE,"first stage");
231610450e9eSJacob Faibussowitsch   // lines of code
23175c6c1daeSBarry Smith   PetscPreLoadStage("second stage");
231810450e9eSJacob Faibussowitsch   // lines of code
23195c6c1daeSBarry Smith   PetscPreLoadEnd();
23205c6c1daeSBarry Smith .ve
23215c6c1daeSBarry Smith 
2322d1f92df0SBarry Smith   Level: intermediate
2323d1f92df0SBarry Smith 
2324811af0c4SBarry Smith   Note:
2325dd01b7e5SBarry Smith   Only works in C/C++ not Fortran
23265c6c1daeSBarry Smith 
2327d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`
23285c6c1daeSBarry Smith M*/
23295c6c1daeSBarry Smith 
2330a4af0ceeSJacob Faibussowitsch   #if PetscDefined(HAVE_DEVICE)
2331a4af0ceeSJacob Faibussowitsch     #include <petsc/private/deviceimpl.h>
23329ffd0706SHong Zhang 
2333cc4c1da9SBarry Smith /*@
2334156b51fbSBarry Smith   PetscLogGpuTime - turn on the logging of GPU time for GPU kernels
2335156b51fbSBarry Smith 
2336811af0c4SBarry Smith   Options Database Key:
2337efa05fe8SBarry Smith . -log_view_gpu_time - provide the GPU times for all events in the `-log_view` output
2338156b51fbSBarry Smith 
2339d1f92df0SBarry Smith   Level: advanced
2340d1f92df0SBarry Smith 
2341156b51fbSBarry Smith   Notes:
234210450e9eSJacob Faibussowitsch   Turning on the timing of the GPU kernels can slow down the entire computation and should only
2343efa05fe8SBarry Smith   be used when studying the performance of individual operations on GPU such as vector operations and
234410450e9eSJacob Faibussowitsch   matrix-vector operations.
2345156b51fbSBarry Smith 
2346efa05fe8SBarry 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
2347efa05fe8SBarry Smith 
234810450e9eSJacob Faibussowitsch   This routine should only be called once near the beginning of the program. Once it is started
234910450e9eSJacob Faibussowitsch   it cannot be turned off.
2350156b51fbSBarry Smith 
2351d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogView()`, `PetscLogGpuFlops()`, `PetscLogGpuTimeEnd()`, `PetscLogGpuTimeBegin()`
2352156b51fbSBarry Smith @*/
2353d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogGpuTime(void)
2354d71ae5a4SJacob Faibussowitsch {
2355473903fcSJunchao Zhang   PetscFunctionBegin;
2356473903fcSJunchao Zhang   PetscCheck(petsc_gtime == 0.0, PETSC_COMM_SELF, PETSC_ERR_SUP, "GPU logging has already been turned on");
2357156b51fbSBarry Smith   PetscLogGpuTimeFlag = PETSC_TRUE;
2358473903fcSJunchao Zhang   PetscFunctionReturn(PETSC_SUCCESS);
2359156b51fbSBarry Smith }
2360156b51fbSBarry Smith 
2361cc4c1da9SBarry Smith /*@
23629ffd0706SHong Zhang   PetscLogGpuTimeBegin - Start timer for device
23639ffd0706SHong Zhang 
2364d1f92df0SBarry Smith   Level: intermediate
2365d1f92df0SBarry Smith 
23669ffd0706SHong Zhang   Notes:
23677d766218SJunchao Zhang   When GPU is enabled, the timer is run on the GPU, it is a separate logging of time
236810450e9eSJacob Faibussowitsch   devoted to GPU computations (excluding kernel launch times).
2369811af0c4SBarry Smith 
23707d766218SJunchao Zhang   When GPU is not available, the timer is run on the CPU, it is a separate logging of
237110450e9eSJacob Faibussowitsch   time devoted to GPU computations (including kernel launch times).
2372811af0c4SBarry Smith 
237310450e9eSJacob Faibussowitsch   There is no need to call WaitForCUDA() or WaitForHIP() between `PetscLogGpuTimeBegin()` and
237410450e9eSJacob Faibussowitsch   `PetscLogGpuTimeEnd()`
2375811af0c4SBarry Smith 
237610450e9eSJacob Faibussowitsch   This timer should NOT include times for data transfers between the GPU and CPU, nor setup
237710450e9eSJacob Faibussowitsch   actions such as allocating space.
2378811af0c4SBarry Smith 
237910450e9eSJacob Faibussowitsch   The regular logging captures the time for data transfers and any CPU activities during the
238010450e9eSJacob Faibussowitsch   event. It is used to compute the flop rate on the GPU as it is actively engaged in running a
238110450e9eSJacob Faibussowitsch   kernel.
23829ffd0706SHong Zhang 
23839ffd0706SHong Zhang   Developer Notes:
238410450e9eSJacob Faibussowitsch   The GPU event timer captures the execution time of all the kernels launched in the default
238554c05997SPierre Jolivet   stream by the CPU between `PetscLogGpuTimeBegin()` and `PetscLogGpuTimeEnd()`.
2386811af0c4SBarry Smith 
238754c05997SPierre Jolivet   `PetscLogGpuTimeBegin()` and `PetscLogGpuTimeEnd()` insert the begin and end events into the
238810450e9eSJacob Faibussowitsch   default stream (stream 0). The device will record a time stamp for the event when it reaches
238910450e9eSJacob Faibussowitsch   that event in the stream. The function xxxEventSynchronize() is called in
239054c05997SPierre Jolivet   `PetscLogGpuTimeEnd()` to block CPU execution, but not continued GPU execution, until the
239110450e9eSJacob Faibussowitsch   timer event is recorded.
23929ffd0706SHong Zhang 
2393d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogView()`, `PetscLogGpuFlops()`, `PetscLogGpuTimeEnd()`, `PetscLogGpuTime()`
23949ffd0706SHong Zhang @*/
2395d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogGpuTimeBegin(void)
2396d71ae5a4SJacob Faibussowitsch {
2397b665b14eSToby Isaac   PetscBool isActive;
2398b665b14eSToby Isaac 
23999ffd0706SHong Zhang   PetscFunctionBegin;
2400b665b14eSToby Isaac   PetscCall(PetscLogEventBeginIsActive(&isActive));
2401b665b14eSToby Isaac   if (!isActive || !PetscLogGpuTimeFlag) PetscFunctionReturn(PETSC_SUCCESS);
2402*5268dc8aSHong Zhang   if (!PetscDefined(HAVE_KOKKOS_WITHOUT_GPU)) {
2403a4af0ceeSJacob Faibussowitsch     PetscDeviceContext dctx;
2404a4af0ceeSJacob Faibussowitsch 
24059566063dSJacob Faibussowitsch     PetscCall(PetscDeviceContextGetCurrentContext(&dctx));
24069566063dSJacob Faibussowitsch     PetscCall(PetscDeviceContextBeginTimer_Internal(dctx));
2407*5268dc8aSHong Zhang   } else {
24087d766218SJunchao Zhang     PetscCall(PetscTimeSubtract(&petsc_gtime));
2409*5268dc8aSHong Zhang   }
24103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24119ffd0706SHong Zhang }
24129ffd0706SHong Zhang 
2413cc4c1da9SBarry Smith /*@
24149ffd0706SHong Zhang   PetscLogGpuTimeEnd - Stop timer for device
24159ffd0706SHong Zhang 
24169ffd0706SHong Zhang   Level: intermediate
24179ffd0706SHong Zhang 
2418d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogView()`, `PetscLogGpuFlops()`, `PetscLogGpuTimeBegin()`
24199ffd0706SHong Zhang @*/
2420d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscLogGpuTimeEnd(void)
2421d71ae5a4SJacob Faibussowitsch {
2422b665b14eSToby Isaac   PetscBool isActive;
2423b665b14eSToby Isaac 
24249ffd0706SHong Zhang   PetscFunctionBegin;
2425b665b14eSToby Isaac   PetscCall(PetscLogEventEndIsActive(&isActive));
2426b665b14eSToby Isaac   if (!isActive || !PetscLogGpuTimeFlag) PetscFunctionReturn(PETSC_SUCCESS);
2427*5268dc8aSHong Zhang   if (!PetscDefined(HAVE_KOKKOS_WITHOUT_GPU)) {
2428a4af0ceeSJacob Faibussowitsch     PetscDeviceContext dctx;
2429a4af0ceeSJacob Faibussowitsch     PetscLogDouble     elapsed;
2430a4af0ceeSJacob Faibussowitsch 
24319566063dSJacob Faibussowitsch     PetscCall(PetscDeviceContextGetCurrentContext(&dctx));
24329566063dSJacob Faibussowitsch     PetscCall(PetscDeviceContextEndTimer_Internal(dctx, &elapsed));
2433a4af0ceeSJacob Faibussowitsch     petsc_gtime += (elapsed / 1000.0);
2434*5268dc8aSHong Zhang     #if PetscDefined(HAVE_CUDA_VERSION_12_2PLUS)
2435*5268dc8aSHong Zhang     if (PetscLogGpuEnergyFlag) {
2436*5268dc8aSHong Zhang       PetscLogDouble power;
2437*5268dc8aSHong Zhang       PetscCall(PetscDeviceContextGetPower_Internal(dctx, &power));
2438*5268dc8aSHong Zhang       petsc_genergy += (power * elapsed / 1000000.0); // convert to Joules
2439a4af0ceeSJacob Faibussowitsch     }
24407d766218SJunchao Zhang     #endif
2441*5268dc8aSHong Zhang   } else {
2442*5268dc8aSHong Zhang     PetscCall(PetscTimeAdd(&petsc_gtime));
2443*5268dc8aSHong Zhang   }
24443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24459ffd0706SHong Zhang }
2446c708d6e3SStefano Zampini 
2447*5268dc8aSHong Zhang /*@
2448*5268dc8aSHong Zhang   PetscLogGpuEnergy - turn on the logging of GPU energy (estimated with power*gtime) for GPU kernels
2449*5268dc8aSHong Zhang 
2450*5268dc8aSHong Zhang   Options Database Key:
2451*5268dc8aSHong Zhang . -log_view_gpu_energy - provide the GPU energy consumption (estimated with power*gtime) for all events in the `-log_view` output
2452*5268dc8aSHong Zhang 
2453*5268dc8aSHong Zhang   Level: advanced
2454*5268dc8aSHong Zhang 
2455*5268dc8aSHong Zhang   Note:
2456*5268dc8aSHong Zhang   This option is mutually exclusive to `-log_view_gpu_energy_meter`.
2457*5268dc8aSHong Zhang 
2458*5268dc8aSHong Zhang   Developer Note:
2459*5268dc8aSHong Zhang   This option turns on energy monitoring of GPU kernels and requires CUDA version >= 12.2. The energy consumption is estimated as
2460*5268dc8aSHong Zhang   instant_power * gpu_kernel_time. Due to the delay in NVML power sampling, we read the instantaneous power draw at the end of each
2461*5268dc8aSHong Zhang   event using `nvmlDeviceGetFieldValues()` with the field ID `NVML_FI_DEV_POWER_INSTANT`.
2462*5268dc8aSHong Zhang 
2463*5268dc8aSHong Zhang .seealso: [](ch_profiling), `PetscLogView()`, `PetscLogGpuEnergyMeter()`
2464*5268dc8aSHong Zhang @*/
2465*5268dc8aSHong Zhang PetscErrorCode PetscLogGpuEnergy(void)
2466*5268dc8aSHong Zhang {
2467*5268dc8aSHong Zhang   PetscFunctionBegin;
2468*5268dc8aSHong Zhang   PetscCheck(PetscDefined(HAVE_CUDA_VERSION_12_2PLUS), PETSC_COMM_WORLD, PETSC_ERR_SUP_SYS, "-log_view_gpu_energy requires CUDA version >= 12.2");
2469*5268dc8aSHong Zhang   PetscCheck(petsc_genergy == 0.0, PETSC_COMM_SELF, PETSC_ERR_SUP, "GPU energy logging has already been turned on");
2470*5268dc8aSHong Zhang   PetscLogGpuEnergyFlag      = PETSC_TRUE;
2471*5268dc8aSHong Zhang   PetscLogGpuEnergyMeterFlag = PETSC_FALSE;
2472*5268dc8aSHong Zhang   PetscFunctionReturn(PETSC_SUCCESS);
2473*5268dc8aSHong Zhang }
2474*5268dc8aSHong Zhang 
2475*5268dc8aSHong Zhang /*@
2476*5268dc8aSHong Zhang   PetscLogGpuEnergyMeter - turn on the logging of GPU energy (readings from energy meters) for GPU kernels
2477*5268dc8aSHong Zhang 
2478*5268dc8aSHong Zhang   Options Database Key:
2479*5268dc8aSHong Zhang . -log_view_gpu_energy_meter - provide the GPU energy (readings from energy meters) consumption for all events in the `-log_view` output
2480*5268dc8aSHong Zhang 
2481*5268dc8aSHong Zhang   Level: advanced
2482*5268dc8aSHong Zhang 
2483*5268dc8aSHong Zhang   Note:
2484*5268dc8aSHong Zhang   This option is mutually exclusive to `-log_view_gpu_energy`.
2485*5268dc8aSHong Zhang 
2486*5268dc8aSHong Zhang   Developer Note:
2487*5268dc8aSHong Zhang   This option turns on energy monitoring of GPU kernels. The energy consumption is measured directly using the NVML API
2488*5268dc8aSHong Zhang   `nvmlDeviceGetTotalEnergyConsumption()`, which returns the total energy used by the GPU since the driver was last initialized.
2489*5268dc8aSHong Zhang   For newer GPUs, energy readings are updated every 20-100ms, so this approach may be inaccurate for short-duration GPU events.
2490*5268dc8aSHong Zhang 
2491*5268dc8aSHong Zhang @*/
2492*5268dc8aSHong Zhang PetscErrorCode PetscLogGpuEnergyMeter(void)
2493*5268dc8aSHong Zhang {
2494*5268dc8aSHong Zhang   PetscFunctionBegin;
2495*5268dc8aSHong Zhang   PetscCheck(petsc_genergy == 0.0, PETSC_COMM_SELF, PETSC_ERR_SUP, "GPU energy logging has already been turned on");
2496*5268dc8aSHong Zhang   PetscLogGpuEnergyMeterFlag = PETSC_TRUE;
2497*5268dc8aSHong Zhang   PetscLogGpuEnergyFlag      = PETSC_FALSE;
2498*5268dc8aSHong Zhang   PetscFunctionReturn(PETSC_SUCCESS);
2499*5268dc8aSHong Zhang }
2500*5268dc8aSHong Zhang 
2501*5268dc8aSHong Zhang /*@
2502*5268dc8aSHong Zhang   PetscLogGpuEnergyMeterBegin - Start energy meter for device
2503*5268dc8aSHong Zhang 
2504*5268dc8aSHong Zhang   Level: intermediate
2505*5268dc8aSHong Zhang 
2506*5268dc8aSHong Zhang   Notes:
2507*5268dc8aSHong Zhang   The GPU event energy meter captures the energy used by the GPU between `PetscLogGpuEnergyMeterBegin()` and `PetscLogGpuEnergyMeterEnd()`.
2508*5268dc8aSHong Zhang 
2509*5268dc8aSHong Zhang   `PetscLogGpuEnergyMeterBegin()` and `PetscLogGpuEnergyMeterEnd()` collect the energy readings using `nvmlDeviceGetTotalEnergyConsumption()`.
2510*5268dc8aSHong Zhang   The function `cupmStreamSynchronize()` is called before the energy query to ensure completion.
2511*5268dc8aSHong Zhang 
2512*5268dc8aSHong Zhang .seealso: [](ch_profiling), `PetscLogView()`, `PetscLogGpuEnergyMeterEnd()`, `PetscLogGpuEnergyMeter()`
2513*5268dc8aSHong Zhang @*/
2514*5268dc8aSHong Zhang PetscErrorCode PetscLogGpuEnergyMeterBegin(void)
2515*5268dc8aSHong Zhang {
2516*5268dc8aSHong Zhang   PetscBool isActive;
2517*5268dc8aSHong Zhang 
2518*5268dc8aSHong Zhang   PetscFunctionBegin;
2519*5268dc8aSHong Zhang   PetscCall(PetscLogEventBeginIsActive(&isActive));
2520*5268dc8aSHong Zhang   if (!isActive || !PetscLogGpuEnergyMeterFlag) PetscFunctionReturn(PETSC_SUCCESS);
2521*5268dc8aSHong Zhang   if (!PetscDefined(HAVE_KOKKOS_WITHOUT_GPU)) {
2522*5268dc8aSHong Zhang     PetscDeviceContext dctx;
2523*5268dc8aSHong Zhang 
2524*5268dc8aSHong Zhang     PetscCall(PetscDeviceContextGetCurrentContext(&dctx));
2525*5268dc8aSHong Zhang     PetscCall(PetscDeviceContextBeginEnergyMeter_Internal(dctx));
2526*5268dc8aSHong Zhang   }
2527*5268dc8aSHong Zhang   PetscFunctionReturn(PETSC_SUCCESS);
2528*5268dc8aSHong Zhang }
2529*5268dc8aSHong Zhang 
2530*5268dc8aSHong Zhang /*@
2531*5268dc8aSHong Zhang   PetscLogGpuEnergyMeterEnd - Stop energy meter for device
2532*5268dc8aSHong Zhang 
2533*5268dc8aSHong Zhang   Level: intermediate
2534*5268dc8aSHong Zhang 
2535*5268dc8aSHong Zhang .seealso: [](ch_profiling), `PetscLogView()`, `PetscLogGpuEnergyMeterBegin()`
2536*5268dc8aSHong Zhang @*/
2537*5268dc8aSHong Zhang PetscErrorCode PetscLogGpuEnergyMeterEnd(void)
2538*5268dc8aSHong Zhang {
2539*5268dc8aSHong Zhang   PetscBool isActive;
2540*5268dc8aSHong Zhang 
2541*5268dc8aSHong Zhang   PetscFunctionBegin;
2542*5268dc8aSHong Zhang   PetscCall(PetscLogEventEndIsActive(&isActive));
2543*5268dc8aSHong Zhang   if (!isActive || !PetscLogGpuEnergyMeterFlag) PetscFunctionReturn(PETSC_SUCCESS);
2544*5268dc8aSHong Zhang   if (!PetscDefined(HAVE_KOKKOS_WITHOUT_GPU)) {
2545*5268dc8aSHong Zhang     PetscDeviceContext dctx;
2546*5268dc8aSHong Zhang     PetscLogDouble     energy;
2547*5268dc8aSHong Zhang 
2548*5268dc8aSHong Zhang     PetscCall(PetscDeviceContextGetCurrentContext(&dctx));
2549*5268dc8aSHong Zhang     PetscCall(PetscDeviceContextEndEnergyMeter_Internal(dctx, &energy));
2550*5268dc8aSHong Zhang     petsc_genergy_meter += (energy / 1000.0); // convert to Joules
2551*5268dc8aSHong Zhang   }
2552*5268dc8aSHong Zhang   PetscFunctionReturn(PETSC_SUCCESS);
2553*5268dc8aSHong Zhang }
25549ffd0706SHong Zhang   #endif /* end of PETSC_HAVE_DEVICE */
25559ffd0706SHong Zhang 
2556cb9ef012SToby Isaac #endif /* PETSC_USE_LOG*/
2557cb9ef012SToby Isaac 
2558dd01b7e5SBarry Smith /* -- Utility functions for logging from Fortran -- */
2559b665b14eSToby Isaac 
2560b665b14eSToby Isaac PETSC_EXTERN PetscErrorCode PetscASend(int count, int datatype)
2561b665b14eSToby Isaac {
2562b665b14eSToby Isaac   PetscFunctionBegin;
2563cb9ef012SToby Isaac #if PetscDefined(USE_LOG)
2564b665b14eSToby Isaac   PetscCall(PetscAddLogDouble(&petsc_send_ct, &petsc_send_ct_th, 1));
2565b665b14eSToby Isaac   #if !defined(MPIUNI_H) && !defined(PETSC_HAVE_BROKEN_RECURSIVE_MACRO)
2566b665b14eSToby Isaac   PetscCall(PetscMPITypeSize(count, MPI_Type_f2c((MPI_Fint)datatype), &petsc_send_len, &petsc_send_len_th));
2567b665b14eSToby Isaac   #endif
2568cb9ef012SToby Isaac #endif
2569b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
2570b665b14eSToby Isaac }
2571b665b14eSToby Isaac 
2572b665b14eSToby Isaac PETSC_EXTERN PetscErrorCode PetscARecv(int count, int datatype)
2573b665b14eSToby Isaac {
2574b665b14eSToby Isaac   PetscFunctionBegin;
2575cb9ef012SToby Isaac #if PetscDefined(USE_LOG)
2576b665b14eSToby Isaac   PetscCall(PetscAddLogDouble(&petsc_recv_ct, &petsc_recv_ct_th, 1));
2577b665b14eSToby Isaac   #if !defined(MPIUNI_H) && !defined(PETSC_HAVE_BROKEN_RECURSIVE_MACRO)
2578b665b14eSToby Isaac   PetscCall(PetscMPITypeSize(count, MPI_Type_f2c((MPI_Fint)datatype), &petsc_recv_len, &petsc_recv_len_th));
2579b665b14eSToby Isaac   #endif
2580cb9ef012SToby Isaac #endif
2581b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
2582b665b14eSToby Isaac }
2583b665b14eSToby Isaac 
2584b665b14eSToby Isaac PETSC_EXTERN PetscErrorCode PetscAReduce(void)
2585b665b14eSToby Isaac {
2586b665b14eSToby Isaac   PetscFunctionBegin;
2587cb9ef012SToby Isaac   if (PetscDefined(USE_LOG)) PetscCall(PetscAddLogDouble(&petsc_allreduce_ct, &petsc_allreduce_ct_th, 1));
2588b665b14eSToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
2589b665b14eSToby Isaac }
2590b665b14eSToby Isaac 
25915c6c1daeSBarry Smith PetscClassId PETSC_LARGEST_CLASSID = PETSC_SMALLEST_CLASSID;
25925c6c1daeSBarry Smith PetscClassId PETSC_OBJECT_CLASSID  = 0;
25935c6c1daeSBarry Smith 
25942611ad71SToby Isaac static PetscBool PetscLogInitializeCalled = PETSC_FALSE;
25952611ad71SToby Isaac 
25962611ad71SToby Isaac PETSC_INTERN PetscErrorCode PetscLogInitialize(void)
25972611ad71SToby Isaac {
25982611ad71SToby Isaac   int stage;
25992611ad71SToby Isaac 
26002611ad71SToby Isaac   PetscFunctionBegin;
26012611ad71SToby Isaac   if (PetscLogInitializeCalled) PetscFunctionReturn(PETSC_SUCCESS);
26022611ad71SToby Isaac   PetscLogInitializeCalled = PETSC_TRUE;
26032611ad71SToby Isaac   if (PetscDefined(USE_LOG)) {
26042611ad71SToby Isaac     /* Setup default logging structures */
26052611ad71SToby Isaac     PetscCall(PetscLogStateCreate(&petsc_log_state));
26062611ad71SToby Isaac     for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
26072611ad71SToby Isaac       if (PetscLogHandlers[i].handler) PetscCall(PetscLogHandlerSetState(PetscLogHandlers[i].handler, petsc_log_state));
26082611ad71SToby Isaac     }
26092611ad71SToby Isaac     PetscCall(PetscLogStateStageRegister(petsc_log_state, "Main Stage", &stage));
26102611ad71SToby Isaac     PetscCall(PetscSpinlockCreate(&PetscLogSpinLock));
26112611ad71SToby Isaac #if defined(PETSC_HAVE_THREADSAFETY)
26122611ad71SToby Isaac     petsc_log_tid = 0;
26132611ad71SToby Isaac     petsc_log_gid = 0;
26142611ad71SToby Isaac #endif
26152611ad71SToby Isaac 
26162611ad71SToby Isaac     /* All processors sync here for more consistent logging */
26172611ad71SToby Isaac     PetscCallMPI(MPI_Barrier(PETSC_COMM_WORLD));
26182611ad71SToby Isaac     PetscCall(PetscTime(&petsc_BaseTime));
26192611ad71SToby Isaac     PetscCall(PetscLogStagePush(stage));
26202611ad71SToby Isaac   }
26212611ad71SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
26222611ad71SToby Isaac }
26232611ad71SToby Isaac 
26242611ad71SToby Isaac PETSC_INTERN PetscErrorCode PetscLogFinalize(void)
26252611ad71SToby Isaac {
26262611ad71SToby Isaac   PetscFunctionBegin;
26272611ad71SToby Isaac   if (PetscDefined(USE_LOG)) {
2628b665b14eSToby Isaac     /* Resetting phase */
2629b665b14eSToby Isaac     // pop remaining stages
2630b665b14eSToby Isaac     if (petsc_log_state) {
26313a7d0413SPierre Jolivet       while (petsc_log_state->current_stage >= 0) PetscCall(PetscLogStagePop());
2632b665b14eSToby Isaac     }
26332611ad71SToby Isaac     for (int i = 0; i < PETSC_LOG_HANDLER_MAX; i++) PetscCall(PetscLogHandlerDestroy(&PetscLogHandlers[i].handler));
26342611ad71SToby Isaac     PetscCall(PetscArrayzero(PetscLogHandlers, PETSC_LOG_HANDLER_MAX));
26352611ad71SToby Isaac     PetscCall(PetscLogStateDestroy(&petsc_log_state));
26362611ad71SToby Isaac 
26372611ad71SToby Isaac     petsc_TotalFlops         = 0.0;
26382611ad71SToby Isaac     petsc_BaseTime           = 0.0;
26392611ad71SToby Isaac     petsc_TotalFlops         = 0.0;
26402611ad71SToby Isaac     petsc_send_ct            = 0.0;
26412611ad71SToby Isaac     petsc_recv_ct            = 0.0;
26422611ad71SToby Isaac     petsc_send_len           = 0.0;
26432611ad71SToby Isaac     petsc_recv_len           = 0.0;
26442611ad71SToby Isaac     petsc_isend_ct           = 0.0;
26452611ad71SToby Isaac     petsc_irecv_ct           = 0.0;
26462611ad71SToby Isaac     petsc_isend_len          = 0.0;
26472611ad71SToby Isaac     petsc_irecv_len          = 0.0;
26482611ad71SToby Isaac     petsc_wait_ct            = 0.0;
26492611ad71SToby Isaac     petsc_wait_any_ct        = 0.0;
26502611ad71SToby Isaac     petsc_wait_all_ct        = 0.0;
26512611ad71SToby Isaac     petsc_sum_of_waits_ct    = 0.0;
26522611ad71SToby Isaac     petsc_allreduce_ct       = 0.0;
26532611ad71SToby Isaac     petsc_gather_ct          = 0.0;
26542611ad71SToby Isaac     petsc_scatter_ct         = 0.0;
26552611ad71SToby Isaac     petsc_TotalFlops_th      = 0.0;
26562611ad71SToby Isaac     petsc_send_ct_th         = 0.0;
26572611ad71SToby Isaac     petsc_recv_ct_th         = 0.0;
26582611ad71SToby Isaac     petsc_send_len_th        = 0.0;
26592611ad71SToby Isaac     petsc_recv_len_th        = 0.0;
26602611ad71SToby Isaac     petsc_isend_ct_th        = 0.0;
26612611ad71SToby Isaac     petsc_irecv_ct_th        = 0.0;
26622611ad71SToby Isaac     petsc_isend_len_th       = 0.0;
26632611ad71SToby Isaac     petsc_irecv_len_th       = 0.0;
26642611ad71SToby Isaac     petsc_wait_ct_th         = 0.0;
26652611ad71SToby Isaac     petsc_wait_any_ct_th     = 0.0;
26662611ad71SToby Isaac     petsc_wait_all_ct_th     = 0.0;
26672611ad71SToby Isaac     petsc_sum_of_waits_ct_th = 0.0;
26682611ad71SToby Isaac     petsc_allreduce_ct_th    = 0.0;
26692611ad71SToby Isaac     petsc_gather_ct_th       = 0.0;
26702611ad71SToby Isaac     petsc_scatter_ct_th      = 0.0;
26712611ad71SToby Isaac 
26722611ad71SToby Isaac     petsc_ctog_ct       = 0.0;
26732611ad71SToby Isaac     petsc_gtoc_ct       = 0.0;
26742611ad71SToby Isaac     petsc_ctog_sz       = 0.0;
26752611ad71SToby Isaac     petsc_gtoc_sz       = 0.0;
26762611ad71SToby Isaac     petsc_gflops        = 0.0;
26772611ad71SToby Isaac     petsc_gtime         = 0.0;
2678*5268dc8aSHong Zhang     petsc_genergy       = 0.0;
2679*5268dc8aSHong Zhang     petsc_genergy_meter = 0.0;
26802611ad71SToby Isaac     petsc_ctog_ct_th    = 0.0;
26812611ad71SToby Isaac     petsc_gtoc_ct_th    = 0.0;
26822611ad71SToby Isaac     petsc_ctog_sz_th    = 0.0;
26832611ad71SToby Isaac     petsc_gtoc_sz_th    = 0.0;
26842611ad71SToby Isaac     petsc_gflops_th     = 0.0;
26852611ad71SToby Isaac     petsc_gtime_th      = 0.0;
26862611ad71SToby Isaac   }
26872611ad71SToby Isaac   PETSC_LARGEST_CLASSID    = PETSC_SMALLEST_CLASSID;
26882611ad71SToby Isaac   PETSC_OBJECT_CLASSID     = 0;
26892611ad71SToby Isaac   PetscLogInitializeCalled = PETSC_FALSE;
26902611ad71SToby Isaac   PetscFunctionReturn(PETSC_SUCCESS);
26912611ad71SToby Isaac }
26922611ad71SToby Isaac 
2693cc4c1da9SBarry Smith /*@
26945c6c1daeSBarry Smith   PetscClassIdRegister - Registers a new class name for objects and logging operations in an application code.
26955c6c1daeSBarry Smith 
26965c6c1daeSBarry Smith   Not Collective
26975c6c1daeSBarry Smith 
26985c6c1daeSBarry Smith   Input Parameter:
26995c6c1daeSBarry Smith . name - The class name
27005c6c1daeSBarry Smith 
27015c6c1daeSBarry Smith   Output Parameter:
27025c6c1daeSBarry Smith . oclass - The class id or classid
27035c6c1daeSBarry Smith 
27045c6c1daeSBarry Smith   Level: developer
27055c6c1daeSBarry Smith 
2706d1f92df0SBarry Smith .seealso: [](ch_profiling), `PetscLogEventRegister()`
27075c6c1daeSBarry Smith @*/
2708d71ae5a4SJacob Faibussowitsch PetscErrorCode PetscClassIdRegister(const char name[], PetscClassId *oclass)
2709d71ae5a4SJacob Faibussowitsch {
27105c6c1daeSBarry Smith   PetscFunctionBegin;
27115c6c1daeSBarry Smith   *oclass = ++PETSC_LARGEST_CLASSID;
27125c6c1daeSBarry Smith #if defined(PETSC_USE_LOG)
2713b665b14eSToby Isaac   {
2714b665b14eSToby Isaac     PetscLogState state;
2715b665b14eSToby Isaac     PetscLogClass logclass;
2716b665b14eSToby Isaac 
2717b665b14eSToby Isaac     PetscCall(PetscLogGetState(&state));
2718b665b14eSToby Isaac     if (state) PetscCall(PetscLogStateClassRegister(state, name, *oclass, &logclass));
2719b665b14eSToby Isaac   }
27205c6c1daeSBarry Smith #endif
27213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27225c6c1daeSBarry Smith }
2723