xref: /petsc/src/mat/tutorials/ex7.c (revision 66af8762ec03dbef0e079729eb2a1734a35ed7ff)
1 static char help[] = "Example use of PetscInfo() as a configurable informative logging or warning tool\n";
2 
3 #include <petscsys.h>
4 #include <petscmat.h>
5 #include <petscvec.h>
6 
7 int main(int argc, char **argv)
8 {
9   Mat A, Aself;
10   Vec b, bself;
11 #if defined(PETSC_USE_INFO)
12   PetscInt testarg = 1234;
13 #endif
14   int          numClasses;
15   PetscClassId testMatClassid, testVecClassid, testSysClassid;
16   PetscBool    isEnabled = PETSC_FALSE, invert = PETSC_FALSE;
17   char        *testClassesStr, *filename;
18   const char  *testMatClassname, *testVecClassname;
19   char       **testClassesStrArr;
20   FILE        *infoFile;
21 
22   PetscFunctionBeginUser;
23   PetscCall(PetscInitialize(&argc, &argv, (char *)0, help));
24 
25   /*
26      Examples on how to call PetscInfo() using different objects with or without arguments, and different communicators.
27       - Until PetscInfoDestroy() is called all PetscInfo() behaviour is goverened by command line options, which
28         are processed during PetscInitialize().
29   */
30   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
31   PetscCall(VecCreate(PETSC_COMM_WORLD, &b));
32 
33   PetscCall(PetscInfo(A, "Mat info on PETSC_COMM_WORLD with no arguments\n"));
34   PetscCall(PetscInfo(A, "Mat info on PETSC_COMM_WORLD with 1 argument equal to 1234: %" PetscInt_FMT "\n", testarg));
35   PetscCall(PetscInfo(b, "Vec info on PETSC_COMM_WORLD with no arguments\n"));
36   PetscCall(PetscInfo(b, "Vec info on PETSC_COMM_WORLD with 1 argument equal to 1234: %" PetscInt_FMT "\n", testarg));
37   PetscCall(PetscInfo(NULL, "Sys info on PETSC_COMM_WORLD with no arguments\n"));
38   PetscCall(PetscInfo(NULL, "Sys info on PETSC_COMM_WORLD with 1 argument equal to 1234: %" PetscInt_FMT "\n", testarg));
39 
40   PetscCall(MatCreate(PETSC_COMM_SELF, &Aself));
41   PetscCall(VecCreate(PETSC_COMM_SELF, &bself));
42 
43   PetscCall(PetscInfo(Aself, "Mat info on PETSC_COMM_SELF with no arguments\n"));
44   PetscCall(PetscInfo(Aself, "Mat info on PETSC_COMM_SELF with 1 argument equal to 1234: %" PetscInt_FMT "\n", testarg));
45   PetscCall(PetscInfo(bself, "Vec info on PETSC_COMM_SELF with no arguments\n"));
46   PetscCall(PetscInfo(bself, "Vec info on PETSC_COMM_SELF with 1 argument equal to 1234: %" PetscInt_FMT "\n", testarg));
47   PetscCall(PetscInfo(NULL, "Sys info on PETSC_COMM_SELF with no arguments\n"));
48   PetscCall(PetscInfo(NULL, "Sys info on PETSC_COMM_SELF with 1 argument equal to 1234: %" PetscInt_FMT "\n", testarg));
49 
50   PetscCall(MatDestroy(&Aself));
51   PetscCall(VecDestroy(&bself));
52   /*
53      First retrieve some basic information regarding the classes for which we want to filter
54   */
55   PetscCall(PetscObjectGetClassId((PetscObject)A, &testMatClassid));
56   PetscCall(PetscObjectGetClassId((PetscObject)b, &testVecClassid));
57   /* Sys class has PetscClassId = PETSC_SMALLEST_CLASSID */
58   testSysClassid = PETSC_SMALLEST_CLASSID;
59   PetscCall(PetscObjectGetClassName((PetscObject)A, &testMatClassname));
60   PetscCall(PetscObjectGetClassName((PetscObject)b, &testVecClassname));
61 
62   /*
63      Examples on how to use individual PetscInfo() commands.
64   */
65   PetscCall(PetscInfoEnabled(testMatClassid, &isEnabled));
66   if (isEnabled) PetscCall(PetscInfo(A, "Mat info is enabled\n"));
67   PetscCall(PetscInfoEnabled(testVecClassid, &isEnabled));
68   if (isEnabled) PetscCall(PetscInfo(b, "Vec info is enabled\n"));
69   PetscCall(PetscInfoEnabled(testSysClassid, &isEnabled));
70   if (isEnabled) PetscCall(PetscInfo(NULL, "Sys info is enabled\n"));
71 
72   /* Retrieve filename to append later entries to */
73   PetscCall(PetscInfoGetFile(&filename, &infoFile));
74 
75   /*
76      Destroy existing PetscInfo() configuration and reset all internal flags to default values. This allows the user to change filters
77      midway through a program.
78   */
79   PetscCall(PetscInfoDestroy());
80 
81   /*
82      Test if existing filters are reset.
83       - Note these should NEVER print.
84   */
85   PetscCall(PetscInfoEnabled(testMatClassid, &isEnabled));
86   if (isEnabled) PetscCall(PetscInfo(A, "Mat info is enabled after PetscInfoDestroy\n"));
87   PetscCall(PetscInfoEnabled(testVecClassid, &isEnabled));
88   if (isEnabled) PetscCall(PetscInfo(b, "Vec info is enabled after PetscInfoDestroy\n"));
89   PetscCall(PetscInfoEnabled(testSysClassid, &isEnabled));
90   if (isEnabled) PetscCall(PetscInfo(NULL, "Sys info is enabled after PetscInfoDestroy\n"));
91 
92   /*
93      Reactivate PetscInfo() printing in one of two ways.
94       - First we must reactivate PetscInfo() printing as a whole.
95       - Keep in mind that by default ALL classes are allowed to print if PetscInfo() is enabled, so we deactivate
96         relevant classes first to demonstrate activation functionality.
97   */
98   PetscCall(PetscInfoAllow(PETSC_TRUE));
99   PetscCall(PetscInfoSetFile(filename, "a"));
100   PetscCall(PetscInfoDeactivateClass(testMatClassid));
101   PetscCall(PetscInfoDeactivateClass(testVecClassid));
102   PetscCall(PetscInfoDeactivateClass(testSysClassid));
103 
104   /*
105      Activate PetscInfo() on a per-class basis
106   */
107   PetscCall(PetscInfoActivateClass(testMatClassid));
108   PetscCall(PetscInfo(A, "Mat info is enabled again through PetscInfoActivateClass\n"));
109   PetscCall(PetscInfoDeactivateClass(testMatClassid));
110   PetscCall(PetscInfoActivateClass(testVecClassid));
111   PetscCall(PetscInfo(b, "Vec info is enabled again through PetscInfoActivateClass\n"));
112   PetscCall(PetscInfoDeactivateClass(testVecClassid));
113   PetscCall(PetscInfoActivateClass(testSysClassid));
114   PetscCall(PetscInfo(NULL, "Sys info is enabled again through PetscInfoActivateClass\n"));
115   PetscCall(PetscInfoDeactivateClass(testVecClassid));
116 
117   /*
118      Activate PetscInfo() by specifying specific classnames to activate
119   */
120   PetscCall(PetscStrallocpy("mat,vec,sys", &testClassesStr));
121   PetscCall(PetscStrToArray((const char *)testClassesStr, ',', &numClasses, &testClassesStrArr));
122   PetscCall(PetscInfoSetClasses(invert, (PetscInt)numClasses, (const char *const *)testClassesStrArr));
123   PetscCall(PetscInfoProcessClass(testMatClassname, 1, &testMatClassid));
124   PetscCall(PetscInfoProcessClass(testVecClassname, 1, &testVecClassid));
125   PetscCall(PetscInfoProcessClass("sys", 1, &testSysClassid));
126 
127   PetscCall(PetscInfo(A, "Mat info is enabled again through PetscInfoSetClasses\n"));
128   PetscCall(PetscInfo(b, "Vec info is enabled again through PetscInfoSetClasses\n"));
129   PetscCall(PetscInfo(NULL, "Sys info is enabled again through PetscInfoSetClasses\n"));
130 
131   PetscCall(PetscStrToArrayDestroy(numClasses, testClassesStrArr));
132   PetscCall(PetscFree(testClassesStr));
133 
134   /*
135      Activate PetscInfo() with an inverted filter selection.
136       - Inverting our selection of filters enables PetscInfo() for all classes EXCEPT those specified.
137       - Note we must reset PetscInfo() internal flags with PetscInfoDestroy() as invoking PetscInfoProcessClass() locks filters in place.
138   */
139   PetscCall(PetscInfoDestroy());
140   PetscCall(PetscInfoAllow(PETSC_TRUE));
141   PetscCall(PetscInfoSetFile(filename, "a"));
142   PetscCall(PetscStrallocpy("vec,sys", &testClassesStr));
143   PetscCall(PetscStrToArray((const char *)testClassesStr, ',', &numClasses, &testClassesStrArr));
144   invert = PETSC_TRUE;
145   PetscCall(PetscInfoSetClasses(invert, (PetscInt)numClasses, (const char *const *)testClassesStrArr));
146   PetscCall(PetscInfoProcessClass(testMatClassname, 1, &testMatClassid));
147   PetscCall(PetscInfoProcessClass(testVecClassname, 1, &testVecClassid));
148   PetscCall(PetscInfoProcessClass("sys", 1, &testSysClassid));
149 
150   /*
151      Here only the Mat() call will successfully print.
152   */
153   PetscCall(PetscInfo(A, "Mat info is enabled again through inverted PetscInfoSetClasses\n"));
154   PetscCall(PetscInfo(b, "Vec info is enabled again through PetscInfoSetClasses\n"));
155   PetscCall(PetscInfo(NULL, "Sys info is enabled again through PetscInfoSetClasses\n"));
156 
157   PetscCall(PetscStrToArrayDestroy(numClasses, testClassesStrArr));
158   PetscCall(PetscFree(testClassesStr));
159   PetscCall(PetscFree(filename));
160   PetscCall(MatDestroy(&A));
161   PetscCall(VecDestroy(&b));
162   PetscCall(PetscFinalize());
163   return 0;
164 }
165 
166 /*TEST
167 
168    test:
169       requires: defined(PETSC_USE_INFO) !defined(PETSC_HAVE_THREADSAFETY)
170       suffix: 1
171       args: -info
172       filter: grep -h -ve Running -ve communicator -ve MPI_Comm -ve OpenMP -ve PetscGetHostName -ve PetscSetFPTrap -ve PetscDetermineInitialFPTrap -ve libpetscbamg -ve PetscDeviceContext -ve PetscDeviceType -ve PetscDeviceInitializeTypeFromOptions_Private
173 
174    test:
175       requires: defined(PETSC_USE_INFO) !defined(PETSC_HAVE_THREADSAFETY)
176       suffix: 2
177       args: -info ex7info.2
178       filter: grep -h -ve Running -ve communicator -ve MPI_Comm -ve OpenMP -ve PetscGetHostName -ve PetscSetFPTrap -ve PetscDetermineInitialFPTrap -ve libpetscbamg -ve PetscDeviceContext -ve PetscDeviceType -ve PetscDeviceInitializeTypeFromOptions_Private "ex7info.2.0"
179 
180    test:
181       requires: defined(PETSC_USE_INFO) !defined(PETSC_HAVE_THREADSAFETY)
182       suffix: 3
183       nsize: 2
184       args: -info ex7info.3
185       filter: grep -h -ve Running -ve communicator -ve MPI_Comm -ve OpenMP -ve PetscGetHostName -ve PetscSetFPTrap -ve PetscDetermineInitialFPTrap -ve libpetscbamg -ve PetscDeviceContext -ve PetscDeviceType -ve PetscDeviceInitializeTypeFromOptions_Private "ex7info.3.0" | sort -b
186 
187    test:
188       requires: defined(PETSC_USE_INFO)
189       suffix: 4
190       args: -info :mat,vec:
191       filter: grep -h -ve Running -ve communicator -ve MPI_Comm -ve OpenMP -ve PetscGetHostName -ve PetscSetFPTrap -ve PetscDetermineInitialFPTrap
192 
193    test:
194       requires: defined(PETSC_USE_INFO)
195       suffix: 5
196       args: -info :~sys:
197       filter: grep -h  -ve PetscSetFPTrap -ve PetscDetermineInitialFPTrap
198 
199    test:
200       requires: defined(PETSC_USE_INFO)
201       suffix: 6
202       nsize: 2
203       args: -info ex7info.6:mat:self
204       filter: grep -h "ex7info.6.0" | sort -b
205 
206    test:
207       requires: defined(PETSC_USE_INFO)
208       suffix: 7
209       nsize: 2
210       args: -info ex7info.7:mat:~self
211       filter: grep -h "ex7info.7.0" | sort -b
212 
213 TEST*/
214