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:   -m  <sx>  : the size of the submatrix\n";

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

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

 24:   PetscInitialize(&argc,&args,(char*)0,help);
 25:   PetscOptionsGetString(NULL,NULL,"-fin",fin,sizeof(fin),&flg);
 27:   PetscViewerBinaryOpen(PETSC_COMM_SELF,fin,FILE_MODE_READ,&fdin);

 29:   PetscOptionsGetString(NULL,NULL,"-fout",fout,sizeof(fout),&flg);
 30:   if (!flg) PetscPrintf(PETSC_COMM_WORLD,"Writing submatrix to file : %s\n",fout);
 31:   PetscViewerBinaryOpen(PETSC_COMM_SELF,fout,FILE_MODE_WRITE,&fdout);

 33:   MatCreate(PETSC_COMM_SELF,&A);
 34:   MatSetType(A,mtype);
 35:   MatLoad(A,fdin);
 36:   PetscViewerDestroy(&fdin);

 38:   MatGetSize(A,&m,&m);
 39:   m /= 2;
 40:   PetscOptionsGetInt(NULL,NULL,"-start",&start,NULL);
 41:   PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);

 43:   ISCreateStride(PETSC_COMM_SELF,m,start,1,&isrow);
 44:   ISCreateStride(PETSC_COMM_SELF,m,start,1,&iscol);
 45:   MatCreateSubMatrices(A,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&B);
 46:   MatView(B[0],fdout);

 48:   VecCreate(PETSC_COMM_SELF,&b);
 49:   VecSetSizes(b,PETSC_DECIDE,m);
 50:   VecSetFromOptions(b);
 51:   MatView(B[0],fdout);
 52:   PetscViewerDestroy(&fdout);

 54:   MatDestroy(&A);
 55:   MatDestroy(&B[0]);
 56:   VecDestroy(&b);
 57:   PetscFree(B);
 58:   ISDestroy(&iscol);
 59:   ISDestroy(&isrow);
 60:   PetscFinalize();
 61:   return 0;
 62: }

 64: /*TEST

 66:     test:
 67:       args: -fin ${DATAFILESPATH}/matrices/small -fout joe -start 2 -m 4
 68:       requires: datafilespath double !complex !defined(PETSC_USE_64BIT_INDICES)

 70: TEST*/