Actual source code: ex10.c

petsc-master 2016-12-08
Report Typos and Errors

  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 */
 27:   PetscReal      *norms;
 28:   PetscInt       n,cstart,cend;
 29:   PetscBool      flg;

 31:   PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
 32:   /*
 33:      Determine files from which we read the two linear systems
 34:      (matrix and right-hand-side vector).
 35:   */
 36:   PetscOptionsGetString(NULL,NULL,"-f",file,PETSC_MAX_PATH_LEN,&flg);
 37:   if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate binary file with the -f option");

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

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

 54:   MatGetSize(A,NULL,&n);
 55:   MatGetOwnershipRangeColumn(A,&cstart,&cend);
 56:   PetscMalloc1(n,&norms);
 57:   MatGetColumnNorms(A,NORM_2,norms);
 58:   PetscRealView(cend-cstart,norms+cstart,PETSC_VIEWER_STDOUT_WORLD);
 59:   PetscFree(norms);

 61:   MatDestroy(&A);
 62:   PetscFinalize();
 63:   return ierr;
 64: }