1 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"; 8 9 #include <petscmat.h> 10 #include <petscvec.h> 11 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; 23 24 PetscCall(PetscInitialize(&argc,&args,(char*)0,help)); 25 PetscCall(PetscOptionsGetString(NULL,NULL,"-fin",fin,sizeof(fin),&flg)); 26 PetscCheck(flg,PETSC_COMM_WORLD,PETSC_ERR_USER,"Must indicate binary file with the -fin option"); 27 PetscCall(PetscViewerBinaryOpen(PETSC_COMM_SELF,fin,FILE_MODE_READ,&fdin)); 28 29 PetscCall(PetscOptionsGetString(NULL,NULL,"-fout",fout,sizeof(fout),&flg)); 30 if (!flg) PetscCall(PetscPrintf(PETSC_COMM_WORLD,"Writing submatrix to file : %s\n",fout)); 31 PetscCall(PetscViewerBinaryOpen(PETSC_COMM_SELF,fout,FILE_MODE_WRITE,&fdout)); 32 33 PetscCall(MatCreate(PETSC_COMM_SELF,&A)); 34 PetscCall(MatSetType(A,mtype)); 35 PetscCall(MatLoad(A,fdin)); 36 PetscCall(PetscViewerDestroy(&fdin)); 37 38 PetscCall(MatGetSize(A,&m,&m)); 39 m /= 2; 40 PetscCall(PetscOptionsGetInt(NULL,NULL,"-start",&start,NULL)); 41 PetscCall(PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL)); 42 43 PetscCall(ISCreateStride(PETSC_COMM_SELF,m,start,1,&isrow)); 44 PetscCall(ISCreateStride(PETSC_COMM_SELF,m,start,1,&iscol)); 45 PetscCall(MatCreateSubMatrices(A,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&B)); 46 PetscCall(MatView(B[0],fdout)); 47 48 PetscCall(VecCreate(PETSC_COMM_SELF,&b)); 49 PetscCall(VecSetSizes(b,PETSC_DECIDE,m)); 50 PetscCall(VecSetFromOptions(b)); 51 PetscCall(MatView(B[0],fdout)); 52 PetscCall(PetscViewerDestroy(&fdout)); 53 54 PetscCall(MatDestroy(&A)); 55 PetscCall(MatDestroy(&B[0])); 56 PetscCall(VecDestroy(&b)); 57 PetscCall(PetscFree(B)); 58 PetscCall(ISDestroy(&iscol)); 59 PetscCall(ISDestroy(&isrow)); 60 PetscCall(PetscFinalize()); 61 return 0; 62 } 63 64 /*TEST 65 66 test: 67 args: -fin ${DATAFILESPATH}/matrices/small -fout joe -start 2 -m 4 68 requires: datafilespath double !complex !defined(PETSC_USE_64BIT_INDICES) 69 70 TEST*/ 71