Actual source code: classLog.c
1: #define PETSC_DLL
3: #include petsc.h
4: #include petsctime.h
5: #include ../src/sys/plog/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.
213: Not Collective
215: Input Parameters:
216: + classLog - The ClassLog
217: - cname - The name associated with the class
219: Output Parameter:
220: . cookie - The cookie
222: Level: developer
224: .keywords: log, class, register
225: .seealso: PetscCookieRegister()
226: @*/
227: PetscErrorCode ClassRegLogRegister(ClassRegLog classLog, const char cname[], PetscCookie cookie)
228: {
229: ClassRegInfo *classInfo;
230: char *str;
231: int c;
236: c = classLog->numClasses++;
237: if (classLog->numClasses > classLog->maxClasses) {
238: PetscMalloc(classLog->maxClasses*2 * sizeof(ClassRegInfo), &classInfo);
239: PetscMemcpy(classInfo, classLog->classInfo, classLog->maxClasses * sizeof(ClassRegInfo));
240: PetscFree(classLog->classInfo);
241: classLog->classInfo = classInfo;
242: classLog->maxClasses *= 2;
243: }
244: PetscStrallocpy(cname, &str);
245: classLog->classInfo[c].name = str;
246: classLog->classInfo[c].cookie = cookie;
247: return(0);
248: }
250: /*------------------------------------------------ Query Functions --------------------------------------------------*/
253: /*@C
254: ClassRegLogGetClass - This function returns the class corresponding to a given cookie.
256: Not Collective
258: Input Parameters:
259: + classLog - The ClassRegLog
260: - cookie - The cookie
261:
262: Output Parameter:
263: . oclass - The class id
265: Level: developer
267: .keywords: log, class, register
268: .seealso: PetscCookieRegister(), PetscLogObjCreateDefault(), PetscLogObjDestroyDefault()
269: @*/
270: PetscErrorCode ClassRegLogGetClass(ClassRegLog classLog, PetscCookie cookie, int *oclass)
271: {
272: int c;
276: for(c = 0; c < classLog->numClasses; c++) {
277: /* Could do bisection here */
278: if (classLog->classInfo[c].cookie == cookie) break;
279: }
280: if (c >= classLog->numClasses) {
281: 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);
282: }
283: *oclass = c;
284: return(0);
285: }
287: /*----------------------------------------------- Logging Functions -------------------------------------------------*/
288: /* Default object create logger */
291: PetscErrorCode PetscLogObjCreateDefault(PetscObject obj)
292: {
293: StageLog stageLog;
294: ClassRegLog classRegLog;
295: ClassPerfLog classPerfLog;
296: Action *tmpAction;
297: Object *tmpObjects;
298: PetscLogDouble start, end;
299: int oclass;
300: int stage;
304: /* Record stage info */
305: PetscLogGetStageLog(&stageLog);
306: StageLogGetCurrent(stageLog, &stage);
307: StageLogGetClassRegLog(stageLog, &classRegLog);
308: StageLogGetClassPerfLog(stageLog, stage, &classPerfLog);
309: ClassRegLogGetClass(classRegLog, obj->cookie, &oclass);
310: classPerfLog->classInfo[oclass].creations++;
311: /* Dynamically enlarge logging structures */
312: if (numActions >= maxActions) {
313: PetscTime(start);
314: PetscMalloc(maxActions*2 * sizeof(Action), &tmpAction);
315: PetscMemcpy(tmpAction, actions, maxActions * sizeof(Action));
316: PetscFree(actions);
317: actions = tmpAction;
318: maxActions *= 2;
319: PetscTime(end);
320: BaseTime += (end - start);
321: }
323: numObjects = obj->id;
324: /* Record the creation action */
325: if (logActions) {
326: PetscTime(actions[numActions].time);
327: actions[numActions].time -= BaseTime;
328: actions[numActions].action = CREATE;
329: actions[numActions].cookie = obj->cookie;
330: actions[numActions].id1 = numObjects;
331: actions[numActions].id2 = -1;
332: actions[numActions].id3 = -1;
333: actions[numActions].flops = _TotalFlops;
334: PetscMallocGetCurrentUsage(&actions[numActions].mem);
335: PetscMallocGetMaximumUsage(&actions[numActions].maxmem);
336: numActions++;
337: }
338: /* Record the object */
339: if (logObjects) {
340: objects[numObjects].parent = -1;
341: objects[numObjects].obj = obj;
342: PetscMemzero(objects[numObjects].name, 64 * sizeof(char));
343: PetscMemzero(objects[numObjects].info, 64 * sizeof(char));
345: /* Dynamically enlarge logging structures */
346: if (numObjects >= maxObjects) {
347: PetscTime(start);
348: PetscMalloc(maxObjects*2 * sizeof(Object), &tmpObjects);
349: PetscMemcpy(tmpObjects, objects, maxObjects * sizeof(Object));
350: PetscFree(objects);
351: objects = tmpObjects;
352: maxObjects *= 2;
353: PetscTime(end);
354: BaseTime += (end - start);
355: }
356: }
357: return(0);
358: }
360: /* Default object destroy logger */
363: PetscErrorCode PetscLogObjDestroyDefault(PetscObject obj)
364: {
365: StageLog stageLog;
366: ClassRegLog classRegLog;
367: ClassPerfLog classPerfLog;
368: Action *tmpAction;
369: PetscLogDouble start, end;
370: int oclass;
371: int stage;
375: /* Record stage info */
376: PetscLogGetStageLog(&stageLog);
377: StageLogGetCurrent(stageLog, &stage);
378: if (stage != -1) {
379: /* That can happen if the log summary is output before some things are destroyed */
380: StageLogGetClassRegLog(stageLog, &classRegLog);
381: StageLogGetClassPerfLog(stageLog, stage, &classPerfLog);
382: ClassRegLogGetClass(classRegLog, obj->cookie, &oclass);
383: classPerfLog->classInfo[oclass].destructions++;
384: classPerfLog->classInfo[oclass].mem += obj->mem;
385: }
386: /* Cannot Credit all ancestors with your memory because they may have already been destroyed*/
387: numObjectsDestroyed++;
388: /* Dynamically enlarge logging structures */
389: if (numActions >= maxActions) {
390: PetscTime(start);
391: PetscMalloc(maxActions*2 * sizeof(Action), &tmpAction);
392: PetscMemcpy(tmpAction, actions, maxActions * sizeof(Action));
393: PetscFree(actions);
394: actions = tmpAction;
395: maxActions *= 2;
396: PetscTime(end);
397: BaseTime += (end - start);
398: }
399: /* Record the destruction action */
400: if (logActions) {
401: PetscTime(actions[numActions].time);
402: actions[numActions].time -= BaseTime;
403: actions[numActions].action = DESTROY;
404: actions[numActions].cookie = obj->cookie;
405: actions[numActions].id1 = obj->id;
406: actions[numActions].id2 = -1;
407: actions[numActions].id3 = -1;
408: actions[numActions].flops = _TotalFlops;
409: PetscMallocGetCurrentUsage(&actions[numActions].mem);
410: PetscMallocGetMaximumUsage(&actions[numActions].maxmem);
411: numActions++;
412: }
413: if (logObjects) {
414: if (obj->name) {
415: PetscStrncpy(objects[obj->id].name, obj->name, 64);
416: }
417: objects[obj->id].obj = PETSC_NULL;
418: objects[obj->id].mem = obj->mem;
419: }
420: return(0);
421: }