xref: /petsc/src/sys/error/pstack.c (revision 5c25fcd7c4e1ecb15ec7a0829572c7e72f90b2d9)
1d9262e54SJed Brown 
2c6db04a5SJed Brown #include <petscsys.h>        /*I  "petscsys.h"   I*/
3d9262e54SJed Brown 
47087cfbeSBarry Smith PetscStack *petscstack = 0;
515681b3cSBarry Smith 
6e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
7e04113cfSBarry Smith #include <petscviewersaws.h>
815681b3cSBarry Smith 
92657e9d9SBarry Smith static PetscBool amsmemstack = PETSC_FALSE;
1015681b3cSBarry Smith 
11d9262e54SJed Brown #undef __FUNCT__
12e04113cfSBarry Smith #define __FUNCT__ "PetscStackSAWsGrantAccess"
1315681b3cSBarry Smith /*@C
14e04113cfSBarry Smith    PetscStackSAWsGrantAccess - Grants access of the PETSc stack frames to the SAWs publisher
1515681b3cSBarry Smith 
1615681b3cSBarry Smith    Collective on PETSC_COMM_WORLD?
1715681b3cSBarry Smith 
1815681b3cSBarry Smith    Level: developer
1915681b3cSBarry Smith 
2015681b3cSBarry Smith    Concepts: publishing object
2115681b3cSBarry Smith 
22e04113cfSBarry Smith    Developers Note: Cannot use PetscFunctionBegin/Return() or PetscStackCallSAWs() since it may be used within those routines
2315681b3cSBarry Smith 
24e04113cfSBarry Smith .seealso: PetscObjectSetName(), PetscObjectSAWsViewOff(), PetscObjectSAWsTakeAccess()
2515681b3cSBarry Smith 
2615681b3cSBarry Smith @*/
27e04113cfSBarry Smith void  PetscStackSAWsGrantAccess(void)
28d9262e54SJed Brown {
29ec957eceSBarry Smith   if (amsmemstack) {
3016ad0300SBarry Smith     /* ignore any errors from SAWs */
319a492a5cSBarry Smith     SAWs_Unlock();
3215681b3cSBarry Smith   }
33d9262e54SJed Brown }
34d9262e54SJed Brown 
35d9262e54SJed Brown #undef __FUNCT__
36e04113cfSBarry Smith #define __FUNCT__ "PetscStackSAWsTakeAccess"
3715681b3cSBarry Smith /*@C
38e04113cfSBarry Smith    PetscStackSAWsTakeAccess - Takes access of the PETSc stack frames to the SAWs publisher
3915681b3cSBarry Smith 
4015681b3cSBarry Smith    Collective on PETSC_COMM_WORLD?
4115681b3cSBarry Smith 
4215681b3cSBarry Smith    Level: developer
4315681b3cSBarry Smith 
4415681b3cSBarry Smith    Concepts: publishing object
4515681b3cSBarry Smith 
46e04113cfSBarry Smith    Developers Note: Cannot use PetscFunctionBegin/Return() or PetscStackCallSAWs() since it may be used within those routines
4715681b3cSBarry Smith 
48e04113cfSBarry Smith .seealso: PetscObjectSetName(), PetscObjectSAWsViewOff(), PetscObjectSAWsTakeAccess()
4915681b3cSBarry Smith 
5015681b3cSBarry Smith @*/
51e04113cfSBarry Smith void  PetscStackSAWsTakeAccess(void)
52d9262e54SJed Brown {
53ec957eceSBarry Smith   if (amsmemstack) {
5416ad0300SBarry Smith     /* ignore any errors from SAWs */
559a492a5cSBarry Smith     SAWs_Lock();
5615681b3cSBarry Smith   }
5715681b3cSBarry Smith }
5815681b3cSBarry Smith 
59e04113cfSBarry Smith PetscErrorCode PetscStackViewSAWs(void)
6015681b3cSBarry Smith {
61d45a07a7SBarry Smith   PetscMPIInt    rank;
62d45a07a7SBarry Smith   PetscErrorCode ierr;
6315681b3cSBarry Smith 
64d45a07a7SBarry Smith   ierr  = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
65d45a07a7SBarry Smith   if (rank) return 0;
66*5c25fcd7SBarry Smith   PetscStackCallSAWs(SAWs_Register,("/PETSc/Stack/functions",petscstack->function,20,SAWs_READ,SAWs_STRING));
67*5c25fcd7SBarry Smith   PetscStackCallSAWs(SAWs_Register,("/PETSc/Stack/__current_size",&petscstack->currentsize,1,SAWs_READ,SAWs_INT));
682657e9d9SBarry Smith   amsmemstack = PETSC_TRUE;
6915681b3cSBarry Smith   return 0;
7015681b3cSBarry Smith }
7115681b3cSBarry Smith 
7215681b3cSBarry Smith #undef __FUNCT__
73e04113cfSBarry Smith #define __FUNCT__ "PetscStackSAWsViewOff"
74e04113cfSBarry Smith PetscErrorCode PetscStackSAWsViewOff(void)
7515681b3cSBarry Smith {
76d9262e54SJed Brown   PetscFunctionBegin;
77d45a07a7SBarry Smith   if (!amsmemstack) PetscFunctionReturn(0);
7816ad0300SBarry Smith   PetscStackCallSAWs(SAWs_Delete,("/PETSc/Stack"));
792657e9d9SBarry Smith   amsmemstack = PETSC_FALSE;
80d9262e54SJed Brown   PetscFunctionReturn(0);
81d9262e54SJed Brown }
82d9262e54SJed Brown 
83c82b4e47SJed Brown #  endif
8476422c65SBarry Smith 
8515681b3cSBarry Smith 
867d5f7e0cSShri Abhyankar PetscErrorCode PetscStackCreate(void)
8774b43855SShri Abhyankar {
8874b43855SShri Abhyankar   PetscStack *petscstack_in;
897fdeb8b9SBarry Smith   PetscInt   i;
907fdeb8b9SBarry Smith 
91dbf62e16SBarry Smith   if (PetscStackActive()) return 0;
9274b43855SShri Abhyankar 
9374b43855SShri Abhyankar   petscstack_in              = (PetscStack*)malloc(sizeof(PetscStack));
9474b43855SShri Abhyankar   petscstack_in->currentsize = 0;
95a2f94806SJed Brown   petscstack_in->hotdepth    = 0;
967fdeb8b9SBarry Smith   for (i=0; i<PETSCSTACKSIZE; i++) {
977fdeb8b9SBarry Smith     petscstack_in->function[i] = 0;
987fdeb8b9SBarry Smith     petscstack_in->file[i]     = 0;
997fdeb8b9SBarry Smith   }
100*5c25fcd7SBarry Smith   petscstack = petscstack_in;
10115681b3cSBarry Smith 
102e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS)
10315681b3cSBarry Smith   {
10415681b3cSBarry Smith   PetscBool flg = PETSC_FALSE;
10515681b3cSBarry Smith   PetscOptionsHasName(NULL,"-stack_view",&flg);
106e04113cfSBarry Smith   if (flg) PetscStackViewSAWs();
10715681b3cSBarry Smith   }
10815681b3cSBarry Smith #endif
10974b43855SShri Abhyankar   return 0;
11074b43855SShri Abhyankar }
11174b43855SShri Abhyankar 
11215681b3cSBarry Smith 
113d9262e54SJed Brown #undef __FUNCT__
114d9262e54SJed Brown #define __FUNCT__ "PetscStackView"
115639ff905SBarry Smith PetscErrorCode  PetscStackView(FILE *file)
116d9262e54SJed Brown {
117d9262e54SJed Brown   int        i;
118d9262e54SJed Brown 
119639ff905SBarry Smith   if (!file) file = PETSC_STDOUT;
120d9262e54SJed Brown 
121d9262e54SJed Brown   if (file == PETSC_STDOUT) {
122d9262e54SJed Brown     (*PetscErrorPrintf)("Note: The EXACT line numbers in the stack are not available,\n");
123d9262e54SJed Brown     (*PetscErrorPrintf)("      INSTEAD the line number of the start of the function\n");
124d9262e54SJed Brown     (*PetscErrorPrintf)("      is given.\n");
125*5c25fcd7SBarry Smith     for (i=petscstack->currentsize-1; i>=0; i--) (*PetscErrorPrintf)("[%d] %s line %d %s\n",PetscGlobalRank,petscstack->function[i],petscstack->line[i],petscstack->file[i]);
126d9262e54SJed Brown   } else {
127d9262e54SJed Brown     fprintf(file,"Note: The EXACT line numbers in the stack are not available,\n");
128d9262e54SJed Brown     fprintf(file,"      INSTEAD the line number of the start of the function\n");
129d9262e54SJed Brown     fprintf(file,"      is given.\n");
130*5c25fcd7SBarry Smith     for (i=petscstack->currentsize-1; i>=0; i--) fprintf(file,"[%d] %s line %d %s\n",PetscGlobalRank,petscstack->function[i],petscstack->line[i],petscstack->file[i]);
131d9262e54SJed Brown   }
132d9262e54SJed Brown   return 0;
133d9262e54SJed Brown }
134d9262e54SJed Brown 
1357d5f7e0cSShri Abhyankar PetscErrorCode PetscStackDestroy(void)
13674b43855SShri Abhyankar {
137dbf62e16SBarry Smith   if (PetscStackActive()) {
138*5c25fcd7SBarry Smith     free(petscstack);
139*5c25fcd7SBarry Smith     petscstack = NULL;
1407d5f7e0cSShri Abhyankar   }
141d9262e54SJed Brown   return 0;
142d9262e54SJed Brown }
143d9262e54SJed Brown 
144d9262e54SJed Brown #undef __FUNCT__
145d9262e54SJed Brown #define __FUNCT__ "PetscStackCopy"
146d9262e54SJed Brown /*  PetscFunctionBegin;  so that make rule checkbadPetscFunctionBegin works */
1477087cfbeSBarry Smith PetscErrorCode  PetscStackCopy(PetscStack *sint,PetscStack *sout)
148d9262e54SJed Brown {
149d9262e54SJed Brown   int i;
150d9262e54SJed Brown 
151a297a907SKarl Rupp   if (!sint) sout->currentsize = 0;
152a297a907SKarl Rupp   else {
153d9262e54SJed Brown     for (i=0; i<sint->currentsize; i++) {
154d9262e54SJed Brown       sout->function[i]     = sint->function[i];
155d9262e54SJed Brown       sout->file[i]         = sint->file[i];
156d9262e54SJed Brown       sout->line[i]         = sint->line[i];
157a8d2bbe5SBarry Smith       sout->petscroutine[i] = sint->petscroutine[i];
158d9262e54SJed Brown     }
159d9262e54SJed Brown     sout->currentsize = sint->currentsize;
160d9262e54SJed Brown   }
161d9262e54SJed Brown   return 0;
162d9262e54SJed Brown }
163d9262e54SJed Brown 
164d9262e54SJed Brown #undef __FUNCT__
165d9262e54SJed Brown #define __FUNCT__ "PetscStackPrint"
166d9262e54SJed Brown /*  PetscFunctionBegin;  so that make rule checkbadPetscFunctionBegin works */
1677087cfbeSBarry Smith PetscErrorCode  PetscStackPrint(PetscStack *sint,FILE *fp)
168d9262e54SJed Brown {
169d9262e54SJed Brown   int i;
170d9262e54SJed Brown 
171d9262e54SJed Brown   if (!sint) return(0);
172efca3c55SSatish 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]);
173d9262e54SJed Brown   return 0;
174d9262e54SJed Brown }
175d9262e54SJed Brown 
176