static char help[] = "Solves a linear system in parallel with KSP. The matrix\n\ uses simple bilinear elements on the unit square. To test the parallel\n\ matrix assembly, the matrix is intentionally laid out across processors\n\ differently from the way it is assembled. Input arguments are:\n\ -m : problem size\n\n"; /*T Concepts: KSP^basic parallel example Concepts: Matrices^inserting elements by blocks 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 "petscksp.h" /* Declare user-defined routines */ extern PetscErrorCode FormElementStiffness(PetscReal,PetscScalar*); extern PetscErrorCode FormElementRhs(PetscReal,PetscReal,PetscReal,PetscScalar*); extern PetscErrorCode VecView_VTK(Vec, const char [], const PetscInt, const PetscInt); #undef __FUNCT__ #define __FUNCT__ "main" int main(int argc,char **args) { Vec u,b,ustar; /* approx solution, RHS, exact solution */ Mat A; /* linear system matrix */ KSP ksp; /* Krylov subspace method context */ PetscInt N; /* dimension of system (global) */ PetscInt M; /* number of elements (global) */ PetscMPIInt rank; /* processor rank */ PetscMPIInt size; /* size of communicator */ PetscScalar Ke[16]; /* element matrix */ PetscScalar r[4]; /* element vector */ PetscReal h; /* mesh width */ PetscReal norm; /* norm of solution error */ PetscReal x,y; PetscScalar val; PetscErrorCode ierr; PetscInt idx[4],count,*rows,i,m = 5,start,end,its; PetscInitialize(&argc,&args,(char *)0,help); ierr = PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);CHKERRQ(ierr); N = (m+1)*(m+1); M = m*m; h = 1.0/m; ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Compute the matrix and right-hand-side vector that define the linear system, Au = b. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ /* Create stiffness matrix */ ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr); ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);CHKERRQ(ierr); ierr = MatSetFromOptions(A);CHKERRQ(ierr); //---------------------------------------------------------------------------- // modification to preallocate by Jed in petsc-user email found by searching "ex3.c" ierr = MatSeqAIJSetPreallocation(A,9,PETSC_NULL);CHKERRQ(ierr); ierr = MatMPIAIJSetPreallocation(A,9,PETSC_NULL,5,PETSC_NULL);CHKERRQ(ierr); /* More than necessary */ //---------------------------------------------------------------------------- start = rank*(M/size) + ((M%size) < rank ? (M%size) : rank); end = start + M/size + ((M%size) > rank); /* Assemble matrix */ ierr = FormElementStiffness(h*h,Ke); for (i=start; i