Actual source code: ex125.c

  1: static char help[] = "Tests MatSolve() and MatMatSolve() (interface to superlu_dist, mumps and mkl_pardiso).\n\
  2: Example: mpiexec -n <np> ./ex125 -f <matrix binary file> -nrhs 4 -mat_solver_type <>\n\n";

  4: /*
  5: -mat_solver_type:
  6:   superlu
  7:   superlu_dist
  8:   mumps
  9:   mkl_pardiso
 10:   cusparse
 11:   petsc
 12: */

 14: #include <petscmat.h>

 16: PetscErrorCode CreateRandom(PetscInt n, PetscInt m, Mat *A)
 17: {
 18:   PetscFunctionBeginUser;
 19:   PetscCall(MatCreate(PETSC_COMM_WORLD, A));
 20:   PetscCall(MatSetType(*A, MATAIJ));
 21:   PetscCall(MatSetFromOptions(*A));
 22:   PetscCall(MatSetSizes(*A, PETSC_DECIDE, PETSC_DECIDE, n, m));
 23:   PetscCall(MatSeqAIJSetPreallocation(*A, 5, NULL));
 24:   PetscCall(MatMPIAIJSetPreallocation(*A, 5, NULL, 5, NULL));
 25:   PetscCall(MatSetRandom(*A, NULL));
 26:   PetscCall(MatAssemblyBegin(*A, MAT_FINAL_ASSEMBLY));
 27:   PetscCall(MatAssemblyEnd(*A, MAT_FINAL_ASSEMBLY));
 28:   PetscFunctionReturn(PETSC_SUCCESS);
 29: }

 31: PetscErrorCode CreateIdentity(PetscInt n, Mat *A)
 32: {
 33:   PetscFunctionBeginUser;
 34:   PetscCall(MatCreate(PETSC_COMM_WORLD, A));
 35:   PetscCall(MatSetType(*A, MATAIJ));
 36:   PetscCall(MatSetFromOptions(*A));
 37:   PetscCall(MatSetSizes(*A, PETSC_DECIDE, PETSC_DECIDE, n, n));
 38:   PetscCall(MatSetUp(*A));
 39:   PetscCall(MatAssemblyBegin(*A, MAT_FINAL_ASSEMBLY));
 40:   PetscCall(MatAssemblyEnd(*A, MAT_FINAL_ASSEMBLY));
 41:   PetscCall(MatShift(*A, 1.0));
 42:   PetscFunctionReturn(PETSC_SUCCESS);
 43: }

 45: int main(int argc, char **args)
 46: {
 47:   Mat           A, Ae, RHS = NULL, RHS1 = NULL, C, F, X;
 48:   Vec           u, x, b;
 49:   PetscMPIInt   size;
 50:   PetscInt      m, n, nfact, nsolve, nrhs;
 51:   PetscReal     norm, tol = 10 * PETSC_SQRT_MACHINE_EPSILON;
 52:   IS            perm = NULL, iperm = NULL;
 53:   MatFactorInfo info;
 54:   PetscRandom   rand;
 55:   PetscBool     flg, symm, testMatSolve = PETSC_TRUE, testMatMatSolve = PETSC_TRUE, testMatMatSolveTranspose = PETSC_TRUE, testMatSolveTranspose = PETSC_TRUE, match = PETSC_FALSE;
 56:   PetscBool     chol = PETSC_FALSE, view = PETSC_FALSE, matsolvexx = PETSC_FALSE, test_inertia;
 57: #if PetscDefined(HAVE_MUMPS)
 58:   PetscBool test_mumps_opts = PETSC_FALSE;
 59: #endif
 60:   PetscViewer fd;                       /* viewer */
 61:   char        file[PETSC_MAX_PATH_LEN]; /* input file name */
 62:   char        pack[PETSC_MAX_PATH_LEN] = "";

 64:   PetscFunctionBeginUser;
 65:   PetscCall(PetscInitialize(&argc, &args, NULL, help));
 66:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));

 68:   /* Determine file from which we read the matrix A */
 69:   PetscCall(PetscOptionsGetString(NULL, NULL, "-f", file, sizeof(file), &flg));
 70:   if (flg) { /* Load matrix A */
 71:     PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, file, FILE_MODE_READ, &fd));
 72:     PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
 73:     PetscCall(MatSetFromOptions(A));
 74:     PetscCall(MatLoad(A, fd));
 75:     PetscCall(PetscViewerDestroy(&fd));
 76:   } else {
 77:     n = 13;
 78:     PetscCall(PetscOptionsGetInt(NULL, NULL, "-n", &n, NULL));
 79:     PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
 80:     PetscCall(MatSetType(A, MATAIJ));
 81:     PetscCall(MatSetFromOptions(A));
 82:     PetscCall(MatSetSizes(A, PETSC_DECIDE, PETSC_DECIDE, n, n));
 83:     PetscCall(MatSetUp(A));
 84:     PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
 85:     PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
 86:     PetscCall(MatShift(A, 1.0));
 87:   }

 89:   /* if A is symmetric, set its flag -- required by MatGetInertia() */
 90:   PetscCall(MatIsSymmetric(A, 0.0, &symm));
 91:   PetscCall(MatSetOption(A, MAT_SYMMETRIC, symm));

 93:   test_inertia = symm;
 94:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-test_inertia", &test_inertia, NULL));

 96:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-cholesky", &chol, NULL));
 97:   PetscCall(PetscOptionsGetReal(NULL, NULL, "-tol", &tol, NULL));

 99:   /* test MATNEST support */
100:   flg = PETSC_FALSE;
101:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-test_nest", &flg, NULL));
102:   if (flg) {
103:     Mat B;

105:     flg = PETSC_FALSE;
106:     PetscCall(PetscOptionsGetBool(NULL, NULL, "-test_nest_bordered", &flg, NULL));
107:     if (!flg) {
108:       Mat mats[9] = {NULL, NULL, A, NULL, A, NULL, A, NULL, NULL};

110:       /* Create a nested matrix representing
111:               | 0 0 A |
112:               | 0 A 0 |
113:               | A 0 0 |
114:       */
115:       PetscCall(MatCreateNest(PETSC_COMM_WORLD, 3, NULL, 3, NULL, mats, &B));
116:       flg = PETSC_TRUE;
117:     } else {
118:       Mat mats[4];

120:       /* Create a nested matrix representing
121:               | Id  R |
122:               | R^t A |
123:       */
124:       PetscCall(MatGetSize(A, NULL, &n));
125:       m = n + 12;
126:       PetscCall(PetscOptionsGetInt(NULL, NULL, "-m", &m, NULL));
127:       PetscCall(CreateIdentity(m, &mats[0]));
128:       PetscCall(CreateRandom(m, n, &mats[1]));
129:       mats[3] = A;

131:       /* use CreateTranspose/CreateHermitianTranspose or explicit matrix for debugging purposes */
132:       flg = PETSC_FALSE;
133:       PetscCall(PetscOptionsGetBool(NULL, NULL, "-expl", &flg, NULL));
134: #if PetscDefined(USE_COMPLEX)
135:       if (chol) { /* Hermitian transpose not supported by MUMPS Cholesky factor */
136:         if (!flg) PetscCall(MatCreateTranspose(mats[1], &mats[2]));
137:         else PetscCall(MatTranspose(mats[1], MAT_INITIAL_MATRIX, &mats[2]));
138:         flg = PETSC_TRUE;
139:       } else {
140:         if (!flg) {
141:           Mat B;

143:           PetscCall(MatDuplicate(mats[1], MAT_COPY_VALUES, &B));
144:           PetscCall(MatCreateHermitianTranspose(B, &mats[2]));
145:           PetscCall(MatDestroy(&B));
146:           if (n == m) {
147:             PetscCall(MatScale(mats[2], PetscCMPLX(4.0, -2.0)));
148:             PetscCall(MatShift(mats[2], PetscCMPLX(-2.0, 1.0))); // mats[2] = (4 - 2i) B* - (2 - i) I
149:             PetscCall(MatCreateHermitianTranspose(mats[2], &B));
150:             PetscCall(MatDestroy(mats + 2));
151:             PetscCall(MatScale(B, 0.5));
152:             PetscCall(MatShift(B, PetscCMPLX(1.0, 0.5)));        // B = 0.5 mats[2]* - (1 - 0.5i) I = (2 + i) B - (1 + 0.5i) I + (1 + 0.5i) I = (2 + i) B
153:             PetscCall(MatCreateHermitianTranspose(B, &mats[2])); // mats[2] = B* = (2 - i) B*
154:             PetscCall(MatDestroy(&B));
155:             PetscCall(MatScale(mats[1], PetscCMPLX(2.0, 1.0))); // mats[1] = (2 + i) B = mats[2]*
156:           } else flg = PETSC_TRUE;
157:         } else PetscCall(MatHermitianTranspose(mats[1], MAT_INITIAL_MATRIX, &mats[2]));
158:       }
159: #else
160:       if (!flg) {
161:         Mat B;

163:         PetscCall(MatDuplicate(mats[1], MAT_COPY_VALUES, &B));
164:         PetscCall(MatCreateTranspose(B, &mats[2]));
165:         PetscCall(MatDestroy(&B));
166:         if (n == m) {
167:           PetscCall(MatScale(mats[2], 4.0));
168:           PetscCall(MatShift(mats[2], -2.0)); // mats[2] = 4 B' - 2 I
169:           PetscCall(MatCreateTranspose(mats[2], &B));
170:           PetscCall(MatDestroy(mats + 2));
171:           PetscCall(MatScale(B, 0.5));
172:           PetscCall(MatShift(B, 1.0));                // B = 0.5 mats[2]' + I = 0.5 (4 B' - 2 I)' + I = 2 B
173:           PetscCall(MatCreateTranspose(B, &mats[2])); // mats[2] = B' = 2 B'
174:           PetscCall(MatDestroy(&B));
175:           PetscCall(MatScale(mats[1], 2.0)); // mats[1] = 2 B = mats[2]'
176:         } else flg = PETSC_TRUE;
177:       } else PetscCall(MatTranspose(mats[1], MAT_INITIAL_MATRIX, &mats[2]));
178: #endif
179:       PetscCall(MatCreateNest(PETSC_COMM_WORLD, 2, NULL, 2, NULL, mats, &B));
180:       PetscCall(MatDestroy(&mats[0]));
181:       PetscCall(MatDestroy(&mats[1]));
182:       PetscCall(MatDestroy(&mats[2]));
183:     }
184:     PetscCall(MatDestroy(&A));
185:     A = B;
186:     PetscCall(MatSetOption(A, MAT_SYMMETRIC, symm));

188:     /* not all the combinations of MatMat operations are supported by MATNEST. */
189:     PetscCall(MatComputeOperator(A, MATAIJ, &Ae));
190:   } else {
191:     PetscCall(PetscObjectReference((PetscObject)A));
192:     Ae  = A;
193:     flg = PETSC_TRUE;
194:   }
195:   PetscCall(MatGetLocalSize(A, &m, &n));
196:   PetscCheck(m == n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "This example is not intended for rectangular matrices (%" PetscInt_FMT ", %" PetscInt_FMT ")", m, n);

198:   PetscCall(MatViewFromOptions(A, NULL, "-A_view"));
199:   PetscCall(MatViewFromOptions(Ae, NULL, "-A_view_expl"));

201:   /* Create dense matrix C and X; C holds true solution with identical columns */
202:   nrhs = 2;
203:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-nrhs", &nrhs, NULL));
204:   PetscCall(PetscPrintf(PETSC_COMM_WORLD, "ex125: nrhs %" PetscInt_FMT "\n", nrhs));
205:   PetscCall(MatCreate(PETSC_COMM_WORLD, &C));
206:   PetscCall(MatSetOptionsPrefix(C, "rhs_"));
207:   PetscCall(MatSetSizes(C, m, PETSC_DECIDE, PETSC_DECIDE, nrhs));
208:   PetscCall(MatSetType(C, MATDENSE));
209:   PetscCall(MatSetFromOptions(C));
210:   PetscCall(MatSetUp(C));

212:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-view_factor", &view, NULL));
213:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-test_matmatsolve", &testMatMatSolve, NULL));
214:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-test_matmatsolvetranspose", &testMatMatSolveTranspose, NULL));
215:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-test_matsolvetranspose", &testMatSolveTranspose, NULL));
216: #if PetscDefined(HAVE_MUMPS)
217:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-test_mumps_opts", &test_mumps_opts, NULL));
218: #endif

220:   PetscCall(PetscRandomCreate(PETSC_COMM_WORLD, &rand));
221:   PetscCall(PetscRandomSetFromOptions(rand));
222:   PetscCall(MatSetRandom(C, rand));
223:   PetscCall(MatDuplicate(C, MAT_DO_NOT_COPY_VALUES, &X));

225:   /* Create vectors */
226:   PetscCall(MatCreateVecs(A, &x, &b));
227:   PetscCall(VecDuplicate(x, &u)); /* save the true solution */

229:   /* Test Factorization */
230:   if (flg) PetscCall(MatGetOrdering(A, MATORDERINGND, &perm, &iperm)); // TODO FIXME: MatConvert_Nest_AIJ() does not support chained MatCreate[Hermitian]Transpose()

232:   PetscCall(PetscOptionsGetString(NULL, NULL, "-mat_solver_type", pack, sizeof(pack), NULL));
233:   if (!pack[0]) PetscCall(PetscStrncpy(pack, MATSOLVERPETSC, sizeof(pack)));

235:   // Get factor matrix F using the solver type from the options database
236:   if (chol == PETSC_TRUE) PetscCall(MatGetFactor(A, pack, MAT_FACTOR_CHOLESKY, &F));
237:   else PetscCall(MatGetFactor(A, pack, MAT_FACTOR_LU, &F));

239:   // Set per-solver flags and print solver name
240: #if PetscDefined(HAVE_SUPERLU)
241:   PetscCall(PetscStrcmp(pack, MATSOLVERSUPERLU, &match));
242:   if (match) {
243:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, " SUPERLU LU:\n"));
244:     matsolvexx = PETSC_FALSE; /* Test MatMatSolve(F,RHS,RHS), RHS is a dense matrix, need further work */
245:   }
246: #endif
247: #if PetscDefined(HAVE_SUPERLU_DIST)
248:   PetscCall(PetscStrcmp(pack, MATSOLVERSUPERLU_DIST, &match));
249:   if (match) {
250:     PetscCall(PetscPrintf(PETSC_COMM_WORLD, " SUPERLU_DIST LU:\n"));
251:     matsolvexx = PETSC_TRUE;
252:     if (symm) { /* A is symmetric */
253:       testMatMatSolveTranspose = PETSC_TRUE;
254:       testMatSolveTranspose    = PETSC_TRUE;
255:     } else { /* superlu_dist does not support solving A^t x = rhs yet */
256:       testMatMatSolveTranspose = PETSC_FALSE;
257:       testMatSolveTranspose    = PETSC_FALSE;
258:     }
259:   }
260: #endif
261: #if PetscDefined(HAVE_MUMPS)
262:   PetscCall(PetscStrcmp(pack, MATSOLVERMUMPS, &match));
263:   if (match) {
264:     if (chol == PETSC_TRUE) PetscCall(PetscPrintf(PETSC_COMM_WORLD, " MUMPS CHOLESKY:\n"));
265:     else PetscCall(PetscPrintf(PETSC_COMM_WORLD, " MUMPS LU:\n"));
266:     matsolvexx = PETSC_TRUE;
267:     if (test_mumps_opts) {
268:       /* test mumps options */
269:       PetscInt  icntl;
270:       PetscReal cntl;

272:       icntl = 2; /* sequential matrix ordering */
273:       PetscCall(MatMumpsSetIcntl(F, 7, icntl));

275:       cntl = 1.e-6; /* threshold for row pivot detection */
276:       PetscCall(MatMumpsSetIcntl(F, 24, 1));
277:       PetscCall(MatMumpsSetCntl(F, 3, cntl));
278:     }
279:   }
280: #endif
281: #if PetscDefined(HAVE_MKL_PARDISO)
282:   PetscCall(PetscStrcmp(pack, MATSOLVERMKL_PARDISO, &match));
283:   if (match) {
284:     if (chol == PETSC_TRUE) PetscCall(PetscPrintf(PETSC_COMM_WORLD, " MKL_PARDISO CHOLESKY:\n"));
285:     else PetscCall(PetscPrintf(PETSC_COMM_WORLD, " MKL_PARDISO LU:\n"));
286:   }
287: #endif
288: #if PetscDefined(HAVE_CUDA)
289:   PetscCall(PetscStrcmp(pack, MATSOLVERCUSPARSE, &match));
290:   if (match) {
291:     if (chol == PETSC_TRUE) PetscCall(PetscPrintf(PETSC_COMM_WORLD, " CUSPARSE CHOLESKY:\n"));
292:     else PetscCall(PetscPrintf(PETSC_COMM_WORLD, " CUSPARSE LU:\n"));
293:     testMatSolveTranspose    = PETSC_FALSE;
294:     testMatMatSolveTranspose = PETSC_FALSE;
295:   }
296: #endif
297: #if PetscDefined(HAVE_CUDA) && PetscDefined(HAVE_CUDSS)
298:   PetscCall(PetscStrcmp(pack, MATSOLVERCUDSS, &match));
299:   if (match == PETSC_TRUE) {
300:     if (chol == PETSC_TRUE) PetscCall(PetscPrintf(PETSC_COMM_WORLD, " cuDSS CHOLESKY:\n"));
301:     else PetscCall(PetscPrintf(PETSC_COMM_WORLD, " cuDSS LU:\n"));
302:     matsolvexx               = PETSC_TRUE;
303:     testMatSolveTranspose    = PETSC_FALSE; // cuDSS does not support MatSolveTranspose()
304:     testMatMatSolveTranspose = PETSC_FALSE;
305:   }
306: #endif
307:   PetscCall(PetscStrcmp(pack, MATSOLVERPETSC, &match));
308:   if (match == PETSC_TRUE) {
309:     if (chol == PETSC_TRUE) PetscCall(PetscPrintf(PETSC_COMM_WORLD, " PETSC CHOLESKY:\n"));
310:     else PetscCall(PetscPrintf(PETSC_COMM_WORLD, " PETSC LU:\n"));
311:     matsolvexx = PETSC_TRUE;
312:   }

314:   PetscCall(MatFactorInfoInitialize(&info));
315:   info.fill      = 5.0;
316:   info.shifttype = (PetscReal)MAT_SHIFT_NONE;
317:   if (chol) {
318:     PetscCall(MatCholeskyFactorSymbolic(F, A, perm, &info));
319:   } else {
320:     PetscCall(MatLUFactorSymbolic(F, A, perm, iperm, &info));
321:   }

323:   for (nfact = 0; nfact < 2; nfact++) {
324:     if (chol) {
325:       PetscCall(PetscPrintf(PETSC_COMM_WORLD, " %" PetscInt_FMT "-the CHOLESKY numfactorization \n", nfact));
326:       PetscCall(MatCholeskyFactorNumeric(F, A, &info));
327:     } else {
328:       PetscCall(PetscPrintf(PETSC_COMM_WORLD, " %" PetscInt_FMT "-the LU numfactorization \n", nfact));
329:       PetscCall(MatLUFactorNumeric(F, A, &info));
330:     }
331:     if (view) {
332:       PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_ASCII_INFO));
333:       PetscCall(MatView(F, PETSC_VIEWER_STDOUT_WORLD));
334:       PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));
335:       view = PETSC_FALSE;
336:     }

338: #if PetscDefined(HAVE_SUPERLU_DIST)
339:     PetscCall(PetscStrcmp(pack, MATSOLVERSUPERLU_DIST, &match));
340:     if (match == PETSC_TRUE) { /* Test MatSuperluDistGetDiagU()
341:        -- input: matrix factor F; output: main diagonal of matrix U on all processes */
342:       PetscInt     M;
343:       PetscScalar *diag;
344:   #if !PetscDefined(USE_COMPLEX)
345:       PetscInt nneg, nzero, npos;
346:   #endif

348:       PetscCall(MatGetSize(F, &M, NULL));
349:       PetscCall(PetscMalloc1(M, &diag));
350:       PetscCall(MatSuperluDistGetDiagU(F, diag));
351:       PetscCall(PetscFree(diag));

353:   #if !PetscDefined(USE_COMPLEX)
354:       /* Test MatGetInertia() */
355:       if (test_inertia) { /* A is symmetric */
356:         PetscCall(MatGetInertia(F, &nneg, &nzero, &npos));
357:         PetscCall(PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD, " MatInertia: nneg: %" PetscInt_FMT ", nzero: %" PetscInt_FMT ", npos: %" PetscInt_FMT "\n", nneg, nzero, npos));
358:       }
359:   #endif
360:     }
361: #endif

363: #if PetscDefined(HAVE_MUMPS)
364:     /* mumps interface allows repeated call of MatCholeskyFactorSymbolic(), while the succession calls do nothing */
365:     PetscCall(PetscStrcmp(pack, MATSOLVERMUMPS, &match));
366:     if (match == PETSC_TRUE) {
367:       if (chol) {
368:         PetscCall(MatCholeskyFactorSymbolic(F, A, perm, &info));
369:         PetscCall(MatCholeskyFactorNumeric(F, A, &info));
370:       } else {
371:         PetscCall(MatLUFactorSymbolic(F, A, perm, iperm, &info));
372:         PetscCall(MatLUFactorNumeric(F, A, &info));
373:       }
374:     }
375: #endif

377:     /* Test MatMatSolve(), A X = B, where B can be dense or sparse */
378:     if (testMatMatSolve) {
379:       if (!nfact) PetscCall(MatMatMult(Ae, C, MAT_INITIAL_MATRIX, 2.0, &RHS));
380:       else PetscCall(MatMatMult(Ae, C, MAT_REUSE_MATRIX, 2.0, &RHS));
381:       for (nsolve = 0; nsolve < 2; nsolve++) {
382:         PetscCall(PetscPrintf(PETSC_COMM_WORLD, "   %" PetscInt_FMT "-the MatMatSolve \n", nsolve));
383:         PetscCall(MatMatSolve(F, RHS, X));

385:         /* Check the error */
386:         PetscCall(MatAXPY(X, -1.0, C, SAME_NONZERO_PATTERN));
387:         PetscCall(MatNorm(X, NORM_FROBENIUS, &norm));
388:         if (norm > tol) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "%" PetscInt_FMT "-the MatMatSolve: Norm of error %g, nsolve %" PetscInt_FMT "\n", nsolve, (double)norm, nsolve));
389:       }

391:       if (matsolvexx) {
392:         /* Test MatMatSolve(F,RHS,RHS), RHS is a dense matrix */
393:         PetscCall(MatCopy(RHS, X, SAME_NONZERO_PATTERN));
394:         PetscCall(MatMatSolve(F, X, X));
395:         /* Check the error */
396:         PetscCall(MatAXPY(X, -1.0, C, SAME_NONZERO_PATTERN));
397:         PetscCall(MatNorm(X, NORM_FROBENIUS, &norm));
398:         if (norm > tol) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "MatMatSolve(F,RHS,RHS): Norm of error %g\n", (double)norm));
399:       }

401:       PetscCall(PetscStrcmp(pack, MATSOLVERMUMPS, &match));
402:       if (match == PETSC_TRUE && size == 1) {
403:         Mat spRHS, spRHST, RHST;

405:         PetscCall(MatTranspose(RHS, MAT_INITIAL_MATRIX, &RHST));
406:         PetscCall(MatConvert(RHST, MATAIJ, MAT_INITIAL_MATRIX, &spRHST));
407:         PetscCall(MatCreateTranspose(spRHST, &spRHS));
408:         for (nsolve = 0; nsolve < 2; nsolve++) {
409:           PetscCall(PetscPrintf(PETSC_COMM_WORLD, "   %" PetscInt_FMT "-the sparse MatMatSolve \n", nsolve));
410:           PetscCall(MatMatSolve(F, spRHS, X));

412:           /* Check the error */
413:           PetscCall(MatAXPY(X, -1.0, C, SAME_NONZERO_PATTERN));
414:           PetscCall(MatNorm(X, NORM_FROBENIUS, &norm));
415:           if (norm > tol) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "%" PetscInt_FMT "-the sparse MatMatSolve: Norm of error %g, nsolve %" PetscInt_FMT "\n", nsolve, (double)norm, nsolve));
416:         }
417:         PetscCall(MatDestroy(&spRHST));
418:         PetscCall(MatDestroy(&spRHS));
419:         PetscCall(MatDestroy(&RHST));
420:       }
421:     }

423:     /* Test testMatMatSolveTranspose(), A^T X = B, where B can be dense or sparse */
424:     if (testMatMatSolveTranspose) {
425:       if (!nfact) {
426:         PetscCall(MatTransposeMatMult(Ae, C, MAT_INITIAL_MATRIX, 2.0, &RHS1));
427:       } else {
428:         PetscCall(MatTransposeMatMult(Ae, C, MAT_REUSE_MATRIX, 2.0, &RHS1));
429:       }

431:       for (nsolve = 0; nsolve < 2; nsolve++) {
432:         PetscCall(PetscPrintf(PETSC_COMM_WORLD, "   %" PetscInt_FMT "-the MatMatSolveTranspose\n", nsolve));
433:         PetscCall(MatMatSolveTranspose(F, RHS1, X));

435:         /* Check the error */
436:         PetscCall(MatAXPY(X, -1.0, C, SAME_NONZERO_PATTERN));
437:         PetscCall(MatNorm(X, NORM_FROBENIUS, &norm));
438:         if (norm > tol) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "%" PetscInt_FMT "-the MatMatSolveTranspose: Norm of error %g, nsolve %" PetscInt_FMT "\n", nsolve, (double)norm, nsolve));
439:       }

441:       PetscCall(PetscStrcmp(pack, MATSOLVERMUMPS, &match));
442:       if (match == PETSC_TRUE && size == 1) {
443:         Mat spRHS, spRHST, RHST;

445:         PetscCall(MatTranspose(RHS1, MAT_INITIAL_MATRIX, &RHST));
446:         PetscCall(MatConvert(RHST, MATAIJ, MAT_INITIAL_MATRIX, &spRHST));
447:         PetscCall(MatCreateTranspose(spRHST, &spRHS));
448:         for (nsolve = 0; nsolve < 2; nsolve++) {
449:           PetscCall(MatMatSolveTranspose(F, spRHS, X));

451:           /* Check the error */
452:           PetscCall(MatAXPY(X, -1.0, C, SAME_NONZERO_PATTERN));
453:           PetscCall(MatNorm(X, NORM_FROBENIUS, &norm));
454:           if (norm > tol) PetscCall(PetscPrintf(PETSC_COMM_WORLD, "%" PetscInt_FMT "-the sparse MatMatSolveTranspose: Norm of error %g, nsolve %" PetscInt_FMT "\n", nsolve, (double)norm, nsolve));
455:         }
456:         PetscCall(MatDestroy(&spRHST));
457:         PetscCall(MatDestroy(&spRHS));
458:         PetscCall(MatDestroy(&RHST));
459:       }
460:     }

462:     /* Test MatSolve() */
463:     if (testMatSolve) {
464:       for (nsolve = 0; nsolve < 2; nsolve++) {
465:         PetscCall(VecSetRandom(x, rand));
466:         PetscCall(VecCopy(x, u));
467:         PetscCall(MatMult(Ae, x, b));

469:         PetscCall(PetscPrintf(PETSC_COMM_WORLD, "   %" PetscInt_FMT "-the MatSolve \n", nsolve));
470:         PetscCall(MatSolve(F, b, x));

472:         /* Check the error */
473:         PetscCall(VecAXPY(u, -1.0, x)); /* u <- (-1.0)x + u */
474:         PetscCall(VecNorm(u, NORM_2, &norm));
475:         if (norm > tol) {
476:           PetscReal resi;
477:           PetscCall(MatMult(Ae, x, u));   /* u = A*x */
478:           PetscCall(VecAXPY(u, -1.0, b)); /* u <- (-1.0)b + u */
479:           PetscCall(VecNorm(u, NORM_2, &resi));
480:           PetscCall(PetscPrintf(PETSC_COMM_WORLD, "MatSolve: Norm of error %g, resi %g, numfact %" PetscInt_FMT "\n", (double)norm, (double)resi, nfact));
481:         }
482:       }
483:     }

485:     /* Test MatSolveTranspose() */
486:     if (testMatSolveTranspose) {
487:       for (nsolve = 0; nsolve < 2; nsolve++) {
488:         PetscCall(VecSetRandom(x, rand));
489:         PetscCall(VecCopy(x, u));
490:         PetscCall(MatMultTranspose(Ae, x, b));

492:         PetscCall(PetscPrintf(PETSC_COMM_WORLD, "   %" PetscInt_FMT "-the MatSolveTranspose\n", nsolve));
493:         PetscCall(MatSolveTranspose(F, b, x));

495:         /* Check the error */
496:         PetscCall(VecAXPY(u, -1.0, x)); /* u <- (-1.0)x + u */
497:         PetscCall(VecNorm(u, NORM_2, &norm));
498:         if (norm > tol) {
499:           PetscReal resi;
500:           PetscCall(MatMultTranspose(Ae, x, u)); /* u = A*x */
501:           PetscCall(VecAXPY(u, -1.0, b));        /* u <- (-1.0)b + u */
502:           PetscCall(VecNorm(u, NORM_2, &resi));
503:           PetscCall(PetscPrintf(PETSC_COMM_WORLD, "MatSolveTranspose: Norm of error %g, resi %g, numfact %" PetscInt_FMT "\n", (double)norm, (double)resi, nfact));
504:         }
505:       }
506:     }
507:   }

509:   /* Free data structures */
510:   PetscCall(MatDestroy(&Ae));
511:   PetscCall(MatDestroy(&A));
512:   PetscCall(MatDestroy(&C));
513:   PetscCall(MatDestroy(&F));
514:   PetscCall(MatDestroy(&X));
515:   PetscCall(MatDestroy(&RHS));
516:   PetscCall(MatDestroy(&RHS1));

518:   PetscCall(PetscRandomDestroy(&rand));
519:   PetscCall(ISDestroy(&perm));
520:   PetscCall(ISDestroy(&iperm));
521:   PetscCall(VecDestroy(&x));
522:   PetscCall(VecDestroy(&b));
523:   PetscCall(VecDestroy(&u));
524:   PetscCall(PetscFinalize());
525:   return 0;
526: }

528: /*TEST

530:    test:
531:       requires: datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
532:       args: -f ${DATAFILESPATH}/matrices/medium -mat_solver_type petsc
533:       output_file: output/ex125.out

535:    test:
536:       suffix: 2
537:       args: -mat_solver_type petsc
538:       output_file: output/ex125.out

540:    test:
541:       suffix: mkl_pardiso
542:       requires: mkl_pardiso datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
543:       args: -f ${DATAFILESPATH}/matrices/small -mat_solver_type mkl_pardiso

545:    test:
546:       suffix: mkl_pardiso_2
547:       requires: mkl_pardiso
548:       args: -mat_solver_type mkl_pardiso
549:       output_file: output/ex125_mkl_pardiso.out

551:    testset:
552:       requires: mumps datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
553:       args: -f ${DATAFILESPATH}/matrices/small -mat_solver_type mumps
554:       output_file: output/ex125_mumps_seq.out

556:       test:
557:         requires: defined(PETSC_HAVE_MUMPS_MIXED_PRECISION)
558:         suffix: mumps_single
559:         args: -pc_precision single -tol 1e-5
560:       test:
561:         suffix: mumps_double
562:         args: -pc_precision double

564:    testset:
565:       requires: mumps datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
566:       args: -f ${DATAFILESPATH}/matrices/small -mat_solver_type mumps -test_nest -test_nest_bordered {{0 1}}
567:       output_file: output/ex125_mumps_seq.out

569:       test:
570:         requires: defined(PETSC_HAVE_MUMPS_MIXED_PRECISION)
571:         suffix: mumps_nest_single
572:         args: -pc_precision single -tol 1e-4
573:       test:
574:         suffix: mumps_nest_double
575:         args: -pc_precision double

577:    testset:
578:       nsize: 3
579:       requires: mumps datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
580:       args: -f ${DATAFILESPATH}/matrices/small -mat_solver_type mumps
581:       output_file: output/ex125_mumps_par.out

583:       test:
584:         requires: defined(PETSC_HAVE_MUMPS_MIXED_PRECISION)
585:         suffix: mumps_2_single
586:         args: -pc_precision single -tol 1e-5
587:       test:
588:         suffix: mumps_2_double
589:         args: -pc_precision double

591:    test:
592:       suffix: mumps_2_nest
593:       nsize: 3
594:       requires: mumps datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
595:       args: -f ${DATAFILESPATH}/matrices/small -mat_solver_type mumps -test_nest -test_nest_bordered {{0 1}}
596:       output_file: output/ex125_mumps_par.out

598:    test:
599:       suffix: mumps_3
600:       requires: mumps
601:       args: -mat_solver_type mumps
602:       output_file: output/ex125_mumps_seq.out

604:    testset:
605:       requires: mumps
606:       args: -mat_solver_type mumps -test_nest -test_nest_bordered {{0 1}}
607:       output_file: output/ex125_mumps_seq.out

609:       test:
610:         requires: !__float128
611:         suffix: mumps_3_nest
612:       test:
613:         suffix: mumps_3_nest_fp128
614:         requires: __float128
615:         args: -tol 1e-8

617:    test:
618:       suffix: mumps_4
619:       nsize: 3
620:       requires: mumps
621:       args: -mat_solver_type mumps
622:       output_file: output/ex125_mumps_par.out

624:    testset:
625:       nsize: 3
626:       requires: mumps
627:       args: -mat_solver_type mumps -test_nest -test_nest_bordered {{0 1}}
628:       output_file: output/ex125_mumps_par.out

630:       test:
631:         requires: !__float128
632:         suffix: mumps_4_nest
633:       test:
634:         suffix: mumps_4_nest_fp128
635:         requires: __float128
636:         args: -tol 1e-8

638:    test:
639:       suffix: mumps_5
640:       nsize: 3
641:       requires: mumps
642:       args: -mat_solver_type mumps -cholesky
643:       output_file: output/ex125_mumps_par_cholesky.out

645:    testset:
646:       nsize: 3
647:       requires: mumps
648:       args: -mat_solver_type mumps -cholesky -test_nest -test_nest_bordered {{0 1}}
649:       output_file: output/ex125_mumps_par_cholesky.out

651:       test:
652:         requires: !__float128
653:         suffix: mumps_5_nest
654:       test:
655:         suffix: mumps_5_nest_fp128
656:         requires: __float128
657:         args: -tol 1e-8

659:    test:
660:       nsize: 2
661:       requires: mumps
662:       args: -mat_solver_type mumps -test_nest -test_nest_bordered -m 13 -n 13
663:       output_file: output/ex125_mumps_par.out

665:       test:
666:         requires: !__float128
667:         suffix: mumps_6
668:       test:
669:         suffix: mumps_6_fp128
670:         requires: __float128
671:         args: -tol 1e-8

673:    test:
674:       suffix: superlu
675:       requires: datafilespath double !complex !defined(PETSC_USE_64BIT_INDICES) superlu
676:       args: -f ${DATAFILESPATH}/matrices/medium -mat_solver_type superlu
677:       output_file: output/ex125_superlu.out

679:    test:
680:       suffix: superlu_dist
681:       nsize: {{1 3}}
682:       requires: datafilespath double !complex !defined(PETSC_USE_64BIT_INDICES) superlu_dist
683:       args: -f ${DATAFILESPATH}/matrices/small -mat_solver_type superlu_dist -mat_superlu_dist_rowperm NOROWPERM
684:       output_file: output/ex125_superlu_dist.out

686:    test:
687:       suffix: superlu_dist_2
688:       nsize: {{1 3}}
689:       requires: superlu_dist !complex
690:       args: -n 36 -mat_solver_type superlu_dist -mat_superlu_dist_rowperm NOROWPERM
691:       output_file: output/ex125_superlu_dist.out

693:    test:
694:       suffix: superlu_dist_3
695:       nsize: {{1 3}}
696:       requires: superlu_dist !complex
697:       requires: datafilespath double !complex !defined(PETSC_USE_64BIT_INDICES) superlu_dist
698:       args: -f ${DATAFILESPATH}/matrices/medium -mat_solver_type superlu_dist -mat_superlu_dist_rowperm NOROWPERM
699:       output_file: output/ex125_superlu_dist_nonsymmetric.out

701:    test:
702:       suffix: superlu_dist_complex
703:       nsize: 3
704:       requires: datafilespath double superlu_dist complex !defined(PETSC_USE_64BIT_INDICES)
705:       args: -f ${DATAFILESPATH}/matrices/farzad_B_rhs -mat_solver_type superlu_dist
706:       output_file: output/ex125_superlu_dist_complex.out

708:    test:
709:       suffix: superlu_dist_complex_2
710:       nsize: 3
711:       requires: superlu_dist complex
712:       args: -mat_solver_type superlu_dist
713:       output_file: output/ex125_superlu_dist_complex_2.out

715:    test:
716:       suffix: cusparse
717:       requires: cuda datafilespath !complex double !defined(PETSC_USE_64BIT_INDICES)
718:       #TODO: fix the bug with cholesky
719:       #args: -mat_type aijcusparse -f ${DATAFILESPATH}/matrices/small -mat_solver_type cusparse -cholesky {{0 1}separate output}
720:       args: -mat_type aijcusparse -f ${DATAFILESPATH}/matrices/small -mat_solver_type cusparse -cholesky {{0}separate output}

722:    test:
723:       suffix: cusparse_2
724:       requires: cuda
725:       args: -mat_type aijcusparse -mat_solver_type cusparse -cholesky {{0 1}separate output}

727:    test:
728:       suffix: cudss
729:       requires: cuda cudss !complex
730:       args: -mat_type aijcusparse -mat_solver_type cudss -cholesky {{0 1}separate output}

732:    test:
733:       suffix: cudss_deterministic
734:       requires: cuda cudss !complex
735:       args: -mat_type aijcusparse -mat_solver_type cudss -mat_cudss_deterministic
736:       output_file: output/ex125_cudss_cholesky-0.out

738:    testset:
739:       nsize: {{1 2}separate output}
740:       requires: double !defined(PETSC_USE_64BIT_INDICES) datafilespath !complex
741:       args: -f ${DATAFILESPATH}/matrices/mixed_poisson
742:       test:
743:         requires: superlu_dist TODO # superlu_dist is broken
744:         suffix: saddle_point_superlu_dist
745:         args: -mat_solver_type superlu_dist -mat_superlu_dist_rowperm {{norowperm largediag_mc64}} -test_inertia 0
746:       test:
747:         requires: mumps
748:         suffix: saddle_point_mumps_lu
749:         args: -mat_solver_type mumps -mat_mumps_icntl_14 100
750:       test:
751:         requires: mumps
752:         suffix: saddle_point_mumps_cholesky
753:         args: -cholesky -mat_solver_type mumps

755: TEST*/