Actual source code: network.c
1: #include <petsc/private/dmnetworkimpl.h>
3: PetscLogEvent DMNetwork_LayoutSetUp;
4: PetscLogEvent DMNetwork_SetUpNetwork;
5: PetscLogEvent DMNetwork_Distribute;
7: /*
8: Creates the component header and value objects for a network point
9: */
10: static PetscErrorCode SetUpNetworkHeaderComponentValue(DM dm, DMNetworkComponentHeader header, DMNetworkComponentValue cvalue)
11: {
12: PetscFunctionBegin;
13: /* Allocate arrays for component information */
14: PetscCall(PetscCalloc5(header->maxcomps, &header->size, header->maxcomps, &header->key, header->maxcomps, &header->offset, header->maxcomps, &header->nvar, header->maxcomps, &header->offsetvarrel));
15: PetscCall(PetscCalloc1(header->maxcomps, &cvalue->data));
17: /* The size of the header is the size of struct _p_DMNetworkComponentHeader. Since the struct contains PetscInt pointers we cannot use sizeof(struct). So, we need to explicitly calculate the size.
18: If the data header struct changes then this header size calculation needs to be updated. */
19: header->hsize = sizeof(struct _p_DMNetworkComponentHeader) + 5 * header->maxcomps * sizeof(PetscInt);
20: header->hsize /= sizeof(DMNetworkComponentGenericDataType);
21: #if defined(__NEC__)
22: /* NEC/LG: quick hack to keep data aligned on 8 bytes. */
23: header->hsize = (header->hsize + (8 - 1)) & ~(8 - 1);
24: #endif
25: PetscFunctionReturn(PETSC_SUCCESS);
26: }
28: PetscErrorCode DMNetworkInitializeHeaderComponentData(DM dm)
29: {
30: DM_Network *network = (DM_Network *)dm->data;
31: PetscInt np, p, defaultnumcomp = DMNETWORK_MAX_COMP_AT_POINT_DEFAULT;
33: PetscFunctionBegin;
34: np = network->cloneshared->pEnd - network->cloneshared->pStart;
35: if (network->header)
36: for (p = 0; p < np; p++) {
37: network->header[p].maxcomps = defaultnumcomp;
38: PetscCall(SetUpNetworkHeaderComponentValue(dm, &network->header[p], &network->cvalue[p]));
39: }
40: PetscFunctionReturn(PETSC_SUCCESS);
41: }
43: /*@
44: DMNetworkGetPlex - Gets the `DMPLEX` associated with this `DMNETWORK`
46: Not Collective
48: Input Parameter:
49: . dm - the `DMNETWORK` object
51: Output Parameter:
52: . plexdm - the `DMPLEX` object
54: Level: advanced
56: .seealso: `DM`, `DMNETWORK`, `DMPLEX`, `DMNetworkCreate()`
57: @*/
58: PetscErrorCode DMNetworkGetPlex(DM dm, DM *plexdm)
59: {
60: DM_Network *network = (DM_Network *)dm->data;
62: PetscFunctionBegin;
63: *plexdm = network->plex;
64: PetscFunctionReturn(PETSC_SUCCESS);
65: }
67: /*@
68: DMNetworkGetNumSubNetworks - Gets the number of subnetworks
70: Not Collective
72: Input Parameter:
73: . dm - the `DMNETWORK` object
75: Output Parameters:
76: + nsubnet - local number of subnetworks, pass `NULL` if not needed
77: - Nsubnet - global number of subnetworks, pass `NULL` if not needed
79: Level: beginner
81: .seealso: `DM`, `DMNETWORK`, `DMNetworkCreate()`, `DMNetworkSetNumSubNetworks()`
82: @*/
83: PetscErrorCode DMNetworkGetNumSubNetworks(DM dm, PeOp PetscInt *nsubnet, PeOp PetscInt *Nsubnet)
84: {
85: DM_Network *network = (DM_Network *)dm->data;
87: PetscFunctionBegin;
88: if (nsubnet) *nsubnet = network->cloneshared->nsubnet;
89: if (Nsubnet) *Nsubnet = network->cloneshared->Nsubnet;
90: PetscFunctionReturn(PETSC_SUCCESS);
91: }
93: /*@
94: DMNetworkSetNumSubNetworks - Sets the number of subnetworks
96: Collective
98: Input Parameters:
99: + dm - the `DMNETWORK` object
100: . nsubnet - local number of subnetworks
101: - Nsubnet - global number of subnetworks
103: Level: beginner
105: .seealso: `DM`, `DMNETWORK`, `DMNetworkCreate()`, `DMNetworkGetNumSubNetworks()`
106: @*/
107: PetscErrorCode DMNetworkSetNumSubNetworks(DM dm, PetscInt nsubnet, PetscInt Nsubnet)
108: {
109: DM_Network *network = (DM_Network *)dm->data;
111: PetscFunctionBegin;
112: PetscCheck(network->cloneshared->Nsubnet == 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_INCOMP, "Network sizes already set, cannot resize the network");
118: if (Nsubnet == PETSC_DECIDE) {
119: PetscCheck(nsubnet >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Number of local subnetworks %" PetscInt_FMT " cannot be less than 0", nsubnet);
120: PetscCallMPI(MPIU_Allreduce(&nsubnet, &Nsubnet, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)dm)));
121: }
122: PetscCheck(Nsubnet >= 1, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_INCOMP, "Number of global subnetworks %" PetscInt_FMT " cannot be less than 1", Nsubnet);
124: network->cloneshared->Nsubnet = Nsubnet;
125: network->cloneshared->nsubnet = 0; /* initial value; will be determined by DMNetworkAddSubnetwork() */
126: PetscCall(PetscCalloc1(Nsubnet, &network->cloneshared->subnet));
128: /* num of shared vertices */
129: network->cloneshared->nsvtx = 0;
130: network->cloneshared->Nsvtx = 0;
131: PetscFunctionReturn(PETSC_SUCCESS);
132: }
134: /*@
135: DMNetworkAddSubnetwork - Add a subnetwork
137: Collective
139: Input Parameters:
140: + dm - the `DMNETWORK` object
141: . name - name of the subnetwork
142: . ne - number of local edges of this subnetwork
143: - edgelist - list of edges for this subnetwork, this is a one dimensional array with pairs of entries being the two vertices (in global numbering
144: of the vertices) of each edge: [first vertex of first edge, second vertex of first edge, first vertex of second edge, second vertex of second edge, etc.]
146: Output Parameter:
147: . netnum - global index of the subnetwork
149: Level: beginner
151: Notes:
152: There is no copy involved in this operation, only the pointer is referenced. The `edgelist` should
153: not be destroyed before the call to `DMNetworkLayoutSetUp()`
155: A network can comprise of a single subnetwork OR multiple subnetworks. For a single subnetwork, the subnetwork can be read either in serial or parallel.
156: For a multiple subnetworks,
157: each subnetwork topology needs to be set on a unique MPI process and the communicator size needs to be at least equal to the number of subnetworks.
159: Example usage:
160: Consider the following networks\:
161: 1) A single subnetwork\:
162: .vb
163: network 0:
164: rank[0]:
165: v0 --> v2; v1 --> v2
166: rank[1]:
167: v3 --> v5; v4 --> v5
168: .ve
170: The resulting input network 0\:
171: .vb
172: rank[0]:
173: ne = 2
174: edgelist = [0 2 | 1 2]
176: rank[1]:
177: ne = 2
178: edgelist = [3 5 | 4 5]
179: .ve
180: 2) Two subnetworks\:
181: .vb
182: subnetwork 0:
183: rank[0]:
184: v0 --> v2; v2 --> v1; v1 --> v3;
185: subnetwork 1:
186: rank[1]:
187: v0 --> v3; v3 --> v2; v2 --> v1;
188: .ve
190: The resulting input subnetwork 0\:
191: .vb
192: rank[0]:
193: ne = 3
194: edgelist = [0 2 | 2 1 | 1 3]
196: rank[1]:
197: ne = 0
198: edgelist = NULL
199: .ve
200: subnetwork 1\:
201: .vb
202: rank[0]:
203: ne = 0
204: edgelist = NULL
206: rank[1]:
207: edgelist = [0 3 | 3 2 | 2 1]
208: .ve
210: .seealso: `DM`, `DMNETWORK`, `DMNetworkCreate()`, `DMNetworkSetNumSubnetworks()`
211: @*/
212: PetscErrorCode DMNetworkAddSubnetwork(DM dm, const char *name, PetscInt ne, PetscInt edgelist[], PetscInt *netnum)
213: {
214: DM_Network *network = (DM_Network *)dm->data;
215: PetscInt i, Nedge, j, Nvtx, nvtx, nvtx_min = -1, nvtx_max = 0;
216: PetscBT table;
218: PetscFunctionBegin;
219: for (i = 0; i < ne; i++) PetscCheck(edgelist[2 * i] != edgelist[2 * i + 1], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Edge %" PetscInt_FMT " has the same vertex %" PetscInt_FMT " at each endpoint", i, edgelist[2 * i]);
221: i = 0;
222: if (ne) nvtx_min = nvtx_max = edgelist[0];
223: for (j = 0; j < ne; j++) {
224: nvtx_min = PetscMin(nvtx_min, edgelist[i]);
225: nvtx_max = PetscMax(nvtx_max, edgelist[i]);
226: i++;
227: nvtx_min = PetscMin(nvtx_min, edgelist[i]);
228: nvtx_max = PetscMax(nvtx_max, edgelist[i]);
229: i++;
230: }
231: Nvtx = nvtx_max - nvtx_min + 1; /* approximated total local nvtx for this subnet */
233: /* Get exact local nvtx for this subnet: counting local values between nvtx_min and nvtx_max */
234: PetscCall(PetscBTCreate(Nvtx, &table));
235: PetscCall(PetscBTMemzero(Nvtx, table));
236: i = 0;
237: for (j = 0; j < ne; j++) {
238: PetscCall(PetscBTSet(table, edgelist[i++] - nvtx_min));
239: PetscCall(PetscBTSet(table, edgelist[i++] - nvtx_min));
240: }
241: nvtx = 0;
242: for (j = 0; j < Nvtx; j++) {
243: if (PetscBTLookup(table, j)) nvtx++;
244: }
246: /* Get global total Nvtx = max(edgelist[])+1 for this subnet */
247: PetscCallMPI(MPIU_Allreduce(&nvtx_max, &Nvtx, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
248: Nvtx++;
249: PetscCall(PetscBTDestroy(&table));
251: /* Get global total Nedge for this subnet */
252: PetscCallMPI(MPIU_Allreduce(&ne, &Nedge, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)dm)));
254: i = network->cloneshared->nsubnet;
255: if (name) PetscCall(PetscStrncpy(network->cloneshared->subnet[i].name, name, sizeof(network->cloneshared->subnet[i].name)));
256: network->cloneshared->subnet[i].nvtx = nvtx; /* include ghost vertices */
257: network->cloneshared->subnet[i].nedge = ne;
258: network->cloneshared->subnet[i].edgelist = edgelist;
259: network->cloneshared->subnet[i].Nvtx = Nvtx;
260: network->cloneshared->subnet[i].Nedge = Nedge;
262: /* ----------------------------------------------------------
263: p=v or e;
264: subnet[0].pStart = 0
265: subnet[i+1].pStart = subnet[i].pEnd = subnet[i].pStart + (nE[i] or NV[i])
266: ----------------------------------------------------------------------- */
267: /* GLOBAL subnet[].vStart and vEnd, used by DMNetworkLayoutSetUp() */
268: network->cloneshared->subnet[i].vStart = network->cloneshared->NVertices;
269: network->cloneshared->subnet[i].vEnd = network->cloneshared->subnet[i].vStart + network->cloneshared->subnet[i].Nvtx; /* global vEnd of subnet[i] */
271: network->cloneshared->nVertices += nvtx; /* include ghost vertices */
272: network->cloneshared->NVertices += network->cloneshared->subnet[i].Nvtx;
274: /* LOCAL subnet[].eStart and eEnd, used by DMNetworkLayoutSetUp() */
275: network->cloneshared->subnet[i].eStart = network->cloneshared->nEdges;
276: network->cloneshared->subnet[i].eEnd = network->cloneshared->subnet[i].eStart + ne;
277: network->cloneshared->nEdges += ne;
278: network->cloneshared->NEdges += network->cloneshared->subnet[i].Nedge;
280: PetscCall(PetscStrncpy(network->cloneshared->subnet[i].name, name, sizeof(network->cloneshared->subnet[i].name)));
281: if (netnum) *netnum = network->cloneshared->nsubnet;
282: network->cloneshared->nsubnet++;
283: PetscFunctionReturn(PETSC_SUCCESS);
284: }
286: /*@C
287: DMNetworkSharedVertexGetInfo - Get info of a shared vertex struct, see petsc/private/dmnetworkimpl.h
289: Not Collective
291: Input Parameters:
292: + dm - the `DM` object
293: - v - vertex point
295: Output Parameters:
296: + gidx - global number of this shared vertex in the internal dmplex, pass `NULL` if not needed
297: . n - number of subnetworks that share this vertex, pass `NULL` if not needed
298: - sv - array of size `n`: sv[2*i,2*i+1]=(net[i], idx[i]), i=0,...,n-1, pass `NULL` if not needed
300: Level: intermediate
302: .seealso: `DM`, `DMNETWORK`, `DMNetworkGetSharedVertices()`
303: @*/
304: PetscErrorCode DMNetworkSharedVertexGetInfo(DM dm, PetscInt v, PeOp PetscInt *gidx, PeOp PetscInt *n, const PeOp PetscInt *sv[])
305: {
306: DM_Network *network = (DM_Network *)dm->data;
307: SVtx *svtx = network->cloneshared->svtx;
308: PetscInt i, gidx_tmp;
310: PetscFunctionBegin;
311: PetscCall(DMNetworkGetGlobalVertexIndex(dm, v, &gidx_tmp));
312: PetscCall(PetscHMapIGetWithDefault(network->cloneshared->svtable, gidx_tmp + 1, 0, &i));
313: PetscCheck(i > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "input vertex is not a shared vertex");
315: i--;
316: if (gidx) *gidx = gidx_tmp;
317: if (n) *n = svtx[i].n;
318: if (sv) *sv = svtx[i].sv;
319: PetscFunctionReturn(PETSC_SUCCESS);
320: }
322: /*
323: VtxGetInfo - Get info of an input vertex=(net,idx)
325: Input Parameters:
326: + Nsvtx - global num of shared vertices
327: . svtx - array of shared vertices (global)
328: - (net,idx) - subnet number and local index for a vertex
330: Output Parameters:
331: + gidx - global index of (net,idx)
332: . svtype - see petsc/private/dmnetworkimpl.h
333: - svtx_idx - ordering in the svtx array
334: */
335: static inline PetscErrorCode VtxGetInfo(PetscInt Nsvtx, SVtx *svtx, PetscInt net, PetscInt idx, PetscInt *gidx, SVtxType *svtype, PetscInt *svtx_idx)
336: {
337: PetscInt i, j, *svto, g_idx;
338: SVtxType vtype;
340: PetscFunctionBegin;
341: if (!Nsvtx) PetscFunctionReturn(PETSC_SUCCESS);
343: g_idx = -1;
344: vtype = SVNONE;
346: for (i = 0; i < Nsvtx; i++) {
347: if (net == svtx[i].sv[0] && idx == svtx[i].sv[1]) {
348: g_idx = svtx[i].gidx;
349: vtype = SVFROM;
350: } else { /* loop over svtx[i].n */
351: for (j = 1; j < svtx[i].n; j++) {
352: svto = svtx[i].sv + 2 * j;
353: if (net == svto[0] && idx == svto[1]) {
354: /* input vertex net.idx is a shared to_vertex, output its global index and its svtype */
355: g_idx = svtx[i].gidx; /* output gidx for to_vertex */
356: vtype = SVTO;
357: }
358: }
359: }
360: if (vtype != SVNONE) break;
361: }
362: if (gidx) *gidx = g_idx;
363: if (svtype) *svtype = vtype;
364: if (svtx_idx) *svtx_idx = i;
365: PetscFunctionReturn(PETSC_SUCCESS);
366: }
368: /*
369: TableAddSVtx - Add a new shared vertice from sedgelist[k] to a ctable svta
371: Input: network, sedgelist, k, svta
372: Output: svta, tdata, ta2sv
373: */
374: static inline PetscErrorCode TableAddSVtx(DM_Network *network, PetscInt *sedgelist, PetscInt k, PetscHMapI svta, PetscInt *tdata, PetscInt *ta2sv)
375: {
376: PetscInt net, idx, gidx;
378: PetscFunctionBegin;
379: net = sedgelist[k];
380: idx = sedgelist[k + 1];
381: gidx = network->cloneshared->subnet[net].vStart + idx;
382: PetscCall(PetscHMapISet(svta, gidx + 1, *tdata + 1));
384: ta2sv[*tdata] = k; /* maps tdata to index of sedgelist */
385: (*tdata)++;
386: PetscFunctionReturn(PETSC_SUCCESS);
387: }
389: /*
390: SharedVtxCreate - Create an array of global shared vertices. See SVtx and SVtxType in dmnetworkimpl.h
392: Input: dm, Nsedgelist, sedgelist
394: Note: Output svtx is organized as
395: sv(net[0],idx[0]) --> sv(net[1],idx[1])
396: --> sv(net[1],idx[1])
397: ...
398: --> sv(net[n-1],idx[n-1])
399: and net[0] < net[1] < ... < net[n-1]
400: where sv[0] has SVFROM type, sv[i], i>0, has SVTO type.
401: */
402: static PetscErrorCode SharedVtxCreate(DM dm, PetscInt Nsedgelist, PetscInt *sedgelist)
403: {
404: SVtx *svtx = NULL;
405: PetscInt *sv, k, j, nsv, *tdata, **ta2sv;
406: PetscHMapI *svtas;
407: PetscInt gidx, net, idx, i, nta, ita, idx_from, idx_to, n, *net_tmp, *idx_tmp, *gidx_tmp;
408: DM_Network *network = (DM_Network *)dm->data;
409: PetscHashIter ppos;
411: PetscFunctionBegin;
412: /* (1) Crete an array of ctables svtas to map (net,idx) -> gidx; a svtas[] for a shared/merged vertex */
413: PetscCall(PetscCalloc3(Nsedgelist, &svtas, Nsedgelist, &tdata, 2 * Nsedgelist, &ta2sv));
415: k = 0; /* sedgelist vertex counter j = 4*k */
416: nta = 0; /* num of svta tables created = num of groups of shared vertices */
418: /* for j=0 */
419: PetscCall(PetscHMapICreateWithSize(2 * Nsedgelist, svtas + nta));
420: PetscCall(PetscMalloc1(2 * Nsedgelist, &ta2sv[nta]));
422: PetscCall(TableAddSVtx(network, sedgelist, k, svtas[nta], &tdata[nta], ta2sv[nta]));
423: PetscCall(TableAddSVtx(network, sedgelist, k + 2, svtas[nta], &tdata[nta], ta2sv[nta]));
424: nta++;
425: k += 4;
427: for (j = 1; j < Nsedgelist; j++) { /* j: sedgelist counter */
428: for (ita = 0; ita < nta; ita++) {
429: /* vfrom */
430: net = sedgelist[k];
431: idx = sedgelist[k + 1];
432: gidx = network->cloneshared->subnet[net].vStart + idx; /* global index of the vertex net.idx before merging shared vertices */
433: PetscCall(PetscHMapIGetWithDefault(svtas[ita], gidx + 1, 0, &idx_from));
435: /* vto */
436: net = sedgelist[k + 2];
437: idx = sedgelist[k + 3];
438: gidx = network->cloneshared->subnet[net].vStart + idx;
439: PetscCall(PetscHMapIGetWithDefault(svtas[ita], gidx + 1, 0, &idx_to));
441: if (idx_from || idx_to) { /* vfrom or vto is on table svtas[ita] */
442: idx_from--;
443: idx_to--;
444: if (idx_from < 0) { /* vto is on svtas[ita] */
445: PetscCall(TableAddSVtx(network, sedgelist, k, svtas[ita], &tdata[ita], ta2sv[ita]));
446: break;
447: } else if (idx_to < 0) {
448: PetscCall(TableAddSVtx(network, sedgelist, k + 2, svtas[ita], &tdata[ita], ta2sv[ita]));
449: break;
450: }
451: }
452: }
454: if (ita == nta) {
455: PetscCall(PetscHMapICreateWithSize(2 * Nsedgelist, svtas + nta));
456: PetscCall(PetscMalloc1(2 * Nsedgelist, &ta2sv[nta]));
458: PetscCall(TableAddSVtx(network, sedgelist, k, svtas[nta], &tdata[nta], ta2sv[nta]));
459: PetscCall(TableAddSVtx(network, sedgelist, k + 2, svtas[nta], &tdata[nta], ta2sv[nta]));
460: nta++;
461: }
462: k += 4;
463: }
465: /* (2) Create svtable for query shared vertices using gidx */
466: PetscCall(PetscHMapICreateWithSize(nta, &network->cloneshared->svtable));
468: /* (3) Construct svtx from svtas
469: svtx: array of SVtx: sv[0]=(net[0],idx[0]) to vertices sv[k], k=1,...,n-1. */
470: PetscCall(PetscMalloc1(nta, &svtx));
471: for (nsv = 0; nsv < nta; nsv++) {
472: /* for a single svtx, put shared vertices in ascending order of gidx */
473: PetscCall(PetscHMapIGetSize(svtas[nsv], &n));
474: PetscCall(PetscCalloc1(2 * n, &sv));
475: PetscCall(PetscMalloc3(n, &gidx_tmp, n, &net_tmp, n, &idx_tmp));
476: svtx[nsv].sv = sv;
477: svtx[nsv].n = n;
478: svtx[nsv].gidx = network->cloneshared->NVertices; /* initialization */
480: PetscHashIterBegin(svtas[nsv], ppos);
481: for (k = 0; k < n; k++) { /* gidx is sorted in ascending order */
482: PetscHashIterGetKey(svtas[nsv], ppos, gidx);
483: PetscHashIterGetVal(svtas[nsv], ppos, i);
484: PetscHashIterNext(svtas[nsv], ppos);
485: gidx--;
486: i--;
487: j = ta2sv[nsv][i]; /* maps i to index of sedgelist */
488: net_tmp[k] = sedgelist[j]; /* subnet number */
489: idx_tmp[k] = sedgelist[j + 1]; /* index on the subnet */
490: gidx_tmp[k] = gidx; /* gidx in un-merged dmnetwork */
491: }
493: /* current implementation requires sv[]=[net,idx] in ascending order of its gidx in un-merged dmnetwork */
494: PetscCall(PetscSortIntWithArrayPair(n, gidx_tmp, net_tmp, idx_tmp));
495: svtx[nsv].gidx = gidx_tmp[0]; /* = min(gidx) */
496: for (k = 0; k < n; k++) {
497: sv[2 * k] = net_tmp[k];
498: sv[2 * k + 1] = idx_tmp[k];
499: }
500: PetscCall(PetscFree3(gidx_tmp, net_tmp, idx_tmp));
502: /* Setup svtable for query shared vertices */
503: PetscCall(PetscHMapISet(network->cloneshared->svtable, svtx[nsv].gidx + 1, nsv + 1));
504: }
506: for (j = 0; j < nta; j++) {
507: PetscCall(PetscHMapIDestroy(svtas + j));
508: PetscCall(PetscFree(ta2sv[j]));
509: }
510: PetscCall(PetscFree3(svtas, tdata, ta2sv));
512: network->cloneshared->Nsvtx = nta;
513: network->cloneshared->svtx = svtx;
514: PetscFunctionReturn(PETSC_SUCCESS);
515: }
517: /*
518: GetEdgelist_Coupling - Get an integrated edgelist for dmplex from user-provided subnet[].edgelist when subnets are coupled by shared vertices
520: Input Parameters:
521: . dm - the dmnetwork object
523: Output Parameters:
524: + edges - the integrated edgelist for dmplex
525: - nmerged_ptr - num of vertices being merged
526: */
527: static PetscErrorCode GetEdgelist_Coupling(DM dm, PetscInt *edges, PetscInt *nmerged_ptr)
528: {
529: MPI_Comm comm;
530: PetscMPIInt size, rank;
531: DM_Network *network = (DM_Network *)dm->data;
532: PetscInt i, j, ctr, np;
533: PetscInt *vidxlTog, Nsv, Nsubnet = network->cloneshared->Nsubnet;
534: PetscInt *sedgelist = network->cloneshared->sedgelist, vrange;
535: PetscInt net, idx, gidx, nmerged, gidx_from, net_from, sv_idx;
536: SVtxType svtype = SVNONE;
537: SVtx *svtx;
539: PetscFunctionBegin;
540: PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
541: PetscCallMPI(MPI_Comm_rank(comm, &rank));
542: PetscCallMPI(MPI_Comm_size(comm, &size));
544: /* (1) Create global svtx[] from sedgelist */
545: PetscCall(SharedVtxCreate(dm, network->cloneshared->Nsvtx, sedgelist));
546: Nsv = network->cloneshared->Nsvtx;
547: svtx = network->cloneshared->svtx;
549: /* (2) Merge shared vto vertices to their vfrom vertex with same global vertex index (gidx) */
550: /* (2.1) compute vrage[rank]: global index of 1st local vertex in proc[rank] */
551: PetscCall(PetscMalloc1(network->cloneshared->nVertices, &vidxlTog));
553: PetscCallMPI(MPI_Scan(&network->cloneshared->nVertices, &vrange, 1, MPIU_INT, MPI_SUM, comm));
554: vrange -= network->cloneshared->nVertices;
556: /* (2.2) Create vidxlTog: maps UN-MERGED local vertex index i to global index gidx (plex, excluding ghost vertices) */
557: i = 0;
558: gidx = 0;
559: nmerged = 0; /* local num of merged vertices */
560: network->cloneshared->nsvtx = 0; /* local num of SVtx structs, including ghosts */
561: for (net = 0; net < Nsubnet; net++) {
562: for (idx = 0; idx < network->cloneshared->subnet[net].Nvtx; idx++) { /* Note: global subnet[net].Nvtx */
563: PetscCall(VtxGetInfo(Nsv, svtx, net, idx, &gidx_from, &svtype, &sv_idx));
564: if (svtype == SVTO) {
565: if (network->cloneshared->subnet[net].nvtx) { /* this proc owns sv_to */
566: net_from = svtx[sv_idx].sv[0]; /* subnet number of its shared vertex */
567: if (network->cloneshared->subnet[net_from].nvtx == 0) {
568: /* this proc does not own v_from, thus a ghost local vertex */
569: network->cloneshared->nsvtx++;
570: }
571: vidxlTog[i++] = gidx_from; /* gidx before merging! Bug??? */
572: nmerged++; /* a shared vertex -- merged */
573: }
574: } else {
575: if (svtype == SVFROM && network->cloneshared->subnet[net].nvtx) {
576: /* this proc owns this v_from, a new local shared vertex */
577: network->cloneshared->nsvtx++;
578: }
579: if (network->cloneshared->subnet[net].nvtx) vidxlTog[i++] = gidx;
580: gidx++;
581: }
582: }
583: }
584: PetscAssert(i == network->cloneshared->nVertices, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "%" PetscInt_FMT " != %" PetscInt_FMT " nVertices", i, network->cloneshared->nVertices);
586: /* (2.3) Shared vertices in the subnetworks are merged, update global NVertices: np = sum(local nmerged) */
587: PetscCallMPI(MPIU_Allreduce(&nmerged, &np, 1, MPIU_INT, MPI_SUM, comm));
588: network->cloneshared->NVertices -= np;
590: ctr = 0;
591: for (net = 0; net < Nsubnet; net++) {
592: for (j = 0; j < network->cloneshared->subnet[net].nedge; j++) {
593: /* vfrom: */
594: i = network->cloneshared->subnet[net].edgelist[2 * j] + (network->cloneshared->subnet[net].vStart - vrange);
595: edges[2 * ctr] = vidxlTog[i];
597: /* vto */
598: i = network->cloneshared->subnet[net].edgelist[2 * j + 1] + (network->cloneshared->subnet[net].vStart - vrange);
599: edges[2 * ctr + 1] = vidxlTog[i];
600: ctr++;
601: }
602: }
603: PetscCall(PetscFree(vidxlTog));
604: PetscCall(PetscFree(sedgelist)); /* created in DMNetworkAddSharedVertices() */
606: *nmerged_ptr = nmerged;
607: PetscFunctionReturn(PETSC_SUCCESS);
608: }
610: PetscErrorCode DMNetworkInitializeNonTopological(DM dm)
611: {
612: DM_Network *network = (DM_Network *)dm->data;
613: PetscInt p, pStart = network->cloneshared->pStart, pEnd = network->cloneshared->pEnd;
614: MPI_Comm comm;
616: PetscFunctionBegin;
617: PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
619: PetscCall(PetscSectionCreate(comm, &network->DataSection));
620: PetscCall(PetscSectionCreate(comm, &network->DofSection));
621: PetscCall(PetscSectionSetChart(network->DataSection, pStart, pEnd));
622: PetscCall(PetscSectionSetChart(network->DofSection, pStart, pEnd));
624: PetscCall(DMNetworkInitializeHeaderComponentData(dm));
626: for (p = 0; p < pEnd - pStart; p++) {
627: network->header[p].ndata = 0;
628: network->header[p].offset[0] = 0;
629: network->header[p].offsetvarrel[0] = 0;
630: PetscCall(PetscSectionAddDof(network->DataSection, p, network->header[p].hsize));
631: }
632: PetscFunctionReturn(PETSC_SUCCESS);
633: }
635: /*@
636: DMNetworkLayoutSetUp - Sets up the bare layout (graph) for the network
638: Not Collective
640: Input Parameter:
641: . dm - the `DMNETWORK` object
643: Level: beginner
645: Notes:
646: This routine should be called after the network sizes and edgelists have been provided. It creates
647: the bare layout of the network and sets up the network to begin insertion of components.
649: All the components should be registered before calling this routine.
651: .seealso: `DM`, `DMNETWORK`, `DMNetworkSetNumSubNetworks()`, `DMNetworkAddSubnetwork()`
652: @*/
653: PetscErrorCode DMNetworkLayoutSetUp(DM dm)
654: {
655: DM_Network *network = (DM_Network *)dm->data;
656: PetscInt i, j, ctr, Nsubnet = network->cloneshared->Nsubnet, np, *edges, *subnetvtx, *subnetedge, e, v, vfrom, vto, net, globaledgeoff;
657: const PetscInt *cone;
658: MPI_Comm comm;
659: PetscMPIInt size;
660: PetscSection sectiong;
661: PetscInt nmerged = 0;
663: PetscFunctionBegin;
664: PetscCall(PetscLogEventBegin(DMNetwork_LayoutSetUp, dm, 0, 0, 0));
665: PetscCheck(network->cloneshared->nsubnet == Nsubnet, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Must call DMNetworkAddSubnetwork() %" PetscInt_FMT " times", Nsubnet);
667: /* This implementation requires user input each subnet by a single processor when Nsubnet>1, thus subnet[net].nvtx=subnet[net].Nvtx when net>0 */
668: for (net = 1; net < Nsubnet; net++) {
669: if (network->cloneshared->subnet[net].nvtx)
670: PetscCheck(network->cloneshared->subnet[net].nvtx == network->cloneshared->subnet[net].Nvtx, PETSC_COMM_SELF, PETSC_ERR_SUP, "subnetwork %" PetscInt_FMT " local num of vertices %" PetscInt_FMT " != %" PetscInt_FMT " global num", net,
671: network->cloneshared->subnet[net].nvtx, network->cloneshared->subnet[net].Nvtx);
672: }
674: PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
675: PetscCallMPI(MPI_Comm_size(comm, &size));
677: /* Create LOCAL edgelist in global vertex ordering for the network by concatenating local input edgelists of the subnetworks */
678: PetscCall(PetscCalloc1(2 * network->cloneshared->nEdges, &edges));
680: if (network->cloneshared->Nsvtx) { /* subnetworks are coupled via shared vertices */
681: PetscCall(GetEdgelist_Coupling(dm, edges, &nmerged));
682: } else { /* subnetworks are not coupled */
683: /* Create a 0-size svtable for query shared vertices */
684: PetscCall(PetscHMapICreate(&network->cloneshared->svtable));
685: ctr = 0;
686: for (i = 0; i < Nsubnet; i++) {
687: for (j = 0; j < network->cloneshared->subnet[i].nedge; j++) {
688: edges[2 * ctr] = network->cloneshared->subnet[i].vStart + network->cloneshared->subnet[i].edgelist[2 * j];
689: edges[2 * ctr + 1] = network->cloneshared->subnet[i].vStart + network->cloneshared->subnet[i].edgelist[2 * j + 1];
690: ctr++;
691: }
692: }
693: }
695: /* Create network->plex; One dimensional network, numCorners=2 */
696: PetscCall(DMCreate(comm, &network->plex));
697: PetscCall(DMSetType(network->plex, DMPLEX));
698: PetscCall(DMSetDimension(network->plex, 1));
700: if (size == 1) PetscCall(DMPlexBuildFromCellList(network->plex, network->cloneshared->nEdges, PETSC_DECIDE, 2, edges));
701: else PetscCall(DMPlexBuildFromCellListParallel(network->plex, network->cloneshared->nEdges, PETSC_DECIDE, PETSC_DECIDE, 2, edges, NULL, NULL));
703: PetscCall(DMPlexGetChart(network->plex, &network->cloneshared->pStart, &network->cloneshared->pEnd));
704: PetscCall(DMPlexGetHeightStratum(network->plex, 0, &network->cloneshared->eStart, &network->cloneshared->eEnd));
705: PetscCall(DMPlexGetHeightStratum(network->plex, 1, &network->cloneshared->vStart, &network->cloneshared->vEnd));
706: np = network->cloneshared->pEnd - network->cloneshared->pStart;
707: PetscCall(PetscCalloc2(np, &network->header, np, &network->cvalue));
709: /* Create edge and vertex arrays for the subnetworks
710: This implementation assumes that DMNetwork reads
711: (1) a single subnetwork in parallel; or
712: (2) n subnetworks using n processors, one subnetwork/processor.
713: */
714: PetscCall(PetscCalloc2(network->cloneshared->nEdges, &subnetedge, network->cloneshared->nVertices + network->cloneshared->nsvtx, &subnetvtx)); /* Maps local edge/vertex to local subnetwork's edge/vertex */
715: network->cloneshared->subnetedge = subnetedge;
716: network->cloneshared->subnetvtx = subnetvtx;
717: for (j = 0; j < Nsubnet; j++) {
718: network->cloneshared->subnet[j].edges = subnetedge;
719: subnetedge = PetscSafePointerPlusOffset(subnetedge, network->cloneshared->subnet[j].nedge);
721: network->cloneshared->subnet[j].vertices = subnetvtx;
722: subnetvtx = PetscSafePointerPlusOffset(subnetvtx, network->cloneshared->subnet[j].nvtx);
723: }
724: network->cloneshared->svertices = subnetvtx;
726: /* Get edge ownership */
727: np = network->cloneshared->eEnd - network->cloneshared->eStart;
728: PetscCallMPI(MPI_Scan(&np, &globaledgeoff, 1, MPIU_INT, MPI_SUM, comm));
729: globaledgeoff -= np;
731: /* Setup local edge and vertex arrays for subnetworks */
732: e = 0;
733: for (i = 0; i < Nsubnet; i++) {
734: ctr = 0;
735: for (j = 0; j < network->cloneshared->subnet[i].nedge; j++) {
736: /* edge e */
737: network->header[e].index = e + globaledgeoff; /* Global edge index */
738: network->header[e].subnetid = i;
739: network->cloneshared->subnet[i].edges[j] = e;
741: /* connected vertices */
742: PetscCall(DMPlexGetCone(network->plex, e, &cone));
744: /* vertex cone[0] */
745: v = cone[0];
746: network->header[v].index = edges[2 * e]; /* Global vertex index */
747: network->header[v].subnetid = i; /* Subnetwork id */
748: if (Nsubnet == 1) {
749: network->cloneshared->subnet[i].vertices[v - network->cloneshared->vStart] = v; /* user's subnet[].idx = petsc's v */
750: } else {
751: vfrom = network->cloneshared->subnet[i].edgelist[2 * ctr]; /* =subnet[i].idx, Global index! */
752: network->cloneshared->subnet[i].vertices[vfrom] = v; /* user's subnet[].dix = petsc's v */
753: }
755: /* vertex cone[1] */
756: v = cone[1];
757: network->header[v].index = edges[2 * e + 1]; /* Global vertex index */
758: network->header[v].subnetid = i; /* Subnetwork id */
759: if (Nsubnet == 1) {
760: network->cloneshared->subnet[i].vertices[v - network->cloneshared->vStart] = v; /* user's subnet[].idx = petsc's v */
761: } else {
762: vto = network->cloneshared->subnet[i].edgelist[2 * ctr + 1]; /* =subnet[i].idx, Global index! */
763: network->cloneshared->subnet[i].vertices[vto] = v; /* user's subnet[].dix = petsc's v */
764: }
766: e++;
767: ctr++;
768: }
769: }
770: PetscCall(PetscFree(edges));
772: /* Set local vertex array for the subnetworks */
773: j = 0;
774: for (v = network->cloneshared->vStart; v < network->cloneshared->vEnd; v++) {
775: /* local shared vertex */
776: PetscCall(PetscHMapIGetWithDefault(network->cloneshared->svtable, network->header[v].index + 1, 0, &i));
777: if (i) network->cloneshared->svertices[j++] = v;
778: }
780: /* Create a global section to be used by DMNetworkIsGhostVertex() which is a non-collective routine */
781: /* see snes_tutorials_network-ex1_4 */
782: PetscCall(DMGetGlobalSection(network->plex, §iong));
783: /* Initialize non-topological data structures */
784: PetscCall(DMNetworkInitializeNonTopological(dm));
785: PetscCall(PetscLogEventEnd(DMNetwork_LayoutSetUp, dm, 0, 0, 0));
786: PetscFunctionReturn(PETSC_SUCCESS);
787: }
789: /*@C
790: DMNetworkGetSubnetwork - Returns the information about a requested subnetwork
792: Not Collective
794: Input Parameters:
795: + dm - the `DMNETWORK` object
796: - netnum - the global index of the subnetwork
798: Output Parameters:
799: + nv - number of vertices (local)
800: . ne - number of edges (local)
801: . vtx - local vertices of the subnetwork
802: - edge - local edges of the subnetwork
804: Level: intermediate
806: Notes:
807: Pass `NULL` for any output argument that is not needed.
809: Cannot call this routine before `DMNetworkLayoutSetup()`
811: The local vertices returned on each rank are determined by `DMNETWORK`. The user does not have any control over what vertices are local.
813: .seealso: `DM`, `DMNETWORK`, `DMNetworkCreate()`, `DMNetworkAddSubnetwork()`, `DMNetworkLayoutSetUp()`
814: @*/
815: PetscErrorCode DMNetworkGetSubnetwork(DM dm, PetscInt netnum, PeOp PetscInt *nv, PeOp PetscInt *ne, PeOp const PetscInt *vtx[], PeOp const PetscInt *edge[])
816: {
817: DM_Network *network = (DM_Network *)dm->data;
819: PetscFunctionBegin;
820: PetscCheck(netnum < network->cloneshared->Nsubnet, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Subnet index %" PetscInt_FMT " exceeds the num of subnets %" PetscInt_FMT, netnum, network->cloneshared->Nsubnet);
821: if (nv) *nv = network->cloneshared->subnet[netnum].nvtx;
822: if (ne) *ne = network->cloneshared->subnet[netnum].nedge;
823: if (vtx) *vtx = network->cloneshared->subnet[netnum].vertices;
824: if (edge) *edge = network->cloneshared->subnet[netnum].edges;
825: PetscFunctionReturn(PETSC_SUCCESS);
826: }
828: /*@
829: DMNetworkAddSharedVertices - Add shared vertices that connect two given subnetworks
831: Collective
833: Input Parameters:
834: + dm - the `DMNETWORK` object
835: . anetnum - first subnetwork global numbering returned by `DMNetworkAddSubnetwork()`
836: . bnetnum - second subnetwork global numbering returned by `DMNetworkAddSubnetwork()`
837: . nsvtx - number of vertices that are shared by the two subnetworks
838: . asvtx - vertex index in the first subnetwork
839: - bsvtx - vertex index in the second subnetwork
841: Level: beginner
843: .seealso: `DM`, `DMNETWORK`, `DMNetworkCreate()`, `DMNetworkAddSubnetwork()`, `DMNetworkGetSharedVertices()`
844: @*/
845: PetscErrorCode DMNetworkAddSharedVertices(DM dm, PetscInt anetnum, PetscInt bnetnum, PetscInt nsvtx, PetscInt asvtx[], PetscInt bsvtx[])
846: {
847: DM_Network *network = (DM_Network *)dm->data;
848: PetscInt i, nsubnet = network->cloneshared->Nsubnet, *sedgelist, Nsvtx = network->cloneshared->Nsvtx;
850: PetscFunctionBegin;
851: PetscCheck(anetnum != bnetnum, PetscObjectComm((PetscObject)dm), PETSC_ERR_USER, "Subnetworks must have different netnum");
852: PetscCheck(anetnum >= 0 && bnetnum >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_USER, "netnum cannot be negative");
853: if (!Nsvtx) {
854: /* allocate network->sedgelist to hold at most 2*nsubnet pairs of shared vertices */
855: PetscCall(PetscMalloc1(2 * 4 * nsubnet, &network->cloneshared->sedgelist));
856: }
858: sedgelist = network->cloneshared->sedgelist;
859: for (i = 0; i < nsvtx; i++) {
860: sedgelist[4 * Nsvtx] = anetnum;
861: sedgelist[4 * Nsvtx + 1] = asvtx[i];
862: sedgelist[4 * Nsvtx + 2] = bnetnum;
863: sedgelist[4 * Nsvtx + 3] = bsvtx[i];
864: Nsvtx++;
865: }
866: PetscCheck(Nsvtx <= 2 * nsubnet, PETSC_COMM_SELF, PETSC_ERR_SUP, "allocate more space for coupling edgelist");
867: network->cloneshared->Nsvtx = Nsvtx;
868: PetscFunctionReturn(PETSC_SUCCESS);
869: }
871: /*@C
872: DMNetworkGetSharedVertices - Returns the info for the shared vertices
874: Not Collective
876: Input Parameter:
877: . dm - the `DMNETWORK` object
879: Output Parameters:
880: + nsv - number of local shared vertices, pass `NULL` if not needed
881: - svtx - local shared vertices, pass `NULL` if not needed
883: Level: intermediate
885: Note:
886: Cannot call this routine before `DMNetworkLayoutSetup()`
888: .seealso: `DM`, `DMNETWORK`, `DMNetworkGetSubnetwork()`, `DMNetworkLayoutSetUp()`, `DMNetworkAddSharedVertices()`
889: @*/
890: PetscErrorCode DMNetworkGetSharedVertices(DM dm, PeOp PetscInt *nsv, PeOp const PetscInt *svtx[])
891: {
892: DM_Network *net = (DM_Network *)dm->data;
894: PetscFunctionBegin;
896: if (nsv) *nsv = net->cloneshared->nsvtx;
897: if (svtx) *svtx = net->cloneshared->svertices;
898: PetscFunctionReturn(PETSC_SUCCESS);
899: }
901: /*@C
902: DMNetworkRegisterComponent - Registers the network component
904: Logically Collective
906: Input Parameters:
907: + dm - the `DMNETWORK` object
908: . name - the component name
909: - size - the storage size in bytes for this component data
911: Output Parameter:
912: . key - an integer key that defines the component
914: Level: beginner
916: Note:
917: This routine should be called by all processors before calling `DMNetworkLayoutSetup()`.
919: .seealso: `DM`, `DMNETWORK`, `DMNetworkCreate()`, `DMNetworkLayoutSetUp()`
920: @*/
921: PetscErrorCode DMNetworkRegisterComponent(DM dm, const char *name, size_t size, PetscInt *key)
922: {
923: DM_Network *network = (DM_Network *)dm->data;
924: DMNetworkComponent *component = NULL, *newcomponent = NULL;
925: PetscBool flg = PETSC_FALSE;
927: PetscFunctionBegin;
928: if (!network->component) PetscCall(PetscCalloc1(network->max_comps_registered, &network->component));
930: for (PetscInt i = 0; i < network->ncomponent; i++) {
931: PetscCall(PetscStrcmp(network->component[i].name, name, &flg));
932: if (flg) {
933: *key = i;
934: PetscFunctionReturn(PETSC_SUCCESS);
935: }
936: }
938: if (network->ncomponent == network->max_comps_registered) {
939: /* Reached max allowed so resize component */
940: network->max_comps_registered += 2;
941: PetscCall(PetscCalloc1(network->max_comps_registered, &newcomponent));
942: /* Copy over the previous component info */
943: for (PetscInt i = 0; i < network->ncomponent; i++) {
944: PetscCall(PetscStrncpy(newcomponent[i].name, network->component[i].name, sizeof(newcomponent[i].name)));
945: newcomponent[i].size = network->component[i].size;
946: }
947: /* Free old one */
948: PetscCall(PetscFree(network->component));
949: /* Update pointer */
950: network->component = newcomponent;
951: }
953: component = &network->component[network->ncomponent];
955: PetscCall(PetscStrncpy(component->name, name, sizeof(component->name)));
956: PetscCheck((size % sizeof(DMNetworkComponentGenericDataType)) == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Size of datatype must be divisible by sizeof(DMNetworkComponentGenericDataType)");
957: PetscCall(PetscIntCast(size / sizeof(DMNetworkComponentGenericDataType), &component->size));
958: *key = network->ncomponent;
959: network->ncomponent++;
960: PetscFunctionReturn(PETSC_SUCCESS);
961: }
963: /*@
964: DMNetworkGetNumVertices - Get the local and global number of vertices for the entire network.
966: Not Collective
968: Input Parameter:
969: . dm - the `DMNETWORK` object
971: Output Parameters:
972: + nVertices - the local number of vertices, pass `NULL` if not needed
973: - NVertices - the global number of vertices, pass `NULL` if not needed
975: Level: beginner
977: .seealso: `DM`, `DMNETWORK`, `DMNetworkGetNumEdges()`
978: @*/
979: PetscErrorCode DMNetworkGetNumVertices(DM dm, PeOp PetscInt *nVertices, PeOp PetscInt *NVertices)
980: {
981: DM_Network *network = (DM_Network *)dm->data;
983: PetscFunctionBegin;
985: if (nVertices) {
986: PetscAssertPointer(nVertices, 2);
987: *nVertices = network->cloneshared->nVertices;
988: }
989: if (NVertices) {
990: PetscAssertPointer(NVertices, 3);
991: *NVertices = network->cloneshared->NVertices;
992: }
993: PetscFunctionReturn(PETSC_SUCCESS);
994: }
996: /*@
997: DMNetworkGetNumEdges - Get the local and global number of edges for the entire network.
999: Not Collective
1001: Input Parameter:
1002: . dm - the `DMNETWORK` object
1004: Output Parameters:
1005: + nEdges - the local number of edges, pass `NULL` if not needed
1006: - NEdges - the global number of edges, pass `NULL` if not needed
1008: Level: beginner
1010: .seealso: `DM`, `DMNETWORK`, `DMNetworkGetNumVertices()`
1011: @*/
1012: PetscErrorCode DMNetworkGetNumEdges(DM dm, PeOp PetscInt *nEdges, PeOp PetscInt *NEdges)
1013: {
1014: DM_Network *network = (DM_Network *)dm->data;
1016: PetscFunctionBegin;
1018: if (nEdges) {
1019: PetscAssertPointer(nEdges, 2);
1020: *nEdges = network->cloneshared->nEdges;
1021: }
1022: if (NEdges) {
1023: PetscAssertPointer(NEdges, 3);
1024: *NEdges = network->cloneshared->NEdges;
1025: }
1026: PetscFunctionReturn(PETSC_SUCCESS);
1027: }
1029: /*@
1030: DMNetworkGetVertexRange - Get the bounds [start, end) (also sometimes called the chart) for the local vertices
1032: Not Collective
1034: Input Parameter:
1035: . dm - the `DMNETWORK` object
1037: Output Parameters:
1038: + vStart - the first vertex point, pass `NULL` if not needed
1039: - vEnd - one beyond the last vertex point, pass `NULL` if not needed
1041: Level: beginner
1043: .seealso: `DM`, `DMNETWORK`, `DMNetworkGetEdgeRange()`, `DMNetworkGetSubnetwork()`
1044: @*/
1045: PetscErrorCode DMNetworkGetVertexRange(DM dm, PeOp PetscInt *vStart, PeOp PetscInt *vEnd)
1046: {
1047: DM_Network *network = (DM_Network *)dm->data;
1049: PetscFunctionBegin;
1050: if (vStart) *vStart = network->cloneshared->vStart;
1051: if (vEnd) *vEnd = network->cloneshared->vEnd;
1052: PetscFunctionReturn(PETSC_SUCCESS);
1053: }
1055: /*@
1056: DMNetworkGetEdgeRange - Get the bounds [start, end) (also sometimes called the chart) for the local edges
1058: Not Collective
1060: Input Parameter:
1061: . dm - the `DMNETWORK` object
1063: Output Parameters:
1064: + eStart - The first edge point, pass `NULL` if not needed
1065: - eEnd - One beyond the last edge point, pass `NULL` if not needed
1067: Level: beginner
1069: .seealso: `DM`, `DMNETWORK`, `DMNetworkGetVertexRange()`, `DMNetworkGetSubnetwork()`
1070: @*/
1071: PetscErrorCode DMNetworkGetEdgeRange(DM dm, PeOp PetscInt *eStart, PeOp PetscInt *eEnd)
1072: {
1073: DM_Network *network = (DM_Network *)dm->data;
1075: PetscFunctionBegin;
1077: if (eStart) *eStart = network->cloneshared->eStart;
1078: if (eEnd) *eEnd = network->cloneshared->eEnd;
1079: PetscFunctionReturn(PETSC_SUCCESS);
1080: }
1082: PetscErrorCode DMNetworkGetIndex(DM dm, PetscInt p, PetscInt *index)
1083: {
1084: DM_Network *network = (DM_Network *)dm->data;
1086: PetscFunctionBegin;
1087: if (network->header) {
1088: *index = network->header[p].index;
1089: } else {
1090: PetscInt offsetp;
1091: DMNetworkComponentHeader header;
1093: PetscCall(PetscSectionGetOffset(network->DataSection, p, &offsetp));
1094: header = (DMNetworkComponentHeader)(network->componentdataarray + offsetp);
1095: *index = header->index;
1096: }
1097: PetscFunctionReturn(PETSC_SUCCESS);
1098: }
1100: PetscErrorCode DMNetworkGetSubnetID(DM dm, PetscInt p, PetscInt *subnetid)
1101: {
1102: DM_Network *network = (DM_Network *)dm->data;
1104: PetscFunctionBegin;
1105: if (network->header) {
1106: *subnetid = network->header[p].subnetid;
1107: } else {
1108: PetscInt offsetp;
1109: DMNetworkComponentHeader header;
1111: PetscCall(PetscSectionGetOffset(network->DataSection, p, &offsetp));
1112: header = (DMNetworkComponentHeader)(network->componentdataarray + offsetp);
1113: *subnetid = header->subnetid;
1114: }
1115: PetscFunctionReturn(PETSC_SUCCESS);
1116: }
1118: /*@
1119: DMNetworkGetGlobalEdgeIndex - Get the global numbering for the edge on the network
1121: Not Collective
1123: Input Parameters:
1124: + dm - `DMNETWORK` object
1125: - p - edge point
1127: Output Parameter:
1128: . index - the global numbering for the edge
1130: Level: intermediate
1132: .seealso: `DM`, `DMNETWORK`, `DMNetworkGetGlobalVertexIndex()`
1133: @*/
1134: PetscErrorCode DMNetworkGetGlobalEdgeIndex(DM dm, PetscInt p, PetscInt *index)
1135: {
1136: PetscFunctionBegin;
1137: PetscCall(DMNetworkGetIndex(dm, p, index));
1138: PetscFunctionReturn(PETSC_SUCCESS);
1139: }
1141: /*@
1142: DMNetworkGetGlobalVertexIndex - Get the global numbering for the vertex on the network
1144: Not Collective
1146: Input Parameters:
1147: + dm - `DMNETWORK` object
1148: - p - vertex point
1150: Output Parameter:
1151: . index - the global numbering for the vertex
1153: Level: intermediate
1155: .seealso: `DM`, `DMNETWORK`, `DMNetworkGetGlobalEdgeIndex()`, `DMNetworkGetLocalVertexIndex()`
1156: @*/
1157: PetscErrorCode DMNetworkGetGlobalVertexIndex(DM dm, PetscInt p, PetscInt *index)
1158: {
1159: PetscFunctionBegin;
1160: PetscCall(DMNetworkGetIndex(dm, p, index));
1161: PetscFunctionReturn(PETSC_SUCCESS);
1162: }
1164: /*@
1165: DMNetworkGetNumComponents - Get the number of components at a vertex/edge
1167: Not Collective
1169: Input Parameters:
1170: + dm - the `DMNETWORK` object
1171: - p - vertex/edge point
1173: Output Parameter:
1174: . numcomponents - Number of components at the vertex/edge
1176: Level: beginner
1178: .seealso: `DM`, `DMNETWORK`, `DMNetworkRegisterComponent()`, `DMNetworkAddComponent()`
1179: @*/
1180: PetscErrorCode DMNetworkGetNumComponents(DM dm, PetscInt p, PetscInt *numcomponents)
1181: {
1182: PetscInt offset;
1183: DM_Network *network = (DM_Network *)dm->data;
1185: PetscFunctionBegin;
1186: PetscCall(PetscSectionGetOffset(network->DataSection, p, &offset));
1187: *numcomponents = ((DMNetworkComponentHeader)(network->componentdataarray + offset))->ndata;
1188: PetscFunctionReturn(PETSC_SUCCESS);
1189: }
1191: /*@
1192: DMNetworkGetLocalVecOffset - Get the offset for accessing the variables associated with a component at the given vertex/edge from the local vector
1194: Not Collective
1196: Input Parameters:
1197: + dm - the `DMNETWORK` object
1198: . p - the edge or vertex point
1199: - compnum - component number; use ALL_COMPONENTS if no specific component is requested
1201: Output Parameter:
1202: . offset - the local offset
1204: Level: intermediate
1206: Notes:
1207: These offsets can be passed to `MatSetValuesLocal()` for matrices obtained with `DMCreateMatrix()`.
1209: For vectors obtained with `DMCreateLocalVector()` the offsets can be used with `VecSetValues()`.
1211: For vectors obtained with `DMCreateLocalVector()` and the array obtained with `VecGetArray`(vec,&array) you can access or set
1212: the vector values with array[offset].
1214: For vectors obtained with `DMCreateGlobalVector()` the offsets can be used with `VecSetValuesLocal()`.
1216: .seealso: `DM`, `DMNETWORK`, `DMGetLocalVector()`, `DMNetworkGetComponent()`, `DMNetworkGetGlobalVecOffset()`, `DMCreateGlobalVector()`, `VecGetArray()`, `VecSetValuesLocal()`, `MatSetValuesLocal()`
1217: @*/
1218: PetscErrorCode DMNetworkGetLocalVecOffset(DM dm, PetscInt p, PetscInt compnum, PetscInt *offset)
1219: {
1220: DM_Network *network = (DM_Network *)dm->data;
1221: PetscInt offsetp, offsetd;
1222: DMNetworkComponentHeader header;
1224: PetscFunctionBegin;
1225: PetscCall(PetscSectionGetOffset(network->plex->localSection, p, &offsetp));
1226: if (compnum == ALL_COMPONENTS) {
1227: *offset = offsetp;
1228: PetscFunctionReturn(PETSC_SUCCESS);
1229: }
1231: PetscCall(PetscSectionGetOffset(network->DataSection, p, &offsetd));
1232: header = (DMNetworkComponentHeader)(network->componentdataarray + offsetd);
1233: *offset = offsetp + header->offsetvarrel[compnum];
1234: PetscFunctionReturn(PETSC_SUCCESS);
1235: }
1237: /*@
1238: DMNetworkGetGlobalVecOffset - Get the global offset for accessing the variables associated with a component for the given vertex/edge from the global vector
1240: Not Collective
1242: Input Parameters:
1243: + dm - the `DMNETWORK` object
1244: . p - the edge or vertex point
1245: - compnum - component number; use ALL_COMPONENTS if no specific component is requested
1247: Output Parameter:
1248: . offsetg - the global offset
1250: Level: intermediate
1252: Notes:
1253: These offsets can be passed to `MatSetValues()` for matrices obtained with `DMCreateMatrix()`.
1255: For vectors obtained with `DMCreateGlobalVector()` the offsets can be used with `VecSetValues()`.
1257: For vectors obtained with `DMCreateGlobalVector()` and the array obtained with `VecGetArray`(vec,&array) you can access or set
1258: the vector values with array[offset - rstart] where restart is obtained with `VecGetOwnershipRange`(v,&rstart,`NULL`);
1260: .seealso: `DM`, `DMNETWORK`, `DMNetworkGetLocalVecOffset()`, `DMGetGlobalVector()`, `DMNetworkGetComponent()`, `DMCreateGlobalVector()`, `VecGetArray()`, `VecSetValues()`, `MatSetValues()`
1261: @*/
1262: PetscErrorCode DMNetworkGetGlobalVecOffset(DM dm, PetscInt p, PetscInt compnum, PetscInt *offsetg)
1263: {
1264: DM_Network *network = (DM_Network *)dm->data;
1265: PetscInt offsetp, offsetd;
1266: DMNetworkComponentHeader header;
1268: PetscFunctionBegin;
1269: PetscCall(PetscSectionGetOffset(network->plex->globalSection, p, &offsetp));
1270: if (offsetp < 0) offsetp = -(offsetp + 1); /* Convert to actual global offset for ghost vertex */
1272: if (compnum == ALL_COMPONENTS) {
1273: *offsetg = offsetp;
1274: PetscFunctionReturn(PETSC_SUCCESS);
1275: }
1276: PetscCall(PetscSectionGetOffset(network->DataSection, p, &offsetd));
1277: header = (DMNetworkComponentHeader)(network->componentdataarray + offsetd);
1278: *offsetg = offsetp + header->offsetvarrel[compnum];
1279: PetscFunctionReturn(PETSC_SUCCESS);
1280: }
1282: /*@
1283: DMNetworkGetEdgeOffset - Get the offset for accessing the variables associated with the given edge from the local subvector
1285: Not Collective
1287: Input Parameters:
1288: + dm - the `DMNETWORK` object
1289: - p - the edge point
1291: Output Parameter:
1292: . offset - the offset
1294: Level: intermediate
1296: .seealso: `DM`, `DMNETWORK`, `DMNetworkGetLocalVecOffset()`, `DMGetLocalVector()`
1297: @*/
1298: PetscErrorCode DMNetworkGetEdgeOffset(DM dm, PetscInt p, PetscInt *offset)
1299: {
1300: DM_Network *network = (DM_Network *)dm->data;
1302: PetscFunctionBegin;
1303: PetscCall(PetscSectionGetOffset(network->edge.DofSection, p, offset));
1304: PetscFunctionReturn(PETSC_SUCCESS);
1305: }
1307: /*@
1308: DMNetworkGetVertexOffset - Get the offset for accessing the variables associated with the given vertex from the local subvector
1310: Not Collective
1312: Input Parameters:
1313: + dm - the `DMNETWORK` object
1314: - p - the vertex point
1316: Output Parameter:
1317: . offset - the offset
1319: Level: intermediate
1321: .seealso: `DM`, `DMNETWORK`, `DMNetworkGetEdgeOffset()`, `DMGetLocalVector()`
1322: @*/
1323: PetscErrorCode DMNetworkGetVertexOffset(DM dm, PetscInt p, PetscInt *offset)
1324: {
1325: DM_Network *network = (DM_Network *)dm->data;
1327: PetscFunctionBegin;
1328: p -= network->cloneshared->vStart;
1329: PetscCall(PetscSectionGetOffset(network->vertex.DofSection, p, offset));
1330: PetscFunctionReturn(PETSC_SUCCESS);
1331: }
1333: /*@
1334: DMNetworkAddComponent - Adds a network component and number of variables at the given point (vertex/edge)
1336: Collective
1338: Input Parameters:
1339: + dm - the DMNetwork
1340: . p - the vertex/edge point. These points are local indices provided by `DMNetworkGetSubnetwork()`
1341: . componentkey - component key returned while registering the component with `DMNetworkRegisterComponent()`
1342: . compvalue - pointer to the data structure for the component, or `NULL` if the component does not require data, this data is not copied so you cannot
1343: free this space until after `DMSetUp()` is called.
1344: - nvar - number of variables for the component at the vertex/edge point, zero if the component does not introduce any degrees of freedom at the point
1346: Level: beginner
1348: Notes:
1349: The owning rank and any other ranks that have this point as a ghost location must call this routine to add a component and number of variables in the same order at the given point.
1351: `DMNetworkLayoutSetUp()` must be called before this routine.
1353: Developer Notes:
1354: The requirement that all the ranks with access to a vertex (as owner or as ghost) add all the components comes from a limitation of the underlying implementation based on `DMPLEX`.
1356: .seealso: `DM`, `DMNETWORK`, `DMNetworkGetComponent()`, `DMNetworkGetSubnetwork()`, `DMNetworkIsGhostVertex()`, `DMNetworkLayoutSetUp()`
1357: @*/
1358: PetscErrorCode DMNetworkAddComponent(DM dm, PetscInt p, PetscInt componentkey, void *compvalue, PetscInt nvar)
1359: {
1360: DM_Network *network = (DM_Network *)dm->data;
1361: DMNetworkComponent *component = &network->component[componentkey];
1362: DMNetworkComponentHeader header;
1363: DMNetworkComponentValue cvalue;
1364: PetscInt compnum;
1365: PetscInt *compsize, *compkey, *compoffset, *compnvar, *compoffsetvarrel;
1366: void **compdata;
1368: PetscFunctionBegin;
1369: PetscCheck(componentkey >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "componentkey %" PetscInt_FMT " cannot be negative. Input a component key returned while registering the component with DMNetworkRegisterComponent()", componentkey);
1370: PetscCheck(network->componentsetup == PETSC_FALSE, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "The network has already finalized the components. No new components can be added.");
1371: /* The owning rank and all ghost ranks add nvar */
1372: PetscCall(PetscSectionAddDof(network->DofSection, p, nvar));
1374: /* The owning rank and all ghost ranks add a component, including compvalue=NULL */
1375: header = &network->header[p];
1376: cvalue = &network->cvalue[p];
1377: if (header->ndata == header->maxcomps) {
1378: PetscInt additional_size;
1380: /* Reached limit so resize header component arrays */
1381: header->maxcomps += 2;
1383: /* Allocate arrays for component information and value */
1384: PetscCall(PetscCalloc5(header->maxcomps, &compsize, header->maxcomps, &compkey, header->maxcomps, &compoffset, header->maxcomps, &compnvar, header->maxcomps, &compoffsetvarrel));
1385: PetscCall(PetscMalloc1(header->maxcomps, &compdata));
1387: /* Recalculate header size */
1388: header->hsize = sizeof(struct _p_DMNetworkComponentHeader) + 5 * header->maxcomps * sizeof(PetscInt);
1389: header->hsize /= sizeof(DMNetworkComponentGenericDataType);
1390: #if defined(__NEC__)
1391: /* NEC/LG: quick hack to keep data aligned on 8 bytes. */
1392: header->hsize = (header->hsize + (8 - 1)) & ~(8 - 1);
1393: #endif
1395: /* Copy over component info */
1396: PetscCall(PetscMemcpy(compsize, header->size, header->ndata * sizeof(PetscInt)));
1397: PetscCall(PetscMemcpy(compkey, header->key, header->ndata * sizeof(PetscInt)));
1398: PetscCall(PetscMemcpy(compoffset, header->offset, header->ndata * sizeof(PetscInt)));
1399: PetscCall(PetscMemcpy(compnvar, header->nvar, header->ndata * sizeof(PetscInt)));
1400: PetscCall(PetscMemcpy(compoffsetvarrel, header->offsetvarrel, header->ndata * sizeof(PetscInt)));
1402: /* Copy over component data pointers */
1403: PetscCall(PetscMemcpy(compdata, cvalue->data, header->ndata * sizeof(void *)));
1405: /* Free old arrays */
1406: PetscCall(PetscFree5(header->size, header->key, header->offset, header->nvar, header->offsetvarrel));
1407: PetscCall(PetscFree(cvalue->data));
1409: /* Update pointers */
1410: header->size = compsize;
1411: header->key = compkey;
1412: header->offset = compoffset;
1413: header->nvar = compnvar;
1414: header->offsetvarrel = compoffsetvarrel;
1416: cvalue->data = compdata;
1418: /* Update DataSection Dofs */
1419: /* The dofs for datasection point p equals sizeof the header (i.e. header->hsize) + sizes of the components added at point p. With the resizing of the header, we need to update the dofs for point p. Hence, we add the extra size added for the header */
1420: additional_size = (5 * (header->maxcomps - header->ndata) * sizeof(PetscInt)) / sizeof(DMNetworkComponentGenericDataType);
1421: PetscCall(PetscSectionAddDof(network->DataSection, p, additional_size));
1422: }
1423: header = &network->header[p];
1424: cvalue = &network->cvalue[p];
1426: compnum = header->ndata;
1428: header->size[compnum] = component->size;
1429: PetscCall(PetscSectionAddDof(network->DataSection, p, component->size));
1430: header->key[compnum] = componentkey;
1431: if (compnum != 0) header->offset[compnum] = header->offset[compnum - 1] + header->size[compnum - 1];
1432: cvalue->data[compnum] = compvalue;
1434: /* variables */
1435: header->nvar[compnum] += nvar;
1436: if (compnum != 0) header->offsetvarrel[compnum] = header->offsetvarrel[compnum - 1] + header->nvar[compnum - 1];
1438: header->ndata++;
1439: PetscFunctionReturn(PETSC_SUCCESS);
1440: }
1442: /*@
1443: DMNetworkGetComponent - Gets the component key, the component data, and the number of variables at a given network point
1445: Not Collective
1447: Input Parameters:
1448: + dm - the `DMNETWORK` object
1449: . p - vertex/edge point
1450: - compnum - component number; use ALL_COMPONENTS if sum up all the components
1452: Output Parameters:
1453: + compkey - the key obtained when registering the component (use `NULL` if not required)
1454: . component - the component data (use `NULL` if not required)
1455: - nvar - number of variables (use `NULL` if not required)
1457: Level: beginner
1459: .seealso: `DM`, `DMNETWORK`, `DMNetworkAddComponent()`, `DMNetworkGetNumComponents()`
1460: @*/
1461: PetscErrorCode DMNetworkGetComponent(DM dm, PetscInt p, PetscInt compnum, PeOp PetscInt *compkey, PetscCtxRt component, PeOp PetscInt *nvar)
1462: {
1463: DM_Network *network = (DM_Network *)dm->data;
1464: PetscInt offset = 0;
1465: DMNetworkComponentHeader header;
1467: PetscFunctionBegin;
1468: if (compnum == ALL_COMPONENTS) {
1469: PetscCall(PetscSectionGetDof(network->DofSection, p, nvar));
1470: PetscFunctionReturn(PETSC_SUCCESS);
1471: }
1473: PetscCall(PetscSectionGetOffset(network->DataSection, p, &offset));
1474: header = (DMNetworkComponentHeader)(network->componentdataarray + offset);
1476: if (compnum >= 0) {
1477: if (compkey) *compkey = header->key[compnum];
1478: if (component) {
1479: offset += header->hsize + header->offset[compnum];
1480: *(void **)component = network->componentdataarray + offset;
1481: }
1482: }
1484: if (nvar) *nvar = header->nvar[compnum];
1485: PetscFunctionReturn(PETSC_SUCCESS);
1486: }
1488: /*
1489: Sets up the array that holds the data for all components and its associated section.
1490: It copies the data for all components in a contiguous array called componentdataarray. The component data is stored pointwise with an additional header (metadata) stored for each point. The header has metadata information such as number of components at each point, number of variables for each component, offsets for the components data, etc.
1491: */
1492: static PetscErrorCode DMNetworkComponentSetUp(DM dm)
1493: {
1494: DM_Network *network = (DM_Network *)dm->data;
1495: PetscInt arr_size, p, offset, offsetp, ncomp, i, *headerarr;
1496: DMNetworkComponentHeader header;
1497: DMNetworkComponentValue cvalue;
1498: DMNetworkComponentHeader headerinfo;
1499: DMNetworkComponentGenericDataType *componentdataarray;
1501: PetscFunctionBegin;
1502: PetscCall(PetscSectionSetUp(network->DataSection));
1503: PetscCall(PetscSectionGetStorageSize(network->DataSection, &arr_size));
1504: /* arr_size+1 fixes pipeline test of opensolaris-misc for src/dm/tests/ex10.c -- Do not know why */
1505: PetscCall(PetscCalloc1(arr_size + 1, &network->componentdataarray));
1506: componentdataarray = network->componentdataarray;
1507: for (p = network->cloneshared->pStart; p < network->cloneshared->pEnd; p++) {
1508: PetscCall(PetscSectionGetOffset(network->DataSection, p, &offsetp));
1509: /* Copy header */
1510: header = &network->header[p];
1511: headerinfo = (DMNetworkComponentHeader)(componentdataarray + offsetp);
1512: PetscCall(PetscMemcpy(headerinfo, header, sizeof(struct _p_DMNetworkComponentHeader)));
1513: headerarr = (PetscInt *)(headerinfo + 1);
1514: PetscCall(PetscMemcpy(headerarr, header->size, header->maxcomps * sizeof(PetscInt)));
1515: headerinfo->size = headerarr;
1516: headerarr += header->maxcomps;
1517: PetscCall(PetscMemcpy(headerarr, header->key, header->maxcomps * sizeof(PetscInt)));
1518: headerinfo->key = headerarr;
1519: headerarr += header->maxcomps;
1520: PetscCall(PetscMemcpy(headerarr, header->offset, header->maxcomps * sizeof(PetscInt)));
1521: headerinfo->offset = headerarr;
1522: headerarr += header->maxcomps;
1523: PetscCall(PetscMemcpy(headerarr, header->nvar, header->maxcomps * sizeof(PetscInt)));
1524: headerinfo->nvar = headerarr;
1525: headerarr += header->maxcomps;
1526: PetscCall(PetscMemcpy(headerarr, header->offsetvarrel, header->maxcomps * sizeof(PetscInt)));
1527: headerinfo->offsetvarrel = headerarr;
1529: /* Copy data */
1530: cvalue = &network->cvalue[p];
1531: ncomp = header->ndata;
1533: for (i = 0; i < ncomp; i++) {
1534: offset = offsetp + header->hsize + header->offset[i];
1535: PetscCall(PetscMemcpy(componentdataarray + offset, cvalue->data[i], header->size[i] * sizeof(DMNetworkComponentGenericDataType)));
1536: }
1537: }
1539: for (i = network->cloneshared->pStart; i < network->cloneshared->pEnd; i++) {
1540: PetscCall(PetscFree5(network->header[i].size, network->header[i].key, network->header[i].offset, network->header[i].nvar, network->header[i].offsetvarrel));
1541: PetscCall(PetscFree(network->cvalue[i].data));
1542: }
1543: PetscCall(PetscFree2(network->header, network->cvalue));
1544: PetscFunctionReturn(PETSC_SUCCESS);
1545: }
1547: /* Sets up the section for dofs. This routine is called during DMSetUp() */
1548: static PetscErrorCode DMNetworkVariablesSetUp(DM dm)
1549: {
1550: DM_Network *network = (DM_Network *)dm->data;
1552: PetscFunctionBegin;
1553: PetscCall(PetscSectionSetUp(network->DofSection));
1554: PetscFunctionReturn(PETSC_SUCCESS);
1555: }
1557: /* Get a subsection from a range of points */
1558: static PetscErrorCode DMNetworkGetSubSection_private(PetscSection main, PetscInt pstart, PetscInt pend, PetscSection *subsection)
1559: {
1560: PetscInt i, nvar;
1562: PetscFunctionBegin;
1563: PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)main), subsection));
1564: PetscCall(PetscSectionSetChart(*subsection, 0, pend - pstart));
1565: for (i = pstart; i < pend; i++) {
1566: PetscCall(PetscSectionGetDof(main, i, &nvar));
1567: PetscCall(PetscSectionSetDof(*subsection, i - pstart, nvar));
1568: }
1570: PetscCall(PetscSectionSetUp(*subsection));
1571: PetscFunctionReturn(PETSC_SUCCESS);
1572: }
1574: /* Create a submap of points with a GlobalToLocal structure */
1575: static PetscErrorCode DMNetworkSetSubMap_private(DM dm, PetscInt pstart, PetscInt pend, ISLocalToGlobalMapping *map)
1576: {
1577: PetscInt i, *subpoints;
1579: PetscFunctionBegin;
1580: /* Create index sets to map from "points" to "subpoints" */
1581: PetscCall(PetscMalloc1(pend - pstart, &subpoints));
1582: for (i = pstart; i < pend; i++) subpoints[i - pstart] = i;
1583: PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), 1, pend - pstart, subpoints, PETSC_COPY_VALUES, map));
1584: PetscCall(PetscFree(subpoints));
1585: PetscFunctionReturn(PETSC_SUCCESS);
1586: }
1588: /*@
1589: DMNetworkAssembleGraphStructures - Assembles vertex and edge data structures. Must be called after `DMNetworkDistribute()`
1591: Collective
1593: Input Parameter:
1594: . dm - the `DMNETWORK` Object
1596: Level: intermediate
1598: Note:
1599: The routine will create alternative orderings for the vertices and edges. Assume global
1600: network points are\:
1602: points = [0 1 2 3 4 5 6]
1604: where edges = [0,1,2,3] and vertices = [4,5,6]. The new orderings will be specific to the subset (i.e vertices = [0,1,2] <- [4,5,6]).
1606: With this new ordering a local `PetscSection`, global `PetscSection` and` PetscSF` will be created specific to the subset.
1608: .seealso: `DMNetworkDistribute()`
1609: @*/
1610: PetscErrorCode DMNetworkAssembleGraphStructures(DM dm)
1611: {
1612: MPI_Comm comm;
1613: PetscMPIInt size;
1614: DM_Network *network = (DM_Network *)dm->data;
1616: PetscFunctionBegin;
1617: PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
1618: PetscCallMPI(MPI_Comm_size(comm, &size));
1620: /* Create maps for vertices and edges */
1621: PetscCall(DMNetworkSetSubMap_private(dm, network->cloneshared->vStart, network->cloneshared->vEnd, &network->vertex.mapping));
1622: PetscCall(DMNetworkSetSubMap_private(dm, network->cloneshared->eStart, network->cloneshared->eEnd, &network->edge.mapping));
1624: /* Create local sub-sections */
1625: PetscCall(DMNetworkGetSubSection_private(network->DofSection, network->cloneshared->vStart, network->cloneshared->vEnd, &network->vertex.DofSection));
1626: PetscCall(DMNetworkGetSubSection_private(network->DofSection, network->cloneshared->eStart, network->cloneshared->eEnd, &network->edge.DofSection));
1628: if (size > 1) {
1629: PetscCall(PetscSFGetSubSF(network->plex->sf, network->vertex.mapping, &network->vertex.sf));
1631: PetscCall(PetscSectionCreateGlobalSection(network->vertex.DofSection, network->vertex.sf, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &network->vertex.GlobalDofSection));
1632: PetscCall(PetscSFGetSubSF(network->plex->sf, network->edge.mapping, &network->edge.sf));
1633: PetscCall(PetscSectionCreateGlobalSection(network->edge.DofSection, network->edge.sf, PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, &network->edge.GlobalDofSection));
1634: } else {
1635: /* create structures for vertex */
1636: PetscCall(PetscSectionClone(network->vertex.DofSection, &network->vertex.GlobalDofSection));
1637: /* create structures for edge */
1638: PetscCall(PetscSectionClone(network->edge.DofSection, &network->edge.GlobalDofSection));
1639: }
1641: /* Add viewers */
1642: PetscCall(PetscObjectSetName((PetscObject)network->edge.GlobalDofSection, "Global edge dof section"));
1643: PetscCall(PetscObjectSetName((PetscObject)network->vertex.GlobalDofSection, "Global vertex dof section"));
1644: PetscCall(PetscSectionViewFromOptions(network->edge.GlobalDofSection, NULL, "-edge_global_section_view"));
1645: PetscCall(PetscSectionViewFromOptions(network->vertex.GlobalDofSection, NULL, "-vertex_global_section_view"));
1646: PetscFunctionReturn(PETSC_SUCCESS);
1647: }
1649: /*
1650: Setup a lookup btable for the input v's owning subnetworks
1651: - add all owing subnetworks that connect to this v to the btable
1652: vertex_subnetid = supportingedge_subnetid
1653: */
1654: static inline PetscErrorCode SetSubnetIdLookupBT(DM dm, PetscInt v, PetscInt Nsubnet, PetscBT btable)
1655: {
1656: PetscInt e, nedges, offset;
1657: const PetscInt *edges;
1658: DM_Network *newDMnetwork = (DM_Network *)dm->data;
1659: DMNetworkComponentHeader header;
1661: PetscFunctionBegin;
1662: PetscCall(PetscBTMemzero(Nsubnet, btable));
1663: PetscCall(DMNetworkGetSupportingEdges(dm, v, &nedges, &edges));
1664: for (e = 0; e < nedges; e++) {
1665: PetscCall(PetscSectionGetOffset(newDMnetwork->DataSection, edges[e], &offset));
1666: header = (DMNetworkComponentHeader)(newDMnetwork->componentdataarray + offset);
1667: PetscCall(PetscBTSet(btable, header->subnetid));
1668: }
1669: PetscFunctionReturn(PETSC_SUCCESS);
1670: }
1672: /*
1673: DMNetworkDistributeCoordinates - Internal function to distribute the coordinate network and coordinates.
1675: Collective
1677: Input Parameters:
1678: + dm - The original `DMNETWORK` object
1679: - migrationSF - The `PetscSF` describing the migration from dm to dmnew
1680: - newDM - The new distributed dmnetwork object.
1681: */
1683: static PetscErrorCode DMNetworkDistributeCoordinates(DM dm, PetscSF migrationSF, DM newDM)
1684: {
1685: DM_Network *newDMnetwork = (DM_Network *)((newDM)->data), *newCoordnetwork, *oldCoordnetwork;
1686: DM cdm, newcdm;
1687: PetscInt cdim, bs, p, pStart, pEnd, offset;
1688: Vec oldCoord, newCoord;
1689: DMNetworkComponentHeader header;
1690: const char *name;
1692: PetscFunctionBegin;
1693: /* Distribute the coordinate network and coordinates */
1694: PetscCall(DMGetCoordinateDim(dm, &cdim));
1695: PetscCall(DMSetCoordinateDim(newDM, cdim));
1697: /* Migrate only if original network had coordinates */
1698: PetscCall(DMGetCoordinatesLocal(dm, &oldCoord));
1699: if (oldCoord) {
1700: PetscCall(DMGetCoordinateDM(dm, &cdm));
1701: PetscCall(DMGetCoordinateDM(newDM, &newcdm));
1702: newCoordnetwork = (DM_Network *)newcdm->data;
1703: oldCoordnetwork = (DM_Network *)cdm->data;
1705: PetscCall(VecCreate(PETSC_COMM_SELF, &newCoord));
1706: PetscCall(PetscObjectGetName((PetscObject)oldCoord, &name));
1707: PetscCall(PetscObjectSetName((PetscObject)newCoord, name));
1708: PetscCall(VecGetBlockSize(oldCoord, &bs));
1709: PetscCall(VecSetBlockSize(newCoord, bs));
1711: PetscCall(DMPlexDistributeField(newDMnetwork->plex, migrationSF, oldCoordnetwork->DofSection, oldCoord, newCoordnetwork->DofSection, newCoord));
1712: PetscCall(DMSetCoordinatesLocal(newDM, newCoord));
1714: PetscCall(VecDestroy(&newCoord));
1715: /* Migrate the components from the original coordinate network to the new coordinate network */
1716: PetscCall(DMPlexDistributeData(newDMnetwork->plex, migrationSF, oldCoordnetwork->DataSection, MPIU_INT, (void *)oldCoordnetwork->componentdataarray, newCoordnetwork->DataSection, (void **)&newCoordnetwork->componentdataarray));
1717: /* update the header pointers in the new coordinate network components */
1718: PetscCall(PetscSectionGetChart(newCoordnetwork->DataSection, &pStart, &pEnd));
1719: for (p = pStart; p < pEnd; p++) {
1720: PetscCall(PetscSectionGetOffset(newCoordnetwork->DataSection, p, &offset));
1721: header = (DMNetworkComponentHeader)(newCoordnetwork->componentdataarray + offset);
1722: /* Update pointers */
1723: header->size = (PetscInt *)(header + 1);
1724: header->key = header->size + header->maxcomps;
1725: header->offset = header->key + header->maxcomps;
1726: header->nvar = header->offset + header->maxcomps;
1727: header->offsetvarrel = header->nvar + header->maxcomps;
1728: }
1730: PetscCall(DMSetLocalSection(newCoordnetwork->plex, newCoordnetwork->DofSection));
1731: PetscCall(DMGetGlobalSection(newCoordnetwork->plex, &newCoordnetwork->GlobalDofSection));
1732: newCoordnetwork->componentsetup = PETSC_TRUE;
1733: }
1734: PetscFunctionReturn(PETSC_SUCCESS);
1735: }
1737: /*@
1738: DMNetworkDistribute - Distributes the network and moves associated component data
1740: Collective
1742: Input Parameters:
1743: + dm - the `DMNETWORK` object
1744: - overlap - the overlap of partitions, 0 is the default
1746: Options Database Keys:
1747: + -dmnetwork_view - Calls `DMView()` at the conclusion of `DMSetUp()`
1748: . -dmnetwork_view_distributed - Calls `DMView()` at the conclusion of `DMNetworkDistribute()`
1749: . -dmnetwork_view_tmpdir dir - Sets the temporary directory to use when viewing with the `draw` option
1750: . -dmnetwork_view_all_ranks - Displays all of the subnetworks for each MPI rank
1751: . -dmnetwork_view_rank_range - Displays the subnetworks for the ranks in a comma-separated list
1752: . -dmnetwork_view_no_vertices - Disables displaying the vertices in the network visualization
1753: - -dmnetwork_view_no_numbering - Disables displaying the numbering of edges and vertices in the network visualization
1755: Level: intermediate
1757: Note:
1758: Distributes the network with overlapping partitioning of the edges.
1760: .seealso: `DM`, `DMNETWORK`, `DMNetworkCreate()`
1761: @*/
1762: PetscErrorCode DMNetworkDistribute(DM *dm, PetscInt overlap)
1763: {
1764: MPI_Comm comm;
1765: PetscMPIInt size;
1766: DM_Network *oldDMnetwork = (DM_Network *)((*dm)->data), *newDMnetwork;
1767: PetscSF pointsf = NULL;
1768: DM newDM;
1769: PetscInt j, e, v, offset, *subnetvtx, *subnetedge, Nsubnet, gidx, svtx_idx, nv, net;
1770: PetscInt *sv;
1771: PetscBT btable;
1772: PetscPartitioner part;
1773: DMNetworkComponentHeader header;
1775: PetscFunctionBegin;
1776: PetscAssertPointer(dm, 1);
1778: PetscCall(PetscObjectGetComm((PetscObject)*dm, &comm));
1779: PetscCallMPI(MPI_Comm_size(comm, &size));
1780: if (size == 1) {
1781: oldDMnetwork->cloneshared->distributecalled = PETSC_TRUE;
1782: PetscFunctionReturn(PETSC_SUCCESS);
1783: }
1785: PetscCheck(!overlap, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "overlap %" PetscInt_FMT " != 0 is not supported yet", overlap);
1787: /* This routine moves the component data to the appropriate processors. It makes use of the DataSection and the componentdataarray to move the component data to appropriate processors and returns a new DataSection and new componentdataarray. */
1788: PetscCall(DMNetworkCreate(PetscObjectComm((PetscObject)*dm), &newDM));
1789: PetscCall(PetscLogEventBegin(DMNetwork_Distribute, newDM, 0, 0, 0));
1790: newDMnetwork = (DM_Network *)newDM->data;
1791: newDMnetwork->max_comps_registered = oldDMnetwork->max_comps_registered;
1792: PetscCall(PetscMalloc1(newDMnetwork->max_comps_registered, &newDMnetwork->component));
1794: /* Enable runtime options for petscpartitioner */
1795: PetscCall(DMPlexGetPartitioner(oldDMnetwork->plex, &part));
1796: PetscCall(PetscPartitionerSetFromOptions(part));
1798: /* Distribute plex dm */
1799: PetscCall(DMPlexDistribute(oldDMnetwork->plex, overlap, &pointsf, &newDMnetwork->plex));
1801: /* Distribute dof section */
1802: PetscCall(PetscSectionCreate(comm, &newDMnetwork->DofSection));
1803: PetscCall(PetscSFDistributeSection(pointsf, oldDMnetwork->DofSection, NULL, newDMnetwork->DofSection));
1805: /* Distribute data and associated section */
1806: PetscCall(PetscSectionCreate(comm, &newDMnetwork->DataSection));
1807: PetscCall(DMPlexDistributeData(newDMnetwork->plex, pointsf, oldDMnetwork->DataSection, MPIU_INT, (void *)oldDMnetwork->componentdataarray, newDMnetwork->DataSection, (void **)&newDMnetwork->componentdataarray));
1809: PetscCall(PetscSectionGetChart(newDMnetwork->DataSection, &newDMnetwork->cloneshared->pStart, &newDMnetwork->cloneshared->pEnd));
1810: PetscCall(DMPlexGetHeightStratum(newDMnetwork->plex, 0, &newDMnetwork->cloneshared->eStart, &newDMnetwork->cloneshared->eEnd));
1811: PetscCall(DMPlexGetHeightStratum(newDMnetwork->plex, 1, &newDMnetwork->cloneshared->vStart, &newDMnetwork->cloneshared->vEnd));
1812: newDMnetwork->cloneshared->nEdges = newDMnetwork->cloneshared->eEnd - newDMnetwork->cloneshared->eStart;
1813: newDMnetwork->cloneshared->nVertices = newDMnetwork->cloneshared->vEnd - newDMnetwork->cloneshared->vStart;
1814: newDMnetwork->cloneshared->NVertices = oldDMnetwork->cloneshared->NVertices;
1815: newDMnetwork->cloneshared->NEdges = oldDMnetwork->cloneshared->NEdges;
1816: newDMnetwork->cloneshared->svtable = oldDMnetwork->cloneshared->svtable; /* global table! */
1817: oldDMnetwork->cloneshared->svtable = NULL;
1819: /* Set Dof section as the section for dm */
1820: PetscCall(DMSetLocalSection(newDMnetwork->plex, newDMnetwork->DofSection));
1821: PetscCall(DMGetGlobalSection(newDMnetwork->plex, &newDMnetwork->GlobalDofSection));
1823: /* Setup subnetwork info in the newDM */
1824: newDMnetwork->cloneshared->Nsubnet = oldDMnetwork->cloneshared->Nsubnet;
1825: newDMnetwork->cloneshared->Nsvtx = oldDMnetwork->cloneshared->Nsvtx;
1826: oldDMnetwork->cloneshared->Nsvtx = 0;
1827: newDMnetwork->cloneshared->svtx = oldDMnetwork->cloneshared->svtx; /* global vertices! */
1828: oldDMnetwork->cloneshared->svtx = NULL;
1829: PetscCall(PetscCalloc1(newDMnetwork->cloneshared->Nsubnet, &newDMnetwork->cloneshared->subnet));
1831: /* Copy over the global number of vertices and edges in each subnetwork.
1832: Note: these are calculated in DMNetworkLayoutSetUp()
1833: */
1834: Nsubnet = newDMnetwork->cloneshared->Nsubnet;
1835: for (j = 0; j < Nsubnet; j++) {
1836: newDMnetwork->cloneshared->subnet[j].Nvtx = oldDMnetwork->cloneshared->subnet[j].Nvtx;
1837: newDMnetwork->cloneshared->subnet[j].Nedge = oldDMnetwork->cloneshared->subnet[j].Nedge;
1838: }
1840: /* Count local nedges for subnetworks */
1841: for (e = newDMnetwork->cloneshared->eStart; e < newDMnetwork->cloneshared->eEnd; e++) {
1842: PetscCall(PetscSectionGetOffset(newDMnetwork->DataSection, e, &offset));
1843: header = (DMNetworkComponentHeader)(newDMnetwork->componentdataarray + offset);
1845: /* Update pointers */
1846: header->size = (PetscInt *)(header + 1);
1847: header->key = header->size + header->maxcomps;
1848: header->offset = header->key + header->maxcomps;
1849: header->nvar = header->offset + header->maxcomps;
1850: header->offsetvarrel = header->nvar + header->maxcomps;
1852: newDMnetwork->cloneshared->subnet[header->subnetid].nedge++;
1853: }
1855: /* Setup a btable to keep track subnetworks owned by this process at a shared vertex */
1856: if (newDMnetwork->cloneshared->Nsvtx) PetscCall(PetscBTCreate(Nsubnet, &btable));
1858: /* Count local nvtx for subnetworks */
1859: for (v = newDMnetwork->cloneshared->vStart; v < newDMnetwork->cloneshared->vEnd; v++) {
1860: PetscCall(PetscSectionGetOffset(newDMnetwork->DataSection, v, &offset));
1861: header = (DMNetworkComponentHeader)(newDMnetwork->componentdataarray + offset);
1863: /* Update pointers */
1864: header->size = (PetscInt *)(header + 1);
1865: header->key = header->size + header->maxcomps;
1866: header->offset = header->key + header->maxcomps;
1867: header->nvar = header->offset + header->maxcomps;
1868: header->offsetvarrel = header->nvar + header->maxcomps;
1870: /* shared vertices: use gidx=header->index to check if v is a shared vertex */
1871: gidx = header->index;
1872: PetscCall(PetscHMapIGetWithDefault(newDMnetwork->cloneshared->svtable, gidx + 1, 0, &svtx_idx));
1873: svtx_idx--;
1875: if (svtx_idx < 0) { /* not a shared vertex */
1876: newDMnetwork->cloneshared->subnet[header->subnetid].nvtx++;
1877: } else { /* a shared vertex belongs to more than one subnetworks, it is being counted by multiple subnets */
1878: /* Setup a lookup btable for this v's owning subnetworks */
1879: PetscCall(SetSubnetIdLookupBT(newDM, v, Nsubnet, btable));
1881: for (j = 0; j < newDMnetwork->cloneshared->svtx[svtx_idx].n; j++) {
1882: sv = newDMnetwork->cloneshared->svtx[svtx_idx].sv + 2 * j;
1883: net = sv[0];
1884: if (PetscBTLookup(btable, net)) newDMnetwork->cloneshared->subnet[net].nvtx++; /* sv is on net owned by this process */
1885: }
1886: }
1887: }
1889: /* Get total local nvtx for subnetworks */
1890: nv = 0;
1891: for (j = 0; j < Nsubnet; j++) nv += newDMnetwork->cloneshared->subnet[j].nvtx;
1892: nv += newDMnetwork->cloneshared->Nsvtx;
1894: /* Now create the vertices and edge arrays for the subnetworks */
1895: PetscCall(PetscCalloc2(newDMnetwork->cloneshared->nEdges, &subnetedge, nv, &subnetvtx)); /* Maps local vertex to local subnetwork's vertex */
1896: newDMnetwork->cloneshared->subnetedge = subnetedge;
1897: newDMnetwork->cloneshared->subnetvtx = subnetvtx;
1898: for (j = 0; j < newDMnetwork->cloneshared->Nsubnet; j++) {
1899: newDMnetwork->cloneshared->subnet[j].edges = subnetedge;
1900: subnetedge += newDMnetwork->cloneshared->subnet[j].nedge;
1902: newDMnetwork->cloneshared->subnet[j].vertices = subnetvtx;
1903: subnetvtx += newDMnetwork->cloneshared->subnet[j].nvtx;
1905: /* Temporarily setting nvtx and nedge to 0 so we can use them as counters in the below for loop. These get updated when the vertices and edges are added. */
1906: newDMnetwork->cloneshared->subnet[j].nvtx = newDMnetwork->cloneshared->subnet[j].nedge = 0;
1907: }
1908: newDMnetwork->cloneshared->svertices = subnetvtx;
1910: /* Set the edges and vertices in each subnetwork */
1911: for (e = newDMnetwork->cloneshared->eStart; e < newDMnetwork->cloneshared->eEnd; e++) {
1912: PetscCall(PetscSectionGetOffset(newDMnetwork->DataSection, e, &offset));
1913: header = (DMNetworkComponentHeader)(newDMnetwork->componentdataarray + offset);
1914: newDMnetwork->cloneshared->subnet[header->subnetid].edges[newDMnetwork->cloneshared->subnet[header->subnetid].nedge++] = e;
1915: }
1917: nv = 0;
1918: for (v = newDMnetwork->cloneshared->vStart; v < newDMnetwork->cloneshared->vEnd; v++) {
1919: PetscCall(PetscSectionGetOffset(newDMnetwork->DataSection, v, &offset));
1920: header = (DMNetworkComponentHeader)(newDMnetwork->componentdataarray + offset);
1922: /* coupling vertices: use gidx = header->index to check if v is a coupling vertex */
1923: PetscCall(PetscHMapIGetWithDefault(newDMnetwork->cloneshared->svtable, header->index + 1, 0, &svtx_idx));
1924: svtx_idx--;
1925: if (svtx_idx < 0) newDMnetwork->cloneshared->subnet[header->subnetid].vertices[newDMnetwork->cloneshared->subnet[header->subnetid].nvtx++] = v;
1926: else {
1927: /* a shared vertex */
1928: newDMnetwork->cloneshared->svertices[nv++] = v;
1930: /* Setup a lookup btable for this v's owning subnetworks */
1931: PetscCall(SetSubnetIdLookupBT(newDM, v, Nsubnet, btable));
1933: for (j = 0; j < newDMnetwork->cloneshared->svtx[svtx_idx].n; j++) {
1934: sv = newDMnetwork->cloneshared->svtx[svtx_idx].sv + 2 * j;
1935: net = sv[0];
1936: if (PetscBTLookup(btable, net)) newDMnetwork->cloneshared->subnet[net].vertices[newDMnetwork->cloneshared->subnet[net].nvtx++] = v;
1937: }
1938: }
1939: }
1940: newDMnetwork->cloneshared->nsvtx = nv; /* num of local shared vertices */
1942: PetscCall(DMNetworkDistributeCoordinates(*dm, pointsf, newDM));
1943: newDM->setupcalled = (*dm)->setupcalled;
1944: newDMnetwork->cloneshared->distributecalled = PETSC_TRUE;
1946: /* Free spaces */
1947: PetscCall(PetscSFDestroy(&pointsf));
1948: PetscCall(DMDestroy(dm));
1949: if (newDMnetwork->cloneshared->Nsvtx) PetscCall(PetscBTDestroy(&btable));
1950: PetscCall(PetscLogEventEnd(DMNetwork_Distribute, newDM, 0, 0, 0));
1952: /* View distributed dmnetwork */
1953: PetscCall(DMViewFromOptions(newDM, NULL, "-dmnetwork_view_distributed"));
1955: *dm = newDM;
1956: PetscFunctionReturn(PETSC_SUCCESS);
1957: }
1959: /*@
1960: PetscSFGetSubSF - Returns an `PetscSF` for a specific subset of points. Leaves are re-numbered to reflect the new ordering
1962: Collective
1964: Input Parameters:
1965: + mainsf - `PetscSF` structure
1966: - map - a `ISLocalToGlobalMapping` that contains the subset of points
1968: Output Parameter:
1969: . subSF - a subset of the `mainSF` for the desired subset.
1971: Level: intermediate
1973: .seealso: `PetscSF`
1974: @*/
1975: PetscErrorCode PetscSFGetSubSF(PetscSF mainsf, ISLocalToGlobalMapping map, PetscSF *subSF)
1976: {
1977: PetscInt nroots, nleaves, *ilocal_sub;
1978: PetscInt i, *ilocal_map, nroots_sub, nleaves_sub = 0;
1979: PetscInt *local_points, *remote_points;
1980: PetscSFNode *iremote_sub;
1981: const PetscInt *ilocal;
1982: const PetscSFNode *iremote;
1984: PetscFunctionBegin;
1985: PetscCall(PetscSFGetGraph(mainsf, &nroots, &nleaves, &ilocal, &iremote));
1987: /* Look for leaves that pertain to the subset of points. Get the local ordering */
1988: PetscCall(PetscMalloc1(nleaves, &ilocal_map));
1989: PetscCall(ISGlobalToLocalMappingApply(map, IS_GTOLM_MASK, nleaves, ilocal, NULL, ilocal_map));
1990: for (i = 0; i < nleaves; i++) {
1991: if (ilocal_map[i] != -1) nleaves_sub += 1;
1992: }
1993: /* Re-number ilocal with subset numbering. Need information from roots */
1994: PetscCall(PetscMalloc2(nroots, &local_points, nroots, &remote_points));
1995: for (i = 0; i < nroots; i++) local_points[i] = i;
1996: PetscCall(ISGlobalToLocalMappingApply(map, IS_GTOLM_MASK, nroots, local_points, NULL, local_points));
1997: PetscCall(PetscSFBcastBegin(mainsf, MPIU_INT, local_points, remote_points, MPI_REPLACE));
1998: PetscCall(PetscSFBcastEnd(mainsf, MPIU_INT, local_points, remote_points, MPI_REPLACE));
1999: /* Fill up graph using local (that is, local to the subset) numbering. */
2000: PetscCall(PetscMalloc1(nleaves_sub, &ilocal_sub));
2001: PetscCall(PetscMalloc1(nleaves_sub, &iremote_sub));
2002: nleaves_sub = 0;
2003: for (i = 0; i < nleaves; i++) {
2004: if (ilocal_map[i] != -1) {
2005: ilocal_sub[nleaves_sub] = ilocal_map[i];
2006: iremote_sub[nleaves_sub].rank = iremote[i].rank;
2007: iremote_sub[nleaves_sub].index = remote_points[ilocal[i]];
2008: nleaves_sub += 1;
2009: }
2010: }
2011: PetscCall(PetscFree2(local_points, remote_points));
2012: PetscCall(ISLocalToGlobalMappingGetSize(map, &nroots_sub));
2014: /* Create new subSF */
2015: PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)mainsf), subSF));
2016: PetscCall(PetscSFSetFromOptions(*subSF));
2017: PetscCall(PetscSFSetGraph(*subSF, nroots_sub, nleaves_sub, ilocal_sub, PETSC_OWN_POINTER, iremote_sub, PETSC_COPY_VALUES));
2018: PetscCall(PetscFree(ilocal_map));
2019: PetscCall(PetscFree(iremote_sub));
2020: PetscFunctionReturn(PETSC_SUCCESS);
2021: }
2023: /*@C
2024: DMNetworkGetSupportingEdges - Return the supporting edges for this vertex point
2026: Not Collective
2028: Input Parameters:
2029: + dm - the `DMNETWORK` object
2030: - vertex - the vertex point
2032: Output Parameters:
2033: + nedges - number of edges connected to this vertex point
2034: - edges - list of edge points, pass `NULL` if not needed
2036: Level: beginner
2038: .seealso: `DM`, `DMNETWORK`, `DMNetworkCreate()`, `DMNetworkGetConnectedVertices()`
2039: @*/
2040: PetscErrorCode DMNetworkGetSupportingEdges(DM dm, PetscInt vertex, PetscInt *nedges, PeOp const PetscInt *edges[])
2041: {
2042: DM_Network *network = (DM_Network *)dm->data;
2044: PetscFunctionBegin;
2045: PetscCall(DMPlexGetSupportSize(network->plex, vertex, nedges));
2046: if (edges) PetscCall(DMPlexGetSupport(network->plex, vertex, edges));
2047: PetscFunctionReturn(PETSC_SUCCESS);
2048: }
2050: /*@C
2051: DMNetworkGetConnectedVertices - Return the connected vertices for this edge point
2053: Not Collective
2055: Input Parameters:
2056: + dm - the `DMNETWORK` object
2057: - edge - the edge point
2059: Output Parameter:
2060: . vertices - vertices connected to this edge
2062: Level: beginner
2064: .seealso: `DM`, `DMNETWORK`, `DMNetworkCreate()`, `DMNetworkGetSupportingEdges()`
2065: @*/
2066: PetscErrorCode DMNetworkGetConnectedVertices(DM dm, PetscInt edge, const PetscInt *vertices[])
2067: {
2068: DM_Network *network = (DM_Network *)dm->data;
2070: PetscFunctionBegin;
2071: PetscCall(DMPlexGetCone(network->plex, edge, vertices));
2072: PetscFunctionReturn(PETSC_SUCCESS);
2073: }
2075: /*@
2076: DMNetworkIsSharedVertex - Returns `PETSC_TRUE` if the vertex is shared by subnetworks
2078: Not Collective
2080: Input Parameters:
2081: + dm - the `DMNETWORK` object
2082: - p - the vertex point
2084: Output Parameter:
2085: . flag - `PETSC_TRUE` if the vertex is shared by subnetworks
2087: Level: beginner
2089: .seealso: `DM`, `DMNETWORK`, `DMNetworkAddSharedVertices()`, `DMNetworkIsGhostVertex()`
2090: @*/
2091: PetscErrorCode DMNetworkIsSharedVertex(DM dm, PetscInt p, PetscBool *flag)
2092: {
2093: PetscFunctionBegin;
2095: PetscAssertPointer(flag, 3);
2096: if (dm->setupcalled) { /* DMNetworkGetGlobalVertexIndex() requires DMSetUp() be called */
2097: DM_Network *network = (DM_Network *)dm->data;
2098: PetscInt gidx;
2100: PetscCall(DMNetworkGetGlobalVertexIndex(dm, p, &gidx));
2101: PetscCall(PetscHMapIHas(network->cloneshared->svtable, gidx + 1, flag));
2102: } else { /* would be removed? */
2103: PetscInt nv;
2104: const PetscInt *vtx;
2106: PetscCall(DMNetworkGetSharedVertices(dm, &nv, &vtx));
2107: for (PetscInt i = 0; i < nv; i++) {
2108: if (p == vtx[i]) {
2109: *flag = PETSC_TRUE;
2110: PetscFunctionReturn(PETSC_SUCCESS);
2111: }
2112: }
2113: *flag = PETSC_FALSE;
2114: }
2115: PetscFunctionReturn(PETSC_SUCCESS);
2116: }
2118: /*@
2119: DMNetworkIsGhostVertex - Returns `PETSC_TRUE` if the vertex is a ghost vertex
2121: Not Collective
2123: Input Parameters:
2124: + dm - the `DMNETWORK` object
2125: - p - the vertex point
2127: Output Parameter:
2128: . isghost - `PETSC_TRUE` if the vertex is a ghost point
2130: Level: beginner
2132: .seealso: `DM`, `DMNETWORK`, `DMNetworkGetConnectedVertices()`, `DMNetworkGetVertexRange()`, `DMNetworkIsSharedVertex()`
2133: @*/
2134: PetscErrorCode DMNetworkIsGhostVertex(DM dm, PetscInt p, PetscBool *isghost)
2135: {
2136: DM_Network *network = (DM_Network *)dm->data;
2137: PetscInt offsetg;
2138: PetscSection sectiong;
2140: PetscFunctionBegin;
2141: *isghost = PETSC_FALSE;
2142: PetscCall(DMGetGlobalSection(network->plex, §iong));
2143: PetscCall(PetscSectionGetOffset(sectiong, p, &offsetg));
2144: if (offsetg < 0) *isghost = PETSC_TRUE;
2145: PetscFunctionReturn(PETSC_SUCCESS);
2146: }
2148: PetscErrorCode DMSetUp_Network(DM dm)
2149: {
2150: PetscFunctionBegin;
2151: PetscCall(PetscLogEventBegin(DMNetwork_SetUpNetwork, dm, 0, 0, 0));
2152: PetscCall(DMNetworkFinalizeComponents(dm));
2153: /* View dmnetwork */
2154: PetscCall(DMViewFromOptions(dm, NULL, "-dmnetwork_view"));
2155: PetscCall(PetscLogEventEnd(DMNetwork_SetUpNetwork, dm, 0, 0, 0));
2156: PetscFunctionReturn(PETSC_SUCCESS);
2157: }
2159: /*@
2160: DMNetworkHasJacobian - Sets global flag for using user's sub Jacobian matrices
2161: -- replaced by DMNetworkSetOption(network,userjacobian,PETSC_TRUE)?
2163: Collective
2165: Input Parameters:
2166: + dm - the `DMNETWORK` object
2167: . eflg - turn the option on (`PETSC_TRUE`) or off (`PETSC_FALSE`) if user provides Jacobian for edges
2168: - vflg - turn the option on (`PETSC_TRUE`) or off (`PETSC_FALSE`) if user provides Jacobian for vertices
2170: Level: intermediate
2172: .seealso: `DMNetworkSetOption()`
2173: @*/
2174: PetscErrorCode DMNetworkHasJacobian(DM dm, PetscBool eflg, PetscBool vflg)
2175: {
2176: DM_Network *network = (DM_Network *)dm->data;
2177: PetscInt nVertices = network->cloneshared->nVertices;
2179: PetscFunctionBegin;
2180: network->userEdgeJacobian = eflg;
2181: network->userVertexJacobian = vflg;
2183: if (eflg && !network->Je) PetscCall(PetscCalloc1(3 * network->cloneshared->nEdges, &network->Je));
2185: if (vflg && !network->Jv && nVertices) {
2186: PetscInt i, *vptr, nedges, vStart = network->cloneshared->vStart;
2187: PetscInt nedges_total;
2188: const PetscInt *edges;
2190: /* count nvertex_total */
2191: nedges_total = 0;
2192: PetscCall(PetscMalloc1(nVertices + 1, &vptr));
2194: vptr[0] = 0;
2195: for (i = 0; i < nVertices; i++) {
2196: PetscCall(DMNetworkGetSupportingEdges(dm, i + vStart, &nedges, &edges));
2197: nedges_total += nedges;
2198: vptr[i + 1] = vptr[i] + 2 * nedges + 1;
2199: }
2201: PetscCall(PetscCalloc1(2 * nedges_total + nVertices, &network->Jv));
2202: network->Jvptr = vptr;
2203: }
2204: PetscFunctionReturn(PETSC_SUCCESS);
2205: }
2207: /*@
2208: DMNetworkEdgeSetMatrix - Sets user-provided Jacobian matrices for this edge to the network
2210: Not Collective
2212: Input Parameters:
2213: + dm - the `DMNETWORK` object
2214: . p - the edge point
2215: - J - array (size = 3) of Jacobian submatrices for this edge point:
2216: J[0]: this edge
2217: J[1] and J[2]: connected vertices, obtained by calling `DMNetworkGetConnectedVertices()`
2219: Level: advanced
2221: .seealso: `DM`, `DMNETWORK`, `DMNetworkVertexSetMatrix()`
2222: @*/
2223: PetscErrorCode DMNetworkEdgeSetMatrix(DM dm, PetscInt p, Mat J[])
2224: {
2225: DM_Network *network = (DM_Network *)dm->data;
2227: PetscFunctionBegin;
2228: PetscCheck(network->Je, PetscObjectComm((PetscObject)dm), PETSC_ERR_ORDER, "Must call DMNetworkHasJacobian() collectively before calling DMNetworkEdgeSetMatrix");
2230: if (J) {
2231: network->Je[3 * p] = J[0];
2232: network->Je[3 * p + 1] = J[1];
2233: network->Je[3 * p + 2] = J[2];
2234: }
2235: PetscFunctionReturn(PETSC_SUCCESS);
2236: }
2238: /*@
2239: DMNetworkVertexSetMatrix - Sets user-provided Jacobian matrix for this vertex to the network
2241: Not Collective
2243: Input Parameters:
2244: + dm - The `DMNETWORK` object
2245: . p - the vertex point
2246: - J - array of Jacobian (size = 2*(num of supporting edges) + 1) submatrices for this vertex point:
2247: J[0]: this vertex
2248: J[1+2*i]: i-th supporting edge
2249: J[1+2*i+1]: i-th connected vertex
2251: Level: advanced
2253: .seealso: `DM`, `DMNETWORK`, `DMNetworkEdgeSetMatrix()`
2254: @*/
2255: PetscErrorCode DMNetworkVertexSetMatrix(DM dm, PetscInt p, Mat J[])
2256: {
2257: DM_Network *network = (DM_Network *)dm->data;
2258: PetscInt i, *vptr, nedges, vStart = network->cloneshared->vStart;
2259: const PetscInt *edges;
2261: PetscFunctionBegin;
2262: PetscCheck(network->Jv, PetscObjectComm((PetscObject)dm), PETSC_ERR_ORDER, "Must call DMNetworkHasJacobian() collectively before calling DMNetworkVertexSetMatrix");
2264: if (J) {
2265: vptr = network->Jvptr;
2266: network->Jv[vptr[p - vStart]] = J[0]; /* Set Jacobian for this vertex */
2268: /* Set Jacobian for each supporting edge and connected vertex */
2269: PetscCall(DMNetworkGetSupportingEdges(dm, p, &nedges, &edges));
2270: for (i = 1; i <= 2 * nedges; i++) network->Jv[vptr[p - vStart] + i] = J[i];
2271: }
2272: PetscFunctionReturn(PETSC_SUCCESS);
2273: }
2275: static inline PetscErrorCode MatSetPreallocationDenseblock_private(PetscInt nrows, PetscInt *rows, PetscInt ncols, PetscBool ghost, Vec vdnz, Vec vonz)
2276: {
2277: PetscInt j;
2278: PetscScalar val = (PetscScalar)ncols;
2280: PetscFunctionBegin;
2281: if (!ghost) {
2282: for (j = 0; j < nrows; j++) PetscCall(VecSetValues(vdnz, 1, &rows[j], &val, ADD_VALUES));
2283: } else {
2284: for (j = 0; j < nrows; j++) PetscCall(VecSetValues(vonz, 1, &rows[j], &val, ADD_VALUES));
2285: }
2286: PetscFunctionReturn(PETSC_SUCCESS);
2287: }
2289: static inline PetscErrorCode MatSetPreallocationUserblock_private(Mat Ju, PetscInt nrows, PetscInt *rows, PetscInt ncols, PetscBool ghost, Vec vdnz, Vec vonz)
2290: {
2291: PetscInt j, ncols_u;
2292: PetscScalar val;
2294: PetscFunctionBegin;
2295: if (!ghost) {
2296: for (j = 0; j < nrows; j++) {
2297: PetscCall(MatGetRow(Ju, j, &ncols_u, NULL, NULL));
2298: val = (PetscScalar)ncols_u;
2299: PetscCall(VecSetValues(vdnz, 1, &rows[j], &val, ADD_VALUES));
2300: PetscCall(MatRestoreRow(Ju, j, &ncols_u, NULL, NULL));
2301: }
2302: } else {
2303: for (j = 0; j < nrows; j++) {
2304: PetscCall(MatGetRow(Ju, j, &ncols_u, NULL, NULL));
2305: val = (PetscScalar)ncols_u;
2306: PetscCall(VecSetValues(vonz, 1, &rows[j], &val, ADD_VALUES));
2307: PetscCall(MatRestoreRow(Ju, j, &ncols_u, NULL, NULL));
2308: }
2309: }
2310: PetscFunctionReturn(PETSC_SUCCESS);
2311: }
2313: static inline PetscErrorCode MatSetPreallocationblock_private(Mat Ju, PetscInt nrows, PetscInt *rows, PetscInt ncols, PetscBool ghost, Vec vdnz, Vec vonz)
2314: {
2315: PetscFunctionBegin;
2316: if (Ju) PetscCall(MatSetPreallocationUserblock_private(Ju, nrows, rows, ncols, ghost, vdnz, vonz));
2317: else PetscCall(MatSetPreallocationDenseblock_private(nrows, rows, ncols, ghost, vdnz, vonz));
2318: PetscFunctionReturn(PETSC_SUCCESS);
2319: }
2321: static inline PetscErrorCode MatSetDenseblock_private(PetscInt nrows, PetscInt *rows, PetscInt ncols, PetscInt cstart, Mat *J)
2322: {
2323: PetscInt j, *cols;
2324: PetscScalar *zeros;
2326: PetscFunctionBegin;
2327: PetscCall(PetscCalloc2(ncols, &cols, nrows * ncols, &zeros));
2328: for (j = 0; j < ncols; j++) cols[j] = j + cstart;
2329: PetscCall(MatSetValues(*J, nrows, rows, ncols, cols, zeros, INSERT_VALUES));
2330: PetscCall(PetscFree2(cols, zeros));
2331: PetscFunctionReturn(PETSC_SUCCESS);
2332: }
2334: static inline PetscErrorCode MatSetUserblock_private(Mat Ju, PetscInt nrows, PetscInt *rows, PetscInt ncols, PetscInt cstart, Mat *J)
2335: {
2336: PetscInt j, M, N, row, col, ncols_u;
2337: const PetscInt *cols;
2338: PetscScalar zero = 0.0;
2340: PetscFunctionBegin;
2341: PetscCall(MatGetSize(Ju, &M, &N));
2342: PetscCheck(nrows == M && ncols == N, PetscObjectComm((PetscObject)Ju), PETSC_ERR_USER, "%" PetscInt_FMT " by %" PetscInt_FMT " must equal %" PetscInt_FMT " by %" PetscInt_FMT, nrows, ncols, M, N);
2344: for (row = 0; row < nrows; row++) {
2345: PetscCall(MatGetRow(Ju, row, &ncols_u, &cols, NULL));
2346: for (j = 0; j < ncols_u; j++) {
2347: col = cols[j] + cstart;
2348: PetscCall(MatSetValues(*J, 1, &rows[row], 1, &col, &zero, INSERT_VALUES));
2349: }
2350: PetscCall(MatRestoreRow(Ju, row, &ncols_u, &cols, NULL));
2351: }
2352: PetscFunctionReturn(PETSC_SUCCESS);
2353: }
2355: static inline PetscErrorCode MatSetblock_private(Mat Ju, PetscInt nrows, PetscInt *rows, PetscInt ncols, PetscInt cstart, Mat *J)
2356: {
2357: PetscFunctionBegin;
2358: if (Ju) PetscCall(MatSetUserblock_private(Ju, nrows, rows, ncols, cstart, J));
2359: else PetscCall(MatSetDenseblock_private(nrows, rows, ncols, cstart, J));
2360: PetscFunctionReturn(PETSC_SUCCESS);
2361: }
2363: /* Creates a GlobalToLocal mapping with a Local and Global section. This is akin to the routine DMGetLocalToGlobalMapping but without the need of providing a dm.
2364: */
2365: static PetscErrorCode CreateSubGlobalToLocalMapping_private(PetscSection globalsec, PetscSection localsec, ISLocalToGlobalMapping *ltog)
2366: {
2367: PetscInt i, size, dof;
2368: PetscInt *glob2loc;
2370: PetscFunctionBegin;
2371: PetscCall(PetscSectionGetStorageSize(localsec, &size));
2372: PetscCall(PetscMalloc1(size, &glob2loc));
2374: for (i = 0; i < size; i++) {
2375: PetscCall(PetscSectionGetOffset(globalsec, i, &dof));
2376: dof = (dof >= 0) ? dof : -(dof + 1);
2377: glob2loc[i] = dof;
2378: }
2380: PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)globalsec), 1, size, glob2loc, PETSC_OWN_POINTER, ltog));
2381: #if 0
2382: PetscCall(PetscIntView(size,glob2loc,PETSC_VIEWER_STDOUT_WORLD));
2383: #endif
2384: PetscFunctionReturn(PETSC_SUCCESS);
2385: }
2387: #include <petsc/private/matimpl.h>
2389: static PetscErrorCode DMCreateMatrix_Network_Nest(DM dm, Mat *J)
2390: {
2391: DM_Network *network = (DM_Network *)dm->data;
2392: PetscInt eDof, vDof;
2393: Mat j11, j12, j21, j22, bA[2][2];
2394: MPI_Comm comm;
2395: ISLocalToGlobalMapping eISMap, vISMap;
2397: PetscFunctionBegin;
2398: PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
2400: PetscCall(PetscSectionGetConstrainedStorageSize(network->edge.GlobalDofSection, &eDof));
2401: PetscCall(PetscSectionGetConstrainedStorageSize(network->vertex.GlobalDofSection, &vDof));
2403: PetscCall(MatCreate(comm, &j11));
2404: PetscCall(MatSetSizes(j11, eDof, eDof, PETSC_DETERMINE, PETSC_DETERMINE));
2405: PetscCall(MatSetType(j11, MATMPIAIJ));
2407: PetscCall(MatCreate(comm, &j12));
2408: PetscCall(MatSetSizes(j12, eDof, vDof, PETSC_DETERMINE, PETSC_DETERMINE));
2409: PetscCall(MatSetType(j12, MATMPIAIJ));
2411: PetscCall(MatCreate(comm, &j21));
2412: PetscCall(MatSetSizes(j21, vDof, eDof, PETSC_DETERMINE, PETSC_DETERMINE));
2413: PetscCall(MatSetType(j21, MATMPIAIJ));
2415: PetscCall(MatCreate(comm, &j22));
2416: PetscCall(MatSetSizes(j22, vDof, vDof, PETSC_DETERMINE, PETSC_DETERMINE));
2417: PetscCall(MatSetType(j22, MATMPIAIJ));
2419: bA[0][0] = j11;
2420: bA[0][1] = j12;
2421: bA[1][0] = j21;
2422: bA[1][1] = j22;
2424: PetscCall(CreateSubGlobalToLocalMapping_private(network->edge.GlobalDofSection, network->edge.DofSection, &eISMap));
2425: PetscCall(CreateSubGlobalToLocalMapping_private(network->vertex.GlobalDofSection, network->vertex.DofSection, &vISMap));
2427: PetscCall(MatSetLocalToGlobalMapping(j11, eISMap, eISMap));
2428: PetscCall(MatSetLocalToGlobalMapping(j12, eISMap, vISMap));
2429: PetscCall(MatSetLocalToGlobalMapping(j21, vISMap, eISMap));
2430: PetscCall(MatSetLocalToGlobalMapping(j22, vISMap, vISMap));
2432: PetscCall(MatSetUp(j11));
2433: PetscCall(MatSetUp(j12));
2434: PetscCall(MatSetUp(j21));
2435: PetscCall(MatSetUp(j22));
2437: PetscCall(MatCreateNest(comm, 2, NULL, 2, NULL, &bA[0][0], J));
2438: PetscCall(MatSetUp(*J));
2439: PetscCall(MatNestSetVecType(*J, VECNEST));
2440: PetscCall(MatDestroy(&j11));
2441: PetscCall(MatDestroy(&j12));
2442: PetscCall(MatDestroy(&j21));
2443: PetscCall(MatDestroy(&j22));
2445: PetscCall(MatAssemblyBegin(*J, MAT_FINAL_ASSEMBLY));
2446: PetscCall(MatAssemblyEnd(*J, MAT_FINAL_ASSEMBLY));
2447: PetscCall(MatSetOption(*J, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE));
2449: /* Free structures */
2450: PetscCall(ISLocalToGlobalMappingDestroy(&eISMap));
2451: PetscCall(ISLocalToGlobalMappingDestroy(&vISMap));
2452: PetscFunctionReturn(PETSC_SUCCESS);
2453: }
2455: PetscErrorCode DMCreateMatrix_Network(DM dm, Mat *J)
2456: {
2457: DM_Network *network = (DM_Network *)dm->data;
2458: PetscInt eStart, eEnd, vStart, vEnd, rstart, nrows, *rows, localSize;
2459: PetscInt cstart, ncols, j, e, v;
2460: PetscBool ghost, ghost_vc, ghost2, isNest;
2461: Mat Juser;
2462: PetscSection sectionGlobal;
2463: PetscInt nedges, *vptr = NULL, vc, *rows_v; /* suppress maybe-uninitialized warning */
2464: const PetscInt *edges, *cone;
2465: MPI_Comm comm;
2466: MatType mtype;
2467: Vec vd_nz, vo_nz;
2468: PetscInt *dnnz, *onnz;
2469: PetscScalar *vdnz, *vonz;
2471: PetscFunctionBegin;
2472: mtype = dm->mattype;
2473: PetscCall(PetscStrcmp(mtype, MATNEST, &isNest));
2474: if (isNest) {
2475: PetscCall(DMCreateMatrix_Network_Nest(dm, J));
2476: PetscCall(MatSetDM(*J, dm));
2477: PetscFunctionReturn(PETSC_SUCCESS);
2478: }
2480: if (!network->userEdgeJacobian && !network->userVertexJacobian) {
2481: /* user does not provide Jacobian blocks */
2482: PetscCall(DMCreateMatrix_Plex(network->plex, J));
2483: PetscCall(MatSetDM(*J, dm));
2484: PetscFunctionReturn(PETSC_SUCCESS);
2485: }
2487: PetscCall(MatCreate(PetscObjectComm((PetscObject)dm), J));
2488: PetscCall(DMGetGlobalSection(network->plex, §ionGlobal));
2489: PetscCall(PetscSectionGetConstrainedStorageSize(sectionGlobal, &localSize));
2490: PetscCall(MatSetSizes(*J, localSize, localSize, PETSC_DETERMINE, PETSC_DETERMINE));
2492: PetscCall(MatSetType(*J, MATAIJ));
2493: PetscCall(MatSetFromOptions(*J));
2495: /* (1) Set matrix preallocation */
2496: PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
2497: PetscCall(VecCreate(comm, &vd_nz));
2498: PetscCall(VecSetSizes(vd_nz, localSize, PETSC_DECIDE));
2499: PetscCall(VecSetFromOptions(vd_nz));
2500: PetscCall(VecDuplicate(vd_nz, &vo_nz));
2502: /* Set preallocation for edges */
2503: PetscCall(DMNetworkGetEdgeRange(dm, &eStart, &eEnd));
2505: PetscCall(PetscMalloc1(localSize, &rows));
2506: for (e = eStart; e < eEnd; e++) {
2507: /* Get row indices */
2508: PetscCall(DMNetworkGetGlobalVecOffset(dm, e, ALL_COMPONENTS, &rstart));
2509: PetscCall(PetscSectionGetDof(network->DofSection, e, &nrows));
2510: if (nrows) {
2511: for (j = 0; j < nrows; j++) rows[j] = j + rstart;
2513: /* Set preallocation for connected vertices */
2514: PetscCall(DMNetworkGetConnectedVertices(dm, e, &cone));
2515: for (v = 0; v < 2; v++) {
2516: PetscCall(PetscSectionGetDof(network->DofSection, cone[v], &ncols));
2518: if (network->Je) Juser = network->Je[3 * e + 1 + v]; /* Jacobian(e,v) */
2519: else Juser = NULL;
2520: PetscCall(DMNetworkIsGhostVertex(dm, cone[v], &ghost));
2521: PetscCall(MatSetPreallocationblock_private(Juser, nrows, rows, ncols, ghost, vd_nz, vo_nz));
2522: }
2524: /* Set preallocation for edge self */
2525: cstart = rstart;
2526: if (network->Je) Juser = network->Je[3 * e]; /* Jacobian(e,e) */
2527: else Juser = NULL;
2528: PetscCall(MatSetPreallocationblock_private(Juser, nrows, rows, nrows, PETSC_FALSE, vd_nz, vo_nz));
2529: }
2530: }
2532: /* Set preallocation for vertices */
2533: PetscCall(DMNetworkGetVertexRange(dm, &vStart, &vEnd));
2534: if (vEnd - vStart) vptr = network->Jvptr;
2536: for (v = vStart; v < vEnd; v++) {
2537: /* Get row indices */
2538: PetscCall(DMNetworkGetGlobalVecOffset(dm, v, ALL_COMPONENTS, &rstart));
2539: PetscCall(PetscSectionGetDof(network->DofSection, v, &nrows));
2540: if (!nrows) continue;
2542: PetscCall(DMNetworkIsGhostVertex(dm, v, &ghost));
2543: if (ghost) PetscCall(PetscMalloc1(nrows, &rows_v));
2544: else rows_v = rows;
2546: for (j = 0; j < nrows; j++) rows_v[j] = j + rstart;
2548: /* Get supporting edges and connected vertices */
2549: PetscCall(DMNetworkGetSupportingEdges(dm, v, &nedges, &edges));
2551: for (e = 0; e < nedges; e++) {
2552: /* Supporting edges */
2553: PetscCall(DMNetworkGetGlobalVecOffset(dm, edges[e], ALL_COMPONENTS, &cstart));
2554: PetscCall(PetscSectionGetDof(network->DofSection, edges[e], &ncols));
2556: if (network->Jv) Juser = network->Jv[vptr[v - vStart] + 2 * e + 1]; /* Jacobian(v,e) */
2557: else Juser = NULL;
2558: PetscCall(MatSetPreallocationblock_private(Juser, nrows, rows_v, ncols, ghost, vd_nz, vo_nz));
2560: /* Connected vertices */
2561: PetscCall(DMNetworkGetConnectedVertices(dm, edges[e], &cone));
2562: vc = (v == cone[0]) ? cone[1] : cone[0];
2563: PetscCall(DMNetworkIsGhostVertex(dm, vc, &ghost_vc));
2565: PetscCall(PetscSectionGetDof(network->DofSection, vc, &ncols));
2567: if (network->Jv) Juser = network->Jv[vptr[v - vStart] + 2 * e + 2]; /* Jacobian(v,vc) */
2568: else Juser = NULL;
2569: if (ghost_vc || ghost) ghost2 = PETSC_TRUE;
2570: else ghost2 = PETSC_FALSE;
2571: PetscCall(MatSetPreallocationblock_private(Juser, nrows, rows_v, ncols, ghost2, vd_nz, vo_nz));
2572: }
2574: /* Set preallocation for vertex self */
2575: PetscCall(DMNetworkIsGhostVertex(dm, v, &ghost));
2576: if (!ghost) {
2577: PetscCall(DMNetworkGetGlobalVecOffset(dm, v, ALL_COMPONENTS, &cstart));
2578: if (network->Jv) Juser = network->Jv[vptr[v - vStart]]; /* Jacobian(v,v) */
2579: else Juser = NULL;
2580: PetscCall(MatSetPreallocationblock_private(Juser, nrows, rows_v, nrows, PETSC_FALSE, vd_nz, vo_nz));
2581: }
2582: if (ghost) PetscCall(PetscFree(rows_v));
2583: }
2585: PetscCall(VecAssemblyBegin(vd_nz));
2586: PetscCall(VecAssemblyBegin(vo_nz));
2588: PetscCall(PetscMalloc2(localSize, &dnnz, localSize, &onnz));
2590: PetscCall(VecAssemblyEnd(vd_nz));
2591: PetscCall(VecAssemblyEnd(vo_nz));
2593: PetscCall(VecGetArray(vd_nz, &vdnz));
2594: PetscCall(VecGetArray(vo_nz, &vonz));
2595: for (j = 0; j < localSize; j++) {
2596: dnnz[j] = (PetscInt)PetscRealPart(vdnz[j]);
2597: onnz[j] = (PetscInt)PetscRealPart(vonz[j]);
2598: }
2599: PetscCall(VecRestoreArray(vd_nz, &vdnz));
2600: PetscCall(VecRestoreArray(vo_nz, &vonz));
2601: PetscCall(VecDestroy(&vd_nz));
2602: PetscCall(VecDestroy(&vo_nz));
2604: PetscCall(MatSeqAIJSetPreallocation(*J, 0, dnnz));
2605: PetscCall(MatMPIAIJSetPreallocation(*J, 0, dnnz, 0, onnz));
2606: PetscCall(MatSetOption(*J, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE));
2608: PetscCall(PetscFree2(dnnz, onnz));
2610: /* (2) Set matrix entries for edges */
2611: for (e = eStart; e < eEnd; e++) {
2612: /* Get row indices */
2613: PetscCall(DMNetworkGetGlobalVecOffset(dm, e, ALL_COMPONENTS, &rstart));
2614: PetscCall(PetscSectionGetDof(network->DofSection, e, &nrows));
2615: if (nrows) {
2616: for (j = 0; j < nrows; j++) rows[j] = j + rstart;
2618: /* Set matrix entries for connected vertices */
2619: PetscCall(DMNetworkGetConnectedVertices(dm, e, &cone));
2620: for (v = 0; v < 2; v++) {
2621: PetscCall(DMNetworkGetGlobalVecOffset(dm, cone[v], ALL_COMPONENTS, &cstart));
2622: PetscCall(PetscSectionGetDof(network->DofSection, cone[v], &ncols));
2624: if (network->Je) Juser = network->Je[3 * e + 1 + v]; /* Jacobian(e,v) */
2625: else Juser = NULL;
2626: PetscCall(MatSetblock_private(Juser, nrows, rows, ncols, cstart, J));
2627: }
2629: /* Set matrix entries for edge self */
2630: cstart = rstart;
2631: if (network->Je) Juser = network->Je[3 * e]; /* Jacobian(e,e) */
2632: else Juser = NULL;
2633: PetscCall(MatSetblock_private(Juser, nrows, rows, nrows, cstart, J));
2634: }
2635: }
2637: /* Set matrix entries for vertices */
2638: for (v = vStart; v < vEnd; v++) {
2639: /* Get row indices */
2640: PetscCall(DMNetworkGetGlobalVecOffset(dm, v, ALL_COMPONENTS, &rstart));
2641: PetscCall(PetscSectionGetDof(network->DofSection, v, &nrows));
2642: if (!nrows) continue;
2644: PetscCall(DMNetworkIsGhostVertex(dm, v, &ghost));
2645: if (ghost) PetscCall(PetscMalloc1(nrows, &rows_v));
2646: else rows_v = rows;
2647: for (j = 0; j < nrows; j++) rows_v[j] = j + rstart;
2649: /* Get supporting edges and connected vertices */
2650: PetscCall(DMNetworkGetSupportingEdges(dm, v, &nedges, &edges));
2652: for (e = 0; e < nedges; e++) {
2653: /* Supporting edges */
2654: PetscCall(DMNetworkGetGlobalVecOffset(dm, edges[e], ALL_COMPONENTS, &cstart));
2655: PetscCall(PetscSectionGetDof(network->DofSection, edges[e], &ncols));
2657: if (network->Jv) Juser = network->Jv[vptr[v - vStart] + 2 * e + 1]; /* Jacobian(v,e) */
2658: else Juser = NULL;
2659: PetscCall(MatSetblock_private(Juser, nrows, rows_v, ncols, cstart, J));
2661: /* Connected vertices */
2662: PetscCall(DMNetworkGetConnectedVertices(dm, edges[e], &cone));
2663: vc = (v == cone[0]) ? cone[1] : cone[0];
2665: PetscCall(DMNetworkGetGlobalVecOffset(dm, vc, ALL_COMPONENTS, &cstart));
2666: PetscCall(PetscSectionGetDof(network->DofSection, vc, &ncols));
2668: if (network->Jv) Juser = network->Jv[vptr[v - vStart] + 2 * e + 2]; /* Jacobian(v,vc) */
2669: else Juser = NULL;
2670: PetscCall(MatSetblock_private(Juser, nrows, rows_v, ncols, cstart, J));
2671: }
2673: /* Set matrix entries for vertex self */
2674: if (!ghost) {
2675: PetscCall(DMNetworkGetGlobalVecOffset(dm, v, ALL_COMPONENTS, &cstart));
2676: if (network->Jv) Juser = network->Jv[vptr[v - vStart]]; /* Jacobian(v,v) */
2677: else Juser = NULL;
2678: PetscCall(MatSetblock_private(Juser, nrows, rows_v, nrows, cstart, J));
2679: }
2680: if (ghost) PetscCall(PetscFree(rows_v));
2681: }
2682: PetscCall(PetscFree(rows));
2684: PetscCall(MatAssemblyBegin(*J, MAT_FINAL_ASSEMBLY));
2685: PetscCall(MatAssemblyEnd(*J, MAT_FINAL_ASSEMBLY));
2687: PetscCall(MatSetDM(*J, dm));
2688: PetscFunctionReturn(PETSC_SUCCESS);
2689: }
2691: static PetscErrorCode DMNetworkDestroyComponentData(DM dm)
2692: {
2693: DM_Network *network = (DM_Network *)dm->data;
2694: PetscInt j, np;
2696: PetscFunctionBegin;
2697: if (network->header) {
2698: np = network->cloneshared->pEnd - network->cloneshared->pStart;
2699: for (j = 0; j < np; j++) {
2700: PetscCall(PetscFree5(network->header[j].size, network->header[j].key, network->header[j].offset, network->header[j].nvar, network->header[j].offsetvarrel));
2701: PetscCall(PetscFree(network->cvalue[j].data));
2702: }
2703: PetscCall(PetscFree2(network->header, network->cvalue));
2704: }
2705: PetscFunctionReturn(PETSC_SUCCESS);
2706: }
2708: PetscErrorCode DMDestroy_Network(DM dm)
2709: {
2710: DM_Network *network = (DM_Network *)dm->data;
2712: PetscFunctionBegin;
2713: /*
2714: Developer Note: Due to the mixed topological definition of DMNetwork and data defined ON the
2715: network like DofSection, DataSection, *componentdataarray, and friends, when cloning, we share
2716: only the true topological data, and make our own data ON the network. Thus refct only refers
2717: to the number of references to topological data, and data ON the network is always destroyed.
2718: It is understood this is atypical for a DM, but is very intentional.
2719: */
2721: /* Always destroy data ON the network */
2722: PetscCall(PetscFree(network->Je));
2723: if (network->Jv) {
2724: PetscCall(PetscFree(network->Jvptr));
2725: PetscCall(PetscFree(network->Jv));
2726: }
2727: PetscCall(PetscSectionDestroy(&network->DataSection));
2728: PetscCall(PetscSectionDestroy(&network->DofSection));
2729: PetscCall(PetscFree(network->component));
2730: PetscCall(PetscFree(network->componentdataarray));
2731: PetscCall(DMNetworkDestroyComponentData(dm));
2733: PetscCall(DMDestroy(&network->plex)); /* this is cloned in DMClone_Network, so safe to destroy */
2735: /*
2736: Developer Note: The DMNetworkVertexInfo and DMNetworkEdgeInfo data structures are completely
2737: destroyed as they are again a mix of topological data:
2738: ISLocalToGlobalMapping mapping;
2739: PetscSF sf;
2740: and data ON the network
2741: PetscSection DofSection;
2742: PetscSection GlobalDofSection;
2743: And the only way to access them currently is through DMNetworkAssembleGraphStructures which assembles
2744: everything. So we must destroy everything and require DMNetworkAssembleGraphStructures is called again
2745: for any clone.
2746: */
2747: PetscCall(ISLocalToGlobalMappingDestroy(&network->vertex.mapping));
2748: PetscCall(PetscSectionDestroy(&network->vertex.DofSection));
2749: PetscCall(PetscSectionDestroy(&network->vertex.GlobalDofSection));
2750: PetscCall(PetscSFDestroy(&network->vertex.sf));
2751: /* edge */
2752: PetscCall(ISLocalToGlobalMappingDestroy(&network->edge.mapping));
2753: PetscCall(PetscSectionDestroy(&network->edge.DofSection));
2754: PetscCall(PetscSectionDestroy(&network->edge.GlobalDofSection));
2755: PetscCall(PetscSFDestroy(&network->edge.sf));
2756: /* viewer options */
2757: PetscCall(ISDestroy(&network->vieweroptions.viewranks));
2758: /* Destroy the potentially cloneshared data */
2759: if (--network->cloneshared->refct <= 0) {
2760: /* Developer Note: I'm not sure if vltog can be reused or not, as I'm not sure what it's purpose is. I
2761: naively think it can be reused. */
2762: PetscCall(PetscFree(network->cloneshared->vltog));
2763: for (PetscInt j = 0; j < network->cloneshared->Nsvtx; j++) PetscCall(PetscFree(network->cloneshared->svtx[j].sv));
2764: PetscCall(PetscFree(network->cloneshared->svtx));
2765: PetscCall(PetscFree2(network->cloneshared->subnetedge, network->cloneshared->subnetvtx));
2766: PetscCall(PetscHMapIDestroy(&network->cloneshared->svtable));
2767: PetscCall(PetscFree(network->cloneshared->subnet));
2768: PetscCall(PetscFree(network->cloneshared));
2769: }
2770: PetscCall(PetscFree(network)); /* Always freed as this structure is copied in a clone, not cloneshared */
2771: PetscFunctionReturn(PETSC_SUCCESS);
2772: }
2774: PetscErrorCode DMGlobalToLocalBegin_Network(DM dm, Vec g, InsertMode mode, Vec l)
2775: {
2776: DM_Network *network = (DM_Network *)dm->data;
2778: PetscFunctionBegin;
2779: PetscCall(DMGlobalToLocalBegin(network->plex, g, mode, l));
2780: PetscFunctionReturn(PETSC_SUCCESS);
2781: }
2783: PetscErrorCode DMGlobalToLocalEnd_Network(DM dm, Vec g, InsertMode mode, Vec l)
2784: {
2785: DM_Network *network = (DM_Network *)dm->data;
2787: PetscFunctionBegin;
2788: PetscCall(DMGlobalToLocalEnd(network->plex, g, mode, l));
2789: PetscFunctionReturn(PETSC_SUCCESS);
2790: }
2792: PetscErrorCode DMLocalToGlobalBegin_Network(DM dm, Vec l, InsertMode mode, Vec g)
2793: {
2794: DM_Network *network = (DM_Network *)dm->data;
2796: PetscFunctionBegin;
2797: PetscCall(DMLocalToGlobalBegin(network->plex, l, mode, g));
2798: PetscFunctionReturn(PETSC_SUCCESS);
2799: }
2801: PetscErrorCode DMLocalToGlobalEnd_Network(DM dm, Vec l, InsertMode mode, Vec g)
2802: {
2803: DM_Network *network = (DM_Network *)dm->data;
2805: PetscFunctionBegin;
2806: PetscCall(DMLocalToGlobalEnd(network->plex, l, mode, g));
2807: PetscFunctionReturn(PETSC_SUCCESS);
2808: }
2810: /*@
2811: DMNetworkGetVertexLocalToGlobalOrdering - Get vertex global index
2813: Not Collective
2815: Input Parameters:
2816: + dm - the `DMNETWORK` object
2817: - vloc - local vertex ordering, start from 0
2819: Output Parameter:
2820: . vg - global vertex ordering, start from 0
2822: Level: advanced
2824: .seealso: `DM`, `DMNETWORK`, `DMNetworkSetVertexLocalToGlobalOrdering()`
2825: @*/
2826: PetscErrorCode DMNetworkGetVertexLocalToGlobalOrdering(DM dm, PetscInt vloc, PetscInt *vg)
2827: {
2828: DM_Network *network = (DM_Network *)dm->data;
2829: PetscInt *vltog = network->cloneshared->vltog;
2831: PetscFunctionBegin;
2832: PetscCheck(vltog, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Must call DMNetworkSetVertexLocalToGlobalOrdering() first");
2833: *vg = vltog[vloc];
2834: PetscFunctionReturn(PETSC_SUCCESS);
2835: }
2837: /*@
2838: DMNetworkSetVertexLocalToGlobalOrdering - Create and setup vertex local to global map
2840: Collective
2842: Input Parameters:
2843: . dm - the `DMNETWORK` object
2845: Level: advanced
2847: .seealso: `DM`, `DMNETWORK`, `DMNetworkGetGlobalVertexIndex()`
2848: @*/
2849: PetscErrorCode DMNetworkSetVertexLocalToGlobalOrdering(DM dm)
2850: {
2851: DM_Network *network = (DM_Network *)dm->data;
2852: MPI_Comm comm;
2853: PetscMPIInt rank, size, *displs = NULL, *recvcounts = NULL, remoterank;
2854: PetscBool ghost;
2855: PetscInt *vltog, nroots, nleaves, *vrange, k, N, lidx, ii;
2856: const PetscSFNode *iremote;
2857: PetscSF vsf;
2858: Vec Vleaves, Vleaves_seq;
2859: VecScatter ctx;
2860: PetscScalar *varr, val;
2861: const PetscScalar *varr_read;
2863: PetscFunctionBegin;
2864: PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
2865: PetscCallMPI(MPI_Comm_size(comm, &size));
2866: PetscCallMPI(MPI_Comm_rank(comm, &rank));
2868: if (size == 1) {
2869: nroots = network->cloneshared->vEnd - network->cloneshared->vStart;
2870: PetscCall(PetscMalloc1(nroots, &vltog));
2871: for (PetscInt i = 0; i < nroots; i++) vltog[i] = i;
2872: network->cloneshared->vltog = vltog;
2873: PetscFunctionReturn(PETSC_SUCCESS);
2874: }
2876: PetscCheck(network->cloneshared->distributecalled, comm, PETSC_ERR_ARG_WRONGSTATE, "Must call DMNetworkDistribute() first");
2877: PetscCall(PetscFree(network->cloneshared->vltog));
2879: PetscCall(DMNetworkSetSubMap_private(dm, network->cloneshared->vStart, network->cloneshared->vEnd, &network->vertex.mapping));
2880: PetscCall(PetscSFGetSubSF(network->plex->sf, network->vertex.mapping, &network->vertex.sf));
2881: vsf = network->vertex.sf;
2883: PetscCall(PetscMalloc3(size + 1, &vrange, size, &displs, size, &recvcounts));
2884: PetscCall(PetscSFGetGraph(vsf, &nroots, &nleaves, NULL, &iremote));
2886: for (PetscMPIInt i = 0; i < size; i++) {
2887: displs[i] = i;
2888: recvcounts[i] = 1;
2889: }
2891: ii = nroots - nleaves; /* local number of vertices, excluding ghosts */
2892: vrange[0] = 0;
2893: PetscCallMPI(MPI_Allgatherv(&ii, 1, MPIU_INT, vrange + 1, recvcounts, displs, MPIU_INT, comm));
2894: for (PetscMPIInt i = 2; i < size + 1; i++) vrange[i] += vrange[i - 1];
2896: PetscCall(PetscMalloc1(nroots, &vltog));
2897: network->cloneshared->vltog = vltog;
2899: /* Set vltog for non-ghost vertices */
2900: k = 0;
2901: for (PetscInt i = 0; i < nroots; i++) {
2902: PetscCall(DMNetworkIsGhostVertex(dm, i + network->cloneshared->vStart, &ghost));
2903: if (ghost) continue;
2904: vltog[i] = vrange[rank] + k++;
2905: }
2906: PetscCall(PetscFree3(vrange, displs, recvcounts));
2908: /* Set vltog for ghost vertices */
2909: /* (a) create parallel Vleaves and sequential Vleaves_seq to convert local iremote[*].index to global index */
2910: PetscCall(VecCreate(comm, &Vleaves));
2911: PetscCall(VecSetSizes(Vleaves, 2 * nleaves, PETSC_DETERMINE));
2912: PetscCall(VecSetFromOptions(Vleaves));
2913: PetscCall(VecGetArray(Vleaves, &varr));
2914: for (PetscInt i = 0; i < nleaves; i++) {
2915: varr[2 * i] = (PetscScalar)iremote[i].rank; /* rank of remote process */
2916: varr[2 * i + 1] = (PetscScalar)iremote[i].index; /* local index in remote process */
2917: }
2918: PetscCall(VecRestoreArray(Vleaves, &varr));
2920: /* (b) scatter local info to remote processes via VecScatter() */
2921: PetscCall(VecScatterCreateToAll(Vleaves, &ctx, &Vleaves_seq));
2922: PetscCall(VecScatterBegin(ctx, Vleaves, Vleaves_seq, INSERT_VALUES, SCATTER_FORWARD));
2923: PetscCall(VecScatterEnd(ctx, Vleaves, Vleaves_seq, INSERT_VALUES, SCATTER_FORWARD));
2925: /* (c) convert local indices to global indices in parallel vector Vleaves */
2926: PetscCall(VecGetSize(Vleaves_seq, &N));
2927: PetscCall(VecGetArrayRead(Vleaves_seq, &varr_read));
2928: for (PetscInt i = 0; i < N; i += 2) {
2929: remoterank = (PetscMPIInt)PetscRealPart(varr_read[i]);
2930: if (remoterank == rank) {
2931: k = i + 1; /* row number */
2932: lidx = (PetscInt)PetscRealPart(varr_read[i + 1]);
2933: val = (PetscScalar)vltog[lidx]; /* global index for non-ghost vertex computed above */
2934: PetscCall(VecSetValues(Vleaves, 1, &k, &val, INSERT_VALUES));
2935: }
2936: }
2937: PetscCall(VecRestoreArrayRead(Vleaves_seq, &varr_read));
2938: PetscCall(VecAssemblyBegin(Vleaves));
2939: PetscCall(VecAssemblyEnd(Vleaves));
2941: /* (d) Set vltog for ghost vertices by copying local values of Vleaves */
2942: PetscCall(VecGetArrayRead(Vleaves, &varr_read));
2943: k = 0;
2944: for (PetscInt i = 0; i < nroots; i++) {
2945: PetscCall(DMNetworkIsGhostVertex(dm, i + network->cloneshared->vStart, &ghost));
2946: if (!ghost) continue;
2947: vltog[i] = (PetscInt)PetscRealPart(varr_read[2 * k + 1]);
2948: k++;
2949: }
2950: PetscCall(VecRestoreArrayRead(Vleaves, &varr_read));
2952: PetscCall(VecDestroy(&Vleaves));
2953: PetscCall(VecDestroy(&Vleaves_seq));
2954: PetscCall(VecScatterDestroy(&ctx));
2955: PetscFunctionReturn(PETSC_SUCCESS);
2956: }
2958: static inline PetscErrorCode DMISAddSize_private(DM_Network *network, PetscInt p, PetscInt numkeys, PetscInt keys[], PetscInt blocksize[], PetscInt nselectedvar[], PetscInt *nidx)
2959: {
2960: PetscInt i, j, ncomps, nvar, key, offset = 0;
2961: DMNetworkComponentHeader header;
2963: PetscFunctionBegin;
2964: PetscCall(PetscSectionGetOffset(network->DataSection, p, &offset));
2965: ncomps = ((DMNetworkComponentHeader)(network->componentdataarray + offset))->ndata;
2966: header = (DMNetworkComponentHeader)(network->componentdataarray + offset);
2968: for (i = 0; i < ncomps; i++) {
2969: key = header->key[i];
2970: nvar = header->nvar[i];
2971: for (j = 0; j < numkeys; j++) {
2972: if (key == keys[j]) {
2973: if (!blocksize || blocksize[j] == -1) {
2974: *nidx += nvar;
2975: } else {
2976: *nidx += nselectedvar[j] * nvar / blocksize[j];
2977: }
2978: }
2979: }
2980: }
2981: PetscFunctionReturn(PETSC_SUCCESS);
2982: }
2984: static inline PetscErrorCode DMISComputeIdx_private(DM dm, PetscInt p, PetscInt numkeys, PetscInt keys[], PetscInt blocksize[], PetscInt nselectedvar[], PetscInt *selectedvar[], PetscInt *ii, PetscInt *idx)
2985: {
2986: PetscInt i, j, ncomps, nvar, key, offsetg, k, k1, offset = 0;
2987: DM_Network *network = (DM_Network *)dm->data;
2988: DMNetworkComponentHeader header;
2990: PetscFunctionBegin;
2991: PetscCall(PetscSectionGetOffset(network->DataSection, p, &offset));
2992: ncomps = ((DMNetworkComponentHeader)(network->componentdataarray + offset))->ndata;
2993: header = (DMNetworkComponentHeader)(network->componentdataarray + offset);
2995: for (i = 0; i < ncomps; i++) {
2996: key = header->key[i];
2997: nvar = header->nvar[i];
2998: for (j = 0; j < numkeys; j++) {
2999: if (key != keys[j]) continue;
3001: PetscCall(DMNetworkGetGlobalVecOffset(dm, p, i, &offsetg));
3002: if (!blocksize || blocksize[j] == -1) {
3003: for (k = 0; k < nvar; k++) idx[(*ii)++] = offsetg + k;
3004: } else {
3005: for (k = 0; k < nvar; k += blocksize[j]) {
3006: for (k1 = 0; k1 < nselectedvar[j]; k1++) idx[(*ii)++] = offsetg + k + selectedvar[j][k1];
3007: }
3008: }
3009: }
3010: }
3011: PetscFunctionReturn(PETSC_SUCCESS);
3012: }
3014: /*@
3015: DMNetworkCreateIS - Create an index set object from the global vector of the network
3017: Collective
3019: Input Parameters:
3020: + dm - `DMNETWORK` object
3021: . numkeys - number of keys
3022: . keys - array of keys that define the components of the variables you wish to extract
3023: . blocksize - block size of the variables associated to the component
3024: . nselectedvar - number of variables in each block to select
3025: - selectedvar - the offset into the block of each variable in each block to select
3027: Output Parameter:
3028: . is - the index set
3030: Level: advanced
3032: Notes:
3033: Use blocksize[i] of -1 to indicate select all the variables of the i-th component, for which nselectvar[i] and selectedvar[i] are ignored. Use` NULL`, `NULL`, `NULL` to indicate for all selected components one wishes to obtain all the values of that component. For example, `DMNetworkCreateIS`(dm,1,&key,NULL,NULL,NULL,&is) will return an is that extracts all the variables for the 0-th component.
3035: .seealso: `DM`, `DMNETWORK`, `DMNetworkCreate()`, `ISCreateGeneral()`, `DMNetworkCreateLocalIS()`
3036: @*/
3037: PetscErrorCode DMNetworkCreateIS(DM dm, PetscInt numkeys, PetscInt keys[], PetscInt blocksize[], PetscInt nselectedvar[], PetscInt *selectedvar[], IS *is)
3038: {
3039: MPI_Comm comm;
3040: DM_Network *network = (DM_Network *)dm->data;
3041: PetscInt i, p, estart, eend, vstart, vend, nidx, *idx;
3042: PetscBool ghost;
3044: PetscFunctionBegin;
3045: PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
3047: /* Check input parameters */
3048: for (i = 0; i < numkeys; i++) {
3049: if (!blocksize || blocksize[i] == -1) continue;
3050: PetscCheck(nselectedvar[i] <= blocksize[i], PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "number of selectedvariables %" PetscInt_FMT " cannot be larger than blocksize %" PetscInt_FMT, nselectedvar[i], blocksize[i]);
3051: }
3053: PetscCall(DMNetworkGetEdgeRange(dm, &estart, &eend));
3054: PetscCall(DMNetworkGetVertexRange(dm, &vstart, &vend));
3056: /* Get local number of idx */
3057: nidx = 0;
3058: for (p = estart; p < eend; p++) PetscCall(DMISAddSize_private(network, p, numkeys, keys, blocksize, nselectedvar, &nidx));
3059: for (p = vstart; p < vend; p++) {
3060: PetscCall(DMNetworkIsGhostVertex(dm, p, &ghost));
3061: if (ghost) continue;
3062: PetscCall(DMISAddSize_private(network, p, numkeys, keys, blocksize, nselectedvar, &nidx));
3063: }
3065: /* Compute idx */
3066: PetscCall(PetscMalloc1(nidx, &idx));
3067: i = 0;
3068: for (p = estart; p < eend; p++) PetscCall(DMISComputeIdx_private(dm, p, numkeys, keys, blocksize, nselectedvar, selectedvar, &i, idx));
3069: for (p = vstart; p < vend; p++) {
3070: PetscCall(DMNetworkIsGhostVertex(dm, p, &ghost));
3071: if (ghost) continue;
3072: PetscCall(DMISComputeIdx_private(dm, p, numkeys, keys, blocksize, nselectedvar, selectedvar, &i, idx));
3073: }
3075: /* Create is */
3076: PetscCall(ISCreateGeneral(comm, nidx, idx, PETSC_COPY_VALUES, is));
3077: PetscCall(PetscFree(idx));
3078: PetscFunctionReturn(PETSC_SUCCESS);
3079: }
3081: static inline PetscErrorCode DMISComputeLocalIdx_private(DM dm, PetscInt p, PetscInt numkeys, PetscInt keys[], PetscInt blocksize[], PetscInt nselectedvar[], PetscInt *selectedvar[], PetscInt *ii, PetscInt *idx)
3082: {
3083: PetscInt i, j, ncomps, nvar, key, offsetl, k, k1, offset = 0;
3084: DM_Network *network = (DM_Network *)dm->data;
3085: DMNetworkComponentHeader header;
3087: PetscFunctionBegin;
3088: PetscCall(PetscSectionGetOffset(network->DataSection, p, &offset));
3089: ncomps = ((DMNetworkComponentHeader)(network->componentdataarray + offset))->ndata;
3090: header = (DMNetworkComponentHeader)(network->componentdataarray + offset);
3092: for (i = 0; i < ncomps; i++) {
3093: key = header->key[i];
3094: nvar = header->nvar[i];
3095: for (j = 0; j < numkeys; j++) {
3096: if (key != keys[j]) continue;
3098: PetscCall(DMNetworkGetLocalVecOffset(dm, p, i, &offsetl));
3099: if (!blocksize || blocksize[j] == -1) {
3100: for (k = 0; k < nvar; k++) idx[(*ii)++] = offsetl + k;
3101: } else {
3102: for (k = 0; k < nvar; k += blocksize[j]) {
3103: for (k1 = 0; k1 < nselectedvar[j]; k1++) idx[(*ii)++] = offsetl + k + selectedvar[j][k1];
3104: }
3105: }
3106: }
3107: }
3108: PetscFunctionReturn(PETSC_SUCCESS);
3109: }
3111: /*@
3112: DMNetworkCreateLocalIS - Create an index set object from the local vector of the network
3114: Not Collective
3116: Input Parameters:
3117: + dm - `DMNETWORK` object
3118: . numkeys - number of keys
3119: . keys - array of keys that define the components of the variables you wish to extract
3120: . blocksize - block size of the variables associated to the component
3121: . nselectedvar - number of variables in each block to select
3122: - selectedvar - the offset into the block of each variable in each block to select
3124: Output Parameter:
3125: . is - the index set
3127: Level: advanced
3129: Notes:
3130: Use blocksize[i] of -1 to indicate select all the variables of the i-th component, for which nselectvar[i] and selectedvar[i] are ignored. Use `NULL`, `NULL`, `NULL` to indicate for all selected components one wishes to obtain all the values of that component. For example, `DMNetworkCreateLocalIS`(dm,1,&key,`NULL`,`NULL`,`NULL`,&is) will return an is that extracts all the variables for the 0-th component.
3132: .seealso: `DM`, `DMNETWORK`, `DMNetworkCreate()`, `DMNetworkCreateIS()`, `ISCreateGeneral()`
3133: @*/
3134: PetscErrorCode DMNetworkCreateLocalIS(DM dm, PetscInt numkeys, PetscInt keys[], PetscInt blocksize[], PetscInt nselectedvar[], PetscInt *selectedvar[], IS *is)
3135: {
3136: DM_Network *network = (DM_Network *)dm->data;
3137: PetscInt i, p, pstart, pend, nidx, *idx;
3139: PetscFunctionBegin;
3140: /* Check input parameters */
3141: for (i = 0; i < numkeys; i++) {
3142: if (!blocksize || blocksize[i] == -1) continue;
3143: PetscCheck(nselectedvar[i] <= blocksize[i], PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "number of selectedvariables %" PetscInt_FMT " cannot be larger than blocksize %" PetscInt_FMT, nselectedvar[i], blocksize[i]);
3144: }
3146: pstart = network->cloneshared->pStart;
3147: pend = network->cloneshared->pEnd;
3149: /* Get local number of idx */
3150: nidx = 0;
3151: for (p = pstart; p < pend; p++) PetscCall(DMISAddSize_private(network, p, numkeys, keys, blocksize, nselectedvar, &nidx));
3153: /* Compute local idx */
3154: PetscCall(PetscMalloc1(nidx, &idx));
3155: i = 0;
3156: for (p = pstart; p < pend; p++) PetscCall(DMISComputeLocalIdx_private(dm, p, numkeys, keys, blocksize, nselectedvar, selectedvar, &i, idx));
3158: /* Create is */
3159: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, nidx, idx, PETSC_COPY_VALUES, is));
3160: PetscCall(PetscFree(idx));
3161: PetscFunctionReturn(PETSC_SUCCESS);
3162: }
3163: /*@
3164: DMNetworkFinalizeComponents - Sets up internal data structures for the sections and components. It is called after registering new components and adding all components
3165: to the cloned network. After calling this subroutine, no new components can be added to the network.
3167: Collective
3169: Input Parameter:
3170: . dm - the `DMNETWORK` object
3172: Level: beginner
3174: .seealso: `DM`, `DMNETWORK`, `DMNetworkAddComponent()`, `DMNetworkRegisterComponent()`, `DMSetUp()`
3175: @*/
3176: PetscErrorCode DMNetworkFinalizeComponents(DM dm)
3177: {
3178: DM_Network *network = (DM_Network *)dm->data;
3180: PetscFunctionBegin;
3181: if (network->componentsetup) PetscFunctionReturn(PETSC_SUCCESS);
3182: PetscCall(DMNetworkComponentSetUp(dm));
3183: PetscCall(DMNetworkVariablesSetUp(dm));
3184: PetscCall(DMSetLocalSection(network->plex, network->DofSection));
3185: PetscCall(DMGetGlobalSection(network->plex, &network->GlobalDofSection));
3186: network->componentsetup = PETSC_TRUE;
3187: PetscFunctionReturn(PETSC_SUCCESS);
3188: }