Actual source code: ex249.c

  1: static char help[] = "Test MatCreateSubMatrices\n\n";

  3: #include <petscis.h>
  4: #include <petscmat.h>

  6: int main(int argc,char **args)
  7: {
  8:   Mat             A,*submats,*submats2;
  9:   IS              *irow,*icol;
 10:   PetscInt        i,n;
 11:   PetscMPIInt     rank;
 12:   PetscViewer     matfd,rowfd,colfd;
 13:   PetscBool       same;
 14:   char            matfile[PETSC_MAX_PATH_LEN],rowfile[PETSC_MAX_PATH_LEN],colfile[PETSC_MAX_PATH_LEN];
 15:   char            rankstr[16]={0};

 17:   PetscInitialize(&argc,&args,(char*)0,help);
 18:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);

 20:   PetscOptionsGetString(NULL,NULL,"-A",matfile,sizeof(matfile),NULL);
 21:   PetscOptionsGetString(NULL,NULL,"-row",rowfile,sizeof(rowfile),NULL);
 22:   PetscOptionsGetString(NULL,NULL,"-col",colfile,sizeof(colfile),NULL);

 24:   /* Each rank has its own files for row/col ISes */
 25:   PetscSNPrintf(rankstr,16,"-%d",rank);
 26:   PetscStrlcat(rowfile,rankstr,PETSC_MAX_PATH_LEN);
 27:   PetscStrlcat(colfile,rankstr,PETSC_MAX_PATH_LEN);

 29:   PetscViewerBinaryOpen(PETSC_COMM_WORLD,matfile,FILE_MODE_READ,&matfd);
 30:   PetscViewerBinaryOpen(PETSC_COMM_SELF,rowfile,FILE_MODE_READ,&rowfd);
 31:   PetscViewerBinaryOpen(PETSC_COMM_SELF,colfile,FILE_MODE_READ,&colfd);

 33:   MatCreate(PETSC_COMM_WORLD,&A);
 34:   MatSetFromOptions(A);
 35:   MatLoad(A,matfd);

 37:   /* We stored the number of ISes at the beginning of rowfd */
 38:   PetscViewerBinaryRead(rowfd,&n,1,NULL,PETSC_INT);
 39:   PetscMalloc2(n,&irow,n,&icol);
 40:   for (i=0; i<n; i++) {
 41:     ISCreate(PETSC_COMM_SELF,&irow[i]);
 42:     ISCreate(PETSC_COMM_SELF,&icol[i]);
 43:     ISLoad(irow[i],rowfd);
 44:     ISLoad(icol[i],colfd);
 45:   }

 47:   PetscViewerDestroy(&matfd);
 48:   PetscViewerDestroy(&rowfd);
 49:   PetscViewerDestroy(&colfd);

 51:   /* Create submats for the first time */
 52:   MatCreateSubMatrices(A,n,irow,icol,MAT_INITIAL_MATRIX,&submats);

 54:   /* Dup submats to submats2 for later comparison */
 55:   PetscMalloc1(n,&submats2);
 56:   for (i=0; i<n; i++) {
 57:     MatDuplicate(submats[i],MAT_COPY_VALUES,&submats2[i]);
 58:   }

 60:   /* Create submats again */
 61:   MatCreateSubMatrices(A,n,irow,icol,MAT_REUSE_MATRIX,&submats);

 63:   /* Compare submats and submats2 */
 64:   for (i=0; i<n; i++) {
 65:     MatEqual(submats[i],submats2[i],&same);
 67:   }

 69:   MatDestroy(&A);
 70:   for (i=0; i<n; i++) {
 71:     ISDestroy(&irow[i]);
 72:     ISDestroy(&icol[i]);
 73:   }
 74:   MatDestroySubMatrices(n,&submats);
 75:   MatDestroyMatrices(n,&submats2);
 76:   PetscFree2(irow,icol);
 77:   PetscFinalize();
 78:   return 0;
 79: }

 81: /*TEST

 83:    test:
 84:      suffix: 1
 85:      nsize: 2
 86:      requires: datafilespath double !complex !defined(PETSC_USE_64BIT_INDICES)
 87:      args: -mat_type {{aij baij}} -A ${DATAFILESPATH}/matrices/CreateSubMatrices/A -row ${DATAFILESPATH}/matrices/CreateSubMatrices/row -col ${DATAFILESPATH}/matrices/CreateSubMatrices/col

 89: TEST*/