Actual source code: ex10.c

petsc-3.3-p7 2013-05-11
  2: static char help[] = "Reads a PETSc matrix and computes the 2 norm of the columns\n\n";

  4: /*T
  5:    Concepts: Mat^loading a binary matrix;
  6:    Processors: n
  7: T*/

  9: /* 
 10:   Include "petscmat.h" so that we can use matrices.
 11:   automatically includes:
 12:      petscsys.h       - base PETSc routines   petscvec.h    - vectors
 13:      petscmat.h    - matrices
 14:      petscis.h     - index sets            petscviewer.h - viewers               
 15: */
 16: #include <petscmat.h>


 21: int main(int argc,char **args)
 22: {
 23:   Mat                   A;                /* matrix */
 24:   PetscViewer           fd;               /* viewer */
 25:   char                  file[PETSC_MAX_PATH_LEN];     /* input file name */
 26:   PetscErrorCode        ierr;
 27:   PetscReal             *norms;
 28:   PetscInt              n,cstart,cend;
 29:   PetscBool             flg;

 31:   PetscInitialize(&argc,&args,(char *)0,help);


 34:   /* 
 35:      Determine files from which we read the two linear systems
 36:      (matrix and right-hand-side vector).
 37:   */
 38:   PetscOptionsGetString(PETSC_NULL,"-f",file,PETSC_MAX_PATH_LEN,&flg);
 39:   if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate binary file with the -f option");

 41:   /* 
 42:      Open binary file.  Note that we use FILE_MODE_READ to indicate
 43:      reading from this file.
 44:   */
 45:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);

 47:   /*
 48:     Load the matrix; then destroy the viewer.
 49:   */
 50:   MatCreate(PETSC_COMM_WORLD,&A);
 51:   MatSetOptionsPrefix(A,"a_");
 52:   MatSetFromOptions(A);
 53:   MatLoad(A,fd);
 54:   PetscViewerDestroy(&fd);

 56:   MatGetSize(A,PETSC_NULL,&n);
 57:   MatGetOwnershipRangeColumn(A,&cstart,&cend);
 58:   PetscMalloc(n*sizeof(PetscReal),&norms);
 59:   MatGetColumnNorms(A,NORM_2,norms);
 60:   PetscRealView(cend-cstart,norms+cstart,PETSC_VIEWER_STDOUT_WORLD);
 61:   PetscFree(norms);

 63:   MatDestroy(&A);
 64:   PetscFinalize();
 65:   return 0;
 66: }