1 2 /* 3 PETSc code to log object creation and destruction and PETSc events. 4 5 This provides the public API used by the rest of PETSc and by users. 6 7 These routines use a private API that is not used elsewhere in PETSc and is not 8 accessible to users. The private API is defined in logimpl.h and the utils directory. 9 10 *** 11 12 This file, and only this file, is for functions that interact with the global logging state 13 */ 14 #include <petsc/private/logimpl.h> /*I "petscsys.h" I*/ 15 #include <petsc/private/loghandlerimpl.h> 16 #include <petsctime.h> 17 #include <petscviewer.h> 18 #include <petscdevice.h> 19 #include <petsc/private/deviceimpl.h> 20 21 #if defined(PETSC_HAVE_THREADSAFETY) 22 23 PetscInt petsc_log_gid = -1; /* Global threadId counter */ 24 PETSC_TLS PetscInt petsc_log_tid = -1; /* Local threadId */ 25 26 /* shared variables */ 27 PetscSpinlock PetscLogSpinLock; 28 29 PetscInt PetscLogGetTid(void) 30 { 31 if (petsc_log_tid < 0) { 32 PetscCall(PetscSpinlockLock(&PetscLogSpinLock)); 33 petsc_log_tid = ++petsc_log_gid; 34 PetscCall(PetscSpinlockUnlock(&PetscLogSpinLock)); 35 } 36 return petsc_log_tid; 37 } 38 39 #endif 40 41 /* Global counters */ 42 PetscLogDouble petsc_BaseTime = 0.0; 43 PetscLogDouble petsc_TotalFlops = 0.0; /* The number of flops */ 44 PetscLogDouble petsc_send_ct = 0.0; /* The number of sends */ 45 PetscLogDouble petsc_recv_ct = 0.0; /* The number of receives */ 46 PetscLogDouble petsc_send_len = 0.0; /* The total length of all sent messages */ 47 PetscLogDouble petsc_recv_len = 0.0; /* The total length of all received messages */ 48 PetscLogDouble petsc_isend_ct = 0.0; /* The number of immediate sends */ 49 PetscLogDouble petsc_irecv_ct = 0.0; /* The number of immediate receives */ 50 PetscLogDouble petsc_isend_len = 0.0; /* The total length of all immediate send messages */ 51 PetscLogDouble petsc_irecv_len = 0.0; /* The total length of all immediate receive messages */ 52 PetscLogDouble petsc_wait_ct = 0.0; /* The number of waits */ 53 PetscLogDouble petsc_wait_any_ct = 0.0; /* The number of anywaits */ 54 PetscLogDouble petsc_wait_all_ct = 0.0; /* The number of waitalls */ 55 PetscLogDouble petsc_sum_of_waits_ct = 0.0; /* The total number of waits */ 56 PetscLogDouble petsc_allreduce_ct = 0.0; /* The number of reductions */ 57 PetscLogDouble petsc_gather_ct = 0.0; /* The number of gathers and gathervs */ 58 PetscLogDouble petsc_scatter_ct = 0.0; /* The number of scatters and scattervs */ 59 60 /* Thread Local storage */ 61 PETSC_TLS PetscLogDouble petsc_TotalFlops_th = 0.0; 62 PETSC_TLS PetscLogDouble petsc_send_ct_th = 0.0; 63 PETSC_TLS PetscLogDouble petsc_recv_ct_th = 0.0; 64 PETSC_TLS PetscLogDouble petsc_send_len_th = 0.0; 65 PETSC_TLS PetscLogDouble petsc_recv_len_th = 0.0; 66 PETSC_TLS PetscLogDouble petsc_isend_ct_th = 0.0; 67 PETSC_TLS PetscLogDouble petsc_irecv_ct_th = 0.0; 68 PETSC_TLS PetscLogDouble petsc_isend_len_th = 0.0; 69 PETSC_TLS PetscLogDouble petsc_irecv_len_th = 0.0; 70 PETSC_TLS PetscLogDouble petsc_wait_ct_th = 0.0; 71 PETSC_TLS PetscLogDouble petsc_wait_any_ct_th = 0.0; 72 PETSC_TLS PetscLogDouble petsc_wait_all_ct_th = 0.0; 73 PETSC_TLS PetscLogDouble petsc_sum_of_waits_ct_th = 0.0; 74 PETSC_TLS PetscLogDouble petsc_allreduce_ct_th = 0.0; 75 PETSC_TLS PetscLogDouble petsc_gather_ct_th = 0.0; 76 PETSC_TLS PetscLogDouble petsc_scatter_ct_th = 0.0; 77 78 PetscLogDouble petsc_ctog_ct = 0.0; /* The total number of CPU to GPU copies */ 79 PetscLogDouble petsc_gtoc_ct = 0.0; /* The total number of GPU to CPU copies */ 80 PetscLogDouble petsc_ctog_sz = 0.0; /* The total size of CPU to GPU copies */ 81 PetscLogDouble petsc_gtoc_sz = 0.0; /* The total size of GPU to CPU copies */ 82 PetscLogDouble petsc_ctog_ct_scalar = 0.0; /* The total number of CPU to GPU copies */ 83 PetscLogDouble petsc_gtoc_ct_scalar = 0.0; /* The total number of GPU to CPU copies */ 84 PetscLogDouble petsc_ctog_sz_scalar = 0.0; /* The total size of CPU to GPU copies */ 85 PetscLogDouble petsc_gtoc_sz_scalar = 0.0; /* The total size of GPU to CPU copies */ 86 PetscLogDouble petsc_gflops = 0.0; /* The flops done on a GPU */ 87 PetscLogDouble petsc_gtime = 0.0; /* The time spent on a GPU */ 88 89 PETSC_TLS PetscLogDouble petsc_ctog_ct_th = 0.0; 90 PETSC_TLS PetscLogDouble petsc_gtoc_ct_th = 0.0; 91 PETSC_TLS PetscLogDouble petsc_ctog_sz_th = 0.0; 92 PETSC_TLS PetscLogDouble petsc_gtoc_sz_th = 0.0; 93 PETSC_TLS PetscLogDouble petsc_ctog_ct_scalar_th = 0.0; 94 PETSC_TLS PetscLogDouble petsc_gtoc_ct_scalar_th = 0.0; 95 PETSC_TLS PetscLogDouble petsc_ctog_sz_scalar_th = 0.0; 96 PETSC_TLS PetscLogDouble petsc_gtoc_sz_scalar_th = 0.0; 97 PETSC_TLS PetscLogDouble petsc_gflops_th = 0.0; 98 PETSC_TLS PetscLogDouble petsc_gtime_th = 0.0; 99 100 PetscBool PetscLogMemory = PETSC_FALSE; 101 PetscBool PetscLogSyncOn = PETSC_FALSE; 102 103 PetscBool PetscLogGpuTimeFlag = PETSC_FALSE; 104 105 PetscLogState petsc_log_state = NULL; 106 107 #define PETSC_LOG_HANDLER_HOT_BLANK \ 108 { \ 109 NULL, NULL, NULL, NULL, NULL, NULL \ 110 } 111 112 PetscLogHandlerHot PetscLogHandlers[PETSC_LOG_HANDLER_MAX] = { 113 PETSC_LOG_HANDLER_HOT_BLANK, 114 PETSC_LOG_HANDLER_HOT_BLANK, 115 PETSC_LOG_HANDLER_HOT_BLANK, 116 PETSC_LOG_HANDLER_HOT_BLANK, 117 }; 118 119 #undef PETSC_LOG_HANDLERS_HOT_BLANK 120 121 #if defined(PETSC_USE_LOG) 122 #include <../src/sys/logging/handler/impls/default/logdefault.h> 123 124 #if defined(PETSC_HAVE_THREADSAFETY) 125 PetscErrorCode PetscAddLogDouble(PetscLogDouble *tot, PetscLogDouble *tot_th, PetscLogDouble tmp) 126 { 127 *tot_th += tmp; 128 PetscCall(PetscSpinlockLock(&PetscLogSpinLock)); 129 *tot += tmp; 130 PetscCall(PetscSpinlockUnlock(&PetscLogSpinLock)); 131 return PETSC_SUCCESS; 132 } 133 134 PetscErrorCode PetscAddLogDoubleCnt(PetscLogDouble *cnt, PetscLogDouble *tot, PetscLogDouble *cnt_th, PetscLogDouble *tot_th, PetscLogDouble tmp) 135 { 136 *cnt_th = *cnt_th + 1; 137 *tot_th += tmp; 138 PetscCall(PetscSpinlockLock(&PetscLogSpinLock)); 139 *tot += (PetscLogDouble)(tmp); 140 *cnt += *cnt + 1; 141 PetscCall(PetscSpinlockUnlock(&PetscLogSpinLock)); 142 return PETSC_SUCCESS; 143 } 144 145 #endif 146 147 static PetscErrorCode PetscLogTryGetHandler(PetscLogHandlerType type, PetscLogHandler *handler) 148 { 149 PetscFunctionBegin; 150 PetscAssertPointer(handler, 2); 151 *handler = NULL; 152 for (int i = 0; i < PETSC_LOG_HANDLER_MAX; i++) { 153 PetscLogHandler h = PetscLogHandlers[i].handler; 154 if (h) { 155 PetscBool match; 156 157 PetscCall(PetscObjectTypeCompare((PetscObject)h, type, &match)); 158 if (match) { 159 *handler = PetscLogHandlers[i].handler; 160 PetscFunctionReturn(PETSC_SUCCESS); 161 } 162 } 163 } 164 PetscFunctionReturn(PETSC_SUCCESS); 165 } 166 167 /*@ 168 PetscLogGetDefaultHandler - Get the default log handler if it is running. 169 170 Not collective 171 172 Output Parameter: 173 . handler - the default `PetscLogHandler`, or `NULL` if it is not running. 174 175 Level: developer 176 177 Notes: 178 The default handler is started with `PetscLogDefaultBegin()`, 179 if the options flags `-log_all` or `-log_view` is given without arguments, 180 or for `-log_view :output:format` if `format` is not `ascii_xml` or `ascii_flamegraph`. 181 182 .seealso: [](ch_profiling) 183 @*/ 184 PetscErrorCode PetscLogGetDefaultHandler(PetscLogHandler *handler) 185 { 186 PetscFunctionBegin; 187 PetscCall(PetscLogTryGetHandler(PETSCLOGHANDLERDEFAULT, handler)); 188 PetscFunctionReturn(PETSC_SUCCESS); 189 } 190 191 static PetscErrorCode PetscLogGetHandler(PetscLogHandlerType type, PetscLogHandler *handler) 192 { 193 PetscFunctionBegin; 194 PetscAssertPointer(handler, 2); 195 PetscCall(PetscLogTryGetHandler(type, handler)); 196 PetscCheck(handler != NULL, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "A PetscLogHandler of type %s has not been started.", type); 197 PetscFunctionReturn(PETSC_SUCCESS); 198 } 199 200 /*@ 201 PetscLogGetState - Get the `PetscLogState` for PETSc's global logging, used 202 by all default log handlers (`PetscLogDefaultBegin()`, 203 `PetscLogNestedBegin()`, `PetscLogTraceBegin()`, `PetscLogMPEBegin()`, 204 `PetscLogPerfstubsBegin()`). 205 206 Collective on `PETSC_COMM_WORLD` 207 208 Output Parameter: 209 . state - The `PetscLogState` changed by registrations (such as 210 `PetscLogEventRegister()`) and actions (such as `PetscLogEventBegin()` or 211 `PetscLogStagePush()`), or NULL if logging is not active 212 213 Level: developer 214 215 .seealso: [](ch_profiling), `PetscLogState` 216 @*/ 217 PetscErrorCode PetscLogGetState(PetscLogState *state) 218 { 219 PetscFunctionBegin; 220 PetscAssertPointer(state, 1); 221 *state = petsc_log_state; 222 PetscFunctionReturn(PETSC_SUCCESS); 223 } 224 225 static PetscErrorCode PetscLogHandlerCopyToHot(PetscLogHandler h, PetscLogHandlerHot *hot) 226 { 227 PetscFunctionBegin; 228 hot->handler = h; 229 hot->eventBegin = h->ops->eventbegin; 230 hot->eventEnd = h->ops->eventend; 231 hot->eventSync = h->ops->eventsync; 232 hot->objectCreate = h->ops->objectcreate; 233 hot->objectDestroy = h->ops->objectdestroy; 234 PetscFunctionReturn(PETSC_SUCCESS); 235 } 236 237 /*@ 238 PetscLogHandlerStart - Connect a log handler to PETSc's global logging stream and state. 239 240 Logically collective 241 242 Input Parameters: 243 . h - a `PetscLogHandler` 244 245 Level: developer 246 247 Notes: 248 249 Users should only need this if they create their own log handlers: handlers that are started 250 from the command line (such as `-log_view` and `-log_trace`) or from a function like 251 `PetscLogNestedBegin()` will automatically be started. 252 253 There is a limit of `PESC_LOG_HANDLER_MAX` handlers that can be active at one time. 254 255 To disconnect a handler from the global stream call `PetscLogHandlerStop()`. 256 257 When a log handler is started, stages that have already been pushed with `PetscLogStagePush()`, 258 will be pushed for the new log handler, but it will not be informed of any events that are 259 in progress. It is recommended to start any user-defined log handlers immediately following 260 before any user-defined stages are pushed. 261 262 .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogState`, `PetscLogHandlerStop()` 263 @*/ 264 PetscErrorCode PetscLogHandlerStart(PetscLogHandler h) 265 { 266 PetscFunctionBegin; 267 for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) { 268 if (PetscLogHandlers[i].handler == h) PetscFunctionReturn(PETSC_SUCCESS); 269 } 270 for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) { 271 if (PetscLogHandlers[i].handler == NULL) { 272 PetscCall(PetscObjectReference((PetscObject)h)); 273 PetscCall(PetscLogHandlerCopyToHot(h, &PetscLogHandlers[i])); 274 if (petsc_log_state) { 275 PetscLogStage stack_height; 276 PetscIntStack orig_stack, temp_stack; 277 278 PetscCall(PetscLogHandlerSetState(h, petsc_log_state)); 279 stack_height = petsc_log_state->stage_stack->top + 1; 280 PetscCall(PetscIntStackCreate(&temp_stack)); 281 orig_stack = petsc_log_state->stage_stack; 282 petsc_log_state->stage_stack = temp_stack; 283 petsc_log_state->current_stage = -1; 284 for (int s = 0; s < stack_height; s++) { 285 PetscLogStage stage = (PetscLogStage)orig_stack->stack[s]; 286 PetscCall(PetscLogHandlerStagePush(h, stage)); 287 PetscCall(PetscIntStackPush(temp_stack, stage)); 288 petsc_log_state->current_stage = stage; 289 } 290 PetscCall(PetscIntStackDestroy(temp_stack)); 291 petsc_log_state->stage_stack = orig_stack; 292 } 293 PetscFunctionReturn(PETSC_SUCCESS); 294 } 295 } 296 SETERRQ(PetscObjectComm((PetscObject)h), PETSC_ERR_ARG_WRONGSTATE, "%d log handlers already started, cannot start another", PETSC_LOG_HANDLER_MAX); 297 PetscFunctionReturn(PETSC_SUCCESS); 298 } 299 300 /*@ 301 PetscLogHandlerStop - Disconnect a log handler from PETSc's global logging stream. 302 303 Logically collective 304 305 Input Parameters: 306 . h - a `PetscLogHandler` 307 308 Level: developer 309 310 Note: 311 After `PetscLogHandlerStop()`, the handler can still access the global logging state 312 with `PetscLogHandlerGetState()`, so that it can access the registry when post-processing 313 (for instance, in `PetscLogHandlerView()`), 314 315 When a log handler is stopped, the remaining stages will be popped before it is 316 disconnected from the log stream. 317 318 .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogState`, `PetscLogHandlerStart()` 319 @*/ 320 PetscErrorCode PetscLogHandlerStop(PetscLogHandler h) 321 { 322 PetscFunctionBegin; 323 for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) { 324 if (PetscLogHandlers[i].handler == h) { 325 if (petsc_log_state) { 326 PetscLogState state; 327 PetscLogStage stack_height; 328 PetscIntStack orig_stack, temp_stack; 329 330 PetscCall(PetscLogHandlerGetState(h, &state)); 331 PetscCheck(state == petsc_log_state, PETSC_COMM_WORLD, PETSC_ERR_ARG_WRONGSTATE, "Called PetscLogHandlerStop() for a PetscLogHander that was not started."); 332 stack_height = petsc_log_state->stage_stack->top + 1; 333 PetscCall(PetscIntStackCreate(&temp_stack)); 334 orig_stack = petsc_log_state->stage_stack; 335 petsc_log_state->stage_stack = temp_stack; 336 for (int s = 0; s < stack_height; s++) { 337 PetscLogStage stage = (PetscLogStage)orig_stack->stack[s]; 338 339 PetscCall(PetscIntStackPush(temp_stack, stage)); 340 } 341 for (int s = 0; s < stack_height; s++) { 342 PetscLogStage stage; 343 PetscBool empty; 344 345 PetscCall(PetscIntStackPop(temp_stack, &stage)); 346 PetscCall(PetscIntStackEmpty(temp_stack, &empty)); 347 if (!empty) { 348 PetscCall(PetscIntStackTop(temp_stack, &petsc_log_state->current_stage)); 349 } else petsc_log_state->current_stage = -1; 350 PetscCall(PetscLogHandlerStagePop(h, stage)); 351 } 352 PetscCall(PetscIntStackDestroy(temp_stack)); 353 petsc_log_state->stage_stack = orig_stack; 354 PetscCall(PetscIntStackTop(petsc_log_state->stage_stack, &petsc_log_state->current_stage)); 355 } 356 PetscCall(PetscArrayzero(&PetscLogHandlers[i], 1)); 357 PetscCall(PetscObjectDereference((PetscObject)h)); 358 } 359 } 360 PetscFunctionReturn(PETSC_SUCCESS); 361 } 362 363 /*@C 364 PetscLogIsActive - Check if logging is currently in progress. 365 366 Not Collective 367 368 Output Parameter: 369 . isActive - `PETSC_TRUE` if logging is in progress, `PETSC_FALSE` otherwise 370 371 Level: beginner 372 373 .seealso: [](ch_profiling), `PetscLogDefaultBegin()` 374 @*/ 375 PetscErrorCode PetscLogIsActive(PetscBool *isActive) 376 { 377 PetscFunctionBegin; 378 *isActive = PETSC_FALSE; 379 if (petsc_log_state) { 380 for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) { 381 if (PetscLogHandlers[i].handler) { 382 *isActive = PETSC_TRUE; 383 PetscFunctionReturn(PETSC_SUCCESS); 384 } 385 } 386 } 387 PetscFunctionReturn(PETSC_SUCCESS); 388 } 389 390 PETSC_UNUSED static PetscErrorCode PetscLogEventBeginIsActive(PetscBool *isActive) 391 { 392 PetscFunctionBegin; 393 *isActive = PETSC_FALSE; 394 if (petsc_log_state) { 395 for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) { 396 if (PetscLogHandlers[i].eventBegin) { 397 *isActive = PETSC_TRUE; 398 PetscFunctionReturn(PETSC_SUCCESS); 399 } 400 } 401 } 402 PetscFunctionReturn(PETSC_SUCCESS); 403 } 404 405 PETSC_UNUSED static PetscErrorCode PetscLogEventEndIsActive(PetscBool *isActive) 406 { 407 PetscFunctionBegin; 408 *isActive = PETSC_FALSE; 409 if (petsc_log_state) { 410 for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) { 411 if (PetscLogHandlers[i].eventEnd) { 412 *isActive = PETSC_TRUE; 413 PetscFunctionReturn(PETSC_SUCCESS); 414 } 415 } 416 } 417 PetscFunctionReturn(PETSC_SUCCESS); 418 } 419 420 static PetscErrorCode PetscLogTypeBegin(PetscLogHandlerType type) 421 { 422 PetscLogHandler handler; 423 424 PetscFunctionBegin; 425 PetscCall(PetscLogTryGetHandler(type, &handler)); 426 if (handler) PetscFunctionReturn(PETSC_SUCCESS); 427 PetscCall(PetscLogHandlerCreate(PETSC_COMM_WORLD, &handler)); 428 PetscCall(PetscLogHandlerSetType(handler, type)); 429 PetscCall(PetscLogHandlerStart(handler)); 430 PetscCall(PetscLogHandlerDestroy(&handler)); 431 PetscFunctionReturn(PETSC_SUCCESS); 432 } 433 434 /*@C 435 PetscLogDefaultBegin - Turns on logging of objects and events using the default log handler. This logs flop 436 rates and object creation and should not slow programs down too much. 437 This routine may be called more than once. 438 439 Logically Collective over `PETSC_COMM_WORLD` 440 441 Options Database Key: 442 . -log_view [viewertype:filename:viewerformat] - Prints summary of flop and timing information to the 443 screen (for code configured with --with-log=1 (which is the default)) 444 445 Example Usage: 446 .vb 447 PetscInitialize(...); 448 PetscLogDefaultBegin(); 449 ... code ... 450 PetscLogView(viewer); or PetscLogDump(); 451 PetscFinalize(); 452 .ve 453 454 Level: advanced 455 456 Note: 457 `PetscLogView()` or `PetscLogDump()` actually cause the printing of 458 the logging information. 459 460 .seealso: [](ch_profiling), `PetscLogDump()`, `PetscLogView()`, `PetscLogTraceBegin()` 461 @*/ 462 PetscErrorCode PetscLogDefaultBegin(void) 463 { 464 PetscFunctionBegin; 465 PetscCall(PetscLogTypeBegin(PETSCLOGHANDLERDEFAULT)); 466 PetscFunctionReturn(PETSC_SUCCESS); 467 } 468 469 /*@C 470 PetscLogTraceBegin - Begins trace logging. Every time a PETSc event 471 begins or ends, the event name is printed. 472 473 Logically Collective on `PETSC_COMM_WORLD` 474 475 Input Parameter: 476 . file - The file to print trace in (e.g. stdout) 477 478 Options Database Key: 479 . -log_trace [filename] - Begins `PetscLogTraceBegin()` 480 481 Level: intermediate 482 483 Notes: 484 `PetscLogTraceBegin()` prints the processor number, the execution time (sec), 485 then "Event begin:" or "Event end:" followed by the event name. 486 487 `PetscLogTraceBegin()` allows tracing of all PETSc calls, which is useful 488 to determine where a program is hanging without running in the 489 debugger. Can be used in conjunction with the -info option. 490 491 .seealso: [](ch_profiling), `PetscLogDump()`, `PetscLogView()`, `PetscLogDefaultBegin()` 492 @*/ 493 PetscErrorCode PetscLogTraceBegin(FILE *file) 494 { 495 PetscLogHandler handler; 496 PetscFunctionBegin; 497 PetscCall(PetscLogTryGetHandler(PETSCLOGHANDLERTRACE, &handler)); 498 if (handler) PetscFunctionReturn(PETSC_SUCCESS); 499 PetscCall(PetscLogHandlerCreateTrace(PETSC_COMM_WORLD, file, &handler)); 500 PetscCall(PetscLogHandlerStart(handler)); 501 PetscCall(PetscLogHandlerDestroy(&handler)); 502 PetscFunctionReturn(PETSC_SUCCESS); 503 } 504 505 PETSC_INTERN PetscErrorCode PetscLogHandlerCreate_Nested(MPI_Comm, PetscLogHandler *); 506 507 /*@C 508 PetscLogNestedBegin - Turns on nested logging of objects and events. This logs flop 509 rates and object creation and should not slow programs down too much. 510 511 Logically Collective over `PETSC_COMM_WORLD` 512 513 Options Database Keys: 514 . -log_view :filename.xml:ascii_xml - Prints an XML summary of flop and timing information to the file 515 516 Example Usage: 517 .vb 518 PetscInitialize(...); 519 PetscLogNestedBegin(); 520 ... code ... 521 PetscLogView(viewer); 522 PetscFinalize(); 523 .ve 524 525 Level: advanced 526 527 .seealso: `PetscLogDump()`, `PetscLogView()`, `PetscLogTraceBegin()`, `PetscLogDefaultBegin()` 528 @*/ 529 PetscErrorCode PetscLogNestedBegin(void) 530 { 531 PetscFunctionBegin; 532 PetscCall(PetscLogTypeBegin(PETSCLOGHANDLERNESTED)); 533 PetscFunctionReturn(PETSC_SUCCESS); 534 } 535 536 /*@C 537 PetscLogLegacyCallbacksBegin - Create and start a log handler from callbacks 538 matching the now deprecated function pointers `PetscLogPLB`, `PetscLogPLE`, 539 `PetscLogPHC`, `PetscLogPHD`. 540 541 Logically Collective over `PETSC_COMM_WORLD` 542 543 Input Parameters: 544 + PetscLogPLB - A callback that will be executed by `PetscLogEventBegin()` (or `NULL`) 545 . PetscLogPLE - A callback that will be executed by `PetscLogEventEnd()` (or `NULL`) 546 . PetscLogPHC - A callback that will be executed by `PetscLogObjectCreate()` (or `NULL`) 547 - PetscLogPHD - A callback that will be executed by `PetscLogObjectCreate()` (or `NULL`) 548 549 Calling sequence of `PetscLogPLB`: 550 + e - a `PetscLogEvent` that is beginning 551 . _i - deprecated, unused 552 . o1 - a `PetscObject` associated with `e` (or `NULL`) 553 . o2 - a `PetscObject` associated with `e` (or `NULL`) 554 . o3 - a `PetscObject` associated with `e` (or `NULL`) 555 - o4 - a `PetscObject` associated with `e` (or `NULL`) 556 557 Calling sequence of `PetscLogPLE`: 558 + e - a `PetscLogEvent` that is beginning 559 . _i - deprecated, unused 560 . o1 - a `PetscObject` associated with `e` (or `NULL`) 561 . o2 - a `PetscObject` associated with `e` (or `NULL`) 562 . o3 - a `PetscObject` associated with `e` (or `NULL`) 563 - o4 - a `PetscObject` associated with `e` (or `NULL`) 564 565 Calling sequence of `PetscLogPHC`: 566 . o - a `PetscObject` that has just been created 567 568 Calling sequence of `PetscLogPHD`: 569 . o - a `PetscObject` that is about to be destroyed 570 571 Level: advanced 572 573 Notes: 574 This is for transitioning from the deprecated function `PetscLogSet()` and should not be used in new code. 575 576 This should help migrate external log handlers to use `PetscLogHandler`, but 577 callbacks that depend on the deprecated `PetscLogStage` datatype will have to be 578 updated. 579 580 .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogHandlerStart()`, `PetscLogState` 581 @*/ 582 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)) 583 { 584 PetscLogHandler handler; 585 586 PetscFunctionBegin; 587 PetscCall(PetscLogHandlerCreateLegacy(PETSC_COMM_WORLD, PetscLogPLB, PetscLogPLE, PetscLogPHC, PetscLogPHD, &handler)); 588 PetscCall(PetscLogHandlerStart(handler)); 589 PetscCall(PetscLogHandlerDestroy(&handler)); 590 PetscFunctionReturn(PETSC_SUCCESS); 591 } 592 593 #if defined(PETSC_HAVE_MPE) 594 #include <mpe.h> 595 static PetscBool PetscBeganMPE = PETSC_FALSE; 596 #endif 597 598 /*@C 599 PetscLogMPEBegin - Turns on MPE logging of events. This creates large log files and slows the 600 program down. 601 602 Collective over `PETSC_COMM_WORLD` 603 604 Options Database Key: 605 . -log_mpe - Prints extensive log information 606 607 Level: advanced 608 609 Note: 610 A related routine is `PetscLogDefaultBegin()` (with the options key `-log_view`), which is 611 intended for production runs since it logs only flop rates and object creation (and should 612 not significantly slow the programs). 613 614 .seealso: [](ch_profiling), `PetscLogDump()`, `PetscLogDefaultBegin()`, `PetscLogEventActivate()`, 615 `PetscLogEventDeactivate()` 616 @*/ 617 PetscErrorCode PetscLogMPEBegin(void) 618 { 619 PetscFunctionBegin; 620 #if defined(PETSC_HAVE_MPE) 621 /* Do MPE initialization */ 622 if (!MPE_Initialized_logging()) { /* This function exists in mpich 1.1.2 and higher */ 623 PetscCall(PetscInfo(0, "Initializing MPE.\n")); 624 PetscCall(MPE_Init_log()); 625 626 PetscBeganMPE = PETSC_TRUE; 627 } else { 628 PetscCall(PetscInfo(0, "MPE already initialized. Not attempting to reinitialize.\n")); 629 } 630 PetscCall(PetscLogTypeBegin(PETSCLOGHANDLERMPE)); 631 #else 632 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP_SYS, "PETSc was configured without MPE support, reconfigure with --with-mpe or --download-mpe"); 633 #endif 634 PetscFunctionReturn(PETSC_SUCCESS); 635 } 636 637 #if defined(PETSC_HAVE_TAU_PERFSTUBS) 638 #include <../src/sys/perfstubs/timer.h> 639 #endif 640 641 /*@C 642 PetscLogPerfstubsBegin - Turns on logging of events using the perfstubs interface. 643 644 Collective over `PETSC_COMM_WORLD` 645 646 Options Database Key: 647 . -log_perfstubs - use an external log handler through the perfstubs interface 648 649 Level: advanced 650 651 .seealso: [](ch_profiling), `PetscLogDefaultBegin()`, `PetscLogEventActivate()` 652 @*/ 653 PetscErrorCode PetscLogPerfstubsBegin(void) 654 { 655 PetscFunctionBegin; 656 #if defined(PETSC_HAVE_TAU_PERFSTUBS) 657 PetscCall(PetscLogTypeBegin(PETSCLOGHANDLERPERFSTUBS)); 658 #else 659 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP_SYS, "PETSc was configured without perfstubs support, reconfigure with --with-tau-perfstubs"); 660 #endif 661 PetscFunctionReturn(PETSC_SUCCESS); 662 } 663 664 /*@ 665 PetscLogActions - Determines whether actions are logged for the default log handler. 666 667 Not Collective 668 669 Input Parameter: 670 . flag - `PETSC_TRUE` if actions are to be logged 671 672 Options Database Key: 673 + -log_exclude_actions - (deprecated) Does nothing 674 - -log_include_actions - Turn on action logging 675 676 Level: intermediate 677 678 Note: 679 Logging of actions continues to consume more memory as the program 680 runs. Long running programs should consider turning this feature off. 681 682 .seealso: [](ch_profiling), `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogGetDefaultHandler()` 683 @*/ 684 PetscErrorCode PetscLogActions(PetscBool flag) 685 { 686 PetscFunctionBegin; 687 for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) { 688 PetscLogHandler h = PetscLogHandlers[i].handler; 689 690 if (h) PetscCall(PetscLogHandlerSetLogActions(h, flag)); 691 } 692 PetscFunctionReturn(PETSC_SUCCESS); 693 } 694 695 /*@ 696 PetscLogObjects - Determines whether objects are logged for the graphical viewer. 697 698 Not Collective 699 700 Input Parameter: 701 . flag - `PETSC_TRUE` if objects are to be logged 702 703 Options Database Key: 704 + -log_exclude_objects - (deprecated) Does nothing 705 - -log_include_objects - Turns on object logging 706 707 Level: intermediate 708 709 Note: 710 Logging of objects continues to consume more memory as the program 711 runs. Long running programs should consider turning this feature off. 712 713 .seealso: [](ch_profiling), `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogGetDefaultHandler()` 714 @*/ 715 PetscErrorCode PetscLogObjects(PetscBool flag) 716 { 717 PetscFunctionBegin; 718 for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) { 719 PetscLogHandler h = PetscLogHandlers[i].handler; 720 721 if (h) PetscCall(PetscLogHandlerSetLogObjects(h, flag)); 722 } 723 PetscFunctionReturn(PETSC_SUCCESS); 724 } 725 726 /*------------------------------------------------ Stage Functions --------------------------------------------------*/ 727 /*@C 728 PetscLogStageRegister - Attaches a character string name to a logging stage. 729 730 Not Collective 731 732 Input Parameter: 733 . sname - The name to associate with that stage 734 735 Output Parameter: 736 . stage - The stage number or -1 if logging is not active (`PetscLogIsActive()`). 737 738 Level: intermediate 739 740 .seealso: [](ch_profiling), `PetscLogStagePush()`, `PetscLogStagePop()` 741 @*/ 742 PetscErrorCode PetscLogStageRegister(const char sname[], PetscLogStage *stage) 743 { 744 PetscLogState state; 745 746 PetscFunctionBegin; 747 *stage = -1; 748 PetscCall(PetscLogGetState(&state)); 749 if (state) PetscCall(PetscLogStateStageRegister(state, sname, stage)); 750 PetscFunctionReturn(PETSC_SUCCESS); 751 } 752 753 /*@C 754 PetscLogStagePush - This function pushes a stage on the logging stack. Events started and stopped until `PetscLogStagePop()` will be associated with the stage 755 756 Not Collective 757 758 Input Parameter: 759 . stage - The stage on which to log 760 761 Example Usage: 762 If the option -log_view is used to run the program containing the 763 following code, then 2 sets of summary data will be printed during 764 PetscFinalize(). 765 .vb 766 PetscInitialize(int *argc,char ***args,0,0); 767 [stage 0 of code] 768 PetscLogStagePush(1); 769 [stage 1 of code] 770 PetscLogStagePop(); 771 PetscBarrier(...); 772 [more stage 0 of code] 773 PetscFinalize(); 774 .ve 775 776 Level: intermediate 777 778 Note: 779 Use `PetscLogStageRegister()` to register a stage. 780 781 .seealso: [](ch_profiling), `PetscLogStagePop()`, `PetscLogStageRegister()`, `PetscBarrier()` 782 @*/ 783 PetscErrorCode PetscLogStagePush(PetscLogStage stage) 784 { 785 PetscLogState state; 786 787 PetscFunctionBegin; 788 PetscCall(PetscLogGetState(&state)); 789 if (!state) PetscFunctionReturn(PETSC_SUCCESS); 790 for (int i = 0; i < PETSC_LOG_HANDLER_MAX; i++) { 791 PetscLogHandler h = PetscLogHandlers[i].handler; 792 if (h) PetscCall(PetscLogHandlerStagePush(h, stage)); 793 } 794 PetscCall(PetscLogStateStagePush(state, stage)); 795 PetscFunctionReturn(PETSC_SUCCESS); 796 } 797 798 /*@C 799 PetscLogStagePop - This function pops a stage from the logging stack that was pushed with `PetscLogStagePush()` 800 801 Not Collective 802 803 Example Usage: 804 If the option -log_view is used to run the program containing the 805 following code, then 2 sets of summary data will be printed during 806 PetscFinalize(). 807 .vb 808 PetscInitialize(int *argc,char ***args,0,0); 809 [stage 0 of code] 810 PetscLogStagePush(1); 811 [stage 1 of code] 812 PetscLogStagePop(); 813 PetscBarrier(...); 814 [more stage 0 of code] 815 PetscFinalize(); 816 .ve 817 818 Level: intermediate 819 820 .seealso: [](ch_profiling), `PetscLogStagePush()`, `PetscLogStageRegister()`, `PetscBarrier()` 821 @*/ 822 PetscErrorCode PetscLogStagePop(void) 823 { 824 PetscLogState state; 825 PetscLogStage current_stage; 826 827 PetscFunctionBegin; 828 PetscCall(PetscLogGetState(&state)); 829 if (!state) PetscFunctionReturn(PETSC_SUCCESS); 830 current_stage = state->current_stage; 831 PetscCall(PetscLogStateStagePop(state)); 832 for (int i = 0; i < PETSC_LOG_HANDLER_MAX; i++) { 833 PetscLogHandler h = PetscLogHandlers[i].handler; 834 if (h) PetscCall(PetscLogHandlerStagePop(h, current_stage)); 835 } 836 PetscFunctionReturn(PETSC_SUCCESS); 837 } 838 839 /*@ 840 PetscLogStageSetActive - Sets if a stage is used for `PetscLogEventBegin()` and `PetscLogEventEnd()`. 841 842 Not Collective 843 844 Input Parameters: 845 + stage - The stage 846 - isActive - The activity flag, `PETSC_TRUE` for logging, else `PETSC_FALSE` (defaults to `PETSC_TRUE`) 847 848 Level: intermediate 849 850 Note: 851 If this is set to `PETSC_FALSE` the logging acts as if the stage did not exist 852 853 .seealso: [](ch_profiling), `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()` 854 @*/ 855 PetscErrorCode PetscLogStageSetActive(PetscLogStage stage, PetscBool isActive) 856 { 857 PetscLogState state; 858 859 PetscFunctionBegin; 860 PetscCall(PetscLogGetState(&state)); 861 if (state) PetscCall(PetscLogStateStageSetActive(state, stage, isActive)); 862 PetscFunctionReturn(PETSC_SUCCESS); 863 } 864 865 /*@ 866 PetscLogStageGetActive - Checks if a stage is used for `PetscLogEventBegin()` and `PetscLogEventEnd()`. 867 868 Not Collective 869 870 Input Parameter: 871 . stage - The stage 872 873 Output Parameter: 874 . isActive - The activity flag, `PETSC_TRUE` for logging, else `PETSC_FALSE` (defaults to `PETSC_TRUE`) 875 876 Level: intermediate 877 878 .seealso: [](ch_profiling), `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()` 879 @*/ 880 PetscErrorCode PetscLogStageGetActive(PetscLogStage stage, PetscBool *isActive) 881 { 882 PetscLogState state; 883 884 PetscFunctionBegin; 885 *isActive = PETSC_FALSE; 886 PetscCall(PetscLogGetState(&state)); 887 if (state) PetscCall(PetscLogStateStageGetActive(state, stage, isActive)); 888 PetscFunctionReturn(PETSC_SUCCESS); 889 } 890 891 /*@ 892 PetscLogStageSetVisible - Determines stage visibility in `PetscLogView()` 893 894 Not Collective 895 896 Input Parameters: 897 + stage - The stage 898 - isVisible - The visibility flag, `PETSC_TRUE` to print, else `PETSC_FALSE` (defaults to `PETSC_TRUE`) 899 900 Level: intermediate 901 902 Developer Notes: 903 Visibility only affects the default log handler in `PetscLogView()`: stages that are 904 set to invisible are suppressed from output. 905 906 .seealso: [](ch_profiling), `PetscLogStageGetVisible()`, `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogView()`, `PetscLogGetDefaultHandler()` 907 @*/ 908 PetscErrorCode PetscLogStageSetVisible(PetscLogStage stage, PetscBool isVisible) 909 910 { 911 PetscFunctionBegin; 912 for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) { 913 PetscLogHandler h = PetscLogHandlers[i].handler; 914 915 if (h) PetscCall(PetscLogHandlerStageSetVisible(h, stage, isVisible)); 916 } 917 PetscFunctionReturn(PETSC_SUCCESS); 918 } 919 920 /*@ 921 PetscLogStageGetVisible - Returns stage visibility in `PetscLogView()` 922 923 Not Collective 924 925 Input Parameter: 926 . stage - The stage 927 928 Output Parameter: 929 . isVisible - The visibility flag, `PETSC_TRUE` to print, else `PETSC_FALSE` (defaults to `PETSC_TRUE`) 930 931 Level: intermediate 932 933 .seealso: [](ch_profiling), `PetscLogStageSetVisible()`, `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogView()`, `PetscLogGetDefaultHandler()` 934 @*/ 935 PetscErrorCode PetscLogStageGetVisible(PetscLogStage stage, PetscBool *isVisible) 936 { 937 PetscLogHandler handler; 938 939 PetscFunctionBegin; 940 *isVisible = PETSC_FALSE; 941 PetscCall(PetscLogTryGetHandler(PETSCLOGHANDLERDEFAULT, &handler)); 942 if (handler) { PetscCall(PetscLogHandlerStageGetVisible(handler, stage, isVisible)); } 943 PetscFunctionReturn(PETSC_SUCCESS); 944 } 945 946 /*@C 947 PetscLogStageGetId - Returns the stage id when given the stage name. 948 949 Not Collective 950 951 Input Parameter: 952 . name - The stage name 953 954 Output Parameter: 955 . stage - The stage, , or -1 if no stage with that name exists 956 957 Level: intermediate 958 959 .seealso: [](ch_profiling), `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()` 960 @*/ 961 PetscErrorCode PetscLogStageGetId(const char name[], PetscLogStage *stage) 962 { 963 PetscLogState state; 964 965 PetscFunctionBegin; 966 *stage = -1; 967 PetscCall(PetscLogGetState(&state)); 968 if (state) PetscCall(PetscLogStateGetStageFromName(state, name, stage)); 969 PetscFunctionReturn(PETSC_SUCCESS); 970 } 971 972 /*@C 973 PetscLogStageGetName - Returns the stage name when given the stage id. 974 975 Not Collective 976 977 Input Parameter: 978 . stage - The stage 979 980 Output Parameter: 981 . name - The stage name 982 983 Level: intermediate 984 985 .seealso: [](ch_profiling), `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()` 986 @*/ 987 PetscErrorCode PetscLogStageGetName(PetscLogStage stage, const char **name) 988 { 989 PetscLogStageInfo stage_info; 990 PetscLogState state; 991 992 PetscFunctionBegin; 993 *name = NULL; 994 PetscCall(PetscLogGetState(&state)); 995 if (!state) PetscFunctionReturn(PETSC_SUCCESS); 996 PetscCall(PetscLogStateStageGetInfo(state, stage, &stage_info)); 997 *name = stage_info.name; 998 PetscFunctionReturn(PETSC_SUCCESS); 999 } 1000 1001 /*------------------------------------------------ Event Functions --------------------------------------------------*/ 1002 1003 /*@C 1004 PetscLogEventRegister - Registers an event name for logging operations 1005 1006 Not Collective 1007 1008 Input Parameters: 1009 + name - The name associated with the event 1010 - classid - The classid associated to the class for this event, obtain either with 1011 `PetscClassIdRegister()` or use a predefined one such as `KSP_CLASSID`, `SNES_CLASSID`, the predefined ones 1012 are only available in C code 1013 1014 Output Parameter: 1015 . event - The event id for use with `PetscLogEventBegin()` and `PetscLogEventEnd()`. 1016 1017 Example Usage: 1018 .vb 1019 PetscLogEvent USER_EVENT; 1020 PetscClassId classid; 1021 PetscLogDouble user_event_flops; 1022 PetscClassIdRegister("class name",&classid); 1023 PetscLogEventRegister("User event name",classid,&USER_EVENT); 1024 PetscLogEventBegin(USER_EVENT,0,0,0,0); 1025 [code segment to monitor] 1026 PetscLogFlops(user_event_flops); 1027 PetscLogEventEnd(USER_EVENT,0,0,0,0); 1028 .ve 1029 1030 Level: intermediate 1031 1032 Notes: 1033 PETSc automatically logs library events if the code has been 1034 configured with --with-log (which is the default) and 1035 -log_view or -log_all is specified. `PetscLogEventRegister()` is 1036 intended for logging user events to supplement this PETSc 1037 information. 1038 1039 PETSc can gather data for use with the utilities Jumpshot 1040 (part of the MPICH distribution). If PETSc has been compiled 1041 with flag -DPETSC_HAVE_MPE (MPE is an additional utility within 1042 MPICH), the user can employ another command line option, -log_mpe, 1043 to create a logfile, "mpe.log", which can be visualized 1044 Jumpshot. 1045 1046 The classid is associated with each event so that classes of events 1047 can be disabled simultaneously, such as all matrix events. The user 1048 can either use an existing classid, such as `MAT_CLASSID`, or create 1049 their own as shown in the example. 1050 1051 If an existing event with the same name exists, its event handle is 1052 returned instead of creating a new event. 1053 1054 .seealso: [](ch_profiling), `PetscLogStageRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogFlops()`, 1055 `PetscLogEventActivate()`, `PetscLogEventDeactivate()`, `PetscClassIdRegister()` 1056 @*/ 1057 PetscErrorCode PetscLogEventRegister(const char name[], PetscClassId classid, PetscLogEvent *event) 1058 { 1059 PetscLogState state; 1060 1061 PetscFunctionBegin; 1062 *event = -1; 1063 PetscCall(PetscLogGetState(&state)); 1064 if (state) PetscCall(PetscLogStateEventRegister(state, name, classid, event)); 1065 PetscFunctionReturn(PETSC_SUCCESS); 1066 } 1067 1068 /*@ 1069 PetscLogEventSetCollective - Indicates that a particular event is collective. 1070 1071 Not Collective 1072 1073 Input Parameters: 1074 + event - The event id 1075 - collective - Boolean flag indicating whether a particular event is collective 1076 1077 Level: developer 1078 1079 Notes: 1080 New events returned from `PetscLogEventRegister()` are collective by default. 1081 1082 Collective events are handled specially if the -log_sync is used. In that case the logging saves information about 1083 two parts of the event; the time for all the MPI ranks to synchronize and then the time for the actual computation/communication 1084 to be performed. This option is useful to debug imbalance within the computations or communications 1085 1086 .seealso: [](ch_profiling), `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogEventRegister()` 1087 @*/ 1088 PetscErrorCode PetscLogEventSetCollective(PetscLogEvent event, PetscBool collective) 1089 { 1090 PetscLogState state; 1091 1092 PetscFunctionBegin; 1093 PetscCall(PetscLogGetState(&state)); 1094 if (state) PetscCall(PetscLogStateEventSetCollective(state, event, collective)); 1095 PetscFunctionReturn(PETSC_SUCCESS); 1096 } 1097 1098 /* 1099 PetscLogClassSetActiveAll - Activate or inactivate logging for all events associated with a PETSc object class in every stage. 1100 1101 Not Collective 1102 1103 Input Parameters: 1104 + classid - The object class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc. 1105 - isActive - if `PETSC_FALSE`, events associated with this class will not be send to log handlers. 1106 1107 Level: developer 1108 1109 .seealso: [](ch_profiling), `PetscLogEventActivate()`, `PetscLogEventActivateAll()`, `PetscLogStageSetActive()`, `PetscLogEventActivateClass()` 1110 */ 1111 static PetscErrorCode PetscLogClassSetActiveAll(PetscClassId classid, PetscBool isActive) 1112 { 1113 PetscLogState state; 1114 1115 PetscFunctionBegin; 1116 PetscCall(PetscLogGetState(&state)); 1117 if (state) PetscCall(PetscLogStateClassSetActiveAll(state, classid, isActive)); 1118 PetscFunctionReturn(PETSC_SUCCESS); 1119 } 1120 1121 /*@ 1122 PetscLogEventIncludeClass - Activates event logging for a PETSc object class in every stage. 1123 1124 Not Collective 1125 1126 Input Parameter: 1127 . classid - The object class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc. 1128 1129 Level: developer 1130 1131 .seealso: [](ch_profiling), `PetscLogEventActivateClass()`, `PetscLogEventDeactivateClass()`, `PetscLogEventActivate()`, `PetscLogEventDeactivate()` 1132 @*/ 1133 PetscErrorCode PetscLogEventIncludeClass(PetscClassId classid) 1134 { 1135 PetscFunctionBegin; 1136 PetscCall(PetscLogClassSetActiveAll(classid, PETSC_TRUE)); 1137 PetscFunctionReturn(PETSC_SUCCESS); 1138 } 1139 1140 /*@ 1141 PetscLogEventExcludeClass - Deactivates event logging for a PETSc object class in every stage. 1142 1143 Not Collective 1144 1145 Input Parameter: 1146 . classid - The object class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc. 1147 1148 Level: developer 1149 1150 Note: 1151 If a class is excluded then events associated with that class are not logged. 1152 1153 .seealso: [](ch_profiling), `PetscLogEventDeactivateClass()`, `PetscLogEventActivateClass()`, `PetscLogEventDeactivate()`, `PetscLogEventActivate()` 1154 @*/ 1155 PetscErrorCode PetscLogEventExcludeClass(PetscClassId classid) 1156 { 1157 PetscFunctionBegin; 1158 PetscCall(PetscLogClassSetActiveAll(classid, PETSC_FALSE)); 1159 PetscFunctionReturn(PETSC_SUCCESS); 1160 } 1161 1162 /* 1163 PetscLogEventSetActive - Activate or inactivate logging for an event in a given stage 1164 1165 Not Collective 1166 1167 Input Parameters: 1168 + stage - A registered `PetscLogStage` (or `PETSC_DEFAULT` for the current stage) 1169 . event - A `PetscLogEvent` 1170 - isActive - If `PETSC_FALSE`, activity from this event (`PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogEventSync()`) will not be sent to log handlers during this stage 1171 1172 Usage: 1173 .vb 1174 PetscLogEventSetActive(VEC_SetValues, PETSC_FALSE); 1175 [code where you do not want to log VecSetValues()] 1176 PetscLogEventSetActive(VEC_SetValues, PETSC_TRUE); 1177 [code where you do want to log VecSetValues()] 1178 .ve 1179 1180 Level: advanced 1181 1182 Note: 1183 The event may be either a pre-defined PETSc event (found in include/petsclog.h) 1184 or an event number obtained with `PetscLogEventRegister()`. 1185 1186 .seealso: [](ch_profiling), `PetscLogEventDeactivatePush()`, `PetscLogEventDeactivatePop()` 1187 */ 1188 static PetscErrorCode PetscLogEventSetActive(PetscLogStage stage, PetscLogEvent event, PetscBool isActive) 1189 { 1190 PetscLogState state; 1191 1192 PetscFunctionBegin; 1193 PetscCall(PetscLogGetState(&state)); 1194 if (state) PetscCall(PetscLogStateEventSetActive(state, stage, event, isActive)); 1195 PetscFunctionReturn(PETSC_SUCCESS); 1196 } 1197 1198 /*@ 1199 PetscLogEventActivate - Indicates that a particular event should be logged. 1200 1201 Not Collective 1202 1203 Input Parameter: 1204 . event - The event id 1205 1206 Example Usage: 1207 .vb 1208 PetscLogEventDeactivate(VEC_SetValues); 1209 [code where you do not want to log VecSetValues()] 1210 PetscLogEventActivate(VEC_SetValues); 1211 [code where you do want to log VecSetValues()] 1212 .ve 1213 1214 Level: advanced 1215 1216 Note: 1217 The event may be either a pre-defined PETSc event (found in include/petsclog.h) 1218 or an event number obtained with `PetscLogEventRegister()`. 1219 1220 .seealso: [](ch_profiling), `PetscLogEventDeactivate()`, `PetscLogEventDeactivatePush()`, `PetscLogEventDeactivatePop()` 1221 @*/ 1222 PetscErrorCode PetscLogEventActivate(PetscLogEvent event) 1223 { 1224 PetscFunctionBegin; 1225 PetscCall(PetscLogEventSetActive(PETSC_DEFAULT, event, PETSC_TRUE)); 1226 PetscFunctionReturn(PETSC_SUCCESS); 1227 } 1228 1229 /*@ 1230 PetscLogEventDeactivate - Indicates that a particular event should not be logged. 1231 1232 Not Collective 1233 1234 Input Parameter: 1235 . event - The event id 1236 1237 Example Usage: 1238 .vb 1239 PetscLogEventDeactivate(VEC_SetValues); 1240 [code where you do not want to log VecSetValues()] 1241 PetscLogEventActivate(VEC_SetValues); 1242 [code where you do want to log VecSetValues()] 1243 .ve 1244 1245 Level: advanced 1246 1247 Note: 1248 The event may be either a pre-defined PETSc event (found in 1249 include/petsclog.h) or an event number obtained with `PetscLogEventRegister()`). 1250 1251 .seealso: [](ch_profiling), `PetscLogEventActivate()`, `PetscLogEventDeactivatePush()`, `PetscLogEventDeactivatePop()` 1252 @*/ 1253 PetscErrorCode PetscLogEventDeactivate(PetscLogEvent event) 1254 { 1255 PetscFunctionBegin; 1256 PetscCall(PetscLogEventSetActive(PETSC_DEFAULT, event, PETSC_FALSE)); 1257 PetscFunctionReturn(PETSC_SUCCESS); 1258 } 1259 1260 /*@ 1261 PetscLogEventDeactivatePush - Indicates that a particular event should not be logged until `PetscLogEventDeactivatePop()` is called 1262 1263 Not Collective 1264 1265 Input Parameter: 1266 . event - The event id 1267 1268 Example Usage: 1269 .vb 1270 PetscLogEventDeactivatePush(VEC_SetValues); 1271 [code where you do not want to log VecSetValues()] 1272 PetscLogEventDeactivatePop(VEC_SetValues); 1273 [code where you do want to log VecSetValues()] 1274 .ve 1275 1276 Level: advanced 1277 1278 Note: 1279 The event may be either a pre-defined PETSc event (found in 1280 include/petsclog.h) or an event number obtained with `PetscLogEventRegister()`). 1281 1282 PETSc's default log handler (`PetscLogDefaultBegin()`) respects this function because it can make the output of `PetscLogView()` easier to interpret, but other handlers (such as the nested handler, `PetscLogNestedBegin()`) ignore it because surpressing events is not helpful in their output formats. 1283 1284 .seealso: [](ch_profiling), `PetscLogEventActivate()`, `PetscLogEVentDeactivate()`, `PetscLogEventDeactivatePop()` 1285 @*/ 1286 PetscErrorCode PetscLogEventDeactivatePush(PetscLogEvent event) 1287 { 1288 PetscFunctionBegin; 1289 for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) { 1290 PetscLogHandler h = PetscLogHandlers[i].handler; 1291 1292 if (h) PetscCall(PetscLogHandlerEventDeactivatePush(h, PETSC_DEFAULT, event)); 1293 } 1294 PetscFunctionReturn(PETSC_SUCCESS); 1295 } 1296 1297 /*@ 1298 PetscLogEventDeactivatePop - Indicates that a particular event should again be logged after the logging was turned off with `PetscLogEventDeactivatePush()` 1299 1300 Not Collective 1301 1302 Input Parameter: 1303 . event - The event id 1304 1305 Example Usage: 1306 .vb 1307 PetscLogEventDeactivatePush(VEC_SetValues); 1308 [code where you do not want to log VecSetValues()] 1309 PetscLogEventDeactivatePop(VEC_SetValues); 1310 [code where you do want to log VecSetValues()] 1311 .ve 1312 1313 Level: advanced 1314 1315 Note: 1316 The event may be either a pre-defined PETSc event (found in 1317 include/petsclog.h) or an event number obtained with `PetscLogEventRegister()`). 1318 1319 .seealso: [](ch_profiling), `PetscLogEventActivate()`, `PetscLogEventDeactivatePush()` 1320 @*/ 1321 PetscErrorCode PetscLogEventDeactivatePop(PetscLogEvent event) 1322 { 1323 PetscFunctionBegin; 1324 for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) { 1325 PetscLogHandler h = PetscLogHandlers[i].handler; 1326 1327 if (h) PetscCall(PetscLogHandlerEventDeactivatePop(h, PETSC_DEFAULT, event)); 1328 } 1329 PetscFunctionReturn(PETSC_SUCCESS); 1330 } 1331 1332 /*@ 1333 PetscLogEventSetActiveAll - Turns on logging of all events 1334 1335 Not Collective 1336 1337 Input Parameters: 1338 + event - The event id 1339 - isActive - The activity flag determining whether the event is logged 1340 1341 Level: advanced 1342 1343 .seealso: [](ch_profiling), `PetscLogEventActivate()`, `PetscLogEventDeactivate()` 1344 @*/ 1345 PetscErrorCode PetscLogEventSetActiveAll(PetscLogEvent event, PetscBool isActive) 1346 { 1347 PetscLogState state; 1348 1349 PetscFunctionBegin; 1350 PetscCall(PetscLogGetState(&state)); 1351 if (state) PetscCall(PetscLogStateEventSetActiveAll(state, event, isActive)); 1352 PetscFunctionReturn(PETSC_SUCCESS); 1353 } 1354 1355 /* 1356 PetscLogClassSetActive - Activates event logging for a PETSc object class for the current stage 1357 1358 Not Collective 1359 1360 Input Parameters: 1361 + stage - A registered `PetscLogStage` (or `PETSC_DEFAULT` for the current stage) 1362 . classid - The event class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc. 1363 - isActive - If `PETSC_FALSE`, events associated with this class are not sent to log handlers. 1364 1365 Level: developer 1366 1367 .seealso: [](ch_profiling), `PetscLogEventIncludeClass()`, `PetscLogEventActivate()`, `PetscLogEventActivateAll()`, `PetscLogStageSetActive()` 1368 */ 1369 static PetscErrorCode PetscLogClassSetActive(PetscLogStage stage, PetscClassId classid, PetscBool isActive) 1370 { 1371 PetscLogState state; 1372 1373 PetscFunctionBegin; 1374 PetscCall(PetscLogGetState(&state)); 1375 if (state) PetscCall(PetscLogStateClassSetActive(state, stage, classid, isActive)); 1376 PetscFunctionReturn(PETSC_SUCCESS); 1377 } 1378 1379 /*@ 1380 PetscLogEventActivateClass - Activates event logging for a PETSc object class for the current stage 1381 1382 Not Collective 1383 1384 Input Parameter: 1385 . classid - The event class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc. 1386 1387 Level: developer 1388 1389 .seealso: [](ch_profiling), `PetscLogEventIncludeClass()`, `PetscLogEventExcludeClass()`, `PetscLogEventDeactivateClass()`, `PetscLogEventActivate()`, `PetscLogEventDeactivate()` 1390 @*/ 1391 PetscErrorCode PetscLogEventActivateClass(PetscClassId classid) 1392 { 1393 PetscFunctionBegin; 1394 PetscCall(PetscLogClassSetActive(PETSC_DEFAULT, classid, PETSC_TRUE)); 1395 PetscFunctionReturn(PETSC_SUCCESS); 1396 } 1397 1398 /*@ 1399 PetscLogEventDeactivateClass - Deactivates event logging for a PETSc object class for the current stage 1400 1401 Not Collective 1402 1403 Input Parameter: 1404 . classid - The event class, for example `MAT_CLASSID`, `SNES_CLASSID`, etc. 1405 1406 Level: developer 1407 1408 .seealso: [](ch_profiling), `PetscLogEventIncludeClass()`, `PetscLogEventExcludeClass()`, `PetscLogEventActivateClass()`, `PetscLogEventActivate()`, `PetscLogEventDeactivate()` 1409 @*/ 1410 PetscErrorCode PetscLogEventDeactivateClass(PetscClassId classid) 1411 { 1412 PetscFunctionBegin; 1413 PetscCall(PetscLogClassSetActive(PETSC_DEFAULT, classid, PETSC_FALSE)); 1414 PetscFunctionReturn(PETSC_SUCCESS); 1415 } 1416 1417 /*MC 1418 PetscLogEventSync - Synchronizes the beginning of a user event. 1419 1420 Synopsis: 1421 #include <petsclog.h> 1422 PetscErrorCode PetscLogEventSync(PetscLogEvent e, MPI_Comm comm) 1423 1424 Collective 1425 1426 Input Parameters: 1427 + e - `PetscLogEvent` obtained from `PetscLogEventRegister()` 1428 - comm - an MPI communicator 1429 1430 Example Usage: 1431 .vb 1432 PetscLogEvent USER_EVENT; 1433 1434 PetscLogEventRegister("User event", 0, &USER_EVENT); 1435 PetscLogEventSync(USER_EVENT, PETSC_COMM_WORLD); 1436 PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0); 1437 [code segment to monitor] 1438 PetscLogEventEnd(USER_EVENT, 0, 0, 0 , 0); 1439 .ve 1440 1441 Level: developer 1442 1443 Note: 1444 This routine should be called only if there is not a `PetscObject` available to pass to 1445 `PetscLogEventBegin()`. 1446 1447 .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()` 1448 M*/ 1449 1450 /*MC 1451 PetscLogEventBegin - Logs the beginning of a user event. 1452 1453 Synopsis: 1454 #include <petsclog.h> 1455 PetscErrorCode PetscLogEventBegin(PetscLogEvent e, PetscObject o1, PetscObject o2, PetscObject o3, PetscObject o4) 1456 1457 Not Collective 1458 1459 Input Parameters: 1460 + e - `PetscLogEvent` obtained from `PetscLogEventRegister()` 1461 . o1 - object assocated with the event, or NULL 1462 . o2 - object assocated with the event, or NULL 1463 . o3 - object assocated with the event, or NULL 1464 - o4 - object assocated with the event, or NULL 1465 1466 Fortran Synopsis: 1467 void PetscLogEventBegin(int e, PetscErrorCode ierr) 1468 1469 Example Usage: 1470 .vb 1471 PetscLogEvent USER_EVENT; 1472 1473 PetscLogDouble user_event_flops; 1474 PetscLogEventRegister("User event",0, &USER_EVENT); 1475 PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0); 1476 [code segment to monitor] 1477 PetscLogFlops(user_event_flops); 1478 PetscLogEventEnd(USER_EVENT, 0, 0, 0, 0); 1479 .ve 1480 1481 Level: intermediate 1482 1483 Developer Note: 1484 `PetscLogEventBegin()` and `PetscLogEventBegin()` return error codes instead of explicitly 1485 handling the errors that occur in the macro directly because other packages that use this 1486 macros have used them in their own functions or methods that do not return error codes and it 1487 would be disruptive to change the current behavior. 1488 1489 .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventEnd()`, `PetscLogFlops()` 1490 M*/ 1491 1492 /*MC 1493 PetscLogEventEnd - Log the end of a user event. 1494 1495 Synopsis: 1496 #include <petsclog.h> 1497 PetscErrorCode PetscLogEventEnd(PetscLogEvent e, PetscObject o1, PetscObject o2, PetscObject o3, PetscObject o4) 1498 1499 Not Collective 1500 1501 Input Parameters: 1502 + e - `PetscLogEvent` obtained from `PetscLogEventRegister()` 1503 . o1 - object assocated with the event, or NULL 1504 . o2 - object assocated with the event, or NULL 1505 . o3 - object assocated with the event, or NULL 1506 - o4 - object assocated with the event, or NULL 1507 1508 Fortran Synopsis: 1509 void PetscLogEventEnd(int e, PetscErrorCode ierr) 1510 1511 Example Usage: 1512 .vb 1513 PetscLogEvent USER_EVENT; 1514 1515 PetscLogDouble user_event_flops; 1516 PetscLogEventRegister("User event", 0, &USER_EVENT); 1517 PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0); 1518 [code segment to monitor] 1519 PetscLogFlops(user_event_flops); 1520 PetscLogEventEnd(USER_EVENT, 0, 0, 0, 0); 1521 .ve 1522 1523 Level: intermediate 1524 1525 .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogFlops()` 1526 M*/ 1527 1528 /*@C 1529 PetscLogEventGetPerfInfo - Return the performance information about the given event in the given stage 1530 1531 Input Parameters: 1532 + stage - The stage number or `PETSC_DETERMINE` for the current stage 1533 - event - The event number 1534 1535 Output Parameter: 1536 . info - This structure is filled with the performance information 1537 1538 Level: intermediate 1539 1540 Note: 1541 This is a low level routine used by the logging functions in PETSc 1542 1543 .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogGetDefaultHandler()` 1544 @*/ 1545 PetscErrorCode PetscLogEventGetPerfInfo(PetscLogStage stage, PetscLogEvent event, PetscEventPerfInfo *info) 1546 { 1547 PetscLogHandler handler; 1548 PetscEventPerfInfo *event_info; 1549 1550 PetscFunctionBegin; 1551 PetscAssertPointer(info, 3); 1552 PetscCall(PetscLogGetHandler(PETSCLOGHANDLERDEFAULT, &handler)); 1553 PetscCall(PetscLogHandlerGetEventPerfInfo(handler, stage, event, &event_info)); 1554 *info = *event_info; 1555 PetscFunctionReturn(PETSC_SUCCESS); 1556 } 1557 1558 /*@C 1559 PetscLogEventSetDof - Set the nth number of degrees of freedom of a numerical problem associated with this event 1560 1561 Not Collective 1562 1563 Input Parameters: 1564 + event - The event id to log 1565 . n - The dof index, in [0, 8) 1566 - dof - The number of dofs 1567 1568 Options Database Key: 1569 . -log_view - Activates log summary 1570 1571 Level: developer 1572 1573 Note: 1574 This is to enable logging of convergence 1575 1576 .seealso: `PetscLogEventSetError()`, `PetscLogEventRegister()`, `PetscLogGetDefaultHandler()` 1577 @*/ 1578 PetscErrorCode PetscLogEventSetDof(PetscLogEvent event, PetscInt n, PetscLogDouble dof) 1579 { 1580 PetscFunctionBegin; 1581 PetscCheck(!(n < 0) && !(n > 7), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Error index %" PetscInt_FMT " is not in [0, 8)", n); 1582 for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) { 1583 PetscLogHandler h = PetscLogHandlers[i].handler; 1584 1585 if (h) { 1586 PetscEventPerfInfo *event_info; 1587 1588 PetscCall(PetscLogHandlerGetEventPerfInfo(h, PETSC_DEFAULT, event, &event_info)); 1589 if (event_info) event_info->dof[n] = dof; 1590 } 1591 } 1592 PetscFunctionReturn(PETSC_SUCCESS); 1593 } 1594 1595 /*@C 1596 PetscLogEventSetError - Set the nth error associated with a numerical problem associated with this event 1597 1598 Not Collective 1599 1600 Input Parameters: 1601 + event - The event id to log 1602 . n - The error index, in [0, 8) 1603 - error - The error 1604 1605 Options Database Key: 1606 . -log_view - Activates log summary 1607 1608 Level: developer 1609 1610 Notes: 1611 This is to enable logging of convergence, and enable users to interpret the errors as they wish. For example, 1612 as different norms, or as errors for different fields 1613 1614 This is a low level routine used by the logging functions in PETSc 1615 1616 .seealso: `PetscLogEventSetDof()`, `PetscLogEventRegister()`, `PetscLogGetDefaultHandler()` 1617 @*/ 1618 PetscErrorCode PetscLogEventSetError(PetscLogEvent event, PetscInt n, PetscLogDouble error) 1619 { 1620 PetscFunctionBegin; 1621 PetscCheck(!(n < 0) && !(n > 7), PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Error index %" PetscInt_FMT " is not in [0, 8)", n); 1622 for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) { 1623 PetscLogHandler h = PetscLogHandlers[i].handler; 1624 1625 if (h) { 1626 PetscEventPerfInfo *event_info; 1627 1628 PetscCall(PetscLogHandlerGetEventPerfInfo(h, PETSC_DEFAULT, event, &event_info)); 1629 if (event_info) event_info->errors[n] = error; 1630 } 1631 } 1632 PetscFunctionReturn(PETSC_SUCCESS); 1633 } 1634 1635 /*@C 1636 PetscLogEventGetId - Returns the event id when given the event name. 1637 1638 Not Collective 1639 1640 Input Parameter: 1641 . name - The event name 1642 1643 Output Parameter: 1644 . event - The event, or -1 if no event with that name exists 1645 1646 Level: intermediate 1647 1648 .seealso: [](ch_profiling), `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogStageGetId()` 1649 @*/ 1650 PetscErrorCode PetscLogEventGetId(const char name[], PetscLogEvent *event) 1651 { 1652 PetscLogState state; 1653 1654 PetscFunctionBegin; 1655 *event = -1; 1656 PetscCall(PetscLogGetState(&state)); 1657 if (state) PetscCall(PetscLogStateGetEventFromName(state, name, event)); 1658 PetscFunctionReturn(PETSC_SUCCESS); 1659 } 1660 1661 /*@C 1662 PetscLogEventGetName - Returns the event name when given the event id. 1663 1664 Not Collective 1665 1666 Input Parameter: 1667 . event - The event 1668 1669 Output Parameter: 1670 . name - The event name 1671 1672 Level: intermediate 1673 1674 .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()` 1675 @*/ 1676 PetscErrorCode PetscLogEventGetName(PetscLogEvent event, const char **name) 1677 { 1678 PetscLogEventInfo event_info; 1679 PetscLogState state; 1680 1681 PetscFunctionBegin; 1682 *name = NULL; 1683 PetscCall(PetscLogGetState(&state)); 1684 if (!state) PetscFunctionReturn(PETSC_SUCCESS); 1685 PetscCall(PetscLogStateEventGetInfo(state, event, &event_info)); 1686 *name = event_info.name; 1687 PetscFunctionReturn(PETSC_SUCCESS); 1688 } 1689 1690 /*@ 1691 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. 1692 1693 Not collective 1694 1695 Level: advanced 1696 1697 Notes: 1698 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()`). 1699 1700 Other log handlers (such as the nested handler, `PetscLogNestedBegin()`) will ignore this function. 1701 1702 .seealso: [](ch_profiling), `PetscLogEventDeactivatePush()`, `PetscLogEventDeactivatePop()`, `PetscLogEventsResume()`, `PetscLogGetDefaultHandler()` 1703 @*/ 1704 PetscErrorCode PetscLogEventsPause(void) 1705 { 1706 PetscFunctionBegin; 1707 for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) { 1708 PetscLogHandler h = PetscLogHandlers[i].handler; 1709 1710 if (h) PetscCall(PetscLogHandlerEventsPause(h)); 1711 } 1712 PetscFunctionReturn(PETSC_SUCCESS); 1713 } 1714 1715 /*@ 1716 PetscLogEventsResume - Return logging to normal behavior after it was paused with `PetscLogEventsPause()`. 1717 1718 Not collective 1719 1720 Level: advanced 1721 1722 .seealso: [](ch_profiling), `PetscLogEventDeactivatePush()`, `PetscLogEventDeactivatePop()`, `PetscLogEventsPause()`, `PetscLogGetDefaultHandler()` 1723 @*/ 1724 PetscErrorCode PetscLogEventsResume(void) 1725 { 1726 PetscFunctionBegin; 1727 for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) { 1728 PetscLogHandler h = PetscLogHandlers[i].handler; 1729 1730 if (h) PetscCall(PetscLogHandlerEventsResume(h)); 1731 } 1732 PetscFunctionReturn(PETSC_SUCCESS); 1733 } 1734 1735 /*------------------------------------------------ Class Functions --------------------------------------------------*/ 1736 1737 /*MC 1738 PetscLogObjectCreate - Log the creation of a `PetscObject` 1739 1740 Synopsis: 1741 #include <petsclog.h> 1742 PetscErrorCode PetscLogObjectCreate(PetscObject h) 1743 1744 Not Collective 1745 1746 Input Parameters: 1747 . h - A `PetscObject` 1748 1749 Level: developer 1750 1751 Developer Note: 1752 Called internally by PETSc when creating objects: users do not need to call this directly. 1753 Notification of the object creation is sent to each `PetscLogHandler` that is running. 1754 1755 .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogObjectDestroy()` 1756 M*/ 1757 1758 /*MC 1759 PetscLogObjectDestroy - Logs the destruction of a `PetscObject` 1760 1761 Synopsis: 1762 #include <petsclog.h> 1763 PetscErrorCode PetscLogObjectDestroy(PetscObject h) 1764 1765 Not Collective 1766 1767 Input Parameters: 1768 . h - A `PetscObject` 1769 1770 Level: developer 1771 1772 Developer Note: 1773 Called internally by PETSc when destroying objects: users do not need to call this directly. 1774 Notification of the object creation is sent to each `PetscLogHandler` that is running. 1775 1776 .seealso: [](ch_profiling), `PetscLogHandler`, `PetscLogObjectCreate()` 1777 M*/ 1778 1779 /*@C 1780 PetscLogClassGetClassId - Returns the `PetscClassId` when given the class name. 1781 1782 Not Collective 1783 1784 Input Parameter: 1785 . name - The class name 1786 1787 Output Parameter: 1788 . classid - The `PetscClassId` id, or -1 if no class with that name exists 1789 1790 Level: intermediate 1791 1792 .seealso: [](ch_profiling), `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogStageGetId()` 1793 @*/ 1794 PetscErrorCode PetscLogClassGetClassId(const char name[], PetscClassId *classid) 1795 { 1796 PetscLogClass log_class; 1797 PetscLogClassInfo class_info; 1798 PetscLogState state; 1799 1800 PetscFunctionBegin; 1801 *classid = -1; 1802 PetscCall(PetscLogGetState(&state)); 1803 if (!state) PetscFunctionReturn(PETSC_SUCCESS); 1804 PetscCall(PetscLogStateGetClassFromName(state, name, &log_class)); 1805 if (log_class < 0) { 1806 *classid = -1; 1807 PetscFunctionReturn(PETSC_SUCCESS); 1808 } 1809 PetscCall(PetscLogStateClassGetInfo(state, log_class, &class_info)); 1810 *classid = class_info.classid; 1811 PetscFunctionReturn(PETSC_SUCCESS); 1812 } 1813 1814 /*@C 1815 PetscLogClassIdGetName - Returns a `PetscClassId`'s name. 1816 1817 Not Collective 1818 1819 Input Parameter: 1820 . classid - A `PetscClassId` 1821 1822 Output Parameter: 1823 . name - The class name 1824 1825 Level: intermediate 1826 1827 .seealso: [](ch_profiling), `PetscLogClassRegister()`, `PetscLogClassBegin()`, `PetscLogClassEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()`, `PetscPreLoadClass()` 1828 @*/ 1829 PetscErrorCode PetscLogClassIdGetName(PetscClassId classid, const char **name) 1830 { 1831 PetscLogClass log_class; 1832 PetscLogClassInfo class_info; 1833 PetscLogState state; 1834 1835 PetscFunctionBegin; 1836 PetscCall(PetscLogGetState(&state)); 1837 PetscCall(PetscLogStateGetClassFromClassId(state, classid, &log_class)); 1838 PetscCall(PetscLogStateClassGetInfo(state, log_class, &class_info)); 1839 *name = class_info.name; 1840 PetscFunctionReturn(PETSC_SUCCESS); 1841 } 1842 1843 /*------------------------------------------------ Output Functions -------------------------------------------------*/ 1844 /*@C 1845 PetscLogDump - Dumps logs of objects to a file. This file is intended to 1846 be read by bin/petscview. This program no longer exists. 1847 1848 Collective on `PETSC_COMM_WORLD` 1849 1850 Input Parameter: 1851 . sname - an optional file name 1852 1853 Example Usage: 1854 .vb 1855 PetscInitialize(...); 1856 PetscLogDefaultBegin(); 1857 // ... code ... 1858 PetscLogDump(filename); 1859 PetscFinalize(); 1860 .ve 1861 1862 Level: advanced 1863 1864 Note: 1865 The default file name is Log.<rank> where <rank> is the MPI process rank. If no name is specified, 1866 this file will be used. 1867 1868 .seealso: [](ch_profiling), `PetscLogDefaultBegin()`, `PetscLogView()`, `PetscLogGetDefaultHandler()` 1869 @*/ 1870 PetscErrorCode PetscLogDump(const char sname[]) 1871 { 1872 PetscLogHandler handler; 1873 1874 PetscFunctionBegin; 1875 PetscCall(PetscLogGetHandler(PETSCLOGHANDLERDEFAULT, &handler)); 1876 PetscCall(PetscLogHandlerDump(handler, sname)); 1877 PetscFunctionReturn(PETSC_SUCCESS); 1878 } 1879 1880 /*@C 1881 PetscLogMPEDump - Dumps the MPE logging info to file for later use with Jumpshot. 1882 1883 Collective over `PETSC_COMM_WORLD` 1884 1885 Input Parameter: 1886 . sname - filename for the MPE logfile 1887 1888 Level: advanced 1889 1890 .seealso: [](ch_profiling), `PetscLogDump()`, `PetscLogMPEBegin()` 1891 @*/ 1892 PetscErrorCode PetscLogMPEDump(const char sname[]) 1893 { 1894 PetscFunctionBegin; 1895 #if defined(PETSC_HAVE_MPE) 1896 if (PetscBeganMPE) { 1897 char name[PETSC_MAX_PATH_LEN]; 1898 1899 PetscCall(PetscInfo(0, "Finalizing MPE.\n")); 1900 if (sname) { 1901 PetscCall(PetscStrncpy(name, sname, sizeof(name))); 1902 } else { 1903 PetscCall(PetscGetProgramName(name, sizeof(name))); 1904 } 1905 PetscCall(MPE_Finish_log(name)); 1906 } else { 1907 PetscCall(PetscInfo(0, "Not finalizing MPE (not started by PETSc).\n")); 1908 } 1909 #else 1910 SETERRQ(PETSC_COMM_WORLD, PETSC_ERR_SUP_SYS, "PETSc was configured without MPE support, reconfigure with --with-mpe or --download-mpe"); 1911 #endif 1912 PetscFunctionReturn(PETSC_SUCCESS); 1913 } 1914 1915 /*@C 1916 PetscLogView - Prints a summary of the logging. 1917 1918 Collective over MPI_Comm 1919 1920 Input Parameter: 1921 . viewer - an ASCII viewer 1922 1923 Options Database Keys: 1924 + -log_view [:filename] - Prints summary of log information 1925 . -log_view :filename.py:ascii_info_detail - Saves logging information from each process as a Python file 1926 . -log_view :filename.xml:ascii_xml - Saves a summary of the logging information in a nested format (see below for how to view it) 1927 . -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) 1928 . -log_view_memory - Also display memory usage in each event 1929 . -log_view_gpu_time - Also display time in each event for GPU kernels (Note this may slow the computation) 1930 . -log_all - Saves a file Log.rank for each MPI rank with details of each step of the computation 1931 - -log_trace [filename] - Displays a trace of what each process is doing 1932 1933 Level: beginner 1934 1935 Notes: 1936 It is possible to control the logging programmatically but we recommend using the options database approach whenever possible 1937 By default the summary is printed to stdout. 1938 1939 Before calling this routine you must have called either PetscLogDefaultBegin() or PetscLogNestedBegin() 1940 1941 If PETSc is configured with --with-logging=0 then this functionality is not available 1942 1943 To view the nested XML format filename.xml first copy ${PETSC_DIR}/share/petsc/xml/performance_xml2html.xsl to the current 1944 directory then open filename.xml with your browser. Specific notes for certain browsers 1945 $ Firefox and Internet explorer - simply open the file 1946 $ Google Chrome - you must start up Chrome with the option --allow-file-access-from-files 1947 $ Safari - see https://ccm.net/faq/36342-safari-how-to-enable-local-file-access 1948 or one can use the package http://xmlsoft.org/XSLT/xsltproc2.html to translate the xml file to html and then open it with 1949 your browser. 1950 Alternatively, use the script ${PETSC_DIR}/lib/petsc/bin/petsc-performance-view to automatically open a new browser 1951 window and render the XML log file contents. 1952 1953 The nested XML format was kindly donated by Koos Huijssen and Christiaan M. Klaij MARITIME RESEARCH INSTITUTE NETHERLANDS 1954 1955 The Flame Graph output can be visualised using either the original Flame Graph script (https://github.com/brendangregg/FlameGraph) 1956 or using speedscope (https://www.speedscope.app). 1957 Old XML profiles may be converted into this format using the script ${PETSC_DIR}/lib/petsc/bin/xml2flamegraph.py. 1958 1959 .seealso: [](ch_profiling), `PetscLogDefaultBegin()`, `PetscLogDump()` 1960 @*/ 1961 PetscErrorCode PetscLogView(PetscViewer viewer) 1962 { 1963 PetscBool isascii; 1964 PetscViewerFormat format; 1965 int stage; 1966 PetscLogState state; 1967 PetscIntStack temp_stack; 1968 PetscLogHandler handler; 1969 PetscBool is_empty; 1970 1971 PetscFunctionBegin; 1972 PetscCall(PetscLogGetState(&state)); 1973 /* Pop off any stages the user forgot to remove */ 1974 PetscCall(PetscIntStackCreate(&temp_stack)); 1975 PetscCall(PetscLogStateGetCurrentStage(state, &stage)); 1976 while (stage >= 0) { 1977 PetscCall(PetscLogStagePop()); 1978 PetscCall(PetscIntStackPush(temp_stack, stage)); 1979 PetscCall(PetscLogStateGetCurrentStage(state, &stage)); 1980 } 1981 PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii)); 1982 PetscCheck(isascii, PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Currently can only view logging to ASCII"); 1983 PetscCall(PetscViewerGetFormat(viewer, &format)); 1984 if (format == PETSC_VIEWER_ASCII_XML || format == PETSC_VIEWER_ASCII_FLAMEGRAPH) { 1985 PetscCall(PetscLogGetHandler(PETSCLOGHANDLERNESTED, &handler)); 1986 PetscCall(PetscLogHandlerView(handler, viewer)); 1987 } else { 1988 PetscCall(PetscLogGetHandler(PETSCLOGHANDLERDEFAULT, &handler)); 1989 PetscCall(PetscLogHandlerView(handler, viewer)); 1990 } 1991 PetscCall(PetscIntStackEmpty(temp_stack, &is_empty)); 1992 while (!is_empty) { 1993 PetscCall(PetscIntStackPop(temp_stack, &stage)); 1994 PetscCall(PetscLogStagePush(stage)); 1995 PetscCall(PetscIntStackEmpty(temp_stack, &is_empty)); 1996 } 1997 PetscCall(PetscIntStackDestroy(temp_stack)); 1998 PetscFunctionReturn(PETSC_SUCCESS); 1999 } 2000 2001 /*@C 2002 PetscLogViewFromOptions - Processes command line options to determine if/how a `PetscLog` is to be viewed. 2003 2004 Collective on `PETSC_COMM_WORLD` 2005 2006 Level: developer 2007 2008 .seealso: [](ch_profiling), `PetscLogView()` 2009 @*/ 2010 PetscErrorCode PetscLogViewFromOptions(void) 2011 { 2012 PetscInt n_max = PETSC_LOG_VIEW_FROM_OPTIONS_MAX; 2013 PetscViewer viewers[PETSC_LOG_VIEW_FROM_OPTIONS_MAX]; 2014 PetscViewerFormat formats[PETSC_LOG_VIEW_FROM_OPTIONS_MAX]; 2015 PetscBool flg; 2016 2017 PetscFunctionBegin; 2018 PetscCall(PetscOptionsGetViewers(PETSC_COMM_WORLD, NULL, NULL, "-log_view", &n_max, viewers, formats, &flg)); 2019 for (PetscInt i = 0; i < n_max; i++) { 2020 PetscCall(PetscViewerPushFormat(viewers[i], formats[i])); 2021 PetscCall(PetscLogView(viewers[i])); 2022 PetscCall(PetscViewerPopFormat(viewers[i])); 2023 PetscCall(PetscViewerDestroy(&(viewers[i]))); 2024 } 2025 PetscFunctionReturn(PETSC_SUCCESS); 2026 } 2027 2028 PETSC_INTERN PetscErrorCode PetscLogHandlerNestedSetThreshold(PetscLogHandler, PetscLogDouble, PetscLogDouble *); 2029 2030 /*@ 2031 PetscLogSetThreshold - Set the threshold time for logging the events; this is a percentage out of 100, so 1. means any event 2032 that takes 1 or more percent of the time. 2033 2034 Logically Collective over `PETSC_COMM_WORLD` 2035 2036 Input Parameter: 2037 . newThresh - the threshold to use 2038 2039 Output Parameter: 2040 . oldThresh - the previously set threshold value 2041 2042 Options Database Keys: 2043 . -log_view :filename.xml:ascii_xml - Prints an XML summary of flop and timing information to the file 2044 2045 Example Usage: 2046 .vb 2047 PetscInitialize(...); 2048 PetscLogNestedBegin(); 2049 PetscLogSetThreshold(0.1,&oldthresh); 2050 // ... code ... 2051 PetscLogView(viewer); 2052 PetscFinalize(); 2053 .ve 2054 2055 Level: advanced 2056 2057 Note: 2058 This threshold is only used by the nested log handler 2059 2060 .seealso: `PetscLogDump()`, `PetscLogView()`, `PetscLogTraceBegin()`, `PetscLogDefaultBegin()`, 2061 `PetscLogNestedBegin()` 2062 @*/ 2063 PetscErrorCode PetscLogSetThreshold(PetscLogDouble newThresh, PetscLogDouble *oldThresh) 2064 { 2065 PetscLogHandler handler; 2066 2067 PetscFunctionBegin; 2068 PetscCall(PetscLogTryGetHandler(PETSCLOGHANDLERNESTED, &handler)); 2069 PetscCall(PetscLogHandlerNestedSetThreshold(handler, newThresh, oldThresh)); 2070 PetscFunctionReturn(PETSC_SUCCESS); 2071 } 2072 2073 /*----------------------------------------------- Counter Functions -------------------------------------------------*/ 2074 /*@C 2075 PetscGetFlops - Returns the number of flops used on this processor 2076 since the program began. 2077 2078 Not Collective 2079 2080 Output Parameter: 2081 . flops - number of floating point operations 2082 2083 Level: intermediate 2084 2085 Notes: 2086 A global counter logs all PETSc flop counts. The user can use 2087 `PetscLogFlops()` to increment this counter to include flops for the 2088 application code. 2089 2090 A separate counter `PetscLogGPUFlops()` logs the flops that occur on any GPU associated with this MPI rank 2091 2092 .seealso: [](ch_profiling), `PetscLogGPUFlops()`, `PetscTime()`, `PetscLogFlops()` 2093 @*/ 2094 PetscErrorCode PetscGetFlops(PetscLogDouble *flops) 2095 { 2096 PetscFunctionBegin; 2097 *flops = petsc_TotalFlops; 2098 PetscFunctionReturn(PETSC_SUCCESS); 2099 } 2100 2101 /*@C 2102 PetscLogObjectState - Record information about an object with the default log handler 2103 2104 Not Collective 2105 2106 Input Parameters: 2107 + obj - the `PetscObject` 2108 . format - a printf-style format string 2109 - ... - printf arguments to format 2110 2111 Level: developer 2112 2113 .seealso: [](ch_profiling), `PetscLogObjectCreate()`, `PetscLogObjectDestroy()`, `PetscLogGetDefaultHandler()` 2114 @*/ 2115 PetscErrorCode PetscLogObjectState(PetscObject obj, const char format[], ...) 2116 { 2117 PetscFunctionBegin; 2118 for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) { 2119 PetscLogHandler h = PetscLogHandlers[i].handler; 2120 2121 if (h) { 2122 va_list Argp; 2123 va_start(Argp, format); 2124 PetscCall(PetscLogHandlerLogObjectState_Internal(h, obj, format, Argp)); 2125 va_end(Argp); 2126 } 2127 } 2128 PetscFunctionReturn(PETSC_SUCCESS); 2129 } 2130 2131 /*MC 2132 PetscLogFlops - Adds floating point operations to the global counter. 2133 2134 Synopsis: 2135 #include <petsclog.h> 2136 PetscErrorCode PetscLogFlops(PetscLogDouble f) 2137 2138 Not Collective 2139 2140 Input Parameter: 2141 . f - flop counter 2142 2143 Example Usage: 2144 .vb 2145 PetscLogEvent USER_EVENT; 2146 2147 PetscLogEventRegister("User event", 0, &USER_EVENT); 2148 PetscLogEventBegin(USER_EVENT, 0, 0, 0, 0); 2149 [code segment to monitor] 2150 PetscLogFlops(user_flops) 2151 PetscLogEventEnd(USER_EVENT, 0, 0, 0, 0); 2152 .ve 2153 2154 Level: intermediate 2155 2156 Note: 2157 A global counter logs all PETSc flop counts. The user can use PetscLogFlops() to increment 2158 this counter to include flops for the application code. 2159 2160 .seealso: [](ch_profiling), `PetscLogGPUFlops()`, `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscGetFlops()` 2161 M*/ 2162 2163 /*MC 2164 PetscPreLoadBegin - Begin a segment of code that may be preloaded (run twice) to get accurate 2165 timings 2166 2167 Synopsis: 2168 #include <petsclog.h> 2169 void PetscPreLoadBegin(PetscBool flag, char *name); 2170 2171 Not Collective 2172 2173 Input Parameters: 2174 + flag - `PETSC_TRUE` to run twice, `PETSC_FALSE` to run once, may be overridden with command 2175 line option `-preload true|false` 2176 - name - name of first stage (lines of code timed separately with `-log_view`) to be preloaded 2177 2178 Example Usage: 2179 .vb 2180 PetscPreLoadBegin(PETSC_TRUE, "first stage"); 2181 // lines of code 2182 PetscPreLoadStage("second stage"); 2183 // lines of code 2184 PetscPreLoadEnd(); 2185 .ve 2186 2187 Level: intermediate 2188 2189 Note: 2190 Only works in C/C++, not Fortran 2191 2192 Flags available within the macro\: 2193 + PetscPreLoadingUsed - `PETSC_TRUE` if we are or have done preloading 2194 . PetscPreLoadingOn - `PETSC_TRUE` if it is CURRENTLY doing preload 2195 . PetscPreLoadIt - `0` for the first computation (with preloading turned off it is only 2196 `0`) `1` for the second 2197 - PetscPreLoadMax - number of times it will do the computation, only one when preloading is 2198 turned on 2199 2200 The first two variables are available throughout the program, the second two only between the 2201 `PetscPreLoadBegin()` and `PetscPreLoadEnd()` 2202 2203 .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadEnd()`, `PetscPreLoadStage()` 2204 M*/ 2205 2206 /*MC 2207 PetscPreLoadEnd - End a segment of code that may be preloaded (run twice) to get accurate 2208 timings 2209 2210 Synopsis: 2211 #include <petsclog.h> 2212 void PetscPreLoadEnd(void); 2213 2214 Not Collective 2215 2216 Example Usage: 2217 .vb 2218 PetscPreLoadBegin(PETSC_TRUE, "first stage"); 2219 // lines of code 2220 PetscPreLoadStage("second stage"); 2221 // lines of code 2222 PetscPreLoadEnd(); 2223 .ve 2224 2225 Level: intermediate 2226 2227 Note: 2228 Only works in C/C++ not fortran 2229 2230 .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadStage()` 2231 M*/ 2232 2233 /*MC 2234 PetscPreLoadStage - Start a new segment of code to be timed separately to get accurate timings 2235 2236 Synopsis: 2237 #include <petsclog.h> 2238 void PetscPreLoadStage(char *name); 2239 2240 Not Collective 2241 2242 Example Usage: 2243 .vb 2244 PetscPreLoadBegin(PETSC_TRUE,"first stage"); 2245 // lines of code 2246 PetscPreLoadStage("second stage"); 2247 // lines of code 2248 PetscPreLoadEnd(); 2249 .ve 2250 2251 Level: intermediate 2252 2253 Note: 2254 Only works in C/C++ not fortran 2255 2256 .seealso: [](ch_profiling), `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscPreLoadBegin()`, `PetscPreLoadEnd()` 2257 M*/ 2258 2259 #if PetscDefined(HAVE_DEVICE) 2260 #include <petsc/private/deviceimpl.h> 2261 2262 /* 2263 This cannot be called by users between PetscInitialize() and PetscFinalize() at any random location in the code 2264 because it will result in timing results that cannot be interpreted. 2265 */ 2266 static PetscErrorCode PetscLogGpuTime_Off(void) 2267 { 2268 PetscLogGpuTimeFlag = PETSC_FALSE; 2269 return PETSC_SUCCESS; 2270 } 2271 2272 /*@C 2273 PetscLogGpuTime - turn on the logging of GPU time for GPU kernels 2274 2275 Options Database Key: 2276 . -log_view_gpu_time - provide the GPU times in the `-log_view` output 2277 2278 Level: advanced 2279 2280 Notes: 2281 Turning on the timing of the GPU kernels can slow down the entire computation and should only 2282 be used when studying the performance of operations on GPU such as vector operations and 2283 matrix-vector operations. 2284 2285 This routine should only be called once near the beginning of the program. Once it is started 2286 it cannot be turned off. 2287 2288 .seealso: [](ch_profiling), `PetscLogView()`, `PetscLogGpuFlops()`, `PetscLogGpuTimeEnd()`, `PetscLogGpuTimeBegin()` 2289 @*/ 2290 PetscErrorCode PetscLogGpuTime(void) 2291 { 2292 if (!PetscLogGpuTimeFlag) PetscCall(PetscRegisterFinalize(PetscLogGpuTime_Off)); 2293 PetscLogGpuTimeFlag = PETSC_TRUE; 2294 return PETSC_SUCCESS; 2295 } 2296 2297 /*@C 2298 PetscLogGpuTimeBegin - Start timer for device 2299 2300 Level: intermediate 2301 2302 Notes: 2303 When CUDA or HIP is enabled, the timer is run on the GPU, it is a separate logging of time 2304 devoted to GPU computations (excluding kernel launch times). 2305 2306 When CUDA or HIP is not available, the timer is run on the CPU, it is a separate logging of 2307 time devoted to GPU computations (including kernel launch times). 2308 2309 There is no need to call WaitForCUDA() or WaitForHIP() between `PetscLogGpuTimeBegin()` and 2310 `PetscLogGpuTimeEnd()` 2311 2312 This timer should NOT include times for data transfers between the GPU and CPU, nor setup 2313 actions such as allocating space. 2314 2315 The regular logging captures the time for data transfers and any CPU activities during the 2316 event. It is used to compute the flop rate on the GPU as it is actively engaged in running a 2317 kernel. 2318 2319 Developer Notes: 2320 The GPU event timer captures the execution time of all the kernels launched in the default 2321 stream by the CPU between `PetscLogGpuTimeBegin()` and `PetsLogGpuTimeEnd()`. 2322 2323 `PetscLogGpuTimeBegin()` and `PetsLogGpuTimeEnd()` insert the begin and end events into the 2324 default stream (stream 0). The device will record a time stamp for the event when it reaches 2325 that event in the stream. The function xxxEventSynchronize() is called in 2326 `PetsLogGpuTimeEnd()` to block CPU execution, but not continued GPU execution, until the 2327 timer event is recorded. 2328 2329 .seealso: [](ch_profiling), `PetscLogView()`, `PetscLogGpuFlops()`, `PetscLogGpuTimeEnd()`, `PetscLogGpuTime()` 2330 @*/ 2331 PetscErrorCode PetscLogGpuTimeBegin(void) 2332 { 2333 PetscBool isActive; 2334 2335 PetscFunctionBegin; 2336 PetscCall(PetscLogEventBeginIsActive(&isActive)); 2337 if (!isActive || !PetscLogGpuTimeFlag) PetscFunctionReturn(PETSC_SUCCESS); 2338 if (PetscDefined(HAVE_DEVICE)) { 2339 PetscDeviceContext dctx; 2340 2341 PetscCall(PetscDeviceContextGetCurrentContext(&dctx)); 2342 PetscCall(PetscDeviceContextBeginTimer_Internal(dctx)); 2343 } else { 2344 PetscCall(PetscTimeSubtract(&petsc_gtime)); 2345 } 2346 PetscFunctionReturn(PETSC_SUCCESS); 2347 } 2348 2349 /*@C 2350 PetscLogGpuTimeEnd - Stop timer for device 2351 2352 Level: intermediate 2353 2354 .seealso: [](ch_profiling), `PetscLogView()`, `PetscLogGpuFlops()`, `PetscLogGpuTimeBegin()` 2355 @*/ 2356 PetscErrorCode PetscLogGpuTimeEnd(void) 2357 { 2358 PetscBool isActive; 2359 2360 PetscFunctionBegin; 2361 PetscCall(PetscLogEventEndIsActive(&isActive)); 2362 if (!isActive || !PetscLogGpuTimeFlag) PetscFunctionReturn(PETSC_SUCCESS); 2363 if (PetscDefined(HAVE_DEVICE)) { 2364 PetscDeviceContext dctx; 2365 PetscLogDouble elapsed; 2366 2367 PetscCall(PetscDeviceContextGetCurrentContext(&dctx)); 2368 PetscCall(PetscDeviceContextEndTimer_Internal(dctx, &elapsed)); 2369 petsc_gtime += (elapsed / 1000.0); 2370 } else { 2371 PetscCall(PetscTimeAdd(&petsc_gtime)); 2372 } 2373 PetscFunctionReturn(PETSC_SUCCESS); 2374 } 2375 2376 #endif /* end of PETSC_HAVE_DEVICE */ 2377 2378 #endif /* PETSC_USE_LOG*/ 2379 2380 /* -- Utility functions for logging from fortran -- */ 2381 2382 PETSC_EXTERN PetscErrorCode PetscASend(int count, int datatype) 2383 { 2384 PetscFunctionBegin; 2385 #if PetscDefined(USE_LOG) 2386 PetscCall(PetscAddLogDouble(&petsc_send_ct, &petsc_send_ct_th, 1)); 2387 #if !defined(MPIUNI_H) && !defined(PETSC_HAVE_BROKEN_RECURSIVE_MACRO) 2388 PetscCall(PetscMPITypeSize(count, MPI_Type_f2c((MPI_Fint)datatype), &petsc_send_len, &petsc_send_len_th)); 2389 #endif 2390 #endif 2391 PetscFunctionReturn(PETSC_SUCCESS); 2392 } 2393 2394 PETSC_EXTERN PetscErrorCode PetscARecv(int count, int datatype) 2395 { 2396 PetscFunctionBegin; 2397 #if PetscDefined(USE_LOG) 2398 PetscCall(PetscAddLogDouble(&petsc_recv_ct, &petsc_recv_ct_th, 1)); 2399 #if !defined(MPIUNI_H) && !defined(PETSC_HAVE_BROKEN_RECURSIVE_MACRO) 2400 PetscCall(PetscMPITypeSize(count, MPI_Type_f2c((MPI_Fint)datatype), &petsc_recv_len, &petsc_recv_len_th)); 2401 #endif 2402 #endif 2403 PetscFunctionReturn(PETSC_SUCCESS); 2404 } 2405 2406 PETSC_EXTERN PetscErrorCode PetscAReduce(void) 2407 { 2408 PetscFunctionBegin; 2409 if (PetscDefined(USE_LOG)) PetscCall(PetscAddLogDouble(&petsc_allreduce_ct, &petsc_allreduce_ct_th, 1)); 2410 PetscFunctionReturn(PETSC_SUCCESS); 2411 } 2412 2413 PetscClassId PETSC_LARGEST_CLASSID = PETSC_SMALLEST_CLASSID; 2414 PetscClassId PETSC_OBJECT_CLASSID = 0; 2415 2416 static PetscBool PetscLogInitializeCalled = PETSC_FALSE; 2417 2418 PETSC_INTERN PetscErrorCode PetscLogInitialize(void) 2419 { 2420 int stage; 2421 2422 PetscFunctionBegin; 2423 if (PetscLogInitializeCalled) PetscFunctionReturn(PETSC_SUCCESS); 2424 PetscLogInitializeCalled = PETSC_TRUE; 2425 if (PetscDefined(USE_LOG)) { 2426 /* Setup default logging structures */ 2427 PetscCall(PetscLogStateCreate(&petsc_log_state)); 2428 for (PetscInt i = 0; i < PETSC_LOG_HANDLER_MAX; i++) { 2429 if (PetscLogHandlers[i].handler) PetscCall(PetscLogHandlerSetState(PetscLogHandlers[i].handler, petsc_log_state)); 2430 } 2431 PetscCall(PetscLogStateStageRegister(petsc_log_state, "Main Stage", &stage)); 2432 PetscCall(PetscSpinlockCreate(&PetscLogSpinLock)); 2433 #if defined(PETSC_HAVE_THREADSAFETY) 2434 petsc_log_tid = 0; 2435 petsc_log_gid = 0; 2436 #endif 2437 2438 /* All processors sync here for more consistent logging */ 2439 PetscCallMPI(MPI_Barrier(PETSC_COMM_WORLD)); 2440 PetscCall(PetscTime(&petsc_BaseTime)); 2441 PetscCall(PetscLogStagePush(stage)); 2442 } 2443 PetscFunctionReturn(PETSC_SUCCESS); 2444 } 2445 2446 PETSC_INTERN PetscErrorCode PetscLogFinalize(void) 2447 { 2448 PetscFunctionBegin; 2449 if (PetscDefined(USE_LOG)) { 2450 /* Resetting phase */ 2451 // pop remaining stages 2452 if (petsc_log_state) { 2453 while (petsc_log_state->current_stage >= 0) { PetscCall(PetscLogStagePop()); } 2454 } 2455 for (int i = 0; i < PETSC_LOG_HANDLER_MAX; i++) PetscCall(PetscLogHandlerDestroy(&PetscLogHandlers[i].handler)); 2456 PetscCall(PetscArrayzero(PetscLogHandlers, PETSC_LOG_HANDLER_MAX)); 2457 PetscCall(PetscLogStateDestroy(&petsc_log_state)); 2458 2459 petsc_TotalFlops = 0.0; 2460 petsc_BaseTime = 0.0; 2461 petsc_TotalFlops = 0.0; 2462 petsc_send_ct = 0.0; 2463 petsc_recv_ct = 0.0; 2464 petsc_send_len = 0.0; 2465 petsc_recv_len = 0.0; 2466 petsc_isend_ct = 0.0; 2467 petsc_irecv_ct = 0.0; 2468 petsc_isend_len = 0.0; 2469 petsc_irecv_len = 0.0; 2470 petsc_wait_ct = 0.0; 2471 petsc_wait_any_ct = 0.0; 2472 petsc_wait_all_ct = 0.0; 2473 petsc_sum_of_waits_ct = 0.0; 2474 petsc_allreduce_ct = 0.0; 2475 petsc_gather_ct = 0.0; 2476 petsc_scatter_ct = 0.0; 2477 petsc_TotalFlops_th = 0.0; 2478 petsc_send_ct_th = 0.0; 2479 petsc_recv_ct_th = 0.0; 2480 petsc_send_len_th = 0.0; 2481 petsc_recv_len_th = 0.0; 2482 petsc_isend_ct_th = 0.0; 2483 petsc_irecv_ct_th = 0.0; 2484 petsc_isend_len_th = 0.0; 2485 petsc_irecv_len_th = 0.0; 2486 petsc_wait_ct_th = 0.0; 2487 petsc_wait_any_ct_th = 0.0; 2488 petsc_wait_all_ct_th = 0.0; 2489 petsc_sum_of_waits_ct_th = 0.0; 2490 petsc_allreduce_ct_th = 0.0; 2491 petsc_gather_ct_th = 0.0; 2492 petsc_scatter_ct_th = 0.0; 2493 2494 petsc_ctog_ct = 0.0; 2495 petsc_gtoc_ct = 0.0; 2496 petsc_ctog_sz = 0.0; 2497 petsc_gtoc_sz = 0.0; 2498 petsc_gflops = 0.0; 2499 petsc_gtime = 0.0; 2500 petsc_ctog_ct_th = 0.0; 2501 petsc_gtoc_ct_th = 0.0; 2502 petsc_ctog_sz_th = 0.0; 2503 petsc_gtoc_sz_th = 0.0; 2504 petsc_gflops_th = 0.0; 2505 petsc_gtime_th = 0.0; 2506 } 2507 PETSC_LARGEST_CLASSID = PETSC_SMALLEST_CLASSID; 2508 PETSC_OBJECT_CLASSID = 0; 2509 PetscLogInitializeCalled = PETSC_FALSE; 2510 PetscFunctionReturn(PETSC_SUCCESS); 2511 } 2512 2513 /*@C 2514 PetscClassIdRegister - Registers a new class name for objects and logging operations in an application code. 2515 2516 Not Collective 2517 2518 Input Parameter: 2519 . name - The class name 2520 2521 Output Parameter: 2522 . oclass - The class id or classid 2523 2524 Level: developer 2525 2526 .seealso: [](ch_profiling), `PetscLogEventRegister()` 2527 @*/ 2528 PetscErrorCode PetscClassIdRegister(const char name[], PetscClassId *oclass) 2529 { 2530 PetscFunctionBegin; 2531 *oclass = ++PETSC_LARGEST_CLASSID; 2532 #if defined(PETSC_USE_LOG) 2533 { 2534 PetscLogState state; 2535 PetscLogClass logclass; 2536 2537 PetscCall(PetscLogGetState(&state)); 2538 if (state) PetscCall(PetscLogStateClassRegister(state, name, *oclass, &logclass)); 2539 } 2540 #endif 2541 PetscFunctionReturn(PETSC_SUCCESS); 2542 } 2543