Actual source code: ex57.c

  2: static char help[] = "Reads in a binary file, extracts a submatrix from it, and writes to another binary file.\n\
  3: Options:\n\
  4:   -fin  <mat>  : input matrix file\n\
  5:   -fout <mat>  : output marrix file\n\
  6:   -start <row> : the row from where the submat should be extracted\n\
  7:   -size  <sx>  : the size of the submatrix\n";

 9:  #include petscmat.h
 10:  #include petscvec.h

 14: int main(int argc,char **args)
 15: {
 16:   char           fin[PETSC_MAX_PATH_LEN],fout[PETSC_MAX_PATH_LEN] ="default.mat";
 17:   PetscViewer    fdin,fdout;
 18:   Vec            b;
 19:   const MatType  mtype = MATSEQBAIJ;
 20:   Mat            A,*B;
 22:   PetscInt       start=0;
 23:   PetscMPIInt    size;
 24:   IS             isrow,iscol;
 25:   PetscTruth     flg;

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


 30:   PetscOptionsGetString(PETSC_NULL,"-fin",fin,PETSC_MAX_PATH_LEN-1,&flg);
 31:   if (!flg) SETERRQ(1,"Must indicate binary file with the -fin option");
 32:   PetscViewerBinaryOpen(PETSC_COMM_SELF,fin,FILE_MODE_READ,&fdin);

 34:   PetscOptionsGetString(PETSC_NULL,"-fout",fout,PETSC_MAX_PATH_LEN-1,&flg);
 35:   if (!flg) {PetscPrintf(PETSC_COMM_WORLD,"Writing submatrix to file : %s\n",fout);}
 36:   PetscViewerBinaryOpen(PETSC_COMM_SELF,fout,FILE_MODE_WRITE,&fdout);

 38:   MatLoad(fdin,mtype,&A);
 39:   PetscViewerDestroy(fdin);
 40: 
 41:   MatGetSize(A,&size,&size);
 42:   size /= 2;
 43:   PetscOptionsGetInt(PETSC_NULL,"-start",&start,PETSC_NULL);
 44:   PetscOptionsGetInt(PETSC_NULL,"-size",&size,PETSC_NULL);
 45: 
 46:   ISCreateStride(PETSC_COMM_SELF,size,start,1,&isrow);
 47:   ISCreateStride(PETSC_COMM_SELF,size,start,1,&iscol);
 48:   MatGetSubMatrices(A,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&B);
 49:   MatView(B[0],fdout);

 51:   VecCreate(PETSC_COMM_SELF,&b);
 52:   VecSetSizes(b,PETSC_DECIDE,size);
 53:   VecSetFromOptions(b);
 54:   MatView(B[0],fdout);
 55:   PetscViewerDestroy(fdout);

 57:   MatDestroy(A);
 58:   MatDestroy(B[0]);
 59:   VecDestroy(b);
 60:   PetscFree(B);
 61:   ISDestroy(iscol);
 62:   ISDestroy(isrow);
 63:   PetscFinalize();
 64:   return 0;
 65: }