Actual source code: ex16.c


  2: static char help[] = "Reads a matrix from PETSc binary file. Use for view or investigating matrix data structure. \n\n";
  3: /*
  4:  Example:
  5:       ./ex16 -f <matrix file> -a_mat_view draw -draw_pause -1
  6:       ./ex16 -f <matrix file> -a_mat_view ascii::ascii_info
  7:  */

  9: #include <petscmat.h>
 10: int main(int argc,char **args)
 11: {
 12:   Mat               A,Asp;
 13:   PetscViewer       fd;                        /* viewer */
 14:   char              file[PETSC_MAX_PATH_LEN];  /* input file name */
 15:   PetscInt          m,n,rstart,rend;
 16:   PetscBool         flg;
 17:   PetscInt          row,ncols,j,nrows,nnzA=0,nnzAsp=0;
 18:   const PetscInt    *cols;
 19:   const PetscScalar *vals;
 20:   PetscReal         norm,percent,val,dtol=1.e-16;
 21:   PetscMPIInt       rank;
 22:   MatInfo           matinfo;
 23:   PetscInt          Dnnz,Onnz;

 25:   PetscInitialize(&argc,&args,(char*)0,help);
 26:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

 28:   /* Determine files from which we read the linear systems. */
 29:   PetscOptionsGetString(NULL,NULL,"-f",file,sizeof(file),&flg);

 32:   /* Open binary file.  Note that we use FILE_MODE_READ to indicate
 33:      reading from this file. */
 34:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);

 36:   /* Load the matrix; then destroy the viewer. */
 37:   MatCreate(PETSC_COMM_WORLD,&A);
 38:   MatSetOptionsPrefix(A,"a_");
 39:   MatSetFromOptions(A);
 40:   MatLoad(A,fd);
 41:   PetscViewerDestroy(&fd);
 42:   MatGetSize(A,&m,&n);
 43:   MatGetInfo(A,MAT_LOCAL,&matinfo);
 44:   /*printf("matinfo.nz_used %g\n",matinfo.nz_used);*/

 46:   /* Get a sparse matrix Asp by dumping zero entries of A */
 47:   MatCreate(PETSC_COMM_WORLD,&Asp);
 48:   MatSetSizes(Asp,m,n,PETSC_DECIDE,PETSC_DECIDE);
 49:   MatSetOptionsPrefix(Asp,"asp_");
 50:   MatSetFromOptions(Asp);
 51:   Dnnz = (PetscInt)matinfo.nz_used/m + 1;
 52:   Onnz = Dnnz/2;
 53:   printf("Dnnz %d %d\n",Dnnz,Onnz);
 54:   MatSeqAIJSetPreallocation(Asp,Dnnz,NULL);
 55:   MatMPIAIJSetPreallocation(Asp,Dnnz,NULL,Onnz,NULL);
 56:   /* The allocation above is approximate so we must set this option to be permissive.
 57:    * Real code should preallocate exactly. */
 58:   MatSetOption(Asp,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);

 60:   /* Check zero rows */
 61:   MatGetOwnershipRange(A,&rstart,&rend);
 62:   nrows = 0;
 63:   for (row=rstart; row<rend; row++) {
 64:     MatGetRow(A,row,&ncols,&cols,&vals);
 65:     nnzA += ncols;
 66:     norm  = 0.0;
 67:     for (j=0; j<ncols; j++) {
 68:       val = PetscAbsScalar(vals[j]);
 69:       if (norm < val) norm = norm;
 70:       if (val > dtol) {
 71:         MatSetValues(Asp,1,&row,1,&cols[j],&vals[j],INSERT_VALUES);
 72:         nnzAsp++;
 73:       }
 74:     }
 75:     if (!norm) nrows++;
 76:     MatRestoreRow(A,row,&ncols,&cols,&vals);
 77:   }
 78:   MatAssemblyBegin(Asp,MAT_FINAL_ASSEMBLY);
 79:   MatAssemblyEnd(Asp,MAT_FINAL_ASSEMBLY);

 81:   percent=(PetscReal)nnzA*100/(m*n);
 82:   PetscPrintf(PETSC_COMM_SELF," [%d] Matrix A local size %d,%d; nnzA %d, %g percent; No. of zero rows: %d\n",rank,m,n,nnzA,percent,nrows);
 83:   percent=(PetscReal)nnzAsp*100/(m*n);
 84:   PetscPrintf(PETSC_COMM_SELF," [%d] Matrix Asp nnzAsp %d, %g percent\n",rank,nnzAsp,percent);

 86:   /* investigate matcoloring for Asp */
 87:   PetscBool Asp_coloring = PETSC_FALSE;
 88:   PetscOptionsHasName(NULL,NULL,"-Asp_color",&Asp_coloring);
 89:   if (Asp_coloring) {
 90:     MatColoring   mc;
 91:     ISColoring    iscoloring;
 92:     MatFDColoring matfdcoloring;
 93:     PetscPrintf(PETSC_COMM_WORLD," Create coloring of Asp...\n");
 94:     MatColoringCreate(Asp,&mc);
 95:     MatColoringSetType(mc,MATCOLORINGSL);
 96:     MatColoringSetFromOptions(mc);
 97:     MatColoringApply(mc,&iscoloring);
 98:     MatColoringDestroy(&mc);
 99:     MatFDColoringCreate(Asp,iscoloring,&matfdcoloring);
100:     MatFDColoringSetFromOptions(matfdcoloring);
101:     MatFDColoringSetUp(Asp,iscoloring,matfdcoloring);
102:     /*MatFDColoringView(matfdcoloring,PETSC_VIEWER_STDOUT_WORLD);*/
103:     ISColoringDestroy(&iscoloring);
104:     MatFDColoringDestroy(&matfdcoloring);
105:   }

107:   /* Write Asp in binary for study - see ~petsc/src/mat/tests/ex124.c */
108:   PetscBool Asp_write = PETSC_FALSE;
109:   PetscOptionsHasName(NULL,NULL,"-Asp_write",&Asp_write);
110:   if (Asp_write) {
111:     PetscViewer viewer;
112:     PetscPrintf(PETSC_COMM_SELF,"Write Asp into file Asp.dat ...\n");
113:     PetscViewerBinaryOpen(PETSC_COMM_WORLD,"Asp.dat",FILE_MODE_WRITE,&viewer);
114:     MatView(Asp,viewer);
115:     PetscViewerDestroy(&viewer);
116:   }

118:   MatDestroy(&A);
119:   MatDestroy(&Asp);
120:   PetscFinalize();
121:   return 0;
122: }