Actual source code: pstack.c

petsc-dev 2014-02-02
Report Typos and Errors
  2: #include <petscsys.h>        /*I  "petscsys.h"   I*/


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


 16: #if defined(PETSC_HAVE_SAWS)
 17: #include <petscviewersaws.h>

 19: static PetscBool amsmemstack = PETSC_FALSE;

 23: /*@C
 24:    PetscStackSAWsGrantAccess - Grants access of the PETSc stack frames to the SAWs publisher

 26:    Collective on PETSC_COMM_WORLD?

 28:    Level: developer

 30:    Concepts: publishing object


 34: .seealso: PetscObjectSetName(), PetscObjectSAWsViewOff(), PetscObjectSAWsTakeAccess()

 36: @*/
 37: void  PetscStackSAWsGrantAccess(void)
 38: {
 39:   if (amsmemstack) {
 40:     /* ignore any errors from SAWs */
 41:     SAWs_Unlock();
 42:   }
 43: }

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

 50:    Collective on PETSC_COMM_WORLD?

 52:    Level: developer

 54:    Concepts: publishing object


 58: .seealso: PetscObjectSetName(), PetscObjectSAWsViewOff(), PetscObjectSAWsTakeAccess()

 60: @*/
 61: void  PetscStackSAWsTakeAccess(void)
 62: {
 63:   if (amsmemstack) {
 64:     /* ignore any errors from SAWs */
 65:     SAWs_Lock();
 66:   }
 67: }

 69: PetscErrorCode PetscStackViewSAWs(void)
 70: {
 71:   PetscStack*    petscstackp;
 72:   PetscMPIInt    rank;

 75:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 76:   if (rank) return 0;
 77:   petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);
 78:   PetscStackCallSAWs(SAWs_Register,("/PETSc/Stack/functions",petscstackp->function,20,SAWs_READ,SAWs_STRING));
 79:   PetscStackCallSAWs(SAWs_Register,("/PETSc/Stack/__current_size",&petscstackp->currentsize,1,SAWs_READ,SAWs_INT));
 80:   amsmemstack = PETSC_TRUE;
 81:   return 0;
 82: }

 86: PetscErrorCode PetscStackSAWsViewOff(void)
 87: {
 89:   if (!amsmemstack) return(0);
 90:   PetscStackCallSAWs(SAWs_Delete,("/PETSc/Stack"));
 91:   amsmemstack = PETSC_FALSE;
 92:   return(0);
 93: }

 95: #  endif


 98: PetscErrorCode PetscStackCreate(void)
 99: {
100:   PetscStack *petscstack_in;
101:   if (PetscStackActive()) return 0;

103:   petscstack_in              = (PetscStack*)malloc(sizeof(PetscStack));
104:   petscstack_in->currentsize = 0;
105:   petscstack_in->hotdepth    = 0;
106:   PetscThreadLocalSetValue((PetscThreadKey*)&petscstack,petscstack_in);

108: #if defined(PETSC_HAVE_SAWS)
109:   {
110:   PetscBool flg = PETSC_FALSE;
111:   PetscOptionsHasName(NULL,"-stack_view",&flg);
112:   if (flg) PetscStackViewSAWs();
113:   }
114: #endif
115:   return 0;
116: }


121: PetscErrorCode  PetscStackView(FILE *file)
122: {
123:   int        i;
124:   PetscStack *petscstackp;

126:   petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);
127:   if (!file) file = PETSC_STDOUT;

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

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

157: PetscErrorCode  PetscStackCopy(PetscStack *sint,PetscStack *sout)
158: {
159:   int i;

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

177: PetscErrorCode  PetscStackPrint(PetscStack *sint,FILE *fp)
178: {
179:   int i;

181:   if (!sint) return(0);
182:   for (i=sint->currentsize-2; i>=0; i--) fprintf(fp,"      [%d]  %s() line %d in %s\n",PetscGlobalRank,sint->function[i],sint->line[i],sint->file[i]);
183:   return 0;
184: }