Actual source code: ex4.c


  2: static char help[] = "Creates a matrix, inserts some values, and tests MatCreateSubMatrices() and MatZeroEntries().\n\n";

  4: #include <petscmat.h>

  6: int main(int argc,char **argv)
  7: {
  8:   Mat            mat,submat,submat1,*submatrices;
  9:   PetscInt       m = 10,n = 10,i = 4,tmp,rstart,rend;
 10:   IS             irow,icol;
 11:   PetscScalar    value = 1.0;
 12:   PetscViewer    sviewer;
 13:   PetscBool      allA = PETSC_FALSE;

 15:   PetscInitialize(&argc,&argv,(char*)0,help);
 16:   PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_COMMON);
 17:   PetscViewerPushFormat(PETSC_VIEWER_STDOUT_SELF,PETSC_VIEWER_ASCII_COMMON);

 19:   MatCreate(PETSC_COMM_WORLD,&mat);
 20:   MatSetSizes(mat,PETSC_DECIDE,PETSC_DECIDE,m,n);
 21:   MatSetFromOptions(mat);
 22:   MatSetUp(mat);
 23:   MatGetOwnershipRange(mat,&rstart,&rend);
 24:   for (i=rstart; i<rend; i++) {
 25:     value = (PetscReal)i+1; tmp = i % 5;
 26:     MatSetValues(mat,1,&tmp,1,&i,&value,INSERT_VALUES);
 27:   }
 28:   MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
 29:   MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);
 30:   PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD,"Original matrix\n");
 31:   MatView(mat,PETSC_VIEWER_STDOUT_WORLD);

 33:   /* Test MatCreateSubMatrix_XXX_All(), i.e., submatrix = A */
 34:   PetscOptionsGetBool(NULL,NULL,"-test_all",&allA,NULL);
 35:   if (allA) {
 36:     ISCreateStride(PETSC_COMM_SELF,m,0,1,&irow);
 37:     ISCreateStride(PETSC_COMM_SELF,n,0,1,&icol);
 38:     MatCreateSubMatrices(mat,1,&irow,&icol,MAT_INITIAL_MATRIX,&submatrices);
 39:     MatCreateSubMatrices(mat,1,&irow,&icol,MAT_REUSE_MATRIX,&submatrices);
 40:     submat = *submatrices;

 42:     /* sviewer will cause the submatrices (one per processor) to be printed in the correct order */
 43:     PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD,"\nSubmatrices with all\n");
 44:     PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD,"--------------------\n");
 45:     PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer);
 46:     MatView(submat,sviewer);
 47:     PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer);
 48:     PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD);

 50:     ISDestroy(&irow);
 51:     ISDestroy(&icol);

 53:     /* test getting a reference on a submat */
 54:     PetscObjectReference((PetscObject)submat);
 55:     MatDestroySubMatrices(1,&submatrices);
 56:     MatDestroy(&submat);
 57:   }

 59:   /* Form submatrix with rows 2-4 and columns 4-8 */
 60:   ISCreateStride(PETSC_COMM_SELF,3,2,1,&irow);
 61:   ISCreateStride(PETSC_COMM_SELF,5,4,1,&icol);
 62:   MatCreateSubMatrices(mat,1,&irow,&icol,MAT_INITIAL_MATRIX,&submatrices);
 63:   submat = *submatrices;

 65:   /* Test reuse submatrices */
 66:   MatCreateSubMatrices(mat,1,&irow,&icol,MAT_REUSE_MATRIX,&submatrices);

 68:   /* sviewer will cause the submatrices (one per processor) to be printed in the correct order */
 69:   PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD,"\nSubmatrices\n");
 70:   PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer);
 71:   MatView(submat,sviewer);
 72:   PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer);
 73:   PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD);
 74:   PetscObjectReference((PetscObject)submat);
 75:   MatDestroySubMatrices(1,&submatrices);
 76:   MatDestroy(&submat);

 78:   /* Form submatrix with rows 2-4 and all columns */
 79:   ISDestroy(&icol);
 80:   ISCreateStride(PETSC_COMM_SELF,10,0,1,&icol);
 81:   MatCreateSubMatrices(mat,1,&irow,&icol,MAT_INITIAL_MATRIX,&submatrices);
 82:   MatCreateSubMatrices(mat,1,&irow,&icol,MAT_REUSE_MATRIX,&submatrices);
 83:   submat = *submatrices;

 85:   PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD,"\nSubmatrices with allcolumns\n");
 86:   PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer);
 87:   MatView(submat,sviewer);
 88:   PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer);
 89:   PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD);

 91:   /* Test MatDuplicate */
 92:   MatDuplicate(submat,MAT_COPY_VALUES,&submat1);
 93:   MatDestroy(&submat1);

 95:   /* Zero the original matrix */
 96:   PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD,"Original zeroed matrix\n");
 97:   MatZeroEntries(mat);
 98:   MatView(mat,PETSC_VIEWER_STDOUT_WORLD);

100:   ISDestroy(&irow);
101:   ISDestroy(&icol);
102:   PetscObjectReference((PetscObject)submat);
103:   MatDestroySubMatrices(1,&submatrices);
104:   MatDestroy(&submat);
105:   MatDestroy(&mat);
106:   PetscFinalize();
107:   return 0;
108: }

110: /*TEST

112:    test:
113:       args: -mat_type aij

115:    test:
116:       suffix: 2
117:       args: -mat_type dense

119:    test:
120:       suffix: 3
121:       nsize: 3
122:       args: -mat_type aij

124:    test:
125:       suffix: 4
126:       nsize: 3
127:       args: -mat_type dense

129:    test:
130:       suffix: 5
131:       nsize: 3
132:       args: -mat_type aij -test_all

134: TEST*/