Actual source code: index.c

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

  7: /* Logging support */
  8: PetscClassId IS_CLASSID;
  9: /* TODO: Much more events are missing! */
 10: PetscLogEvent IS_View;
 11: PetscLogEvent IS_Load;

 13: /*@
 14:   ISRenumber - Renumbers the non-negative entries of an index set in a contiguous way, starting from 0.

 16:   Collective

 18:   Input Parameters:
 19: + subset      - the index set
 20: - subset_mult - the multiplicity of each entry in subset (optional, can be `NULL`)

 22:   Output Parameters:
 23: + N        - one past the largest entry of the new `IS`
 24: - subset_n - the new `IS`

 26:   Level: intermediate

 28:   Note:
 29:   All negative entries are mapped to -1. Indices with non positive multiplicities are skipped.

 31: .seealso: `IS`
 32: @*/
 33: PetscErrorCode ISRenumber(IS subset, IS subset_mult, PetscInt *N, IS *subset_n)
 34: {
 35:   PetscSF         sf;
 36:   PetscLayout     map;
 37:   const PetscInt *idxs, *idxs_mult = NULL;
 38:   PetscInt       *leaf_data, *root_data, *gidxs, *ilocal, *ilocalneg;
 39:   PetscInt        N_n, n, i, bounds[2], Nl, ibs;
 40:   PetscInt        n_n, nlocals, start, first_index, npos, nneg;
 41:   PetscMPIInt     commsize;
 42:   PetscBool       first_found, isblock;

 44:   PetscFunctionBegin;
 47:   if (N) PetscAssertPointer(N, 3);
 48:   else if (!subset_n) PetscFunctionReturn(PETSC_SUCCESS);
 49:   PetscCall(ISGetLocalSize(subset, &n));
 50:   if (subset_mult) {
 51:     PetscCall(ISGetLocalSize(subset_mult, &i));
 52:     PetscCheck(i == n, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Local subset and multiplicity sizes don't match! %" PetscInt_FMT " != %" PetscInt_FMT, n, i);
 53:   }
 54:   /* create workspace layout for computing global indices of subset */
 55:   PetscCall(PetscMalloc1(n, &ilocal));
 56:   PetscCall(PetscMalloc1(n, &ilocalneg));
 57:   PetscCall(ISGetIndices(subset, &idxs));
 58:   PetscCall(ISGetBlockSize(subset, &ibs));
 59:   PetscCall(PetscObjectTypeCompare((PetscObject)subset, ISBLOCK, &isblock));
 60:   if (subset_mult) PetscCall(ISGetIndices(subset_mult, &idxs_mult));
 61:   bounds[0] = PETSC_INT_MAX;
 62:   bounds[1] = PETSC_INT_MIN;
 63:   for (i = 0, npos = 0, nneg = 0; i < n; i++) {
 64:     if (idxs[i] < 0) {
 65:       ilocalneg[nneg++] = i;
 66:       continue;
 67:     }
 68:     if (idxs[i] < bounds[0]) bounds[0] = idxs[i];
 69:     if (idxs[i] > bounds[1]) bounds[1] = idxs[i];
 70:     ilocal[npos++] = i;
 71:   }
 72:   if (npos == n) {
 73:     PetscCall(PetscFree(ilocal));
 74:     PetscCall(PetscFree(ilocalneg));
 75:   }

 77:   /* create sf : leaf_data == multiplicity of indexes, root data == global index in layout */
 78:   PetscCall(PetscMalloc1(n, &leaf_data));
 79:   for (i = 0; i < n; i++) leaf_data[i] = idxs_mult ? PetscMax(idxs_mult[i], 0) : 1;

 81:   /* local size of new subset */
 82:   n_n = 0;
 83:   for (i = 0; i < n; i++) n_n += leaf_data[i];
 84:   if (ilocalneg)
 85:     for (i = 0; i < nneg; i++) leaf_data[ilocalneg[i]] = 0;
 86:   PetscCall(PetscFree(ilocalneg));
 87:   PetscCall(PetscMalloc1(PetscMax(n_n, n), &gidxs)); /* allocating extra space to reuse gidxs */
 88:   /* check for early termination (all negative) */
 89:   PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)subset), bounds, bounds));
 90:   if (bounds[1] < bounds[0]) {
 91:     if (N) *N = 0;
 92:     if (subset_n) { /* all negative */
 93:       for (i = 0; i < n_n; i++) gidxs[i] = -1;
 94:       PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)subset), n_n, gidxs, PETSC_COPY_VALUES, subset_n));
 95:     }
 96:     PetscCall(PetscFree(leaf_data));
 97:     PetscCall(PetscFree(gidxs));
 98:     PetscCall(ISRestoreIndices(subset, &idxs));
 99:     if (subset_mult) PetscCall(ISRestoreIndices(subset_mult, &idxs_mult));
100:     PetscCall(PetscFree(ilocal));
101:     PetscCall(PetscFree(ilocalneg));
102:     PetscFunctionReturn(PETSC_SUCCESS);
103:   }

105:   /* split work */
106:   N_n = bounds[1] - bounds[0] + 1;
107:   PetscCall(PetscLayoutCreate(PetscObjectComm((PetscObject)subset), &map));
108:   PetscCall(PetscLayoutSetBlockSize(map, 1));
109:   PetscCall(PetscLayoutSetSize(map, N_n));
110:   PetscCall(PetscLayoutSetUp(map));
111:   PetscCall(PetscLayoutGetLocalSize(map, &Nl));

113:   /* global indexes in layout */
114:   for (i = 0; i < npos; i++) gidxs[i] = (ilocal ? idxs[ilocal[i]] : idxs[i]) - bounds[0];
115:   PetscCall(ISRestoreIndices(subset, &idxs));
116:   PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)subset), &sf));
117:   PetscCall(PetscSFSetGraphLayout(sf, map, npos, ilocal, PETSC_USE_POINTER, gidxs));
118:   PetscCall(PetscLayoutDestroy(&map));

120:   /* reduce from leaves to roots */
121:   PetscCall(PetscCalloc1(Nl, &root_data));
122:   PetscCall(PetscSFReduceBegin(sf, MPIU_INT, leaf_data, root_data, MPI_MAX));
123:   PetscCall(PetscSFReduceEnd(sf, MPIU_INT, leaf_data, root_data, MPI_MAX));

125:   /* count indexes in local part of layout */
126:   nlocals     = 0;
127:   first_index = -1;
128:   first_found = PETSC_FALSE;
129:   for (i = 0; i < Nl; i++) {
130:     if (!first_found && root_data[i]) {
131:       first_found = PETSC_TRUE;
132:       first_index = i;
133:     }
134:     nlocals += root_data[i];
135:   }

137:   /* cumulative of number of indexes and size of subset without holes */
138: #if PetscDefined(HAVE_MPI_EXSCAN)
139:   start = 0;
140:   PetscCallMPI(MPI_Exscan(&nlocals, &start, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)subset)));
141: #else
142:   PetscCallMPI(MPI_Scan(&nlocals, &start, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)subset)));
143:   start = start - nlocals;
144: #endif

146:   if (N) { /* compute total size of new subset if requested */
147:     *N = start + nlocals;
148:     PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)subset), &commsize));
149:     PetscCallMPI(MPI_Bcast(N, 1, MPIU_INT, commsize - 1, PetscObjectComm((PetscObject)subset)));
150:   }

152:   if (!subset_n) {
153:     PetscCall(PetscFree(gidxs));
154:     PetscCall(PetscSFDestroy(&sf));
155:     PetscCall(PetscFree(leaf_data));
156:     PetscCall(PetscFree(root_data));
157:     PetscCall(PetscFree(ilocal));
158:     if (subset_mult) PetscCall(ISRestoreIndices(subset_mult, &idxs_mult));
159:     PetscFunctionReturn(PETSC_SUCCESS);
160:   }

162:   /* adapt root data with cumulative */
163:   if (first_found) {
164:     PetscInt old_index;

166:     root_data[first_index] += start;
167:     old_index = first_index;
168:     for (i = first_index + 1; i < Nl; i++) {
169:       if (root_data[i]) {
170:         root_data[i] += root_data[old_index];
171:         old_index = i;
172:       }
173:     }
174:   }

176:   /* from roots to leaves */
177:   PetscCall(PetscSFBcastBegin(sf, MPIU_INT, root_data, leaf_data, MPI_REPLACE));
178:   PetscCall(PetscSFBcastEnd(sf, MPIU_INT, root_data, leaf_data, MPI_REPLACE));
179:   PetscCall(PetscSFDestroy(&sf));

181:   /* create new IS with global indexes without holes */
182:   for (i = 0; i < n_n; i++) gidxs[i] = -1;
183:   if (subset_mult) {
184:     PetscInt cum;

186:     isblock = PETSC_FALSE;
187:     for (i = 0, cum = 0; i < n; i++)
188:       for (PetscInt j = 0; j < idxs_mult[i]; j++) gidxs[cum++] = leaf_data[i] - idxs_mult[i] + j;
189:   } else
190:     for (i = 0; i < n; i++) gidxs[i] = leaf_data[i] - 1;

192:   if (isblock) {
193:     if (ibs > 1)
194:       for (i = 0; i < n_n / ibs; i++) gidxs[i] = gidxs[i * ibs] / ibs;
195:     PetscCall(ISCreateBlock(PetscObjectComm((PetscObject)subset), ibs, n_n / ibs, gidxs, PETSC_COPY_VALUES, subset_n));
196:   } else {
197:     PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)subset), n_n, gidxs, PETSC_COPY_VALUES, subset_n));
198:   }
199:   if (subset_mult) PetscCall(ISRestoreIndices(subset_mult, &idxs_mult));
200:   PetscCall(PetscFree(gidxs));
201:   PetscCall(PetscFree(leaf_data));
202:   PetscCall(PetscFree(root_data));
203:   PetscCall(PetscFree(ilocal));
204:   PetscFunctionReturn(PETSC_SUCCESS);
205: }

207: /*@
208:   ISCreateSubIS - Create a sub index set from a global index set selecting some components.

210:   Collective

212:   Input Parameters:
213: + is    - the index set
214: - comps - which components we will extract from `is`

216:   Output Parameters:
217: . subis - the new sub index set

219:   Example usage:
220:   We have an index set `is` living on 3 processes with the following values\:
221:   | 4 9 0 | 2 6 7 | 10 11 1|
222:   and another index set `comps` used to indicate which components of is  we want to take,
223:   | 7 5  | 1 2 | 0 4|
224:   The output index set `subis` should look like\:
225:   | 11 7 | 9 0 | 4 6|

227:   Level: intermediate

229: .seealso: `IS`, `VecGetSubVector()`, `MatCreateSubMatrix()`
230: @*/
231: PetscErrorCode ISCreateSubIS(IS is, IS comps, IS *subis)
232: {
233:   PetscSF         sf;
234:   const PetscInt *is_indices, *comps_indices;
235:   PetscInt       *subis_indices, nroots, nleaves, *mine, i, lidx;
236:   PetscMPIInt     owner;
237:   PetscSFNode    *remote;
238:   MPI_Comm        comm;

240:   PetscFunctionBegin;
243:   PetscAssertPointer(subis, 3);

245:   PetscCall(PetscObjectGetComm((PetscObject)is, &comm));
246:   PetscCall(ISGetLocalSize(comps, &nleaves));
247:   PetscCall(ISGetLocalSize(is, &nroots));
248:   PetscCall(PetscMalloc1(nleaves, &remote));
249:   PetscCall(PetscMalloc1(nleaves, &mine));
250:   PetscCall(ISGetIndices(comps, &comps_indices));
251:   /*
252:    * Construct a PetscSF in which "is" data serves as roots and "subis" is leaves.
253:    * Root data are sent to leaves using PetscSFBcast().
254:    * */
255:   for (i = 0; i < nleaves; i++) {
256:     mine[i] = i;
257:     /* Connect a remote root with the current leaf. The value on the remote root
258:      * will be received by the current local leaf.
259:      * */
260:     owner = -1;
261:     lidx  = -1;
262:     PetscCall(PetscLayoutFindOwnerIndex(is->map, comps_indices[i], &owner, &lidx));
263:     remote[i].rank  = owner;
264:     remote[i].index = lidx;
265:   }
266:   PetscCall(ISRestoreIndices(comps, &comps_indices));
267:   PetscCall(PetscSFCreate(comm, &sf));
268:   PetscCall(PetscSFSetFromOptions(sf));
269:   PetscCall(PetscSFSetGraph(sf, nroots, nleaves, mine, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER));

271:   PetscCall(PetscMalloc1(nleaves, &subis_indices));
272:   PetscCall(ISGetIndices(is, &is_indices));
273:   PetscCall(PetscSFBcastBegin(sf, MPIU_INT, is_indices, subis_indices, MPI_REPLACE));
274:   PetscCall(PetscSFBcastEnd(sf, MPIU_INT, is_indices, subis_indices, MPI_REPLACE));
275:   PetscCall(ISRestoreIndices(is, &is_indices));
276:   PetscCall(PetscSFDestroy(&sf));
277:   PetscCall(ISCreateGeneral(comm, nleaves, subis_indices, PETSC_OWN_POINTER, subis));
278:   PetscFunctionReturn(PETSC_SUCCESS);
279: }

281: /*@
282:   ISClearInfoCache - clear the cache of computed index set properties

284:   Not Collective

286:   Input Parameters:
287: + is                    - the index set
288: - clear_permanent_local - whether to remove the permanent status of local properties

290:   Level: developer

292:   Note:
293:   Because all processes must agree on the global permanent status of a property,
294:   the permanent status can only be changed with `ISSetInfo()`, because this routine is not collective

296: .seealso: `IS`, `ISInfo`, `ISInfoType`, `ISSetInfo()`
297: @*/
298: PetscErrorCode ISClearInfoCache(IS is, PetscBool clear_permanent_local)
299: {
300:   PetscInt i;

302:   PetscFunctionBegin;
305:   for (i = 0; i < IS_INFO_MAX; i++) {
306:     if (clear_permanent_local) is->info_permanent[IS_LOCAL][i] = PETSC_FALSE;
307:     for (PetscInt j = 0; j < 2; j++) {
308:       if (!is->info_permanent[j][i]) is->info[j][i] = IS_INFO_UNKNOWN;
309:     }
310:   }
311:   PetscFunctionReturn(PETSC_SUCCESS);
312: }

314: static PetscErrorCode ISSetInfo_Internal(IS is, ISInfo info, ISInfoType type, ISInfoBool ipermanent, PetscBool flg)
315: {
316:   ISInfoBool iflg          = flg ? IS_INFO_TRUE : IS_INFO_FALSE;
317:   PetscInt   itype         = (type == IS_LOCAL) ? 0 : 1;
318:   PetscBool  permanent_set = (ipermanent == IS_INFO_UNKNOWN) ? PETSC_FALSE : PETSC_TRUE;
319:   PetscBool  permanent     = (ipermanent == IS_INFO_TRUE) ? PETSC_TRUE : PETSC_FALSE;

321:   PetscFunctionBegin;
322:   /* set this property */
323:   is->info[itype][(int)info] = iflg;
324:   if (permanent_set) is->info_permanent[itype][(int)info] = permanent;
325:   /* set implications */
326:   switch (info) {
327:   case IS_SORTED:
328:     if (PetscDefined(USE_DEBUG) && flg) {
329:       PetscInt        n;
330:       const PetscInt *indices;

332:       PetscCall(ISGetLocalSize(is, &n));
333:       PetscCall(ISGetIndices(is, &indices));
334:       PetscCall(PetscSortedInt(n, indices, &flg));
335:       if (type == IS_GLOBAL) PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &flg, 1, MPI_C_BOOL, MPI_LAND, PetscObjectComm((PetscObject)is)));
336:       PetscCheck(flg, type == IS_GLOBAL ? PetscObjectComm((PetscObject)is) : PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "IS is not sorted");
337:       PetscCall(ISRestoreIndices(is, &indices));
338:     }
339:     if (flg && type == IS_GLOBAL) { /* an array that is globally sorted is also locally sorted */
340:       is->info[IS_LOCAL][(int)info] = IS_INFO_TRUE;
341:       /* global permanence implies local permanence */
342:       if (permanent_set && permanent) is->info_permanent[IS_LOCAL][(int)info] = PETSC_TRUE;
343:     }
344:     if (!flg) { /* if an array is not sorted, it cannot be an interval or the identity */
345:       is->info[itype][IS_INTERVAL] = IS_INFO_FALSE;
346:       is->info[itype][IS_IDENTITY] = IS_INFO_FALSE;
347:       if (permanent_set) {
348:         is->info_permanent[itype][IS_INTERVAL] = permanent;
349:         is->info_permanent[itype][IS_IDENTITY] = permanent;
350:       }
351:     }
352:     break;
353:   case IS_UNIQUE:
354:     if (flg && type == IS_GLOBAL) { /* an array that is globally unique is also locally unique */
355:       is->info[IS_LOCAL][(int)info] = IS_INFO_TRUE;
356:       /* global permanence implies local permanence */
357:       if (permanent_set && permanent) is->info_permanent[IS_LOCAL][(int)info] = PETSC_TRUE;
358:     }
359:     if (!flg) { /* if an array is not unique, it cannot be a permutation, and interval, or the identity */
360:       is->info[itype][IS_PERMUTATION] = IS_INFO_FALSE;
361:       is->info[itype][IS_INTERVAL]    = IS_INFO_FALSE;
362:       is->info[itype][IS_IDENTITY]    = IS_INFO_FALSE;
363:       if (permanent_set) {
364:         is->info_permanent[itype][IS_PERMUTATION] = permanent;
365:         is->info_permanent[itype][IS_INTERVAL]    = permanent;
366:         is->info_permanent[itype][IS_IDENTITY]    = permanent;
367:       }
368:     }
369:     break;
370:   case IS_PERMUTATION:
371:     if (flg) { /* an array that is a permutation is unique and is unique locally */
372:       is->info[itype][IS_UNIQUE]    = IS_INFO_TRUE;
373:       is->info[IS_LOCAL][IS_UNIQUE] = IS_INFO_TRUE;
374:       if (permanent_set && permanent) {
375:         is->info_permanent[itype][IS_UNIQUE]    = PETSC_TRUE;
376:         is->info_permanent[IS_LOCAL][IS_UNIQUE] = PETSC_TRUE;
377:       }
378:     } else { /* an array that is not a permutation cannot be the identity */
379:       is->info[itype][IS_IDENTITY] = IS_INFO_FALSE;
380:       if (permanent_set) is->info_permanent[itype][IS_IDENTITY] = permanent;
381:     }
382:     break;
383:   case IS_INTERVAL:
384:     if (flg) { /* an array that is an interval is sorted and unique */
385:       is->info[itype][IS_SORTED]    = IS_INFO_TRUE;
386:       is->info[IS_LOCAL][IS_SORTED] = IS_INFO_TRUE;
387:       is->info[itype][IS_UNIQUE]    = IS_INFO_TRUE;
388:       is->info[IS_LOCAL][IS_UNIQUE] = IS_INFO_TRUE;
389:       if (permanent_set && permanent) {
390:         is->info_permanent[itype][IS_SORTED]    = PETSC_TRUE;
391:         is->info_permanent[IS_LOCAL][IS_SORTED] = PETSC_TRUE;
392:         is->info_permanent[itype][IS_UNIQUE]    = PETSC_TRUE;
393:         is->info_permanent[IS_LOCAL][IS_UNIQUE] = PETSC_TRUE;
394:       }
395:     } else { /* an array that is not an interval cannot be the identity */
396:       is->info[itype][IS_IDENTITY] = IS_INFO_FALSE;
397:       if (permanent_set) is->info_permanent[itype][IS_IDENTITY] = permanent;
398:     }
399:     break;
400:   case IS_IDENTITY:
401:     if (flg) { /* an array that is the identity is sorted, unique, an interval, and a permutation */
402:       is->info[itype][IS_SORTED]      = IS_INFO_TRUE;
403:       is->info[IS_LOCAL][IS_SORTED]   = IS_INFO_TRUE;
404:       is->info[itype][IS_UNIQUE]      = IS_INFO_TRUE;
405:       is->info[IS_LOCAL][IS_UNIQUE]   = IS_INFO_TRUE;
406:       is->info[itype][IS_PERMUTATION] = IS_INFO_TRUE;
407:       is->info[itype][IS_INTERVAL]    = IS_INFO_TRUE;
408:       is->info[IS_LOCAL][IS_INTERVAL] = IS_INFO_TRUE;
409:       if (permanent_set && permanent) {
410:         is->info_permanent[itype][IS_SORTED]      = PETSC_TRUE;
411:         is->info_permanent[IS_LOCAL][IS_SORTED]   = PETSC_TRUE;
412:         is->info_permanent[itype][IS_UNIQUE]      = PETSC_TRUE;
413:         is->info_permanent[IS_LOCAL][IS_UNIQUE]   = PETSC_TRUE;
414:         is->info_permanent[itype][IS_PERMUTATION] = PETSC_TRUE;
415:         is->info_permanent[itype][IS_INTERVAL]    = PETSC_TRUE;
416:         is->info_permanent[IS_LOCAL][IS_INTERVAL] = PETSC_TRUE;
417:       }
418:     }
419:     break;
420:   default:
421:     PetscCheck(type != IS_LOCAL, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unknown IS property");
422:     SETERRQ(PetscObjectComm((PetscObject)is), PETSC_ERR_ARG_OUTOFRANGE, "Unknown IS property");
423:   }
424:   PetscFunctionReturn(PETSC_SUCCESS);
425: }

427: // PetscClangLinter pragma disable: -fdoc-section-header-unknown
428: /*@
429:   ISSetInfo - Set known information about an index set.

431:   Logically Collective if `ISInfoType` is `IS_GLOBAL`

433:   Input Parameters:
434: + is        - the index set
435: . info      - describing a property of the index set, one of those listed below,
436: . type      - `IS_LOCAL` if the information describes the local portion of the index set,
437:           `IS_GLOBAL` if it describes the whole index set
438: . permanent - `PETSC_TRUE` if it is known that the property will persist through changes to the index set, `PETSC_FALSE` otherwise
439:                If the user sets a property as permanently known, it will bypass computation of that property
440: - flg       - set the described property as true (`PETSC_TRUE`) or false (`PETSC_FALSE`)

442:   Values of `info` Describing `IS` Structure:
443: + `IS_SORTED`      - the [local part of the] index set is sorted in ascending order
444: . `IS_UNIQUE`      - each entry in the [local part of the] index set is unique
445: . `IS_PERMUTATION` - the [local part of the] index set is a permutation of the integers {0, 1, ..., N-1}, where N is the size of the [local part of the] index set
446: . `IS_INTERVAL`    - the [local part of the] index set is equal to a contiguous range of integers {f, f + 1, ..., f + N-1}
447: - `IS_IDENTITY`    - the [local part of the] index set is equal to the integers {0, 1, ..., N-1}

449:   Level: advanced

451:   Notes:
452:   If type is `IS_GLOBAL`, all processes that share the index set must pass the same value in flg

454:   It is possible to set a property with `ISSetInfo()` that contradicts what would be previously computed with `ISGetInfo()`

456: .seealso: `ISInfo`, `ISInfoType`, `IS`
457: @*/
458: PetscErrorCode ISSetInfo(IS is, ISInfo info, ISInfoType type, PetscBool permanent, PetscBool flg)
459: {
460:   MPI_Comm    comm, errcomm;
461:   PetscMPIInt size;

463:   PetscFunctionBegin;
466:   comm = PetscObjectComm((PetscObject)is);
467:   if (type == IS_GLOBAL) {
471:     errcomm = comm;
472:   } else {
473:     errcomm = PETSC_COMM_SELF;
474:   }

476:   PetscCheck((int)info > IS_INFO_MIN && (int)info < IS_INFO_MAX, errcomm, PETSC_ERR_ARG_OUTOFRANGE, "Option %d is out of range", (int)info);

478:   PetscCallMPI(MPI_Comm_size(comm, &size));
479:   /* do not use global values if size == 1: it makes it easier to keep the implications straight */
480:   if (size == 1) type = IS_LOCAL;
481:   PetscCall(ISSetInfo_Internal(is, info, type, permanent ? IS_INFO_TRUE : IS_INFO_FALSE, flg));
482:   PetscFunctionReturn(PETSC_SUCCESS);
483: }

485: static PetscErrorCode ISGetInfo_Sorted_Private(IS is, ISInfoType type, PetscBool *flg)
486: {
487:   MPI_Comm    comm;
488:   PetscMPIInt size, rank;

490:   PetscFunctionBegin;
491:   comm = PetscObjectComm((PetscObject)is);
492:   PetscCallMPI(MPI_Comm_size(comm, &size));
493:   PetscCallMPI(MPI_Comm_rank(comm, &rank));
494:   if (type == IS_GLOBAL && is->ops->sortedglobal) {
495:     PetscUseTypeMethod(is, sortedglobal, flg);
496:   } else {
497:     /* determine if the array is locally sorted */
498:     *flg = PETSC_FALSE;
499:     if (type == IS_GLOBAL && size > 1) {
500:       /* call ISGetInfo so that a cached value will be used if possible */
501:       PetscCall(ISGetInfo(is, IS_SORTED, IS_LOCAL, PETSC_TRUE, flg));
502:     } else if (is->ops->sortedlocal) {
503:       PetscUseTypeMethod(is, sortedlocal, flg);
504:     } else {
505:       /* default: get the local indices and directly check */
506:       const PetscInt *idx;
507:       PetscInt        n;

509:       PetscCall(ISGetIndices(is, &idx));
510:       PetscCall(ISGetLocalSize(is, &n));
511:       PetscCall(PetscSortedInt(n, idx, flg));
512:       PetscCall(ISRestoreIndices(is, &idx));
513:     }

515:     if (type == IS_GLOBAL && size > 1) {
516:       PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, flg, 1, MPI_C_BOOL, MPI_LAND, comm));
517:       if (*flg) {
518:         PetscInt n, min = PETSC_INT_MAX, max = PETSC_INT_MIN;
519:         PetscInt maxprev;

521:         PetscCall(ISGetLocalSize(is, &n));
522:         if (n) PetscCall(ISGetMinMax(is, &min, &max));
523:         maxprev = PETSC_INT_MIN;
524:         PetscCallMPI(MPI_Exscan(&max, &maxprev, 1, MPIU_INT, MPI_MAX, comm));
525:         if (rank && (maxprev > min)) *flg = PETSC_FALSE;
526:         PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, flg, 1, MPI_C_BOOL, MPI_LAND, comm));
527:       }
528:     }
529:   }
530:   PetscFunctionReturn(PETSC_SUCCESS);
531: }

533: static PetscErrorCode ISGetIndicesCopy_Private(IS is, PetscInt idx[]);

535: static PetscErrorCode ISGetInfo_Unique_Private(IS is, ISInfoType type, PetscBool *flg)
536: {
537:   MPI_Comm    comm;
538:   PetscMPIInt size, rank;
539:   PetscInt    i;

541:   PetscFunctionBegin;
542:   comm = PetscObjectComm((PetscObject)is);
543:   PetscCallMPI(MPI_Comm_size(comm, &size));
544:   PetscCallMPI(MPI_Comm_rank(comm, &rank));
545:   if (type == IS_GLOBAL && is->ops->uniqueglobal) PetscUseTypeMethod(is, uniqueglobal, flg);
546:   else {
547:     PetscInt  n   = -1;
548:     PetscInt *idx = NULL;

550:     /* determine if the array is locally unique */
551:     if (type == IS_GLOBAL && size > 1) {
552:       /* call ISGetInfo so that a cached value will be used if possible */
553:       PetscCall(ISGetInfo(is, IS_UNIQUE, IS_LOCAL, PETSC_TRUE, flg));
554:     } else if (is->ops->uniquelocal) {
555:       PetscUseTypeMethod(is, uniquelocal, flg);
556:     } else {
557:       /* default: get the local indices and directly check */
558:       *flg = PETSC_TRUE;
559:       PetscCall(ISGetLocalSize(is, &n));
560:       PetscCall(PetscMalloc1(n, &idx));
561:       PetscCall(ISGetIndicesCopy_Private(is, idx));
562:       PetscCall(PetscIntSortSemiOrdered(n, idx));
563:       for (i = 1; i < n; i++)
564:         if (idx[i] == idx[i - 1]) break;
565:       if (i < n) *flg = PETSC_FALSE;
566:     }

568:     PetscCall(PetscFree(idx));
569:     if (type == IS_GLOBAL && size > 1) {
570:       PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, flg, 1, MPI_C_BOOL, MPI_LAND, comm));
571:       if (*flg) {
572:         PetscInt min = PETSC_INT_MAX, max = PETSC_INT_MIN, maxprev;

574:         if (!idx) {
575:           PetscCall(ISGetLocalSize(is, &n));
576:           PetscCall(PetscMalloc1(n, &idx));
577:           PetscCall(ISGetIndicesCopy_Private(is, idx));
578:         }
579:         PetscCall(PetscParallelSortInt(is->map, is->map, idx, idx));
580:         if (n) {
581:           min = idx[0];
582:           max = idx[n - 1];
583:         }
584:         for (i = 1; i < n; i++)
585:           if (idx[i] == idx[i - 1]) break;
586:         if (i < n) *flg = PETSC_FALSE;
587:         maxprev = PETSC_INT_MIN;
588:         PetscCallMPI(MPI_Exscan(&max, &maxprev, 1, MPIU_INT, MPI_MAX, comm));
589:         if (rank && (maxprev == min)) *flg = PETSC_FALSE;
590:         PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, flg, 1, MPI_C_BOOL, MPI_LAND, comm));
591:       }
592:     }
593:     PetscCall(PetscFree(idx));
594:   }
595:   PetscFunctionReturn(PETSC_SUCCESS);
596: }

598: static PetscErrorCode ISGetInfo_Permutation(IS is, ISInfoType type, PetscBool *flg)
599: {
600:   MPI_Comm    comm;
601:   PetscMPIInt size;

603:   PetscFunctionBegin;
604:   comm = PetscObjectComm((PetscObject)is);
605:   PetscCallMPI(MPI_Comm_size(comm, &size));
606:   if (type == IS_GLOBAL && is->ops->permglobal) {
607:     PetscUseTypeMethod(is, permglobal, flg);
608:   } else if (type == IS_LOCAL && is->ops->permlocal) {
609:     PetscUseTypeMethod(is, permlocal, flg);
610:   } else {
611:     PetscInt  n, i, rStart;
612:     PetscInt *idx;

614:     PetscCall(ISGetLocalSize(is, &n));
615:     PetscCall(PetscMalloc1(n, &idx));
616:     PetscCall(ISGetIndicesCopy_Private(is, idx));
617:     if (type == IS_GLOBAL) {
618:       PetscCall(PetscParallelSortInt(is->map, is->map, idx, idx));
619:       PetscCall(PetscLayoutGetRange(is->map, &rStart, NULL));
620:     } else {
621:       PetscCall(PetscIntSortSemiOrdered(n, idx));
622:       rStart = 0;
623:     }
624:     *flg = PETSC_TRUE;
625:     for (i = 0; i < n; i++) {
626:       if (idx[i] != rStart + i) break;
627:     }
628:     if (i < n) *flg = PETSC_FALSE;
629:     if (type == IS_GLOBAL && size > 1) PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, flg, 1, MPI_C_BOOL, MPI_LAND, comm));
630:     PetscCall(PetscFree(idx));
631:   }
632:   PetscFunctionReturn(PETSC_SUCCESS);
633: }

635: static PetscErrorCode ISGetInfo_Interval(IS is, ISInfoType type, PetscBool *flg)
636: {
637:   MPI_Comm    comm;
638:   PetscMPIInt size, rank;
639:   PetscInt    i;

641:   PetscFunctionBegin;
642:   comm = PetscObjectComm((PetscObject)is);
643:   PetscCallMPI(MPI_Comm_size(comm, &size));
644:   PetscCallMPI(MPI_Comm_rank(comm, &rank));
645:   if (type == IS_GLOBAL && is->ops->intervalglobal) {
646:     PetscUseTypeMethod(is, intervalglobal, flg);
647:   } else {
648:     /* determine if the array is locally an interval */
649:     if (type == IS_GLOBAL && size > 1) {
650:       /* call ISGetInfo so that a cached value will be used if possible */
651:       PetscCall(ISGetInfo(is, IS_INTERVAL, IS_LOCAL, PETSC_TRUE, flg));
652:     } else if (is->ops->intervallocal) {
653:       PetscUseTypeMethod(is, intervallocal, flg);
654:     } else {
655:       PetscInt        n;
656:       const PetscInt *idx;
657:       /* default: get the local indices and directly check */
658:       *flg = PETSC_TRUE;
659:       PetscCall(ISGetLocalSize(is, &n));
660:       PetscCall(ISGetIndices(is, &idx));
661:       for (i = 1; i < n; i++)
662:         if (idx[i] != idx[i - 1] + 1) break;
663:       if (i < n) *flg = PETSC_FALSE;
664:       PetscCall(ISRestoreIndices(is, &idx));
665:     }

667:     if (type == IS_GLOBAL && size > 1) {
668:       PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, flg, 1, MPI_C_BOOL, MPI_LAND, comm));
669:       if (*flg) {
670:         PetscInt n, min = PETSC_INT_MAX, max = PETSC_INT_MIN;
671:         PetscInt maxprev;

673:         PetscCall(ISGetLocalSize(is, &n));
674:         if (n) PetscCall(ISGetMinMax(is, &min, &max));
675:         maxprev = PETSC_INT_MIN;
676:         PetscCallMPI(MPI_Exscan(&max, &maxprev, 1, MPIU_INT, MPI_MAX, comm));
677:         if (rank && n && (maxprev != min - 1)) *flg = PETSC_FALSE;
678:         PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, flg, 1, MPI_C_BOOL, MPI_LAND, comm));
679:       }
680:     }
681:   }
682:   PetscFunctionReturn(PETSC_SUCCESS);
683: }

685: static PetscErrorCode ISGetInfo_Identity(IS is, ISInfoType type, PetscBool *flg)
686: {
687:   MPI_Comm    comm;
688:   PetscMPIInt size;

690:   PetscFunctionBegin;
691:   comm = PetscObjectComm((PetscObject)is);
692:   PetscCallMPI(MPI_Comm_size(comm, &size));
693:   if (type == IS_GLOBAL && is->ops->intervalglobal) {
694:     PetscBool isinterval;

696:     PetscUseTypeMethod(is, intervalglobal, &isinterval);
697:     *flg = PETSC_FALSE;
698:     if (isinterval) {
699:       PetscInt min;

701:       PetscCall(ISGetMinMax(is, &min, NULL));
702:       PetscCallMPI(MPI_Bcast(&min, 1, MPIU_INT, 0, comm));
703:       if (min == 0) *flg = PETSC_TRUE;
704:     }
705:   } else if (type == IS_LOCAL && is->ops->intervallocal) {
706:     PetscBool isinterval;

708:     PetscUseTypeMethod(is, intervallocal, &isinterval);
709:     *flg = PETSC_FALSE;
710:     if (isinterval) {
711:       PetscInt min;

713:       PetscCall(ISGetMinMax(is, &min, NULL));
714:       if (min == 0) *flg = PETSC_TRUE;
715:     }
716:   } else {
717:     PetscInt        n, i, rStart;
718:     const PetscInt *idx;

720:     PetscCall(ISGetLocalSize(is, &n));
721:     PetscCall(ISGetIndices(is, &idx));
722:     PetscCall(PetscLayoutGetRange(is->map, &rStart, NULL));
723:     *flg = PETSC_TRUE;
724:     for (i = 0; i < n; i++) {
725:       if (idx[i] != rStart + i) break;
726:     }
727:     if (i < n) *flg = PETSC_FALSE;
728:     if (type == IS_GLOBAL && size > 1) PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, flg, 1, MPI_C_BOOL, MPI_LAND, comm));
729:     PetscCall(ISRestoreIndices(is, &idx));
730:   }
731:   PetscFunctionReturn(PETSC_SUCCESS);
732: }

734: /*@
735:   ISGetInfo - Determine whether an index set satisfies a given property

737:   Collective or Logically Collective if the type is `IS_GLOBAL` (logically collective if the value of the property has been permanently set with `ISSetInfo()`)

739:   Input Parameters:
740: + is      - the index set
741: . info    - describing a property of the index set, one of those listed in the documentation of `ISSetInfo()`
742: . compute - if `PETSC_FALSE`, the property will not be computed if it is not already known and the property will be assumed to be false
743: - type    - whether the property is local (`IS_LOCAL`) or global (`IS_GLOBAL`)

745:   Output Parameter:
746: . flg - whether the property is true (`PETSC_TRUE`) or false (`PETSC_FALSE`)

748:   Level: advanced

750:   Notes:
751:   `ISGetInfo()` uses cached values when possible, which will be incorrect if `ISSetInfo()` has been called with incorrect information.

753:   To clear cached values, use `ISClearInfoCache()`.

755: .seealso: `IS`, `ISInfo`, `ISInfoType`, `ISSetInfo()`, `ISClearInfoCache()`
756: @*/
757: PetscErrorCode ISGetInfo(IS is, ISInfo info, ISInfoType type, PetscBool compute, PetscBool *flg)
758: {
759:   MPI_Comm    comm, errcomm;
760:   PetscMPIInt rank, size;
761:   PetscInt    itype;
762:   PetscBool   hasprop;
763:   PetscBool   infer;

765:   PetscFunctionBegin;
768:   comm = PetscObjectComm((PetscObject)is);
769:   if (type == IS_GLOBAL) {
771:     errcomm = comm;
772:   } else {
773:     errcomm = PETSC_COMM_SELF;
774:   }

776:   PetscCallMPI(MPI_Comm_size(comm, &size));
777:   PetscCallMPI(MPI_Comm_rank(comm, &rank));

779:   PetscCheck((int)info > IS_INFO_MIN && (int)info < IS_INFO_MAX, errcomm, PETSC_ERR_ARG_OUTOFRANGE, "Option %d is out of range", (int)info);
780:   if (size == 1) type = IS_LOCAL;
781:   itype   = (type == IS_LOCAL) ? 0 : 1;
782:   hasprop = PETSC_FALSE;
783:   infer   = PETSC_FALSE;
784:   if (is->info_permanent[itype][(int)info]) {
785:     hasprop = (is->info[itype][(int)info] == IS_INFO_TRUE) ? PETSC_TRUE : PETSC_FALSE;
786:     infer   = PETSC_TRUE;
787:   } else if (itype == IS_LOCAL && is->info[IS_LOCAL][info] != IS_INFO_UNKNOWN) {
788:     /* we can cache local properties as long as we clear them when the IS changes */
789:     /* NOTE: we only cache local values because there is no ISAssemblyBegin()/ISAssemblyEnd(),
790:      so we have no way of knowing when a cached value has been invalidated by changes on a different process */
791:     hasprop = (is->info[itype][(int)info] == IS_INFO_TRUE) ? PETSC_TRUE : PETSC_FALSE;
792:     infer   = PETSC_TRUE;
793:   } else if (compute) {
794:     switch (info) {
795:     case IS_SORTED:
796:       PetscCall(ISGetInfo_Sorted_Private(is, type, &hasprop));
797:       break;
798:     case IS_UNIQUE:
799:       PetscCall(ISGetInfo_Unique_Private(is, type, &hasprop));
800:       break;
801:     case IS_PERMUTATION:
802:       PetscCall(ISGetInfo_Permutation(is, type, &hasprop));
803:       break;
804:     case IS_INTERVAL:
805:       PetscCall(ISGetInfo_Interval(is, type, &hasprop));
806:       break;
807:     case IS_IDENTITY:
808:       PetscCall(ISGetInfo_Identity(is, type, &hasprop));
809:       break;
810:     default:
811:       SETERRQ(errcomm, PETSC_ERR_ARG_OUTOFRANGE, "Unknown IS property");
812:     }
813:     infer = PETSC_TRUE;
814:   }
815:   /* call ISSetInfo_Internal to keep all of the implications straight */
816:   if (infer) PetscCall(ISSetInfo_Internal(is, info, type, IS_INFO_UNKNOWN, hasprop));
817:   *flg = hasprop;
818:   PetscFunctionReturn(PETSC_SUCCESS);
819: }

821: static PetscErrorCode ISCopyInfo_Private(IS source, IS dest)
822: {
823:   PetscFunctionBegin;
824:   PetscCall(PetscArraycpy(&dest->info[0], &source->info[0], 2));
825:   PetscCall(PetscArraycpy(&dest->info_permanent[0], &source->info_permanent[0], 2));
826:   PetscFunctionReturn(PETSC_SUCCESS);
827: }

829: /*@
830:   ISIdentity - Determines whether index set is the identity mapping.

832:   Collective

834:   Input Parameter:
835: . is - the index set

837:   Output Parameter:
838: . ident - `PETSC_TRUE` if an identity, else `PETSC_FALSE`

840:   Level: intermediate

842:   Note:
843:   If `ISSetIdentity()` (or `ISSetInfo()` for a permanent property) has been called,
844:   `ISIdentity()` will return its answer without communication between processes, but
845:   otherwise the output ident will be computed from `ISGetInfo()`,
846:   which may require synchronization on the communicator of `is`.  To avoid this computation,
847:   call `ISGetInfo()` directly with the compute flag set to `PETSC_FALSE`, and ident will be assumed false.

849: .seealso: `IS`, `ISSetIdentity()`, `ISGetInfo()`
850: @*/
851: PetscErrorCode ISIdentity(IS is, PetscBool *ident)
852: {
853:   PetscFunctionBegin;
855:   PetscAssertPointer(ident, 2);
856:   PetscCall(ISGetInfo(is, IS_IDENTITY, IS_GLOBAL, PETSC_TRUE, ident));
857:   PetscFunctionReturn(PETSC_SUCCESS);
858: }

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

863:   Logically Collective

865:   Input Parameter:
866: . is - the index set

868:   Level: intermediate

870:   Notes:
871:   `is` will be considered the identity permanently, even if indices have been changes (for example, with
872:   `ISGeneralSetIndices()`).  It's a good idea to only set this property if `is` will not change in the future.

874:   To clear this property, use `ISClearInfoCache()`.

876:   Developer Notes:
877:   Some of these info routines have statements about values changing in the `IS`, this seems to contradict the fact that `IS` cannot be changed?

879: .seealso: `IS`, `ISIdentity()`, `ISSetInfo()`, `ISClearInfoCache()`
880: @*/
881: PetscErrorCode ISSetIdentity(IS is)
882: {
883:   PetscFunctionBegin;
885:   PetscCall(ISSetInfo(is, IS_IDENTITY, IS_GLOBAL, PETSC_TRUE, PETSC_TRUE));
886:   PetscFunctionReturn(PETSC_SUCCESS);
887: }

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

892:   Not Collective

894:   Input Parameters:
895: + is     - the index set
896: . gstart - global start
897: - gend   - global end

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

903:   Level: developer

905: .seealso: `IS`, `ISGetLocalSize()`, `VecGetOwnershipRange()`
906: @*/
907: PetscErrorCode ISContiguousLocal(IS is, PetscInt gstart, PetscInt gend, PetscInt *start, PetscBool *contig)
908: {
909:   PetscFunctionBegin;
911:   PetscAssertPointer(start, 4);
912:   PetscAssertPointer(contig, 5);
913:   PetscCheck(gstart <= gend, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "gstart %" PetscInt_FMT " must be less than or equal to gend %" PetscInt_FMT, gstart, gend);
914:   *start  = -1;
915:   *contig = PETSC_FALSE;
916:   PetscTryTypeMethod(is, contiguous, gstart, gend, start, contig);
917:   PetscFunctionReturn(PETSC_SUCCESS);
918: }

920: /*@
921:   ISPermutation - `PETSC_TRUE` or `PETSC_FALSE` depending on whether the
922:   index set has been declared to be a permutation.

924:   Logically Collective

926:   Input Parameter:
927: . is - the index set

929:   Output Parameter:
930: . perm - `PETSC_TRUE` if a permutation, else `PETSC_FALSE`

932:   Level: intermediate

934:   Note:
935:   If it is not already known that `is` is a permutation (if `ISSetPermutation()`
936:   or `ISSetInfo()` has not been called), this routine will not attempt to compute
937:   whether the index set is a permutation and will assume `perm` is `PETSC_FALSE`.
938:   To compute the value when it is not already known, use `ISGetInfo()` with
939:   the compute flag set to `PETSC_TRUE`.

941:   Developer Notes:
942:   Perhaps some of these routines should use the `PetscBool3` enum to return appropriate values

944: .seealso: `IS`, `ISSetPermutation()`, `ISGetInfo()`
945: @*/
946: PetscErrorCode ISPermutation(IS is, PetscBool *perm)
947: {
948:   PetscFunctionBegin;
950:   PetscAssertPointer(perm, 2);
951:   PetscCall(ISGetInfo(is, IS_PERMUTATION, IS_GLOBAL, PETSC_FALSE, perm));
952:   PetscFunctionReturn(PETSC_SUCCESS);
953: }

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

958:   Logically Collective

960:   Input Parameter:
961: . is - the index set

963:   Level: intermediate

965:   Notes:
966:   `is` will be considered a permutation permanently, even if indices have been changes (for example, with
967:   `ISGeneralSetIndices()`).  It's a good idea to only set this property if `is` will not change in the future.

969:   To clear this property, use `ISClearInfoCache()`.

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

974: .seealso: `IS`, `ISPermutation()`, `ISSetInfo()`, `ISClearInfoCache().`
975: @*/
976: PetscErrorCode ISSetPermutation(IS is)
977: {
978:   PetscFunctionBegin;
980:   if (PetscDefined(USE_DEBUG)) {
981:     PetscMPIInt size;

983:     PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)is), &size));
984:     if (size == 1) {
985:       PetscInt        i, n, *idx;
986:       const PetscInt *iidx;

988:       PetscCall(ISGetSize(is, &n));
989:       PetscCall(PetscMalloc1(n, &idx));
990:       PetscCall(ISGetIndices(is, &iidx));
991:       PetscCall(PetscArraycpy(idx, iidx, n));
992:       PetscCall(PetscIntSortSemiOrdered(n, idx));
993:       for (i = 0; i < n; i++) PetscCheck(idx[i] == i, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Index set is not a permutation");
994:       PetscCall(PetscFree(idx));
995:       PetscCall(ISRestoreIndices(is, &iidx));
996:     }
997:   }
998:   PetscCall(ISSetInfo(is, IS_PERMUTATION, IS_GLOBAL, PETSC_TRUE, PETSC_TRUE));
999:   PetscFunctionReturn(PETSC_SUCCESS);
1000: }

1002: /*@
1003:   ISDestroy - Destroys an index set.

1005:   Collective

1007:   Input Parameter:
1008: . is - the index set

1010:   Level: beginner

1012: .seealso: `IS`, `ISCreateGeneral()`, `ISCreateStride()`, `ISCreateBlock()`
1013: @*/
1014: PetscErrorCode ISDestroy(IS *is)
1015: {
1016:   PetscFunctionBegin;
1017:   if (!*is) PetscFunctionReturn(PETSC_SUCCESS);
1019:   if (--((PetscObject)*is)->refct > 0) {
1020:     *is = NULL;
1021:     PetscFunctionReturn(PETSC_SUCCESS);
1022:   }
1023:   if ((*is)->complement) {
1024:     PetscInt refcnt;
1025:     PetscCall(PetscObjectGetReference((PetscObject)((*is)->complement), &refcnt));
1026:     PetscCheck(refcnt <= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Nonlocal IS has not been restored");
1027:     PetscCall(ISDestroy(&(*is)->complement));
1028:   }
1029:   PetscTryTypeMethod(*is, destroy);
1030:   PetscCall(PetscLayoutDestroy(&(*is)->map));
1031:   /* Destroy local representations of offproc data. */
1032:   PetscCall(PetscFree((*is)->total));
1033:   PetscCall(PetscFree((*is)->nonlocal));
1034:   PetscCall(PetscHeaderDestroy(is));
1035:   PetscFunctionReturn(PETSC_SUCCESS);
1036: }

1038: /*@
1039:   ISInvertPermutation - Creates a new permutation that is the inverse of
1040:   a given permutation.

1042:   Collective

1044:   Input Parameters:
1045: + is     - the index set
1046: - nlocal - number of indices on this processor in result (ignored for 1 processor) or
1047:             use `PETSC_DECIDE`

1049:   Output Parameter:
1050: . isout - the inverse permutation

1052:   Level: intermediate

1054:   Note:
1055:   For parallel index sets this does the complete parallel permutation, but the
1056:   code is not efficient for huge index sets (10,000,000 indices).

1058: .seealso: `IS`, `ISGetInfo()`, `ISSetPermutation()`, `ISGetPermutation()`
1059: @*/
1060: PetscErrorCode ISInvertPermutation(IS is, PetscInt nlocal, IS *isout)
1061: {
1062:   PetscBool isperm, isidentity, issame;

1064:   PetscFunctionBegin;
1066:   PetscAssertPointer(isout, 3);
1067:   PetscCall(ISGetInfo(is, IS_PERMUTATION, IS_GLOBAL, PETSC_TRUE, &isperm));
1068:   PetscCheck(isperm, PetscObjectComm((PetscObject)is), PETSC_ERR_ARG_WRONG, "Not a permutation");
1069:   PetscCall(ISGetInfo(is, IS_IDENTITY, IS_GLOBAL, PETSC_TRUE, &isidentity));
1070:   issame = PETSC_FALSE;
1071:   if (isidentity) {
1072:     PetscInt n;

1074:     PetscCall(ISGetLocalSize(is, &n));
1075:     issame = (PetscBool)(n == nlocal);
1076:     PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &issame, 1, MPI_C_BOOL, MPI_LAND, PetscObjectComm((PetscObject)is)));
1077:   }
1078:   if (issame) PetscCall(ISDuplicate(is, isout));
1079:   else {
1080:     PetscUseTypeMethod(is, invertpermutation, nlocal, isout);
1081:     PetscCall(ISSetPermutation(*isout));
1082:   }
1083:   PetscFunctionReturn(PETSC_SUCCESS);
1084: }

1086: /*@
1087:   ISGetSize - Returns the global length of an index set.

1089:   Not Collective

1091:   Input Parameter:
1092: . is - the index set

1094:   Output Parameter:
1095: . size - the global size

1097:   Level: beginner

1099: .seealso: `IS`
1100: @*/
1101: PetscErrorCode ISGetSize(IS is, PetscInt *size)
1102: {
1103:   PetscFunctionBegin;
1105:   PetscAssertPointer(size, 2);
1106:   *size = is->map->N;
1107:   PetscFunctionReturn(PETSC_SUCCESS);
1108: }

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

1113:   Not Collective

1115:   Input Parameter:
1116: . is - the index set

1118:   Output Parameter:
1119: . size - the local size

1121:   Level: beginner

1123: .seealso: `IS`, `ISGetSize()`
1124: @*/
1125: PetscErrorCode ISGetLocalSize(IS is, PetscInt *size)
1126: {
1127:   PetscFunctionBegin;
1129:   PetscAssertPointer(size, 2);
1130:   *size = is->map->n;
1131:   PetscFunctionReturn(PETSC_SUCCESS);
1132: }

1134: /*@
1135:   ISGetLayout - get `PetscLayout` describing index set layout

1137:   Not Collective

1139:   Input Parameter:
1140: . is - the index set

1142:   Output Parameter:
1143: . map - the layout

1145:   Level: developer

1147: .seealso: `IS`, `PetscLayout`, `ISSetLayout()`, `ISGetSize()`, `ISGetLocalSize()`
1148: @*/
1149: PetscErrorCode ISGetLayout(IS is, PetscLayout *map)
1150: {
1151:   PetscFunctionBegin;
1153:   PetscAssertPointer(map, 2);
1154:   *map = is->map;
1155:   PetscFunctionReturn(PETSC_SUCCESS);
1156: }

1158: /*@
1159:   ISSetLayout - set `PetscLayout` describing index set layout

1161:   Collective

1163:   Input Parameters:
1164: + is  - the index set
1165: - map - the layout

1167:   Level: developer

1169:   Notes:
1170:   Users should typically use higher level functions such as `ISCreateGeneral()`.

1172:   This function can be useful in some special cases of constructing a new `IS`, e.g. after `ISCreate()` and before `ISLoad()`.
1173:   Otherwise, it is only valid to replace the layout with a layout known to be equivalent.

1175: .seealso: `IS`, `PetscLayout`, `ISCreate()`, `ISGetLayout()`, `ISGetSize()`, `ISGetLocalSize()`
1176: @*/
1177: PetscErrorCode ISSetLayout(IS is, PetscLayout map)
1178: {
1179:   PetscFunctionBegin;
1181:   PetscAssertPointer(map, 2);
1182:   PetscCall(PetscLayoutReference(map, &is->map));
1183:   PetscFunctionReturn(PETSC_SUCCESS);
1184: }

1186: /*@
1187:   ISGetIndices - Returns a pointer to the indices.  The user should call
1188:   `ISRestoreIndices()` after having looked at the indices.  The user should
1189:   NOT change the indices.

1191:   Not Collective

1193:   Input Parameter:
1194: . is - the index set

1196:   Output Parameter:
1197: . ptr - the location to put the pointer to the indices

1199:   Level: intermediate

1201:   Fortran Note:
1202: .vb
1203:   PetscInt, pointer :: ptr(:)
1204: .ve

1206: .seealso: `IS`, `ISRestoreIndices()`
1207: @*/
1208: PetscErrorCode ISGetIndices(IS is, const PetscInt *ptr[])
1209: {
1210:   PetscFunctionBegin;
1212:   PetscAssertPointer(ptr, 2);
1213:   PetscUseTypeMethod(is, getindices, ptr);
1214:   PetscFunctionReturn(PETSC_SUCCESS);
1215: }

1217: /*@
1218:   ISGetMinMax - Gets the minimum and maximum values in an `IS`

1220:   Not Collective

1222:   Input Parameter:
1223: . is - the index set

1225:   Output Parameters:
1226: + min - the minimum value, you may pass `NULL`
1227: - max - the maximum value, you may pass `NULL`

1229:   Level: intermediate

1231:   Notes:
1232:   Empty index sets return min=`PETSC_INT_MAX` and max=`PETSC_INT_MIN`.

1234:   In parallel, it returns the `min` and `max` of the local portion of `is`

1236: .seealso: `IS`, `ISGetIndices()`, `ISRestoreIndices()`
1237: @*/
1238: PetscErrorCode ISGetMinMax(IS is, PetscInt *min, PetscInt *max)
1239: {
1240:   PetscFunctionBegin;
1242:   if (min) *min = is->min;
1243:   if (max) *max = is->max;
1244:   PetscFunctionReturn(PETSC_SUCCESS);
1245: }

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

1250:   Not Collective

1252:   Input Parameters:
1253: + is  - the index set
1254: - key - the search key

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

1259:   Level: intermediate

1261: .seealso: `IS`
1262:  @*/
1263: PetscErrorCode ISLocate(IS is, PetscInt key, PetscInt *location)
1264: {
1265:   PetscFunctionBegin;
1266:   if (is->ops->locate) PetscUseTypeMethod(is, locate, key, location);
1267:   else {
1268:     PetscInt        numIdx;
1269:     PetscBool       sorted;
1270:     const PetscInt *idx;

1272:     PetscCall(ISGetLocalSize(is, &numIdx));
1273:     PetscCall(ISGetIndices(is, &idx));
1274:     PetscCall(ISSorted(is, &sorted));
1275:     if (sorted) {
1276:       PetscCall(PetscFindInt(key, numIdx, idx, location));
1277:     } else {
1278:       *location = -1;
1279:       for (PetscInt i = 0; i < numIdx; i++) {
1280:         if (idx[i] == key) {
1281:           *location = i;
1282:           break;
1283:         }
1284:       }
1285:     }
1286:     PetscCall(ISRestoreIndices(is, &idx));
1287:   }
1288:   PetscFunctionReturn(PETSC_SUCCESS);
1289: }

1291: /*@
1292:   ISRestoreIndices - Restores an index set to a usable state after a call to `ISGetIndices()`.

1294:   Not Collective

1296:   Input Parameters:
1297: + is  - the index set
1298: - ptr - the pointer obtained by `ISGetIndices()`

1300:   Level: intermediate

1302:   Fortran Note:
1303: .vb
1304:   PetscInt, pointer :: ptr(:)
1305: .ve

1307: .seealso: `IS`, `ISGetIndices()`
1308: @*/
1309: PetscErrorCode ISRestoreIndices(IS is, const PetscInt *ptr[])
1310: {
1311:   PetscFunctionBegin;
1313:   PetscAssertPointer(ptr, 2);
1314:   PetscTryTypeMethod(is, restoreindices, ptr);
1315:   PetscFunctionReturn(PETSC_SUCCESS);
1316: }

1318: static PetscErrorCode ISGatherTotal_Private(IS is)
1319: {
1320:   PetscInt        i, n, N;
1321:   const PetscInt *lindices;
1322:   MPI_Comm        comm;
1323:   PetscMPIInt     rank, size, *sizes = NULL, *offsets = NULL, nn;

1325:   PetscFunctionBegin;

1328:   PetscCall(PetscObjectGetComm((PetscObject)is, &comm));
1329:   PetscCallMPI(MPI_Comm_size(comm, &size));
1330:   PetscCallMPI(MPI_Comm_rank(comm, &rank));
1331:   PetscCall(ISGetLocalSize(is, &n));
1332:   PetscCall(PetscMalloc2(size, &sizes, size, &offsets));

1334:   PetscCall(PetscMPIIntCast(n, &nn));
1335:   PetscCallMPI(MPI_Allgather(&nn, 1, MPI_INT, sizes, 1, MPI_INT, comm));
1336:   offsets[0] = 0;
1337:   for (i = 1; i < size; ++i) offsets[i] = offsets[i - 1] + sizes[i - 1];
1338:   N = offsets[size - 1] + sizes[size - 1];

1340:   PetscCall(PetscMalloc1(N, &is->total));
1341:   PetscCall(ISGetIndices(is, &lindices));
1342:   PetscCallMPI(MPI_Allgatherv((void *)lindices, nn, MPIU_INT, is->total, sizes, offsets, MPIU_INT, comm));
1343:   PetscCall(ISRestoreIndices(is, &lindices));
1344:   is->local_offset = offsets[rank];
1345:   PetscCall(PetscFree2(sizes, offsets));
1346:   PetscFunctionReturn(PETSC_SUCCESS);
1347: }

1349: /*@
1350:   ISGetTotalIndices - Retrieve an array containing all indices across the communicator.

1352:   Collective

1354:   Input Parameter:
1355: . is - the index set

1357:   Output Parameter:
1358: . indices - total indices with rank 0 indices first, and so on; total array size is
1359:              the same as returned with `ISGetSize()`.

1361:   Level: intermediate

1363:   Notes:
1364:   this is potentially nonscalable, but depends on the size of the total index set
1365:   and the size of the communicator. This may be feasible for index sets defined on
1366:   subcommunicators, such that the set size does not grow with `PETSC_WORLD_COMM`.
1367:   Note also that there is no way to tell where the local part of the indices starts
1368:   (use `ISGetIndices()` and `ISGetNonlocalIndices()` to retrieve just the local and just
1369:   the nonlocal part (complement), respectively).

1371: .seealso: `IS`, `ISRestoreTotalIndices()`, `ISGetNonlocalIndices()`, `ISGetSize()`
1372: @*/
1373: PetscErrorCode ISGetTotalIndices(IS is, const PetscInt *indices[])
1374: {
1375:   PetscMPIInt size;

1377:   PetscFunctionBegin;
1379:   PetscAssertPointer(indices, 2);
1380:   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)is), &size));
1381:   if (size == 1) PetscUseTypeMethod(is, getindices, indices);
1382:   else {
1383:     if (!is->total) PetscCall(ISGatherTotal_Private(is));
1384:     *indices = is->total;
1385:   }
1386:   PetscFunctionReturn(PETSC_SUCCESS);
1387: }

1389: /*@
1390:   ISRestoreTotalIndices - Restore the index array obtained with `ISGetTotalIndices()`.

1392:   Not Collective.

1394:   Input Parameters:
1395: + is      - the index set
1396: - indices - index array; must be the array obtained with `ISGetTotalIndices()`

1398:   Level: intermediate

1400: .seealso: `IS`, `ISGetNonlocalIndices()`
1401: @*/
1402: PetscErrorCode ISRestoreTotalIndices(IS is, const PetscInt *indices[])
1403: {
1404:   PetscMPIInt size;

1406:   PetscFunctionBegin;
1408:   PetscAssertPointer(indices, 2);
1409:   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)is), &size));
1410:   if (size == 1) {
1411:     PetscUseTypeMethod(is, restoreindices, indices);
1412:   } else {
1413:     PetscCheck(is->total == *indices, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Index array pointer being restored does not point to the array obtained from the IS.");
1414:   }
1415:   PetscFunctionReturn(PETSC_SUCCESS);
1416: }

1418: /*@
1419:   ISGetNonlocalIndices - Retrieve an array of indices from remote processors
1420:   in this communicator.

1422:   Collective

1424:   Input Parameter:
1425: . is - the index set

1427:   Output Parameter:
1428: . indices - indices with rank 0 indices first, and so on,  omitting
1429:              the current rank.  Total number of indices is the difference
1430:              total and local, obtained with `ISGetSize()` and `ISGetLocalSize()`,
1431:              respectively.

1433:   Level: intermediate

1435:   Notes:
1436:   Restore the indices using `ISRestoreNonlocalIndices()`.

1438:   The same scalability considerations as those for `ISGetTotalIndices()` apply here.

1440: .seealso: `IS`, `ISGetTotalIndices()`, `ISRestoreNonlocalIndices()`, `ISGetSize()`, `ISGetLocalSize().`
1441: @*/
1442: PetscErrorCode ISGetNonlocalIndices(IS is, const PetscInt *indices[])
1443: {
1444:   PetscMPIInt size;
1445:   PetscInt    n, N;

1447:   PetscFunctionBegin;
1449:   PetscAssertPointer(indices, 2);
1450:   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)is), &size));
1451:   if (size == 1) *indices = NULL;
1452:   else {
1453:     if (!is->total) PetscCall(ISGatherTotal_Private(is));
1454:     PetscCall(ISGetLocalSize(is, &n));
1455:     PetscCall(ISGetSize(is, &N));
1456:     PetscCall(PetscMalloc1(N - n, &is->nonlocal));
1457:     PetscCall(PetscArraycpy(is->nonlocal, is->total, is->local_offset));
1458:     PetscCall(PetscArraycpy(is->nonlocal + is->local_offset, is->total + is->local_offset + n, N - is->local_offset - n));
1459:     *indices = is->nonlocal;
1460:   }
1461:   PetscFunctionReturn(PETSC_SUCCESS);
1462: }

1464: /*@
1465:   ISRestoreNonlocalIndices - Restore the index array obtained with `ISGetNonlocalIndices()`.

1467:   Not Collective.

1469:   Input Parameters:
1470: + is      - the index set
1471: - indices - index array; must be the array obtained with `ISGetNonlocalIndices()`

1473:   Level: intermediate

1475: .seealso: `IS`, `ISGetTotalIndices()`, `ISGetNonlocalIndices()`, `ISRestoreTotalIndices()`
1476: @*/
1477: PetscErrorCode ISRestoreNonlocalIndices(IS is, const PetscInt *indices[])
1478: {
1479:   PetscFunctionBegin;
1481:   PetscAssertPointer(indices, 2);
1482:   PetscCheck(is->nonlocal == *indices, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Index array pointer being restored does not point to the array obtained from the IS.");
1483:   PetscFunctionReturn(PETSC_SUCCESS);
1484: }

1486: /*@
1487:   ISGetNonlocalIS - Gather all nonlocal indices for this `IS` and present
1488:   them as another sequential index set.

1490:   Collective

1492:   Input Parameter:
1493: . is - the index set

1495:   Output Parameter:
1496: . complement - sequential `IS` with indices identical to the result of
1497:                 `ISGetNonlocalIndices()`

1499:   Level: intermediate

1501:   Notes:
1502:   Complement represents the result of `ISGetNonlocalIndices()` as an `IS`.
1503:   Therefore scalability issues similar to `ISGetNonlocalIndices()` apply.

1505:   The resulting `IS` must be restored using `ISRestoreNonlocalIS()`.

1507: .seealso: `IS`, `ISGetNonlocalIndices()`, `ISRestoreNonlocalIndices()`, `ISAllGather()`, `ISGetSize()`
1508: @*/
1509: PetscErrorCode ISGetNonlocalIS(IS is, IS *complement)
1510: {
1511:   PetscFunctionBegin;
1513:   PetscAssertPointer(complement, 2);
1514:   /* Check if the complement exists already. */
1515:   if (is->complement) {
1516:     *complement = is->complement;
1517:     PetscCall(PetscObjectReference((PetscObject)is->complement));
1518:   } else {
1519:     PetscInt        N, n;
1520:     const PetscInt *idx;
1521:     PetscCall(ISGetSize(is, &N));
1522:     PetscCall(ISGetLocalSize(is, &n));
1523:     PetscCall(ISGetNonlocalIndices(is, &idx));
1524:     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, N - n, idx, PETSC_USE_POINTER, &is->complement));
1525:     PetscCall(PetscObjectReference((PetscObject)is->complement));
1526:     *complement = is->complement;
1527:   }
1528:   PetscFunctionReturn(PETSC_SUCCESS);
1529: }

1531: /*@
1532:   ISRestoreNonlocalIS - Restore the `IS` obtained with `ISGetNonlocalIS()`.

1534:   Not collective.

1536:   Input Parameters:
1537: + is         - the index set
1538: - complement - index set of `is`'s nonlocal indices

1540:   Level: intermediate

1542: .seealso: `IS`, `ISGetNonlocalIS()`, `ISGetNonlocalIndices()`, `ISRestoreNonlocalIndices()`
1543: @*/
1544: PetscErrorCode ISRestoreNonlocalIS(IS is, IS *complement)
1545: {
1546:   PetscInt refcnt;

1548:   PetscFunctionBegin;
1550:   PetscAssertPointer(complement, 2);
1551:   PetscCheck(*complement == is->complement, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Complement IS being restored was not obtained with ISGetNonlocalIS()");
1552:   PetscCall(PetscObjectGetReference((PetscObject)is->complement, &refcnt));
1553:   PetscCheck(refcnt > 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Duplicate call to ISRestoreNonlocalIS() detected");
1554:   PetscCall(PetscObjectDereference((PetscObject)is->complement));
1555:   PetscFunctionReturn(PETSC_SUCCESS);
1556: }

1558: /*@
1559:   ISViewFromOptions - View an `IS` based on options in the options database

1561:   Collective

1563:   Input Parameters:
1564: + A    - the index set
1565: . obj  - optional object that provides the prefix for the options database, pass `NULL` to use the options prefix of `A`
1566: - name - command line option

1568:   Options Database Key:
1569: . -name viewer_specification - See `PetscOptionsCreateViewer()` for the values of `viewer_specification`

1571:   Level: intermediate

1573:   Note:
1574:   This checks the options database, creates the viewer on-the-fly, uses it and then destroys it. Hence it should not be called in heavily used routines,
1575:   rather `PetscOptionsCreateViewer()` should be used to construct the viewer once which can then be utilized in the heavily used routine.

1577: .seealso: `IS`, `ISView()`, `PetscObjectViewFromOptions()`, `ISCreate()`, `PetscOptionsCreateViewer()`
1578: @*/
1579: PetscErrorCode ISViewFromOptions(IS A, PetscObject obj, const char name[])
1580: {
1581:   PetscFunctionBegin;
1583:   PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
1584:   PetscFunctionReturn(PETSC_SUCCESS);
1585: }

1587: /*@
1588:   ISView - Displays an index set.

1590:   Collective

1592:   Input Parameters:
1593: + is     - the index set
1594: - viewer - viewer used to display the set, for example `PETSC_VIEWER_STDOUT_SELF`.

1596:   Level: intermediate

1598: .seealso: `IS`, `PetscViewer`, `PetscViewerASCIIOpen()`, `ISViewFromOptions()`
1599: @*/
1600: PetscErrorCode ISView(IS is, PetscViewer viewer)
1601: {
1602:   PetscFunctionBegin;
1604:   if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)is), &viewer));
1606:   PetscCheckSameComm(is, 1, viewer, 2);

1608:   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)is, viewer));
1609:   PetscCall(PetscLogEventBegin(IS_View, is, viewer, 0, 0));
1610:   PetscUseTypeMethod(is, view, viewer);
1611:   PetscCall(PetscLogEventEnd(IS_View, is, viewer, 0, 0));
1612:   PetscFunctionReturn(PETSC_SUCCESS);
1613: }

1615: /*@
1616:   ISLoad - Loads an index set that has been stored in binary or HDF5 format with `ISView()`.

1618:   Collective

1620:   Input Parameters:
1621: + is     - the newly loaded index set, this needs to have been created with `ISCreate()` or some related function before a call to `ISLoad()`.
1622: - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or HDF5 file viewer, obtained from `PetscViewerHDF5Open()`

1624:   Level: intermediate

1626:   Notes:
1627:   IF using HDF5, you must assign the IS the same name as was used in `is`
1628:   that was stored in the file using `PetscObjectSetName()`. Otherwise you will
1629:   get the error message: "Cannot H5DOpen2() with Vec name NAMEOFOBJECT"

1631: .seealso: `IS`, `PetscViewerBinaryOpen()`, `ISView()`, `MatLoad()`, `VecLoad()`
1632: @*/
1633: PetscErrorCode ISLoad(IS is, PetscViewer viewer)
1634: {
1635:   PetscBool isbinary, ishdf5;

1637:   PetscFunctionBegin;
1640:   PetscCheckSameComm(is, 1, viewer, 2);
1641:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
1642:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
1643:   PetscCheck(isbinary || ishdf5, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen()");
1644:   if (!((PetscObject)is)->type_name) PetscCall(ISSetType(is, ISGENERAL));
1645:   PetscCall(PetscLogEventBegin(IS_Load, is, viewer, 0, 0));
1646:   PetscUseTypeMethod(is, load, viewer);
1647:   PetscCall(PetscLogEventEnd(IS_Load, is, viewer, 0, 0));
1648:   PetscFunctionReturn(PETSC_SUCCESS);
1649: }

1651: /*@
1652:   ISSort - Sorts the indices of an index set.

1654:   Collective

1656:   Input Parameter:
1657: . is - the index set

1659:   Level: intermediate

1661: .seealso: `IS`, `ISSortRemoveDups()`, `ISSorted()`
1662: @*/
1663: PetscErrorCode ISSort(IS is)
1664: {
1665:   PetscBool flg;

1667:   PetscFunctionBegin;
1669:   PetscCall(ISGetInfo(is, IS_SORTED, IS_LOCAL, PETSC_FALSE, &flg));
1670:   if (!flg) {
1671:     PetscUseTypeMethod(is, sort);
1672:     PetscCall(ISSetInfo(is, IS_SORTED, IS_LOCAL, is->info_permanent[IS_LOCAL][IS_SORTED], PETSC_TRUE));
1673:   }
1674:   PetscFunctionReturn(PETSC_SUCCESS);
1675: }

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

1680:   Collective

1682:   Input Parameter:
1683: . is - the index set

1685:   Level: intermediate

1687: .seealso: `IS`, `ISSort()`, `ISSorted()`
1688: @*/
1689: PetscErrorCode ISSortRemoveDups(IS is)
1690: {
1691:   PetscFunctionBegin;
1693:   PetscCall(ISClearInfoCache(is, PETSC_FALSE));
1694:   PetscUseTypeMethod(is, sortremovedups);
1695:   PetscCall(ISSetInfo(is, IS_SORTED, IS_LOCAL, is->info_permanent[IS_LOCAL][IS_SORTED], PETSC_TRUE));
1696:   PetscCall(ISSetInfo(is, IS_UNIQUE, IS_LOCAL, is->info_permanent[IS_LOCAL][IS_UNIQUE], PETSC_TRUE));
1697:   PetscFunctionReturn(PETSC_SUCCESS);
1698: }

1700: /*@
1701:   ISToGeneral - Converts an IS object of any type to `ISGENERAL` type

1703:   Collective

1705:   Input Parameter:
1706: . is - the index set

1708:   Level: intermediate

1710: .seealso: `IS`, `ISSorted()`
1711: @*/
1712: PetscErrorCode ISToGeneral(IS is)
1713: {
1714:   PetscFunctionBegin;
1716:   PetscUseTypeMethod(is, togeneral);
1717:   PetscFunctionReturn(PETSC_SUCCESS);
1718: }

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

1723:   Not Collective

1725:   Input Parameter:
1726: . is - the index set

1728:   Output Parameter:
1729: . flg - output flag, either `PETSC_TRUE` if the index set is sorted,
1730:          or `PETSC_FALSE` otherwise.

1732:   Level: intermediate

1734:   Note:
1735:   For parallel IS objects this only indicates if the local part of `is`
1736:   is sorted. So some processors may return `PETSC_TRUE` while others may
1737:   return `PETSC_FALSE`.

1739: .seealso: `ISSort()`, `ISSortRemoveDups()`
1740: @*/
1741: PetscErrorCode ISSorted(IS is, PetscBool *flg)
1742: {
1743:   PetscFunctionBegin;
1745:   PetscAssertPointer(flg, 2);
1746:   PetscCall(ISGetInfo(is, IS_SORTED, IS_LOCAL, PETSC_TRUE, flg));
1747:   PetscFunctionReturn(PETSC_SUCCESS);
1748: }

1750: /*@
1751:   ISDuplicate - Creates a duplicate copy of an index set.

1753:   Collective

1755:   Input Parameter:
1756: . is - the index set

1758:   Output Parameter:
1759: . newIS - the copy of the index set

1761:   Level: beginner

1763: .seealso: `IS`, `ISCreateGeneral()`, `ISCopy()`
1764: @*/
1765: PetscErrorCode ISDuplicate(IS is, IS *newIS)
1766: {
1767:   PetscFunctionBegin;
1769:   PetscAssertPointer(newIS, 2);
1770:   PetscUseTypeMethod(is, duplicate, newIS);
1771:   PetscCall(ISCopyInfo_Private(is, *newIS));
1772:   PetscFunctionReturn(PETSC_SUCCESS);
1773: }

1775: /*@
1776:   ISCopy - Copies an index set.

1778:   Collective

1780:   Input Parameter:
1781: . is - the index set

1783:   Output Parameter:
1784: . isy - the copy of the index set

1786:   Level: beginner

1788: .seealso: `IS`, `ISDuplicate()`, `ISShift()`
1789: @*/
1790: PetscErrorCode ISCopy(IS is, IS isy)
1791: {
1792:   PetscInt bs, bsy;

1794:   PetscFunctionBegin;
1797:   PetscCheckSameComm(is, 1, isy, 2);
1798:   if (is == isy) PetscFunctionReturn(PETSC_SUCCESS);
1799:   PetscCall(PetscLayoutGetBlockSize(is->map, &bs));
1800:   PetscCall(PetscLayoutGetBlockSize(isy->map, &bsy));
1801:   PetscCheck(is->map->N == isy->map->N, PetscObjectComm((PetscObject)is), PETSC_ERR_ARG_INCOMP, "Index sets have different global size %" PetscInt_FMT " != %" PetscInt_FMT, is->map->N, isy->map->N);
1802:   PetscCheck(is->map->n == isy->map->n, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Index sets have different local size %" PetscInt_FMT " != %" PetscInt_FMT, is->map->n, isy->map->n);
1803:   PetscCheck(bs == bsy, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Index sets have different block size %" PetscInt_FMT " != %" PetscInt_FMT, bs, bsy);
1804:   PetscCall(ISCopyInfo_Private(is, isy));
1805:   isy->max = is->max;
1806:   isy->min = is->min;
1807:   PetscUseTypeMethod(is, copy, isy);
1808:   PetscFunctionReturn(PETSC_SUCCESS);
1809: }

1811: /*@
1812:   ISShift - Shift all indices by given offset

1814:   Collective

1816:   Input Parameters:
1817: + is     - the index set
1818: - offset - the offset

1820:   Output Parameter:
1821: . isy - the shifted copy of the input index set

1823:   Level: beginner

1825:   Notes:
1826:   The `offset` can be different across processes.

1828:   `is` and `isy` can be the same.

1830: .seealso: `ISDuplicate()`, `ISCopy()`
1831: @*/
1832: PetscErrorCode ISShift(IS is, PetscInt offset, IS isy)
1833: {
1834:   PetscFunctionBegin;
1837:   PetscCheckSameComm(is, 1, isy, 3);
1838:   if (!offset) {
1839:     PetscCall(ISCopy(is, isy));
1840:     PetscFunctionReturn(PETSC_SUCCESS);
1841:   }
1842:   PetscCheck(is->map->N == isy->map->N, PetscObjectComm((PetscObject)is), PETSC_ERR_ARG_INCOMP, "Index sets have different global size %" PetscInt_FMT " != %" PetscInt_FMT, is->map->N, isy->map->N);
1843:   PetscCheck(is->map->n == isy->map->n, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Index sets have different local size %" PetscInt_FMT " != %" PetscInt_FMT, is->map->n, isy->map->n);
1844:   PetscCheck(is->map->bs == isy->map->bs, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Index sets have different block size %" PetscInt_FMT " != %" PetscInt_FMT, is->map->bs, isy->map->bs);
1845:   PetscCall(ISCopyInfo_Private(is, isy));
1846:   if (offset > 0) {
1847:     isy->max = is->max < PETSC_INT_MAX - offset ? is->max + offset : PETSC_INT_MAX;
1848:     isy->min = is->min < PETSC_INT_MAX - offset ? is->min + offset : PETSC_INT_MAX;
1849:   } else {
1850:     isy->max = is->max > PETSC_INT_MIN - offset ? is->max + offset : PETSC_INT_MIN;
1851:     isy->min = is->min > PETSC_INT_MIN - offset ? is->min + offset : PETSC_INT_MIN;
1852:   }
1853:   PetscUseMethod(is, "ISShift_C", (IS, PetscInt, IS), (is, offset, isy));
1854:   PetscFunctionReturn(PETSC_SUCCESS);
1855: }

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

1860:   Collective

1862:   Input Parameters:
1863: + is   - index set
1864: . comm - communicator for new index set
1865: - mode - copy semantics, `PETSC_USE_POINTER` for no-copy if possible, otherwise `PETSC_COPY_VALUES`

1867:   Output Parameter:
1868: . newis - new `IS` on `comm`

1870:   Level: advanced

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

1875:   This function is useful if serial `IS`s must be created independently, or to view many
1876:   logically independent serial `IS`s.

1878:   The input `IS` must have the same type on every MPI process.

1880: .seealso: `IS`
1881: @*/
1882: PetscErrorCode ISOnComm(IS is, MPI_Comm comm, PetscCopyMode mode, IS *newis)
1883: {
1884:   PetscMPIInt match;

1886:   PetscFunctionBegin;
1888:   PetscAssertPointer(newis, 4);
1889:   PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)is), comm, &match));
1890:   if (mode != PETSC_COPY_VALUES && (match == MPI_IDENT || match == MPI_CONGRUENT)) {
1891:     PetscCall(PetscObjectReference((PetscObject)is));
1892:     *newis = is;
1893:   } else PetscUseTypeMethod(is, oncomm, comm, mode, newis);
1894:   PetscFunctionReturn(PETSC_SUCCESS);
1895: }

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

1900:   Logicall Collective

1902:   Input Parameters:
1903: + is - index set
1904: - bs - block size

1906:   Level: intermediate

1908:   Notes:
1909:   This is much like the block size for `Vec`s. It indicates that one can think of the indices as
1910:   being in a collection of equal size blocks. For `ISBLOCK` these collections of blocks are all contiguous
1911:   within a block but this is not the case for other `IS`. For example, an `IS` with entries {0, 2, 3, 4, 6, 7} could
1912:   have a block size of three set.

1914:   `ISBlockGetIndices()` only works for `ISBLOCK`, not others.

1916: .seealso: `IS`, `ISGetBlockSize()`, `ISCreateBlock()`, `ISBlockGetIndices()`
1917: @*/
1918: PetscErrorCode ISSetBlockSize(IS is, PetscInt bs)
1919: {
1920:   PetscFunctionBegin;
1923:   PetscCheck(bs >= 1, PetscObjectComm((PetscObject)is), PETSC_ERR_ARG_OUTOFRANGE, "Block size %" PetscInt_FMT ", must be positive", bs);
1924:   if (PetscDefined(USE_DEBUG)) {
1925:     const PetscInt *indices;
1926:     PetscInt        length, i, j;
1927:     PetscCall(ISGetIndices(is, &indices));
1928:     if (indices) {
1929:       PetscCall(ISGetLocalSize(is, &length));
1930:       PetscCheck(length % bs == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Local size %" PetscInt_FMT " not compatible with proposed block size %" PetscInt_FMT, length, bs);
1931:       for (i = 1; i < length / bs; i += bs) {
1932:         for (j = 1; j < bs - 1; j++) {
1933:           PetscCheck(indices[i * bs + j] == indices[(i - 1) * bs + j] + indices[i * bs] - indices[(i - 1) * bs], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Proposed block size %" PetscInt_FMT " is incompatible with the indices", bs);
1934:         }
1935:       }
1936:     }
1937:     PetscCall(ISRestoreIndices(is, &indices));
1938:   }
1939:   PetscUseTypeMethod(is, setblocksize, bs);
1940:   PetscFunctionReturn(PETSC_SUCCESS);
1941: }

1943: /*@
1944:   ISGetBlockSize - Returns the number of elements in a block.

1946:   Not Collective

1948:   Input Parameter:
1949: . is - the index set

1951:   Output Parameter:
1952: . size - the number of elements in a block

1954:   Level: intermediate

1956:   Note:
1957:   See `ISSetBlockSize()`

1959: .seealso: `IS`, `ISBlockGetSize()`, `ISGetSize()`, `ISCreateBlock()`, `ISSetBlockSize()`
1960: @*/
1961: PetscErrorCode ISGetBlockSize(IS is, PetscInt *size)
1962: {
1963:   PetscFunctionBegin;
1964:   PetscCall(PetscLayoutGetBlockSize(is->map, size));
1965:   PetscFunctionReturn(PETSC_SUCCESS);
1966: }

1968: /*@
1969:   ISSetCompressOutput - set the flag for output compression

1971:   Logicall Collective

1973:   Input Parameters:
1974: + is       - index set
1975: - compress - flag for output compression

1977:   Level: intermediate

1979: .seealso: `IS`, `ISGetCompressOutput()`, `ISView()`
1980: @*/
1981: PetscErrorCode ISSetCompressOutput(IS is, PetscBool compress)
1982: {
1983:   PetscFunctionBegin;
1986:   is->compressOutput = compress;
1987:   PetscFunctionReturn(PETSC_SUCCESS);
1988: }

1990: /*@
1991:   ISGetCompressOutput - Returns the flag for output compression

1993:   Not Collective

1995:   Input Parameter:
1996: . is - the index set

1998:   Output Parameter:
1999: . compress - the flag to compress output

2001:   Level: intermediate

2003: .seealso: `IS`, `ISSetCompressOutput()`, `ISView()`
2004: @*/
2005: PetscErrorCode ISGetCompressOutput(IS is, PetscBool *compress)
2006: {
2007:   PetscFunctionBegin;
2009:   PetscAssertPointer(compress, 2);
2010:   *compress = is->compressOutput;
2011:   PetscFunctionReturn(PETSC_SUCCESS);
2012: }

2014: static PetscErrorCode ISGetIndicesCopy_Private(IS is, PetscInt idx[])
2015: {
2016:   PetscInt        len;
2017:   const PetscInt *ptr;

2019:   PetscFunctionBegin;
2020:   PetscCall(ISGetLocalSize(is, &len));
2021:   PetscCall(ISGetIndices(is, &ptr));
2022:   for (PetscInt i = 0; i < len; i++) idx[i] = ptr[i];
2023:   PetscCall(ISRestoreIndices(is, &ptr));
2024:   PetscFunctionReturn(PETSC_SUCCESS);
2025: }