Actual source code: isdiff.c
2: #include <petsc/private/isimpl.h>
3: #include <petsc/private/sectionimpl.h>
4: #include <petscbt.h>
6: /*@
7: ISDifference - Computes the difference between two index sets.
9: Collective on IS
11: Input Parameters:
12: + is1 - first index, to have items removed from it
13: - is2 - index values to be removed
15: Output Parameters:
16: . isout - is1 - is2
18: Notes:
19: Negative values are removed from the lists. is2 may have values
20: that are not in is1. This requires O(imax-imin) memory and O(imax-imin)
21: work, where imin and imax are the bounds on the indices in is1.
23: If is2 is NULL, the result is the same as for an empty IS, i.e., a duplicate of is1.
25: Level: intermediate
27: .seealso: ISDestroy(), ISView(), ISSum(), ISExpand()
28: @*/
29: PetscErrorCode ISDifference(IS is1,IS is2,IS *isout)
30: {
32: PetscInt i,n1,n2,imin,imax,nout,*iout;
33: const PetscInt *i1,*i2;
34: PetscBT mask;
35: MPI_Comm comm;
40: if (!is2) {
41: ISDuplicate(is1, isout);
42: return(0);
43: }
46: ISGetIndices(is1,&i1);
47: ISGetLocalSize(is1,&n1);
49: /* Create a bit mask array to contain required values */
50: if (n1) {
51: imin = PETSC_MAX_INT;
52: imax = 0;
53: for (i=0; i<n1; i++) {
54: if (i1[i] < 0) continue;
55: imin = PetscMin(imin,i1[i]);
56: imax = PetscMax(imax,i1[i]);
57: }
58: } else imin = imax = 0;
60: PetscBTCreate(imax-imin,&mask);
61: /* Put the values from is1 */
62: for (i=0; i<n1; i++) {
63: if (i1[i] < 0) continue;
64: PetscBTSet(mask,i1[i] - imin);
65: }
66: ISRestoreIndices(is1,&i1);
67: /* Remove the values from is2 */
68: ISGetIndices(is2,&i2);
69: ISGetLocalSize(is2,&n2);
70: for (i=0; i<n2; i++) {
71: if (i2[i] < imin || i2[i] > imax) continue;
72: PetscBTClear(mask,i2[i] - imin);
73: }
74: ISRestoreIndices(is2,&i2);
76: /* Count the number in the difference */
77: nout = 0;
78: for (i=0; i<imax-imin+1; i++) {
79: if (PetscBTLookup(mask,i)) nout++;
80: }
82: /* create the new IS containing the difference */
83: PetscMalloc1(nout,&iout);
84: nout = 0;
85: for (i=0; i<imax-imin+1; i++) {
86: if (PetscBTLookup(mask,i)) iout[nout++] = i + imin;
87: }
88: PetscObjectGetComm((PetscObject)is1,&comm);
89: ISCreateGeneral(comm,nout,iout,PETSC_OWN_POINTER,isout);
91: PetscBTDestroy(&mask);
92: return(0);
93: }
95: /*@
96: ISSum - Computes the sum (union) of two index sets.
98: Only sequential version (at the moment)
100: Input Parameters:
101: + is1 - index set to be extended
102: - is2 - index values to be added
104: Output Parameter:
105: . is3 - the sum; this can not be is1 or is2
107: Notes:
108: If n1 and n2 are the sizes of the sets, this takes O(n1+n2) time;
110: Both index sets need to be sorted on input.
112: Level: intermediate
114: .seealso: ISDestroy(), ISView(), ISDifference(), ISExpand()
116: @*/
117: PetscErrorCode ISSum(IS is1,IS is2,IS *is3)
118: {
119: MPI_Comm comm;
120: PetscBool f;
121: PetscMPIInt size;
122: const PetscInt *i1,*i2;
123: PetscInt n1,n2,n3, p1,p2, *iout;
129: PetscObjectGetComm((PetscObject)(is1),&comm);
130: MPI_Comm_size(comm,&size);
131: if (size>1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Currently only for uni-processor IS");
133: ISSorted(is1,&f);
134: if (!f) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Arg 1 is not sorted");
135: ISSorted(is2,&f);
136: if (!f) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Arg 2 is not sorted");
138: ISGetLocalSize(is1,&n1);
139: ISGetLocalSize(is2,&n2);
140: if (!n2) {
141: ISDuplicate(is1,is3);
142: return(0);
143: }
144: ISGetIndices(is1,&i1);
145: ISGetIndices(is2,&i2);
147: p1 = 0; p2 = 0; n3 = 0;
148: do {
149: if (p1==n1) { /* cleanup for is2 */ n3 += n2-p2; break;
150: } else {
151: while (p2<n2 && i2[p2]<i1[p1]) {
152: n3++; p2++;
153: }
154: if (p2==n2) {
155: /* cleanup for is1 */
156: n3 += n1-p1; break;
157: } else {
158: if (i2[p2]==i1[p1]) { n3++; p1++; p2++; }
159: }
160: }
161: if (p2==n2) {
162: /* cleanup for is1 */
163: n3 += n1-p1; break;
164: } else {
165: while (p1<n1 && i1[p1]<i2[p2]) {
166: n3++; p1++;
167: }
168: if (p1==n1) {
169: /* clean up for is2 */
170: n3 += n2-p2; break;
171: } else {
172: if (i1[p1]==i2[p2]) { n3++; p1++; p2++; }
173: }
174: }
175: } while (p1<n1 || p2<n2);
177: if (n3==n1) { /* no new elements to be added */
178: ISRestoreIndices(is1,&i1);
179: ISRestoreIndices(is2,&i2);
180: ISDuplicate(is1,is3);
181: return(0);
182: }
183: PetscMalloc1(n3,&iout);
185: p1 = 0; p2 = 0; n3 = 0;
186: do {
187: if (p1==n1) { /* cleanup for is2 */
188: while (p2<n2) iout[n3++] = i2[p2++];
189: break;
190: } else {
191: while (p2<n2 && i2[p2]<i1[p1]) iout[n3++] = i2[p2++];
192: if (p2==n2) { /* cleanup for is1 */
193: while (p1<n1) iout[n3++] = i1[p1++];
194: break;
195: } else {
196: if (i2[p2]==i1[p1]) { iout[n3++] = i1[p1++]; p2++; }
197: }
198: }
199: if (p2==n2) { /* cleanup for is1 */
200: while (p1<n1) iout[n3++] = i1[p1++];
201: break;
202: } else {
203: while (p1<n1 && i1[p1]<i2[p2]) iout[n3++] = i1[p1++];
204: if (p1==n1) { /* clean up for is2 */
205: while (p2<n2) iout[n3++] = i2[p2++];
206: break;
207: } else {
208: if (i1[p1]==i2[p2]) { iout[n3++] = i1[p1++]; p2++; }
209: }
210: }
211: } while (p1<n1 || p2<n2);
213: ISRestoreIndices(is1,&i1);
214: ISRestoreIndices(is2,&i2);
215: ISCreateGeneral(comm,n3,iout,PETSC_OWN_POINTER,is3);
216: return(0);
217: }
219: /*@
220: ISExpand - Computes the union of two index sets, by concatenating 2 lists and
221: removing duplicates.
223: Collective on IS
225: Input Parameters:
226: + is1 - first index set
227: - is2 - index values to be added
229: Output Parameters:
230: . isout - is1 + is2 The index set is2 is appended to is1 removing duplicates
232: Notes:
233: Negative values are removed from the lists. This requires O(imax-imin)
234: memory and O(imax-imin) work, where imin and imax are the bounds on the
235: indices in is1 and is2.
237: The IS's do not need to be sorted.
239: Level: intermediate
241: .seealso: ISDestroy(), ISView(), ISDifference(), ISSum()
243: @*/
244: PetscErrorCode ISExpand(IS is1,IS is2,IS *isout)
245: {
247: PetscInt i,n1,n2,imin,imax,nout,*iout;
248: const PetscInt *i1,*i2;
249: PetscBT mask;
250: MPI_Comm comm;
257: if (!is1 && !is2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Both arguments cannot be NULL");
258: if (!is1) {ISDuplicate(is2, isout);return(0);}
259: if (!is2) {ISDuplicate(is1, isout);return(0);}
260: ISGetIndices(is1,&i1);
261: ISGetLocalSize(is1,&n1);
262: ISGetIndices(is2,&i2);
263: ISGetLocalSize(is2,&n2);
265: /* Create a bit mask array to contain required values */
266: if (n1 || n2) {
267: imin = PETSC_MAX_INT;
268: imax = 0;
269: for (i=0; i<n1; i++) {
270: if (i1[i] < 0) continue;
271: imin = PetscMin(imin,i1[i]);
272: imax = PetscMax(imax,i1[i]);
273: }
274: for (i=0; i<n2; i++) {
275: if (i2[i] < 0) continue;
276: imin = PetscMin(imin,i2[i]);
277: imax = PetscMax(imax,i2[i]);
278: }
279: } else imin = imax = 0;
281: PetscMalloc1(n1+n2,&iout);
282: nout = 0;
283: PetscBTCreate(imax-imin,&mask);
284: /* Put the values from is1 */
285: for (i=0; i<n1; i++) {
286: if (i1[i] < 0) continue;
287: if (!PetscBTLookupSet(mask,i1[i] - imin)) iout[nout++] = i1[i];
288: }
289: ISRestoreIndices(is1,&i1);
290: /* Put the values from is2 */
291: for (i=0; i<n2; i++) {
292: if (i2[i] < 0) continue;
293: if (!PetscBTLookupSet(mask,i2[i] - imin)) iout[nout++] = i2[i];
294: }
295: ISRestoreIndices(is2,&i2);
297: /* create the new IS containing the sum */
298: PetscObjectGetComm((PetscObject)is1,&comm);
299: ISCreateGeneral(comm,nout,iout,PETSC_OWN_POINTER,isout);
301: PetscBTDestroy(&mask);
302: return(0);
303: }
305: /*@
306: ISIntersect - Computes the intersection of two index sets, by sorting and comparing.
308: Collective on IS
310: Input Parameters:
311: + is1 - first index set
312: - is2 - second index set
314: Output Parameters:
315: . isout - the sorted intersection of is1 and is2
317: Notes:
318: Negative values are removed from the lists. This requires O(min(is1,is2))
319: memory and O(max(is1,is2)log(max(is1,is2))) work
321: The IS's do not need to be sorted.
323: Level: intermediate
325: .seealso: ISDestroy(), ISView(), ISDifference(), ISSum(), ISExpand()
326: @*/
327: PetscErrorCode ISIntersect(IS is1,IS is2,IS *isout)
328: {
330: PetscInt i,n1,n2,nout,*iout;
331: const PetscInt *i1,*i2;
332: IS is1sorted = NULL, is2sorted = NULL;
333: PetscBool sorted, lsorted;
334: MPI_Comm comm;
341: PetscObjectGetComm((PetscObject)is1,&comm);
343: ISGetLocalSize(is1,&n1);
344: ISGetLocalSize(is2,&n2);
345: if (n1 < n2) {
346: IS tempis = is1;
347: PetscInt ntemp = n1;
349: is1 = is2;
350: is2 = tempis;
351: n1 = n2;
352: n2 = ntemp;
353: }
354: ISSorted(is1,&lsorted);
355: MPIU_Allreduce(&lsorted,&sorted,1,MPIU_BOOL,MPI_LAND,comm);
356: if (!sorted) {
357: ISDuplicate(is1,&is1sorted);
358: ISSort(is1sorted);
359: ISGetIndices(is1sorted,&i1);
360: } else {
361: is1sorted = is1;
362: PetscObjectReference((PetscObject)is1);
363: ISGetIndices(is1,&i1);
364: }
365: ISSorted(is2,&lsorted);
366: MPIU_Allreduce(&lsorted,&sorted,1,MPIU_BOOL,MPI_LAND,comm);
367: if (!sorted) {
368: ISDuplicate(is2,&is2sorted);
369: ISSort(is2sorted);
370: ISGetIndices(is2sorted,&i2);
371: } else {
372: is2sorted = is2;
373: PetscObjectReference((PetscObject)is2);
374: ISGetIndices(is2,&i2);
375: }
377: PetscMalloc1(n2,&iout);
379: for (i = 0, nout = 0; i < n2; i++) {
380: PetscInt key = i2[i];
381: PetscInt loc;
383: ISLocate(is1sorted,key,&loc);
384: if (loc >= 0) {
385: if (!nout || iout[nout-1] < key) {
386: iout[nout++] = key;
387: }
388: }
389: }
390: PetscRealloc(nout*sizeof(PetscInt),&iout);
392: /* create the new IS containing the sum */
393: ISCreateGeneral(comm,nout,iout,PETSC_OWN_POINTER,isout);
395: ISRestoreIndices(is2sorted,&i2);
396: ISDestroy(&is2sorted);
397: ISRestoreIndices(is1sorted,&i1);
398: ISDestroy(&is1sorted);
399: return(0);
400: }
402: PetscErrorCode ISIntersect_Caching_Internal(IS is1, IS is2, IS *isect)
403: {
407: *isect = NULL;
408: if (is2 && is1) {
409: char composeStr[33] = {0};
410: PetscObjectId is2id;
412: PetscObjectGetId((PetscObject)is2,&is2id);
413: PetscSNPrintf(composeStr,32,"ISIntersect_Caching_%x",is2id);
414: PetscObjectQuery((PetscObject) is1, composeStr, (PetscObject *) isect);
415: if (*isect == NULL) {
416: ISIntersect(is1, is2, isect);
417: PetscObjectCompose((PetscObject) is1, composeStr, (PetscObject) *isect);
418: } else {
419: PetscObjectReference((PetscObject) *isect);
420: }
421: }
422: return(0);
423: }
425: /*@
426: ISConcatenate - Forms a new IS by locally concatenating the indices from an IS list without reordering.
428: Collective.
430: Input Parameters:
431: + comm - communicator of the concatenated IS.
432: . len - size of islist array (nonnegative)
433: - islist - array of index sets
435: Output Parameters:
436: . isout - The concatenated index set; empty, if len == 0.
438: Notes:
439: The semantics of calling this on comm imply that the comms of the members if islist also contain this rank.
441: Level: intermediate
443: .seealso: ISDifference(), ISSum(), ISExpand()
445: @*/
446: PetscErrorCode ISConcatenate(MPI_Comm comm, PetscInt len, const IS islist[], IS *isout)
447: {
449: PetscInt i,n,N;
450: const PetscInt *iidx;
451: PetscInt *idx;
455: if (PetscDefined(USE_DEBUG)) {
457: }
459: if (!len) {
460: ISCreateStride(comm, 0,0,0, isout);
461: return(0);
462: }
463: if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Negative array length: %D", len);
464: N = 0;
465: for (i = 0; i < len; ++i) {
466: if (islist[i]) {
467: ISGetLocalSize(islist[i], &n);
468: N += n;
469: }
470: }
471: PetscMalloc1(N, &idx);
472: N = 0;
473: for (i = 0; i < len; ++i) {
474: if (islist[i]) {
475: ISGetLocalSize(islist[i], &n);
476: ISGetIndices(islist[i], &iidx);
477: PetscArraycpy(idx+N,iidx, n);
478: ISRestoreIndices(islist[i], &iidx);
479: N += n;
480: }
481: }
482: ISCreateGeneral(comm, N, idx, PETSC_OWN_POINTER, isout);
483: return(0);
484: }
486: /*@
487: ISListToPair - convert an IS list to a pair of ISs of equal length defining an equivalent integer multimap.
488: Each IS on the input list is assigned an integer j so that all of the indices of that IS are
489: mapped to j.
491: Collective.
493: Input arguments:
494: + comm - MPI_Comm
495: . listlen - IS list length
496: - islist - IS list
498: Output arguments:
499: + xis - domain IS
500: - yis - range IS
502: Level: advanced
504: Notes:
505: The global integers assigned to the ISs of the local input list might not correspond to the
506: local numbers of the ISs on that list, but the two *orderings* are the same: the global
507: integers assigned to the ISs on the local list form a strictly increasing sequence.
509: The ISs on the input list can belong to subcommunicators of comm, and the subcommunicators
510: on the input IS list are assumed to be in a "deadlock-free" order.
512: Local lists of PetscObjects (or their subcommes) on a comm are "deadlock-free" if subcomm1
513: preceeds subcomm2 on any local list, then it preceeds subcomm2 on all ranks.
514: Equivalently, the local numbers of the subcomms on each local list are drawn from some global
515: numbering. This is ensured, for example, by ISPairToList().
517: .seealso ISPairToList()
518: @*/
519: PetscErrorCode ISListToPair(MPI_Comm comm, PetscInt listlen, IS islist[], IS *xis, IS *yis)
520: {
522: PetscInt ncolors, *colors,i, leni,len,*xinds, *yinds,k,j;
523: const PetscInt *indsi;
526: PetscMalloc1(listlen, &colors);
527: PetscObjectsListGetGlobalNumbering(comm, listlen, (PetscObject*)islist,&ncolors, colors);
528: len = 0;
529: for (i = 0; i < listlen; ++i) {
530: ISGetLocalSize(islist[i], &leni);
531: len += leni;
532: }
533: PetscMalloc1(len, &xinds);
534: PetscMalloc1(len, &yinds);
535: k = 0;
536: for (i = 0; i < listlen; ++i) {
537: ISGetLocalSize(islist[i], &leni);
538: ISGetIndices(islist[i],&indsi);
539: for (j = 0; j < leni; ++j) {
540: xinds[k] = indsi[j];
541: yinds[k] = colors[i];
542: ++k;
543: }
544: }
545: PetscFree(colors);
546: ISCreateGeneral(comm,len,xinds,PETSC_OWN_POINTER,xis);
547: ISCreateGeneral(comm,len,yinds,PETSC_OWN_POINTER,yis);
548: return(0);
549: }
551: /*@
552: ISPairToList - convert an IS pair encoding an integer map to a list of ISs.
553: Each IS on the output list contains the preimage for each index on the second input IS.
554: The ISs on the output list are constructed on the subcommunicators of the input IS pair.
555: Each subcommunicator corresponds to the preimage of some index j -- this subcomm contains
556: exactly the ranks that assign some indices i to j. This is essentially the inverse of
557: ISListToPair().
559: Collective on indis.
561: Input arguments:
562: + xis - domain IS
563: - yis - range IS
565: Output arguments:
566: + listlen - length of islist
567: - islist - list of ISs breaking up indis by color
569: Note:
570: + xis and yis must be of the same length and have congruent communicators.
571: - The resulting ISs have subcommunicators in a "deadlock-free" order (see ISListToPair()).
573: Level: advanced
575: .seealso ISListToPair()
576: @*/
577: PetscErrorCode ISPairToList(IS xis, IS yis, PetscInt *listlen, IS **islist)
578: {
580: IS indis = xis, coloris = yis;
581: PetscInt *inds, *colors, llen, ilen, lstart, lend, lcount,l;
582: PetscMPIInt rank, size, llow, lhigh, low, high,color,subsize;
583: const PetscInt *ccolors, *cinds;
584: MPI_Comm comm, subcomm;
592: PetscObjectGetComm((PetscObject)xis,&comm);
593: MPI_Comm_rank(comm, &rank);
594: MPI_Comm_rank(comm, &size);
595: /* Extract, copy and sort the local indices and colors on the color. */
596: ISGetLocalSize(coloris, &llen);
597: ISGetLocalSize(indis, &ilen);
598: if (llen != ilen) SETERRQ2(comm, PETSC_ERR_ARG_SIZ, "Incompatible IS sizes: %D and %D", ilen, llen);
599: ISGetIndices(coloris, &ccolors);
600: ISGetIndices(indis, &cinds);
601: PetscMalloc2(ilen,&inds,llen,&colors);
602: PetscArraycpy(inds,cinds,ilen);
603: PetscArraycpy(colors,ccolors,llen);
604: PetscSortIntWithArray(llen, colors, inds);
605: /* Determine the global extent of colors. */
606: llow = 0; lhigh = -1;
607: lstart = 0; lcount = 0;
608: while (lstart < llen) {
609: lend = lstart+1;
610: while (lend < llen && colors[lend] == colors[lstart]) ++lend;
611: llow = PetscMin(llow,colors[lstart]);
612: lhigh = PetscMax(lhigh,colors[lstart]);
613: ++lcount;
614: }
615: MPIU_Allreduce(&llow,&low,1,MPI_INT,MPI_MIN,comm);
616: MPIU_Allreduce(&lhigh,&high,1,MPI_INT,MPI_MAX,comm);
617: *listlen = 0;
618: if (low <= high) {
619: if (lcount > 0) {
620: *listlen = lcount;
621: if (!*islist) {
622: PetscMalloc1(lcount, islist);
623: }
624: }
625: /*
626: Traverse all possible global colors, and participate in the subcommunicators
627: for the locally-supported colors.
628: */
629: lcount = 0;
630: lstart = 0; lend = 0;
631: for (l = low; l <= high; ++l) {
632: /*
633: Find the range of indices with the same color, which is not smaller than l.
634: Observe that, since colors is sorted, and is a subsequence of [low,high],
635: as soon as we find a new color, it is >= l.
636: */
637: if (lstart < llen) {
638: /* The start of the next locally-owned color is identified. Now look for the end. */
639: if (lstart == lend) {
640: lend = lstart+1;
641: while (lend < llen && colors[lend] == colors[lstart]) ++lend;
642: }
643: /* Now check whether the identified color segment matches l. */
644: if (colors[lstart] < l) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Locally owned color %D at location %D is < than the next global color %D", colors[lstart], lcount, l);
645: }
646: color = (PetscMPIInt)(colors[lstart] == l);
647: /* Check whether a proper subcommunicator exists. */
648: MPIU_Allreduce(&color,&subsize,1,MPI_INT,MPI_SUM,comm);
650: if (subsize == 1) subcomm = PETSC_COMM_SELF;
651: else if (subsize == size) subcomm = comm;
652: else {
653: /* a proper communicator is necessary, so we create it. */
654: MPI_Comm_split(comm, color, rank, &subcomm);
655: }
656: if (colors[lstart] == l) {
657: /* If we have l among the local colors, we create an IS to hold the corresponding indices. */
658: ISCreateGeneral(subcomm, lend-lstart,inds+lstart,PETSC_COPY_VALUES,*islist+lcount);
659: /* Position lstart at the beginning of the next local color. */
660: lstart = lend;
661: /* Increment the counter of the local colors split off into an IS. */
662: ++lcount;
663: }
664: if (subsize > 0 && subsize < size) {
665: /*
666: Irrespective of color, destroy the split off subcomm:
667: a subcomm used in the IS creation above is duplicated
668: into a proper PETSc comm.
669: */
670: MPI_Comm_free(&subcomm);
671: }
672: } /* for (l = low; l < high; ++l) */
673: } /* if (low <= high) */
674: PetscFree2(inds,colors);
675: return(0);
676: }
678: /*@
679: ISEmbed - embed IS a into IS b by finding the locations in b that have the same indices as in a.
680: If c is the IS of these locations, we have a = b*c, regarded as a composition of the
681: corresponding ISLocalToGlobalMaps.
683: Not collective.
685: Input arguments:
686: + a - IS to embed
687: . b - IS to embed into
688: - drop - flag indicating whether to drop a's indices that are not in b.
690: Output arguments:
691: . c - local embedding indices
693: Note:
694: If some of a's global indices are not among b's indices the embedding is impossible. The local indices of a
695: corresponding to these global indices are either mapped to -1 (if !drop) or are omitted (if drop). In the former
696: case the size of c is that same as that of a, in the latter case c's size may be smaller.
698: The resulting IS is sequential, since the index substition it encodes is purely local.
700: Level: advanced
702: .seealso ISLocalToGlobalMapping
703: @*/
704: PetscErrorCode ISEmbed(IS a, IS b, PetscBool drop, IS *c)
705: {
706: PetscErrorCode ierr;
707: ISLocalToGlobalMapping ltog;
708: ISGlobalToLocalMappingMode gtoltype = IS_GTOLM_DROP;
709: PetscInt alen, clen, *cindices, *cindices2;
710: const PetscInt *aindices;
716: ISLocalToGlobalMappingCreateIS(b, <og);
717: ISGetLocalSize(a, &alen);
718: ISGetIndices(a, &aindices);
719: PetscMalloc1(alen, &cindices);
720: if (!drop) gtoltype = IS_GTOLM_MASK;
721: ISGlobalToLocalMappingApply(ltog,gtoltype,alen,aindices,&clen,cindices);
722: ISLocalToGlobalMappingDestroy(<og);
723: if (clen != alen) {
724: cindices2 = cindices;
725: PetscMalloc1(clen, &cindices);
726: PetscArraycpy(cindices,cindices2,clen);
727: PetscFree(cindices2);
728: }
729: ISCreateGeneral(PETSC_COMM_SELF,clen,cindices,PETSC_OWN_POINTER,c);
730: return(0);
731: }
733: /*@
734: ISSortPermutation - calculate the permutation of the indices into a nondecreasing order.
736: Not collective.
738: Input arguments:
739: + f - IS to sort
740: - always - build the permutation even when f's indices are nondecreasing.
742: Output argument:
743: . h - permutation or NULL, if f is nondecreasing and always == PETSC_FALSE.
745: Note: Indices in f are unchanged. f[h[i]] is the i-th smallest f index.
746: If always == PETSC_FALSE, an extra check is peformed to see whether
747: the f indices are nondecreasing. h is built on PETSC_COMM_SELF, since
748: the permutation has a local meaning only.
750: Level: advanced
752: .seealso ISLocalToGlobalMapping, ISSort()
753: @*/
754: PetscErrorCode ISSortPermutation(IS f,PetscBool always,IS *h)
755: {
756: PetscErrorCode ierr;
757: const PetscInt *findices;
758: PetscInt fsize,*hindices,i;
759: PetscBool isincreasing;
764: ISGetLocalSize(f,&fsize);
765: ISGetIndices(f,&findices);
766: *h = NULL;
767: if (!always) {
768: isincreasing = PETSC_TRUE;
769: for (i = 1; i < fsize; ++i) {
770: if (findices[i] <= findices[i-1]) {
771: isincreasing = PETSC_FALSE;
772: break;
773: }
774: }
775: if (isincreasing) {
776: ISRestoreIndices(f,&findices);
777: return(0);
778: }
779: }
780: PetscMalloc1(fsize,&hindices);
781: for (i = 0; i < fsize; ++i) hindices[i] = i;
782: PetscSortIntWithPermutation(fsize,findices,hindices);
783: ISRestoreIndices(f,&findices);
784: ISCreateGeneral(PETSC_COMM_SELF,fsize,hindices,PETSC_OWN_POINTER,h);
785: ISSetInfo(*h,IS_PERMUTATION,IS_LOCAL,PETSC_FALSE,PETSC_TRUE);
786: return(0);
787: }