Actual source code: ex181.c

petsc-3.7.3 2016-08-01
Report Typos and Errors
  2: static char help[] = "Tests MatGetSubmatrix() with entire matrix, modified from ex59.c.";

  4: #include <petscmat.h>

  8: int main(int argc,char **args)
  9: {
 10:   Mat            C,A;
 11:   PetscInt       i,j,m = 3,n = 2,rstart,rend;
 12:   PetscMPIInt    size,rank;
 14:   PetscScalar    v;
 15:   IS             isrow;
 16:   PetscBool      detect_bug = PETSC_FALSE;

 18:   PetscInitialize(&argc,&args,(char*)0,help);
 19:   PetscOptionsHasName(NULL,NULL,"-detect_bug",&detect_bug);
 20:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 21:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 22:   n    = 2*size;

 24:   MatCreate(PETSC_COMM_WORLD,&C);
 25:   MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);
 26:   MatSetFromOptions(C);
 27:   MatSetUp(C);

 29:   /*
 30:         This is JUST to generate a nice test matrix, all processors fill up
 31:     the entire matrix. This is not something one would ever do in practice.
 32:   */
 33:   MatGetOwnershipRange(C,&rstart,&rend);
 34:   for (i=rstart; i<rend; i++) {
 35:     for (j=0; j<m*n; j++) {
 36:       v    = i + j + 1;
 37:       MatSetValues(C,1,&i,1,&j,&v,INSERT_VALUES);
 38:     }
 39:   }
 40:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 41:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
 42:   PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_COMMON);
 43:   MatView(C,PETSC_VIEWER_STDOUT_WORLD);
 44:   PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);

 46:   /*
 47:      Generate a new matrix consisting every row and column of the original matrix
 48:   */
 49:   MatGetOwnershipRange(C,&rstart,&rend);
 50: 
 51:   /* Create parallel IS with the rows we want on THIS processor */
 52:   if (detect_bug && !rank) {
 53:     ISCreateStride(PETSC_COMM_WORLD,1,rstart,1,&isrow);
 54:   } else {
 55:     ISCreateStride(PETSC_COMM_WORLD,rend-rstart,rstart,1,&isrow);
 56:   }
 57:   MatGetSubMatrix(C,isrow,NULL,MAT_INITIAL_MATRIX,&A);

 59:   /* Change C to test the case MAT_REUSE_MATRIX */
 60:   if (!rank ) {
 61:     i = 0; j = 0; v = 100;
 62:     MatSetValues(C,1,&i,1,&j,&v,INSERT_VALUES);
 63:   }
 64:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 65:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
 66: 
 67:   MatGetSubMatrix(C,isrow,NULL,MAT_REUSE_MATRIX,&A);
 68:   PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_COMMON);
 69:   MatView(A,PETSC_VIEWER_STDOUT_WORLD);
 70:   PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
 71: 
 72:   ISDestroy(&isrow);
 73:   MatDestroy(&A);
 74:   MatDestroy(&C);
 75:   PetscFinalize();
 76:   return 0;
 77: }