Actual source code: ex4.c

petsc-3.8.3 2017-12-09
Report Typos and Errors

  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,*submatrices;
  9:   PetscInt       m = 10,n = 10,i = 4,tmp,rstart,rend;
 11:   IS             irow,icol;
 12:   PetscScalar    value = 1.0;
 13:   PetscViewer    sviewer;
 14:   PetscBool      allA = PETSC_FALSE;

 16:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 17:   PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_COMMON);
 18:   PetscViewerPushFormat(PETSC_VIEWER_STDOUT_SELF,PETSC_VIEWER_ASCII_COMMON);

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

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

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

 51:     ISDestroy(&irow);
 52:     ISDestroy(&icol);
 53:     MatDestroySubMatrices(1,&submatrices);
 54:   }

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

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

 65:   /* sviewer will cause the submatrices (one per processor) to be printed in the correct order */
 66:   PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD,"\nSubmatrices\n");
 67:   PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer);
 68:   MatView(submat,sviewer);
 69:   PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer);
 70:   PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD);
 71:   MatDestroySubMatrices(1,&submatrices);

 73:   /* Form submatrix with rows 2-4 and all columns */
 74:   ISDestroy(&icol);
 75:   ISCreateStride(PETSC_COMM_SELF,10,0,1,&icol);
 76:   MatCreateSubMatrices(mat,1,&irow,&icol,MAT_INITIAL_MATRIX,&submatrices);
 77:   MatCreateSubMatrices(mat,1,&irow,&icol,MAT_REUSE_MATRIX,&submatrices);
 78:   submat = *submatrices;

 80:   PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD,"\nSubmatrices with allcolumns\n");
 81:   PetscViewerGetSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer);
 82:   MatView(submat,sviewer);
 83:   PetscViewerRestoreSubViewer(PETSC_VIEWER_STDOUT_WORLD,PETSC_COMM_SELF,&sviewer);
 84:   PetscViewerFlush(PETSC_VIEWER_STDOUT_WORLD);

 86:   /* Zero the original matrix */
 87:   PetscViewerASCIIPrintf(PETSC_VIEWER_STDOUT_WORLD,"Original zeroed matrix\n");
 88:   MatZeroEntries(mat);
 89:   MatView(mat,PETSC_VIEWER_STDOUT_WORLD);

 91:   ISDestroy(&irow);
 92:   ISDestroy(&icol);
 93:   MatDestroySubMatrices(1,&submatrices);
 94:   MatDestroy(&mat);
 95:   PetscFinalize();
 96:   return ierr;
 97: }