Date: 2013-07-02T14:38:41+00:00

Actual source code: pstack.c
petsc-3.4.2 2013-07-02

      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: #endif
     10: #else
     11: PetscStack *petscstack = 0;
     12: #endif

     15: #if defined(PETSC_HAVE_AMS)
     16: #include <petscviewerams.h>

     18: static AMS_Memory amsmemstack = -1;

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

     25:    Collective on PETSC_COMM_WORLD?

     27:    Level: developer

     29:    Concepts: publishing object

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

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

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

     48:    Collective on PETSC_COMM_WORLD?

     50:    Level: developer

     52:    Concepts: publishing object

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

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

     66: PetscErrorCode PetscStackViewAMS(void)
     67: {
     68:   AMS_Comm       acomm;
     70:   AMS_Memory     mem;
     71:   PetscStack*    petscstackp;

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

     87: PetscErrorCode PetscStackAMSViewOff(void)
     88: {

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

     98: #endif

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

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

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

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

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

    130:   if (file == PETSC_STDOUT) {
    131:     (*PetscErrorPrintf)("Note: The EXACT line numbers in the stack are not available,\n");
    132:     (*PetscErrorPrintf)("      INSTEAD the line number of the start of the function\n");
    133:     (*PetscErrorPrintf)("      is given.\n");
    134:     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]);
    135:   } else {
    136:     fprintf(file,"Note: The EXACT line numbers in the stack are not available,\n");
    137:     fprintf(file,"      INSTEAD the line number of the start of the function\n");
    138:     fprintf(file,"      is given.\n");
    139:     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]);
    140:   }
    141:   return 0;
    142: }

    144: PetscErrorCode PetscStackDestroy(void)
    145: {
    146:   if (PetscStackActive()) {
    147:     PetscStack *petscstack_in;
    148:     petscstack_in = (PetscStack*)PetscThreadLocalGetValue(petscstack);
    149:     free(petscstack_in);
    150:     PetscThreadLocalSetValue((PetscThreadKey*)&petscstack,(PetscStack*)0);
    151:     PetscThreadLocalDestroy(petscstack); /* Deletes pthread_key if it was used */
    152:   }
    153:   return 0;
    154: }

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

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

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

    184:   if (!sint) return(0);
    185:   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]);
    186:   return 0;
    187: }

    189: #else

    193: PetscErrorCode  PetscStackCreate(void)
    194: {
    196:   return(0);
    197: }
    200: PetscErrorCode  PetscStackView(FILE *file)
    201: {
    203:   return(0);
    204: }
    207: PetscErrorCode  PetscStackDestroy(void)
    208: {
    210:   return(0);
    211: }

    213: #if defined(PETSC_HAVE_AMS)     /* AMS stack functions do nothing in optimized mode */
    214: void PetscStackAMSGrantAccess(void) {}
    215: void PetscStackAMSTakeAccess(void) {}

    217: PetscErrorCode PetscStackViewAMS(void)
    218: {
    219:   return 0;
    220: }

    224: PetscErrorCode  PetscStackAMSViewOff(void)
    225: {
    227:   return(0);
    228: }
    229: #endif

    231: #endif