Actual source code: index.c

petsc-master 2018-04-25
Report Typos and Errors

  2: /*
  3:    Defines the abstract operations on index sets, i.e. the public interface.
  4: */
  5:  #include <petsc/private/isimpl.h>
  6:  #include <petscviewer.h>
  7:  #include <petscsf.h>

  9: /* Logging support */
 10: PetscClassId IS_CLASSID;

 12: /*@
 13:    ISRenumber - Renumbers an index set (with multiplicities) in a contiguous way.

 15:    Collective on IS

 17:    Input Parmeters:
 18: .  subset - the index set
 19: .  subset_mult - the multiplcity of each entry in subset (optional, can be NULL)

 21:    Output Parameters:
 22: .  N - the maximum entry of the new IS
 23: .  subset_n - the new IS

 25:    Level: intermediate

 27: .seealso:
 28: @*/
 29: PetscErrorCode ISRenumber(IS subset, IS subset_mult, PetscInt *N, IS *subset_n)
 30: {
 31:   PetscSF        sf;
 32:   PetscLayout    map;
 33:   const PetscInt *idxs;
 34:   PetscInt       *leaf_data,*root_data,*gidxs;
 35:   PetscInt       N_n,n,i,lbounds[2],gbounds[2],Nl;
 36:   PetscInt       n_n,nlocals,start,first_index;
 37:   PetscMPIInt    commsize;
 38:   PetscBool      first_found;

 42:   ISGetLocalSize(subset,&n);
 43:   if (subset_mult) {
 45:     ISGetLocalSize(subset,&i);
 46:     if (i != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Local subset and multiplicity sizes don't match! %d != %d",n,i);
 47:   }
 48:   /* create workspace layout for computing global indices of subset */
 49:   ISGetIndices(subset,&idxs);
 50:   lbounds[0] = lbounds[1] = 0;
 51:   for (i=0;i<n;i++) {
 52:     if (idxs[i] < lbounds[0]) lbounds[0] = idxs[i];
 53:     else if (idxs[i] > lbounds[1]) lbounds[1] = idxs[i];
 54:   }
 55:   lbounds[0] = -lbounds[0];
 56:   MPIU_Allreduce(lbounds,gbounds,2,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)subset));
 57:   gbounds[0] = -gbounds[0];
 58:   N_n= gbounds[1] - gbounds[0] + 1;
 59:   PetscLayoutCreate(PetscObjectComm((PetscObject)subset),&map);
 60:   PetscLayoutSetBlockSize(map,1);
 61:   PetscLayoutSetSize(map,N_n);
 62:   PetscLayoutSetUp(map);
 63:   PetscLayoutGetLocalSize(map,&Nl);

 65:   /* create sf : leaf_data == multiplicity of indexes, root data == global index in layout */
 66:   PetscMalloc2(n,&leaf_data,Nl,&root_data);
 67:   if (subset_mult) {
 68:     const PetscInt* idxs_mult;

 70:     ISGetIndices(subset_mult,&idxs_mult);
 71:     PetscMemcpy(leaf_data,idxs_mult,n*sizeof(PetscInt));
 72:     ISRestoreIndices(subset_mult,&idxs_mult);
 73:   } else {
 74:     for (i=0;i<n;i++) leaf_data[i] = 1;
 75:   }
 76:   /* local size of new subset */
 77:   n_n = 0;
 78:   for (i=0;i<n;i++) n_n += leaf_data[i];

 80:   /* global indexes in layout */
 81:   PetscMalloc1(n_n,&gidxs); /* allocating possibly extra space in gidxs which will be used later */
 82:   for (i=0;i<n;i++) gidxs[i] = idxs[i] - gbounds[0];
 83:   ISRestoreIndices(subset,&idxs);
 84:   PetscSFCreate(PetscObjectComm((PetscObject)subset),&sf);
 85:   PetscSFSetGraphLayout(sf,map,n,NULL,PETSC_COPY_VALUES,gidxs);
 86:   PetscLayoutDestroy(&map);

 88:   /* reduce from leaves to roots */
 89:   PetscMemzero(root_data,Nl*sizeof(PetscInt));
 90:   PetscSFReduceBegin(sf,MPIU_INT,leaf_data,root_data,MPI_MAX);
 91:   PetscSFReduceEnd(sf,MPIU_INT,leaf_data,root_data,MPI_MAX);

 93:   /* count indexes in local part of layout */
 94:   nlocals = 0;
 95:   first_index = -1;
 96:   first_found = PETSC_FALSE;
 97:   for (i=0;i<Nl;i++) {
 98:     if (!first_found && root_data[i]) {
 99:       first_found = PETSC_TRUE;
100:       first_index = i;
101:     }
102:     nlocals += root_data[i];
103:   }

105:   /* cumulative of number of indexes and size of subset without holes */
106: #if defined(PETSC_HAVE_MPI_EXSCAN)
107:   start = 0;
108:   MPI_Exscan(&nlocals,&start,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)subset));
109: #else
110:   MPI_Scan(&nlocals,&start,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)subset));
111:   start = start-nlocals;
112: #endif

114:   if (N) { /* compute total size of new subset if requested */
115:     *N = start + nlocals;
116:     MPI_Comm_size(PetscObjectComm((PetscObject)subset),&commsize);
117:     MPI_Bcast(N,1,MPIU_INT,commsize-1,PetscObjectComm((PetscObject)subset));
118:   }

120:   /* adapt root data with cumulative */
121:   if (first_found) {
122:     PetscInt old_index;

124:     root_data[first_index] += start;
125:     old_index = first_index;
126:     for (i=first_index+1;i<Nl;i++) {
127:       if (root_data[i]) {
128:         root_data[i] += root_data[old_index];
129:         old_index = i;
130:       }
131:     }
132:   }

134:   /* from roots to leaves */
135:   PetscSFBcastBegin(sf,MPIU_INT,root_data,leaf_data);
136:   PetscSFBcastEnd(sf,MPIU_INT,root_data,leaf_data);
137:   PetscSFDestroy(&sf);

139:   /* create new IS with global indexes without holes */
140:   if (subset_mult) {
141:     const PetscInt* idxs_mult;
142:     PetscInt        cum;

144:     cum = 0;
145:     ISGetIndices(subset_mult,&idxs_mult);
146:     for (i=0;i<n;i++) {
147:       PetscInt j;
148:       for (j=0;j<idxs_mult[i];j++) gidxs[cum++] = leaf_data[i] - idxs_mult[i] + j;
149:     }
150:     ISRestoreIndices(subset_mult,&idxs_mult);
151:   } else {
152:     for (i=0;i<n;i++) {
153:       gidxs[i] = leaf_data[i]-1;
154:     }
155:   }
156:   ISCreateGeneral(PetscObjectComm((PetscObject)subset),n_n,gidxs,PETSC_OWN_POINTER,subset_n);
157:   PetscFree2(leaf_data,root_data);
158:   return(0);
159: }

161: /*@
162:    ISIdentity - Determines whether index set is the identity mapping.

164:    Collective on IS

166:    Input Parmeters:
167: .  is - the index set

169:    Output Parameters:
170: .  ident - PETSC_TRUE if an identity, else PETSC_FALSE

172:    Level: intermediate

174:    Concepts: identity mapping
175:    Concepts: index sets^is identity

177: .seealso: ISSetIdentity()
178: @*/
179: PetscErrorCode  ISIdentity(IS is,PetscBool  *ident)
180: {

186:   *ident = is->isidentity;
187:   if (*ident) return(0);
188:   if (is->ops->identity) {
189:     (*is->ops->identity)(is,ident);
190:   }
191:   return(0);
192: }

194: /*@
195:    ISSetIdentity - Informs the index set that it is an identity.

197:    Logically Collective on IS

199:    Input Parmeters:
200: .  is - the index set

202:    Level: intermediate

204:    Concepts: identity mapping
205:    Concepts: index sets^is identity

207: .seealso: ISIdentity()
208: @*/
209: PetscErrorCode  ISSetIdentity(IS is)
210: {

215:   is->isidentity = PETSC_TRUE;
216:   ISSetPermutation(is);
217:   return(0);
218: }

220: /*@
221:    ISContiguousLocal - Locates an index set with contiguous range within a global range, if possible

223:    Not Collective

225:    Input Parmeters:
226: +  is - the index set
227: .  gstart - global start
228: .  gend - global end

230:    Output Parameters:
231: +  start - start of contiguous block, as an offset from gstart
232: -  contig - PETSC_TRUE if the index set refers to contiguous entries on this process, else PETSC_FALSE

234:    Level: developer

236:    Concepts: index sets^is contiguous

238: .seealso: ISGetLocalSize(), VecGetOwnershipRange()
239: @*/
240: PetscErrorCode  ISContiguousLocal(IS is,PetscInt gstart,PetscInt gend,PetscInt *start,PetscBool *contig)
241: {

248:   if (is->ops->contiguous) {
249:     (*is->ops->contiguous)(is,gstart,gend,start,contig);
250:   } else {
251:     *start  = -1;
252:     *contig = PETSC_FALSE;
253:   }
254:   return(0);
255: }

257: /*@
258:    ISPermutation - PETSC_TRUE or PETSC_FALSE depending on whether the
259:    index set has been declared to be a permutation.

261:    Logically Collective on IS

263:    Input Parmeters:
264: .  is - the index set

266:    Output Parameters:
267: .  perm - PETSC_TRUE if a permutation, else PETSC_FALSE

269:    Level: intermediate

271:   Concepts: permutation
272:   Concepts: index sets^is permutation

274: .seealso: ISSetPermutation()
275: @*/
276: PetscErrorCode  ISPermutation(IS is,PetscBool  *perm)
277: {
281:   *perm = (PetscBool) is->isperm;
282:   return(0);
283: }

285: /*@
286:    ISSetPermutation - Informs the index set that it is a permutation.

288:    Logically Collective on IS

290:    Input Parmeters:
291: .  is - the index set

293:    Level: intermediate

295:   Concepts: permutation
296:   Concepts: index sets^permutation

298:    The debug version of the libraries (./configure --with-debugging=1) checks if the
299:   index set is actually a permutation. The optimized version just believes you.

301: .seealso: ISPermutation()
302: @*/
303: PetscErrorCode  ISSetPermutation(IS is)
304: {
307: #if defined(PETSC_USE_DEBUG)
308:   {
309:     PetscMPIInt    size;

312:     MPI_Comm_size(PetscObjectComm((PetscObject)is),&size);
313:     if (size == 1) {
314:       PetscInt       i,n,*idx;
315:       const PetscInt *iidx;

317:       ISGetSize(is,&n);
318:       PetscMalloc1(n,&idx);
319:       ISGetIndices(is,&iidx);
320:       PetscMemcpy(idx,iidx,n*sizeof(PetscInt));
321:       PetscSortInt(n,idx);
322:       for (i=0; i<n; i++) {
323:         if (idx[i] != i) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Index set is not a permutation");
324:       }
325:       PetscFree(idx);
326:       ISRestoreIndices(is,&iidx);
327:     }
328:   }
329: #endif
330:   is->isperm = PETSC_TRUE;
331:   return(0);
332: }

334: /*@
335:    ISDestroy - Destroys an index set.

337:    Collective on IS

339:    Input Parameters:
340: .  is - the index set

342:    Level: beginner

344: .seealso: ISCreateGeneral(), ISCreateStride(), ISCreateBlocked()
345: @*/
346: PetscErrorCode  ISDestroy(IS *is)
347: {

351:   if (!*is) return(0);
353:   if (--((PetscObject)(*is))->refct > 0) {*is = 0; return(0);}
354:   if ((*is)->complement) {
355:     PetscInt refcnt;
356:     PetscObjectGetReference((PetscObject)((*is)->complement), &refcnt);
357:     if (refcnt > 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Nonlocal IS has not been restored");
358:     ISDestroy(&(*is)->complement);
359:   }
360:   if ((*is)->ops->destroy) {
361:     (*(*is)->ops->destroy)(*is);
362:   }
363:   PetscLayoutDestroy(&(*is)->map);
364:   /* Destroy local representations of offproc data. */
365:   PetscFree((*is)->total);
366:   PetscFree((*is)->nonlocal);
367:   PetscHeaderDestroy(is);
368:   return(0);
369: }

371: /*@
372:    ISInvertPermutation - Creates a new permutation that is the inverse of
373:                          a given permutation.

375:    Collective on IS

377:    Input Parameter:
378: +  is - the index set
379: -  nlocal - number of indices on this processor in result (ignored for 1 proccessor) or
380:             use PETSC_DECIDE

382:    Output Parameter:
383: .  isout - the inverse permutation

385:    Level: intermediate

387:    Notes:
388:     For parallel index sets this does the complete parallel permutation, but the
389:     code is not efficient for huge index sets (10,000,000 indices).

391:    Concepts: inverse permutation
392:    Concepts: permutation^inverse
393:    Concepts: index sets^inverting
394: @*/
395: PetscErrorCode  ISInvertPermutation(IS is,PetscInt nlocal,IS *isout)
396: {

402:   if (!is->isperm) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Not a permutation, must call ISSetPermutation() on the IS first");
403:   if (is->isidentity) {
404:     ISDuplicate(is,isout);
405:   } else {
406:     (*is->ops->invertpermutation)(is,nlocal,isout);
407:     ISSetPermutation(*isout);
408:   }
409:   return(0);
410: }

412: /*@
413:    ISGetSize - Returns the global length of an index set.

415:    Not Collective

417:    Input Parameter:
418: .  is - the index set

420:    Output Parameter:
421: .  size - the global size

423:    Level: beginner

425:    Concepts: size^of index set
426:    Concepts: index sets^size

428: @*/
429: PetscErrorCode  ISGetSize(IS is,PetscInt *size)
430: {

436:   (*is->ops->getsize)(is,size);
437:   return(0);
438: }

440: /*@
441:    ISGetLocalSize - Returns the local (processor) length of an index set.

443:    Not Collective

445:    Input Parameter:
446: .  is - the index set

448:    Output Parameter:
449: .  size - the local size

451:    Level: beginner

453:    Concepts: size^of index set
454:    Concepts: local size^of index set
455:    Concepts: index sets^local size

457: @*/
458: PetscErrorCode  ISGetLocalSize(IS is,PetscInt *size)
459: {

465:   (*is->ops->getlocalsize)(is,size);
466:   return(0);
467: }

469: /*@C
470:    ISGetIndices - Returns a pointer to the indices.  The user should call
471:    ISRestoreIndices() after having looked at the indices.  The user should
472:    NOT change the indices.

474:    Not Collective

476:    Input Parameter:
477: .  is - the index set

479:    Output Parameter:
480: .  ptr - the location to put the pointer to the indices

482:    Fortran Note:
483:    This routine has two different interfaces from Fortran; the first is not recommend, it does not require Fortran 90
484: $    IS          is
485: $    integer     is_array(1)
486: $    PetscOffset i_is
487: $    int         ierr
488: $       call ISGetIndices(is,is_array,i_is,ierr)
489: $
490: $   Access first local entry in list
491: $      value = is_array(i_is + 1)
492: $
493: $      ...... other code
494: $       call ISRestoreIndices(is,is_array,i_is,ierr)
495:    The second Fortran interface is recommended.
496: $          use petscisdef
497: $          PetscInt, pointer :: array(:)
498: $          PetscErrorCode  ierr
499: $          IS       i
500: $          call ISGetIndicesF90(i,array,ierr)



504:    See the Fortran chapter of the users manual and
505:    petsc/src/is/examples/[tutorials,tests] for details.

507:    Level: intermediate

509:    Concepts: index sets^getting indices
510:    Concepts: indices of index set

512: .seealso: ISRestoreIndices(), ISGetIndicesF90()
513: @*/
514: PetscErrorCode  ISGetIndices(IS is,const PetscInt *ptr[])
515: {

521:   (*is->ops->getindices)(is,ptr);
522:   return(0);
523: }

525: /*@C
526:    ISGetMinMax - Gets the minimum and maximum values in an IS

528:    Not Collective

530:    Input Parameter:
531: .  is - the index set

533:    Output Parameter:
534: +   min - the minimum value
535: -   max - the maximum value

537:    Level: intermediate

539:    Notes:
540:     Empty index sets return min=PETSC_MAX_INT and max=PETSC_MIN_INT.

542:    Concepts: index sets^getting indices
543:    Concepts: indices of index set

545: .seealso: ISGetIndices(), ISRestoreIndices(), ISGetIndicesF90()
546: @*/
547: PetscErrorCode  ISGetMinMax(IS is,PetscInt *min,PetscInt *max)
548: {
551:   if (min) *min = is->min;
552:   if (max) *max = is->max;
553:   return(0);
554: }

556: /*@
557:   ISLocate - determine the location of an index within the local component of an index set

559:   Not Collective

561:   Input Parameter:
562: + is - the index set
563: - key - the search key

565:   Output Parameter:
566: . location - if >= 0, a location within the index set that is equal to the key, otherwise the key is not in the index set

568:   Level: intermediate
569: @*/
570: PetscErrorCode ISLocate(IS is, PetscInt key, PetscInt *location)
571: {

575:   if (is->ops->locate) {
576:     (*is->ops->locate)(is,key,location);
577:   } else {
578:     PetscInt       numIdx;
579:     PetscBool      sorted;
580:     const PetscInt *idx;

582:     ISGetLocalSize(is,&numIdx);
583:     ISGetIndices(is,&idx);
584:     ISSorted(is,&sorted);
585:     if (sorted) {
586:       PetscFindInt(key,numIdx,idx,location);
587:     } else {
588:       PetscInt i;

590:       *location = -1;
591:       for (i = 0; i < numIdx; i++) {
592:         if (idx[i] == key) {
593:           *location = i;
594:           break;
595:         }
596:       }
597:     }
598:     ISRestoreIndices(is,&idx);
599:   }
600:   return(0);
601: }

603: /*@C
604:    ISRestoreIndices - Restores an index set to a usable state after a call
605:                       to ISGetIndices().

607:    Not Collective

609:    Input Parameters:
610: +  is - the index set
611: -  ptr - the pointer obtained by ISGetIndices()

613:    Fortran Note:
614:    This routine is used differently from Fortran
615: $    IS          is
616: $    integer     is_array(1)
617: $    PetscOffset i_is
618: $    int         ierr
619: $       call ISGetIndices(is,is_array,i_is,ierr)
620: $
621: $   Access first local entry in list
622: $      value = is_array(i_is + 1)
623: $
624: $      ...... other code
625: $       call ISRestoreIndices(is,is_array,i_is,ierr)

627:    See the Fortran chapter of the users manual and
628:    petsc/src/is/examples/[tutorials,tests] for details.

630:    Level: intermediate

632:    Note:
633:    This routine zeros out ptr. This is to prevent accidental us of the array after it has been restored.

635: .seealso: ISGetIndices(), ISRestoreIndicesF90()
636: @*/
637: PetscErrorCode  ISRestoreIndices(IS is,const PetscInt *ptr[])
638: {

644:   if (is->ops->restoreindices) {
645:     (*is->ops->restoreindices)(is,ptr);
646:   }
647:   return(0);
648: }

650: static PetscErrorCode ISGatherTotal_Private(IS is)
651: {
653:   PetscInt       i,n,N;
654:   const PetscInt *lindices;
655:   MPI_Comm       comm;
656:   PetscMPIInt    rank,size,*sizes = NULL,*offsets = NULL,nn;


661:   PetscObjectGetComm((PetscObject)is,&comm);
662:   MPI_Comm_size(comm,&size);
663:   MPI_Comm_rank(comm,&rank);
664:   ISGetLocalSize(is,&n);
665:   PetscMalloc2(size,&sizes,size,&offsets);

667:   PetscMPIIntCast(n,&nn);
668:   MPI_Allgather(&nn,1,MPI_INT,sizes,1,MPI_INT,comm);
669:   offsets[0] = 0;
670:   for (i=1; i<size; ++i) offsets[i] = offsets[i-1] + sizes[i-1];
671:   N = offsets[size-1] + sizes[size-1];

673:   PetscMalloc1(N,&(is->total));
674:   ISGetIndices(is,&lindices);
675:   MPI_Allgatherv((void*)lindices,nn,MPIU_INT,is->total,sizes,offsets,MPIU_INT,comm);
676:   ISRestoreIndices(is,&lindices);
677:   is->local_offset = offsets[rank];
678:   PetscFree2(sizes,offsets);
679:   return(0);
680: }

682: /*@C
683:    ISGetTotalIndices - Retrieve an array containing all indices across the communicator.

685:    Collective on IS

687:    Input Parameter:
688: .  is - the index set

690:    Output Parameter:
691: .  indices - total indices with rank 0 indices first, and so on; total array size is
692:              the same as returned with ISGetSize().

694:    Level: intermediate

696:    Notes:
697:     this is potentially nonscalable, but depends on the size of the total index set
698:      and the size of the communicator. This may be feasible for index sets defined on
699:      subcommunicators, such that the set size does not grow with PETSC_WORLD_COMM.
700:      Note also that there is no way to tell where the local part of the indices starts
701:      (use ISGetIndices() and ISGetNonlocalIndices() to retrieve just the local and just
702:       the nonlocal part (complement), respectively).

704:    Concepts: index sets^getting nonlocal indices
705: .seealso: ISRestoreTotalIndices(), ISGetNonlocalIndices(), ISGetSize()
706: @*/
707: PetscErrorCode ISGetTotalIndices(IS is, const PetscInt *indices[])
708: {
710:   PetscMPIInt    size;

715:   MPI_Comm_size(PetscObjectComm((PetscObject)is), &size);
716:   if (size == 1) {
717:     (*is->ops->getindices)(is,indices);
718:   } else {
719:     if (!is->total) {
720:       ISGatherTotal_Private(is);
721:     }
722:     *indices = is->total;
723:   }
724:   return(0);
725: }

727: /*@C
728:    ISRestoreTotalIndices - Restore the index array obtained with ISGetTotalIndices().

730:    Not Collective.

732:    Input Parameter:
733: +  is - the index set
734: -  indices - index array; must be the array obtained with ISGetTotalIndices()

736:    Level: intermediate

738:    Concepts: index sets^getting nonlocal indices
739:    Concepts: index sets^restoring nonlocal indices
740: .seealso: ISRestoreTotalIndices(), ISGetNonlocalIndices()
741: @*/
742: PetscErrorCode  ISRestoreTotalIndices(IS is, const PetscInt *indices[])
743: {
745:   PetscMPIInt    size;

750:   MPI_Comm_size(PetscObjectComm((PetscObject)is), &size);
751:   if (size == 1) {
752:     (*is->ops->restoreindices)(is,indices);
753:   } else {
754:     if (is->total != *indices) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Index array pointer being restored does not point to the array obtained from the IS.");
755:   }
756:   return(0);
757: }
758: /*@C
759:    ISGetNonlocalIndices - Retrieve an array of indices from remote processors
760:                        in this communicator.

762:    Collective on IS

764:    Input Parameter:
765: .  is - the index set

767:    Output Parameter:
768: .  indices - indices with rank 0 indices first, and so on,  omitting
769:              the current rank.  Total number of indices is the difference
770:              total and local, obtained with ISGetSize() and ISGetLocalSize(),
771:              respectively.

773:    Level: intermediate

775:    Notes:
776:     restore the indices using ISRestoreNonlocalIndices().
777:           The same scalability considerations as those for ISGetTotalIndices
778:           apply here.

780:    Concepts: index sets^getting nonlocal indices
781: .seealso: ISGetTotalIndices(), ISRestoreNonlocalIndices(), ISGetSize(), ISGetLocalSize().
782: @*/
783: PetscErrorCode  ISGetNonlocalIndices(IS is, const PetscInt *indices[])
784: {
786:   PetscMPIInt    size;
787:   PetscInt       n, N;

792:   MPI_Comm_size(PetscObjectComm((PetscObject)is), &size);
793:   if (size == 1) *indices = NULL;
794:   else {
795:     if (!is->total) {
796:       ISGatherTotal_Private(is);
797:     }
798:     ISGetLocalSize(is,&n);
799:     ISGetSize(is,&N);
800:     PetscMalloc1(N-n, &(is->nonlocal));
801:     PetscMemcpy(is->nonlocal, is->total, sizeof(PetscInt)*is->local_offset);
802:     PetscMemcpy(is->nonlocal+is->local_offset, is->total+is->local_offset+n, sizeof(PetscInt)*(N - is->local_offset - n));
803:     *indices = is->nonlocal;
804:   }
805:   return(0);
806: }

808: /*@C
809:    ISRestoreTotalIndices - Restore the index array obtained with ISGetNonlocalIndices().

811:    Not Collective.

813:    Input Parameter:
814: +  is - the index set
815: -  indices - index array; must be the array obtained with ISGetNonlocalIndices()

817:    Level: intermediate

819:    Concepts: index sets^getting nonlocal indices
820:    Concepts: index sets^restoring nonlocal indices
821: .seealso: ISGetTotalIndices(), ISGetNonlocalIndices(), ISRestoreTotalIndices()
822: @*/
823: PetscErrorCode  ISRestoreNonlocalIndices(IS is, const PetscInt *indices[])
824: {
828:   if (is->nonlocal != *indices) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Index array pointer being restored does not point to the array obtained from the IS.");
829:   return(0);
830: }

832: /*@
833:    ISGetNonlocalIS - Gather all nonlocal indices for this IS and present
834:                      them as another sequential index set.


837:    Collective on IS

839:    Input Parameter:
840: .  is - the index set

842:    Output Parameter:
843: .  complement - sequential IS with indices identical to the result of
844:                 ISGetNonlocalIndices()

846:    Level: intermediate

848:    Notes:
849:     complement represents the result of ISGetNonlocalIndices as an IS.
850:           Therefore scalability issues similar to ISGetNonlocalIndices apply.
851:           The resulting IS must be restored using ISRestoreNonlocalIS().

853:    Concepts: index sets^getting nonlocal indices
854: .seealso: ISGetNonlocalIndices(), ISRestoreNonlocalIndices(),  ISAllGather(), ISGetSize()
855: @*/
856: PetscErrorCode  ISGetNonlocalIS(IS is, IS *complement)
857: {

863:   /* Check if the complement exists already. */
864:   if (is->complement) {
865:     *complement = is->complement;
866:     PetscObjectReference((PetscObject)(is->complement));
867:   } else {
868:     PetscInt       N, n;
869:     const PetscInt *idx;
870:     ISGetSize(is, &N);
871:     ISGetLocalSize(is,&n);
872:     ISGetNonlocalIndices(is, &idx);
873:     ISCreateGeneral(PETSC_COMM_SELF, N-n,idx, PETSC_USE_POINTER, &(is->complement));
874:     PetscObjectReference((PetscObject)is->complement);
875:     *complement = is->complement;
876:   }
877:   return(0);
878: }


881: /*@
882:    ISRestoreNonlocalIS - Restore the IS obtained with ISGetNonlocalIS().

884:    Not collective.

886:    Input Parameter:
887: +  is         - the index set
888: -  complement - index set of is's nonlocal indices

890:    Level: intermediate


893:    Concepts: index sets^getting nonlocal indices
894:    Concepts: index sets^restoring nonlocal indices
895: .seealso: ISGetNonlocalIS(), ISGetNonlocalIndices(), ISRestoreNonlocalIndices()
896: @*/
897: PetscErrorCode  ISRestoreNonlocalIS(IS is, IS *complement)
898: {
900:   PetscInt       refcnt;

905:   if (*complement != is->complement) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Complement IS being restored was not obtained with ISGetNonlocalIS()");
906:   PetscObjectGetReference((PetscObject)(is->complement), &refcnt);
907:   if (refcnt <= 1) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Duplicate call to ISRestoreNonlocalIS() detected");
908:   PetscObjectDereference((PetscObject)(is->complement));
909:   return(0);
910: }

912: /*@C
913:    ISView - Displays an index set.

915:    Collective on IS

917:    Input Parameters:
918: +  is - the index set
919: -  viewer - viewer used to display the set, for example PETSC_VIEWER_STDOUT_SELF.

921:    Level: intermediate

923: .seealso: PetscViewerASCIIOpen()
924: @*/
925: PetscErrorCode  ISView(IS is,PetscViewer viewer)
926: {

931:   if (!viewer) {
932:     PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)is),&viewer);
933:   }

937:   PetscObjectPrintClassNamePrefixType((PetscObject)is,viewer);
938:   (*is->ops->view)(is,viewer);
939:   return(0);
940: }

942: /*@
943:   ISLoad - Loads a vector that has been stored in binary or HDF5 format with ISView().

945:   Collective on PetscViewer

947:   Input Parameters:
948: + is - the newly loaded vector, this needs to have been created with ISCreate() or some related function before a call to ISLoad().
949: - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or HDF5 file viewer, obtained from PetscViewerHDF5Open()

951:   Level: intermediate

953:   Notes:
954:   IF using HDF5, you must assign the IS the same name as was used in the IS
955:   that was stored in the file using PetscObjectSetName(). Otherwise you will
956:   get the error message: "Cannot H5DOpen2() with Vec name NAMEOFOBJECT"

958:   Concepts: index set^loading from file

960: .seealso: PetscViewerBinaryOpen(), ISView(), MatLoad(), VecLoad()
961: @*/
962: PetscErrorCode ISLoad(IS is, PetscViewer viewer)
963: {
964:   PetscBool      isbinary, ishdf5;

970:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERBINARY, &isbinary);
971:   PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);
972:   if (!isbinary && !ishdf5) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()");
973:   if (!((PetscObject)is)->type_name) {ISSetType(is, ISGENERAL);}
974:   (*is->ops->load)(is, viewer);
975:   return(0);
976: }

978: /*@
979:    ISSort - Sorts the indices of an index set.

981:    Collective on IS

983:    Input Parameters:
984: .  is - the index set

986:    Level: intermediate

988:    Concepts: index sets^sorting
989:    Concepts: sorting^index set

991: .seealso: ISSortRemoveDups(), ISSorted()
992: @*/
993: PetscErrorCode  ISSort(IS is)
994: {

999:   (*is->ops->sort)(is);
1000:   return(0);
1001: }

1003: /*@
1004:   ISSortRemoveDups - Sorts the indices of an index set, removing duplicates.

1006:   Collective on IS

1008:   Input Parameters:
1009: . is - the index set

1011:   Level: intermediate

1013:   Concepts: index sets^sorting
1014:   Concepts: sorting^index set

1016: .seealso: ISSort(), ISSorted()
1017: @*/
1018: PetscErrorCode ISSortRemoveDups(IS is)
1019: {

1024:   (*is->ops->sortremovedups)(is);
1025:   return(0);
1026: }

1028: /*@
1029:    ISToGeneral - Converts an IS object of any type to ISGENERAL type

1031:    Collective on IS

1033:    Input Parameters:
1034: .  is - the index set

1036:    Level: intermediate

1038:    Concepts: index sets^sorting
1039:    Concepts: sorting^index set

1041: .seealso: ISSorted()
1042: @*/
1043: PetscErrorCode  ISToGeneral(IS is)
1044: {

1049:   if (is->ops->togeneral) {
1050:     (*is->ops->togeneral)(is);
1051:   } else SETERRQ1(PetscObjectComm((PetscObject)is),PETSC_ERR_SUP,"Not written for this type %s",((PetscObject)is)->type_name);
1052:   return(0);
1053: }

1055: /*@
1056:    ISSorted - Checks the indices to determine whether they have been sorted.

1058:    Collective on IS

1060:    Input Parameter:
1061: .  is - the index set

1063:    Output Parameter:
1064: .  flg - output flag, either PETSC_TRUE if the index set is sorted,
1065:          or PETSC_FALSE otherwise.

1067:    Notes:
1068:     For parallel IS objects this only indicates if the local part of the IS
1069:           is sorted. So some processors may return PETSC_TRUE while others may
1070:           return PETSC_FALSE.

1072:    Level: intermediate

1074: .seealso: ISSort(), ISSortRemoveDups()
1075: @*/
1076: PetscErrorCode  ISSorted(IS is,PetscBool  *flg)
1077: {

1083:   (*is->ops->sorted)(is,flg);
1084:   return(0);
1085: }

1087: /*@
1088:    ISDuplicate - Creates a duplicate copy of an index set.

1090:    Collective on IS

1092:    Input Parmeters:
1093: .  is - the index set

1095:    Output Parameters:
1096: .  isnew - the copy of the index set

1098:    Level: beginner

1100:    Concepts: index sets^duplicating

1102: .seealso: ISCreateGeneral(), ISCopy()
1103: @*/
1104: PetscErrorCode  ISDuplicate(IS is,IS *newIS)
1105: {

1111:   (*is->ops->duplicate)(is,newIS);
1112:   (*newIS)->isidentity = is->isidentity;
1113:   (*newIS)->isperm     = is->isperm;
1114:   return(0);
1115: }

1117: /*@
1118:    ISCopy - Copies an index set.

1120:    Collective on IS

1122:    Input Parmeters:
1123: .  is - the index set

1125:    Output Parameters:
1126: .  isy - the copy of the index set

1128:    Level: beginner

1130:    Concepts: index sets^copying

1132: .seealso: ISDuplicate()
1133: @*/
1134: PetscErrorCode  ISCopy(IS is,IS isy)
1135: {

1142:   if (is == isy) return(0);
1143:   (*is->ops->copy)(is,isy);
1144:   isy->isperm     = is->isperm;
1145:   isy->max        = is->max;
1146:   isy->min        = is->min;
1147:   isy->isidentity = is->isidentity;
1148:   return(0);
1149: }

1151: /*@
1152:    ISOnComm - Split a parallel IS on subcomms (usually self) or concatenate index sets on subcomms into a parallel index set

1154:    Collective on IS and comm

1156:    Input Arguments:
1157: + is - index set
1158: . comm - communicator for new index set
1159: - mode - copy semantics, PETSC_USE_POINTER for no-copy if possible, otherwise PETSC_COPY_VALUES

1161:    Output Arguments:
1162: . newis - new IS on comm

1164:    Level: advanced

1166:    Notes:
1167:    It is usually desirable to create a parallel IS and look at the local part when necessary.

1169:    This function is useful if serial ISs must be created independently, or to view many
1170:    logically independent serial ISs.

1172:    The input IS must have the same type on every process.

1174: .seealso: ISSplit()
1175: @*/
1176: PetscErrorCode  ISOnComm(IS is,MPI_Comm comm,PetscCopyMode mode,IS *newis)
1177: {
1179:   PetscMPIInt    match;

1184:   MPI_Comm_compare(PetscObjectComm((PetscObject)is),comm,&match);
1185:   if (mode != PETSC_COPY_VALUES && (match == MPI_IDENT || match == MPI_CONGRUENT)) {
1186:     PetscObjectReference((PetscObject)is);
1187:     *newis = is;
1188:   } else {
1189:     (*is->ops->oncomm)(is,comm,mode,newis);
1190:   }
1191:   return(0);
1192: }

1194: /*@
1195:    ISSetBlockSize - informs an index set that it has a given block size

1197:    Logicall Collective on IS

1199:    Input Arguments:
1200: + is - index set
1201: - bs - block size

1203:    Level: intermediate

1205: .seealso: ISGetBlockSize(), ISCreateBlock()
1206: @*/
1207: PetscErrorCode  ISSetBlockSize(IS is,PetscInt bs)
1208: {

1214:   if (bs < 1) SETERRQ1(PetscObjectComm((PetscObject)is),PETSC_ERR_ARG_OUTOFRANGE,"Block size %D, must be positive",bs);
1215:   (*is->ops->setblocksize)(is,bs);
1216:   return(0);
1217: }

1219: /*@
1220:    ISGetBlockSize - Returns the number of elements in a block.

1222:    Not Collective

1224:    Input Parameter:
1225: .  is - the index set

1227:    Output Parameter:
1228: .  size - the number of elements in a block

1230:    Level: intermediate

1232:    Concepts: IS^block size
1233:    Concepts: index sets^block size

1235: .seealso: ISBlockGetSize(), ISGetSize(), ISCreateBlock(), ISSetBlockSize()
1236: @*/
1237: PetscErrorCode  ISGetBlockSize(IS is,PetscInt *size)
1238: {

1242:   PetscLayoutGetBlockSize(is->map, size);
1243:   return(0);
1244: }

1246: PetscErrorCode ISGetIndicesCopy(IS is, PetscInt idx[])
1247: {
1249:   PetscInt       len,i;
1250:   const PetscInt *ptr;

1253:   ISGetSize(is,&len);
1254:   ISGetIndices(is,&ptr);
1255:   for (i=0; i<len; i++) idx[i] = ptr[i];
1256:   ISRestoreIndices(is,&ptr);
1257:   return(0);
1258: }

1260: /*MC
1261:     ISGetIndicesF90 - Accesses the elements of an index set from Fortran90.
1262:     The users should call ISRestoreIndicesF90() after having looked at the
1263:     indices.  The user should NOT change the indices.

1265:     Synopsis:
1266:     ISGetIndicesF90(IS x,{integer, pointer :: xx_v(:)},integer ierr)

1268:     Not collective

1270:     Input Parameter:
1271: .   x - index set

1273:     Output Parameters:
1274: +   xx_v - the Fortran90 pointer to the array
1275: -   ierr - error code

1277:     Example of Usage:
1278: .vb
1279:     PetscScalar, pointer xx_v(:)
1280:     ....
1281:     call ISGetIndicesF90(x,xx_v,ierr)
1282:     a = xx_v(3)
1283:     call ISRestoreIndicesF90(x,xx_v,ierr)
1284: .ve

1286:     Notes:
1287:     Not yet supported for all F90 compilers.

1289:     Level: intermediate

1291: .seealso:  ISRestoreIndicesF90(), ISGetIndices(), ISRestoreIndices()

1293:   Concepts: index sets^getting indices in f90
1294:   Concepts: indices of index set in f90

1296: M*/

1298: /*MC
1299:     ISRestoreIndicesF90 - Restores an index set to a usable state after
1300:     a call to ISGetIndicesF90().

1302:     Synopsis:
1303:     ISRestoreIndicesF90(IS x,{integer, pointer :: xx_v(:)},integer ierr)

1305:     Not collective

1307:     Input Parameters:
1308: .   x - index set
1309: .   xx_v - the Fortran90 pointer to the array

1311:     Output Parameter:
1312: .   ierr - error code


1315:     Example of Usage:
1316: .vb
1317:     PetscScalar, pointer xx_v(:)
1318:     ....
1319:     call ISGetIndicesF90(x,xx_v,ierr)
1320:     a = xx_v(3)
1321:     call ISRestoreIndicesF90(x,xx_v,ierr)
1322: .ve

1324:     Notes:
1325:     Not yet supported for all F90 compilers.

1327:     Level: intermediate

1329: .seealso:  ISGetIndicesF90(), ISGetIndices(), ISRestoreIndices()

1331: M*/

1333: /*MC
1334:     ISBlockGetIndicesF90 - Accesses the elements of an index set from Fortran90.
1335:     The users should call ISBlockRestoreIndicesF90() after having looked at the
1336:     indices.  The user should NOT change the indices.

1338:     Synopsis:
1339:     ISBlockGetIndicesF90(IS x,{integer, pointer :: xx_v(:)},integer ierr)

1341:     Not collective

1343:     Input Parameter:
1344: .   x - index set

1346:     Output Parameters:
1347: +   xx_v - the Fortran90 pointer to the array
1348: -   ierr - error code
1349:     Example of Usage:
1350: .vb
1351:     PetscScalar, pointer xx_v(:)
1352:     ....
1353:     call ISBlockGetIndicesF90(x,xx_v,ierr)
1354:     a = xx_v(3)
1355:     call ISBlockRestoreIndicesF90(x,xx_v,ierr)
1356: .ve

1358:     Notes:
1359:     Not yet supported for all F90 compilers

1361:     Level: intermediate

1363: .seealso:  ISBlockRestoreIndicesF90(), ISGetIndices(), ISRestoreIndices(),
1364:            ISRestoreIndices()

1366:   Concepts: index sets^getting block indices in f90
1367:   Concepts: indices of index set in f90
1368:   Concepts: block^ indices of index set in f90

1370: M*/

1372: /*MC
1373:     ISBlockRestoreIndicesF90 - Restores an index set to a usable state after
1374:     a call to ISBlockGetIndicesF90().

1376:     Synopsis:
1377:     ISBlockRestoreIndicesF90(IS x,{integer, pointer :: xx_v(:)},integer ierr)

1379:     Not Collective

1381:     Input Parameters:
1382: +   x - index set
1383: -   xx_v - the Fortran90 pointer to the array

1385:     Output Parameter:
1386: .   ierr - error code

1388:     Example of Usage:
1389: .vb
1390:     PetscScalar, pointer xx_v(:)
1391:     ....
1392:     call ISBlockGetIndicesF90(x,xx_v,ierr)
1393:     a = xx_v(3)
1394:     call ISBlockRestoreIndicesF90(x,xx_v,ierr)
1395: .ve

1397:     Notes:
1398:     Not yet supported for all F90 compilers

1400:     Level: intermediate

1402: .seealso:  ISBlockGetIndicesF90(), ISGetIndices(), ISRestoreIndices(), ISRestoreIndicesF90()

1404: M*/