1d9262e54SJed Brown 2c6db04a5SJed Brown #include <petscsys.h> /*I "petscsys.h" I*/ 3d9262e54SJed Brown 4d9262e54SJed Brown 51f46d60fSShri Abhyankar #if defined(PETSC_HAVE_PTHREADCLASSES) 6997ce2baSShri Abhyankar #if defined(PETSC_PTHREAD_LOCAL) 7997ce2baSShri Abhyankar PETSC_PTHREAD_LOCAL PetscStack *petscstack = 0; 876386721SLisandro Dalcin #else 976386721SLisandro Dalcin PetscThreadKey petscstack; 101f46d60fSShri Abhyankar #endif 11fe89fe5aSShri Abhyankar #else 127087cfbeSBarry Smith PetscStack *petscstack = 0; 13fe89fe5aSShri Abhyankar #endif 14d9262e54SJed Brown 1515681b3cSBarry Smith 16e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 17e04113cfSBarry Smith #include <petscviewersaws.h> 1815681b3cSBarry Smith 192657e9d9SBarry Smith static PetscBool amsmemstack = PETSC_FALSE; 2015681b3cSBarry Smith 21d9262e54SJed Brown #undef __FUNCT__ 22e04113cfSBarry Smith #define __FUNCT__ "PetscStackSAWsGrantAccess" 2315681b3cSBarry Smith /*@C 24e04113cfSBarry Smith PetscStackSAWsGrantAccess - Grants access of the PETSc stack frames to the SAWs publisher 2515681b3cSBarry Smith 2615681b3cSBarry Smith Collective on PETSC_COMM_WORLD? 2715681b3cSBarry Smith 2815681b3cSBarry Smith Level: developer 2915681b3cSBarry Smith 3015681b3cSBarry Smith Concepts: publishing object 3115681b3cSBarry Smith 32e04113cfSBarry Smith Developers Note: Cannot use PetscFunctionBegin/Return() or PetscStackCallSAWs() since it may be used within those routines 3315681b3cSBarry Smith 34e04113cfSBarry Smith .seealso: PetscObjectSetName(), PetscObjectSAWsViewOff(), PetscObjectSAWsTakeAccess() 3515681b3cSBarry Smith 3615681b3cSBarry Smith @*/ 37e04113cfSBarry Smith void PetscStackSAWsGrantAccess(void) 38d9262e54SJed Brown { 39ec957eceSBarry Smith if (amsmemstack) { 4016ad0300SBarry Smith /* ignore any errors from SAWs */ 419a492a5cSBarry Smith SAWs_Unlock(); 4215681b3cSBarry Smith } 43d9262e54SJed Brown } 44d9262e54SJed Brown 45d9262e54SJed Brown #undef __FUNCT__ 46e04113cfSBarry Smith #define __FUNCT__ "PetscStackSAWsTakeAccess" 4715681b3cSBarry Smith /*@C 48e04113cfSBarry Smith PetscStackSAWsTakeAccess - Takes access of the PETSc stack frames to the SAWs publisher 4915681b3cSBarry Smith 5015681b3cSBarry Smith Collective on PETSC_COMM_WORLD? 5115681b3cSBarry Smith 5215681b3cSBarry Smith Level: developer 5315681b3cSBarry Smith 5415681b3cSBarry Smith Concepts: publishing object 5515681b3cSBarry Smith 56e04113cfSBarry Smith Developers Note: Cannot use PetscFunctionBegin/Return() or PetscStackCallSAWs() since it may be used within those routines 5715681b3cSBarry Smith 58e04113cfSBarry Smith .seealso: PetscObjectSetName(), PetscObjectSAWsViewOff(), PetscObjectSAWsTakeAccess() 5915681b3cSBarry Smith 6015681b3cSBarry Smith @*/ 61e04113cfSBarry Smith void PetscStackSAWsTakeAccess(void) 62d9262e54SJed Brown { 63ec957eceSBarry Smith if (amsmemstack) { 6416ad0300SBarry Smith /* ignore any errors from SAWs */ 659a492a5cSBarry Smith SAWs_Lock(); 6615681b3cSBarry Smith } 6715681b3cSBarry Smith } 6815681b3cSBarry Smith 69e04113cfSBarry Smith PetscErrorCode PetscStackViewSAWs(void) 7015681b3cSBarry Smith { 7115681b3cSBarry Smith PetscStack* petscstackp; 72d45a07a7SBarry Smith PetscMPIInt rank; 73d45a07a7SBarry Smith PetscErrorCode ierr; 7415681b3cSBarry Smith 75d45a07a7SBarry Smith ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); 76d45a07a7SBarry Smith if (rank) return 0; 7715681b3cSBarry Smith petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack); 7867a1e3b6SBarry Smith PetscStackCallSAWs(SAWs_Register,("/PETSc/Stack/functions",petscstackp->function,20,SAWs_READ,SAWs_STRING)); 79d50831c4SBarry Smith PetscStackCallSAWs(SAWs_Register,("/PETSc/Stack/__current_size",&petscstackp->currentsize,1,SAWs_READ,SAWs_INT)); 802657e9d9SBarry Smith amsmemstack = PETSC_TRUE; 8115681b3cSBarry Smith return 0; 8215681b3cSBarry Smith } 8315681b3cSBarry Smith 8415681b3cSBarry Smith #undef __FUNCT__ 85e04113cfSBarry Smith #define __FUNCT__ "PetscStackSAWsViewOff" 86e04113cfSBarry Smith PetscErrorCode PetscStackSAWsViewOff(void) 8715681b3cSBarry Smith { 88d9262e54SJed Brown PetscFunctionBegin; 89d45a07a7SBarry Smith if (!amsmemstack) PetscFunctionReturn(0); 9016ad0300SBarry Smith PetscStackCallSAWs(SAWs_Delete,("/PETSc/Stack")); 912657e9d9SBarry Smith amsmemstack = PETSC_FALSE; 92d9262e54SJed Brown PetscFunctionReturn(0); 93d9262e54SJed Brown } 94d9262e54SJed Brown 95c82b4e47SJed Brown # endif 9676422c65SBarry Smith 9715681b3cSBarry Smith 987d5f7e0cSShri Abhyankar PetscErrorCode PetscStackCreate(void) 9974b43855SShri Abhyankar { 10074b43855SShri Abhyankar PetscStack *petscstack_in; 101*7fdeb8b9SBarry Smith PetscInt i; 102*7fdeb8b9SBarry Smith 103dbf62e16SBarry Smith if (PetscStackActive()) return 0; 10474b43855SShri Abhyankar 10574b43855SShri Abhyankar petscstack_in = (PetscStack*)malloc(sizeof(PetscStack)); 10674b43855SShri Abhyankar petscstack_in->currentsize = 0; 107a2f94806SJed Brown petscstack_in->hotdepth = 0; 108*7fdeb8b9SBarry Smith for (i=0; i<PETSCSTACKSIZE; i++) { 109*7fdeb8b9SBarry Smith petscstack_in->function[i] = 0; 110*7fdeb8b9SBarry Smith petscstack_in->file[i] = 0; 111*7fdeb8b9SBarry Smith } 112047240e1SBarry Smith PetscThreadLocalSetValue((PetscThreadKey*)&petscstack,petscstack_in); 11315681b3cSBarry Smith 114e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 11515681b3cSBarry Smith { 11615681b3cSBarry Smith PetscBool flg = PETSC_FALSE; 11715681b3cSBarry Smith PetscOptionsHasName(NULL,"-stack_view",&flg); 118e04113cfSBarry Smith if (flg) PetscStackViewSAWs(); 11915681b3cSBarry Smith } 12015681b3cSBarry Smith #endif 12174b43855SShri Abhyankar return 0; 12274b43855SShri Abhyankar } 12374b43855SShri Abhyankar 12415681b3cSBarry Smith 125d9262e54SJed Brown #undef __FUNCT__ 126d9262e54SJed Brown #define __FUNCT__ "PetscStackView" 127639ff905SBarry Smith PetscErrorCode PetscStackView(FILE *file) 128d9262e54SJed Brown { 129d9262e54SJed Brown int i; 1301f46d60fSShri Abhyankar PetscStack *petscstackp; 131d9262e54SJed Brown 1321f46d60fSShri Abhyankar petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack); 133639ff905SBarry Smith if (!file) file = PETSC_STDOUT; 134d9262e54SJed Brown 135d9262e54SJed Brown if (file == PETSC_STDOUT) { 136d9262e54SJed Brown (*PetscErrorPrintf)("Note: The EXACT line numbers in the stack are not available,\n"); 137d9262e54SJed Brown (*PetscErrorPrintf)(" INSTEAD the line number of the start of the function\n"); 138d9262e54SJed Brown (*PetscErrorPrintf)(" is given.\n"); 139efca3c55SSatish Balay 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]); 140d9262e54SJed Brown } else { 141d9262e54SJed Brown fprintf(file,"Note: The EXACT line numbers in the stack are not available,\n"); 142d9262e54SJed Brown fprintf(file," INSTEAD the line number of the start of the function\n"); 143d9262e54SJed Brown fprintf(file," is given.\n"); 144efca3c55SSatish Balay 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]); 145d9262e54SJed Brown } 146d9262e54SJed Brown return 0; 147d9262e54SJed Brown } 148d9262e54SJed Brown 1497d5f7e0cSShri Abhyankar PetscErrorCode PetscStackDestroy(void) 15074b43855SShri Abhyankar { 151dbf62e16SBarry Smith if (PetscStackActive()) { 1521f46d60fSShri Abhyankar PetscStack *petscstack_in; 1531f46d60fSShri Abhyankar petscstack_in = (PetscStack*)PetscThreadLocalGetValue(petscstack); 15474b43855SShri Abhyankar free(petscstack_in); 15576386721SLisandro Dalcin PetscThreadLocalSetValue((PetscThreadKey*)&petscstack,NULL); 1567d5f7e0cSShri Abhyankar } 157d9262e54SJed Brown return 0; 158d9262e54SJed Brown } 159d9262e54SJed Brown 160d9262e54SJed Brown #undef __FUNCT__ 161d9262e54SJed Brown #define __FUNCT__ "PetscStackCopy" 162d9262e54SJed Brown /* PetscFunctionBegin; so that make rule checkbadPetscFunctionBegin works */ 1637087cfbeSBarry Smith PetscErrorCode PetscStackCopy(PetscStack *sint,PetscStack *sout) 164d9262e54SJed Brown { 165d9262e54SJed Brown int i; 166d9262e54SJed Brown 167a297a907SKarl Rupp if (!sint) sout->currentsize = 0; 168a297a907SKarl Rupp else { 169d9262e54SJed Brown for (i=0; i<sint->currentsize; i++) { 170d9262e54SJed Brown sout->function[i] = sint->function[i]; 171d9262e54SJed Brown sout->file[i] = sint->file[i]; 172d9262e54SJed Brown sout->line[i] = sint->line[i]; 173a8d2bbe5SBarry Smith sout->petscroutine[i] = sint->petscroutine[i]; 174d9262e54SJed Brown } 175d9262e54SJed Brown sout->currentsize = sint->currentsize; 176d9262e54SJed Brown } 177d9262e54SJed Brown return 0; 178d9262e54SJed Brown } 179d9262e54SJed Brown 180d9262e54SJed Brown #undef __FUNCT__ 181d9262e54SJed Brown #define __FUNCT__ "PetscStackPrint" 182d9262e54SJed Brown /* PetscFunctionBegin; so that make rule checkbadPetscFunctionBegin works */ 1837087cfbeSBarry Smith PetscErrorCode PetscStackPrint(PetscStack *sint,FILE *fp) 184d9262e54SJed Brown { 185d9262e54SJed Brown int i; 186d9262e54SJed Brown 187d9262e54SJed Brown if (!sint) return(0); 188efca3c55SSatish Balay 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]); 189d9262e54SJed Brown return 0; 190d9262e54SJed Brown } 191d9262e54SJed Brown 192