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