static char help[] = "Reads a PETSc matrix and vector from a file and solves a linear system.\n\ This version first preloads and solves a small system, then loads \n\ another (larger) system and solves it as well. This example illustrates\n\ preloading of instructions with the smaller system so that more accurate\n\ performance monitoring can be done with the larger one (that actually\n\ is the system of interest). See the 'Performance Hints' chapter of the\n\ users manual for a discussion of preloading. Input parameters include\n\ -f0 : first file to load (small system)\n\ -f1 : second file to load (larger system)\n\n\ -trans : solve transpose system instead\n\n"; /* This code can be used to test PETSc interface to other packages.\n\ Examples of command line options: \n\ ./ex10 -f0 -ksp_type preonly \n\ -help -ksp_view \n\ -num_numfac -num_rhs \n\ -ksp_type preonly -pc_type lu -pc_factor_mat_solver_package superlu or superlu_dist or mumps \n\ -ksp_type preonly -pc_type cholesky -pc_factor_mat_solver_package mumps \n\ mpiexec -n ./ex10 -f0 -ksp_type cg -pc_type asm -pc_asm_type basic -sub_pc_type icc -mat_type sbaij \n\n"; */ /*T Concepts: KSP^solving a linear system Processors: n T*/ /* Include "petscksp.h" so that we can use KSP solvers. Note that this file automatically includes: petscsys.h - base PETSc routines petscvec.h - vectors petscmat.h - matrices petscis.h - index sets petscksp.h - Krylov subspace methods petscviewer.h - viewers petscpc.h - preconditioners */ #include #include #undef __FUNCT__ #define __FUNCT__ "main" int main(int argc,char **args) { KSP ksp; /* linear solver context */ Mat A; /* matrix */ Vec x,b,u; /* approx solution, RHS, exact solution */ PetscViewer fd; /* viewer */ char file[4][PETSC_MAX_PATH_LEN]; /* input file name */ PetscBool table=PETSC_FALSE,flg,trans=PETSC_FALSE,initialguess = PETSC_FALSE; PetscBool outputSoln=PETSC_FALSE; PetscErrorCode ierr; PetscInt its,num_numfac,m,n,M; PetscReal norm; PetscLogDouble tsetup,tsetup1,tsetup2,tsolve,tsolve1,tsolve2; PetscBool preload=PETSC_TRUE,isSymmetric,cknorm=PETSC_FALSE,initialguessfile = PETSC_FALSE; PetscMPIInt rank; char initialguessfilename[PETSC_MAX_PATH_LEN]; PetscInitialize(&argc,&args,(char *)0,help); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); MPI_Comm coarseMpiComm; MPI_Comm_split(MPI_COMM_WORLD, rank / 4, rank, &coarseMpiComm); ierr = PetscViewerBinaryOpen(coarseMpiComm, "coarsegrid.mat",FILE_MODE_READ,&fd);CHKERRQ(ierr); ierr = MatCreate(coarseMpiComm,&A);CHKERRQ(ierr); ierr = MatSetFromOptions(A);CHKERRQ(ierr); ierr = MatLoad(A,fd);CHKERRQ(ierr); ierr = PetscViewerDestroy(&fd);CHKERRQ(ierr); MatGetVecs(A, &x, &b); VecSet(b, 1.0); ierr = KSPCreate(coarseMpiComm, &ksp);CHKERRQ(ierr); ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr); ierr = KSPSetOperators(ksp, A, A, SAME_NONZERO_PATTERN);CHKERRQ(ierr); for (int i = 0; i < 1000; i++) ierr = KSPSolve(ksp, b, x);CHKERRQ(ierr); MatDestroy(&A); VecDestroy(&x); VecDestroy(&b); KSPDestroy(&ksp); ierr = PetscFinalize(); return 0; }