xref: /petsc/src/sys/error/pstack.c (revision 3e1910f1ab6113d8365e15c6b8c907ccce7ce4ea)
1 
2 #include <petscsys.h>        /*I  "petscsys.h"   I*/
3 
4 #if defined(PETSC_USE_DEBUG)
5 
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
13 
14 
15 #if defined(PETSC_HAVE_AMS)
16 #include <petscviewerams.h>
17 
18 static AMS_Memory amsmemstack = -1;
19 
20 #undef __FUNCT__
21 #define __FUNCT__ "PetscStackAMSGrantAccess"
22 /*@C
23    PetscStackAMSGrantAccess - Grants access of the PETSc stack frames to the AMS publisher
24 
25    Collective on PETSC_COMM_WORLD?
26 
27    Level: developer
28 
29    Concepts: publishing object
30 
31    Developers Note: Cannot use PetscFunctionBegin/Return() or PetscStackCallAMS() since it may be used within those routines
32 
33 .seealso: PetscObjectSetName(), PetscObjectAMSViewOff(), PetscObjectAMSTakeAccess()
34 
35 @*/
36 void  PetscStackAMSGrantAccess(void)
37 {
38   if (amsmemstack != -1) {
39     AMS_Memory_grant_access(amsmemstack);
40   }
41 }
42 
43 #undef __FUNCT__
44 #define __FUNCT__ "PetscStackAMSTakeAccess"
45 /*@C
46    PetscStackAMSTakeAccess - Takes access of the PETSc stack frames to the AMS publisher
47 
48    Collective on PETSC_COMM_WORLD?
49 
50    Level: developer
51 
52    Concepts: publishing object
53 
54    Developers Note: Cannot use PetscFunctionBegin/Return() or PetscStackCallAMS() since it may be used within those routines
55 
56 .seealso: PetscObjectSetName(), PetscObjectAMSViewOff(), PetscObjectAMSTakeAccess()
57 
58 @*/
59 void  PetscStackAMSTakeAccess(void)
60 {
61   if (amsmemstack != -1) {
62     AMS_Memory_take_access(amsmemstack);
63   }
64 }
65 
66 PetscErrorCode PetscStackViewAMS(void)
67 {
68   AMS_Comm       acomm;
69   PetscErrorCode ierr;
70   AMS_Memory     mem;
71   PetscStack*    petscstackp;
72 
73   petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);
74   ierr = PetscViewerAMSGetAMSComm(PETSC_VIEWER_AMS_WORLD,&acomm);CHKERRQ(ierr);
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 }
84 
85 #undef __FUNCT__
86 #define __FUNCT__ "PetscStackAMSViewOff"
87 PetscErrorCode PetscStackAMSViewOff(void)
88 {
89   PetscErrorCode ierr;
90 
91   PetscFunctionBegin;
92   if (amsmemstack == -1) PetscFunctionReturn(0);
93   ierr        = AMS_Memory_destroy(amsmemstack);CHKERRQ(ierr);
94   amsmemstack = -1;
95   PetscFunctionReturn(0);
96 }
97 
98 #endif
99 
100 PetscErrorCode PetscStackCreate(void)
101 {
102   PetscStack *petscstack_in;
103   if (PetscStackActive()) return 0;
104 
105   petscstack_in              = (PetscStack*)malloc(sizeof(PetscStack));
106   petscstack_in->currentsize = 0;
107   PetscThreadLocalSetValue((PetscThreadKey*)&petscstack,petscstack_in);
108 
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 }
118 
119 
120 #undef __FUNCT__
121 #define __FUNCT__ "PetscStackView"
122 PetscErrorCode  PetscStackView(FILE *file)
123 {
124   int        i;
125   PetscStack *petscstackp;
126 
127   petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);
128   if (!file) file = PETSC_STDOUT;
129 
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 }
143 
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 }
155 
156 #undef __FUNCT__
157 #define __FUNCT__ "PetscStackCopy"
158 /*  PetscFunctionBegin;  so that make rule checkbadPetscFunctionBegin works */
159 PetscErrorCode  PetscStackCopy(PetscStack *sint,PetscStack *sout)
160 {
161   int i;
162 
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 }
176 
177 #undef __FUNCT__
178 #define __FUNCT__ "PetscStackPrint"
179 /*  PetscFunctionBegin;  so that make rule checkbadPetscFunctionBegin works */
180 PetscErrorCode  PetscStackPrint(PetscStack *sint,FILE *fp)
181 {
182   int i;
183 
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 }
188 
189 #else
190 
191 #undef __FUNCT__
192 #define __FUNCT__ "PetscStackCreate"
193 PetscErrorCode  PetscStackCreate(void)
194 {
195   PetscFunctionBegin;
196   PetscFunctionReturn(0);
197 }
198 #undef __FUNCT__
199 #define __FUNCT__ "PetscStackView"
200 PetscErrorCode  PetscStackView(FILE *file)
201 {
202   PetscFunctionBegin;
203   PetscFunctionReturn(0);
204 }
205 #undef __FUNCT__
206 #define __FUNCT__ "PetscStackDestroy"
207 PetscErrorCode  PetscStackDestroy(void)
208 {
209   PetscFunctionBegin;
210   PetscFunctionReturn(0);
211 }
212 
213 #if defined(PETSC_HAVE_AMS)     /* AMS stack functions do nothing in optimized mode */
214 void PetscStackAMSGrantAccess(void) {}
215 void PetscStackAMSTakeAccess(void) {}
216 
217 PetscErrorCode PetscStackViewAMS(void)
218 {
219   return 0;
220 }
221 
222 #undef __FUNCT__
223 #define __FUNCT__ "PetscStackAMSViewOff"
224 PetscErrorCode  PetscStackAMSViewOff(void)
225 {
226   PetscFunctionBegin;
227   PetscFunctionReturn(0);
228 }
229 #endif
230 
231 #endif
232 
233