Actual source code: ex194.c

petsc-3.7.3 2016-08-01
Report Typos and Errors
  2: static char help[] = "Tests MatGetSubmatrix() with certain entire rows of matrix, modified from ex181.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,cstart,cend;
 12:   PetscMPIInt    size,rank;
 14:   PetscScalar    v;
 15:   IS             isrow,iscol;

 17:   PetscInitialize(&argc,&args,(char*)0,help);
 18:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 19:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 20:   n    = 2*size;

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

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

 50:   ISCreateStride(PETSC_COMM_WORLD,rend-rstart > 0 ? rend-rstart-1 : 0,rstart,1,&isrow);
 51:   ISCreateStride(PETSC_COMM_WORLD,cend-cstart,cstart,1,&iscol);
 52:   MatGetSubMatrix(C,isrow,NULL,MAT_INITIAL_MATRIX,&A);

 54:   /* Change C to test the case MAT_REUSE_MATRIX */
 55:   if (!rank ) {
 56:     i = 0; j = 0; v = 100;
 57:     MatSetValues(C,1,&i,1,&j,&v,INSERT_VALUES);
 58:   }
 59:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
 60:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

 62:   MatGetSubMatrix(C,isrow,NULL,MAT_REUSE_MATRIX,&A);
 63:   PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_COMMON);
 64:   MatView(A,PETSC_VIEWER_STDOUT_WORLD);
 65:   PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
 66: 
 67:   ISDestroy(&isrow);
 68:   ISDestroy(&iscol);
 69:   MatDestroy(&A);
 70:   MatDestroy(&C);
 71:   PetscFinalize();
 72:   return 0;
 73: }