Actual source code: ex10.c

petsc-3.9.4 2018-09-11
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>


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

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

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

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

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

 59:   MatDestroy(&A);
 60:   PetscFinalize();
 61:   return ierr;
 62: }



 66: /*TEST

 68:    test:
 69:       suffix: mpiaij
 70:       nsize: 2
 71:       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES)
 72:       args: -f ${DATAFILESPATH}/matrices/small -a_mat_type mpiaij

 74:    test:
 75:       suffix: mpidense
 76:       nsize: 2
 77:       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES)
 78:       args: -f ${DATAFILESPATH}/matrices/small -a_mat_type mpidense

 80:    test:
 81:       suffix: seqaij
 82:       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES)
 83:       args: -f ${DATAFILESPATH}/matrices/small -a_mat_type seqaij

 85:    test:
 86:       suffix: seqdense
 87:       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES)
 88:       args: -f ${DATAFILESPATH}/matrices/small -a_mat_type seqdense

 90: TEST*/