1 2 static char help[] = "Tests external Clique direct solvers. Simplified from ex130.c\n\ 3 Example: mpiexec -n <np> ./ex168 -f <matrix binary file> \n\n"; 4 5 #include <petscmat.h> 6 7 int main(int argc,char **args) 8 { 9 Mat A,F; 10 Vec u,x,b; 11 PetscErrorCode ierr; 12 PetscMPIInt rank,size; 13 PetscInt m,n,nfact; 14 PetscReal norm,tol=1.e-12,Anorm; 15 IS perm,iperm; 16 MatFactorInfo info; 17 PetscBool flg,testMatSolve=PETSC_TRUE; 18 PetscViewer fd; /* viewer */ 19 char file[PETSC_MAX_PATH_LEN]; /* input file name */ 20 21 ierr = PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr; 22 CHKERRMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank)); 23 CHKERRMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size)); 24 25 /* Determine file from which we read the matrix A */ 26 CHKERRQ(PetscOptionsGetString(NULL,NULL,"-f",file,sizeof(file),&flg)); 27 PetscCheck(flg,PETSC_COMM_WORLD,PETSC_ERR_USER,"Must indicate binary file with the -f option"); 28 29 /* Load matrix A */ 30 CHKERRQ(PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd)); 31 CHKERRQ(MatCreate(PETSC_COMM_WORLD,&A)); 32 CHKERRQ(MatLoad(A,fd)); 33 CHKERRQ(VecCreate(PETSC_COMM_WORLD,&b)); 34 CHKERRQ(VecLoad(b,fd)); 35 CHKERRQ(PetscViewerDestroy(&fd)); 36 CHKERRQ(MatGetLocalSize(A,&m,&n)); 37 PetscCheckFalse(m != n,PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ, "This example is not intended for rectangular matrices (%d, %d)", m, n); 38 CHKERRQ(MatNorm(A,NORM_INFINITY,&Anorm)); 39 40 /* Create vectors */ 41 CHKERRQ(VecDuplicate(b,&x)); 42 CHKERRQ(VecDuplicate(x,&u)); /* save the true solution */ 43 44 /* Test Cholesky Factorization */ 45 CHKERRQ(MatGetOrdering(A,MATORDERINGNATURAL,&perm,&iperm)); 46 47 if (rank == 0) printf(" Clique Cholesky:\n"); 48 CHKERRQ(MatGetFactor(A,MATSOLVERCLIQUE,MAT_FACTOR_CHOLESKY,&F)); 49 50 info.fill = 5.0; 51 CHKERRQ(MatCholeskyFactorSymbolic(F,A,perm,&info)); 52 53 for (nfact = 0; nfact < 1; nfact++) { 54 if (rank == 0) printf(" %d-the Cholesky numfactorization \n",nfact); 55 CHKERRQ(MatCholeskyFactorNumeric(F,A,&info)); 56 57 /* Test MatSolve() */ 58 if (testMatSolve && nfact == 2) { 59 CHKERRQ(MatSolve(F,b,x)); 60 61 /* Check the residual */ 62 CHKERRQ(MatMult(A,x,u)); 63 CHKERRQ(VecAXPY(u,-1.0,b)); 64 CHKERRQ(VecNorm(u,NORM_INFINITY,&norm)); 65 /* if (norm > tol) { */ 66 if (rank == 0) { 67 CHKERRQ(PetscPrintf(PETSC_COMM_SELF,"MatSolve: rel residual %g/%g = %g, LU numfact %d\n",norm,Anorm,norm/Anorm,nfact)); 68 } 69 /*} */ 70 } 71 } 72 73 /* Free data structures */ 74 CHKERRQ(MatDestroy(&A)); 75 CHKERRQ(MatDestroy(&F)); 76 CHKERRQ(ISDestroy(&perm)); 77 CHKERRQ(ISDestroy(&iperm)); 78 CHKERRQ(VecDestroy(&x)); 79 CHKERRQ(VecDestroy(&b)); 80 CHKERRQ(VecDestroy(&u)); 81 ierr = PetscFinalize(); 82 return ierr; 83 } 84