static char help[] = "Reads a PETSc matrix and vector from a file and solves the normal equations.\n\n"; /*T Concepts: KSP^solving a linear system Concepts: Normal equations Processors: n T*/ /* Include "petscksp.h" so that we can use KSP solvers. Note that this file automatically includes: petsc.h - base PETSc routines petscvec.h - vectors petscsys.h - system routines petscmat.h - matrices petscis.h - index sets petscksp.h - Krylov subspace methods petscviewer.h - viewers petscpc.h - preconditioners */ #include "petscksp.h" #undef __FUNCT__ #define __FUNCT__ "main" int main(int argc,char **args) { KSP ksp; /* linear solver context */ Mat A,N; /* matrix */ Vec x,b,u,Ab; /* approx solution, RHS, exact solution */ PetscViewer fd; /* viewer */ char file[PETSC_MAX_PATH_LEN]; /* input file name */ PetscErrorCode ierr,ierrp; PetscInt its,n,m; PetscReal norm; PetscInitialize(&argc,&args,(char *)0,help); /* Determine files from which we read the linear system (matrix and right-hand-side vector). */ ierr = PetscOptionsGetString(PETSC_NULL,"-f",file,PETSC_MAX_PATH_LEN-1,PETSC_NULL);CHKERRQ(ierr); /* ----------------------------------------------------------- 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). */ PreLoadBegin(PETSC_FALSE,"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,FILE_MODE_READ,&fd);CHKERRQ(ierr); /* Load the matrix and vector; then destroy the viewer. */ ierr = MatLoad(fd,MATMPIAIJ,&A);CHKERRQ(ierr); ierr = PetscPushErrorHandler(PetscIgnoreErrorHandler,PETSC_NULL);CHKERRQ(ierr); ierrp = VecLoad(fd,PETSC_NULL,&b); ierr = PetscPopErrorHandler();CHKERRQ(ierr); ierr = MatGetLocalSize(A,&m,&n);CHKERRQ(ierr); if (ierrp) { /* if file contains no RHS, then use a vector of all ones */ PetscScalar one = 1.0; ierr = VecCreate(PETSC_COMM_WORLD,&b);CHKERRQ(ierr); ierr = VecSetSizes(b,m,PETSC_DECIDE);CHKERRQ(ierr); ierr = VecSetFromOptions(b);CHKERRQ(ierr); ierr = VecSet(b,one);CHKERRQ(ierr); } ierr = PetscViewerDestroy(fd);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. */ { PetscInt j,mvec,start,end,idex; Vec tmp; PetscScalar *bold; /* Create a new vector b by padding the old one */ ierr = VecCreate(PETSC_COMM_WORLD,&tmp);CHKERRQ(ierr); ierr = VecSetSizes(tmp,m,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