xref: /petsc/src/sys/error/pstack.c (revision f23aa3dd738493dcb3a70a8c0c7f5454aa9150c2)
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(FILE *file)
56 {
57   int            i;
58   PetscStack     *petscstackp;
59 
60   petscstackp = (PetscStack*)PetscThreadLocalGetValue(petscstack);
61   if (!file) file = PETSC_STDOUT;
62 
63   if (file == PETSC_STDOUT) {
64     (*PetscErrorPrintf)("Note: The EXACT line numbers in the stack are not available,\n");
65     (*PetscErrorPrintf)("      INSTEAD the line number of the start of the function\n");
66     (*PetscErrorPrintf)("      is given.\n");
67     for (i=petscstackp->currentsize-1; i>=0; i--) {
68       (*PetscErrorPrintf)("[%d] %s line %d %s%s\n",PetscGlobalRank,petscstackp->function[i],petscstackp->line[i],petscstackp->directory[i],petscstackp->file[i]);
69     }
70   } else {
71     fprintf(file,"Note: The EXACT line numbers in the stack are not available,\n");
72     fprintf(file,"      INSTEAD the line number of the start of the function\n");
73     fprintf(file,"      is given.\n");
74     for (i=petscstackp->currentsize-1; i>=0; i--) {
75       fprintf(file,"[%d] %s line %d %s%s\n",PetscGlobalRank,petscstackp->function[i],petscstackp->line[i],petscstackp->directory[i],petscstackp->file[i]);
76     }
77   }
78   return 0;
79 }
80 
81 PetscErrorCode PetscStackDestroy_kernel(PetscInt trank)
82 {
83   if (PetscStackActive) {
84     PetscStack *petscstack_in;
85     petscstack_in = (PetscStack*)PetscThreadLocalGetValue(petscstack);
86     free(petscstack_in);
87     PetscThreadLocalSetValue(petscstack,(PetscStack*)0);
88   }
89   return 0;
90 }
91 
92 #undef __FUNCT__
93 #define __FUNCT__ "PetscStackDestroy"
94 /*  PetscFunctionBegin;  so that make rule checkbadPetscFunctionBegin works */
95 PetscErrorCode  PetscStackDestroy(void)
96 {
97   PetscErrorCode ierr;
98   ierr = PetscThreadCommRunKernel0(PETSC_COMM_SELF,(PetscThreadKernel)PetscStackDestroy_kernel);CHKERRQ(ierr);
99   ierr = PetscThreadCommBarrier(PETSC_COMM_SELF);CHKERRQ(ierr);
100   PetscThreadLocalDestroy(petscstack); /* Deletes pthread_key if it was used */
101   return 0;
102 }
103 
104 #undef __FUNCT__
105 #define __FUNCT__ "PetscStackCopy"
106 /*  PetscFunctionBegin;  so that make rule checkbadPetscFunctionBegin works */
107 PetscErrorCode  PetscStackCopy(PetscStack* sint,PetscStack* sout)
108 {
109   int i;
110 
111   if (!sint) {
112     sout->currentsize = 0;
113   } else {
114     for (i=0; i<sint->currentsize; i++) {
115       sout->function[i]     = sint->function[i];
116       sout->file[i]         = sint->file[i];
117       sout->directory[i]    = sint->directory[i];
118       sout->line[i]         = sint->line[i];
119       sout->petscroutine[i] = sint->petscroutine[i];
120     }
121     sout->currentsize = sint->currentsize;
122   }
123   return 0;
124 }
125 
126 #undef __FUNCT__
127 #define __FUNCT__ "PetscStackPrint"
128 /*  PetscFunctionBegin;  so that make rule checkbadPetscFunctionBegin works */
129 PetscErrorCode  PetscStackPrint(PetscStack* sint,FILE *fp)
130 {
131   int i;
132 
133   if (!sint) return(0);
134   for (i=sint->currentsize-2; i>=0; i--) {
135     fprintf(fp,"      [%d]  %s() line %d in %s%s\n",PetscGlobalRank,sint->function[i],sint->line[i],sint->directory[i],sint->file[i]);
136   }
137   return 0;
138 }
139 
140 #else
141 #undef __FUNCT__
142 #define __FUNCT__ "PetscStackPublish"
143 PetscErrorCode  PetscStackPublish(void)
144 {
145   PetscFunctionBegin;
146   PetscFunctionReturn(0);
147 }
148 #undef __FUNCT__
149 #define __FUNCT__ "PetscStackDepublish"
150 PetscErrorCode  PetscStackDepublish(void)
151 {
152   PetscFunctionBegin;
153   PetscFunctionReturn(0);
154 }
155 #undef __FUNCT__
156 #define __FUNCT__ "PetscStackCreate"
157 PetscErrorCode  PetscStackCreate(void)
158 {
159   PetscFunctionBegin;
160   PetscFunctionReturn(0);
161 }
162 #undef __FUNCT__
163 #define __FUNCT__ "PetscStackView"
164 PetscErrorCode  PetscStackView(FILE *file)
165 {
166   PetscFunctionBegin;
167   PetscFunctionReturn(0);
168 }
169 #undef __FUNCT__
170 #define __FUNCT__ "PetscStackDestroy"
171 PetscErrorCode  PetscStackDestroy(void)
172 {
173   PetscFunctionBegin;
174   PetscFunctionReturn(0);
175 }
176 
177 #endif
178 
179