Actual source code: pstack.c

petsc-3.4.5 2014-06-29
  2: #include <petscsys.h>        /*I  "petscsys.h"   I*/

  4: #if defined(PETSC_USE_DEBUG)

  6: #if defined(PETSC_HAVE_PTHREADCLASSES)
  7: #if defined(PETSC_PTHREAD_LOCAL)
  8: PETSC_PTHREAD_LOCAL PetscStack *petscstack = 0;
  9: #else
 10: PetscThreadKey petscstack;
 11: #endif
 12: #else
 13: PetscStack *petscstack = 0;
 14: #endif


 17: #if defined(PETSC_HAVE_AMS)
 18: #include <petscviewerams.h>

 20: static AMS_Memory amsmemstack = -1;

 24: /*@C
 25:    PetscStackAMSGrantAccess - Grants access of the PETSc stack frames to the AMS publisher

 27:    Collective on PETSC_COMM_WORLD?

 29:    Level: developer

 31:    Concepts: publishing object


 35: .seealso: PetscObjectSetName(), PetscObjectAMSViewOff(), PetscObjectAMSTakeAccess()

 37: @*/
 38: void  PetscStackAMSGrantAccess(void)
 39: {
 40:   if (amsmemstack != -1) {
 41:     AMS_Memory_grant_access(amsmemstack);
 42:   }
 43: }

 47: /*@C
 48:    PetscStackAMSTakeAccess - Takes access of the PETSc stack frames to the AMS publisher

 50:    Collective on PETSC_COMM_WORLD?

 52:    Level: developer

 54:    Concepts: publishing object


 58: .seealso: PetscObjectSetName(), PetscObjectAMSViewOff(), PetscObjectAMSTakeAccess()

 60: @*/
 61: void  PetscStackAMSTakeAccess(void)
 62: {
 63:   if (amsmemstack != -1) {
 64:     AMS_Memory_take_access(amsmemstack);
 65:   }
 66: }

 68: PetscErrorCode PetscStackViewAMS(void)
 69: {
 70:   AMS_Comm       acomm;
 72:   AMS_Memory     mem;
 73:   PetscStack*    petscstackp;

 75:   petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);
 76:   PetscViewerAMSGetAMSComm(PETSC_VIEWER_AMS_WORLD,&acomm);
 77:   PetscStackCallAMS(AMS_Memory_create,(acomm,"Stack",&mem));
 78:   PetscStackCallAMS(AMS_Memory_take_access,(mem));
 79:   PetscStackCallAMS(AMS_Memory_add_field,(mem,"functions",petscstackp->function,10,AMS_STRING,AMS_READ,AMS_COMMON,AMS_REDUCT_UNDEF));
 80:   PetscStackCallAMS(AMS_Memory_add_field,(mem,"current size",&petscstackp->currentsize,1,AMS_INT,AMS_READ,AMS_COMMON,AMS_REDUCT_UNDEF));
 81:   PetscStackCallAMS(AMS_Memory_publish,(mem));
 82:   PetscStackCallAMS(AMS_Memory_grant_access,(mem));
 83:   amsmemstack = mem;
 84:   return 0;
 85: }

 89: PetscErrorCode PetscStackAMSViewOff(void)
 90: {

 94:   if (amsmemstack == -1) return(0);
 95:   AMS_Memory_destroy(amsmemstack);
 96:   amsmemstack = -1;
 97:   return(0);
 98: }

100: #endif

102: PetscErrorCode PetscStackCreate(void)
103: {
104:   PetscStack *petscstack_in;
105:   if (PetscStackActive()) return 0;

107:   petscstack_in              = (PetscStack*)malloc(sizeof(PetscStack));
108:   petscstack_in->currentsize = 0;
109:   PetscThreadLocalSetValue((PetscThreadKey*)&petscstack,petscstack_in);

111: #if defined(PETSC_HAVE_AMS)
112:   {
113:   PetscBool flg = PETSC_FALSE;
114:   PetscOptionsHasName(NULL,"-stack_view",&flg);
115:   if (flg) PetscStackViewAMS();
116:   }
117: #endif
118:   return 0;
119: }


124: PetscErrorCode  PetscStackView(FILE *file)
125: {
126:   int        i;
127:   PetscStack *petscstackp;

129:   petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);
130:   if (!file) file = PETSC_STDOUT;

132:   if (file == PETSC_STDOUT) {
133:     (*PetscErrorPrintf)("Note: The EXACT line numbers in the stack are not available,\n");
134:     (*PetscErrorPrintf)("      INSTEAD the line number of the start of the function\n");
135:     (*PetscErrorPrintf)("      is given.\n");
136:     for (i=petscstackp->currentsize-1; i>=0; i--) (*PetscErrorPrintf)("[%d] %s line %d %s%s\n",PetscGlobalRank,petscstackp->function[i],petscstackp->line[i],petscstackp->directory[i],petscstackp->file[i]);
137:   } else {
138:     fprintf(file,"Note: The EXACT line numbers in the stack are not available,\n");
139:     fprintf(file,"      INSTEAD the line number of the start of the function\n");
140:     fprintf(file,"      is given.\n");
141:     for (i=petscstackp->currentsize-1; i>=0; i--) fprintf(file,"[%d] %s line %d %s%s\n",PetscGlobalRank,petscstackp->function[i],petscstackp->line[i],petscstackp->directory[i],petscstackp->file[i]);
142:   }
143:   return 0;
144: }

146: PetscErrorCode PetscStackDestroy(void)
147: {
148:   if (PetscStackActive()) {
149:     PetscStack *petscstack_in;
150:     petscstack_in = (PetscStack*)PetscThreadLocalGetValue(petscstack);
151:     free(petscstack_in);
152:     PetscThreadLocalSetValue((PetscThreadKey*)&petscstack,NULL);
153:   }
154:   return 0;
155: }

160: PetscErrorCode  PetscStackCopy(PetscStack *sint,PetscStack *sout)
161: {
162:   int i;

164:   if (!sint) sout->currentsize = 0;
165:   else {
166:     for (i=0; i<sint->currentsize; i++) {
167:       sout->function[i]     = sint->function[i];
168:       sout->file[i]         = sint->file[i];
169:       sout->directory[i]    = sint->directory[i];
170:       sout->line[i]         = sint->line[i];
171:       sout->petscroutine[i] = sint->petscroutine[i];
172:     }
173:     sout->currentsize = sint->currentsize;
174:   }
175:   return 0;
176: }

181: PetscErrorCode  PetscStackPrint(PetscStack *sint,FILE *fp)
182: {
183:   int i;

185:   if (!sint) return(0);
186:   for (i=sint->currentsize-2; i>=0; i--) fprintf(fp,"      [%d]  %s() line %d in %s%s\n",PetscGlobalRank,sint->function[i],sint->line[i],sint->directory[i],sint->file[i]);
187:   return 0;
188: }

190: #else

192: #if defined(PETSC_HAVE_PTHREADCLASSES)
193: #if defined(PETSC_PTHREAD_LOCAL)
194: PETSC_PTHREAD_LOCAL void *petscstack = 0;
195: #else
196: PetscThreadKey petscstack;
197: #endif
198: #else
199: void *petscstack = 0;
200: #endif

204: PetscErrorCode  PetscStackCreate(void)
205: {
207:   return(0);
208: }
211: PetscErrorCode  PetscStackView(PETSC_UNUSED FILE *file)
212: {
214:   return(0);
215: }
218: PetscErrorCode  PetscStackDestroy(void)
219: {
221:   return(0);
222: }
225: PetscErrorCode  PetscStackCopy(PETSC_UNUSED void *sint,PETSC_UNUSED void *sout)
226: {
228:   return(0);
229: }
232: PetscErrorCode  PetscStackPrint(PETSC_UNUSED void *sint,PETSC_UNUSED FILE *fp)
233: {
235:   return(0);
236: }

238: #if defined(PETSC_HAVE_AMS)     /* AMS stack functions do nothing in optimized mode */
239: void PetscStackAMSGrantAccess(void) {}
240: void PetscStackAMSTakeAccess(void) {}

242: PetscErrorCode PetscStackViewAMS(void)
243: {
244:   return 0;
245: }

249: PetscErrorCode  PetscStackAMSViewOff(void)
250: {
252:   return(0);
253: }
254: #endif

256: #endif