Actual source code: isdiff.c

petsc-master 2019-04-18
Report Typos and Errors

  2:  #include <petsc/private/isimpl.h>
  3:  #include <petscbt.h>

  5: /*@
  6:    ISDifference - Computes the difference between two index sets.

  8:    Collective on IS

 10:    Input Parameter:
 11: +  is1 - first index, to have items removed from it
 12: -  is2 - index values to be removed

 14:    Output Parameters:
 15: .  isout - is1 - is2

 17:    Notes:
 18:    Negative values are removed from the lists. is2 may have values
 19:    that are not in is1. This requires O(imax-imin) memory and O(imax-imin)
 20:    work, where imin and imax are the bounds on the indices in is1.

 22:    Level: intermediate

 24:    Concepts: index sets^difference
 25:    Concepts: IS^difference

 27: .seealso: ISDestroy(), ISView(), ISSum(), ISExpand()

 29: @*/
 30: PetscErrorCode  ISDifference(IS is1,IS is2,IS *isout)
 31: {
 33:   PetscInt       i,n1,n2,imin,imax,nout,*iout;
 34:   const PetscInt *i1,*i2;
 35:   PetscBT        mask;
 36:   MPI_Comm       comm;


 43:   ISGetIndices(is1,&i1);
 44:   ISGetLocalSize(is1,&n1);

 46:   /* Create a bit mask array to contain required values */
 47:   if (n1) {
 48:     imin = PETSC_MAX_INT;
 49:     imax = 0;
 50:     for (i=0; i<n1; i++) {
 51:       if (i1[i] < 0) continue;
 52:       imin = PetscMin(imin,i1[i]);
 53:       imax = PetscMax(imax,i1[i]);
 54:     }
 55:   } else imin = imax = 0;

 57:   PetscBTCreate(imax-imin,&mask);
 58:   /* Put the values from is1 */
 59:   for (i=0; i<n1; i++) {
 60:     if (i1[i] < 0) continue;
 61:     PetscBTSet(mask,i1[i] - imin);
 62:   }
 63:   ISRestoreIndices(is1,&i1);
 64:   /* Remove the values from is2 */
 65:   ISGetIndices(is2,&i2);
 66:   ISGetLocalSize(is2,&n2);
 67:   for (i=0; i<n2; i++) {
 68:     if (i2[i] < imin || i2[i] > imax) continue;
 69:     PetscBTClear(mask,i2[i] - imin);
 70:   }
 71:   ISRestoreIndices(is2,&i2);

 73:   /* Count the number in the difference */
 74:   nout = 0;
 75:   for (i=0; i<imax-imin+1; i++) {
 76:     if (PetscBTLookup(mask,i)) nout++;
 77:   }

 79:   /* create the new IS containing the difference */
 80:   PetscMalloc1(nout,&iout);
 81:   nout = 0;
 82:   for (i=0; i<imax-imin+1; i++) {
 83:     if (PetscBTLookup(mask,i)) iout[nout++] = i + imin;
 84:   }
 85:   PetscObjectGetComm((PetscObject)is1,&comm);
 86:   ISCreateGeneral(comm,nout,iout,PETSC_OWN_POINTER,isout);

 88:   PetscBTDestroy(&mask);
 89:   return(0);
 90: }

 92: /*@
 93:    ISSum - Computes the sum (union) of two index sets.

 95:    Only sequential version (at the moment)

 97:    Input Parameter:
 98: +  is1 - index set to be extended
 99: -  is2 - index values to be added

101:    Output Parameter:
102: .   is3 - the sum; this can not be is1 or is2

104:    Notes:
105:    If n1 and n2 are the sizes of the sets, this takes O(n1+n2) time;

107:    Both index sets need to be sorted on input.

109:    Level: intermediate

111: .seealso: ISDestroy(), ISView(), ISDifference(), ISExpand()

113:    Concepts: index sets^union
114:    Concepts: IS^union

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 Parameter:
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:    Concepts: index sets^difference
244:    Concepts: IS^difference

246: @*/
247: PetscErrorCode ISExpand(IS is1,IS is2,IS *isout)
248: {
250:   PetscInt       i,n1,n2,imin,imax,nout,*iout;
251:   const PetscInt *i1,*i2;
252:   PetscBT        mask;
253:   MPI_Comm       comm;


260:   if (!is1 && !is2) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Both arguments cannot be NULL");
261:   if (!is1) {ISDuplicate(is2, isout);return(0);}
262:   if (!is2) {ISDuplicate(is1, isout);return(0);}
263:   ISGetIndices(is1,&i1);
264:   ISGetLocalSize(is1,&n1);
265:   ISGetIndices(is2,&i2);
266:   ISGetLocalSize(is2,&n2);

268:   /* Create a bit mask array to contain required values */
269:   if (n1 || n2) {
270:     imin = PETSC_MAX_INT;
271:     imax = 0;
272:     for (i=0; i<n1; i++) {
273:       if (i1[i] < 0) continue;
274:       imin = PetscMin(imin,i1[i]);
275:       imax = PetscMax(imax,i1[i]);
276:     }
277:     for (i=0; i<n2; i++) {
278:       if (i2[i] < 0) continue;
279:       imin = PetscMin(imin,i2[i]);
280:       imax = PetscMax(imax,i2[i]);
281:     }
282:   } else imin = imax = 0;

284:   PetscMalloc1(n1+n2,&iout);
285:   nout = 0;
286:   PetscBTCreate(imax-imin,&mask);
287:   /* Put the values from is1 */
288:   for (i=0; i<n1; i++) {
289:     if (i1[i] < 0) continue;
290:     if (!PetscBTLookupSet(mask,i1[i] - imin)) iout[nout++] = i1[i];
291:   }
292:   ISRestoreIndices(is1,&i1);
293:   /* Put the values from is2 */
294:   for (i=0; i<n2; i++) {
295:     if (i2[i] < 0) continue;
296:     if (!PetscBTLookupSet(mask,i2[i] - imin)) iout[nout++] = i2[i];
297:   }
298:   ISRestoreIndices(is2,&i2);

300:   /* create the new IS containing the sum */
301:   PetscObjectGetComm((PetscObject)is1,&comm);
302:   ISCreateGeneral(comm,nout,iout,PETSC_OWN_POINTER,isout);

304:   PetscBTDestroy(&mask);
305:   return(0);
306: }

308: /*@
309:    ISIntersect - Computes the intersection of two index sets, by sorting and comparing.

311:    Collective on IS

313:    Input Parameter:
314: +  is1 - first index set
315: -  is2 - second index set

317:    Output Parameters:
318: .  isout - the sorted intersection of is1 and is2

320:    Notes:
321:    Negative values are removed from the lists. This requires O(min(is1,is2))
322:    memory and O(max(is1,is2)log(max(is1,is2))) work

324:    The IS's do not need to be sorted.

326:    Level: intermediate

328: .seealso: ISDestroy(), ISView(), ISDifference(), ISSum(), ISExpand()

330:    Concepts: index sets^intersection
331:    Concepts: IS^intersection

333: @*/
334: PetscErrorCode ISIntersect(IS is1,IS is2,IS *isout)
335: {
337:   PetscInt       i,n1,n2,nout,*iout;
338:   const PetscInt *i1,*i2;
339:   IS             is1sorted = NULL, is2sorted = NULL;
340:   PetscBool      sorted;
341:   MPI_Comm       comm;


348:   ISGetLocalSize(is1,&n1);
349:   ISGetLocalSize(is2,&n2);
350:   if (n1 < n2) {
351:     IS tempis = is1;
352:     int ntemp = n1;

354:     is1 = is2;
355:     is2 = tempis;
356:     n1  = n2;
357:     n2  = ntemp;
358:   }
359:   ISSorted(is1,&sorted);
360:   if (!sorted) {
361:     ISDuplicate(is1,&is1sorted);
362:     ISSort(is1sorted);
363:     ISGetIndices(is1sorted,&i1);
364:   } else {
365:     is1sorted = is1;
366:     PetscObjectReference((PetscObject)is1);
367:     ISGetIndices(is1,&i1);
368:   }
369:   ISSorted(is2,&sorted);
370:   if (!sorted) {
371:     ISDuplicate(is2,&is2sorted);
372:     ISSort(is2sorted);
373:     ISGetIndices(is2sorted,&i2);
374:   } else {
375:     is2sorted = is2;
376:     PetscObjectReference((PetscObject)is2);
377:     ISGetIndices(is2,&i2);
378:   }

380:   PetscMalloc1(n2,&iout);

382:   for (i = 0, nout = 0; i < n2; i++) {
383:     PetscInt key = i2[i];
384:     PetscInt loc;

386:     ISLocate(is1sorted,key,&loc);
387:     if (loc >= 0) {
388:       if (!nout || iout[nout-1] < key) {
389:         iout[nout++] = key;
390:       }
391:     }
392:   }
393:   PetscRealloc(sizeof (PetscInt) * (size_t) nout,&iout);

395:   /* create the new IS containing the sum */
396:   PetscObjectGetComm((PetscObject)is1,&comm);
397:   ISCreateGeneral(comm,nout,iout,PETSC_OWN_POINTER,isout);

399:   ISRestoreIndices(is2sorted,&i2);
400:   ISDestroy(&is2sorted);
401:   ISRestoreIndices(is1sorted,&i1);
402:   ISDestroy(&is1sorted);
403:   return(0);
404: }

406: PetscErrorCode ISIntersect_Caching_Internal(IS is1, IS is2, IS *isect)
407: {

411:   *isect = NULL;
412:   if (is2 && is1) {
413:     char           composeStr[33] = {0};
414:     PetscObjectId  is2id;

416:     PetscObjectGetId((PetscObject)is2,&is2id);
417:     PetscSNPrintf(composeStr,32,"ISIntersect_Caching_%x",is2id);
418:     PetscObjectQuery((PetscObject) is1, composeStr, (PetscObject *) isect);
419:     if (*isect == NULL) {
420:       ISIntersect(is1, is2, isect);
421:       PetscObjectCompose((PetscObject) is1, composeStr, (PetscObject) *isect);
422:     } else {
423:       PetscObjectReference((PetscObject) *isect);
424:     }
425:   }
426:   return(0);
427: }

429: /*@
430:    ISConcatenate - Forms a new IS by locally concatenating the indices from an IS list without reordering.


433:    Collective on comm.

435:    Input Parameter:
436: +  comm    - communicator of the concatenated IS.
437: .  len     - size of islist array (nonnegative)
438: -  islist  - array of index sets

440:    Output Parameters:
441: .  isout   - The concatenated index set; empty, if len == 0.

443:    Notes:
444:     The semantics of calling this on comm imply that the comms of the members if islist also contain this rank.

446:    Level: intermediate

448: .seealso: ISDifference(), ISSum(), ISExpand()

450:    Concepts: index sets^concatenation
451:    Concepts: IS^concatenation

453: @*/
454: PetscErrorCode ISConcatenate(MPI_Comm comm, PetscInt len, const IS islist[], IS *isout)
455: {
457:   PetscInt i,n,N;
458:   const PetscInt *iidx;
459:   PetscInt *idx;

463: #if defined(PETSC_USE_DEBUG)
465: #endif
467:   if (!len) {
468:     ISCreateStride(comm, 0,0,0, isout);
469:     return(0);
470:   }
471:   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Negative array length: %D", len);
472:   N = 0;
473:   for (i = 0; i < len; ++i) {
474:     if (islist[i]) {
475:       ISGetLocalSize(islist[i], &n);
476:       N   += n;
477:     }
478:   }
479:   PetscMalloc1(N, &idx);
480:   N = 0;
481:   for (i = 0; i < len; ++i) {
482:     if (islist[i]) {
483:       ISGetLocalSize(islist[i], &n);
484:       ISGetIndices(islist[i], &iidx);
485:       PetscMemcpy(idx+N,iidx, sizeof(PetscInt)*n);
486:       ISRestoreIndices(islist[i], &iidx);
487:       N   += n;
488:     }
489:   }
490:   ISCreateGeneral(comm, N, idx, PETSC_OWN_POINTER, isout);
491:   return(0);
492: }

494: /*@
495:    ISListToPair     -    convert an IS list to a pair of ISs of equal length defining an equivalent integer multimap.
496:                         Each IS on the input list is assigned an integer j so that all of the indices of that IS are
497:                         mapped to j.


500:   Collective on comm.

502:   Input arguments:
503: + comm    -  MPI_Comm
504: . listlen -  IS list length
505: - islist  -  IS list

507:   Output arguments:
508: + xis -  domain IS
509: - yis -  range  IS

511:   Level: advanced

513:   Notes:
514:   The global integers assigned to the ISs of the local input list might not correspond to the
515:   local numbers of the ISs on that list, but the two *orderings* are the same: the global
516:   integers assigned to the ISs on the local list form a strictly increasing sequence.

518:   The ISs on the input list can belong to subcommunicators of comm, and the subcommunicators
519:   on the input IS list are assumed to be in a "deadlock-free" order.

521:   Local lists of PetscObjects (or their subcommes) on a comm are "deadlock-free" if subcomm1
522:   preceeds subcomm2 on any local list, then it preceeds subcomm2 on all ranks.
523:   Equivalently, the local numbers of the subcomms on each local list are drawn from some global
524:   numbering. This is ensured, for example, by ISPairToList().

526: .seealso ISPairToList()
527: @*/
528: PetscErrorCode ISListToPair(MPI_Comm comm, PetscInt listlen, IS islist[], IS *xis, IS *yis)
529: {
531:   PetscInt       ncolors, *colors,i, leni,len,*xinds, *yinds,k,j;
532:   const PetscInt *indsi;

535:   PetscMalloc1(listlen, &colors);
536:   PetscObjectsListGetGlobalNumbering(comm, listlen, (PetscObject*)islist,&ncolors, colors);
537:   len  = 0;
538:   for (i = 0; i < listlen; ++i) {
539:     ISGetLocalSize(islist[i], &leni);
540:     len += leni;
541:   }
542:   PetscMalloc1(len, &xinds);
543:   PetscMalloc1(len, &yinds);
544:   k    = 0;
545:   for (i = 0; i < listlen; ++i) {
546:     ISGetLocalSize(islist[i], &leni);
547:     ISGetIndices(islist[i],&indsi);
548:     for (j = 0; j < leni; ++j) {
549:       xinds[k] = indsi[j];
550:       yinds[k] = colors[i];
551:       ++k;
552:     }
553:   }
554:   PetscFree(colors);
555:   ISCreateGeneral(comm,len,xinds,PETSC_OWN_POINTER,xis);
556:   ISCreateGeneral(comm,len,yinds,PETSC_OWN_POINTER,yis);
557:   return(0);
558: }


561: /*@
562:    ISPairToList   -   convert an IS pair encoding an integer map to a list of ISs.
563:                      Each IS on the output list contains the preimage for each index on the second input IS.
564:                      The ISs on the output list are constructed on the subcommunicators of the input IS pair.
565:                      Each subcommunicator corresponds to the preimage of some index j -- this subcomm contains
566:                      exactly the ranks that assign some indices i to j.  This is essentially the inverse of
567:                      ISListToPair().

569:   Collective on indis.

571:   Input arguments:
572: + xis -  domain IS
573: - yis -  range IS

575:   Output arguments:
576: + listlen -  length of islist
577: - islist  -  list of ISs breaking up indis by color

579:   Note:
580: + xis and yis must be of the same length and have congruent communicators.
581: - The resulting ISs have subcommunicators in a "deadlock-free" order (see ISListToPair()).

583:   Level: advanced

585: .seealso ISListToPair()
586:  @*/
587: PetscErrorCode ISPairToList(IS xis, IS yis, PetscInt *listlen, IS **islist)
588: {
590:   IS             indis = xis, coloris = yis;
591:   PetscInt       *inds, *colors, llen, ilen, lstart, lend, lcount,l;
592:   PetscMPIInt    rank, size, llow, lhigh, low, high,color,subsize;
593:   const PetscInt *ccolors, *cinds;
594:   MPI_Comm       comm, subcomm;

602:   PetscObjectGetComm((PetscObject)xis,&comm);
603:   MPI_Comm_rank(comm, &rank);
604:   MPI_Comm_rank(comm, &size);
605:   /* Extract, copy and sort the local indices and colors on the color. */
606:   ISGetLocalSize(coloris, &llen);
607:   ISGetLocalSize(indis,   &ilen);
608:   if (llen != ilen) SETERRQ2(comm, PETSC_ERR_ARG_SIZ, "Incompatible IS sizes: %D and %D", ilen, llen);
609:   ISGetIndices(coloris, &ccolors);
610:   ISGetIndices(indis, &cinds);
611:   PetscMalloc2(ilen,&inds,llen,&colors);
612:   PetscMemcpy(inds,cinds,ilen*sizeof(PetscInt));
613:   PetscMemcpy(colors,ccolors,llen*sizeof(PetscInt));
614:   PetscSortIntWithArray(llen, colors, inds);
615:   /* Determine the global extent of colors. */
616:   llow   = 0; lhigh  = -1;
617:   lstart = 0; lcount = 0;
618:   while (lstart < llen) {
619:     lend = lstart+1;
620:     while (lend < llen && colors[lend] == colors[lstart]) ++lend;
621:     llow  = PetscMin(llow,colors[lstart]);
622:     lhigh = PetscMax(lhigh,colors[lstart]);
623:     ++lcount;
624:   }
625:   MPIU_Allreduce(&llow,&low,1,MPI_INT,MPI_MIN,comm);
626:   MPIU_Allreduce(&lhigh,&high,1,MPI_INT,MPI_MAX,comm);
627:   *listlen = 0;
628:   if (low <= high) {
629:     if (lcount > 0) {
630:       *listlen = lcount;
631:       if (!*islist) {
632:         PetscMalloc1(lcount, islist);
633:       }
634:     }
635:     /*
636:      Traverse all possible global colors, and participate in the subcommunicators
637:      for the locally-supported colors.
638:      */
639:     lcount = 0;
640:     lstart = 0; lend = 0;
641:     for (l = low; l <= high; ++l) {
642:       /*
643:        Find the range of indices with the same color, which is not smaller than l.
644:        Observe that, since colors is sorted, and is a subsequence of [low,high],
645:        as soon as we find a new color, it is >= l.
646:        */
647:       if (lstart < llen) {
648:         /* The start of the next locally-owned color is identified.  Now look for the end. */
649:         if (lstart == lend) {
650:           lend = lstart+1;
651:           while (lend < llen && colors[lend] == colors[lstart]) ++lend;
652:         }
653:         /* Now check whether the identified color segment matches l. */
654:         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);
655:       }
656:       color = (PetscMPIInt)(colors[lstart] == l);
657:       /* Check whether a proper subcommunicator exists. */
658:       MPIU_Allreduce(&color,&subsize,1,MPI_INT,MPI_SUM,comm);

660:       if (subsize == 1) subcomm = PETSC_COMM_SELF;
661:       else if (subsize == size) subcomm = comm;
662:       else {
663:         /* a proper communicator is necessary, so we create it. */
664:         MPI_Comm_split(comm, color, rank, &subcomm);
665:       }
666:       if (colors[lstart] == l) {
667:         /* If we have l among the local colors, we create an IS to hold the corresponding indices. */
668:         ISCreateGeneral(subcomm, lend-lstart,inds+lstart,PETSC_COPY_VALUES,*islist+lcount);
669:         /* Position lstart at the beginning of the next local color. */
670:         lstart = lend;
671:         /* Increment the counter of the local colors split off into an IS. */
672:         ++lcount;
673:       }
674:       if (subsize > 0 && subsize < size) {
675:         /*
676:          Irrespective of color, destroy the split off subcomm:
677:          a subcomm used in the IS creation above is duplicated
678:          into a proper PETSc comm.
679:          */
680:         MPI_Comm_free(&subcomm);
681:       }
682:     } /* for (l = low; l < high; ++l) */
683:   } /* if (low <= high) */
684:   PetscFree2(inds,colors);
685:   return(0);
686: }


689: /*@
690:    ISEmbed   -   embed IS a into IS b by finding the locations in b that have the same indices as in a.
691:                  If c is the IS of these locations, we have a = b*c, regarded as a composition of the
692:                  corresponding ISLocalToGlobalMaps.

694:   Not collective.

696:   Input arguments:
697: + a    -  IS to embed
698: . b    -  IS to embed into
699: - drop -  flag indicating whether to drop a's indices that are not in b.

701:   Output arguments:
702: . c    -  local embedding indices

704:   Note:
705:   If some of a's global indices are not among b's indices the embedding is impossible.  The local indices of a
706:   corresponding to these global indices are either mapped to -1 (if !drop) or are omitted (if drop).  In the former
707:   case the size of c is that same as that of a, in the latter case c's size may be smaller.

709:   The resulting IS is sequential, since the index substition it encodes is purely local.

711:   Level: advanced

713: .seealso ISLocalToGlobalMapping
714:  @*/
715: PetscErrorCode ISEmbed(IS a, IS b, PetscBool drop, IS *c)
716: {
717:   PetscErrorCode             ierr;
718:   ISLocalToGlobalMapping     ltog;
719:   ISGlobalToLocalMappingMode gtoltype = IS_GTOLM_DROP;
720:   PetscInt                   alen, clen, *cindices, *cindices2;
721:   const PetscInt             *aindices;

727:   ISLocalToGlobalMappingCreateIS(b, &ltog);
728:   ISGetLocalSize(a, &alen);
729:   ISGetIndices(a, &aindices);
730:   PetscMalloc1(alen, &cindices);
731:   if (!drop) gtoltype = IS_GTOLM_MASK;
732:   ISGlobalToLocalMappingApply(ltog,gtoltype,alen,aindices,&clen,cindices);
733:   ISLocalToGlobalMappingDestroy(&ltog);
734:   if (clen != alen) {
735:     cindices2 = cindices;
736:     PetscMalloc1(clen, &cindices);
737:     PetscMemcpy(cindices,cindices2,clen*sizeof(PetscInt));
738:     PetscFree(cindices2);
739:   }
740:   ISCreateGeneral(PETSC_COMM_SELF,clen,cindices,PETSC_OWN_POINTER,c);
741:   return(0);
742: }


745: /*@
746:   ISSortPermutation  -  calculate the permutation of the indices into a nondecreasing order.

748:   Not collective.

750:   Input arguments:
751: + f      -  IS to sort
752: - always -  build the permutation even when f's indices are nondecreasin.

754:   Output argument:
755: . h    -  permutation or NULL, if f is nondecreasing and always == PETSC_TRUE.


758:   Note: Indices in f are unchanged. f[h[i]] is the i-th smallest f index.
759:         If always == PETSC_FALSE, an extra check is peformed to see whether
760:         the f indices are nondecreasing. h is built on PETSC_COMM_SELF, since
761:         the permutation has a local meaning only.

763:   Level: advanced

765: .seealso ISLocalToGlobalMapping, ISSort(), PetscIntSortWithPermutation()
766:  @*/
767: PetscErrorCode ISSortPermutation(IS f,PetscBool always,IS *h)
768: {
769:   PetscErrorCode  ierr;
770:   const PetscInt  *findices;
771:   PetscInt        fsize,*hindices,i;
772:   PetscBool       isincreasing;

777:   ISGetLocalSize(f,&fsize);
778:   ISGetIndices(f,&findices);
779:   *h = NULL;
780:   if (!always) {
781:     isincreasing = PETSC_TRUE;
782:     for (i = 1; i < fsize; ++i) {
783:       if (findices[i] <= findices[i-1]) {
784:         isincreasing = PETSC_FALSE;
785:         break;
786:       }
787:     }
788:     if (isincreasing) {
789:       ISRestoreIndices(f,&findices);
790:       return(0);
791:     }
792:   }
793:   PetscMalloc1(fsize,&hindices);
794:   for (i = 0; i < fsize; ++i) hindices[i] = i;
795:   PetscSortIntWithPermutation(fsize,findices,hindices);
796:   ISRestoreIndices(f,&findices);
797:   ISCreateGeneral(PETSC_COMM_SELF,fsize,hindices,PETSC_OWN_POINTER,h);
798:   return(0);
799: }