Actual source code: plog.c


  2: /*
  3:       PETSc code to log object creation and destruction and PETSc events.

  5:       This provides the public API used by the rest of PETSc and by users.

  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.

 10: */
 11: #include <petsc/private/logimpl.h>
 12: #include <petsctime.h>
 13: #include <petscviewer.h>

 15: PetscErrorCode PetscLogObjectParent(PetscObject p,PetscObject c)
 16: {
 17:   if (!c || !p) return 0;
 18:   c->parent   = p;
 19:   c->parentid = p->id;
 20:   return 0;
 21: }

 23: /*@C
 24:    PetscLogObjectMemory - Adds to an object a count of additional amount of memory that is used by the object.

 26:    Not collective.

 28:    Input Parameters:
 29: +  obj  - the PETSc object
 30: -  mem  - the amount of memory that is being added to the object

 32:    Level: developer

 34:    Developer Notes:
 35:     Currently we do not always do a good job of associating all memory allocations with an object.

 37: .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscInitializeNoArguments()

 39: @*/
 40: PetscErrorCode PetscLogObjectMemory(PetscObject p,PetscLogDouble m)
 41: {
 42:   if (!p) return 0;
 43:   p->mem += m;
 44:   return 0;
 45: }

 47: PetscLogEvent PETSC_LARGEST_EVENT = PETSC_EVENT;

 49: #if defined(PETSC_USE_LOG)
 50: #include <petscmachineinfo.h>
 51: #include <petscconfiginfo.h>

 53: /* used in the MPI_XXX() count macros in petsclog.h */

 55: /* Action and object logging variables */
 56: Action    *petsc_actions            = NULL;
 57: Object    *petsc_objects            = NULL;
 58: PetscBool petsc_logActions          = PETSC_FALSE;
 59: PetscBool petsc_logObjects          = PETSC_FALSE;
 60: int       petsc_numActions          = 0, petsc_maxActions = 100;
 61: int       petsc_numObjects          = 0, petsc_maxObjects = 100;
 62: int       petsc_numObjectsDestroyed = 0;

 64: /* Global counters */
 65: PetscLogDouble petsc_BaseTime        = 0.0;
 66: PetscLogDouble petsc_TotalFlops      = 0.0;  /* The number of flops */
 67: PetscLogDouble petsc_tmp_flops       = 0.0;  /* The incremental number of flops */
 68: PetscLogDouble petsc_send_ct         = 0.0;  /* The number of sends */
 69: PetscLogDouble petsc_recv_ct         = 0.0;  /* The number of receives */
 70: PetscLogDouble petsc_send_len        = 0.0;  /* The total length of all sent messages */
 71: PetscLogDouble petsc_recv_len        = 0.0;  /* The total length of all received messages */
 72: PetscLogDouble petsc_isend_ct        = 0.0;  /* The number of immediate sends */
 73: PetscLogDouble petsc_irecv_ct        = 0.0;  /* The number of immediate receives */
 74: PetscLogDouble petsc_isend_len       = 0.0;  /* The total length of all immediate send messages */
 75: PetscLogDouble petsc_irecv_len       = 0.0;  /* The total length of all immediate receive messages */
 76: PetscLogDouble petsc_wait_ct         = 0.0;  /* The number of waits */
 77: PetscLogDouble petsc_wait_any_ct     = 0.0;  /* The number of anywaits */
 78: PetscLogDouble petsc_wait_all_ct     = 0.0;  /* The number of waitalls */
 79: PetscLogDouble petsc_sum_of_waits_ct = 0.0;  /* The total number of waits */
 80: PetscLogDouble petsc_allreduce_ct    = 0.0;  /* The number of reductions */
 81: PetscLogDouble petsc_gather_ct       = 0.0;  /* The number of gathers and gathervs */
 82: PetscLogDouble petsc_scatter_ct      = 0.0;  /* The number of scatters and scattervs */
 83: #if defined(PETSC_HAVE_DEVICE)
 84: PetscLogDouble petsc_ctog_ct         = 0.0;  /* The total number of CPU to GPU copies */
 85: PetscLogDouble petsc_gtoc_ct         = 0.0;  /* The total number of GPU to CPU copies */
 86: PetscLogDouble petsc_ctog_sz         = 0.0;  /* The total size of CPU to GPU copies */
 87: PetscLogDouble petsc_gtoc_sz         = 0.0;  /* The total size of GPU to CPU copies */
 88: PetscLogDouble petsc_ctog_ct_scalar  = 0.0;  /* The total number of CPU to GPU copies */
 89: PetscLogDouble petsc_gtoc_ct_scalar  = 0.0;  /* The total number of GPU to CPU copies */
 90: PetscLogDouble petsc_ctog_sz_scalar  = 0.0;  /* The total size of CPU to GPU copies */
 91: PetscLogDouble petsc_gtoc_sz_scalar  = 0.0;  /* The total size of GPU to CPU copies */
 92: PetscLogDouble petsc_gflops          = 0.0;  /* The flops done on a GPU */
 93: PetscLogDouble petsc_gtime           = 0.0;  /* The time spent on a GPU */
 94: #endif

 96: /* Logging functions */
 97: PetscErrorCode (*PetscLogPHC)(PetscObject) = NULL;
 98: PetscErrorCode (*PetscLogPHD)(PetscObject) = NULL;
 99: PetscErrorCode (*PetscLogPLB)(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject) = NULL;
100: PetscErrorCode (*PetscLogPLE)(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject) = NULL;

102: /* Tracing event logging variables */
103: FILE             *petsc_tracefile            = NULL;
104: int              petsc_tracelevel            = 0;
105: const char       *petsc_traceblanks          = "                                                                                                    ";
106: char             petsc_tracespace[128]       = " ";
107: PetscLogDouble   petsc_tracetime             = 0.0;
108: static PetscBool PetscLogInitializeCalled = PETSC_FALSE;

110: PETSC_INTERN PetscErrorCode PetscLogInitialize(void)
111: {
112:   int            stage;
113:   PetscBool      opt;

115:   if (PetscLogInitializeCalled) return 0;
116:   PetscLogInitializeCalled = PETSC_TRUE;

118:   PetscOptionsHasName(NULL,NULL, "-log_exclude_actions", &opt);
119:   if (opt) petsc_logActions = PETSC_FALSE;
120:   PetscOptionsHasName(NULL,NULL, "-log_exclude_objects", &opt);
121:   if (opt) petsc_logObjects = PETSC_FALSE;
122:   if (petsc_logActions) {
123:     PetscMalloc1(petsc_maxActions, &petsc_actions);
124:   }
125:   if (petsc_logObjects) {
126:     PetscMalloc1(petsc_maxObjects, &petsc_objects);
127:   }
128:   PetscLogPHC = PetscLogObjCreateDefault;
129:   PetscLogPHD = PetscLogObjDestroyDefault;
130:   /* Setup default logging structures */
131:   PetscStageLogCreate(&petsc_stageLog);
132:   PetscStageLogRegister(petsc_stageLog, "Main Stage", &stage);

134:   /* All processors sync here for more consistent logging */
135:   MPI_Barrier(PETSC_COMM_WORLD);
136:   PetscTime(&petsc_BaseTime);
137:   PetscLogStagePush(stage);
138:   return 0;
139: }

141: PETSC_INTERN PetscErrorCode PetscLogFinalize(void)
142: {
143:   PetscStageLog  stageLog;

145:   PetscFree(petsc_actions);
146:   PetscFree(petsc_objects);
147:   PetscLogNestedEnd();
148:   PetscLogSet(NULL, NULL);

150:   /* Resetting phase */
151:   PetscLogGetStageLog(&stageLog);
152:   PetscStageLogDestroy(stageLog);

154:   petsc_TotalFlops            = 0.0;
155:   petsc_numActions            = 0;
156:   petsc_numObjects            = 0;
157:   petsc_numObjectsDestroyed   = 0;
158:   petsc_maxActions            = 100;
159:   petsc_maxObjects            = 100;
160:   petsc_actions               = NULL;
161:   petsc_objects               = NULL;
162:   petsc_logActions            = PETSC_FALSE;
163:   petsc_logObjects            = PETSC_FALSE;
164:   petsc_BaseTime              = 0.0;
165:   petsc_TotalFlops            = 0.0;
166:   petsc_tmp_flops             = 0.0;
167:   petsc_send_ct               = 0.0;
168:   petsc_recv_ct               = 0.0;
169:   petsc_send_len              = 0.0;
170:   petsc_recv_len              = 0.0;
171:   petsc_isend_ct              = 0.0;
172:   petsc_irecv_ct              = 0.0;
173:   petsc_isend_len             = 0.0;
174:   petsc_irecv_len             = 0.0;
175:   petsc_wait_ct               = 0.0;
176:   petsc_wait_any_ct           = 0.0;
177:   petsc_wait_all_ct           = 0.0;
178:   petsc_sum_of_waits_ct       = 0.0;
179:   petsc_allreduce_ct          = 0.0;
180:   petsc_gather_ct             = 0.0;
181:   petsc_scatter_ct            = 0.0;
182:   #if defined(PETSC_HAVE_DEVICE)
183:   petsc_ctog_ct               = 0.0;
184:   petsc_gtoc_ct               = 0.0;
185:   petsc_ctog_sz               = 0.0;
186:   petsc_gtoc_sz               = 0.0;
187:   petsc_gflops                = 0.0;
188:   petsc_gtime                 = 0.0;
189:   #endif
190:   PETSC_LARGEST_EVENT         = PETSC_EVENT;
191:   PetscLogPHC                 = NULL;
192:   PetscLogPHD                 = NULL;
193:   petsc_tracefile             = NULL;
194:   petsc_tracelevel            = 0;
195:   petsc_traceblanks           = "                                                                                                    ";
196:   petsc_tracespace[0]         = ' '; petsc_tracespace[1] = 0;
197:   petsc_tracetime             = 0.0;
198:   PETSC_LARGEST_CLASSID       = PETSC_SMALLEST_CLASSID;
199:   PETSC_OBJECT_CLASSID        = 0;
200:   petsc_stageLog              = NULL;
201:   PetscLogInitializeCalled    = PETSC_FALSE;
202:   return 0;
203: }

205: /*@C
206:   PetscLogSet - Sets the logging functions called at the beginning and ending of every event.

208:   Not Collective

210:   Input Parameters:
211: + b - The function called at beginning of event
212: - e - The function called at end of event

214:   Level: developer

216: .seealso: PetscLogDump(), PetscLogDefaultBegin(), PetscLogAllBegin(), PetscLogTraceBegin()
217: @*/
218: PetscErrorCode  PetscLogSet(PetscErrorCode (*b)(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject),
219:                             PetscErrorCode (*e)(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject))
220: {
221:   PetscLogPLB = b;
222:   PetscLogPLE = e;
223:   return 0;
224: }

226: /*@C
227:   PetscLogIsActive - Check if logging is currently in progress.

229:   Not Collective

231:   Output Parameter:
232: . isActive - PETSC_TRUE if logging is in progress, PETSC_FALSE otherwise

234:   Level: beginner

236: .seealso: PetscLogDefaultBegin(), PetscLogAllBegin(), PetscLogSet()
237: @*/
238: PetscErrorCode PetscLogIsActive(PetscBool *isActive)
239: {
240:   *isActive = (PetscLogPLB && PetscLogPLE) ? PETSC_TRUE : PETSC_FALSE;
241:   return 0;
242: }

244: /*@C
245:   PetscLogDefaultBegin - Turns on logging of objects and events. This logs flop
246:   rates and object creation and should not slow programs down too much.
247:   This routine may be called more than once.

249:   Logically Collective over PETSC_COMM_WORLD

251:   Options Database Keys:
252: . -log_view [viewertype:filename:viewerformat] - Prints summary of flop and timing information to the
253:                   screen (for code configured with --with-log=1 (which is the default))

255:   Usage:
256: .vb
257:       PetscInitialize(...);
258:       PetscLogDefaultBegin();
259:        ... code ...
260:       PetscLogView(viewer); or PetscLogDump();
261:       PetscFinalize();
262: .ve

264:   Notes:
265:   PetscLogView(viewer) or PetscLogDump() actually cause the printing of
266:   the logging information.

268:   Level: advanced

270: .seealso: PetscLogDump(), PetscLogAllBegin(), PetscLogView(), PetscLogTraceBegin()
271: @*/
272: PetscErrorCode  PetscLogDefaultBegin(void)
273: {
274:   PetscLogSet(PetscLogEventBeginDefault, PetscLogEventEndDefault);
275:   return 0;
276: }

278: /*@C
279:   PetscLogAllBegin - Turns on extensive logging of objects and events. Logs
280:   all events. This creates large log files and slows the program down.

282:   Logically Collective on PETSC_COMM_WORLD

284:   Options Database Keys:
285: . -log_all - Prints extensive log information

287:   Usage:
288: .vb
289:      PetscInitialize(...);
290:      PetscLogAllBegin();
291:      ... code ...
292:      PetscLogDump(filename);
293:      PetscFinalize();
294: .ve

296:   Notes:
297:   A related routine is PetscLogDefaultBegin() (with the options key -log), which is
298:   intended for production runs since it logs only flop rates and object
299:   creation (and shouldn't significantly slow the programs).

301:   Level: advanced

303: .seealso: PetscLogDump(), PetscLogDefaultBegin(), PetscLogTraceBegin()
304: @*/
305: PetscErrorCode  PetscLogAllBegin(void)
306: {
307:   PetscLogSet(PetscLogEventBeginComplete, PetscLogEventEndComplete);
308:   return 0;
309: }

311: /*@C
312:   PetscLogTraceBegin - Activates trace logging.  Every time a PETSc event
313:   begins or ends, the event name is printed.

315:   Logically Collective on PETSC_COMM_WORLD

317:   Input Parameter:
318: . file - The file to print trace in (e.g. stdout)

320:   Options Database Key:
321: . -log_trace [filename] - Activates PetscLogTraceBegin()

323:   Notes:
324:   PetscLogTraceBegin() prints the processor number, the execution time (sec),
325:   then "Event begin:" or "Event end:" followed by the event name.

327:   PetscLogTraceBegin() allows tracing of all PETSc calls, which is useful
328:   to determine where a program is hanging without running in the
329:   debugger.  Can be used in conjunction with the -info option.

331:   Level: intermediate

333: .seealso: PetscLogDump(), PetscLogAllBegin(), PetscLogView(), PetscLogDefaultBegin()
334: @*/
335: PetscErrorCode  PetscLogTraceBegin(FILE *file)
336: {
337:   petsc_tracefile = file;

339:   PetscLogSet(PetscLogEventBeginTrace, PetscLogEventEndTrace);
340:   return 0;
341: }

343: /*@
344:   PetscLogActions - Determines whether actions are logged for the graphical viewer.

346:   Not Collective

348:   Input Parameter:
349: . flag - PETSC_TRUE if actions are to be logged

351:   Level: intermediate

353:   Note: Logging of actions continues to consume more memory as the program
354:   runs. Long running programs should consider turning this feature off.

356:   Options Database Keys:
357: . -log_exclude_actions - Turns off actions logging

359: .seealso: PetscLogStagePush(), PetscLogStagePop()
360: @*/
361: PetscErrorCode  PetscLogActions(PetscBool flag)
362: {
363:   petsc_logActions = flag;
364:   return 0;
365: }

367: /*@
368:   PetscLogObjects - Determines whether objects are logged for the graphical viewer.

370:   Not Collective

372:   Input Parameter:
373: . flag - PETSC_TRUE if objects are to be logged

375:   Level: intermediate

377:   Note: Logging of objects continues to consume more memory as the program
378:   runs. Long running programs should consider turning this feature off.

380:   Options Database Keys:
381: . -log_exclude_objects - Turns off objects logging

383: .seealso: PetscLogStagePush(), PetscLogStagePop()
384: @*/
385: PetscErrorCode  PetscLogObjects(PetscBool flag)
386: {
387:   petsc_logObjects = flag;
388:   return 0;
389: }

391: /*------------------------------------------------ Stage Functions --------------------------------------------------*/
392: /*@C
393:   PetscLogStageRegister - Attaches a character string name to a logging stage.

395:   Not Collective

397:   Input Parameter:
398: . sname - The name to associate with that stage

400:   Output Parameter:
401: . stage - The stage number

403:   Level: intermediate

405: .seealso: PetscLogStagePush(), PetscLogStagePop()
406: @*/
407: PetscErrorCode  PetscLogStageRegister(const char sname[],PetscLogStage *stage)
408: {
409:   PetscStageLog  stageLog;
410:   PetscLogEvent  event;

412:   PetscLogGetStageLog(&stageLog);
413:   PetscStageLogRegister(stageLog, sname, stage);
414:   /* Copy events already changed in the main stage, this sucks */
415:   PetscEventPerfLogEnsureSize(stageLog->stageInfo[*stage].eventLog, stageLog->eventLog->numEvents);
416:   for (event = 0; event < stageLog->eventLog->numEvents; event++) {
417:     PetscEventPerfInfoCopy(&stageLog->stageInfo[0].eventLog->eventInfo[event],&stageLog->stageInfo[*stage].eventLog->eventInfo[event]);
418:   }
419:   PetscClassPerfLogEnsureSize(stageLog->stageInfo[*stage].classLog, stageLog->classLog->numClasses);
420:   return 0;
421: }

423: /*@C
424:   PetscLogStagePush - This function pushes a stage on the stack.

426:   Not Collective

428:   Input Parameter:
429: . stage - The stage on which to log

431:   Usage:
432:   If the option -log_sumary is used to run the program containing the
433:   following code, then 2 sets of summary data will be printed during
434:   PetscFinalize().
435: .vb
436:       PetscInitialize(int *argc,char ***args,0,0);
437:       [stage 0 of code]
438:       PetscLogStagePush(1);
439:       [stage 1 of code]
440:       PetscLogStagePop();
441:       PetscBarrier(...);
442:       [more stage 0 of code]
443:       PetscFinalize();
444: .ve

446:   Notes:
447:   Use PetscLogStageRegister() to register a stage.

449:   Level: intermediate

451: .seealso: PetscLogStagePop(), PetscLogStageRegister(), PetscBarrier()
452: @*/
453: PetscErrorCode  PetscLogStagePush(PetscLogStage stage)
454: {
455:   PetscStageLog  stageLog;

457:   PetscLogGetStageLog(&stageLog);
458:   PetscStageLogPush(stageLog, stage);
459:   return 0;
460: }

462: /*@C
463:   PetscLogStagePop - This function pops a stage from the stack.

465:   Not Collective

467:   Usage:
468:   If the option -log_sumary is used to run the program containing the
469:   following code, then 2 sets of summary data will be printed during
470:   PetscFinalize().
471: .vb
472:       PetscInitialize(int *argc,char ***args,0,0);
473:       [stage 0 of code]
474:       PetscLogStagePush(1);
475:       [stage 1 of code]
476:       PetscLogStagePop();
477:       PetscBarrier(...);
478:       [more stage 0 of code]
479:       PetscFinalize();
480: .ve

482:   Notes:
483:   Use PetscLogStageRegister() to register a stage.

485:   Level: intermediate

487: .seealso: PetscLogStagePush(), PetscLogStageRegister(), PetscBarrier()
488: @*/
489: PetscErrorCode  PetscLogStagePop(void)
490: {
491:   PetscStageLog  stageLog;

493:   PetscLogGetStageLog(&stageLog);
494:   PetscStageLogPop(stageLog);
495:   return 0;
496: }

498: /*@
499:   PetscLogStageSetActive - Determines stage activity for PetscLogEventBegin() and PetscLogEventEnd().

501:   Not Collective

503:   Input Parameters:
504: + stage    - The stage
505: - isActive - The activity flag, PETSC_TRUE for logging, else PETSC_FALSE (defaults to PETSC_TRUE)

507:   Level: intermediate

509: .seealso: PetscLogStagePush(), PetscLogStagePop(), PetscLogEventBegin(), PetscLogEventEnd(), PetscPreLoadBegin(), PetscPreLoadEnd(), PetscPreLoadStage()
510: @*/
511: PetscErrorCode  PetscLogStageSetActive(PetscLogStage stage, PetscBool isActive)
512: {
513:   PetscStageLog  stageLog;

515:   PetscLogGetStageLog(&stageLog);
516:   PetscStageLogSetActive(stageLog, stage, isActive);
517:   return 0;
518: }

520: /*@
521:   PetscLogStageGetActive - Returns stage activity for PetscLogEventBegin() and PetscLogEventEnd().

523:   Not Collective

525:   Input Parameter:
526: . stage    - The stage

528:   Output Parameter:
529: . isActive - The activity flag, PETSC_TRUE for logging, else PETSC_FALSE (defaults to PETSC_TRUE)

531:   Level: intermediate

533: .seealso: PetscLogStagePush(), PetscLogStagePop(), PetscLogEventBegin(), PetscLogEventEnd(), PetscPreLoadBegin(), PetscPreLoadEnd(), PetscPreLoadStage()
534: @*/
535: PetscErrorCode  PetscLogStageGetActive(PetscLogStage stage, PetscBool  *isActive)
536: {
537:   PetscStageLog  stageLog;

539:   PetscLogGetStageLog(&stageLog);
540:   PetscStageLogGetActive(stageLog, stage, isActive);
541:   return 0;
542: }

544: /*@
545:   PetscLogStageSetVisible - Determines stage visibility in PetscLogView()

547:   Not Collective

549:   Input Parameters:
550: + stage     - The stage
551: - isVisible - The visibility flag, PETSC_TRUE to print, else PETSC_FALSE (defaults to PETSC_TRUE)

553:   Level: intermediate

555: .seealso: PetscLogStagePush(), PetscLogStagePop(), PetscLogView()
556: @*/
557: PetscErrorCode  PetscLogStageSetVisible(PetscLogStage stage, PetscBool isVisible)
558: {
559:   PetscStageLog  stageLog;

561:   PetscLogGetStageLog(&stageLog);
562:   PetscStageLogSetVisible(stageLog, stage, isVisible);
563:   return 0;
564: }

566: /*@
567:   PetscLogStageGetVisible - Returns stage visibility in PetscLogView()

569:   Not Collective

571:   Input Parameter:
572: . stage     - The stage

574:   Output Parameter:
575: . isVisible - The visibility flag, PETSC_TRUE to print, else PETSC_FALSE (defaults to PETSC_TRUE)

577:   Level: intermediate

579: .seealso: PetscLogStagePush(), PetscLogStagePop(), PetscLogView()
580: @*/
581: PetscErrorCode  PetscLogStageGetVisible(PetscLogStage stage, PetscBool  *isVisible)
582: {
583:   PetscStageLog  stageLog;

585:   PetscLogGetStageLog(&stageLog);
586:   PetscStageLogGetVisible(stageLog, stage, isVisible);
587:   return 0;
588: }

590: /*@C
591:   PetscLogStageGetId - Returns the stage id when given the stage name.

593:   Not Collective

595:   Input Parameter:
596: . name  - The stage name

598:   Output Parameter:
599: . stage - The stage, , or -1 if no stage with that name exists

601:   Level: intermediate

603: .seealso: PetscLogStagePush(), PetscLogStagePop(), PetscPreLoadBegin(), PetscPreLoadEnd(), PetscPreLoadStage()
604: @*/
605: PetscErrorCode  PetscLogStageGetId(const char name[], PetscLogStage *stage)
606: {
607:   PetscStageLog  stageLog;

609:   PetscLogGetStageLog(&stageLog);
610:   PetscStageLogGetStage(stageLog, name, stage);
611:   return 0;
612: }

614: /*------------------------------------------------ Event Functions --------------------------------------------------*/
615: /*@C
616:   PetscLogEventRegister - Registers an event name for logging operations in an application code.

618:   Not Collective

620:   Input Parameters:
621: + name   - The name associated with the event
622: - classid - The classid associated to the class for this event, obtain either with
623:            PetscClassIdRegister() or use a predefined one such as KSP_CLASSID, SNES_CLASSID, the predefined ones
624:            are only available in C code

626:   Output Parameter:
627: . event - The event id for use with PetscLogEventBegin() and PetscLogEventEnd().

629:   Example of Usage:
630: .vb
631:       PetscLogEvent USER_EVENT;
632:       PetscClassId classid;
633:       PetscLogDouble user_event_flops;
634:       PetscClassIdRegister("class name",&classid);
635:       PetscLogEventRegister("User event name",classid,&USER_EVENT);
636:       PetscLogEventBegin(USER_EVENT,0,0,0,0);
637:          [code segment to monitor]
638:          PetscLogFlops(user_event_flops);
639:       PetscLogEventEnd(USER_EVENT,0,0,0,0);
640: .ve

642:   Notes:
643:   PETSc automatically logs library events if the code has been
644:   configured with --with-log (which is the default) and
645:   -log_view or -log_all is specified.  PetscLogEventRegister() is
646:   intended for logging user events to supplement this PETSc
647:   information.

649:   PETSc can gather data for use with the utilities Jumpshot
650:   (part of the MPICH distribution).  If PETSc has been compiled
651:   with flag -DPETSC_HAVE_MPE (MPE is an additional utility within
652:   MPICH), the user can employ another command line option, -log_mpe,
653:   to create a logfile, "mpe.log", which can be visualized
654:   Jumpshot.

656:   The classid is associated with each event so that classes of events
657:   can be disabled simultaneously, such as all matrix events. The user
658:   can either use an existing classid, such as MAT_CLASSID, or create
659:   their own as shown in the example.

661:   If an existing event with the same name exists, its event handle is
662:   returned instead of creating a new event.

664:   Level: intermediate

666: .seealso: PetscLogEventBegin(), PetscLogEventEnd(), PetscLogFlops(),
667:           PetscLogEventActivate(), PetscLogEventDeactivate(), PetscClassIdRegister()
668: @*/
669: PetscErrorCode  PetscLogEventRegister(const char name[],PetscClassId classid,PetscLogEvent *event)
670: {
671:   PetscStageLog  stageLog;
672:   int            stage;

674:   *event = PETSC_DECIDE;
675:   PetscLogGetStageLog(&stageLog);
676:   PetscEventRegLogGetEvent(stageLog->eventLog, name, event);
677:   if (*event > 0) return 0;
678:   PetscEventRegLogRegister(stageLog->eventLog, name, classid, event);
679:   for (stage = 0; stage < stageLog->numStages; stage++) {
680:     PetscEventPerfLogEnsureSize(stageLog->stageInfo[stage].eventLog, stageLog->eventLog->numEvents);
681:     PetscClassPerfLogEnsureSize(stageLog->stageInfo[stage].classLog, stageLog->classLog->numClasses);
682:   }
683:   return 0;
684: }

686: /*@
687:   PetscLogEventSetCollective - Indicates that a particular event is collective.

689:   Not Collective

691:   Input Parameters:
692: + event - The event id
693: - collective - Bolean flag indicating whether a particular event is collective

695:   Note:
696:   New events returned from PetscLogEventRegister() are collective by default.

698:   Level: developer

700: .seealso: PetscLogEventRegister()
701: @*/
702: PetscErrorCode PetscLogEventSetCollective(PetscLogEvent event,PetscBool collective)
703: {
704:   PetscStageLog    stageLog;
705:   PetscEventRegLog eventRegLog;

707:   PetscLogGetStageLog(&stageLog);
708:   PetscStageLogGetEventRegLog(stageLog,&eventRegLog);
710:   eventRegLog->eventInfo[event].collective = collective;
711:   return 0;
712: }

714: /*@
715:   PetscLogEventIncludeClass - Activates event logging for a PETSc object class in every stage.

717:   Not Collective

719:   Input Parameter:
720: . classid - The object class, for example MAT_CLASSID, SNES_CLASSID, etc.

722:   Level: developer

724: .seealso: PetscLogEventActivateClass(),PetscLogEventDeactivateClass(),PetscLogEventActivate(),PetscLogEventDeactivate()
725: @*/
726: PetscErrorCode  PetscLogEventIncludeClass(PetscClassId classid)
727: {
728:   PetscStageLog  stageLog;
729:   int            stage;

731:   PetscLogGetStageLog(&stageLog);
732:   for (stage = 0; stage < stageLog->numStages; stage++) {
733:     PetscEventPerfLogActivateClass(stageLog->stageInfo[stage].eventLog, stageLog->eventLog, classid);
734:   }
735:   return 0;
736: }

738: /*@
739:   PetscLogEventExcludeClass - Deactivates event logging for a PETSc object class in every stage.

741:   Not Collective

743:   Input Parameter:
744: . classid - The object class, for example MAT_CLASSID, SNES_CLASSID, etc.

746:   Level: developer

748: .seealso: PetscLogEventDeactivateClass(),PetscLogEventActivateClass(),PetscLogEventDeactivate(),PetscLogEventActivate()
749: @*/
750: PetscErrorCode  PetscLogEventExcludeClass(PetscClassId classid)
751: {
752:   PetscStageLog  stageLog;
753:   int            stage;

755:   PetscLogGetStageLog(&stageLog);
756:   for (stage = 0; stage < stageLog->numStages; stage++) {
757:     PetscEventPerfLogDeactivateClass(stageLog->stageInfo[stage].eventLog, stageLog->eventLog, classid);
758:   }
759:   return 0;
760: }

762: /*@
763:   PetscLogEventActivate - Indicates that a particular event should be logged.

765:   Not Collective

767:   Input Parameter:
768: . event - The event id

770:   Usage:
771: .vb
772:       PetscLogEventDeactivate(VEC_SetValues);
773:         [code where you do not want to log VecSetValues()]
774:       PetscLogEventActivate(VEC_SetValues);
775:         [code where you do want to log VecSetValues()]
776: .ve

778:   Note:
779:   The event may be either a pre-defined PETSc event (found in include/petsclog.h)
780:   or an event number obtained with PetscLogEventRegister().

782:   Level: advanced

784: .seealso: PlogEventDeactivate(), PlogEventDeactivatePush(), PetscLogEventDeactivatePop()
785: @*/
786: PetscErrorCode  PetscLogEventActivate(PetscLogEvent event)
787: {
788:   PetscStageLog  stageLog;
789:   int            stage;

791:   PetscLogGetStageLog(&stageLog);
792:   PetscStageLogGetCurrent(stageLog, &stage);
793:   PetscEventPerfLogActivate(stageLog->stageInfo[stage].eventLog, event);
794:   return 0;
795: }

797: /*@
798:   PetscLogEventDeactivate - Indicates that a particular event should not be logged.

800:   Not Collective

802:   Input Parameter:
803: . event - The event id

805:   Usage:
806: .vb
807:       PetscLogEventDeactivate(VEC_SetValues);
808:         [code where you do not want to log VecSetValues()]
809:       PetscLogEventActivate(VEC_SetValues);
810:         [code where you do want to log VecSetValues()]
811: .ve

813:   Note:
814:   The event may be either a pre-defined PETSc event (found in
815:   include/petsclog.h) or an event number obtained with PetscLogEventRegister()).

817:   Level: advanced

819: .seealso: PetscLogEventActivate(), PetscLogEventDeactivatePush(), PetscLogEventDeactivatePop()
820: @*/
821: PetscErrorCode  PetscLogEventDeactivate(PetscLogEvent event)
822: {
823:   PetscStageLog  stageLog;
824:   int            stage;

826:   PetscLogGetStageLog(&stageLog);
827:   PetscStageLogGetCurrent(stageLog, &stage);
828:   PetscEventPerfLogDeactivate(stageLog->stageInfo[stage].eventLog, event);
829:   return 0;
830: }

832: /*@
833:   PetscLogEventDeactivatePush - Indicates that a particular event should not be logged.

835:   Not Collective

837:   Input Parameter:
838: . event - The event id

840:   Usage:
841: .vb
842:       PetscLogEventDeactivatePush(VEC_SetValues);
843:         [code where you do not want to log VecSetValues()]
844:       PetscLogEventDeactivatePop(VEC_SetValues);
845:         [code where you do want to log VecSetValues()]
846: .ve

848:   Note:
849:   The event may be either a pre-defined PETSc event (found in
850:   include/petsclog.h) or an event number obtained with PetscLogEventRegister()).

852:   Level: advanced

854: .seealso: PetscLogEventActivate(), PetscLogEventDeactivatePop()
855: @*/
856: PetscErrorCode  PetscLogEventDeactivatePush(PetscLogEvent event)
857: {
858:   PetscStageLog  stageLog;
859:   int            stage;

861:   PetscLogGetStageLog(&stageLog);
862:   PetscStageLogGetCurrent(stageLog, &stage);
863:   PetscEventPerfLogDeactivatePush(stageLog->stageInfo[stage].eventLog, event);
864:   return 0;
865: }

867: /*@
868:   PetscLogEventDeactivatePop - Indicates that a particular event should be logged.

870:   Not Collective

872:   Input Parameter:
873: . event - The event id

875:   Usage:
876: .vb
877:       PetscLogEventDeactivatePush(VEC_SetValues);
878:         [code where you do not want to log VecSetValues()]
879:       PetscLogEventDeactivatePop(VEC_SetValues);
880:         [code where you do want to log VecSetValues()]
881: .ve

883:   Note:
884:   The event may be either a pre-defined PETSc event (found in
885:   include/petsclog.h) or an event number obtained with PetscLogEventRegister()).

887:   Level: advanced

889: .seealso: PetscLogEventActivate(), PetscLogEventDeactivatePush()
890: @*/
891: PetscErrorCode  PetscLogEventDeactivatePop(PetscLogEvent event)
892: {
893:   PetscStageLog  stageLog;
894:   int            stage;

896:   PetscLogGetStageLog(&stageLog);
897:   PetscStageLogGetCurrent(stageLog, &stage);
898:   PetscEventPerfLogDeactivatePop(stageLog->stageInfo[stage].eventLog, event);
899:   return 0;
900: }

902: /*@
903:   PetscLogEventSetActiveAll - Sets the event activity in every stage.

905:   Not Collective

907:   Input Parameters:
908: + event    - The event id
909: - isActive - The activity flag determining whether the event is logged

911:   Level: advanced

913: .seealso: PlogEventActivate(),PlogEventDeactivate()
914: @*/
915: PetscErrorCode  PetscLogEventSetActiveAll(PetscLogEvent event, PetscBool isActive)
916: {
917:   PetscStageLog  stageLog;
918:   int            stage;

920:   PetscLogGetStageLog(&stageLog);
921:   for (stage = 0; stage < stageLog->numStages; stage++) {
922:     if (isActive) {
923:       PetscEventPerfLogActivate(stageLog->stageInfo[stage].eventLog, event);
924:     } else {
925:       PetscEventPerfLogDeactivate(stageLog->stageInfo[stage].eventLog, event);
926:     }
927:   }
928:   return 0;
929: }

931: /*@
932:   PetscLogEventActivateClass - Activates event logging for a PETSc object class.

934:   Not Collective

936:   Input Parameter:
937: . classid - The event class, for example MAT_CLASSID, SNES_CLASSID, etc.

939:   Level: developer

941: .seealso: PetscLogEventDeactivateClass(),PetscLogEventActivate(),PetscLogEventDeactivate()
942: @*/
943: PetscErrorCode  PetscLogEventActivateClass(PetscClassId classid)
944: {
945:   PetscStageLog  stageLog;
946:   int            stage;

948:   PetscLogGetStageLog(&stageLog);
949:   PetscStageLogGetCurrent(stageLog, &stage);
950:   PetscEventPerfLogActivateClass(stageLog->stageInfo[stage].eventLog, stageLog->eventLog, classid);
951:   return 0;
952: }

954: /*@
955:   PetscLogEventDeactivateClass - Deactivates event logging for a PETSc object class.

957:   Not Collective

959:   Input Parameter:
960: . classid - The event class, for example MAT_CLASSID, SNES_CLASSID, etc.

962:   Level: developer

964: .seealso: PetscLogEventActivateClass(),PetscLogEventActivate(),PetscLogEventDeactivate()
965: @*/
966: PetscErrorCode  PetscLogEventDeactivateClass(PetscClassId classid)
967: {
968:   PetscStageLog  stageLog;
969:   int            stage;

971:   PetscLogGetStageLog(&stageLog);
972:   PetscStageLogGetCurrent(stageLog, &stage);
973:   PetscEventPerfLogDeactivateClass(stageLog->stageInfo[stage].eventLog, stageLog->eventLog, classid);
974:   return 0;
975: }

977: /*MC
978:    PetscLogEventSync - Synchronizes the beginning of a user event.

980:    Synopsis:
981: #include <petsclog.h>
982:    PetscErrorCode PetscLogEventSync(int e,MPI_Comm comm)

984:    Collective

986:    Input Parameters:
987: +  e - integer associated with the event obtained from PetscLogEventRegister()
988: -  comm - an MPI communicator

990:    Usage:
991: .vb
992:      PetscLogEvent USER_EVENT;
993:      PetscLogEventRegister("User event",0,&USER_EVENT);
994:      PetscLogEventSync(USER_EVENT,PETSC_COMM_WORLD);
995:      PetscLogEventBegin(USER_EVENT,0,0,0,0);
996:         [code segment to monitor]
997:      PetscLogEventEnd(USER_EVENT,0,0,0,0);
998: .ve

1000:    Notes:
1001:    This routine should be called only if there is not a
1002:    PetscObject available to pass to PetscLogEventBegin().

1004:    Level: developer

1006: .seealso: PetscLogEventRegister(), PetscLogEventBegin(), PetscLogEventEnd()

1008: M*/

1010: /*MC
1011:    PetscLogEventBegin - Logs the beginning of a user event.

1013:    Synopsis:
1014: #include <petsclog.h>
1015:    PetscErrorCode PetscLogEventBegin(int e,PetscObject o1,PetscObject o2,PetscObject o3,PetscObject o4)

1017:    Not Collective

1019:    Input Parameters:
1020: +  e - integer associated with the event obtained from PetscLogEventRegister()
1021: -  o1,o2,o3,o4 - objects associated with the event, or 0

1023:    Fortran Synopsis:
1024:    void PetscLogEventBegin(int e,PetscErrorCode ierr)

1026:    Usage:
1027: .vb
1028:      PetscLogEvent USER_EVENT;
1029:      PetscLogDouble user_event_flops;
1030:      PetscLogEventRegister("User event",0,&USER_EVENT);
1031:      PetscLogEventBegin(USER_EVENT,0,0,0,0);
1032:         [code segment to monitor]
1033:         PetscLogFlops(user_event_flops);
1034:      PetscLogEventEnd(USER_EVENT,0,0,0,0);
1035: .ve

1037:    Notes:
1038:    You need to register each integer event with the command
1039:    PetscLogEventRegister().

1041:    Level: intermediate

1043: .seealso: PetscLogEventRegister(), PetscLogEventEnd(), PetscLogFlops()

1045: M*/

1047: /*MC
1048:    PetscLogEventEnd - Log the end of a user event.

1050:    Synopsis:
1051: #include <petsclog.h>
1052:    PetscErrorCode PetscLogEventEnd(int e,PetscObject o1,PetscObject o2,PetscObject o3,PetscObject o4)

1054:    Not Collective

1056:    Input Parameters:
1057: +  e - integer associated with the event obtained with PetscLogEventRegister()
1058: -  o1,o2,o3,o4 - objects associated with the event, or 0

1060:    Fortran Synopsis:
1061:    void PetscLogEventEnd(int e,PetscErrorCode ierr)

1063:    Usage:
1064: .vb
1065:      PetscLogEvent USER_EVENT;
1066:      PetscLogDouble user_event_flops;
1067:      PetscLogEventRegister("User event",0,&USER_EVENT,);
1068:      PetscLogEventBegin(USER_EVENT,0,0,0,0);
1069:         [code segment to monitor]
1070:         PetscLogFlops(user_event_flops);
1071:      PetscLogEventEnd(USER_EVENT,0,0,0,0);
1072: .ve

1074:    Notes:
1075:    You should also register each additional integer event with the command
1076:    PetscLogEventRegister().

1078:    Level: intermediate

1080: .seealso: PetscLogEventRegister(), PetscLogEventBegin(), PetscLogFlops()

1082: M*/

1084: /*@C
1085:   PetscLogEventGetId - Returns the event id when given the event name.

1087:   Not Collective

1089:   Input Parameter:
1090: . name  - The event name

1092:   Output Parameter:
1093: . event - The event, or -1 if no event with that name exists

1095:   Level: intermediate

1097: .seealso: PetscLogEventBegin(), PetscLogEventEnd(), PetscLogStageGetId()
1098: @*/
1099: PetscErrorCode  PetscLogEventGetId(const char name[], PetscLogEvent *event)
1100: {
1101:   PetscStageLog  stageLog;

1103:   PetscLogGetStageLog(&stageLog);
1104:   PetscEventRegLogGetEvent(stageLog->eventLog, name, event);
1105:   return 0;
1106: }

1108: /*------------------------------------------------ Output Functions -------------------------------------------------*/
1109: /*@C
1110:   PetscLogDump - Dumps logs of objects to a file. This file is intended to
1111:   be read by bin/petscview. This program no longer exists.

1113:   Collective on PETSC_COMM_WORLD

1115:   Input Parameter:
1116: . name - an optional file name

1118:   Usage:
1119: .vb
1120:      PetscInitialize(...);
1121:      PetscLogDefaultBegin(); or PetscLogAllBegin();
1122:      ... code ...
1123:      PetscLogDump(filename);
1124:      PetscFinalize();
1125: .ve

1127:   Notes:
1128:   The default file name is
1129: $    Log.<rank>
1130:   where <rank> is the processor number. If no name is specified,
1131:   this file will be used.

1133:   Level: advanced

1135: .seealso: PetscLogDefaultBegin(), PetscLogAllBegin(), PetscLogView()
1136: @*/
1137: PetscErrorCode  PetscLogDump(const char sname[])
1138: {
1139:   PetscStageLog      stageLog;
1140:   PetscEventPerfInfo *eventInfo;
1141:   FILE               *fd;
1142:   char               file[PETSC_MAX_PATH_LEN], fname[PETSC_MAX_PATH_LEN];
1143:   PetscLogDouble     flops, _TotalTime;
1144:   PetscMPIInt        rank;
1145:   int                action, object, curStage;
1146:   PetscLogEvent      event;
1147:   PetscErrorCode     ierr;

1149:   /* Calculate the total elapsed time */
1150:   PetscTime(&_TotalTime);
1151:   _TotalTime -= petsc_BaseTime;
1152:   /* Open log file */
1153:   MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
1154:   if (sname && sname[0]) sprintf(file, "%s.%d", sname, rank);
1155:   else sprintf(file, "Log.%d", rank);
1156:   PetscFixFilename(file, fname);
1157:   PetscFOpen(PETSC_COMM_WORLD, fname, "w", &fd);
1159:   /* Output totals */
1160:   PetscFPrintf(PETSC_COMM_WORLD, fd, "Total Flop %14e %16.8e\n", petsc_TotalFlops, _TotalTime);
1161:   PetscFPrintf(PETSC_COMM_WORLD, fd, "Clock Resolution %g\n", 0.0);
1162:   /* Output actions */
1163:   if (petsc_logActions) {
1164:     PetscFPrintf(PETSC_COMM_WORLD, fd, "Actions accomplished %d\n", petsc_numActions);
1165:     for (action = 0; action < petsc_numActions; action++) {
1166:       PetscFPrintf(PETSC_COMM_WORLD, fd, "%g %d %d %d %d %d %d %g %g %g\n",
1167:                           petsc_actions[action].time, petsc_actions[action].action, (int)petsc_actions[action].event, (int)petsc_actions[action].classid, petsc_actions[action].id1,
1168:                           petsc_actions[action].id2, petsc_actions[action].id3, petsc_actions[action].flops, petsc_actions[action].mem, petsc_actions[action].maxmem);
1169:     }
1170:   }
1171:   /* Output objects */
1172:   if (petsc_logObjects) {
1173:     PetscFPrintf(PETSC_COMM_WORLD, fd, "Objects created %d destroyed %d\n", petsc_numObjects, petsc_numObjectsDestroyed);
1174:     for (object = 0; object < petsc_numObjects; object++) {
1175:       PetscFPrintf(PETSC_COMM_WORLD, fd, "Parent ID: %d Memory: %d\n", petsc_objects[object].parent, (int) petsc_objects[object].mem);
1176:       if (!petsc_objects[object].name[0]) {
1177:         PetscFPrintf(PETSC_COMM_WORLD, fd,"No Name\n");
1178:       } else {
1179:         PetscFPrintf(PETSC_COMM_WORLD, fd, "Name: %s\n", petsc_objects[object].name);
1180:       }
1181:       if (petsc_objects[object].info[0] != 0) {
1182:         PetscFPrintf(PETSC_COMM_WORLD, fd, "No Info\n");
1183:       } else {
1184:         PetscFPrintf(PETSC_COMM_WORLD, fd, "Info: %s\n", petsc_objects[object].info);
1185:       }
1186:     }
1187:   }
1188:   /* Output events */
1189:   PetscFPrintf(PETSC_COMM_WORLD, fd, "Event log:\n");
1190:   PetscLogGetStageLog(&stageLog);
1191:   PetscIntStackTop(stageLog->stack, &curStage);
1192:   eventInfo = stageLog->stageInfo[curStage].eventLog->eventInfo;
1193:   for (event = 0; event < stageLog->stageInfo[curStage].eventLog->numEvents; event++) {
1194:     if (eventInfo[event].time != 0.0) flops = eventInfo[event].flops/eventInfo[event].time;
1195:     else flops = 0.0;
1196:     PetscFPrintf(PETSC_COMM_WORLD, fd, "%d %16d %16g %16g %16g\n", event, eventInfo[event].count,
1197:                         eventInfo[event].flops, eventInfo[event].time, flops);
1198:   }
1199:   PetscFClose(PETSC_COMM_WORLD, fd);
1200:   return 0;
1201: }

1203: /*
1204:   PetscLogView_Detailed - Each process prints the times for its own events

1206: */
1207: PetscErrorCode  PetscLogView_Detailed(PetscViewer viewer)
1208: {
1209:   PetscStageLog      stageLog;
1210:   PetscEventPerfInfo *eventInfo = NULL, *stageInfo = NULL;
1211:   PetscLogDouble     locTotalTime, numRed, maxMem;
1212:   int                numStages,numEvents,stage,event;
1213:   MPI_Comm           comm = PetscObjectComm((PetscObject) viewer);
1214:   PetscMPIInt        rank,size;
1215:   PetscErrorCode     ierr;

1217:   MPI_Comm_size(comm, &size);
1218:   MPI_Comm_rank(comm, &rank);
1219:   /* Must preserve reduction count before we go on */
1220:   numRed = petsc_allreduce_ct + petsc_gather_ct + petsc_scatter_ct;
1221:   /* Get the total elapsed time */
1222:   PetscTime(&locTotalTime);  locTotalTime -= petsc_BaseTime;
1223:   PetscViewerASCIIPrintf(viewer,"size = %d\n",size);
1224:   PetscViewerASCIIPrintf(viewer,"LocalTimes = {}\n");
1225:   PetscViewerASCIIPrintf(viewer,"LocalMessages = {}\n");
1226:   PetscViewerASCIIPrintf(viewer,"LocalMessageLens = {}\n");
1227:   PetscViewerASCIIPrintf(viewer,"LocalReductions = {}\n");
1228:   PetscViewerASCIIPrintf(viewer,"LocalFlop = {}\n");
1229:   PetscViewerASCIIPrintf(viewer,"LocalObjects = {}\n");
1230:   PetscViewerASCIIPrintf(viewer,"LocalMemory = {}\n");
1231:   PetscLogGetStageLog(&stageLog);
1232:   MPI_Allreduce(&stageLog->numStages, &numStages, 1, MPI_INT, MPI_MAX, comm);
1233:   PetscViewerASCIIPrintf(viewer,"Stages = {}\n");
1234:   for (stage=0; stage<numStages; stage++) {
1235:     PetscViewerASCIIPrintf(viewer,"Stages[\"%s\"] = {}\n",stageLog->stageInfo[stage].name);
1236:     PetscViewerASCIIPrintf(viewer,"Stages[\"%s\"][\"summary\"] = {}\n",stageLog->stageInfo[stage].name);
1237:     MPI_Allreduce(&stageLog->stageInfo[stage].eventLog->numEvents, &numEvents, 1, MPI_INT, MPI_MAX, comm);
1238:     for (event = 0; event < numEvents; event++) {
1239:       PetscViewerASCIIPrintf(viewer,"Stages[\"%s\"][\"%s\"] = {}\n",stageLog->stageInfo[stage].name,stageLog->eventLog->eventInfo[event].name);
1240:     }
1241:   }
1242:   PetscMallocGetMaximumUsage(&maxMem);
1243:   PetscViewerASCIIPushSynchronized(viewer);
1244:   PetscViewerASCIISynchronizedPrintf(viewer,"LocalTimes[%d] = %g\n",rank,locTotalTime);
1245:   PetscViewerASCIISynchronizedPrintf(viewer,"LocalMessages[%d] = %g\n",rank,(petsc_irecv_ct + petsc_isend_ct + petsc_recv_ct + petsc_send_ct));
1246:   PetscViewerASCIISynchronizedPrintf(viewer,"LocalMessageLens[%d] = %g\n",rank,(petsc_irecv_len + petsc_isend_len + petsc_recv_len + petsc_send_len));
1247:   PetscViewerASCIISynchronizedPrintf(viewer,"LocalReductions[%d] = %g\n",rank,numRed);
1248:   PetscViewerASCIISynchronizedPrintf(viewer,"LocalFlop[%d] = %g\n",rank,petsc_TotalFlops);
1249:   PetscViewerASCIISynchronizedPrintf(viewer,"LocalObjects[%d] = %d\n",rank,petsc_numObjects);
1250:   PetscViewerASCIISynchronizedPrintf(viewer,"LocalMemory[%d] = %g\n",rank,maxMem);
1251:   PetscViewerFlush(viewer);
1252:   for (stage=0; stage<numStages; stage++) {
1253:     stageInfo = &stageLog->stageInfo[stage].perfInfo;
1254:     PetscViewerASCIISynchronizedPrintf(viewer,"Stages[\"%s\"][\"summary\"][%d] = {\"time\" : %g, \"numMessages\" : %g, \"messageLength\" : %g, \"numReductions\" : %g, \"flop\" : %g}\n",
1255:                                               stageLog->stageInfo[stage].name,rank,
1256:                                               stageInfo->time,stageInfo->numMessages,stageInfo->messageLength,stageInfo->numReductions,stageInfo->flops);
1257:     MPI_Allreduce(&stageLog->stageInfo[stage].eventLog->numEvents, &numEvents, 1, MPI_INT, MPI_MAX, comm);
1258:     for (event = 0; event < numEvents; event++) {
1259:       eventInfo = &stageLog->stageInfo[stage].eventLog->eventInfo[event];
1260:       PetscViewerASCIISynchronizedPrintf(viewer,"Stages[\"%s\"][\"%s\"][%d] = {\"count\" : %d, \"time\" : %g, \"syncTime\" : %g, \"numMessages\" : %g, \"messageLength\" : %g, \"numReductions\" : %g, \"flop\" : %g",
1261:                                                 stageLog->stageInfo[stage].name,stageLog->eventLog->eventInfo[event].name,rank,
1262:                                                 eventInfo->count,eventInfo->time,eventInfo->syncTime,eventInfo->numMessages,eventInfo->messageLength,eventInfo->numReductions,eventInfo->flops);
1263:       if (eventInfo->dof[0] >= 0.) {
1264:         PetscInt d, e;

1266:         PetscViewerASCIISynchronizedPrintf(viewer, ", \"dof\" : [");
1267:         for (d = 0; d < 8; ++d) {
1268:           if (d > 0) PetscViewerASCIISynchronizedPrintf(viewer, ", ");
1269:           PetscViewerASCIISynchronizedPrintf(viewer, "%g", eventInfo->dof[d]);
1270:         }
1271:         PetscViewerASCIISynchronizedPrintf(viewer, "]");
1272:         PetscViewerASCIISynchronizedPrintf(viewer, ", \"error\" : [");
1273:         for (e = 0; e < 8; ++e) {
1274:           if (e > 0) PetscViewerASCIISynchronizedPrintf(viewer, ", ");
1275:           PetscViewerASCIISynchronizedPrintf(viewer, "%g", eventInfo->errors[e]);
1276:         }
1277:         PetscViewerASCIISynchronizedPrintf(viewer, "]");
1278:       }
1279:       PetscViewerASCIISynchronizedPrintf(viewer,"}\n");
1280:     }
1281:   }
1282:   PetscViewerFlush(viewer);
1283:   PetscViewerASCIIPopSynchronized(viewer);
1284:   return 0;
1285: }

1287: /*
1288:   PetscLogView_CSV - Each process prints the times for its own events in Comma-Separated Value Format
1289: */
1290: PetscErrorCode  PetscLogView_CSV(PetscViewer viewer)
1291: {
1292:   PetscStageLog      stageLog;
1293:   PetscEventPerfInfo *eventInfo = NULL;
1294:   PetscLogDouble     locTotalTime, maxMem;
1295:   int                numStages,numEvents,stage,event;
1296:   MPI_Comm           comm = PetscObjectComm((PetscObject) viewer);
1297:   PetscMPIInt        rank,size;
1298:   PetscErrorCode     ierr;

1300:   MPI_Comm_size(comm, &size);
1301:   MPI_Comm_rank(comm, &rank);
1302:   /* Must preserve reduction count before we go on */
1303:   /* Get the total elapsed time */
1304:   PetscTime(&locTotalTime);  locTotalTime -= petsc_BaseTime;
1305:   PetscLogGetStageLog(&stageLog);
1306:   MPI_Allreduce(&stageLog->numStages, &numStages, 1, MPI_INT, MPI_MAX, comm);
1307:   PetscMallocGetMaximumUsage(&maxMem);
1308:   PetscViewerASCIIPushSynchronized(viewer);
1309:   PetscViewerASCIIPrintf(viewer,"Stage Name,Event Name,Rank,Count,Time,Num Messages,Message Length,Num Reductions,FLOP,dof0,dof1,dof2,dof3,dof4,dof5,dof6,dof7,e0,e1,e2,e3,e4,e5,e6,e7,%d\n", size);
1310:   PetscViewerFlush(viewer);
1311:   for (stage=0; stage<numStages; stage++) {
1312:     PetscEventPerfInfo *stageInfo = &stageLog->stageInfo[stage].perfInfo;

1314:     PetscViewerASCIISynchronizedPrintf(viewer,"%s,summary,%d,1,%g,%g,%g,%g,%g\n",
1315:                                               stageLog->stageInfo[stage].name,rank,stageInfo->time,stageInfo->numMessages,stageInfo->messageLength,stageInfo->numReductions,stageInfo->flops);
1316:     MPI_Allreduce(&stageLog->stageInfo[stage].eventLog->numEvents, &numEvents, 1, MPI_INT, MPI_MAX, comm);
1317:     for (event = 0; event < numEvents; event++) {
1318:       eventInfo = &stageLog->stageInfo[stage].eventLog->eventInfo[event];
1319:       PetscViewerASCIISynchronizedPrintf(viewer,"%s,%s,%d,%d,%g,%g,%g,%g,%g",stageLog->stageInfo[stage].name,
1320:                                                 stageLog->eventLog->eventInfo[event].name,rank,eventInfo->count,eventInfo->time,eventInfo->numMessages,
1321:                                                 eventInfo->messageLength,eventInfo->numReductions,eventInfo->flops);
1322:       if (eventInfo->dof[0] >= 0.) {
1323:         PetscInt d, e;

1325:         for (d = 0; d < 8; ++d) {
1326:           PetscViewerASCIISynchronizedPrintf(viewer, ",%g", eventInfo->dof[d]);
1327:         }
1328:         for (e = 0; e < 8; ++e) {
1329:           PetscViewerASCIISynchronizedPrintf(viewer, ",%g", eventInfo->errors[e]);
1330:         }
1331:       }
1332:       PetscViewerASCIISynchronizedPrintf(viewer,"\n");
1333:     }
1334:   }
1335:   PetscViewerFlush(viewer);
1336:   PetscViewerASCIIPopSynchronized(viewer);
1337:   return 0;
1338: }

1340: static PetscErrorCode PetscLogViewWarnSync(MPI_Comm comm,FILE *fd)
1341: {
1342:   if (!PetscLogSyncOn) return 0;
1343:   PetscFPrintf(comm, fd, "\n\n");
1344:   PetscFPrintf(comm, fd, "      ##########################################################\n");
1345:   PetscFPrintf(comm, fd, "      #                                                        #\n");
1346:   PetscFPrintf(comm, fd, "      #                       WARNING!!!                       #\n");
1347:   PetscFPrintf(comm, fd, "      #                                                        #\n");
1348:   PetscFPrintf(comm, fd, "      #   This program was run with logging synchronization.   #\n");
1349:   PetscFPrintf(comm, fd, "      #   This option provides more meaningful imbalance       #\n");
1350:   PetscFPrintf(comm, fd, "      #   figures at the expense of slowing things down and    #\n");
1351:   PetscFPrintf(comm, fd, "      #   providing a distorted view of the overall runtime.   #\n");
1352:   PetscFPrintf(comm, fd, "      #                                                        #\n");
1353:   PetscFPrintf(comm, fd, "      ##########################################################\n\n\n");
1354:   return 0;
1355: }

1357: static PetscErrorCode PetscLogViewWarnDebugging(MPI_Comm comm,FILE *fd)
1358: {
1359:   if (PetscDefined(USE_DEBUG)) {
1360:     PetscFPrintf(comm, fd, "\n\n");
1361:     PetscFPrintf(comm, fd, "      ##########################################################\n");
1362:     PetscFPrintf(comm, fd, "      #                                                        #\n");
1363:     PetscFPrintf(comm, fd, "      #                       WARNING!!!                       #\n");
1364:     PetscFPrintf(comm, fd, "      #                                                        #\n");
1365:     PetscFPrintf(comm, fd, "      #   This code was compiled with a debugging option.      #\n");
1366:     PetscFPrintf(comm, fd, "      #   To get timing results run ./configure                #\n");
1367:     PetscFPrintf(comm, fd, "      #   using --with-debugging=no, the performance will      #\n");
1368:     PetscFPrintf(comm, fd, "      #   be generally two or three times faster.              #\n");
1369:     PetscFPrintf(comm, fd, "      #                                                        #\n");
1370:     PetscFPrintf(comm, fd, "      ##########################################################\n\n\n");
1371:   }
1372:   return 0;
1373: }

1375: static PetscErrorCode PetscLogViewWarnNoGpuAwareMpi(MPI_Comm comm,FILE *fd)
1376: {
1377: #if defined(PETSC_HAVE_DEVICE)
1378:   PetscMPIInt    size;

1380:   MPI_Comm_size(comm, &size);
1381:   if (use_gpu_aware_mpi || size == 1) return 0;
1382:   PetscFPrintf(comm, fd, "\n\n");
1383:   PetscFPrintf(comm, fd, "      ##########################################################\n");
1384:   PetscFPrintf(comm, fd, "      #                                                        #\n");
1385:   PetscFPrintf(comm, fd, "      #                       WARNING!!!                       #\n");
1386:   PetscFPrintf(comm, fd, "      #                                                        #\n");
1387:   PetscFPrintf(comm, fd, "      #   This code was compiled with GPU support and you've   #\n");
1388:   PetscFPrintf(comm, fd, "      #   created PETSc/GPU objects, but you intentionally     #\n");
1389:   PetscFPrintf(comm, fd, "      #   used -use_gpu_aware_mpi 0, requiring PETSc to copy   #\n");
1390:   PetscFPrintf(comm, fd, "      #   additional data between the GPU and CPU. To obtain   #\n");
1391:   PetscFPrintf(comm, fd, "      #   meaningful timing results on multi-rank runs, use    #\n");
1392:   PetscFPrintf(comm, fd, "      #   GPU-aware MPI instead.                               #\n");
1393:   PetscFPrintf(comm, fd, "      #                                                        #\n");
1394:   PetscFPrintf(comm, fd, "      ##########################################################\n\n\n");
1395:   return 0;
1396: #else
1397:   return 0;
1398: #endif
1399: }

1401: PetscErrorCode  PetscLogView_Default(PetscViewer viewer)
1402: {
1403:   FILE               *fd;
1404:   PetscLogDouble     zero       = 0.0;
1405:   PetscStageLog      stageLog;
1406:   PetscStageInfo     *stageInfo = NULL;
1407:   PetscEventPerfInfo *eventInfo = NULL;
1408:   PetscClassPerfInfo *classInfo;
1409:   char               arch[128],hostname[128],username[128],pname[PETSC_MAX_PATH_LEN],date[128];
1410:   const char         *name;
1411:   PetscLogDouble     locTotalTime, TotalTime, TotalFlops;
1412:   PetscLogDouble     numMessages, messageLength, avgMessLen, numReductions;
1413:   PetscLogDouble     stageTime, flops, flopr, mem, mess, messLen, red;
1414:   PetscLogDouble     fracTime, fracFlops, fracMessages, fracLength, fracReductions, fracMess, fracMessLen, fracRed;
1415:   PetscLogDouble     fracStageTime, fracStageFlops, fracStageMess, fracStageMessLen, fracStageRed;
1416:   PetscLogDouble     min, max, tot, ratio, avg, x, y;
1417:   PetscLogDouble     minf, maxf, totf, ratf, mint, maxt, tott, ratt, ratC, totm, totml, totr, mal, malmax, emalmax;
1418:   #if defined(PETSC_HAVE_DEVICE)
1419:   PetscLogDouble     cct, gct, csz, gsz, gmaxt, gflops, gflopr, fracgflops;
1420:   #endif
1421:   PetscMPIInt        minC, maxC;
1422:   PetscMPIInt        size, rank;
1423:   PetscBool          *localStageUsed,    *stageUsed;
1424:   PetscBool          *localStageVisible, *stageVisible;
1425:   int                numStages, localNumEvents, numEvents;
1426:   int                stage, oclass;
1427:   PetscLogEvent      event;
1428:   PetscErrorCode     ierr;
1429:   char               version[256];
1430:   MPI_Comm           comm;

1432:   PetscObjectGetComm((PetscObject)viewer,&comm);
1433:   PetscViewerASCIIGetPointer(viewer,&fd);
1434:   MPI_Comm_size(comm, &size);
1435:   MPI_Comm_rank(comm, &rank);
1436:   /* Get the total elapsed time */
1437:   PetscTime(&locTotalTime);  locTotalTime -= petsc_BaseTime;

1439:   PetscFPrintf(comm, fd, "**************************************** ***********************************************************************************************************************\n");
1440:   PetscFPrintf(comm, fd, "***                                WIDEN YOUR WINDOW TO 160 CHARACTERS.  Use 'enscript -r -fCourier9' to print this document                                 ***\n");
1441:   PetscFPrintf(comm, fd, "****************************************************************************************************************************************************************\n");
1442:   PetscFPrintf(comm, fd, "\n------------------------------------------------------------------ PETSc Performance Summary: -------------------------------------------------------------------\n\n");
1443:   PetscLogViewWarnSync(comm,fd);
1444:   PetscLogViewWarnDebugging(comm,fd);
1445:   PetscLogViewWarnNoGpuAwareMpi(comm,fd);
1446:   PetscGetArchType(arch,sizeof(arch));
1447:   PetscGetHostName(hostname,sizeof(hostname));
1448:   PetscGetUserName(username,sizeof(username));
1449:   PetscGetProgramName(pname,sizeof(pname));
1450:   PetscGetDate(date,sizeof(date));
1451:   PetscGetVersion(version,sizeof(version));
1452:   if (size == 1) {
1453:     PetscFPrintf(comm,fd,"%s on a %s named %s with %d processor, by %s %s\n", pname, arch, hostname, size, username, date);
1454:   } else {
1455:     PetscFPrintf(comm,fd,"%s on a %s named %s with %d processors, by %s %s\n", pname, arch, hostname, size, username, date);
1456:   }
1457: #if defined(PETSC_HAVE_OPENMP)
1458:   PetscFPrintf(comm,fd,"Using %" PetscInt_FMT " OpenMP threads\n", PetscNumOMPThreads);
1459: #endif
1460:   PetscFPrintf(comm, fd, "Using %s\n", version);

1462:   /* Must preserve reduction count before we go on */
1463:   red = petsc_allreduce_ct + petsc_gather_ct + petsc_scatter_ct;

1465:   /* Calculate summary information */
1466:   PetscFPrintf(comm, fd, "\n                         Max       Max/Min     Avg       Total\n");
1467:   /*   Time */
1468:   MPI_Allreduce(&locTotalTime, &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm);
1469:   MPI_Allreduce(&locTotalTime, &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm);
1470:   MPI_Allreduce(&locTotalTime, &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1471:   avg  = tot/((PetscLogDouble) size);
1472:   if (min != 0.0) ratio = max/min; else ratio = 0.0;
1473:   PetscFPrintf(comm, fd, "Time (sec):           %5.3e   %7.3f   %5.3e\n", max, ratio, avg);
1474:   TotalTime = tot;
1475:   /*   Objects */
1476:   avg  = (PetscLogDouble) petsc_numObjects;
1477:   MPI_Allreduce(&avg,          &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm);
1478:   MPI_Allreduce(&avg,          &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm);
1479:   MPI_Allreduce(&avg,          &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1480:   avg  = tot/((PetscLogDouble) size);
1481:   if (min != 0.0) ratio = max/min; else ratio = 0.0;
1482:   PetscFPrintf(comm, fd, "Objects:              %5.3e   %7.3f   %5.3e\n", max, ratio, avg);
1483:   /*   Flops */
1484:   MPI_Allreduce(&petsc_TotalFlops,  &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm);
1485:   MPI_Allreduce(&petsc_TotalFlops,  &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm);
1486:   MPI_Allreduce(&petsc_TotalFlops,  &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1487:   avg  = tot/((PetscLogDouble) size);
1488:   if (min != 0.0) ratio = max/min; else ratio = 0.0;
1489:   PetscFPrintf(comm, fd, "Flops:                %5.3e   %7.3f   %5.3e  %5.3e\n", max, ratio, avg, tot);
1490:   TotalFlops = tot;
1491:   /*   Flops/sec -- Must talk to Barry here */
1492:   if (locTotalTime != 0.0) flops = petsc_TotalFlops/locTotalTime; else flops = 0.0;
1493:   MPI_Allreduce(&flops,        &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm);
1494:   MPI_Allreduce(&flops,        &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm);
1495:   MPI_Allreduce(&flops,        &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1496:   avg  = tot/((PetscLogDouble) size);
1497:   if (min != 0.0) ratio = max/min; else ratio = 0.0;
1498:   PetscFPrintf(comm, fd, "Flops/sec:            %5.3e   %7.3f   %5.3e  %5.3e\n", max, ratio, avg, tot);
1499:   /*   Memory */
1500:   PetscMallocGetMaximumUsage(&mem);
1501:   if (mem > 0.0) {
1502:     MPI_Allreduce(&mem,          &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm);
1503:     MPI_Allreduce(&mem,          &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm);
1504:     MPI_Allreduce(&mem,          &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1505:     avg  = tot/((PetscLogDouble) size);
1506:     if (min != 0.0) ratio = max/min; else ratio = 0.0;
1507:     PetscFPrintf(comm, fd, "Memory (bytes):       %5.3e   %7.3f   %5.3e  %5.3e\n", max, ratio, avg, tot);
1508:   }
1509:   /*   Messages */
1510:   mess = 0.5*(petsc_irecv_ct + petsc_isend_ct + petsc_recv_ct + petsc_send_ct);
1511:   MPI_Allreduce(&mess,         &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm);
1512:   MPI_Allreduce(&mess,         &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm);
1513:   MPI_Allreduce(&mess,         &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1514:   avg  = tot/((PetscLogDouble) size);
1515:   if (min != 0.0) ratio = max/min; else ratio = 0.0;
1516:   PetscFPrintf(comm, fd, "MPI Msg Count:        %5.3e   %7.3f   %5.3e  %5.3e\n", max, ratio, avg, tot);
1517:   numMessages = tot;
1518:   /*   Message Lengths */
1519:   mess = 0.5*(petsc_irecv_len + petsc_isend_len + petsc_recv_len + petsc_send_len);
1520:   MPI_Allreduce(&mess,         &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm);
1521:   MPI_Allreduce(&mess,         &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm);
1522:   MPI_Allreduce(&mess,         &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1523:   if (numMessages != 0) avg = tot/numMessages; else avg = 0.0;
1524:   if (min != 0.0) ratio = max/min; else ratio = 0.0;
1525:   PetscFPrintf(comm, fd, "MPI Msg Len (bytes):  %5.3e   %7.3f   %5.3e  %5.3e\n", max, ratio, avg, tot);
1526:   messageLength = tot;
1527:   /*   Reductions */
1528:   MPI_Allreduce(&red,          &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm);
1529:   MPI_Allreduce(&red,          &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm);
1530:   MPI_Allreduce(&red,          &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1531:   if (min != 0.0) ratio = max/min; else ratio = 0.0;
1532:   PetscFPrintf(comm, fd, "MPI Reductions:       %5.3e   %7.3f\n", max, ratio);
1533:   numReductions = red; /* wrong because uses count from process zero */
1534:   PetscFPrintf(comm, fd, "\nFlop counting convention: 1 flop = 1 real number operation of type (multiply/divide/add/subtract)\n");
1535:   PetscFPrintf(comm, fd, "                            e.g., VecAXPY() for real vectors of length N --> 2N flops\n");
1536:   PetscFPrintf(comm, fd, "                            and VecAXPY() for complex vectors of length N --> 8N flops\n");

1538:   /* Get total number of stages --
1539:        Currently, a single processor can register more stages than another, but stages must all be registered in order.
1540:        We can removed this requirement if necessary by having a global stage numbering and indirection on the stage ID.
1541:        This seems best accomplished by assoicating a communicator with each stage.
1542:   */
1543:   PetscLogGetStageLog(&stageLog);
1544:   MPI_Allreduce(&stageLog->numStages, &numStages, 1, MPI_INT, MPI_MAX, comm);
1545:   PetscMalloc1(numStages, &localStageUsed);
1546:   PetscMalloc1(numStages, &stageUsed);
1547:   PetscMalloc1(numStages, &localStageVisible);
1548:   PetscMalloc1(numStages, &stageVisible);
1549:   if (numStages > 0) {
1550:     stageInfo = stageLog->stageInfo;
1551:     for (stage = 0; stage < numStages; stage++) {
1552:       if (stage < stageLog->numStages) {
1553:         localStageUsed[stage]    = stageInfo[stage].used;
1554:         localStageVisible[stage] = stageInfo[stage].perfInfo.visible;
1555:       } else {
1556:         localStageUsed[stage]    = PETSC_FALSE;
1557:         localStageVisible[stage] = PETSC_TRUE;
1558:       }
1559:     }
1560:     MPI_Allreduce(localStageUsed,    stageUsed,    numStages, MPIU_BOOL, MPI_LOR,  comm);
1561:     MPI_Allreduce(localStageVisible, stageVisible, numStages, MPIU_BOOL, MPI_LAND, comm);
1562:     for (stage = 0; stage < numStages; stage++) {
1563:       if (stageUsed[stage]) {
1564:         PetscFPrintf(comm, fd, "\nSummary of Stages:   ----- Time ------  ----- Flop ------  --- Messages ---  -- Message Lengths --  -- Reductions --\n");
1565:         PetscFPrintf(comm, fd, "                        Avg     %%Total     Avg     %%Total    Count   %%Total     Avg         %%Total    Count   %%Total\n");
1566:         break;
1567:       }
1568:     }
1569:     for (stage = 0; stage < numStages; stage++) {
1570:       if (!stageUsed[stage]) continue;
1571:       /* CANNOT use MPI_Allreduce() since it might fail the line number check */
1572:       if (localStageUsed[stage]) {
1573:         MPI_Allreduce(&stageInfo[stage].perfInfo.time,          &stageTime, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1574:         MPI_Allreduce(&stageInfo[stage].perfInfo.flops,         &flops,     1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1575:         MPI_Allreduce(&stageInfo[stage].perfInfo.numMessages,   &mess,      1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1576:         MPI_Allreduce(&stageInfo[stage].perfInfo.messageLength, &messLen,   1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1577:         MPI_Allreduce(&stageInfo[stage].perfInfo.numReductions, &red,       1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1578:         name = stageInfo[stage].name;
1579:       } else {
1580:         MPI_Allreduce(&zero,                           &stageTime, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1581:         MPI_Allreduce(&zero,                           &flops,     1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1582:         MPI_Allreduce(&zero,                           &mess,      1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1583:         MPI_Allreduce(&zero,                           &messLen,   1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1584:         MPI_Allreduce(&zero,                           &red,       1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1585:         name = "";
1586:       }
1587:       mess *= 0.5; messLen *= 0.5; red /= size;
1588:       if (TotalTime     != 0.0) fracTime       = stageTime/TotalTime;    else fracTime       = 0.0;
1589:       if (TotalFlops    != 0.0) fracFlops      = flops/TotalFlops;       else fracFlops      = 0.0;
1590:       /* Talk to Barry if (stageTime     != 0.0) flops          = (size*flops)/stageTime; else flops          = 0.0; */
1591:       if (numMessages   != 0.0) fracMessages   = mess/numMessages;       else fracMessages   = 0.0;
1592:       if (mess          != 0.0) avgMessLen     = messLen/mess;           else avgMessLen     = 0.0;
1593:       if (messageLength != 0.0) fracLength     = messLen/messageLength;  else fracLength     = 0.0;
1594:       if (numReductions != 0.0) fracReductions = red/numReductions;      else fracReductions = 0.0;
1595:       PetscFPrintf(comm, fd, "%2d: %15s: %6.4e %5.1f%%  %6.4e %5.1f%%  %5.3e %5.1f%%  %5.3e      %5.1f%%  %5.3e %5.1f%%\n",
1596:                           stage, name, stageTime/size, 100.0*fracTime, flops, 100.0*fracFlops,
1597:                           mess, 100.0*fracMessages, avgMessLen, 100.0*fracLength, red, 100.0*fracReductions);
1598:     }
1599:   }

1601:   PetscFPrintf(comm, fd,"\n------------------------------------------------------------------------------------------------------------------------\n");
1602:   PetscFPrintf(comm, fd, "See the 'Profiling' chapter of the users' manual for details on interpreting output.\n");
1603:   PetscFPrintf(comm, fd, "Phase summary info:\n");
1604:   PetscFPrintf(comm, fd, "   Count: number of times phase was executed\n");
1605:   PetscFPrintf(comm, fd, "   Time and Flop: Max - maximum over all processors\n");
1606:   PetscFPrintf(comm, fd, "                  Ratio - ratio of maximum to minimum over all processors\n");
1607:   PetscFPrintf(comm, fd, "   Mess: number of messages sent\n");
1608:   PetscFPrintf(comm, fd, "   AvgLen: average message length (bytes)\n");
1609:   PetscFPrintf(comm, fd, "   Reduct: number of global reductions\n");
1610:   PetscFPrintf(comm, fd, "   Global: entire computation\n");
1611:   PetscFPrintf(comm, fd, "   Stage: stages of a computation. Set stages with PetscLogStagePush() and PetscLogStagePop().\n");
1612:   PetscFPrintf(comm, fd, "      %%T - percent time in this phase         %%F - percent flop in this phase\n");
1613:   PetscFPrintf(comm, fd, "      %%M - percent messages in this phase     %%L - percent message lengths in this phase\n");
1614:   PetscFPrintf(comm, fd, "      %%R - percent reductions in this phase\n");
1615:   PetscFPrintf(comm, fd, "   Total Mflop/s: 10e-6 * (sum of flop over all processors)/(max time over all processors)\n");
1616:   if (PetscLogMemory) {
1617:     PetscFPrintf(comm, fd, "   Malloc Mbytes: Memory allocated and kept during event (sum over all calls to event)\n");
1618:     PetscFPrintf(comm, fd, "   EMalloc Mbytes: extra memory allocated during event and then freed (maximum over all calls to events)\n");
1619:     PetscFPrintf(comm, fd, "   MMalloc Mbytes: Increase in high water mark of allocated memory (sum over all calls to event)\n");
1620:     PetscFPrintf(comm, fd, "   RMI Mbytes: Increase in resident memory (sum over all calls to event)\n");
1621:   }
1622:   #if defined(PETSC_HAVE_DEVICE)
1623:   PetscFPrintf(comm, fd, "   GPU Mflop/s: 10e-6 * (sum of flop on GPU over all processors)/(max GPU time over all processors)\n");
1624:   PetscFPrintf(comm, fd, "   CpuToGpu Count: total number of CPU to GPU copies per processor\n");
1625:   PetscFPrintf(comm, fd, "   CpuToGpu Size (Mbytes): 10e-6 * (total size of CPU to GPU copies per processor)\n");
1626:   PetscFPrintf(comm, fd, "   GpuToCpu Count: total number of GPU to CPU copies per processor\n");
1627:   PetscFPrintf(comm, fd, "   GpuToCpu Size (Mbytes): 10e-6 * (total size of GPU to CPU copies per processor)\n");
1628:   PetscFPrintf(comm, fd, "   GPU %%F: percent flops on GPU in this event\n");
1629:   #endif
1630:   PetscFPrintf(comm, fd, "------------------------------------------------------------------------------------------------------------------------\n");

1632:   PetscLogViewWarnDebugging(comm,fd);

1634:   /* Report events */
1635:   PetscFPrintf(comm, fd,"Event                Count      Time (sec)     Flop                              --- Global ---  --- Stage ----  Total");
1636:   if (PetscLogMemory) {
1637:     PetscFPrintf(comm, fd,"  Malloc EMalloc MMalloc RMI");
1638:   }
1639:   #if defined(PETSC_HAVE_DEVICE)
1640:   PetscFPrintf(comm, fd,"   GPU    - CpuToGpu -   - GpuToCpu - GPU");
1641:   #endif
1642:   PetscFPrintf(comm, fd,"\n");
1643:   PetscFPrintf(comm, fd,"                   Max Ratio  Max     Ratio   Max  Ratio  Mess   AvgLen  Reduct  %%T %%F %%M %%L %%R  %%T %%F %%M %%L %%R Mflop/s");
1644:   if (PetscLogMemory) {
1645:     PetscFPrintf(comm, fd," Mbytes Mbytes Mbytes Mbytes");
1646:   }
1647:   #if defined(PETSC_HAVE_DEVICE)
1648:   PetscFPrintf(comm, fd," Mflop/s Count   Size   Count   Size  %%F");
1649:   #endif
1650:   PetscFPrintf(comm, fd,"\n");
1651:   PetscFPrintf(comm, fd,"------------------------------------------------------------------------------------------------------------------------");
1652:   if (PetscLogMemory) {
1653:     PetscFPrintf(comm, fd,"-----------------------------");
1654:   }
1655:   #if defined(PETSC_HAVE_DEVICE)
1656:   PetscFPrintf(comm, fd,"---------------------------------------");
1657:   #endif
1658:   PetscFPrintf(comm, fd,"\n");

1660:   /* Problem: The stage name will not show up unless the stage executed on proc 1 */
1661:   for (stage = 0; stage < numStages; stage++) {
1662:     if (!stageVisible[stage]) continue;
1663:     /* CANNOT use MPI_Allreduce() since it might fail the line number check */
1664:     if (localStageUsed[stage]) {
1665:       PetscFPrintf(comm, fd, "\n--- Event Stage %d: %s\n\n", stage, stageInfo[stage].name);
1666:       MPI_Allreduce(&stageInfo[stage].perfInfo.time,          &stageTime, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1667:       MPI_Allreduce(&stageInfo[stage].perfInfo.flops,         &flops,     1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1668:       MPI_Allreduce(&stageInfo[stage].perfInfo.numMessages,   &mess,      1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1669:       MPI_Allreduce(&stageInfo[stage].perfInfo.messageLength, &messLen,   1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1670:       MPI_Allreduce(&stageInfo[stage].perfInfo.numReductions, &red,       1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1671:     } else {
1672:       PetscFPrintf(comm, fd, "\n--- Event Stage %d: Unknown\n\n", stage);
1673:       MPI_Allreduce(&zero,                           &stageTime, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1674:       MPI_Allreduce(&zero,                           &flops,     1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1675:       MPI_Allreduce(&zero,                           &mess,      1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1676:       MPI_Allreduce(&zero,                           &messLen,   1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1677:       MPI_Allreduce(&zero,                           &red,       1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1678:     }
1679:     mess *= 0.5; messLen *= 0.5; red /= size;

1681:     /* Get total number of events in this stage --
1682:        Currently, a single processor can register more events than another, but events must all be registered in order,
1683:        just like stages. We can removed this requirement if necessary by having a global event numbering and indirection
1684:        on the event ID. This seems best accomplished by associating a communicator with each stage.

1686:        Problem: If the event did not happen on proc 1, its name will not be available.
1687:        Problem: Event visibility is not implemented
1688:     */
1689:     if (localStageUsed[stage]) {
1690:       eventInfo      = stageLog->stageInfo[stage].eventLog->eventInfo;
1691:       localNumEvents = stageLog->stageInfo[stage].eventLog->numEvents;
1692:     } else localNumEvents = 0;
1693:     MPI_Allreduce(&localNumEvents, &numEvents, 1, MPI_INT, MPI_MAX, comm);
1694:     for (event = 0; event < numEvents; event++) {
1695:       /* CANNOT use MPI_Allreduce() since it might fail the line number check */
1696:       if (localStageUsed[stage] && (event < stageLog->stageInfo[stage].eventLog->numEvents) && (eventInfo[event].depth == 0)) {
1697:         if ((eventInfo[event].count > 0) && (eventInfo[event].time > 0.0)) flopr = eventInfo[event].flops; else flopr = 0.0;
1698:         MPI_Allreduce(&flopr,                          &minf,  1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm);
1699:         MPI_Allreduce(&flopr,                          &maxf,  1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm);
1700:         MPI_Allreduce(&eventInfo[event].flops,         &totf,  1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1701:         MPI_Allreduce(&eventInfo[event].time,          &mint,  1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm);
1702:         MPI_Allreduce(&eventInfo[event].time,          &maxt,  1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm);
1703:         MPI_Allreduce(&eventInfo[event].time,          &tott,  1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1704:         MPI_Allreduce(&eventInfo[event].numMessages,   &totm,  1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1705:         MPI_Allreduce(&eventInfo[event].messageLength, &totml, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1706:         MPI_Allreduce(&eventInfo[event].numReductions, &totr,  1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1707:         MPI_Allreduce(&eventInfo[event].count,         &minC,  1, MPI_INT,             MPI_MIN, comm);
1708:         MPI_Allreduce(&eventInfo[event].count,         &maxC,  1, MPI_INT,             MPI_MAX, comm);
1709:         if (PetscLogMemory) {
1710:           MPI_Allreduce(&eventInfo[event].memIncrease,    &mem,   1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1711:           MPI_Allreduce(&eventInfo[event].mallocSpace,    &mal,   1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1712:           MPI_Allreduce(&eventInfo[event].mallocIncrease, &malmax,1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1713:           MPI_Allreduce(&eventInfo[event].mallocIncreaseEvent, &emalmax,1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1714:         }
1715:         #if defined(PETSC_HAVE_DEVICE)
1716:         MPI_Allreduce(&eventInfo[event].CpuToGpuCount,    &cct,   1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1717:         MPI_Allreduce(&eventInfo[event].GpuToCpuCount,    &gct,   1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1718:         MPI_Allreduce(&eventInfo[event].CpuToGpuSize,     &csz,   1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1719:         MPI_Allreduce(&eventInfo[event].GpuToCpuSize,     &gsz,   1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1720:         MPI_Allreduce(&eventInfo[event].GpuFlops,         &gflops,1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1721:         MPI_Allreduce(&eventInfo[event].GpuTime,          &gmaxt ,1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm);
1722:         #endif
1723:         name = stageLog->eventLog->eventInfo[event].name;
1724:       } else {
1725:         flopr = 0.0;
1726:         MPI_Allreduce(&flopr,                         &minf,  1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm);
1727:         MPI_Allreduce(&flopr,                         &maxf,  1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm);
1728:         MPI_Allreduce(&zero,                          &totf,  1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1729:         MPI_Allreduce(&zero,                          &mint,  1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm);
1730:         MPI_Allreduce(&zero,                          &maxt,  1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm);
1731:         MPI_Allreduce(&zero,                          &tott,  1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1732:         MPI_Allreduce(&zero,                          &totm,  1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1733:         MPI_Allreduce(&zero,                          &totml, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1734:         MPI_Allreduce(&zero,                          &totr,  1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1735:         MPI_Allreduce(&ierr,                          &minC,  1, MPI_INT,             MPI_MIN, comm);
1736:         MPI_Allreduce(&ierr,                          &maxC,  1, MPI_INT,             MPI_MAX, comm);
1737:         if (PetscLogMemory) {
1738:           MPI_Allreduce(&zero,                        &mem,    1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1739:           MPI_Allreduce(&zero,                        &mal,    1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1740:           MPI_Allreduce(&zero,                        &malmax, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1741:           MPI_Allreduce(&zero,                        &emalmax,1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1742:         }
1743:         #if defined(PETSC_HAVE_DEVICE)
1744:         MPI_Allreduce(&zero,                          &cct,    1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1745:         MPI_Allreduce(&zero,                          &gct,    1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1746:         MPI_Allreduce(&zero,                          &csz,    1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1747:         MPI_Allreduce(&zero,                          &gsz,    1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1748:         MPI_Allreduce(&zero,                          &gflops, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);
1749:         MPI_Allreduce(&zero,                          &gmaxt , 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm);
1750:         #endif
1751:         name  = "";
1752:       }
1753:       if (mint < 0.0) {
1754:         PetscFPrintf(comm, fd, "WARNING!!! Minimum time %g over all processors for %s is negative! This happens\n on some machines whose times cannot handle too rapid calls.!\n artificially changing minimum to zero.\n",mint,name);
1755:         mint = 0;
1756:       }
1758:       totm *= 0.5; totml *= 0.5; totr /= size;

1760:       if (maxC != 0) {
1761:         if (minC          != 0)   ratC             = ((PetscLogDouble)maxC)/minC;else ratC             = 0.0;
1762:         if (mint          != 0.0) ratt             = maxt/mint;                  else ratt             = 0.0;
1763:         if (minf          != 0.0) ratf             = maxf/minf;                  else ratf             = 0.0;
1764:         if (TotalTime     != 0.0) fracTime         = tott/TotalTime;             else fracTime         = 0.0;
1765:         if (TotalFlops    != 0.0) fracFlops        = totf/TotalFlops;            else fracFlops        = 0.0;
1766:         if (stageTime     != 0.0) fracStageTime    = tott/stageTime;             else fracStageTime    = 0.0;
1767:         if (flops         != 0.0) fracStageFlops   = totf/flops;                 else fracStageFlops   = 0.0;
1768:         if (numMessages   != 0.0) fracMess         = totm/numMessages;           else fracMess         = 0.0;
1769:         if (messageLength != 0.0) fracMessLen      = totml/messageLength;        else fracMessLen      = 0.0;
1770:         if (numReductions != 0.0) fracRed          = totr/numReductions;         else fracRed          = 0.0;
1771:         if (mess          != 0.0) fracStageMess    = totm/mess;                  else fracStageMess    = 0.0;
1772:         if (messLen       != 0.0) fracStageMessLen = totml/messLen;              else fracStageMessLen = 0.0;
1773:         if (red           != 0.0) fracStageRed     = totr/red;                   else fracStageRed     = 0.0;
1774:         if (totm          != 0.0) totml           /= totm;                       else totml            = 0.0;
1775:         if (maxt          != 0.0) flopr            = totf/maxt;                  else flopr            = 0.0;
1776:         if (fracStageTime > 1.00) PetscFPrintf(comm, fd,"Warning -- total time of event greater than time of entire stage -- something is wrong with the timer\n");
1777:         PetscFPrintf(comm, fd,
1778:                             "%-16s %7d%4.1f %5.4e%4.1f %3.2e%4.1f %2.1e %2.1e %2.1e%3.0f%3.0f%3.0f%3.0f%3.0f %3.0f%3.0f%3.0f%3.0f%3.0f %5.0f",
1779:                             name, maxC, ratC, maxt, ratt, maxf, ratf, totm, totml, totr,
1780:                             100.0*fracTime, 100.0*fracFlops, 100.0*fracMess, 100.0*fracMessLen, 100.0*fracRed,
1781:                             100.0*fracStageTime, 100.0*fracStageFlops, 100.0*fracStageMess, 100.0*fracStageMessLen, 100.0*fracStageRed,
1782:                             PetscAbs(flopr)/1.0e6);
1783:         if (PetscLogMemory) {
1784:           PetscFPrintf(comm, fd," %5.0f   %5.0f   %5.0f   %5.0f",mal/1.0e6,emalmax/1.0e6,malmax/1.0e6,mem/1.0e6);
1785:         }
1786:         #if defined(PETSC_HAVE_DEVICE)
1787:         if (totf  != 0.0) fracgflops = gflops/totf;  else fracgflops = 0.0;
1788:         if (gmaxt != 0.0) gflopr     = gflops/gmaxt; else gflopr     = 0.0;
1789:         PetscFPrintf(comm, fd,"   %5.0f   %4.0f %3.2e %4.0f %3.2e% 3.0f",PetscAbs(gflopr)/1.0e6,cct/size,csz/(1.0e6*size),gct/size,gsz/(1.0e6*size),100.0*fracgflops);
1790:         #endif
1791:         PetscFPrintf(comm, fd,"\n");
1792:       }
1793:     }
1794:   }

1796:   /* Memory usage and object creation */
1797:   PetscFPrintf(comm, fd, "------------------------------------------------------------------------------------------------------------------------");
1798:   if (PetscLogMemory) {
1799:     PetscFPrintf(comm, fd, "-----------------------------");
1800:   }
1801:   #if defined(PETSC_HAVE_DEVICE)
1802:   PetscFPrintf(comm, fd, "---------------------------------------");
1803:   #endif
1804:   PetscFPrintf(comm, fd, "\n");
1805:   PetscFPrintf(comm, fd, "\n");
1806:   PetscFPrintf(comm, fd, "Memory usage is given in bytes:\n\n");

1808:   /* Right now, only stages on the first processor are reported here, meaning only objects associated with
1809:      the global communicator, or MPI_COMM_SELF for proc 1. We really should report global stats and then
1810:      stats for stages local to processor sets.
1811:   */
1812:   /* We should figure out the longest object name here (now 20 characters) */
1813:   PetscFPrintf(comm, fd, "Object Type          Creations   Destructions     Memory  Descendants' Mem.\n");
1814:   PetscFPrintf(comm, fd, "Reports information only for process 0.\n");
1815:   for (stage = 0; stage < numStages; stage++) {
1816:     if (localStageUsed[stage]) {
1817:       classInfo = stageLog->stageInfo[stage].classLog->classInfo;
1818:       PetscFPrintf(comm, fd, "\n--- Event Stage %d: %s\n\n", stage, stageInfo[stage].name);
1819:       for (oclass = 0; oclass < stageLog->stageInfo[stage].classLog->numClasses; oclass++) {
1820:         if ((classInfo[oclass].creations > 0) || (classInfo[oclass].destructions > 0)) {
1821:           PetscFPrintf(comm, fd, "%20s %5d          %5d  %11.0f     %g\n", stageLog->classLog->classInfo[oclass].name,
1822:                               classInfo[oclass].creations, classInfo[oclass].destructions, classInfo[oclass].mem,
1823:                               classInfo[oclass].descMem);
1824:         }
1825:       }
1826:     } else {
1827:       if (!localStageVisible[stage]) continue;
1828:       PetscFPrintf(comm, fd, "\n--- Event Stage %d: Unknown\n\n", stage);
1829:     }
1830:   }

1832:   PetscFree(localStageUsed);
1833:   PetscFree(stageUsed);
1834:   PetscFree(localStageVisible);
1835:   PetscFree(stageVisible);

1837:   /* Information unrelated to this particular run */
1838:   PetscFPrintf(comm, fd, "========================================================================================================================\n");
1839:   PetscTime(&y);
1840:   PetscTime(&x);
1841:   PetscTime(&y); PetscTime(&y); PetscTime(&y); PetscTime(&y); PetscTime(&y);
1842:   PetscTime(&y); PetscTime(&y); PetscTime(&y); PetscTime(&y); PetscTime(&y);
1843:   PetscFPrintf(comm,fd,"Average time to get PetscTime(): %g\n", (y-x)/10.0);
1844:   /* MPI information */
1845:   if (size > 1) {
1846:     MPI_Status  status;
1847:     PetscMPIInt tag;
1848:     MPI_Comm    newcomm;

1850:     MPI_Barrier(comm);
1851:     PetscTime(&x);
1852:     MPI_Barrier(comm);
1853:     MPI_Barrier(comm);
1854:     MPI_Barrier(comm);
1855:     MPI_Barrier(comm);
1856:     MPI_Barrier(comm);
1857:     PetscTime(&y);
1858:     PetscFPrintf(comm, fd, "Average time for MPI_Barrier(): %g\n", (y-x)/5.0);
1859:     PetscCommDuplicate(comm,&newcomm, &tag);
1860:     MPI_Barrier(comm);
1861:     if (rank) {
1862:       MPI_Recv(NULL, 0, MPI_INT, rank-1,            tag, newcomm, &status);
1863:       MPI_Send(NULL, 0, MPI_INT, (rank+1)%size, tag, newcomm);
1864:     } else {
1865:       PetscTime(&x);
1866:       MPI_Send(NULL, 0, MPI_INT, 1,          tag, newcomm);
1867:       MPI_Recv(NULL, 0, MPI_INT, size-1, tag, newcomm, &status);
1868:       PetscTime(&y);
1869:       PetscFPrintf(comm,fd,"Average time for zero size MPI_Send(): %g\n", (y-x)/size);
1870:     }
1871:     PetscCommDestroy(&newcomm);
1872:   }
1873:   PetscOptionsView(NULL,viewer);

1875:   /* Machine and compile information */
1876: #if defined(PETSC_USE_FORTRAN_KERNELS)
1877:   PetscFPrintf(comm, fd, "Compiled with FORTRAN kernels\n");
1878: #else
1879:   PetscFPrintf(comm, fd, "Compiled without FORTRAN kernels\n");
1880: #endif
1881: #if defined(PETSC_USE_64BIT_INDICES)
1882:   PetscFPrintf(comm, fd, "Compiled with 64 bit PetscInt\n");
1883: #elif defined(PETSC_USE___FLOAT128)
1884:   PetscFPrintf(comm, fd, "Compiled with 32 bit PetscInt\n");
1885: #endif
1886: #if defined(PETSC_USE_REAL_SINGLE)
1887:   PetscFPrintf(comm, fd, "Compiled with single precision PetscScalar and PetscReal\n");
1888: #elif defined(PETSC_USE___FLOAT128)
1889:   PetscFPrintf(comm, fd, "Compiled with 128 bit precision PetscScalar and PetscReal\n");
1890: #endif
1891: #if defined(PETSC_USE_REAL_MAT_SINGLE)
1892:   PetscFPrintf(comm, fd, "Compiled with single precision matrices\n");
1893: #else
1894:   PetscFPrintf(comm, fd, "Compiled with full precision matrices (default)\n");
1895: #endif
1896:   PetscFPrintf(comm, fd, "sizeof(short) %d sizeof(int) %d sizeof(long) %d sizeof(void*) %d sizeof(PetscScalar) %d sizeof(PetscInt) %d\n",
1897:                       (int) sizeof(short), (int) sizeof(int), (int) sizeof(long), (int) sizeof(void*),(int) sizeof(PetscScalar),(int) sizeof(PetscInt));

1899:   PetscFPrintf(comm, fd, "Configure options: %s",petscconfigureoptions);
1900:   PetscFPrintf(comm, fd, "%s", petscmachineinfo);
1901:   PetscFPrintf(comm, fd, "%s", petsccompilerinfo);
1902:   PetscFPrintf(comm, fd, "%s", petsccompilerflagsinfo);
1903:   PetscFPrintf(comm, fd, "%s", petsclinkerinfo);

1905:   /* Cleanup */
1906:   PetscFPrintf(comm, fd, "\n");
1907:   PetscLogViewWarnNoGpuAwareMpi(comm,fd);
1908:   PetscLogViewWarnDebugging(comm,fd);
1909:   return 0;
1910: }

1912: /*@C
1913:   PetscLogView - Prints a summary of the logging.

1915:   Collective over MPI_Comm

1917:   Input Parameter:
1918: .  viewer - an ASCII viewer

1920:   Options Database Keys:
1921: +  -log_view [:filename] - Prints summary of log information
1922: .  -log_view :filename.py:ascii_info_detail - Saves logging information from each process as a Python file
1923: .  -log_view :filename.xml:ascii_xml - Saves a summary of the logging information in a nested format (see below for how to view it)
1924: .  -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)
1925: .  -log_all - Saves a file Log.rank for each MPI process with details of each step of the computation
1926: -  -log_trace [filename] - Displays a trace of what each process is doing

1928:   Notes:
1929:   It is possible to control the logging programatically but we recommend using the options database approach whenever possible
1930:   By default the summary is printed to stdout.

1932:   Before calling this routine you must have called either PetscLogDefaultBegin() or PetscLogNestedBegin()

1934:   If PETSc is configured with --with-logging=0 then this functionality is not available

1936:   To view the nested XML format filename.xml first copy  ${PETSC_DIR}/share/petsc/xml/performance_xml2html.xsl to the current
1937:   directory then open filename.xml with your browser. Specific notes for certain browsers
1938: $    Firefox and Internet explorer - simply open the file
1939: $    Google Chrome - you must start up Chrome with the option --allow-file-access-from-files
1940: $    Safari - see https://ccm.net/faq/36342-safari-how-to-enable-local-file-access
1941:   or one can use the package http://xmlsoft.org/XSLT/xsltproc2.html to translate the xml file to html and then open it with
1942:   your browser.
1943:   Alternatively, use the script ${PETSC_DIR}/lib/petsc/bin/petsc-performance-view to automatically open a new browser
1944:   window and render the XML log file contents.

1946:   The nested XML format was kindly donated by Koos Huijssen and Christiaan M. Klaij  MARITIME  RESEARCH  INSTITUTE  NETHERLANDS

1948:   The Flame Graph output can be visualised using either the original Flame Graph script (https://github.com/brendangregg/FlameGraph)
1949:   or using speedscope (https://www.speedscope.app).
1950:   Old XML profiles may be converted into this format using the script ${PETSC_DIR}/lib/petsc/bin/xml2flamegraph.py.

1952:   Level: beginner

1954: .seealso: PetscLogDefaultBegin(), PetscLogDump()
1955: @*/
1956: PetscErrorCode  PetscLogView(PetscViewer viewer)
1957: {
1958:   PetscBool         isascii;
1959:   PetscViewerFormat format;
1960:   int               stage, lastStage;
1961:   PetscStageLog     stageLog;

1964:   /* Pop off any stages the user forgot to remove */
1965:   lastStage = 0;
1966:   PetscLogGetStageLog(&stageLog);
1967:   PetscStageLogGetCurrent(stageLog, &stage);
1968:   while (stage >= 0) {
1969:     lastStage = stage;
1970:     PetscStageLogPop(stageLog);
1971:     PetscStageLogGetCurrent(stageLog, &stage);
1972:   }
1973:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
1975:   PetscViewerGetFormat(viewer,&format);
1976:   if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO) {
1977:     PetscLogView_Default(viewer);
1978:   } else if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
1979:     PetscLogView_Detailed(viewer);
1980:   } else if (format == PETSC_VIEWER_ASCII_CSV) {
1981:     PetscLogView_CSV(viewer);
1982:   } else if (format == PETSC_VIEWER_ASCII_XML) {
1983:     PetscLogView_Nested(viewer);
1984:   } else if (format == PETSC_VIEWER_ASCII_FLAMEGRAPH) {
1985:     PetscLogView_Flamegraph(viewer);
1986:   }
1987:   PetscStageLogPush(stageLog, lastStage);
1988:   return 0;
1989: }

1991: /*@C
1992:   PetscLogViewFromOptions - Processes command line options to determine if/how a PetscLog is to be viewed.

1994:   Collective on PETSC_COMM_WORLD

1996:   Not normally called by user

1998:   Level: intermediate

2000: @*/
2001: PetscErrorCode PetscLogViewFromOptions(void)
2002: {
2003:   PetscViewer       viewer;
2004:   PetscBool         flg;
2005:   PetscViewerFormat format;

2007:   PetscOptionsGetViewer(PETSC_COMM_WORLD,NULL,NULL,"-log_view",&viewer,&format,&flg);
2008:   if (flg) {
2009:     PetscViewerPushFormat(viewer,format);
2010:     PetscLogView(viewer);
2011:     PetscViewerPopFormat(viewer);
2012:     PetscViewerDestroy(&viewer);
2013:   }
2014:   return 0;
2015: }

2017: /*----------------------------------------------- Counter Functions -------------------------------------------------*/
2018: /*@C
2019:    PetscGetFlops - Returns the number of flops used on this processor
2020:    since the program began.

2022:    Not Collective

2024:    Output Parameter:
2025:    flops - number of floating point operations

2027:    Notes:
2028:    A global counter logs all PETSc flop counts.  The user can use
2029:    PetscLogFlops() to increment this counter to include flops for the
2030:    application code.

2032:    Level: intermediate

2034: .seealso: PetscTime(), PetscLogFlops()
2035: @*/
2036: PetscErrorCode  PetscGetFlops(PetscLogDouble *flops)
2037: {
2038:   *flops = petsc_TotalFlops;
2039:   return 0;
2040: }

2042: PetscErrorCode  PetscLogObjectState(PetscObject obj, const char format[], ...)
2043: {
2044:   size_t         fullLength;
2045:   va_list        Argp;

2047:   if (!petsc_logObjects) return 0;
2048:   va_start(Argp, format);
2049:   PetscVSNPrintf(petsc_objects[obj->id].info, 64,format,&fullLength, Argp);
2050:   va_end(Argp);
2051:   return 0;
2052: }

2054: /*MC
2055:    PetscLogFlops - Adds floating point operations to the global counter.

2057:    Synopsis:
2058: #include <petsclog.h>
2059:    PetscErrorCode PetscLogFlops(PetscLogDouble f)

2061:    Not Collective

2063:    Input Parameter:
2064: .  f - flop counter

2066:    Usage:
2067: .vb
2068:      PetscLogEvent USER_EVENT;
2069:      PetscLogEventRegister("User event",0,&USER_EVENT);
2070:      PetscLogEventBegin(USER_EVENT,0,0,0,0);
2071:         [code segment to monitor]
2072:         PetscLogFlops(user_flops)
2073:      PetscLogEventEnd(USER_EVENT,0,0,0,0);
2074: .ve

2076:    Notes:
2077:    A global counter logs all PETSc flop counts.  The user can use
2078:    PetscLogFlops() to increment this counter to include flops for the
2079:    application code.

2081:    Level: intermediate

2083: .seealso: PetscLogEventRegister(), PetscLogEventBegin(), PetscLogEventEnd(), PetscGetFlops()

2085: M*/

2087: /*MC
2088:    PetscPreLoadBegin - Begin a segment of code that may be preloaded (run twice)
2089:     to get accurate timings

2091:    Synopsis:
2092: #include <petsclog.h>
2093:    void PetscPreLoadBegin(PetscBool  flag,char *name);

2095:    Not Collective

2097:    Input Parameters:
2098: +   flag - PETSC_TRUE to run twice, PETSC_FALSE to run once, may be overridden
2099:            with command line option -preload true or -preload false
2100: -   name - name of first stage (lines of code timed separately with -log_view) to
2101:            be preloaded

2103:    Usage:
2104: .vb
2105:      PetscPreLoadBegin(PETSC_TRUE,"first stage);
2106:        lines of code
2107:        PetscPreLoadStage("second stage");
2108:        lines of code
2109:      PetscPreLoadEnd();
2110: .ve

2112:    Notes:
2113:     Only works in C/C++, not Fortran

2115:      Flags available within the macro.
2116: +    PetscPreLoadingUsed - true if we are or have done preloading
2117: .    PetscPreLoadingOn - true if it is CURRENTLY doing preload
2118: .    PetscPreLoadIt - 0 for the first computation (with preloading turned off it is only 0) 1 for the second
2119: -    PetscPreLoadMax - number of times it will do the computation, only one when preloading is turned on
2120:      The first two variables are available throughout the program, the second two only between the PetscPreLoadBegin()
2121:      and PetscPreLoadEnd()

2123:    Level: intermediate

2125: .seealso: PetscLogEventRegister(), PetscLogEventBegin(), PetscLogEventEnd(), PetscPreLoadEnd(), PetscPreLoadStage()

2127: M*/

2129: /*MC
2130:    PetscPreLoadEnd - End a segment of code that may be preloaded (run twice)
2131:     to get accurate timings

2133:    Synopsis:
2134: #include <petsclog.h>
2135:    void PetscPreLoadEnd(void);

2137:    Not Collective

2139:    Usage:
2140: .vb
2141:      PetscPreLoadBegin(PETSC_TRUE,"first stage);
2142:        lines of code
2143:        PetscPreLoadStage("second stage");
2144:        lines of code
2145:      PetscPreLoadEnd();
2146: .ve

2148:    Notes:
2149:     only works in C/C++ not fortran

2151:    Level: intermediate

2153: .seealso: PetscLogEventRegister(), PetscLogEventBegin(), PetscLogEventEnd(), PetscPreLoadBegin(), PetscPreLoadStage()

2155: M*/

2157: /*MC
2158:    PetscPreLoadStage - Start a new segment of code to be timed separately.
2159:     to get accurate timings

2161:    Synopsis:
2162: #include <petsclog.h>
2163:    void PetscPreLoadStage(char *name);

2165:    Not Collective

2167:    Usage:
2168: .vb
2169:      PetscPreLoadBegin(PETSC_TRUE,"first stage);
2170:        lines of code
2171:        PetscPreLoadStage("second stage");
2172:        lines of code
2173:      PetscPreLoadEnd();
2174: .ve

2176:    Notes:
2177:     only works in C/C++ not fortran

2179:    Level: intermediate

2181: .seealso: PetscLogEventRegister(), PetscLogEventBegin(), PetscLogEventEnd(), PetscPreLoadBegin(), PetscPreLoadEnd()

2183: M*/

2185: #if PetscDefined(HAVE_DEVICE)
2186: #include <petsc/private/deviceimpl.h>

2188: /*-------------------------------------------- GPU event Functions ----------------------------------------------*/
2189: /*@C
2190:   PetscLogGpuTimeBegin - Start timer for device

2192:   Notes:
2193:     When CUDA or HIP is enabled, the timer is run on the GPU, it is a separate logging of time devoted to GPU computations (excluding kernel launch times).
2194:     When CUDA or HIP is not available, the timer is run on the CPU, it is a separate logging of time devoted to GPU computations (including kernel launch times).
2195:     There is no need to call WaitForCUDA() or WaitForHIP() between PetscLogGpuTimeBegin and PetscLogGpuTimeEnd
2196:     This timer should NOT include times for data transfers between the GPU and CPU, nor setup actions such as allocating space.
2197:     The regular logging captures the time for data transfers and any CPU activites during the event
2198:     It is used to compute the flop rate on the GPU as it is actively engaged in running a kernel.

2200:   Developer Notes:
2201:     The GPU event timer captures the execution time of all the kernels launched in the default stream by the CPU between PetscLogGpuTimeBegin() and PetsLogGpuTimeEnd().
2202:     PetscLogGpuTimeBegin() and PetsLogGpuTimeEnd() insert the begin and end events into the default stream (stream 0). The device will record a time stamp for the event when it reaches that event in the stream. The function xxxEventSynchronize() is called in PetsLogGpuTimeEnd() to block CPU execution, but not continued GPU excution, until the timer event is recorded.

2204:   Level: intermediate

2206: .seealso:  PetscLogView(), PetscLogGpuFlops(), PetscLogGpuTimeEnd()
2207: @*/
2208: PetscErrorCode PetscLogGpuTimeBegin(void)
2209: {
2210:   if (!PetscLogPLB) return 0;
2211:   if (PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_HIP)) {
2212:     PetscDeviceContext dctx;

2214:     PetscDeviceContextGetCurrentContext(&dctx);
2215:     PetscDeviceContextBeginTimer_Internal(dctx);
2216:   } else {
2217:     PetscTimeSubtract(&petsc_gtime);
2218:   }
2219:   return 0;
2220: }

2222: /*@C
2223:   PetscLogGpuTimeEnd - Stop timer for device

2225:   Level: intermediate

2227: .seealso:  PetscLogView(), PetscLogGpuFlops(), PetscLogGpuTimeBegin()
2228: @*/
2229: PetscErrorCode PetscLogGpuTimeEnd(void)
2230: {
2231:   if (!PetscLogPLE) return 0;
2232:   if (PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_HIP)) {
2233:     PetscDeviceContext dctx;
2234:     PetscLogDouble     elapsed;

2236:     PetscDeviceContextGetCurrentContext(&dctx);
2237:     PetscDeviceContextEndTimer_Internal(dctx,&elapsed);
2238:     petsc_gtime += (elapsed/1000.0);
2239:   } else {
2240:     PetscTimeAdd(&petsc_gtime);
2241:   }
2242:   return 0;
2243: }
2244: #endif /* end of PETSC_HAVE_DEVICE */

2246: #else /* end of -DPETSC_USE_LOG section */

2248: PetscErrorCode  PetscLogObjectState(PetscObject obj, const char format[], ...)
2249: {
2250:   return 0;
2251: }

2253: #endif /* PETSC_USE_LOG*/

2255: PetscClassId PETSC_LARGEST_CLASSID = PETSC_SMALLEST_CLASSID;
2256: PetscClassId PETSC_OBJECT_CLASSID  = 0;

2258: /*@C
2259:   PetscClassIdRegister - Registers a new class name for objects and logging operations in an application code.

2261:   Not Collective

2263:   Input Parameter:
2264: . name   - The class name

2266:   Output Parameter:
2267: . oclass - The class id or classid

2269:   Level: developer

2271: @*/
2272: PetscErrorCode  PetscClassIdRegister(const char name[],PetscClassId *oclass)
2273: {
2274: #if defined(PETSC_USE_LOG)
2275:   PetscStageLog  stageLog;
2276:   PetscInt       stage;
2277: #endif

2279:   *oclass = ++PETSC_LARGEST_CLASSID;
2280: #if defined(PETSC_USE_LOG)
2281:   PetscLogGetStageLog(&stageLog);
2282:   PetscClassRegLogRegister(stageLog->classLog, name, *oclass);
2283:   for (stage = 0; stage < stageLog->numStages; stage++) {
2284:     PetscClassPerfLogEnsureSize(stageLog->stageInfo[stage].classLog, stageLog->classLog->numClasses);
2285:   }
2286: #endif
2287:   return 0;
2288: }

2290: #if defined(PETSC_USE_LOG) && defined(PETSC_HAVE_MPE)
2291: #include <mpe.h>

2293: PetscBool PetscBeganMPE = PETSC_FALSE;

2295: PETSC_INTERN PetscErrorCode PetscLogEventBeginMPE(PetscLogEvent,int,PetscObject,PetscObject,PetscObject,PetscObject);
2296: PETSC_INTERN PetscErrorCode PetscLogEventEndMPE(PetscLogEvent,int,PetscObject,PetscObject,PetscObject,PetscObject);

2298: /*@C
2299:    PetscLogMPEBegin - Turns on MPE logging of events. This creates large log files
2300:    and slows the program down.

2302:    Collective over PETSC_COMM_WORLD

2304:    Options Database Keys:
2305: . -log_mpe - Prints extensive log information

2307:    Notes:
2308:    A related routine is PetscLogDefaultBegin() (with the options key -log_view), which is
2309:    intended for production runs since it logs only flop rates and object
2310:    creation (and should not significantly slow the programs).

2312:    Level: advanced

2314: .seealso: PetscLogDump(), PetscLogDefaultBegin(), PetscLogAllBegin(), PetscLogEventActivate(),
2315:           PetscLogEventDeactivate()
2316: @*/
2317: PetscErrorCode  PetscLogMPEBegin(void)
2318: {
2319:   /* Do MPE initialization */
2320:   if (!MPE_Initialized_logging()) { /* This function exists in mpich 1.1.2 and higher */
2321:     PetscInfo(0,"Initializing MPE.\n");
2322:     MPE_Init_log();

2324:     PetscBeganMPE = PETSC_TRUE;
2325:   } else {
2326:     PetscInfo(0,"MPE already initialized. Not attempting to reinitialize.\n");
2327:   }
2328:   PetscLogSet(PetscLogEventBeginMPE, PetscLogEventEndMPE);
2329:   return 0;
2330: }

2332: /*@C
2333:    PetscLogMPEDump - Dumps the MPE logging info to file for later use with Jumpshot.

2335:    Collective over PETSC_COMM_WORLD

2337:    Level: advanced

2339: .seealso: PetscLogDump(), PetscLogAllBegin(), PetscLogMPEBegin()
2340: @*/
2341: PetscErrorCode  PetscLogMPEDump(const char sname[])
2342: {
2343:   char           name[PETSC_MAX_PATH_LEN];

2345:   if (PetscBeganMPE) {
2346:     PetscInfo(0,"Finalizing MPE.\n");
2347:     if (sname) {
2348:       PetscStrcpy(name,sname);
2349:     } else {
2350:       PetscGetProgramName(name,sizeof(name));
2351:     }
2352:     MPE_Finish_log(name);
2353:   } else {
2354:     PetscInfo(0,"Not finalizing MPE (not started by PETSc).\n");
2355:   }
2356:   return 0;
2357: }

2359: #define PETSC_RGB_COLORS_MAX 39
2360: static const char *PetscLogMPERGBColors[PETSC_RGB_COLORS_MAX] = {
2361:   "OliveDrab:      ",
2362:   "BlueViolet:     ",
2363:   "CadetBlue:      ",
2364:   "CornflowerBlue: ",
2365:   "DarkGoldenrod:  ",
2366:   "DarkGreen:      ",
2367:   "DarkKhaki:      ",
2368:   "DarkOliveGreen: ",
2369:   "DarkOrange:     ",
2370:   "DarkOrchid:     ",
2371:   "DarkSeaGreen:   ",
2372:   "DarkSlateGray:  ",
2373:   "DarkTurquoise:  ",
2374:   "DeepPink:       ",
2375:   "DarkKhaki:      ",
2376:   "DimGray:        ",
2377:   "DodgerBlue:     ",
2378:   "GreenYellow:    ",
2379:   "HotPink:        ",
2380:   "IndianRed:      ",
2381:   "LavenderBlush:  ",
2382:   "LawnGreen:      ",
2383:   "LemonChiffon:   ",
2384:   "LightCoral:     ",
2385:   "LightCyan:      ",
2386:   "LightPink:      ",
2387:   "LightSalmon:    ",
2388:   "LightSlateGray: ",
2389:   "LightYellow:    ",
2390:   "LimeGreen:      ",
2391:   "MediumPurple:   ",
2392:   "MediumSeaGreen: ",
2393:   "MediumSlateBlue:",
2394:   "MidnightBlue:   ",
2395:   "MintCream:      ",
2396:   "MistyRose:      ",
2397:   "NavajoWhite:    ",
2398:   "NavyBlue:       ",
2399:   "OliveDrab:      "
2400: };

2402: /*@C
2403:   PetscLogMPEGetRGBColor - This routine returns a rgb color useable with PetscLogEventRegister()

2405:   Not collective. Maybe it should be?

2407:   Output Parameter:
2408: . str - character string representing the color

2410:   Level: developer

2412: .seealso: PetscLogEventRegister
2413: @*/
2414: PetscErrorCode  PetscLogMPEGetRGBColor(const char *str[])
2415: {
2416:   static int idx = 0;

2418:   *str = PetscLogMPERGBColors[idx];
2419:   idx  = (idx + 1)% PETSC_RGB_COLORS_MAX;
2420:   return 0;
2421: }

2423: #endif /* PETSC_USE_LOG && PETSC_HAVE_MPE */