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