xref: /petsc/src/sys/error/pstack.c (revision 519f805a543c2a7f195bcba21173bb2cdfadb956)
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       sout->petscroutine[i] = sint->petscroutine[i];
131     }
132     sout->currentsize = sint->currentsize;
133   }
134   return 0;
135 }
136 
137 #undef __FUNCT__
138 #define __FUNCT__ "PetscStackPrint"
139 /*  PetscFunctionBegin;  so that make rule checkbadPetscFunctionBegin works */
140 PetscErrorCode  PetscStackPrint(PetscStack* sint,FILE *fp)
141 {
142   int i;
143 
144   if (!sint) return(0);
145   for (i=sint->currentsize-2; i>=0; i--) {
146     fprintf(fp,"      [%d]  %s() line %d in %s%s\n",PetscGlobalRank,sint->function[i],sint->line[i],sint->directory[i],sint->file[i]);
147   }
148   return 0;
149 }
150 
151 #else
152 #undef __FUNCT__
153 #define __FUNCT__ "PetscStackPublish"
154 PetscErrorCode  PetscStackPublish(void)
155 {
156   PetscFunctionBegin;
157   PetscFunctionReturn(0);
158 }
159 #undef __FUNCT__
160 #define __FUNCT__ "PetscStackDepublish"
161 PetscErrorCode  PetscStackDepublish(void)
162 {
163   PetscFunctionBegin;
164   PetscFunctionReturn(0);
165 }
166 #undef __FUNCT__
167 #define __FUNCT__ "PetscStackCreate"
168 PetscErrorCode  PetscStackCreate(void)
169 {
170   PetscFunctionBegin;
171   PetscFunctionReturn(0);
172 }
173 #undef __FUNCT__
174 #define __FUNCT__ "PetscStackView"
175 PetscErrorCode  PetscStackView(PetscViewer viewer)
176 {
177   PetscFunctionBegin;
178   PetscFunctionReturn(0);
179 }
180 #undef __FUNCT__
181 #define __FUNCT__ "PetscStackDestroy"
182 PetscErrorCode  PetscStackDestroy(void)
183 {
184   PetscFunctionBegin;
185   PetscFunctionReturn(0);
186 }
187 
188 #endif
189 
190