Actual source code: ex3.c

  1: static char help[] = "Test query functions for DMNetwork \n\n";

  3: #include <petscdmnetwork.h>

  5: /*
  6: CreateStarGraphEdgeList - Create a k-Star Graph Edgelist on current processor
  7:   Not Collective

  9:   Input Parameters:
 10: . k    - order of the star graph (number of edges)
 11: . directin - if true direction of edges is towards the center vertex, otherwise they are directed out of the center vertex.

 13:   Output Parameters:
 14: .  ne - number of edges of this star graph
 15: .  edgelist - list of edges for this star graph, this is a one dimensional array with pairs of entries being the two vertices (in global numbering of the vertices) of each edge,
 16:               [first vertex of first edge, second vertex of first edge, first vertex of second edge, second vertex of second edge, etc].

 18:               User is responsible for deallocating this memory.
 19: */
 20: PetscErrorCode StarGraphCreateEdgeList(PetscInt k, PetscBool directin, PetscInt *ne, PetscInt *edgelist[])
 21: {
 22:   PetscFunctionBegin;
 23:   *ne = k;
 24:   PetscCall(PetscCalloc1(2 * k, edgelist));

 26:   if (directin) {
 27:     for (PetscInt i = 0; i < k; i++) {
 28:       (*edgelist)[2 * i]     = i + 1;
 29:       (*edgelist)[2 * i + 1] = 0;
 30:     }
 31:   } else {
 32:     for (PetscInt i = 0; i < k; i++) {
 33:       (*edgelist)[2 * i]     = 0;
 34:       (*edgelist)[2 * i + 1] = i + 1;
 35:     }
 36:   }
 37:   PetscFunctionReturn(PETSC_SUCCESS);
 38: }

 40: /*
 41: CreateSimpleStarGraph - Create a Distributed k-Star Graph DMNetwork with a single PetscInt component on
 42: all edges and vertices, a selectable number of dofs on vertices and edges. Intended mostly to be used for testing purposes.

 44:   Input Parameters:
 45: . comm       - the communicator of the dm
 46: . numdofvert - number of degrees of freedom (dofs) on vertices
 47: . numdofedge - number of degrees of freedom (dofs) on edges
 48: . k          - order of the star graph (number of edges)
 49: . directin   - if true direction of edges is towards the center vertex, otherwise they are directed out of the center vertex

 51:   Output Parameter:
 52: . newdm       - The created and distributed simple Star Graph
 53: */
 54: PetscErrorCode StarGraphCreate(MPI_Comm comm, PetscInt numdofvert, PetscInt numdofedge, PetscInt k, PetscBool directin, DM *newdm)
 55: {
 56:   DM          dm;
 57:   PetscMPIInt rank;
 58:   PetscInt    ne       = 0, compkey, eStart, eEnd, vStart, vEnd, e, v;
 59:   PetscInt   *edgelist = NULL, *compedge, *compvert;

 61:   PetscFunctionBegin;
 62:   PetscCall(DMNetworkCreate(comm, &dm));
 63:   PetscCall(DMNetworkSetNumSubNetworks(dm, PETSC_DECIDE, 1));
 64:   PetscCallMPI(MPI_Comm_rank(comm, &rank));
 65:   if (rank == 0) PetscCall(StarGraphCreateEdgeList(k, directin, &ne, &edgelist));
 66:   PetscCall(DMNetworkAddSubnetwork(dm, "Main", ne, edgelist, NULL));
 67:   PetscCall(DMNetworkRegisterComponent(dm, "unused", sizeof(PetscInt), &compkey));
 68:   PetscCall(DMNetworkLayoutSetUp(dm));
 69:   PetscCall(PetscFree(edgelist));
 70:   PetscCall(DMNetworkGetEdgeRange(dm, &eStart, &eEnd));
 71:   PetscCall(DMNetworkGetVertexRange(dm, &vStart, &vEnd));
 72:   PetscCall(PetscMalloc2(eEnd - eStart, &compedge, vEnd - vStart, &compvert));
 73:   for (e = eStart; e < eEnd; e++) {
 74:     compedge[e - eStart] = e;
 75:     PetscCall(DMNetworkAddComponent(dm, e, compkey, &compedge[e - eStart], numdofedge));
 76:   }
 77:   for (v = vStart; v < vEnd; v++) {
 78:     compvert[v - vStart] = v;
 79:     PetscCall(DMNetworkAddComponent(dm, v, compkey, &compvert[v - vStart], numdofvert));
 80:   }
 81:   PetscCall(DMSetFromOptions(dm));
 82:   PetscCall(DMViewFromOptions(dm, NULL, "-dm_view"));
 83:   PetscCall(DMSetUp(dm));
 84:   PetscCall(PetscFree2(compedge, compvert));
 85:   *newdm = dm;
 86:   PetscFunctionReturn(PETSC_SUCCESS);
 87: }

 89: PetscErrorCode StarGraphTestQuery(DM dm, PetscInt ne)
 90: {
 91:   PetscInt globalnumvert, localnumvert, globalnumedge, localnumedge;

 93:   PetscFunctionBegin;
 94:   PetscCall(DMNetworkGetNumEdges(dm, &localnumedge, &globalnumedge));
 95:   PetscCall(DMNetworkGetNumVertices(dm, &localnumvert, &globalnumvert));

 97:   PetscCheck(globalnumedge == ne, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Global number of edges should be %" PetscInt_FMT "instead was %" PetscInt_FMT, ne, globalnumedge);
 98:   PetscCheck(globalnumvert == ne + 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Global number of vertices should be %" PetscInt_FMT "instead was %" PetscInt_FMT, ne + 1, globalnumvert);
 99:   PetscFunctionReturn(PETSC_SUCCESS);
100: }

102: int main(int argc, char **argv)
103: {
104:   DM          dm;
105:   PetscInt    ne = 1;
106:   PetscMPIInt rank;

108:   PetscFunctionBeginUser;
109:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
110:   PetscCallMPI(MPI_Comm_rank(MPI_COMM_WORLD, &rank));
111:   /* create a distributed k-Star graph DMNetwork */
112:   PetscCall(PetscOptionsGetInt(NULL, NULL, "-ne", &ne, NULL));
113:   PetscCall(StarGraphCreate(PETSC_COMM_WORLD, 1, 0, ne, PETSC_TRUE, &dm));
114:   PetscCall(DMNetworkDistribute(&dm, 0));
115:   /* Test if query functions for DMNetwork run successfully */
116:   PetscCall(StarGraphTestQuery(dm, ne));
117:   PetscCall(DMDestroy(&dm));
118:   PetscCall(PetscFinalize());
119: }

121: /*TEST
122:   test:
123:     suffix: 0
124:     args: -ne 5
125:     output_file: output/empty.out
126:   test:
127:     suffix: 1
128:     nsize: 2
129:     args: -ne 5 -petscpartitioner_type simple
130:     output_file: output/empty.out
131:  TEST*/