Actual source code: classLog.c

  1: #define PETSC_DLL

 3:  #include petsc.h
 4:  #include petsctime.h
 5:  #include plog.h

  7: /*----------------------------------------------- Creation Functions -------------------------------------------------*/
  8: /* Note: these functions do not have prototypes in a public directory, so they are considered "internal" and not exported. */
 11: /*@C
 12:   ClassRegLogCreate - This creates a ClassRegLog object.

 14:   Not collective

 16:   Input Parameter:
 17: . classLog - The ClassRegLog

 19:   Level: beginner

 21: .keywords: log, class, create
 22: .seealso: ClassRegLogDestroy(), StageLogCreate()
 23: @*/
 24: PetscErrorCode ClassRegLogCreate(ClassRegLog *classLog)
 25: {
 26:   ClassRegLog l;

 30:   PetscNew(struct _n_ClassRegLog, &l);
 31:   l->numClasses = 0;
 32:   l->maxClasses = 100;
 33:   PetscMalloc(l->maxClasses * sizeof(ClassRegInfo), &l->classInfo);
 34:   *classLog = l;
 35:   return(0);
 36: }

 40: /*@C
 41:   ClassRegLogDestroy - This destroys a ClassRegLog object.

 43:   Not collective

 45:   Input Paramter:
 46: . classLog - The ClassRegLog

 48:   Level: beginner

 50: .keywords: log, event, destroy
 51: .seealso: ClassRegLogCreate()
 52: @*/
 53: PetscErrorCode ClassRegLogDestroy(ClassRegLog classLog)\
 54: {
 55:   int c;

 59:   for(c = 0; c < classLog->numClasses; c++) {
 60:     ClassRegInfoDestroy(&classLog->classInfo[c]);
 61:   }
 62:   PetscFree(classLog->classInfo);
 63:   PetscFree(classLog);
 64:   return(0);
 65: }

 69: /*@C
 70:   ClassRegInfoDestroy - This destroys a ClassRegInfo object.

 72:   Not collective

 74:   Input Parameter:
 75: . c - The ClassRegInfo

 77:   Level: beginner

 79: .keywords: log, class, destroy
 80: .seealso: StageLogDestroy(), EventLogDestroy()
 81: @*/
 82: PetscErrorCode ClassRegInfoDestroy(ClassRegInfo *c)
 83: {

 87:   PetscFree(c->name);
 88:   return(0);
 89: }

 93: /*@C
 94:   ClassPerfLogCreate - This creates a ClassPerfLog object.

 96:   Not collective

 98:   Input Parameter:
 99: . classLog - The ClassPerfLog

101:   Level: beginner

103: .keywords: log, class, create
104: .seealso: ClassPerfLogDestroy(), StageLogCreate()
105: @*/
106: PetscErrorCode ClassPerfLogCreate(ClassPerfLog *classLog)
107: {
108:   ClassPerfLog l;

112:   PetscNew(struct _n_ClassPerfLog, &l);
113:   l->numClasses = 0;
114:   l->maxClasses = 100;
115:   PetscMalloc(l->maxClasses * sizeof(ClassPerfInfo), &l->classInfo);
116:   *classLog = l;
117:   return(0);
118: }

122: /*@C
123:   ClassPerfLogDestroy - This destroys a ClassPerfLog object.

125:   Not collective

127:   Input Paramter:
128: . classLog - The ClassPerfLog

130:   Level: beginner

132: .keywords: log, event, destroy
133: .seealso: ClassPerfLogCreate()
134: @*/
135: PetscErrorCode ClassPerfLogDestroy(ClassPerfLog classLog)
136: {

140:   PetscFree(classLog->classInfo);
141:   PetscFree(classLog);
142:   return(0);
143: }

145: /*------------------------------------------------ General Functions -------------------------------------------------*/
148: /*@C
149:   ClassPerfInfoClear - This clears a ClassPerfInfo object.

151:   Not collective

153:   Input Paramter:
154: . classInfo - The ClassPerfInfo

156:   Level: beginner

158: .keywords: log, class, destroy
159: .seealso: ClassPerfLogCreate()
160: @*/
161: PetscErrorCode ClassPerfInfoClear(ClassPerfInfo *classInfo)
162: {
164:   classInfo->id           = -1;
165:   classInfo->creations    = 0;
166:   classInfo->destructions = 0;
167:   classInfo->mem          = 0.0;
168:   classInfo->descMem      = 0.0;
169:   return(0);
170: }

174: /*@C
175:   ClassPerfLogEnsureSize - This ensures that a ClassPerfLog is at least of a certain size.

177:   Not collective

179:   Input Paramters:
180: + classLog - The ClassPerfLog
181: - size     - The size

183:   Level: intermediate

185: .keywords: log, class, size, ensure
186: .seealso: ClassPerfLogCreate()
187: @*/
188: PetscErrorCode ClassPerfLogEnsureSize(ClassPerfLog classLog, int size)
189: {
190:   ClassPerfInfo *classInfo;

194:   while(size > classLog->maxClasses) {
195:     PetscMalloc(classLog->maxClasses*2 * sizeof(ClassPerfInfo), &classInfo);
196:     PetscMemcpy(classInfo, classLog->classInfo, classLog->maxClasses * sizeof(ClassPerfInfo));
197:     PetscFree(classLog->classInfo);
198:     classLog->classInfo   = classInfo;
199:     classLog->maxClasses *= 2;
200:   }
201:   while(classLog->numClasses < size) {
202:     ClassPerfInfoClear(&classLog->classInfo[classLog->numClasses++]);
203:   }
204:   return(0);
205: }

207: /*--------------------------------------------- Registration Functions ----------------------------------------------*/
210: /*@C
211:   ClassRegLogRegister - Registers a class for logging operations in an application code.
212:   A prefered cookie is given on input, and the actual cookie is returned on output. If
213:   the user has no preference, PETSC_DECIDE will cause the cookie to be automatically
214:   assigned, and unique in this ClassLog.

216:   Not Collective

218:   Input Parameters:
219: + classLog - The ClassLog
220: . cname    - The name associated with the class
221: - cookie   - The prefered cookie (or PETSC_DECIDE), and the actual cookie on output

223:   Level: developer

225: .keywords: log, class, register
226: .seealso: PetscLogClassRegister()
227: @*/
228: PetscErrorCode ClassRegLogRegister(ClassRegLog classLog, const char cname[], PetscCookie *cookie)
229: {
230:   ClassRegInfo *classInfo;
231:   char         *str;
232:   int           c;

238:   c = classLog->numClasses++;
239:   if (classLog->numClasses > classLog->maxClasses) {
240:     PetscMalloc(classLog->maxClasses*2 * sizeof(ClassRegInfo), &classInfo);
241:     PetscMemcpy(classInfo, classLog->classInfo, classLog->maxClasses * sizeof(ClassRegInfo));
242:     PetscFree(classLog->classInfo);
243:     classLog->classInfo   = classInfo;
244:     classLog->maxClasses *= 2;
245:   }
246:   PetscStrallocpy(cname, &str);
247:   classLog->classInfo[c].name     = str;
248:   PetscCookieRegister(cookie);
249:   classLog->classInfo[c].cookie = *cookie;
250:   return(0);
251: }

253: /*------------------------------------------------ Query Functions --------------------------------------------------*/
256: /*@C
257:   ClassRegLogGetClass - This function returns the class corresponding to a given cookie.

259:   Not Collective

261:   Input Parameters:
262: + classLog - The ClassRegLog
263: - cookie   - The cookie
264:             
265:   Output Parameter:
266: . oclass   - The class id

268:   Level: developer

270: .keywords: log, class, register
271: .seealso: PetscLogClassRegister(), PetscLogObjCreateDefault(), PetscLogObjDestroyDefault()
272: @*/
273: PetscErrorCode ClassRegLogGetClass(ClassRegLog classLog, PetscCookie cookie, int *oclass)
274: {
275:   int c;

279:   for(c = 0; c < classLog->numClasses; c++) {
280:     /* Could do bisection here */
281:     if (classLog->classInfo[c].cookie == cookie) break;
282:   }
283:   if (c >= classLog->numClasses) {
284:     SETERRQ1(PETSC_ERR_ARG_WRONG, "Invalid object cookie %d\nThis often happens if you compile with PETSC_USE_DYNAMIC_LIBRARIES, but link with static libraries.", cookie);
285:   }
286:   *oclass = c;
287:   return(0);
288: }

290: /*----------------------------------------------- Logging Functions -------------------------------------------------*/
291: /* Default object create logger */
294: PetscErrorCode PetscLogObjCreateDefault(PetscObject obj)
295: {
296:   StageLog       stageLog;
297:   ClassRegLog    classRegLog;
298:   ClassPerfLog   classPerfLog;
299:   Action        *tmpAction;
300:   Object        *tmpObjects;
301:   PetscLogDouble start, end;
302:   int            oclass;
303:   int            stage;

307:   /* Record stage info */
308:   PetscLogGetStageLog(&stageLog);
309:   StageLogGetCurrent(stageLog, &stage);
310:   StageLogGetClassRegLog(stageLog, &classRegLog);
311:   StageLogGetClassPerfLog(stageLog, stage, &classPerfLog);
312:   ClassRegLogGetClass(classRegLog, obj->cookie, &oclass);
313:   classPerfLog->classInfo[oclass].creations++;
314:   /* Dynamically enlarge logging structures */
315:   if (numActions >= maxActions) {
316:     PetscTime(start);
317:     PetscMalloc(maxActions*2 * sizeof(Action), &tmpAction);
318:     PetscMemcpy(tmpAction, actions, maxActions * sizeof(Action));
319:     PetscFree(actions);
320:     actions     = tmpAction;
321:     maxActions *= 2;
322:     PetscTime(end);
323:     BaseTime += (end - start);
324:   }

326:   numObjects = obj->id;
327:   /* Record the creation action */
328:   if (logActions) {
329:     PetscTime(actions[numActions].time);
330:     actions[numActions].time  -= BaseTime;
331:     actions[numActions].action = CREATE;
332:     actions[numActions].cookie = obj->cookie;
333:     actions[numActions].id1    = numObjects;
334:     actions[numActions].id2    = -1;
335:     actions[numActions].id3    = -1;
336:     actions[numActions].flops  = _TotalFlops;
337:     PetscMallocGetCurrentUsage(&actions[numActions].mem);
338:     PetscMallocGetMaximumUsage(&actions[numActions].maxmem);
339:     numActions++;
340:   }
341:   /* Record the object */
342:   if (logObjects) {
343:     objects[numObjects].parent = -1;
344:     objects[numObjects].obj    = obj;
345:     PetscMemzero(objects[numObjects].name, 64 * sizeof(char));
346:     PetscMemzero(objects[numObjects].info, 64 * sizeof(char));

348:   /* Dynamically enlarge logging structures */
349:     if (numObjects >= maxObjects) {
350:       PetscTime(start);
351:       PetscMalloc(maxObjects*2 * sizeof(Object), &tmpObjects);
352:       PetscMemcpy(tmpObjects, objects, maxObjects * sizeof(Object));
353:       PetscFree(objects);
354:       objects     = tmpObjects;
355:       maxObjects *= 2;
356:       PetscTime(end);
357:       BaseTime += (end - start);
358:     }
359:   }
360:   return(0);
361: }

363: /* Default object destroy logger */
366: PetscErrorCode PetscLogObjDestroyDefault(PetscObject obj)
367: {
368:   StageLog       stageLog;
369:   ClassRegLog    classRegLog;
370:   ClassPerfLog   classPerfLog;
371:   Action        *tmpAction;
372:   PetscLogDouble start, end;
373:   int            oclass;
374:   int            stage;

378:   /* Record stage info */
379:   PetscLogGetStageLog(&stageLog);
380:   StageLogGetCurrent(stageLog, &stage);
381:   if (stage != -1) {
382:     /* That can happen if the log summary is output before some things are destroyed */
383:     StageLogGetClassRegLog(stageLog, &classRegLog);
384:     StageLogGetClassPerfLog(stageLog, stage, &classPerfLog);
385:     ClassRegLogGetClass(classRegLog, obj->cookie, &oclass);
386:     classPerfLog->classInfo[oclass].destructions++;
387:     classPerfLog->classInfo[oclass].mem += obj->mem;
388:   }
389:   /* Cannot Credit all ancestors with your memory because they may have already been destroyed*/
390:   numObjectsDestroyed++;
391:   /* Dynamically enlarge logging structures */
392:   if (numActions >= maxActions) {
393:     PetscTime(start);
394:     PetscMalloc(maxActions*2 * sizeof(Action), &tmpAction);
395:     PetscMemcpy(tmpAction, actions, maxActions * sizeof(Action));
396:     PetscFree(actions);
397:     actions     = tmpAction;
398:     maxActions *= 2;
399:     PetscTime(end);
400:     BaseTime += (end - start);
401:   }
402:   /* Record the destruction action */
403:   if (logActions) {
404:     PetscTime(actions[numActions].time);
405:     actions[numActions].time  -= BaseTime;
406:     actions[numActions].action = DESTROY;
407:     actions[numActions].cookie = obj->cookie;
408:     actions[numActions].id1    = obj->id;
409:     actions[numActions].id2    = -1;
410:     actions[numActions].id3    = -1;
411:     actions[numActions].flops  = _TotalFlops;
412:     PetscMallocGetCurrentUsage(&actions[numActions].mem);
413:     PetscMallocGetMaximumUsage(&actions[numActions].maxmem);
414:     numActions++;
415:   }
416:   if (logObjects) {
417:     if (obj->name) {
418:       PetscStrncpy(objects[obj->id].name, obj->name, 64);
419:     }
420:     objects[obj->id].obj      = PETSC_NULL;
421:     objects[obj->id].mem      = obj->mem;
422:   }
423:   return(0);
424: }