Actual source code: ex277.c

  1: static char help[] = "Tests MatCreateKAIJAB() with a general second operand: K = (A x T) + (B x S).\n\n";

  3: #include <petscmat.h>
  4: #include <petsc/private/petscimpl.h>

  6: // Check K2 = (A \otimes T) + (B \otimes S) against the sum of the two single-operand KAIJ matrices
  7: static PetscErrorCode CheckMult(Mat K2, Mat KAT, Mat KBS, Vec x, Vec y2, Vec yr, Vec ytmp, const char label[])
  8: {
  9:   PetscReal nrm, ynrm, tol = 1.e3 * PETSC_MACHINE_EPSILON;

 11:   PetscFunctionBeginUser;
 12:   for (PetscInt i = 0; i < 10; i++) {
 13:     PetscCall(VecSetRandom(x, NULL));
 14:     PetscCall(MatMult(K2, x, y2));
 15:     PetscCall(MatMult(KAT, x, yr));
 16:     PetscCall(MatMult(KBS, x, ytmp));
 17:     PetscCall(VecAXPY(yr, 1.0, ytmp)); // yr = (A x T)x + (B x S)x
 18:     PetscCall(VecNorm(y2, NORM_2, &ynrm));
 19:     PetscCall(VecAXPY(yr, -1.0, y2));
 20:     PetscCall(VecNorm(yr, NORM_2, &nrm));
 21:     PetscCheck(nrm <= tol * (ynrm + 1.0), PETSC_COMM_WORLD, PETSC_ERR_CONV_FAILED, "MatCreateKAIJAB() MatMult mismatch (%s): ||K2*x - ((AxT)+(BxS))*x|| = %g", label, (double)nrm);
 22:   }
 23:   PetscFunctionReturn(PETSC_SUCCESS);
 24: }

 26: // With B = beta I, the block diagonal must match that of the single-operand KAIJ with S replaced by beta S
 27: static PetscErrorCode CheckInvertBlockDiagonal(Mat A, PetscInt p, PetscInt q, const PetscScalar S[], const PetscScalar T[])
 28: {
 29:   Mat                Bid, Kab, Kref;
 30:   Vec                d;
 31:   PetscScalar       *Sbeta;
 32:   const PetscScalar  beta = 0.5; // exactly representable, so both paths form beta*S identically
 33:   const PetscScalar *dab, *dref;
 34:   PetscReal          tol = 1.e3 * PETSC_MACHINE_EPSILON;
 35:   PetscInt           nblk, rstart, rend;

 37:   PetscFunctionBeginUser;
 38:   PetscCall(MatGetOwnershipRange(A, &rstart, &rend));
 39:   // Bid has A's nonzero structure (hence A's ghost columns) but is numerically beta*I
 40:   PetscCall(MatDuplicate(A, MAT_COPY_VALUES, &Bid));
 41:   PetscCall(MatZeroEntries(Bid));
 42:   PetscCall(MatCreateVecs(Bid, NULL, &d));
 43:   PetscCall(VecSet(d, beta));
 44:   PetscCall(MatDiagonalSet(Bid, d, INSERT_VALUES));
 45:   PetscCall(VecDestroy(&d));

 47:   PetscCall(PetscMalloc1(p * q, &Sbeta));
 48:   for (PetscInt k = 0; k < p * q; k++) Sbeta[k] = beta * S[k];

 50:   PetscCall(MatCreateKAIJAB(A, Bid, p, q, S, T, &Kab));
 51:   PetscCall(MatCreateKAIJ(A, p, q, Sbeta, T, &Kref));
 52:   PetscCall(MatInvertBlockDiagonal(Kab, &dab));
 53:   PetscCall(MatInvertBlockDiagonal(Kref, &dref));
 54:   nblk = p * p * (rend - rstart);
 55:   for (PetscInt k = 0; k < nblk; k++)
 56:     PetscCheck(PetscAbsScalar(dab[k] - dref[k]) <= tol * (1.0 + PetscAbsScalar(dref[k])), PETSC_COMM_SELF, PETSC_ERR_CONV_FAILED, "MatInvertBlockDiagonal() with a general B differs from the equivalent single-operand KAIJ at entry %" PetscInt_FMT, k);

 58:   PetscCall(PetscFree(Sbeta));
 59:   PetscCall(MatDestroy(&Kab));
 60:   PetscCall(MatDestroy(&Kref));
 61:   PetscCall(MatDestroy(&Bid));
 62:   PetscFunctionReturn(PETSC_SUCCESS);
 63: }

 65: int main(int argc, char **argv)
 66: {
 67:   Mat                A, B, K2, KAT, KBS, Bget, Dblk;
 68:   Vec                x, y2, yr, ytmp;
 69:   PetscScalar       *S, *T;
 70:   PetscScalar        vals[3];
 71:   const PetscScalar *dblk1, *dblk2;
 72:   PetscObjectState   st0, st1, nz0, nz1;
 73:   PetscInt           n = 30, nc, p = 3, q = 3, rstart, rend, cols[3], mk, nk, md, nd, row0 = 0;
 74:   PetscBool          bnull = PETSC_FALSE;

 76:   PetscFunctionBeginUser;
 77:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
 78:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
 79:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-p", &p, NULL));
 80:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-q", &q, NULL));
 81:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-b_null", &bnull, NULL));

 83:   // Build a parallel tridiagonal AIJ matrix A (nonzero off-diagonal coupling exercises the MPI ghost path)
 84:   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
 85:   PetscCall(MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, n, n));
 86:   PetscCall(MatSetType(A, MATAIJ));
 87:   PetscCall(MatSeqAIJSetPreallocation(A, 3, NULL));
 88:   PetscCall(MatMPIAIJSetPreallocation(A, 3, NULL, 1, NULL));
 89:   PetscCall(MatGetOwnershipRange(A, &rstart, &rend));
 90:   for (PetscInt i = rstart; i < rend; i++) {
 91:     nc = 0;
 92:     if (i > 0) {
 93:       cols[nc]   = i - 1;
 94:       vals[nc++] = -1.0;
 95:     }
 96:     cols[nc]   = i;
 97:     vals[nc++] = 2.0 + (PetscScalar)i;
 98:     if (i < n - 1) {
 99:       cols[nc]   = i + 1;
100:       vals[nc++] = -1.0 - 0.5 * (PetscScalar)i;
101:     }
102:     PetscCall(MatSetValues(A, 1, &i, nc, cols, vals, INSERT_VALUES));
103:   }
104:   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
105:   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));

107:   // B shares A's nonzero structure but has distinct values
108:   PetscCall(MatDuplicate(A, MAT_COPY_VALUES, &B));
109:   PetscCall(MatScale(B, 0.37));

111:   // Nonsymmetric, diagonally dominant S and T (column-major), so that every block a T + b S is invertible
112:   PetscCall(PetscMalloc2(p * q, &S, p * q, &T));
113:   for (PetscInt i = 0; i < p; i++) {
114:     for (PetscInt j = 0; j < q; j++) {
115:       S[i + p * j] = (i == j ? 2.0 : 0.0) + ((PetscReal)((i + 1) * (j + 2))) / ((PetscReal)(p + q));
116:       T[i + p * j] = (i == j ? 2.0 : 0.0) + ((PetscReal)((p - i) + 2 * j + 1)) / ((PetscReal)(p * q));
117:     }
118:   }

120:   // K2 = (A \otimes T) + (B \otimes S), or (A \otimes T) + (I \otimes S) with -b_null
121:   PetscCall(MatCreateKAIJAB(A, bnull ? NULL : B, p, q, S, T, &K2));
122:   PetscCall(MatKAIJGetB(K2, &Bget));
123:   PetscCheck(Bget == (bnull ? NULL : B), PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatKAIJGetB() did not return the matrix given to MatCreateKAIJAB()");

125:   // MatGetDiagonalBlock() must work before the first MatMult()
126:   PetscCall(MatGetDiagonalBlock(K2, &Dblk));
127:   PetscCall(MatGetLocalSize(K2, &mk, &nk));
128:   PetscCall(MatGetLocalSize(Dblk, &md, &nd));
129:   PetscCheck(md == mk && nd == nk, PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatGetDiagonalBlock() returned a %" PetscInt_FMT " x %" PetscInt_FMT " block, expected %" PetscInt_FMT " x %" PetscInt_FMT, md, nd, mk, nk);

131:   // Reference operators
132:   PetscCall(MatCreateKAIJ(A, p, q, NULL, T, &KAT));            // A \otimes T
133:   if (bnull) PetscCall(MatCreateKAIJ(B, p, q, S, NULL, &KBS)); // I \otimes S (matches identity B)
134:   else PetscCall(MatCreateKAIJ(B, p, q, NULL, S, &KBS));       // B \otimes S

136:   PetscCall(MatCreateVecs(K2, &x, &y2));
137:   PetscCall(VecDuplicate(y2, &yr));
138:   PetscCall(VecDuplicate(y2, &ytmp));

140:   PetscCall(CheckMult(K2, KAT, KBS, x, y2, yr, ytmp, "initial"));

142:   // Reassembling an operand must rebuild the cached submatrices of a MATMPIKAIJ
143:   PetscCall(MatScale(A, 1.3));
144:   PetscCall(CheckMult(K2, KAT, KBS, x, y2, yr, ytmp, "after MatScale(A)"));
145:   PetscCall(MatScale(B, -0.8));
146:   PetscCall(CheckMult(K2, KAT, KBS, x, y2, yr, ytmp, "after MatScale(B)"));

148:   // The reassembly replaced the diagonal block, so fetch it again
149:   PetscCall(MatGetDiagonalBlock(K2, &Dblk));
150:   PetscCall(MatGetLocalSize(Dblk, &md, &nd));
151:   PetscCheck(md == mk && nd == nk, PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatGetDiagonalBlock() returned a %" PetscInt_FMT " x %" PetscInt_FMT " block after reassembly, expected %" PetscInt_FMT " x %" PetscInt_FMT, md, nd, mk, nk);

153:   // Changing S must invalidate the cached submatrices, which hold a copy of S
154:   for (PetscInt i = 0; i < p * q; i++) S[i] *= 1.7;
155:   PetscCall(MatKAIJSetS(K2, p, q, S));
156:   PetscCall(MatDestroy(&KBS));
157:   if (bnull) PetscCall(MatCreateKAIJ(B, p, q, S, NULL, &KBS));
158:   else PetscCall(MatCreateKAIJ(B, p, q, NULL, S, &KBS));
159:   PetscCall(CheckMult(K2, KAT, KBS, x, y2, yr, ytmp, "after MatKAIJSetS"));

161:   if (p == q) PetscCall(CheckInvertBlockDiagonal(A, p, q, S, T));

163:   // The array from MatInvertBlockDiagonal() must survive a rebuild of the cached submatrices
164:   if (p == q) {
165:     PetscCall(MatInvertBlockDiagonal(K2, &dblk1));
166:     // Keep the old submatrix alive, so that a freed buffer cannot be reused at the same address
167:     PetscCall(MatGetDiagonalBlock(K2, &Dblk));
168:     PetscCall(PetscObjectReference((PetscObject)Dblk));
169:     PetscCall(MatScale(A, 1.1));
170:     PetscCall(MatMult(K2, x, y2)); // rebuilds the cached submatrices, releasing the previous ones
171:     PetscCall(MatInvertBlockDiagonal(K2, &dblk2));
172:     PetscCheck(dblk1 == dblk2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatInvertBlockDiagonal() returned an array that a later rebuild of the cached submatrices invalidated");
173:     PetscCall(MatDestroy(&Dblk));
174:     PetscCall(CheckMult(K2, KAT, KBS, x, y2, yr, ytmp, "after MatScale(A) with a cached block diagonal"));
175:   }

177:   // Replacing B with the identity after setup must take effect
178:   PetscCall(MatKAIJSetB(K2, NULL));
179:   PetscCall(MatKAIJGetB(K2, &Bget));
180:   PetscCheck(!Bget, PETSC_COMM_SELF, PETSC_ERR_PLIB, "MatKAIJGetB() did not report the identity after MatKAIJSetB(K, NULL)");
181:   PetscCall(MatDestroy(&KBS));
182:   PetscCall(MatCreateKAIJ(A, p, q, S, NULL, &KBS)); // I \otimes S
183:   PetscCall(CheckMult(K2, KAT, KBS, x, y2, yr, ytmp, "after MatKAIJSetB(K, NULL)"));

185:   // ... and setting it back to a general operand must take effect too
186:   PetscCall(MatKAIJSetB(K2, B));
187:   PetscCall(MatDestroy(&KBS));
188:   PetscCall(MatCreateKAIJ(B, p, q, NULL, S, &KBS)); // B \otimes S
189:   PetscCall(CheckMult(K2, KAT, KBS, x, y2, yr, ytmp, "after MatKAIJSetB(K, B)"));

191:   // Assembling K2 after a value-only change to A must raise its state but not its nonzero state
192:   PetscCall(PetscObjectStateGet((PetscObject)K2, &st0));
193:   PetscCall(MatGetNonzeroState(K2, &nz0));
194:   PetscCall(MatScale(A, 2.0)); // value change, same nonzero pattern
195:   PetscCall(MatAssemblyBegin(K2, MAT_FINAL_ASSEMBLY));
196:   PetscCall(MatAssemblyEnd(K2, MAT_FINAL_ASSEMBLY));
197:   PetscCall(PetscObjectStateGet((PetscObject)K2, &st1));
198:   PetscCall(MatGetNonzeroState(K2, &nz1));
199:   PetscCheck(st1 != st0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Assembling the KAIJ matrix after reassembling an operand did not raise its object state");
200:   PetscCheck(nz1 == nz0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "A value-only change to an operand was reported as a nonzero-pattern change of the KAIJ matrix");

202:   // A new nonzero in A must raise the nonzero state of K2, and row 0 has a free preallocated slot for (0, 2)
203:   if (n > 2) {
204:     PetscCall(MatSetOption(A, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE));
205:     if (rstart == 0) {
206:       cols[0] = 2;
207:       vals[0] = 0.25;
208:       PetscCall(MatSetValues(A, 1, &row0, 1, cols, vals, INSERT_VALUES));
209:     }
210:     PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
211:     PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
212:     PetscCall(MatGetNonzeroState(K2, &nz0));
213:     PetscCall(MatAssemblyBegin(K2, MAT_FINAL_ASSEMBLY));
214:     PetscCall(MatAssemblyEnd(K2, MAT_FINAL_ASSEMBLY));
215:     PetscCall(MatGetNonzeroState(K2, &nz1));
216:     PetscCheck(nz1 != nz0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "A new nonzero in an operand was not forwarded to the KAIJ matrix's nonzero state");
217:   }

219:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "MatCreateKAIJAB() MatMult matches (A x T) + (B x S)\n"));

221:   PetscCall(PetscFree2(S, T));
222:   PetscCall(VecDestroy(&x));
223:   PetscCall(VecDestroy(&y2));
224:   PetscCall(VecDestroy(&yr));
225:   PetscCall(VecDestroy(&ytmp));
226:   PetscCall(MatDestroy(&K2));
227:   PetscCall(MatDestroy(&KAT));
228:   PetscCall(MatDestroy(&KBS));
229:   PetscCall(MatDestroy(&A));
230:   PetscCall(MatDestroy(&B));
231:   PetscCall(PetscFinalize());
232:   return 0;
233: }

235: /*TEST

237:   test:
238:     suffix: 1
239:     nsize: {{1 2 4}}
240:     args: -p {{2 3}} -q {{2 3}}

242:   test:
243:     suffix: b_null
244:     nsize: {{1 3}}
245:     args: -b_null -p 3 -q 3

247: TEST*/