xref: /petsc/src/sys/error/pstack.c (revision fcaff9f8f8ca2c78da8a06e5dc3b76048f2afb85)
1 
2 #include <petscsys.h>        /*I  "petscsys.h"   I*/
3 
4 
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
14 
15 
16 #if defined(PETSC_HAVE_SAWS)
17 #include <petscviewersaws.h>
18 
19 static PetscBool amsmemstack = PETSC_FALSE;
20 
21 #undef __FUNCT__
22 #define __FUNCT__ "PetscStackSAWsGrantAccess"
23 /*@C
24    PetscStackSAWsGrantAccess - Grants access of the PETSc stack frames to the SAWs publisher
25 
26    Collective on PETSC_COMM_WORLD?
27 
28    Level: developer
29 
30    Concepts: publishing object
31 
32    Developers Note: Cannot use PetscFunctionBegin/Return() or PetscStackCallSAWs() since it may be used within those routines
33 
34 .seealso: PetscObjectSetName(), PetscObjectSAWsViewOff(), PetscObjectSAWsTakeAccess()
35 
36 @*/
37 void  PetscStackSAWsGrantAccess(void)
38 {
39   if (amsmemstack) {
40     /* ignore any errors from SAWs */
41     SAWs_Unlock();
42   }
43 }
44 
45 #undef __FUNCT__
46 #define __FUNCT__ "PetscStackSAWsTakeAccess"
47 /*@C
48    PetscStackSAWsTakeAccess - Takes access of the PETSc stack frames to the SAWs publisher
49 
50    Collective on PETSC_COMM_WORLD?
51 
52    Level: developer
53 
54    Concepts: publishing object
55 
56    Developers Note: Cannot use PetscFunctionBegin/Return() or PetscStackCallSAWs() since it may be used within those routines
57 
58 .seealso: PetscObjectSetName(), PetscObjectSAWsViewOff(), PetscObjectSAWsTakeAccess()
59 
60 @*/
61 void  PetscStackSAWsTakeAccess(void)
62 {
63   if (amsmemstack) {
64     /* ignore any errors from SAWs */
65     SAWs_Lock();
66   }
67 }
68 
69 PetscErrorCode PetscStackViewSAWs(void)
70 {
71   PetscStack*    petscstackp;
72   PetscMPIInt    rank;
73   PetscErrorCode ierr;
74 
75   ierr  = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr);
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 }
83 
84 #undef __FUNCT__
85 #define __FUNCT__ "PetscStackSAWsViewOff"
86 PetscErrorCode PetscStackSAWsViewOff(void)
87 {
88   PetscFunctionBegin;
89   if (!amsmemstack) PetscFunctionReturn(0);
90   PetscStackCallSAWs(SAWs_Delete,("/PETSc/Stack"));
91   amsmemstack = PETSC_FALSE;
92   PetscFunctionReturn(0);
93 }
94 
95 #  else
96 
97 /* AMS stack functions do nothing in optimized mode */
98 void PetscStackAMSGrantAccess(void) {}
99 void PetscStackAMSTakeAccess(void) {}
100 
101 PetscErrorCode PetscStackViewAMS(void)
102 {
103   return 0;
104 }
105 
106 #undef __FUNCT__
107 #define __FUNCT__ "PetscStackAMSViewOff"
108 PetscErrorCode  PetscStackAMSViewOff(void)
109 {
110   PetscFunctionBegin;
111   PetscFunctionReturn(0);
112 }
113 
114 #  endif
115 #endif
116 
117 PetscErrorCode PetscStackCreate(void)
118 {
119   PetscStack *petscstack_in;
120   if (PetscStackActive()) return 0;
121 
122   petscstack_in              = (PetscStack*)malloc(sizeof(PetscStack));
123   petscstack_in->currentsize = 0;
124   petscstack_in->hotdepth    = 0;
125   PetscThreadLocalSetValue((PetscThreadKey*)&petscstack,petscstack_in);
126 
127 #if defined(PETSC_HAVE_SAWS)
128   {
129   PetscBool flg = PETSC_FALSE;
130   PetscOptionsHasName(NULL,"-stack_view",&flg);
131   if (flg) PetscStackViewSAWs();
132   }
133 #endif
134   return 0;
135 }
136 
137 
138 #undef __FUNCT__
139 #define __FUNCT__ "PetscStackView"
140 PetscErrorCode  PetscStackView(FILE *file)
141 {
142   int        i;
143   PetscStack *petscstackp;
144 
145   petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);
146   if (!file) file = PETSC_STDOUT;
147 
148   if (file == PETSC_STDOUT) {
149     (*PetscErrorPrintf)("Note: The EXACT line numbers in the stack are not available,\n");
150     (*PetscErrorPrintf)("      INSTEAD the line number of the start of the function\n");
151     (*PetscErrorPrintf)("      is given.\n");
152     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]);
153   } else {
154     fprintf(file,"Note: The EXACT line numbers in the stack are not available,\n");
155     fprintf(file,"      INSTEAD the line number of the start of the function\n");
156     fprintf(file,"      is given.\n");
157     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]);
158   }
159   return 0;
160 }
161 
162 PetscErrorCode PetscStackDestroy(void)
163 {
164   if (PetscStackActive()) {
165     PetscStack *petscstack_in;
166     petscstack_in = (PetscStack*)PetscThreadLocalGetValue(petscstack);
167     free(petscstack_in);
168     PetscThreadLocalSetValue((PetscThreadKey*)&petscstack,NULL);
169   }
170   return 0;
171 }
172 
173 #undef __FUNCT__
174 #define __FUNCT__ "PetscStackCopy"
175 /*  PetscFunctionBegin;  so that make rule checkbadPetscFunctionBegin works */
176 PetscErrorCode  PetscStackCopy(PetscStack *sint,PetscStack *sout)
177 {
178   int i;
179 
180   if (!sint) sout->currentsize = 0;
181   else {
182     for (i=0; i<sint->currentsize; i++) {
183       sout->function[i]     = sint->function[i];
184       sout->file[i]         = sint->file[i];
185       sout->line[i]         = sint->line[i];
186       sout->petscroutine[i] = sint->petscroutine[i];
187     }
188     sout->currentsize = sint->currentsize;
189   }
190   return 0;
191 }
192 
193 #undef __FUNCT__
194 #define __FUNCT__ "PetscStackPrint"
195 /*  PetscFunctionBegin;  so that make rule checkbadPetscFunctionBegin works */
196 PetscErrorCode  PetscStackPrint(PetscStack *sint,FILE *fp)
197 {
198   int i;
199 
200   if (!sint) return(0);
201   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]);
202   return 0;
203 }
204 
205