Actual source code: ex10.c

petsc-3.11.4 2019-09-28
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;
 28:   PetscViewerFormat format;

 30:   PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
 31:   /*
 32:      Determine files from which we read the matrix
 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:   PetscViewerCreate(PETSC_COMM_WORLD,&fd);
 42:   PetscViewerSetType(fd,PETSCVIEWERBINARY);
 43:   PetscViewerSetFromOptions(fd);
 44:   PetscOptionsGetEnum(NULL,NULL,"-viewer_format",PetscViewerFormats,(PetscEnum*)&format,&flg);
 45:   if (flg) {PetscViewerPushFormat(fd,format);}
 46:   PetscViewerFileSetMode(fd,FILE_MODE_READ);
 47:   PetscViewerFileSetName(fd,file);

 49:   /*
 50:     Load the matrix; then destroy the viewer.
 51:     Matrix type is set automatically but you can override it by MatSetType() prior to MatLoad().
 52:     Do that only if you really insist on the given type.
 53:   */
 54:   MatCreate(PETSC_COMM_WORLD,&A);
 55:   MatSetOptionsPrefix(A,"a_");
 56:   PetscObjectSetName((PetscObject) A,"A");
 57:   MatSetFromOptions(A);
 58:   MatLoad(A,fd);
 59:   PetscViewerDestroy(&fd);

 61:   MatGetSize(A,NULL,&n);
 62:   MatGetOwnershipRangeColumn(A,&cstart,&cend);
 63:   PetscMalloc1(n,&norms);
 64:   MatGetColumnNorms(A,NORM_2,norms);
 65:   PetscRealView(cend-cstart,norms+cstart,PETSC_VIEWER_STDOUT_WORLD);
 66:   PetscFree(norms);

 68:   PetscObjectPrintClassNamePrefixType((PetscObject)A,PETSC_VIEWER_STDOUT_WORLD);
 69:   MatGetOption(A,MAT_SYMMETRIC,&flg);
 70:   PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD,"MAT_SYMMETRIC: %D\n",flg);

 72:   MatDestroy(&A);
 73:   PetscFinalize();
 74:   return ierr;
 75: }



 79: /*TEST

 81:    test:
 82:       suffix: mpiaij
 83:       nsize: 2
 84:       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES)
 85:       args: -f ${DATAFILESPATH}/matrices/small -a_mat_type mpiaij
 86:       args: -a_matload_symmetric

 88:    test:
 89:       suffix: mpiaij_hdf5
 90:       nsize: 2
 91:       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES) hdf5 zlib
 92:       args: -f ${DATAFILESPATH}/matrices/matlab/small.mat -a_mat_type mpiaij -viewer_type hdf5 -viewer_format hdf5_mat
 93:       args: -a_matload_symmetric

 95:    test:
 96:       # test for more processes than rows
 97:       suffix: mpiaij_hdf5_tiny
 98:       nsize: 8
 99:       requires: double !complex !define(PETSC_USE_64BIT_INDICES) hdf5 zlib
100:       args: -f ${wPETSC_DIR}/share/petsc/datafiles/matrices/tiny_system_with_x0.mat -a_mat_type mpiaij -viewer_type hdf5 -viewer_format hdf5_mat
101:       args: -a_matload_symmetric

103:    test:
104:       # test for more processes than rows, complex
105:       TODO: not yet implemented for MATLAB complex format
106:       suffix: mpiaij_hdf5_tiny_complex
107:       nsize: 8
108:       requires: double complex !define(PETSC_USE_64BIT_INDICES) hdf5 zlib
109:       args: -f ${wPETSC_DIR}/share/petsc/datafiles/matrices/tiny_system_with_x0_complex.mat -a_mat_type mpiaij -viewer_type hdf5 -viewer_format hdf5_mat
110:       args: -a_matload_symmetric

112:    test:
113:       TODO: mpibaij not supported yet
114:       suffix: mpibaij_hdf5
115:       nsize: 2
116:       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES) hdf5 zlib
117:       args: -f ${DATAFILESPATH}/matrices/matlab/small.mat -a_mat_type mpibaij -a_mat_block_size 2 -viewer_type hdf5 -viewer_format hdf5_mat
118:       args: -a_matload_symmetric

120:    test:
121:       suffix: mpidense
122:       nsize: 2
123:       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES)
124:       args: -f ${DATAFILESPATH}/matrices/small -a_mat_type mpidense
125:       args: -a_matload_symmetric

127:    test:
128:       suffix: seqaij
129:       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES)
130:       args: -f ${DATAFILESPATH}/matrices/small -a_mat_type seqaij
131:       args: -a_matload_symmetric

133:    test:
134:       suffix: seqaij_hdf5
135:       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES) hdf5 zlib
136:       args: -f ${DATAFILESPATH}/matrices/matlab/small.mat -a_mat_type seqaij -viewer_type hdf5 -viewer_format hdf5_mat
137:       args: -a_matload_symmetric

139:    test:
140:       suffix: seqdense
141:       requires: datafilespath double !complex !define(PETSC_USE_64BIT_INDICES)
142:       args: -f ${DATAFILESPATH}/matrices/small -a_mat_type seqdense
143:       args: -a_matload_symmetric

145: TEST*/