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;

 16:   PetscInitialize(&argc, &args, (char *)0, help);
 17:   PetscOptionsHasName(NULL, NULL, "-detect_bug", &detect_bug);
 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);

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

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

 57:   /* Change C to test the case MAT_REUSE_MATRIX */
 58:   if (rank == 0) {
 59:     i = 0;
 60:     j = 0;
 61:     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);

 67:   MatCreateSubMatrix(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);

 72:   /* Test MatDuplicate */
 73:   MatDuplicate(A, MAT_COPY_VALUES, &Adup);
 74:   MatDestroy(&Adup);

 76:   ISDestroy(&isrow);
 77:   MatDestroy(&A);
 78:   MatDestroy(&C);
 79:   PetscFinalize();
 80:   return 0;
 81: }

 83: /*TEST

 85:    test:
 86:       nsize: 2
 87:       filter: grep -v "Mat Object"
 88:       requires: !complex

 90:    test:
 91:       suffix: 2
 92:       nsize: 3
 93:       args: -detect_bug
 94:       filter: grep -v "Mat Object"
 95:       requires: !complex

 97: TEST*/