xref: /petsc/include/petsclog.h (revision f5d6ab90bb5db6585845037d9bc4f2d2e84f9091)
197bb86f7SLois Curfman McInnes /*
27588ac45SBarry Smith     Defines profile/logging in PETSc.
397bb86f7SLois Curfman McInnes */
497bb86f7SLois Curfman McInnes 
5b0a32e0cSBarry Smith #if !defined(__PetscLog_H)
6b0a32e0cSBarry Smith #define __PetscLog_H
7aaa7dc30SBarry Smith #include <petscsys.h>
8c8d78d4dSSatish Balay 
9b859824cSBarry Smith /*MC
10b859824cSBarry Smith     PetscLogEvent - id used to identify PETSc or user events which timed portions (blocks of executable)
11b859824cSBarry Smith      code.
12b859824cSBarry Smith 
13b859824cSBarry Smith     Level: intermediate
14b859824cSBarry Smith 
15b859824cSBarry Smith .seealso: PetscLogEventRegister(), PetscLogEventBegin(), PetscLogEventEnd(), PetscLogStage
16b859824cSBarry Smith M*/
17b859824cSBarry Smith typedef int PetscLogEvent;
18b859824cSBarry Smith 
19b859824cSBarry Smith /*MC
20b859824cSBarry Smith     PetscLogStage - id used to identify user stages (phases, sections) of runs - for logging
21b859824cSBarry Smith 
22b859824cSBarry Smith     Level: intermediate
23b859824cSBarry Smith 
24522ce795SJed Brown .seealso: PetscLogStageRegister(), PetscLogStagePush(), PetscLogStagePop(), PetscLogEvent
25b859824cSBarry Smith M*/
26b859824cSBarry Smith typedef int PetscLogStage;
27b859824cSBarry Smith 
288ba1e511SMatthew Knepley #define PETSC_EVENT  1311311
29014dd563SJed Brown PETSC_EXTERN PetscLogEvent PETSC_LARGEST_EVENT;
3097bb86f7SLois Curfman McInnes 
3119b02663SBarry Smith /* Global flop counter */
32014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_TotalFlops;
33014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_tmp_flops;
3419b02663SBarry Smith 
35edde42fcSLois Curfman McInnes /* General logging of information; different from event logging */
36014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInfo_Private(const char[],void*,const char[],...);
376cf91177SBarry Smith #if defined(PETSC_USE_INFO)
382714b66aSJed Brown #define PetscInfo(A,S)                       PetscInfo_Private(PETSC_FUNCTION_NAME,A,S)
392714b66aSJed Brown #define PetscInfo1(A,S,a1)                   PetscInfo_Private(PETSC_FUNCTION_NAME,A,S,a1)
402714b66aSJed Brown #define PetscInfo2(A,S,a1,a2)                PetscInfo_Private(PETSC_FUNCTION_NAME,A,S,a1,a2)
412714b66aSJed Brown #define PetscInfo3(A,S,a1,a2,a3)             PetscInfo_Private(PETSC_FUNCTION_NAME,A,S,a1,a2,a3)
422714b66aSJed Brown #define PetscInfo4(A,S,a1,a2,a3,a4)          PetscInfo_Private(PETSC_FUNCTION_NAME,A,S,a1,a2,a3,a4)
432714b66aSJed Brown #define PetscInfo5(A,S,a1,a2,a3,a4,a5)       PetscInfo_Private(PETSC_FUNCTION_NAME,A,S,a1,a2,a3,a4,a5)
442714b66aSJed Brown #define PetscInfo6(A,S,a1,a2,a3,a4,a5,a6)    PetscInfo_Private(PETSC_FUNCTION_NAME,A,S,a1,a2,a3,a4,a5,a6)
452714b66aSJed Brown #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)
4663ba0a88SBarry Smith #else
47ae15b995SBarry Smith #define PetscInfo(A,S)                       0
48ae15b995SBarry Smith #define PetscInfo1(A,S,a1)                   0
49ae15b995SBarry Smith #define PetscInfo2(A,S,a1,a2)                0
50ae15b995SBarry Smith #define PetscInfo3(A,S,a1,a2,a3)             0
51ae15b995SBarry Smith #define PetscInfo4(A,S,a1,a2,a3,a4)          0
52ae15b995SBarry Smith #define PetscInfo5(A,S,a1,a2,a3,a4,a5)       0
53ae15b995SBarry Smith #define PetscInfo6(A,S,a1,a2,a3,a4,a5,a6)    0
54ae15b995SBarry Smith #define PetscInfo7(A,S,a1,a2,a3,a4,a5,a6,a7) 0
5563ba0a88SBarry Smith #endif
56014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInfoDeactivateClass(PetscClassId);
57014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscInfoActivateClass(PetscClassId);
58014dd563SJed Brown PETSC_EXTERN PetscBool PetscLogPrintInfo;  /* if true, indicates PetscInfo() is turned on */
59614700edSBarry Smith 
6031d06abdSBarry Smith /* We must make the following structures available to access the event
6131d06abdSBarry Smith      activation flags in the PetscLogEventBegin/End() macros. These are not part of the PETSc public
6231d06abdSBarry Smith      API and are not intended to be used by other parts of PETSc or by users.
63d49d4b11SBarry Smith 
6431d06abdSBarry Smith      The code that manipulates these structures is in src/sys/plog/utils.
6531d06abdSBarry Smith */
66aa213bdcSJed Brown typedef struct _n_PetscIntStack *PetscIntStack;
676a6a9b46SSatish Balay 
68217044c2SLisandro Dalcin /* -----------------------------------------------------------------------------------------------------*/
69c60ec953SBarry Smith /*
70aa213bdcSJed Brown     PetscClassRegInfo, PetscClassPerfInfo - Each class has two data structures associated with it. The first has
71c60ec953SBarry Smith        static information about it, the second collects statistics on how many objects of the class are created,
72c60ec953SBarry Smith        how much memory they use, etc.
73c60ec953SBarry Smith 
74aa213bdcSJed Brown     PetscClassRegLog, PetscClassPerfLog - arrays of the PetscClassRegInfo and PetscClassPerfInfo for all classes.
75c60ec953SBarry Smith */
766a6a9b46SSatish Balay typedef struct  {
77c60ec953SBarry Smith   char           *name;   /* The class name */
78c60ec953SBarry Smith   PetscClassId   classid; /* The integer identifying this class */
79aa213bdcSJed Brown } PetscClassRegInfo;
806a6a9b46SSatish Balay 
816a6a9b46SSatish Balay typedef struct {
820700a824SBarry Smith   PetscClassId   id;           /* The integer identifying this class */
836a6a9b46SSatish Balay   int            creations;    /* The number of objects of this class created */
846a6a9b46SSatish Balay   int            destructions; /* The number of objects of this class destroyed */
856a6a9b46SSatish Balay   PetscLogDouble mem;          /* The total memory allocated by objects of this class */
866a6a9b46SSatish Balay   PetscLogDouble descMem;      /* The total memory allocated by descendents of these objects */
87aa213bdcSJed Brown } PetscClassPerfInfo;
886a6a9b46SSatish Balay 
89aa213bdcSJed Brown typedef struct _n_PetscClassRegLog *PetscClassRegLog;
90aa213bdcSJed Brown struct _n_PetscClassRegLog {
91c60ec953SBarry Smith   int               numClasses; /* The number of classes registered */
92c60ec953SBarry Smith   int               maxClasses; /* The maximum number of classes */
93aa213bdcSJed Brown   PetscClassRegInfo *classInfo; /* The structure for class information (classids are monotonicly increasing) */
94c60ec953SBarry Smith };
956a6a9b46SSatish Balay 
96aa213bdcSJed Brown typedef struct _n_PetscClassPerfLog *PetscClassPerfLog;
97aa213bdcSJed Brown struct _n_PetscClassPerfLog {
98c60ec953SBarry Smith   int                numClasses; /* The number of logging classes */
99c60ec953SBarry Smith   int                maxClasses; /* The maximum number of classes */
100aa213bdcSJed Brown   PetscClassPerfInfo *classInfo; /* The structure for class information (classids are monotonicly increasing) */
101c60ec953SBarry Smith };
102c60ec953SBarry Smith /* -----------------------------------------------------------------------------------------------------*/
103c60ec953SBarry Smith /*
104aa213bdcSJed Brown     PetscEventRegInfo, PetscEventPerfInfo - Each event has two data structures associated with it. The first has
105c60ec953SBarry Smith        static information about it, the second collects statistics on how many times the event is used, how
106c60ec953SBarry Smith        much time it takes, etc.
107c60ec953SBarry Smith 
108aa213bdcSJed Brown     PetscEventRegLog, PetscEventPerfLog - an array of all PetscEventRegInfo and PetscEventPerfInfo for all events. There is one
109c60ec953SBarry Smith       of these for each stage.
110c60ec953SBarry Smith 
111c60ec953SBarry Smith */
1126a6a9b46SSatish Balay typedef struct {
1136a6a9b46SSatish Balay   char         *name;         /* The name of this event */
114c60ec953SBarry Smith   PetscClassId classid;       /* The class the event is associated with */
115217044c2SLisandro Dalcin   PetscBool    collective;    /* Flag this event as collective */
1166a6a9b46SSatish Balay #if defined (PETSC_HAVE_MPE)
1176a6a9b46SSatish Balay   int          mpe_id_begin; /* MPE IDs that define the event */
1186a6a9b46SSatish Balay   int          mpe_id_end;
1196a6a9b46SSatish Balay #endif
120aa213bdcSJed Brown } PetscEventRegInfo;
1216a6a9b46SSatish Balay 
122c60ec953SBarry Smith typedef struct {
123c60ec953SBarry Smith   int            id;            /* The integer identifying this event */
124ace3abfcSBarry Smith   PetscBool      active;        /* The flag to activate logging */
125ace3abfcSBarry Smith   PetscBool      visible;       /* The flag to print info in summary */
126c60ec953SBarry Smith   int            depth;         /* The nesting depth of the event call */
127c60ec953SBarry Smith   int            count;         /* The number of times this event was executed */
128a42e5b54SMatthew G Knepley   PetscLogDouble flops, flops2, flopsTmp; /* The flops and flops^2 used in this event */
129a42e5b54SMatthew G Knepley   PetscLogDouble time, time2, timeTmp;    /* The time and time^2 taken for this event */
130217044c2SLisandro Dalcin   PetscLogDouble syncTime;                /* The synchronization barrier time */
131c60ec953SBarry Smith   PetscLogDouble numMessages;   /* The number of messages in this event */
132c60ec953SBarry Smith   PetscLogDouble messageLength; /* The total message lengths in this event */
133c60ec953SBarry Smith   PetscLogDouble numReductions; /* The number of reductions in this event */
134aa213bdcSJed Brown } PetscEventPerfInfo;
135c60ec953SBarry Smith 
136aa213bdcSJed Brown typedef struct _n_PetscEventRegLog *PetscEventRegLog;
137aa213bdcSJed Brown struct _n_PetscEventRegLog {
1386a6a9b46SSatish Balay   int               numEvents;  /* The number of registered events */
1396a6a9b46SSatish Balay   int               maxEvents;  /* The maximum number of events */
140aa213bdcSJed Brown   PetscEventRegInfo *eventInfo; /* The registration information for each event */
1416a6a9b46SSatish Balay };
1426a6a9b46SSatish Balay 
143aa213bdcSJed Brown typedef struct _n_PetscEventPerfLog *PetscEventPerfLog;
144aa213bdcSJed Brown struct _n_PetscEventPerfLog {
1456a6a9b46SSatish Balay   int                numEvents;  /* The number of logging events */
1466a6a9b46SSatish Balay   int                maxEvents;  /* The maximum number of events */
147aa213bdcSJed Brown   PetscEventPerfInfo *eventInfo; /* The performance information for each event */
1486a6a9b46SSatish Balay };
149c60ec953SBarry Smith /* ------------------------------------------------------------------------------------------------------------*/
150c60ec953SBarry Smith /*
151aa213bdcSJed Brown    PetscStageInfo - Contains all the information about a particular stage.
1526a6a9b46SSatish Balay 
153aa213bdcSJed Brown    PetscStageLog - An array of PetscStageInfo for each registered stage. There is a single one of these in the code.
154c60ec953SBarry Smith */
155aa213bdcSJed Brown typedef struct _PetscStageInfo {
1566a6a9b46SSatish Balay   char               *name;     /* The stage name */
157ace3abfcSBarry Smith   PetscBool          used;      /* The stage was pushed on this processor */
158aa213bdcSJed Brown   PetscEventPerfInfo perfInfo;  /* The stage performance information */
159aa213bdcSJed Brown   PetscEventPerfLog  eventLog;  /* The event information for this stage */
160aa213bdcSJed Brown   PetscClassPerfLog  classLog;  /* The class information for this stage */
161aa213bdcSJed Brown } PetscStageInfo;
1626a6a9b46SSatish Balay 
163aa213bdcSJed Brown typedef struct _n_PetscStageLog *PetscStageLog;
164aa213bdcSJed Brown struct _n_PetscStageLog {
1656a6a9b46SSatish Balay   int              numStages;   /* The number of registered stages */
1666a6a9b46SSatish Balay   int              maxStages;   /* The maximum number of stages */
167aa213bdcSJed Brown   PetscIntStack    stack;       /* The stack for active stages */
168aa213bdcSJed Brown   int              curStage;    /* The current stage (only used in macros so we don't call PetscIntStackTop) */
169aa213bdcSJed Brown   PetscStageInfo   *stageInfo;  /* The information for each stage */
170aa213bdcSJed Brown   PetscEventRegLog eventLog;    /* The registered events */
171aa213bdcSJed Brown   PetscClassRegLog classLog;    /* The registered classes */
1726a6a9b46SSatish Balay };
173217044c2SLisandro Dalcin /* -----------------------------------------------------------------------------------------------------*/
1746a6a9b46SSatish Balay 
1753bb1ff40SBarry Smith PETSC_EXTERN PetscErrorCode PetscLogObjectParent(PetscObject,PetscObject);
1763bb1ff40SBarry Smith PETSC_EXTERN PetscErrorCode PetscLogObjectMemory(PetscObject,PetscLogDouble);
1773bb1ff40SBarry Smith 
178fee518ebSJed Brown 
179aa482453SBarry Smith #if defined(PETSC_USE_LOG)  /* --- Logging is turned on --------------------------------*/
180aeeda9f6SJohn Fettig PETSC_EXTERN PetscStageLog petsc_stageLog;
181*f5d6ab90SLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscLogGetStageLog(PetscStageLog*);
182*f5d6ab90SLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscStageLogGetCurrent(PetscStageLog,int*);
183*f5d6ab90SLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscStageLogGetEventPerfLog(PetscStageLog,int,PetscEventPerfLog*);
184614700edSBarry Smith 
185da63de55SLois Curfman McInnes /*
186da63de55SLois Curfman McInnes    Flop counting:  We count each arithmetic operation (e.g., addition, multiplication) separately.
187da63de55SLois Curfman McInnes 
188da63de55SLois Curfman McInnes    For the complex numbers version, note that
189da63de55SLois Curfman McInnes        1 complex addition = 2 flops
190da63de55SLois Curfman McInnes        1 complex multiplication = 6 flops,
191da63de55SLois Curfman McInnes    where we define 1 flop as that for a double precision scalar.  We roughly approximate
192da63de55SLois Curfman McInnes    flop counting for complex numbers by multiplying the total flops by 4; this corresponds
193da63de55SLois Curfman McInnes    to the assumption that we're counting mostly additions and multiplications -- and
194da63de55SLois Curfman McInnes    roughly the same number of each.  More accurate counting could be done by distinguishing
195da63de55SLois Curfman McInnes    among the various arithmetic operations.
196da63de55SLois Curfman McInnes  */
197da63de55SLois Curfman McInnes 
198aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
199542d4b3fSSatish Balay #define PETSC_FLOPS_PER_OP 4.0
200da63de55SLois Curfman McInnes #else
201542d4b3fSSatish Balay #define PETSC_FLOPS_PER_OP 1.0
202bf3909cdSBarry Smith #endif
203bf3909cdSBarry Smith 
204dd19b90bSBarry Smith PETSC_STATIC_INLINE PetscErrorCode PetscLogFlops(PetscLogDouble n)
205dd19b90bSBarry Smith {
206dd19b90bSBarry Smith   PetscFunctionBegin;
207bf3909cdSBarry Smith #if defined(PETSC_USE_DEBUG)
208dd19b90bSBarry Smith   if (n < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Cannot log negative flops");
209da63de55SLois Curfman McInnes #endif
210dd19b90bSBarry Smith   petsc_TotalFlops += PETSC_FLOPS_PER_OP*n;
211dd19b90bSBarry Smith   PetscFunctionReturn(0);
212dd19b90bSBarry Smith }
2133906def6SBarry Smith PETSC_EXTERN PetscErrorCode PetscLogSetThreshold(PetscLogDouble,PetscLogDouble*);
21477c4ece6SBarry Smith 
215aa482453SBarry Smith #if defined (PETSC_HAVE_MPE)
216014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogMPEBegin(void);
217014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogMPEDump(const char[]);
21877c4ece6SBarry Smith #endif
21977c4ece6SBarry Smith 
220014dd563SJed Brown PETSC_EXTERN PetscErrorCode (*PetscLogPLB)(PetscLogEvent,int,PetscObject,PetscObject,PetscObject,PetscObject);
221014dd563SJed Brown PETSC_EXTERN PetscErrorCode (*PetscLogPLE)(PetscLogEvent,int,PetscObject,PetscObject,PetscObject,PetscObject);
222014dd563SJed Brown PETSC_EXTERN PetscErrorCode (*PetscLogPHC)(PetscObject);
223014dd563SJed Brown PETSC_EXTERN PetscErrorCode (*PetscLogPHD)(PetscObject);
22477c4ece6SBarry Smith 
2253bb1ff40SBarry Smith #define PetscLogObjectParents(p,n,d)  0;{int _i; for (_i=0; _i<n; _i++) {ierr = PetscLogObjectParent((PetscObject)p,(PetscObject)(d)[_i]);CHKERRQ(ierr);}}
2268b5db460SBarry Smith #define PetscLogObjectCreate(h)      ((PetscLogPHC) ? (*PetscLogPHC)((PetscObject)h) : 0)
2278b5db460SBarry Smith #define PetscLogObjectDestroy(h)     ((PetscLogPHD) ? (*PetscLogPHD)((PetscObject)h) : 0)
2288ba1e511SMatthew Knepley /* Initialization functions */
229bb1d7374SBarry Smith PETSC_EXTERN PetscErrorCode PetscLogDefaultBegin(void);
230014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogAllBegin(void);
231bb1d7374SBarry Smith PETSC_EXTERN PetscErrorCode PetscLogNestedBegin(void);
232014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogTraceBegin(FILE *);
233014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogActions(PetscBool);
234014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogObjects(PetscBool);
235fe866b2dSBarry Smith 
2368ba1e511SMatthew Knepley /* General functions */
237014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogDestroy(void);
238014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogSet(PetscErrorCode (*)(int, int, PetscObject, PetscObject, PetscObject, PetscObject),
2396849ba73SBarry Smith                                         PetscErrorCode (*)(int, int, PetscObject, PetscObject, PetscObject, PetscObject));
240014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogObjectState(PetscObject, const char[], ...);
2418ba1e511SMatthew Knepley /* Output functions */
242014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogView(PetscViewer);
243f14045dbSBarry Smith PETSC_EXTERN PetscErrorCode PetscLogViewFromOptions(void);
244014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogDump(const char[]);
24531d06abdSBarry Smith 
246014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGetFlops(PetscLogDouble *);
24731d06abdSBarry Smith 
248014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogStageRegister(const char[],PetscLogStage*);
249014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogStagePush(PetscLogStage);
250014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogStagePop(void);
251014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogStageSetActive(PetscLogStage, PetscBool );
252014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogStageGetActive(PetscLogStage, PetscBool  *);
253014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogStageSetVisible(PetscLogStage, PetscBool );
254014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogStageGetVisible(PetscLogStage, PetscBool  *);
255014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogStageGetId(const char [], PetscLogStage *);
256*f5d6ab90SLisandro Dalcin 
2578ba1e511SMatthew Knepley /* Event functions */
258014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogEventRegister(const char[], PetscClassId,PetscLogEvent*);
259217044c2SLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscLogEventSetCollective(PetscLogEvent,PetscBool);
260014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogEventActivate(PetscLogEvent);
261014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogEventDeactivate(PetscLogEvent);
262014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogEventSetActiveAll(PetscLogEvent, PetscBool );
263014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogEventActivateClass(PetscClassId);
264014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogEventDeactivateClass(PetscClassId);
2658c49a8d9SBarry Smith PETSC_EXTERN PetscErrorCode PetscLogEventGetId(const char[],PetscLogEvent*);
26613230571SMichael Lange PETSC_EXTERN PetscErrorCode PetscLogEventGetPerfInfo(int, PetscLogEvent, PetscEventPerfInfo *);
2678ba1e511SMatthew Knepley 
2688ba1e511SMatthew Knepley /* Global counters */
269014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_irecv_ct;
270014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_isend_ct;
271014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_recv_ct;
272014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_send_ct;
273014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_irecv_len;
274014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_isend_len;
275014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_recv_len;
276014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_send_len;
277014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_allreduce_ct;
278014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_gather_ct;
279014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_scatter_ct;
280014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_wait_ct;
281014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_wait_any_ct;
282014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_wait_all_ct;
283014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_sum_of_waits_ct;
2848ba1e511SMatthew Knepley 
285217044c2SLisandro Dalcin PETSC_EXTERN PetscBool PetscLogSyncOn;
28677c4ece6SBarry Smith 
28752e6d16bSBarry Smith #define PetscLogEventBegin(e,o1,o2,o3,o4) \
2888b5db460SBarry Smith   (((PetscLogPLB && petsc_stageLog->stageInfo[petsc_stageLog->curStage].perfInfo.active && petsc_stageLog->stageInfo[petsc_stageLog->curStage].eventLog->eventInfo[e].active) ? \
289495fc317SBarry Smith     (*PetscLogPLB)((e),0,(PetscObject)(o1),(PetscObject)(o2),(PetscObject)(o3),(PetscObject)(o4)) : 0 ))
290043328b6SSatish Balay 
29152e6d16bSBarry Smith #define PetscLogEventEnd(e,o1,o2,o3,o4) \
2928b5db460SBarry Smith   (((PetscLogPLE && petsc_stageLog->stageInfo[petsc_stageLog->curStage].perfInfo.active && petsc_stageLog->stageInfo[petsc_stageLog->curStage].eventLog->eventInfo[e].active) ? \
293495fc317SBarry Smith     (*PetscLogPLE)((e),0,(PetscObject)(o1),(PetscObject)(o2),(PetscObject)(o3),(PetscObject)(o4)) : 0 ))
29477c4ece6SBarry Smith 
295014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogEventGetFlops(PetscLogEvent, PetscLogDouble*);
296014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogEventZeroFlops(PetscLogEvent);
297003131ecSBarry Smith 
298ce85283eSBarry Smith /*
299f621e05eSBarry Smith      These are used internally in the PETSc routines to keep a count of MPI messages and
300f621e05eSBarry Smith    their sizes.
301f621e05eSBarry Smith 
302af0996ceSBarry Smith      This does not work for MPI-Uni because our include/petsc/mpiuni/mpi.h file
303ce85283eSBarry Smith    uses macros to defined the MPI operations.
30415308354SBarry Smith 
30515308354SBarry Smith      It does not work correctly from HP-UX because it processes the
306bb4af37aSBarry Smith    macros in a way that sometimes it double counts, hence
307b6410449SSatish Balay    PETSC_HAVE_BROKEN_RECURSIVE_MACRO
3087c1e34a4SSatish Balay 
309f569fd43SBarry Smith      It does not work with Windows because winmpich lacks MPI_Type_size()
310ce85283eSBarry Smith */
311c8217ed5SSatish Balay #if !defined(__MPIUNI_H) && !defined(PETSC_HAVE_BROKEN_RECURSIVE_MACRO) && !defined (PETSC_HAVE_MPI_MISSING_TYPESIZE)
31277a39924SBarry Smith /*
31377a39924SBarry Smith    Logging of MPI activities
31477a39924SBarry Smith */
315ad39c06fSJed Brown PETSC_STATIC_INLINE PetscErrorCode PetscMPITypeSize(PetscLogDouble *buff,PetscMPIInt count,MPI_Datatype type)
316f95db71bSBarry Smith {
3177d30fa4aSBarry Smith   PetscMPIInt mysize;
3189c72ff1eSSatish Balay   PetscErrorCode _myierr;
3197d30fa4aSBarry Smith   if (type == MPI_DATATYPE_NULL) return 0;
3209c72ff1eSSatish Balay   _myierr = MPI_Type_size(type,&mysize);CHKERRQ(_myierr);
3219c72ff1eSSatish Balay   *buff += (PetscLogDouble) (count*mysize);
3229c72ff1eSSatish Balay   return 0;
323f95db71bSBarry Smith }
32477a39924SBarry Smith 
325ad39c06fSJed Brown PETSC_STATIC_INLINE PetscErrorCode PetscMPITypeSizeComm(MPI_Comm comm, PetscLogDouble *buff,PetscMPIInt *counts,MPI_Datatype type)
3263b9284c0SMatthew G Knepley {
3273b9284c0SMatthew G Knepley   PetscMPIInt mysize, commsize, p;
3283b9284c0SMatthew G Knepley   PetscErrorCode _myierr;
3293b9284c0SMatthew G Knepley 
3307d30fa4aSBarry Smith   if (type == MPI_DATATYPE_NULL) return 0;
3313b9284c0SMatthew G Knepley   _myierr = MPI_Comm_size(comm,&commsize);CHKERRQ(_myierr);
3323b9284c0SMatthew G Knepley   _myierr = MPI_Type_size(type,&mysize);CHKERRQ(_myierr);
3333b9284c0SMatthew G Knepley   for (p = 0; p < commsize; ++p) {
3343b9284c0SMatthew G Knepley     *buff += (PetscLogDouble) (counts[p]*mysize);
3353b9284c0SMatthew G Knepley   }
3363b9284c0SMatthew G Knepley   return 0;
3373b9284c0SMatthew G Knepley }
3383b9284c0SMatthew G Knepley 
33972b11a25SBarry Smith /*
34072b11a25SBarry Smith     Returns 1 if the communicator is parallel else zero
34172b11a25SBarry Smith */
34272b11a25SBarry Smith PETSC_STATIC_INLINE int PetscMPIParallelComm(MPI_Comm comm)
34372b11a25SBarry Smith {
34472b11a25SBarry Smith   PetscMPIInt size; MPI_Comm_size(comm,&size); return size > 1;
34572b11a25SBarry Smith }
34672b11a25SBarry Smith 
34777a39924SBarry Smith #define MPI_Irecv(buf,count,datatype,source,tag,comm,request) \
3485fa69175SJed Brown   ((petsc_irecv_ct++,0) || PetscMPITypeSize(&(petsc_irecv_len),(count),(datatype)) || MPI_Irecv((buf),(count),(datatype),(source),(tag),(comm),(request)))
34915308354SBarry Smith 
35077a39924SBarry Smith #define MPI_Isend(buf,count,datatype,dest,tag,comm,request) \
3515fa69175SJed Brown   ((petsc_isend_ct++,0) || PetscMPITypeSize(&(petsc_isend_len),(count),(datatype)) || MPI_Isend((buf),(count),(datatype),(dest),(tag),(comm),(request)))
35215308354SBarry Smith 
3530d4b0b6cSBarry Smith #define MPI_Startall_irecv(count,number,requests) \
3545fa69175SJed Brown   ((petsc_irecv_ct += (PetscLogDouble)(number),0) || PetscMPITypeSize(&(petsc_irecv_len),(count),(MPIU_SCALAR)) || MPI_Startall((number),(requests)))
3550d4b0b6cSBarry Smith 
3560d4b0b6cSBarry Smith #define MPI_Startall_isend(count,number,requests) \
3575fa69175SJed Brown   ((petsc_isend_ct += (PetscLogDouble)(number),0) || PetscMPITypeSize(&(petsc_isend_len),(count),(MPIU_SCALAR)) || MPI_Startall((number),(requests)))
3580d4b0b6cSBarry Smith 
3590d4b0b6cSBarry Smith #define MPI_Start_isend(count,requests) \
3605fa69175SJed Brown   ((petsc_isend_ct++,0) || PetscMPITypeSize((&petsc_isend_len),(count),(MPIU_SCALAR)) || MPI_Start((requests)))
3610d4b0b6cSBarry Smith 
362ce85283eSBarry Smith #define MPI_Recv(buf,count,datatype,source,tag,comm,status) \
3635fa69175SJed Brown   ((petsc_recv_ct++,0) || PetscMPITypeSize((&petsc_recv_len),(count),(datatype)) || MPI_Recv((buf),(count),(datatype),(source),(tag),(comm),(status)))
36415308354SBarry Smith 
36577a39924SBarry Smith #define MPI_Send(buf,count,datatype,dest,tag,comm) \
3665fa69175SJed Brown   ((petsc_send_ct++,0) || PetscMPITypeSize((&petsc_send_len),(count),(datatype)) || MPI_Send((buf),(count),(datatype),(dest),(tag),(comm)))
36777a39924SBarry Smith 
36877a39924SBarry Smith #define MPI_Wait(request,status) \
3695fa69175SJed Brown   ((petsc_wait_ct++,petsc_sum_of_waits_ct++,0) || MPI_Wait((request),(status)))
37077a39924SBarry Smith 
37177a39924SBarry Smith #define MPI_Waitany(a,b,c,d) \
3725fa69175SJed Brown   ((petsc_wait_any_ct++,petsc_sum_of_waits_ct++,0) || MPI_Waitany((a),(b),(c),(d)))
37377a39924SBarry Smith 
37477a39924SBarry Smith #define MPI_Waitall(count,array_of_requests,array_of_statuses) \
3755fa69175SJed Brown   ((petsc_wait_all_ct++,petsc_sum_of_waits_ct += (PetscLogDouble) (count),0) || MPI_Waitall((count),(array_of_requests),(array_of_statuses)))
37677a39924SBarry Smith 
37777a39924SBarry Smith #define MPI_Allreduce(sendbuf,recvbuf,count,datatype,op,comm) \
3785fa69175SJed Brown   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || MPI_Allreduce((sendbuf),(recvbuf),(count),(datatype),(op),(comm)))
3793914022bSBarry Smith 
38054fe5c21SBarry Smith #define MPI_Bcast(buffer,count,datatype,root,comm) \
3815fa69175SJed Brown   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || MPI_Bcast((buffer),(count),(datatype),(root),(comm)))
38254fe5c21SBarry Smith 
383d6e4c47cSJed Brown #define MPI_Reduce_scatter_block(sendbuf,recvbuf,recvcount,datatype,op,comm) \
3845fa69175SJed Brown   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || MPI_Reduce_scatter_block((sendbuf),(recvbuf),(recvcount),(datatype),(op),(comm)))
385d6e4c47cSJed Brown 
3863b9284c0SMatthew G Knepley #define MPI_Alltoall(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,comm) \
3875fa69175SJed Brown   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || PetscMPITypeSize((&petsc_send_len),(sendcount),(sendtype)) || MPI_Alltoall((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(comm)))
3883b9284c0SMatthew G Knepley 
3893b9284c0SMatthew G Knepley #define MPI_Alltoallv(sendbuf,sendcnts,sdispls,sendtype,recvbuf,recvcnts,rdispls,recvtype,comm) \
3905fa69175SJed Brown   ((petsc_allreduce_ct += PetscMPIParallelComm((comm)),0) || PetscMPITypeSizeComm((comm),(&petsc_send_len),(sendcnts),(sendtype)) || MPI_Alltoallv((sendbuf),(sendcnts),(sdispls),(sendtype),(recvbuf),(recvcnts),(rdispls),(recvtype),(comm)))
3913b9284c0SMatthew G Knepley 
39201faf4e4SMatthew Knepley #define MPI_Allgather(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,comm) \
3935fa69175SJed Brown   ((petsc_gather_ct += PetscMPIParallelComm((comm)),0) || MPI_Allgather((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(comm)))
39401faf4e4SMatthew Knepley 
39501faf4e4SMatthew Knepley #define MPI_Allgatherv(sendbuf,sendcount,sendtype,recvbuf,recvcount,displs,recvtype,comm) \
3965fa69175SJed Brown   ((petsc_gather_ct += PetscMPIParallelComm((comm)),0) || MPI_Allgatherv((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(displs),(recvtype),(comm)))
39701faf4e4SMatthew Knepley 
39801faf4e4SMatthew Knepley #define MPI_Gather(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,root,comm) \
3995fa69175SJed Brown   ((petsc_gather_ct++,0) || PetscMPITypeSize((&petsc_send_len),(sendcount),(sendtype)) || MPI_Gather((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm)))
40001faf4e4SMatthew Knepley 
40101faf4e4SMatthew Knepley #define MPI_Gatherv(sendbuf,sendcount,sendtype,recvbuf,recvcount,displs,recvtype,root,comm) \
4025fa69175SJed Brown   ((petsc_gather_ct++,0) || PetscMPITypeSize((&petsc_send_len),(sendcount),(sendtype)) || MPI_Gatherv((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(displs),(recvtype),(root),(comm)))
40301faf4e4SMatthew Knepley 
40401faf4e4SMatthew Knepley #define MPI_Scatter(sendbuf,sendcount,sendtype,recvbuf,recvcount,recvtype,root,comm) \
4055fa69175SJed Brown   ((petsc_scatter_ct++,0) || PetscMPITypeSize((&petsc_recv_len),(recvcount),(recvtype)) || MPI_Scatter((sendbuf),(sendcount),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm)))
40601faf4e4SMatthew Knepley 
40701faf4e4SMatthew Knepley #define MPI_Scatterv(sendbuf,sendcount,displs,sendtype,recvbuf,recvcount,recvtype,root,comm) \
4085fa69175SJed Brown   ((petsc_scatter_ct++,0) || PetscMPITypeSize((&petsc_recv_len),(recvcount),(recvtype)) || MPI_Scatterv((sendbuf),(sendcount),(displs),(sendtype),(recvbuf),(recvcount),(recvtype),(root),(comm)))
40901faf4e4SMatthew Knepley 
4100d4b0b6cSBarry Smith #else
4110d4b0b6cSBarry Smith 
4120d4b0b6cSBarry Smith #define MPI_Startall_irecv(count,number,requests) \
4135fa69175SJed Brown   (MPI_Startall((number),(requests)))
4140d4b0b6cSBarry Smith 
4150d4b0b6cSBarry Smith #define MPI_Startall_isend(count,number,requests) \
4165fa69175SJed Brown   (MPI_Startall((number),(requests)))
4170d4b0b6cSBarry Smith 
4180d4b0b6cSBarry Smith #define MPI_Start_isend(count,requests) \
4195fa69175SJed Brown   (MPI_Start((requests)))
4200d4b0b6cSBarry Smith 
421c8217ed5SSatish Balay #endif /* !__MPIUNI_H && ! PETSC_HAVE_BROKEN_RECURSIVE_MACRO */
422614700edSBarry Smith 
423df8cf0b5SBarry Smith #else  /* ---Logging is turned off --------------------------------------------*/
424614700edSBarry Smith 
425b0a32e0cSBarry Smith #define PetscLogFlops(n)                   0
4263906def6SBarry Smith #define PetscLogSetThreshold(a,b)          0
427614700edSBarry Smith 
428*f5d6ab90SLisandro Dalcin #define PetscLogStageSetActive(a,b)        0
429*f5d6ab90SLisandro Dalcin #define PetscLogStageGetActive(a,b)        0
430*f5d6ab90SLisandro Dalcin #define PetscLogStageGetVisible(a,b)       0
431*f5d6ab90SLisandro Dalcin #define PetscLogStageSetVisible(a,b)       0
432*f5d6ab90SLisandro Dalcin #define PetscLogStageGetId(a,b)            (*(b)=0,0)
433*f5d6ab90SLisandro Dalcin 
434b0a32e0cSBarry Smith #define PetscLogEventActivate(a)           0
435b0a32e0cSBarry Smith #define PetscLogEventDeactivate(a)         0
436b0a32e0cSBarry Smith #define PetscLogEventActivateClass(a)      0
437b0a32e0cSBarry Smith #define PetscLogEventDeactivateClass(a)    0
438e8e7597cSSatish Balay #define PetscLogEventSetActiveAll(a,b)     0
43946eb3cd7SSatish Balay #define PetscLogEventGetId(a,b)            (*(b)=0,0)
440561b2a67SSatish Balay #define PetscLogEventGetPerfInfo(a,b,c)    0
44177c4ece6SBarry Smith 
4428b5db460SBarry Smith #define PetscLogPLB                        0
4438b5db460SBarry Smith #define PetscLogPLE                        0
4448b5db460SBarry Smith #define PetscLogPHC                        0
4458b5db460SBarry Smith #define PetscLogPHD                        0
446*f5d6ab90SLisandro Dalcin 
44799de4ba8SSatish Balay #define PetscGetFlops(a)                   (*(a) = 0.0,0)
448b0a32e0cSBarry Smith #define PetscLogEventBegin(e,o1,o2,o3,o4)  0
449b0a32e0cSBarry Smith #define PetscLogEventEnd(e,o1,o2,o3,o4)    0
450efee365bSSatish Balay #define PetscLogObjectParents(p,n,c)       0
45152e6d16bSBarry Smith #define PetscLogObjectCreate(h)            0
45252e6d16bSBarry Smith #define PetscLogObjectDestroy(h)           0
453b0a32e0cSBarry Smith #define PetscLogDestroy()                  0
454*f5d6ab90SLisandro Dalcin #define PetscLogStageRegister(a,b)         0
455b0a32e0cSBarry Smith #define PetscLogStagePush(a)               0
456b0a32e0cSBarry Smith #define PetscLogStagePop()                 0
45791eabc43SBarry Smith #define PetscLogView(viewer)               0
458f14045dbSBarry Smith #define PetscLogViewFromOptions()          0
459bb1d7374SBarry Smith #define PetscLogDefaultBegin()             0
460b0a32e0cSBarry Smith #define PetscLogTraceBegin(file)           0
461b0a32e0cSBarry Smith #define PetscLogSet(lb,le)                 0
462b0a32e0cSBarry Smith #define PetscLogAllBegin()                 0
463bb1d7374SBarry Smith #define PetscLogNestedBegin()              0
464b0a32e0cSBarry Smith #define PetscLogDump(c)                    0
465043328b6SSatish Balay #define PetscLogEventRegister(a,b,c)       0
466217044c2SLisandro Dalcin #define PetscLogEventSetCollective(a,b)    0
46773fda44aSBarry Smith #define PetscLogObjects(a)                 0
46873fda44aSBarry Smith #define PetscLogActions(a)                 0
469014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogObjectState(PetscObject,const char[],...);
470ce6058e1SBarry Smith 
471aa482453SBarry Smith /* If PETSC_USE_LOG is NOT defined, these still need to be! */
472ca161407SBarry Smith #define MPI_Startall_irecv(count,number,requests) MPI_Startall(number,requests)
473ca161407SBarry Smith #define MPI_Startall_isend(count,number,requests) MPI_Startall(number,requests)
474ca161407SBarry Smith #define MPI_Start_isend(count,requests)           MPI_Start(requests)
475f141ce34SMatthew Knepley 
476aa482453SBarry Smith #endif   /* PETSC_USE_LOG */
4776daaf66cSBarry Smith 
478204a6943SJed Brown #define PetscPreLoadBegin(flag,name) \
479204a6943SJed Brown do {\
480204a6943SJed Brown   PetscBool      PetscPreLoading = flag;\
481204a6943SJed Brown   int            PetscPreLoadMax,PetscPreLoadIt;\
482166c7f25SBarry Smith   PetscLogStage  _stageNum;\
4838cbcd9ccSBarry Smith   PetscErrorCode _3_ierr; \
484c5929fdfSBarry Smith   _3_ierr = PetscOptionsGetBool(NULL,NULL,"-preload",&PetscPreLoading,NULL);CHKERRQ(_3_ierr); \
485204a6943SJed Brown   PetscPreLoadMax = (int)(PetscPreLoading);\
486204a6943SJed Brown   PetscPreLoadingUsed = PetscPreLoading ? PETSC_TRUE : PetscPreLoadingUsed;\
487204a6943SJed Brown   for (PetscPreLoadIt=0; PetscPreLoadIt<=PetscPreLoadMax; PetscPreLoadIt++) {\
488204a6943SJed Brown     PetscPreLoadingOn = PetscPreLoading;\
4890298fd71SBarry Smith     _3_ierr = PetscBarrier(NULL);CHKERRQ(_3_ierr);\
490204a6943SJed Brown     if (PetscPreLoadIt>0) {\
4918e58c17dSMatthew Knepley       _3_ierr = PetscLogStageGetId(name,&_stageNum);CHKERRQ(_3_ierr);\
4928e58c17dSMatthew Knepley     } else {\
493a3bc4eb9SBarry Smith       _3_ierr = PetscLogStageRegister(name,&_stageNum);CHKERRQ(_3_ierr); \
4948e58c17dSMatthew Knepley     }\
495204a6943SJed Brown     _3_ierr = PetscLogStageSetActive(_stageNum,(PetscBool)(!PetscPreLoadMax || PetscPreLoadIt));\
4968e58c17dSMatthew Knepley     _3_ierr = PetscLogStagePush(_stageNum);CHKERRQ(_3_ierr);
4978e58c17dSMatthew Knepley 
498204a6943SJed Brown #define PetscPreLoadEnd() \
499043328b6SSatish Balay     _3_ierr = PetscLogStagePop();CHKERRQ(_3_ierr);\
500204a6943SJed Brown     PetscPreLoading = PETSC_FALSE;\
501043328b6SSatish Balay   }\
502204a6943SJed Brown } while (0)
5038e58c17dSMatthew Knepley 
504204a6943SJed Brown #define PetscPreLoadStage(name) do {                                         \
505043328b6SSatish Balay     _3_ierr = PetscLogStagePop();CHKERRQ(_3_ierr);                      \
506204a6943SJed Brown     if (PetscPreLoadIt>0) {                                                  \
5078e58c17dSMatthew Knepley       _3_ierr = PetscLogStageGetId(name,&_stageNum);CHKERRQ(_3_ierr);   \
5088e58c17dSMatthew Knepley     } else {                                                            \
509a3bc4eb9SBarry Smith       _3_ierr = PetscLogStageRegister(name,&_stageNum);CHKERRQ(_3_ierr); \
5108e58c17dSMatthew Knepley     }                                                                   \
511204a6943SJed Brown     _3_ierr = PetscLogStageSetActive(_stageNum,(PetscBool)(!PetscPreLoadMax || PetscPreLoadIt)); \
512204a6943SJed Brown     _3_ierr = PetscLogStagePush(_stageNum);CHKERRQ(_3_ierr);            \
513204a6943SJed Brown   } while (0)
514e9fa29b7SSatish Balay 
515ad14c47eSJed Brown /* some vars for logging */
516ad14c47eSJed Brown PETSC_EXTERN PetscBool PetscPreLoadingUsed;       /* true if we are or have done preloading */
517ad14c47eSJed Brown PETSC_EXTERN PetscBool PetscPreLoadingOn;         /* true if we are currently in a preloading calculation */
518ad14c47eSJed Brown 
51997bb86f7SLois Curfman McInnes #endif
520