Actual source code: ex194.c


  2: static char help[] = "Tests MatCreateSubmatrix() with certain entire rows of matrix, modified from ex181.c.";

  4: #include <petscmat.h>

  6: int main(int argc,char **args)
  7: {
  8:   Mat            C,A;
  9:   PetscInt       i,j,m = 3,n = 2,rstart,rend,cstart,cend;
 10:   PetscMPIInt    size,rank;
 11:   PetscScalar    v;
 12:   IS             isrow,iscol;

 14:   PetscInitialize(&argc,&args,(char*)0,help);
 15:   MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
 16:   MPI_Comm_size(PETSC_COMM_WORLD,&size);
 17:   n    = 2*size;

 19:   MatCreate(PETSC_COMM_WORLD,&C);
 20:   MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);
 21:   MatSetFromOptions(C);
 22:   MatSetUp(C);

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

 41:   /*
 42:      Generate a new matrix consisting every column of the original matrix
 43:   */
 44:   MatGetOwnershipRange(C,&rstart,&rend);
 45:   MatGetOwnershipRangeColumn(C,&cstart,&cend);

 47:   ISCreateStride(PETSC_COMM_WORLD,rend-rstart > 0 ? rend-rstart-1 : 0,rstart,1,&isrow);
 48:   ISCreateStride(PETSC_COMM_WORLD,cend-cstart,cstart,1,&iscol);
 49:   MatCreateSubMatrix(C,isrow,NULL,MAT_INITIAL_MATRIX,&A);

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

 59:   MatCreateSubMatrix(C,isrow,NULL,MAT_REUSE_MATRIX,&A);
 60:   PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_COMMON);
 61:   MatView(A,PETSC_VIEWER_STDOUT_WORLD);
 62:   PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);

 64:   ISDestroy(&isrow);
 65:   ISDestroy(&iscol);
 66:   MatDestroy(&A);
 67:   MatDestroy(&C);
 68:   PetscFinalize();
 69:   return 0;
 70: }

 72: /*TEST

 74:    test:
 75:       nsize: 2

 77: TEST*/