xref: /petsc/include/petsclog.h (revision 2611ad710242b3c70d66651ef7e40f9450d305e2)
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>
96873511fSToby Isaac #include <petscbt.h>
101c1ad86eSToby Isaac #include <petsclogtypes.h>
11c8d78d4dSSatish Balay 
121c1ad86eSToby Isaac /* SUBMANSEC = Profiling */
13ac09b921SBarry Smith 
14fa2bb9feSLisandro Dalcin /* General logging of information; different from event logging */
153ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscInfo_Private(const char[], PetscObject, const char[], ...) PETSC_ATTRIBUTE_FORMAT(3, 4);
16fa2bb9feSLisandro Dalcin #if defined(PETSC_USE_INFO)
177d3de750SJacob Faibussowitsch   #define PetscInfo(A, ...) PetscInfo_Private(PETSC_FUNCTION_NAME, ((PetscObject)A), __VA_ARGS__)
18fa2bb9feSLisandro Dalcin #else
193ba16761SJacob Faibussowitsch   #define PetscInfo(A, ...) PETSC_SUCCESS
20fa2bb9feSLisandro Dalcin #endif
21e94e781bSJacob Faibussowitsch 
22edd03b47SJacob Faibussowitsch #define PetscInfo1(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "PetscInfo()", ) PetscInfo(__VA_ARGS__)
23edd03b47SJacob Faibussowitsch #define PetscInfo2(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "PetscInfo()", ) PetscInfo(__VA_ARGS__)
24edd03b47SJacob Faibussowitsch #define PetscInfo3(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "PetscInfo()", ) PetscInfo(__VA_ARGS__)
25edd03b47SJacob Faibussowitsch #define PetscInfo4(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "PetscInfo()", ) PetscInfo(__VA_ARGS__)
26edd03b47SJacob Faibussowitsch #define PetscInfo5(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "PetscInfo()", ) PetscInfo(__VA_ARGS__)
27edd03b47SJacob Faibussowitsch #define PetscInfo6(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "PetscInfo()", ) PetscInfo(__VA_ARGS__)
28edd03b47SJacob Faibussowitsch #define PetscInfo7(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "PetscInfo()", ) PetscInfo(__VA_ARGS__)
29edd03b47SJacob Faibussowitsch #define PetscInfo8(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "PetscInfo()", ) PetscInfo(__VA_ARGS__)
30edd03b47SJacob Faibussowitsch #define PetscInfo9(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "PetscInfo()", ) PetscInfo(__VA_ARGS__)
317d3de750SJacob Faibussowitsch 
32e94e781bSJacob Faibussowitsch /*E
3316a05f60SBarry Smith   PetscInfoCommFlag - Describes the method by which to filter information displayed by `PetscInfo()` by communicator size
34e94e781bSJacob Faibussowitsch 
35667f096bSBarry Smith   Values:
36d1f92df0SBarry Smith + `PETSC_INFO_COMM_ALL` - Default uninitialized value. `PetscInfo()` will not filter based on
378b86dd2eSJacob Faibussowitsch                           communicator size (i.e. will print for all communicators)
38d1f92df0SBarry Smith . `PETSC_INFO_COMM_NO_SELF` - `PetscInfo()` will NOT print for communicators with size = 1 (i.e. *_COMM_SELF)
39d1f92df0SBarry Smith - `PETSC_INFO_COMM_ONLY_SELF` - `PetscInfo()` will ONLY print for communicators with size = 1
40e94e781bSJacob Faibussowitsch 
41e94e781bSJacob Faibussowitsch   Level: intermediate
42e94e781bSJacob Faibussowitsch 
43667f096bSBarry Smith   Note:
44667f096bSBarry Smith   Used as an input for `PetscInfoSetFilterCommSelf()`
45667f096bSBarry Smith 
46db781477SPatrick Sanan .seealso: `PetscInfo()`, `PetscInfoSetFromOptions()`, `PetscInfoSetFilterCommSelf()`
47e94e781bSJacob Faibussowitsch E*/
48e94e781bSJacob Faibussowitsch typedef enum {
49e94e781bSJacob Faibussowitsch   PETSC_INFO_COMM_ALL       = -1,
50e94e781bSJacob Faibussowitsch   PETSC_INFO_COMM_NO_SELF   = 0,
51e94e781bSJacob Faibussowitsch   PETSC_INFO_COMM_ONLY_SELF = 1
52e94e781bSJacob Faibussowitsch } PetscInfoCommFlag;
53e94e781bSJacob Faibussowitsch 
54e94e781bSJacob Faibussowitsch PETSC_EXTERN const char *const PetscInfoCommFlags[];
55fa2bb9feSLisandro Dalcin PETSC_EXTERN PetscErrorCode    PetscInfoDeactivateClass(PetscClassId);
56fa2bb9feSLisandro Dalcin PETSC_EXTERN PetscErrorCode    PetscInfoActivateClass(PetscClassId);
57e94e781bSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode    PetscInfoEnabled(PetscClassId, PetscBool *);
58e94e781bSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode    PetscInfoAllow(PetscBool);
59e94e781bSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode    PetscInfoSetFile(const char[], const char[]);
60e94e781bSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode    PetscInfoGetFile(char **, FILE **);
61e94e781bSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode    PetscInfoSetClasses(PetscBool, PetscInt, const char *const *);
62e94e781bSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode    PetscInfoGetClass(const char *, PetscBool *);
63e94e781bSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode    PetscInfoGetInfo(PetscBool *, PetscBool *, PetscBool *, PetscBool *, PetscInfoCommFlag *);
648b86dd2eSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode    PetscInfoProcessClass(const char[], PetscInt, const PetscClassId[]);
65e94e781bSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode    PetscInfoSetFilterCommSelf(PetscInfoCommFlag);
66e94e781bSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode    PetscInfoSetFromOptions(PetscOptions);
67e94e781bSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode    PetscInfoDestroy(void);
68fa2bb9feSLisandro Dalcin PETSC_EXTERN PetscBool         PetscLogPrintInfo; /* if true, indicates PetscInfo() is turned on */
69fa2bb9feSLisandro Dalcin 
708ba1e511SMatthew Knepley #define PETSC_EVENT 1311311
71014dd563SJed Brown PETSC_EXTERN PetscLogEvent PETSC_LARGEST_EVENT;
7297bb86f7SLois Curfman McInnes 
731c1ad86eSToby Isaac PETSC_EXTERN PetscErrorCode PetscIntStackCreate(PetscIntStack *);
741c1ad86eSToby Isaac PETSC_EXTERN PetscErrorCode PetscIntStackDestroy(PetscIntStack);
751c1ad86eSToby Isaac PETSC_EXTERN PetscErrorCode PetscIntStackPush(PetscIntStack, int);
761c1ad86eSToby Isaac PETSC_EXTERN PetscErrorCode PetscIntStackPop(PetscIntStack, int *);
771c1ad86eSToby Isaac PETSC_EXTERN PetscErrorCode PetscIntStackTop(PetscIntStack, int *);
781c1ad86eSToby Isaac PETSC_EXTERN PetscErrorCode PetscIntStackEmpty(PetscIntStack, PetscBool *);
791c1ad86eSToby Isaac 
806873511fSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStateCreate(PetscLogState *);
816873511fSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStateDestroy(PetscLogState *);
826873511fSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStateGetRegistry(PetscLogState, PetscLogRegistry *);
836873511fSToby Isaac 
846873511fSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStateClassRegister(PetscLogState, const char[], PetscClassId, PetscLogStage *);
856873511fSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStateClassSetActive(PetscLogState, PetscLogStage, PetscClassId, PetscBool);
866873511fSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStateClassSetActiveAll(PetscLogState, PetscClassId, PetscBool);
876873511fSToby Isaac 
886873511fSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStateStageRegister(PetscLogState, const char[], PetscLogStage *);
896873511fSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStateStagePush(PetscLogState, PetscLogStage);
906873511fSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStateStagePop(PetscLogState);
916873511fSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStateStageSetActive(PetscLogState, PetscLogStage, PetscBool);
926873511fSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStateStageGetActive(PetscLogState, PetscLogStage, PetscBool *);
936873511fSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStateGetCurrentStage(PetscLogState, PetscLogStage *);
946873511fSToby Isaac 
956873511fSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStateEventRegister(PetscLogState, const char[], PetscClassId, PetscLogEvent *);
966873511fSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStateEventSetCollective(PetscLogState, PetscLogEvent, PetscBool);
976873511fSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStateEventSetActive(PetscLogState, PetscLogStage, PetscLogEvent, PetscBool);
986873511fSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStateEventSetActiveAll(PetscLogState, PetscLogEvent, PetscBool);
996873511fSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStateEventGetActive(PetscLogState, PetscLogStage, PetscLogEvent, PetscBool *);
1006873511fSToby Isaac 
1016873511fSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStateGetEventFromName(PetscLogState, const char[], PetscLogEvent *);
1026873511fSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStateGetStageFromName(PetscLogState, const char[], PetscLogStage *);
1036873511fSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStateGetClassFromName(PetscLogState, const char[], PetscLogClass *);
1046873511fSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStateGetClassFromClassId(PetscLogState, PetscClassId, PetscLogClass *);
1056873511fSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStateGetNumEvents(PetscLogState, PetscInt *);
1066873511fSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStateGetNumStages(PetscLogState, PetscInt *);
1076873511fSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStateGetNumClasses(PetscLogState, PetscInt *);
1086873511fSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStateEventGetInfo(PetscLogState, PetscLogEvent, PetscLogEventInfo *);
1096873511fSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStateStageGetInfo(PetscLogState, PetscLogStage, PetscLogStageInfo *);
1106873511fSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStateClassGetInfo(PetscLogState, PetscLogClass, PetscLogClassInfo *);
1116873511fSToby Isaac 
11219ef957cSToby Isaac PETSC_EXTERN PetscClassId PETSCLOGHANDLER_CLASSID;
11319ef957cSToby Isaac 
11419ef957cSToby Isaac PETSC_EXTERN PetscFunctionList PetscLogHandlerList;
11519ef957cSToby Isaac 
11619ef957cSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogHandlerRegister(const char[], PetscErrorCode (*)(PetscLogHandler));
11719ef957cSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogHandlerCreate(MPI_Comm, PetscLogHandler *);
11819ef957cSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogHandlerSetType(PetscLogHandler, PetscLogHandlerType);
11919ef957cSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogHandlerGetType(PetscLogHandler, PetscLogHandlerType *);
12019ef957cSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogHandlerDestroy(PetscLogHandler *);
12119ef957cSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogHandlerSetState(PetscLogHandler, PetscLogState);
12219ef957cSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogHandlerGetState(PetscLogHandler, PetscLogState *);
12319ef957cSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogHandlerEventBegin(PetscLogHandler, PetscLogEvent, PetscObject, PetscObject, PetscObject, PetscObject);
12419ef957cSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogHandlerEventEnd(PetscLogHandler, PetscLogEvent, PetscObject, PetscObject, PetscObject, PetscObject);
12519ef957cSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogHandlerEventSync(PetscLogHandler, PetscLogEvent, MPI_Comm);
12619ef957cSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogHandlerObjectCreate(PetscLogHandler, PetscObject);
12719ef957cSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogHandlerObjectDestroy(PetscLogHandler, PetscObject);
12819ef957cSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogHandlerStagePush(PetscLogHandler, PetscLogStage);
12919ef957cSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogHandlerStagePop(PetscLogHandler, PetscLogStage);
13019ef957cSToby Isaac PETSC_EXTERN PetscErrorCode PetscLogHandlerView(PetscLogHandler, PetscViewer);
13119ef957cSToby Isaac 
1326467efc9SToby Isaac PETSC_EXTERN PetscErrorCode PetscLogHandlerCreateTrace(MPI_Comm, FILE *, PetscLogHandler *);
1336467efc9SToby Isaac 
1346873511fSToby Isaac /* All events are inactive if an invalid stage is set, like if there have been more stage pops than stage pushes */
1356873511fSToby Isaac #define PetscLogStateStageEventIsActive(state, stage, event) ((stage >= 0) && PetscBTLookup((state)->active, (stage)) && PetscBTLookup((state)->active, (stage) + (event + 1) * (state)->bt_num_stages))
1366873511fSToby Isaac 
13753e0a2f3SToby Isaac /* PetscLogHandler with critical methods exposed for speed */
13853e0a2f3SToby Isaac typedef struct _n_PetscLogHandlerHot {
13953e0a2f3SToby Isaac   PetscLogHandler handler;
14053e0a2f3SToby Isaac   PetscErrorCode (*eventBegin)(PetscLogHandler, PetscLogEvent, PetscObject, PetscObject, PetscObject, PetscObject);
14153e0a2f3SToby Isaac   PetscErrorCode (*eventEnd)(PetscLogHandler, PetscLogEvent, PetscObject, PetscObject, PetscObject, PetscObject);
14253e0a2f3SToby Isaac   PetscErrorCode (*eventSync)(PetscLogHandler, PetscLogEvent, MPI_Comm);
14353e0a2f3SToby Isaac   PetscErrorCode (*objectCreate)(PetscLogHandler, PetscObject);
14453e0a2f3SToby Isaac   PetscErrorCode (*objectDestroy)(PetscLogHandler, PetscObject);
14553e0a2f3SToby Isaac } PetscLogHandlerHot;
14653e0a2f3SToby Isaac 
147c708d6e3SStefano Zampini /* Handle multithreading */
148c708d6e3SStefano Zampini #if defined(PETSC_HAVE_THREADSAFETY)
149c708d6e3SStefano Zampini   #if defined(__cplusplus)
150c708d6e3SStefano Zampini     #define PETSC_TLS thread_local
151c708d6e3SStefano Zampini   #else
152c708d6e3SStefano Zampini     #define PETSC_TLS _Thread_local
153c708d6e3SStefano Zampini   #endif
154c708d6e3SStefano Zampini   #define PETSC_EXTERN_TLS extern PETSC_TLS PETSC_VISIBILITY_PUBLIC
155c708d6e3SStefano Zampini PETSC_EXTERN PetscErrorCode PetscAddLogDouble(PetscLogDouble *, PetscLogDouble *, PetscLogDouble);
156c708d6e3SStefano Zampini PETSC_EXTERN PetscErrorCode PetscAddLogDoubleCnt(PetscLogDouble *, PetscLogDouble *, PetscLogDouble *, PetscLogDouble *, PetscLogDouble);
157c708d6e3SStefano Zampini #else
158c708d6e3SStefano Zampini   #define PETSC_EXTERN_TLS PETSC_EXTERN
159c708d6e3SStefano Zampini   #define PETSC_TLS
1603ba16761SJacob Faibussowitsch   #define PetscAddLogDouble(a, b, c)          ((PetscErrorCode)((*(a) += (c), PETSC_SUCCESS) || ((*(b) += (c)), PETSC_SUCCESS)))
1613ba16761SJacob Faibussowitsch   #define PetscAddLogDoubleCnt(a, b, c, d, e) ((PetscErrorCode)(PetscAddLogDouble(a, c, 1) || PetscAddLogDouble(b, d, e)))
162c708d6e3SStefano Zampini #endif
16319b02663SBarry Smith 
164c60ec953SBarry Smith /*
165aa213bdcSJed Brown     PetscClassRegInfo, PetscClassPerfInfo - Each class has two data structures associated with it. The first has
166c60ec953SBarry Smith        static information about it, the second collects statistics on how many objects of the class are created,
167c60ec953SBarry Smith        how much memory they use, etc.
168c60ec953SBarry Smith 
169aa213bdcSJed Brown     PetscClassRegLog, PetscClassPerfLog - arrays of the PetscClassRegInfo and PetscClassPerfInfo for all classes.
170c60ec953SBarry Smith */
1716a6a9b46SSatish Balay typedef struct {
172c60ec953SBarry Smith   char        *name;    /* The class name */
173c60ec953SBarry Smith   PetscClassId classid; /* The integer identifying this class */
174aa213bdcSJed Brown } PetscClassRegInfo;
1756a6a9b46SSatish Balay 
1766a6a9b46SSatish Balay typedef struct {
1770700a824SBarry Smith   PetscClassId   id;           /* The integer identifying this class */
1786a6a9b46SSatish Balay   int            creations;    /* The number of objects of this class created */
1796a6a9b46SSatish Balay   int            destructions; /* The number of objects of this class destroyed */
1804851f57dSBarry Smith   PetscLogDouble mem;          /* The total memory allocated by objects of this class; this is completely wrong and should possibly be removed */
1814851f57dSBarry Smith   PetscLogDouble descMem;      /* The total memory allocated by descendents of these objects; this is completely wrong and should possibly be removed */
182aa213bdcSJed Brown } PetscClassPerfInfo;
1836a6a9b46SSatish Balay 
184aa213bdcSJed Brown typedef struct _n_PetscClassRegLog *PetscClassRegLog;
185aa213bdcSJed Brown struct _n_PetscClassRegLog {
186c60ec953SBarry Smith   int                numClasses; /* The number of classes registered */
187c60ec953SBarry Smith   int                maxClasses; /* The maximum number of classes */
188aa213bdcSJed Brown   PetscClassRegInfo *classInfo;  /* The structure for class information (classids are monotonicly increasing) */
189c60ec953SBarry Smith };
1906a6a9b46SSatish Balay 
191aa213bdcSJed Brown typedef struct _n_PetscClassPerfLog *PetscClassPerfLog;
192aa213bdcSJed Brown struct _n_PetscClassPerfLog {
193c60ec953SBarry Smith   int                 numClasses; /* The number of logging classes */
194c60ec953SBarry Smith   int                 maxClasses; /* The maximum number of classes */
195aa213bdcSJed Brown   PetscClassPerfInfo *classInfo;  /* The structure for class information (classids are monotonicly increasing) */
196c60ec953SBarry Smith };
19716a05f60SBarry Smith 
198c60ec953SBarry Smith /*
199aa213bdcSJed Brown     PetscEventRegInfo, PetscEventPerfInfo - Each event has two data structures associated with it. The first has
200c60ec953SBarry Smith        static information about it, the second collects statistics on how many times the event is used, how
201c60ec953SBarry Smith        much time it takes, etc.
202c60ec953SBarry Smith 
203aa213bdcSJed Brown     PetscEventRegLog, PetscEventPerfLog - an array of all PetscEventRegInfo and PetscEventPerfInfo for all events. There is one
204c60ec953SBarry Smith       of these for each stage.
205c60ec953SBarry Smith 
206c60ec953SBarry Smith */
2076a6a9b46SSatish Balay typedef struct {
2086a6a9b46SSatish Balay   char        *name;       /* The name of this event */
209c60ec953SBarry Smith   PetscClassId classid;    /* The class the event is associated with */
210217044c2SLisandro Dalcin   PetscBool    collective; /* Flag this event as collective */
211a0c7f9aaSSamuel Khuvis #if defined(PETSC_HAVE_TAU_PERFSTUBS)
212a0c7f9aaSSamuel Khuvis   void *timer; /* Associated external tool timer for this event */
213a0c7f9aaSSamuel Khuvis #endif
2146a6a9b46SSatish Balay #if defined(PETSC_HAVE_MPE)
2156a6a9b46SSatish Balay   int mpe_id_begin; /* MPE IDs that define the event */
2166a6a9b46SSatish Balay   int mpe_id_end;
2176a6a9b46SSatish Balay #endif
218aa213bdcSJed Brown } PetscEventRegInfo;
2196a6a9b46SSatish Balay 
220aa213bdcSJed Brown typedef struct _n_PetscEventRegLog *PetscEventRegLog;
221aa213bdcSJed Brown struct _n_PetscEventRegLog {
2226a6a9b46SSatish Balay   int                numEvents; /* The number of registered events */
2236a6a9b46SSatish Balay   int                maxEvents; /* The maximum number of events */
224aa213bdcSJed Brown   PetscEventRegInfo *eventInfo; /* The registration information for each event */
2256a6a9b46SSatish Balay };
2266a6a9b46SSatish Balay 
227aa213bdcSJed Brown typedef struct _n_PetscEventPerfLog *PetscEventPerfLog;
228aa213bdcSJed Brown struct _n_PetscEventPerfLog {
2296a6a9b46SSatish Balay   int                 numEvents; /* The number of logging events */
2306a6a9b46SSatish Balay   int                 maxEvents; /* The maximum number of events */
231aa213bdcSJed Brown   PetscEventPerfInfo *eventInfo; /* The performance information for each event */
2326a6a9b46SSatish Balay };
23316a05f60SBarry Smith 
234c60ec953SBarry Smith /*
235aa213bdcSJed Brown    PetscStageInfo - Contains all the information about a particular stage.
2366a6a9b46SSatish Balay 
237aa213bdcSJed Brown    PetscStageLog - An array of PetscStageInfo for each registered stage. There is a single one of these in the code.
238c60ec953SBarry Smith */
239aa213bdcSJed Brown typedef struct _PetscStageInfo {
2406a6a9b46SSatish Balay   char              *name;     /* The stage name */
241ace3abfcSBarry Smith   PetscBool          used;     /* The stage was pushed on this processor */
242aa213bdcSJed Brown   PetscEventPerfInfo perfInfo; /* The stage performance information */
243aa213bdcSJed Brown   PetscEventPerfLog  eventLog; /* The event information for this stage */
244aa213bdcSJed Brown   PetscClassPerfLog  classLog; /* The class information for this stage */
245a0c7f9aaSSamuel Khuvis #if defined(PETSC_HAVE_TAU_PERFSTUBS)
246a0c7f9aaSSamuel Khuvis   void *timer; /* Associated external tool timer for this stage */
247a0c7f9aaSSamuel Khuvis #endif
248aa213bdcSJed Brown } PetscStageInfo;
2496a6a9b46SSatish Balay 
250aa213bdcSJed Brown typedef struct _n_PetscStageLog *PetscStageLog;
251aa213bdcSJed Brown struct _n_PetscStageLog {
2526a6a9b46SSatish Balay   int              numStages; /* The number of registered stages */
2536a6a9b46SSatish Balay   int              maxStages; /* The maximum number of stages */
254aa213bdcSJed Brown   PetscIntStack    stack;     /* The stack for active stages */
255aa213bdcSJed Brown   int              curStage;  /* The current stage (only used in macros so we don't call PetscIntStackTop) */
256aa213bdcSJed Brown   PetscStageInfo  *stageInfo; /* The information for each stage */
257aa213bdcSJed Brown   PetscEventRegLog eventLog;  /* The registered events */
258aa213bdcSJed Brown   PetscClassRegLog classLog;  /* The registered classes */
2596a6a9b46SSatish Balay };
260fee518ebSJed Brown 
261edd03b47SJacob Faibussowitsch PETSC_DEPRECATED_FUNCTION(3, 18, 0, "PetscLogObjectParent()", ) static inline PetscErrorCode PetscLogObjectParent(PetscObject o, PetscObject p)
262d71ae5a4SJacob Faibussowitsch {
2634f839811SJacob Faibussowitsch   (void)o;
2644f839811SJacob Faibussowitsch   (void)p;
2653ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
2664dfa11a4SJacob Faibussowitsch }
267*2611ad71SToby Isaac #define PetscLogObjectParents(p, n, d) PetscMacroReturnStandard(for (int _i = 0; _i < (n); ++_i) PetscCall(PetscLogObjectParent((PetscObject)(p), (PetscObject)(d)[_i]));)
2684dfa11a4SJacob Faibussowitsch 
269edd03b47SJacob Faibussowitsch PETSC_DEPRECATED_FUNCTION(3, 18, 0, "PetscLogObjectMemory()", ) static inline PetscErrorCode PetscLogObjectMemory(PetscObject o, PetscLogDouble m)
270d71ae5a4SJacob Faibussowitsch {
2714f839811SJacob Faibussowitsch   (void)o;
2724f839811SJacob Faibussowitsch   (void)m;
2733ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
2744dfa11a4SJacob Faibussowitsch }
2753bb1ff40SBarry Smith 
276*2611ad71SToby Isaac /* Global flop counter */
277*2611ad71SToby Isaac PETSC_EXTERN PetscLogDouble petsc_TotalFlops;
278*2611ad71SToby Isaac PETSC_EXTERN PetscLogDouble petsc_irecv_ct;
279*2611ad71SToby Isaac PETSC_EXTERN PetscLogDouble petsc_isend_ct;
280*2611ad71SToby Isaac PETSC_EXTERN PetscLogDouble petsc_recv_ct;
281*2611ad71SToby Isaac PETSC_EXTERN PetscLogDouble petsc_send_ct;
282*2611ad71SToby Isaac PETSC_EXTERN PetscLogDouble petsc_irecv_len;
283*2611ad71SToby Isaac PETSC_EXTERN PetscLogDouble petsc_isend_len;
284*2611ad71SToby Isaac PETSC_EXTERN PetscLogDouble petsc_recv_len;
285*2611ad71SToby Isaac PETSC_EXTERN PetscLogDouble petsc_send_len;
286*2611ad71SToby Isaac PETSC_EXTERN PetscLogDouble petsc_allreduce_ct;
287*2611ad71SToby Isaac PETSC_EXTERN PetscLogDouble petsc_gather_ct;
288*2611ad71SToby Isaac PETSC_EXTERN PetscLogDouble petsc_scatter_ct;
289*2611ad71SToby Isaac PETSC_EXTERN PetscLogDouble petsc_wait_ct;
290*2611ad71SToby Isaac PETSC_EXTERN PetscLogDouble petsc_wait_any_ct;
291*2611ad71SToby Isaac PETSC_EXTERN PetscLogDouble petsc_wait_all_ct;
292*2611ad71SToby Isaac PETSC_EXTERN PetscLogDouble petsc_sum_of_waits_ct;
293*2611ad71SToby Isaac 
294*2611ad71SToby Isaac /* Thread local storage */
295*2611ad71SToby Isaac PETSC_EXTERN_TLS PetscLogDouble petsc_TotalFlops_th;
296*2611ad71SToby Isaac PETSC_EXTERN_TLS PetscLogDouble petsc_irecv_ct_th;
297*2611ad71SToby Isaac PETSC_EXTERN_TLS PetscLogDouble petsc_isend_ct_th;
298*2611ad71SToby Isaac PETSC_EXTERN_TLS PetscLogDouble petsc_recv_ct_th;
299*2611ad71SToby Isaac PETSC_EXTERN_TLS PetscLogDouble petsc_send_ct_th;
300*2611ad71SToby Isaac PETSC_EXTERN_TLS PetscLogDouble petsc_irecv_len_th;
301*2611ad71SToby Isaac PETSC_EXTERN_TLS PetscLogDouble petsc_isend_len_th;
302*2611ad71SToby Isaac PETSC_EXTERN_TLS PetscLogDouble petsc_recv_len_th;
303*2611ad71SToby Isaac PETSC_EXTERN_TLS PetscLogDouble petsc_send_len_th;
304*2611ad71SToby Isaac PETSC_EXTERN_TLS PetscLogDouble petsc_allreduce_ct_th;
305*2611ad71SToby Isaac PETSC_EXTERN_TLS PetscLogDouble petsc_gather_ct_th;
306*2611ad71SToby Isaac PETSC_EXTERN_TLS PetscLogDouble petsc_scatter_ct_th;
307*2611ad71SToby Isaac PETSC_EXTERN_TLS PetscLogDouble petsc_wait_ct_th;
308*2611ad71SToby Isaac PETSC_EXTERN_TLS PetscLogDouble petsc_wait_any_ct_th;
309*2611ad71SToby Isaac PETSC_EXTERN_TLS PetscLogDouble petsc_wait_all_ct_th;
310*2611ad71SToby Isaac PETSC_EXTERN_TLS PetscLogDouble petsc_sum_of_waits_ct_th;
311*2611ad71SToby Isaac 
312*2611ad71SToby Isaac /* Global GPU counters */
313*2611ad71SToby Isaac PETSC_EXTERN PetscLogDouble petsc_ctog_ct;
314*2611ad71SToby Isaac PETSC_EXTERN PetscLogDouble petsc_gtoc_ct;
315*2611ad71SToby Isaac PETSC_EXTERN PetscLogDouble petsc_ctog_sz;
316*2611ad71SToby Isaac PETSC_EXTERN PetscLogDouble petsc_gtoc_sz;
317*2611ad71SToby Isaac PETSC_EXTERN PetscLogDouble petsc_ctog_ct_scalar;
318*2611ad71SToby Isaac PETSC_EXTERN PetscLogDouble petsc_gtoc_ct_scalar;
319*2611ad71SToby Isaac PETSC_EXTERN PetscLogDouble petsc_ctog_sz_scalar;
320*2611ad71SToby Isaac PETSC_EXTERN PetscLogDouble petsc_gtoc_sz_scalar;
321*2611ad71SToby Isaac PETSC_EXTERN PetscLogDouble petsc_gflops;
322*2611ad71SToby Isaac PETSC_EXTERN PetscLogDouble petsc_gtime;
323*2611ad71SToby Isaac 
324*2611ad71SToby Isaac /* Thread local storage */
325*2611ad71SToby Isaac PETSC_EXTERN_TLS PetscLogDouble petsc_ctog_ct_th;
326*2611ad71SToby Isaac PETSC_EXTERN_TLS PetscLogDouble petsc_gtoc_ct_th;
327*2611ad71SToby Isaac PETSC_EXTERN_TLS PetscLogDouble petsc_ctog_sz_th;
328*2611ad71SToby Isaac PETSC_EXTERN_TLS PetscLogDouble petsc_gtoc_sz_th;
329*2611ad71SToby Isaac PETSC_EXTERN_TLS PetscLogDouble petsc_ctog_ct_scalar_th;
330*2611ad71SToby Isaac PETSC_EXTERN_TLS PetscLogDouble petsc_gtoc_ct_scalar_th;
331*2611ad71SToby Isaac PETSC_EXTERN_TLS PetscLogDouble petsc_ctog_sz_scalar_th;
332*2611ad71SToby Isaac PETSC_EXTERN_TLS PetscLogDouble petsc_gtoc_sz_scalar_th;
333*2611ad71SToby Isaac PETSC_EXTERN_TLS PetscLogDouble petsc_gflops_th;
334*2611ad71SToby Isaac PETSC_EXTERN_TLS PetscLogDouble petsc_gtime_th;
335*2611ad71SToby Isaac 
336*2611ad71SToby Isaac PETSC_EXTERN PetscBool PetscLogMemory;
337*2611ad71SToby Isaac PETSC_EXTERN PetscBool PetscLogSyncOn; /* true if logging synchronization is enabled */
338*2611ad71SToby Isaac 
339*2611ad71SToby Isaac PETSC_EXTERN PetscLogState petsc_log_state;
340*2611ad71SToby Isaac 
341*2611ad71SToby Isaac #define PETSC_LOG_HANDLER_MAX 4
342*2611ad71SToby Isaac PETSC_EXTERN PetscLogHandlerHot PetscLogHandlers[PETSC_LOG_HANDLER_MAX];
343*2611ad71SToby Isaac 
344aa482453SBarry Smith #if defined(PETSC_USE_LOG) /* --- Logging is turned on --------------------------------*/
345aeeda9f6SJohn Fettig PETSC_EXTERN PetscStageLog  petsc_stageLog;
346f5d6ab90SLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscLogGetStageLog(PetscStageLog *);
347f5d6ab90SLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscStageLogGetCurrent(PetscStageLog, int *);
348f5d6ab90SLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscStageLogGetEventPerfLog(PetscStageLog, int, PetscEventPerfLog *);
349614700edSBarry Smith 
350fa2bb9feSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscGetFlops(PetscLogDouble *);
35177c4ece6SBarry Smith 
352014dd563SJed Brown PETSC_EXTERN PetscErrorCode (*PetscLogPLB)(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject);
353014dd563SJed Brown PETSC_EXTERN PetscErrorCode (*PetscLogPLE)(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject);
354014dd563SJed Brown PETSC_EXTERN PetscErrorCode (*PetscLogPHC)(PetscObject);
355014dd563SJed Brown PETSC_EXTERN PetscErrorCode (*PetscLogPHD)(PetscObject);
35677c4ece6SBarry Smith 
3573ba16761SJacob Faibussowitsch   #define PetscLogObjectCreate(h)  ((PetscLogPHC) ? (*PetscLogPHC)((PetscObject)(h)) : PETSC_SUCCESS)
3583ba16761SJacob Faibussowitsch   #define PetscLogObjectDestroy(h) ((PetscLogPHD) ? (*PetscLogPHD)((PetscObject)(h)) : PETSC_SUCCESS)
3593ca90d2dSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscLogObjectState(PetscObject, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);
360fa2bb9feSLisandro Dalcin 
3618ba1e511SMatthew Knepley /* Initialization functions */
362bb1d7374SBarry Smith PETSC_EXTERN PetscErrorCode PetscLogDefaultBegin(void);
363014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogAllBegin(void);
364bb1d7374SBarry Smith PETSC_EXTERN PetscErrorCode PetscLogNestedBegin(void);
365014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogTraceBegin(FILE *);
366*2611ad71SToby Isaac PETSC_EXTERN PetscErrorCode PetscLogMPEBegin(void);
367*2611ad71SToby Isaac PETSC_EXTERN PetscErrorCode PetscLogPerfstubsBegin(void);
368*2611ad71SToby Isaac PETSC_EXTERN PetscErrorCode PetscLogLegacyCallbacksBegin(PetscErrorCode (*)(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject), PetscErrorCode (*)(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject), PetscErrorCode (*)(PetscObject), PetscErrorCode (*)(PetscObject));
369014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogActions(PetscBool);
370014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogObjects(PetscBool);
371fa2bb9feSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscLogSetThreshold(PetscLogDouble, PetscLogDouble *);
3729371c9d4SSatish Balay PETSC_EXTERN PetscErrorCode PetscLogSet(PetscErrorCode (*)(int, int, PetscObject, PetscObject, PetscObject, PetscObject), PetscErrorCode (*)(int, int, PetscObject, PetscObject, PetscObject, PetscObject));
373fa2bb9feSLisandro Dalcin 
3748ba1e511SMatthew Knepley /* Output functions */
375014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogView(PetscViewer);
376f14045dbSBarry Smith PETSC_EXTERN PetscErrorCode PetscLogViewFromOptions(void);
377014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogDump(const char[]);
378*2611ad71SToby Isaac PETSC_EXTERN PetscErrorCode PetscLogMPEDump(const char[]);
37931d06abdSBarry Smith 
38053e0a2f3SToby Isaac PETSC_EXTERN PetscErrorCode PetscLogGetState(PetscLogState *);
38153e0a2f3SToby Isaac PETSC_EXTERN PetscErrorCode PetscLogHandlerStart(PetscLogHandler);
38253e0a2f3SToby Isaac PETSC_EXTERN PetscErrorCode PetscLogHandlerStop(PetscLogHandler);
38353e0a2f3SToby Isaac 
3844dd65854SConnor Ward /* Status checking functions */
3854dd65854SConnor Ward PETSC_EXTERN PetscErrorCode PetscLogIsActive(PetscBool *);
3864dd65854SConnor Ward 
387fa2bb9feSLisandro Dalcin /* Stage functions */
388014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogStageRegister(const char[], PetscLogStage *);
389014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogStagePush(PetscLogStage);
390014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogStagePop(void);
391014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogStageSetActive(PetscLogStage, PetscBool);
392014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogStageGetActive(PetscLogStage, PetscBool *);
393014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogStageSetVisible(PetscLogStage, PetscBool);
394014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogStageGetVisible(PetscLogStage, PetscBool *);
395014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogStageGetId(const char[], PetscLogStage *);
39653e0a2f3SToby Isaac PETSC_EXTERN PetscErrorCode PetscLogStageGetName(PetscLogEvent, const char **);
397f5d6ab90SLisandro Dalcin 
3988ba1e511SMatthew Knepley /* Event functions */
399014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogEventRegister(const char[], PetscClassId, PetscLogEvent *);
400217044c2SLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscLogEventSetCollective(PetscLogEvent, PetscBool);
401fa2bb9feSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscLogEventIncludeClass(PetscClassId);
402fa2bb9feSLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscLogEventExcludeClass(PetscClassId);
403014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogEventActivate(PetscLogEvent);
404014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogEventDeactivate(PetscLogEvent);
405c00cb57fSBarry Smith PETSC_EXTERN PetscErrorCode PetscLogEventDeactivatePush(PetscLogEvent);
406c00cb57fSBarry Smith PETSC_EXTERN PetscErrorCode PetscLogEventDeactivatePop(PetscLogEvent);
407014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogEventSetActiveAll(PetscLogEvent, PetscBool);
408014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogEventActivateClass(PetscClassId);
409014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogEventDeactivateClass(PetscClassId);
4108c49a8d9SBarry Smith PETSC_EXTERN PetscErrorCode PetscLogEventGetId(const char[], PetscLogEvent *);
41153e0a2f3SToby Isaac PETSC_EXTERN PetscErrorCode PetscLogEventGetName(PetscLogEvent, const char **);
41253e0a2f3SToby Isaac PETSC_EXTERN PetscErrorCode PetscLogEventGetPerfInfo(PetscLogStage, PetscLogEvent, PetscEventPerfInfo *);
413891e75beSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscLogEventSetDof(PetscLogEvent, PetscInt, PetscLogDouble);
4145d68e14cSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscLogEventSetError(PetscLogEvent, PetscInt, PetscLogDouble);
4157a101e5eSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscLogPushCurrentEvent_Internal(PetscLogEvent);
4167a101e5eSJacob Faibussowitsch PETSC_EXTERN PetscErrorCode PetscLogPopCurrentEvent_Internal(void);
41753e0a2f3SToby Isaac PETSC_EXTERN PetscErrorCode PetscLogEventsPause(void);
41853e0a2f3SToby Isaac PETSC_EXTERN PetscErrorCode PetscLogEventsResume(void);
41953e0a2f3SToby Isaac 
42053e0a2f3SToby Isaac /* Class functions */
42153e0a2f3SToby Isaac PETSC_EXTERN PetscErrorCode PetscLogClassGetClassId(const char[], PetscClassId *);
42253e0a2f3SToby Isaac PETSC_EXTERN PetscErrorCode PetscLogClassIdGetName(PetscClassId, const char **);
4238ba1e511SMatthew Knepley 
42462872c28SLisandro Dalcin PETSC_EXTERN PetscErrorCode PetscLogEventSynchronize(PetscLogEvent, MPI_Comm);
42562872c28SLisandro Dalcin 
42662872c28SLisandro Dalcin   #define PetscLogEventSync(e, comm) \
4273ba16761SJacob Faibussowitsch     ((PetscErrorCode)(((PetscLogPLB && petsc_stageLog->stageInfo[petsc_stageLog->curStage].perfInfo.active && petsc_stageLog->stageInfo[petsc_stageLog->curStage].eventLog->eventInfo[e].active) ? PetscLogEventSynchronize((e), (comm)) : PETSC_SUCCESS)))
42877c4ece6SBarry Smith 
42952e6d16bSBarry Smith   #define PetscLogEventBegin(e, o1, o2, o3, o4) \
4303ba16761SJacob Faibussowitsch     ((PetscErrorCode)((PetscLogPLB && petsc_stageLog->stageInfo[petsc_stageLog->curStage].perfInfo.active && petsc_stageLog->stageInfo[petsc_stageLog->curStage].eventLog->eventInfo[e].active) ? (PetscErrorCode)(((*PetscLogPLB)((e), 0, (PetscObject)(o1), (PetscObject)(o2), (PetscObject)(o3), (PetscObject)(o4))) || PetscLogPushCurrentEvent_Internal(e)) : PETSC_SUCCESS))
431043328b6SSatish Balay 
43252e6d16bSBarry Smith   #define PetscLogEventEnd(e, o1, o2, o3, o4) \
4333ba16761SJacob Faibussowitsch     ((PetscErrorCode)((PetscLogPLE && petsc_stageLog->stageInfo[petsc_stageLog->curStage].perfInfo.active && petsc_stageLog->stageInfo[petsc_stageLog->curStage].eventLog->eventInfo[e].active) ? (PetscErrorCode)(((*PetscLogPLE)((e), 0, (PetscObject)(o1), (PetscObject)(o2), (PetscObject)(o3), (PetscObject)(o4))) || PetscLogPopCurrentEvent_Internal()) : PETSC_SUCCESS))
43477c4ece6SBarry Smith 
435014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogEventGetFlops(PetscLogEvent, PetscLogDouble *);
436014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscLogEventZeroFlops(PetscLogEvent);
437003131ecSBarry Smith 
438c708d6e3SStefano Zampini   /*
439c708d6e3SStefano Zampini    Flop counting:  We count each arithmetic operation (e.g., addition, multiplication) separately.
440c708d6e3SStefano Zampini 
441c708d6e3SStefano Zampini    For the complex numbers version, note that
442c708d6e3SStefano Zampini        1 complex addition = 2 flops
443c708d6e3SStefano Zampini        1 complex multiplication = 6 flops,
444c708d6e3SStefano Zampini    where we define 1 flop as that for a double precision scalar.  We roughly approximate
445c708d6e3SStefano Zampini    flop counting for complex numbers by multiplying the total flops by 4; this corresponds
446c708d6e3SStefano Zampini    to the assumption that we're counting mostly additions and multiplications -- and
447c708d6e3SStefano Zampini    roughly the same number of each.  More accurate counting could be done by distinguishing
448c708d6e3SStefano Zampini    among the various arithmetic operations.
449c708d6e3SStefano Zampini  */
450c708d6e3SStefano Zampini 
451c708d6e3SStefano Zampini   #if defined(PETSC_USE_COMPLEX)
452c708d6e3SStefano Zampini     #define PETSC_FLOPS_PER_OP 4.0
453c708d6e3SStefano Zampini   #else
454c708d6e3SStefano Zampini     #define PETSC_FLOPS_PER_OP 1.0
455c708d6e3SStefano Zampini   #endif
456c708d6e3SStefano Zampini 
457c708d6e3SStefano Zampini /*@C
458c708d6e3SStefano Zampini        PetscLogFlops - Log how many flops are performed in a calculation
459c708d6e3SStefano Zampini 
460c708d6e3SStefano Zampini    Input Parameter:
461c708d6e3SStefano Zampini .   flops - the number of flops
462c708d6e3SStefano Zampini 
463c708d6e3SStefano Zampini    Level: intermediate
464c708d6e3SStefano Zampini 
465c200f75eSSatish Balay    Note:
466c200f75eSSatish Balay      To limit the chance of integer overflow when multiplying by a constant, represent the constant as a double,
467c200f75eSSatish Balay      not an integer. Use `PetscLogFlops`(4.0*n) not `PetscLogFlops`(4*n)
468c200f75eSSatish Balay 
469c200f75eSSatish Balay .seealso: [](ch_profiling), `PetscLogView()`, `PetscLogGpuFlops()`
470c708d6e3SStefano Zampini @*/
471c708d6e3SStefano Zampini static inline PetscErrorCode PetscLogFlops(PetscLogDouble n)
472c708d6e3SStefano Zampini {
473c708d6e3SStefano Zampini   PetscAssert(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot log negative flops");
474c708d6e3SStefano Zampini   return PetscAddLogDouble(&petsc_TotalFlops, &petsc_TotalFlops_th, PETSC_FLOPS_PER_OP * n);
475c708d6e3SStefano Zampini }
476c708d6e3SStefano Zampini 
477ce85283eSBarry Smith   /*
478f621e05eSBarry Smith      These are used internally in the PETSc routines to keep a count of MPI messages and
479f621e05eSBarry Smith    their sizes.
480f621e05eSBarry Smith 
481af0996ceSBarry Smith      This does not work for MPI-Uni because our include/petsc/mpiuni/mpi.h file
482ce85283eSBarry Smith    uses macros to defined the MPI operations.
48315308354SBarry Smith 
48415308354SBarry Smith      It does not work correctly from HP-UX because it processes the
485bb4af37aSBarry Smith    macros in a way that sometimes it double counts, hence
486b6410449SSatish Balay    PETSC_HAVE_BROKEN_RECURSIVE_MACRO
4877c1e34a4SSatish Balay 
488f569fd43SBarry Smith      It does not work with Windows because winmpich lacks MPI_Type_size()
489ce85283eSBarry Smith */
490d5bc873cSPierre Jolivet   #if !defined(MPIUNI_H) && !defined(PETSC_HAVE_BROKEN_RECURSIVE_MACRO)
49177a39924SBarry Smith /*
49277a39924SBarry Smith    Logging of MPI activities
49377a39924SBarry Smith */
494c708d6e3SStefano Zampini static inline PetscErrorCode PetscMPITypeSize(PetscInt count, MPI_Datatype type, PetscLogDouble *length, PetscLogDouble *length_th)
495d71ae5a4SJacob Faibussowitsch {
496f12d10ccSJunchao Zhang   PetscMPIInt typesize;
4975f80ce2aSJacob Faibussowitsch 
4983ba16761SJacob Faibussowitsch   if (type == MPI_DATATYPE_NULL) return PETSC_SUCCESS;
4999566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Type_size(type, &typesize));
500c708d6e3SStefano Zampini   return PetscAddLogDouble(length, length_th, (PetscLogDouble)(count * typesize));
501f95db71bSBarry Smith }
50277a39924SBarry Smith 
503c708d6e3SStefano Zampini static inline PetscErrorCode PetscMPITypeSizeComm(MPI_Comm comm, const PetscMPIInt *counts, MPI_Datatype type, PetscLogDouble *length, PetscLogDouble *length_th)
504d71ae5a4SJacob Faibussowitsch {
505f12d10ccSJunchao Zhang   PetscMPIInt    typesize, size, p;
506c708d6e3SStefano Zampini   PetscLogDouble l;
5073b9284c0SMatthew G Knepley 
5083ba16761SJacob Faibussowitsch   if (type == MPI_DATATYPE_NULL) return PETSC_SUCCESS;
5099566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(comm, &size));
5109566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Type_size(type, &typesize));
511c708d6e3SStefano Zampini   for (p = 0, l = 0.0; p < size; ++p) l += (PetscLogDouble)(counts[p] * typesize);
512c708d6e3SStefano Zampini   return PetscAddLogDouble(length, length_th, l);
5133b9284c0SMatthew G Knepley }
5143b9284c0SMatthew G Knepley 
51572b11a25SBarry Smith /*
51672b11a25SBarry Smith     Returns 1 if the communicator is parallel else zero
51772b11a25SBarry Smith */
518d71ae5a4SJacob Faibussowitsch static inline int PetscMPIParallelComm(MPI_Comm comm)
519d71ae5a4SJacob Faibussowitsch {
5209371c9d4SSatish Balay   PetscMPIInt size;
5219371c9d4SSatish Balay   MPI_Comm_size(comm, &size);
5229371c9d4SSatish Balay   return size > 1;
52372b11a25SBarry Smith }
52472b11a25SBarry Smith 
525c708d6e3SStefano Zampini     #define MPI_Irecv(buf, count, datatype, source, tag, comm, request) \
526c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_irecv_ct, &petsc_irecv_ct_th, 1) || PetscMPITypeSize((count), (datatype), &(petsc_irecv_len), &(petsc_irecv_len_th)) || MPI_Irecv((buf), (count), (datatype), (source), (tag), (comm), (request)))
52715308354SBarry Smith 
528c708d6e3SStefano Zampini     #define MPI_Irecv_c(buf, count, datatype, source, tag, comm, request) \
529c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_irecv_ct, &petsc_irecv_ct_th, 1) || PetscMPITypeSize((count), (datatype), &(petsc_irecv_len), &(petsc_irecv_len_th)) || MPI_Irecv_c((buf), (count), (datatype), (source), (tag), (comm), (request)))
530c87b50c4SJunchao Zhang 
531c708d6e3SStefano Zampini     #define MPI_Isend(buf, count, datatype, dest, tag, comm, request) \
532c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_isend_ct, &petsc_isend_ct_th, 1) || PetscMPITypeSize((count), (datatype), &(petsc_isend_len), &(petsc_isend_len_th)) || MPI_Isend((buf), (count), (datatype), (dest), (tag), (comm), (request)))
53315308354SBarry Smith 
534c708d6e3SStefano Zampini     #define MPI_Isend_c(buf, count, datatype, dest, tag, comm, request) \
535c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_isend_ct, &petsc_isend_ct_th, 1) || PetscMPITypeSize((count), (datatype), &(petsc_isend_len), &(petsc_isend_len_th)) || MPI_Isend_c((buf), (count), (datatype), (dest), (tag), (comm), (request)))
536c87b50c4SJunchao Zhang 
537c708d6e3SStefano Zampini     #define MPI_Startall_irecv(count, datatype, number, requests) \
538c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_irecv_ct, &petsc_irecv_ct_th, number) || PetscMPITypeSize((count), (datatype), &(petsc_irecv_len), &(petsc_irecv_len_th)) || ((number) && MPI_Startall((number), (requests))))
5390d4b0b6cSBarry Smith 
540c708d6e3SStefano Zampini     #define MPI_Startall_isend(count, datatype, number, requests) \
541c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_isend_ct, &petsc_isend_ct_th, number) || PetscMPITypeSize((count), (datatype), &(petsc_isend_len), &(petsc_isend_len_th)) || ((number) && MPI_Startall((number), (requests))))
5420d4b0b6cSBarry Smith 
543c708d6e3SStefano Zampini     #define MPI_Start_isend(count, datatype, requests) (PetscAddLogDouble(&petsc_isend_ct, &petsc_isend_ct_th, 1) || PetscMPITypeSize((count), (datatype), (&petsc_isend_len), (&petsc_isend_len_th)) || MPI_Start((requests)))
5440d4b0b6cSBarry Smith 
545c708d6e3SStefano Zampini     #define MPI_Recv(buf, count, datatype, source, tag, comm, status) \
546c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_recv_ct, &petsc_recv_ct_th, 1) || PetscMPITypeSize((count), (datatype), (&petsc_recv_len), (&petsc_recv_len_th)) || MPI_Recv((buf), (count), (datatype), (source), (tag), (comm), (status)))
54715308354SBarry Smith 
548c708d6e3SStefano Zampini     #define MPI_Recv_c(buf, count, datatype, source, tag, comm, status) \
549c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_recv_ct, &petsc_recv_ct_th, 1) || PetscMPITypeSize((count), (datatype), (&petsc_recv_len), &(petsc_recv_len_th)) || MPI_Recv_c((buf), (count), (datatype), (source), (tag), (comm), (status)))
550c87b50c4SJunchao Zhang 
551c708d6e3SStefano Zampini     #define MPI_Send(buf, count, datatype, dest, tag, comm) \
552c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_send_ct, &petsc_send_ct_th, 1) || PetscMPITypeSize((count), (datatype), (&petsc_send_len), (&petsc_send_len_th)) || MPI_Send((buf), (count), (datatype), (dest), (tag), (comm)))
55377a39924SBarry Smith 
554c708d6e3SStefano Zampini     #define MPI_Send_c(buf, count, datatype, dest, tag, comm) \
555c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_send_ct, &petsc_send_ct_th, 1) || PetscMPITypeSize((count), (datatype), (&petsc_send_len), (&petsc_send_len_th)) || MPI_Send_c((buf), (count), (datatype), (dest), (tag), (comm)))
556c87b50c4SJunchao Zhang 
557c708d6e3SStefano Zampini     #define MPI_Wait(request, status) (PetscAddLogDouble(&petsc_wait_ct, &petsc_wait_ct_th, 1) || PetscAddLogDouble(&petsc_sum_of_waits_ct, &petsc_sum_of_waits_ct_th, 1) || MPI_Wait((request), (status)))
55877a39924SBarry Smith 
559c708d6e3SStefano Zampini     #define MPI_Waitany(a, b, c, d) (PetscAddLogDouble(&petsc_wait_any_ct, &petsc_wait_any_ct_th, 1) || PetscAddLogDouble(&petsc_sum_of_waits_ct, &petsc_sum_of_waits_ct_th, 1) || MPI_Waitany((a), (b), (c), (d)))
56077a39924SBarry Smith 
561c708d6e3SStefano Zampini     #define MPI_Waitall(count, array_of_requests, array_of_statuses) \
562c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_wait_all_ct, &petsc_wait_all_ct_th, 1) || PetscAddLogDouble(&petsc_sum_of_waits_ct, &petsc_sum_of_waits_ct_th, count) || MPI_Waitall((count), (array_of_requests), (array_of_statuses)))
56377a39924SBarry Smith 
564c708d6e3SStefano Zampini     #define MPI_Allreduce(sendbuf, recvbuf, count, datatype, op, comm) (PetscAddLogDouble(&petsc_allreduce_ct, &petsc_allreduce_ct_th, PetscMPIParallelComm(comm)) || MPI_Allreduce((sendbuf), (recvbuf), (count), (datatype), (op), (comm)))
5653914022bSBarry Smith 
566c708d6e3SStefano Zampini     #define MPI_Bcast(buffer, count, datatype, root, comm) (PetscAddLogDouble(&petsc_allreduce_ct, &petsc_allreduce_ct_th, PetscMPIParallelComm(comm)) || MPI_Bcast((buffer), (count), (datatype), (root), (comm)))
56754fe5c21SBarry Smith 
568c708d6e3SStefano Zampini     #define MPI_Reduce_scatter_block(sendbuf, recvbuf, recvcount, datatype, op, comm) \
569c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_allreduce_ct, &petsc_allreduce_ct_th, PetscMPIParallelComm(comm)) || MPI_Reduce_scatter_block((sendbuf), (recvbuf), (recvcount), (datatype), (op), (comm)))
570d6e4c47cSJed Brown 
5713b9284c0SMatthew G Knepley     #define MPI_Alltoall(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm) \
572c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_allreduce_ct, &petsc_allreduce_ct_th, PetscMPIParallelComm(comm)) || PetscMPITypeSize((sendcount), (sendtype), (&petsc_send_len), (&petsc_send_len_th)) || MPI_Alltoall((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (recvtype), (comm)))
5733b9284c0SMatthew G Knepley 
5743b9284c0SMatthew G Knepley     #define MPI_Alltoallv(sendbuf, sendcnts, sdispls, sendtype, recvbuf, recvcnts, rdispls, recvtype, comm) \
575c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_allreduce_ct, &petsc_allreduce_ct_th, PetscMPIParallelComm(comm)) || PetscMPITypeSizeComm((comm), (sendcnts), (sendtype), (&petsc_send_len), (&petsc_send_len_th)) || MPI_Alltoallv((sendbuf), (sendcnts), (sdispls), (sendtype), (recvbuf), (recvcnts), (rdispls), (recvtype), (comm)))
5763b9284c0SMatthew G Knepley 
577c708d6e3SStefano Zampini     #define MPI_Allgather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm) \
578c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_gather_ct, &petsc_gather_ct_th, PetscMPIParallelComm(comm)) || MPI_Allgather((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (recvtype), (comm)))
57901faf4e4SMatthew Knepley 
58001faf4e4SMatthew Knepley     #define MPI_Allgatherv(sendbuf, sendcount, sendtype, recvbuf, recvcount, displs, recvtype, comm) \
581c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_gather_ct, &petsc_gather_ct_th, PetscMPIParallelComm(comm)) || MPI_Allgatherv((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (displs), (recvtype), (comm)))
58201faf4e4SMatthew Knepley 
58301faf4e4SMatthew Knepley     #define MPI_Gather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm) \
584c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_gather_ct, &petsc_gather_ct_th, 1) || PetscMPITypeSize((sendcount), (sendtype), (&petsc_send_len), (&petsc_send_len_th)) || MPI_Gather((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (recvtype), (root), (comm)))
58501faf4e4SMatthew Knepley 
58601faf4e4SMatthew Knepley     #define MPI_Gatherv(sendbuf, sendcount, sendtype, recvbuf, recvcount, displs, recvtype, root, comm) \
587c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_gather_ct, &petsc_gather_ct_th, 1) || PetscMPITypeSize((sendcount), (sendtype), (&petsc_send_len), (&petsc_send_len_th)) || MPI_Gatherv((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (displs), (recvtype), (root), (comm)))
58801faf4e4SMatthew Knepley 
58901faf4e4SMatthew Knepley     #define MPI_Scatter(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm) \
590c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_scatter_ct, &petsc_scatter_ct_th, 1) || PetscMPITypeSize((recvcount), (recvtype), (&petsc_recv_len), &(petsc_recv_len_th)) || MPI_Scatter((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (recvtype), (root), (comm)))
59101faf4e4SMatthew Knepley 
59201faf4e4SMatthew Knepley     #define MPI_Scatterv(sendbuf, sendcount, displs, sendtype, recvbuf, recvcount, recvtype, root, comm) \
593c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_scatter_ct, &petsc_scatter_ct_th, 1) || PetscMPITypeSize((recvcount), (recvtype), (&petsc_recv_len), &(petsc_recv_len_th)) || MPI_Scatterv((sendbuf), (sendcount), (displs), (sendtype), (recvbuf), (recvcount), (recvtype), (root), (comm)))
594f12d10ccSJunchao Zhang 
595f12d10ccSJunchao Zhang     #define MPI_Ialltoall(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm, request) \
596c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_allreduce_ct, &petsc_allreduce_ct_th, PetscMPIParallelComm(comm)) || PetscMPITypeSize((sendcount), (sendtype), (&petsc_send_len), (&petsc_send_len_th)) || MPI_Ialltoall((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (recvtype), (comm), (request)))
597f12d10ccSJunchao Zhang 
598f12d10ccSJunchao Zhang     #define MPI_Ialltoallv(sendbuf, sendcnts, sdispls, sendtype, recvbuf, recvcnts, rdispls, recvtype, comm, request) \
599c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_allreduce_ct, &petsc_allreduce_ct_th, PetscMPIParallelComm(comm)) || PetscMPITypeSizeComm((comm), (sendcnts), (sendtype), (&petsc_send_len), (&petsc_send_len_th)) || MPI_Ialltoallv((sendbuf), (sendcnts), (sdispls), (sendtype), (recvbuf), (recvcnts), (rdispls), (recvtype), (comm), (request)))
600f12d10ccSJunchao Zhang 
601f12d10ccSJunchao Zhang     #define MPI_Iallgather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm, request) \
602c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_gather_ct, &petsc_gather_ct_th, PetscMPIParallelComm(comm)) || MPI_Iallgather((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (recvtype), (comm), (request)))
603f12d10ccSJunchao Zhang 
604f12d10ccSJunchao Zhang     #define MPI_Iallgatherv(sendbuf, sendcount, sendtype, recvbuf, recvcount, displs, recvtype, comm, request) \
605c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_gather_ct, &petsc_gather_ct_th, PetscMPIParallelComm(comm)) || MPI_Iallgatherv((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (displs), (recvtype), (comm), (request)))
606f12d10ccSJunchao Zhang 
607f12d10ccSJunchao Zhang     #define MPI_Igather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm, request) \
608c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_gather_ct, &petsc_gather_ct_th, 1) || PetscMPITypeSize((sendcount), (sendtype), (&petsc_send_len), (&petsc_send_len_th)) || MPI_Igather((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (recvtype), (root), (comm), (request)))
609f12d10ccSJunchao Zhang 
610f12d10ccSJunchao Zhang     #define MPI_Igatherv(sendbuf, sendcount, sendtype, recvbuf, recvcount, displs, recvtype, root, comm, request) \
611c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_gather_ct, &petsc_gather_ct_th, 1) || PetscMPITypeSize((sendcount), (sendtype), (&petsc_send_len), (&petsc_send_len_th)) || MPI_Igatherv((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (displs), (recvtype), (root), (comm), (request)))
612f12d10ccSJunchao Zhang 
613f12d10ccSJunchao Zhang     #define MPI_Iscatter(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm, request) \
614c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_scatter_ct, &petsc_scatter_ct_th, 1) || PetscMPITypeSize((recvcount), (recvtype), (&petsc_recv_len), (&petsc_recv_len_th)) || MPI_Iscatter((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (recvtype), (root), (comm), (request)))
615f12d10ccSJunchao Zhang 
616f12d10ccSJunchao Zhang     #define MPI_Iscatterv(sendbuf, sendcount, displs, sendtype, recvbuf, recvcount, recvtype, root, comm, request) \
617c708d6e3SStefano Zampini       (PetscAddLogDouble(&petsc_scatter_ct, &petsc_scatter_ct_th, 1) || PetscMPITypeSize((recvcount), (recvtype), (&petsc_recv_len), (&petsc_recv_len_th)) || MPI_Iscatterv((sendbuf), (sendcount), (displs), (sendtype), (recvbuf), (recvcount), (recvtype), (root), (comm), (request)))
618f12d10ccSJunchao Zhang 
619715b587bSJunchao Zhang     #define MPIX_Send_enqueue(buf, count, datatype, dest, tag, comm) \
620715b587bSJunchao Zhang       (PetscAddLogDouble(&petsc_send_ct, &petsc_send_ct_th, 1) || PetscMPITypeSize((count), (datatype), (&petsc_send_len), (&petsc_send_len_th)) || MPIX_Send_enqueue((buf), (count), (datatype), (dest), (tag), (comm)))
621715b587bSJunchao Zhang 
622715b587bSJunchao Zhang     #define MPIX_Recv_enqueue(buf, count, datatype, source, tag, comm, status) \
623715b587bSJunchao Zhang       (PetscAddLogDouble(&petsc_recv_ct, &petsc_recv_ct_th, 1) || PetscMPITypeSize((count), (datatype), (&petsc_recv_len), (&petsc_recv_len_th)) || MPIX_Recv_enqueue((buf), (count), (datatype), (source), (tag), (comm), (status)))
624715b587bSJunchao Zhang 
625715b587bSJunchao Zhang     #define MPIX_Isend_enqueue(buf, count, datatype, dest, tag, comm, request) \
626715b587bSJunchao Zhang       (PetscAddLogDouble(&petsc_isend_ct, &petsc_isend_ct_th, 1) || PetscMPITypeSize((count), (datatype), &(petsc_isend_len), &(petsc_isend_len_th)) || MPIX_Isend_enqueue((buf), (count), (datatype), (dest), (tag), (comm), (request)))
627715b587bSJunchao Zhang 
628715b587bSJunchao Zhang     #define MPIX_Irecv_enqueue(buf, count, datatype, source, tag, comm, request) \
629715b587bSJunchao Zhang       (PetscAddLogDouble(&petsc_irecv_ct, &petsc_irecv_ct_th, 1) || PetscMPITypeSize((count), (datatype), &(petsc_irecv_len), &(petsc_irecv_len_th)) || MPIX_Irecv_enqueue((buf), (count), (datatype), (source), (tag), (comm), (request)))
630715b587bSJunchao Zhang 
631715b587bSJunchao Zhang     #define MPIX_Allreduce_enqueue(sendbuf, recvbuf, count, datatype, op, comm) \
632715b587bSJunchao Zhang       (PetscAddLogDouble(&petsc_allreduce_ct, &petsc_allreduce_ct_th, PetscMPIParallelComm(comm)) || MPIX_Allreduce_enqueue((sendbuf), (recvbuf), (count), (datatype), (op), (comm)))
633715b587bSJunchao Zhang 
634715b587bSJunchao Zhang     #define MPIX_Wait_enqueue(request, status) (PetscAddLogDouble(&petsc_wait_ct, &petsc_wait_ct_th, 1) || PetscAddLogDouble(&petsc_sum_of_waits_ct, &petsc_sum_of_waits_ct_th, 1) || MPIX_Wait_enqueue((request), (status)))
635715b587bSJunchao Zhang 
636715b587bSJunchao Zhang     #define MPIX_Waitall_enqueue(count, array_of_requests, array_of_statuses) \
637715b587bSJunchao Zhang       (PetscAddLogDouble(&petsc_wait_all_ct, &petsc_wait_all_ct_th, 1) || PetscAddLogDouble(&petsc_sum_of_waits_ct, &petsc_sum_of_waits_ct_th, count) || MPIX_Waitall_enqueue((count), (array_of_requests), (array_of_statuses)))
6380d4b0b6cSBarry Smith   #else
6390d4b0b6cSBarry Smith 
6409371c9d4SSatish Balay     #define MPI_Startall_irecv(count, datatype, number, requests) ((number) && MPI_Startall((number), (requests)))
6410d4b0b6cSBarry Smith 
6429371c9d4SSatish Balay     #define MPI_Startall_isend(count, datatype, number, requests) ((number) && MPI_Startall((number), (requests)))
6430d4b0b6cSBarry Smith 
6449371c9d4SSatish Balay     #define MPI_Start_isend(count, datatype, requests) (MPI_Start((requests)))
6450d4b0b6cSBarry Smith 
646c9b973beSBarry Smith   #endif /* !MPIUNI_H && ! PETSC_HAVE_BROKEN_RECURSIVE_MACRO */
647614700edSBarry Smith 
648df8cf0b5SBarry Smith #else /* ---Logging is turned off --------------------------------------------*/
649614700edSBarry Smith 
65053e0a2f3SToby Isaac   #define PetscLogGetState(a)     (*(a) = NULL, PETSC_SUCCESS)
65153e0a2f3SToby Isaac   #define PetscLogHandlerStart(a) ((void)(a), PETSC_SUCCESS)
65253e0a2f3SToby Isaac   #define PetscLogHandlerStop(a)  ((void)(a), PETSC_SUCCESS)
65353e0a2f3SToby Isaac 
6543ba16761SJacob Faibussowitsch   #define PetscLogFlops(n) ((void)(n), PETSC_SUCCESS)
6553ba16761SJacob Faibussowitsch   #define PetscGetFlops(a) (*(a) = 0.0, PETSC_SUCCESS)
656614700edSBarry Smith 
657*2611ad71SToby Isaac   #define PetscLogStageRegister(a, b)   ((void)(a), *(b) = -1, PETSC_SUCCESS)
658*2611ad71SToby Isaac   #define PetscLogStagePush(a)          ((void)(a), PETSC_SUCCESS)
6593ba16761SJacob Faibussowitsch   #define PetscLogStagePop()            PETSC_SUCCESS
660*2611ad71SToby Isaac   #define PetscLogStageSetActive(a, b)  ((void)(a), (void)(b), PETSC_SUCCESS)
661*2611ad71SToby Isaac   #define PetscLogStageGetActive(a, b)  ((void)(a), *(b) = PETSC_FALSE, PETSC_SUCCESS)
662*2611ad71SToby Isaac   #define PetscLogStageGetVisible(a, b) ((void)(a), *(b) = PETSC_FALSE, PETSC_SUCCESS)
663*2611ad71SToby Isaac   #define PetscLogStageSetVisible(a, b) ((void)(a), (void)(b), PETSC_SUCCESS)
664*2611ad71SToby Isaac   #define PetscLogStageGetId(a, b)      ((void)(a), *(b) = -1, PETSC_SUCCESS)
66553e0a2f3SToby Isaac   #define PetscLogStageGetName(a, b)    ((void)(a), *(b) = NULL, PETSC_SUCCESS)
666f5d6ab90SLisandro Dalcin 
667*2611ad71SToby Isaac   #define PetscLogEventRegister(a, b, c)    ((void)(a), (void)(b), *(c) = -1, PETSC_SUCCESS)
668*2611ad71SToby Isaac   #define PetscLogEventSetCollective(a, b)  ((void)(a), (void)(b), PETSC_SUCCESS)
669*2611ad71SToby Isaac   #define PetscLogEventIncludeClass(a)      ((void)(a), PETSC_SUCCESS)
670*2611ad71SToby Isaac   #define PetscLogEventExcludeClass(a)      ((void)(a), PETSC_SUCCESS)
671*2611ad71SToby Isaac   #define PetscLogEventActivate(a)          ((void)(a), PETSC_SUCCESS)
672*2611ad71SToby Isaac   #define PetscLogEventDeactivate(a)        ((void)(a), PETSC_SUCCESS)
673*2611ad71SToby Isaac   #define PetscLogEventDeactivatePush(a)    ((void)(a), PETSC_SUCCESS)
674*2611ad71SToby Isaac   #define PetscLogEventDeactivatePop(a)     ((void)(a), PETSC_SUCCESS)
675*2611ad71SToby Isaac   #define PetscLogEventActivateClass(a)     ((void)(a), PETSC_SUCCESS)
676*2611ad71SToby Isaac   #define PetscLogEventDeactivateClass(a)   ((void)(a), PETSC_SUCCESS)
677*2611ad71SToby Isaac   #define PetscLogEventSetActiveAll(a, b)   ((void)(a), PETSC_SUCCESS)
678*2611ad71SToby Isaac   #define PetscLogEventGetId(a, b)          ((void)(a), *(b) = -1, PETSC_SUCCESS)
67953e0a2f3SToby Isaac   #define PetscLogEventGetName(a, b)        ((void)(a), *(b) = NULL, PETSC_SUCCESS)
680*2611ad71SToby Isaac   #define PetscLogEventGetPerfInfo(a, b, c) ((void)(a), (void)(b), *(c) = (const PetscEventPerfInfo){0}, PETSC_SUCCESS)
681*2611ad71SToby Isaac 
682*2611ad71SToby Isaac   #define PetscLogEventSetDof(a, b, c)   ((void)(a), (void)(b), (void)(c), PETSC_SUCCESS)
683*2611ad71SToby Isaac   #define PetscLogEventSetError(a, b, c) ((void)(a), (void)(b), (void)(c), PETSC_SUCCESS)
68453e0a2f3SToby Isaac   #define PetscLogEventsPause()          PETSC_SUCCESS
68553e0a2f3SToby Isaac   #define PetscLogEventsResume()         PETSC_SUCCESS
68653e0a2f3SToby Isaac 
68753e0a2f3SToby Isaac   #define PetscLogClassGetClassId(a, b) (*(b) = -1, PETSC_SUCCESS)
68853e0a2f3SToby Isaac   #define PetscLogClassIdGetName(a, b)  (*(b) = NULL, PETSC_SUCCESS)
68977c4ece6SBarry Smith 
690*2611ad71SToby Isaac PETSC_UNUSED static PetscErrorCode (*PetscLogPLB)(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject) = NULL;
691*2611ad71SToby Isaac PETSC_UNUSED static PetscErrorCode (*PetscLogPLE)(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject) = NULL;
692*2611ad71SToby Isaac PETSC_UNUSED static PetscErrorCode (*PetscLogPHC)(PetscObject)                                                            = NULL;
693*2611ad71SToby Isaac PETSC_UNUSED static PetscErrorCode (*PetscLogPHD)(PetscObject)                                                            = NULL;
694f5d6ab90SLisandro Dalcin 
695*2611ad71SToby Isaac   #define PetscLogObjectCreate(h)        ((void)(h), PETSC_SUCCESS)
696*2611ad71SToby Isaac   #define PetscLogObjectDestroy(h)       ((void)(h), PETSC_SUCCESS)
697*2611ad71SToby Isaac   #define PetscLogObjectState(h, c, ...) ((void)(h), (void)(c), PETSC_SUCCESS)
698fa2bb9feSLisandro Dalcin 
6993ba16761SJacob Faibussowitsch   #define PetscLogDefaultBegin()                   PETSC_SUCCESS
7003ba16761SJacob Faibussowitsch   #define PetscLogAllBegin()                       PETSC_SUCCESS
7013ba16761SJacob Faibussowitsch   #define PetscLogNestedBegin()                    PETSC_SUCCESS
702*2611ad71SToby Isaac   #define PetscLogTraceBegin(file)                 ((void)(file), PETSC_SUCCESS)
703*2611ad71SToby Isaac   #define PetscLogMPEBegin()                       PETSC_SUCCESS
70453e0a2f3SToby Isaac   #define PetscLogPerfstubsBegin()                 PETSC_SUCCESS
70553e0a2f3SToby Isaac   #define PetscLogLegacyCallbacksBegin(a, b, c, d) ((void)(a), (void)(b), (void)(c), (void)(d), PETSC_SUCCESS)
706*2611ad71SToby Isaac   #define PetscLogActions(a)                       ((void)(a), PETSC_SUCCESS)
707*2611ad71SToby Isaac   #define PetscLogObjects(a)                       ((void)(a), PETSC_SUCCESS)
708*2611ad71SToby Isaac   #define PetscLogSetThreshold(a, b)               ((void)(a), (void)(b), PETSC_SUCCESS)
70953e0a2f3SToby Isaac 
7103ba16761SJacob Faibussowitsch   #define PetscLogSet(lb, le)    PETSC_SUCCESS
7113ba16761SJacob Faibussowitsch   #define PetscLogIsActive(flag) (*(flag) = PETSC_FALSE, PETSC_SUCCESS)
712fa2bb9feSLisandro Dalcin 
713*2611ad71SToby Isaac   #define PetscLogView(viewer)      ((void)(viewer), PETSC_SUCCESS)
7143ba16761SJacob Faibussowitsch   #define PetscLogViewFromOptions() PETSC_SUCCESS
715*2611ad71SToby Isaac   #define PetscLogDump(c)           ((void)(c), PETSC_SUCCESS)
716*2611ad71SToby Isaac   #define PetscLogMPEDump(c)        ((void)(c), PETSC_SUCCESS)
717fa2bb9feSLisandro Dalcin 
718*2611ad71SToby Isaac   #define PetscLogEventSync(e, comm)                            ((void)(e), (void)(comm), PETSC_SUCCESS)
719*2611ad71SToby Isaac   #define PetscLogEventBegin(e, o1, o2, o3, o4)                 ((void)(e), (void)(o1), (void)(o2), (void)(o3), PETSC_SUCCESS)
720*2611ad71SToby Isaac   #define PetscLogEventEnd(e, o1, o2, o3, o4)                   ((void)(e), (void)(o1), (void)(o2), (void)(o3), PETSC_SUCCESS)
721ce6058e1SBarry Smith 
72223c86b1bSSatish Balay   /* If PETSC_USE_LOG is NOT defined, these still need to be! */
72323c86b1bSSatish Balay   #define MPI_Startall_irecv(count, datatype, number, requests) ((number) && MPI_Startall(number, requests))
72423c86b1bSSatish Balay   #define MPI_Startall_isend(count, datatype, number, requests) ((number) && MPI_Startall(number, requests))
72523c86b1bSSatish Balay   #define MPI_Start_isend(count, datatype, requests)            MPI_Start(requests)
72623c86b1bSSatish Balay 
72723c86b1bSSatish Balay #endif /* PETSC_USE_LOG */
72823c86b1bSSatish Balay 
729204a6943SJed Brown #define PetscPreLoadBegin(flag, name) \
730204a6943SJed Brown   do { \
731204a6943SJed Brown     PetscBool     PetscPreLoading = flag; \
732204a6943SJed Brown     int           PetscPreLoadMax, PetscPreLoadIt; \
733166c7f25SBarry Smith     PetscLogStage _stageNum; \
7349566063dSJacob Faibussowitsch     PetscCall(PetscOptionsGetBool(NULL, NULL, "-preload", &PetscPreLoading, NULL)); \
735204a6943SJed Brown     PetscPreLoadMax     = (int)(PetscPreLoading); \
736204a6943SJed Brown     PetscPreLoadingUsed = PetscPreLoading ? PETSC_TRUE : PetscPreLoadingUsed; \
737204a6943SJed Brown     for (PetscPreLoadIt = 0; PetscPreLoadIt <= PetscPreLoadMax; PetscPreLoadIt++) { \
738204a6943SJed Brown       PetscPreLoadingOn = PetscPreLoading; \
7399566063dSJacob Faibussowitsch       PetscCall(PetscBarrier(NULL)); \
7409566063dSJacob Faibussowitsch       if (PetscPreLoadIt > 0) PetscCall(PetscLogStageGetId(name, &_stageNum)); \
7419566063dSJacob Faibussowitsch       else PetscCall(PetscLogStageRegister(name, &_stageNum)); \
7429566063dSJacob Faibussowitsch       PetscCall(PetscLogStageSetActive(_stageNum, (PetscBool)(!PetscPreLoadMax || PetscPreLoadIt))); \
743a8f51744SPierre Jolivet       PetscCall(PetscLogStagePush(_stageNum))
7448e58c17dSMatthew Knepley 
745204a6943SJed Brown #define PetscPreLoadEnd() \
7469566063dSJacob Faibussowitsch   PetscCall(PetscLogStagePop()); \
747204a6943SJed Brown   PetscPreLoading = PETSC_FALSE; \
748043328b6SSatish Balay   } \
7499371c9d4SSatish Balay   } \
7509371c9d4SSatish Balay   while (0)
7518e58c17dSMatthew Knepley 
7529371c9d4SSatish Balay #define PetscPreLoadStage(name) \
7539371c9d4SSatish Balay   do { \
7549566063dSJacob Faibussowitsch     PetscCall(PetscLogStagePop()); \
7559566063dSJacob Faibussowitsch     if (PetscPreLoadIt > 0) PetscCall(PetscLogStageGetId(name, &_stageNum)); \
7569566063dSJacob Faibussowitsch     else PetscCall(PetscLogStageRegister(name, &_stageNum)); \
7579566063dSJacob Faibussowitsch     PetscCall(PetscLogStageSetActive(_stageNum, (PetscBool)(!PetscPreLoadMax || PetscPreLoadIt))); \
7589566063dSJacob Faibussowitsch     PetscCall(PetscLogStagePush(_stageNum)); \
759204a6943SJed Brown   } while (0)
760e9fa29b7SSatish Balay 
761ad14c47eSJed Brown /* some vars for logging */
762ad14c47eSJed Brown PETSC_EXTERN PetscBool PetscPreLoadingUsed; /* true if we are or have done preloading */
763ad14c47eSJed Brown PETSC_EXTERN PetscBool PetscPreLoadingOn;   /* true if we are currently in a preloading calculation */
764ad14c47eSJed Brown 
765c708d6e3SStefano Zampini #if defined(PETSC_USE_LOG) && defined(PETSC_HAVE_DEVICE)
766c708d6e3SStefano Zampini 
767c708d6e3SStefano Zampini PETSC_EXTERN PetscErrorCode PetscLogGpuTime(void);
768c708d6e3SStefano Zampini PETSC_EXTERN PetscErrorCode PetscLogGpuTimeBegin(void);
769c708d6e3SStefano Zampini PETSC_EXTERN PetscErrorCode PetscLogGpuTimeEnd(void);
770c708d6e3SStefano Zampini 
771c708d6e3SStefano Zampini /*@C
772c708d6e3SStefano Zampini        PetscLogGpuFlops - Log how many flops are performed in a calculation on the device
773c708d6e3SStefano Zampini 
774c708d6e3SStefano Zampini    Input Parameter:
775c708d6e3SStefano Zampini .   flops - the number of flops
776c708d6e3SStefano Zampini 
777c200f75eSSatish Balay    Level: intermediate
778c200f75eSSatish Balay 
779c708d6e3SStefano Zampini    Notes:
780c708d6e3SStefano Zampini      To limit the chance of integer overflow when multiplying by a constant, represent the constant as a double,
781c200f75eSSatish Balay      not an integer. Use `PetscLogFlops`(4.0*n) not `PetscLogFlops`(4*n)
782c708d6e3SStefano Zampini 
783c708d6e3SStefano Zampini      The values are also added to the total flop count for the MPI rank that is set with `PetscLogFlops()`; hence the number of flops
784c708d6e3SStefano Zampini      just on the CPU would be the value from set from `PetscLogFlops()` minus the value set from `PetscLogGpuFlops()`
785c708d6e3SStefano Zampini 
786c200f75eSSatish Balay .seealso: [](ch_profiling), `PetscLogView()`, `PetscLogFlops()`, `PetscLogGpuTimeBegin()`, `PetscLogGpuTimeEnd()`
787c708d6e3SStefano Zampini @*/
788c708d6e3SStefano Zampini static inline PetscErrorCode PetscLogGpuFlops(PetscLogDouble n)
789c708d6e3SStefano Zampini {
790c708d6e3SStefano Zampini   PetscAssert(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot log negative flops");
7913ba16761SJacob Faibussowitsch   PetscCall(PetscAddLogDouble(&petsc_TotalFlops, &petsc_TotalFlops_th, PETSC_FLOPS_PER_OP * n));
7923ba16761SJacob Faibussowitsch   PetscCall(PetscAddLogDouble(&petsc_gflops, &petsc_gflops_th, PETSC_FLOPS_PER_OP * n));
7933ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
794c708d6e3SStefano Zampini }
795c708d6e3SStefano Zampini 
796c708d6e3SStefano Zampini static inline PetscErrorCode PetscLogGpuTimeAdd(PetscLogDouble t)
797c708d6e3SStefano Zampini {
7983ba16761SJacob Faibussowitsch   return PetscAddLogDouble(&petsc_gtime, &petsc_gtime_th, t);
799c708d6e3SStefano Zampini }
800c708d6e3SStefano Zampini 
801c708d6e3SStefano Zampini static inline PetscErrorCode PetscLogCpuToGpu(PetscLogDouble size)
802c708d6e3SStefano Zampini {
8033ba16761SJacob Faibussowitsch   return PetscAddLogDoubleCnt(&petsc_ctog_ct, &petsc_ctog_sz, &petsc_ctog_ct_th, &petsc_ctog_sz_th, size);
804c708d6e3SStefano Zampini }
805c708d6e3SStefano Zampini 
806c708d6e3SStefano Zampini static inline PetscErrorCode PetscLogGpuToCpu(PetscLogDouble size)
807c708d6e3SStefano Zampini {
8083ba16761SJacob Faibussowitsch   return PetscAddLogDoubleCnt(&petsc_gtoc_ct, &petsc_gtoc_sz, &petsc_gtoc_ct_th, &petsc_gtoc_sz_th, size);
809c708d6e3SStefano Zampini }
810c708d6e3SStefano Zampini 
811c708d6e3SStefano Zampini static inline PetscErrorCode PetscLogCpuToGpuScalar(PetscLogDouble size)
812c708d6e3SStefano Zampini {
8133ba16761SJacob Faibussowitsch   return PetscAddLogDoubleCnt(&petsc_ctog_ct_scalar, &petsc_ctog_sz_scalar, &petsc_ctog_ct_scalar_th, &petsc_ctog_sz_scalar_th, size);
814c708d6e3SStefano Zampini }
815c708d6e3SStefano Zampini 
816c708d6e3SStefano Zampini static inline PetscErrorCode PetscLogGpuToCpuScalar(PetscLogDouble size)
817c708d6e3SStefano Zampini {
8183ba16761SJacob Faibussowitsch   return PetscAddLogDoubleCnt(&petsc_gtoc_ct_scalar, &petsc_gtoc_sz_scalar, &petsc_gtoc_ct_scalar_th, &petsc_gtoc_sz_scalar_th, size);
819c708d6e3SStefano Zampini }
820c708d6e3SStefano Zampini #else
821c708d6e3SStefano Zampini 
8223ba16761SJacob Faibussowitsch   #define PetscLogCpuToGpu(a)       PETSC_SUCCESS
8233ba16761SJacob Faibussowitsch   #define PetscLogGpuToCpu(a)       PETSC_SUCCESS
8243ba16761SJacob Faibussowitsch   #define PetscLogCpuToGpuScalar(a) PETSC_SUCCESS
8253ba16761SJacob Faibussowitsch   #define PetscLogGpuToCpuScalar(a) PETSC_SUCCESS
8263ba16761SJacob Faibussowitsch   #define PetscLogGpuFlops(a)       PETSC_SUCCESS
8273ba16761SJacob Faibussowitsch   #define PetscLogGpuTime()         PETSC_SUCCESS
8283ba16761SJacob Faibussowitsch   #define PetscLogGpuTimeAdd(a)     PETSC_SUCCESS
8293ba16761SJacob Faibussowitsch   #define PetscLogGpuTimeBegin()    PETSC_SUCCESS
8303ba16761SJacob Faibussowitsch   #define PetscLogGpuTimeEnd()      PETSC_SUCCESS
831c708d6e3SStefano Zampini 
832c708d6e3SStefano Zampini #endif /* PETSC_USE_LOG && PETSC_HAVE_DEVICE */
833c708d6e3SStefano Zampini 
834c708d6e3SStefano Zampini /* remove TLS defines */
835c708d6e3SStefano Zampini #undef PETSC_EXTERN_TLS
836c708d6e3SStefano Zampini #undef PETSC_TLS
837c708d6e3SStefano Zampini 
83897bb86f7SLois Curfman McInnes #endif
839