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