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