Actual source code: logdefault.c

  1: #include <petsc/private/logimpl.h>
  2: #include <petsc/private/loghandlerimpl.h>
  3: #include <petsc/private/deviceimpl.h>
  4: #include <petscconfiginfo.h>
  5: #include <petscmachineinfo.h>
  6: #include "logdefault.h"

  8: static PetscErrorCode PetscEventPerfInfoInit(PetscEventPerfInfo *eventInfo)
  9: {
 10:   PetscFunctionBegin;
 11:   PetscCall(PetscMemzero(eventInfo, sizeof(*eventInfo)));
 12:   eventInfo->visible   = PETSC_TRUE;
 13:   eventInfo->id        = -1;
 14:   eventInfo->dof[0]    = -1.0;
 15:   eventInfo->dof[1]    = -1.0;
 16:   eventInfo->dof[2]    = -1.0;
 17:   eventInfo->dof[3]    = -1.0;
 18:   eventInfo->dof[4]    = -1.0;
 19:   eventInfo->dof[5]    = -1.0;
 20:   eventInfo->dof[6]    = -1.0;
 21:   eventInfo->dof[7]    = -1.0;
 22:   eventInfo->errors[0] = -1.0;
 23:   eventInfo->errors[1] = -1.0;
 24:   eventInfo->errors[2] = -1.0;
 25:   eventInfo->errors[3] = -1.0;
 26:   eventInfo->errors[4] = -1.0;
 27:   eventInfo->errors[5] = -1.0;
 28:   eventInfo->errors[6] = -1.0;
 29:   eventInfo->errors[7] = -1.0;
 30:   PetscFunctionReturn(PETSC_SUCCESS);
 31: }

 33: static PetscErrorCode PetscEventPerfInfoTic_Internal(PetscEventPerfInfo *eventInfo, PetscLogDouble time, PetscBool logMemory, int event, PetscBool resume)
 34: {
 35:   PetscFunctionBegin;
 36:   if (resume) {
 37:     eventInfo->timeTmp -= time;
 38:     eventInfo->flopsTmp -= petsc_TotalFlops_th;
 39:   } else {
 40:     eventInfo->timeTmp  = -time;
 41:     eventInfo->flopsTmp = -petsc_TotalFlops_th;
 42:   }
 43:   eventInfo->numMessages -= petsc_irecv_ct_th + petsc_isend_ct_th + petsc_recv_ct_th + petsc_send_ct_th;
 44:   eventInfo->messageLength -= petsc_irecv_len_th + petsc_isend_len_th + petsc_recv_len_th + petsc_send_len_th;
 45:   eventInfo->numReductions -= petsc_allreduce_ct_th + petsc_gather_ct_th + petsc_scatter_ct_th;
 46: #if PetscDefined(HAVE_DEVICE)
 47:   eventInfo->CpuToGpuCount -= petsc_ctog_ct_th;
 48:   eventInfo->GpuToCpuCount -= petsc_gtoc_ct_th;
 49:   eventInfo->CpuToGpuSize -= petsc_ctog_sz_th;
 50:   eventInfo->GpuToCpuSize -= petsc_gtoc_sz_th;
 51:   eventInfo->GpuFlops -= petsc_gflops_th;
 52:   eventInfo->GpuTime -= petsc_gtime;
 53:   if (PetscLogGpuEnergyFlag) eventInfo->GpuEnergy -= petsc_genergy;
 54:   if (PetscLogGpuEnergyMeterFlag) eventInfo->GpuEnergy -= petsc_genergy_meter;
 55: #endif
 56:   if (logMemory) {
 57:     PetscLogDouble usage;
 58:     PetscCall(PetscMemoryGetCurrentUsage(&usage));
 59:     eventInfo->memIncrease -= usage;
 60:     PetscCall(PetscMallocGetCurrentUsage(&usage));
 61:     eventInfo->mallocSpace -= usage;
 62:     PetscCall(PetscMallocGetMaximumUsage(&usage));
 63:     eventInfo->mallocIncrease -= usage;
 64:     PetscCall(PetscMallocPushMaximumUsage(event));
 65:   }
 66:   PetscFunctionReturn(PETSC_SUCCESS);
 67: }

 69: static PetscErrorCode PetscEventPerfInfoTic(PetscEventPerfInfo *eventInfo, PetscLogDouble time, PetscBool logMemory, int event)
 70: {
 71:   PetscFunctionBegin;
 72:   PetscCall(PetscEventPerfInfoTic_Internal(eventInfo, time, logMemory, event, PETSC_FALSE));
 73:   PetscFunctionReturn(PETSC_SUCCESS);
 74: }

 76: static PetscErrorCode PetscEventPerfInfoResume(PetscEventPerfInfo *eventInfo, PetscLogDouble time, PetscBool logMemory, int event)
 77: {
 78:   PetscFunctionBegin;
 79:   PetscCall(PetscEventPerfInfoTic_Internal(eventInfo, time, logMemory, event, PETSC_TRUE));
 80:   PetscFunctionReturn(PETSC_SUCCESS);
 81: }

 83: static PetscErrorCode PetscEventPerfInfoToc_Internal(PetscEventPerfInfo *eventInfo, PetscLogDouble time, PetscBool logMemory, int event, PetscBool pause)
 84: {
 85:   PetscFunctionBegin;
 86:   eventInfo->timeTmp += time;
 87:   eventInfo->flopsTmp += petsc_TotalFlops_th;
 88:   if (!pause) {
 89:     eventInfo->time += eventInfo->timeTmp;
 90:     eventInfo->time2 += eventInfo->timeTmp * eventInfo->timeTmp;
 91:     eventInfo->flops += eventInfo->flopsTmp;
 92:     eventInfo->flops2 += eventInfo->flopsTmp * eventInfo->flopsTmp;
 93:   }
 94:   eventInfo->numMessages += petsc_irecv_ct_th + petsc_isend_ct_th + petsc_recv_ct_th + petsc_send_ct_th;
 95:   eventInfo->messageLength += petsc_irecv_len_th + petsc_isend_len_th + petsc_recv_len + petsc_send_len_th;
 96:   eventInfo->numReductions += petsc_allreduce_ct_th + petsc_gather_ct_th + petsc_scatter_ct_th;
 97: #if PetscDefined(HAVE_DEVICE)
 98:   eventInfo->CpuToGpuCount += petsc_ctog_ct_th;
 99:   eventInfo->GpuToCpuCount += petsc_gtoc_ct_th;
100:   eventInfo->CpuToGpuSize += petsc_ctog_sz_th;
101:   eventInfo->GpuToCpuSize += petsc_gtoc_sz_th;
102:   eventInfo->GpuFlops += petsc_gflops_th;
103:   eventInfo->GpuTime += petsc_gtime;
104:   if (PetscLogGpuEnergyFlag) eventInfo->GpuEnergy += petsc_genergy;
105:   if (PetscLogGpuEnergyMeterFlag) eventInfo->GpuEnergy += petsc_genergy_meter;
106: #endif
107:   if (logMemory) {
108:     PetscLogDouble usage, musage;
109:     PetscCall(PetscMemoryGetCurrentUsage(&usage)); /* the comments below match the column labels printed in PetscLogView_Default() */
110:     eventInfo->memIncrease += usage;               /* RMI */
111:     PetscCall(PetscMallocGetCurrentUsage(&usage));
112:     eventInfo->mallocSpace += usage; /* Malloc */
113:     PetscCall(PetscMallocPopMaximumUsage(event, &musage));
114:     eventInfo->mallocIncreaseEvent = PetscMax(musage - usage, eventInfo->mallocIncreaseEvent); /* EMalloc */
115:     PetscCall(PetscMallocGetMaximumUsage(&usage));
116:     eventInfo->mallocIncrease += usage; /* MMalloc */
117:   }
118:   PetscFunctionReturn(PETSC_SUCCESS);
119: }

121: static PetscErrorCode PetscEventPerfInfoToc(PetscEventPerfInfo *eventInfo, PetscLogDouble time, PetscBool logMemory, int event)
122: {
123:   PetscFunctionBegin;
124:   PetscCall(PetscEventPerfInfoToc_Internal(eventInfo, time, logMemory, event, PETSC_FALSE));
125:   PetscFunctionReturn(PETSC_SUCCESS);
126: }

128: static PetscErrorCode PetscEventPerfInfoPause(PetscEventPerfInfo *eventInfo, PetscLogDouble time, PetscBool logMemory, int event)
129: {
130:   PetscFunctionBegin;
131:   PetscCall(PetscEventPerfInfoToc_Internal(eventInfo, time, logMemory, event, PETSC_TRUE));
132:   PetscFunctionReturn(PETSC_SUCCESS);
133: }

135: static PetscErrorCode PetscEventPerfInfoAdd_Internal(const PetscEventPerfInfo *eventInfo, PetscEventPerfInfo *outInfo)
136: {
137:   PetscFunctionBegin;
138:   outInfo->count += eventInfo->count;
139:   outInfo->time += eventInfo->time;
140:   outInfo->time2 += eventInfo->time2;
141:   outInfo->flops += eventInfo->flops;
142:   outInfo->flops2 += eventInfo->flops2;
143:   outInfo->numMessages += eventInfo->numMessages;
144:   outInfo->messageLength += eventInfo->messageLength;
145:   outInfo->numReductions += eventInfo->numReductions;
146: #if PetscDefined(HAVE_DEVICE)
147:   outInfo->CpuToGpuCount += eventInfo->CpuToGpuCount;
148:   outInfo->GpuToCpuCount += eventInfo->GpuToCpuCount;
149:   outInfo->CpuToGpuSize += eventInfo->CpuToGpuSize;
150:   outInfo->GpuToCpuSize += eventInfo->GpuToCpuSize;
151:   outInfo->GpuFlops += eventInfo->GpuFlops;
152:   outInfo->GpuTime += eventInfo->GpuTime;
153:   outInfo->GpuEnergy += eventInfo->GpuEnergy;
154: #endif
155:   outInfo->memIncrease += eventInfo->memIncrease;
156:   outInfo->mallocSpace += eventInfo->mallocSpace;
157:   outInfo->mallocIncreaseEvent += eventInfo->mallocIncreaseEvent;
158:   outInfo->mallocIncrease += eventInfo->mallocIncrease;
159:   PetscFunctionReturn(PETSC_SUCCESS);
160: }

162: PETSC_LOG_RESIZABLE_ARRAY(EventPerfArray, PetscEventPerfInfo, PetscLogEvent, PetscEventPerfInfoInit, NULL, NULL)

164: /* --- PetscClassPerf --- */

166: typedef struct {
167:   int            creations;    /* The number of objects of this class created */
168:   int            destructions; /* The number of objects of this class destroyed */
169:   PetscLogDouble mem;          /* The total memory allocated by objects of this class; this is completely wrong and should possibly be removed */
170:   PetscLogDouble descMem;      /* The total memory allocated by descendents of these objects; this is completely wrong and should possibly be removed */
171: } PetscClassPerf;

173: static PetscErrorCode PetscClassPerfInit(PetscClassPerf *classInfo)
174: {
175:   PetscFunctionBegin;
176:   PetscCall(PetscMemzero(classInfo, sizeof(*classInfo)));
177:   PetscFunctionReturn(PETSC_SUCCESS);
178: }

180: PETSC_LOG_RESIZABLE_ARRAY(ClassPerfArray, PetscClassPerf, PetscLogClass, PetscClassPerfInit, NULL, NULL)

182: /* --- PetscStagePerf --- */

184: typedef struct _PetscStagePerf {
185:   PetscBool              used;     /* The stage was pushed on this processor */
186:   PetscEventPerfInfo     perfInfo; /* The stage performance information */
187:   PetscLogEventPerfArray eventLog; /* The event information for this stage */
188:   PetscLogClassPerfArray classLog; /* The class information for this stage */
189: } PetscStagePerf;

191: static PetscErrorCode PetscStageInfoInit(PetscStagePerf *stageInfo)
192: {
193:   PetscFunctionBegin;
194:   PetscCall(PetscMemzero(stageInfo, sizeof(*stageInfo)));
195:   PetscCall(PetscLogEventPerfArrayCreate(128, &stageInfo->eventLog));
196:   PetscCall(PetscLogClassPerfArrayCreate(128, &stageInfo->classLog));
197:   PetscCall(PetscEventPerfInfoInit(&stageInfo->perfInfo));
198:   PetscFunctionReturn(PETSC_SUCCESS);
199: }

201: static PetscErrorCode PetscStageInfoReset(PetscStagePerf *stageInfo)
202: {
203:   PetscFunctionBegin;
204:   PetscCall(PetscLogEventPerfArrayDestroy(&stageInfo->eventLog));
205:   PetscCall(PetscLogClassPerfArrayDestroy(&stageInfo->classLog));
206:   PetscFunctionReturn(PETSC_SUCCESS);
207: }

209: PETSC_LOG_RESIZABLE_ARRAY(StageInfoArray, PetscStagePerf, PetscLogStage, PetscStageInfoInit, PetscStageInfoReset, NULL)

211: /* --- Action --- */

213: /* The structure for action logging */
214: typedef enum {
215:   PETSC_LOG_ACTION_CREATE,
216:   PETSC_LOG_ACTION_DESTROY,
217:   PETSC_LOG_ACTION_BEGIN,
218:   PETSC_LOG_ACTION_END,
219: } PetscLogActionType;

221: typedef struct _Action {
222:   PetscLogActionType action;        /* The type of execution */
223:   PetscLogEvent      event;         /* The event number */
224:   PetscClassId       classid;       /* The event class id */
225:   PetscLogDouble     time;          /* The time of occurrence */
226:   PetscLogDouble     flops;         /* The cumulative flops */
227:   PetscLogDouble     mem;           /* The current memory usage */
228:   PetscLogDouble     maxmem;        /* The maximum memory usage */
229:   PetscObjectId      id1, id2, id3; /* The ids of associated objects */
230: } Action;

232: PETSC_LOG_RESIZABLE_ARRAY(ActionArray, Action, PetscLogEvent, NULL, NULL, NULL)

234: /* --- Object --- */

236: /* The structure for object logging */
237: typedef struct _Object {
238:   PetscObject    obj;      /* The associated PetscObject */
239:   int            parent;   /* The parent id */
240:   PetscLogDouble mem;      /* The memory associated with the object */
241:   char           name[64]; /* The object name */
242:   char           info[64]; /* The information string */
243: } Object;

245: PETSC_LOG_RESIZABLE_ARRAY(ObjectArray, Object, PetscObject, NULL, NULL, NULL)

247: /* Map from (threadid,stage,event) to perfInfo data struct */
248: #include <petsc/private/hashmapijk.h>

250: PETSC_HASH_MAP(HMapEvent, PetscHashIJKKey, PetscEventPerfInfo *, PetscHashIJKKeyHash, PetscHashIJKKeyEqual, NULL)

252: typedef struct _n_PetscLogHandler_Default *PetscLogHandler_Default;
253: struct _n_PetscLogHandler_Default {
254:   PetscLogStageInfoArray stages;
255:   PetscSpinlock          lock;
256:   PetscLogActionArray    petsc_actions;
257:   PetscLogObjectArray    petsc_objects;
258:   PetscBool              petsc_logActions;
259:   PetscBool              petsc_logObjects;
260:   int                    petsc_numObjectsCreated;
261:   int                    petsc_numObjectsDestroyed;
262:   PetscHMapEvent         eventInfoMap_th;
263:   int                    pause_depth;
264:   PetscBool              use_threadsafe;
265: };

267: /* --- PetscLogHandler_Default --- */

269: static PetscErrorCode PetscLogHandlerContextCreate_Default(PetscLogHandler_Default *def_p)
270: {
271:   PetscLogHandler_Default def;

273:   PetscFunctionBegin;
274:   PetscCall(PetscNew(def_p));
275:   def = *def_p;
276:   PetscCall(PetscLogStageInfoArrayCreate(8, &def->stages));
277:   PetscCall(PetscLogActionArrayCreate(64, &def->petsc_actions));
278:   PetscCall(PetscLogObjectArrayCreate(64, &def->petsc_objects));
279:   PetscCall(PetscSpinlockCreate(&def->lock));

281:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_include_actions", &def->petsc_logActions, NULL));
282:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_include_objects", &def->petsc_logObjects, NULL));
283:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-log_handler_default_use_threadsafe_events", &def->use_threadsafe, NULL));
284:   if (PetscDefined(HAVE_THREADSAFETY) || def->use_threadsafe) PetscCall(PetscHMapEventCreate(&def->eventInfoMap_th));
285:   PetscFunctionReturn(PETSC_SUCCESS);
286: }

288: static PetscErrorCode PetscLogHandlerDestroy_Default(PetscLogHandler h)
289: {
290:   PetscLogHandler_Default def = (PetscLogHandler_Default)h->data;

292:   PetscFunctionBegin;
293:   PetscCall(PetscLogStageInfoArrayDestroy(&def->stages));
294:   PetscCall(PetscLogActionArrayDestroy(&def->petsc_actions));
295:   PetscCall(PetscLogObjectArrayDestroy(&def->petsc_objects));
296:   PetscCall(PetscSpinlockDestroy(&def->lock));
297:   if (def->eventInfoMap_th) {
298:     PetscEventPerfInfo **array;
299:     PetscInt             n, off = 0;

301:     PetscCall(PetscHMapEventGetSize(def->eventInfoMap_th, &n));
302:     PetscCall(PetscMalloc1(n, &array));
303:     PetscCall(PetscHMapEventGetVals(def->eventInfoMap_th, &off, array));
304:     for (PetscInt i = 0; i < n; i++) PetscCall(PetscFree(array[i]));
305:     PetscCall(PetscFree(array));
306:     PetscCall(PetscHMapEventDestroy(&def->eventInfoMap_th));
307:   }
308:   PetscCall(PetscObjectComposeFunction((PetscObject)h, "PetscLogHandlerGetEventPerfInfo_C", NULL));
309:   PetscCall(PetscObjectComposeFunction((PetscObject)h, "PetscLogHandlerGetStagePerfInfo_C", NULL));
310:   PetscCall(PetscObjectComposeFunction((PetscObject)h, "PetscLogHandlerSetLogActions_C", NULL));
311:   PetscCall(PetscObjectComposeFunction((PetscObject)h, "PetscLogHandlerSetLogObjects_C", NULL));
312:   PetscCall(PetscObjectComposeFunction((PetscObject)h, "PetscLogHandlerLogObjectState_C", NULL));
313:   PetscCall(PetscObjectComposeFunction((PetscObject)h, "PetscLogHandlerGetNumObjects_C", NULL));
314:   PetscCall(PetscObjectComposeFunction((PetscObject)h, "PetscLogHandlerEventDeactivatePush_C", NULL));
315:   PetscCall(PetscObjectComposeFunction((PetscObject)h, "PetscLogHandlerEventDeactivatePop_C", NULL));
316:   PetscCall(PetscObjectComposeFunction((PetscObject)h, "PetscLogHandlerEventsPause_C", NULL));
317:   PetscCall(PetscObjectComposeFunction((PetscObject)h, "PetscLogHandlerEventsResume_C", NULL));
318:   PetscCall(PetscObjectComposeFunction((PetscObject)h, "PetscLogHandlerDump_C", NULL));
319:   PetscCall(PetscObjectComposeFunction((PetscObject)h, "PetscLogHandlerStageSetVisible_C", NULL));
320:   PetscCall(PetscObjectComposeFunction((PetscObject)h, "PetscLogHandlerStageGetVisible_C", NULL));
321:   PetscCall(PetscFree(def));
322:   PetscFunctionReturn(PETSC_SUCCESS);
323: }

325: static PetscErrorCode PetscLogHandlerDefaultGetStageInfo(PetscLogHandler handler, PetscLogStage stage, PetscStagePerf **stage_info_p)
326: {
327:   PetscStagePerf         *stage_info = NULL;
328:   PetscLogHandler_Default def        = (PetscLogHandler_Default)handler->data;

330:   PetscFunctionBegin;
331:   PetscCall(PetscLogStageInfoArrayResize(def->stages, stage + 1));
332:   PetscCall(PetscLogStageInfoArrayGetRef(def->stages, stage, &stage_info));
333:   *stage_info_p = stage_info;
334:   PetscFunctionReturn(PETSC_SUCCESS);
335: }

337: static PetscErrorCode PetscLogHandlerGetEventPerfInfo_Default(PetscLogHandler handler, PetscLogStage stage, PetscLogEvent event, PetscEventPerfInfo **event_info_p)
338: {
339:   PetscEventPerfInfo    *event_info = NULL;
340:   PetscStagePerf        *stage_info = NULL;
341:   PetscLogEventPerfArray event_log;

343:   PetscFunctionBegin;
344:   if (stage < 0) PetscCall(PetscLogStateGetCurrentStage(handler->state, &stage));
345:   PetscCall(PetscLogHandlerDefaultGetStageInfo(handler, stage, &stage_info));
346:   event_log = stage_info->eventLog;
347:   PetscCall(PetscLogEventPerfArrayResize(event_log, event + 1));
348:   PetscCall(PetscLogEventPerfArrayGetRef(event_log, event, &event_info));
349:   event_info->id = event;
350:   *event_info_p  = event_info;
351:   PetscFunctionReturn(PETSC_SUCCESS);
352: }

354: static PetscErrorCode PetscLogHandlerGetStagePerfInfo_Default(PetscLogHandler handler, PetscLogStage stage, PetscEventPerfInfo **stage_info_p)
355: {
356:   PetscStagePerf *stage_perf_info = NULL;

358:   PetscFunctionBegin;
359:   if (stage < 0) PetscCall(PetscLogStateGetCurrentStage(handler->state, &stage));
360:   PetscCall(PetscLogHandlerDefaultGetStageInfo(handler, stage, &stage_perf_info));
361:   *stage_info_p = &stage_perf_info->perfInfo;
362:   PetscFunctionReturn(PETSC_SUCCESS);
363: }

365: static PetscErrorCode PetscLogHandlerDefaultGetClassPerf(PetscLogHandler handler, PetscLogStage stage, PetscLogClass clss, PetscClassPerf **class_info)
366: {
367:   PetscLogClassPerfArray class_log;
368:   PetscStagePerf        *stage_info;

370:   PetscFunctionBegin;
371:   PetscCall(PetscLogHandlerDefaultGetStageInfo(handler, stage, &stage_info));
372:   class_log = stage_info->classLog;
373:   PetscCall(PetscLogClassPerfArrayResize(class_log, clss + 1));
374:   PetscCall(PetscLogClassPerfArrayGetRef(class_log, clss, class_info));
375:   PetscFunctionReturn(PETSC_SUCCESS);
376: }

378: PETSC_PRAGMA_DIAGNOSTIC_IGNORED_BEGIN("-Wconversion")
379: static PetscErrorCode PetscLogHandlerObjectCreate_Default(PetscLogHandler h, PetscObject obj)
380: {
381:   PetscLogHandler_Default def = (PetscLogHandler_Default)h->data;
382:   PetscLogState           state;
383:   PetscLogStage           stage;
384:   PetscClassPerf         *classInfo;
385:   int                     oclass = 0;

387:   PetscFunctionBegin;
388:   PetscCall(PetscLogHandlerGetState(h, &state));
389:   PetscCall(PetscSpinlockLock(&def->lock));
390:   /* Record stage info */
391:   PetscCall(PetscLogStateGetCurrentStage(state, &stage));
392:   PetscCall(PetscLogStateGetClassFromClassId(state, obj->classid, &oclass));
393:   PetscCall(PetscLogHandlerDefaultGetClassPerf(h, stage, oclass, &classInfo));
394:   classInfo->creations++;
395:   /* Record the creation action */
396:   if (def->petsc_logActions) {
397:     Action new_action;

399:     PetscCall(PetscTime(&new_action.time));
400:     new_action.time -= petsc_BaseTime;
401:     new_action.action  = PETSC_LOG_ACTION_CREATE;
402:     new_action.event   = -1;
403:     new_action.classid = obj->classid;
404:     new_action.id1     = obj->id;
405:     new_action.id2     = -1;
406:     new_action.id3     = -1;
407:     new_action.flops   = petsc_TotalFlops;
408:     PetscCall(PetscMallocGetCurrentUsage(&new_action.mem));
409:     PetscCall(PetscMallocGetMaximumUsage(&new_action.maxmem));
410:     PetscCall(PetscLogActionArrayPush(def->petsc_actions, new_action));
411:   }
412:   /* We don't just use obj->id to count all objects that are created
413:      because PetscLogHandlers are objects and PetscLogObjectDestroy() will not
414:      be called for them: the number of objects created and destroyed as counted
415:      here and below would have an imbalance */
416:   def->petsc_numObjectsCreated++;
417:   /* Record the object */
418:   if (def->petsc_logObjects) {
419:     Object   new_object;
420:     PetscInt objid;

422:     new_object.parent = -1;
423:     new_object.obj    = obj;
424:     new_object.mem    = 0;

426:     PetscCall(PetscMemzero(new_object.name, sizeof(new_object.name)));
427:     PetscCall(PetscMemzero(new_object.info, sizeof(new_object.info)));
428:     PetscAssert(obj->id >= 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Object ids from PetscObjectNewId_Internal() start at 1");
429:     PetscCall(PetscIntCast(obj->id, &objid));
430:     PetscCall(PetscLogObjectArrayResize(def->petsc_objects, objid));
431:     PetscCall(PetscLogObjectArraySet(def->petsc_objects, objid - 1, new_object));
432:   }
433:   PetscCall(PetscSpinlockUnlock(&def->lock));
434:   PetscFunctionReturn(PETSC_SUCCESS);
435: }

437: static PetscErrorCode PetscLogHandlerObjectDestroy_Default(PetscLogHandler h, PetscObject obj)
438: {
439:   PetscLogHandler_Default def = (PetscLogHandler_Default)h->data;
440:   PetscLogState           state;
441:   PetscLogStage           stage;
442:   PetscClassPerf         *classInfo;
443:   int                     oclass = 0;

445:   PetscFunctionBegin;
446:   PetscCall(PetscLogHandlerGetState(h, &state));
447:   /* Record stage info */
448:   PetscCall(PetscSpinlockLock(&def->lock));
449:   PetscCall(PetscLogStateGetCurrentStage(state, &stage));
450:   if (stage >= 0) {
451:     /* stage < 0 can happen if the log summary is output before some things are destroyed */
452:     PetscCall(PetscLogStateGetClassFromClassId(state, obj->classid, &oclass));
453:     PetscCall(PetscLogHandlerDefaultGetClassPerf(h, stage, oclass, &classInfo));
454:     classInfo->destructions++;
455:   }
456:   /* Cannot Credit all ancestors with your memory because they may have already been destroyed*/
457:   def->petsc_numObjectsDestroyed++;
458:   /* Dynamically enlarge logging structures */
459:   /* Record the destruction action */
460:   if (def->petsc_logActions) {
461:     Action new_action;

463:     PetscCall(PetscTime(&new_action.time));
464:     new_action.time -= petsc_BaseTime;
465:     new_action.event   = -1;
466:     new_action.action  = PETSC_LOG_ACTION_DESTROY;
467:     new_action.classid = obj->classid;
468:     new_action.id1     = obj->id;
469:     new_action.id2     = -1;
470:     new_action.id3     = -1;
471:     new_action.flops   = petsc_TotalFlops;
472:     PetscCall(PetscMallocGetCurrentUsage(&new_action.mem));
473:     PetscCall(PetscMallocGetMaximumUsage(&new_action.maxmem));
474:     PetscCall(PetscLogActionArrayPush(def->petsc_actions, new_action));
475:   }
476:   if (def->petsc_logObjects) {
477:     Object  *obj_entry = NULL;
478:     PetscInt objid;

480:     PetscAssert(obj->id >= 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Object ids from PetscObjectNewId_Internal() start at 1");
481:     PetscCall(PetscIntCast(obj->id - 1, &objid));
482:     PetscCall(PetscLogObjectArrayGetRef(def->petsc_objects, objid, &obj_entry));
483:     if (obj->name) PetscCall(PetscStrncpy(obj_entry->name, obj->name, 64));
484:     obj_entry->obj = NULL;
485:   }
486:   PetscCall(PetscSpinlockUnlock(&def->lock));
487:   PetscFunctionReturn(PETSC_SUCCESS);
488: }

490: static PetscErrorCode PetscLogHandlerEventSync_Default(PetscLogHandler h, PetscLogEvent event, MPI_Comm comm)
491: {
492:   PetscLogState       state;
493:   PetscLogEventInfo   event_info;
494:   PetscEventPerfInfo *event_perf_info;
495:   int                 stage;
496:   PetscLogDouble      time = 0.0;

498:   PetscFunctionBegin;
499:   if (!PetscLogSyncOn || comm == MPI_COMM_NULL) PetscFunctionReturn(PETSC_SUCCESS);
500:   PetscCall(PetscLogHandlerGetState(h, &state));
501:   PetscCall(PetscLogStateEventGetInfo(state, event, &event_info));
502:   if (!event_info.collective) PetscFunctionReturn(PETSC_SUCCESS);
503:   PetscCall(PetscLogStateGetCurrentStage(state, &stage));
504:   PetscCall(PetscLogHandlerGetEventPerfInfo_Default(h, stage, event, &event_perf_info));
505:   if (event_perf_info->depth > 0) PetscFunctionReturn(PETSC_SUCCESS);

507:   PetscCall(PetscTimeSubtract(&time));
508:   PetscCallMPI(MPI_Barrier(comm));
509:   PetscCall(PetscTimeAdd(&time));
510:   event_perf_info->syncTime += time;
511:   PetscFunctionReturn(PETSC_SUCCESS);
512: }

514: static PetscErrorCode PetscLogGetStageEventPerfInfo_threaded(PetscLogHandler_Default def, PetscLogStage stage, PetscLogEvent event, PetscEventPerfInfo **eventInfo)
515: {
516:   PetscEventPerfInfo *leventInfo = NULL;
517:   PetscHashIJKKey     key;

519:   PetscFunctionBegin;
520: #if PetscDefined(HAVE_THREADSAFETY)
521:   key.i = PetscLogGetTid();
522: #else
523:   key.i = 0;
524: #endif
525:   key.j = stage;
526:   key.k = event;
527:   PetscCall(PetscSpinlockLock(&def->lock));
528:   PetscCall(PetscHMapEventGet(def->eventInfoMap_th, key, &leventInfo));
529:   if (!leventInfo) {
530:     PetscCall(PetscNew(&leventInfo));
531:     leventInfo->id = event;
532:     PetscCall(PetscHMapEventSet(def->eventInfoMap_th, key, leventInfo));
533:   }
534:   PetscCall(PetscSpinlockUnlock(&def->lock));
535:   *eventInfo = leventInfo;
536:   PetscFunctionReturn(PETSC_SUCCESS);
537: }

539: static PetscErrorCode PetscLogHandlerEventBegin_Default(PetscLogHandler h, PetscLogEvent event, PetscObject o1, PetscObject o2, PetscObject o3, PetscObject o4)
540: {
541:   PetscLogHandler_Default def             = (PetscLogHandler_Default)h->data;
542:   PetscEventPerfInfo     *event_perf_info = NULL;
543:   PetscLogEventInfo       event_info;
544:   PetscLogDouble          time;
545:   PetscLogState           state;
546:   PetscLogStage           stage;

548:   PetscFunctionBegin;
549:   PetscCall(PetscLogHandlerGetState(h, &state));
550:   if (PetscDefined(USE_DEBUG)) {
551:     PetscCall(PetscLogStateEventGetInfo(state, event, &event_info));
556:     if (event_info.collective && o1) {
557:       PetscInt64 b2[2];

559:       b2[0] = -o1->cidx;
560:       b2[1] = o1->cidx;
561:       PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, b2, 2, MPIU_INT64, MPI_MAX, PetscObjectComm(o1)));
562:       PetscCheck(-b2[0] == b2[1], PETSC_COMM_SELF, PETSC_ERR_PLIB, "Collective event %s not called collectively %" PetscInt64_FMT " != %" PetscInt64_FMT, event_info.name, -b2[0], b2[1]);
563:     }
564:   }
565:   /* Synchronization */
566:   PetscCall(PetscLogHandlerEventSync_Default(h, event, PetscObjectComm(o1)));
567:   PetscCall(PetscLogStateGetCurrentStage(state, &stage));
568:   if (def->pause_depth > 0) stage = 0; // in pause-mode, all events run on the main stage
569:   if (PetscDefined(HAVE_THREADSAFETY) || def->use_threadsafe) {
570:     PetscCall(PetscLogGetStageEventPerfInfo_threaded(def, stage, event, &event_perf_info));
571:     if (event_perf_info->depth == 0) PetscCall(PetscEventPerfInfoInit(event_perf_info));
572:   } else {
573:     PetscCall(PetscLogHandlerGetEventPerfInfo_Default(h, stage, event, &event_perf_info));
574:   }
575:   PetscCheck(event_perf_info->depth >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Trying to begin a paused event, this is not allowed");
576:   event_perf_info->depth++;
577:   /* Check for double counting */
578:   if (event_perf_info->depth > 1) PetscFunctionReturn(PETSC_SUCCESS);
579:   PetscCall(PetscLogStateEventGetInfo(state, event, &event_info));
580:   /* Log the performance info */
581:   event_perf_info->count++;
582:   PetscCall(PetscTime(&time));
583:   PetscCall(PetscEventPerfInfoTic(event_perf_info, time, PetscLogMemory, event));
584:   if (def->petsc_logActions) {
585:     PetscLogDouble curTime;
586:     Action         new_action;

588:     PetscCall(PetscTime(&curTime));
589:     new_action.time    = curTime - petsc_BaseTime;
590:     new_action.action  = PETSC_LOG_ACTION_BEGIN;
591:     new_action.event   = event;
592:     new_action.classid = event_info.classid;
593:     new_action.id1     = o1 ? o1->id : -1;
594:     new_action.id2     = o2 ? o2->id : -1;
595:     new_action.id3     = o3 ? o3->id : -1;
596:     new_action.flops   = petsc_TotalFlops;
597:     PetscCall(PetscMallocGetCurrentUsage(&new_action.mem));
598:     PetscCall(PetscMallocGetMaximumUsage(&new_action.maxmem));
599:     PetscCall(PetscLogActionArrayPush(def->petsc_actions, new_action));
600:   }
601:   PetscFunctionReturn(PETSC_SUCCESS);
602: }

604: static PetscErrorCode PetscLogHandlerEventEnd_Default(PetscLogHandler h, PetscLogEvent event, PetscObject o1, PetscObject o2, PetscObject o3, PetscObject o4)
605: {
606:   PetscLogHandler_Default def             = (PetscLogHandler_Default)h->data;
607:   PetscEventPerfInfo     *event_perf_info = NULL;
608:   PetscLogDouble          time;
609:   PetscLogState           state;
610:   int                     stage;
611:   PetscLogEventInfo       event_info;

613:   PetscFunctionBegin;
614:   PetscCall(PetscLogHandlerGetState(h, &state));
615:   if (PetscDefined(USE_DEBUG)) {
616:     PetscCall(PetscLogStateEventGetInfo(state, event, &event_info));
621:     if (event_info.collective && o1) {
622:       PetscInt64 b2[2];

624:       b2[0] = -o1->cidx;
625:       b2[1] = o1->cidx;
626:       PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, b2, 2, MPIU_INT64, MPI_MAX, PetscObjectComm(o1)));
627:       PetscCheck(-b2[0] == b2[1], PETSC_COMM_SELF, PETSC_ERR_PLIB, "Collective event %s not called collectively %" PetscInt64_FMT " != %" PetscInt64_FMT, event_info.name, -b2[0], b2[1]);
628:     }
629:   }
630:   if (def->petsc_logActions) {
631:     PetscLogDouble curTime;
632:     Action         new_action;

634:     PetscCall(PetscLogStateEventGetInfo(state, event, &event_info));
635:     PetscCall(PetscTime(&curTime));
636:     new_action.time    = curTime - petsc_BaseTime;
637:     new_action.action  = PETSC_LOG_ACTION_END;
638:     new_action.event   = event;
639:     new_action.classid = event_info.classid;
640:     new_action.id1     = o1 ? o1->id : -1;
641:     new_action.id2     = o2 ? o2->id : -2;
642:     new_action.id3     = o3 ? o3->id : -3;
643:     new_action.flops   = petsc_TotalFlops;
644:     PetscCall(PetscMallocGetCurrentUsage(&new_action.mem));
645:     PetscCall(PetscMallocGetMaximumUsage(&new_action.maxmem));
646:     PetscCall(PetscLogActionArrayPush(def->petsc_actions, new_action));
647:   }
648:   PetscCall(PetscLogStateGetCurrentStage(state, &stage));
649:   if (def->pause_depth > 0) stage = 0; // all events run on the main stage in pause-mode
650:   if (PetscDefined(HAVE_THREADSAFETY) || def->use_threadsafe) {
651:     PetscCall(PetscLogGetStageEventPerfInfo_threaded(def, stage, event, &event_perf_info));
652:   } else {
653:     PetscCall(PetscLogHandlerGetEventPerfInfo_Default(h, stage, event, &event_perf_info));
654:   }
655:   PetscCheck(event_perf_info->depth > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Trying to end paused event, not allowed");
656:   event_perf_info->depth--;
657:   /* Check for double counting */
658:   if (event_perf_info->depth > 0) PetscFunctionReturn(PETSC_SUCCESS);
659:   else PetscCheck(event_perf_info->depth == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Logging event had unbalanced begin/end pairs");

661:   /* Log performance info */
662:   PetscCall(PetscTime(&time));
663:   PetscCall(PetscEventPerfInfoToc(event_perf_info, time, PetscLogMemory, event));
664:   if (PetscDefined(HAVE_THREADSAFETY) || def->use_threadsafe) {
665:     PetscEventPerfInfo *event_perf_info_global;
666:     PetscCall(PetscSpinlockLock(&def->lock));
667:     PetscCall(PetscLogHandlerGetEventPerfInfo_Default(h, stage, event, &event_perf_info_global));
668:     PetscCall(PetscEventPerfInfoAdd_Internal(event_perf_info, event_perf_info_global));
669:     PetscCall(PetscSpinlockUnlock(&def->lock));
670:   }
671:   PetscFunctionReturn(PETSC_SUCCESS);
672: }

674: static PetscErrorCode PetscLogHandlerEventDeactivatePush_Default(PetscLogHandler h, PetscLogStage stage, PetscLogEvent event)
675: {
676:   PetscEventPerfInfo *event_perf_info;

678:   PetscFunctionBegin;
679:   if (stage < 0) PetscCall(PetscLogStateGetCurrentStage(h->state, &stage));
680:   PetscCall(PetscLogHandlerGetEventPerfInfo_Default(h, stage, event, &event_perf_info));
681:   event_perf_info->depth++;
682:   PetscFunctionReturn(PETSC_SUCCESS);
683: }

685: static PetscErrorCode PetscLogHandlerEventDeactivatePop_Default(PetscLogHandler h, PetscLogStage stage, PetscLogEvent event)
686: {
687:   PetscEventPerfInfo *event_perf_info;

689:   PetscFunctionBegin;
690:   if (stage < 0) PetscCall(PetscLogStateGetCurrentStage(h->state, &stage));
691:   PetscCall(PetscLogHandlerGetEventPerfInfo_Default(h, stage, event, &event_perf_info));
692:   event_perf_info->depth--;
693:   PetscFunctionReturn(PETSC_SUCCESS);
694: }

696: static PetscErrorCode PetscLogHandlerEventsPause_Default(PetscLogHandler h)
697: {
698:   PetscLogHandler_Default def = (PetscLogHandler_Default)h->data;
699:   PetscLogDouble          time;
700:   PetscInt                num_stages;

702:   PetscFunctionBegin;
703:   if (def->pause_depth++ > 0) PetscFunctionReturn(PETSC_SUCCESS);
704:   PetscCall(PetscLogStageInfoArrayGetSize(def->stages, &num_stages, NULL));
705:   PetscCall(PetscTime(&time));
706:   /* Pause stages in reverse of the order they were pushed */
707:   for (PetscInt stage = num_stages - 1; stage >= 0; stage--) {
708:     PetscStagePerf *stage_info = NULL;
709:     PetscInt        num_events;

711:     PetscCall(PetscLogStageInfoArrayGetRef(def->stages, stage, &stage_info));
712:     PetscCall(PetscLogEventPerfArrayGetSize(stage_info->eventLog, &num_events, NULL));
713:     /* Pause events in reverse of the order they were pushed */
714:     for (PetscInt event = num_events - 1; event >= 0; event--) {
715:       PetscEventPerfInfo *event_info = NULL;
716:       PetscCall(PetscLogEventPerfArrayGetRef(stage_info->eventLog, event, &event_info));
717:       if (event_info->depth > 0) {
718:         event_info->depth *= -1;
719:         PetscCall(PetscEventPerfInfoPause(event_info, time, PetscLogMemory, event));
720:       }
721:     }
722:     if (stage > 0 && stage_info->perfInfo.depth > 0) {
723:       stage_info->perfInfo.depth *= -1;
724:       PetscCall(PetscEventPerfInfoPause(&stage_info->perfInfo, time, PetscLogMemory, -(stage + 2)));
725:     }
726:   }
727:   PetscFunctionReturn(PETSC_SUCCESS);
728: }

730: static PetscErrorCode PetscLogHandlerEventsResume_Default(PetscLogHandler h)
731: {
732:   PetscLogHandler_Default def = (PetscLogHandler_Default)h->data;
733:   PetscLogDouble          time;
734:   PetscInt                num_stages;

736:   PetscFunctionBegin;
737:   if (--def->pause_depth > 0) PetscFunctionReturn(PETSC_SUCCESS);
738:   PetscCall(PetscLogStageInfoArrayGetSize(def->stages, &num_stages, NULL));
739:   PetscCall(PetscTime(&time));
740:   /* Unpause stages in the same order they were pushed */
741:   for (PetscInt stage = 0; stage < num_stages; stage++) {
742:     PetscStagePerf *stage_info = NULL;
743:     PetscInt        num_events;

745:     PetscCall(PetscLogStageInfoArrayGetRef(def->stages, stage, &stage_info));
746:     PetscCall(PetscLogEventPerfArrayGetSize(stage_info->eventLog, &num_events, NULL));
747:     /* Unpause events in the same order they were pushed */
748:     for (PetscInt event = 0; event < num_events; event++) {
749:       PetscEventPerfInfo *event_info = NULL;
750:       PetscCall(PetscLogEventPerfArrayGetRef(stage_info->eventLog, event, &event_info));
751:       if (event_info->depth < 0) {
752:         event_info->depth *= -1;
753:         PetscCall(PetscEventPerfInfoResume(event_info, time, PetscLogMemory, event));
754:       }
755:     }
756:     if (stage > 0 && stage_info->perfInfo.depth < 0) {
757:       stage_info->perfInfo.depth *= -1;
758:       PetscCall(PetscEventPerfInfoResume(&stage_info->perfInfo, time, PetscLogMemory, -(stage + 2)));
759:     }
760:   }
761:   PetscFunctionReturn(PETSC_SUCCESS);
762: }

764: static PetscErrorCode PetscLogHandlerStagePush_Default(PetscLogHandler h, PetscLogStage new_stage)
765: {
766:   PetscLogHandler_Default def = (PetscLogHandler_Default)h->data;
767:   PetscLogDouble          time;
768:   PetscLogState           state;
769:   PetscLogStage           current_stage;
770:   PetscStagePerf         *new_stage_info;

772:   PetscFunctionBegin;
773:   if (def->pause_depth > 0) PetscFunctionReturn(PETSC_SUCCESS);
774:   PetscCall(PetscLogHandlerGetState(h, &state));
775:   current_stage = state->current_stage;
776:   PetscCall(PetscLogHandlerDefaultGetStageInfo(h, new_stage, &new_stage_info));
777:   PetscCall(PetscTime(&time));

779:   /* Record flops/time of previous stage */
780:   if (current_stage >= 0) {
781:     if (PetscBTLookup(state->active, current_stage)) {
782:       PetscStagePerf *current_stage_info;
783:       PetscCall(PetscLogHandlerDefaultGetStageInfo(h, current_stage, &current_stage_info));
784:       PetscCall(PetscEventPerfInfoToc(&current_stage_info->perfInfo, time, PetscLogMemory, -(current_stage + 2)));
785:     }
786:   }
787:   new_stage_info->used = PETSC_TRUE;
788:   new_stage_info->perfInfo.count++;
789:   new_stage_info->perfInfo.depth++;
790:   /* Subtract current quantities so that we obtain the difference when we pop */
791:   if (PetscBTLookup(state->active, new_stage)) PetscCall(PetscEventPerfInfoTic(&new_stage_info->perfInfo, time, PetscLogMemory, -(new_stage + 2)));
792:   PetscFunctionReturn(PETSC_SUCCESS);
793: }

795: static PetscErrorCode PetscLogHandlerStagePop_Default(PetscLogHandler h, PetscLogStage old_stage)
796: {
797:   PetscLogHandler_Default def = (PetscLogHandler_Default)h->data;
798:   PetscLogStage           current_stage;
799:   PetscStagePerf         *old_stage_info;
800:   PetscLogState           state;
801:   PetscLogDouble          time;

803:   PetscFunctionBegin;
804:   if (def->pause_depth > 0) PetscFunctionReturn(PETSC_SUCCESS);
805:   PetscCall(PetscLogHandlerGetState(h, &state));
806:   current_stage = state->current_stage;
807:   PetscCall(PetscLogHandlerDefaultGetStageInfo(h, old_stage, &old_stage_info));
808:   PetscCall(PetscTime(&time));
809:   old_stage_info->perfInfo.depth--;
810:   if (PetscBTLookup(state->active, old_stage)) PetscCall(PetscEventPerfInfoToc(&old_stage_info->perfInfo, time, PetscLogMemory, -(old_stage + 2)));
811:   if (current_stage >= 0) {
812:     if (PetscBTLookup(state->active, current_stage)) {
813:       PetscStagePerf *current_stage_info;
814:       PetscCall(PetscLogHandlerDefaultGetStageInfo(h, current_stage, &current_stage_info));
815:       PetscCall(PetscEventPerfInfoTic(&current_stage_info->perfInfo, time, PetscLogMemory, -(current_stage + 2)));
816:     }
817:   }
818:   PetscFunctionReturn(PETSC_SUCCESS);
819: }

821: static PetscErrorCode PetscLogHandlerStageSetVisible_Default(PetscLogHandler h, PetscLogStage stage, PetscBool is_visible)
822: {
823:   PetscStagePerf *stage_info;

825:   PetscFunctionBegin;
826:   if (stage < 0) PetscCall(PetscLogStateGetCurrentStage(h->state, &stage));
827:   PetscCall(PetscLogHandlerDefaultGetStageInfo(h, stage, &stage_info));
828:   stage_info->perfInfo.visible = is_visible;
829:   PetscFunctionReturn(PETSC_SUCCESS);
830: }

832: static PetscErrorCode PetscLogHandlerStageGetVisible_Default(PetscLogHandler h, PetscLogStage stage, PetscBool *is_visible)
833: {
834:   PetscStagePerf *stage_info;

836:   PetscFunctionBegin;
837:   if (stage < 0) PetscCall(PetscLogStateGetCurrentStage(h->state, &stage));
838:   PetscCall(PetscLogHandlerDefaultGetStageInfo(h, stage, &stage_info));
839:   *is_visible = stage_info->perfInfo.visible;
840:   PetscFunctionReturn(PETSC_SUCCESS);
841: }

843: static PetscErrorCode PetscLogHandlerSetLogActions_Default(PetscLogHandler handler, PetscBool flag)
844: {
845:   PetscLogHandler_Default def = (PetscLogHandler_Default)handler->data;

847:   PetscFunctionBegin;
848:   def->petsc_logActions = flag;
849:   PetscFunctionReturn(PETSC_SUCCESS);
850: }

852: static PetscErrorCode PetscLogHandlerSetLogObjects_Default(PetscLogHandler handler, PetscBool flag)
853: {
854:   PetscLogHandler_Default def = (PetscLogHandler_Default)handler->data;

856:   PetscFunctionBegin;
857:   def->petsc_logObjects = flag;
858:   PetscFunctionReturn(PETSC_SUCCESS);
859: }

861: static PetscErrorCode PetscLogHandlerLogObjectState_Default(PetscLogHandler handler, PetscObject obj, const char format[], va_list Argp)
862: {
863:   PetscLogHandler_Default def = (PetscLogHandler_Default)handler->data;
864:   size_t                  fullLength;

866:   PetscFunctionBegin;
867:   if (def->petsc_logObjects) {
868:     Object  *obj_entry = NULL;
869:     PetscInt objid;

871:     PetscCall(PetscIntCast(obj->id - 1, &objid));
872:     PetscCall(PetscLogObjectArrayGetRef(def->petsc_objects, objid, &obj_entry));
873:     PetscCall(PetscVSNPrintf(obj_entry->info, 64, format, &fullLength, Argp));
874:   }
875:   PetscFunctionReturn(PETSC_SUCCESS);
876: }

878: static PetscErrorCode PetscLogHandlerGetNumObjects_Default(PetscLogHandler handler, PetscInt *num_objects)
879: {
880:   PetscLogHandler_Default def = (PetscLogHandler_Default)handler->data;

882:   PetscFunctionBegin;
883:   PetscCall(PetscLogObjectArrayGetSize(def->petsc_objects, num_objects, NULL));
884:   PetscFunctionReturn(PETSC_SUCCESS);
885: }

887: static PetscErrorCode PetscLogHandlerDump_Default(PetscLogHandler handler, const char sname[])
888: {
889:   PetscLogHandler_Default def = (PetscLogHandler_Default)handler->data;
890:   FILE                   *fd;
891:   char                    file[PETSC_MAX_PATH_LEN], fname[PETSC_MAX_PATH_LEN];
892:   PetscLogDouble          flops, _TotalTime;
893:   PetscMPIInt             rank;
894:   int                     curStage;
895:   PetscLogState           state;
896:   PetscInt                num_events;
897:   PetscLogEvent           event;

899:   PetscFunctionBegin;
900:   /* Calculate the total elapsed time */
901:   PetscCall(PetscTime(&_TotalTime));
902:   _TotalTime -= petsc_BaseTime;
903:   /* Open log file */
904:   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)handler), &rank));
905:   PetscCall(PetscSNPrintf(file, PETSC_STATIC_ARRAY_LENGTH(file), "%s.%d", sname && sname[0] ? sname : "Log", rank));
906:   PetscCall(PetscFixFilename(file, fname));
907:   PetscCall(PetscFOpen(PETSC_COMM_SELF, fname, "w", &fd));
908:   PetscCheck(!(rank == 0) || !(!fd), PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Cannot open file: %s", fname);
909:   /* Output totals */
910:   PetscCall(PetscFPrintf(PETSC_COMM_SELF, fd, "Total Flop %14e %16.8e\n", petsc_TotalFlops, _TotalTime));
911:   PetscCall(PetscFPrintf(PETSC_COMM_SELF, fd, "Clock Resolution %g\n", 0.0));
912:   /* Output actions */
913:   if (def->petsc_logActions) {
914:     PetscInt num_actions;
915:     PetscCall(PetscLogActionArrayGetSize(def->petsc_actions, &num_actions, NULL));
916:     PetscCall(PetscFPrintf(PETSC_COMM_SELF, fd, "Actions accomplished %" PetscInt_FMT "\n", num_actions));
917:     for (int a = 0; a < num_actions; a++) {
918:       Action *action;

920:       PetscCall(PetscLogActionArrayGetRef(def->petsc_actions, a, &action));
921:       PetscCall(PetscFPrintf(PETSC_COMM_SELF, fd, "%g %d %d %d  %" PetscInt64_FMT " %" PetscInt64_FMT " %" PetscInt64_FMT " %g %g %g\n", action->time, action->action, action->event, action->classid, action->id1, action->id2, action->id3, action->flops,
922:                              action->mem, action->maxmem));
923:     }
924:   }
925:   /* Output objects */
926:   if (def->petsc_logObjects) {
927:     PetscInt num_objects;

929:     PetscCall(PetscLogObjectArrayGetSize(def->petsc_objects, &num_objects, NULL));
930:     PetscCall(PetscFPrintf(PETSC_COMM_SELF, fd, "Objects created %d destroyed %d\n", def->petsc_numObjectsCreated, def->petsc_numObjectsDestroyed));
931:     for (int o = 0; o < num_objects; o++) {
932:       Object *object = NULL;

934:       PetscCall(PetscLogObjectArrayGetRef(def->petsc_objects, o, &object));
935:       if (object->parent != -1) continue; // object with this id wasn't logged, probably a PetscLogHandler
936:       PetscCall(PetscFPrintf(PETSC_COMM_SELF, fd, "Parent ID: %d Memory: %d\n", object->parent, (int)object->mem));
937:       if (!object->name[0]) {
938:         PetscCall(PetscFPrintf(PETSC_COMM_SELF, fd, "No Name\n"));
939:       } else {
940:         PetscCall(PetscFPrintf(PETSC_COMM_SELF, fd, "Name: %s\n", object->name));
941:       }
942:       if (!object->info[0]) {
943:         PetscCall(PetscFPrintf(PETSC_COMM_SELF, fd, "No Info\n"));
944:       } else {
945:         PetscCall(PetscFPrintf(PETSC_COMM_SELF, fd, "Info: %s\n", object->info));
946:       }
947:     }
948:   }
949:   /* Output events */
950:   PetscCall(PetscFPrintf(PETSC_COMM_SELF, fd, "Event log:\n"));
951:   PetscCall(PetscLogHandlerGetState(handler, &state));
952:   PetscCall(PetscLogStateGetNumEvents(state, &num_events));
953:   PetscCall(PetscLogStateGetCurrentStage(state, &curStage));
954:   for (event = 0; event < num_events; event++) {
955:     PetscEventPerfInfo *event_info;

957:     PetscCall(PetscLogHandlerGetEventPerfInfo_Default(handler, curStage, event, &event_info));
958:     if (event_info->time != 0.0) flops = event_info->flops / event_info->time;
959:     else flops = 0.0;
960:     PetscCall(PetscFPrintf(PETSC_COMM_SELF, fd, "%d %16d %16g %16g %16g\n", event, event_info->count, event_info->flops, event_info->time, flops));
961:   }
962:   PetscCall(PetscFClose(PETSC_COMM_SELF, fd));
963:   PetscFunctionReturn(PETSC_SUCCESS);
964: }

966: /*
967:   PetscLogView_Detailed - Each process prints the times for its own events

969: */
970: static PetscErrorCode PetscLogHandlerView_Default_Detailed(PetscLogHandler handler, PetscViewer viewer)
971: {
972:   PetscLogHandler_Default def = (PetscLogHandler_Default)handler->data;
973:   PetscLogDouble          locTotalTime, numRed, maxMem;
974:   PetscInt                numStages, numEvents;
975:   MPI_Comm                comm = PetscObjectComm((PetscObject)viewer);
976:   PetscMPIInt             rank, size;
977:   PetscLogGlobalNames     global_stages, global_events;
978:   PetscLogState           state;
979:   PetscEventPerfInfo      zero_info;

981:   PetscFunctionBegin;
982:   PetscCall(PetscLogHandlerGetState(handler, &state));
983:   PetscCallMPI(MPI_Comm_size(comm, &size));
984:   PetscCallMPI(MPI_Comm_rank(comm, &rank));
985:   /* Must preserve reduction count before we go on */
986:   numRed = petsc_allreduce_ct + petsc_gather_ct + petsc_scatter_ct;
987:   /* Get the total elapsed time */
988:   PetscCall(PetscTime(&locTotalTime));
989:   locTotalTime -= petsc_BaseTime;
990:   PetscCall(PetscViewerASCIIPrintf(viewer, "size = %d\n", size));
991:   PetscCall(PetscViewerASCIIPrintf(viewer, "LocalTimes = {}\n"));
992:   PetscCall(PetscViewerASCIIPrintf(viewer, "LocalMessages = {}\n"));
993:   PetscCall(PetscViewerASCIIPrintf(viewer, "LocalMessageLens = {}\n"));
994:   PetscCall(PetscViewerASCIIPrintf(viewer, "LocalReductions = {}\n"));
995:   PetscCall(PetscViewerASCIIPrintf(viewer, "LocalFlop = {}\n"));
996:   PetscCall(PetscViewerASCIIPrintf(viewer, "LocalObjects = {}\n"));
997:   PetscCall(PetscViewerASCIIPrintf(viewer, "LocalMemory = {}\n"));
998:   PetscCall(PetscLogRegistryCreateGlobalStageNames(comm, state->registry, &global_stages));
999:   PetscCall(PetscLogRegistryCreateGlobalEventNames(comm, state->registry, &global_events));
1000:   PetscCall(PetscLogGlobalNamesGetSize(global_stages, NULL, &numStages));
1001:   PetscCall(PetscLogGlobalNamesGetSize(global_events, NULL, &numEvents));
1002:   PetscCall(PetscMemzero(&zero_info, sizeof(zero_info)));
1003:   PetscCall(PetscViewerASCIIPrintf(viewer, "Stages = {}\n"));
1004:   for (PetscInt stage = 0; stage < numStages; stage++) {
1005:     PetscInt    stage_id;
1006:     const char *stage_name;

1008:     PetscCall(PetscLogGlobalNamesGlobalGetLocal(global_stages, stage, &stage_id));
1009:     PetscCall(PetscLogGlobalNamesGlobalGetName(global_stages, stage, &stage_name));
1010:     PetscCall(PetscViewerASCIIPrintf(viewer, "Stages[\"%s\"] = {}\n", stage_name));
1011:     PetscCall(PetscViewerASCIIPrintf(viewer, "Stages[\"%s\"][\"summary\"] = {}\n", stage_name));
1012:     for (PetscInt event = 0; event < numEvents; event++) {
1013:       PetscEventPerfInfo *eventInfo = &zero_info;
1014:       PetscBool           is_zero   = PETSC_FALSE;
1015:       PetscInt            event_id;
1016:       const char         *event_name;

1018:       PetscCall(PetscLogGlobalNamesGlobalGetLocal(global_events, event, &event_id));
1019:       PetscCall(PetscLogGlobalNamesGlobalGetName(global_events, event, &event_name));
1020:       if (event_id >= 0 && stage_id >= 0) PetscCall(PetscLogHandlerGetEventPerfInfo_Default(handler, stage_id, event_id, &eventInfo));
1021:       is_zero = eventInfo->count == 0 ? PETSC_TRUE : PETSC_FALSE;
1022:       PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &is_zero, 1, MPI_C_BOOL, MPI_LAND, comm));
1023:       if (!is_zero) PetscCall(PetscViewerASCIIPrintf(viewer, "Stages[\"%s\"][\"%s\"] = {}\n", stage_name, event_name));
1024:     }
1025:   }
1026:   PetscCall(PetscMallocGetMaximumUsage(&maxMem));
1027:   PetscCall(PetscViewerASCIIPushSynchronized(viewer));
1028:   PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "LocalTimes[%d] = %g\n", rank, locTotalTime));
1029:   PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "LocalMessages[%d] = %g\n", rank, petsc_irecv_ct + petsc_isend_ct + petsc_recv_ct + petsc_send_ct));
1030:   PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "LocalMessageLens[%d] = %g\n", rank, petsc_irecv_len + petsc_isend_len + petsc_recv_len + petsc_send_len));
1031:   PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "LocalReductions[%d] = %g\n", rank, numRed));
1032:   PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "LocalFlop[%d] = %g\n", rank, petsc_TotalFlops));
1033:   {
1034:     PetscInt num_objects;

1036:     PetscCall(PetscLogObjectArrayGetSize(def->petsc_objects, &num_objects, NULL));
1037:     PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "LocalObjects[%d] = %" PetscInt_FMT "\n", rank, num_objects));
1038:   }
1039:   PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "LocalMemory[%d] = %g\n", rank, maxMem));
1040:   PetscCall(PetscViewerFlush(viewer));
1041:   for (PetscInt stage = 0; stage < numStages; stage++) {
1042:     PetscEventPerfInfo *stage_perf_info = &zero_info;
1043:     PetscInt            stage_id;
1044:     const char         *stage_name;

1046:     PetscCall(PetscLogGlobalNamesGlobalGetLocal(global_stages, stage, &stage_id));
1047:     PetscCall(PetscLogGlobalNamesGlobalGetName(global_stages, stage, &stage_name));
1048:     if (stage_id >= 0) {
1049:       PetscStagePerf *stage_info;
1050:       PetscCall(PetscLogHandlerDefaultGetStageInfo(handler, stage_id, &stage_info));
1051:       stage_perf_info = &stage_info->perfInfo;
1052:     }
1053:     PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "Stages[\"%s\"][\"summary\"][%d] = {\"time\" : %g, \"numMessages\" : %g, \"messageLength\" : %g, \"numReductions\" : %g, \"flop\" : %g}\n", stage_name, rank, stage_perf_info->time,
1054:                                                  stage_perf_info->numMessages, stage_perf_info->messageLength, stage_perf_info->numReductions, stage_perf_info->flops));
1055:     for (PetscInt event = 0; event < numEvents; event++) {
1056:       PetscEventPerfInfo *eventInfo = &zero_info;
1057:       PetscBool           is_zero   = PETSC_FALSE;
1058:       PetscInt            event_id;
1059:       const char         *event_name;

1061:       PetscCall(PetscLogGlobalNamesGlobalGetLocal(global_events, event, &event_id));
1062:       PetscCall(PetscLogGlobalNamesGlobalGetName(global_events, event, &event_name));
1063:       if (event_id >= 0 && stage_id >= 0) PetscCall(PetscLogHandlerGetEventPerfInfo_Default(handler, stage_id, event_id, &eventInfo));
1064:       is_zero = eventInfo->count == 0 ? PETSC_TRUE : PETSC_FALSE;
1065:       PetscCall(PetscArraycmp(eventInfo, &zero_info, 1, &is_zero));
1066:       PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &is_zero, 1, MPI_C_BOOL, MPI_LAND, comm));
1067:       if (!is_zero) {
1068:         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "Stages[\"%s\"][\"%s\"][%d] = {\"count\" : %d, \"time\" : %g, \"syncTime\" : %g, \"numMessages\" : %g, \"messageLength\" : %g, \"numReductions\" : %g, \"flop\" : %g", stage_name, event_name, rank,
1069:                                                      eventInfo->count, eventInfo->time, eventInfo->syncTime, eventInfo->numMessages, eventInfo->messageLength, eventInfo->numReductions, eventInfo->flops));
1070:         if (eventInfo->dof[0] >= 0.) {
1071:           PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ", \"dof\" : ["));
1072:           for (PetscInt d = 0; d < 8; ++d) {
1073:             if (d > 0) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ", "));
1074:             PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%g", eventInfo->dof[d]));
1075:           }
1076:           PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "]"));
1077:           PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ", \"error\" : ["));
1078:           for (PetscInt e = 0; e < 8; ++e) {
1079:             if (e > 0) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ", "));
1080:             PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%g", eventInfo->errors[e]));
1081:           }
1082:           PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "]"));
1083:         }
1084:       }
1085:       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "}\n"));
1086:     }
1087:   }
1088:   PetscCall(PetscViewerFlush(viewer));
1089:   PetscCall(PetscViewerASCIIPopSynchronized(viewer));
1090:   PetscCall(PetscLogGlobalNamesDestroy(&global_events));
1091:   PetscCall(PetscLogGlobalNamesDestroy(&global_stages));
1092:   PetscFunctionReturn(PETSC_SUCCESS);
1093: }

1095: /*
1096:   PetscLogView_CSV - Each process prints the times for its own events in Comma-Separated Value Format
1097: */
1098: static PetscErrorCode PetscLogHandlerView_Default_CSV(PetscLogHandler handler, PetscViewer viewer)
1099: {
1100:   PetscLogDouble      locTotalTime, maxMem;
1101:   PetscInt            numStages, numEvents, stage, event;
1102:   MPI_Comm            comm = PetscObjectComm((PetscObject)viewer);
1103:   PetscMPIInt         rank, size;
1104:   PetscLogGlobalNames global_stages, global_events;
1105:   PetscLogState       state;
1106:   PetscEventPerfInfo  zero_info;

1108:   PetscFunctionBegin;
1109:   PetscCall(PetscLogHandlerGetState(handler, &state));
1110:   PetscCallMPI(MPI_Comm_size(comm, &size));
1111:   PetscCallMPI(MPI_Comm_rank(comm, &rank));
1112:   /* Must preserve reduction count before we go on */
1113:   /* Get the total elapsed time */
1114:   PetscCall(PetscTime(&locTotalTime));
1115:   locTotalTime -= petsc_BaseTime;
1116:   PetscCall(PetscMallocGetMaximumUsage(&maxMem));
1117:   PetscCall(PetscLogRegistryCreateGlobalStageNames(comm, state->registry, &global_stages));
1118:   PetscCall(PetscLogRegistryCreateGlobalEventNames(comm, state->registry, &global_events));
1119:   PetscCall(PetscLogGlobalNamesGetSize(global_stages, NULL, &numStages));
1120:   PetscCall(PetscLogGlobalNamesGetSize(global_events, NULL, &numEvents));
1121:   PetscCall(PetscMemzero(&zero_info, sizeof(zero_info)));
1122:   PetscCall(PetscViewerASCIIPushSynchronized(viewer));
1123:   PetscCall(PetscViewerASCIIPrintf(viewer, "Stage Name,Event Name,Rank,Count,Time,Num Messages,Message Length,Num Reductions,FLOP,dof0,dof1,dof2,dof3,dof4,dof5,dof6,dof7,e0,e1,e2,e3,e4,e5,e6,e7,%d\n", size));
1124:   PetscCall(PetscViewerFlush(viewer));
1125:   for (stage = 0; stage < numStages; stage++) {
1126:     PetscEventPerfInfo *stage_perf_info;
1127:     PetscInt            stage_id;
1128:     const char         *stage_name;

1130:     PetscCall(PetscLogGlobalNamesGlobalGetLocal(global_stages, stage, &stage_id));
1131:     PetscCall(PetscLogGlobalNamesGlobalGetName(global_stages, stage, &stage_name));
1132:     stage_perf_info = &zero_info;
1133:     if (stage_id >= 0) {
1134:       PetscStagePerf *stage_info;
1135:       PetscCall(PetscLogHandlerDefaultGetStageInfo(handler, stage_id, &stage_info));
1136:       stage_perf_info = &stage_info->perfInfo;
1137:     }
1138:     PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%s,summary,%d,1,%g,%g,%g,%g,%g\n", stage_name, rank, stage_perf_info->time, stage_perf_info->numMessages, stage_perf_info->messageLength, stage_perf_info->numReductions, stage_perf_info->flops));
1139:     for (event = 0; event < numEvents; event++) {
1140:       PetscEventPerfInfo *eventInfo = &zero_info;
1141:       PetscBool           is_zero   = PETSC_FALSE;
1142:       PetscInt            event_id;
1143:       const char         *event_name;

1145:       PetscCall(PetscLogGlobalNamesGlobalGetLocal(global_events, event, &event_id));
1146:       PetscCall(PetscLogGlobalNamesGlobalGetName(global_events, event, &event_name));
1147:       if (event_id >= 0 && stage_id >= 0) PetscCall(PetscLogHandlerGetEventPerfInfo_Default(handler, stage_id, event_id, &eventInfo));
1148:       PetscCall(PetscArraycmp(eventInfo, &zero_info, 1, &is_zero));
1149:       PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &is_zero, 1, MPI_C_BOOL, MPI_LAND, comm));
1150:       if (!is_zero) {
1151:         PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "%s,%s,%d,%d,%g,%g,%g,%g,%g", stage_name, event_name, rank, eventInfo->count, eventInfo->time, eventInfo->numMessages, eventInfo->messageLength, eventInfo->numReductions, eventInfo->flops));
1152:         if (eventInfo->dof[0] >= 0.) {
1153:           for (PetscInt d = 0; d < 8; ++d) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ",%g", eventInfo->dof[d]));
1154:           for (PetscInt e = 0; e < 8; ++e) PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, ",%g", eventInfo->errors[e]));
1155:         }
1156:       }
1157:       PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "\n"));
1158:     }
1159:   }
1160:   PetscCall(PetscViewerFlush(viewer));
1161:   PetscCall(PetscViewerASCIIPopSynchronized(viewer));
1162:   PetscCall(PetscLogGlobalNamesDestroy(&global_stages));
1163:   PetscCall(PetscLogGlobalNamesDestroy(&global_events));
1164:   PetscFunctionReturn(PETSC_SUCCESS);
1165: }

1167: static PetscErrorCode PetscLogViewWarnSync(PetscViewer viewer)
1168: {
1169:   PetscFunctionBegin;
1170:   if (!PetscLogSyncOn) PetscFunctionReturn(PETSC_SUCCESS);
1171:   PetscCall(PetscViewerASCIIPrintf(viewer, "\n\n"));
1172:   PetscCall(PetscViewerASCIIPrintf(viewer, "      ##########################################################\n"));
1173:   PetscCall(PetscViewerASCIIPrintf(viewer, "      #                                                        #\n"));
1174:   PetscCall(PetscViewerASCIIPrintf(viewer, "      #                       WARNING!!!                       #\n"));
1175:   PetscCall(PetscViewerASCIIPrintf(viewer, "      #                                                        #\n"));
1176:   PetscCall(PetscViewerASCIIPrintf(viewer, "      #   This program was run with logging synchronization.   #\n"));
1177:   PetscCall(PetscViewerASCIIPrintf(viewer, "      #   This option provides more meaningful imbalance       #\n"));
1178:   PetscCall(PetscViewerASCIIPrintf(viewer, "      #   figures at the expense of slowing things down and    #\n"));
1179:   PetscCall(PetscViewerASCIIPrintf(viewer, "      #   providing a distorted view of the overall runtime.   #\n"));
1180:   PetscCall(PetscViewerASCIIPrintf(viewer, "      #                                                        #\n"));
1181:   PetscCall(PetscViewerASCIIPrintf(viewer, "      ##########################################################\n\n\n"));
1182:   PetscFunctionReturn(PETSC_SUCCESS);
1183: }

1185: static PetscErrorCode PetscLogViewWarnDebugging(PetscViewer viewer)
1186: {
1187:   PetscFunctionBegin;
1188:   if (PetscDefined(USE_DEBUG)) {
1189:     PetscCall(PetscViewerASCIIPrintf(viewer, "\n\n"));
1190:     PetscCall(PetscViewerASCIIPrintf(viewer, "      ##########################################################\n"));
1191:     PetscCall(PetscViewerASCIIPrintf(viewer, "      #                                                        #\n"));
1192:     PetscCall(PetscViewerASCIIPrintf(viewer, "      #                       WARNING!!!                       #\n"));
1193:     PetscCall(PetscViewerASCIIPrintf(viewer, "      #                                                        #\n"));
1194:     PetscCall(PetscViewerASCIIPrintf(viewer, "      #   This code was compiled with a debugging option.      #\n"));
1195:     PetscCall(PetscViewerASCIIPrintf(viewer, "      #   To get timing results run ./configure                #\n"));
1196:     PetscCall(PetscViewerASCIIPrintf(viewer, "      #   using --with-debugging=no, the performance will      #\n"));
1197:     PetscCall(PetscViewerASCIIPrintf(viewer, "      #   be generally two or three times faster.              #\n"));
1198:     PetscCall(PetscViewerASCIIPrintf(viewer, "      #                                                        #\n"));
1199:     PetscCall(PetscViewerASCIIPrintf(viewer, "      ##########################################################\n\n\n"));
1200:   }
1201:   PetscFunctionReturn(PETSC_SUCCESS);
1202: }

1204: static PetscErrorCode PetscLogViewWarnNoGpuAwareMpi(PetscViewer viewer)
1205: {
1206: #if PetscDefined(HAVE_DEVICE)
1207:   PetscMPIInt size;
1208:   PetscBool   deviceInitialized = PETSC_FALSE;

1210:   PetscFunctionBegin;
1211:   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)viewer), &size));
1212:   for (int i = PETSC_DEVICE_HOST + 1; i < PETSC_DEVICE_MAX; ++i) {
1213:     const PetscDeviceType dtype = PetscDeviceTypeCast(i);
1214:     if (PetscDeviceInitialized(dtype)) { /* a non-host device was initialized */
1215:       deviceInitialized = PETSC_TRUE;
1216:       break;
1217:     }
1218:   }
1219:   /* the last condition says PETSc is configured with device but it is a pure CPU run, so don't print misleading warnings */
1220:   if (use_gpu_aware_mpi || size == 1 || !deviceInitialized) PetscFunctionReturn(PETSC_SUCCESS);
1221:   PetscCall(PetscViewerASCIIPrintf(viewer, "\n\n"));
1222:   PetscCall(PetscViewerASCIIPrintf(viewer, "      ##########################################################\n"));
1223:   PetscCall(PetscViewerASCIIPrintf(viewer, "      #                                                        #\n"));
1224:   PetscCall(PetscViewerASCIIPrintf(viewer, "      #                       WARNING!!!                       #\n"));
1225:   PetscCall(PetscViewerASCIIPrintf(viewer, "      #                                                        #\n"));
1226:   PetscCall(PetscViewerASCIIPrintf(viewer, "      #   This code was compiled with GPU support and you've   #\n"));
1227:   PetscCall(PetscViewerASCIIPrintf(viewer, "      #   created PETSc/GPU objects, but you intentionally     #\n"));
1228:   PetscCall(PetscViewerASCIIPrintf(viewer, "      #   used -use_gpu_aware_mpi 0, requiring PETSc to copy   #\n"));
1229:   PetscCall(PetscViewerASCIIPrintf(viewer, "      #   additional data between the GPU and CPU. To obtain   #\n"));
1230:   PetscCall(PetscViewerASCIIPrintf(viewer, "      #   meaningful timing results on multi-rank runs, use    #\n"));
1231:   PetscCall(PetscViewerASCIIPrintf(viewer, "      #   GPU-aware MPI instead.                               #\n"));
1232:   PetscCall(PetscViewerASCIIPrintf(viewer, "      #                                                        #\n"));
1233:   PetscCall(PetscViewerASCIIPrintf(viewer, "      ##########################################################\n\n\n"));
1234:   PetscFunctionReturn(PETSC_SUCCESS);
1235: #else
1236:   return PETSC_SUCCESS;
1237: #endif
1238: }

1240: static PetscErrorCode PetscLogViewWarnGpuTime(PetscViewer viewer)
1241: {
1242: #if PetscDefined(HAVE_DEVICE) && !PetscDefined(HAVE_KOKKOS_WITHOUT_GPU)

1244:   PetscFunctionBegin;
1245:   if (!PetscLogGpuTimeFlag || petsc_gflops == 0) PetscFunctionReturn(PETSC_SUCCESS);
1246:   PetscCall(PetscViewerASCIIPrintf(viewer, "\n\n"));
1247:   PetscCall(PetscViewerASCIIPrintf(viewer, "      ##########################################################\n"));
1248:   PetscCall(PetscViewerASCIIPrintf(viewer, "      #                                                        #\n"));
1249:   PetscCall(PetscViewerASCIIPrintf(viewer, "      #                       WARNING!!!                       #\n"));
1250:   PetscCall(PetscViewerASCIIPrintf(viewer, "      #                                                        #\n"));
1251:   PetscCall(PetscViewerASCIIPrintf(viewer, "      #   This code was run with -log_view_gpu_time            #\n"));
1252:   PetscCall(PetscViewerASCIIPrintf(viewer, "      #   This provides accurate timing within the GPU kernels #\n"));
1253:   PetscCall(PetscViewerASCIIPrintf(viewer, "      #   but can slow down the entire computation by a        #\n"));
1254:   PetscCall(PetscViewerASCIIPrintf(viewer, "      #   measurable amount. For fastest runs we recommend     #\n"));
1255:   PetscCall(PetscViewerASCIIPrintf(viewer, "      #   not using this option.                               #\n"));
1256:   PetscCall(PetscViewerASCIIPrintf(viewer, "      #                                                        #\n"));
1257:   PetscCall(PetscViewerASCIIPrintf(viewer, "      ##########################################################\n\n\n"));
1258:   PetscFunctionReturn(PETSC_SUCCESS);
1259: #else
1260:   return PETSC_SUCCESS;
1261: #endif
1262: }

1264: PETSC_INTERN int    PetscGlobalArgc;
1265: PETSC_INTERN char **PetscGlobalArgs;

1267: static PetscErrorCode PetscLogHandlerView_Default_Info(PetscLogHandler handler, PetscViewer viewer)
1268: {
1269:   PetscLogHandler_Default def = (PetscLogHandler_Default)handler->data;
1270:   char                    arch[128], hostname[128], username[128], pname[PETSC_MAX_PATH_LEN], date[128];
1271:   PetscLogDouble          locTotalTime, TotalTime, TotalFlops;
1272:   PetscLogDouble          numMessages, messageLength, avgMessLen, numReductions;
1273:   PetscLogDouble          stageTime, flops, flopr, mem, mess, messLen, red;
1274:   PetscLogDouble          fracTime, fracFlops, fracMessages, fracLength, fracReductions, fracMess, fracMessLen, fracRed;
1275:   PetscLogDouble          fracStageTime, fracStageFlops, fracStageMess, fracStageMessLen, fracStageRed;
1276:   PetscLogDouble          min, max, tot, ratio, avg, x, y;
1277:   PetscLogDouble          minf, maxf, totf, ratf, mint, maxt, tott, ratt, ratC, totm, totml, totr, mal, malmax, emalmax;
1278: #if PetscDefined(HAVE_DEVICE)
1279:   PetscLogEvent  KSP_Solve, SNES_Solve, TS_Step, TAO_Solve; /* These need to be fixed to be some events registered with certain objects */
1280:   PetscLogDouble cct, gct, csz, gsz, gmaxt, gflops, gflopr, fracgflops, gjoules, geff;
1281: #endif
1282:   PetscMPIInt   minC, maxC;
1283:   PetscMPIInt   size, rank;
1284:   PetscBool    *localStageUsed, *stageUsed;
1285:   PetscBool    *stageVisible;
1286:   PetscInt      numStages, numEvents;
1287:   int           stage, oclass;
1288:   PetscLogEvent event;
1289:   char          version[256];
1290:   MPI_Comm      comm;
1291: #if PetscDefined(HAVE_DEVICE) && !PetscDefined(HAVE_KOKKOS_WITHOUT_GPU)
1292:   PetscInt64 nas = 0x7FF0000000000002;
1293: #endif
1294:   PetscLogGlobalNames global_stages, global_events;
1295:   PetscEventPerfInfo  zero_info;
1296:   PetscLogState       state;

1298:   PetscFunctionBegin;
1299:   PetscCall(PetscLogHandlerGetState(handler, &state));
1300:   PetscCall(PetscFPTrapPush(PETSC_FP_TRAP_OFF));
1301:   PetscCall(PetscObjectGetComm((PetscObject)viewer, &comm));
1302:   PetscCallMPI(MPI_Comm_size(comm, &size));
1303:   PetscCallMPI(MPI_Comm_rank(comm, &rank));
1304:   /* Get the total elapsed time */
1305:   PetscCall(PetscTime(&locTotalTime));
1306:   locTotalTime -= petsc_BaseTime;

1308:   PetscCall(PetscViewerASCIIPrintf(viewer, "****************************************************************************************************************************************************************\n"));
1309:   PetscCall(PetscViewerASCIIPrintf(viewer, "***                                WIDEN YOUR WINDOW TO 160 CHARACTERS.  Use 'enscript -r -fCourier9' to print this document                                 ***\n"));
1310:   PetscCall(PetscViewerASCIIPrintf(viewer, "****************************************************************************************************************************************************************\n"));
1311:   PetscCall(PetscViewerASCIIPrintf(viewer, "\n------------------------------------------------------------------ PETSc Performance Summary: ------------------------------------------------------------------\n\n"));
1312:   PetscCall(PetscLogViewWarnSync(viewer));
1313:   PetscCall(PetscLogViewWarnDebugging(viewer));
1314:   PetscCall(PetscLogViewWarnNoGpuAwareMpi(viewer));
1315:   PetscCall(PetscLogViewWarnGpuTime(viewer));
1316:   PetscCall(PetscGetArchType(arch, sizeof(arch)));
1317:   PetscCall(PetscGetHostName(hostname, sizeof(hostname)));
1318:   PetscCall(PetscGetUserName(username, sizeof(username)));
1319:   PetscCall(PetscGetProgramName(pname, sizeof(pname)));
1320:   PetscCall(PetscGetDate(date, sizeof(date)));
1321:   PetscCall(PetscGetVersion(version, sizeof(version)));

1323: #if PetscDefined(HAVE_CUPM)
1324:   const char *cupm = PetscDefined(HAVE_CUDA) ? "CUDA" : "HIP";
1325:   if (PetscDeviceCUPMRuntimeArch)
1326:     PetscCall(PetscViewerASCIIPrintf(viewer, "%s on a %s named %s with %d process%s and %s architecture %d, by %s on %s\n", pname, arch, hostname, size, size > 1 ? "es" : "", cupm, PetscDeviceCUPMRuntimeArch, username, date));
1327:   else
1328: #endif
1329:     PetscCall(PetscViewerASCIIPrintf(viewer, "%s on a %s named %s with %d process%s, by %s on %s\n", pname, arch, hostname, size, size > 1 ? "es" : "", username, date));

1331: #if PetscDefined(HAVE_OPENMP)
1332:   PetscCall(PetscViewerASCIIPrintf(viewer, "Using %" PetscInt_FMT " OpenMP threads\n", PetscNumOMPThreads));
1333: #endif
1334:   PetscCall(PetscViewerASCIIPrintf(viewer, "Using %s\n", version));

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

1339:   /* Calculate summary information */
1340:   PetscCall(PetscViewerASCIIPrintf(viewer, "\n                         Max       Max/Min     Avg       Total\n"));
1341:   /*   Time */
1342:   PetscCallMPI(MPIU_Allreduce(&locTotalTime, &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm));
1343:   PetscCallMPI(MPIU_Allreduce(&locTotalTime, &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm));
1344:   PetscCallMPI(MPIU_Allreduce(&locTotalTime, &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1345:   avg = tot / ((PetscLogDouble)size);
1346:   if (min != 0.0) ratio = max / min;
1347:   else ratio = 0.0;
1348:   PetscCall(PetscViewerASCIIPrintf(viewer, "Time (sec):           %5.3e   %7.3f   %5.3e\n", max, ratio, avg));
1349:   TotalTime = tot;
1350:   /*   Objects */
1351:   {
1352:     PetscInt num_objects;

1354:     PetscCall(PetscLogObjectArrayGetSize(def->petsc_objects, &num_objects, NULL));
1355:     avg = (PetscLogDouble)num_objects;
1356:   }
1357:   PetscCallMPI(MPIU_Allreduce(&avg, &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm));
1358:   PetscCallMPI(MPIU_Allreduce(&avg, &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm));
1359:   PetscCallMPI(MPIU_Allreduce(&avg, &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1360:   avg = tot / ((PetscLogDouble)size);
1361:   if (min != 0.0) ratio = max / min;
1362:   else ratio = 0.0;
1363:   PetscCall(PetscViewerASCIIPrintf(viewer, "Objects:              %5.3e   %7.3f   %5.3e\n", max, ratio, avg));
1364:   /*   Flops */
1365:   PetscCallMPI(MPIU_Allreduce(&petsc_TotalFlops, &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm));
1366:   PetscCallMPI(MPIU_Allreduce(&petsc_TotalFlops, &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm));
1367:   PetscCallMPI(MPIU_Allreduce(&petsc_TotalFlops, &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1368:   avg = tot / ((PetscLogDouble)size);
1369:   if (min != 0.0) ratio = max / min;
1370:   else ratio = 0.0;
1371:   PetscCall(PetscViewerASCIIPrintf(viewer, "Flops:                %5.3e   %7.3f   %5.3e  %5.3e\n", max, ratio, avg, tot));
1372:   TotalFlops = tot;
1373:   /*   Flops/sec -- Must talk to Barry here */
1374:   if (locTotalTime != 0.0) flops = petsc_TotalFlops / locTotalTime;
1375:   else flops = 0.0;
1376:   PetscCallMPI(MPIU_Allreduce(&flops, &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm));
1377:   PetscCallMPI(MPIU_Allreduce(&flops, &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm));
1378:   PetscCallMPI(MPIU_Allreduce(&flops, &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1379:   avg = tot / ((PetscLogDouble)size);
1380:   if (min != 0.0) ratio = max / min;
1381:   else ratio = 0.0;
1382:   PetscCall(PetscViewerASCIIPrintf(viewer, "Flops/sec:            %5.3e   %7.3f   %5.3e  %5.3e\n", max, ratio, avg, tot));
1383:   /*   Memory */
1384:   PetscCall(PetscMallocGetMaximumUsage(&mem));
1385:   if (mem > 0.0) {
1386:     PetscCallMPI(MPIU_Allreduce(&mem, &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm));
1387:     PetscCallMPI(MPIU_Allreduce(&mem, &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm));
1388:     PetscCallMPI(MPIU_Allreduce(&mem, &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1389:     avg = tot / ((PetscLogDouble)size);
1390:     if (min != 0.0) ratio = max / min;
1391:     else ratio = 0.0;
1392:     PetscCall(PetscViewerASCIIPrintf(viewer, "Memory (bytes):       %5.3e   %7.3f   %5.3e  %5.3e\n", max, ratio, avg, tot));
1393:   }
1394:   /*   Messages */
1395:   mess = 0.5 * (petsc_irecv_ct + petsc_isend_ct + petsc_recv_ct + petsc_send_ct);
1396:   PetscCallMPI(MPIU_Allreduce(&mess, &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm));
1397:   PetscCallMPI(MPIU_Allreduce(&mess, &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm));
1398:   PetscCallMPI(MPIU_Allreduce(&mess, &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1399:   avg = tot / ((PetscLogDouble)size);
1400:   if (min != 0.0) ratio = max / min;
1401:   else ratio = 0.0;
1402:   PetscCall(PetscViewerASCIIPrintf(viewer, "MPI Msg Count:        %5.3e   %7.3f   %5.3e  %5.3e\n", max, ratio, avg, tot));
1403:   numMessages = tot;
1404:   /*   Message Lengths */
1405:   mess = 0.5 * (petsc_irecv_len + petsc_isend_len + petsc_recv_len + petsc_send_len);
1406:   PetscCallMPI(MPIU_Allreduce(&mess, &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm));
1407:   PetscCallMPI(MPIU_Allreduce(&mess, &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm));
1408:   PetscCallMPI(MPIU_Allreduce(&mess, &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1409:   if (numMessages != 0) avg = tot / numMessages;
1410:   else avg = 0.0;
1411:   if (min != 0.0) ratio = max / min;
1412:   else ratio = 0.0;
1413:   PetscCall(PetscViewerASCIIPrintf(viewer, "MPI Msg Len (bytes):  %5.3e   %7.3f   %5.3e  %5.3e\n", max, ratio, avg, tot));
1414:   messageLength = tot;
1415:   /*   Reductions */
1416:   PetscCallMPI(MPIU_Allreduce(&red, &min, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm));
1417:   PetscCallMPI(MPIU_Allreduce(&red, &max, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm));
1418:   PetscCallMPI(MPIU_Allreduce(&red, &tot, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1419:   if (min != 0.0) ratio = max / min;
1420:   else ratio = 0.0;
1421:   PetscCall(PetscViewerASCIIPrintf(viewer, "MPI Reductions:       %5.3e   %7.3f\n", max, ratio));
1422:   numReductions = red; /* wrong because uses count from process zero */
1423:   PetscCall(PetscViewerASCIIPrintf(viewer, "\nFlop counting convention: 1 flop = 1 real number operation of type (multiply/divide/add/subtract)\n"));
1424:   PetscCall(PetscViewerASCIIPrintf(viewer, "                            e.g., VecAXPY() for real vectors of length N --> 2N flops\n"));
1425:   PetscCall(PetscViewerASCIIPrintf(viewer, "                            and VecAXPY() for complex vectors of length N --> 8N flops\n"));

1427:   PetscCall(PetscLogRegistryCreateGlobalStageNames(comm, state->registry, &global_stages));
1428:   PetscCall(PetscLogRegistryCreateGlobalEventNames(comm, state->registry, &global_events));
1429:   PetscCall(PetscLogGlobalNamesGetSize(global_stages, NULL, &numStages));
1430:   PetscCall(PetscLogGlobalNamesGetSize(global_events, NULL, &numEvents));
1431:   PetscCall(PetscMemzero(&zero_info, sizeof(zero_info)));
1432:   PetscCall(PetscMalloc1(numStages, &localStageUsed));
1433:   PetscCall(PetscMalloc1(numStages, &stageUsed));
1434:   PetscCall(PetscMalloc1(numStages, &stageVisible));
1435:   if (numStages > 0) {
1436:     for (stage = 0; stage < numStages; stage++) {
1437:       PetscInt stage_id;

1439:       PetscCall(PetscLogGlobalNamesGlobalGetLocal(global_stages, stage, &stage_id));
1440:       if (stage_id >= 0) {
1441:         PetscStagePerf *stage_info;

1443:         PetscCall(PetscLogHandlerDefaultGetStageInfo(handler, stage, &stage_info));
1444:         localStageUsed[stage] = stage_info->used;
1445:         stageVisible[stage]   = stage_info->perfInfo.visible;
1446:       } else {
1447:         localStageUsed[stage] = PETSC_FALSE;
1448:         stageVisible[stage]   = PETSC_TRUE;
1449:       }
1450:     }
1451:     PetscCallMPI(MPIU_Allreduce(localStageUsed, stageUsed, numStages, MPI_C_BOOL, MPI_LOR, comm));
1452:     PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, stageVisible, numStages, MPI_C_BOOL, MPI_LAND, comm));
1453:     for (stage = 0; stage < numStages; stage++) {
1454:       if (stageUsed[stage] && stageVisible[stage]) {
1455:         PetscCall(PetscViewerASCIIPrintf(viewer, "\nSummary of Stages:   ----- Time ------  ----- Flop ------  --- Messages ---  -- Message Lengths --  -- Reductions --\n"));
1456:         PetscCall(PetscViewerASCIIPrintf(viewer, "                        Avg     %%Total     Avg     %%Total    Count   %%Total     Avg         %%Total    Count   %%Total\n"));
1457:         break;
1458:       }
1459:     }
1460:     for (stage = 0; stage < numStages; stage++) {
1461:       PetscInt            stage_id;
1462:       PetscEventPerfInfo *stage_info;
1463:       const char         *stage_name;

1465:       if (!(stageUsed[stage] && stageVisible[stage])) continue;
1466:       PetscCall(PetscLogGlobalNamesGlobalGetLocal(global_stages, stage, &stage_id));
1467:       PetscCall(PetscLogGlobalNamesGlobalGetName(global_stages, stage, &stage_name));
1468:       stage_info = &zero_info;
1469:       if (localStageUsed[stage]) {
1470:         PetscStagePerf *stage_perf_info;

1472:         PetscCall(PetscLogHandlerDefaultGetStageInfo(handler, stage, &stage_perf_info));
1473:         stage_info = &stage_perf_info->perfInfo;
1474:       }
1475:       PetscCallMPI(MPIU_Allreduce(&stage_info->time, &stageTime, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1476:       PetscCallMPI(MPIU_Allreduce(&stage_info->flops, &flops, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1477:       PetscCallMPI(MPIU_Allreduce(&stage_info->numMessages, &mess, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1478:       PetscCallMPI(MPIU_Allreduce(&stage_info->messageLength, &messLen, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1479:       PetscCallMPI(MPIU_Allreduce(&stage_info->numReductions, &red, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1480:       mess *= 0.5;
1481:       messLen *= 0.5;
1482:       red /= size;
1483:       if (TotalTime != 0.0) fracTime = stageTime / TotalTime;
1484:       else fracTime = 0.0;
1485:       if (TotalFlops != 0.0) fracFlops = flops / TotalFlops;
1486:       else fracFlops = 0.0;
1487:       /* Talk to Barry if (stageTime     != 0.0) flops          = (size*flops)/stageTime; else flops          = 0.0; */
1488:       if (numMessages != 0.0) fracMessages = mess / numMessages;
1489:       else fracMessages = 0.0;
1490:       if (mess != 0.0) avgMessLen = messLen / mess;
1491:       else avgMessLen = 0.0;
1492:       if (messageLength != 0.0) fracLength = messLen / messageLength;
1493:       else fracLength = 0.0;
1494:       if (numReductions != 0.0) fracReductions = red / numReductions;
1495:       else fracReductions = 0.0;
1496:       PetscCall(PetscViewerASCIIPrintf(viewer, "%2d: %15s: %6.4e %5.1f%%  %6.4e %5.1f%%  %5.3e %5.1f%%  %5.3e      %5.1f%%  %5.3e %5.1f%%\n", stage, stage_name, stageTime / size, 100.0 * fracTime, flops, 100.0 * fracFlops, mess, 100.0 * fracMessages, avgMessLen, 100.0 * fracLength, red, 100.0 * fracReductions));
1497:     }
1498:   }

1500:   PetscCall(PetscViewerASCIIPrintf(viewer, "\n------------------------------------------------------------------------------------------------------------------------\n"));
1501:   PetscCall(PetscViewerASCIIPrintf(viewer, "See the 'Profiling' chapter of the users' manual for details on interpreting output.\n"));
1502:   PetscCall(PetscViewerASCIIPrintf(viewer, "Phase summary info:\n"));
1503:   PetscCall(PetscViewerASCIIPrintf(viewer, "   Count: number of times phase was executed\n"));
1504:   PetscCall(PetscViewerASCIIPrintf(viewer, "   Time and Flop: Max - maximum over all processes\n"));
1505:   PetscCall(PetscViewerASCIIPrintf(viewer, "                  Ratio - ratio of maximum to minimum over all processes\n"));
1506:   PetscCall(PetscViewerASCIIPrintf(viewer, "   Mess: number of messages sent\n"));
1507:   PetscCall(PetscViewerASCIIPrintf(viewer, "   AvgLen: average message length (bytes)\n"));
1508:   PetscCall(PetscViewerASCIIPrintf(viewer, "   Reduct: number of global reductions\n"));
1509:   PetscCall(PetscViewerASCIIPrintf(viewer, "   Global: entire computation\n"));
1510:   PetscCall(PetscViewerASCIIPrintf(viewer, "   Stage: stages of a computation. Set stages with PetscLogStagePush() and PetscLogStagePop().\n"));
1511:   PetscCall(PetscViewerASCIIPrintf(viewer, "      %%T - percent time in this phase         %%F - percent flop in this phase\n"));
1512:   PetscCall(PetscViewerASCIIPrintf(viewer, "      %%M - percent messages in this phase     %%L - percent message lengths in this phase\n"));
1513:   PetscCall(PetscViewerASCIIPrintf(viewer, "      %%R - percent reductions in this phase\n"));
1514:   PetscCall(PetscViewerASCIIPrintf(viewer, "   Total Mflop/s: 1e-6 * (sum of flop over all processes)/(max time over all processes)\n"));
1515:   if (PetscLogMemory) {
1516:     PetscCall(PetscViewerASCIIPrintf(viewer, "   Memory usage is summed over all MPI processes, it is given in mega-bytes\n"));
1517:     PetscCall(PetscViewerASCIIPrintf(viewer, "   Malloc Mbytes: Memory allocated and kept during event (sum over all calls to event). May be negative\n"));
1518:     PetscCall(PetscViewerASCIIPrintf(viewer, "   EMalloc Mbytes: extra memory allocated during event and then freed (maximum over all calls to events). Never negative\n"));
1519:     PetscCall(PetscViewerASCIIPrintf(viewer, "   MMalloc Mbytes: Increase in high water mark of allocated memory (sum over all calls to event). Never negative\n"));
1520:     PetscCall(PetscViewerASCIIPrintf(viewer, "   RMI Mbytes: Increase in resident memory (sum over all calls to event)\n"));
1521:   }
1522: #if PetscDefined(HAVE_DEVICE)
1523:   PetscCall(PetscViewerASCIIPrintf(viewer, "   GPU Mflop/s: 1e-6 * (sum of flop on GPU over all processes)/(max GPU time over all processes)\n"));
1524:   PetscCall(PetscViewerASCIIPrintf(viewer, "   CpuToGpu Count: max number of CPU to GPU copies over all processes\n"));
1525:   PetscCall(PetscViewerASCIIPrintf(viewer, "   CpuToGpu Size (Mbytes): 1e-6 * (sum of size of CPU to GPU copies over all processes)/(number of processes)\n"));
1526:   PetscCall(PetscViewerASCIIPrintf(viewer, "   GpuToCpu Count: max number of GPU to CPU copies over all processes\n"));
1527:   PetscCall(PetscViewerASCIIPrintf(viewer, "   GpuToCpu Size (Mbytes): 1e-6 * (sum of size of GPU to CPU copies over all processes)/(number of processes)\n"));
1528:   PetscCall(PetscViewerASCIIPrintf(viewer, "   GPU %%F: percent flops on GPU in this event\n"));
1529: #endif
1530:   PetscCall(PetscViewerASCIIPrintf(viewer, "------------------------------------------------------------------------------------------------------------------------\n"));

1532:   PetscCall(PetscLogViewWarnDebugging(viewer));

1534:   /* Report events */
1535:   PetscCall(PetscViewerASCIIPrintf(viewer, "Event                Count      Time (sec)     Flop                              --- Global ---  --- Stage ----  Total"));
1536:   if (PetscLogMemory) PetscCall(PetscViewerASCIIPrintf(viewer, "  Malloc EMalloc MMalloc RMI"));
1537: #if PetscDefined(HAVE_DEVICE)
1538:   PetscCall(PetscViewerASCIIPrintf(viewer, "   GPU    - CpuToGpu -   - GpuToCpu - GPU"));
1539:   if (PetscLogGpuEnergyFlag || PetscLogGpuEnergyMeterFlag) PetscCall(PetscViewerASCIIPrintf(viewer, "    GPU      GPU"));
1540: #endif
1541:   PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
1542:   PetscCall(PetscViewerASCIIPrintf(viewer, "                   Max Ratio  Max     Ratio   Max  Ratio  Mess   AvgLen  Reduct  %%T %%F %%M %%L %%R  %%T %%F %%M %%L %%R Mflop/s"));
1543:   if (PetscLogMemory) PetscCall(PetscViewerASCIIPrintf(viewer, " Mbytes Mbytes Mbytes Mbytes"));
1544: #if PetscDefined(HAVE_DEVICE)
1545:   PetscCall(PetscViewerASCIIPrintf(viewer, " Mflop/s Count   Size   Count   Size  %%F"));
1546:   if (PetscLogGpuEnergyFlag || PetscLogGpuEnergyMeterFlag) PetscCall(PetscViewerASCIIPrintf(viewer, "   Joule   Mflop/J"));
1547: #endif
1548:   PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
1549:   PetscCall(PetscViewerASCIIPrintf(viewer, "------------------------------------------------------------------------------------------------------------------------"));
1550:   if (PetscLogMemory) PetscCall(PetscViewerASCIIPrintf(viewer, "-----------------------------"));
1551: #if PetscDefined(HAVE_DEVICE)
1552:   PetscCall(PetscViewerASCIIPrintf(viewer, "---------------------------------------"));
1553:   if (PetscLogGpuEnergyFlag || PetscLogGpuEnergyMeterFlag) PetscCall(PetscViewerASCIIPrintf(viewer, "-------------------"));
1554: #endif
1555:   PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));

1557: #if PetscDefined(HAVE_DEVICE)
1558:   /* this indirect way of accessing these values is needed when PETSc is build with multiple libraries since the symbols are not in libpetscsys */
1559:   PetscCall(PetscLogStateGetEventFromName(state, "TaoSolve", &TAO_Solve));
1560:   PetscCall(PetscLogStateGetEventFromName(state, "TSStep", &TS_Step));
1561:   PetscCall(PetscLogStateGetEventFromName(state, "SNESSolve", &SNES_Solve));
1562:   PetscCall(PetscLogStateGetEventFromName(state, "KSPSolve", &KSP_Solve));
1563: #endif

1565:   for (stage = 0; stage < numStages; stage++) {
1566:     PetscInt            stage_id;
1567:     PetscEventPerfInfo *stage_info;
1568:     const char         *stage_name;

1570:     if (!(stageVisible[stage] && stageUsed[stage])) continue;
1571:     PetscCall(PetscLogGlobalNamesGlobalGetLocal(global_stages, stage, &stage_id));
1572:     PetscCall(PetscLogGlobalNamesGlobalGetName(global_stages, stage, &stage_name));
1573:     PetscCall(PetscViewerASCIIPrintf(viewer, "\n--- Event Stage %d: %s\n\n", stage, stage_name));
1574:     stage_info = &zero_info;
1575:     if (localStageUsed[stage]) {
1576:       PetscStagePerf *stage_perf_info;

1578:       PetscCall(PetscLogHandlerDefaultGetStageInfo(handler, stage_id, &stage_perf_info));
1579:       stage_info = &stage_perf_info->perfInfo;
1580:     }
1581:     PetscCallMPI(MPIU_Allreduce(&stage_info->time, &stageTime, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1582:     PetscCallMPI(MPIU_Allreduce(&stage_info->flops, &flops, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1583:     PetscCallMPI(MPIU_Allreduce(&stage_info->numMessages, &mess, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1584:     PetscCallMPI(MPIU_Allreduce(&stage_info->messageLength, &messLen, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1585:     PetscCallMPI(MPIU_Allreduce(&stage_info->numReductions, &red, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1586:     mess *= 0.5;
1587:     messLen *= 0.5;
1588:     red /= size;

1590:     for (event = 0; event < numEvents; event++) {
1591:       PetscInt            event_id;
1592:       PetscEventPerfInfo *event_info = &zero_info;
1593:       PetscBool           is_zero    = PETSC_FALSE;
1594:       const char         *event_name;

1596:       PetscCall(PetscLogGlobalNamesGlobalGetLocal(global_events, event, &event_id));
1597:       PetscCall(PetscLogGlobalNamesGlobalGetName(global_events, event, &event_name));
1598:       if (event_id >= 0 && stage_id >= 0) PetscCall(PetscLogHandlerGetEventPerfInfo_Default(handler, stage_id, event_id, &event_info));
1599:       PetscCall(PetscArraycmp(event_info, &zero_info, 1, &is_zero));
1600:       PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &is_zero, 1, MPI_C_BOOL, MPI_LAND, comm));
1601:       if (!is_zero) {
1602:         flopr = event_info->flops;
1603:         PetscCallMPI(MPIU_Allreduce(&flopr, &minf, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm));
1604:         PetscCallMPI(MPIU_Allreduce(&flopr, &maxf, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm));
1605:         PetscCallMPI(MPIU_Allreduce(&event_info->flops, &totf, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1606:         PetscCallMPI(MPIU_Allreduce(&event_info->time, &mint, 1, MPIU_PETSCLOGDOUBLE, MPI_MIN, comm));
1607:         PetscCallMPI(MPIU_Allreduce(&event_info->time, &maxt, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm));
1608:         PetscCallMPI(MPIU_Allreduce(&event_info->time, &tott, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1609:         PetscCallMPI(MPIU_Allreduce(&event_info->numMessages, &totm, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1610:         PetscCallMPI(MPIU_Allreduce(&event_info->messageLength, &totml, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1611:         PetscCallMPI(MPIU_Allreduce(&event_info->numReductions, &totr, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1612:         PetscCallMPI(MPIU_Allreduce(&event_info->count, &minC, 1, MPI_INT, MPI_MIN, comm));
1613:         PetscCallMPI(MPIU_Allreduce(&event_info->count, &maxC, 1, MPI_INT, MPI_MAX, comm));
1614:         if (PetscLogMemory) {
1615:           PetscCallMPI(MPIU_Allreduce(&event_info->memIncrease, &mem, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1616:           PetscCallMPI(MPIU_Allreduce(&event_info->mallocSpace, &mal, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1617:           PetscCallMPI(MPIU_Allreduce(&event_info->mallocIncrease, &malmax, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1618:           PetscCallMPI(MPIU_Allreduce(&event_info->mallocIncreaseEvent, &emalmax, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1619:         }
1620: #if PetscDefined(HAVE_DEVICE)
1621:         /* the counts are the maximum over the processes, so that a copy done on a subset of them is not rounded away by an average below one half */
1622:         PetscCallMPI(MPIU_Allreduce(&event_info->CpuToGpuCount, &cct, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm));
1623:         PetscCallMPI(MPIU_Allreduce(&event_info->GpuToCpuCount, &gct, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm));
1624:         PetscCallMPI(MPIU_Allreduce(&event_info->CpuToGpuSize, &csz, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1625:         PetscCallMPI(MPIU_Allreduce(&event_info->GpuToCpuSize, &gsz, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1626:         PetscCallMPI(MPIU_Allreduce(&event_info->GpuFlops, &gflops, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1627:         PetscCallMPI(MPIU_Allreduce(&event_info->GpuTime, &gmaxt, 1, MPIU_PETSCLOGDOUBLE, MPI_MAX, comm));
1628:         if (PetscLogGpuEnergyFlag || PetscLogGpuEnergyMeterFlag) PetscCallMPI(MPIU_Allreduce(&event_info->GpuEnergy, &gjoules, 1, MPIU_PETSCLOGDOUBLE, MPI_SUM, comm));
1629: #endif
1630:         if (mint < 0.0) {
1631:           PetscCall(PetscViewerASCIIPrintf(viewer, "WARNING!!! Minimum time %g over all processes 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, event_name));
1632:           mint = 0;
1633:         }
1634:         PetscCheck(minf >= 0.0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Minimum flop %g over all processes for %s is negative! Not possible!", minf, event_name);
1635: #if PetscDefined(HAVE_DEVICE) && !PetscDefined(HAVE_KOKKOS_WITHOUT_GPU)
1636:         /* Put NaN into the time for all events that may not be time accurately since they may happen asynchronously on the GPU */
1637:         if (!PetscLogGpuTimeFlag && petsc_gflops > 0) {
1638:           memcpy(&gmaxt, &nas, sizeof(PetscLogDouble));
1639:           if (event_id != SNES_Solve && event_id != KSP_Solve && event_id != TS_Step && event_id != TAO_Solve) {
1640:             memcpy(&mint, &nas, sizeof(PetscLogDouble));
1641:             memcpy(&maxt, &nas, sizeof(PetscLogDouble));
1642:           }
1643:         }
1644: #endif
1645:         totm *= 0.5;
1646:         totml *= 0.5;
1647:         totr /= size;

1649:         if (maxC != 0) {
1650:           if (minC != 0) ratC = ((PetscLogDouble)maxC) / minC;
1651:           else ratC = 0.0;
1652:           if (mint != 0.0) ratt = maxt / mint;
1653:           else ratt = 0.0;
1654:           if (minf != 0.0) ratf = maxf / minf;
1655:           else ratf = 0.0;
1656:           if (TotalTime != 0.0) fracTime = tott / TotalTime;
1657:           else fracTime = 0.0;
1658:           if (TotalFlops != 0.0) fracFlops = totf / TotalFlops;
1659:           else fracFlops = 0.0;
1660:           if (stageTime != 0.0) fracStageTime = tott / stageTime;
1661:           else fracStageTime = 0.0;
1662:           if (flops != 0.0) fracStageFlops = totf / flops;
1663:           else fracStageFlops = 0.0;
1664:           if (numMessages != 0.0) fracMess = totm / numMessages;
1665:           else fracMess = 0.0;
1666:           if (messageLength != 0.0) fracMessLen = totml / messageLength;
1667:           else fracMessLen = 0.0;
1668:           if (numReductions != 0.0) fracRed = totr / numReductions;
1669:           else fracRed = 0.0;
1670:           if (mess != 0.0) fracStageMess = totm / mess;
1671:           else fracStageMess = 0.0;
1672:           if (messLen != 0.0) fracStageMessLen = totml / messLen;
1673:           else fracStageMessLen = 0.0;
1674:           if (red != 0.0) fracStageRed = totr / red;
1675:           else fracStageRed = 0.0;
1676:           if (totm != 0.0) totml /= totm;
1677:           else totml = 0.0;
1678:           if (maxt != 0.0) flopr = totf / maxt;
1679:           else flopr = 0.0;
1680:           if (fracStageTime > 1.0 || fracStageFlops > 1.0 || fracStageMess > 1.0 || fracStageMessLen > 1.0 || fracStageRed > 1.0) {
1681:             if (PetscIsNanReal(maxt))
1682:               PetscCall(PetscViewerASCIIPrintf(viewer, "%-16s %7d %3.1f  n/a     n/a   %3.2e %3.1f %2.1e %2.1e %2.1e %2.0f %2.0f %2.0f %2.0f %2.0f Multiple stages n/a", event_name, maxC, ratC, maxf, ratf, totm, totml, totr, 100.0 * fracTime, 100.0 * fracFlops, 100.0 * fracMess, 100.0 * fracMessLen, 100.0 * fracRed));
1683:             else
1684:               PetscCall(PetscViewerASCIIPrintf(viewer, "%-16s %7d %3.1f %5.4e %3.1f %3.2e %3.1f %2.1e %2.1e %2.1e %2.0f %2.0f %2.0f %2.0f %2.0f Multiple stages %5.0f", event_name, maxC, ratC, maxt, ratt, maxf, ratf, totm, totml, totr, 100.0 * fracTime, 100.0 * fracFlops, 100.0 * fracMess, 100.0 * fracMessLen, 100.0 * fracRed, PetscAbs(flopr) / 1.0e6));
1685:           } else {
1686:             if (PetscIsNanReal(maxt)) { // when maxt, ratt, flopr are NaN (i.e., run with GPUs but without -log_view_gpu_time), replace the confusing "nan" with "n/a"
1687:               PetscCall(PetscViewerASCIIPrintf(viewer, "%-16s %7d %3.1f  n/a     n/a   %3.2e %3.1f %2.1e %2.1e %2.1e %2.0f %2.0f %2.0f %2.0f %2.0f %3.0f %2.0f %2.0f %2.0f %2.0f  n/a", event_name, maxC, ratC, maxf, ratf, totm, totml, totr, 100.0 * fracTime, 100.0 * fracFlops, 100.0 * fracMess, 100.0 * fracMessLen, 100.0 * fracRed, 100.0 * fracStageTime, 100.0 * fracStageFlops, 100.0 * fracStageMess, 100.0 * fracStageMessLen, 100.0 * fracStageRed));
1688:             } else {
1689:               PetscCall(PetscViewerASCIIPrintf(viewer, "%-16s %7d %3.1f %5.4e %3.1f %3.2e %3.1f %2.1e %2.1e %2.1e %2.0f %2.0f %2.0f %2.0f %2.0f %3.0f %2.0f %2.0f %2.0f %2.0f %5.0f", event_name, maxC, ratC, maxt, ratt, maxf, ratf, totm, totml, totr, 100.0 * fracTime, 100.0 * fracFlops, 100.0 * fracMess, 100.0 * fracMessLen, 100.0 * fracRed, 100.0 * fracStageTime, 100.0 * fracStageFlops, 100.0 * fracStageMess, 100.0 * fracStageMessLen, 100.0 * fracStageRed, PetscAbs(flopr) / 1.0e6));
1690:             }
1691:           }
1692:           if (PetscLogMemory) PetscCall(PetscViewerASCIIPrintf(viewer, " %5.0f   %5.0f   %5.0f   %5.0f", mal / 1.0e6, emalmax / 1.0e6, malmax / 1.0e6, mem / 1.0e6));
1693: #if PetscDefined(HAVE_DEVICE)
1694:           if (totf != 0.0) fracgflops = gflops / totf;
1695:           else fracgflops = 0.0;
1696:           if (gmaxt != 0.0) gflopr = gflops / gmaxt;
1697:           else gflopr = 0.0;
1698:           if (PetscIsNanReal(gflopr)) {
1699:             PetscCall(PetscViewerASCIIPrintf(viewer, "    n/a    %4.0f %3.2e %4.0f %3.2e %2.0f", cct, csz / (1.0e6 * size), gct, gsz / (1.0e6 * size), 100.0 * fracgflops));
1700:           } else {
1701:             PetscCall(PetscViewerASCIIPrintf(viewer, "   %5.0f   %4.0f %3.2e %4.0f %3.2e %2.0f", PetscAbs(gflopr) / 1.0e6, cct, csz / (1.0e6 * size), gct, gsz / (1.0e6 * size), 100.0 * fracgflops));
1702:           }
1703:           if (PetscLogGpuEnergyFlag || PetscLogGpuEnergyMeterFlag) {
1704:             if (gjoules != 0.0) geff = gflops / gjoules;
1705:             else geff = 0.0;
1706:             PetscCall(PetscViewerASCIIPrintf(viewer, "  %4.2e  %4.2e", gjoules, geff));
1707:           }
1708: #endif
1709:           PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
1710:         }
1711:       }
1712:     }
1713:   }

1715:   /* Memory usage and object creation */
1716:   PetscCall(PetscViewerASCIIPrintf(viewer, "------------------------------------------------------------------------------------------------------------------------"));
1717:   if (PetscLogMemory) PetscCall(PetscViewerASCIIPrintf(viewer, "-----------------------------"));
1718: #if PetscDefined(HAVE_DEVICE)
1719:   PetscCall(PetscViewerASCIIPrintf(viewer, "---------------------------------------"));
1720:   if (PetscLogGpuEnergyFlag || PetscLogGpuEnergyMeterFlag) PetscCall(PetscViewerASCIIPrintf(viewer, "-------------------"));
1721: #endif
1722:   PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
1723:   PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));

1725:   /* Right now, only stages on the first processor are reported here, meaning only objects associated with
1726:      the global communicator, or MPI_COMM_SELF for proc 1. We really should report global stats and then
1727:      stats for stages local to processor sets.
1728:   */
1729:   /* We should figure out the longest object name here (now 20 characters) */
1730:   PetscCall(PetscViewerASCIIPrintf(viewer, "Object Type          Creations   Destructions. Reports information only for process 0.\n"));
1731:   for (stage = 0; stage < numStages; stage++) {
1732:     const char *stage_name;

1734:     PetscCall(PetscLogGlobalNamesGlobalGetName(global_stages, stage, &stage_name));
1735:     PetscCall(PetscViewerASCIIPrintf(viewer, "\n--- Event Stage %d: %s\n\n", stage, stage_name));
1736:     if (localStageUsed[stage]) {
1737:       PetscInt num_classes;

1739:       PetscCall(PetscLogStateGetNumClasses(state, &num_classes));
1740:       for (oclass = 0; oclass < num_classes; oclass++) {
1741:         PetscClassPerf *class_perf_info;

1743:         PetscCall(PetscLogHandlerDefaultGetClassPerf(handler, stage, oclass, &class_perf_info));
1744:         if (class_perf_info->creations > 0 || class_perf_info->destructions > 0) {
1745:           PetscLogClassInfo class_reg_info;
1746:           PetscBool         flg = PETSC_FALSE;

1748:           PetscCall(PetscLogStateClassGetInfo(state, oclass, &class_reg_info));
1749:           if (stage == 0 && oclass == num_classes - 1) {
1750:             if (PetscGlobalArgc == 0 && PetscGlobalArgs == NULL) {
1751:               PetscCall(PetscStrcmp(class_reg_info.name, "Viewer", &flg));
1752:               if (flg && class_perf_info->creations == PetscLogNumViewersCreated && class_perf_info->destructions == PetscLogNumViewersDestroyed) continue;
1753:             }
1754:           }
1755:           PetscCall(PetscViewerASCIIPrintf(viewer, "%20s %5d          %5d\n", class_reg_info.name, class_perf_info->creations, class_perf_info->destructions));
1756:         }
1757:       }
1758:     }
1759:   }

1761:   PetscCall(PetscFree(localStageUsed));
1762:   PetscCall(PetscFree(stageUsed));
1763:   PetscCall(PetscFree(stageVisible));
1764:   PetscCall(PetscLogGlobalNamesDestroy(&global_stages));
1765:   PetscCall(PetscLogGlobalNamesDestroy(&global_events));

1767:   /* Information unrelated to this particular run */
1768:   PetscCall(PetscViewerASCIIPrintf(viewer, "========================================================================================================================\n"));
1769:   PetscCall(PetscTime(&y));
1770:   PetscCall(PetscTime(&x));
1771:   PetscCall(PetscTime(&y));
1772:   PetscCall(PetscTime(&y));
1773:   PetscCall(PetscTime(&y));
1774:   PetscCall(PetscTime(&y));
1775:   PetscCall(PetscTime(&y));
1776:   PetscCall(PetscTime(&y));
1777:   PetscCall(PetscTime(&y));
1778:   PetscCall(PetscTime(&y));
1779:   PetscCall(PetscTime(&y));
1780:   PetscCall(PetscTime(&y));
1781:   PetscCall(PetscViewerASCIIPrintf(viewer, "Average time to get PetscTime(): %g\n", (y - x) / 10.0));
1782:   /* MPI information */
1783:   if (size > 1) {
1784:     MPI_Status  status;
1785:     PetscMPIInt tag;
1786:     MPI_Comm    newcomm;

1788:     PetscCallMPI(MPI_Barrier(comm));
1789:     PetscCall(PetscTime(&x));
1790:     PetscCallMPI(MPI_Barrier(comm));
1791:     PetscCallMPI(MPI_Barrier(comm));
1792:     PetscCallMPI(MPI_Barrier(comm));
1793:     PetscCallMPI(MPI_Barrier(comm));
1794:     PetscCallMPI(MPI_Barrier(comm));
1795:     PetscCall(PetscTime(&y));
1796:     PetscCall(PetscViewerASCIIPrintf(viewer, "Average time for MPI_Barrier(): %g\n", (y - x) / 5.0));
1797:     PetscCall(PetscCommDuplicate(comm, &newcomm, &tag));
1798:     PetscCallMPI(MPI_Barrier(comm));
1799:     if (rank) {
1800:       PetscCallMPI(MPI_Recv(NULL, 0, MPI_INT, rank - 1, tag, newcomm, &status));
1801:       PetscCallMPI(MPI_Send(NULL, 0, MPI_INT, (rank + 1) % size, tag, newcomm));
1802:     } else {
1803:       PetscCall(PetscTime(&x));
1804:       PetscCallMPI(MPI_Send(NULL, 0, MPI_INT, 1, tag, newcomm));
1805:       PetscCallMPI(MPI_Recv(NULL, 0, MPI_INT, size - 1, tag, newcomm, &status));
1806:       PetscCall(PetscTime(&y));
1807:       PetscCall(PetscViewerASCIIPrintf(viewer, "Average time for zero size MPI_Send(): %g\n", (y - x) / size));
1808:     }
1809:     PetscCall(PetscCommDestroy(&newcomm));
1810:   }
1811:   PetscCall(PetscOptionsView(NULL, viewer));

1813:   /* Machine and compile information */
1814:   if (PetscDefined(USE_FORTRAN_KERNELS)) {
1815:     PetscCall(PetscViewerASCIIPrintf(viewer, "Compiled with FORTRAN kernels\n"));
1816:   } else {
1817:     PetscCall(PetscViewerASCIIPrintf(viewer, "Compiled without FORTRAN kernels\n"));
1818:   }
1819:   if (PetscDefined(USE_64BIT_INDICES)) {
1820:     PetscCall(PetscViewerASCIIPrintf(viewer, "Compiled with 64-bit PetscInt\n"));
1821:   } else if (PetscDefined(USE___FLOAT128)) {
1822:     PetscCall(PetscViewerASCIIPrintf(viewer, "Compiled with 32-bit PetscInt\n"));
1823:   }
1824:   if (PetscDefined(USE_REAL_SINGLE)) {
1825:     PetscCall(PetscViewerASCIIPrintf(viewer, "Compiled with single precision PetscScalar and PetscReal\n"));
1826:   } else if (PetscDefined(USE___FLOAT128)) {
1827:     PetscCall(PetscViewerASCIIPrintf(viewer, "Compiled with 128 bit precision PetscScalar and PetscReal\n"));
1828:   }
1829:   if (PetscDefined(USE_REAL_MAT_SINGLE)) {
1830:     PetscCall(PetscViewerASCIIPrintf(viewer, "Compiled with single precision matrices\n"));
1831:   } else {
1832:     PetscCall(PetscViewerASCIIPrintf(viewer, "Compiled with full precision matrices (default)\n"));
1833:   }
1834:   PetscCall(PetscViewerASCIIPrintf(viewer, "sizeof(short) %d sizeof(int) %d sizeof(long) %d sizeof(void*) %d sizeof(PetscScalar) %d sizeof(PetscInt) %d\n", (int)sizeof(short), (int)sizeof(int), (int)sizeof(long), (int)sizeof(void *), (int)sizeof(PetscScalar), (int)sizeof(PetscInt)));

1836:   PetscCall(PetscViewerASCIIPrintf(viewer, "Configure options: %s", petscconfigureoptions));
1837:   PetscCall(PetscViewerASCIIPrintf(viewer, "%s", petscmachineinfo));
1838:   PetscCall(PetscViewerASCIIPrintf(viewer, "%s", petsccompilerinfo));
1839:   PetscCall(PetscViewerASCIIPrintf(viewer, "%s", petsccompilerflagsinfo));
1840:   PetscCall(PetscViewerASCIIPrintf(viewer, "%s", petsclinkerinfo));

1842:   /* Cleanup */
1843:   PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
1844:   PetscCall(PetscLogViewWarnNoGpuAwareMpi(viewer));
1845:   PetscCall(PetscLogViewWarnDebugging(viewer));
1846:   PetscCall(PetscFPTrapPop());
1847:   PetscFunctionReturn(PETSC_SUCCESS);
1848: }
1849: PETSC_PRAGMA_DIAGNOSTIC_IGNORED_END()

1851: static PetscErrorCode PetscLogHandlerView_Default(PetscLogHandler handler, PetscViewer viewer)
1852: {
1853:   PetscViewerFormat format;

1855:   PetscFunctionBegin;
1856:   PetscCall(PetscViewerGetFormat(viewer, &format));
1857:   if (format == PETSC_VIEWER_DEFAULT || format == PETSC_VIEWER_ASCII_INFO) {
1858:     PetscCall(PetscLogHandlerView_Default_Info(handler, viewer));
1859:   } else if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
1860:     PetscCall(PetscLogHandlerView_Default_Detailed(handler, viewer));
1861:   } else if (format == PETSC_VIEWER_ASCII_CSV) {
1862:     PetscCall(PetscLogHandlerView_Default_CSV(handler, viewer));
1863:   }
1864:   PetscFunctionReturn(PETSC_SUCCESS);
1865: }

1867: /*MC
1868:   PETSCLOGHANDLERDEFAULT - PETSCLOGHANDLERDEFAULT = "default" -  A `PetscLogHandler` that collects data for PETSc
1869:   default profiling log viewers (`PetscLogView()` and `PetscLogDump()`).  A log handler of this type is
1870:   created and started (`PetscLogHandlerStart()`) by `PetscLogDefaultBegin()`.

1872:   Options Database Keys:
1873: + -log_include_actions (true|false) - include a growing list of actions (event beginnings and endings, object creations and destructions) in `PetscLogDump()` (`PetscLogActions()`).
1874: - -log_include_objects (true|false) - include a growing list of object creations and destructions in `PetscLogDump()` (`PetscLogObjects()`).

1876:   Level: developer

1878: .seealso: [](ch_profiling), `PetscLogHandler`
1879: M*/

1881: PETSC_INTERN PetscErrorCode PetscLogHandlerCreate_Default(PetscLogHandler handler)
1882: {
1883:   PetscFunctionBegin;
1884:   PetscCall(PetscLogHandlerContextCreate_Default((PetscLogHandler_Default *)&handler->data));
1885:   handler->ops->destroy       = PetscLogHandlerDestroy_Default;
1886:   handler->ops->eventbegin    = PetscLogHandlerEventBegin_Default;
1887:   handler->ops->eventend      = PetscLogHandlerEventEnd_Default;
1888:   handler->ops->eventsync     = PetscLogHandlerEventSync_Default;
1889:   handler->ops->objectcreate  = PetscLogHandlerObjectCreate_Default;
1890:   handler->ops->objectdestroy = PetscLogHandlerObjectDestroy_Default;
1891:   handler->ops->stagepush     = PetscLogHandlerStagePush_Default;
1892:   handler->ops->stagepop      = PetscLogHandlerStagePop_Default;
1893:   handler->ops->view          = PetscLogHandlerView_Default;
1894:   PetscCall(PetscObjectComposeFunction((PetscObject)handler, "PetscLogHandlerGetEventPerfInfo_C", PetscLogHandlerGetEventPerfInfo_Default));
1895:   PetscCall(PetscObjectComposeFunction((PetscObject)handler, "PetscLogHandlerGetStagePerfInfo_C", PetscLogHandlerGetStagePerfInfo_Default));
1896:   PetscCall(PetscObjectComposeFunction((PetscObject)handler, "PetscLogHandlerSetLogActions_C", PetscLogHandlerSetLogActions_Default));
1897:   PetscCall(PetscObjectComposeFunction((PetscObject)handler, "PetscLogHandlerSetLogObjects_C", PetscLogHandlerSetLogObjects_Default));
1898:   PetscCall(PetscObjectComposeFunction((PetscObject)handler, "PetscLogHandlerLogObjectState_C", PetscLogHandlerLogObjectState_Default));
1899:   PetscCall(PetscObjectComposeFunction((PetscObject)handler, "PetscLogHandlerGetNumObjects_C", PetscLogHandlerGetNumObjects_Default));
1900:   PetscCall(PetscObjectComposeFunction((PetscObject)handler, "PetscLogHandlerEventDeactivatePush_C", PetscLogHandlerEventDeactivatePush_Default));
1901:   PetscCall(PetscObjectComposeFunction((PetscObject)handler, "PetscLogHandlerEventDeactivatePop_C", PetscLogHandlerEventDeactivatePop_Default));
1902:   PetscCall(PetscObjectComposeFunction((PetscObject)handler, "PetscLogHandlerEventsPause_C", PetscLogHandlerEventsPause_Default));
1903:   PetscCall(PetscObjectComposeFunction((PetscObject)handler, "PetscLogHandlerEventsResume_C", PetscLogHandlerEventsResume_Default));
1904:   PetscCall(PetscObjectComposeFunction((PetscObject)handler, "PetscLogHandlerDump_C", PetscLogHandlerDump_Default));
1905:   PetscCall(PetscObjectComposeFunction((PetscObject)handler, "PetscLogHandlerStageSetVisible_C", PetscLogHandlerStageSetVisible_Default));
1906:   PetscCall(PetscObjectComposeFunction((PetscObject)handler, "PetscLogHandlerStageGetVisible_C", PetscLogHandlerStageGetVisible_Default));
1907:   PetscFunctionReturn(PETSC_SUCCESS);
1908: }