Actual source code: ex7.c
1: static char help[] = "Example use of PetscInfo() as a configurable informative logging or warning tool\n";
3: #include <petscsys.h>
4: #include <petscmat.h>
5: #include <petscvec.h>
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;
23: PetscInitialize(&argc, &argv, (char *)0, help);
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: MatCreate(PETSC_COMM_WORLD, &A);
31: VecCreate(PETSC_COMM_WORLD, &b);
33: PetscInfo(A, "Mat info on PETSC_COMM_WORLD with no arguments\n");
34: PetscInfo(A, "Mat info on PETSC_COMM_WORLD with 1 argument equal to 1234: %" PetscInt_FMT "\n", testarg);
35: PetscInfo(b, "Vec info on PETSC_COMM_WORLD with no arguments\n");
36: PetscInfo(b, "Vec info on PETSC_COMM_WORLD with 1 argument equal to 1234: %" PetscInt_FMT "\n", testarg);
37: PetscInfo(NULL, "Sys info on PETSC_COMM_WORLD with no arguments\n");
38: PetscInfo(NULL, "Sys info on PETSC_COMM_WORLD with 1 argument equal to 1234: %" PetscInt_FMT "\n", testarg);
40: MatCreate(PETSC_COMM_SELF, &Aself);
41: VecCreate(PETSC_COMM_SELF, &bself);
43: PetscInfo(Aself, "Mat info on PETSC_COMM_SELF with no arguments\n");
44: PetscInfo(Aself, "Mat info on PETSC_COMM_SELF with 1 argument equal to 1234: %" PetscInt_FMT "\n", testarg);
45: PetscInfo(bself, "Vec info on PETSC_COMM_SELF with no arguments\n");
46: PetscInfo(bself, "Vec info on PETSC_COMM_SELF with 1 argument equal to 1234: %" PetscInt_FMT "\n", testarg);
47: PetscInfo(NULL, "Sys info on PETSC_COMM_SELF with no arguments\n");
48: PetscInfo(NULL, "Sys info on PETSC_COMM_SELF with 1 argument equal to 1234: %" PetscInt_FMT "\n", testarg);
50: MatDestroy(&Aself);
51: VecDestroy(&bself);
52: /*
53: First retrieve some basic information regarding the classes for which we want to filter
54: */
55: PetscObjectGetClassId((PetscObject)A, &testMatClassid);
56: PetscObjectGetClassId((PetscObject)b, &testVecClassid);
57: /* Sys class has PetscClassId = PETSC_SMALLEST_CLASSID */
58: testSysClassid = PETSC_SMALLEST_CLASSID;
59: PetscObjectGetClassName((PetscObject)A, &testMatClassname);
60: PetscObjectGetClassName((PetscObject)b, &testVecClassname);
62: /*
63: Examples on how to use individual PetscInfo() commands.
64: */
65: PetscInfoEnabled(testMatClassid, &isEnabled);
66: if (isEnabled) PetscInfo(A, "Mat info is enabled\n");
67: PetscInfoEnabled(testVecClassid, &isEnabled);
68: if (isEnabled) PetscInfo(b, "Vec info is enabled\n");
69: PetscInfoEnabled(testSysClassid, &isEnabled);
70: if (isEnabled) PetscInfo(NULL, "Sys info is enabled\n");
72: /* Retrieve filename to append later entries to */
73: PetscInfoGetFile(&filename, &infoFile);
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: PetscInfoDestroy();
81: /*
82: Test if existing filters are reset.
83: - Note these should NEVER print.
84: */
85: PetscInfoEnabled(testMatClassid, &isEnabled);
86: if (isEnabled) PetscInfo(A, "Mat info is enabled after PetscInfoDestroy\n");
87: PetscInfoEnabled(testVecClassid, &isEnabled);
88: if (isEnabled) PetscInfo(b, "Vec info is enabled after PetscInfoDestroy\n");
89: PetscInfoEnabled(testSysClassid, &isEnabled);
90: if (isEnabled) PetscInfo(NULL, "Sys info is enabled after PetscInfoDestroy\n");
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: PetscInfoAllow(PETSC_TRUE);
99: PetscInfoSetFile(filename, "a");
100: PetscInfoDeactivateClass(testMatClassid);
101: PetscInfoDeactivateClass(testVecClassid);
102: PetscInfoDeactivateClass(testSysClassid);
104: /*
105: Activate PetscInfo() on a per-class basis
106: */
107: PetscInfoActivateClass(testMatClassid);
108: PetscInfo(A, "Mat info is enabled again through PetscInfoActivateClass\n");
109: PetscInfoDeactivateClass(testMatClassid);
110: PetscInfoActivateClass(testVecClassid);
111: PetscInfo(b, "Vec info is enabled again through PetscInfoActivateClass\n");
112: PetscInfoDeactivateClass(testVecClassid);
113: PetscInfoActivateClass(testSysClassid);
114: PetscInfo(NULL, "Sys info is enabled again through PetscInfoActivateClass\n");
115: PetscInfoDeactivateClass(testVecClassid);
117: /*
118: Activate PetscInfo() by specifying specific classnames to activate
119: */
120: PetscStrallocpy("mat,vec,sys", &testClassesStr);
121: PetscStrToArray((const char *)testClassesStr, ',', &numClasses, &testClassesStrArr);
122: PetscInfoSetClasses(invert, (PetscInt)numClasses, (const char *const *)testClassesStrArr);
123: PetscInfoProcessClass(testMatClassname, 1, &testMatClassid);
124: PetscInfoProcessClass(testVecClassname, 1, &testVecClassid);
125: PetscInfoProcessClass("sys", 1, &testSysClassid);
127: PetscInfo(A, "Mat info is enabled again through PetscInfoSetClasses\n");
128: PetscInfo(b, "Vec info is enabled again through PetscInfoSetClasses\n");
129: PetscInfo(NULL, "Sys info is enabled again through PetscInfoSetClasses\n");
131: PetscStrToArrayDestroy(numClasses, testClassesStrArr);
132: PetscFree(testClassesStr);
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: PetscInfoDestroy();
140: PetscInfoAllow(PETSC_TRUE);
141: PetscInfoSetFile(filename, "a");
142: PetscStrallocpy("vec,sys", &testClassesStr);
143: PetscStrToArray((const char *)testClassesStr, ',', &numClasses, &testClassesStrArr);
144: invert = PETSC_TRUE;
145: PetscInfoSetClasses(invert, (PetscInt)numClasses, (const char *const *)testClassesStrArr);
146: PetscInfoProcessClass(testMatClassname, 1, &testMatClassid);
147: PetscInfoProcessClass(testVecClassname, 1, &testVecClassid);
148: PetscInfoProcessClass("sys", 1, &testSysClassid);
150: /*
151: Here only the Mat() call will successfully print.
152: */
153: PetscInfo(A, "Mat info is enabled again through inverted PetscInfoSetClasses\n");
154: PetscInfo(b, "Vec info is enabled again through PetscInfoSetClasses\n");
155: PetscInfo(NULL, "Sys info is enabled again through PetscInfoSetClasses\n");
157: PetscStrToArrayDestroy(numClasses, testClassesStrArr);
158: PetscFree(testClassesStr);
159: PetscFree(filename);
160: MatDestroy(&A);
161: VecDestroy(&b);
162: PetscFinalize();
163: return 0;
164: }
166: /*TEST
168: test:
169: requires: defined(PETSC_USE_INFO)
170: suffix: 1
171: args: -info
172: filter: grep -h -ve Running -ve communicator -ve MPI_Comm -ve OpenMP -ve PetscGetHostName -ve PetscDetermineInitialFPTrap -ve libpetscbamg -ve PetscDeviceContext -ve PetscDeviceType -ve PetscDeviceInitializeTypeFromOptions_Private
174: test:
175: requires: defined(PETSC_USE_INFO)
176: suffix: 2
177: args: -info ex7info.2
178: filter: grep -h -ve Running -ve communicator -ve MPI_Comm -ve OpenMP -ve PetscGetHostName -ve PetscDetermineInitialFPTrap -ve libpetscbamg -ve PetscDeviceContext -ve PetscDeviceType -ve PetscDeviceInitializeTypeFromOptions_Private "ex7info.2.0"
180: test:
181: requires: defined(PETSC_USE_INFO)
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 PetscDetermineInitialFPTrap -ve libpetscbamg -ve PetscDeviceContext -ve PetscDeviceType -ve PetscDeviceInitializeTypeFromOptions_Private "ex7info.3.0" | sort -b
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 PetscDetermineInitialFPTrap
193: test:
194: requires: defined(PETSC_USE_INFO)
195: suffix: 5
196: args: -info :~sys:
197: filter: grep -h -ve PetscDetermineInitialFPTrap
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
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
213: TEST*/