xref: /petsc/include/petsclog.h (revision d71ae5a4db6382e7f06317b8d368875286fe9008)
197bb86f7SLois Curfman McInnes /*
27588ac45SBarry Smith     Defines profile/logging in PETSc.
397bb86f7SLois Curfman McInnes */
46524c165SJacob Faibussowitsch #ifndef PETSCLOG_H
526bd1501SBarry Smith #define PETSCLOG_H
6ac09b921SBarry Smith 
7aaa7dc30SBarry Smith #include <petscsys.h>
8958c4211Shannah_mairs #include <petsctime.h>
9c8d78d4dSSatish Balay 
10ac09b921SBarry Smith /* SUBMANSEC = Sys */
11ac09b921SBarry Smith 
12fa2bb9feSLisandro Dalcin /* General logging of information; different from event logging */
133ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscInfo_Private(const char[], PetscObject, const char[], ...) PETSC_ATTRIBUTE_FORMAT(3, 4);
14fa2bb9feSLisandro Dalcin #if defined(PETSC_USE_INFO)
157d3de750SJacob Faibussowitsch   #define PetscInfo(A, ...) PetscInfo_Private(PETSC_FUNCTION_NAME, ((PetscObject)A), __VA_ARGS__)
16fa2bb9feSLisandro Dalcin #else
177d3de750SJacob Faibussowitsch   #define PetscInfo(A, ...) 0
18fa2bb9feSLisandro Dalcin #endif
19e94e781bSJacob Faibussowitsch 
207d3de750SJacob Faibussowitsch #define PetscInfo1(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use PetscInfo() (since version 3.17)\"") PetscInfo(__VA_ARGS__)
217d3de750SJacob Faibussowitsch #define PetscInfo2(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use PetscInfo() (since version 3.17)\"") PetscInfo(__VA_ARGS__)
225899db6dSLisandro Dalcin #define PetscInfo3(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use PetscInfo() (since version 3.17)\"") PetscInfo(__VA_ARGS__)
237d3de750SJacob Faibussowitsch #define PetscInfo4(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use PetscInfo() (since version 3.17)\"") PetscInfo(__VA_ARGS__)
247d3de750SJacob Faibussowitsch #define PetscInfo5(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use PetscInfo() (since version 3.17)\"") PetscInfo(__VA_ARGS__)
257d3de750SJacob Faibussowitsch #define PetscInfo6(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use PetscInfo() (since version 3.17)\"") PetscInfo(__VA_ARGS__)
267d3de750SJacob Faibussowitsch #define PetscInfo7(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use PetscInfo() (since version 3.17)\"") PetscInfo(__VA_ARGS__)
277d3de750SJacob Faibussowitsch #define PetscInfo8(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use PetscInfo() (since version 3.17)\"") PetscInfo(__VA_ARGS__)
287d3de750SJacob Faibussowitsch #define PetscInfo9(...) PETSC_DEPRECATED_MACRO("GCC warning \"Use PetscInfo() (since version 3.17)\"") PetscInfo(__VA_ARGS__)
297d3de750SJacob Faibussowitsch 
30e94e781bSJacob Faibussowitsch /*E
3187497f52SBarry Smith   PetscInfoCommFlag - Describes the method by which to filter `PetscInfo()` by communicator size
32e94e781bSJacob Faibussowitsch 
338b86dd2eSJacob Faibussowitsch   Used as an input for `PetscInfoSetFilterCommSelf()`
34e94e781bSJacob Faibussowitsch 
358b86dd2eSJacob Faibussowitsch $ `PETSC_INFO_COMM_ALL` - Default uninitialized value. `PetscInfo()` will not filter based on
368b86dd2eSJacob Faibussowitsch communicator size (i.e. will print for all communicators)
378b86dd2eSJacob Faibussowitsch $ `PETSC_INFO_COMM_NO_SELF` - `PetscInfo()` will NOT print for communicators with size = 1 (i.e. *_COMM_SELF)
388b86dd2eSJacob Faibussowitsch $ `PETSC_INFO_COMM_ONLY_SELF` - `PetscInfo()` will ONLY print for communicators with size = 1
39e94e781bSJacob Faibussowitsch 
40e94e781bSJacob Faibussowitsch   Level: intermediate
41e94e781bSJacob Faibussowitsch 
42db781477SPatrick Sanan .seealso: `PetscInfo()`, `PetscInfoSetFromOptions()`, `PetscInfoSetFilterCommSelf()`
43e94e781bSJacob Faibussowitsch E*/
44e94e781bSJacob Faibussowitsch typedef enum {
45e94e781bSJacob Faibussowitsch   PETSC_INFO_COMM_ALL       = -1,
46e94e781bSJacob Faibussowitsch   PETSC_INFO_COMM_NO_SELF   = 0,
47e94e781bSJacob Faibussowitsch   PETSC_INFO_COMM_ONLY_SELF = 1
48e94e781bSJacob Faibussowitsch } PetscInfoCommFlag;
49e94e781bSJacob Faibussowitsch 
50e94e781bSJacob Faibussowitsch PETSC_EXTERN const char *const PetscInfoCommFlags[];
51fa2bb9feSLisandro Dalcin PETSC_EXTERN PetscErrorCode    PetscInfoDeactivateClass(PetscClassId);
52fa2bb9feSLisandro Dalcin PETSC_EXTERN PetscErrorCode    PetscInfoActivateClass(PetscClassId);
53e94e781bSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode    PetscInfoEnabled(PetscClassId, PetscBool *);
54e94e781bSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode    PetscInfoAllow(PetscBool);
55e94e781bSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode    PetscInfoSetFile(const char[], const char[]);
56e94e781bSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode    PetscInfoGetFile(char **, FILE **);
57e94e781bSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode    PetscInfoSetClasses(PetscBool, PetscInt, const char *const *);
58e94e781bSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode    PetscInfoGetClass(const char *, PetscBool *);
59e94e781bSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode    PetscInfoGetInfo(PetscBool *, PetscBool *, PetscBool *, PetscBool *, PetscInfoCommFlag *);
608b86dd2eSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode    PetscInfoProcessClass(const char[], PetscInt, const PetscClassId[]);
61e94e781bSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode    PetscInfoSetFilterCommSelf(PetscInfoCommFlag);
62e94e781bSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode    PetscInfoSetFromOptions(PetscOptions);
63e94e781bSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode    PetscInfoDestroy(void);
64fa2bb9feSLisandro Dalcin PETSC_EXTERN PetscBool         PetscLogPrintInfo; /* if true, indicates PetscInfo() is turned on */
65fa2bb9feSLisandro Dalcin 
66b859824cSBarry Smith /*MC
67b859824cSBarry Smith     PetscLogEvent - id used to identify PETSc or user events which timed portions (blocks of executable)
68b859824cSBarry Smith      code.
69b859824cSBarry Smith 
70b859824cSBarry Smith     Level: intermediate
71b859824cSBarry Smith 
72db781477SPatrick Sanan .seealso: `PetscLogEventRegister()`, `PetscLogEventBegin()`, `PetscLogEventEnd()`, `PetscLogStage`
73b859824cSBarry Smith M*/
74b859824cSBarry Smith typedef int PetscLogEvent;
75b859824cSBarry Smith 
76b859824cSBarry Smith /*MC
77b859824cSBarry Smith     PetscLogStage - id used to identify user stages (phases, sections) of runs - for logging
78b859824cSBarry Smith 
79b859824cSBarry Smith     Level: intermediate
80b859824cSBarry Smith 
81db781477SPatrick Sanan .seealso: `PetscLogStageRegister()`, `PetscLogStagePush()`, `PetscLogStagePop()`, `PetscLogEvent`
82b859824cSBarry Smith M*/
83b859824cSBarry Smith typedef int PetscLogStage;
84b859824cSBarry Smith 
858ba1e511SMatthew Knepley #define PETSC_EVENT 1311311
86014dd563SJed Brown PETSC_EXTERN PetscLogEvent PETSC_LARGEST_EVENT;
8797bb86f7SLois Curfman McInnes 
8819b02663SBarry Smith /* Global flop counter */
89014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_TotalFlops;
90014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_tmp_flops;
9119b02663SBarry Smith 
9231d06abdSBarry Smith /* We must make the following structures available to access the event
9331d06abdSBarry Smith      activation flags in the PetscLogEventBegin/End() macros. These are not part of the PETSc public
9431d06abdSBarry Smith      API and are not intended to be used by other parts of PETSc or by users.
95d49d4b11SBarry Smith 
96fa2bb9feSLisandro Dalcin      The code that manipulates these structures is in src/sys/logging/utils.
9731d06abdSBarry Smith */
98aa213bdcSJed Brown typedef struct _n_PetscIntStack *PetscIntStack;
996a6a9b46SSatish Balay 
100217044c2SLisandro Dalcin /* -----------------------------------------------------------------------------------------------------*/
101c60ec953SBarry Smith /*
102aa213bdcSJed Brown     PetscClassRegInfo, PetscClassPerfInfo - Each class has two data structures associated with it. The first has
103c60ec953SBarry Smith        static information about it, the second collects statistics on how many objects of the class are created,
104c60ec953SBarry Smith        how much memory they use, etc.
105c60ec953SBarry Smith 
106aa213bdcSJed Brown     PetscClassRegLog, PetscClassPerfLog - arrays of the PetscClassRegInfo and PetscClassPerfInfo for all classes.
107c60ec953SBarry Smith */
1086a6a9b46SSatish Balay typedef struct {
109c60ec953SBarry Smith   char        *name;    /* The class name */
110c60ec953SBarry Smith   PetscClassId classid; /* The integer identifying this class */
111aa213bdcSJed Brown } PetscClassRegInfo;
1126a6a9b46SSatish Balay 
1136a6a9b46SSatish Balay typedef struct {
1140700a824SBarry Smith   PetscClassId   id;           /* The integer identifying this class */
1156a6a9b46SSatish Balay   int            creations;    /* The number of objects of this class created */
1166a6a9b46SSatish Balay   int            destructions; /* The number of objects of this class destroyed */
1174851f57dSBarry Smith   PetscLogDouble mem;          /* The total memory allocated by objects of this class; this is completely wrong and should possibly be removed */
1184851f57dSBarry Smith   PetscLogDouble descMem;      /* The total memory allocated by descendents of these objects; this is completely wrong and should possibly be removed */
119aa213bdcSJed Brown } PetscClassPerfInfo;
1206a6a9b46SSatish Balay 
121aa213bdcSJed Brown typedef struct _n_PetscClassRegLog *PetscClassRegLog;
122aa213bdcSJed Brown struct _n_PetscClassRegLog {
123c60ec953SBarry Smith   int                numClasses; /* The number of classes registered */
124c60ec953SBarry Smith   int                maxClasses; /* The maximum number of classes */
125aa213bdcSJed Brown   PetscClassRegInfo *classInfo;  /* The structure for class information (classids are monotonicly increasing) */
126c60ec953SBarry Smith };
1276a6a9b46SSatish Balay 
128aa213bdcSJed Brown typedef struct _n_PetscClassPerfLog *PetscClassPerfLog;
129aa213bdcSJed Brown struct _n_PetscClassPerfLog {
130c60ec953SBarry Smith   int                 numClasses; /* The number of logging classes */
131c60ec953SBarry Smith   int                 maxClasses; /* The maximum number of classes */
132aa213bdcSJed Brown   PetscClassPerfInfo *classInfo;  /* The structure for class information (classids are monotonicly increasing) */
133c60ec953SBarry Smith };
134c60ec953SBarry Smith /* -----------------------------------------------------------------------------------------------------*/
135c60ec953SBarry Smith /*
136aa213bdcSJed Brown     PetscEventRegInfo, PetscEventPerfInfo - Each event has two data structures associated with it. The first has
137c60ec953SBarry Smith        static information about it, the second collects statistics on how many times the event is used, how
138c60ec953SBarry Smith        much time it takes, etc.
139c60ec953SBarry Smith 
140aa213bdcSJed Brown     PetscEventRegLog, PetscEventPerfLog - an array of all PetscEventRegInfo and PetscEventPerfInfo for all events. There is one
141c60ec953SBarry Smith       of these for each stage.
142c60ec953SBarry Smith 
143c60ec953SBarry Smith */
1446a6a9b46SSatish Balay typedef struct {
1456a6a9b46SSatish Balay   char        *name;       /* The name of this event */
146c60ec953SBarry Smith   PetscClassId classid;    /* The class the event is associated with */
147217044c2SLisandro Dalcin   PetscBool    collective; /* Flag this event as collective */
1486a6a9b46SSatish Balay #if defined(PETSC_HAVE_MPE)
1496a6a9b46SSatish Balay   int mpe_id_begin; /* MPE IDs that define the event */
1506a6a9b46SSatish Balay   int mpe_id_end;
1516a6a9b46SSatish Balay #endif
152aa213bdcSJed Brown } PetscEventRegInfo;
1536a6a9b46SSatish Balay 
154c60ec953SBarry Smith typedef struct {
155c60ec953SBarry Smith   int            id;                      /* The integer identifying this event */
156ace3abfcSBarry Smith   PetscBool      active;                  /* The flag to activate logging */
157ace3abfcSBarry Smith   PetscBool      visible;                 /* The flag to print info in summary */
158c60ec953SBarry Smith   int            depth;                   /* The nesting depth of the event call */
159c60ec953SBarry Smith   int            count;                   /* The number of times this event was executed */
160a42e5b54SMatthew G Knepley   PetscLogDouble flops, flops2, flopsTmp; /* The flops and flops^2 used in this event */
161a42e5b54SMatthew G Knepley   PetscLogDouble time, time2, timeTmp;    /* The time and time^2 taken for this event */
162217044c2SLisandro Dalcin   PetscLogDouble syncTime;                /* The synchronization barrier time */
163891e75beSMatthew G. Knepley   PetscLogDouble dof[8];                  /* The number of degrees of freedom associated with this event */
1645d68e14cSMatthew G. Knepley   PetscLogDouble errors[8];               /* The errors (user-defined) associated with this event */
165c60ec953SBarry Smith   PetscLogDouble numMessages;             /* The number of messages in this event */
166c60ec953SBarry Smith   PetscLogDouble messageLength;           /* The total message lengths in this event */
167c60ec953SBarry Smith   PetscLogDouble numReductions;           /* The number of reductions in this event */
168e3ed9ee7SBarry Smith   PetscLogDouble memIncrease;             /* How much the resident memory has increased in this event */
169e3ed9ee7SBarry Smith   PetscLogDouble mallocIncrease;          /* How much the maximum malloced space has increased in this event */
170e3ed9ee7SBarry Smith   PetscLogDouble mallocSpace;             /* How much the space was malloced and kept during this event */
171e3ed9ee7SBarry Smith   PetscLogDouble mallocIncreaseEvent;     /* Maximum of the high water mark with in event minus memory available at the end of the event */
172091bc5acSJunchao Zhang #if defined(PETSC_HAVE_DEVICE)
173bec0b493Shannah_mairs   PetscLogDouble CpuToGpuCount; /* The total number of CPU to GPU copies */
174bec0b493Shannah_mairs   PetscLogDouble GpuToCpuCount; /* The total number of GPU to CPU copies */
175bec0b493Shannah_mairs   PetscLogDouble CpuToGpuSize;  /* The total size of CPU to GPU copies */
176bec0b493Shannah_mairs   PetscLogDouble GpuToCpuSize;  /* The total size of GPU to CPU copies */
177958c4211Shannah_mairs   PetscLogDouble GpuFlops;      /* The flops done on a GPU in this event */
178958c4211Shannah_mairs   PetscLogDouble GpuTime;       /* The time spent on a GPU in this event */
179bec0b493Shannah_mairs #endif
180aa213bdcSJed Brown } PetscEventPerfInfo;
181c60ec953SBarry Smith 
182aa213bdcSJed Brown typedef struct _n_PetscEventRegLog *PetscEventRegLog;
183aa213bdcSJed Brown struct _n_PetscEventRegLog {
1846a6a9b46SSatish Balay   int                numEvents; /* The number of registered events */
1856a6a9b46SSatish Balay   int                maxEvents; /* The maximum number of events */
186aa213bdcSJed Brown   PetscEventRegInfo *eventInfo; /* The registration information for each event */
1876a6a9b46SSatish Balay };
1886a6a9b46SSatish Balay 
189aa213bdcSJed Brown typedef struct _n_PetscEventPerfLog *PetscEventPerfLog;
190aa213bdcSJed Brown struct _n_PetscEventPerfLog {
1916a6a9b46SSatish Balay   int                 numEvents; /* The number of logging events */
1926a6a9b46SSatish Balay   int                 maxEvents; /* The maximum number of events */
193aa213bdcSJed Brown   PetscEventPerfInfo *eventInfo; /* The performance information for each event */
1946a6a9b46SSatish Balay };
195c60ec953SBarry Smith /* ------------------------------------------------------------------------------------------------------------*/
196c60ec953SBarry Smith /*
197aa213bdcSJed Brown    PetscStageInfo - Contains all the information about a particular stage.
1986a6a9b46SSatish Balay 
199aa213bdcSJed Brown    PetscStageLog - An array of PetscStageInfo for each registered stage. There is a single one of these in the code.
200c60ec953SBarry Smith */
201aa213bdcSJed Brown typedef struct _PetscStageInfo {
2026a6a9b46SSatish Balay   char              *name;     /* The stage name */
203ace3abfcSBarry Smith   PetscBool          used;     /* The stage was pushed on this processor */
204aa213bdcSJed Brown   PetscEventPerfInfo perfInfo; /* The stage performance information */
205aa213bdcSJed Brown   PetscEventPerfLog  eventLog; /* The event information for this stage */
206aa213bdcSJed Brown   PetscClassPerfLog  classLog; /* The class information for this stage */
207aa213bdcSJed Brown } PetscStageInfo;
2086a6a9b46SSatish Balay 
209aa213bdcSJed Brown typedef struct _n_PetscStageLog *PetscStageLog;
210aa213bdcSJed Brown struct _n_PetscStageLog {
2116a6a9b46SSatish Balay   int              numStages; /* The number of registered stages */
2126a6a9b46SSatish Balay   int              maxStages; /* The maximum number of stages */
213aa213bdcSJed Brown   PetscIntStack    stack;     /* The stack for active stages */
214aa213bdcSJed Brown   int              curStage;  /* The current stage (only used in macros so we don't call PetscIntStackTop) */
215aa213bdcSJed Brown   PetscStageInfo  *stageInfo; /* The information for each stage */
216aa213bdcSJed Brown   PetscEventRegLog eventLog;  /* The registered events */
217aa213bdcSJed Brown   PetscClassRegLog classLog;  /* The registered classes */
2186a6a9b46SSatish Balay };
219217044c2SLisandro Dalcin /* -----------------------------------------------------------------------------------------------------*/
220fee518ebSJed Brown 
221*d71ae5a4SJacob Faibussowitsch PETSC_DEPRECATED_FUNCTION("PetscLogObjectParent() is deprecated (since version 3.18)") static inline PetscErrorCode PetscLogObjectParent(PetscObject o, PetscObject p)
222*d71ae5a4SJacob Faibussowitsch {
2234f839811SJacob Faibussowitsch   (void)o;
2244f839811SJacob Faibussowitsch   (void)p;
2254dfa11a4SJacob Faibussowitsch   return 0;
2264dfa11a4SJacob Faibussowitsch }
2274dfa11a4SJacob Faibussowitsch 
228*d71ae5a4SJacob Faibussowitsch PETSC_DEPRECATED_FUNCTION("PetscLogObjectMemory() is deprecated (since version 3.18)") static inline PetscErrorCode PetscLogObjectMemory(PetscObject o, PetscLogDouble m)
229*d71ae5a4SJacob Faibussowitsch {
2304f839811SJacob Faibussowitsch   (void)o;
2314f839811SJacob Faibussowitsch   (void)m;
2324dfa11a4SJacob Faibussowitsch   return 0;
2334dfa11a4SJacob Faibussowitsch }
2343bb1ff40SBarry Smith 
235aa482453SBarry Smith #if defined(PETSC_USE_LOG) /* --- Logging is turned on --------------------------------*/
236aeeda9f6SJohn Fettig PETSC_EXTERN PetscStageLog  petsc_stageLog;
237f5d6ab90SLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscLogGetStageLog(PetscStageLog *);
238f5d6ab90SLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscStageLogGetCurrent(PetscStageLog, int *);
239f5d6ab90SLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscStageLogGetEventPerfLog(PetscStageLog, int, PetscEventPerfLog *);
240614700edSBarry Smith 
241da63de55SLois Curfman McInnes   /*
242da63de55SLois Curfman McInnes    Flop counting:  We count each arithmetic operation (e.g., addition, multiplication) separately.
243da63de55SLois Curfman McInnes 
244da63de55SLois Curfman McInnes    For the complex numbers version, note that
245da63de55SLois Curfman McInnes        1 complex addition = 2 flops
246da63de55SLois Curfman McInnes        1 complex multiplication = 6 flops,
247da63de55SLois Curfman McInnes    where we define 1 flop as that for a double precision scalar.  We roughly approximate
248da63de55SLois Curfman McInnes    flop counting for complex numbers by multiplying the total flops by 4; this corresponds
249da63de55SLois Curfman McInnes    to the assumption that we're counting mostly additions and multiplications -- and
250da63de55SLois Curfman McInnes    roughly the same number of each.  More accurate counting could be done by distinguishing
251da63de55SLois Curfman McInnes    among the various arithmetic operations.
252da63de55SLois Curfman McInnes  */
253da63de55SLois Curfman McInnes 
254aa482453SBarry Smith   #if defined(PETSC_USE_COMPLEX)
255542d4b3fSSatish Balay     #define PETSC_FLOPS_PER_OP 4.0
256da63de55SLois Curfman McInnes   #else
257542d4b3fSSatish Balay     #define PETSC_FLOPS_PER_OP 1.0
258bf3909cdSBarry Smith   #endif
259bf3909cdSBarry Smith 
260f8bfbb56SSatish Balay /*@C
261ca0c957dSBarry Smith        PetscLogFlops - Log how many flops are performed in a calculation
262ca0c957dSBarry Smith 
263c3b874c4SJose E. Roman    Input Parameter:
264c3b874c4SJose E. Roman .   flops - the number of flops
265ca0c957dSBarry Smith 
266ca0c957dSBarry Smith    Notes:
267ca0c957dSBarry Smith      To limit the chance of integer overflow when multiplying by a constant, represent the constant as a double,
268ca0c957dSBarry Smith      not an integer. Use PetscLogFlops(4.0*n) not PetscLogFlops(4*n)
269ca0c957dSBarry Smith 
270ca0c957dSBarry Smith    Level: intermediate
271ca0c957dSBarry Smith 
272db781477SPatrick Sanan .seealso: `PetscLogView()`, `PetscLogGpuFlops()`
273ca0c957dSBarry Smith @*/
274ca0c957dSBarry Smith 
275*d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscLogFlops(PetscLogDouble n)
276*d71ae5a4SJacob Faibussowitsch {
277dd19b90bSBarry Smith   PetscFunctionBegin;
278bf3909cdSBarry Smith   #if defined(PETSC_USE_DEBUG)
2792c71b3e2SJacob Faibussowitsch   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot log negative flops");
280da63de55SLois Curfman McInnes   #endif
281dd19b90bSBarry Smith   petsc_TotalFlops += PETSC_FLOPS_PER_OP * n;
282dd19b90bSBarry Smith   PetscFunctionReturn(0);
283dd19b90bSBarry Smith }
284fa2bb9feSLisandro Dalcin 
285fa2bb9feSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscGetFlops(PetscLogDouble *);
28677c4ece6SBarry Smith 
287aa482453SBarry Smith   #if defined(PETSC_HAVE_MPE)
288014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogMPEBegin(void);
289014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogMPEDump(const char[]);
29077c4ece6SBarry Smith   #endif
29177c4ece6SBarry Smith 
292014dd563SJed Brown PETSC_EXTERN PetscErrorCode (*PetscLogPLB)(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject);
293014dd563SJed Brown PETSC_EXTERN PetscErrorCode (*PetscLogPLE)(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject);
294014dd563SJed Brown PETSC_EXTERN PetscErrorCode (*PetscLogPHC)(PetscObject);
295014dd563SJed Brown PETSC_EXTERN PetscErrorCode (*PetscLogPHD)(PetscObject);
29677c4ece6SBarry Smith 
2979566063dSJacob Faibussowitsch   #define PetscLogObjectParents(p, n, d) PetscMacroReturnStandard(for (int _i = 0; _i < (n); ++_i) PetscCall(PetscLogObjectParent((PetscObject)(p), (PetscObject)(d)[_i]));)
298fa2bb9feSLisandro Dalcin   #define PetscLogObjectCreate(h)        ((PetscLogPHC) ? (*PetscLogPHC)((PetscObject)(h)) : 0)
299fa2bb9feSLisandro Dalcin   #define PetscLogObjectDestroy(h)       ((PetscLogPHD) ? (*PetscLogPHD)((PetscObject)(h)) : 0)
3003ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscLogObjectState(PetscObject, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);
301fa2bb9feSLisandro Dalcin 
3028ba1e511SMatthew Knepley /* Initialization functions */
303bb1d7374SBarry Smith PETSC_EXTERN PetscErrorCode PetscLogDefaultBegin(void);
304014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogAllBegin(void);
305bb1d7374SBarry Smith PETSC_EXTERN PetscErrorCode PetscLogNestedBegin(void);
306014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogTraceBegin(FILE *);
307014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogActions(PetscBool);
308014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogObjects(PetscBool);
309fa2bb9feSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscLogSetThreshold(PetscLogDouble, PetscLogDouble *);
3109371c9d4SSatish Balay PETSC_EXTERN PetscErrorCode PetscLogSet(PetscErrorCode (*)(int, int, PetscObject, PetscObject, PetscObject, PetscObject), PetscErrorCode (*)(int, int, PetscObject, PetscObject, PetscObject, PetscObject));
311fa2bb9feSLisandro Dalcin 
3128ba1e511SMatthew Knepley /* Output functions */
313014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogView(PetscViewer);
314f14045dbSBarry Smith PETSC_EXTERN PetscErrorCode PetscLogViewFromOptions(void);
315014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogDump(const char[]);
31631d06abdSBarry Smith 
3174dd65854SConnor Ward /* Status checking functions */
3184dd65854SConnor Ward PETSC_EXTERN PetscErrorCode PetscLogIsActive(PetscBool *);
3194dd65854SConnor Ward 
320fa2bb9feSLisandro Dalcin /* Stage functions */
321014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogStageRegister(const char[], PetscLogStage *);
322014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogStagePush(PetscLogStage);
323014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogStagePop(void);
324014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogStageSetActive(PetscLogStage, PetscBool);
325014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogStageGetActive(PetscLogStage, PetscBool *);
326014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogStageSetVisible(PetscLogStage, PetscBool);
327014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogStageGetVisible(PetscLogStage, PetscBool *);
328014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogStageGetId(const char[], PetscLogStage *);
329f5d6ab90SLisandro Dalcin 
3308ba1e511SMatthew Knepley /* Event functions */
331014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogEventRegister(const char[], PetscClassId, PetscLogEvent *);
332217044c2SLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscLogEventSetCollective(PetscLogEvent, PetscBool);
333fa2bb9feSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscLogEventIncludeClass(PetscClassId);
334fa2bb9feSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscLogEventExcludeClass(PetscClassId);
335014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogEventActivate(PetscLogEvent);
336014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogEventDeactivate(PetscLogEvent);
337c00cb57fSBarry Smith PETSC_EXTERN PetscErrorCode PetscLogEventDeactivatePush(PetscLogEvent);
338c00cb57fSBarry Smith PETSC_EXTERN PetscErrorCode PetscLogEventDeactivatePop(PetscLogEvent);
339014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogEventSetActiveAll(PetscLogEvent, PetscBool);
340014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogEventActivateClass(PetscClassId);
341014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogEventDeactivateClass(PetscClassId);
3428c49a8d9SBarry Smith PETSC_EXTERN PetscErrorCode PetscLogEventGetId(const char[], PetscLogEvent *);
34313230571SMichael Lange PETSC_EXTERN PetscErrorCode PetscLogEventGetPerfInfo(int, PetscLogEvent, PetscEventPerfInfo *);
344891e75beSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscLogEventSetDof(PetscLogEvent, PetscInt, PetscLogDouble);
3455d68e14cSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscLogEventSetError(PetscLogEvent, PetscInt, PetscLogDouble);
3467a101e5eSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscLogPushCurrentEvent_Internal(PetscLogEvent);
3477a101e5eSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscLogPopCurrentEvent_Internal(void);
3488ba1e511SMatthew Knepley 
3498ba1e511SMatthew Knepley /* Global counters */
350014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_irecv_ct;
351014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_isend_ct;
352014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_recv_ct;
353014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_send_ct;
354014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_irecv_len;
355014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_isend_len;
356014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_recv_len;
357014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_send_len;
358014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_allreduce_ct;
359014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_gather_ct;
360014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_scatter_ct;
361014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_wait_ct;
362014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_wait_any_ct;
363014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_wait_all_ct;
364014dd563SJed Brown PETSC_EXTERN PetscLogDouble petsc_sum_of_waits_ct;
3658ba1e511SMatthew Knepley 
366e3ed9ee7SBarry Smith PETSC_EXTERN PetscBool PetscLogMemory;
367e3ed9ee7SBarry Smith 
368fa2bb9feSLisandro Dalcin PETSC_EXTERN PetscBool      PetscLogSyncOn; /* true if logging synchronization is enabled */
36962872c28SLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscLogEventSynchronize(PetscLogEvent, MPI_Comm);
37062872c28SLisandro Dalcin 
37162872c28SLisandro Dalcin   #define PetscLogEventSync(e, comm) \
3729371c9d4SSatish Balay     (((PetscLogPLB && petsc_stageLog->stageInfo[petsc_stageLog->curStage].perfInfo.active && petsc_stageLog->stageInfo[petsc_stageLog->curStage].eventLog->eventInfo[e].active) ? PetscLogEventSynchronize((e), (comm)) : 0))
37377c4ece6SBarry Smith 
37452e6d16bSBarry Smith   #define PetscLogEventBegin(e, o1, o2, o3, o4) \
3759371c9d4SSatish Balay     ((PetscLogPLB && petsc_stageLog->stageInfo[petsc_stageLog->curStage].perfInfo.active && petsc_stageLog->stageInfo[petsc_stageLog->curStage].eventLog->eventInfo[e].active) ? (((*PetscLogPLB)((e), 0, (PetscObject)(o1), (PetscObject)(o2), (PetscObject)(o3), (PetscObject)(o4))) || PetscLogPushCurrentEvent_Internal(e)) : 0)
376043328b6SSatish Balay 
37752e6d16bSBarry Smith   #define PetscLogEventEnd(e, o1, o2, o3, o4) \
3789371c9d4SSatish Balay     ((PetscLogPLE && petsc_stageLog->stageInfo[petsc_stageLog->curStage].perfInfo.active && petsc_stageLog->stageInfo[petsc_stageLog->curStage].eventLog->eventInfo[e].active) ? (((*PetscLogPLE)((e), 0, (PetscObject)(o1), (PetscObject)(o2), (PetscObject)(o3), (PetscObject)(o4))) || PetscLogPopCurrentEvent_Internal()) : 0)
37977c4ece6SBarry Smith 
380014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogEventGetFlops(PetscLogEvent, PetscLogDouble *);
381014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogEventZeroFlops(PetscLogEvent);
382003131ecSBarry Smith 
383ce85283eSBarry Smith   /*
384f621e05eSBarry Smith      These are used internally in the PETSc routines to keep a count of MPI messages and
385f621e05eSBarry Smith    their sizes.
386f621e05eSBarry Smith 
387af0996ceSBarry Smith      This does not work for MPI-Uni because our include/petsc/mpiuni/mpi.h file
388ce85283eSBarry Smith    uses macros to defined the MPI operations.
38915308354SBarry Smith 
39015308354SBarry Smith      It does not work correctly from HP-UX because it processes the
391bb4af37aSBarry Smith    macros in a way that sometimes it double counts, hence
392b6410449SSatish Balay    PETSC_HAVE_BROKEN_RECURSIVE_MACRO
3937c1e34a4SSatish Balay 
394f569fd43SBarry Smith      It does not work with Windows because winmpich lacks MPI_Type_size()
395ce85283eSBarry Smith */
396c9b973beSBarry Smith   #if !defined(MPIUNI_H) && !defined(PETSC_HAVE_BROKEN_RECURSIVE_MACRO) && !defined(PETSC_HAVE_MPI_MISSING_TYPESIZE)
39777a39924SBarry Smith /*
39877a39924SBarry Smith    Logging of MPI activities
39977a39924SBarry Smith */
400*d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscMPITypeSize(PetscInt count, MPI_Datatype type, PetscLogDouble *length)
401*d71ae5a4SJacob Faibussowitsch {
402f12d10ccSJunchao Zhang   PetscMPIInt typesize;
4035f80ce2aSJacob Faibussowitsch 
4047d30fa4aSBarry Smith   if (type == MPI_DATATYPE_NULL) return 0;
4059566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Type_size(type, &typesize));
406f12d10ccSJunchao Zhang   *length += (PetscLogDouble)(count * typesize);
4079c72ff1eSSatish Balay   return 0;
408f95db71bSBarry Smith }
40977a39924SBarry Smith 
410*d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscMPITypeSizeComm(MPI_Comm comm, const PetscMPIInt *counts, MPI_Datatype type, PetscLogDouble *length)
411*d71ae5a4SJacob Faibussowitsch {
412f12d10ccSJunchao Zhang   PetscMPIInt typesize, size, p;
4133b9284c0SMatthew G Knepley 
4147d30fa4aSBarry Smith   if (type == MPI_DATATYPE_NULL) return 0;
4159566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
4169566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Type_size(type, &typesize));
4175f80ce2aSJacob Faibussowitsch   for (p = 0; p < size; ++p) *length += (PetscLogDouble)(counts[p] * typesize);
418f12d10ccSJunchao Zhang   return 0;
419f12d10ccSJunchao Zhang }
420f12d10ccSJunchao Zhang 
421*d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscMPITypeSizeCount(PetscInt n, const PetscMPIInt *counts, MPI_Datatype type, PetscLogDouble *length)
422*d71ae5a4SJacob Faibussowitsch {
423f12d10ccSJunchao Zhang   PetscMPIInt typesize, p;
424f12d10ccSJunchao Zhang 
425f12d10ccSJunchao Zhang   if (type == MPI_DATATYPE_NULL) return 0;
4269566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Type_size(type, &typesize));
4275f80ce2aSJacob Faibussowitsch   for (p = 0; p < n; ++p) *length += (PetscLogDouble)(counts[p] * typesize);
4283b9284c0SMatthew G Knepley   return 0;
4293b9284c0SMatthew G Knepley }
4303b9284c0SMatthew G Knepley 
43172b11a25SBarry Smith /*
43272b11a25SBarry Smith     Returns 1 if the communicator is parallel else zero
43372b11a25SBarry Smith */
434*d71ae5a4SJacob Faibussowitsch static inline int PetscMPIParallelComm(MPI_Comm comm)
435*d71ae5a4SJacob Faibussowitsch {
4369371c9d4SSatish Balay   PetscMPIInt size;
4379371c9d4SSatish Balay   MPI_Comm_size(comm, &size);
4389371c9d4SSatish Balay   return size > 1;
43972b11a25SBarry Smith }
44072b11a25SBarry Smith 
4419371c9d4SSatish Balay     #define MPI_Irecv(buf, count, datatype, source, tag, comm, request) ((petsc_irecv_ct++, 0) || PetscMPITypeSize((count), (datatype), &(petsc_irecv_len)) || MPI_Irecv((buf), (count), (datatype), (source), (tag), (comm), (request)))
44215308354SBarry Smith 
4439371c9d4SSatish Balay     #define MPI_Irecv_c(buf, count, datatype, source, tag, comm, request) ((petsc_irecv_ct++, 0) || PetscMPITypeSize((count), (datatype), &(petsc_irecv_len)) || MPI_Irecv_c((buf), (count), (datatype), (source), (tag), (comm), (request)))
444c87b50c4SJunchao Zhang 
4459371c9d4SSatish Balay     #define MPI_Isend(buf, count, datatype, dest, tag, comm, request) ((petsc_isend_ct++, 0) || PetscMPITypeSize((count), (datatype), &(petsc_isend_len)) || MPI_Isend((buf), (count), (datatype), (dest), (tag), (comm), (request)))
44615308354SBarry Smith 
4479371c9d4SSatish Balay     #define MPI_Isend_c(buf, count, datatype, dest, tag, comm, request) ((petsc_isend_ct++, 0) || PetscMPITypeSize((count), (datatype), &(petsc_isend_len)) || MPI_Isend_c((buf), (count), (datatype), (dest), (tag), (comm), (request)))
448c87b50c4SJunchao Zhang 
4499371c9d4SSatish Balay     #define MPI_Startall_irecv(count, datatype, number, requests) ((petsc_irecv_ct += (PetscLogDouble)(number), 0) || PetscMPITypeSize((count), (datatype), &(petsc_irecv_len)) || ((number) && MPI_Startall((number), (requests))))
4500d4b0b6cSBarry Smith 
4519371c9d4SSatish Balay     #define MPI_Startall_isend(count, datatype, number, requests) ((petsc_isend_ct += (PetscLogDouble)(number), 0) || PetscMPITypeSize((count), (datatype), &(petsc_isend_len)) || ((number) && MPI_Startall((number), (requests))))
4520d4b0b6cSBarry Smith 
4539371c9d4SSatish Balay     #define MPI_Start_isend(count, datatype, requests) ((petsc_isend_ct++, 0) || PetscMPITypeSize((count), (datatype), (&petsc_isend_len)) || MPI_Start((requests)))
4540d4b0b6cSBarry Smith 
4559371c9d4SSatish Balay     #define MPI_Recv(buf, count, datatype, source, tag, comm, status) ((petsc_recv_ct++, 0) || PetscMPITypeSize((count), (datatype), (&petsc_recv_len)) || MPI_Recv((buf), (count), (datatype), (source), (tag), (comm), (status)))
45615308354SBarry Smith 
4579371c9d4SSatish Balay     #define MPI_Recv_c(buf, count, datatype, source, tag, comm, status) ((petsc_recv_ct++, 0) || PetscMPITypeSize((count), (datatype), (&petsc_recv_len)) || MPI_Recv_c((buf), (count), (datatype), (source), (tag), (comm), (status)))
458c87b50c4SJunchao Zhang 
4599371c9d4SSatish Balay     #define MPI_Send(buf, count, datatype, dest, tag, comm) ((petsc_send_ct++, 0) || PetscMPITypeSize((count), (datatype), (&petsc_send_len)) || MPI_Send((buf), (count), (datatype), (dest), (tag), (comm)))
46077a39924SBarry Smith 
4619371c9d4SSatish Balay     #define MPI_Send_c(buf, count, datatype, dest, tag, comm) ((petsc_send_ct++, 0) || PetscMPITypeSize((count), (datatype), (&petsc_send_len)) || MPI_Send_c((buf), (count), (datatype), (dest), (tag), (comm)))
462c87b50c4SJunchao Zhang 
4639371c9d4SSatish Balay     #define MPI_Wait(request, status) ((petsc_wait_ct++, petsc_sum_of_waits_ct++, 0) || MPI_Wait((request), (status)))
46477a39924SBarry Smith 
4659371c9d4SSatish Balay     #define MPI_Waitany(a, b, c, d) ((petsc_wait_any_ct++, petsc_sum_of_waits_ct++, 0) || MPI_Waitany((a), (b), (c), (d)))
46677a39924SBarry Smith 
4679371c9d4SSatish Balay     #define MPI_Waitall(count, array_of_requests, array_of_statuses) ((petsc_wait_all_ct++, petsc_sum_of_waits_ct += (PetscLogDouble)(count), 0) || MPI_Waitall((count), (array_of_requests), (array_of_statuses)))
46877a39924SBarry Smith 
4699371c9d4SSatish Balay     #define MPI_Allreduce(sendbuf, recvbuf, count, datatype, op, comm) (petsc_allreduce_ct += PetscMPIParallelComm((comm)), MPI_Allreduce((sendbuf), (recvbuf), (count), (datatype), (op), (comm)))
4703914022bSBarry Smith 
4719371c9d4SSatish Balay     #define MPI_Bcast(buffer, count, datatype, root, comm) ((petsc_allreduce_ct += PetscMPIParallelComm((comm)), 0) || MPI_Bcast((buffer), (count), (datatype), (root), (comm)))
47254fe5c21SBarry Smith 
4739371c9d4SSatish Balay     #define MPI_Reduce_scatter_block(sendbuf, recvbuf, recvcount, datatype, op, comm) ((petsc_allreduce_ct += PetscMPIParallelComm((comm)), 0) || MPI_Reduce_scatter_block((sendbuf), (recvbuf), (recvcount), (datatype), (op), (comm)))
474d6e4c47cSJed Brown 
4753b9284c0SMatthew G Knepley     #define MPI_Alltoall(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm) \
476f12d10ccSJunchao Zhang       ((petsc_allreduce_ct += PetscMPIParallelComm((comm)), 0) || PetscMPITypeSize((sendcount), (sendtype), (&petsc_send_len)) || MPI_Alltoall((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (recvtype), (comm)))
4773b9284c0SMatthew G Knepley 
4783b9284c0SMatthew G Knepley     #define MPI_Alltoallv(sendbuf, sendcnts, sdispls, sendtype, recvbuf, recvcnts, rdispls, recvtype, comm) \
479f12d10ccSJunchao Zhang       ((petsc_allreduce_ct += PetscMPIParallelComm((comm)), 0) || PetscMPITypeSizeComm((comm), (sendcnts), (sendtype), (&petsc_send_len)) || MPI_Alltoallv((sendbuf), (sendcnts), (sdispls), (sendtype), (recvbuf), (recvcnts), (rdispls), (recvtype), (comm)))
4803b9284c0SMatthew G Knepley 
4819371c9d4SSatish Balay     #define MPI_Allgather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm) ((petsc_gather_ct += PetscMPIParallelComm((comm)), 0) || MPI_Allgather((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (recvtype), (comm)))
48201faf4e4SMatthew Knepley 
48301faf4e4SMatthew Knepley     #define MPI_Allgatherv(sendbuf, sendcount, sendtype, recvbuf, recvcount, displs, recvtype, comm) \
4845fa69175SJed Brown       ((petsc_gather_ct += PetscMPIParallelComm((comm)), 0) || MPI_Allgatherv((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (displs), (recvtype), (comm)))
48501faf4e4SMatthew Knepley 
48601faf4e4SMatthew Knepley     #define MPI_Gather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm) \
487f12d10ccSJunchao Zhang       ((petsc_gather_ct++, 0) || PetscMPITypeSize((sendcount), (sendtype), (&petsc_send_len)) || MPI_Gather((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (recvtype), (root), (comm)))
48801faf4e4SMatthew Knepley 
48901faf4e4SMatthew Knepley     #define MPI_Gatherv(sendbuf, sendcount, sendtype, recvbuf, recvcount, displs, recvtype, root, comm) \
490f12d10ccSJunchao Zhang       ((petsc_gather_ct++, 0) || PetscMPITypeSize((sendcount), (sendtype), (&petsc_send_len)) || MPI_Gatherv((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (displs), (recvtype), (root), (comm)))
49101faf4e4SMatthew Knepley 
49201faf4e4SMatthew Knepley     #define MPI_Scatter(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm) \
493f12d10ccSJunchao Zhang       ((petsc_scatter_ct++, 0) || PetscMPITypeSize((recvcount), (recvtype), (&petsc_recv_len)) || MPI_Scatter((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (recvtype), (root), (comm)))
49401faf4e4SMatthew Knepley 
49501faf4e4SMatthew Knepley     #define MPI_Scatterv(sendbuf, sendcount, displs, sendtype, recvbuf, recvcount, recvtype, root, comm) \
496f12d10ccSJunchao Zhang       ((petsc_scatter_ct++, 0) || PetscMPITypeSize((recvcount), (recvtype), (&petsc_recv_len)) || MPI_Scatterv((sendbuf), (sendcount), (displs), (sendtype), (recvbuf), (recvcount), (recvtype), (root), (comm)))
497f12d10ccSJunchao Zhang 
498f12d10ccSJunchao Zhang     #define MPI_Ialltoall(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm, request) \
499f12d10ccSJunchao Zhang       ((petsc_allreduce_ct += PetscMPIParallelComm((comm)), 0) || PetscMPITypeSize((sendcount), (sendtype), (&petsc_send_len)) || MPI_Ialltoall((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (recvtype), (comm), (request)))
500f12d10ccSJunchao Zhang 
501f12d10ccSJunchao Zhang     #define MPI_Ialltoallv(sendbuf, sendcnts, sdispls, sendtype, recvbuf, recvcnts, rdispls, recvtype, comm, request) \
502f12d10ccSJunchao Zhang       ((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)))
503f12d10ccSJunchao Zhang 
504f12d10ccSJunchao Zhang     #define MPI_Iallgather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm, request) \
505f12d10ccSJunchao Zhang       ((petsc_gather_ct += PetscMPIParallelComm((comm)), 0) || MPI_Iallgather((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (recvtype), (comm), (request)))
506f12d10ccSJunchao Zhang 
507f12d10ccSJunchao Zhang     #define MPI_Iallgatherv(sendbuf, sendcount, sendtype, recvbuf, recvcount, displs, recvtype, comm, request) \
508f12d10ccSJunchao Zhang       ((petsc_gather_ct += PetscMPIParallelComm((comm)), 0) || MPI_Iallgatherv((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (displs), (recvtype), (comm), (request)))
509f12d10ccSJunchao Zhang 
510f12d10ccSJunchao Zhang     #define MPI_Igather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm, request) \
511f12d10ccSJunchao Zhang       ((petsc_gather_ct++, 0) || PetscMPITypeSize((sendcount), (sendtype), (&petsc_send_len)) || MPI_Igather((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (recvtype), (root), (comm), (request)))
512f12d10ccSJunchao Zhang 
513f12d10ccSJunchao Zhang     #define MPI_Igatherv(sendbuf, sendcount, sendtype, recvbuf, recvcount, displs, recvtype, root, comm, request) \
514f12d10ccSJunchao Zhang       ((petsc_gather_ct++, 0) || PetscMPITypeSize((sendcount), (sendtype), (&petsc_send_len)) || MPI_Igatherv((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (displs), (recvtype), (root), (comm), (request)))
515f12d10ccSJunchao Zhang 
516f12d10ccSJunchao Zhang     #define MPI_Iscatter(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm, request) \
517f12d10ccSJunchao Zhang       ((petsc_scatter_ct++, 0) || PetscMPITypeSize((recvcount), (recvtype), (&petsc_recv_len)) || MPI_Iscatter((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (recvtype), (root), (comm), (request)))
518f12d10ccSJunchao Zhang 
519f12d10ccSJunchao Zhang     #define MPI_Iscatterv(sendbuf, sendcount, displs, sendtype, recvbuf, recvcount, recvtype, root, comm, request) \
520f12d10ccSJunchao Zhang       ((petsc_scatter_ct++, 0) || PetscMPITypeSize((recvcount), (recvtype), (&petsc_recv_len)) || MPI_Iscatterv((sendbuf), (sendcount), (displs), (sendtype), (recvbuf), (recvcount), (recvtype), (root), (comm), (request)))
521f12d10ccSJunchao Zhang 
5220d4b0b6cSBarry Smith   #else
5230d4b0b6cSBarry Smith 
5249371c9d4SSatish Balay     #define MPI_Startall_irecv(count, datatype, number, requests) ((number) && MPI_Startall((number), (requests)))
5250d4b0b6cSBarry Smith 
5269371c9d4SSatish Balay     #define MPI_Startall_isend(count, datatype, number, requests) ((number) && MPI_Startall((number), (requests)))
5270d4b0b6cSBarry Smith 
5289371c9d4SSatish Balay     #define MPI_Start_isend(count, datatype, requests) (MPI_Start((requests)))
5290d4b0b6cSBarry Smith 
530c9b973beSBarry Smith   #endif /* !MPIUNI_H && ! PETSC_HAVE_BROKEN_RECURSIVE_MACRO */
531614700edSBarry Smith 
532df8cf0b5SBarry Smith #else /* ---Logging is turned off --------------------------------------------*/
533614700edSBarry Smith 
534e3ed9ee7SBarry Smith   #define PetscLogMemory PETSC_FALSE
535e3ed9ee7SBarry Smith 
536b0a32e0cSBarry Smith   #define PetscLogFlops(n) 0
537fa2bb9feSLisandro Dalcin   #define PetscGetFlops(a) (*(a) = 0.0, 0)
538614700edSBarry Smith 
539fa2bb9feSLisandro Dalcin   #define PetscLogStageRegister(a, b)   0
540fa2bb9feSLisandro Dalcin   #define PetscLogStagePush(a)          0
541fa2bb9feSLisandro Dalcin   #define PetscLogStagePop()            0
542f5d6ab90SLisandro Dalcin   #define PetscLogStageSetActive(a, b)  0
543f5d6ab90SLisandro Dalcin   #define PetscLogStageGetActive(a, b)  0
544f5d6ab90SLisandro Dalcin   #define PetscLogStageGetVisible(a, b) 0
545f5d6ab90SLisandro Dalcin   #define PetscLogStageSetVisible(a, b) 0
546f5d6ab90SLisandro Dalcin   #define PetscLogStageGetId(a, b)      (*(b) = 0, 0)
547f5d6ab90SLisandro Dalcin 
548fa2bb9feSLisandro Dalcin   #define PetscLogEventRegister(a, b, c)    0
549fa2bb9feSLisandro Dalcin   #define PetscLogEventSetCollective(a, b)  0
550fa2bb9feSLisandro Dalcin   #define PetscLogEventIncludeClass(a)      0
551fa2bb9feSLisandro Dalcin   #define PetscLogEventExcludeClass(a)      0
552b0a32e0cSBarry Smith   #define PetscLogEventActivate(a)          0
553b0a32e0cSBarry Smith   #define PetscLogEventDeactivate(a)        0
554c00cb57fSBarry Smith   #define PetscLogEventDeactivatePush(a)    0
555c00cb57fSBarry Smith   #define PetscLogEventDeactivatePop(a)     0
556b0a32e0cSBarry Smith   #define PetscLogEventActivateClass(a)     0
557b0a32e0cSBarry Smith   #define PetscLogEventDeactivateClass(a)   0
558e8e7597cSSatish Balay   #define PetscLogEventSetActiveAll(a, b)   0
55946eb3cd7SSatish Balay   #define PetscLogEventGetId(a, b)          (*(b) = 0, 0)
560561b2a67SSatish Balay   #define PetscLogEventGetPerfInfo(a, b, c) 0
5619c702cfcSMatthew G. Knepley   #define PetscLogEventSetDof(a, b, c)      0
5629c702cfcSMatthew G. Knepley   #define PetscLogEventSetError(a, b, c)    0
56377c4ece6SBarry Smith 
5648b5db460SBarry Smith   #define PetscLogPLB 0
5658b5db460SBarry Smith   #define PetscLogPLE 0
5668b5db460SBarry Smith   #define PetscLogPHC 0
5678b5db460SBarry Smith   #define PetscLogPHD 0
568f5d6ab90SLisandro Dalcin 
569efee365bSSatish Balay   #define PetscLogObjectParents(p, n, c) 0
57052e6d16bSBarry Smith   #define PetscLogObjectCreate(h)        0
57152e6d16bSBarry Smith   #define PetscLogObjectDestroy(h)       0
5723ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscLogObjectState(PetscObject, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);
573fa2bb9feSLisandro Dalcin 
574bb1d7374SBarry Smith   #define PetscLogDefaultBegin()     0
575b0a32e0cSBarry Smith   #define PetscLogAllBegin()         0
576bb1d7374SBarry Smith   #define PetscLogNestedBegin()      0
577fa2bb9feSLisandro Dalcin   #define PetscLogTraceBegin(file)   0
57873fda44aSBarry Smith   #define PetscLogActions(a)         0
579fa2bb9feSLisandro Dalcin   #define PetscLogObjects(a)         0
580fa2bb9feSLisandro Dalcin   #define PetscLogSetThreshold(a, b) 0
581fa2bb9feSLisandro Dalcin   #define PetscLogSet(lb, le)        0
582340002ceSLawrence Mitchell   #define PetscLogIsActive(flag)     (*(flag) = PETSC_FALSE, 0)
583fa2bb9feSLisandro Dalcin 
584fa2bb9feSLisandro Dalcin   #define PetscLogView(viewer)      0
585fa2bb9feSLisandro Dalcin   #define PetscLogViewFromOptions() 0
586fa2bb9feSLisandro Dalcin   #define PetscLogDump(c)           0
587fa2bb9feSLisandro Dalcin 
58862872c28SLisandro Dalcin   #define PetscLogEventSync(e, comm)                            0
589fa2bb9feSLisandro Dalcin   #define PetscLogEventBegin(e, o1, o2, o3, o4)                 0
590fa2bb9feSLisandro Dalcin   #define PetscLogEventEnd(e, o1, o2, o3, o4)                   0
591ce6058e1SBarry Smith 
59223c86b1bSSatish Balay   /* If PETSC_USE_LOG is NOT defined, these still need to be! */
59323c86b1bSSatish Balay   #define MPI_Startall_irecv(count, datatype, number, requests) ((number) && MPI_Startall(number, requests))
59423c86b1bSSatish Balay   #define MPI_Startall_isend(count, datatype, number, requests) ((number) && MPI_Startall(number, requests))
59523c86b1bSSatish Balay   #define MPI_Start_isend(count, datatype, requests)            MPI_Start(requests)
59623c86b1bSSatish Balay 
59723c86b1bSSatish Balay #endif /* PETSC_USE_LOG */
59823c86b1bSSatish Balay 
59923c86b1bSSatish Balay #if defined(PETSC_USE_LOG) && defined(PETSC_HAVE_DEVICE)
60023c86b1bSSatish Balay 
60123c86b1bSSatish Balay /* Global GPU counters */
60223c86b1bSSatish Balay PETSC_EXTERN PetscLogDouble petsc_ctog_ct;
60323c86b1bSSatish Balay PETSC_EXTERN PetscLogDouble petsc_gtoc_ct;
60423c86b1bSSatish Balay PETSC_EXTERN PetscLogDouble petsc_ctog_sz;
60523c86b1bSSatish Balay PETSC_EXTERN PetscLogDouble petsc_gtoc_sz;
60623c86b1bSSatish Balay PETSC_EXTERN PetscLogDouble petsc_ctog_ct_scalar;
60723c86b1bSSatish Balay PETSC_EXTERN PetscLogDouble petsc_gtoc_ct_scalar;
60823c86b1bSSatish Balay PETSC_EXTERN PetscLogDouble petsc_ctog_sz_scalar;
60923c86b1bSSatish Balay PETSC_EXTERN PetscLogDouble petsc_gtoc_sz_scalar;
61023c86b1bSSatish Balay PETSC_EXTERN PetscLogDouble petsc_gflops;
61123c86b1bSSatish Balay PETSC_EXTERN PetscLogDouble petsc_gtime;
61223c86b1bSSatish Balay 
613*d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscLogCpuToGpu(PetscLogDouble size)
614*d71ae5a4SJacob Faibussowitsch {
61523c86b1bSSatish Balay   PetscFunctionBegin;
61623c86b1bSSatish Balay   petsc_ctog_ct += 1;
61723c86b1bSSatish Balay   petsc_ctog_sz += size;
61823c86b1bSSatish Balay   PetscFunctionReturn(0);
61923c86b1bSSatish Balay }
62023c86b1bSSatish Balay 
621*d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscLogGpuToCpu(PetscLogDouble size)
622*d71ae5a4SJacob Faibussowitsch {
62323c86b1bSSatish Balay   PetscFunctionBegin;
62423c86b1bSSatish Balay   petsc_gtoc_ct += 1;
62523c86b1bSSatish Balay   petsc_gtoc_sz += size;
62623c86b1bSSatish Balay   PetscFunctionReturn(0);
62723c86b1bSSatish Balay }
62823c86b1bSSatish Balay 
629*d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscLogCpuToGpuScalar(PetscLogDouble size)
630*d71ae5a4SJacob Faibussowitsch {
63123c86b1bSSatish Balay   PetscFunctionBegin;
63223c86b1bSSatish Balay   petsc_ctog_ct_scalar += 1;
63323c86b1bSSatish Balay   petsc_ctog_sz_scalar += size;
63423c86b1bSSatish Balay   PetscFunctionReturn(0);
63523c86b1bSSatish Balay }
63623c86b1bSSatish Balay 
637*d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscLogGpuToCpuScalar(PetscLogDouble size)
638*d71ae5a4SJacob Faibussowitsch {
63923c86b1bSSatish Balay   PetscFunctionBegin;
64023c86b1bSSatish Balay   petsc_gtoc_ct_scalar += 1;
64123c86b1bSSatish Balay   petsc_gtoc_sz_scalar += size;
64223c86b1bSSatish Balay   PetscFunctionReturn(0);
64323c86b1bSSatish Balay }
64423c86b1bSSatish Balay 
64523c86b1bSSatish Balay /*@C
64623c86b1bSSatish Balay        PetscLogGpuFlops - Log how many flops are performed in a calculation on the device
64723c86b1bSSatish Balay 
64823c86b1bSSatish Balay    Input Parameter:
64923c86b1bSSatish Balay .   flops - the number of flops
65023c86b1bSSatish Balay 
65123c86b1bSSatish Balay    Notes:
65223c86b1bSSatish Balay      To limit the chance of integer overflow when multiplying by a constant, represent the constant as a double,
65323c86b1bSSatish Balay      not an integer. Use PetscLogFlops(4.0*n) not PetscLogFlops(4*n)
65423c86b1bSSatish Balay 
6554851f57dSBarry Smith      The values are also added to the total flop count for the MPI rank that is set with `PetscLogFlops()`; hence the number of flops
6564851f57dSBarry Smith      just on the CPU would be the value from set from `PetscLogFlops()` minus the value set from `PetscLogGpuFlops()`
6574851f57dSBarry Smith 
65823c86b1bSSatish Balay    Level: intermediate
65923c86b1bSSatish Balay 
660db781477SPatrick Sanan .seealso: `PetscLogView()`, `PetscLogFlops()`, `PetscLogGpuTimeBegin()`, `PetscLogGpuTimeEnd()`
66123c86b1bSSatish Balay @*/
662*d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscLogGpuFlops(PetscLogDouble n)
663*d71ae5a4SJacob Faibussowitsch {
66423c86b1bSSatish Balay   PetscFunctionBegin;
6652c71b3e2SJacob Faibussowitsch   PetscCheck(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot log negative flops");
66623c86b1bSSatish Balay   petsc_TotalFlops += PETSC_FLOPS_PER_OP * n;
66723c86b1bSSatish Balay   petsc_gflops += PETSC_FLOPS_PER_OP * n;
66823c86b1bSSatish Balay   PetscFunctionReturn(0);
66923c86b1bSSatish Balay }
67023c86b1bSSatish Balay 
671*d71ae5a4SJacob Faibussowitsch static inline PetscErrorCode PetscLogGpuTimeAdd(PetscLogDouble t)
672*d71ae5a4SJacob Faibussowitsch {
67323c86b1bSSatish Balay   PetscFunctionBegin;
67423c86b1bSSatish Balay   petsc_gtime += t;
67523c86b1bSSatish Balay   PetscFunctionReturn(0);
67623c86b1bSSatish Balay }
67723c86b1bSSatish Balay 
678156b51fbSBarry Smith PETSC_EXTERN PetscErrorCode PetscLogGpuTime(void);
67923c86b1bSSatish Balay PETSC_EXTERN PetscErrorCode PetscLogGpuTimeBegin(void);
68023c86b1bSSatish Balay PETSC_EXTERN PetscErrorCode PetscLogGpuTimeEnd(void);
68123c86b1bSSatish Balay 
68223c86b1bSSatish Balay #else
68323c86b1bSSatish Balay 
6848157aad8SJunchao Zhang   #define PetscLogCpuToGpu(a)       0
6858157aad8SJunchao Zhang   #define PetscLogGpuToCpu(a)       0
68645c4b7c1SBarry Smith   #define PetscLogCpuToGpuScalar(a) 0
68745c4b7c1SBarry Smith   #define PetscLogGpuToCpuScalar(a) 0
6888157aad8SJunchao Zhang   #define PetscLogGpuFlops(a)       0
6897a101e5eSJacob Faibussowitsch   #define PetscLogGpuTime()         0
69053b96e71SJacob Faibussowitsch   #define PetscLogGpuTimeAdd(a)     0
6918157aad8SJunchao Zhang   #define PetscLogGpuTimeBegin()    0
6928157aad8SJunchao Zhang   #define PetscLogGpuTimeEnd()      0
6938157aad8SJunchao Zhang 
69423c86b1bSSatish Balay #endif /* PETSC_USE_LOG && PETSC_HAVE_DEVICE */
6956daaf66cSBarry Smith 
696204a6943SJed Brown #define PetscPreLoadBegin(flag, name) \
697204a6943SJed Brown   do { \
698204a6943SJed Brown     PetscBool     PetscPreLoading = flag; \
699204a6943SJed Brown     int           PetscPreLoadMax, PetscPreLoadIt; \
700166c7f25SBarry Smith     PetscLogStage _stageNum; \
7019566063dSJacob Faibussowitsch     PetscCall(PetscOptionsGetBool(NULL, NULL, "-preload", &PetscPreLoading, NULL)); \
702204a6943SJed Brown     PetscPreLoadMax     = (int)(PetscPreLoading); \
703204a6943SJed Brown     PetscPreLoadingUsed = PetscPreLoading ? PETSC_TRUE : PetscPreLoadingUsed; \
704204a6943SJed Brown     for (PetscPreLoadIt = 0; PetscPreLoadIt <= PetscPreLoadMax; PetscPreLoadIt++) { \
705204a6943SJed Brown       PetscPreLoadingOn = PetscPreLoading; \
7069566063dSJacob Faibussowitsch       PetscCall(PetscBarrier(NULL)); \
7079566063dSJacob Faibussowitsch       if (PetscPreLoadIt > 0) PetscCall(PetscLogStageGetId(name, &_stageNum)); \
7089566063dSJacob Faibussowitsch       else PetscCall(PetscLogStageRegister(name, &_stageNum)); \
7099566063dSJacob Faibussowitsch       PetscCall(PetscLogStageSetActive(_stageNum, (PetscBool)(!PetscPreLoadMax || PetscPreLoadIt))); \
7109566063dSJacob Faibussowitsch       PetscCall(PetscLogStagePush(_stageNum));
7118e58c17dSMatthew Knepley 
712204a6943SJed Brown #define PetscPreLoadEnd() \
7139566063dSJacob Faibussowitsch   PetscCall(PetscLogStagePop()); \
714204a6943SJed Brown   PetscPreLoading = PETSC_FALSE; \
715043328b6SSatish Balay   } \
7169371c9d4SSatish Balay   } \
7179371c9d4SSatish Balay   while (0)
7188e58c17dSMatthew Knepley 
7199371c9d4SSatish Balay #define PetscPreLoadStage(name) \
7209371c9d4SSatish Balay   do { \
7219566063dSJacob Faibussowitsch     PetscCall(PetscLogStagePop()); \
7229566063dSJacob Faibussowitsch     if (PetscPreLoadIt > 0) PetscCall(PetscLogStageGetId(name, &_stageNum)); \
7239566063dSJacob Faibussowitsch     else PetscCall(PetscLogStageRegister(name, &_stageNum)); \
7249566063dSJacob Faibussowitsch     PetscCall(PetscLogStageSetActive(_stageNum, (PetscBool)(!PetscPreLoadMax || PetscPreLoadIt))); \
7259566063dSJacob Faibussowitsch     PetscCall(PetscLogStagePush(_stageNum)); \
726204a6943SJed Brown   } while (0)
727e9fa29b7SSatish Balay 
728ad14c47eSJed Brown /* some vars for logging */
729ad14c47eSJed Brown PETSC_EXTERN PetscBool PetscPreLoadingUsed; /* true if we are or have done preloading */
730ad14c47eSJed Brown PETSC_EXTERN PetscBool PetscPreLoadingOn;   /* true if we are currently in a preloading calculation */
731ad14c47eSJed Brown 
73297bb86f7SLois Curfman McInnes #endif
733