xref: /petsc/include/petsclog.h (revision ccfb0f9f40a0131988d7995ed9679700dae2a75a)
1 /*
2     Defines profile/logging in PETSc.
3 */
4 #pragma once
5 
6 #include <petscsys.h>
7 #include <petsctime.h>
8 #include <petscbt.h>
9 #include <petsclogtypes.h>
10 
11 /* MANSEC = Sys */
12 /* SUBMANSEC = Log */
13 
14 /* General logging of information; different from event logging */
15 PETSC_EXTERN PetscErrorCode PetscInfo_Private(const char[], PetscObject, const char[], ...) PETSC_ATTRIBUTE_FORMAT(3, 4);
16 #if defined(PETSC_USE_INFO)
17   #define PetscInfo(A, ...) PetscInfo_Private(PETSC_FUNCTION_NAME, ((PetscObject)A), __VA_ARGS__)
18 #else
19   #define PetscInfo(A, ...) PETSC_SUCCESS
20 #endif
21 
22 #define PetscInfo1(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "PetscInfo()", ) PetscInfo(__VA_ARGS__)
23 #define PetscInfo2(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "PetscInfo()", ) PetscInfo(__VA_ARGS__)
24 #define PetscInfo3(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "PetscInfo()", ) PetscInfo(__VA_ARGS__)
25 #define PetscInfo4(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "PetscInfo()", ) PetscInfo(__VA_ARGS__)
26 #define PetscInfo5(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "PetscInfo()", ) PetscInfo(__VA_ARGS__)
27 #define PetscInfo6(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "PetscInfo()", ) PetscInfo(__VA_ARGS__)
28 #define PetscInfo7(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "PetscInfo()", ) PetscInfo(__VA_ARGS__)
29 #define PetscInfo8(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "PetscInfo()", ) PetscInfo(__VA_ARGS__)
30 #define PetscInfo9(...) PETSC_DEPRECATED_MACRO(3, 17, 0, "PetscInfo()", ) PetscInfo(__VA_ARGS__)
31 
32 /*E
33   PetscInfoCommFlag - Describes the method by which to filter information displayed by `PetscInfo()` by communicator size
34 
35   Values:
36 + `PETSC_INFO_COMM_ALL`       - Default uninitialized value. `PetscInfo()` will not filter based on
37                                 communicator size (i.e. will print for all communicators)
38 . `PETSC_INFO_COMM_NO_SELF`   - `PetscInfo()` will NOT print for communicators with size = 1 (i.e. *_COMM_SELF)
39 - `PETSC_INFO_COMM_ONLY_SELF` - `PetscInfo()` will ONLY print for communicators with size = 1
40 
41   Level: intermediate
42 
43   Note:
44   Used as an input for `PetscInfoSetFilterCommSelf()`
45 
46 .seealso: `PetscInfo()`, `PetscInfoSetFromOptions()`, `PetscInfoSetFilterCommSelf()`
47 E*/
48 typedef enum {
49   PETSC_INFO_COMM_ALL       = -1,
50   PETSC_INFO_COMM_NO_SELF   = 0,
51   PETSC_INFO_COMM_ONLY_SELF = 1
52 } PetscInfoCommFlag;
53 
54 PETSC_EXTERN const char *const PetscInfoCommFlags[];
55 PETSC_EXTERN PetscErrorCode    PetscInfoDeactivateClass(PetscClassId);
56 PETSC_EXTERN PetscErrorCode    PetscInfoActivateClass(PetscClassId);
57 PETSC_EXTERN PetscErrorCode    PetscInfoEnabled(PetscClassId, PetscBool *);
58 PETSC_EXTERN PetscErrorCode    PetscInfoAllow(PetscBool);
59 PETSC_EXTERN PetscErrorCode    PetscInfoSetFile(const char[], const char[]);
60 PETSC_EXTERN PetscErrorCode    PetscInfoGetFile(char **, FILE **);
61 PETSC_EXTERN PetscErrorCode    PetscInfoSetClasses(PetscBool, PetscInt, const char *const *);
62 PETSC_EXTERN PetscErrorCode    PetscInfoGetClass(const char *, PetscBool *);
63 PETSC_EXTERN PetscErrorCode    PetscInfoGetInfo(PetscBool *, PetscBool *, PetscBool *, PetscBool *, PetscInfoCommFlag *);
64 PETSC_EXTERN PetscErrorCode    PetscInfoProcessClass(const char[], PetscInt, const PetscClassId[]);
65 PETSC_EXTERN PetscErrorCode    PetscInfoSetFilterCommSelf(PetscInfoCommFlag);
66 PETSC_EXTERN PetscErrorCode    PetscInfoSetFromOptions(PetscOptions);
67 PETSC_EXTERN PetscErrorCode    PetscInfoDestroy(void);
68 PETSC_EXTERN PetscBool         PetscLogPrintInfo; /* if true, indicates PetscInfo() is turned on */
69 
70 PETSC_EXTERN PetscErrorCode PetscIntStackCreate(PetscIntStack *);
71 PETSC_EXTERN PetscErrorCode PetscIntStackDestroy(PetscIntStack);
72 PETSC_EXTERN PetscErrorCode PetscIntStackPush(PetscIntStack, int);
73 PETSC_EXTERN PetscErrorCode PetscIntStackPop(PetscIntStack, int *);
74 PETSC_EXTERN PetscErrorCode PetscIntStackTop(PetscIntStack, int *);
75 PETSC_EXTERN PetscErrorCode PetscIntStackEmpty(PetscIntStack, PetscBool *);
76 
77 PETSC_EXTERN PetscErrorCode PetscLogStateCreate(PetscLogState *);
78 PETSC_EXTERN PetscErrorCode PetscLogStateDestroy(PetscLogState *);
79 PETSC_EXTERN PetscErrorCode PetscLogStateGetRegistry(PetscLogState, PetscLogRegistry *);
80 
81 PETSC_EXTERN PetscErrorCode PetscLogStateClassRegister(PetscLogState, const char[], PetscClassId, PetscLogStage *);
82 PETSC_EXTERN PetscErrorCode PetscLogStateClassSetActive(PetscLogState, PetscLogStage, PetscClassId, PetscBool);
83 PETSC_EXTERN PetscErrorCode PetscLogStateClassSetActiveAll(PetscLogState, PetscClassId, PetscBool);
84 
85 PETSC_EXTERN PetscErrorCode PetscLogStateStageRegister(PetscLogState, const char[], PetscLogStage *);
86 PETSC_EXTERN PetscErrorCode PetscLogStateStagePush(PetscLogState, PetscLogStage);
87 PETSC_EXTERN PetscErrorCode PetscLogStateStagePop(PetscLogState);
88 PETSC_EXTERN PetscErrorCode PetscLogStateStageSetActive(PetscLogState, PetscLogStage, PetscBool);
89 PETSC_EXTERN PetscErrorCode PetscLogStateStageGetActive(PetscLogState, PetscLogStage, PetscBool *);
90 PETSC_EXTERN PetscErrorCode PetscLogStateGetCurrentStage(PetscLogState, PetscLogStage *);
91 
92 PETSC_EXTERN PetscErrorCode PetscLogStateEventRegister(PetscLogState, const char[], PetscClassId, PetscLogEvent *);
93 PETSC_EXTERN PetscErrorCode PetscLogStateEventSetCollective(PetscLogState, PetscLogEvent, PetscBool);
94 PETSC_EXTERN PetscErrorCode PetscLogStateEventSetActive(PetscLogState, PetscLogStage, PetscLogEvent, PetscBool);
95 PETSC_EXTERN PetscErrorCode PetscLogStateEventSetActiveAll(PetscLogState, PetscLogEvent, PetscBool);
96 PETSC_EXTERN PetscErrorCode PetscLogStateEventGetActive(PetscLogState, PetscLogStage, PetscLogEvent, PetscBool *);
97 
98 PETSC_EXTERN PetscErrorCode PetscLogStateGetEventFromName(PetscLogState, const char[], PetscLogEvent *);
99 PETSC_EXTERN PetscErrorCode PetscLogStateGetStageFromName(PetscLogState, const char[], PetscLogStage *);
100 PETSC_EXTERN PetscErrorCode PetscLogStateGetClassFromName(PetscLogState, const char[], PetscLogClass *);
101 PETSC_EXTERN PetscErrorCode PetscLogStateGetClassFromClassId(PetscLogState, PetscClassId, PetscLogClass *);
102 PETSC_EXTERN PetscErrorCode PetscLogStateGetNumEvents(PetscLogState, PetscInt *);
103 PETSC_EXTERN PetscErrorCode PetscLogStateGetNumStages(PetscLogState, PetscInt *);
104 PETSC_EXTERN PetscErrorCode PetscLogStateGetNumClasses(PetscLogState, PetscInt *);
105 PETSC_EXTERN PetscErrorCode PetscLogStateEventGetInfo(PetscLogState, PetscLogEvent, PetscLogEventInfo *);
106 PETSC_EXTERN PetscErrorCode PetscLogStateStageGetInfo(PetscLogState, PetscLogStage, PetscLogStageInfo *);
107 PETSC_EXTERN PetscErrorCode PetscLogStateClassGetInfo(PetscLogState, PetscLogClass, PetscLogClassInfo *);
108 
109 PETSC_EXTERN PetscClassId PETSCLOGHANDLER_CLASSID;
110 
111 PETSC_EXTERN PetscFunctionList PetscLogHandlerList;
112 
113 PETSC_EXTERN PetscErrorCode PetscLogHandlerRegister(const char[], PetscErrorCode (*)(PetscLogHandler));
114 PETSC_EXTERN PetscErrorCode PetscLogHandlerCreate(MPI_Comm, PetscLogHandler *);
115 PETSC_EXTERN PetscErrorCode PetscLogHandlerSetType(PetscLogHandler, PetscLogHandlerType);
116 PETSC_EXTERN PetscErrorCode PetscLogHandlerGetType(PetscLogHandler, PetscLogHandlerType *);
117 PETSC_EXTERN PetscErrorCode PetscLogHandlerDestroy(PetscLogHandler *);
118 PETSC_EXTERN PetscErrorCode PetscLogHandlerSetState(PetscLogHandler, PetscLogState);
119 PETSC_EXTERN PetscErrorCode PetscLogHandlerGetState(PetscLogHandler, PetscLogState *);
120 PETSC_EXTERN PetscErrorCode PetscLogHandlerEventBegin(PetscLogHandler, PetscLogEvent, PetscObject, PetscObject, PetscObject, PetscObject);
121 PETSC_EXTERN PetscErrorCode PetscLogHandlerEventEnd(PetscLogHandler, PetscLogEvent, PetscObject, PetscObject, PetscObject, PetscObject);
122 PETSC_EXTERN PetscErrorCode PetscLogHandlerEventSync(PetscLogHandler, PetscLogEvent, MPI_Comm);
123 PETSC_EXTERN PetscErrorCode PetscLogHandlerObjectCreate(PetscLogHandler, PetscObject);
124 PETSC_EXTERN PetscErrorCode PetscLogHandlerObjectDestroy(PetscLogHandler, PetscObject);
125 PETSC_EXTERN PetscErrorCode PetscLogHandlerStagePush(PetscLogHandler, PetscLogStage);
126 PETSC_EXTERN PetscErrorCode PetscLogHandlerStagePop(PetscLogHandler, PetscLogStage);
127 PETSC_EXTERN PetscErrorCode PetscLogHandlerView(PetscLogHandler, PetscViewer);
128 
129 PETSC_EXTERN PetscErrorCode PetscLogHandlerGetEventPerfInfo(PetscLogHandler, PetscLogStage, PetscLogEvent, PetscEventPerfInfo **);
130 PETSC_EXTERN PetscErrorCode PetscLogHandlerGetStagePerfInfo(PetscLogHandler, PetscLogStage, PetscEventPerfInfo **);
131 PETSC_EXTERN PetscErrorCode PetscLogHandlerSetLogActions(PetscLogHandler, PetscBool);
132 PETSC_EXTERN PetscErrorCode PetscLogHandlerSetLogObjects(PetscLogHandler, PetscBool);
133 PETSC_EXTERN PetscErrorCode PetscLogHandlerLogObjectState(PetscLogHandler, PetscObject, const char[], ...);
134 PETSC_EXTERN PetscErrorCode PetscLogHandlerGetNumObjects(PetscLogHandler, PetscInt *);
135 PETSC_EXTERN PetscErrorCode PetscLogHandlerEventDeactivatePush(PetscLogHandler, PetscLogStage, PetscLogEvent);
136 PETSC_EXTERN PetscErrorCode PetscLogHandlerEventDeactivatePop(PetscLogHandler, PetscLogStage, PetscLogEvent);
137 PETSC_EXTERN PetscErrorCode PetscLogHandlerEventsPause(PetscLogHandler);
138 PETSC_EXTERN PetscErrorCode PetscLogHandlerEventsResume(PetscLogHandler);
139 PETSC_EXTERN PetscErrorCode PetscLogHandlerDump(PetscLogHandler, const char[]);
140 PETSC_EXTERN PetscErrorCode PetscLogHandlerStageSetVisible(PetscLogHandler, PetscLogStage, PetscBool);
141 PETSC_EXTERN PetscErrorCode PetscLogHandlerStageGetVisible(PetscLogHandler, PetscLogStage, PetscBool *);
142 
143 PETSC_EXTERN PetscErrorCode PetscLogHandlerCreateTrace(MPI_Comm, FILE *, PetscLogHandler *);
144 PETSC_EXTERN PetscErrorCode PetscLogHandlerCreateLegacy(MPI_Comm, PetscErrorCode (*)(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject), PetscErrorCode (*)(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject), PetscErrorCode (*)(PetscObject), PetscErrorCode (*)(PetscObject), PetscLogHandler *);
145 
146 /* All events are inactive if an invalid stage is set, like if there have been more stage pops than stage pushes */
147 #define PetscLogStateStageEventIsActive(state, stage, event) ((stage >= 0) && PetscBTLookup((state)->active, (stage)) && PetscBTLookup((state)->active, (stage) + (event + 1) * (state)->bt_num_stages))
148 #define PetscLogStateEventCurrentlyActive(state, event)      ((state) && PetscLogStateStageEventIsActive(state, (state)->current_stage, event))
149 
150 /* PetscLogHandler with critical methods exposed for speed */
151 typedef struct _n_PetscLogHandlerHot {
152   PetscLogHandler handler;
153   PetscErrorCode (*eventBegin)(PetscLogHandler, PetscLogEvent, PetscObject, PetscObject, PetscObject, PetscObject);
154   PetscErrorCode (*eventEnd)(PetscLogHandler, PetscLogEvent, PetscObject, PetscObject, PetscObject, PetscObject);
155   PetscErrorCode (*eventSync)(PetscLogHandler, PetscLogEvent, MPI_Comm);
156   PetscErrorCode (*objectCreate)(PetscLogHandler, PetscObject);
157   PetscErrorCode (*objectDestroy)(PetscLogHandler, PetscObject);
158 } PetscLogHandlerHot;
159 
160 /* Handle multithreading */
161 #if defined(PETSC_HAVE_THREADSAFETY)
162   #if defined(__cplusplus)
163     #define PETSC_TLS thread_local
164   #else
165     #define PETSC_TLS _Thread_local
166   #endif
167   #define PETSC_EXTERN_TLS extern PETSC_TLS PETSC_VISIBILITY_PUBLIC
168 #else
169   #define PETSC_EXTERN_TLS PETSC_EXTERN
170   #define PETSC_TLS
171 #endif
172 #if defined(PETSC_HAVE_THREADSAFETY) && defined(PETSC_USE_LOG)
173 PETSC_EXTERN PetscErrorCode PetscAddLogDouble(PetscLogDouble *, PetscLogDouble *, PetscLogDouble);
174 PETSC_EXTERN PetscErrorCode PetscAddLogDoubleCnt(PetscLogDouble *, PetscLogDouble *, PetscLogDouble *, PetscLogDouble *, PetscLogDouble);
175 #else
176   #define PetscAddLogDouble(a, b, c)          ((PetscErrorCode)((*(a) += (c), PETSC_SUCCESS) || ((*(b) += (c)), PETSC_SUCCESS)))
177   #define PetscAddLogDoubleCnt(a, b, c, d, e) ((PetscErrorCode)(PetscAddLogDouble(a, c, 1) || PetscAddLogDouble(b, d, e)))
178 #endif
179 
180 PETSC_DEPRECATED_FUNCTION(3, 18, 0, "PetscLogObjectParent()", ) static inline PetscErrorCode PetscLogObjectParent(PetscObject o, PetscObject p)
181 {
182   (void)o;
183   (void)p;
184   return PETSC_SUCCESS;
185 }
186 #define PetscLogObjectParents(p, n, d) PetscMacroReturnStandard(for (int _i = 0; _i < (n); ++_i) PetscCall(PetscLogObjectParent((PetscObject)(p), (PetscObject)(d)[_i]));)
187 
188 PETSC_DEPRECATED_FUNCTION(3, 18, 0, "PetscLogObjectMemory()", ) static inline PetscErrorCode PetscLogObjectMemory(PetscObject o, PetscLogDouble m)
189 {
190   (void)o;
191   (void)m;
192   return PETSC_SUCCESS;
193 }
194 
195 /* Global flop counter */
196 PETSC_EXTERN PetscLogDouble petsc_TotalFlops;
197 PETSC_EXTERN PetscLogDouble petsc_irecv_ct;
198 PETSC_EXTERN PetscLogDouble petsc_isend_ct;
199 PETSC_EXTERN PetscLogDouble petsc_recv_ct;
200 PETSC_EXTERN PetscLogDouble petsc_send_ct;
201 PETSC_EXTERN PetscLogDouble petsc_irecv_len;
202 PETSC_EXTERN PetscLogDouble petsc_isend_len;
203 PETSC_EXTERN PetscLogDouble petsc_recv_len;
204 PETSC_EXTERN PetscLogDouble petsc_send_len;
205 PETSC_EXTERN PetscLogDouble petsc_allreduce_ct;
206 PETSC_EXTERN PetscLogDouble petsc_gather_ct;
207 PETSC_EXTERN PetscLogDouble petsc_scatter_ct;
208 PETSC_EXTERN PetscLogDouble petsc_wait_ct;
209 PETSC_EXTERN PetscLogDouble petsc_wait_any_ct;
210 PETSC_EXTERN PetscLogDouble petsc_wait_all_ct;
211 PETSC_EXTERN PetscLogDouble petsc_sum_of_waits_ct;
212 
213 /* Thread local storage */
214 PETSC_EXTERN_TLS PetscLogDouble petsc_TotalFlops_th;
215 PETSC_EXTERN_TLS PetscLogDouble petsc_irecv_ct_th;
216 PETSC_EXTERN_TLS PetscLogDouble petsc_isend_ct_th;
217 PETSC_EXTERN_TLS PetscLogDouble petsc_recv_ct_th;
218 PETSC_EXTERN_TLS PetscLogDouble petsc_send_ct_th;
219 PETSC_EXTERN_TLS PetscLogDouble petsc_irecv_len_th;
220 PETSC_EXTERN_TLS PetscLogDouble petsc_isend_len_th;
221 PETSC_EXTERN_TLS PetscLogDouble petsc_recv_len_th;
222 PETSC_EXTERN_TLS PetscLogDouble petsc_send_len_th;
223 PETSC_EXTERN_TLS PetscLogDouble petsc_allreduce_ct_th;
224 PETSC_EXTERN_TLS PetscLogDouble petsc_gather_ct_th;
225 PETSC_EXTERN_TLS PetscLogDouble petsc_scatter_ct_th;
226 PETSC_EXTERN_TLS PetscLogDouble petsc_wait_ct_th;
227 PETSC_EXTERN_TLS PetscLogDouble petsc_wait_any_ct_th;
228 PETSC_EXTERN_TLS PetscLogDouble petsc_wait_all_ct_th;
229 PETSC_EXTERN_TLS PetscLogDouble petsc_sum_of_waits_ct_th;
230 
231 /* Global GPU counters */
232 PETSC_EXTERN PetscLogDouble petsc_ctog_ct;
233 PETSC_EXTERN PetscLogDouble petsc_gtoc_ct;
234 PETSC_EXTERN PetscLogDouble petsc_ctog_sz;
235 PETSC_EXTERN PetscLogDouble petsc_gtoc_sz;
236 PETSC_EXTERN PetscLogDouble petsc_ctog_ct_scalar;
237 PETSC_EXTERN PetscLogDouble petsc_gtoc_ct_scalar;
238 PETSC_EXTERN PetscLogDouble petsc_ctog_sz_scalar;
239 PETSC_EXTERN PetscLogDouble petsc_gtoc_sz_scalar;
240 PETSC_EXTERN PetscLogDouble petsc_gflops;
241 PETSC_EXTERN PetscLogDouble petsc_gtime;
242 
243 /* Thread local storage */
244 PETSC_EXTERN_TLS PetscLogDouble petsc_ctog_ct_th;
245 PETSC_EXTERN_TLS PetscLogDouble petsc_gtoc_ct_th;
246 PETSC_EXTERN_TLS PetscLogDouble petsc_ctog_sz_th;
247 PETSC_EXTERN_TLS PetscLogDouble petsc_gtoc_sz_th;
248 PETSC_EXTERN_TLS PetscLogDouble petsc_ctog_ct_scalar_th;
249 PETSC_EXTERN_TLS PetscLogDouble petsc_gtoc_ct_scalar_th;
250 PETSC_EXTERN_TLS PetscLogDouble petsc_ctog_sz_scalar_th;
251 PETSC_EXTERN_TLS PetscLogDouble petsc_gtoc_sz_scalar_th;
252 PETSC_EXTERN_TLS PetscLogDouble petsc_gflops_th;
253 PETSC_EXTERN_TLS PetscLogDouble petsc_gtime_th;
254 
255 PETSC_EXTERN PetscBool PetscLogMemory;
256 PETSC_EXTERN PetscBool PetscLogSyncOn; /* true if logging synchronization is enabled */
257 
258 PETSC_EXTERN PetscLogState petsc_log_state;
259 
260 #define PETSC_LOG_HANDLER_MAX 4
261 PETSC_EXTERN PetscLogHandlerHot PetscLogHandlers[PETSC_LOG_HANDLER_MAX];
262 
263 #if defined(PETSC_USE_LOG) /* --- Logging is turned on --------------------------------*/
264 PETSC_EXTERN PetscErrorCode PetscGetFlops(PetscLogDouble *);
265 
266 PETSC_EXTERN PetscErrorCode PetscLogObjectState(PetscObject, const char[], ...) PETSC_ATTRIBUTE_FORMAT(2, 3);
267 
268 /* Initialization functions */
269 PETSC_EXTERN PetscErrorCode PetscLogDefaultBegin(void);
270 PETSC_EXTERN PetscErrorCode PetscLogNestedBegin(void);
271 PETSC_EXTERN PetscErrorCode PetscLogTraceBegin(FILE *);
272 PETSC_EXTERN PetscErrorCode PetscLogMPEBegin(void);
273 PETSC_EXTERN PetscErrorCode PetscLogPerfstubsBegin(void);
274 PETSC_EXTERN PetscErrorCode PetscLogLegacyCallbacksBegin(PetscErrorCode (*)(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject), PetscErrorCode (*)(PetscLogEvent, int, PetscObject, PetscObject, PetscObject, PetscObject), PetscErrorCode (*)(PetscObject), PetscErrorCode (*)(PetscObject));
275 PETSC_EXTERN PetscErrorCode PetscLogActions(PetscBool);
276 PETSC_EXTERN PetscErrorCode PetscLogObjects(PetscBool);
277 PETSC_EXTERN PetscErrorCode PetscLogSetThreshold(PetscLogDouble, PetscLogDouble *);
278 
279 /* Output functions */
280 PETSC_EXTERN PetscErrorCode PetscLogView(PetscViewer);
281 PETSC_EXTERN PetscErrorCode PetscLogViewFromOptions(void);
282 PETSC_EXTERN PetscErrorCode PetscLogDump(const char[]);
283 PETSC_EXTERN PetscErrorCode PetscLogMPEDump(const char[]);
284 
285 PETSC_EXTERN PetscErrorCode PetscLogGetState(PetscLogState *);
286 PETSC_EXTERN PetscErrorCode PetscLogGetDefaultHandler(PetscLogHandler *);
287 PETSC_EXTERN PetscErrorCode PetscLogHandlerStart(PetscLogHandler);
288 PETSC_EXTERN PetscErrorCode PetscLogHandlerStop(PetscLogHandler);
289 
290 /* Status checking functions */
291 PETSC_EXTERN PetscErrorCode PetscLogIsActive(PetscBool *);
292 
293 /* Stage functions */
294 PETSC_EXTERN PetscErrorCode PetscLogStageRegister(const char[], PetscLogStage *);
295 PETSC_EXTERN PetscErrorCode PetscLogStagePush(PetscLogStage);
296 PETSC_EXTERN PetscErrorCode PetscLogStagePop(void);
297 PETSC_EXTERN PetscErrorCode PetscLogStageSetActive(PetscLogStage, PetscBool);
298 PETSC_EXTERN PetscErrorCode PetscLogStageGetActive(PetscLogStage, PetscBool *);
299 PETSC_EXTERN PetscErrorCode PetscLogStageSetVisible(PetscLogStage, PetscBool);
300 PETSC_EXTERN PetscErrorCode PetscLogStageGetVisible(PetscLogStage, PetscBool *);
301 PETSC_EXTERN PetscErrorCode PetscLogStageGetId(const char[], PetscLogStage *);
302 PETSC_EXTERN PetscErrorCode PetscLogStageGetName(PetscLogEvent, const char **);
303 PETSC_EXTERN PetscErrorCode PetscLogStageGetPerfInfo(PetscLogStage, PetscEventPerfInfo *);
304 
305 /* Event functions */
306 PETSC_EXTERN PetscErrorCode PetscLogEventRegister(const char[], PetscClassId, PetscLogEvent *);
307 PETSC_EXTERN PetscErrorCode PetscLogEventSetCollective(PetscLogEvent, PetscBool);
308 PETSC_EXTERN PetscErrorCode PetscLogEventIncludeClass(PetscClassId);
309 PETSC_EXTERN PetscErrorCode PetscLogEventExcludeClass(PetscClassId);
310 PETSC_EXTERN PetscErrorCode PetscLogEventActivate(PetscLogEvent);
311 PETSC_EXTERN PetscErrorCode PetscLogEventDeactivate(PetscLogEvent);
312 PETSC_EXTERN PetscErrorCode PetscLogEventDeactivatePush(PetscLogEvent);
313 PETSC_EXTERN PetscErrorCode PetscLogEventDeactivatePop(PetscLogEvent);
314 PETSC_EXTERN PetscErrorCode PetscLogEventSetActiveAll(PetscLogEvent, PetscBool);
315 PETSC_EXTERN PetscErrorCode PetscLogEventActivateClass(PetscClassId);
316 PETSC_EXTERN PetscErrorCode PetscLogEventDeactivateClass(PetscClassId);
317 PETSC_EXTERN PetscErrorCode PetscLogEventGetId(const char[], PetscLogEvent *);
318 PETSC_EXTERN PetscErrorCode PetscLogEventGetName(PetscLogEvent, const char **);
319 PETSC_EXTERN PetscErrorCode PetscLogEventGetPerfInfo(PetscLogStage, PetscLogEvent, PetscEventPerfInfo *);
320 PETSC_EXTERN PetscErrorCode PetscLogEventSetDof(PetscLogEvent, PetscInt, PetscLogDouble);
321 PETSC_EXTERN PetscErrorCode PetscLogEventSetError(PetscLogEvent, PetscInt, PetscLogDouble);
322 PETSC_EXTERN PetscErrorCode PetscLogEventsPause(void);
323 PETSC_EXTERN PetscErrorCode PetscLogEventsResume(void);
324 
325 /* Class functions */
326 PETSC_EXTERN PetscErrorCode PetscLogClassGetClassId(const char[], PetscClassId *);
327 PETSC_EXTERN PetscErrorCode PetscLogClassIdGetName(PetscClassId, const char **);
328 
329 static inline PetscErrorCode PetscLogEventSync(PetscLogEvent e, MPI_Comm comm)
330 {
331   if (PetscLogStateEventCurrentlyActive(petsc_log_state, e)) {
332     for (int i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
333       PetscLogHandlerHot *h = &PetscLogHandlers[i];
334       if (h->eventSync) {
335         PetscErrorCode err = (*h->eventSync)(h->handler, e, comm);
336         if (err != PETSC_SUCCESS) return err;
337       }
338     }
339   }
340   return PETSC_SUCCESS;
341 }
342 
343 static inline PetscErrorCode PetscLogEventBegin_Internal(PetscLogEvent e, PetscObject o1, PetscObject o2, PetscObject o3, PetscObject o4)
344 {
345   if (PetscLogStateEventCurrentlyActive(petsc_log_state, e)) {
346     for (int i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
347       PetscLogHandlerHot *h = &PetscLogHandlers[i];
348       if (h->eventBegin) {
349         PetscErrorCode err = (*h->eventBegin)(h->handler, e, o1, o2, o3, o4);
350         if (err != PETSC_SUCCESS) return err;
351       }
352     }
353   }
354   return PETSC_SUCCESS;
355 }
356   #define PetscLogEventBegin(e, o1, o2, o3, o4) PetscLogEventBegin_Internal(e, (PetscObject)(o1), (PetscObject)(o2), (PetscObject)(o3), (PetscObject)(o4))
357 
358 static inline PetscErrorCode PetscLogEventEnd_Internal(PetscLogEvent e, PetscObject o1, PetscObject o2, PetscObject o3, PetscObject o4)
359 {
360   if (PetscLogStateEventCurrentlyActive(petsc_log_state, e)) {
361     for (int i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
362       PetscLogHandlerHot *h = &PetscLogHandlers[i];
363       if (h->eventEnd) {
364         PetscErrorCode err = (*h->eventEnd)(h->handler, e, o1, o2, o3, o4);
365         if (err != PETSC_SUCCESS) return err;
366       }
367     }
368   }
369   return PETSC_SUCCESS;
370 }
371   #define PetscLogEventEnd(e, o1, o2, o3, o4) PetscLogEventEnd_Internal(e, (PetscObject)(o1), (PetscObject)(o2), (PetscObject)(o3), (PetscObject)(o4))
372 
373 /* Object functions */
374 static inline PetscErrorCode PetscLogObjectCreate(PetscObject o)
375 {
376   if (petsc_log_state) {
377     for (int i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
378       PetscLogHandlerHot *h = &PetscLogHandlers[i];
379       if (h->objectCreate) {
380         PetscErrorCode err = (*h->objectCreate)(h->handler, o);
381         if (err != PETSC_SUCCESS) return err;
382       }
383     }
384   }
385   return PETSC_SUCCESS;
386 }
387 
388 static inline PetscErrorCode PetscLogObjectDestroy(PetscObject o)
389 {
390   if (petsc_log_state) {
391     for (int i = 0; i < PETSC_LOG_HANDLER_MAX; i++) {
392       PetscLogHandlerHot *h = &PetscLogHandlers[i];
393       if (h->objectDestroy) {
394         PetscErrorCode err = (*h->objectDestroy)(h->handler, o);
395         if (err != PETSC_SUCCESS) return err;
396       }
397     }
398   }
399   return PETSC_SUCCESS;
400 }
401 
402 /*
403    Flop counting:  We count each arithmetic operation (e.g., addition, multiplication) separately.
404 
405    For the complex numbers version, note that
406        1 complex addition = 2 flops
407        1 complex multiplication = 6 flops,
408    where we define 1 flop as that for a double precision scalar.  We roughly approximate
409    flop counting for complex numbers by multiplying the total flops by 4; this corresponds
410    to the assumption that we're counting mostly additions and multiplications -- and
411    roughly the same number of each.  More accurate counting could be done by distinguishing
412    among the various arithmetic operations.
413  */
414 
415   #if defined(PETSC_USE_COMPLEX)
416     #define PETSC_FLOPS_PER_OP 4.0
417   #else
418     #define PETSC_FLOPS_PER_OP 1.0
419   #endif
420 
421 /*@
422    PetscLogFlops - Log how many flops are performed in a calculation
423 
424    Input Parameter:
425 .   flops - the number of flops
426 
427    Level: intermediate
428 
429    Note:
430    To limit the chance of integer overflow when multiplying by a constant, represent the constant as a double,
431    not an integer. Use `PetscLogFlops`(4.0*n) not `PetscLogFlops`(4*n)
432 
433 .seealso: [](ch_profiling), `PetscLogView()`, `PetscLogGpuFlops()`
434 @*/
435 static inline PetscErrorCode PetscLogFlops(PetscLogDouble n)
436 {
437   PetscAssert(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot log negative flops");
438   return PetscAddLogDouble(&petsc_TotalFlops, &petsc_TotalFlops_th, PETSC_FLOPS_PER_OP * n);
439 }
440 
441   /*
442      These are used internally in the PETSc routines to keep a count of MPI messages and
443    their sizes.
444 
445      This does not work for MPI-Uni because our include/petsc/mpiuni/mpi.h file
446    uses macros to defined the MPI operations.
447 
448      It does not work correctly from HP-UX because it processes the
449    macros in a way that sometimes it double counts, hence
450    PETSC_HAVE_BROKEN_RECURSIVE_MACRO
451 
452      It does not work with Windows because winmpich lacks MPI_Type_size()
453 */
454   #if !defined(MPIUNI_H) && !defined(PETSC_HAVE_BROKEN_RECURSIVE_MACRO)
455 /*
456    Logging of MPI activities
457 */
458 static inline PetscErrorCode PetscMPITypeSize(PetscCount count, MPI_Datatype type, PetscLogDouble *length, PetscLogDouble *length_th)
459 {
460   PetscMPIInt typesize;
461 
462   if (type == MPI_DATATYPE_NULL) return PETSC_SUCCESS;
463   PetscCallMPI(MPI_Type_size(type, &typesize));
464   return PetscAddLogDouble(length, length_th, (PetscLogDouble)(count * typesize));
465 }
466 
467 static inline PetscErrorCode PetscMPITypeSizeComm(MPI_Comm comm, const PetscMPIInt *counts, MPI_Datatype type, PetscLogDouble *length, PetscLogDouble *length_th)
468 {
469   PetscMPIInt    typesize, size, p;
470   PetscLogDouble l;
471 
472   if (type == MPI_DATATYPE_NULL) return PETSC_SUCCESS;
473   PetscCallMPI(MPI_Comm_size(comm, &size));
474   PetscCallMPI(MPI_Type_size(type, &typesize));
475   for (p = 0, l = 0.0; p < size; ++p) l += (PetscLogDouble)(counts[p] * typesize);
476   return PetscAddLogDouble(length, length_th, l);
477 }
478 
479 /*
480     Returns 1 if the communicator is parallel else zero
481 */
482 static inline int PetscMPIParallelComm(MPI_Comm comm)
483 {
484   PetscMPIInt size;
485   MPI_Comm_size(comm, &size);
486   return size > 1;
487 }
488 
489     #define MPI_Irecv(buf, count, datatype, source, tag, comm, request) \
490       (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)))
491 
492     #define MPI_Irecv_c(buf, count, datatype, source, tag, comm, request) \
493       (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)))
494 
495     #define MPI_Isend(buf, count, datatype, dest, tag, comm, request) \
496       (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)))
497 
498     #define MPI_Isend_c(buf, count, datatype, dest, tag, comm, request) \
499       (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)))
500 
501     #define MPI_Startall_irecv(count, datatype, number, requests) \
502       (PetscAddLogDouble(&petsc_irecv_ct, &petsc_irecv_ct_th, number) || PetscMPITypeSize((count), (datatype), &(petsc_irecv_len), &(petsc_irecv_len_th)) || ((number) && MPI_Startall((number), (requests))))
503 
504     #define MPI_Startall_isend(count, datatype, number, requests) \
505       (PetscAddLogDouble(&petsc_isend_ct, &petsc_isend_ct_th, number) || PetscMPITypeSize((count), (datatype), &(petsc_isend_len), &(petsc_isend_len_th)) || ((number) && MPI_Startall((number), (requests))))
506 
507     #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))
508 
509     #define MPI_Recv(buf, count, datatype, source, tag, comm, status) \
510       (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)))
511 
512     #define MPI_Recv_c(buf, count, datatype, source, tag, comm, status) \
513       (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)))
514 
515     #define MPI_Send(buf, count, datatype, dest, tag, comm) \
516       (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)))
517 
518     #define MPI_Send_c(buf, count, datatype, dest, tag, comm) \
519       (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)))
520 
521     #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)))
522 
523     #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)))
524 
525     #define MPI_Waitall(count, array_of_requests, array_of_statuses) \
526       (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)))
527 
528     #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)))
529 
530     #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)))
531 
532     #define MPI_Reduce_scatter_block(sendbuf, recvbuf, recvcount, datatype, op, comm) \
533       (PetscAddLogDouble(&petsc_allreduce_ct, &petsc_allreduce_ct_th, PetscMPIParallelComm(comm)) || MPI_Reduce_scatter_block((sendbuf), (recvbuf), (recvcount), (datatype), (op), (comm)))
534 
535     #define MPI_Alltoall(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm) \
536       (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)))
537 
538     #define MPI_Alltoallv(sendbuf, sendcnts, sdispls, sendtype, recvbuf, recvcnts, rdispls, recvtype, comm) \
539       (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)))
540 
541     #define MPI_Allgather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm) \
542       (PetscAddLogDouble(&petsc_gather_ct, &petsc_gather_ct_th, PetscMPIParallelComm(comm)) || MPI_Allgather((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (recvtype), (comm)))
543 
544     #define MPI_Allgatherv(sendbuf, sendcount, sendtype, recvbuf, recvcount, displs, recvtype, comm) \
545       (PetscAddLogDouble(&petsc_gather_ct, &petsc_gather_ct_th, PetscMPIParallelComm(comm)) || MPI_Allgatherv((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (displs), (recvtype), (comm)))
546 
547     #define MPI_Gather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm) \
548       (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)))
549 
550     #define MPI_Gatherv(sendbuf, sendcount, sendtype, recvbuf, recvcount, displs, recvtype, root, comm) \
551       (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)))
552 
553     #define MPI_Scatter(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm) \
554       (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)))
555 
556     #define MPI_Scatterv(sendbuf, sendcount, displs, sendtype, recvbuf, recvcount, recvtype, root, comm) \
557       (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)))
558 
559     #define MPI_Ialltoall(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm, request) \
560       (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)))
561 
562     #define MPI_Ialltoallv(sendbuf, sendcnts, sdispls, sendtype, recvbuf, recvcnts, rdispls, recvtype, comm, request) \
563       (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)))
564 
565     #define MPI_Iallgather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, comm, request) \
566       (PetscAddLogDouble(&petsc_gather_ct, &petsc_gather_ct_th, PetscMPIParallelComm(comm)) || MPI_Iallgather((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (recvtype), (comm), (request)))
567 
568     #define MPI_Iallgatherv(sendbuf, sendcount, sendtype, recvbuf, recvcount, displs, recvtype, comm, request) \
569       (PetscAddLogDouble(&petsc_gather_ct, &petsc_gather_ct_th, PetscMPIParallelComm(comm)) || MPI_Iallgatherv((sendbuf), (sendcount), (sendtype), (recvbuf), (recvcount), (displs), (recvtype), (comm), (request)))
570 
571     #define MPI_Igather(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm, request) \
572       (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)))
573 
574     #define MPI_Igatherv(sendbuf, sendcount, sendtype, recvbuf, recvcount, displs, recvtype, root, comm, request) \
575       (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)))
576 
577     #define MPI_Iscatter(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm, request) \
578       (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)))
579 
580     #define MPI_Iscatterv(sendbuf, sendcount, displs, sendtype, recvbuf, recvcount, recvtype, root, comm, request) \
581       (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)))
582 
583     #define MPIX_Send_enqueue(buf, count, datatype, dest, tag, comm) \
584       (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)))
585 
586     #define MPIX_Recv_enqueue(buf, count, datatype, source, tag, comm, status) \
587       (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)))
588 
589     #define MPIX_Isend_enqueue(buf, count, datatype, dest, tag, comm, request) \
590       (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)))
591 
592     #define MPIX_Irecv_enqueue(buf, count, datatype, source, tag, comm, request) \
593       (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)))
594 
595     #define MPIX_Allreduce_enqueue(sendbuf, recvbuf, count, datatype, op, comm) \
596       (PetscAddLogDouble(&petsc_allreduce_ct, &petsc_allreduce_ct_th, PetscMPIParallelComm(comm)) || MPIX_Allreduce_enqueue((sendbuf), (recvbuf), (count), (datatype), (op), (comm)))
597 
598     #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)))
599 
600     #define MPIX_Waitall_enqueue(count, array_of_requests, array_of_statuses) \
601       (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)))
602   #else
603 
604     #define MPI_Startall_irecv(count, datatype, number, requests) ((number) && MPI_Startall((number), (requests)))
605 
606     #define MPI_Startall_isend(count, datatype, number, requests) ((number) && MPI_Startall((number), (requests)))
607 
608     #define MPI_Start_isend(count, datatype, requests) (MPI_Start(requests))
609 
610   #endif /* !MPIUNI_H && ! PETSC_HAVE_BROKEN_RECURSIVE_MACRO */
611 
612 #else /* ---Logging is turned off --------------------------------------------*/
613 
614   #define PetscLogGetState(a)          (*(a) = NULL, PETSC_SUCCESS)
615   #define PetscLogGetDefaultHandler(a) (*(a) = NULL, PETSC_SUCCESS)
616   #define PetscLogHandlerStart(a)      ((void)(a), PETSC_SUCCESS)
617   #define PetscLogHandlerStop(a)       ((void)(a), PETSC_SUCCESS)
618 
619   #define PetscLogFlops(n) ((void)(n), PETSC_SUCCESS)
620   #define PetscGetFlops(a) (*(a) = 0.0, PETSC_SUCCESS)
621 
622   #define PetscLogStageRegister(a, b)    ((void)(a), *(b) = -1, PETSC_SUCCESS)
623   #define PetscLogStagePush(a)           ((void)(a), PETSC_SUCCESS)
624   #define PetscLogStagePop()             PETSC_SUCCESS
625   #define PetscLogStageSetActive(a, b)   ((void)(a), (void)(b), PETSC_SUCCESS)
626   #define PetscLogStageGetActive(a, b)   ((void)(a), *(b) = PETSC_FALSE, PETSC_SUCCESS)
627   #define PetscLogStageGetVisible(a, b)  ((void)(a), *(b) = PETSC_FALSE, PETSC_SUCCESS)
628   #define PetscLogStageSetVisible(a, b)  ((void)(a), (void)(b), PETSC_SUCCESS)
629   #define PetscLogStageGetId(a, b)       ((void)(a), *(b) = -1, PETSC_SUCCESS)
630   #define PetscLogStageGetName(a, b)     ((void)(a), *(b) = NULL, PETSC_SUCCESS)
631   #define PetscLogStageGetPerfInfo(a, b) ((void)(a), *(b) = (const PetscEventPerfInfo){0}, PETSC_SUCCESS)
632 
633   #define PetscLogEventRegister(a, b, c)    ((void)(a), (void)(b), *(c) = -1, PETSC_SUCCESS)
634   #define PetscLogEventSetCollective(a, b)  ((void)(a), (void)(b), PETSC_SUCCESS)
635   #define PetscLogEventIncludeClass(a)      ((void)(a), PETSC_SUCCESS)
636   #define PetscLogEventExcludeClass(a)      ((void)(a), PETSC_SUCCESS)
637   #define PetscLogEventActivate(a)          ((void)(a), PETSC_SUCCESS)
638   #define PetscLogEventDeactivate(a)        ((void)(a), PETSC_SUCCESS)
639   #define PetscLogEventDeactivatePush(a)    ((void)(a), PETSC_SUCCESS)
640   #define PetscLogEventDeactivatePop(a)     ((void)(a), PETSC_SUCCESS)
641   #define PetscLogEventActivateClass(a)     ((void)(a), PETSC_SUCCESS)
642   #define PetscLogEventDeactivateClass(a)   ((void)(a), PETSC_SUCCESS)
643   #define PetscLogEventSetActiveAll(a, b)   ((void)(a), PETSC_SUCCESS)
644   #define PetscLogEventGetId(a, b)          ((void)(a), *(b) = -1, PETSC_SUCCESS)
645   #define PetscLogEventGetName(a, b)        ((void)(a), *(b) = NULL, PETSC_SUCCESS)
646   #define PetscLogEventGetPerfInfo(a, b, c) ((void)(a), (void)(b), *(c) = (const PetscEventPerfInfo){0}, PETSC_SUCCESS)
647 
648   #define PetscLogEventSetDof(a, b, c)   ((void)(a), (void)(b), (void)(c), PETSC_SUCCESS)
649   #define PetscLogEventSetError(a, b, c) ((void)(a), (void)(b), (void)(c), PETSC_SUCCESS)
650   #define PetscLogEventsPause()          PETSC_SUCCESS
651   #define PetscLogEventsResume()         PETSC_SUCCESS
652 
653   #define PetscLogClassGetClassId(a, b) (*(b) = -1, PETSC_SUCCESS)
654   #define PetscLogClassIdGetName(a, b)  (*(b) = NULL, PETSC_SUCCESS)
655 
656   #define PetscLogObjectCreate(h)        ((void)(h), PETSC_SUCCESS)
657   #define PetscLogObjectDestroy(h)       ((void)(h), PETSC_SUCCESS)
658   #define PetscLogObjectState(h, c, ...) ((void)(h), (void)(c), PETSC_SUCCESS)
659 
660   #define PetscLogDefaultBegin()                   PETSC_SUCCESS
661   #define PetscLogNestedBegin()                    PETSC_SUCCESS
662   #define PetscLogTraceBegin(file)                 ((void)(file), PETSC_SUCCESS)
663   #define PetscLogMPEBegin()                       PETSC_SUCCESS
664   #define PetscLogPerfstubsBegin()                 PETSC_SUCCESS
665   #define PetscLogLegacyCallbacksBegin(a, b, c, d) ((void)(a), (void)(b), (void)(c), (void)(d), PETSC_SUCCESS)
666   #define PetscLogActions(a)                       ((void)(a), PETSC_SUCCESS)
667   #define PetscLogObjects(a)                       ((void)(a), PETSC_SUCCESS)
668   #define PetscLogSetThreshold(a, b)               ((void)(a), (void)(b), PETSC_SUCCESS)
669 
670   #define PetscLogIsActive(flag) (*(flag) = PETSC_FALSE, PETSC_SUCCESS)
671 
672   #define PetscLogView(viewer)      ((void)(viewer), PETSC_SUCCESS)
673   #define PetscLogViewFromOptions() PETSC_SUCCESS
674   #define PetscLogDump(c)           ((void)(c), PETSC_SUCCESS)
675   #define PetscLogMPEDump(c)        ((void)(c), PETSC_SUCCESS)
676 
677   #define PetscLogEventSync(e, comm)                            ((void)(e), (void)(comm), PETSC_SUCCESS)
678   #define PetscLogEventBegin(e, o1, o2, o3, o4)                 ((void)(e), (void)(o1), (void)(o2), (void)(o3), PETSC_SUCCESS)
679   #define PetscLogEventEnd(e, o1, o2, o3, o4)                   ((void)(e), (void)(o1), (void)(o2), (void)(o3), PETSC_SUCCESS)
680 
681   /* If PETSC_USE_LOG is NOT defined, these still need to be! */
682   #define MPI_Startall_irecv(count, datatype, number, requests) ((number) && MPI_Startall(number, requests))
683   #define MPI_Startall_isend(count, datatype, number, requests) ((number) && MPI_Startall(number, requests))
684   #define MPI_Start_isend(count, datatype, requests)            MPI_Start(requests)
685 
686 #endif /* PETSC_USE_LOG */
687 
688 #define PetscPreLoadBegin(flag, name) \
689   do { \
690     PetscBool     PetscPreLoading = flag; \
691     int           PetscPreLoadMax, PetscPreLoadIt; \
692     PetscLogStage _stageNum; \
693     PetscCall(PetscOptionsGetBool(NULL, NULL, "-preload", &PetscPreLoading, NULL)); \
694     PetscPreLoadMax     = (int)(PetscPreLoading); \
695     PetscPreLoadingUsed = PetscPreLoading ? PETSC_TRUE : PetscPreLoadingUsed; \
696     PetscCall(PetscLogStageGetId(name, &_stageNum)); \
697     for (PetscPreLoadIt = (_stageNum == -1) ? 0 : PetscPreLoadMax; PetscPreLoadIt <= PetscPreLoadMax; PetscPreLoadIt++) { \
698       PetscPreLoadingOn = (PetscBool)(PetscPreLoadIt < PetscPreLoadMax); \
699       PetscCall(PetscBarrier(NULL)); \
700       if (_stageNum == -1) PetscCall(PetscLogStageRegister(name, &_stageNum)); \
701       PetscCall(PetscLogStageSetActive(_stageNum, (PetscBool)(PetscPreLoadIt == PetscPreLoadMax))); \
702       PetscCall(PetscLogStagePush(_stageNum))
703 
704 #define PetscPreLoadEnd() \
705   PetscCall(PetscLogStagePop()); \
706   } \
707   } \
708   while (0)
709 
710 #define PetscPreLoadStage(name) \
711   do { \
712     PetscCall(PetscLogStagePop()); \
713     PetscCall(PetscLogStageGetId(name, &_stageNum)); \
714     if (_stageNum == -1) PetscCall(PetscLogStageRegister(name, &_stageNum)); \
715     PetscCall(PetscLogStageSetActive(_stageNum, (PetscBool)(!PetscPreLoadMax || PetscPreLoadIt))); \
716     PetscCall(PetscLogStagePush(_stageNum)); \
717   } while (0)
718 
719 /* some vars for logging */
720 PETSC_EXTERN PetscBool PetscPreLoadingUsed; /* true if we are or have done preloading */
721 PETSC_EXTERN PetscBool PetscPreLoadingOn;   /* true if we are currently in a preloading calculation */
722 
723 #if defined(PETSC_USE_LOG) && defined(PETSC_HAVE_DEVICE)
724 
725 PETSC_EXTERN PetscErrorCode PetscLogGpuTime(void);
726 PETSC_EXTERN PetscErrorCode PetscLogGpuTimeBegin(void);
727 PETSC_EXTERN PetscErrorCode PetscLogGpuTimeEnd(void);
728 
729 /*@
730    PetscLogGpuFlops - Log how many flops are performed in a calculation on the device
731 
732    Input Parameter:
733 .  flops - the number of flops
734 
735    Level: intermediate
736 
737    Notes:
738    To limit the chance of integer overflow when multiplying by a constant, represent the constant as a double,
739    not an integer. Use `PetscLogFlops`(4.0*n) not `PetscLogFlops`(4*n)
740 
741    The values are also added to the total flop count for the MPI rank that is set with `PetscLogFlops()`; hence the number of flops
742    just on the CPU would be the value from set from `PetscLogFlops()` minus the value set from `PetscLogGpuFlops()`
743 
744    Developer Note:
745    Currently Fortran stub generator cannot run through files in include
746 
747 .seealso: [](ch_profiling), `PetscLogView()`, `PetscLogFlops()`, `PetscLogGpuTimeBegin()`, `PetscLogGpuTimeEnd()`
748 @*/
749 static inline PetscErrorCode PetscLogGpuFlops(PetscLogDouble n)
750 {
751   PetscAssert(n >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot log negative flops");
752   PetscCall(PetscAddLogDouble(&petsc_TotalFlops, &petsc_TotalFlops_th, PETSC_FLOPS_PER_OP * n));
753   PetscCall(PetscAddLogDouble(&petsc_gflops, &petsc_gflops_th, PETSC_FLOPS_PER_OP * n));
754   return PETSC_SUCCESS;
755 }
756 
757 static inline PetscErrorCode PetscLogGpuTimeAdd(PetscLogDouble t)
758 {
759   return PetscAddLogDouble(&petsc_gtime, &petsc_gtime_th, t);
760 }
761 
762 static inline PetscErrorCode PetscLogCpuToGpu(PetscLogDouble size)
763 {
764   return PetscAddLogDoubleCnt(&petsc_ctog_ct, &petsc_ctog_sz, &petsc_ctog_ct_th, &petsc_ctog_sz_th, size);
765 }
766 
767 static inline PetscErrorCode PetscLogGpuToCpu(PetscLogDouble size)
768 {
769   return PetscAddLogDoubleCnt(&petsc_gtoc_ct, &petsc_gtoc_sz, &petsc_gtoc_ct_th, &petsc_gtoc_sz_th, size);
770 }
771 
772 static inline PetscErrorCode PetscLogCpuToGpuScalar(PetscLogDouble size)
773 {
774   return PetscAddLogDoubleCnt(&petsc_ctog_ct_scalar, &petsc_ctog_sz_scalar, &petsc_ctog_ct_scalar_th, &petsc_ctog_sz_scalar_th, size);
775 }
776 
777 static inline PetscErrorCode PetscLogGpuToCpuScalar(PetscLogDouble size)
778 {
779   return PetscAddLogDoubleCnt(&petsc_gtoc_ct_scalar, &petsc_gtoc_sz_scalar, &petsc_gtoc_ct_scalar_th, &petsc_gtoc_sz_scalar_th, size);
780 }
781 #else
782 
783   #define PetscLogCpuToGpu(a)       PETSC_SUCCESS
784   #define PetscLogGpuToCpu(a)       PETSC_SUCCESS
785   #define PetscLogCpuToGpuScalar(a) PETSC_SUCCESS
786   #define PetscLogGpuToCpuScalar(a) PETSC_SUCCESS
787   #define PetscLogGpuFlops(a)       PETSC_SUCCESS
788   #define PetscLogGpuTime()         PETSC_SUCCESS
789   #define PetscLogGpuTimeAdd(a)     PETSC_SUCCESS
790   #define PetscLogGpuTimeBegin()    PETSC_SUCCESS
791   #define PetscLogGpuTimeEnd()      PETSC_SUCCESS
792 
793 #endif /* PETSC_USE_LOG && PETSC_HAVE_DEVICE */
794 
795 /* remove TLS defines */
796 #undef PETSC_EXTERN_TLS
797 #undef PETSC_TLS
798 
799 #include <petsclogdeprecated.h>
800