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