Actual source code: ex11.c

  1: static char help[] = "Tests MatMeshToCellGraph()\n\n";

  3: /*
  4:   Include "petscmat.h" so that we can use matrices.
  5:   automatically includes:
  6:      petscsys.h       - base PETSc routines   petscvec.h    - vectors
  7:      petscmat.h    - matrices
  8:      petscis.h     - index sets            petscviewer.h - viewers
  9: */
 10: #include <petscmat.h>

 12: int main(int argc, char **args)
 13: {
 14:   Mat             mesh, dual;
 15:   PetscInt        Nvertices = 6; /* total number of vertices */
 16:   PetscInt        ncells, *ii, *jj;
 17:   PetscMPIInt     size, rank;
 18:   MatPartitioning part;
 19:   IS              is;

 21:   PetscFunctionBeginUser;
 22:   PetscCall(PetscInitialize(&argc, &args, NULL, help));
 23:   PetscCallMPI(MPI_Comm_size(MPI_COMM_WORLD, &size));
 24:   PetscCallMPI(MPI_Comm_rank(MPI_COMM_WORLD, &rank));

 26:   if (size == 1) {
 27:     ncells = 4;
 28:     PetscCall(PetscMalloc1(ncells + 1, &ii));
 29:     PetscCall(PetscMalloc1(3 * ncells, &jj));
 30:     ii[0] = 0;
 31:     ii[1] = 3;
 32:     ii[2] = 6;
 33:     ii[3] = 9;
 34:     ii[4] = 12;
 35:     /* cell 0 */
 36:     jj[0] = 0;
 37:     jj[1] = 1;
 38:     jj[2] = 2;
 39:     /* cell 1 */
 40:     jj[3] = 1;
 41:     jj[4] = 2;
 42:     jj[5] = 3;
 43:     /* cell 2 */
 44:     jj[6] = 1;
 45:     jj[7] = 4;
 46:     jj[8] = 5;
 47:     /* cell 3 */
 48:     jj[9]  = 1;
 49:     jj[10] = 3;
 50:     jj[11] = 5;
 51:   } else {
 52:     PetscCheck(size == 2, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "This example is for one or two processes");
 53:     ncells = 2;
 54:     PetscCall(PetscMalloc1(3, &ii));
 55:     PetscCall(PetscMalloc1(6, &jj));
 56:     ii[0] = 0;
 57:     ii[1] = 3;
 58:     ii[2] = 6;
 59:     if (rank == 0) {
 60:       jj[0] = 0;
 61:       jj[1] = 1;
 62:       jj[2] = 2;
 63:       jj[3] = 1;
 64:       jj[4] = 2;
 65:       jj[5] = 3;
 66:     } else {
 67:       jj[0] = 1;
 68:       jj[1] = 4;
 69:       jj[2] = 5;
 70:       jj[3] = 1;
 71:       jj[4] = 3;
 72:       jj[5] = 5;
 73:     }
 74:   }
 75:   PetscCall(MatCreateMPIAdj(MPI_COMM_WORLD, ncells, Nvertices, ii, jj, NULL, &mesh));
 76:   PetscCall(MatMeshToCellGraph(mesh, 2, &dual));
 77:   PetscCall(MatView(dual, PETSC_VIEWER_STDOUT_WORLD));

 79:   PetscCall(MatPartitioningCreate(MPI_COMM_WORLD, &part));
 80:   PetscCall(MatPartitioningSetAdjacency(part, dual));
 81:   PetscCall(MatPartitioningSetFromOptions(part));
 82:   PetscCall(MatPartitioningApply(part, &is));
 83:   PetscCall(ISView(is, PETSC_VIEWER_STDOUT_WORLD));
 84:   PetscCall(ISDestroy(&is));
 85:   PetscCall(MatPartitioningDestroy(&part));

 87:   PetscCall(MatDestroy(&mesh));
 88:   PetscCall(MatDestroy(&dual));
 89:   PetscCall(PetscFinalize());
 90:   return 0;
 91: }

 93: /*TEST

 95:    test:
 96:       suffix: 1
 97:       nsize: 2
 98:       requires: parmetis

100:    test:
101:       suffix: metis
102:       nsize: 1
103:       requires: metis
104:       args: -mat_mesh_to_cell_graph_type metis -mat_partitioning_type current

106: TEST*/