Actual source code: stageLog.c

      1: #define PETSC_DLL

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

      7: StageLog PETSC_DLLEXPORT _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 PETSC_DLLEXPORT 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 PETSC_DLLEXPORT StageLogDestroy(StageLog stageLog)
     51: {
     52:   int stage;

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

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

     72:   Not Collective

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

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

     81:   Level: intermediate

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

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

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

    127:   Not Collective

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

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

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

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

    155:   Level: intermediate

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

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

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

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

    204:   Not Collective

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

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

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

    227:   Level: intermediate

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

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

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

    271:   Not Collective

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

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

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

    282:   Level: intermediate

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

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

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

    312:   Not Collective

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

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

    320:   Level: intermediate

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

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

    338:   Not Collective

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

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

    346:   Level: intermediate

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

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

    364:   Not Collective

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

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

    373:   Level: intermediate

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

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

    394:   Not Collective

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

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

    403:   Level: intermediate

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

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

    424:   Not Collective

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

    431:   Level: intermediate

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

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

    451:   Not Collective

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

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

    460:   Level: intermediate

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

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

    481:   Not Collective

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

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

    491:   Level: intermediate

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

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

    511:   Not Collective

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

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

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

    523:   Level: intermediate

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

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

    544:   Not Collective

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

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

    553:   Level: intermediate

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

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

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

    582:   Not collective

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

    587:   Level: beginner

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

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