xref: /petsc/src/sys/logging/plog.c (revision fa17ad41d4319bf289742c142d49375a1b3c5cff)
1 
2 /*
3       PETSc code to log object creation and destruction and PETSc events.
4 
5       This provides the public API used by the rest of PETSc and by users.
6 
7       These routines use a private API that is not used elsewhere in PETSc and is not
8       accessible to users. The private API is defined in logimpl.h and the utils directory.
9 
10 */
11 #include <petsc/private/logimpl.h>        /*I    "petscsys.h"   I*/
12 #include <petsctime.h>
13 #include <petscviewer.h>
14 
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 }
22 
23 /*@C
24    PetscLogObjectMemory - Adds to an object a count of additional amount of memory that is used by the object.
25 
26    Not collective.
27 
28    Input Parameters:
29 +  obj  - the PETSc object
30 -  mem  - the amount of memory that is being added to the object
31 
32    Level: developer
33 
34    Developer Notes:
35     Currently we do not always do a good job of associating all memory allocations with an object.
36 
37 .seealso: PetscFinalize(), PetscInitializeFortran(), PetscGetArgs(), PetscInitializeNoArguments()
38 
39 @*/
40 PetscErrorCode PetscLogObjectMemory(PetscObject p,PetscLogDouble m)
41 {
42   if (!p) return 0;
43   p->mem += m;
44   return 0;
45 }
46 
47 PetscLogEvent PETSC_LARGEST_EVENT = PETSC_EVENT;
48 
49 #if defined(PETSC_USE_LOG)
50 #include <petscmachineinfo.h>
51 #include <petscconfiginfo.h>
52 
53 /* used in the MPI_XXX() count macros in petsclog.h */
54 
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;
63 
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 
84 /* Logging functions */
85 PetscErrorCode (*PetscLogPHC)(PetscObject) = NULL;
86 PetscErrorCode (*PetscLogPHD)(PetscObject) = NULL;
87 PetscErrorCode (*PetscLogPLB)(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject) = NULL;
88 PetscErrorCode (*PetscLogPLE)(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject) = NULL;
89 
90 /* Tracing event logging variables */
91 FILE             *petsc_tracefile            = NULL;
92 int              petsc_tracelevel            = 0;
93 const char       *petsc_traceblanks          = "                                                                                                    ";
94 char             petsc_tracespace[128]       = " ";
95 PetscLogDouble   petsc_tracetime             = 0.0;
96 static PetscBool PetscLogInitializeCalled = PETSC_FALSE;
97 
98 /*---------------------------------------------- General Functions --------------------------------------------------*/
99 /*@C
100   PetscLogDestroy - Destroys the object and event logging data and resets the global counters.
101 
102   Not Collective
103 
104   Notes:
105   This routine should not usually be used by programmers. Instead employ
106   PetscLogStagePush() and PetscLogStagePop().
107 
108   Level: developer
109 
110 .keywords: log, destroy
111 .seealso: PetscLogDump(), PetscLogAllBegin(), PetscLogView(), PetscLogStagePush(), PlogStagePop()
112 @*/
113 PetscErrorCode  PetscLogDestroy(void)
114 {
115   PetscStageLog  stageLog;
116   PetscErrorCode ierr;
117 
118   PetscFunctionBegin;
119   ierr = PetscFree(petsc_actions);CHKERRQ(ierr);
120   ierr = PetscFree(petsc_objects);CHKERRQ(ierr);
121   ierr = PetscLogNestedEnd();CHKERRQ(ierr);
122   ierr = PetscLogSet(NULL, NULL);CHKERRQ(ierr);
123 
124   /* Resetting phase */
125   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
126   ierr = PetscStageLogDestroy(stageLog);CHKERRQ(ierr);
127 
128   petsc_TotalFlops            = 0.0;
129   petsc_numActions            = 0;
130   petsc_numObjects            = 0;
131   petsc_numObjectsDestroyed   = 0;
132   petsc_maxActions            = 100;
133   petsc_maxObjects            = 100;
134   petsc_actions               = NULL;
135   petsc_objects               = NULL;
136   petsc_logActions            = PETSC_FALSE;
137   petsc_logObjects            = PETSC_FALSE;
138   petsc_BaseTime              = 0.0;
139   petsc_TotalFlops            = 0.0;
140   petsc_tmp_flops             = 0.0;
141   petsc_send_ct               = 0.0;
142   petsc_recv_ct               = 0.0;
143   petsc_send_len              = 0.0;
144   petsc_recv_len              = 0.0;
145   petsc_isend_ct              = 0.0;
146   petsc_irecv_ct              = 0.0;
147   petsc_isend_len             = 0.0;
148   petsc_irecv_len             = 0.0;
149   petsc_wait_ct               = 0.0;
150   petsc_wait_any_ct           = 0.0;
151   petsc_wait_all_ct           = 0.0;
152   petsc_sum_of_waits_ct       = 0.0;
153   petsc_allreduce_ct          = 0.0;
154   petsc_gather_ct             = 0.0;
155   petsc_scatter_ct            = 0.0;
156   PETSC_LARGEST_EVENT         = PETSC_EVENT;
157   PetscLogPHC                 = NULL;
158   PetscLogPHD                 = NULL;
159   petsc_tracefile             = NULL;
160   petsc_tracelevel            = 0;
161   petsc_traceblanks           = "                                                                                                    ";
162   petsc_tracespace[0]         = ' '; petsc_tracespace[1] = 0;
163   petsc_tracetime             = 0.0;
164   PETSC_LARGEST_CLASSID       = PETSC_SMALLEST_CLASSID;
165   PETSC_OBJECT_CLASSID        = 0;
166   petsc_stageLog              = 0;
167   PetscLogInitializeCalled = PETSC_FALSE;
168   PetscFunctionReturn(0);
169 }
170 
171 /*@C
172   PetscLogSet - Sets the logging functions called at the beginning and ending of every event.
173 
174   Not Collective
175 
176   Input Parameters:
177 + b - The function called at beginning of event
178 - e - The function called at end of event
179 
180   Level: developer
181 
182 .seealso: PetscLogDump(), PetscLogDefaultBegin(), PetscLogAllBegin(), PetscLogTraceBegin()
183 @*/
184 PetscErrorCode  PetscLogSet(PetscErrorCode (*b)(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject),
185                             PetscErrorCode (*e)(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject))
186 {
187   PetscFunctionBegin;
188   PetscLogPLB = b;
189   PetscLogPLE = e;
190   PetscFunctionReturn(0);
191 }
192 
193 /*------------------------------------------- Initialization Functions ----------------------------------------------*/
194 /*
195     The data structures for logging are always created even if no logging is turned on. This is so events etc can
196   be registered in the code before the actually logging is turned on.
197  */
198 PetscErrorCode  PetscLogInitialize(void)
199 {
200   int            stage;
201   PetscBool      opt;
202   PetscErrorCode ierr;
203 
204   PetscFunctionBegin;
205   if (PetscLogInitializeCalled) PetscFunctionReturn(0);
206   PetscLogInitializeCalled = PETSC_TRUE;
207 
208   ierr = PetscOptionsHasName(NULL,NULL, "-log_exclude_actions", &opt);CHKERRQ(ierr);
209   if (opt) petsc_logActions = PETSC_FALSE;
210   ierr = PetscOptionsHasName(NULL,NULL, "-log_exclude_objects", &opt);CHKERRQ(ierr);
211   if (opt) petsc_logObjects = PETSC_FALSE;
212   if (petsc_logActions) {
213     ierr = PetscMalloc1(petsc_maxActions, &petsc_actions);CHKERRQ(ierr);
214   }
215   if (petsc_logObjects) {
216     ierr = PetscMalloc1(petsc_maxObjects, &petsc_objects);CHKERRQ(ierr);
217   }
218   PetscLogPHC = PetscLogObjCreateDefault;
219   PetscLogPHD = PetscLogObjDestroyDefault;
220   /* Setup default logging structures */
221   ierr = PetscStageLogCreate(&petsc_stageLog);CHKERRQ(ierr);
222   ierr = PetscStageLogRegister(petsc_stageLog, "Main Stage", &stage);CHKERRQ(ierr);
223 
224   /* All processors sync here for more consistent logging */
225   ierr = MPI_Barrier(PETSC_COMM_WORLD);CHKERRQ(ierr);
226   PetscTime(&petsc_BaseTime);
227   ierr = PetscLogStagePush(stage);CHKERRQ(ierr);
228   PetscFunctionReturn(0);
229 }
230 
231 /*@C
232   PetscLogDefaultBegin - Turns on logging of objects and events. This logs flop
233   rates and object creation and should not slow programs down too much.
234   This routine may be called more than once.
235 
236   Logically Collective over PETSC_COMM_WORLD
237 
238   Options Database Keys:
239 . -log_view [viewertype:filename:viewerformat] - Prints summary of flop and timing information to the
240                   screen (for code configured with --with-log=1 (which is the default))
241 
242   Usage:
243 .vb
244       PetscInitialize(...);
245       PetscLogDefaultBegin();
246        ... code ...
247       PetscLogView(viewer); or PetscLogDump();
248       PetscFinalize();
249 .ve
250 
251   Notes:
252   PetscLogView(viewer) or PetscLogDump() actually cause the printing of
253   the logging information.
254 
255   Level: advanced
256 
257 .keywords: log, begin
258 .seealso: PetscLogDump(), PetscLogAllBegin(), PetscLogView(), PetscLogTraceBegin()
259 @*/
260 PetscErrorCode  PetscLogDefaultBegin(void)
261 {
262   PetscErrorCode ierr;
263 
264   PetscFunctionBegin;
265   ierr = PetscLogSet(PetscLogEventBeginDefault, PetscLogEventEndDefault);CHKERRQ(ierr);
266   PetscFunctionReturn(0);
267 }
268 
269 /*@C
270   PetscLogAllBegin - Turns on extensive logging of objects and events. Logs
271   all events. This creates large log files and slows the program down.
272 
273   Logically Collective on PETSC_COMM_WORLD
274 
275   Options Database Keys:
276 . -log_all - Prints extensive log information
277 
278   Usage:
279 .vb
280      PetscInitialize(...);
281      PetscLogAllBegin();
282      ... code ...
283      PetscLogDump(filename);
284      PetscFinalize();
285 .ve
286 
287   Notes:
288   A related routine is PetscLogDefaultBegin() (with the options key -log), which is
289   intended for production runs since it logs only flop rates and object
290   creation (and shouldn't significantly slow the programs).
291 
292   Level: advanced
293 
294 .keywords: log, all, begin
295 .seealso: PetscLogDump(), PetscLogDefaultBegin(), PetscLogTraceBegin()
296 @*/
297 PetscErrorCode  PetscLogAllBegin(void)
298 {
299   PetscErrorCode ierr;
300 
301   PetscFunctionBegin;
302   ierr = PetscLogSet(PetscLogEventBeginComplete, PetscLogEventEndComplete);CHKERRQ(ierr);
303   PetscFunctionReturn(0);
304 }
305 
306 /*@
307   PetscLogTraceBegin - Activates trace logging.  Every time a PETSc event
308   begins or ends, the event name is printed.
309 
310   Logically Collective on PETSC_COMM_WORLD
311 
312   Input Parameter:
313 . file - The file to print trace in (e.g. stdout)
314 
315   Options Database Key:
316 . -log_trace [filename] - Activates PetscLogTraceBegin()
317 
318   Notes:
319   PetscLogTraceBegin() prints the processor number, the execution time (sec),
320   then "Event begin:" or "Event end:" followed by the event name.
321 
322   PetscLogTraceBegin() allows tracing of all PETSc calls, which is useful
323   to determine where a program is hanging without running in the
324   debugger.  Can be used in conjunction with the -info option.
325 
326   Level: intermediate
327 
328 .seealso: PetscLogDump(), PetscLogAllBegin(), PetscLogView(), PetscLogDefaultBegin()
329 @*/
330 PetscErrorCode  PetscLogTraceBegin(FILE *file)
331 {
332   PetscErrorCode ierr;
333 
334   PetscFunctionBegin;
335   petsc_tracefile = file;
336 
337   ierr = PetscLogSet(PetscLogEventBeginTrace, PetscLogEventEndTrace);CHKERRQ(ierr);
338   PetscFunctionReturn(0);
339 }
340 
341 /*@
342   PetscLogActions - Determines whether actions are logged for the graphical viewer.
343 
344   Not Collective
345 
346   Input Parameter:
347 . flag - PETSC_TRUE if actions are to be logged
348 
349   Level: intermediate
350 
351   Note: Logging of actions continues to consume more memory as the program
352   runs. Long running programs should consider turning this feature off.
353 
354   Options Database Keys:
355 . -log_exclude_actions - Turns off actions logging
356 
357 .keywords: log, stage, register
358 .seealso: PetscLogStagePush(), PetscLogStagePop()
359 @*/
360 PetscErrorCode  PetscLogActions(PetscBool flag)
361 {
362   PetscFunctionBegin;
363   petsc_logActions = flag;
364   PetscFunctionReturn(0);
365 }
366 
367 /*@
368   PetscLogObjects - Determines whether objects are logged for the graphical viewer.
369 
370   Not Collective
371 
372   Input Parameter:
373 . flag - PETSC_TRUE if objects are to be logged
374 
375   Level: intermediate
376 
377   Note: Logging of objects continues to consume more memory as the program
378   runs. Long running programs should consider turning this feature off.
379 
380   Options Database Keys:
381 . -log_exclude_objects - Turns off objects logging
382 
383 .keywords: log, stage, register
384 .seealso: PetscLogStagePush(), PetscLogStagePop()
385 @*/
386 PetscErrorCode  PetscLogObjects(PetscBool flag)
387 {
388   PetscFunctionBegin;
389   petsc_logObjects = flag;
390   PetscFunctionReturn(0);
391 }
392 
393 /*------------------------------------------------ Stage Functions --------------------------------------------------*/
394 /*@C
395   PetscLogStageRegister - Attaches a character string name to a logging stage.
396 
397   Not Collective
398 
399   Input Parameter:
400 . sname - The name to associate with that stage
401 
402   Output Parameter:
403 . stage - The stage number
404 
405   Level: intermediate
406 
407 .keywords: log, stage, register
408 .seealso: PetscLogStagePush(), PetscLogStagePop()
409 @*/
410 PetscErrorCode  PetscLogStageRegister(const char sname[],PetscLogStage *stage)
411 {
412   PetscStageLog  stageLog;
413   PetscLogEvent  event;
414   PetscErrorCode ierr;
415 
416   PetscFunctionBegin;
417   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
418   ierr = PetscStageLogRegister(stageLog, sname, stage);CHKERRQ(ierr);
419   /* Copy events already changed in the main stage, this sucks */
420   ierr = PetscEventPerfLogEnsureSize(stageLog->stageInfo[*stage].eventLog, stageLog->eventLog->numEvents);CHKERRQ(ierr);
421   for (event = 0; event < stageLog->eventLog->numEvents; event++) {
422     ierr = PetscEventPerfInfoCopy(&stageLog->stageInfo[0].eventLog->eventInfo[event],&stageLog->stageInfo[*stage].eventLog->eventInfo[event]);CHKERRQ(ierr);
423   }
424   ierr = PetscClassPerfLogEnsureSize(stageLog->stageInfo[*stage].classLog, stageLog->classLog->numClasses);CHKERRQ(ierr);
425   PetscFunctionReturn(0);
426 }
427 
428 /*@C
429   PetscLogStagePush - This function pushes a stage on the stack.
430 
431   Not Collective
432 
433   Input Parameter:
434 . stage - The stage on which to log
435 
436   Usage:
437   If the option -log_sumary is used to run the program containing the
438   following code, then 2 sets of summary data will be printed during
439   PetscFinalize().
440 .vb
441       PetscInitialize(int *argc,char ***args,0,0);
442       [stage 0 of code]
443       PetscLogStagePush(1);
444       [stage 1 of code]
445       PetscLogStagePop();
446       PetscBarrier(...);
447       [more stage 0 of code]
448       PetscFinalize();
449 .ve
450 
451   Notes:
452   Use PetscLogStageRegister() to register a stage.
453 
454   Level: intermediate
455 
456 .keywords: log, push, stage
457 .seealso: PetscLogStagePop(), PetscLogStageRegister(), PetscBarrier()
458 @*/
459 PetscErrorCode  PetscLogStagePush(PetscLogStage stage)
460 {
461   PetscStageLog  stageLog;
462   PetscErrorCode ierr;
463 
464   PetscFunctionBegin;
465   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
466   ierr = PetscStageLogPush(stageLog, stage);CHKERRQ(ierr);
467   PetscFunctionReturn(0);
468 }
469 
470 /*@C
471   PetscLogStagePop - This function pops a stage from the stack.
472 
473   Not Collective
474 
475   Usage:
476   If the option -log_sumary is used to run the program containing the
477   following code, then 2 sets of summary data will be printed during
478   PetscFinalize().
479 .vb
480       PetscInitialize(int *argc,char ***args,0,0);
481       [stage 0 of code]
482       PetscLogStagePush(1);
483       [stage 1 of code]
484       PetscLogStagePop();
485       PetscBarrier(...);
486       [more stage 0 of code]
487       PetscFinalize();
488 .ve
489 
490   Notes:
491   Use PetscLogStageRegister() to register a stage.
492 
493   Level: intermediate
494 
495 .keywords: log, pop, stage
496 .seealso: PetscLogStagePush(), PetscLogStageRegister(), PetscBarrier()
497 @*/
498 PetscErrorCode  PetscLogStagePop(void)
499 {
500   PetscStageLog  stageLog;
501   PetscErrorCode ierr;
502 
503   PetscFunctionBegin;
504   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
505   ierr = PetscStageLogPop(stageLog);CHKERRQ(ierr);
506   PetscFunctionReturn(0);
507 }
508 
509 /*@
510   PetscLogStageSetActive - Determines stage activity for PetscLogEventBegin() and PetscLogEventEnd().
511 
512   Not Collective
513 
514   Input Parameters:
515 + stage    - The stage
516 - isActive - The activity flag, PETSC_TRUE for logging, else PETSC_FALSE (defaults to PETSC_TRUE)
517 
518   Level: intermediate
519 
520 .seealso: PetscLogStagePush(), PetscLogStagePop(), PetscLogEventBegin(), PetscLogEventEnd(), PetscPreLoadBegin(), PetscPreLoadEnd(), PetscPreLoadStage()
521 @*/
522 PetscErrorCode  PetscLogStageSetActive(PetscLogStage stage, PetscBool isActive)
523 {
524   PetscStageLog  stageLog;
525   PetscErrorCode ierr;
526 
527   PetscFunctionBegin;
528   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
529   ierr = PetscStageLogSetActive(stageLog, stage, isActive);CHKERRQ(ierr);
530   PetscFunctionReturn(0);
531 }
532 
533 /*@
534   PetscLogStageGetActive - Returns stage activity for PetscLogEventBegin() and PetscLogEventEnd().
535 
536   Not Collective
537 
538   Input Parameter:
539 . stage    - The stage
540 
541   Output Parameter:
542 . isActive - The activity flag, PETSC_TRUE for logging, else PETSC_FALSE (defaults to PETSC_TRUE)
543 
544   Level: intermediate
545 
546 .seealso: PetscLogStagePush(), PetscLogStagePop(), PetscLogEventBegin(), PetscLogEventEnd(), PetscPreLoadBegin(), PetscPreLoadEnd(), PetscPreLoadStage()
547 @*/
548 PetscErrorCode  PetscLogStageGetActive(PetscLogStage stage, PetscBool  *isActive)
549 {
550   PetscStageLog  stageLog;
551   PetscErrorCode ierr;
552 
553   PetscFunctionBegin;
554   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
555   ierr = PetscStageLogGetActive(stageLog, stage, isActive);CHKERRQ(ierr);
556   PetscFunctionReturn(0);
557 }
558 
559 /*@
560   PetscLogStageSetVisible - Determines stage visibility in PetscLogView()
561 
562   Not Collective
563 
564   Input Parameters:
565 + stage     - The stage
566 - isVisible - The visibility flag, PETSC_TRUE to print, else PETSC_FALSE (defaults to PETSC_TRUE)
567 
568   Level: intermediate
569 
570 .seealso: PetscLogStagePush(), PetscLogStagePop(), PetscLogView()
571 @*/
572 PetscErrorCode  PetscLogStageSetVisible(PetscLogStage stage, PetscBool isVisible)
573 {
574   PetscStageLog  stageLog;
575   PetscErrorCode ierr;
576 
577   PetscFunctionBegin;
578   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
579   ierr = PetscStageLogSetVisible(stageLog, stage, isVisible);CHKERRQ(ierr);
580   PetscFunctionReturn(0);
581 }
582 
583 /*@
584   PetscLogStageGetVisible - Returns stage visibility in PetscLogView()
585 
586   Not Collective
587 
588   Input Parameter:
589 . stage     - The stage
590 
591   Output Parameter:
592 . isVisible - The visibility flag, PETSC_TRUE to print, else PETSC_FALSE (defaults to PETSC_TRUE)
593 
594   Level: intermediate
595 
596 .seealso: PetscLogStagePush(), PetscLogStagePop(), PetscLogView()
597 @*/
598 PetscErrorCode  PetscLogStageGetVisible(PetscLogStage stage, PetscBool  *isVisible)
599 {
600   PetscStageLog  stageLog;
601   PetscErrorCode ierr;
602 
603   PetscFunctionBegin;
604   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
605   ierr = PetscStageLogGetVisible(stageLog, stage, isVisible);CHKERRQ(ierr);
606   PetscFunctionReturn(0);
607 }
608 
609 /*@C
610   PetscLogStageGetId - Returns the stage id when given the stage name.
611 
612   Not Collective
613 
614   Input Parameter:
615 . name  - The stage name
616 
617   Output Parameter:
618 . stage - The stage, , or -1 if no stage with that name exists
619 
620   Level: intermediate
621 
622 .seealso: PetscLogStagePush(), PetscLogStagePop(), PetscPreLoadBegin(), PetscPreLoadEnd(), PetscPreLoadStage()
623 @*/
624 PetscErrorCode  PetscLogStageGetId(const char name[], PetscLogStage *stage)
625 {
626   PetscStageLog  stageLog;
627   PetscErrorCode ierr;
628 
629   PetscFunctionBegin;
630   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
631   ierr = PetscStageLogGetStage(stageLog, name, stage);CHKERRQ(ierr);
632   PetscFunctionReturn(0);
633 }
634 
635 /*------------------------------------------------ Event Functions --------------------------------------------------*/
636 /*@C
637   PetscLogEventRegister - Registers an event name for logging operations in an application code.
638 
639   Not Collective
640 
641   Input Parameter:
642 + name   - The name associated with the event
643 - classid - The classid associated to the class for this event, obtain either with
644            PetscClassIdRegister() or use a predefined one such as KSP_CLASSID, SNES_CLASSID, the predefined ones
645            are only available in C code
646 
647   Output Parameter:
648 . event - The event id for use with PetscLogEventBegin() and PetscLogEventEnd().
649 
650   Example of Usage:
651 .vb
652       PetscLogEvent USER_EVENT;
653       PetscClassId classid;
654       PetscLogDouble user_event_flops;
655       PetscClassIdRegister("class name",&classid);
656       PetscLogEventRegister("User event name",classid,&USER_EVENT);
657       PetscLogEventBegin(USER_EVENT,0,0,0,0);
658          [code segment to monitor]
659          PetscLogFlops(user_event_flops);
660       PetscLogEventEnd(USER_EVENT,0,0,0,0);
661 .ve
662 
663   Notes:
664   PETSc automatically logs library events if the code has been
665   configured with --with-log (which is the default) and
666   -log_view or -log_all is specified.  PetscLogEventRegister() is
667   intended for logging user events to supplement this PETSc
668   information.
669 
670   PETSc can gather data for use with the utilities Jumpshot
671   (part of the MPICH distribution).  If PETSc has been compiled
672   with flag -DPETSC_HAVE_MPE (MPE is an additional utility within
673   MPICH), the user can employ another command line option, -log_mpe,
674   to create a logfile, "mpe.log", which can be visualized
675   Jumpshot.
676 
677   The classid is associated with each event so that classes of events
678   can be disabled simultaneously, such as all matrix events. The user
679   can either use an existing classid, such as MAT_CLASSID, or create
680   their own as shown in the example.
681 
682   If an existing event with the same name exists, its event handle is
683   returned instead of creating a new event.
684 
685   Level: intermediate
686 
687 .keywords: log, event, register
688 .seealso: PetscLogEventBegin(), PetscLogEventEnd(), PetscLogFlops(),
689           PetscLogEventActivate(), PetscLogEventDeactivate(), PetscClassIdRegister()
690 @*/
691 PetscErrorCode  PetscLogEventRegister(const char name[],PetscClassId classid,PetscLogEvent *event)
692 {
693   PetscStageLog  stageLog;
694   int            stage;
695   PetscErrorCode ierr;
696 
697   PetscFunctionBegin;
698   *event = PETSC_DECIDE;
699   ierr   = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
700   ierr   = PetscEventRegLogGetEvent(stageLog->eventLog, name, event);CHKERRQ(ierr);
701   if (*event > 0) PetscFunctionReturn(0);
702   ierr   = PetscEventRegLogRegister(stageLog->eventLog, name, classid, event);CHKERRQ(ierr);
703   for (stage = 0; stage < stageLog->numStages; stage++) {
704     ierr = PetscEventPerfLogEnsureSize(stageLog->stageInfo[stage].eventLog, stageLog->eventLog->numEvents);CHKERRQ(ierr);
705     ierr = PetscClassPerfLogEnsureSize(stageLog->stageInfo[stage].classLog, stageLog->classLog->numClasses);CHKERRQ(ierr);
706   }
707   PetscFunctionReturn(0);
708 }
709 
710 /*@
711   PetscLogEventSetCollective - Indicates that a particular event is collective.
712 
713   Not Collective
714 
715   Input Parameter:
716 + event - The event id
717 - collective - Bolean flag indicating whether a particular event is collective
718 
719   Note:
720   New events returned from PetscLogEventRegister() are collective by default.
721 
722   Level: developer
723 
724 .keywords: log, event, collective
725 .seealso: PetscLogEventRegister()
726 @*/
727 PetscErrorCode PetscLogEventSetCollective(PetscLogEvent event,PetscBool collective)
728 {
729   PetscStageLog    stageLog;
730   PetscEventRegLog eventRegLog;
731   PetscErrorCode   ierr;
732 
733   PetscFunctionBegin;
734   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
735   ierr = PetscStageLogGetEventRegLog(stageLog,&eventRegLog);CHKERRQ(ierr);
736   if (event < 0 || event > eventRegLog->numEvents) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Invalid event id");
737   eventRegLog->eventInfo[event].collective = collective;
738   PetscFunctionReturn(0);
739 }
740 
741 /*@
742   PetscLogEventActivate - Indicates that a particular event should be logged.
743 
744   Not Collective
745 
746   Input Parameter:
747 . event - The event id
748 
749   Usage:
750 .vb
751       PetscLogEventDeactivate(VEC_SetValues);
752         [code where you do not want to log VecSetValues()]
753       PetscLogEventActivate(VEC_SetValues);
754         [code where you do want to log VecSetValues()]
755 .ve
756 
757   Note:
758   The event may be either a pre-defined PETSc event (found in include/petsclog.h)
759   or an event number obtained with PetscLogEventRegister().
760 
761   Level: advanced
762 
763 .keywords: log, event, activate
764 .seealso: PlogEventDeactivate()
765 @*/
766 PetscErrorCode  PetscLogEventActivate(PetscLogEvent event)
767 {
768   PetscStageLog  stageLog;
769   int            stage;
770   PetscErrorCode ierr;
771 
772   PetscFunctionBegin;
773   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
774   ierr = PetscStageLogGetCurrent(stageLog, &stage);CHKERRQ(ierr);
775   ierr = PetscEventPerfLogActivate(stageLog->stageInfo[stage].eventLog, event);CHKERRQ(ierr);
776   PetscFunctionReturn(0);
777 }
778 
779 /*@
780   PetscLogEventDeactivate - Indicates that a particular event should not be logged.
781 
782   Not Collective
783 
784   Input Parameter:
785 . event - The event id
786 
787   Usage:
788 .vb
789       PetscLogEventDeactivate(VEC_SetValues);
790         [code where you do not want to log VecSetValues()]
791       PetscLogEventActivate(VEC_SetValues);
792         [code where you do want to log VecSetValues()]
793 .ve
794 
795   Note:
796   The event may be either a pre-defined PETSc event (found in
797   include/petsclog.h) or an event number obtained with PetscLogEventRegister()).
798 
799   Level: advanced
800 
801 .keywords: log, event, deactivate
802 .seealso: PlogEventActivate()
803 @*/
804 PetscErrorCode  PetscLogEventDeactivate(PetscLogEvent event)
805 {
806   PetscStageLog  stageLog;
807   int            stage;
808   PetscErrorCode ierr;
809 
810   PetscFunctionBegin;
811   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
812   ierr = PetscStageLogGetCurrent(stageLog, &stage);CHKERRQ(ierr);
813   ierr = PetscEventPerfLogDeactivate(stageLog->stageInfo[stage].eventLog, event);CHKERRQ(ierr);
814   PetscFunctionReturn(0);
815 }
816 
817 /*@
818   PetscLogEventSetActiveAll - Sets the event activity in every stage.
819 
820   Not Collective
821 
822   Input Parameters:
823 + event    - The event id
824 - isActive - The activity flag determining whether the event is logged
825 
826   Level: advanced
827 
828 .keywords: log, event, activate
829 .seealso: PlogEventActivate(),PlogEventDeactivate()
830 @*/
831 PetscErrorCode  PetscLogEventSetActiveAll(PetscLogEvent event, PetscBool isActive)
832 {
833   PetscStageLog  stageLog;
834   int            stage;
835   PetscErrorCode ierr;
836 
837   PetscFunctionBegin;
838   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
839   for (stage = 0; stage < stageLog->numStages; stage++) {
840     if (isActive) {
841       ierr = PetscEventPerfLogActivate(stageLog->stageInfo[stage].eventLog, event);CHKERRQ(ierr);
842     } else {
843       ierr = PetscEventPerfLogDeactivate(stageLog->stageInfo[stage].eventLog, event);CHKERRQ(ierr);
844     }
845   }
846   PetscFunctionReturn(0);
847 }
848 
849 /*@
850   PetscLogEventActivateClass - Activates event logging for a PETSc object class.
851 
852   Not Collective
853 
854   Input Parameter:
855 . classid - The event class, for example MAT_CLASSID, SNES_CLASSID, etc.
856 
857   Level: developer
858 
859 .keywords: log, event, activate, class
860 .seealso: PetscInfoActivate(),PetscInfo(),PetscInfoAllow(),PetscLogEventDeactivateClass(), PetscLogEventActivate(),PetscLogEventDeactivate()
861 @*/
862 PetscErrorCode  PetscLogEventActivateClass(PetscClassId classid)
863 {
864   PetscStageLog  stageLog;
865   int            stage;
866   PetscErrorCode ierr;
867 
868   PetscFunctionBegin;
869   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
870   ierr = PetscStageLogGetCurrent(stageLog, &stage);CHKERRQ(ierr);
871   ierr = PetscEventPerfLogActivateClass(stageLog->stageInfo[stage].eventLog, stageLog->eventLog, classid);CHKERRQ(ierr);
872   PetscFunctionReturn(0);
873 }
874 
875 /*@
876   PetscLogEventDeactivateClass - Deactivates event logging for a PETSc object class.
877 
878   Not Collective
879 
880   Input Parameter:
881 . classid - The event class, for example MAT_CLASSID, SNES_CLASSID, etc.
882 
883   Level: developer
884 
885 .keywords: log, event, deactivate, class
886 .seealso: PetscInfoActivate(),PetscInfo(),PetscInfoAllow(),PetscLogEventActivateClass(), PetscLogEventActivate(),PetscLogEventDeactivate()
887 @*/
888 PetscErrorCode  PetscLogEventDeactivateClass(PetscClassId classid)
889 {
890   PetscStageLog  stageLog;
891   int            stage;
892   PetscErrorCode ierr;
893 
894   PetscFunctionBegin;
895   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
896   ierr = PetscStageLogGetCurrent(stageLog, &stage);CHKERRQ(ierr);
897   ierr = PetscEventPerfLogDeactivateClass(stageLog->stageInfo[stage].eventLog, stageLog->eventLog, classid);CHKERRQ(ierr);
898   PetscFunctionReturn(0);
899 }
900 
901 /*MC
902    PetscLogEventBegin - Logs the beginning of a user event.
903 
904    Synopsis:
905    #include <petsclog.h>
906    PetscErrorCode PetscLogEventBegin(int e,PetscObject o1,PetscObject o2,PetscObject o3,PetscObject o4)
907 
908    Not Collective
909 
910    Input Parameters:
911 +  e - integer associated with the event obtained from PetscLogEventRegister()
912 -  o1,o2,o3,o4 - objects associated with the event, or 0
913 
914 
915    Fortran Synopsis:
916    void PetscLogEventBegin(int e,PetscErrorCode ierr)
917 
918    Usage:
919 .vb
920      PetscLogEvent USER_EVENT;
921      PetscLogDouble user_event_flops;
922      PetscLogEventRegister("User event",0,&USER_EVENT);
923      PetscLogEventBegin(USER_EVENT,0,0,0,0);
924         [code segment to monitor]
925         PetscLogFlops(user_event_flops);
926      PetscLogEventEnd(USER_EVENT,0,0,0,0);
927 .ve
928 
929    Notes:
930    You need to register each integer event with the command
931    PetscLogEventRegister().
932 
933    Level: intermediate
934 
935 .seealso: PetscLogEventRegister(), PetscLogEventEnd(), PetscLogFlops()
936 
937 .keywords: log, event, begin
938 M*/
939 
940 /*MC
941    PetscLogEventEnd - Log the end of a user event.
942 
943    Synopsis:
944    #include <petsclog.h>
945    PetscErrorCode PetscLogEventEnd(int e,PetscObject o1,PetscObject o2,PetscObject o3,PetscObject o4)
946 
947    Not Collective
948 
949    Input Parameters:
950 +  e - integer associated with the event obtained with PetscLogEventRegister()
951 -  o1,o2,o3,o4 - objects associated with the event, or 0
952 
953 
954    Fortran Synopsis:
955    void PetscLogEventEnd(int e,PetscErrorCode ierr)
956 
957    Usage:
958 .vb
959      PetscLogEvent USER_EVENT;
960      PetscLogDouble user_event_flops;
961      PetscLogEventRegister("User event",0,&USER_EVENT,);
962      PetscLogEventBegin(USER_EVENT,0,0,0,0);
963         [code segment to monitor]
964         PetscLogFlops(user_event_flops);
965      PetscLogEventEnd(USER_EVENT,0,0,0,0);
966 .ve
967 
968    Notes:
969    You should also register each additional integer event with the command
970    PetscLogEventRegister().
971 
972    Level: intermediate
973 
974 .seealso: PetscLogEventRegister(), PetscLogEventBegin(), PetscLogFlops()
975 
976 .keywords: log, event, end
977 M*/
978 
979 /*@C
980   PetscLogEventGetId - Returns the event id when given the event name.
981 
982   Not Collective
983 
984   Input Parameter:
985 . name  - The event name
986 
987   Output Parameter:
988 . event - The event, or -1 if no event with that name exists
989 
990   Level: intermediate
991 
992 .seealso: PetscLogEventBegin(), PetscLogEventEnd(), PetscLogStageGetId()
993 @*/
994 PetscErrorCode  PetscLogEventGetId(const char name[], PetscLogEvent *event)
995 {
996   PetscStageLog  stageLog;
997   PetscErrorCode ierr;
998 
999   PetscFunctionBegin;
1000   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
1001   ierr = PetscEventRegLogGetEvent(stageLog->eventLog, name, event);CHKERRQ(ierr);
1002   PetscFunctionReturn(0);
1003 }
1004 
1005 
1006 /*------------------------------------------------ Output Functions -------------------------------------------------*/
1007 /*@C
1008   PetscLogDump - Dumps logs of objects to a file. This file is intended to
1009   be read by bin/petscview. This program no longer exists.
1010 
1011   Collective on PETSC_COMM_WORLD
1012 
1013   Input Parameter:
1014 . name - an optional file name
1015 
1016   Usage:
1017 .vb
1018      PetscInitialize(...);
1019      PetscLogDefaultBegin(); or PetscLogAllBegin();
1020      ... code ...
1021      PetscLogDump(filename);
1022      PetscFinalize();
1023 .ve
1024 
1025   Notes:
1026   The default file name is
1027 $    Log.<rank>
1028   where <rank> is the processor number. If no name is specified,
1029   this file will be used.
1030 
1031   Level: advanced
1032 
1033 .keywords: log, dump
1034 .seealso: PetscLogDefaultBegin(), PetscLogAllBegin(), PetscLogView()
1035 @*/
1036 PetscErrorCode  PetscLogDump(const char sname[])
1037 {
1038   PetscStageLog      stageLog;
1039   PetscEventPerfInfo *eventInfo;
1040   FILE               *fd;
1041   char               file[PETSC_MAX_PATH_LEN], fname[PETSC_MAX_PATH_LEN];
1042   PetscLogDouble     flops, _TotalTime;
1043   PetscMPIInt        rank;
1044   int                action, object, curStage;
1045   PetscLogEvent      event;
1046   PetscErrorCode     ierr;
1047 
1048   PetscFunctionBegin;
1049   /* Calculate the total elapsed time */
1050   PetscTime(&_TotalTime);
1051   _TotalTime -= petsc_BaseTime;
1052   /* Open log file */
1053   ierr = MPI_Comm_rank(PETSC_COMM_WORLD, &rank);CHKERRQ(ierr);
1054   if (sname) sprintf(file, "%s.%d", sname, rank);
1055   else sprintf(file, "Log.%d", rank);
1056   ierr = PetscFixFilename(file, fname);CHKERRQ(ierr);
1057   ierr = PetscFOpen(PETSC_COMM_WORLD, fname, "w", &fd);CHKERRQ(ierr);
1058   if ((!rank) && (!fd)) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN, "Cannot open file: %s", fname);
1059   /* Output totals */
1060   ierr = PetscFPrintf(PETSC_COMM_WORLD, fd, "Total Flop %14e %16.8e\n", petsc_TotalFlops, _TotalTime);CHKERRQ(ierr);
1061   ierr = PetscFPrintf(PETSC_COMM_WORLD, fd, "Clock Resolution %g\n", 0.0);CHKERRQ(ierr);
1062   /* Output actions */
1063   if (petsc_logActions) {
1064     ierr = PetscFPrintf(PETSC_COMM_WORLD, fd, "Actions accomplished %d\n", petsc_numActions);CHKERRQ(ierr);
1065     for (action = 0; action < petsc_numActions; action++) {
1066       ierr = PetscFPrintf(PETSC_COMM_WORLD, fd, "%g %d %d %d %d %d %d %g %g %g\n",
1067                           petsc_actions[action].time, petsc_actions[action].action, (int)petsc_actions[action].event, (int)petsc_actions[action].classid, petsc_actions[action].id1,
1068                           petsc_actions[action].id2, petsc_actions[action].id3, petsc_actions[action].flops, petsc_actions[action].mem, petsc_actions[action].maxmem);CHKERRQ(ierr);
1069     }
1070   }
1071   /* Output objects */
1072   if (petsc_logObjects) {
1073     ierr = PetscFPrintf(PETSC_COMM_WORLD, fd, "Objects created %d destroyed %d\n", petsc_numObjects, petsc_numObjectsDestroyed);CHKERRQ(ierr);
1074     for (object = 0; object < petsc_numObjects; object++) {
1075       ierr = PetscFPrintf(PETSC_COMM_WORLD, fd, "Parent ID: %d Memory: %d\n", petsc_objects[object].parent, (int) petsc_objects[object].mem);CHKERRQ(ierr);
1076       if (!petsc_objects[object].name[0]) {
1077         ierr = PetscFPrintf(PETSC_COMM_WORLD, fd,"No Name\n");CHKERRQ(ierr);
1078       } else {
1079         ierr = PetscFPrintf(PETSC_COMM_WORLD, fd, "Name: %s\n", petsc_objects[object].name);CHKERRQ(ierr);
1080       }
1081       if (petsc_objects[object].info[0] != 0) {
1082         ierr = PetscFPrintf(PETSC_COMM_WORLD, fd, "No Info\n");CHKERRQ(ierr);
1083       } else {
1084         ierr = PetscFPrintf(PETSC_COMM_WORLD, fd, "Info: %s\n", petsc_objects[object].info);CHKERRQ(ierr);
1085       }
1086     }
1087   }
1088   /* Output events */
1089   ierr = PetscFPrintf(PETSC_COMM_WORLD, fd, "Event log:\n");CHKERRQ(ierr);
1090   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
1091   ierr = PetscIntStackTop(stageLog->stack, &curStage);CHKERRQ(ierr);
1092   eventInfo = stageLog->stageInfo[curStage].eventLog->eventInfo;
1093   for (event = 0; event < stageLog->stageInfo[curStage].eventLog->numEvents; event++) {
1094     if (eventInfo[event].time != 0.0) flops = eventInfo[event].flops/eventInfo[event].time;
1095     else flops = 0.0;
1096     ierr = PetscFPrintf(PETSC_COMM_WORLD, fd, "%d %16d %16g %16g %16g\n", event, eventInfo[event].count,
1097                         eventInfo[event].flops, eventInfo[event].time, flops);CHKERRQ(ierr);
1098   }
1099   ierr = PetscFClose(PETSC_COMM_WORLD, fd);CHKERRQ(ierr);
1100   PetscFunctionReturn(0);
1101 }
1102 
1103 /*
1104   PetscLogView_Detailed - Each process prints the times for its own events
1105 
1106 */
1107 PetscErrorCode  PetscLogView_Detailed(PetscViewer viewer)
1108 {
1109   PetscStageLog      stageLog;
1110   PetscEventPerfInfo *eventInfo = NULL, *stageInfo = NULL;
1111   PetscLogDouble     locTotalTime, numRed, maxMem;
1112   int                numStages,numEvents,stage,event;
1113   MPI_Comm           comm = PetscObjectComm((PetscObject) viewer);
1114   PetscMPIInt        rank,size;
1115   PetscErrorCode     ierr;
1116 
1117   PetscFunctionBegin;
1118   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
1119   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
1120   /* Must preserve reduction count before we go on */
1121   numRed = petsc_allreduce_ct + petsc_gather_ct + petsc_scatter_ct;
1122   /* Get the total elapsed time */
1123   PetscTime(&locTotalTime);  locTotalTime -= petsc_BaseTime;
1124   ierr = PetscViewerASCIIPrintf(viewer,"size = %d\n",size);CHKERRQ(ierr);
1125   ierr = PetscViewerASCIIPrintf(viewer,"LocalTimes = {}\n");CHKERRQ(ierr);
1126   ierr = PetscViewerASCIIPrintf(viewer,"LocalMessages = {}\n");CHKERRQ(ierr);
1127   ierr = PetscViewerASCIIPrintf(viewer,"LocalMessageLens = {}\n");CHKERRQ(ierr);
1128   ierr = PetscViewerASCIIPrintf(viewer,"LocalReductions = {}\n");CHKERRQ(ierr);
1129   ierr = PetscViewerASCIIPrintf(viewer,"LocalFlop = {}\n");CHKERRQ(ierr);
1130   ierr = PetscViewerASCIIPrintf(viewer,"LocalObjects = {}\n");CHKERRQ(ierr);
1131   ierr = PetscViewerASCIIPrintf(viewer,"LocalMemory = {}\n");CHKERRQ(ierr);
1132   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
1133   ierr = MPIU_Allreduce(&stageLog->numStages, &numStages, 1, MPI_INT, MPI_MAX, comm);CHKERRQ(ierr);
1134   ierr = PetscViewerASCIIPrintf(viewer,"Stages = {}\n");CHKERRQ(ierr);
1135   for (stage=0; stage<numStages; stage++) {
1136     ierr = PetscViewerASCIIPrintf(viewer,"Stages[\"%s\"] = {}\n",stageLog->stageInfo[stage].name);CHKERRQ(ierr);
1137     ierr = PetscViewerASCIIPrintf(viewer,"Stages[\"%s\"][\"summary\"] = {}\n",stageLog->stageInfo[stage].name);CHKERRQ(ierr);
1138     ierr = MPIU_Allreduce(&stageLog->stageInfo[stage].eventLog->numEvents, &numEvents, 1, MPI_INT, MPI_MAX, comm);CHKERRQ(ierr);
1139     for (event = 0; event < numEvents; event++) {
1140       ierr = PetscViewerASCIIPrintf(viewer,"Stages[\"%s\"][\"%s\"] = {}\n",stageLog->stageInfo[stage].name,stageLog->eventLog->eventInfo[event].name);CHKERRQ(ierr);
1141     }
1142   }
1143   ierr = PetscMallocGetMaximumUsage(&maxMem);CHKERRQ(ierr);
1144   ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr);
1145   ierr = PetscViewerASCIISynchronizedPrintf(viewer,"LocalTimes[%d] = %g\n",rank,locTotalTime);CHKERRQ(ierr);
1146   ierr = PetscViewerASCIISynchronizedPrintf(viewer,"LocalMessages[%d] = %g\n",rank,(petsc_irecv_ct + petsc_isend_ct + petsc_recv_ct + petsc_send_ct));CHKERRQ(ierr);
1147   ierr = PetscViewerASCIISynchronizedPrintf(viewer,"LocalMessageLens[%d] = %g\n",rank,(petsc_irecv_len + petsc_isend_len + petsc_recv_len + petsc_send_len));CHKERRQ(ierr);
1148   ierr = PetscViewerASCIISynchronizedPrintf(viewer,"LocalReductions[%d] = %g\n",rank,numRed);CHKERRQ(ierr);
1149   ierr = PetscViewerASCIISynchronizedPrintf(viewer,"LocalFlop[%d] = %g\n",rank,petsc_TotalFlops);CHKERRQ(ierr);
1150   ierr = PetscViewerASCIISynchronizedPrintf(viewer,"LocalObjects[%d] = %d\n",rank,petsc_numObjects);CHKERRQ(ierr);
1151   ierr = PetscViewerASCIISynchronizedPrintf(viewer,"LocalMemory[%d] = %g\n",rank,maxMem);CHKERRQ(ierr);
1152   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
1153   for (stage=0; stage<numStages; stage++) {
1154     stageInfo = &stageLog->stageInfo[stage].perfInfo;
1155     ierr = PetscViewerASCIISynchronizedPrintf(viewer,"Stages[\"%s\"][\"summary\"][%d] = {\"time\" : %g, \"numMessages\" : %g, \"messageLength\" : %g, \"numReductions\" : %g, \"flop\" : %g}\n",
1156                                               stageLog->stageInfo[stage].name,rank,
1157                                               stageInfo->time,stageInfo->numMessages,stageInfo->messageLength,stageInfo->numReductions,stageInfo->flops);CHKERRQ(ierr);
1158     ierr = MPIU_Allreduce(&stageLog->stageInfo[stage].eventLog->numEvents, &numEvents, 1, MPI_INT, MPI_MAX, comm);CHKERRQ(ierr);
1159     for (event = 0; event < numEvents; event++) {
1160       eventInfo = &stageLog->stageInfo[stage].eventLog->eventInfo[event];
1161       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"Stages[\"%s\"][\"%s\"][%d] = {\"count\" : %D, \"time\" : %g, \"syncTime\" : %g, \"numMessages\" : %g, \"messageLength\" : %g, \"numReductions\" : %g, \"flop\" : %g}\n",
1162                                                 stageLog->stageInfo[stage].name,stageLog->eventLog->eventInfo[event].name,rank,
1163                                                 eventInfo->count,eventInfo->time,eventInfo->syncTime,eventInfo->numMessages,eventInfo->messageLength,eventInfo->numReductions,eventInfo->flops);CHKERRQ(ierr);
1164     }
1165   }
1166   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
1167   ierr = PetscViewerASCIIPopSynchronized(viewer);CHKERRQ(ierr);
1168   PetscFunctionReturn(0);
1169 }
1170 
1171 static PetscErrorCode PetscLogViewWarnSync(MPI_Comm comm,FILE *fd)
1172 {
1173   PetscErrorCode ierr;
1174   PetscFunctionBegin;
1175   if (!PetscLogSyncOn) PetscFunctionReturn(0);
1176   ierr = PetscFPrintf(comm, fd, "\n\n");CHKERRQ(ierr);
1177   ierr = PetscFPrintf(comm, fd, "      ##########################################################\n");CHKERRQ(ierr);
1178   ierr = PetscFPrintf(comm, fd, "      #                                                        #\n");CHKERRQ(ierr);
1179   ierr = PetscFPrintf(comm, fd, "      #                       WARNING!!!                       #\n");CHKERRQ(ierr);
1180   ierr = PetscFPrintf(comm, fd, "      #                                                        #\n");CHKERRQ(ierr);
1181   ierr = PetscFPrintf(comm, fd, "      #   This program was run with logging synchronization.   #\n");CHKERRQ(ierr);
1182   ierr = PetscFPrintf(comm, fd, "      #   This option provides more meaningful imbalance       #\n");CHKERRQ(ierr);
1183   ierr = PetscFPrintf(comm, fd, "      #   figures at the expense of slowing things down and    #\n");CHKERRQ(ierr);
1184   ierr = PetscFPrintf(comm, fd, "      #   providing a distorted view of the overall runtime.   #\n");CHKERRQ(ierr);
1185   ierr = PetscFPrintf(comm, fd, "      #                                                        #\n");CHKERRQ(ierr);
1186   ierr = PetscFPrintf(comm, fd, "      ##########################################################\n\n\n");CHKERRQ(ierr);
1187   PetscFunctionReturn(0);
1188   PetscFunctionReturn(0);
1189 }
1190 
1191 static PetscErrorCode PetscLogViewWarnDebugging(MPI_Comm comm,FILE *fd)
1192 {
1193 #if defined(PETSC_USE_DEBUG)
1194   PetscErrorCode ierr;
1195 
1196   PetscFunctionBegin;
1197   ierr = PetscFPrintf(comm, fd, "\n\n");CHKERRQ(ierr);
1198   ierr = PetscFPrintf(comm, fd, "      ##########################################################\n");CHKERRQ(ierr);
1199   ierr = PetscFPrintf(comm, fd, "      #                                                        #\n");CHKERRQ(ierr);
1200   ierr = PetscFPrintf(comm, fd, "      #                       WARNING!!!                       #\n");CHKERRQ(ierr);
1201   ierr = PetscFPrintf(comm, fd, "      #                                                        #\n");CHKERRQ(ierr);
1202   ierr = PetscFPrintf(comm, fd, "      #   This code was compiled with a debugging option.      #\n");CHKERRQ(ierr);
1203   ierr = PetscFPrintf(comm, fd, "      #   To get timing results run ./configure                #\n");CHKERRQ(ierr);
1204   ierr = PetscFPrintf(comm, fd, "      #   using --with-debugging=no, the performance will      #\n");CHKERRQ(ierr);
1205   ierr = PetscFPrintf(comm, fd, "      #   be generally two or three times faster.              #\n");CHKERRQ(ierr);
1206   ierr = PetscFPrintf(comm, fd, "      #                                                        #\n");CHKERRQ(ierr);
1207   ierr = PetscFPrintf(comm, fd, "      ##########################################################\n\n\n");CHKERRQ(ierr);
1208   PetscFunctionReturn(0);
1209 #else
1210   return 0;
1211 #endif
1212 }
1213 
1214 PetscErrorCode  PetscLogView_Default(PetscViewer viewer)
1215 {
1216   FILE               *fd;
1217   PetscLogDouble     zero       = 0.0;
1218   PetscStageLog      stageLog;
1219   PetscStageInfo     *stageInfo = NULL;
1220   PetscEventPerfInfo *eventInfo = NULL;
1221   PetscClassPerfInfo *classInfo;
1222   char               arch[128],hostname[128],username[128],pname[PETSC_MAX_PATH_LEN],date[128];
1223   const char         *name;
1224   PetscLogDouble     locTotalTime, TotalTime, TotalFlops;
1225   PetscLogDouble     numMessages, messageLength, avgMessLen, numReductions;
1226   PetscLogDouble     stageTime, flops, flopr, mem, mess, messLen, red;
1227   PetscLogDouble     fracTime, fracFlops, fracMessages, fracLength, fracReductions, fracMess, fracMessLen, fracRed;
1228   PetscLogDouble     fracStageTime, fracStageFlops, fracStageMess, fracStageMessLen, fracStageRed;
1229   PetscLogDouble     min, max, tot, ratio, avg, x, y;
1230   PetscLogDouble     minf, maxf, totf, ratf, mint, maxt, tott, ratt, ratC, totm, totml, totr;
1231   PetscMPIInt        minC, maxC;
1232   PetscMPIInt        size, rank;
1233   PetscBool          *localStageUsed,    *stageUsed;
1234   PetscBool          *localStageVisible, *stageVisible;
1235   int                numStages, localNumEvents, numEvents;
1236   int                stage, oclass;
1237   PetscLogEvent      event;
1238   PetscErrorCode     ierr;
1239   char               version[256];
1240   MPI_Comm           comm;
1241 
1242   PetscFunctionBegin;
1243   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
1244   ierr = PetscViewerASCIIGetPointer(viewer,&fd);CHKERRQ(ierr);
1245   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
1246   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
1247   /* Get the total elapsed time */
1248   PetscTime(&locTotalTime);  locTotalTime -= petsc_BaseTime;
1249 
1250   ierr = PetscFPrintf(comm, fd, "************************************************************************************************************************\n");CHKERRQ(ierr);
1251   ierr = PetscFPrintf(comm, fd, "***             WIDEN YOUR WINDOW TO 120 CHARACTERS.  Use 'enscript -r -fCourier9' to print this document            ***\n");CHKERRQ(ierr);
1252   ierr = PetscFPrintf(comm, fd, "************************************************************************************************************************\n");CHKERRQ(ierr);
1253   ierr = PetscFPrintf(comm, fd, "\n---------------------------------------------- PETSc Performance Summary: ----------------------------------------------\n\n");CHKERRQ(ierr);
1254   ierr = PetscLogViewWarnSync(comm,fd);CHKERRQ(ierr);
1255   ierr = PetscLogViewWarnDebugging(comm,fd);CHKERRQ(ierr);
1256   ierr = PetscGetArchType(arch,sizeof(arch));CHKERRQ(ierr);
1257   ierr = PetscGetHostName(hostname,sizeof(hostname));CHKERRQ(ierr);
1258   ierr = PetscGetUserName(username,sizeof(username));CHKERRQ(ierr);
1259   ierr = PetscGetProgramName(pname,sizeof(pname));CHKERRQ(ierr);
1260   ierr = PetscGetDate(date,sizeof(date));CHKERRQ(ierr);
1261   ierr = PetscGetVersion(version,sizeof(version));CHKERRQ(ierr);
1262   if (size == 1) {
1263     ierr = PetscFPrintf(comm,fd,"%s on a %s named %s with %d processor, by %s %s\n", pname, arch, hostname, size, username, date);CHKERRQ(ierr);
1264   } else {
1265     ierr = PetscFPrintf(comm,fd,"%s on a %s named %s with %d processors, by %s %s\n", pname, arch, hostname, size, username, date);CHKERRQ(ierr);
1266   }
1267 
1268   ierr = PetscFPrintf(comm, fd, "Using %s\n", version);CHKERRQ(ierr);
1269 
1270   /* Must preserve reduction count before we go on */
1271   red = petsc_allreduce_ct + petsc_gather_ct + petsc_scatter_ct;
1272 
1273   /* Calculate summary information */
1274   ierr = PetscFPrintf(comm, fd, "\n                         Max       Max/Min     Avg       Total \n");CHKERRQ(ierr);
1275   /*   Time */
1276   ierr = MPIU_Allreduce(&locTotalTime, &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm);CHKERRQ(ierr);
1277   ierr = MPIU_Allreduce(&locTotalTime, &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm);CHKERRQ(ierr);
1278   ierr = MPIU_Allreduce(&locTotalTime, &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1279   avg  = tot/((PetscLogDouble) size);
1280   if (min != 0.0) ratio = max/min; else ratio = 0.0;
1281   ierr = PetscFPrintf(comm, fd, "Time (sec):           %5.3e   %7.3f   %5.3e\n", max, ratio, avg);CHKERRQ(ierr);
1282   TotalTime = tot;
1283   /*   Objects */
1284   avg  = (PetscLogDouble) petsc_numObjects;
1285   ierr = MPIU_Allreduce(&avg,          &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm);CHKERRQ(ierr);
1286   ierr = MPIU_Allreduce(&avg,          &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm);CHKERRQ(ierr);
1287   ierr = MPIU_Allreduce(&avg,          &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1288   avg  = tot/((PetscLogDouble) size);
1289   if (min != 0.0) ratio = max/min; else ratio = 0.0;
1290   ierr = PetscFPrintf(comm, fd, "Objects:              %5.3e   %7.3f   %5.3e\n", max, ratio, avg);CHKERRQ(ierr);
1291   /*   Flops */
1292   ierr = MPIU_Allreduce(&petsc_TotalFlops,  &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm);CHKERRQ(ierr);
1293   ierr = MPIU_Allreduce(&petsc_TotalFlops,  &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm);CHKERRQ(ierr);
1294   ierr = MPIU_Allreduce(&petsc_TotalFlops,  &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1295   avg  = tot/((PetscLogDouble) size);
1296   if (min != 0.0) ratio = max/min; else ratio = 0.0;
1297   ierr = PetscFPrintf(comm, fd, "Flop:                 %5.3e   %7.3f   %5.3e  %5.3e\n", max, ratio, avg, tot);CHKERRQ(ierr);
1298   TotalFlops = tot;
1299   /*   Flops/sec -- Must talk to Barry here */
1300   if (locTotalTime != 0.0) flops = petsc_TotalFlops/locTotalTime; else flops = 0.0;
1301   ierr = MPIU_Allreduce(&flops,        &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm);CHKERRQ(ierr);
1302   ierr = MPIU_Allreduce(&flops,        &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm);CHKERRQ(ierr);
1303   ierr = MPIU_Allreduce(&flops,        &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1304   avg  = tot/((PetscLogDouble) size);
1305   if (min != 0.0) ratio = max/min; else ratio = 0.0;
1306   ierr = PetscFPrintf(comm, fd, "Flop/sec:             %5.3e   %7.3f   %5.3e  %5.3e\n", max, ratio, avg, tot);CHKERRQ(ierr);
1307   /*   Memory */
1308   ierr = PetscMallocGetMaximumUsage(&mem);CHKERRQ(ierr);
1309   if (mem > 0.0) {
1310     ierr = MPIU_Allreduce(&mem,          &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm);CHKERRQ(ierr);
1311     ierr = MPIU_Allreduce(&mem,          &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm);CHKERRQ(ierr);
1312     ierr = MPIU_Allreduce(&mem,          &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1313     avg  = tot/((PetscLogDouble) size);
1314     if (min != 0.0) ratio = max/min; else ratio = 0.0;
1315     ierr = PetscFPrintf(comm, fd, "Memory:               %5.3e   %7.3f   %5.3e  %5.3e\n", max, ratio, avg, tot);CHKERRQ(ierr);
1316   }
1317   /*   Messages */
1318   mess = 0.5*(petsc_irecv_ct + petsc_isend_ct + petsc_recv_ct + petsc_send_ct);
1319   ierr = MPIU_Allreduce(&mess,         &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm);CHKERRQ(ierr);
1320   ierr = MPIU_Allreduce(&mess,         &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm);CHKERRQ(ierr);
1321   ierr = MPIU_Allreduce(&mess,         &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1322   avg  = tot/((PetscLogDouble) size);
1323   if (min != 0.0) ratio = max/min; else ratio = 0.0;
1324   ierr = PetscFPrintf(comm, fd, "MPI Messages:         %5.3e   %7.3f   %5.3e  %5.3e\n", max, ratio, avg, tot);CHKERRQ(ierr);
1325   numMessages = tot;
1326   /*   Message Lengths */
1327   mess = 0.5*(petsc_irecv_len + petsc_isend_len + petsc_recv_len + petsc_send_len);
1328   ierr = MPIU_Allreduce(&mess,         &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm);CHKERRQ(ierr);
1329   ierr = MPIU_Allreduce(&mess,         &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm);CHKERRQ(ierr);
1330   ierr = MPIU_Allreduce(&mess,         &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1331   if (numMessages != 0) avg = tot/numMessages; else avg = 0.0;
1332   if (min != 0.0) ratio = max/min; else ratio = 0.0;
1333   ierr = PetscFPrintf(comm, fd, "MPI Message Lengths:  %5.3e   %7.3f   %5.3e  %5.3e\n", max, ratio, avg, tot);CHKERRQ(ierr);
1334   messageLength = tot;
1335   /*   Reductions */
1336   ierr = MPIU_Allreduce(&red,          &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm);CHKERRQ(ierr);
1337   ierr = MPIU_Allreduce(&red,          &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm);CHKERRQ(ierr);
1338   ierr = MPIU_Allreduce(&red,          &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1339   if (min != 0.0) ratio = max/min; else ratio = 0.0;
1340   ierr = PetscFPrintf(comm, fd, "MPI Reductions:       %5.3e   %7.3f\n", max, ratio);CHKERRQ(ierr);
1341   numReductions = red; /* wrong because uses count from process zero */
1342   ierr = PetscFPrintf(comm, fd, "\nFlop counting convention: 1 flop = 1 real number operation of type (multiply/divide/add/subtract)\n");CHKERRQ(ierr);
1343   ierr = PetscFPrintf(comm, fd, "                            e.g., VecAXPY() for real vectors of length N --> 2N flop\n");CHKERRQ(ierr);
1344   ierr = PetscFPrintf(comm, fd, "                            and VecAXPY() for complex vectors of length N --> 8N flop\n");CHKERRQ(ierr);
1345 
1346   /* Get total number of stages --
1347        Currently, a single processor can register more stages than another, but stages must all be registered in order.
1348        We can removed this requirement if necessary by having a global stage numbering and indirection on the stage ID.
1349        This seems best accomplished by assoicating a communicator with each stage.
1350   */
1351   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
1352   ierr = MPIU_Allreduce(&stageLog->numStages, &numStages, 1, MPI_INT, MPI_MAX, comm);CHKERRQ(ierr);
1353   ierr = PetscMalloc1(numStages, &localStageUsed);CHKERRQ(ierr);
1354   ierr = PetscMalloc1(numStages, &stageUsed);CHKERRQ(ierr);
1355   ierr = PetscMalloc1(numStages, &localStageVisible);CHKERRQ(ierr);
1356   ierr = PetscMalloc1(numStages, &stageVisible);CHKERRQ(ierr);
1357   if (numStages > 0) {
1358     stageInfo = stageLog->stageInfo;
1359     for (stage = 0; stage < numStages; stage++) {
1360       if (stage < stageLog->numStages) {
1361         localStageUsed[stage]    = stageInfo[stage].used;
1362         localStageVisible[stage] = stageInfo[stage].perfInfo.visible;
1363       } else {
1364         localStageUsed[stage]    = PETSC_FALSE;
1365         localStageVisible[stage] = PETSC_TRUE;
1366       }
1367     }
1368     ierr = MPIU_Allreduce(localStageUsed,    stageUsed,    numStages, MPIU_BOOL, MPI_LOR,  comm);CHKERRQ(ierr);
1369     ierr = MPIU_Allreduce(localStageVisible, stageVisible, numStages, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
1370     for (stage = 0; stage < numStages; stage++) {
1371       if (stageUsed[stage]) {
1372         ierr = PetscFPrintf(comm, fd, "\nSummary of Stages:   ----- Time ------  ----- Flop ------  --- Messages ---  -- Message Lengths --  -- Reductions --\n");CHKERRQ(ierr);
1373         ierr = PetscFPrintf(comm, fd, "                        Avg     %%Total     Avg     %%Total    Count   %%Total     Avg         %%Total    Count   %%Total \n");CHKERRQ(ierr);
1374         break;
1375       }
1376     }
1377     for (stage = 0; stage < numStages; stage++) {
1378       if (!stageUsed[stage]) continue;
1379       /* CANNOT use MPIU_Allreduce() since it might fail the line number check */
1380       if (localStageUsed[stage]) {
1381         ierr = MPI_Allreduce(&stageInfo[stage].perfInfo.time,          &stageTime, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1382         ierr = MPI_Allreduce(&stageInfo[stage].perfInfo.flops,         &flops,     1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1383         ierr = MPI_Allreduce(&stageInfo[stage].perfInfo.numMessages,   &mess,      1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1384         ierr = MPI_Allreduce(&stageInfo[stage].perfInfo.messageLength, &messLen,   1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1385         ierr = MPI_Allreduce(&stageInfo[stage].perfInfo.numReductions, &red,       1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1386         name = stageInfo[stage].name;
1387       } else {
1388         ierr = MPI_Allreduce(&zero,                           &stageTime, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1389         ierr = MPI_Allreduce(&zero,                           &flops,     1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1390         ierr = MPI_Allreduce(&zero,                           &mess,      1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1391         ierr = MPI_Allreduce(&zero,                           &messLen,   1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1392         ierr = MPI_Allreduce(&zero,                           &red,       1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1393         name = "";
1394       }
1395       mess *= 0.5; messLen *= 0.5; red /= size;
1396       if (TotalTime     != 0.0) fracTime       = stageTime/TotalTime;    else fracTime       = 0.0;
1397       if (TotalFlops    != 0.0) fracFlops      = flops/TotalFlops;       else fracFlops      = 0.0;
1398       /* Talk to Barry if (stageTime     != 0.0) flops          = (size*flops)/stageTime; else flops          = 0.0; */
1399       if (numMessages   != 0.0) fracMessages   = mess/numMessages;       else fracMessages   = 0.0;
1400       if (mess          != 0.0) avgMessLen     = messLen/mess;           else avgMessLen     = 0.0;
1401       if (messageLength != 0.0) fracLength     = messLen/messageLength;  else fracLength     = 0.0;
1402       if (numReductions != 0.0) fracReductions = red/numReductions;      else fracReductions = 0.0;
1403       ierr = 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",
1404                           stage, name, stageTime/size, 100.0*fracTime, flops, 100.0*fracFlops,
1405                           mess, 100.0*fracMessages, avgMessLen, 100.0*fracLength, red, 100.0*fracReductions);CHKERRQ(ierr);
1406     }
1407   }
1408 
1409   ierr = PetscFPrintf(comm, fd,"\n------------------------------------------------------------------------------------------------------------------------\n");CHKERRQ(ierr);
1410   ierr = PetscFPrintf(comm, fd, "See the 'Profiling' chapter of the users' manual for details on interpreting output.\n");CHKERRQ(ierr);
1411   ierr = PetscFPrintf(comm, fd, "Phase summary info:\n");CHKERRQ(ierr);
1412   ierr = PetscFPrintf(comm, fd, "   Count: number of times phase was executed\n");CHKERRQ(ierr);
1413   ierr = PetscFPrintf(comm, fd, "   Time and Flop: Max - maximum over all processors\n");CHKERRQ(ierr);
1414   ierr = PetscFPrintf(comm, fd, "                  Ratio - ratio of maximum to minimum over all processors\n");CHKERRQ(ierr);
1415   ierr = PetscFPrintf(comm, fd, "   Mess: number of messages sent\n");CHKERRQ(ierr);
1416   ierr = PetscFPrintf(comm, fd, "   AvgLen: average message length (bytes)\n");CHKERRQ(ierr);
1417   ierr = PetscFPrintf(comm, fd, "   Reduct: number of global reductions\n");CHKERRQ(ierr);
1418   ierr = PetscFPrintf(comm, fd, "   Global: entire computation\n");CHKERRQ(ierr);
1419   ierr = PetscFPrintf(comm, fd, "   Stage: stages of a computation. Set stages with PetscLogStagePush() and PetscLogStagePop().\n");CHKERRQ(ierr);
1420   ierr = PetscFPrintf(comm, fd, "      %%T - percent time in this phase         %%F - percent flop in this phase\n");CHKERRQ(ierr);
1421   ierr = PetscFPrintf(comm, fd, "      %%M - percent messages in this phase     %%L - percent message lengths in this phase\n");CHKERRQ(ierr);
1422   ierr = PetscFPrintf(comm, fd, "      %%R - percent reductions in this phase\n");CHKERRQ(ierr);
1423   ierr = PetscFPrintf(comm, fd, "   Total Mflop/s: 10e-6 * (sum of flop over all processors)/(max time over all processors)\n");CHKERRQ(ierr);
1424   ierr = PetscFPrintf(comm, fd, "------------------------------------------------------------------------------------------------------------------------\n");CHKERRQ(ierr);
1425 
1426   ierr = PetscLogViewWarnDebugging(comm,fd);CHKERRQ(ierr);
1427 
1428   /* Report events */
1429   ierr = PetscFPrintf(comm, fd,"Event                Count      Time (sec)     Flop                              --- Global ---  --- Stage ----  Total\n");CHKERRQ(ierr);
1430   ierr = PetscFPrintf(comm, fd,"                   Max Ratio  Max     Ratio   Max  Ratio  Mess   AvgLen  Reduct  %%T %%F %%M %%L %%R  %%T %%F %%M %%L %%R Mflop/s\n");CHKERRQ(ierr);
1431   ierr = PetscFPrintf(comm,fd,"------------------------------------------------------------------------------------------------------------------------\n");CHKERRQ(ierr);
1432 
1433   /* Problem: The stage name will not show up unless the stage executed on proc 1 */
1434   for (stage = 0; stage < numStages; stage++) {
1435     if (!stageVisible[stage]) continue;
1436     /* CANNOT use MPIU_Allreduce() since it might fail the line number check */
1437     if (localStageUsed[stage]) {
1438       ierr = PetscFPrintf(comm, fd, "\n--- Event Stage %d: %s\n\n", stage, stageInfo[stage].name);CHKERRQ(ierr);
1439       ierr = MPI_Allreduce(&stageInfo[stage].perfInfo.time,          &stageTime, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1440       ierr = MPI_Allreduce(&stageInfo[stage].perfInfo.flops,         &flops,     1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1441       ierr = MPI_Allreduce(&stageInfo[stage].perfInfo.numMessages,   &mess,      1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1442       ierr = MPI_Allreduce(&stageInfo[stage].perfInfo.messageLength, &messLen,   1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1443       ierr = MPI_Allreduce(&stageInfo[stage].perfInfo.numReductions, &red,       1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1444     } else {
1445       ierr = PetscFPrintf(comm, fd, "\n--- Event Stage %d: Unknown\n\n", stage);CHKERRQ(ierr);
1446       ierr = MPI_Allreduce(&zero,                           &stageTime, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1447       ierr = MPI_Allreduce(&zero,                           &flops,     1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1448       ierr = MPI_Allreduce(&zero,                           &mess,      1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1449       ierr = MPI_Allreduce(&zero,                           &messLen,   1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1450       ierr = MPI_Allreduce(&zero,                           &red,       1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1451     }
1452     mess *= 0.5; messLen *= 0.5; red /= size;
1453 
1454     /* Get total number of events in this stage --
1455        Currently, a single processor can register more events than another, but events must all be registered in order,
1456        just like stages. We can removed this requirement if necessary by having a global event numbering and indirection
1457        on the event ID. This seems best accomplished by associating a communicator with each stage.
1458 
1459        Problem: If the event did not happen on proc 1, its name will not be available.
1460        Problem: Event visibility is not implemented
1461     */
1462     if (localStageUsed[stage]) {
1463       eventInfo      = stageLog->stageInfo[stage].eventLog->eventInfo;
1464       localNumEvents = stageLog->stageInfo[stage].eventLog->numEvents;
1465     } else localNumEvents = 0;
1466     ierr = MPIU_Allreduce(&localNumEvents, &numEvents, 1, MPI_INT, MPI_MAX, comm);CHKERRQ(ierr);
1467     for (event = 0; event < numEvents; event++) {
1468       /* CANNOT use MPIU_Allreduce() since it might fail the line number check */
1469       if (localStageUsed[stage] && (event < stageLog->stageInfo[stage].eventLog->numEvents) && (eventInfo[event].depth == 0)) {
1470         if ((eventInfo[event].count > 0) && (eventInfo[event].time > 0.0)) flopr = eventInfo[event].flops; else flopr = 0.0;
1471         ierr = MPI_Allreduce(&flopr,                          &minf,  1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm);CHKERRQ(ierr);
1472         ierr = MPI_Allreduce(&flopr,                          &maxf,  1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm);CHKERRQ(ierr);
1473         ierr = MPI_Allreduce(&eventInfo[event].flops,         &totf,  1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1474         ierr = MPI_Allreduce(&eventInfo[event].time,          &mint,  1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm);CHKERRQ(ierr);
1475         ierr = MPI_Allreduce(&eventInfo[event].time,          &maxt,  1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm);CHKERRQ(ierr);
1476         ierr = MPI_Allreduce(&eventInfo[event].time,          &tott,  1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1477         ierr = MPI_Allreduce(&eventInfo[event].numMessages,   &totm,  1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1478         ierr = MPI_Allreduce(&eventInfo[event].messageLength, &totml, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1479         ierr = MPI_Allreduce(&eventInfo[event].numReductions, &totr,  1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1480         ierr = MPI_Allreduce(&eventInfo[event].count,         &minC,  1, MPI_INT,             MPI_MIN, comm);CHKERRQ(ierr);
1481         ierr = MPI_Allreduce(&eventInfo[event].count,         &maxC,  1, MPI_INT,             MPI_MAX, comm);CHKERRQ(ierr);
1482         name = stageLog->eventLog->eventInfo[event].name;
1483       } else {
1484         flopr = 0.0;
1485         ierr  = MPI_Allreduce(&flopr,                         &minf,  1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm);CHKERRQ(ierr);
1486         ierr  = MPI_Allreduce(&flopr,                         &maxf,  1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm);CHKERRQ(ierr);
1487         ierr  = MPI_Allreduce(&zero,                          &totf,  1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1488         ierr  = MPI_Allreduce(&zero,                          &mint,  1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm);CHKERRQ(ierr);
1489         ierr  = MPI_Allreduce(&zero,                          &maxt,  1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm);CHKERRQ(ierr);
1490         ierr  = MPI_Allreduce(&zero,                          &tott,  1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1491         ierr  = MPI_Allreduce(&zero,                          &totm,  1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1492         ierr  = MPI_Allreduce(&zero,                          &totml, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1493         ierr  = MPI_Allreduce(&zero,                          &totr,  1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm);CHKERRQ(ierr);
1494         ierr  = MPI_Allreduce(&ierr,                          &minC,  1, MPI_INT,             MPI_MIN, comm);CHKERRQ(ierr);
1495         ierr  = MPI_Allreduce(&ierr,                          &maxC,  1, MPI_INT,             MPI_MAX, comm);CHKERRQ(ierr);
1496         name  = "";
1497       }
1498       if (mint < 0.0) {
1499         ierr = 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);
1500         mint = 0;
1501       }
1502       if (minf < 0.0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Minimum flop %g over all processors for %s is negative! Not possible!",minf,name);
1503       totm *= 0.5; totml *= 0.5; totr /= size;
1504 
1505       if (maxC != 0) {
1506         if (minC          != 0)   ratC             = ((PetscLogDouble)maxC)/minC;else ratC             = 0.0;
1507         if (mint          != 0.0) ratt             = maxt/mint;                  else ratt             = 0.0;
1508         if (minf          != 0.0) ratf             = maxf/minf;                  else ratf             = 0.0;
1509         if (TotalTime     != 0.0) fracTime         = tott/TotalTime;             else fracTime         = 0.0;
1510         if (TotalFlops    != 0.0) fracFlops        = totf/TotalFlops;            else fracFlops        = 0.0;
1511         if (stageTime     != 0.0) fracStageTime    = tott/stageTime;             else fracStageTime    = 0.0;
1512         if (flops         != 0.0) fracStageFlops   = totf/flops;                 else fracStageFlops   = 0.0;
1513         if (numMessages   != 0.0) fracMess         = totm/numMessages;           else fracMess         = 0.0;
1514         if (messageLength != 0.0) fracMessLen      = totml/messageLength;        else fracMessLen      = 0.0;
1515         if (numReductions != 0.0) fracRed          = totr/numReductions;         else fracRed          = 0.0;
1516         if (mess          != 0.0) fracStageMess    = totm/mess;                  else fracStageMess    = 0.0;
1517         if (messLen       != 0.0) fracStageMessLen = totml/messLen;              else fracStageMessLen = 0.0;
1518         if (red           != 0.0) fracStageRed     = totr/red;                   else fracStageRed     = 0.0;
1519         if (totm          != 0.0) totml           /= totm;                       else totml            = 0.0;
1520         if (maxt          != 0.0) flopr            = totf/maxt;                  else flopr            = 0.0;
1521         if (fracStageTime > 1.00)  ierr = PetscFPrintf(comm, fd,"Warning -- total time of event greater than time of entire stage -- something is wrong with the timer\n");CHKERRQ(ierr);
1522         ierr = PetscFPrintf(comm, fd,
1523                             "%-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\n",
1524                             name, maxC, ratC, maxt, ratt, maxf, ratf, totm, totml, totr,
1525                             100.0*fracTime, 100.0*fracFlops, 100.0*fracMess, 100.0*fracMessLen, 100.0*fracRed,
1526                             100.0*fracStageTime, 100.0*fracStageFlops, 100.0*fracStageMess, 100.0*fracStageMessLen, 100.0*fracStageRed,
1527                             PetscAbs(flopr)/1.0e6);CHKERRQ(ierr);
1528       }
1529     }
1530   }
1531 
1532   /* Memory usage and object creation */
1533   ierr = PetscFPrintf(comm, fd, "------------------------------------------------------------------------------------------------------------------------\n");CHKERRQ(ierr);
1534   ierr = PetscFPrintf(comm, fd, "\n");CHKERRQ(ierr);
1535   ierr = PetscFPrintf(comm, fd, "Memory usage is given in bytes:\n\n");CHKERRQ(ierr);
1536 
1537   /* Right now, only stages on the first processor are reported here, meaning only objects associated with
1538      the global communicator, or MPI_COMM_SELF for proc 1. We really should report global stats and then
1539      stats for stages local to processor sets.
1540   */
1541   /* We should figure out the longest object name here (now 20 characters) */
1542   ierr = PetscFPrintf(comm, fd, "Object Type          Creations   Destructions     Memory  Descendants' Mem.\n");CHKERRQ(ierr);
1543   ierr = PetscFPrintf(comm, fd, "Reports information only for process 0.\n");CHKERRQ(ierr);
1544   for (stage = 0; stage < numStages; stage++) {
1545     if (localStageUsed[stage]) {
1546       classInfo = stageLog->stageInfo[stage].classLog->classInfo;
1547       ierr = PetscFPrintf(comm, fd, "\n--- Event Stage %d: %s\n\n", stage, stageInfo[stage].name);CHKERRQ(ierr);
1548       for (oclass = 0; oclass < stageLog->stageInfo[stage].classLog->numClasses; oclass++) {
1549         if ((classInfo[oclass].creations > 0) || (classInfo[oclass].destructions > 0)) {
1550           ierr = PetscFPrintf(comm, fd, "%20s %5d          %5d  %11.0f     %g\n", stageLog->classLog->classInfo[oclass].name,
1551                               classInfo[oclass].creations, classInfo[oclass].destructions, classInfo[oclass].mem,
1552                               classInfo[oclass].descMem);CHKERRQ(ierr);
1553         }
1554       }
1555     } else {
1556       ierr = PetscFPrintf(comm, fd, "\n--- Event Stage %d: Unknown\n\n", stage);CHKERRQ(ierr);
1557     }
1558   }
1559 
1560   ierr = PetscFree(localStageUsed);CHKERRQ(ierr);
1561   ierr = PetscFree(stageUsed);CHKERRQ(ierr);
1562   ierr = PetscFree(localStageVisible);CHKERRQ(ierr);
1563   ierr = PetscFree(stageVisible);CHKERRQ(ierr);
1564 
1565   /* Information unrelated to this particular run */
1566   ierr = PetscFPrintf(comm, fd, "========================================================================================================================\n");CHKERRQ(ierr);
1567   PetscTime(&y);
1568   PetscTime(&x);
1569   PetscTime(&y); PetscTime(&y); PetscTime(&y); PetscTime(&y); PetscTime(&y);
1570   PetscTime(&y); PetscTime(&y); PetscTime(&y); PetscTime(&y); PetscTime(&y);
1571   ierr = PetscFPrintf(comm,fd,"Average time to get PetscTime(): %g\n", (y-x)/10.0);CHKERRQ(ierr);
1572   /* MPI information */
1573   if (size > 1) {
1574     MPI_Status  status;
1575     PetscMPIInt tag;
1576     MPI_Comm    newcomm;
1577 
1578     ierr = MPI_Barrier(comm);CHKERRQ(ierr);
1579     PetscTime(&x);
1580     ierr = MPI_Barrier(comm);CHKERRQ(ierr);
1581     ierr = MPI_Barrier(comm);CHKERRQ(ierr);
1582     ierr = MPI_Barrier(comm);CHKERRQ(ierr);
1583     ierr = MPI_Barrier(comm);CHKERRQ(ierr);
1584     ierr = MPI_Barrier(comm);CHKERRQ(ierr);
1585     PetscTime(&y);
1586     ierr = PetscFPrintf(comm, fd, "Average time for MPI_Barrier(): %g\n", (y-x)/5.0);CHKERRQ(ierr);
1587     ierr = PetscCommDuplicate(comm,&newcomm, &tag);CHKERRQ(ierr);
1588     ierr = MPI_Barrier(comm);CHKERRQ(ierr);
1589     if (rank) {
1590       ierr = MPI_Recv(NULL, 0, MPI_INT, rank-1,            tag, newcomm, &status);CHKERRQ(ierr);
1591       ierr = MPI_Send(NULL, 0, MPI_INT, (rank+1)%size, tag, newcomm);CHKERRQ(ierr);
1592     } else {
1593       PetscTime(&x);
1594       ierr = MPI_Send(NULL, 0, MPI_INT, 1,          tag, newcomm);CHKERRQ(ierr);
1595       ierr = MPI_Recv(NULL, 0, MPI_INT, size-1, tag, newcomm, &status);CHKERRQ(ierr);
1596       PetscTime(&y);
1597       ierr = PetscFPrintf(comm,fd,"Average time for zero size MPI_Send(): %g\n", (y-x)/size);CHKERRQ(ierr);
1598     }
1599     ierr = PetscCommDestroy(&newcomm);CHKERRQ(ierr);
1600   }
1601   ierr = PetscOptionsView(NULL,viewer);CHKERRQ(ierr);
1602 
1603   /* Machine and compile information */
1604 #if defined(PETSC_USE_FORTRAN_KERNELS)
1605   ierr = PetscFPrintf(comm, fd, "Compiled with FORTRAN kernels\n");CHKERRQ(ierr);
1606 #else
1607   ierr = PetscFPrintf(comm, fd, "Compiled without FORTRAN kernels\n");CHKERRQ(ierr);
1608 #endif
1609 #if defined(PETSC_USE_64BIT_INDICES)
1610   ierr = PetscFPrintf(comm, fd, "Compiled with 64 bit PetscInt\n");CHKERRQ(ierr);
1611 #elif defined(PETSC_USE___FLOAT128)
1612   ierr = PetscFPrintf(comm, fd, "Compiled with 32 bit PetscInt\n");CHKERRQ(ierr);
1613 #endif
1614 #if defined(PETSC_USE_REAL_SINGLE)
1615   ierr = PetscFPrintf(comm, fd, "Compiled with single precision PetscScalar and PetscReal\n");CHKERRQ(ierr);
1616 #elif defined(PETSC_USE___FLOAT128)
1617   ierr = PetscFPrintf(comm, fd, "Compiled with 128 bit precision PetscScalar and PetscReal\n");CHKERRQ(ierr);
1618 #endif
1619 #if defined(PETSC_USE_REAL_MAT_SINGLE)
1620   ierr = PetscFPrintf(comm, fd, "Compiled with single precision matrices\n");CHKERRQ(ierr);
1621 #else
1622   ierr = PetscFPrintf(comm, fd, "Compiled with full precision matrices (default)\n");CHKERRQ(ierr);
1623 #endif
1624   ierr = PetscFPrintf(comm, fd, "sizeof(short) %d sizeof(int) %d sizeof(long) %d sizeof(void*) %d sizeof(PetscScalar) %d sizeof(PetscInt) %d\n",
1625                       (int) sizeof(short), (int) sizeof(int), (int) sizeof(long), (int) sizeof(void*),(int) sizeof(PetscScalar),(int) sizeof(PetscInt));CHKERRQ(ierr);
1626 
1627   ierr = PetscFPrintf(comm, fd, "Configure options: %s",petscconfigureoptions);CHKERRQ(ierr);
1628   ierr = PetscFPrintf(comm, fd, "%s", petscmachineinfo);CHKERRQ(ierr);
1629   ierr = PetscFPrintf(comm, fd, "%s", petsccompilerinfo);CHKERRQ(ierr);
1630   ierr = PetscFPrintf(comm, fd, "%s", petsccompilerflagsinfo);CHKERRQ(ierr);
1631   ierr = PetscFPrintf(comm, fd, "%s", petsclinkerinfo);CHKERRQ(ierr);
1632 
1633   /* Cleanup */
1634   ierr = PetscFPrintf(comm, fd, "\n");CHKERRQ(ierr);
1635   ierr = PetscLogViewWarnDebugging(comm,fd);CHKERRQ(ierr);
1636   PetscFunctionReturn(0);
1637 }
1638 
1639 /*@C
1640   PetscLogView - Prints a summary of the logging.
1641 
1642   Collective over MPI_Comm
1643 
1644   Input Parameter:
1645 .  viewer - an ASCII viewer
1646 
1647   Options Database Keys:
1648 +  -log_view [:filename] - Prints summary of log information
1649 .  -log_view :filename.py:ascii_info_detail - Saves logging information from each process as a Python file
1650 .  -log_view :filename.xml:ascii_xml - Saves a summary of the logging information in a nested format (see below for how to view it)
1651 .  -log_all - Saves a file Log.rank for each MPI process with details of each step of the computation
1652 -  -log_trace [filename] - Displays a trace of what each process is doing
1653 
1654   Notes:
1655   It is possible to control the logging programatically but we recommend using the options database approach whenever possible
1656   By default the summary is printed to stdout.
1657 
1658   Before calling this routine you must have called either PetscLogDefaultBegin() or PetscLogNestedBegin()
1659 
1660   If PETSc is configured with --with-logging=0 then this functionality is not available
1661 
1662   To view the nested XML format filename.xml first copy  ${PETSC_DIR}/share/petsc/xml/performance_xml2html.xsl to the current
1663   directory then open filename.xml with your browser. Specific notes for certain browsers
1664 $    Firefox and Internet explorer - simply open the file
1665 $    Google Chrome - you must start up Chrome with the option --allow-file-access-from-files
1666 $    Safari - see http://ccm.net/faq/36342-safari-how-to-enable-local-file-access
1667   or one can use the package http://xmlsoft.org/XSLT/xsltproc2.html to translate the xml file to html and then open it with
1668   your browser.
1669 
1670   The nested XML format was kindly donated by Koos Huijssen and Christiaan M. Klaij  MARITIME  RESEARCH  INSTITUTE  NETHERLANDS
1671 
1672   Level: beginner
1673 
1674 .keywords: log, dump, print
1675 .seealso: PetscLogDefaultBegin(), PetscLogDump()
1676 @*/
1677 PetscErrorCode  PetscLogView(PetscViewer viewer)
1678 {
1679   PetscErrorCode    ierr;
1680   PetscBool         isascii;
1681   PetscViewerFormat format;
1682   int               stage, lastStage;
1683   PetscStageLog     stageLog;
1684 
1685   PetscFunctionBegin;
1686   if (!PetscLogPLB) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Must use -log_view or PetscLogDefaultBegin() before calling this routine");
1687   /* Pop off any stages the user forgot to remove */
1688   lastStage = 0;
1689   ierr      = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
1690   ierr      = PetscStageLogGetCurrent(stageLog, &stage);CHKERRQ(ierr);
1691   while (stage >= 0) {
1692     lastStage = stage;
1693     ierr      = PetscStageLogPop(stageLog);CHKERRQ(ierr);
1694     ierr      = PetscStageLogGetCurrent(stageLog, &stage);CHKERRQ(ierr);
1695   }
1696   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);CHKERRQ(ierr);
1697   if (!isascii) SETERRQ(PetscObjectComm((PetscObject)viewer),PETSC_ERR_SUP,"Currently can only view logging to ASCII");
1698   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
1699   if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO) {
1700     ierr = PetscLogView_Default(viewer);CHKERRQ(ierr);
1701   } else if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
1702     ierr = PetscLogView_Detailed(viewer);CHKERRQ(ierr);
1703   } else if (format == PETSC_VIEWER_ASCII_XML) {
1704     ierr = PetscLogView_Nested(viewer);CHKERRQ(ierr);
1705   }
1706   ierr = PetscStageLogPush(stageLog, lastStage);CHKERRQ(ierr);
1707   PetscFunctionReturn(0);
1708 }
1709 
1710 /*@C
1711   PetscLogViewFromOptions - Processes command line options to determine if/how a PetscLog is to be viewed.
1712 
1713   Collective on PETSC_COMM_WORLD
1714 
1715   Not normally called by user
1716 
1717   Level: intermediate
1718 
1719 @*/
1720 PetscErrorCode PetscLogViewFromOptions(void)
1721 {
1722   PetscErrorCode    ierr;
1723   PetscViewer       viewer;
1724   PetscBool         flg;
1725   PetscViewerFormat format;
1726 
1727   PetscFunctionBegin;
1728   ierr   = PetscOptionsGetViewer(PETSC_COMM_WORLD,NULL,"-log_view",&viewer,&format,&flg);CHKERRQ(ierr);
1729   if (flg) {
1730     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
1731     ierr = PetscLogView(viewer);CHKERRQ(ierr);
1732     ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);
1733     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
1734   }
1735   PetscFunctionReturn(0);
1736 }
1737 
1738 
1739 
1740 /*----------------------------------------------- Counter Functions -------------------------------------------------*/
1741 /*@C
1742    PetscGetFlops - Returns the number of flops used on this processor
1743    since the program began.
1744 
1745    Not Collective
1746 
1747    Output Parameter:
1748    flops - number of floating point operations
1749 
1750    Notes:
1751    A global counter logs all PETSc flop counts.  The user can use
1752    PetscLogFlops() to increment this counter to include flops for the
1753    application code.
1754 
1755    Level: intermediate
1756 
1757 .keywords: log, flops, floating point operations
1758 
1759 .seealso: PetscTime(), PetscLogFlops()
1760 @*/
1761 PetscErrorCode  PetscGetFlops(PetscLogDouble *flops)
1762 {
1763   PetscFunctionBegin;
1764   *flops = petsc_TotalFlops;
1765   PetscFunctionReturn(0);
1766 }
1767 
1768 PetscErrorCode  PetscLogObjectState(PetscObject obj, const char format[], ...)
1769 {
1770   PetscErrorCode ierr;
1771   size_t         fullLength;
1772   va_list        Argp;
1773 
1774   PetscFunctionBegin;
1775   if (!petsc_logObjects) PetscFunctionReturn(0);
1776   va_start(Argp, format);
1777   ierr = PetscVSNPrintf(petsc_objects[obj->id].info, 64,format,&fullLength, Argp);CHKERRQ(ierr);
1778   va_end(Argp);
1779   PetscFunctionReturn(0);
1780 }
1781 
1782 
1783 /*MC
1784    PetscLogFlops - Adds floating point operations to the global counter.
1785 
1786    Synopsis:
1787    #include <petsclog.h>
1788    PetscErrorCode PetscLogFlops(PetscLogDouble f)
1789 
1790    Not Collective
1791 
1792    Input Parameter:
1793 .  f - flop counter
1794 
1795 
1796    Usage:
1797 .vb
1798      PetscLogEvent USER_EVENT;
1799      PetscLogEventRegister("User event",0,&USER_EVENT);
1800      PetscLogEventBegin(USER_EVENT,0,0,0,0);
1801         [code segment to monitor]
1802         PetscLogFlops(user_flops)
1803      PetscLogEventEnd(USER_EVENT,0,0,0,0);
1804 .ve
1805 
1806    Notes:
1807    A global counter logs all PETSc flop counts.  The user can use
1808    PetscLogFlops() to increment this counter to include flops for the
1809    application code.
1810 
1811    Level: intermediate
1812 
1813 .seealso: PetscLogEventRegister(), PetscLogEventBegin(), PetscLogEventEnd(), PetscGetFlops()
1814 
1815 .keywords: log, flops, floating point operations
1816 M*/
1817 
1818 /*MC
1819    PetscPreLoadBegin - Begin a segment of code that may be preloaded (run twice)
1820     to get accurate timings
1821 
1822    Synopsis:
1823    #include <petsclog.h>
1824    void PetscPreLoadBegin(PetscBool  flag,char *name);
1825 
1826    Not Collective
1827 
1828    Input Parameter:
1829 +   flag - PETSC_TRUE to run twice, PETSC_FALSE to run once, may be overridden
1830            with command line option -preload true or -preload false
1831 -   name - name of first stage (lines of code timed separately with -log_view) to
1832            be preloaded
1833 
1834    Usage:
1835 .vb
1836      PetscPreLoadBegin(PETSC_TRUE,"first stage);
1837        lines of code
1838        PetscPreLoadStage("second stage");
1839        lines of code
1840      PetscPreLoadEnd();
1841 .ve
1842 
1843    Notes:
1844     Only works in C/C++, not Fortran
1845 
1846      Flags available within the macro.
1847 +    PetscPreLoadingUsed - true if we are or have done preloading
1848 .    PetscPreLoadingOn - true if it is CURRENTLY doing preload
1849 .    PetscPreLoadIt - 0 for the first computation (with preloading turned off it is only 0) 1 for the second
1850 -    PetscPreLoadMax - number of times it will do the computation, only one when preloading is turned on
1851      The first two variables are available throughout the program, the second two only between the PetscPreLoadBegin()
1852      and PetscPreLoadEnd()
1853 
1854    Level: intermediate
1855 
1856 .seealso: PetscLogEventRegister(), PetscLogEventBegin(), PetscLogEventEnd(), PetscPreLoadEnd(), PetscPreLoadStage()
1857 
1858    Concepts: preloading
1859    Concepts: timing^accurate
1860    Concepts: paging^eliminating effects of
1861 
1862 
1863 M*/
1864 
1865 /*MC
1866    PetscPreLoadEnd - End a segment of code that may be preloaded (run twice)
1867     to get accurate timings
1868 
1869    Synopsis:
1870    #include <petsclog.h>
1871    void PetscPreLoadEnd(void);
1872 
1873    Not Collective
1874 
1875    Usage:
1876 .vb
1877      PetscPreLoadBegin(PETSC_TRUE,"first stage);
1878        lines of code
1879        PetscPreLoadStage("second stage");
1880        lines of code
1881      PetscPreLoadEnd();
1882 .ve
1883 
1884    Notes:
1885     only works in C/C++ not fortran
1886 
1887    Level: intermediate
1888 
1889 .seealso: PetscLogEventRegister(), PetscLogEventBegin(), PetscLogEventEnd(), PetscPreLoadBegin(), PetscPreLoadStage()
1890 
1891 M*/
1892 
1893 /*MC
1894    PetscPreLoadStage - Start a new segment of code to be timed separately.
1895     to get accurate timings
1896 
1897    Synopsis:
1898    #include <petsclog.h>
1899    void PetscPreLoadStage(char *name);
1900 
1901    Not Collective
1902 
1903    Usage:
1904 .vb
1905      PetscPreLoadBegin(PETSC_TRUE,"first stage);
1906        lines of code
1907        PetscPreLoadStage("second stage");
1908        lines of code
1909      PetscPreLoadEnd();
1910 .ve
1911 
1912    Notes:
1913     only works in C/C++ not fortran
1914 
1915    Level: intermediate
1916 
1917 .seealso: PetscLogEventRegister(), PetscLogEventBegin(), PetscLogEventEnd(), PetscPreLoadBegin(), PetscPreLoadEnd()
1918 
1919 M*/
1920 
1921 
1922 #else /* end of -DPETSC_USE_LOG section */
1923 
1924 PetscErrorCode  PetscLogObjectState(PetscObject obj, const char format[], ...)
1925 {
1926   PetscFunctionBegin;
1927   PetscFunctionReturn(0);
1928 }
1929 
1930 #endif /* PETSC_USE_LOG*/
1931 
1932 
1933 PetscClassId PETSC_LARGEST_CLASSID = PETSC_SMALLEST_CLASSID;
1934 PetscClassId PETSC_OBJECT_CLASSID  = 0;
1935 
1936 /*@C
1937   PetscClassIdRegister - Registers a new class name for objects and logging operations in an application code.
1938 
1939   Not Collective
1940 
1941   Input Parameter:
1942 . name   - The class name
1943 
1944   Output Parameter:
1945 . oclass - The class id or classid
1946 
1947   Level: developer
1948 
1949 .keywords: log, class, register
1950 
1951 @*/
1952 PetscErrorCode  PetscClassIdRegister(const char name[],PetscClassId *oclass)
1953 {
1954 #if defined(PETSC_USE_LOG)
1955   PetscStageLog  stageLog;
1956   PetscInt       stage;
1957   PetscErrorCode ierr;
1958 #endif
1959 
1960   PetscFunctionBegin;
1961   *oclass = ++PETSC_LARGEST_CLASSID;
1962 #if defined(PETSC_USE_LOG)
1963   ierr = PetscLogGetStageLog(&stageLog);CHKERRQ(ierr);
1964   ierr = PetscClassRegLogRegister(stageLog->classLog, name, *oclass);CHKERRQ(ierr);
1965   for (stage = 0; stage < stageLog->numStages; stage++) {
1966     ierr = PetscClassPerfLogEnsureSize(stageLog->stageInfo[stage].classLog, stageLog->classLog->numClasses);CHKERRQ(ierr);
1967   }
1968 #endif
1969   PetscFunctionReturn(0);
1970 }
1971 
1972 #if defined(PETSC_USE_LOG) && defined(PETSC_HAVE_MPE)
1973 #include <mpe.h>
1974 
1975 PetscBool PetscBeganMPE = PETSC_FALSE;
1976 
1977 PETSC_INTERN PetscErrorCode PetscLogEventBeginMPE(PetscLogEvent,int,PetscObject,PetscObject,PetscObject,PetscObject);
1978 PETSC_INTERN PetscErrorCode PetscLogEventEndMPE(PetscLogEvent,int,PetscObject,PetscObject,PetscObject,PetscObject);
1979 
1980 /*@C
1981    PetscLogMPEBegin - Turns on MPE logging of events. This creates large log files
1982    and slows the program down.
1983 
1984    Collective over PETSC_COMM_WORLD
1985 
1986    Options Database Keys:
1987 . -log_mpe - Prints extensive log information
1988 
1989    Notes:
1990    A related routine is PetscLogDefaultBegin() (with the options key -log_view), which is
1991    intended for production runs since it logs only flop rates and object
1992    creation (and should not significantly slow the programs).
1993 
1994    Level: advanced
1995 
1996    Concepts: logging^MPE
1997    Concepts: logging^message passing
1998 
1999 .seealso: PetscLogDump(), PetscLogDefaultBegin(), PetscLogAllBegin(), PetscLogEventActivate(),
2000           PetscLogEventDeactivate()
2001 @*/
2002 PetscErrorCode  PetscLogMPEBegin(void)
2003 {
2004   PetscErrorCode ierr;
2005 
2006   PetscFunctionBegin;
2007   /* Do MPE initialization */
2008   if (!MPE_Initialized_logging()) { /* This function exists in mpich 1.1.2 and higher */
2009     ierr = PetscInfo(0,"Initializing MPE.\n");CHKERRQ(ierr);
2010     ierr = MPE_Init_log();CHKERRQ(ierr);
2011 
2012     PetscBeganMPE = PETSC_TRUE;
2013   } else {
2014     ierr = PetscInfo(0,"MPE already initialized. Not attempting to reinitialize.\n");CHKERRQ(ierr);
2015   }
2016   ierr = PetscLogSet(PetscLogEventBeginMPE, PetscLogEventEndMPE);CHKERRQ(ierr);
2017   PetscFunctionReturn(0);
2018 }
2019 
2020 /*@C
2021    PetscLogMPEDump - Dumps the MPE logging info to file for later use with Jumpshot.
2022 
2023    Collective over PETSC_COMM_WORLD
2024 
2025    Level: advanced
2026 
2027 .seealso: PetscLogDump(), PetscLogAllBegin(), PetscLogMPEBegin()
2028 @*/
2029 PetscErrorCode  PetscLogMPEDump(const char sname[])
2030 {
2031   char           name[PETSC_MAX_PATH_LEN];
2032   PetscErrorCode ierr;
2033 
2034   PetscFunctionBegin;
2035   if (PetscBeganMPE) {
2036     ierr = PetscInfo(0,"Finalizing MPE.\n");CHKERRQ(ierr);
2037     if (sname) {
2038       ierr = PetscStrcpy(name,sname);CHKERRQ(ierr);
2039     } else {
2040       ierr = PetscGetProgramName(name,PETSC_MAX_PATH_LEN);CHKERRQ(ierr);
2041     }
2042     ierr = MPE_Finish_log(name);CHKERRQ(ierr);
2043   } else {
2044     ierr = PetscInfo(0,"Not finalizing MPE (not started by PETSc).\n");CHKERRQ(ierr);
2045   }
2046   PetscFunctionReturn(0);
2047 }
2048 
2049 #define PETSC_RGB_COLORS_MAX 39
2050 static const char *PetscLogMPERGBColors[PETSC_RGB_COLORS_MAX] = {
2051   "OliveDrab:      ",
2052   "BlueViolet:     ",
2053   "CadetBlue:      ",
2054   "CornflowerBlue: ",
2055   "DarkGoldenrod:  ",
2056   "DarkGreen:      ",
2057   "DarkKhaki:      ",
2058   "DarkOliveGreen: ",
2059   "DarkOrange:     ",
2060   "DarkOrchid:     ",
2061   "DarkSeaGreen:   ",
2062   "DarkSlateGray:  ",
2063   "DarkTurquoise:  ",
2064   "DeepPink:       ",
2065   "DarkKhaki:      ",
2066   "DimGray:        ",
2067   "DodgerBlue:     ",
2068   "GreenYellow:    ",
2069   "HotPink:        ",
2070   "IndianRed:      ",
2071   "LavenderBlush:  ",
2072   "LawnGreen:      ",
2073   "LemonChiffon:   ",
2074   "LightCoral:     ",
2075   "LightCyan:      ",
2076   "LightPink:      ",
2077   "LightSalmon:    ",
2078   "LightSlateGray: ",
2079   "LightYellow:    ",
2080   "LimeGreen:      ",
2081   "MediumPurple:   ",
2082   "MediumSeaGreen: ",
2083   "MediumSlateBlue:",
2084   "MidnightBlue:   ",
2085   "MintCream:      ",
2086   "MistyRose:      ",
2087   "NavajoWhite:    ",
2088   "NavyBlue:       ",
2089   "OliveDrab:      "
2090 };
2091 
2092 /*@C
2093   PetscLogMPEGetRGBColor - This routine returns a rgb color useable with PetscLogEventRegister()
2094 
2095   Not collective. Maybe it should be?
2096 
2097   Output Parameter
2098 . str - character string representing the color
2099 
2100   Level: developer
2101 
2102 .keywords: log, mpe , color
2103 .seealso: PetscLogEventRegister
2104 @*/
2105 PetscErrorCode  PetscLogMPEGetRGBColor(const char *str[])
2106 {
2107   static int idx = 0;
2108 
2109   PetscFunctionBegin;
2110   *str = PetscLogMPERGBColors[idx];
2111   idx  = (idx + 1)% PETSC_RGB_COLORS_MAX;
2112   PetscFunctionReturn(0);
2113 }
2114 
2115 #endif /* PETSC_USE_LOG && PETSC_HAVE_MPE */
2116