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:   PetscFunctionBeginUser;
 15:   PetscCall(PetscInitialize(&argc, &args, (char *)0, help));
 16:   PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank));
 17:   PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
 18:   n = 2 * size;

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

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

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

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

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

 62:   PetscCall(MatCreateSubMatrix(C, isrow, NULL, MAT_REUSE_MATRIX, &A));
 63:   PetscCall(PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_ASCII_COMMON));
 64:   PetscCall(MatView(A, PETSC_VIEWER_STDOUT_WORLD));
 65:   PetscCall(PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD));

 67:   PetscCall(ISDestroy(&isrow));
 68:   PetscCall(ISDestroy(&iscol));
 69:   PetscCall(MatDestroy(&A));
 70:   PetscCall(MatDestroy(&C));
 71:   PetscCall(PetscFinalize());
 72:   return 0;
 73: }

 75: /*TEST

 77:    test:
 78:       nsize: 2

 80: TEST*/