Actual source code: ex270.c
1: #include <petscmat.h>
3: static const char help[] = "Test MatGetValue/Row for hypre matrix on device\n";
5: static PetscErrorCode CheckBandedMatrix(MPI_Comm comm, PetscInt N, PetscInt bw, const char fill[])
6: {
7: Mat A;
8: PetscInt rstart, rend;
9: PetscBool iscoo, isconvert, ok = PETSC_TRUE;
10: PetscScalar expected;
12: PetscFunctionBeginUser;
13: PetscCall(PetscStrcmp(fill, "coo", &iscoo));
14: PetscCall(PetscStrcmp(fill, "convert", &isconvert));
16: PetscCall(MatCreate(comm, &A));
17: PetscCall(MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, N, N));
18: PetscCall(MatSetType(A, isconvert ? MATAIJ : MATHYPRE));
19: PetscCall(MatSeqAIJSetPreallocation(A, 2 * bw + 1, NULL));
20: PetscCall(MatMPIAIJSetPreallocation(A, 2 * bw + 1, NULL, 2 * bw + 1, NULL));
21: PetscCall(MatSetUp(A));
22: PetscCall(MatGetOwnershipRange(A, &rstart, &rend));
24: if (iscoo) {
25: PetscCount ncoo = 0;
26: PetscInt *coo_i, *coo_j;
27: PetscInt nmax = (rend - rstart) * (2 * bw + 1); /* an upper bound; rows at the ends hold fewer */
28: PetscScalar *coo_v;
30: PetscCall(PetscMalloc3(nmax, &coo_i, nmax, &coo_j, nmax, &coo_v));
31: for (PetscInt i = rstart; i < rend; i++) {
32: for (PetscInt j = PetscMax(0, i - bw); j <= PetscMin(N - 1, i + bw); j++) {
33: coo_i[ncoo] = i;
34: coo_j[ncoo] = j;
35: coo_v[ncoo] = (PetscScalar)(100 * (i + 1) + j + 1);
36: ncoo++;
37: }
38: }
39: PetscCall(MatSetPreallocationCOO(A, ncoo, coo_i, coo_j));
40: PetscCall(MatSetValuesCOO(A, coo_v, INSERT_VALUES));
41: PetscCall(PetscFree3(coo_i, coo_j, coo_v));
42: } else {
43: for (PetscInt i = rstart; i < rend; i++) {
44: for (PetscInt j = PetscMax(0, i - bw); j <= PetscMin(N - 1, i + bw); j++) {
45: expected = (PetscScalar)(100 * (i + 1) + j + 1);
46: PetscCall(MatSetValues(A, 1, &i, 1, &j, &expected, INSERT_VALUES));
47: }
48: }
49: PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
50: PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
51: }
53: if (isconvert) {
54: Mat H;
56: PetscCall(MatConvert(A, MATHYPRE, MAT_INITIAL_MATRIX, &H));
57: PetscCall(MatDestroy(&A));
58: A = H;
59: /* the conversion leaves the matrix on the host; the reordering only matters once it moves */
60: PetscCall(MatBindToCPU(A, PETSC_FALSE));
61: }
63: for (PetscInt i = rstart; i < rend && ok; i++) {
64: const PetscInt *cols;
65: const PetscScalar *rowvals;
66: PetscInt ncols;
68: /* MatGetValues() over the whole row range, so that structural zeros are covered too */
69: for (PetscInt j = 0; j < N; j++) {
70: PetscScalar got;
72: expected = (j >= i - bw && j <= i + bw) ? (PetscScalar)(100 * (i + 1) + j + 1) : 0.0;
73: PetscCall(MatGetValues(A, 1, &i, 1, &j, &got));
74: if (PetscAbsScalar(got - expected) > PETSC_SMALL) ok = PETSC_FALSE;
75: }
77: PetscCall(MatGetRow(A, i, &ncols, &cols, &rowvals));
78: for (PetscInt k = 0; k < ncols; k++) {
79: expected = (PetscScalar)(100 * (i + 1) + cols[k] + 1);
80: if (PetscAbsScalar(rowvals[k] - expected) > PETSC_SMALL) ok = PETSC_FALSE;
81: }
82: PetscCall(MatRestoreRow(A, i, &ncols, &cols, &rowvals));
83: }
85: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &ok, 1, MPI_C_BOOL, MPI_LAND, comm));
86: PetscCall(PetscPrintf(comm, "Banded matrix check (%s fill): %s\n", fill, ok ? "OK" : "FAILED"));
88: PetscCall(MatDestroy(&A));
89: PetscFunctionReturn(PETSC_SUCCESS);
90: }
92: int main(int argc, char **argv)
93: {
94: PetscInt n = 10; // elements
95: PetscReal L = 10.0; // domain length
97: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
99: PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
100: PetscCall(PetscOptionsGetReal(NULL, NULL, "-L", &L, NULL));
102: const PetscInt N = n + 1; // nodes
103: const PetscReal h = L / (PetscReal)n;
105: Mat M;
106: PetscCall(MatCreate(PETSC_COMM_WORLD, &M));
107: PetscCall(MatSetSizes(M, PETSC_DECIDE, PETSC_DECIDE, N, N));
108: PetscCall(MatSetType(M, MATAIJ));
109: PetscCall(MatSetFromOptions(M));
110: // Tridiagonal pattern: up to 3 nnz/row (ends have 2). Use same for on/off diag for simplicity.
111: PetscCall(MatSetUp(M));
112: PetscCall(MatMPIAIJSetPreallocation(M, 3, NULL, 3, NULL));
113: PetscCall(MatSeqAIJSetPreallocation(M, 3, NULL));
115: // Ownership range for rows (nodes)
116: PetscInt rstart, rend;
117: PetscCall(MatGetOwnershipRange(M, &rstart, &rend));
119: // Element matrix (scaled)
120: const PetscScalar s = (PetscScalar)(h / 6.0);
121: const PetscScalar ae[4] = {2 * s, 1 * s, 1 * s, 2 * s};
123: // Assemble by looping over elements that start at locally-owned row i
124: PetscInt idx[2];
125: for (PetscInt i = PetscMax(rstart, 0); i < PetscMin(rend, n); ++i) {
126: idx[0] = i;
127: idx[1] = i + 1;
128: PetscCall(MatSetValues(M, 2, idx, 2, idx, ae, ADD_VALUES));
129: }
131: PetscCall(MatAssemblyBegin(M, MAT_FINAL_ASSEMBLY));
132: PetscCall(MatAssemblyEnd(M, MAT_FINAL_ASSEMBLY));
134: // --- Verification of MatGetRow ------------------------------------------
135: {
136: const PetscReal tol = 100 * PETSC_MACHINE_EPSILON; // tight but safe
137: PetscBool ok = PETSC_TRUE;
139: for (PetscInt i = rstart; i < rend; ++i) {
140: const PetscInt *cols;
141: const PetscScalar *getrowvals;
142: PetscScalar *getvals;
143: PetscInt ncols = 0, expected_ncols = 0;
144: PetscInt expected_cols[3];
145: PetscScalar expected_vals[3];
147: // Build expected data
148: if (i > 0) {
149: expected_cols[expected_ncols] = i - 1;
150: expected_vals[expected_ncols] = s;
151: ++expected_ncols;
152: }
153: expected_cols[expected_ncols] = i;
154: expected_vals[expected_ncols] = (i == 0 || i == N - 1) ? (2 * s) : (4 * s); // diag: h/3 at ends, 2h/3 interior
155: ++expected_ncols;
156: if (i + 1 < N) {
157: expected_cols[expected_ncols] = i + 1;
158: expected_vals[expected_ncols] = s;
159: ++expected_ncols;
160: }
162: PetscCall(PetscMalloc1(expected_ncols, &getvals));
163: PetscCall(MatGetRow(M, i, &ncols, &cols, &getrowvals));
164: PetscCall(MatGetValues(M, 1, &i, expected_ncols, (PetscInt *)expected_cols, getvals));
166: // Compare counts
167: if (ncols != expected_ncols) {
168: ok = PETSC_FALSE;
169: goto rowdone;
170: }
172: // Compare values (match by column)
173: for (PetscInt k = 0; k < ncols; ++k) {
174: PetscInt expected_k = -1;
175: /* Matrix is small. Just do a linear search */
176: for (PetscInt l = 0; l < expected_ncols; ++l) {
177: if (expected_cols[l] == cols[k]) {
178: expected_k = l;
179: break;
180: }
181: }
182: if (expected_k < 0) {
183: ok = PETSC_FALSE;
184: goto rowdone;
185: }
186: if ((PetscAbsScalar(getrowvals[k] - expected_vals[expected_k]) > tol) || (PetscAbsScalar(getvals[expected_k] - expected_vals[expected_k]) > tol)) {
187: ok = PETSC_FALSE;
188: goto rowdone;
189: }
190: }
192: rowdone:
193: PetscCall(MatRestoreRow(M, i, &ncols, &cols, &getrowvals));
194: PetscCall(PetscFree(getvals));
195: if (!ok) break;
196: }
198: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &ok, 1, MPI_C_BOOL, MPI_LAND, PETSC_COMM_WORLD));
199: if (ok) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Mass matrix check: OK\n"));
200: else PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Mass matrix check: FAILED\n"));
201: }
202: // --------------------------------------------------------------------------
204: PetscCall(MatDestroy(&M));
206: {
207: PetscInt bw = 3;
208: PetscBool flg;
209: char fill[16];
211: PetscCall(PetscOptionsGetInt(NULL, NULL, "-bw", &bw, NULL));
212: PetscCall(PetscOptionsGetString(NULL, NULL, "-fill", fill, sizeof(fill), &flg));
213: if (!flg) PetscCall(PetscStrncpy(fill, "setvalues", sizeof(fill)));
214: PetscCall(CheckBandedMatrix(PETSC_COMM_WORLD, N, bw, fill));
215: }
217: PetscCall(PetscFinalize());
218: return 0;
219: }
221: /*TEST
223: test:
224: requires: hypre
225: suffix: 1
226: args: -mat_type hypre
228: test:
229: requires: hypre
230: suffix: 2
231: args: -mat_type hypre
232: nsize: 4
234: test:
235: requires: hypre
236: suffix: coo
237: output_file: output/ex270_coo.out
238: args: -mat_type hypre -fill coo
239: nsize: {{1 4}}
241: test:
242: requires: hypre
243: suffix: convert
244: output_file: output/ex270_convert.out
245: args: -mat_type hypre -fill convert
246: nsize: {{1 4}}
248: TEST*/