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 <petscviewer.h>
6: #include <petscsf.h>
8: /* Logging support */
9: PetscClassId IS_CLASSID;
10: /* TODO: Much more events are missing! */
11: PetscLogEvent IS_View;
12: PetscLogEvent IS_Load;
14: /*@
15: ISRenumber - Renumbers the non-negative entries of an index set in a contiguous way, starting from 0.
17: Collective
19: Input Parameters:
20: + subset - the index set
21: - subset_mult - the multiplicity of each entry in subset (optional, can be `NULL`)
23: Output Parameters:
24: + N - one past the largest entry of the new `IS`
25: - subset_n - the new `IS`
27: Level: intermediate
29: Note:
30: All negative entries are mapped to -1. Indices with non positive multiplicities are skipped.
32: .seealso: `IS`
33: @*/
34: PetscErrorCode ISRenumber(IS subset, IS subset_mult, PetscInt *N, IS *subset_n)
35: {
36: PetscSF sf;
37: PetscLayout map;
38: const PetscInt *idxs, *idxs_mult = NULL;
39: PetscInt *leaf_data, *root_data, *gidxs, *ilocal, *ilocalneg;
40: PetscInt N_n, n, i, bounds[2], Nl, ibs;
41: PetscInt n_n, nlocals, start, first_index, npos, nneg;
42: PetscMPIInt commsize;
43: PetscBool first_found, isblock;
45: PetscFunctionBegin;
48: if (N) PetscAssertPointer(N, 3);
49: else if (!subset_n) PetscFunctionReturn(PETSC_SUCCESS);
50: PetscCall(ISGetLocalSize(subset, &n));
51: if (subset_mult) {
52: PetscCall(ISGetLocalSize(subset_mult, &i));
53: PetscCheck(i == n, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Local subset and multiplicity sizes don't match! %" PetscInt_FMT " != %" PetscInt_FMT, n, i);
54: }
55: /* create workspace layout for computing global indices of subset */
56: PetscCall(PetscMalloc1(n, &ilocal));
57: PetscCall(PetscMalloc1(n, &ilocalneg));
58: PetscCall(ISGetIndices(subset, &idxs));
59: PetscCall(ISGetBlockSize(subset, &ibs));
60: PetscCall(PetscObjectTypeCompare((PetscObject)subset, ISBLOCK, &isblock));
61: if (subset_mult) PetscCall(ISGetIndices(subset_mult, &idxs_mult));
62: bounds[0] = PETSC_INT_MAX;
63: bounds[1] = PETSC_INT_MIN;
64: for (i = 0, npos = 0, nneg = 0; i < n; i++) {
65: if (idxs[i] < 0) {
66: ilocalneg[nneg++] = i;
67: continue;
68: }
69: if (idxs[i] < bounds[0]) bounds[0] = idxs[i];
70: if (idxs[i] > bounds[1]) bounds[1] = idxs[i];
71: ilocal[npos++] = i;
72: }
73: if (npos == n) {
74: PetscCall(PetscFree(ilocal));
75: PetscCall(PetscFree(ilocalneg));
76: }
78: /* create sf : leaf_data == multiplicity of indexes, root data == global index in layout */
79: PetscCall(PetscMalloc1(n, &leaf_data));
80: for (i = 0; i < n; i++) leaf_data[i] = idxs_mult ? PetscMax(idxs_mult[i], 0) : 1;
82: /* local size of new subset */
83: n_n = 0;
84: for (i = 0; i < n; i++) n_n += leaf_data[i];
85: if (ilocalneg)
86: for (i = 0; i < nneg; i++) leaf_data[ilocalneg[i]] = 0;
87: PetscCall(PetscFree(ilocalneg));
88: PetscCall(PetscMalloc1(PetscMax(n_n, n), &gidxs)); /* allocating extra space to reuse gidxs */
89: /* check for early termination (all negative) */
90: PetscCall(PetscGlobalMinMaxInt(PetscObjectComm((PetscObject)subset), bounds, bounds));
91: if (bounds[1] < bounds[0]) {
92: if (N) *N = 0;
93: if (subset_n) { /* all negative */
94: for (i = 0; i < n_n; i++) gidxs[i] = -1;
95: PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)subset), n_n, gidxs, PETSC_COPY_VALUES, subset_n));
96: }
97: PetscCall(PetscFree(leaf_data));
98: PetscCall(PetscFree(gidxs));
99: PetscCall(ISRestoreIndices(subset, &idxs));
100: if (subset_mult) PetscCall(ISRestoreIndices(subset_mult, &idxs_mult));
101: PetscCall(PetscFree(ilocal));
102: PetscCall(PetscFree(ilocalneg));
103: PetscFunctionReturn(PETSC_SUCCESS);
104: }
106: /* split work */
107: N_n = bounds[1] - bounds[0] + 1;
108: PetscCall(PetscLayoutCreate(PetscObjectComm((PetscObject)subset), &map));
109: PetscCall(PetscLayoutSetBlockSize(map, 1));
110: PetscCall(PetscLayoutSetSize(map, N_n));
111: PetscCall(PetscLayoutSetUp(map));
112: PetscCall(PetscLayoutGetLocalSize(map, &Nl));
114: /* global indexes in layout */
115: for (i = 0; i < npos; i++) gidxs[i] = (ilocal ? idxs[ilocal[i]] : idxs[i]) - bounds[0];
116: PetscCall(ISRestoreIndices(subset, &idxs));
117: PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)subset), &sf));
118: PetscCall(PetscSFSetGraphLayout(sf, map, npos, ilocal, PETSC_USE_POINTER, gidxs));
119: PetscCall(PetscLayoutDestroy(&map));
121: /* reduce from leaves to roots */
122: PetscCall(PetscCalloc1(Nl, &root_data));
123: PetscCall(PetscSFReduceBegin(sf, MPIU_INT, leaf_data, root_data, MPI_MAX));
124: PetscCall(PetscSFReduceEnd(sf, MPIU_INT, leaf_data, root_data, MPI_MAX));
126: /* count indexes in local part of layout */
127: nlocals = 0;
128: first_index = -1;
129: first_found = PETSC_FALSE;
130: for (i = 0; i < Nl; i++) {
131: if (!first_found && root_data[i]) {
132: first_found = PETSC_TRUE;
133: first_index = i;
134: }
135: nlocals += root_data[i];
136: }
138: /* cumulative of number of indexes and size of subset without holes */
139: #if PetscDefined(HAVE_MPI_EXSCAN)
140: start = 0;
141: PetscCallMPI(MPI_Exscan(&nlocals, &start, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)subset)));
142: #else
143: PetscCallMPI(MPI_Scan(&nlocals, &start, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)subset)));
144: start = start - nlocals;
145: #endif
147: if (N) { /* compute total size of new subset if requested */
148: *N = start + nlocals;
149: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)subset), &commsize));
150: PetscCallMPI(MPI_Bcast(N, 1, MPIU_INT, commsize - 1, PetscObjectComm((PetscObject)subset)));
151: }
153: if (!subset_n) {
154: PetscCall(PetscFree(gidxs));
155: PetscCall(PetscSFDestroy(&sf));
156: PetscCall(PetscFree(leaf_data));
157: PetscCall(PetscFree(root_data));
158: PetscCall(PetscFree(ilocal));
159: if (subset_mult) PetscCall(ISRestoreIndices(subset_mult, &idxs_mult));
160: PetscFunctionReturn(PETSC_SUCCESS);
161: }
163: /* adapt root data with cumulative */
164: if (first_found) {
165: PetscInt old_index;
167: root_data[first_index] += start;
168: old_index = first_index;
169: for (i = first_index + 1; i < Nl; i++) {
170: if (root_data[i]) {
171: root_data[i] += root_data[old_index];
172: old_index = i;
173: }
174: }
175: }
177: /* from roots to leaves */
178: PetscCall(PetscSFBcastBegin(sf, MPIU_INT, root_data, leaf_data, MPI_REPLACE));
179: PetscCall(PetscSFBcastEnd(sf, MPIU_INT, root_data, leaf_data, MPI_REPLACE));
180: PetscCall(PetscSFDestroy(&sf));
182: /* create new IS with global indexes without holes */
183: for (i = 0; i < n_n; i++) gidxs[i] = -1;
184: if (subset_mult) {
185: PetscInt cum;
187: isblock = PETSC_FALSE;
188: for (i = 0, cum = 0; i < n; i++)
189: for (PetscInt j = 0; j < idxs_mult[i]; j++) gidxs[cum++] = leaf_data[i] - idxs_mult[i] + j;
190: } else
191: for (i = 0; i < n; i++) gidxs[i] = leaf_data[i] - 1;
193: if (isblock) {
194: if (ibs > 1)
195: for (i = 0; i < n_n / ibs; i++) gidxs[i] = gidxs[i * ibs] / ibs;
196: PetscCall(ISCreateBlock(PetscObjectComm((PetscObject)subset), ibs, n_n / ibs, gidxs, PETSC_COPY_VALUES, subset_n));
197: } else {
198: PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)subset), n_n, gidxs, PETSC_COPY_VALUES, subset_n));
199: }
200: if (subset_mult) PetscCall(ISRestoreIndices(subset_mult, &idxs_mult));
201: PetscCall(PetscFree(gidxs));
202: PetscCall(PetscFree(leaf_data));
203: PetscCall(PetscFree(root_data));
204: PetscCall(PetscFree(ilocal));
205: PetscFunctionReturn(PETSC_SUCCESS);
206: }
208: /*@
209: ISCreateSubIS - Create a sub index set from a global index set selecting some components.
211: Collective
213: Input Parameters:
214: + is - the index set
215: - comps - which components we will extract from `is`
217: Output Parameters:
218: . subis - the new sub index set
220: Example usage:
221: We have an index set `is` living on 3 processes with the following values\:
222: | 4 9 0 | 2 6 7 | 10 11 1|
223: and another index set `comps` used to indicate which components of is we want to take,
224: | 7 5 | 1 2 | 0 4|
225: The output index set `subis` should look like\:
226: | 11 7 | 9 0 | 4 6|
228: Level: intermediate
230: .seealso: `IS`, `VecGetSubVector()`, `MatCreateSubMatrix()`
231: @*/
232: PetscErrorCode ISCreateSubIS(IS is, IS comps, IS *subis)
233: {
234: PetscSF sf;
235: const PetscInt *is_indices, *comps_indices;
236: PetscInt *subis_indices, nroots, nleaves, *mine, i, lidx;
237: PetscMPIInt owner;
238: PetscSFNode *remote;
239: MPI_Comm comm;
241: PetscFunctionBegin;
244: PetscAssertPointer(subis, 3);
246: PetscCall(PetscObjectGetComm((PetscObject)is, &comm));
247: PetscCall(ISGetLocalSize(comps, &nleaves));
248: PetscCall(ISGetLocalSize(is, &nroots));
249: PetscCall(PetscMalloc1(nleaves, &remote));
250: PetscCall(PetscMalloc1(nleaves, &mine));
251: PetscCall(ISGetIndices(comps, &comps_indices));
252: /*
253: * Construct a PetscSF in which "is" data serves as roots and "subis" is leaves.
254: * Root data are sent to leaves using PetscSFBcast().
255: * */
256: for (i = 0; i < nleaves; i++) {
257: mine[i] = i;
258: /* Connect a remote root with the current leaf. The value on the remote root
259: * will be received by the current local leaf.
260: * */
261: owner = -1;
262: lidx = -1;
263: PetscCall(PetscLayoutFindOwnerIndex(is->map, comps_indices[i], &owner, &lidx));
264: remote[i].rank = owner;
265: remote[i].index = lidx;
266: }
267: PetscCall(ISRestoreIndices(comps, &comps_indices));
268: PetscCall(PetscSFCreate(comm, &sf));
269: PetscCall(PetscSFSetFromOptions(sf));
270: PetscCall(PetscSFSetGraph(sf, nroots, nleaves, mine, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER));
272: PetscCall(PetscMalloc1(nleaves, &subis_indices));
273: PetscCall(ISGetIndices(is, &is_indices));
274: PetscCall(PetscSFBcastBegin(sf, MPIU_INT, is_indices, subis_indices, MPI_REPLACE));
275: PetscCall(PetscSFBcastEnd(sf, MPIU_INT, is_indices, subis_indices, MPI_REPLACE));
276: PetscCall(ISRestoreIndices(is, &is_indices));
277: PetscCall(PetscSFDestroy(&sf));
278: PetscCall(ISCreateGeneral(comm, nleaves, subis_indices, PETSC_OWN_POINTER, subis));
279: PetscFunctionReturn(PETSC_SUCCESS);
280: }
282: /*@
283: ISClearInfoCache - clear the cache of computed index set properties
285: Not Collective
287: Input Parameters:
288: + is - the index set
289: - clear_permanent_local - whether to remove the permanent status of local properties
291: Level: developer
293: Note:
294: Because all processes must agree on the global permanent status of a property,
295: the permanent status can only be changed with `ISSetInfo()`, because this routine is not collective
297: .seealso: `IS`, `ISInfo`, `ISInfoType`, `ISSetInfo()`
298: @*/
299: PetscErrorCode ISClearInfoCache(IS is, PetscBool clear_permanent_local)
300: {
301: PetscInt i;
303: PetscFunctionBegin;
306: for (i = 0; i < IS_INFO_MAX; i++) {
307: if (clear_permanent_local) is->info_permanent[IS_LOCAL][i] = PETSC_FALSE;
308: for (PetscInt j = 0; j < 2; j++) {
309: if (!is->info_permanent[j][i]) is->info[j][i] = IS_INFO_UNKNOWN;
310: }
311: }
312: PetscFunctionReturn(PETSC_SUCCESS);
313: }
315: static PetscErrorCode ISSetInfo_Internal(IS is, ISInfo info, ISInfoType type, ISInfoBool ipermanent, PetscBool flg)
316: {
317: ISInfoBool iflg = flg ? IS_INFO_TRUE : IS_INFO_FALSE;
318: PetscInt itype = (type == IS_LOCAL) ? 0 : 1;
319: PetscBool permanent_set = (ipermanent == IS_INFO_UNKNOWN) ? PETSC_FALSE : PETSC_TRUE;
320: PetscBool permanent = (ipermanent == IS_INFO_TRUE) ? PETSC_TRUE : PETSC_FALSE;
322: PetscFunctionBegin;
323: /* set this property */
324: is->info[itype][(int)info] = iflg;
325: if (permanent_set) is->info_permanent[itype][(int)info] = permanent;
326: /* set implications */
327: switch (info) {
328: case IS_SORTED:
329: if (PetscDefined(USE_DEBUG) && flg) {
330: PetscInt n;
331: const PetscInt *indices;
333: PetscCall(ISGetLocalSize(is, &n));
334: PetscCall(ISGetIndices(is, &indices));
335: PetscCall(PetscSortedInt(n, indices, &flg));
336: if (type == IS_GLOBAL) PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &flg, 1, MPI_C_BOOL, MPI_LAND, PetscObjectComm((PetscObject)is)));
337: PetscCheck(flg, type == IS_GLOBAL ? PetscObjectComm((PetscObject)is) : PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "IS is not sorted");
338: PetscCall(ISRestoreIndices(is, &indices));
339: }
340: if (flg && type == IS_GLOBAL) { /* an array that is globally sorted is also locally sorted */
341: is->info[IS_LOCAL][(int)info] = IS_INFO_TRUE;
342: /* global permanence implies local permanence */
343: if (permanent_set && permanent) is->info_permanent[IS_LOCAL][(int)info] = PETSC_TRUE;
344: }
345: if (!flg) { /* if an array is not sorted, it cannot be an interval or the identity */
346: is->info[itype][IS_INTERVAL] = IS_INFO_FALSE;
347: is->info[itype][IS_IDENTITY] = IS_INFO_FALSE;
348: if (permanent_set) {
349: is->info_permanent[itype][IS_INTERVAL] = permanent;
350: is->info_permanent[itype][IS_IDENTITY] = permanent;
351: }
352: }
353: break;
354: case IS_UNIQUE:
355: if (flg && type == IS_GLOBAL) { /* an array that is globally unique is also locally unique */
356: is->info[IS_LOCAL][(int)info] = IS_INFO_TRUE;
357: /* global permanence implies local permanence */
358: if (permanent_set && permanent) is->info_permanent[IS_LOCAL][(int)info] = PETSC_TRUE;
359: }
360: if (!flg) { /* if an array is not unique, it cannot be a permutation, and interval, or the identity */
361: is->info[itype][IS_PERMUTATION] = IS_INFO_FALSE;
362: is->info[itype][IS_INTERVAL] = IS_INFO_FALSE;
363: is->info[itype][IS_IDENTITY] = IS_INFO_FALSE;
364: if (permanent_set) {
365: is->info_permanent[itype][IS_PERMUTATION] = permanent;
366: is->info_permanent[itype][IS_INTERVAL] = permanent;
367: is->info_permanent[itype][IS_IDENTITY] = permanent;
368: }
369: }
370: break;
371: case IS_PERMUTATION:
372: if (flg) { /* an array that is a permutation is unique and is unique locally */
373: is->info[itype][IS_UNIQUE] = IS_INFO_TRUE;
374: is->info[IS_LOCAL][IS_UNIQUE] = IS_INFO_TRUE;
375: if (permanent_set && permanent) {
376: is->info_permanent[itype][IS_UNIQUE] = PETSC_TRUE;
377: is->info_permanent[IS_LOCAL][IS_UNIQUE] = PETSC_TRUE;
378: }
379: } else { /* an array that is not a permutation cannot be the identity */
380: is->info[itype][IS_IDENTITY] = IS_INFO_FALSE;
381: if (permanent_set) is->info_permanent[itype][IS_IDENTITY] = permanent;
382: }
383: break;
384: case IS_INTERVAL:
385: if (flg) { /* an array that is an interval is sorted and unique */
386: is->info[itype][IS_SORTED] = IS_INFO_TRUE;
387: is->info[IS_LOCAL][IS_SORTED] = IS_INFO_TRUE;
388: is->info[itype][IS_UNIQUE] = IS_INFO_TRUE;
389: is->info[IS_LOCAL][IS_UNIQUE] = IS_INFO_TRUE;
390: if (permanent_set && permanent) {
391: is->info_permanent[itype][IS_SORTED] = PETSC_TRUE;
392: is->info_permanent[IS_LOCAL][IS_SORTED] = PETSC_TRUE;
393: is->info_permanent[itype][IS_UNIQUE] = PETSC_TRUE;
394: is->info_permanent[IS_LOCAL][IS_UNIQUE] = PETSC_TRUE;
395: }
396: } else { /* an array that is not an interval cannot be the identity */
397: is->info[itype][IS_IDENTITY] = IS_INFO_FALSE;
398: if (permanent_set) is->info_permanent[itype][IS_IDENTITY] = permanent;
399: }
400: break;
401: case IS_IDENTITY:
402: if (flg) { /* an array that is the identity is sorted, unique, an interval, and a permutation */
403: is->info[itype][IS_SORTED] = IS_INFO_TRUE;
404: is->info[IS_LOCAL][IS_SORTED] = IS_INFO_TRUE;
405: is->info[itype][IS_UNIQUE] = IS_INFO_TRUE;
406: is->info[IS_LOCAL][IS_UNIQUE] = IS_INFO_TRUE;
407: is->info[itype][IS_PERMUTATION] = IS_INFO_TRUE;
408: is->info[itype][IS_INTERVAL] = IS_INFO_TRUE;
409: is->info[IS_LOCAL][IS_INTERVAL] = IS_INFO_TRUE;
410: if (permanent_set && permanent) {
411: is->info_permanent[itype][IS_SORTED] = PETSC_TRUE;
412: is->info_permanent[IS_LOCAL][IS_SORTED] = PETSC_TRUE;
413: is->info_permanent[itype][IS_UNIQUE] = PETSC_TRUE;
414: is->info_permanent[IS_LOCAL][IS_UNIQUE] = PETSC_TRUE;
415: is->info_permanent[itype][IS_PERMUTATION] = PETSC_TRUE;
416: is->info_permanent[itype][IS_INTERVAL] = PETSC_TRUE;
417: is->info_permanent[IS_LOCAL][IS_INTERVAL] = PETSC_TRUE;
418: }
419: }
420: break;
421: default:
422: PetscCheck(type != IS_LOCAL, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Unknown IS property");
423: SETERRQ(PetscObjectComm((PetscObject)is), PETSC_ERR_ARG_OUTOFRANGE, "Unknown IS property");
424: }
425: PetscFunctionReturn(PETSC_SUCCESS);
426: }
428: // PetscClangLinter pragma disable: -fdoc-section-header-unknown
429: /*@
430: ISSetInfo - Set known information about an index set.
432: Logically Collective if `ISInfoType` is `IS_GLOBAL`
434: Input Parameters:
435: + is - the index set
436: . info - describing a property of the index set, one of those listed below,
437: . type - `IS_LOCAL` if the information describes the local portion of the index set,
438: `IS_GLOBAL` if it describes the whole index set
439: . permanent - `PETSC_TRUE` if it is known that the property will persist through changes to the index set, `PETSC_FALSE` otherwise
440: If the user sets a property as permanently known, it will bypass computation of that property
441: - flg - set the described property as true (`PETSC_TRUE`) or false (`PETSC_FALSE`)
443: Values of `info` Describing `IS` Structure:
444: + `IS_SORTED` - the [local part of the] index set is sorted in ascending order
445: . `IS_UNIQUE` - each entry in the [local part of the] index set is unique
446: . `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
447: . `IS_INTERVAL` - the [local part of the] index set is equal to a contiguous range of integers {f, f + 1, ..., f + N-1}
448: - `IS_IDENTITY` - the [local part of the] index set is equal to the integers {0, 1, ..., N-1}
450: Level: advanced
452: Notes:
453: If type is `IS_GLOBAL`, all processes that share the index set must pass the same value in flg
455: It is possible to set a property with `ISSetInfo()` that contradicts what would be previously computed with `ISGetInfo()`
457: .seealso: `ISInfo`, `ISInfoType`, `IS`
458: @*/
459: PetscErrorCode ISSetInfo(IS is, ISInfo info, ISInfoType type, PetscBool permanent, PetscBool flg)
460: {
461: MPI_Comm comm, errcomm;
462: PetscMPIInt size;
464: PetscFunctionBegin;
467: comm = PetscObjectComm((PetscObject)is);
468: if (type == IS_GLOBAL) {
472: errcomm = comm;
473: } else {
474: errcomm = PETSC_COMM_SELF;
475: }
477: PetscCheck((int)info > IS_INFO_MIN && (int)info < IS_INFO_MAX, errcomm, PETSC_ERR_ARG_OUTOFRANGE, "Option %d is out of range", (int)info);
479: PetscCallMPI(MPI_Comm_size(comm, &size));
480: /* do not use global values if size == 1: it makes it easier to keep the implications straight */
481: if (size == 1) type = IS_LOCAL;
482: PetscCall(ISSetInfo_Internal(is, info, type, permanent ? IS_INFO_TRUE : IS_INFO_FALSE, flg));
483: PetscFunctionReturn(PETSC_SUCCESS);
484: }
486: static PetscErrorCode ISGetInfo_Sorted_Private(IS is, ISInfoType type, PetscBool *flg)
487: {
488: MPI_Comm comm;
489: PetscMPIInt size, rank;
491: PetscFunctionBegin;
492: comm = PetscObjectComm((PetscObject)is);
493: PetscCallMPI(MPI_Comm_size(comm, &size));
494: PetscCallMPI(MPI_Comm_rank(comm, &rank));
495: if (type == IS_GLOBAL && is->ops->sortedglobal) {
496: PetscUseTypeMethod(is, sortedglobal, flg);
497: } else {
498: /* determine if the array is locally sorted */
499: *flg = PETSC_FALSE;
500: if (type == IS_GLOBAL && size > 1) {
501: /* call ISGetInfo so that a cached value will be used if possible */
502: PetscCall(ISGetInfo(is, IS_SORTED, IS_LOCAL, PETSC_TRUE, flg));
503: } else if (is->ops->sortedlocal) {
504: PetscUseTypeMethod(is, sortedlocal, flg);
505: } else {
506: /* default: get the local indices and directly check */
507: const PetscInt *idx;
508: PetscInt n;
510: PetscCall(ISGetIndices(is, &idx));
511: PetscCall(ISGetLocalSize(is, &n));
512: PetscCall(PetscSortedInt(n, idx, flg));
513: PetscCall(ISRestoreIndices(is, &idx));
514: }
516: if (type == IS_GLOBAL && size > 1) {
517: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, flg, 1, MPI_C_BOOL, MPI_LAND, comm));
518: if (*flg) {
519: PetscInt n, min = PETSC_INT_MAX, max = PETSC_INT_MIN;
520: PetscInt maxprev;
522: PetscCall(ISGetLocalSize(is, &n));
523: if (n) PetscCall(ISGetMinMax(is, &min, &max));
524: maxprev = PETSC_INT_MIN;
525: PetscCallMPI(MPI_Exscan(&max, &maxprev, 1, MPIU_INT, MPI_MAX, comm));
526: if (rank && (maxprev > min)) *flg = PETSC_FALSE;
527: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, flg, 1, MPI_C_BOOL, MPI_LAND, comm));
528: }
529: }
530: }
531: PetscFunctionReturn(PETSC_SUCCESS);
532: }
534: static PetscErrorCode ISGetIndicesCopy_Private(IS is, PetscInt idx[]);
536: static PetscErrorCode ISGetInfo_Unique_Private(IS is, ISInfoType type, PetscBool *flg)
537: {
538: MPI_Comm comm;
539: PetscMPIInt size, rank;
540: PetscInt i;
542: PetscFunctionBegin;
543: comm = PetscObjectComm((PetscObject)is);
544: PetscCallMPI(MPI_Comm_size(comm, &size));
545: PetscCallMPI(MPI_Comm_rank(comm, &rank));
546: if (type == IS_GLOBAL && is->ops->uniqueglobal) PetscUseTypeMethod(is, uniqueglobal, flg);
547: else {
548: PetscInt n = -1;
549: PetscInt *idx = NULL;
551: /* determine if the array is locally unique */
552: if (type == IS_GLOBAL && size > 1) {
553: /* call ISGetInfo so that a cached value will be used if possible */
554: PetscCall(ISGetInfo(is, IS_UNIQUE, IS_LOCAL, PETSC_TRUE, flg));
555: } else if (is->ops->uniquelocal) {
556: PetscUseTypeMethod(is, uniquelocal, flg);
557: } else {
558: /* default: get the local indices and directly check */
559: *flg = PETSC_TRUE;
560: PetscCall(ISGetLocalSize(is, &n));
561: PetscCall(PetscMalloc1(n, &idx));
562: PetscCall(ISGetIndicesCopy_Private(is, idx));
563: PetscCall(PetscIntSortSemiOrdered(n, idx));
564: for (i = 1; i < n; i++)
565: if (idx[i] == idx[i - 1]) break;
566: if (i < n) *flg = PETSC_FALSE;
567: }
569: PetscCall(PetscFree(idx));
570: if (type == IS_GLOBAL && size > 1) {
571: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, flg, 1, MPI_C_BOOL, MPI_LAND, comm));
572: if (*flg) {
573: PetscInt min = PETSC_INT_MAX, max = PETSC_INT_MIN, maxprev;
575: if (!idx) {
576: PetscCall(ISGetLocalSize(is, &n));
577: PetscCall(PetscMalloc1(n, &idx));
578: PetscCall(ISGetIndicesCopy_Private(is, idx));
579: }
580: PetscCall(PetscParallelSortInt(is->map, is->map, idx, idx));
581: if (n) {
582: min = idx[0];
583: max = idx[n - 1];
584: }
585: for (i = 1; i < n; i++)
586: if (idx[i] == idx[i - 1]) break;
587: if (i < n) *flg = PETSC_FALSE;
588: maxprev = PETSC_INT_MIN;
589: PetscCallMPI(MPI_Exscan(&max, &maxprev, 1, MPIU_INT, MPI_MAX, comm));
590: if (rank && (maxprev == min)) *flg = PETSC_FALSE;
591: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, flg, 1, MPI_C_BOOL, MPI_LAND, comm));
592: }
593: }
594: PetscCall(PetscFree(idx));
595: }
596: PetscFunctionReturn(PETSC_SUCCESS);
597: }
599: static PetscErrorCode ISGetInfo_Permutation(IS is, ISInfoType type, PetscBool *flg)
600: {
601: MPI_Comm comm;
602: PetscMPIInt size;
604: PetscFunctionBegin;
605: comm = PetscObjectComm((PetscObject)is);
606: PetscCallMPI(MPI_Comm_size(comm, &size));
607: if (type == IS_GLOBAL && is->ops->permglobal) {
608: PetscUseTypeMethod(is, permglobal, flg);
609: } else if (type == IS_LOCAL && is->ops->permlocal) {
610: PetscUseTypeMethod(is, permlocal, flg);
611: } else {
612: PetscInt n, i, rStart;
613: PetscInt *idx;
615: PetscCall(ISGetLocalSize(is, &n));
616: PetscCall(PetscMalloc1(n, &idx));
617: PetscCall(ISGetIndicesCopy_Private(is, idx));
618: if (type == IS_GLOBAL) {
619: PetscCall(PetscParallelSortInt(is->map, is->map, idx, idx));
620: PetscCall(PetscLayoutGetRange(is->map, &rStart, NULL));
621: } else {
622: PetscCall(PetscIntSortSemiOrdered(n, idx));
623: rStart = 0;
624: }
625: *flg = PETSC_TRUE;
626: for (i = 0; i < n; i++) {
627: if (idx[i] != rStart + i) break;
628: }
629: if (i < n) *flg = PETSC_FALSE;
630: if (type == IS_GLOBAL && size > 1) PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, flg, 1, MPI_C_BOOL, MPI_LAND, comm));
631: PetscCall(PetscFree(idx));
632: }
633: PetscFunctionReturn(PETSC_SUCCESS);
634: }
636: static PetscErrorCode ISGetInfo_Interval(IS is, ISInfoType type, PetscBool *flg)
637: {
638: MPI_Comm comm;
639: PetscMPIInt size, rank;
640: PetscInt i;
642: PetscFunctionBegin;
643: comm = PetscObjectComm((PetscObject)is);
644: PetscCallMPI(MPI_Comm_size(comm, &size));
645: PetscCallMPI(MPI_Comm_rank(comm, &rank));
646: if (type == IS_GLOBAL && is->ops->intervalglobal) {
647: PetscUseTypeMethod(is, intervalglobal, flg);
648: } else {
649: /* determine if the array is locally an interval */
650: if (type == IS_GLOBAL && size > 1) {
651: /* call ISGetInfo so that a cached value will be used if possible */
652: PetscCall(ISGetInfo(is, IS_INTERVAL, IS_LOCAL, PETSC_TRUE, flg));
653: } else if (is->ops->intervallocal) {
654: PetscUseTypeMethod(is, intervallocal, flg);
655: } else {
656: PetscInt n;
657: const PetscInt *idx;
658: /* default: get the local indices and directly check */
659: *flg = PETSC_TRUE;
660: PetscCall(ISGetLocalSize(is, &n));
661: PetscCall(ISGetIndices(is, &idx));
662: for (i = 1; i < n; i++)
663: if (idx[i] != idx[i - 1] + 1) break;
664: if (i < n) *flg = PETSC_FALSE;
665: PetscCall(ISRestoreIndices(is, &idx));
666: }
668: if (type == IS_GLOBAL && size > 1) {
669: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, flg, 1, MPI_C_BOOL, MPI_LAND, comm));
670: if (*flg) {
671: PetscInt n, min = PETSC_INT_MAX, max = PETSC_INT_MIN;
672: PetscInt maxprev;
674: PetscCall(ISGetLocalSize(is, &n));
675: if (n) PetscCall(ISGetMinMax(is, &min, &max));
676: maxprev = PETSC_INT_MIN;
677: PetscCallMPI(MPI_Exscan(&max, &maxprev, 1, MPIU_INT, MPI_MAX, comm));
678: if (rank && n && (maxprev != min - 1)) *flg = PETSC_FALSE;
679: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, flg, 1, MPI_C_BOOL, MPI_LAND, comm));
680: }
681: }
682: }
683: PetscFunctionReturn(PETSC_SUCCESS);
684: }
686: static PetscErrorCode ISGetInfo_Identity(IS is, ISInfoType type, PetscBool *flg)
687: {
688: MPI_Comm comm;
689: PetscMPIInt size;
691: PetscFunctionBegin;
692: comm = PetscObjectComm((PetscObject)is);
693: PetscCallMPI(MPI_Comm_size(comm, &size));
694: if (type == IS_GLOBAL && is->ops->intervalglobal) {
695: PetscBool isinterval;
697: PetscUseTypeMethod(is, intervalglobal, &isinterval);
698: *flg = PETSC_FALSE;
699: if (isinterval) {
700: PetscInt min;
702: PetscCall(ISGetMinMax(is, &min, NULL));
703: PetscCallMPI(MPI_Bcast(&min, 1, MPIU_INT, 0, comm));
704: if (min == 0) *flg = PETSC_TRUE;
705: }
706: } else if (type == IS_LOCAL && is->ops->intervallocal) {
707: PetscBool isinterval;
709: PetscUseTypeMethod(is, intervallocal, &isinterval);
710: *flg = PETSC_FALSE;
711: if (isinterval) {
712: PetscInt min;
714: PetscCall(ISGetMinMax(is, &min, NULL));
715: if (min == 0) *flg = PETSC_TRUE;
716: }
717: } else {
718: PetscInt n, i, rStart;
719: const PetscInt *idx;
721: PetscCall(ISGetLocalSize(is, &n));
722: PetscCall(ISGetIndices(is, &idx));
723: PetscCall(PetscLayoutGetRange(is->map, &rStart, NULL));
724: *flg = PETSC_TRUE;
725: for (i = 0; i < n; i++) {
726: if (idx[i] != rStart + i) break;
727: }
728: if (i < n) *flg = PETSC_FALSE;
729: if (type == IS_GLOBAL && size > 1) PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, flg, 1, MPI_C_BOOL, MPI_LAND, comm));
730: PetscCall(ISRestoreIndices(is, &idx));
731: }
732: PetscFunctionReturn(PETSC_SUCCESS);
733: }
735: /*@
736: ISGetInfo - Determine whether an index set satisfies a given property
738: Collective or Logically Collective if the type is `IS_GLOBAL` (logically collective if the value of the property has been permanently set with `ISSetInfo()`)
740: Input Parameters:
741: + is - the index set
742: . info - describing a property of the index set, one of those listed in the documentation of `ISSetInfo()`
743: . 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
744: - type - whether the property is local (`IS_LOCAL`) or global (`IS_GLOBAL`)
746: Output Parameter:
747: . flg - whether the property is true (`PETSC_TRUE`) or false (`PETSC_FALSE`)
749: Level: advanced
751: Notes:
752: `ISGetInfo()` uses cached values when possible, which will be incorrect if `ISSetInfo()` has been called with incorrect information.
754: To clear cached values, use `ISClearInfoCache()`.
756: .seealso: `IS`, `ISInfo`, `ISInfoType`, `ISSetInfo()`, `ISClearInfoCache()`
757: @*/
758: PetscErrorCode ISGetInfo(IS is, ISInfo info, ISInfoType type, PetscBool compute, PetscBool *flg)
759: {
760: MPI_Comm comm, errcomm;
761: PetscMPIInt rank, size;
762: PetscInt itype;
763: PetscBool hasprop;
764: PetscBool infer;
766: PetscFunctionBegin;
769: comm = PetscObjectComm((PetscObject)is);
770: if (type == IS_GLOBAL) {
772: errcomm = comm;
773: } else {
774: errcomm = PETSC_COMM_SELF;
775: }
777: PetscCallMPI(MPI_Comm_size(comm, &size));
778: PetscCallMPI(MPI_Comm_rank(comm, &rank));
780: PetscCheck((int)info > IS_INFO_MIN && (int)info < IS_INFO_MAX, errcomm, PETSC_ERR_ARG_OUTOFRANGE, "Option %d is out of range", (int)info);
781: if (size == 1) type = IS_LOCAL;
782: itype = (type == IS_LOCAL) ? 0 : 1;
783: hasprop = PETSC_FALSE;
784: infer = PETSC_FALSE;
785: if (is->info_permanent[itype][(int)info]) {
786: hasprop = (is->info[itype][(int)info] == IS_INFO_TRUE) ? PETSC_TRUE : PETSC_FALSE;
787: infer = PETSC_TRUE;
788: } else if ((itype == IS_LOCAL) && (is->info[IS_LOCAL][info] != IS_INFO_UNKNOWN)) {
789: /* we can cache local properties as long as we clear them when the IS changes */
790: /* NOTE: we only cache local values because there is no ISAssemblyBegin()/ISAssemblyEnd(),
791: so we have no way of knowing when a cached value has been invalidated by changes on a different process */
792: hasprop = (is->info[itype][(int)info] == IS_INFO_TRUE) ? PETSC_TRUE : PETSC_FALSE;
793: infer = PETSC_TRUE;
794: } else if (compute) {
795: switch (info) {
796: case IS_SORTED:
797: PetscCall(ISGetInfo_Sorted_Private(is, type, &hasprop));
798: break;
799: case IS_UNIQUE:
800: PetscCall(ISGetInfo_Unique_Private(is, type, &hasprop));
801: break;
802: case IS_PERMUTATION:
803: PetscCall(ISGetInfo_Permutation(is, type, &hasprop));
804: break;
805: case IS_INTERVAL:
806: PetscCall(ISGetInfo_Interval(is, type, &hasprop));
807: break;
808: case IS_IDENTITY:
809: PetscCall(ISGetInfo_Identity(is, type, &hasprop));
810: break;
811: default:
812: SETERRQ(errcomm, PETSC_ERR_ARG_OUTOFRANGE, "Unknown IS property");
813: }
814: infer = PETSC_TRUE;
815: }
816: /* call ISSetInfo_Internal to keep all of the implications straight */
817: if (infer) PetscCall(ISSetInfo_Internal(is, info, type, IS_INFO_UNKNOWN, hasprop));
818: *flg = hasprop;
819: PetscFunctionReturn(PETSC_SUCCESS);
820: }
822: static PetscErrorCode ISCopyInfo_Private(IS source, IS dest)
823: {
824: PetscFunctionBegin;
825: PetscCall(PetscArraycpy(&dest->info[0], &source->info[0], 2));
826: PetscCall(PetscArraycpy(&dest->info_permanent[0], &source->info_permanent[0], 2));
827: PetscFunctionReturn(PETSC_SUCCESS);
828: }
830: /*@
831: ISIdentity - Determines whether index set is the identity mapping.
833: Collective
835: Input Parameter:
836: . is - the index set
838: Output Parameter:
839: . ident - `PETSC_TRUE` if an identity, else `PETSC_FALSE`
841: Level: intermediate
843: Note:
844: If `ISSetIdentity()` (or `ISSetInfo()` for a permanent property) has been called,
845: `ISIdentity()` will return its answer without communication between processes, but
846: otherwise the output ident will be computed from `ISGetInfo()`,
847: which may require synchronization on the communicator of `is`. To avoid this computation,
848: call `ISGetInfo()` directly with the compute flag set to `PETSC_FALSE`, and ident will be assumed false.
850: .seealso: `IS`, `ISSetIdentity()`, `ISGetInfo()`
851: @*/
852: PetscErrorCode ISIdentity(IS is, PetscBool *ident)
853: {
854: PetscFunctionBegin;
856: PetscAssertPointer(ident, 2);
857: PetscCall(ISGetInfo(is, IS_IDENTITY, IS_GLOBAL, PETSC_TRUE, ident));
858: PetscFunctionReturn(PETSC_SUCCESS);
859: }
861: /*@
862: ISSetIdentity - Informs the index set that it is an identity.
864: Logically Collective
866: Input Parameter:
867: . is - the index set
869: Level: intermediate
871: Notes:
872: `is` will be considered the identity permanently, even if indices have been changes (for example, with
873: `ISGeneralSetIndices()`). It's a good idea to only set this property if `is` will not change in the future.
875: To clear this property, use `ISClearInfoCache()`.
877: Developer Notes:
878: Some of these info routines have statements about values changing in the `IS`, this seems to contradict the fact that `IS` cannot be changed?
880: .seealso: `IS`, `ISIdentity()`, `ISSetInfo()`, `ISClearInfoCache()`
881: @*/
882: PetscErrorCode ISSetIdentity(IS is)
883: {
884: PetscFunctionBegin;
886: PetscCall(ISSetInfo(is, IS_IDENTITY, IS_GLOBAL, PETSC_TRUE, PETSC_TRUE));
887: PetscFunctionReturn(PETSC_SUCCESS);
888: }
890: /*@
891: ISContiguousLocal - Locates an index set with contiguous range within a global range, if possible
893: Not Collective
895: Input Parameters:
896: + is - the index set
897: . gstart - global start
898: - gend - global end
900: Output Parameters:
901: + start - start of contiguous block, as an offset from `gstart`
902: - contig - `PETSC_TRUE` if the index set refers to contiguous entries on this process, else `PETSC_FALSE`
904: Level: developer
906: .seealso: `IS`, `ISGetLocalSize()`, `VecGetOwnershipRange()`
907: @*/
908: PetscErrorCode ISContiguousLocal(IS is, PetscInt gstart, PetscInt gend, PetscInt *start, PetscBool *contig)
909: {
910: PetscFunctionBegin;
912: PetscAssertPointer(start, 4);
913: PetscAssertPointer(contig, 5);
914: 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);
915: *start = -1;
916: *contig = PETSC_FALSE;
917: PetscTryTypeMethod(is, contiguous, gstart, gend, start, contig);
918: PetscFunctionReturn(PETSC_SUCCESS);
919: }
921: /*@
922: ISPermutation - `PETSC_TRUE` or `PETSC_FALSE` depending on whether the
923: index set has been declared to be a permutation.
925: Logically Collective
927: Input Parameter:
928: . is - the index set
930: Output Parameter:
931: . perm - `PETSC_TRUE` if a permutation, else `PETSC_FALSE`
933: Level: intermediate
935: Note:
936: If it is not already known that `is` is a permutation (if `ISSetPermutation()`
937: or `ISSetInfo()` has not been called), this routine will not attempt to compute
938: whether the index set is a permutation and will assume `perm` is `PETSC_FALSE`.
939: To compute the value when it is not already known, use `ISGetInfo()` with
940: the compute flag set to `PETSC_TRUE`.
942: Developer Notes:
943: Perhaps some of these routines should use the `PetscBool3` enum to return appropriate values
945: .seealso: `IS`, `ISSetPermutation()`, `ISGetInfo()`
946: @*/
947: PetscErrorCode ISPermutation(IS is, PetscBool *perm)
948: {
949: PetscFunctionBegin;
951: PetscAssertPointer(perm, 2);
952: PetscCall(ISGetInfo(is, IS_PERMUTATION, IS_GLOBAL, PETSC_FALSE, perm));
953: PetscFunctionReturn(PETSC_SUCCESS);
954: }
956: /*@
957: ISSetPermutation - Informs the index set that it is a permutation.
959: Logically Collective
961: Input Parameter:
962: . is - the index set
964: Level: intermediate
966: Notes:
967: `is` will be considered a permutation permanently, even if indices have been changes (for example, with
968: `ISGeneralSetIndices()`). It's a good idea to only set this property if `is` will not change in the future.
970: To clear this property, use `ISClearInfoCache()`.
972: The debug version of the libraries (./configure --with-debugging=1) checks if the
973: index set is actually a permutation. The optimized version just believes you.
975: .seealso: `IS`, `ISPermutation()`, `ISSetInfo()`, `ISClearInfoCache().`
976: @*/
977: PetscErrorCode ISSetPermutation(IS is)
978: {
979: PetscFunctionBegin;
981: if (PetscDefined(USE_DEBUG)) {
982: PetscMPIInt size;
984: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)is), &size));
985: if (size == 1) {
986: PetscInt i, n, *idx;
987: const PetscInt *iidx;
989: PetscCall(ISGetSize(is, &n));
990: PetscCall(PetscMalloc1(n, &idx));
991: PetscCall(ISGetIndices(is, &iidx));
992: PetscCall(PetscArraycpy(idx, iidx, n));
993: PetscCall(PetscIntSortSemiOrdered(n, idx));
994: for (i = 0; i < n; i++) PetscCheck(idx[i] == i, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Index set is not a permutation");
995: PetscCall(PetscFree(idx));
996: PetscCall(ISRestoreIndices(is, &iidx));
997: }
998: }
999: PetscCall(ISSetInfo(is, IS_PERMUTATION, IS_GLOBAL, PETSC_TRUE, PETSC_TRUE));
1000: PetscFunctionReturn(PETSC_SUCCESS);
1001: }
1003: /*@
1004: ISDestroy - Destroys an index set.
1006: Collective
1008: Input Parameter:
1009: . is - the index set
1011: Level: beginner
1013: .seealso: `IS`, `ISCreateGeneral()`, `ISCreateStride()`, `ISCreateBlock()`
1014: @*/
1015: PetscErrorCode ISDestroy(IS *is)
1016: {
1017: PetscFunctionBegin;
1018: if (!*is) PetscFunctionReturn(PETSC_SUCCESS);
1020: if (--((PetscObject)*is)->refct > 0) {
1021: *is = NULL;
1022: PetscFunctionReturn(PETSC_SUCCESS);
1023: }
1024: if ((*is)->complement) {
1025: PetscInt refcnt;
1026: PetscCall(PetscObjectGetReference((PetscObject)((*is)->complement), &refcnt));
1027: PetscCheck(refcnt <= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Nonlocal IS has not been restored");
1028: PetscCall(ISDestroy(&(*is)->complement));
1029: }
1030: PetscTryTypeMethod(*is, destroy);
1031: PetscCall(PetscLayoutDestroy(&(*is)->map));
1032: /* Destroy local representations of offproc data. */
1033: PetscCall(PetscFree((*is)->total));
1034: PetscCall(PetscFree((*is)->nonlocal));
1035: PetscCall(PetscHeaderDestroy(is));
1036: PetscFunctionReturn(PETSC_SUCCESS);
1037: }
1039: /*@
1040: ISInvertPermutation - Creates a new permutation that is the inverse of
1041: a given permutation.
1043: Collective
1045: Input Parameters:
1046: + is - the index set
1047: - nlocal - number of indices on this processor in result (ignored for 1 processor) or
1048: use `PETSC_DECIDE`
1050: Output Parameter:
1051: . isout - the inverse permutation
1053: Level: intermediate
1055: Note:
1056: For parallel index sets this does the complete parallel permutation, but the
1057: code is not efficient for huge index sets (10,000,000 indices).
1059: .seealso: `IS`, `ISGetInfo()`, `ISSetPermutation()`, `ISGetPermutation()`
1060: @*/
1061: PetscErrorCode ISInvertPermutation(IS is, PetscInt nlocal, IS *isout)
1062: {
1063: PetscBool isperm, isidentity, issame;
1065: PetscFunctionBegin;
1067: PetscAssertPointer(isout, 3);
1068: PetscCall(ISGetInfo(is, IS_PERMUTATION, IS_GLOBAL, PETSC_TRUE, &isperm));
1069: PetscCheck(isperm, PetscObjectComm((PetscObject)is), PETSC_ERR_ARG_WRONG, "Not a permutation");
1070: PetscCall(ISGetInfo(is, IS_IDENTITY, IS_GLOBAL, PETSC_TRUE, &isidentity));
1071: issame = PETSC_FALSE;
1072: if (isidentity) {
1073: PetscInt n;
1075: PetscCall(ISGetLocalSize(is, &n));
1076: issame = (PetscBool)(n == nlocal);
1077: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &issame, 1, MPI_C_BOOL, MPI_LAND, PetscObjectComm((PetscObject)is)));
1078: }
1079: if (issame) PetscCall(ISDuplicate(is, isout));
1080: else {
1081: PetscUseTypeMethod(is, invertpermutation, nlocal, isout);
1082: PetscCall(ISSetPermutation(*isout));
1083: }
1084: PetscFunctionReturn(PETSC_SUCCESS);
1085: }
1087: /*@
1088: ISGetSize - Returns the global length of an index set.
1090: Not Collective
1092: Input Parameter:
1093: . is - the index set
1095: Output Parameter:
1096: . size - the global size
1098: Level: beginner
1100: .seealso: `IS`
1101: @*/
1102: PetscErrorCode ISGetSize(IS is, PetscInt *size)
1103: {
1104: PetscFunctionBegin;
1106: PetscAssertPointer(size, 2);
1107: *size = is->map->N;
1108: PetscFunctionReturn(PETSC_SUCCESS);
1109: }
1111: /*@
1112: ISGetLocalSize - Returns the local (processor) length of an index set.
1114: Not Collective
1116: Input Parameter:
1117: . is - the index set
1119: Output Parameter:
1120: . size - the local size
1122: Level: beginner
1124: .seealso: `IS`, `ISGetSize()`
1125: @*/
1126: PetscErrorCode ISGetLocalSize(IS is, PetscInt *size)
1127: {
1128: PetscFunctionBegin;
1130: PetscAssertPointer(size, 2);
1131: *size = is->map->n;
1132: PetscFunctionReturn(PETSC_SUCCESS);
1133: }
1135: /*@
1136: ISGetLayout - get `PetscLayout` describing index set layout
1138: Not Collective
1140: Input Parameter:
1141: . is - the index set
1143: Output Parameter:
1144: . map - the layout
1146: Level: developer
1148: .seealso: `IS`, `PetscLayout`, `ISSetLayout()`, `ISGetSize()`, `ISGetLocalSize()`
1149: @*/
1150: PetscErrorCode ISGetLayout(IS is, PetscLayout *map)
1151: {
1152: PetscFunctionBegin;
1154: PetscAssertPointer(map, 2);
1155: *map = is->map;
1156: PetscFunctionReturn(PETSC_SUCCESS);
1157: }
1159: /*@
1160: ISSetLayout - set `PetscLayout` describing index set layout
1162: Collective
1164: Input Parameters:
1165: + is - the index set
1166: - map - the layout
1168: Level: developer
1170: Notes:
1171: Users should typically use higher level functions such as `ISCreateGeneral()`.
1173: This function can be useful in some special cases of constructing a new `IS`, e.g. after `ISCreate()` and before `ISLoad()`.
1174: Otherwise, it is only valid to replace the layout with a layout known to be equivalent.
1176: .seealso: `IS`, `PetscLayout`, `ISCreate()`, `ISGetLayout()`, `ISGetSize()`, `ISGetLocalSize()`
1177: @*/
1178: PetscErrorCode ISSetLayout(IS is, PetscLayout map)
1179: {
1180: PetscFunctionBegin;
1182: PetscAssertPointer(map, 2);
1183: PetscCall(PetscLayoutReference(map, &is->map));
1184: PetscFunctionReturn(PETSC_SUCCESS);
1185: }
1187: /*@
1188: ISGetIndices - Returns a pointer to the indices. The user should call
1189: `ISRestoreIndices()` after having looked at the indices. The user should
1190: NOT change the indices.
1192: Not Collective
1194: Input Parameter:
1195: . is - the index set
1197: Output Parameter:
1198: . ptr - the location to put the pointer to the indices
1200: Level: intermediate
1202: Fortran Note:
1203: .vb
1204: PetscInt, pointer :: ptr(:)
1205: .ve
1207: .seealso: `IS`, `ISRestoreIndices()`
1208: @*/
1209: PetscErrorCode ISGetIndices(IS is, const PetscInt *ptr[])
1210: {
1211: PetscFunctionBegin;
1213: PetscAssertPointer(ptr, 2);
1214: PetscUseTypeMethod(is, getindices, ptr);
1215: PetscFunctionReturn(PETSC_SUCCESS);
1216: }
1218: /*@
1219: ISGetMinMax - Gets the minimum and maximum values in an `IS`
1221: Not Collective
1223: Input Parameter:
1224: . is - the index set
1226: Output Parameters:
1227: + min - the minimum value, you may pass `NULL`
1228: - max - the maximum value, you may pass `NULL`
1230: Level: intermediate
1232: Notes:
1233: Empty index sets return min=`PETSC_INT_MAX` and max=`PETSC_INT_MIN`.
1235: In parallel, it returns the `min` and `max` of the local portion of `is`
1237: .seealso: `IS`, `ISGetIndices()`, `ISRestoreIndices()`
1238: @*/
1239: PetscErrorCode ISGetMinMax(IS is, PetscInt *min, PetscInt *max)
1240: {
1241: PetscFunctionBegin;
1243: if (min) *min = is->min;
1244: if (max) *max = is->max;
1245: PetscFunctionReturn(PETSC_SUCCESS);
1246: }
1248: /*@
1249: ISLocate - determine the location of an index within the local component of an index set
1251: Not Collective
1253: Input Parameters:
1254: + is - the index set
1255: - key - the search key
1257: Output Parameter:
1258: . location - if >= 0, a location within the index set that is equal to the key, otherwise the key is not in the index set
1260: Level: intermediate
1262: .seealso: `IS`
1263: @*/
1264: PetscErrorCode ISLocate(IS is, PetscInt key, PetscInt *location)
1265: {
1266: PetscFunctionBegin;
1267: if (is->ops->locate) PetscUseTypeMethod(is, locate, key, location);
1268: else {
1269: PetscInt numIdx;
1270: PetscBool sorted;
1271: const PetscInt *idx;
1273: PetscCall(ISGetLocalSize(is, &numIdx));
1274: PetscCall(ISGetIndices(is, &idx));
1275: PetscCall(ISSorted(is, &sorted));
1276: if (sorted) {
1277: PetscCall(PetscFindInt(key, numIdx, idx, location));
1278: } else {
1279: *location = -1;
1280: for (PetscInt i = 0; i < numIdx; i++) {
1281: if (idx[i] == key) {
1282: *location = i;
1283: break;
1284: }
1285: }
1286: }
1287: PetscCall(ISRestoreIndices(is, &idx));
1288: }
1289: PetscFunctionReturn(PETSC_SUCCESS);
1290: }
1292: /*@
1293: ISRestoreIndices - Restores an index set to a usable state after a call to `ISGetIndices()`.
1295: Not Collective
1297: Input Parameters:
1298: + is - the index set
1299: - ptr - the pointer obtained by `ISGetIndices()`
1301: Level: intermediate
1303: Fortran Note:
1304: .vb
1305: PetscInt, pointer :: ptr(:)
1306: .ve
1308: .seealso: `IS`, `ISGetIndices()`
1309: @*/
1310: PetscErrorCode ISRestoreIndices(IS is, const PetscInt *ptr[])
1311: {
1312: PetscFunctionBegin;
1314: PetscAssertPointer(ptr, 2);
1315: PetscTryTypeMethod(is, restoreindices, ptr);
1316: PetscFunctionReturn(PETSC_SUCCESS);
1317: }
1319: static PetscErrorCode ISGatherTotal_Private(IS is)
1320: {
1321: PetscInt i, n, N;
1322: const PetscInt *lindices;
1323: MPI_Comm comm;
1324: PetscMPIInt rank, size, *sizes = NULL, *offsets = NULL, nn;
1326: PetscFunctionBegin;
1329: PetscCall(PetscObjectGetComm((PetscObject)is, &comm));
1330: PetscCallMPI(MPI_Comm_size(comm, &size));
1331: PetscCallMPI(MPI_Comm_rank(comm, &rank));
1332: PetscCall(ISGetLocalSize(is, &n));
1333: PetscCall(PetscMalloc2(size, &sizes, size, &offsets));
1335: PetscCall(PetscMPIIntCast(n, &nn));
1336: PetscCallMPI(MPI_Allgather(&nn, 1, MPI_INT, sizes, 1, MPI_INT, comm));
1337: offsets[0] = 0;
1338: for (i = 1; i < size; ++i) offsets[i] = offsets[i - 1] + sizes[i - 1];
1339: N = offsets[size - 1] + sizes[size - 1];
1341: PetscCall(PetscMalloc1(N, &is->total));
1342: PetscCall(ISGetIndices(is, &lindices));
1343: PetscCallMPI(MPI_Allgatherv((void *)lindices, nn, MPIU_INT, is->total, sizes, offsets, MPIU_INT, comm));
1344: PetscCall(ISRestoreIndices(is, &lindices));
1345: is->local_offset = offsets[rank];
1346: PetscCall(PetscFree2(sizes, offsets));
1347: PetscFunctionReturn(PETSC_SUCCESS);
1348: }
1350: /*@
1351: ISGetTotalIndices - Retrieve an array containing all indices across the communicator.
1353: Collective
1355: Input Parameter:
1356: . is - the index set
1358: Output Parameter:
1359: . indices - total indices with rank 0 indices first, and so on; total array size is
1360: the same as returned with `ISGetSize()`.
1362: Level: intermediate
1364: Notes:
1365: this is potentially nonscalable, but depends on the size of the total index set
1366: and the size of the communicator. This may be feasible for index sets defined on
1367: subcommunicators, such that the set size does not grow with `PETSC_WORLD_COMM`.
1368: Note also that there is no way to tell where the local part of the indices starts
1369: (use `ISGetIndices()` and `ISGetNonlocalIndices()` to retrieve just the local and just
1370: the nonlocal part (complement), respectively).
1372: .seealso: `IS`, `ISRestoreTotalIndices()`, `ISGetNonlocalIndices()`, `ISGetSize()`
1373: @*/
1374: PetscErrorCode ISGetTotalIndices(IS is, const PetscInt *indices[])
1375: {
1376: PetscMPIInt size;
1378: PetscFunctionBegin;
1380: PetscAssertPointer(indices, 2);
1381: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)is), &size));
1382: if (size == 1) PetscUseTypeMethod(is, getindices, indices);
1383: else {
1384: if (!is->total) PetscCall(ISGatherTotal_Private(is));
1385: *indices = is->total;
1386: }
1387: PetscFunctionReturn(PETSC_SUCCESS);
1388: }
1390: /*@
1391: ISRestoreTotalIndices - Restore the index array obtained with `ISGetTotalIndices()`.
1393: Not Collective.
1395: Input Parameters:
1396: + is - the index set
1397: - indices - index array; must be the array obtained with `ISGetTotalIndices()`
1399: Level: intermediate
1401: .seealso: `IS`, `ISGetNonlocalIndices()`
1402: @*/
1403: PetscErrorCode ISRestoreTotalIndices(IS is, const PetscInt *indices[])
1404: {
1405: PetscMPIInt size;
1407: PetscFunctionBegin;
1409: PetscAssertPointer(indices, 2);
1410: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)is), &size));
1411: if (size == 1) {
1412: PetscUseTypeMethod(is, restoreindices, indices);
1413: } else {
1414: 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.");
1415: }
1416: PetscFunctionReturn(PETSC_SUCCESS);
1417: }
1419: /*@
1420: ISGetNonlocalIndices - Retrieve an array of indices from remote processors
1421: in this communicator.
1423: Collective
1425: Input Parameter:
1426: . is - the index set
1428: Output Parameter:
1429: . indices - indices with rank 0 indices first, and so on, omitting
1430: the current rank. Total number of indices is the difference
1431: total and local, obtained with `ISGetSize()` and `ISGetLocalSize()`,
1432: respectively.
1434: Level: intermediate
1436: Notes:
1437: Restore the indices using `ISRestoreNonlocalIndices()`.
1439: The same scalability considerations as those for `ISGetTotalIndices()` apply here.
1441: .seealso: `IS`, `ISGetTotalIndices()`, `ISRestoreNonlocalIndices()`, `ISGetSize()`, `ISGetLocalSize().`
1442: @*/
1443: PetscErrorCode ISGetNonlocalIndices(IS is, const PetscInt *indices[])
1444: {
1445: PetscMPIInt size;
1446: PetscInt n, N;
1448: PetscFunctionBegin;
1450: PetscAssertPointer(indices, 2);
1451: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)is), &size));
1452: if (size == 1) *indices = NULL;
1453: else {
1454: if (!is->total) PetscCall(ISGatherTotal_Private(is));
1455: PetscCall(ISGetLocalSize(is, &n));
1456: PetscCall(ISGetSize(is, &N));
1457: PetscCall(PetscMalloc1(N - n, &is->nonlocal));
1458: PetscCall(PetscArraycpy(is->nonlocal, is->total, is->local_offset));
1459: PetscCall(PetscArraycpy(is->nonlocal + is->local_offset, is->total + is->local_offset + n, N - is->local_offset - n));
1460: *indices = is->nonlocal;
1461: }
1462: PetscFunctionReturn(PETSC_SUCCESS);
1463: }
1465: /*@
1466: ISRestoreNonlocalIndices - Restore the index array obtained with `ISGetNonlocalIndices()`.
1468: Not Collective.
1470: Input Parameters:
1471: + is - the index set
1472: - indices - index array; must be the array obtained with `ISGetNonlocalIndices()`
1474: Level: intermediate
1476: .seealso: `IS`, `ISGetTotalIndices()`, `ISGetNonlocalIndices()`, `ISRestoreTotalIndices()`
1477: @*/
1478: PetscErrorCode ISRestoreNonlocalIndices(IS is, const PetscInt *indices[])
1479: {
1480: PetscFunctionBegin;
1482: PetscAssertPointer(indices, 2);
1483: 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.");
1484: PetscFunctionReturn(PETSC_SUCCESS);
1485: }
1487: /*@
1488: ISGetNonlocalIS - Gather all nonlocal indices for this `IS` and present
1489: them as another sequential index set.
1491: Collective
1493: Input Parameter:
1494: . is - the index set
1496: Output Parameter:
1497: . complement - sequential `IS` with indices identical to the result of
1498: `ISGetNonlocalIndices()`
1500: Level: intermediate
1502: Notes:
1503: Complement represents the result of `ISGetNonlocalIndices()` as an `IS`.
1504: Therefore scalability issues similar to `ISGetNonlocalIndices()` apply.
1506: The resulting `IS` must be restored using `ISRestoreNonlocalIS()`.
1508: .seealso: `IS`, `ISGetNonlocalIndices()`, `ISRestoreNonlocalIndices()`, `ISAllGather()`, `ISGetSize()`
1509: @*/
1510: PetscErrorCode ISGetNonlocalIS(IS is, IS *complement)
1511: {
1512: PetscFunctionBegin;
1514: PetscAssertPointer(complement, 2);
1515: /* Check if the complement exists already. */
1516: if (is->complement) {
1517: *complement = is->complement;
1518: PetscCall(PetscObjectReference((PetscObject)is->complement));
1519: } else {
1520: PetscInt N, n;
1521: const PetscInt *idx;
1522: PetscCall(ISGetSize(is, &N));
1523: PetscCall(ISGetLocalSize(is, &n));
1524: PetscCall(ISGetNonlocalIndices(is, &idx));
1525: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, N - n, idx, PETSC_USE_POINTER, &is->complement));
1526: PetscCall(PetscObjectReference((PetscObject)is->complement));
1527: *complement = is->complement;
1528: }
1529: PetscFunctionReturn(PETSC_SUCCESS);
1530: }
1532: /*@
1533: ISRestoreNonlocalIS - Restore the `IS` obtained with `ISGetNonlocalIS()`.
1535: Not collective.
1537: Input Parameters:
1538: + is - the index set
1539: - complement - index set of `is`'s nonlocal indices
1541: Level: intermediate
1543: .seealso: `IS`, `ISGetNonlocalIS()`, `ISGetNonlocalIndices()`, `ISRestoreNonlocalIndices()`
1544: @*/
1545: PetscErrorCode ISRestoreNonlocalIS(IS is, IS *complement)
1546: {
1547: PetscInt refcnt;
1549: PetscFunctionBegin;
1551: PetscAssertPointer(complement, 2);
1552: PetscCheck(*complement == is->complement, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Complement IS being restored was not obtained with ISGetNonlocalIS()");
1553: PetscCall(PetscObjectGetReference((PetscObject)is->complement, &refcnt));
1554: PetscCheck(refcnt > 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Duplicate call to ISRestoreNonlocalIS() detected");
1555: PetscCall(PetscObjectDereference((PetscObject)is->complement));
1556: PetscFunctionReturn(PETSC_SUCCESS);
1557: }
1559: /*@
1560: ISViewFromOptions - View an `IS` based on options in the options database
1562: Collective
1564: Input Parameters:
1565: + A - the index set
1566: . obj - Optional object that provides the prefix for the options database
1567: - name - command line option
1569: Options Database Key:
1570: . -name [viewertype][:...] - option name and values. See `PetscObjectViewFromOptions()` for the possible arguments
1572: Level: intermediate
1574: .seealso: `IS`, `ISView()`, `PetscObjectViewFromOptions()`, `ISCreate()`
1575: @*/
1576: PetscErrorCode ISViewFromOptions(IS A, PetscObject obj, const char name[])
1577: {
1578: PetscFunctionBegin;
1580: PetscCall(PetscObjectViewFromOptions((PetscObject)A, obj, name));
1581: PetscFunctionReturn(PETSC_SUCCESS);
1582: }
1584: /*@
1585: ISView - Displays an index set.
1587: Collective
1589: Input Parameters:
1590: + is - the index set
1591: - viewer - viewer used to display the set, for example `PETSC_VIEWER_STDOUT_SELF`.
1593: Level: intermediate
1595: .seealso: `IS`, `PetscViewer`, `PetscViewerASCIIOpen()`, `ISViewFromOptions()`
1596: @*/
1597: PetscErrorCode ISView(IS is, PetscViewer viewer)
1598: {
1599: PetscFunctionBegin;
1601: if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)is), &viewer));
1603: PetscCheckSameComm(is, 1, viewer, 2);
1605: PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)is, viewer));
1606: PetscCall(PetscLogEventBegin(IS_View, is, viewer, 0, 0));
1607: PetscUseTypeMethod(is, view, viewer);
1608: PetscCall(PetscLogEventEnd(IS_View, is, viewer, 0, 0));
1609: PetscFunctionReturn(PETSC_SUCCESS);
1610: }
1612: /*@
1613: ISLoad - Loads an index set that has been stored in binary or HDF5 format with `ISView()`.
1615: Collective
1617: Input Parameters:
1618: + is - the newly loaded index set, this needs to have been created with `ISCreate()` or some related function before a call to `ISLoad()`.
1619: - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` or HDF5 file viewer, obtained from `PetscViewerHDF5Open()`
1621: Level: intermediate
1623: Notes:
1624: IF using HDF5, you must assign the IS the same name as was used in `is`
1625: that was stored in the file using `PetscObjectSetName()`. Otherwise you will
1626: get the error message: "Cannot H5DOpen2() with Vec name NAMEOFOBJECT"
1628: .seealso: `IS`, `PetscViewerBinaryOpen()`, `ISView()`, `MatLoad()`, `VecLoad()`
1629: @*/
1630: PetscErrorCode ISLoad(IS is, PetscViewer viewer)
1631: {
1632: PetscBool isbinary, ishdf5;
1634: PetscFunctionBegin;
1637: PetscCheckSameComm(is, 1, viewer, 2);
1638: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
1639: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
1640: PetscCheck(isbinary || ishdf5, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen()");
1641: if (!((PetscObject)is)->type_name) PetscCall(ISSetType(is, ISGENERAL));
1642: PetscCall(PetscLogEventBegin(IS_Load, is, viewer, 0, 0));
1643: PetscUseTypeMethod(is, load, viewer);
1644: PetscCall(PetscLogEventEnd(IS_Load, is, viewer, 0, 0));
1645: PetscFunctionReturn(PETSC_SUCCESS);
1646: }
1648: /*@
1649: ISSort - Sorts the indices of an index set.
1651: Collective
1653: Input Parameter:
1654: . is - the index set
1656: Level: intermediate
1658: .seealso: `IS`, `ISSortRemoveDups()`, `ISSorted()`
1659: @*/
1660: PetscErrorCode ISSort(IS is)
1661: {
1662: PetscBool flg;
1664: PetscFunctionBegin;
1666: PetscCall(ISGetInfo(is, IS_SORTED, IS_LOCAL, PETSC_FALSE, &flg));
1667: if (!flg) {
1668: PetscUseTypeMethod(is, sort);
1669: PetscCall(ISSetInfo(is, IS_SORTED, IS_LOCAL, is->info_permanent[IS_LOCAL][IS_SORTED], PETSC_TRUE));
1670: }
1671: PetscFunctionReturn(PETSC_SUCCESS);
1672: }
1674: /*@
1675: ISSortRemoveDups - Sorts the indices of an index set, removing duplicates.
1677: Collective
1679: Input Parameter:
1680: . is - the index set
1682: Level: intermediate
1684: .seealso: `IS`, `ISSort()`, `ISSorted()`
1685: @*/
1686: PetscErrorCode ISSortRemoveDups(IS is)
1687: {
1688: PetscFunctionBegin;
1690: PetscCall(ISClearInfoCache(is, PETSC_FALSE));
1691: PetscUseTypeMethod(is, sortremovedups);
1692: PetscCall(ISSetInfo(is, IS_SORTED, IS_LOCAL, is->info_permanent[IS_LOCAL][IS_SORTED], PETSC_TRUE));
1693: PetscCall(ISSetInfo(is, IS_UNIQUE, IS_LOCAL, is->info_permanent[IS_LOCAL][IS_UNIQUE], PETSC_TRUE));
1694: PetscFunctionReturn(PETSC_SUCCESS);
1695: }
1697: /*@
1698: ISToGeneral - Converts an IS object of any type to `ISGENERAL` type
1700: Collective
1702: Input Parameter:
1703: . is - the index set
1705: Level: intermediate
1707: .seealso: `IS`, `ISSorted()`
1708: @*/
1709: PetscErrorCode ISToGeneral(IS is)
1710: {
1711: PetscFunctionBegin;
1713: PetscUseTypeMethod(is, togeneral);
1714: PetscFunctionReturn(PETSC_SUCCESS);
1715: }
1717: /*@
1718: ISSorted - Checks the indices to determine whether they have been sorted.
1720: Not Collective
1722: Input Parameter:
1723: . is - the index set
1725: Output Parameter:
1726: . flg - output flag, either `PETSC_TRUE` if the index set is sorted,
1727: or `PETSC_FALSE` otherwise.
1729: Level: intermediate
1731: Note:
1732: For parallel IS objects this only indicates if the local part of `is`
1733: is sorted. So some processors may return `PETSC_TRUE` while others may
1734: return `PETSC_FALSE`.
1736: .seealso: `ISSort()`, `ISSortRemoveDups()`
1737: @*/
1738: PetscErrorCode ISSorted(IS is, PetscBool *flg)
1739: {
1740: PetscFunctionBegin;
1742: PetscAssertPointer(flg, 2);
1743: PetscCall(ISGetInfo(is, IS_SORTED, IS_LOCAL, PETSC_TRUE, flg));
1744: PetscFunctionReturn(PETSC_SUCCESS);
1745: }
1747: /*@
1748: ISDuplicate - Creates a duplicate copy of an index set.
1750: Collective
1752: Input Parameter:
1753: . is - the index set
1755: Output Parameter:
1756: . newIS - the copy of the index set
1758: Level: beginner
1760: .seealso: `IS`, `ISCreateGeneral()`, `ISCopy()`
1761: @*/
1762: PetscErrorCode ISDuplicate(IS is, IS *newIS)
1763: {
1764: PetscFunctionBegin;
1766: PetscAssertPointer(newIS, 2);
1767: PetscUseTypeMethod(is, duplicate, newIS);
1768: PetscCall(ISCopyInfo_Private(is, *newIS));
1769: PetscFunctionReturn(PETSC_SUCCESS);
1770: }
1772: /*@
1773: ISCopy - Copies an index set.
1775: Collective
1777: Input Parameter:
1778: . is - the index set
1780: Output Parameter:
1781: . isy - the copy of the index set
1783: Level: beginner
1785: .seealso: `IS`, `ISDuplicate()`, `ISShift()`
1786: @*/
1787: PetscErrorCode ISCopy(IS is, IS isy)
1788: {
1789: PetscInt bs, bsy;
1791: PetscFunctionBegin;
1794: PetscCheckSameComm(is, 1, isy, 2);
1795: if (is == isy) PetscFunctionReturn(PETSC_SUCCESS);
1796: PetscCall(PetscLayoutGetBlockSize(is->map, &bs));
1797: PetscCall(PetscLayoutGetBlockSize(isy->map, &bsy));
1798: 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);
1799: 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);
1800: PetscCheck(bs == bsy, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Index sets have different block size %" PetscInt_FMT " != %" PetscInt_FMT, bs, bsy);
1801: PetscCall(ISCopyInfo_Private(is, isy));
1802: isy->max = is->max;
1803: isy->min = is->min;
1804: PetscUseTypeMethod(is, copy, isy);
1805: PetscFunctionReturn(PETSC_SUCCESS);
1806: }
1808: /*@
1809: ISShift - Shift all indices by given offset
1811: Collective
1813: Input Parameters:
1814: + is - the index set
1815: - offset - the offset
1817: Output Parameter:
1818: . isy - the shifted copy of the input index set
1820: Level: beginner
1822: Notes:
1823: The `offset` can be different across processes.
1825: `is` and `isy` can be the same.
1827: .seealso: `ISDuplicate()`, `ISCopy()`
1828: @*/
1829: PetscErrorCode ISShift(IS is, PetscInt offset, IS isy)
1830: {
1831: PetscFunctionBegin;
1834: PetscCheckSameComm(is, 1, isy, 3);
1835: if (!offset) {
1836: PetscCall(ISCopy(is, isy));
1837: PetscFunctionReturn(PETSC_SUCCESS);
1838: }
1839: 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);
1840: 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);
1841: 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);
1842: PetscCall(ISCopyInfo_Private(is, isy));
1843: if (offset > 0) {
1844: isy->max = is->max < PETSC_INT_MAX - offset ? is->max + offset : PETSC_INT_MAX;
1845: isy->min = is->min < PETSC_INT_MAX - offset ? is->min + offset : PETSC_INT_MAX;
1846: } else {
1847: isy->max = is->max > PETSC_INT_MIN - offset ? is->max + offset : PETSC_INT_MIN;
1848: isy->min = is->min > PETSC_INT_MIN - offset ? is->min + offset : PETSC_INT_MIN;
1849: }
1850: PetscUseMethod(is, "ISShift_C", (IS, PetscInt, IS), (is, offset, isy));
1851: PetscFunctionReturn(PETSC_SUCCESS);
1852: }
1854: /*@
1855: ISOnComm - Split a parallel `IS` on subcomms (usually self) or concatenate index sets on subcomms into a parallel index set
1857: Collective
1859: Input Parameters:
1860: + is - index set
1861: . comm - communicator for new index set
1862: - mode - copy semantics, `PETSC_USE_POINTER` for no-copy if possible, otherwise `PETSC_COPY_VALUES`
1864: Output Parameter:
1865: . newis - new `IS` on `comm`
1867: Level: advanced
1869: Notes:
1870: It is usually desirable to create a parallel `IS` and look at the local part when necessary.
1872: This function is useful if serial `IS`s must be created independently, or to view many
1873: logically independent serial `IS`s.
1875: The input `IS` must have the same type on every MPI process.
1877: .seealso: `IS`
1878: @*/
1879: PetscErrorCode ISOnComm(IS is, MPI_Comm comm, PetscCopyMode mode, IS *newis)
1880: {
1881: PetscMPIInt match;
1883: PetscFunctionBegin;
1885: PetscAssertPointer(newis, 4);
1886: PetscCallMPI(MPI_Comm_compare(PetscObjectComm((PetscObject)is), comm, &match));
1887: if (mode != PETSC_COPY_VALUES && (match == MPI_IDENT || match == MPI_CONGRUENT)) {
1888: PetscCall(PetscObjectReference((PetscObject)is));
1889: *newis = is;
1890: } else PetscUseTypeMethod(is, oncomm, comm, mode, newis);
1891: PetscFunctionReturn(PETSC_SUCCESS);
1892: }
1894: /*@
1895: ISSetBlockSize - informs an index set that it has a given block size
1897: Logicall Collective
1899: Input Parameters:
1900: + is - index set
1901: - bs - block size
1903: Level: intermediate
1905: Notes:
1906: This is much like the block size for `Vec`s. It indicates that one can think of the indices as
1907: being in a collection of equal size blocks. For `ISBLOCK` these collections of blocks are all contiguous
1908: 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
1909: have a block size of three set.
1911: `ISBlockGetIndices()` only works for `ISBLOCK`, not others.
1913: .seealso: `IS`, `ISGetBlockSize()`, `ISCreateBlock()`, `ISBlockGetIndices()`
1914: @*/
1915: PetscErrorCode ISSetBlockSize(IS is, PetscInt bs)
1916: {
1917: PetscFunctionBegin;
1920: PetscCheck(bs >= 1, PetscObjectComm((PetscObject)is), PETSC_ERR_ARG_OUTOFRANGE, "Block size %" PetscInt_FMT ", must be positive", bs);
1921: if (PetscDefined(USE_DEBUG)) {
1922: const PetscInt *indices;
1923: PetscInt length, i, j;
1924: PetscCall(ISGetIndices(is, &indices));
1925: if (indices) {
1926: PetscCall(ISGetLocalSize(is, &length));
1927: 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);
1928: for (i = 1; i < length / bs; i += bs) {
1929: for (j = 1; j < bs - 1; j++) {
1930: 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);
1931: }
1932: }
1933: }
1934: PetscCall(ISRestoreIndices(is, &indices));
1935: }
1936: PetscUseTypeMethod(is, setblocksize, bs);
1937: PetscFunctionReturn(PETSC_SUCCESS);
1938: }
1940: /*@
1941: ISGetBlockSize - Returns the number of elements in a block.
1943: Not Collective
1945: Input Parameter:
1946: . is - the index set
1948: Output Parameter:
1949: . size - the number of elements in a block
1951: Level: intermediate
1953: Note:
1954: See `ISSetBlockSize()`
1956: .seealso: `IS`, `ISBlockGetSize()`, `ISGetSize()`, `ISCreateBlock()`, `ISSetBlockSize()`
1957: @*/
1958: PetscErrorCode ISGetBlockSize(IS is, PetscInt *size)
1959: {
1960: PetscFunctionBegin;
1961: PetscCall(PetscLayoutGetBlockSize(is->map, size));
1962: PetscFunctionReturn(PETSC_SUCCESS);
1963: }
1965: /*@
1966: ISSetCompressOutput - set the flag for output compression
1968: Logicall Collective
1970: Input Parameters:
1971: + is - index set
1972: - compress - flag for output compression
1974: Level: intermediate
1976: .seealso: `IS`, `ISGetCompressOutput()`, `ISView()`
1977: @*/
1978: PetscErrorCode ISSetCompressOutput(IS is, PetscBool compress)
1979: {
1980: PetscFunctionBegin;
1983: is->compressOutput = compress;
1984: PetscFunctionReturn(PETSC_SUCCESS);
1985: }
1987: /*@
1988: ISGetCompressOutput - Returns the flag for output compression
1990: Not Collective
1992: Input Parameter:
1993: . is - the index set
1995: Output Parameter:
1996: . compress - the flag to compress output
1998: Level: intermediate
2000: .seealso: `IS`, `ISSetCompressOutput()`, `ISView()`
2001: @*/
2002: PetscErrorCode ISGetCompressOutput(IS is, PetscBool *compress)
2003: {
2004: PetscFunctionBegin;
2006: PetscAssertPointer(compress, 2);
2007: *compress = is->compressOutput;
2008: PetscFunctionReturn(PETSC_SUCCESS);
2009: }
2011: static PetscErrorCode ISGetIndicesCopy_Private(IS is, PetscInt idx[])
2012: {
2013: PetscInt len;
2014: const PetscInt *ptr;
2016: PetscFunctionBegin;
2017: PetscCall(ISGetLocalSize(is, &len));
2018: PetscCall(ISGetIndices(is, &ptr));
2019: for (PetscInt i = 0; i < len; i++) idx[i] = ptr[i];
2020: PetscCall(ISRestoreIndices(is, &ptr));
2021: PetscFunctionReturn(PETSC_SUCCESS);
2022: }