xref: /petsc/src/sys/error/pstack.c (revision 1f46d60f66d2379a7cf045b103b4a98b2ddbb736)
1 
2 #include <petscsys.h>        /*I  "petscsys.h"   I*/
3 #include <petscthreadcomm.h>
4 
5 #if defined(PETSC_USE_DEBUG)
6 
7 #if defined(PETSC_HAVE_PTHREADCLASSES)
8 #if defined(PETSC_PTHREAD_LOCAL)
9 PETSC_PTHREAD_LOCAL PetscStack  *petscstack = 0;
10 #endif
11 #else
12 PetscStack *petscstack = 0;
13 #endif
14 
15 #undef __FUNCT__
16 #define __FUNCT__ "PetscStackPublish"
17 PetscErrorCode  PetscStackPublish(void)
18 {
19   PetscFunctionBegin;
20   PetscFunctionReturn(0);
21 }
22 
23 #undef __FUNCT__
24 #define __FUNCT__ "PetscStackDepublish"
25 PetscErrorCode  PetscStackDepublish(void)
26 {
27   PetscFunctionBegin;
28   PetscFunctionReturn(0);
29 }
30 
31 PetscErrorCode PetscStackCreate_kernel(PetscInt trank)
32 {
33   PetscStack *petscstack_in;
34   if(PetscStackActive) return 0;
35 
36   petscstack_in = (PetscStack*)malloc(sizeof(PetscStack));
37   petscstack_in->currentsize = 0;
38   PetscThreadLocalSetValue(petscstack,petscstack_in);
39   return 0;
40 }
41 
42 #undef __FUNCT__
43 #define __FUNCT__ "PetscStackCreate"
44 PetscErrorCode  PetscStackCreate(void)
45 {
46   PetscErrorCode ierr;
47 
48   ierr = PetscThreadCommRunKernel0(PETSC_COMM_SELF,(PetscThreadKernel)PetscStackCreate_kernel);CHKERRQ(ierr);
49   ierr = PetscThreadCommBarrier(PETSC_COMM_SELF);CHKERRQ(ierr);
50   return 0;
51 }
52 
53 #undef __FUNCT__
54 #define __FUNCT__ "PetscStackView"
55 PetscErrorCode  PetscStackView(PetscViewer viewer)
56 {
57   PetscErrorCode ierr;
58   int  i;
59   FILE *file;
60   PetscStack* petscstackp;
61 
62   petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);
63   if (!viewer) viewer = PETSC_VIEWER_STDOUT_SELF;
64   ierr = PetscViewerASCIIGetPointer(viewer,&file);CHKERRQ(ierr);
65 
66   if (file == PETSC_STDOUT) {
67     (*PetscErrorPrintf)("Note: The EXACT line numbers in the stack are not available,\n");
68     (*PetscErrorPrintf)("      INSTEAD the line number of the start of the function\n");
69     (*PetscErrorPrintf)("      is given.\n");
70     for (i=petscstackp->currentsize-1; i>=0; i--) {
71       (*PetscErrorPrintf)("[%d] %s line %d %s%s\n",PetscGlobalRank,
72                                                    petscstackp->function[i],
73                                                    petscstackp->line[i],
74                                                    petscstackp->directory[i],
75                                                    petscstackp->file[i]);
76     }
77   } else {
78     fprintf(file,"Note: The EXACT line numbers in the stack are not available,\n");
79     fprintf(file,"      INSTEAD the line number of the start of the function\n");
80     fprintf(file,"      is given.\n");
81     for (i=petscstackp->currentsize-1; i>=0; i--) {
82       fprintf(file,"[%d] %s line %d %s%s\n",PetscGlobalRank,
83                                             petscstackp->function[i],
84                                             petscstackp->line[i],
85                                             petscstackp->directory[i],
86                                             petscstackp->file[i]);
87     }
88   }
89   return 0;
90 }
91 
92 PetscErrorCode PetscStackDestroy_kernel(PetscInt trank)
93 {
94   if(PetscStackActive) {
95     PetscStack *petscstack_in;
96     petscstack_in = (PetscStack*)PetscThreadLocalGetValue(petscstack);
97     free(petscstack_in);
98     PetscThreadLocalSetValue(petscstack,(PetscStack*)0);
99   }
100   return 0;
101 }
102 
103 #undef __FUNCT__
104 #define __FUNCT__ "PetscStackDestroy"
105 /*  PetscFunctionBegin;  so that make rule checkbadPetscFunctionBegin works */
106 PetscErrorCode  PetscStackDestroy(void)
107 {
108   PetscErrorCode ierr;
109   ierr = PetscThreadCommRunKernel0(PETSC_COMM_SELF,(PetscThreadKernel)PetscStackDestroy_kernel);CHKERRQ(ierr);
110   ierr = PetscThreadCommBarrier(PETSC_COMM_SELF);CHKERRQ(ierr);
111   PetscThreadLocalDestroy(petscstack); /* Deletes pthread_key if it was used */
112   return 0;
113 }
114 
115 #undef __FUNCT__
116 #define __FUNCT__ "PetscStackCopy"
117 /*  PetscFunctionBegin;  so that make rule checkbadPetscFunctionBegin works */
118 PetscErrorCode  PetscStackCopy(PetscStack* sint,PetscStack* sout)
119 {
120   int i;
121 
122   if (!sint) {
123     sout->currentsize = 0;
124   } else {
125     for (i=0; i<sint->currentsize; i++) {
126       sout->function[i]  = sint->function[i];
127       sout->file[i]      = sint->file[i];
128       sout->directory[i] = sint->directory[i];
129       sout->line[i]      = sint->line[i];
130     }
131     sout->currentsize = sint->currentsize;
132   }
133   return 0;
134 }
135 
136 #undef __FUNCT__
137 #define __FUNCT__ "PetscStackPrint"
138 /*  PetscFunctionBegin;  so that make rule checkbadPetscFunctionBegin works */
139 PetscErrorCode  PetscStackPrint(PetscStack* sint,FILE *fp)
140 {
141   int i;
142 
143   if (!sint) return(0);
144   for (i=sint->currentsize-3; i>=0; i--) {
145     fprintf(fp,"      [%d]  %s() line %d in %s%s\n",PetscGlobalRank,sint->function[i],sint->line[i],sint->directory[i],sint->file[i]);
146   }
147   return 0;
148 }
149 
150 #else
151 #undef __FUNCT__
152 #define __FUNCT__ "PetscStackPublish"
153 PetscErrorCode  PetscStackPublish(void)
154 {
155   PetscFunctionBegin;
156   PetscFunctionReturn(0);
157 }
158 #undef __FUNCT__
159 #define __FUNCT__ "PetscStackDepublish"
160 PetscErrorCode  PetscStackDepublish(void)
161 {
162   PetscFunctionBegin;
163   PetscFunctionReturn(0);
164 }
165 #undef __FUNCT__
166 #define __FUNCT__ "PetscStackCreate"
167 PetscErrorCode  PetscStackCreate(void)
168 {
169   PetscFunctionBegin;
170   PetscFunctionReturn(0);
171 }
172 #undef __FUNCT__
173 #define __FUNCT__ "PetscStackView"
174 PetscErrorCode  PetscStackView(PetscViewer viewer)
175 {
176   PetscFunctionBegin;
177   PetscFunctionReturn(0);
178 }
179 #undef __FUNCT__
180 #define __FUNCT__ "PetscStackDestroy"
181 PetscErrorCode  PetscStackDestroy(void)
182 {
183   PetscFunctionBegin;
184   PetscFunctionReturn(0);
185 }
186 
187 #endif
188 
189