Actual source code: ex57.c
petsc-3.7.3 2016-08-01
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: MatType mtype = MATSEQBAIJ;
20: Mat A,*B;
22: PetscInt start=0;
23: PetscMPIInt size;
24: IS isrow,iscol;
25: PetscBool flg;
27: PetscInitialize(&argc,&args,(char*)0,help);
30: PetscOptionsGetString(NULL,NULL,"-fin",fin,PETSC_MAX_PATH_LEN,&flg);
31: if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate binary file with the -fin option");
32: PetscViewerBinaryOpen(PETSC_COMM_SELF,fin,FILE_MODE_READ,&fdin);
34: PetscOptionsGetString(NULL,NULL,"-fout",fout,PETSC_MAX_PATH_LEN,&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: MatCreate(PETSC_COMM_SELF,&A);
39: MatSetType(A,mtype);
40: MatLoad(A,fdin);
41: PetscViewerDestroy(&fdin);
43: MatGetSize(A,&size,&size);
44: size /= 2;
45: PetscOptionsGetInt(NULL,NULL,"-start",&start,NULL);
46: PetscOptionsGetInt(NULL,NULL,"-size",&size,NULL);
48: ISCreateStride(PETSC_COMM_SELF,size,start,1,&isrow);
49: ISCreateStride(PETSC_COMM_SELF,size,start,1,&iscol);
50: MatGetSubMatrices(A,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&B);
51: MatView(B[0],fdout);
53: VecCreate(PETSC_COMM_SELF,&b);
54: VecSetSizes(b,PETSC_DECIDE,size);
55: VecSetFromOptions(b);
56: MatView(B[0],fdout);
57: PetscViewerDestroy(&fdout);
59: MatDestroy(&A);
60: MatDestroy(&B[0]);
61: VecDestroy(&b);
62: PetscFree(B);
63: ISDestroy(&iscol);
64: ISDestroy(&isrow);
65: PetscFinalize();
66: return 0;
67: }