Actual source code: ex181.c


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

  4: #include <petscmat.h>

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

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

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

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

 43:   /*
 44:      Generate a new matrix consisting every row and column of the original matrix
 45:   */
 46:   MatGetOwnershipRange(C,&rstart,&rend);

 48:   /* Create parallel IS with the rows we want on THIS processor */
 49:   if (detect_bug && rank == 0) {
 50:     ISCreateStride(PETSC_COMM_WORLD,1,rstart,1,&isrow);
 51:   } else {
 52:     ISCreateStride(PETSC_COMM_WORLD,rend-rstart,rstart,1,&isrow);
 53:   }
 54:   MatCreateSubMatrix(C,isrow,NULL,MAT_INITIAL_MATRIX,&A);

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

 64:   MatCreateSubMatrix(C,isrow,NULL,MAT_REUSE_MATRIX,&A);
 65:   PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_COMMON);
 66:   MatView(A,PETSC_VIEWER_STDOUT_WORLD);
 67:   PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);

 69:   /* Test MatDuplicate */
 70:   MatDuplicate(A,MAT_COPY_VALUES,&Adup);
 71:   MatDestroy(&Adup);

 73:   ISDestroy(&isrow);
 74:   MatDestroy(&A);
 75:   MatDestroy(&C);
 76:   PetscFinalize();
 77:   return 0;
 78: }

 80: /*TEST

 82:    test:
 83:       nsize: 2
 84:       filter: grep -v "Mat Object"
 85:       requires: !complex

 87:    test:
 88:       suffix: 2
 89:       nsize: 3
 90:       args: -detect_bug
 91:       filter: grep -v "Mat Object"
 92:       requires: !complex

 94: TEST*/