Actual source code: petsclog.h

      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[],PetscObject,const char[],...) PETSC_ATTRIBUTE_FORMAT(3,4);
     12: #if defined(PETSC_USE_INFO)
     13: #define PetscInfo(A,...) PetscInfo_Private(PETSC_FUNCTION_NAME,((PetscObject)A),__VA_ARGS__)
     14: #else
     15: #define PetscInfo(A,...) 0
     16: #endif

     18: #define PetscInfo1(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use PetscInfo() (since version 3.17)\"") PetscInfo(__VA_ARGS__)
     19: #define PetscInfo2(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use PetscInfo() (since version 3.17)\"") PetscInfo(__VA_ARGS__)
     20: #define PetscInfo3(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use PetscInfo() (since version 3.17)\"") PetscInfo(__VA_ARGS__)
     21: #define PetscInfo4(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use PetscInfo() (since version 3.17)\"") PetscInfo(__VA_ARGS__)
     22: #define PetscInfo5(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use PetscInfo() (since version 3.17)\"") PetscInfo(__VA_ARGS__)
     23: #define PetscInfo6(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use PetscInfo() (since version 3.17)\"") PetscInfo(__VA_ARGS__)
     24: #define PetscInfo7(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use PetscInfo() (since version 3.17)\"") PetscInfo(__VA_ARGS__)
     25: #define PetscInfo8(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use PetscInfo() (since version 3.17)\"") PetscInfo(__VA_ARGS__)
     26: #define PetscInfo9(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use PetscInfo() (since version 3.17)\"") PetscInfo(__VA_ARGS__)

     28: /*E
     29:     PetscInfoCommFlag - Describes the method by which to filter PetscInfo() by communicator size

     31:     Used as an input for PetscInfoSetFilterCommSelf()

     33: $   PETSC_INFO_COMM_ALL - Default uninitialized value. PetscInfo() will not filter based on communicator size (i.e. will
     34: print for all communicators)
     35: $   PETSC_INFO_COMM_NO_SELF - PetscInfo() will NOT print for communicators with size = 1 (i.e. *_COMM_SELF)
     36: $   PETSC_INFO_COMM_ONLY_SELF - PetscInfo will ONLY print for communicators with size = 1

     38:     Level: intermediate

     40: .seealso: PetscInfo(), PetscInfoSetFromOptions(), PetscInfoSetFilterCommSelf()
     41: E*/
     42: typedef enum {
     43:   PETSC_INFO_COMM_ALL = -1,
     44:   PETSC_INFO_COMM_NO_SELF = 0,
     45:   PETSC_INFO_COMM_ONLY_SELF = 1
     46: } PetscInfoCommFlag;

     48: PETSC_EXTERN const char * const PetscInfoCommFlags[];
     49: PETSC_EXTERN PetscErrorCode PetscInfoDeactivateClass(PetscClassId);
     50: PETSC_EXTERN PetscErrorCode PetscInfoActivateClass(PetscClassId);
     51: PETSC_EXTERN PetscErrorCode PetscInfoEnabled(PetscClassId, PetscBool *);
     52: PETSC_EXTERN PetscErrorCode PetscInfoAllow(PetscBool);
     53: PETSC_EXTERN PetscErrorCode PetscInfoSetFile(const char[],const char[]);
     54: PETSC_EXTERN PetscErrorCode PetscInfoGetFile(char **,FILE **);
     55: PETSC_EXTERN PetscErrorCode PetscInfoSetClasses(PetscBool,PetscInt,const char *const *);
     56: PETSC_EXTERN PetscErrorCode PetscInfoGetClass(const char *, PetscBool *);
     57: PETSC_EXTERN PetscErrorCode PetscInfoGetInfo(PetscBool *,PetscBool *,PetscBool *,PetscBool *,PetscInfoCommFlag *);
     58: PETSC_EXTERN PetscErrorCode PetscInfoProcessClass(const char[],PetscInt,PetscClassId[]);
     59: PETSC_EXTERN PetscErrorCode PetscInfoSetFilterCommSelf(PetscInfoCommFlag);
     60: PETSC_EXTERN PetscErrorCode PetscInfoSetFromOptions(PetscOptions);
     61: PETSC_EXTERN PetscErrorCode PetscInfoDestroy(void);
     62: PETSC_EXTERN PetscBool      PetscLogPrintInfo;  /* if true, indicates PetscInfo() is turned on */

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

     68:     Level: intermediate

     70: .seealso: PetscLogEventRegister(), PetscLogEventBegin(), PetscLogEventEnd(), PetscLogStage
     71: M*/
     72: typedef int PetscLogEvent;

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

     77:     Level: intermediate

     79: .seealso: PetscLogStageRegister(), PetscLogStagePush(), PetscLogStagePop(), PetscLogEvent
     80: M*/
     81: typedef int PetscLogStage;

     83: #define PETSC_EVENT  1311311
     84: PETSC_EXTERN PetscLogEvent PETSC_LARGEST_EVENT;

     86: /* Global flop counter */
     87: PETSC_EXTERN PetscLogDouble petsc_TotalFlops;
     88: PETSC_EXTERN PetscLogDouble petsc_tmp_flops;

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

     94:      The code that manipulates these structures is in src/sys/logging/utils.
     95: */
     96: typedef struct _n_PetscIntStack *PetscIntStack;

     98: /* -----------------------------------------------------------------------------------------------------*/
     99: /*
    100:     PetscClassRegInfo, PetscClassPerfInfo - Each class has two data structures associated with it. The first has
    101:        static information about it, the second collects statistics on how many objects of the class are created,
    102:        how much memory they use, etc.

    104:     PetscClassRegLog, PetscClassPerfLog - arrays of the PetscClassRegInfo and PetscClassPerfInfo for all classes.
    105: */
    106: typedef struct  {
    107:   char           *name;   /* The class name */
    108:   PetscClassId   classid; /* The integer identifying this class */
    109: } PetscClassRegInfo;

    111: typedef struct {
    112:   PetscClassId   id;           /* The integer identifying this class */
    113:   int            creations;    /* The number of objects of this class created */
    114:   int            destructions; /* The number of objects of this class destroyed */
    115:   PetscLogDouble mem;          /* The total memory allocated by objects of this class */
    116:   PetscLogDouble descMem;      /* The total memory allocated by descendents of these objects */
    117: } PetscClassPerfInfo;

    119: typedef struct _n_PetscClassRegLog *PetscClassRegLog;
    120: struct _n_PetscClassRegLog {
    121:   int               numClasses; /* The number of classes registered */
    122:   int               maxClasses; /* The maximum number of classes */
    123:   PetscClassRegInfo *classInfo; /* The structure for class information (classids are monotonicly increasing) */
    124: };

    126: typedef struct _n_PetscClassPerfLog *PetscClassPerfLog;
    127: struct _n_PetscClassPerfLog {
    128:   int                numClasses; /* The number of logging classes */
    129:   int                maxClasses; /* The maximum number of classes */
    130:   PetscClassPerfInfo *classInfo; /* The structure for class information (classids are monotonicly increasing) */
    131: };
    132: /* -----------------------------------------------------------------------------------------------------*/
    133: /*
    134:     PetscEventRegInfo, PetscEventPerfInfo - Each event has two data structures associated with it. The first has
    135:        static information about it, the second collects statistics on how many times the event is used, how
    136:        much time it takes, etc.

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

    141: */
    142: typedef struct {
    143:   char         *name;         /* The name of this event */
    144:   PetscClassId classid;       /* The class the event is associated with */
    145:   PetscBool    collective;    /* Flag this event as collective */
    146: #if defined (PETSC_HAVE_MPE)
    147:   int          mpe_id_begin;  /* MPE IDs that define the event */
    148:   int          mpe_id_end;
    149: #endif
    150: } PetscEventRegInfo;

    152: typedef struct {
    153:   int            id;            /* The integer identifying this event */
    154:   PetscBool      active;        /* The flag to activate logging */
    155:   PetscBool      visible;       /* The flag to print info in summary */
    156:   int            depth;         /* The nesting depth of the event call */
    157:   int            count;         /* The number of times this event was executed */
    158:   PetscLogDouble flops, flops2, flopsTmp; /* The flops and flops^2 used in this event */
    159:   PetscLogDouble time, time2, timeTmp;    /* The time and time^2 taken for this event */
    160:   PetscLogDouble syncTime;                /* The synchronization barrier time */
    161:   PetscLogDouble dof[8];        /* The number of degrees of freedom associated with this event */
    162:   PetscLogDouble errors[8];     /* The errors (user-defined) associated with this event */
    163:   PetscLogDouble numMessages;   /* The number of messages in this event */
    164:   PetscLogDouble messageLength; /* The total message lengths in this event */
    165:   PetscLogDouble numReductions; /* The number of reductions in this event */
    166:   PetscLogDouble memIncrease;   /* How much the resident memory has increased in this event */
    167:   PetscLogDouble mallocIncrease;/* How much the maximum malloced space has increased in this event */
    168:   PetscLogDouble mallocSpace;   /* How much the space was malloced and kept during this event */
    169:   PetscLogDouble mallocIncreaseEvent;  /* Maximum of the high water mark with in event minus memory available at the end of the event */
    170:   #if defined(PETSC_HAVE_DEVICE)
    171:   PetscLogDouble CpuToGpuCount; /* The total number of CPU to GPU copies */
    172:   PetscLogDouble GpuToCpuCount; /* The total number of GPU to CPU copies */
    173:   PetscLogDouble CpuToGpuSize;  /* The total size of CPU to GPU copies */
    174:   PetscLogDouble GpuToCpuSize;  /* The total size of GPU to CPU copies */
    175:   PetscLogDouble GpuFlops;      /* The flops done on a GPU in this event */
    176:   PetscLogDouble GpuTime;       /* The time spent on a GPU in this event */
    177:   #endif
    178: } PetscEventPerfInfo;

    180: typedef struct _n_PetscEventRegLog *PetscEventRegLog;
    181: struct _n_PetscEventRegLog {
    182:   int               numEvents;  /* The number of registered events */
    183:   int               maxEvents;  /* The maximum number of events */
    184:   PetscEventRegInfo *eventInfo; /* The registration information for each event */
    185: };

    187: typedef struct _n_PetscEventPerfLog *PetscEventPerfLog;
    188: struct _n_PetscEventPerfLog {
    189:   int                numEvents;  /* The number of logging events */
    190:   int                maxEvents;  /* The maximum number of events */
    191:   PetscEventPerfInfo *eventInfo; /* The performance information for each event */
    192: };
    193: /* ------------------------------------------------------------------------------------------------------------*/
    194: /*
    195:    PetscStageInfo - Contains all the information about a particular stage.

    197:    PetscStageLog - An array of PetscStageInfo for each registered stage. There is a single one of these in the code.
    198: */
    199: typedef struct _PetscStageInfo {
    200:   char               *name;     /* The stage name */
    201:   PetscBool          used;      /* The stage was pushed on this processor */
    202:   PetscEventPerfInfo perfInfo;  /* The stage performance information */
    203:   PetscEventPerfLog  eventLog;  /* The event information for this stage */
    204:   PetscClassPerfLog  classLog;  /* The class information for this stage */
    205: } PetscStageInfo;

    207: typedef struct _n_PetscStageLog *PetscStageLog;
    208: struct _n_PetscStageLog {
    209:   int              numStages;   /* The number of registered stages */
    210:   int              maxStages;   /* The maximum number of stages */
    211:   PetscIntStack    stack;       /* The stack for active stages */
    212:   int              curStage;    /* The current stage (only used in macros so we don't call PetscIntStackTop) */
    213:   PetscStageInfo   *stageInfo;  /* The information for each stage */
    214:   PetscEventRegLog eventLog;    /* The registered events */
    215:   PetscClassRegLog classLog;    /* The registered classes */
    216: };
    217: /* -----------------------------------------------------------------------------------------------------*/

    219: PETSC_EXTERN PetscErrorCode PetscLogObjectParent(PetscObject,PetscObject);
    220: PETSC_EXTERN PetscErrorCode PetscLogObjectMemory(PetscObject,PetscLogDouble);

    222: #if defined(PETSC_USE_LOG)  /* --- Logging is turned on --------------------------------*/
    223: PETSC_EXTERN PetscStageLog petsc_stageLog;
    224: PETSC_EXTERN PetscErrorCode PetscLogGetStageLog(PetscStageLog*);
    225: PETSC_EXTERN PetscErrorCode PetscStageLogGetCurrent(PetscStageLog,int*);
    226: PETSC_EXTERN PetscErrorCode PetscStageLogGetEventPerfLog(PetscStageLog,int,PetscEventPerfLog*);

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

    231:    For the complex numbers version, note that
    232:        1 complex addition = 2 flops
    233:        1 complex multiplication = 6 flops,
    234:    where we define 1 flop as that for a double precision scalar.  We roughly approximate
    235:    flop counting for complex numbers by multiplying the total flops by 4; this corresponds
    236:    to the assumption that we're counting mostly additions and multiplications -- and
    237:    roughly the same number of each.  More accurate counting could be done by distinguishing
    238:    among the various arithmetic operations.
    239:  */

    241: #if defined(PETSC_USE_COMPLEX)
    242: #define PETSC_FLOPS_PER_OP 4.0
    243: #else
    244: #define PETSC_FLOPS_PER_OP 1.0
    245: #endif

    247: /*@C
    248:        PetscLogFlops - Log how many flops are performed in a calculation

    250:    Input Parameter:
    251: .   flops - the number of flops

    253:    Notes:
    254:      To limit the chance of integer overflow when multiplying by a constant, represent the constant as a double,
    255:      not an integer. Use PetscLogFlops(4.0*n) not PetscLogFlops(4*n)

    257:    Level: intermediate

    259: .seealso: PetscLogView(), PetscLogGpuFlops()
    260: @*/

    262: static inline PetscErrorCode PetscLogFlops(PetscLogDouble n)
    263: {
    264: #if defined(PETSC_USE_DEBUG)
    266: #endif
    267:   petsc_TotalFlops += PETSC_FLOPS_PER_OP*n;
    268:   return 0;
    269: }

    271: PETSC_EXTERN PetscErrorCode PetscGetFlops(PetscLogDouble *);

    273: #if defined (PETSC_HAVE_MPE)
    274: PETSC_EXTERN PetscErrorCode PetscLogMPEBegin(void);
    275: PETSC_EXTERN PetscErrorCode PetscLogMPEDump(const char[]);
    276: #endif

    278: PETSC_EXTERN PetscErrorCode (*PetscLogPLB)(PetscLogEvent,int,PetscObject,PetscObject,PetscObject,PetscObject);
    279: PETSC_EXTERN PetscErrorCode (*PetscLogPLE)(PetscLogEvent,int,PetscObject,PetscObject,PetscObject,PetscObject);
    280: PETSC_EXTERN PetscErrorCode (*PetscLogPHC)(PetscObject);
    281: PETSC_EXTERN PetscErrorCode (*PetscLogPHD)(PetscObject);

    283: #define PetscLogObjectParents(p,n,d)  PetscMacroReturnStandard(for (int _i=0; _i<(n); ++_i) PetscLogObjectParent((PetscObject)(p),(PetscObject)(d)[_i]);)
    284: #define PetscLogObjectCreate(h)      ((PetscLogPHC) ? (*PetscLogPHC)((PetscObject)(h)) : 0)
    285: #define PetscLogObjectDestroy(h)     ((PetscLogPHD) ? (*PetscLogPHD)((PetscObject)(h)) : 0)
    286: PETSC_EXTERN PetscErrorCode PetscLogObjectState(PetscObject, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2,3);

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

    299: /* Output functions */
    300: PETSC_EXTERN PetscErrorCode PetscLogView(PetscViewer);
    301: PETSC_EXTERN PetscErrorCode PetscLogViewFromOptions(void);
    302: PETSC_EXTERN PetscErrorCode PetscLogDump(const char[]);

    304: /* Status checking functions */
    305: PETSC_EXTERN PetscErrorCode PetscLogIsActive(PetscBool*);

    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 PetscLogEventDeactivatePush(PetscLogEvent);
    325: PETSC_EXTERN PetscErrorCode PetscLogEventDeactivatePop(PetscLogEvent);
    326: PETSC_EXTERN PetscErrorCode PetscLogEventSetActiveAll(PetscLogEvent,PetscBool);
    327: PETSC_EXTERN PetscErrorCode PetscLogEventActivateClass(PetscClassId);
    328: PETSC_EXTERN PetscErrorCode PetscLogEventDeactivateClass(PetscClassId);
    329: PETSC_EXTERN PetscErrorCode PetscLogEventGetId(const char[],PetscLogEvent*);
    330: PETSC_EXTERN PetscErrorCode PetscLogEventGetPerfInfo(int,PetscLogEvent,PetscEventPerfInfo*);
    331: PETSC_EXTERN PetscErrorCode PetscLogEventSetDof(PetscLogEvent, PetscInt, PetscLogDouble);
    332: PETSC_EXTERN PetscErrorCode PetscLogEventSetError(PetscLogEvent, PetscInt, PetscLogDouble);

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

    351: PETSC_EXTERN PetscBool      PetscLogMemory;

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

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

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

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

    368: PETSC_EXTERN PetscErrorCode PetscLogEventGetFlops(PetscLogEvent,PetscLogDouble*);
    369: PETSC_EXTERN PetscErrorCode PetscLogEventZeroFlops(PetscLogEvent);

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

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

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

    382:      It does not work with Windows because winmpich lacks MPI_Type_size()
    383: */
    384: #if !defined(MPIUNI_H) && !defined(PETSC_HAVE_BROKEN_RECURSIVE_MACRO) && !defined (PETSC_HAVE_MPI_MISSING_TYPESIZE)
    385: /*
    386:    Logging of MPI activities
    387: */
    388: static inline PetscErrorCode PetscMPITypeSize(PetscInt count,MPI_Datatype type,PetscLogDouble *length)
    389: {
    390:   PetscMPIInt typesize;

    392:   if (type == MPI_DATATYPE_NULL) return 0;
    393:   MPI_Type_size(type,&typesize);
    394:   *length += (PetscLogDouble) (count*typesize);
    395:   return 0;
    396: }

    398: static inline PetscErrorCode PetscMPITypeSizeComm(MPI_Comm comm,const PetscMPIInt *counts,MPI_Datatype type,PetscLogDouble *length)
    399: {
    400:   PetscMPIInt    typesize,size,p;

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

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

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

    419: /*
    420:     Returns 1 if the communicator is parallel else zero
    421: */
    422: static inline int PetscMPIParallelComm(MPI_Comm comm)
    423: {
    424:   PetscMPIInt size; MPI_Comm_size(comm,&size); return size > 1;
    425: }

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

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

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

    436: #define MPI_Isend_c(buf,count,datatype,dest,tag,comm,request) \
    437:   ((petsc_isend_ct++,0) || PetscMPITypeSize((count),(datatype),&(petsc_isend_len)) || MPI_Isend_c((buf),(count),(datatype),(dest),(tag),(comm),(request)))

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

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

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

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

    451: #define MPI_Recv_c(buf,count,datatype,source,tag,comm,status) \
    452:   ((petsc_recv_ct++,0) || PetscMPITypeSize((count),(datatype),(&petsc_recv_len)) || MPI_Recv_c((buf),(count),(datatype),(source),(tag),(comm),(status)))

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

    457: #define MPI_Send_c(buf,count,datatype,dest,tag,comm) \
    458:   ((petsc_send_ct++,0) || PetscMPITypeSize((count),(datatype),(&petsc_send_len)) || MPI_Send_c((buf),(count),(datatype),(dest),(tag),(comm)))

    460: #define MPI_Wait(request,status) \
    461:   ((petsc_wait_ct++,petsc_sum_of_waits_ct++,0) || MPI_Wait((request),(status)))

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

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

    469: #define MPI_Allreduce(sendbuf,recvbuf,count,datatype,op,comm) \
    470:   (petsc_allreduce_ct += PetscMPIParallelComm((comm)),MPI_Allreduce((sendbuf),(recvbuf),(count),(datatype),(op),(comm)))

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

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

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

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

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

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

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

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

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

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

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

    505: #define MPI_Ialltoallv(sendbuf,sendcnts,sdispls,sendtype,recvbuf,recvcnts,rdispls,recvtype,comm,request) \
    506:   ((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)))

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

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

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

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

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

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

    526: #else

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

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

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

    537: #endif /* !MPIUNI_H && ! PETSC_HAVE_BROKEN_RECURSIVE_MACRO */

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

    541: #define PetscLogMemory                     PETSC_FALSE

    543: #define PetscLogFlops(n)                   0
    544: #define PetscGetFlops(a)                   (*(a) = 0.0,0)

    546: #define PetscLogStageRegister(a,b)         0
    547: #define PetscLogStagePush(a)               0
    548: #define PetscLogStagePop()                 0
    549: #define PetscLogStageSetActive(a,b)        0
    550: #define PetscLogStageGetActive(a,b)        0
    551: #define PetscLogStageGetVisible(a,b)       0
    552: #define PetscLogStageSetVisible(a,b)       0
    553: #define PetscLogStageGetId(a,b)            (*(b)=0,0)

    555: #define PetscLogEventRegister(a,b,c)       0
    556: #define PetscLogEventSetCollective(a,b)    0
    557: #define PetscLogEventIncludeClass(a)       0
    558: #define PetscLogEventExcludeClass(a)       0
    559: #define PetscLogEventActivate(a)           0
    560: #define PetscLogEventDeactivate(a)         0
    561: #define PetscLogEventDeactivatePush(a)     0
    562: #define PetscLogEventDeactivatePop(a)      0
    563: #define PetscLogEventActivateClass(a)      0
    564: #define PetscLogEventDeactivateClass(a)    0
    565: #define PetscLogEventSetActiveAll(a,b)     0
    566: #define PetscLogEventGetId(a,b)            (*(b)=0,0)
    567: #define PetscLogEventGetPerfInfo(a,b,c)    0
    568: #define PetscLogEventSetDof(a,b,c)         0
    569: #define PetscLogEventSetError(a,b,c)       0

    571: #define PetscLogPLB                        0
    572: #define PetscLogPLE                        0
    573: #define PetscLogPHC                        0
    574: #define PetscLogPHD                        0

    576: #define PetscLogObjectParents(p,n,c)       0
    577: #define PetscLogObjectCreate(h)            0
    578: #define PetscLogObjectDestroy(h)           0
    579: PETSC_EXTERN PetscErrorCode PetscLogObjectState(PetscObject,const char[],...) PETSC_ATTRIBUTE_FORMAT(2,3);

    581: #define PetscLogDefaultBegin()             0
    582: #define PetscLogAllBegin()                 0
    583: #define PetscLogNestedBegin()              0
    584: #define PetscLogTraceBegin(file)           0
    585: #define PetscLogActions(a)                 0
    586: #define PetscLogObjects(a)                 0
    587: #define PetscLogSetThreshold(a,b)          0
    588: #define PetscLogSet(lb,le)                 0
    589: #define PetscLogIsActive(flag)             (*(flag) = PETSC_FALSE,0)

    591: #define PetscLogView(viewer)               0
    592: #define PetscLogViewFromOptions()          0
    593: #define PetscLogDump(c)                    0

    595: #define PetscLogEventSync(e,comm)          0
    596: #define PetscLogEventBegin(e,o1,o2,o3,o4)  0
    597: #define PetscLogEventEnd(e,o1,o2,o3,o4)    0

    599: /* If PETSC_USE_LOG is NOT defined, these still need to be! */
    600: #define MPI_Startall_irecv(count,datatype,number,requests) ((number) && MPI_Startall(number,requests))
    601: #define MPI_Startall_isend(count,datatype,number,requests) ((number) && MPI_Startall(number,requests))
    602: #define MPI_Start_isend(count,datatype,requests)           MPI_Start(requests)

    604: #endif   /* PETSC_USE_LOG */

    606: #if defined (PETSC_USE_LOG) && defined(PETSC_HAVE_DEVICE)

    608: /* Global GPU counters */
    609: PETSC_EXTERN PetscLogDouble petsc_ctog_ct;
    610: PETSC_EXTERN PetscLogDouble petsc_gtoc_ct;
    611: PETSC_EXTERN PetscLogDouble petsc_ctog_sz;
    612: PETSC_EXTERN PetscLogDouble petsc_gtoc_sz;
    613: PETSC_EXTERN PetscLogDouble petsc_ctog_ct_scalar;
    614: PETSC_EXTERN PetscLogDouble petsc_gtoc_ct_scalar;
    615: PETSC_EXTERN PetscLogDouble petsc_ctog_sz_scalar;
    616: PETSC_EXTERN PetscLogDouble petsc_gtoc_sz_scalar;
    617: PETSC_EXTERN PetscLogDouble petsc_gflops;
    618: PETSC_EXTERN PetscLogDouble petsc_gtime;

    620: static inline PetscErrorCode PetscLogCpuToGpu(PetscLogDouble size)
    621: {
    622:   petsc_ctog_ct += 1;
    623:   petsc_ctog_sz += size;
    624:   return 0;
    625: }

    627: static inline PetscErrorCode PetscLogGpuToCpu(PetscLogDouble size)
    628: {
    629:   petsc_gtoc_ct += 1;
    630:   petsc_gtoc_sz += size;
    631:   return 0;
    632: }

    634: static inline PetscErrorCode PetscLogCpuToGpuScalar(PetscLogDouble size)
    635: {
    636:   petsc_ctog_ct_scalar += 1;
    637:   petsc_ctog_sz_scalar += size;
    638:   return 0;
    639: }

    641: static inline PetscErrorCode PetscLogGpuToCpuScalar(PetscLogDouble size)
    642: {
    643:   petsc_gtoc_ct_scalar += 1;
    644:   petsc_gtoc_sz_scalar += size;
    645:   return 0;
    646: }

    648: /*@C
    649:        PetscLogGpuFlops - Log how many flops are performed in a calculation on the device

    651:    Input Parameter:
    652: .   flops - the number of flops

    654:    Notes:
    655:      To limit the chance of integer overflow when multiplying by a constant, represent the constant as a double,
    656:      not an integer. Use PetscLogFlops(4.0*n) not PetscLogFlops(4*n)

    658:    Level: intermediate

    660: .seealso: PetscLogView(), PetscLogFlops(), PetscLogGpuTimeBegin(), PetscLogGpuTimeEnd()
    661: @*/
    662: static inline PetscErrorCode PetscLogGpuFlops(PetscLogDouble n)
    663: {
    665:   petsc_TotalFlops += PETSC_FLOPS_PER_OP*n;
    666:   petsc_gflops += PETSC_FLOPS_PER_OP*n;
    667:   return 0;
    668: }

    670: static inline PetscErrorCode PetscLogGpuTimeAdd(PetscLogDouble t)
    671: {
    672:   petsc_gtime += t;
    673:   return 0;
    674: }

    676: PETSC_EXTERN PetscErrorCode PetscLogGpuTimeBegin(void);
    677: PETSC_EXTERN PetscErrorCode PetscLogGpuTimeEnd(void);

    679: #else

    681: #define PetscLogCpuToGpu(a)                0
    682: #define PetscLogGpuToCpu(a)                0
    683: #define PetscLogCpuToGpuScalar(a)          0
    684: #define PetscLogGpuToCpuScalar(a)          0
    685: #define PetscLogGpuFlops(a)                0
    686: #define PetscLogGpuTimeAdd(a)              0
    687: #define PetscLogGpuTimeBegin()             0
    688: #define PetscLogGpuTimeEnd()               0

    690: #endif /* PETSC_USE_LOG && PETSC_HAVE_DEVICE */

    692: #define PetscPreLoadBegin(flag,name) \
    693: do {\
    694:   PetscBool      PetscPreLoading = flag;\
    695:   int            PetscPreLoadMax,PetscPreLoadIt;\
    696:   PetscLogStage  _stageNum;\
    697:   PetscOptionsGetBool(NULL,NULL,"-preload",&PetscPreLoading,NULL);     \
    698:   PetscPreLoadMax = (int)(PetscPreLoading);\
    699:   PetscPreLoadingUsed = PetscPreLoading ? PETSC_TRUE : PetscPreLoadingUsed;\
    700:   for (PetscPreLoadIt=0; PetscPreLoadIt<=PetscPreLoadMax; PetscPreLoadIt++) {\
    701:     PetscPreLoadingOn = PetscPreLoading;\
    702:     PetscBarrier(NULL);\
    703:     if (PetscPreLoadIt>0) PetscLogStageGetId(name,&_stageNum);\
    704:     else PetscLogStageRegister(name,&_stageNum);\
    705:     PetscLogStageSetActive(_stageNum,(PetscBool)(!PetscPreLoadMax || PetscPreLoadIt));\
    706:     PetscLogStagePush(_stageNum);

    708: #define PetscPreLoadEnd() \
    709:     PetscLogStagePop();\
    710:     PetscPreLoading = PETSC_FALSE;\
    711:   }\
    712: } while (0)

    714: #define PetscPreLoadStage(name) do {                                                           \
    715:     PetscLogStagePop();                                                               \
    716:     if (PetscPreLoadIt>0)   PetscLogStageGetId(name,&_stageNum);                      \
    717:     else PetscLogStageRegister(name,&_stageNum);                                      \
    718:     PetscLogStageSetActive(_stageNum,(PetscBool)(!PetscPreLoadMax || PetscPreLoadIt)); \
    719:     PetscLogStagePush(_stageNum);                                                     \
    720:   } while (0)

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

    726: #endif