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