xref: /petsc/src/sys/error/pstack.c (revision ec957ece147126e1303fa8daf36bd57228a49e96)
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 = NULL;
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) {
39     AMS_Unlock_Memory(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) {
62     AMS_Lock_Memory(amsmemstack);
63   }
64 }
65 
66 PetscErrorCode PetscStackViewAMS(void)
67 {
68   AMS_Memory     mem;
69   PetscStack*    petscstackp;
70 
71   petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);
72   PetscStackCallAMS(AMS_Memory_Create,("Stack",&mem));
73   PetscStackCallAMS(AMS_New_Field,(mem,"functions",petscstackp->function,10,AMS_READ,AMS_STRING));
74   PetscStackCallAMS(AMS_New_Field,(mem,"current size",&petscstackp->currentsize,1,AMS_READ,AMS_INT));
75   amsmemstack = mem;
76   return 0;
77 }
78 
79 #undef __FUNCT__
80 #define __FUNCT__ "PetscStackAMSViewOff"
81 PetscErrorCode PetscStackAMSViewOff(void)
82 {
83   PetscErrorCode ierr;
84 
85   PetscFunctionBegin;
86   if (!amsmemstack) PetscFunctionReturn(0);
87   ierr        = AMS_Memory_Destroy(amsmemstack);CHKERRQ(ierr);
88   amsmemstack = NULL;
89   PetscFunctionReturn(0);
90 }
91 
92 #endif
93 
94 PetscErrorCode PetscStackCreate(void)
95 {
96   PetscStack *petscstack_in;
97   if (PetscStackActive()) return 0;
98 
99   petscstack_in              = (PetscStack*)malloc(sizeof(PetscStack));
100   petscstack_in->currentsize = 0;
101   PetscThreadLocalSetValue((PetscThreadKey*)&petscstack,petscstack_in);
102 
103 #if defined(PETSC_HAVE_AMS)
104   {
105   PetscBool flg = PETSC_FALSE;
106   PetscOptionsHasName(NULL,"-stack_view",&flg);
107   if (flg) PetscStackViewAMS();
108   }
109 #endif
110   return 0;
111 }
112 
113 
114 #undef __FUNCT__
115 #define __FUNCT__ "PetscStackView"
116 PetscErrorCode  PetscStackView(FILE *file)
117 {
118   int        i;
119   PetscStack *petscstackp;
120 
121   petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);
122   if (!file) file = PETSC_STDOUT;
123 
124   if (file == PETSC_STDOUT) {
125     (*PetscErrorPrintf)("Note: The EXACT line numbers in the stack are not available,\n");
126     (*PetscErrorPrintf)("      INSTEAD the line number of the start of the function\n");
127     (*PetscErrorPrintf)("      is given.\n");
128     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]);
129   } else {
130     fprintf(file,"Note: The EXACT line numbers in the stack are not available,\n");
131     fprintf(file,"      INSTEAD the line number of the start of the function\n");
132     fprintf(file,"      is given.\n");
133     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]);
134   }
135   return 0;
136 }
137 
138 PetscErrorCode PetscStackDestroy(void)
139 {
140   if (PetscStackActive()) {
141     PetscStack *petscstack_in;
142     petscstack_in = (PetscStack*)PetscThreadLocalGetValue(petscstack);
143     free(petscstack_in);
144     PetscThreadLocalSetValue((PetscThreadKey*)&petscstack,(PetscStack*)0);
145     PetscThreadLocalDestroy(petscstack); /* Deletes pthread_key if it was used */
146   }
147   return 0;
148 }
149 
150 #undef __FUNCT__
151 #define __FUNCT__ "PetscStackCopy"
152 /*  PetscFunctionBegin;  so that make rule checkbadPetscFunctionBegin works */
153 PetscErrorCode  PetscStackCopy(PetscStack *sint,PetscStack *sout)
154 {
155   int i;
156 
157   if (!sint) sout->currentsize = 0;
158   else {
159     for (i=0; i<sint->currentsize; i++) {
160       sout->function[i]     = sint->function[i];
161       sout->file[i]         = sint->file[i];
162       sout->directory[i]    = sint->directory[i];
163       sout->line[i]         = sint->line[i];
164       sout->petscroutine[i] = sint->petscroutine[i];
165     }
166     sout->currentsize = sint->currentsize;
167   }
168   return 0;
169 }
170 
171 #undef __FUNCT__
172 #define __FUNCT__ "PetscStackPrint"
173 /*  PetscFunctionBegin;  so that make rule checkbadPetscFunctionBegin works */
174 PetscErrorCode  PetscStackPrint(PetscStack *sint,FILE *fp)
175 {
176   int i;
177 
178   if (!sint) return(0);
179   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]);
180   return 0;
181 }
182 
183 #else
184 
185 #undef __FUNCT__
186 #define __FUNCT__ "PetscStackCreate"
187 PetscErrorCode  PetscStackCreate(void)
188 {
189   PetscFunctionBegin;
190   PetscFunctionReturn(0);
191 }
192 #undef __FUNCT__
193 #define __FUNCT__ "PetscStackView"
194 PetscErrorCode  PetscStackView(FILE *file)
195 {
196   PetscFunctionBegin;
197   PetscFunctionReturn(0);
198 }
199 #undef __FUNCT__
200 #define __FUNCT__ "PetscStackDestroy"
201 PetscErrorCode  PetscStackDestroy(void)
202 {
203   PetscFunctionBegin;
204   PetscFunctionReturn(0);
205 }
206 
207 #if defined(PETSC_HAVE_AMS)     /* AMS stack functions do nothing in optimized mode */
208 void PetscStackAMSGrantAccess(void) {}
209 void PetscStackAMSTakeAccess(void) {}
210 
211 PetscErrorCode PetscStackViewAMS(void)
212 {
213   return 0;
214 }
215 
216 #undef __FUNCT__
217 #define __FUNCT__ "PetscStackAMSViewOff"
218 PetscErrorCode  PetscStackAMSViewOff(void)
219 {
220   PetscFunctionBegin;
221   PetscFunctionReturn(0);
222 }
223 #endif
224 
225 #endif
226 
227