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\ -nearnulldim <0> : number of vectors in the near-null space immediately following matrix\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 #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,nearnulldim = 0; PetscReal norm; 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); ierr = PetscOptionsGetBool(NULL,"-table",&table,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetBool(NULL,"-trans",&trans,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetBool(NULL,"-initialguess",&initialguess,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetBool(NULL,"-output_solution",&outputSoln,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetString(NULL,"-initialguessfilename",initialguessfilename,PETSC_MAX_PATH_LEN,&initialguessfile);CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL,"-nearnulldim",&nearnulldim,NULL);CHKERRQ(ierr); /* Determine files from which we read the two linear systems (matrix and right-hand-side vector). */ ierr = PetscOptionsGetString(NULL,"-f",file[0],PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); if (flg) { ierr = PetscStrcpy(file[1],file[0]);CHKERRQ(ierr); preload = PETSC_FALSE; } else { ierr = PetscOptionsGetString(NULL,"-f0",file[0],PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate binary file with the -f0 or -f option"); ierr = PetscOptionsGetString(NULL,"-f1",file[1],PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); if (!flg) preload = PETSC_FALSE; /* don't bother with second system */ } /* ----------------------------------------------------------- Beginning of linear solver loop ----------------------------------------------------------- */ /* Loop through the linear solve 2 times. - The intention here is to preload and solve a small system; then load another (larger) system and solve it as well. This process preloads the instructions with the smaller system so that more accurate performance monitoring (via -log_summary) can be done with the larger one (that actually is the system of interest). */ PetscPreLoadBegin(preload,"Load system"); /* - - - - - - - - - - - New Stage - - - - - - - - - - - - - Load system - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ /* Open binary file. Note that we use FILE_MODE_READ to indicate reading from this file. */ ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,file[PetscPreLoadIt],FILE_MODE_READ,&fd);CHKERRQ(ierr); /* Load the matrix and vector; then destroy the viewer. */ ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr); ierr = MatSetFromOptions(A);CHKERRQ(ierr); ierr = MatLoad(A,fd);CHKERRQ(ierr); if (nearnulldim) { MatNullSpace nullsp; Vec *nullvecs; PetscInt i; ierr = PetscMalloc(nearnulldim*sizeof(nullvecs[0]),&nullvecs);CHKERRQ(ierr); for (i=0; i -test_zeropivot -set_row_zero -pc_factor_shift_nonzero */ flg = PETSC_FALSE; ierr = PetscOptionsGetBool(NULL, "-test_zeropivot", &flg,NULL);CHKERRQ(ierr); if (flg) { PetscInt row,ncols; const PetscInt *cols; const PetscScalar *vals; PetscBool flg1=PETSC_FALSE; PetscScalar *zeros; row = 0; ierr = MatGetRow(A,row,&ncols,&cols,&vals);CHKERRQ(ierr); ierr = PetscMalloc(sizeof(PetscScalar)*(ncols+1),&zeros); ierr = PetscMemzero(zeros,(ncols+1)*sizeof(PetscScalar));CHKERRQ(ierr); ierr = PetscOptionsGetBool(NULL, "-set_row_zero", &flg1,NULL);CHKERRQ(ierr); if (flg1) { /* set entire row as zero */ ierr = MatSetValues(A,1,&row,ncols,cols,zeros,INSERT_VALUES);CHKERRQ(ierr); } else { /* only set (row,row) entry as zero */ ierr = MatSetValues(A,1,&row,1,&row,zeros,INSERT_VALUES);CHKERRQ(ierr); } ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); } /* Check whether A is symmetric */ flg = PETSC_FALSE; ierr = PetscOptionsGetBool(NULL, "-check_symmetry", &flg,NULL);CHKERRQ(ierr); if (flg) { Mat Atrans; ierr = MatTranspose(A, MAT_INITIAL_MATRIX,&Atrans); ierr = MatEqual(A, Atrans, &isSymmetric); if (isSymmetric) { ierr = MatSetOption(A,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr); } else { PetscPrintf(PETSC_COMM_WORLD,"Warning: A is non-symmetric \n");CHKERRQ(ierr); } ierr = MatDestroy(&Atrans);CHKERRQ(ierr); } /* If the loaded matrix is larger than the vector (due to being padded to match the block size of the system), then create a new padded vector. */ ierr = MatGetLocalSize(A,&m,&n);CHKERRQ(ierr); /* if (m != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ, "This example is not intended for rectangular matrices (%d, %d)", m, n);*/ ierr = MatGetSize(A,&M,NULL);CHKERRQ(ierr); ierr = VecGetSize(b,&m);CHKERRQ(ierr); if (M != m) { /* Create a new vector b by padding the old one */ PetscInt j,mvec,start,end,indx; Vec tmp; PetscScalar *bold; ierr = VecCreate(PETSC_COMM_WORLD,&tmp);CHKERRQ(ierr); ierr = VecSetSizes(tmp,n,PETSC_DECIDE);CHKERRQ(ierr); ierr = VecSetFromOptions(tmp);CHKERRQ(ierr); ierr = VecGetOwnershipRange(b,&start,&end);CHKERRQ(ierr); ierr = VecGetLocalSize(b,&mvec);CHKERRQ(ierr); ierr = VecGetArray(b,&bold);CHKERRQ(ierr); for (j=0; j