Actual source code: ex57.c
petsc-3.8.4 2018-03-24
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>
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;
20: PetscInt start=0;
21: PetscMPIInt size;
22: IS isrow,iscol;
23: PetscBool flg;
25: PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
26: PetscOptionsGetString(NULL,NULL,"-fin",fin,PETSC_MAX_PATH_LEN,&flg);
27: if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate binary file with the -fin option");
28: PetscViewerBinaryOpen(PETSC_COMM_SELF,fin,FILE_MODE_READ,&fdin);
30: PetscOptionsGetString(NULL,NULL,"-fout",fout,PETSC_MAX_PATH_LEN,&flg);
31: if (!flg) {PetscPrintf(PETSC_COMM_WORLD,"Writing submatrix to file : %s\n",fout);}
32: PetscViewerBinaryOpen(PETSC_COMM_SELF,fout,FILE_MODE_WRITE,&fdout);
34: MatCreate(PETSC_COMM_SELF,&A);
35: MatSetType(A,mtype);
36: MatLoad(A,fdin);
37: PetscViewerDestroy(&fdin);
39: MatGetSize(A,&size,&size);
40: size /= 2;
41: PetscOptionsGetInt(NULL,NULL,"-start",&start,NULL);
42: PetscOptionsGetInt(NULL,NULL,"-size",&size,NULL);
44: ISCreateStride(PETSC_COMM_SELF,size,start,1,&isrow);
45: ISCreateStride(PETSC_COMM_SELF,size,start,1,&iscol);
46: MatCreateSubMatrices(A,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&B);
47: MatView(B[0],fdout);
49: VecCreate(PETSC_COMM_SELF,&b);
50: VecSetSizes(b,PETSC_DECIDE,size);
51: VecSetFromOptions(b);
52: MatView(B[0],fdout);
53: PetscViewerDestroy(&fdout);
55: MatDestroy(&A);
56: MatDestroy(&B[0]);
57: VecDestroy(&b);
58: PetscFree(B);
59: ISDestroy(&iscol);
60: ISDestroy(&isrow);
61: PetscFinalize();
62: return ierr;
63: }