xref: /petsc/include/petsclog.h (revision 634064b45b5c838063ae82f97ffb7e99245dcdb5)
1 /*
2     Defines profile/logging in PETSc.
3 */
4 
5 #if !defined(__PetscLog_H)
6 #define __PetscLog_H
7 #include "petsc.h"
8 PETSC_EXTERN_CXX_BEGIN
9 /*
10   Each PETSc object class has it's own cookie (internal integer in the
11   data structure used for error checking). These are all defined by an offset
12   from the lowest one, PETSC_COOKIE.
13 */
14 #define PETSC_COOKIE 1211211
15 extern int PETSC_LARGEST_COOKIE;
16 #define PETSC_EVENT  1311311
17 extern int PETSC_LARGEST_EVENT;
18 
19 /* Events for the Petsc standard library */
20 extern int PETSC_Barrier;
21 
22 /* Global flop counter */
23 extern PetscLogDouble _TotalFlops;
24 
25 /* General logging of information; different from event logging */
26 EXTERN int        PetscLogInfo(void*,const char[],...) PETSC_PRINTF_FORMAT_CHECK(2,3);
27 EXTERN int        PetscLogInfoDeactivateClass(int);
28 EXTERN int        PetscLogInfoActivateClass(int);
29 extern PetscTruth PetscLogPrintInfo;  /* if true, indicates PetscLogInfo() is turned on */
30 
31 #if defined(PETSC_USE_LOG)  /* --- Logging is turned on --------------------------------*/
32 
33 /*
34    Flop counting:  We count each arithmetic operation (e.g., addition, multiplication) separately.
35 
36    For the complex numbers version, note that
37        1 complex addition = 2 flops
38        1 complex multiplication = 6 flops,
39    where we define 1 flop as that for a double precision scalar.  We roughly approximate
40    flop counting for complex numbers by multiplying the total flops by 4; this corresponds
41    to the assumption that we're counting mostly additions and multiplications -- and
42    roughly the same number of each.  More accurate counting could be done by distinguishing
43    among the various arithmetic operations.
44  */
45 
46 #if defined(PETSC_USE_COMPLEX)
47 #define PetscLogFlops(n) (_TotalFlops += (4*n),0)
48 #else
49 #define PetscLogFlops(n) (_TotalFlops += (n),0)
50 #endif
51 
52 #if defined (PETSC_HAVE_MPE)
53 #include "mpe.h"
54 EXTERN int        PetscLogMPEBegin(void);
55 EXTERN int        PetscLogMPEDump(const char[]);
56 extern PetscTruth UseMPE;
57 #define PETSC_LOG_EVENT_MPE_BEGIN(e) \
58   if(UseMPE && _stageLog->stageInfo[_stageLog->curStage].eventLog->eventInfo[e].active) \
59     MPE_Log_event(_stageLog->eventLog->eventInfo[e].mpe_id_begin,0,(char*)"");
60 
61 #define PETSC_LOG_EVENT_MPE_END(e) \
62   if(UseMPE && _stageLog->stageInfo[_stageLog->curStage].eventLog->eventInfo[e].active) \
63     MPE_Log_event(_stageLog->eventLog->eventInfo[e].mpe_id_end,0,(char*)"");
64 
65 #else
66 #define PETSC_LOG_EVENT_MPE_BEGIN(e)
67 #define PETSC_LOG_EVENT_MPE_END(e)
68 #endif
69 
70 EXTERN int (*_PetscLogPLB)(int,int,PetscObject,PetscObject,PetscObject,PetscObject);
71 EXTERN int (*_PetscLogPLE)(int,int,PetscObject,PetscObject,PetscObject,PetscObject);
72 EXTERN int (*_PetscLogPHC)(PetscObject);
73 EXTERN int (*_PetscLogPHD)(PetscObject);
74 
75 #define PetscLogObjectParent(p,c) \
76   if (c) {\
77     PetscValidHeader((PetscObject)(c),2);\
78     PetscValidHeader((PetscObject)(p),1);\
79     ((PetscObject)(c))->parent = (PetscObject)(p);\
80     ((PetscObject)(c))->parentid = ((PetscObject)p)->id;\
81   }
82 #define PetscLogObjectParents(p,n,d) {int _i; for (_i=0; _i<n; _i++) PetscLogObjectParent(p,(d)[_i]);}
83 #define PetscLogObjectCreate(h)      {if (_PetscLogPHC) (*_PetscLogPHC)((PetscObject)h);}
84 #define PetscLogObjectDestroy(h)     {if (_PetscLogPHD) (*_PetscLogPHD)((PetscObject)h);}
85 #define PetscLogObjectMemory(p,m)    {PetscValidHeader((PetscObject)p,1);((PetscObject)(p))->mem += (m);}
86 /* Initialization functions */
87 EXTERN int PetscLogBegin(void);
88 EXTERN int PetscLogAllBegin(void);
89 EXTERN int PetscLogTraceBegin(FILE *);
90 EXTERN int PetscLogActions(PetscTruth);
91 EXTERN int PetscLogObjects(PetscTruth);
92 /* General functions */
93 EXTERN int PetscLogGetRGBColor(const char*[]);
94 EXTERN int PetscLogDestroy(void);
95 EXTERN int PetscLogSet(int (*)(int, int, PetscObject, PetscObject, PetscObject, PetscObject),
96                    int (*)(int, int, PetscObject, PetscObject, PetscObject, PetscObject));
97 EXTERN int PetscLogObjectState(PetscObject, const char[], ...)  PETSC_PRINTF_FORMAT_CHECK(2,3);
98 /* Output functions */
99 EXTERN int PetscLogPrintSummary(MPI_Comm, const char[]);
100 EXTERN int PetscLogDump(const char[]);
101 /* Counter functions */
102 EXTERN int PetscGetFlops(PetscLogDouble *);
103 /* Stage functions */
104 EXTERN int PetscLogStageRegister(int*, const char[]);
105 EXTERN int PetscLogStagePush(int);
106 EXTERN int PetscLogStagePop(void);
107 EXTERN int PetscLogStageSetActive(int, PetscTruth);
108 EXTERN int PetscLogStageGetActive(int, PetscTruth *);
109 EXTERN int PetscLogStageSetVisible(int, PetscTruth);
110 EXTERN int PetscLogStageGetVisible(int, PetscTruth *);
111 EXTERN int PetscLogStageGetId(const char [], int *);
112 /* Event functions */
113 EXTERN int PetscLogEventRegister(int*, const char[], int);
114 EXTERN int PetscLogEventActivate(int);
115 EXTERN int PetscLogEventDeactivate(int);
116 EXTERN int PetscLogEventSetActiveAll(int, PetscTruth);
117 EXTERN int PetscLogEventActivateClass(int);
118 EXTERN int PetscLogEventDeactivateClass(int);
119 /* Class functions */
120 EXTERN int PetscLogClassRegister(int*, const char []);
121 
122 /* Global counters */
123 extern PetscLogDouble irecv_ct,  isend_ct,  recv_ct,  send_ct;
124 extern PetscLogDouble irecv_len, isend_len, recv_len, send_len;
125 extern PetscLogDouble allreduce_ct;
126 extern PetscLogDouble wait_ct, wait_any_ct, wait_all_ct, sum_of_waits_ct;
127 extern int            PETSC_DUMMY, PETSC_DUMMY_SIZE;
128 
129 /* We must make these structures available if we are to access the event
130    activation flags in the PetscLogEventBegin/End() macros. If we forced a
131    function call each time, we could leave these structures in plog.h
132 */
133 /* Default log */
134 typedef struct _StageLog *StageLog;
135 extern StageLog _stageLog;
136 
137 /* A simple stack (should replace) */
138 typedef struct _IntStack *IntStack;
139 
140 /* The structures for logging performance */
141 typedef struct _EventPerfInfo {
142   int            id;            /* The integer identifying this section */
143   PetscTruth     active;        /* The flag to activate logging */
144   PetscTruth     visible;       /* The flag to print info in summary */
145   int            depth;         /* The nesting depth of the event call */
146   int            count;         /* The number of times this section was executed */
147   PetscLogDouble flops;         /* The flops used in this section */
148   PetscLogDouble time;          /* The time taken for this section */
149   PetscLogDouble numMessages;   /* The number of messages in this section */
150   PetscLogDouble messageLength; /* The total message lengths in this section */
151   PetscLogDouble numReductions; /* The number of reductions in this section */
152 } EventPerfInfo;
153 
154 typedef struct _ClassPerfInfo {
155   int            id;           /* The integer identifying this class */
156   int            creations;    /* The number of objects of this class created */
157   int            destructions; /* The number of objects of this class destroyed */
158   PetscLogDouble mem;          /* The total memory allocated by objects of this class */
159   PetscLogDouble descMem;      /* The total memory allocated by descendents of these objects */
160 } ClassPerfInfo;
161 
162 /* The structures for logging registration */
163 typedef struct _ClassRegInfo {
164   char *name;   /* The class name */
165   int   cookie; /* The integer identifying this class */
166 } ClassRegInfo;
167 
168 typedef struct _EventRegInfo {
169   char *name;   /* The name of this event */
170   int   cookie; /* The class id for this event (should maybe give class ID instead) */
171 #if defined (PETSC_HAVE_MPE)
172   int   mpe_id_begin; /* MPE IDs that define the event */
173   int   mpe_id_end;
174 #endif
175 } EventRegInfo;
176 
177 /* The structure for logging events */
178 typedef int PetscEvent;
179 
180 typedef struct _EventRegLog *EventRegLog;
181 struct _EventRegLog {
182   int           numEvents; /* The number of registered events */
183   int           maxEvents; /* The maximum number of events */
184   EventRegInfo *eventInfo; /* The registration information for each event */
185 };
186 
187 typedef struct _EventPerfLog *EventPerfLog;
188 struct _EventPerfLog {
189   int            numEvents; /* The number of logging events */
190   int            maxEvents; /* The maximum number of events */
191   EventPerfInfo *eventInfo; /* The performance information for each event */
192 };
193 
194 /* The structure for logging class information */
195 typedef struct _ClassRegLog *ClassRegLog;
196 struct _ClassRegLog {
197   int           numClasses; /* The number of classes registered */
198   int           maxClasses; /* The maximum number of classes */
199   ClassRegInfo *classInfo;  /* The structure for class information (cookies are monotonicly increasing) */
200 };
201 
202 typedef struct _ClassPerfLog *ClassPerfLog;
203 struct _ClassPerfLog {
204   int            numClasses; /* The number of logging classes */
205   int            maxClasses; /* The maximum number of classes */
206   ClassPerfInfo *classInfo;  /* The structure for class information (cookies are monotonicly increasing) */
207 };
208 
209 /* The structures for logging in stages */
210 typedef struct _StageInfo {
211   char         *name;     /* The stage name */
212   PetscTruth    used;     /* The stage was pushed on this processor */
213   EventPerfInfo perfInfo; /* The stage performance information */
214   EventPerfLog  eventLog; /* The event information for this stage */
215   ClassPerfLog  classLog; /* The class information for this stage */
216 } StageInfo;
217 
218 struct _StageLog {
219   /* Size information */
220   int         numStages; /* The number of registered stages */
221   int         maxStages; /* The maximum number of stages */
222   /* Runtime information */
223   IntStack    stack;     /* The stack for active stages */
224   int         curStage;  /* The current stage (only used in macros so we don't call StackTop) */
225   /* Stage specific information */
226   StageInfo  *stageInfo; /* The information for each stage */
227   EventRegLog eventLog;  /* The registered events */
228   ClassRegLog classLog;  /* The registered classes */
229 };
230 
231 #define PetscLogEventBarrierBegin(e,o1,o2,o3,o4,cm) 0; \
232 {\
233   int _2_ierr;\
234   if (_PetscLogPLB && \
235       _stageLog->stageInfo[_stageLog->curStage].perfInfo.active && \
236       _stageLog->stageInfo[_stageLog->curStage].eventLog->eventInfo[e].active) {\
237     _2_ierr = PetscLogEventBegin((e),o1,o2,o3,o4);CHKERRQ(_2_ierr);\
238     _2_ierr = MPI_Barrier(cm);CHKERRQ(_2_ierr);\
239     _2_ierr = PetscLogEventEnd((e),o1,o2,o3,o4);CHKERRQ(_2_ierr);\
240   }\
241   _2_ierr = PetscLogEventBegin((e)+1,o1,o2,o3,o4);CHKERRQ(_2_ierr);\
242 }
243 
244 #define PetscLogEventBegin(e,o1,o2,o3,o4) 0; \
245 {\
246   if (_PetscLogPLB && \
247       _stageLog->stageInfo[_stageLog->curStage].perfInfo.active && \
248       _stageLog->stageInfo[_stageLog->curStage].eventLog->eventInfo[e].active) {\
249     (*_PetscLogPLB)((e),0,(PetscObject)(o1),(PetscObject)(o2),(PetscObject)(o3),(PetscObject)(o4));\
250   }\
251   PETSC_LOG_EVENT_MPE_BEGIN(e); \
252 }
253 
254 #define PetscLogEventBarrierEnd(e,o1,o2,o3,o4,cm) PetscLogEventEnd(e+1,o1,o2,o3,o4)
255 
256 #define PetscLogEventEnd(e,o1,o2,o3,o4) 0; \
257 {\
258   if (_PetscLogPLE && \
259       _stageLog->stageInfo[_stageLog->curStage].perfInfo.active && \
260       _stageLog->stageInfo[_stageLog->curStage].eventLog->eventInfo[e].active) {\
261     (*_PetscLogPLE)((e),0,(PetscObject)(o1),(PetscObject)(o2),(PetscObject)(o3),(PetscObject)(o4));\
262   }\
263   PETSC_LOG_EVENT_MPE_END(e); \
264 }
265 
266 /* Creation and destruction functions */
267 EXTERN int StageLogCreate(StageLog *);
268 EXTERN int StageLogDestroy(StageLog);
269 /* Registration functions */
270 EXTERN int StageLogRegister(StageLog, const char [], int *);
271 /* Runtime functions */
272 EXTERN int PetscLogGetStageLog(StageLog *);
273 EXTERN int StageLogPush(StageLog, int);
274 EXTERN int StageLogPop(StageLog);
275 EXTERN int StageLogGetCurrent(StageLog, int *);
276 EXTERN int StageLogSetActive(StageLog, int, PetscTruth);
277 EXTERN int StageLogGetActive(StageLog, int, PetscTruth *);
278 EXTERN int StageLogSetVisible(StageLog, int, PetscTruth);
279 EXTERN int StageLogGetVisible(StageLog, int, PetscTruth *);
280 EXTERN int StageLogGetStage(StageLog, const char [], int *);
281 
282 /*
283      This does not work for MPI-Uni because our include/mpiuni/mpi.h file
284    uses macros to defined the MPI operations.
285 
286      It does not work correctly from HP-UX because it processes the
287    macros in a way that sometimes it double counts, hence
288    PETSC_HAVE_BROKEN_RECURSIVE_MACRO
289 
290      It does not work with Windows because winmpich lacks MPI_Type_size()
291 */
292 #if !defined(_petsc_mpi_uni) && !defined(PETSC_HAVE_BROKEN_RECURSIVE_MACRO) && !defined (PETSC_HAVE_MPI_MISSING_TYPESIZE)
293 /*
294    Logging of MPI activities
295 */
296 
297 #define TypeSize(buff,count,type) \
298 (\
299   MPI_Type_size(type,&PETSC_DUMMY_SIZE),buff += ((PetscLogDouble) ((count)*PETSC_DUMMY_SIZE))\
300 )
301 
302 #define MPI_Irecv(buf,count, datatype,source,tag,comm,request) \
303 (\
304   PETSC_DUMMY = MPI_Irecv(buf,count, datatype,source,tag,comm,request),\
305   irecv_ct++,TypeSize(irecv_len,count,datatype),PETSC_DUMMY\
306 )
307 
308 #define MPI_Isend(buf,count, datatype,dest,tag,comm,request) \
309 (\
310   PETSC_DUMMY = MPI_Isend(buf,count, datatype,dest,tag,comm,request),\
311   isend_ct++,  TypeSize(isend_len,count,datatype),PETSC_DUMMY\
312 )
313 
314 #define MPI_Startall_irecv(count,number,requests) \
315 (\
316   PETSC_DUMMY = MPI_Startall(number,requests),\
317   irecv_ct += (PetscLogDouble)(number),irecv_len += ((PetscLogDouble) ((count)*sizeof(PetscScalar))),PETSC_DUMMY\
318 )
319 
320 #define MPI_Startall_isend(count,number,requests) \
321 (\
322   PETSC_DUMMY = MPI_Startall(number,requests),\
323   isend_ct += (PetscLogDouble)(number),isend_len += ((PetscLogDouble) ((count)*sizeof(PetscScalar))),PETSC_DUMMY\
324 )
325 
326 #define MPI_Start_isend(count, requests) \
327 (\
328   PETSC_DUMMY = MPI_Start(requests),\
329   isend_ct++,isend_len += ((PetscLogDouble) ((count)*sizeof(PetscScalar))),PETSC_DUMMY\
330 )
331 
332 #define MPI_Recv(buf,count, datatype,source,tag,comm,status) \
333 (\
334   PETSC_DUMMY = MPI_Recv(buf,count, datatype,source,tag,comm,status),\
335   recv_ct++,TypeSize(recv_len,count,datatype),PETSC_DUMMY\
336 )
337 
338 #define MPI_Send(buf,count, datatype,dest,tag,comm) \
339 (\
340   PETSC_DUMMY = MPI_Send(buf,count, datatype,dest,tag,comm),\
341   send_ct++, TypeSize(send_len,count,datatype),PETSC_DUMMY\
342 )
343 
344 #define MPI_Wait(request,status) \
345 (\
346   wait_ct++,sum_of_waits_ct++,\
347   MPI_Wait(request,status)\
348 )
349 
350 #define MPI_Waitany(a,b,c,d) \
351 (\
352   wait_any_ct++,sum_of_waits_ct++,\
353   MPI_Waitany(a,b,c,d)\
354 )
355 
356 #define MPI_Waitall(count,array_of_requests,array_of_statuses) \
357 (\
358   wait_all_ct++,sum_of_waits_ct += (PetscLogDouble) (count),\
359   MPI_Waitall(count,array_of_requests,array_of_statuses)\
360 )
361 
362 #define MPI_Allreduce(sendbuf, recvbuf,count,datatype,op,comm) \
363 (\
364   allreduce_ct++,MPI_Allreduce(sendbuf,recvbuf,count,datatype,op,comm)\
365 )
366 
367 #else
368 
369 #define MPI_Startall_irecv(count,number,requests) \
370 (\
371   MPI_Startall(number,requests)\
372 )
373 
374 #define MPI_Startall_isend(count,number,requests) \
375 (\
376   MPI_Startall(number,requests)\
377 )
378 
379 #define MPI_Start_isend(count, requests) \
380 (\
381   MPI_Start(requests)\
382 )
383 
384 #endif /* !_petsc_mpi_uni && ! PETSC_HAVE_BROKEN_RECURSIVE_MACRO */
385 
386 #else  /* ---Logging is turned off --------------------------------------------*/
387 
388 #define PetscLogFlops(n) 0
389 
390 /*
391      With logging turned off, then MPE has to be turned off
392 */
393 #define PetscLogMPEBegin()         0
394 #define PetscLogMPEDump(a)         0
395 
396 #define PetscLogEventActivate(a)   0
397 #define PetscLogEventDeactivate(a) 0
398 
399 #define PetscLogEventActivateClass(a)   0
400 #define PetscLogEventDeactivateClass(a) 0
401 
402 #define _PetscLogPLB                        0
403 #define _PetscLogPLE                        0
404 #define _PetscLogPHC                        0
405 #define _PetscLogPHD                        0
406 #define PetscGetFlops(a)                (*(a) = 0.0,0)
407 #define PetscLogEventBegin(e,o1,o2,o3,o4)   0
408 #define PetscLogEventEnd(e,o1,o2,o3,o4)     0
409 #define PetscLogEventBarrierBegin(e,o1,o2,o3,o4,cm) 0
410 #define PetscLogEventBarrierEnd(e,o1,o2,o3,o4,cm)   0
411 #define PetscLogObjectParent(p,c)
412 #define PetscLogObjectParents(p,n,c)
413 #define PetscLogObjectCreate(h)
414 #define PetscLogObjectDestroy(h)
415 #define PetscLogObjectMemory(p,m)
416 #define PetscLogDestroy()                   0
417 #define PetscLogStagePush(a)                0
418 #define PetscLogStagePop()                  0
419 #define PetscLogStageRegister(a,b)          0
420 #define PetscLogStagePrint(a,flg)           0
421 #define PetscLogPrintSummary(comm,file)     0
422 #define PetscLogBegin()                     0
423 #define PetscLogTraceBegin(file)            0
424 #define PetscLogSet(lb,le)                  0
425 #define PetscLogAllBegin()                  0
426 #define PetscLogDump(c)                     0
427 #define PetscLogEventRegister(a,b,c)        0
428 #define PetscLogObjects(a)                  0
429 #define PetscLogActions(a)                  0
430 EXTERN int PetscLogObjectState(PetscObject,const char[],...) PETSC_PRINTF_FORMAT_CHECK(2,3);
431 
432 /* If PETSC_USE_LOG is NOT defined, these still need to be! */
433 #define MPI_Startall_irecv(count,number,requests) MPI_Startall(number,requests)
434 #define MPI_Startall_isend(count,number,requests) MPI_Startall(number,requests)
435 #define MPI_Start_isend(count,requests) MPI_Start(requests)
436 
437 /* Creation and destruction functions */
438 #define StageLogCreate(stageLog)                     0
439 #define StageLogDestroy(stageLog)                    0
440 /* Registration functions */
441 #define StageLogRegister(stageLog, name, stage)      0
442 /* Runtime functions */
443 #define PetscLogGetStageLog(stageLog)                0
444 #define StageLogPush(stageLog, stage)                0
445 #define StageLogPop(stageLog)                        0
446 #define StageLogGetCurrent(stageLog, stage)          0
447 #define StageLogSetActive(stageLog, stage, active)   0
448 #define StageLogGetActive(stageLog, stage, active)   0
449 #define StageLogSetVisible(stageLog, stage, visible) 0
450 #define StageLogGetVisible(stageLog, stage, visible) 0
451 #define StageLogGetStage(stageLog, name, stage)      0
452 
453 #endif   /* PETSC_USE_LOG */
454 
455 extern PetscTruth PetscPreLoadingUsed;       /* true if we are or have done preloading */
456 extern PetscTruth PetscPreLoadingOn;         /* true if we are currently in a preloading calculation */
457 
458 #define PreLoadBegin(flag,name) \
459 {\
460   PetscTruth PreLoading = flag;\
461   int        PreLoadMax,PreLoadIt,_stageNum,_3_ierr;\
462   _3_ierr = PetscOptionsGetLogical(PETSC_NULL,"-preload",&PreLoading,PETSC_NULL);CHKERRQ(_3_ierr);\
463   PreLoadMax = (int)(PreLoading);\
464   PetscPreLoadingUsed = PreLoading ? PETSC_TRUE : PetscPreLoadingUsed;\
465   for (PreLoadIt=0; PreLoadIt<=PreLoadMax; PreLoadIt++) {\
466     PetscPreLoadingOn = PreLoading;\
467     _3_ierr = PetscBarrier(PETSC_NULL);CHKERRQ(_3_ierr);\
468     if (PreLoadIt>0) {\
469       _3_ierr = PetscLogStageGetId(name,&_stageNum);CHKERRQ(_3_ierr);\
470     } else {\
471       _3_ierr = PetscLogStageRegister(&_stageNum,name);CHKERRQ(_3_ierr);\
472     }\
473     _3_ierr = PetscLogStageSetActive(_stageNum,(PetscTruth)(!PreLoadMax || PreLoadIt));\
474     _3_ierr = PetscLogStagePush(_stageNum);CHKERRQ(_3_ierr);
475 
476 #define PreLoadEnd() \
477     _3_ierr = PetscLogStagePop();CHKERRQ(_3_ierr);\
478     PreLoading = PETSC_FALSE;\
479   }\
480 }
481 
482 #define PreLoadStage(name) \
483   _3_ierr = PetscLogStagePop();CHKERRQ(_3_ierr);\
484   if (PreLoadIt>0) {\
485     _3_ierr = PetscLogStageGetId(name,&_stageNum);CHKERRQ(_3_ierr);\
486   } else {\
487     _3_ierr = PetscLogStageRegister(&_stageNum,name);CHKERRQ(_3_ierr);\
488   }\
489   _3_ierr = PetscLogStageSetActive(_stageNum,(PetscTruth)(!PreLoadMax || PreLoadIt));\
490   _3_ierr = PetscLogStagePush(_stageNum);CHKERRQ(_3_ierr);
491 
492 PETSC_EXTERN_CXX_END
493 #endif
494