Actual source code: petsclog.h
petsc-3.12.1 2019-10-22
Report Typos and Errors

      1: /*
      2:     Defines profile/logging in PETSc.
      3: */

      5: #if !defined(PETSCLOG_H)
      6: #define PETSCLOG_H
      7:  #include <petscsys.h>
      8:  #include <petsctime.h>

     10: /* General logging of information; different from event logging */
     11: PETSC_EXTERN PetscErrorCode PetscInfo_Private(const char[],void*,const char[],...);
     12: #if defined(PETSC_USE_INFO)
     13: #define PetscInfo(A,S)                       PetscInfo_Private(PETSC_FUNCTION_NAME,A,S)
     14: #define PetscInfo1(A,S,a1)                   PetscInfo_Private(PETSC_FUNCTION_NAME,A,S,a1)
     15: #define PetscInfo2(A,S,a1,a2)                PetscInfo_Private(PETSC_FUNCTION_NAME,A,S,a1,a2)
     16: #define PetscInfo3(A,S,a1,a2,a3)             PetscInfo_Private(PETSC_FUNCTION_NAME,A,S,a1,a2,a3)
     17: #define PetscInfo4(A,S,a1,a2,a3,a4)          PetscInfo_Private(PETSC_FUNCTION_NAME,A,S,a1,a2,a3,a4)
     18: #define PetscInfo5(A,S,a1,a2,a3,a4,a5)       PetscInfo_Private(PETSC_FUNCTION_NAME,A,S,a1,a2,a3,a4,a5)
     19: #define PetscInfo6(A,S,a1,a2,a3,a4,a5,a6)    PetscInfo_Private(PETSC_FUNCTION_NAME,A,S,a1,a2,a3,a4,a5,a6)
     20: #define PetscInfo7(A,S,a1,a2,a3,a4,a5,a6,a7) PetscInfo_Private(PETSC_FUNCTION_NAME,A,S,a1,a2,a3,a4,a5,a6,a7)
     21: #else
     22: #define PetscInfo(A,S)                       0
     23: #define PetscInfo1(A,S,a1)                   0
     24: #define PetscInfo2(A,S,a1,a2)                0
     25: #define PetscInfo3(A,S,a1,a2,a3)             0
     26: #define PetscInfo4(A,S,a1,a2,a3,a4)          0
     27: #define PetscInfo5(A,S,a1,a2,a3,a4,a5)       0
     28: #define PetscInfo6(A,S,a1,a2,a3,a4,a5,a6)    0
     29: #define PetscInfo7(A,S,a1,a2,a3,a4,a5,a6,a7) 0
     30: #endif
     31: PETSC_EXTERN PetscErrorCode PetscInfoDeactivateClass(PetscClassId);
     32: PETSC_EXTERN PetscErrorCode PetscInfoActivateClass(PetscClassId);
     33: PETSC_EXTERN PetscBool PetscLogPrintInfo;  /* if true, indicates PetscInfo() is turned on */

     35: /*MC
     36:     PetscLogEvent - id used to identify PETSc or user events which timed portions (blocks of executable)
     37:      code.

     39:     Level: intermediate

     41: .seealso: PetscLogEventRegister(), PetscLogEventBegin(), PetscLogEventEnd(), PetscLogStage
     42: M*/
     43: typedef int PetscLogEvent;

     45: /*MC
     46:     PetscLogStage - id used to identify user stages (phases, sections) of runs - for logging

     48:     Level: intermediate

     50: .seealso: PetscLogStageRegister(), PetscLogStagePush(), PetscLogStagePop(), PetscLogEvent
     51: M*/
     52: typedef int PetscLogStage;

     54: #define PETSC_EVENT  1311311
     55: PETSC_EXTERN PetscLogEvent PETSC_LARGEST_EVENT;

     57: /* Global flop counter */
     58: PETSC_EXTERN PetscLogDouble petsc_TotalFlops;
     59: PETSC_EXTERN PetscLogDouble petsc_tmp_flops;

     61: /* Global GPU counters */
     62: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
     63: PETSC_EXTERN PetscLogDouble petsc_ctog_ct;
     64: PETSC_EXTERN PetscLogDouble petsc_gtoc_ct;
     65: PETSC_EXTERN PetscLogDouble petsc_ctog_sz;
     66: PETSC_EXTERN PetscLogDouble petsc_gtoc_sz;
     67: PETSC_EXTERN PetscLogDouble petsc_gflops;
     68: PETSC_EXTERN PetscLogDouble petsc_gtime;
     69: #endif

     71: /* We must make the following structures available to access the event
     72:      activation flags in the PetscLogEventBegin/End() macros. These are not part of the PETSc public
     73:      API and are not intended to be used by other parts of PETSc or by users.

     75:      The code that manipulates these structures is in src/sys/logging/utils.
     76: */
     77: typedef struct _n_PetscIntStack *PetscIntStack;

     79: /* -----------------------------------------------------------------------------------------------------*/
     80: /*
     81:     PetscClassRegInfo, PetscClassPerfInfo - Each class has two data structures associated with it. The first has
     82:        static information about it, the second collects statistics on how many objects of the class are created,
     83:        how much memory they use, etc.

     85:     PetscClassRegLog, PetscClassPerfLog - arrays of the PetscClassRegInfo and PetscClassPerfInfo for all classes.
     86: */
     87: typedef struct  {
     88:   char           *name;   /* The class name */
     89:   PetscClassId   classid; /* The integer identifying this class */
     90: } PetscClassRegInfo;

     92: typedef struct {
     93:   PetscClassId   id;           /* The integer identifying this class */
     94:   int            creations;    /* The number of objects of this class created */
     95:   int            destructions; /* The number of objects of this class destroyed */
     96:   PetscLogDouble mem;          /* The total memory allocated by objects of this class */
     97:   PetscLogDouble descMem;      /* The total memory allocated by descendents of these objects */
     98: } PetscClassPerfInfo;

    100: typedef struct _n_PetscClassRegLog *PetscClassRegLog;
    101: struct _n_PetscClassRegLog {
    102:   int               numClasses; /* The number of classes registered */
    103:   int               maxClasses; /* The maximum number of classes */
    104:   PetscClassRegInfo *classInfo; /* The structure for class information (classids are monotonicly increasing) */
    105: };

    107: typedef struct _n_PetscClassPerfLog *PetscClassPerfLog;
    108: struct _n_PetscClassPerfLog {
    109:   int                numClasses; /* The number of logging classes */
    110:   int                maxClasses; /* The maximum number of classes */
    111:   PetscClassPerfInfo *classInfo; /* The structure for class information (classids are monotonicly increasing) */
    112: };
    113: /* -----------------------------------------------------------------------------------------------------*/
    114: /*
    115:     PetscEventRegInfo, PetscEventPerfInfo - Each event has two data structures associated with it. The first has
    116:        static information about it, the second collects statistics on how many times the event is used, how
    117:        much time it takes, etc.

    119:     PetscEventRegLog, PetscEventPerfLog - an array of all PetscEventRegInfo and PetscEventPerfInfo for all events. There is one
    120:       of these for each stage.

    122: */
    123: typedef struct {
    124:   char         *name;         /* The name of this event */
    125:   PetscClassId classid;       /* The class the event is associated with */
    126:   PetscBool    collective;    /* Flag this event as collective */
    127: #if defined (PETSC_HAVE_MPE)
    128:   int          mpe_id_begin;  /* MPE IDs that define the event */
    129:   int          mpe_id_end;
    130: #endif
    131: } PetscEventRegInfo;

    133: typedef struct {
    134:   int            id;            /* The integer identifying this event */
    135:   PetscBool      active;        /* The flag to activate logging */
    136:   PetscBool      visible;       /* The flag to print info in summary */
    137:   int            depth;         /* The nesting depth of the event call */
    138:   int            count;         /* The number of times this event was executed */
    139:   PetscLogDouble flops, flops2, flopsTmp; /* The flops and flops^2 used in this event */
    140:   PetscLogDouble time, time2, timeTmp;    /* The time and time^2 taken for this event */
    141:   PetscLogDouble syncTime;                /* The synchronization barrier time */
    142:   PetscLogDouble dof[8];        /* The number of degrees of freedom associated with this event */
    143:   PetscLogDouble errors[8];     /* The errors (user-defined) associated with this event */
    144:   PetscLogDouble numMessages;   /* The number of messages in this event */
    145:   PetscLogDouble messageLength; /* The total message lengths in this event */
    146:   PetscLogDouble numReductions; /* The number of reductions in this event */
    147:   PetscLogDouble memIncrease;   /* How much the resident memory has increased in this event */
    148:   PetscLogDouble mallocIncrease;/* How much the maximum malloced space has increased in this event */
    149:   PetscLogDouble mallocSpace;   /* How much the space was malloced and kept during this event */
    150:   PetscLogDouble mallocIncreaseEvent;  /* Maximum of the high water mark with in event minus memory available at the end of the event */
    151:   #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
    152:   PetscLogDouble CpuToGpuCount; /* The total number of CPU to GPU copies */
    153:   PetscLogDouble GpuToCpuCount; /* The total number of GPU to CPU copies */
    154:   PetscLogDouble CpuToGpuSize;  /* The total size of CPU to GPU copies */
    155:   PetscLogDouble GpuToCpuSize;  /* The total size of GPU to CPU copies */
    156:   PetscLogDouble GpuFlops;      /* The flops done on a GPU in this event */
    157:   PetscLogDouble GpuTime;       /* The time spent on a GPU in this event */
    158:   #endif
    159: } PetscEventPerfInfo;

    161: typedef struct _n_PetscEventRegLog *PetscEventRegLog;
    162: struct _n_PetscEventRegLog {
    163:   int               numEvents;  /* The number of registered events */
    164:   int               maxEvents;  /* The maximum number of events */
    165:   PetscEventRegInfo *eventInfo; /* The registration information for each event */
    166: };

    168: typedef struct _n_PetscEventPerfLog *PetscEventPerfLog;
    169: struct _n_PetscEventPerfLog {
    170:   int                numEvents;  /* The number of logging events */
    171:   int                maxEvents;  /* The maximum number of events */
    172:   PetscEventPerfInfo *eventInfo; /* The performance information for each event */
    173: };
    174: /* ------------------------------------------------------------------------------------------------------------*/
    175: /*
    176:    PetscStageInfo - Contains all the information about a particular stage.

    178:    PetscStageLog - An array of PetscStageInfo for each registered stage. There is a single one of these in the code.
    179: */
    180: typedef struct _PetscStageInfo {
    181:   char               *name;     /* The stage name */
    182:   PetscBool          used;      /* The stage was pushed on this processor */
    183:   PetscEventPerfInfo perfInfo;  /* The stage performance information */
    184:   PetscEventPerfLog  eventLog;  /* The event information for this stage */
    185:   PetscClassPerfLog  classLog;  /* The class information for this stage */
    186: } PetscStageInfo;

    188: typedef struct _n_PetscStageLog *PetscStageLog;
    189: struct _n_PetscStageLog {
    190:   int              numStages;   /* The number of registered stages */
    191:   int              maxStages;   /* The maximum number of stages */
    192:   PetscIntStack    stack;       /* The stack for active stages */
    193:   int              curStage;    /* The current stage (only used in macros so we don't call PetscIntStackTop) */
    194:   PetscStageInfo   *stageInfo;  /* The information for each stage */
    195:   PetscEventRegLog eventLog;    /* The registered events */
    196:   PetscClassRegLog classLog;    /* The registered classes */
    197: };
    198: /* -----------------------------------------------------------------------------------------------------*/

    200: PETSC_EXTERN PetscErrorCode PetscLogObjectParent(PetscObject,PetscObject);
    201: PETSC_EXTERN PetscErrorCode PetscLogObjectMemory(PetscObject,PetscLogDouble);

    203: #if defined(PETSC_USE_LOG)  /* --- Logging is turned on --------------------------------*/
    204: PETSC_EXTERN PetscStageLog petsc_stageLog;
    205: PETSC_EXTERN PetscErrorCode PetscLogGetStageLog(PetscStageLog*);
    206: PETSC_EXTERN PetscErrorCode PetscStageLogGetCurrent(PetscStageLog,int*);
    207: PETSC_EXTERN PetscErrorCode PetscStageLogGetEventPerfLog(PetscStageLog,int,PetscEventPerfLog*);

    209: /*
    210:    Flop counting:  We count each arithmetic operation (e.g., addition, multiplication) separately.

    212:    For the complex numbers version, note that
    213:        1 complex addition = 2 flops
    214:        1 complex multiplication = 6 flops,
    215:    where we define 1 flop as that for a double precision scalar.  We roughly approximate
    216:    flop counting for complex numbers by multiplying the total flops by 4; this corresponds
    217:    to the assumption that we're counting mostly additions and multiplications -- and
    218:    roughly the same number of each.  More accurate counting could be done by distinguishing
    219:    among the various arithmetic operations.
    220:  */

    222: #if defined(PETSC_USE_COMPLEX)
    223: #define PETSC_FLOPS_PER_OP 4.0
    224: #else
    225: #define PETSC_FLOPS_PER_OP 1.0
    226: #endif

    228: PETSC_STATIC_INLINE PetscErrorCode PetscLogFlops(PetscLogDouble n)
    229: {
    231: #if defined(PETSC_USE_DEBUG)
    232:   if (n < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Cannot log negative flops");
    233: #endif
    234:   petsc_TotalFlops += PETSC_FLOPS_PER_OP*n;
    235:   return(0);
    236: }

    238: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
    239: PETSC_STATIC_INLINE PetscErrorCode PetscLogCpuToGpu(PetscLogDouble size){
    241:   petsc_ctog_ct += 1;
    242:   petsc_ctog_sz += size;
    243:   return(0);
    244: }
    245: PETSC_STATIC_INLINE PetscErrorCode PetscLogGpuToCpu(PetscLogDouble size){
    247:   petsc_gtoc_ct += 1;
    248:   petsc_gtoc_sz += size;
    249:   return(0);
    250: }
    251: PETSC_STATIC_INLINE PetscErrorCode PetscLogGpuFlops(PetscLogDouble n){
    253: #if defined(PETSC_USE_DEBUG)
    254:   if (n < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Cannot log negative flops");
    255: #endif
    256:   petsc_TotalFlops += PETSC_FLOPS_PER_OP*n;
    257:   petsc_gflops += PETSC_FLOPS_PER_OP*n;
    258:   return(0);
    259: }
    260: PETSC_STATIC_INLINE PetscErrorCode PetscLogGpuTimeBegin(){
    263:   PetscTimeSubtract(&petsc_gtime);
    264:   return(0);
    265: }
    266: PETSC_STATIC_INLINE PetscErrorCode PetscLogGpuTimeEnd(){
    269:   PetscTimeAdd(&petsc_gtime);
    270:   return(0);
    271: }
    272: #endif

    274: PETSC_EXTERN PetscErrorCode PetscGetFlops(PetscLogDouble *);

    276: #if defined (PETSC_HAVE_MPE)
    277: PETSC_EXTERN PetscErrorCode PetscLogMPEBegin(void);
    278: PETSC_EXTERN PetscErrorCode PetscLogMPEDump(const char[]);
    279: #endif

    281: PETSC_EXTERN PetscErrorCode (*PetscLogPLB)(PetscLogEvent,int,PetscObject,PetscObject,PetscObject,PetscObject);
    282: PETSC_EXTERN PetscErrorCode (*PetscLogPLE)(PetscLogEvent,int,PetscObject,PetscObject,PetscObject,PetscObject);
    283: PETSC_EXTERN PetscErrorCode (*PetscLogPHC)(PetscObject);
    284: PETSC_EXTERN PetscErrorCode (*PetscLogPHD)(PetscObject);

    286: #define PetscLogObjectParents(p,n,d)  0;do{int _i; for (_i=0; _i<(n); _i++) {PetscLogObjectParent((PetscObject)(p),(PetscObject)(d)[_i]);}}while(0)
    287: #define PetscLogObjectCreate(h)      ((PetscLogPHC) ? (*PetscLogPHC)((PetscObject)(h)) : 0)
    288: #define PetscLogObjectDestroy(h)     ((PetscLogPHD) ? (*PetscLogPHD)((PetscObject)(h)) : 0)
    289: PETSC_EXTERN PetscErrorCode PetscLogObjectState(PetscObject, const char[], ...);

    291: /* Initialization functions */
    292: PETSC_EXTERN PetscErrorCode PetscLogDefaultBegin(void);
    293: PETSC_EXTERN PetscErrorCode PetscLogAllBegin(void);
    294: PETSC_EXTERN PetscErrorCode PetscLogNestedBegin(void);
    295: PETSC_EXTERN PetscErrorCode PetscLogTraceBegin(FILE *);
    296: PETSC_EXTERN PetscErrorCode PetscLogActions(PetscBool);
    297: PETSC_EXTERN PetscErrorCode PetscLogObjects(PetscBool);
    298: PETSC_EXTERN PetscErrorCode PetscLogSetThreshold(PetscLogDouble,PetscLogDouble*);
    299: PETSC_EXTERN PetscErrorCode PetscLogSet(PetscErrorCode (*)(int, int, PetscObject, PetscObject, PetscObject, PetscObject),
    300:                                         PetscErrorCode (*)(int, int, PetscObject, PetscObject, PetscObject, PetscObject));

    302: /* Output functions */
    303: PETSC_EXTERN PetscErrorCode PetscLogView(PetscViewer);
    304: PETSC_EXTERN PetscErrorCode PetscLogViewFromOptions(void);
    305: PETSC_EXTERN PetscErrorCode PetscLogDump(const char[]);

    307: /* Stage functions */
    308: PETSC_EXTERN PetscErrorCode PetscLogStageRegister(const char[],PetscLogStage*);
    309: PETSC_EXTERN PetscErrorCode PetscLogStagePush(PetscLogStage);
    310: PETSC_EXTERN PetscErrorCode PetscLogStagePop(void);
    311: PETSC_EXTERN PetscErrorCode PetscLogStageSetActive(PetscLogStage,PetscBool);
    312: PETSC_EXTERN PetscErrorCode PetscLogStageGetActive(PetscLogStage,PetscBool*);
    313: PETSC_EXTERN PetscErrorCode PetscLogStageSetVisible(PetscLogStage,PetscBool);
    314: PETSC_EXTERN PetscErrorCode PetscLogStageGetVisible(PetscLogStage,PetscBool*);
    315: PETSC_EXTERN PetscErrorCode PetscLogStageGetId(const char[],PetscLogStage*);

    317: /* Event functions */
    318: PETSC_EXTERN PetscErrorCode PetscLogEventRegister(const char[],PetscClassId,PetscLogEvent*);
    319: PETSC_EXTERN PetscErrorCode PetscLogEventSetCollective(PetscLogEvent,PetscBool);
    320: PETSC_EXTERN PetscErrorCode PetscLogEventIncludeClass(PetscClassId);
    321: PETSC_EXTERN PetscErrorCode PetscLogEventExcludeClass(PetscClassId);
    322: PETSC_EXTERN PetscErrorCode PetscLogEventActivate(PetscLogEvent);
    323: PETSC_EXTERN PetscErrorCode PetscLogEventDeactivate(PetscLogEvent);
    324: PETSC_EXTERN PetscErrorCode PetscLogEventSetActiveAll(PetscLogEvent,PetscBool);
    325: PETSC_EXTERN PetscErrorCode PetscLogEventActivateClass(PetscClassId);
    326: PETSC_EXTERN PetscErrorCode PetscLogEventDeactivateClass(PetscClassId);
    327: PETSC_EXTERN PetscErrorCode PetscLogEventGetId(const char[],PetscLogEvent*);
    328: PETSC_EXTERN PetscErrorCode PetscLogEventGetPerfInfo(int,PetscLogEvent,PetscEventPerfInfo*);
    329: PETSC_EXTERN PetscErrorCode PetscLogEventSetDof(PetscLogEvent, PetscInt, PetscLogDouble);
    330: PETSC_EXTERN PetscErrorCode PetscLogEventSetError(PetscLogEvent, PetscInt, PetscLogDouble);

    332: /* Global counters */
    333: PETSC_EXTERN PetscLogDouble petsc_irecv_ct;
    334: PETSC_EXTERN PetscLogDouble petsc_isend_ct;
    335: PETSC_EXTERN PetscLogDouble petsc_recv_ct;
    336: PETSC_EXTERN PetscLogDouble petsc_send_ct;
    337: PETSC_EXTERN PetscLogDouble petsc_irecv_len;
    338: PETSC_EXTERN PetscLogDouble petsc_isend_len;
    339: PETSC_EXTERN PetscLogDouble petsc_recv_len;
    340: PETSC_EXTERN PetscLogDouble petsc_send_len;
    341: PETSC_EXTERN PetscLogDouble petsc_allreduce_ct;
    342: PETSC_EXTERN PetscLogDouble petsc_gather_ct;
    343: PETSC_EXTERN PetscLogDouble petsc_scatter_ct;
    344: PETSC_EXTERN PetscLogDouble petsc_wait_ct;
    345: PETSC_EXTERN PetscLogDouble petsc_wait_any_ct;
    346: PETSC_EXTERN PetscLogDouble petsc_wait_all_ct;
    347: PETSC_EXTERN PetscLogDouble petsc_sum_of_waits_ct;

    349: PETSC_EXTERN PetscBool      PetscLogMemory;

    351: PETSC_EXTERN PetscBool PetscLogSyncOn;  /* true if logging synchronization is enabled */
    352: PETSC_EXTERN PetscErrorCode PetscLogEventSynchronize(PetscLogEvent, MPI_Comm);

    354: #define PetscLogEventSync(e,comm) \
    355:   (((PetscLogPLB && petsc_stageLog->stageInfo[petsc_stageLog->curStage].perfInfo.active && petsc_stageLog->stageInfo[petsc_stageLog->curStage].eventLog->eventInfo[e].active) ? \
    356:     PetscLogEventSynchronize((e),(comm)) : 0 ))

    358: #define PetscLogEventBegin(e,o1,o2,o3,o4) \
    359:   (((PetscLogPLB && petsc_stageLog->stageInfo[petsc_stageLog->curStage].perfInfo.active && petsc_stageLog->stageInfo[petsc_stageLog->curStage].eventLog->eventInfo[e].active) ? \
    360:     (*PetscLogPLB)((e),0,(PetscObject)(o1),(PetscObject)(o2),(PetscObject)(o3),(PetscObject)(o4)) : 0 ))

    362: #define PetscLogEventEnd(e,o1,o2,o3,o4) \
    363:   (((PetscLogPLE && petsc_stageLog->stageInfo[petsc_stageLog->curStage].perfInfo.active && petsc_stageLog->stageInfo[petsc_stageLog->curStage].eventLog->eventInfo[e].active) ? \
    364:     (*PetscLogPLE)((e),0,(PetscObject)(o1),(PetscObject)(o2),(PetscObject)(o3),(PetscObject)(o4)) : 0 ))

    366: PETSC_EXTERN PetscErrorCode PetscLogEventGetFlops(PetscLogEvent,PetscLogDouble*);
    367: PETSC_EXTERN PetscErrorCode PetscLogEventZeroFlops(PetscLogEvent);

    369: /*
    370:      These are used internally in the PETSc routines to keep a count of MPI messages and
    371:    their sizes.

    373:      This does not work for MPI-Uni because our include/petsc/mpiuni/mpi.h file
    374:    uses macros to defined the MPI operations.

    376:      It does not work correctly from HP-UX because it processes the
    377:    macros in a way that sometimes it double counts, hence
    378:    PETSC_HAVE_BROKEN_RECURSIVE_MACRO

    380:      It does not work with Windows because winmpich lacks MPI_Type_size()
    381: */
    382: #if !defined(MPIUNI_H) && !defined(PETSC_HAVE_BROKEN_RECURSIVE_MACRO) && !defined (PETSC_HAVE_MPI_MISSING_TYPESIZE)
    383: /*
    384:    Logging of MPI activities
    385: */
    386: PETSC_STATIC_INLINE PetscErrorCode PetscMPITypeSize(PetscInt count,MPI_Datatype type,PetscLogDouble *length)
    387: {
    388:   PetscMPIInt    typesize;
    390:   if (type == MPI_DATATYPE_NULL) return 0;
    391:   MPI_Type_size(type,&typesize);
    392:   *length += (PetscLogDouble) (count*typesize);
    393:   return 0;
    394: }

    396: PETSC_STATIC_INLINE PetscErrorCode PetscMPITypeSizeComm(MPI_Comm comm,const PetscMPIInt *counts,MPI_Datatype type,PetscLogDouble *length)
    397: {
    398:   PetscMPIInt    typesize,size,p;

    401:   if (type == MPI_DATATYPE_NULL) return 0;
    402:   MPI_Comm_size(comm,&size);
    403:   MPI_Type_size(type,&typesize);
    404:   for (p=0; p<size; ++p) {
    405:     *length += (PetscLogDouble) (counts[p]*typesize);
    406:   }
    407:   return 0;
    408: }

    410: PETSC_STATIC_INLINE PetscErrorCode PetscMPITypeSizeCount(PetscInt n,const PetscMPIInt *counts,MPI_Datatype type,PetscLogDouble *length)
    411: {
    412:   PetscMPIInt    typesize,p;

    415:   if (type == MPI_DATATYPE_NULL) return 0;
    416:   MPI_Type_size(type,&typesize);
    417:   for (p=0; p<n; ++p) {
    418:     *length += (PetscLogDouble) (counts[p]*typesize);
    419:   }
    420:   return 0;
    421: }

    423: /*
    424:     Returns 1 if the communicator is parallel else zero
    425: */
    426: PETSC_STATIC_INLINE int PetscMPIParallelComm(MPI_Comm comm)
    427: {
    428:   PetscMPIInt size; MPI_Comm_size(comm,&size); return size > 1;
    429: }

    431: #define MPI_Irecv(buf,count,datatype,source,tag,comm,request) \
    432:   ((petsc_irecv_ct++,0) || PetscMPITypeSize((count),(datatype),&(petsc_irecv_len)) || MPI_Irecv((buf),(count),(datatype),(source),(tag),(comm),(request)))

    434: #define MPI_Isend(buf,count,datatype,dest,tag,comm,request) \
    435:   ((petsc_isend_ct++,0) || PetscMPITypeSize((count),(datatype),&(petsc_isend_len)) || MPI_Isend((buf),(count),(datatype),(dest),(tag),(comm),(request)))

    437: #define MPI_Startall_irecv(count,datatype,number,requests) \
    438:   ((petsc_irecv_ct += (PetscLogDouble)(number),0) || PetscMPITypeSize((count),(datatype),&(petsc_irecv_len)) || ((number) && MPI_Startall((number),(requests))))

    440: #define MPI_Startall_isend(count,datatype,number,requests) \
    441:   ((petsc_isend_ct += (PetscLogDouble)(number),0) || PetscMPITypeSize((count),(datatype),&(petsc_isend_len)) || ((number) && MPI_Startall((number),(requests))))

    443: #define MPI_Start_isend(count,datatype,requests) \
    444:   ((petsc_isend_ct++,0) || PetscMPITypeSize((count),(datatype),(&petsc_isend_len)) || MPI_Start((requests)))

    446: #define MPI_Recv(buf,count,datatype,source,tag,comm,status) \
    447:   ((petsc_recv_ct++,0) || PetscMPITypeSize((count),(datatype),(&petsc_recv_len)) || MPI_Recv((buf),(count),(datatype),(source),(tag),(comm),(status)))

    449: #define MPI_Send(buf,count,datatype,dest,tag,comm) \
    450:   ((petsc_send_ct++,0) || PetscMPITypeSize((count),(datatype),(&petsc_send_len)) || MPI_Send((buf),(count),(datatype),(dest),(tag),(comm)))

    452: #define MPI_Wait(request,status) \
    453:   ((petsc_wait_ct++,petsc_sum_of_waits_ct++,0) || MPI_Wait((request),(status)))

    455: #define MPI_Waitany(a,b,c,d) \
    456:   ((petsc_wait_any_ct++,petsc_sum_of_waits_ct++,0) || MPI_Waitany((a),(b),(c),(d)))

    458: #define MPI_Waitall(count,array_of_requests,array_of_statuses) \
    459:   ((petsc_wait_all_ct++,petsc_sum_of_waits_ct += (PetscLogDouble) (count),0) || MPI_Waitall((count),(array_of_requests),(array_of_statuses)))

    461: #define MPI_Allreduce(sendbuf,recvbuf,count,datatype,op,comm) \
    462:   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || MPI_Allreduce((sendbuf),(recvbuf),(count),(datatype),(op),(comm)))

    464: #define MPI_Bcast(buffer,count,datatype,root,comm) \
    465:   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || MPI_Bcast((buffer),(count),(datatype),(root),(comm)))

    467: #define MPI_Reduce_scatter_block(sendbuf,recvbuf,recvcount,datatype,op,comm) \
    468:   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || MPI_Reduce_scatter_block((sendbuf),(recvbuf),(recvcount),(datatype),(op),(comm)))

    470: #define MPI_Alltoall(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,comm) \
    471:   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || PetscMPITypeSize((sendcount),(sendtype),(&petsc_send_len)) || MPI_Alltoall((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(comm)))

    473: #define MPI_Alltoallv(sendbuf,sendcnts,sdispls,sendtype,recvbuf,recvcnts,rdispls,recvtype,comm) \
    474:   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || PetscMPITypeSizeComm((comm),(sendcnts),(sendtype),(&petsc_send_len)) || MPI_Alltoallv((sendbuf),(sendcnts),(sdispls),(sendtype),(recvbuf),(recvcnts),(rdispls),(recvtype),(comm)))

    476: #define MPI_Allgather(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,comm) \
    477:   ((petsc_gather_ct += PetscMPIParallelComm((comm)),0) || MPI_Allgather((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(comm)))

    479: #define MPI_Allgatherv(sendbuf,sendcount,sendtype,recvbuf,recvcount,displs,recvtype,comm) \
    480:   ((petsc_gather_ct += PetscMPIParallelComm((comm)),0) || MPI_Allgatherv((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(displs),(recvtype),(comm)))

    482: #define MPI_Gather(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,root,comm) \
    483:   ((petsc_gather_ct++,0) || PetscMPITypeSize((sendcount),(sendtype),(&petsc_send_len)) || MPI_Gather((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm)))

    485: #define MPI_Gatherv(sendbuf,sendcount,sendtype,recvbuf,recvcount,displs,recvtype,root,comm) \
    486:   ((petsc_gather_ct++,0) || PetscMPITypeSize((sendcount),(sendtype),(&petsc_send_len)) || MPI_Gatherv((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(displs),(recvtype),(root),(comm)))

    488: #define MPI_Scatter(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,root,comm) \
    489:   ((petsc_scatter_ct++,0) || PetscMPITypeSize((recvcount),(recvtype),(&petsc_recv_len)) || MPI_Scatter((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm)))

    491: #define MPI_Scatterv(sendbuf,sendcount,displs,sendtype,recvbuf,recvcount,recvtype,root,comm) \
    492:   ((petsc_scatter_ct++,0) || PetscMPITypeSize((recvcount),(recvtype),(&petsc_recv_len)) || MPI_Scatterv((sendbuf),(sendcount),(displs),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm)))

    494: #define MPI_Ialltoall(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,comm,request) \
    495:   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || PetscMPITypeSize((sendcount),(sendtype),(&petsc_send_len)) || MPI_Ialltoall((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(comm),(request)))

    497: #define MPI_Ialltoallv(sendbuf,sendcnts,sdispls,sendtype,recvbuf,recvcnts,rdispls,recvtype,comm,request) \
    498:   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || PetscMPITypeSizeComm((comm),(sendcnts),(sendtype),(&petsc_send_len)) || MPI_Ialltoallv((sendbuf),(sendcnts),(sdispls),(sendtype),(recvbuf),(recvcnts),(rdispls),(recvtype),(comm),(request)))

    500: #define MPI_Iallgather(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,comm,request) \
    501:   ((petsc_gather_ct += PetscMPIParallelComm((comm)),0) || MPI_Iallgather((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(comm),(request)))

    503: #define MPI_Iallgatherv(sendbuf,sendcount,sendtype,recvbuf,recvcount,displs,recvtype,comm,request) \
    504:   ((petsc_gather_ct += PetscMPIParallelComm((comm)),0) || MPI_Iallgatherv((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(displs),(recvtype),(comm),(request)))

    506: #define MPI_Igather(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,root,comm,request) \
    507:   ((petsc_gather_ct++,0) || PetscMPITypeSize((sendcount),(sendtype),(&petsc_send_len)) || MPI_Igather((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm),(request)))

    509: #define MPI_Igatherv(sendbuf,sendcount,sendtype,recvbuf,recvcount,displs,recvtype,root,comm,request) \
    510:   ((petsc_gather_ct++,0) || PetscMPITypeSize((sendcount),(sendtype),(&petsc_send_len)) || MPI_Igatherv((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(displs),(recvtype),(root),(comm),(request)))

    512: #define MPI_Iscatter(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,root,comm,request) \
    513:   ((petsc_scatter_ct++,0) || PetscMPITypeSize((recvcount),(recvtype),(&petsc_recv_len)) || MPI_Iscatter((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm),(request)))

    515: #define MPI_Iscatterv(sendbuf,sendcount,displs,sendtype,recvbuf,recvcount,recvtype,root,comm,request) \
    516:   ((petsc_scatter_ct++,0) || PetscMPITypeSize((recvcount),(recvtype),(&petsc_recv_len)) || MPI_Iscatterv((sendbuf),(sendcount),(displs),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm),(request)))

    518: /* We treat MPI_Ineighbor_alltoallv as a set of isend/irecv instead of a traditional MPI collective.
    519:    OpenMPI-3.0 ran into error with outdegree = indegree = 0, so we use ((outdegree) || (indegree)) as a workaround.
    520:  */
    521: #define MPI_Start_ineighbor_alltoallv(outdegree,indegree,sendbuf,sendcnts,sdispls,sendtype,recvbuf,recvcnts,rdispls,recvtype,comm,request) \
    522:   ((petsc_isend_ct += (PetscLogDouble)(outdegree),0) || (petsc_irecv_ct += (PetscLogDouble)(indegree),0) || PetscMPITypeSizeCount((outdegree),(sendcnts),(sendtype),(&petsc_isend_len)) || PetscMPITypeSizeCount((indegree),(recvcnts),(recvtype),(&petsc_irecv_len)) || (((outdegree) || (indegree)) && MPI_Ineighbor_alltoallv((sendbuf),(sendcnts),(sdispls),(sendtype),(recvbuf),(recvcnts),(rdispls),(recvtype),(comm),(request))))

    524: #define MPI_Start_neighbor_alltoallv(outdegree,indegree,sendbuf,sendcnts,sdispls,sendtype,recvbuf,recvcnts,rdispls,recvtype,comm) \
    525:   ((petsc_isend_ct += (PetscLogDouble)(outdegree),0) || (petsc_irecv_ct += (PetscLogDouble)(indegree),0) || PetscMPITypeSizeCount((outdegree),(sendcnts),(sendtype),(&petsc_isend_len)) || PetscMPITypeSizeCount((indegree),(recvcnts),(recvtype),(&petsc_irecv_len)) || (((outdegree) || (indegree)) && MPI_Neighbor_alltoallv((sendbuf),(sendcnts),(sdispls),(sendtype),(recvbuf),(recvcnts),(rdispls),(recvtype),(comm))))

    527: #else

    529: #define MPI_Startall_irecv(count,datatype,number,requests) \
    530:   ((number) && MPI_Startall((number),(requests)))

    532: #define MPI_Startall_isend(count,datatype,number,requests) \
    533:   ((number) && MPI_Startall((number),(requests)))

    535: #define MPI_Start_isend(count,datatype,requests) \
    536:   (MPI_Start((requests)))

    538: #define MPI_Start_ineighbor_alltoallv(outdegree,indegree,sendbuf,sendcnts,sdispls,sendtype,recvbuf,recvcnts,rdispls,recvtype,comm,request) \
    539:   (((outdegree) || (indegree)) && MPI_Ineighbor_alltoallv((sendbuf),(sendcnts),(sdispls),(sendtype),(recvbuf),(recvcnts),(rdispls),(recvtype),(comm),(request)))

    541: #define MPI_Start_neighbor_alltoallv(outdegree,indegree,sendbuf,sendcnts,sdispls,sendtype,recvbuf,recvcnts,rdispls,recvtype,comm) \
    542:   (((outdegree) || (indegree)) && MPI_Neighbor_alltoallv((sendbuf),(sendcnts),(sdispls),(sendtype),(recvbuf),(recvcnts),(rdispls),(recvtype),(comm)))
    543: #endif /* !MPIUNI_H && ! PETSC_HAVE_BROKEN_RECURSIVE_MACRO */

    545: #else  /* ---Logging is turned off --------------------------------------------*/

    547: #define PetscLogMemory                     PETSC_FALSE

    549: #define PetscLogFlops(n)                   0
    550: #define PetscGetFlops(a)                   (*(a) = 0.0,0)

    552: #define PetscLogStageRegister(a,b)         0
    553: #define PetscLogStagePush(a)               0
    554: #define PetscLogStagePop()                 0
    555: #define PetscLogStageSetActive(a,b)        0
    556: #define PetscLogStageGetActive(a,b)        0
    557: #define PetscLogStageGetVisible(a,b)       0
    558: #define PetscLogStageSetVisible(a,b)       0
    559: #define PetscLogStageGetId(a,b)            (*(b)=0,0)

    561: #define PetscLogEventRegister(a,b,c)       0
    562: #define PetscLogEventSetCollective(a,b)    0
    563: #define PetscLogEventIncludeClass(a)       0
    564: #define PetscLogEventExcludeClass(a)       0
    565: #define PetscLogEventActivate(a)           0
    566: #define PetscLogEventDeactivate(a)         0
    567: #define PetscLogEventActivateClass(a)      0
    568: #define PetscLogEventDeactivateClass(a)    0
    569: #define PetscLogEventSetActiveAll(a,b)     0
    570: #define PetscLogEventGetId(a,b)            (*(b)=0,0)
    571: #define PetscLogEventGetPerfInfo(a,b,c)    0
    572: #define PetscLogEventSetDof(a,b,c)         0
    573: #define PetscLogEventSetError(a,b,c)       0

    575: #define PetscLogPLB                        0
    576: #define PetscLogPLE                        0
    577: #define PetscLogPHC                        0
    578: #define PetscLogPHD                        0

    580: #define PetscLogObjectParents(p,n,c)       0
    581: #define PetscLogObjectCreate(h)            0
    582: #define PetscLogObjectDestroy(h)           0
    583: PETSC_EXTERN PetscErrorCode PetscLogObjectState(PetscObject,const char[],...);

    585: #define PetscLogDefaultBegin()             0
    586: #define PetscLogAllBegin()                 0
    587: #define PetscLogNestedBegin()              0
    588: #define PetscLogTraceBegin(file)           0
    589: #define PetscLogActions(a)                 0
    590: #define PetscLogObjects(a)                 0
    591: #define PetscLogSetThreshold(a,b)          0
    592: #define PetscLogSet(lb,le)                 0

    594: #define PetscLogView(viewer)               0
    595: #define PetscLogViewFromOptions()          0
    596: #define PetscLogDump(c)                    0

    598: #define PetscLogEventSync(e,comm)          0
    599: #define PetscLogEventBegin(e,o1,o2,o3,o4)  0
    600: #define PetscLogEventEnd(e,o1,o2,o3,o4)    0

    602: /* If PETSC_USE_LOG is NOT defined, these still need to be! */
    603: #define MPI_Startall_irecv(count,datatype,number,requests) ((number) && MPI_Startall(number,requests))
    604: #define MPI_Startall_isend(count,datatype,number,requests) ((number) && MPI_Startall(number,requests))
    605: #define MPI_Start_isend(count,datatype,requests)           MPI_Start(requests)
    606: #define MPI_Start_ineighbor_alltoallv(outdegree,indegree,sendbuf,sendcnts,sdispls,sendtype,recvbuf,recvcnts,rdispls,recvtype,comm,request) \
    607:   (((outdegree) || (indegree)) && MPI_Ineighbor_alltoallv((sendbuf),(sendcnts),(sdispls),(sendtype),(recvbuf),(recvcnts),(rdispls),(recvtype),(comm),(request)))
    608: #define MPI_Start_neighbor_alltoallv(outdegree,indegree,sendbuf,sendcnts,sdispls,sendtype,recvbuf,recvcnts,rdispls,recvtype,comm) \
    609:   (((outdegree) || (indegree)) && MPI_Neighbor_alltoallv((sendbuf),(sendcnts),(sdispls),(sendtype),(recvbuf),(recvcnts),(rdispls),(recvtype),(comm)))

    611: #endif   /* PETSC_USE_LOG */

    613: #define PetscPreLoadBegin(flag,name) \
    614: do {\
    615:   PetscBool      PetscPreLoading = flag;\
    616:   int            PetscPreLoadMax,PetscPreLoadIt;\
    617:   PetscLogStage  _stageNum;\
    618:   PetscErrorCode _3_ierr; \
    619:   _3_PetscOptionsGetBool(NULL,NULL,"-preload",&PetscPreLoading,NULL);CHKERRQ(_3_ierr); \
    620:   PetscPreLoadMax = (int)(PetscPreLoading);\
    621:   PetscPreLoadingUsed = PetscPreLoading ? PETSC_TRUE : PetscPreLoadingUsed;\
    622:   for (PetscPreLoadIt=0; PetscPreLoadIt<=PetscPreLoadMax; PetscPreLoadIt++) {\
    623:     PetscPreLoadingOn = PetscPreLoading;\
    624:     _3_PetscBarrier(NULL);CHKERRQ(_3_ierr);\
    625:     if (PetscPreLoadIt>0) {\
    626:       _3_PetscLogStageGetId(name,&_stageNum);CHKERRQ(_3_ierr);\
    627:     } else {\
    628:       _3_PetscLogStageRegister(name,&_stageNum);CHKERRQ(_3_ierr); \
    629:     }\
    630:     _3_PetscLogStageSetActive(_stageNum,(PetscBool)(!PetscPreLoadMax || PetscPreLoadIt));\
    631:     _3_PetscLogStagePush(_stageNum);CHKERRQ(_3_ierr);

    633: #define PetscPreLoadEnd() \
    634:     _3_PetscLogStagePop();CHKERRQ(_3_ierr);\
    635:     PetscPreLoading = PETSC_FALSE;\
    636:   }\
    637: } while (0)

    639: #define PetscPreLoadStage(name) do {                                         \
    640:     _3_PetscLogStagePop();CHKERRQ(_3_ierr);                      \
    641:     if (PetscPreLoadIt>0) {                                                  \
    642:       _3_PetscLogStageGetId(name,&_stageNum);CHKERRQ(_3_ierr);   \
    643:     } else {                                                            \
    644:       _3_PetscLogStageRegister(name,&_stageNum);CHKERRQ(_3_ierr); \
    645:     }                                                                   \
    646:     _3_PetscLogStageSetActive(_stageNum,(PetscBool)(!PetscPreLoadMax || PetscPreLoadIt)); \
    647:     _3_PetscLogStagePush(_stageNum);CHKERRQ(_3_ierr);            \
    648:   } while (0)

    650: /* some vars for logging */
    651: PETSC_EXTERN PetscBool PetscPreLoadingUsed;       /* true if we are or have done preloading */
    652: PETSC_EXTERN PetscBool PetscPreLoadingOn;         /* true if we are currently in a preloading calculation */

    654: #endif