Actual source code: stageLog.c

  1: #define PETSC_DLL

 3:  #include petsc.h
 4:  #include petsctime.h
 5:  #include src/sys/plog/plog.h

  7: StageLog  _stageLog = 0;

 11: /*@C
 12:   StageInfoDestroy - This destroys a StageInfo object.

 14:   Not collective

 16:   Input Paramter:
 17: . stageInfo - The StageInfo

 19:   Level: beginner

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

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

 37: /*@
 38:   StageLogDestroy - This destroys a StageLog object.

 40:   Not collective

 42:   Input Paramter:
 43: . stageLog - The StageLog

 45:   Level: beginner

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

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

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

 73:   Not Collective

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

 79:   Output Parameter:
 80: . stage    - The stage index

 82:   Level: intermediate

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

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

125: /*@
126:   StageLogPush - This function pushes a stage on the stack.

128:   Not Collective

130:   Input Parameters:
131: + stageLog   - The StageLog
132: - stage - The stage to log

134:   Database Options:
135: . -log_summary - Activates logging

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

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

156:   Level: intermediate

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

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

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

202: /*@
203:   StageLogPop - This function pops a stage from the stack.

205:   Not Collective

207:   Input Parameter:
208: . stageLog - The StageLog

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

225:   Notes:
226:   Use StageLogRegister() to register a stage.

228:   Level: intermediate

230: .keywords: log, pop, stage
231: .seealso: StageLogPush(), StageLogGetCurrent(), StageLogRegister(), PetscLogGetStageLog()
232: @*/
233: PetscErrorCode  StageLogPop(StageLog stageLog)
234: {
235:   int        curStage;
236:   PetscTruth empty;

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

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

272:   Not Collective

274:   Input Parameter:
275: . stageLog - The StageLog

277:   Output Parameter:
278: . stage    - The current stage

280:   Notes:
281:   If no stage is currently active, stage is set to -1.

283:   Level: intermediate

285: .keywords: log, stage
286: .seealso: StageLogPush(), StageLogPop(), PetscLogGetStageLog()
287: @*/
288: PetscErrorCode  StageLogGetCurrent(StageLog stageLog, int *stage)
289: {
290:   PetscTruth empty;

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

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

313:   Not Collective

315:   Input Parameters:
316: . stageLog - The StageLog

318:   Output Parameter:
319: . classLog - The ClassRegLog

321:   Level: intermediate

323: .keywords: log, stage
324: .seealso: StageLogPush(), StageLogPop(), PetscLogGetStageLog()
325: @*/
326: PetscErrorCode  StageLogGetClassRegLog(StageLog stageLog, ClassRegLog *classLog)
327: {
330:   *classLog = stageLog->classLog;
331:   return(0);
332: }

336: /*@C
337:   StageLogGetEventRegLog - This function returns the EventRegLog.

339:   Not Collective

341:   Input Parameters:
342: . stageLog - The StageLog

344:   Output Parameter:
345: . eventLog - The EventRegLog

347:   Level: intermediate

349: .keywords: log, stage
350: .seealso: StageLogPush(), StageLogPop(), PetscLogGetStageLog()
351: @*/
352: PetscErrorCode  StageLogGetEventRegLog(StageLog stageLog, EventRegLog *eventLog)
353: {
356:   *eventLog = stageLog->eventLog;
357:   return(0);
358: }

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

365:   Not Collective

367:   Input Parameters:
368: + stageLog - The StageLog
369: - stage    - The stage

371:   Output Parameter:
372: . classLog - The ClassPerfLog

374:   Level: intermediate

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

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

395:   Not Collective

397:   Input Parameters:
398: + stageLog - The StageLog
399: - stage    - The stage

401:   Output Parameter:
402: . eventLog - The EventPerfLog

404:   Level: intermediate

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

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

425:   Not Collective

427:   Input Parameters:
428: + stageLog - The StageLog
429: . stage    - The stage to log
430: - isActive - The activity flag, PETSC_TRUE for logging, otherwise PETSC_FALSE (default is PETSC_TRUE)

432:   Level: intermediate

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

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

452:   Not Collective

454:   Input Parameters:
455: + stageLog - The StageLog
456: - stage    - The stage to log

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

461:   Level: intermediate

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

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

482:   Not Collective

484:   Input Parameters:
485: + stageLog  - The StageLog
486: . stage     - The stage to log
487: - isVisible - The visibility flag, PETSC_TRUE for printing, otherwise PETSC_FALSE (default is PETSC_TRUE)

489:   Database Options:
490: . -log_summary - Activates log summary

492:   Level: intermediate

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

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

512:   Not Collective

514:   Input Parameters:
515: + stageLog  - The StageLog
516: - stage     - The stage to log

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

521:   Database Options:
522: . -log_summary - Activates log summary

524:   Level: intermediate

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

542: /*@
543:   StageLogGetStage - This function the stage id given the stage name.

545:   Not Collective

547:   Input Parameters:
548: + stageLog - The StageLog
549: - name     - The stage name

551:   Output Parameter:
552: . stage    - The stage id

554:   Level: intermediate

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

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

580: /*@
581:   StageLogCreate - This creates a StageLog object.

583:   Not collective

585:   Input Parameter:
586: . stageLog - The StageLog

588:   Level: beginner

590: .keywords: log, stage, create
591: .seealso: StageLogCreate()
592: @*/
593: PetscErrorCode  StageLogCreate(StageLog *stageLog)
594: {
595:   StageLog l;

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