Actual source code: stageLog.c

  1: #define PETSC_DLL

 3:  #include ../src/sys/plog/plog.h

  5: StageLog  _stageLog = 0;

  9: /*@C
 10:   StageInfoDestroy - This destroys a StageInfo object.

 12:   Not collective

 14:   Input Paramter:
 15: . stageInfo - The StageInfo

 17:   Level: beginner

 19: .keywords: log, stage, destroy
 20: .seealso: StageLogCreate()
 21: @*/
 22: PetscErrorCode  StageInfoDestroy(StageInfo *stageInfo)
 23: {

 27:   PetscFree(stageInfo->name);
 28:   EventPerfLogDestroy(stageInfo->eventLog);
 29:   ClassPerfLogDestroy(stageInfo->classLog);
 30:   return(0);
 31: }

 35: /*@
 36:   StageLogDestroy - This destroys a StageLog object.

 38:   Not collective

 40:   Input Paramter:
 41: . stageLog - The StageLog

 43:   Level: beginner

 45: .keywords: log, stage, destroy
 46: .seealso: StageLogCreate()
 47: @*/
 48: PetscErrorCode  StageLogDestroy(StageLog stageLog)
 49: {
 50:   int            stage;

 54:   if (!stageLog) return(0);
 55:   StackDestroy(stageLog->stack);
 56:   EventRegLogDestroy(stageLog->eventLog);
 57:   ClassRegLogDestroy(stageLog->classLog);
 58:   for(stage = 0; stage < stageLog->numStages; stage++) {
 59:     StageInfoDestroy(&stageLog->stageInfo[stage]);
 60:   }
 61:   PetscFree(stageLog->stageInfo);
 62:   PetscFree(stageLog);
 63:   return(0);
 64: }

 68: /*@
 69:   StageLogRegister - Registers a stage name for logging operations in an application code.

 71:   Not Collective

 73:   Input Parameter:
 74: + stageLog - The StageLog
 75: - sname    - the name to associate with that stage

 77:   Output Parameter:
 78: . stage    - The stage index

 80:   Level: intermediate

 82: .keywords: log, stage, register
 83: .seealso: StageLogPush(), StageLogPop(), StageLogCreate()
 84: @*/
 85: PetscErrorCode  StageLogRegister(StageLog stageLog, const char sname[], int *stage)
 86: {
 87:   StageInfo *stageInfo;
 88:   char      *str;
 89:   int        s, st;

 95:   for(st = 0; st < stageLog->numStages; ++st) {
 96:     PetscTruth same;

 98:     PetscStrcmp(stageLog->stageInfo[st].name, sname, &same);
 99:     if (same) {SETERRQ1(PETSC_ERR_ARG_WRONG, "Duplicate stage name given: %s", sname);}
100:   }
101:   s = stageLog->numStages++;
102:   if (stageLog->numStages > stageLog->maxStages) {
103:     PetscMalloc(stageLog->maxStages*2 * sizeof(StageInfo), &stageInfo);
104:     PetscMemcpy(stageInfo, stageLog->stageInfo, stageLog->maxStages * sizeof(StageInfo));
105:     PetscFree(stageLog->stageInfo);
106:     stageLog->stageInfo  = stageInfo;
107:     stageLog->maxStages *= 2;
108:   }
109:   /* Setup stage */
110:   PetscStrallocpy(sname, &str);
111:   stageLog->stageInfo[s].name                   = str;
112:   stageLog->stageInfo[s].used                   = PETSC_FALSE;
113:   stageLog->stageInfo[s].perfInfo.active        = PETSC_TRUE;
114:   stageLog->stageInfo[s].perfInfo.visible       = PETSC_TRUE;
115:   stageLog->stageInfo[s].perfInfo.count         = 0;
116:   stageLog->stageInfo[s].perfInfo.flops         = 0.0;
117:   stageLog->stageInfo[s].perfInfo.time          = 0.0;
118:   stageLog->stageInfo[s].perfInfo.numMessages   = 0.0;
119:   stageLog->stageInfo[s].perfInfo.messageLength = 0.0;
120:   stageLog->stageInfo[s].perfInfo.numReductions = 0.0;
121:   EventPerfLogCreate(&stageLog->stageInfo[s].eventLog);
122:   ClassPerfLogCreate(&stageLog->stageInfo[s].classLog);
123:   *stage = s;
124:   return(0);
125: }

129: /*@
130:   StageLogPush - This function pushes a stage on the stack.

132:   Not Collective

134:   Input Parameters:
135: + stageLog   - The StageLog
136: - stage - The stage to log

138:   Database Options:
139: . -log_summary - Activates logging

141:   Usage:
142:   If the option -log_sumary is used to run the program containing the 
143:   following code, then 2 sets of summary data will be printed during
144:   PetscFinalize().
145: .vb
146:       PetscInitialize(int *argc,char ***args,0,0);
147:       [stage 0 of code]   
148:       StageLogPush(stageLog,1);
149:       [stage 1 of code]
150:       StageLogPop(stageLog);
151:       PetscBarrier(...);
152:       [more stage 0 of code]   
153:       PetscFinalize();
154: .ve

156:   Notes:
157:   Use PetscLogStageRegister() to register a stage. All previous stages are
158:   accumulating time and flops, but events will only be logged in this stage.

160:   Level: intermediate

162: .keywords: log, push, stage
163: .seealso: StageLogPop(), StageLogGetCurrent(), StageLogRegister(), PetscLogGetStageLog()
164: @*/
165: PetscErrorCode  StageLogPush(StageLog stageLog, int stage)
166: {
167:   int             curStage = 0;
168:   PetscTruth      empty;

172:   if ((stage < 0) || (stage >= stageLog->numStages)) {
173:     SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE, "Invalid stage %d should be in [0,%d)", stage, stageLog->numStages);
174:   }

176:   /* Record flops/time of previous stage */
177:   StackEmpty(stageLog->stack, &empty);
178:   if (!empty) {
179:     StackTop(stageLog->stack, &curStage);
180:     if (stageLog->stageInfo[curStage].perfInfo.active) {
181:       PetscTimeAdd(stageLog->stageInfo[curStage].perfInfo.time);
182:       stageLog->stageInfo[curStage].perfInfo.flops         += _TotalFlops;
183:       stageLog->stageInfo[curStage].perfInfo.numMessages   += irecv_ct  + isend_ct  + recv_ct  + send_ct;
184:       stageLog->stageInfo[curStage].perfInfo.messageLength += irecv_len + isend_len + recv_len + send_len;
185:       stageLog->stageInfo[curStage].perfInfo.numReductions += allreduce_ct;
186:     }
187:   }
188:   /* Activate the stage */
189:   StackPush(stageLog->stack, stage);
190:   stageLog->stageInfo[stage].used = PETSC_TRUE;
191:   stageLog->stageInfo[stage].perfInfo.count++;
192:   stageLog->curStage = stage;
193:   /* Subtract current quantities so that we obtain the difference when we pop */
194:   if (stageLog->stageInfo[stage].perfInfo.active) {
195:     PetscTimeSubtract(stageLog->stageInfo[stage].perfInfo.time);
196:     stageLog->stageInfo[stage].perfInfo.flops         -= _TotalFlops;
197:     stageLog->stageInfo[stage].perfInfo.numMessages   -= irecv_ct  + isend_ct  + recv_ct  + send_ct;
198:     stageLog->stageInfo[stage].perfInfo.messageLength -= irecv_len + isend_len + recv_len + send_len;
199:     stageLog->stageInfo[stage].perfInfo.numReductions -= allreduce_ct;
200:   }
201:   return(0);
202: }

206: /*@
207:   StageLogPop - This function pops a stage from the stack.

209:   Not Collective

211:   Input Parameter:
212: . stageLog - The StageLog

214:   Usage:
215:   If the option -log_sumary is used to run the program containing the 
216:   following code, then 2 sets of summary data will be printed during
217:   PetscFinalize().
218: .vb
219:       PetscInitialize(int *argc,char ***args,0,0);
220:       [stage 0 of code]   
221:       StageLogPush(stageLog,1);
222:       [stage 1 of code]
223:       StageLogPop(stageLog);
224:       PetscBarrier(...);
225:       [more stage 0 of code]   
226:       PetscFinalize();
227: .ve

229:   Notes:
230:   Use StageLogRegister() to register a stage.

232:   Level: intermediate

234: .keywords: log, pop, stage
235: .seealso: StageLogPush(), StageLogGetCurrent(), StageLogRegister(), PetscLogGetStageLog()
236: @*/
237: PetscErrorCode  StageLogPop(StageLog stageLog)
238: {
239:   int             curStage;
240:   PetscTruth      empty;

244:   /* Record flops/time of current stage */
245:   StackPop(stageLog->stack, &curStage);
246:   if (stageLog->stageInfo[curStage].perfInfo.active) {
247:     PetscTimeAdd(stageLog->stageInfo[curStage].perfInfo.time);
248:     stageLog->stageInfo[curStage].perfInfo.flops         += _TotalFlops;
249:     stageLog->stageInfo[curStage].perfInfo.numMessages   += irecv_ct  + isend_ct  + recv_ct  + send_ct;
250:     stageLog->stageInfo[curStage].perfInfo.messageLength += irecv_len + isend_len + recv_len + send_len;
251:     stageLog->stageInfo[curStage].perfInfo.numReductions += allreduce_ct;
252:   }
253:   StackEmpty(stageLog->stack, &empty);
254:   if (!empty) {
255:     /* Subtract current quantities so that we obtain the difference when we pop */
256:     StackTop(stageLog->stack, &curStage);
257:     if (stageLog->stageInfo[curStage].perfInfo.active) {
258:       PetscTimeSubtract(stageLog->stageInfo[curStage].perfInfo.time);
259:       stageLog->stageInfo[curStage].perfInfo.flops         -= _TotalFlops;
260:       stageLog->stageInfo[curStage].perfInfo.numMessages   -= irecv_ct  + isend_ct  + recv_ct  + send_ct;
261:       stageLog->stageInfo[curStage].perfInfo.messageLength -= irecv_len + isend_len + recv_len + send_len;
262:       stageLog->stageInfo[curStage].perfInfo.numReductions -= allreduce_ct;
263:     }
264:     stageLog->curStage                           = curStage;
265:   } else {
266:     stageLog->curStage                           = -1;
267:   }
268:   return(0);
269: }

273: /*@
274:   StageLogGetCurrent - This function returns the stage from the top of the stack.

276:   Not Collective

278:   Input Parameter:
279: . stageLog - The StageLog

281:   Output Parameter:
282: . stage    - The current stage

284:   Notes:
285:   If no stage is currently active, stage is set to -1.

287:   Level: intermediate

289: .keywords: log, stage
290: .seealso: StageLogPush(), StageLogPop(), PetscLogGetStageLog()
291: @*/
292: PetscErrorCode  StageLogGetCurrent(StageLog stageLog, int *stage)
293: {
294:   PetscTruth     empty;

298:   StackEmpty(stageLog->stack, &empty);
299:   if (empty) {
300:     *stage = -1;
301:   } else {
302:     StackTop(stageLog->stack, stage);
303:   }
304: #ifdef PETSC_USE_DEBUG
305:   if (*stage != stageLog->curStage) {
306:     SETERRQ2(PETSC_ERR_PLIB, "Inconsistency in stage log: stage %d should be %d", *stage, stageLog->curStage);
307:   }
308: #endif
309:   return(0);
310: }

314: /*@C
315:   StageLogGetClassRegLog - This function returns the ClassRegLog for the given stage.

317:   Not Collective

319:   Input Parameters:
320: . stageLog - The StageLog

322:   Output Parameter:
323: . classLog - The ClassRegLog

325:   Level: intermediate

327: .keywords: log, stage
328: .seealso: StageLogPush(), StageLogPop(), PetscLogGetStageLog()
329: @*/
330: PetscErrorCode  StageLogGetClassRegLog(StageLog stageLog, ClassRegLog *classLog)
331: {
334:   *classLog = stageLog->classLog;
335:   return(0);
336: }

340: /*@C
341:   StageLogGetEventRegLog - This function returns the EventRegLog.

343:   Not Collective

345:   Input Parameters:
346: . stageLog - The StageLog

348:   Output Parameter:
349: . eventLog - The EventRegLog

351:   Level: intermediate

353: .keywords: log, stage
354: .seealso: StageLogPush(), StageLogPop(), PetscLogGetStageLog()
355: @*/
356: PetscErrorCode  StageLogGetEventRegLog(StageLog stageLog, EventRegLog *eventLog)
357: {
360:   *eventLog = stageLog->eventLog;
361:   return(0);
362: }

366: /*@C
367:   StageLogGetClassPerfLog - This function returns the ClassPerfLog for the given stage.

369:   Not Collective

371:   Input Parameters:
372: + stageLog - The StageLog
373: - stage    - The stage

375:   Output Parameter:
376: . classLog - The ClassPerfLog

378:   Level: intermediate

380: .keywords: log, stage
381: .seealso: StageLogPush(), StageLogPop(), PetscLogGetStageLog()
382: @*/
383: PetscErrorCode  StageLogGetClassPerfLog(StageLog stageLog, int stage, ClassPerfLog *classLog)
384: {
387:   if ((stage < 0) || (stage >= stageLog->numStages)) {
388:     SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE, "Invalid stage %d should be in [0,%d)", stage, stageLog->numStages);
389:   }
390:   *classLog = stageLog->stageInfo[stage].classLog;
391:   return(0);
392: }

396: /*@C
397:   StageLogGetEventPerfLog - This function returns the EventPerfLog for the given stage.

399:   Not Collective

401:   Input Parameters:
402: + stageLog - The StageLog
403: - stage    - The stage

405:   Output Parameter:
406: . eventLog - The EventPerfLog

408:   Level: intermediate

410: .keywords: log, stage
411: .seealso: StageLogPush(), StageLogPop(), PetscLogGetStageLog()
412: @*/
413: PetscErrorCode  StageLogGetEventPerfLog(StageLog stageLog, int stage, EventPerfLog *eventLog)
414: {
417:   if ((stage < 0) || (stage >= stageLog->numStages)) {
418:     SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE, "Invalid stage %d should be in [0,%d)", stage, stageLog->numStages);
419:   }
420:   *eventLog = stageLog->stageInfo[stage].eventLog;
421:   return(0);
422: }

426: /*@
427:   StageLogSetActive - This function determines whether events will be logged during this state.

429:   Not Collective

431:   Input Parameters:
432: + stageLog - The StageLog
433: . stage    - The stage to log
434: - isActive - The activity flag, PETSC_TRUE for logging, otherwise PETSC_FALSE (default is PETSC_TRUE)

436:   Level: intermediate

438: .keywords: log, active, stage
439: .seealso: StageLogGetActive(), StageLogGetCurrent(), StageLogRegister(), PetscLogGetStageLog()
440: @*/
441: PetscErrorCode  StageLogSetActive(StageLog stageLog, int stage, PetscTruth isActive)
442: {
444:   if ((stage < 0) || (stage >= stageLog->numStages)) {
445:     SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE, "Invalid stage %d should be in [0,%d)", stage, stageLog->numStages);
446:   }
447:   stageLog->stageInfo[stage].perfInfo.active = isActive;
448:   return(0);
449: }

453: /*@
454:   StageLogGetActive - This function returns whether events will be logged suring this stage.

456:   Not Collective

458:   Input Parameters:
459: + stageLog - The StageLog
460: - stage    - The stage to log

462:   Output Parameter:
463: . isActive - The activity flag, PETSC_TRUE for logging, otherwise PETSC_FALSE (default is PETSC_TRUE)

465:   Level: intermediate

467: .keywords: log, visible, stage
468: .seealso: StageLogSetActive(), StageLogGetCurrent(), StageLogRegister(), PetscLogGetStageLog()
469: @*/
470: PetscErrorCode  StageLogGetActive(StageLog stageLog, int stage, PetscTruth *isActive)
471: {
473:   if ((stage < 0) || (stage >= stageLog->numStages)) {
474:     SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE, "Invalid stage %d should be in [0,%d)", stage, stageLog->numStages);
475:   }
477:   *isActive = stageLog->stageInfo[stage].perfInfo.active;
478:   return(0);
479: }

483: /*@
484:   StageLogSetVisible - This function determines whether a stage is printed during PetscLogPrintSummary()

486:   Not Collective

488:   Input Parameters:
489: + stageLog  - The StageLog
490: . stage     - The stage to log
491: - isVisible - The visibility flag, PETSC_TRUE for printing, otherwise PETSC_FALSE (default is PETSC_TRUE)

493:   Database Options:
494: . -log_summary - Activates log summary

496:   Level: intermediate

498: .keywords: log, visible, stage
499: .seealso: StageLogGetVisible(), StageLogGetCurrent(), StageLogRegister(), PetscLogGetStageLog()
500: @*/
501: PetscErrorCode  StageLogSetVisible(StageLog stageLog, int stage, PetscTruth isVisible)
502: {
504:   if ((stage < 0) || (stage >= stageLog->numStages)) {
505:     SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE, "Invalid stage %d should be in [0,%d)", stage, stageLog->numStages);
506:   }
507:   stageLog->stageInfo[stage].perfInfo.visible = isVisible;
508:   return(0);
509: }

513: /*@
514:   StageLogGetVisible - This function returns whether a stage is printed during PetscLogPrintSummary()

516:   Not Collective

518:   Input Parameters:
519: + stageLog  - The StageLog
520: - stage     - The stage to log

522:   Output Parameter:
523: . isVisible - The visibility flag, PETSC_TRUE for printing, otherwise PETSC_FALSE (default is PETSC_TRUE)

525:   Database Options:
526: . -log_summary - Activates log summary

528:   Level: intermediate

530: .keywords: log, visible, stage
531: .seealso: StageLogSetVisible(), StageLogGetCurrent(), StageLogRegister(), PetscLogGetStageLog()
532: @*/
533: PetscErrorCode  StageLogGetVisible(StageLog stageLog, int stage, PetscTruth *isVisible)
534: {
536:   if ((stage < 0) || (stage >= stageLog->numStages)) {
537:     SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE, "Invalid stage %d should be in [0,%d)", stage, stageLog->numStages);
538:   }
540:   *isVisible = stageLog->stageInfo[stage].perfInfo.visible;
541:   return(0);
542: }

546: /*@
547:   StageLogGetStage - This function returns the stage id given the stage name.

549:   Not Collective

551:   Input Parameters:
552: + stageLog - The StageLog
553: - name     - The stage name

555:   Output Parameter:
556: . stage    - The stage id

558:   Level: intermediate

560: .keywords: log, stage
561: .seealso: StageLogGetCurrent(), StageLogRegister(), PetscLogGetStageLog()
562: @*/
563: PetscErrorCode  StageLogGetStage(StageLog stageLog, const char name[], int *stage)
564: {
565:   PetscTruth match;
566:   int        s;

572:   *stage = -1;
573:   for(s = 0; s < stageLog->numStages; s++) {
574:     PetscStrcasecmp(stageLog->stageInfo[s].name, name, &match);
575:     if (match) break;
576:   }
577:   if (s == stageLog->numStages) SETERRQ1(PETSC_ERR_ARG_WRONG, "No stage named %s", name);
578:   *stage = s;
579:   return(0);
580: }

584: /*@
585:   StageLogCreate - This creates a StageLog object.

587:   Not collective

589:   Input Parameter:
590: . stageLog - The StageLog

592:   Level: beginner

594: .keywords: log, stage, create
595: .seealso: StageLogCreate()
596: @*/
597: PetscErrorCode  StageLogCreate(StageLog *stageLog)
598: {
599:   StageLog       l;

603:   PetscNew(struct _n_StageLog, &l);
604:   l->numStages = 0;
605:   l->maxStages = 10;
606:   l->curStage  = -1;
607:   StackCreate(&l->stack);
608:   PetscMalloc(l->maxStages * sizeof(StageInfo), &l->stageInfo);
609:   EventRegLogCreate(&l->eventLog);
610:   ClassRegLogCreate(&l->classLog);
611:   *stageLog = l;
612:   return(0);
613: }