Actual source code: ex168.c

  1: static char help[] = "Tests external Clique direct solvers. Simplified from ex130.c\n\
  2: Example: mpiexec -n <np> ./ex168 -f <matrix binary file> \n\n";

  4: #include <petscmat.h>

  6: int main(int argc, char **args)
  7: {
  8:   Mat           A, F;
  9:   Vec           u, x, b;
 10:   PetscMPIInt   rank, size;
 11:   PetscInt      m, n, nfact;
 12:   PetscReal     norm, tol = 1.e-12, Anorm;
 13:   IS            perm, iperm;
 14:   MatFactorInfo info;
 15:   PetscBool     flg, testMatSolve = PETSC_TRUE;
 16:   PetscViewer   fd;                       /* viewer */
 17:   char          file[PETSC_MAX_PATH_LEN]; /* input file name */

 19:   PetscFunctionBeginUser;
 20:   PetscCall(PetscInitialize(&argc, &args, NULL, help));
 21:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
 22:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));

 24:   /* Determine file from which we read the matrix A */
 25:   PetscCall(PetscOptionsGetString(NULL, NULL, "-f", file, sizeof(file), &flg));
 26:   PetscCheck(flg, PETSC_COMM_WORLD, PETSC_ERR_USER, "Must indicate binary file with the -f option");

 28:   /* Load matrix A */
 29:   PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD, file, FILE_MODE_READ, &fd));
 30:   PetscCall(MatCreate(PETSC_COMM_WORLD, &A));
 31:   PetscCall(MatLoad(A, fd));
 32:   PetscCall(VecCreate(PETSC_COMM_WORLD, &b));
 33:   PetscCall(VecLoad(b, fd));
 34:   PetscCall(PetscViewerDestroy(&fd));
 35:   PetscCall(MatGetLocalSize(A, &m, &n));
 36:   PetscCheck(m == n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "This example is not intended for rectangular matrices (%d, %d)", m, n);
 37:   PetscCall(MatNorm(A, NORM_INFINITY, &Anorm));

 39:   /* Create vectors */
 40:   PetscCall(VecDuplicate(b, &x));
 41:   PetscCall(VecDuplicate(x, &u)); /* save the true solution */

 43:   /* Test Cholesky Factorization */
 44:   PetscCall(MatGetOrdering(A, MATORDERINGNATURAL, &perm, &iperm));

 46:   if (rank == 0) printf(" Clique Cholesky:\n");
 47:   PetscCall(MatGetFactor(A, MATSOLVERCLIQUE, MAT_FACTOR_CHOLESKY, &F));

 49:   info.fill = 5.0;
 50:   PetscCall(MatCholeskyFactorSymbolic(F, A, perm, &info));

 52:   for (nfact = 0; nfact < 1; nfact++) {
 53:     if (rank == 0) printf(" %d-the Cholesky numfactorization \n", nfact);
 54:     PetscCall(MatCholeskyFactorNumeric(F, A, &info));

 56:     /* Test MatSolve() */
 57:     if (testMatSolve && nfact == 2) {
 58:       PetscCall(MatSolve(F, b, x));

 60:       /* Check the residual */
 61:       PetscCall(MatMult(A, x, u));
 62:       PetscCall(VecAXPY(u, -1.0, b));
 63:       PetscCall(VecNorm(u, NORM_INFINITY, &norm));
 64:       /* if (norm > tol) { */
 65:       if (rank == 0) PetscCall(PetscPrintf(PETSC_COMM_SELF, "MatSolve: rel residual %g/%g = %g, LU numfact %d\n", norm, Anorm, norm / Anorm, nfact));
 66:       /*} */
 67:     }
 68:   }

 70:   /* Free data structures */
 71:   PetscCall(MatDestroy(&A));
 72:   PetscCall(MatDestroy(&F));
 73:   PetscCall(ISDestroy(&perm));
 74:   PetscCall(ISDestroy(&iperm));
 75:   PetscCall(VecDestroy(&x));
 76:   PetscCall(VecDestroy(&b));
 77:   PetscCall(VecDestroy(&u));
 78:   PetscCall(PetscFinalize());
 79:   return 0;
 80: }