xref: /petsc/include/petscis.h (revision 5d7a6ebe9dde080aedbe86be0085708de8f97bb7)
1 /*
2    An index set is a generalization of a subset of integers.  Index sets
3    are used for defining scatters and gathers.
4 */
5 #ifndef PETSCIS_H
6 #define PETSCIS_H
7 
8 #include <petscsys.h>
9 #include <petscsftypes.h>
10 #include <petscsectiontypes.h>
11 #include <petscistypes.h> /*I  "petscis.h" I*/
12 
13 /* SUBMANSEC = IS */
14 
15 #define IS_FILE_CLASSID 1211218
16 PETSC_EXTERN PetscClassId IS_CLASSID;
17 
18 PETSC_EXTERN PetscErrorCode ISInitializePackage(void);
19 PETSC_EXTERN PetscErrorCode ISFinalizePackage(void);
20 
21 /*J
22     ISType - String with the name of a PETSc index set type
23 
24    Values:
25 +  `ISGENERAL` - the values are stored with an array of indices and generally have no structure
26 .  `ISSTRIDE` - the values have a simple structure of an initial offset and then a step size between values
27 -  `ISBLOCK` - values are an array of indices, each representing a block of values
28 
29    Level: beginner
30 
31 .seealso: `ISSetType()`, `IS`, `ISCreateGeneral()`, `ISCreateStride()`, `ISCreateBlock()`, `ISCreate()`, `ISRegister()`,
32           `VecScatterCreate()`, `MatGetSubMatrices()`
33 J*/
34 typedef const char *ISType;
35 #define ISGENERAL "general"
36 #define ISSTRIDE  "stride"
37 #define ISBLOCK   "block"
38 
39 /* Dynamic creation and loading functions */
40 PETSC_EXTERN PetscFunctionList ISList;
41 PETSC_EXTERN PetscErrorCode    ISSetType(IS, ISType);
42 PETSC_EXTERN PetscErrorCode    ISGetType(IS, ISType *);
43 PETSC_EXTERN PetscErrorCode    ISRegister(const char[], PetscErrorCode (*)(IS));
44 PETSC_EXTERN PetscErrorCode    ISRegisterAll(void);
45 PETSC_EXTERN PetscErrorCode    ISCreate(MPI_Comm, IS *);
46 
47 PETSC_EXTERN PetscErrorCode ISDestroy(IS *);
48 PETSC_EXTERN PetscErrorCode ISSetPermutation(IS);
49 PETSC_EXTERN PetscErrorCode ISPermutation(IS, PetscBool *);
50 PETSC_EXTERN PetscErrorCode ISSetIdentity(IS);
51 PETSC_EXTERN PetscErrorCode ISIdentity(IS, PetscBool *);
52 PETSC_EXTERN PetscErrorCode ISContiguousLocal(IS, PetscInt, PetscInt, PetscInt *, PetscBool *);
53 
54 /*E
55     ISInfo - Info that may either be computed or set as known for an index set
56 
57     Level: intermediate
58 
59    Developer Notes:
60    Entries that are negative need not be called collectively by all processes.
61 
62    Any additions/changes here MUST also be made in include/petsc/finclude/petscis.h
63 
64    Any additions/changes here must also be made in src/vec/vec/interface/dlregisvec.c in ISInfos[]
65 
66 .seealso: `IS`, `ISType`, `ISSetInfo()`
67 E*/
68 typedef enum {
69   IS_INFO_MIN    = -1,
70   IS_SORTED      = 0,
71   IS_UNIQUE      = 1,
72   IS_PERMUTATION = 2,
73   IS_INTERVAL    = 3,
74   IS_IDENTITY    = 4,
75   IS_INFO_MAX    = 5
76 } ISInfo;
77 
78 typedef enum {
79   IS_LOCAL,
80   IS_GLOBAL
81 } ISInfoType;
82 
83 PETSC_EXTERN PetscErrorCode ISSetInfo(IS, ISInfo, ISInfoType, PetscBool, PetscBool);
84 PETSC_EXTERN PetscErrorCode ISGetInfo(IS, ISInfo, ISInfoType, PetscBool, PetscBool *);
85 PETSC_EXTERN PetscErrorCode ISClearInfoCache(IS, PetscBool);
86 PETSC_EXTERN PetscErrorCode ISGetIndices(IS, const PetscInt *[]);
87 PETSC_EXTERN PetscErrorCode ISRestoreIndices(IS, const PetscInt *[]);
88 PETSC_EXTERN PetscErrorCode ISGetTotalIndices(IS, const PetscInt *[]);
89 PETSC_EXTERN PetscErrorCode ISRestoreTotalIndices(IS, const PetscInt *[]);
90 PETSC_EXTERN PetscErrorCode ISGetNonlocalIndices(IS, const PetscInt *[]);
91 PETSC_EXTERN PetscErrorCode ISRestoreNonlocalIndices(IS, const PetscInt *[]);
92 PETSC_EXTERN PetscErrorCode ISGetNonlocalIS(IS, IS *);
93 PETSC_EXTERN PetscErrorCode ISRestoreNonlocalIS(IS, IS *);
94 PETSC_EXTERN PetscErrorCode ISGetSize(IS, PetscInt *);
95 PETSC_EXTERN PetscErrorCode ISGetLocalSize(IS, PetscInt *);
96 PETSC_EXTERN PetscErrorCode ISInvertPermutation(IS, PetscInt, IS *);
97 PETSC_EXTERN PetscErrorCode ISView(IS, PetscViewer);
98 PETSC_EXTERN PetscErrorCode ISViewFromOptions(IS, PetscObject, const char[]);
99 PETSC_EXTERN PetscErrorCode ISLoad(IS, PetscViewer);
100 PETSC_EXTERN PetscErrorCode ISEqual(IS, IS, PetscBool *);
101 PETSC_EXTERN PetscErrorCode ISEqualUnsorted(IS, IS, PetscBool *);
102 PETSC_EXTERN PetscErrorCode ISSort(IS);
103 PETSC_EXTERN PetscErrorCode ISSortRemoveDups(IS);
104 PETSC_EXTERN PetscErrorCode ISSorted(IS, PetscBool *);
105 PETSC_EXTERN PetscErrorCode ISDifference(IS, IS, IS *);
106 PETSC_EXTERN PetscErrorCode ISSum(IS, IS, IS *);
107 PETSC_EXTERN PetscErrorCode ISExpand(IS, IS, IS *);
108 PETSC_EXTERN PetscErrorCode ISIntersect(IS, IS, IS *);
109 PETSC_EXTERN PetscErrorCode ISGetMinMax(IS, PetscInt *, PetscInt *);
110 
111 PETSC_EXTERN PetscErrorCode ISLocate(IS, PetscInt, PetscInt *);
112 PETSC_EXTERN PetscErrorCode ISGetPointRange(IS, PetscInt *, PetscInt *, const PetscInt **);
113 PETSC_EXTERN PetscErrorCode ISRestorePointRange(IS, PetscInt *, PetscInt *, const PetscInt **);
114 PETSC_EXTERN PetscErrorCode ISGetPointSubrange(IS, PetscInt, PetscInt, const PetscInt *);
115 
116 PETSC_EXTERN PetscErrorCode ISGetBlockSize(IS, PetscInt *);
117 PETSC_EXTERN PetscErrorCode ISSetBlockSize(IS, PetscInt);
118 
119 PETSC_EXTERN PetscErrorCode ISToGeneral(IS);
120 
121 PETSC_EXTERN PetscErrorCode ISDuplicate(IS, IS *);
122 PETSC_EXTERN PetscErrorCode ISCopy(IS, IS);
123 PETSC_EXTERN PetscErrorCode ISShift(IS, PetscInt, IS);
124 PETSC_EXTERN PetscErrorCode ISAllGather(IS, IS *);
125 PETSC_EXTERN PetscErrorCode ISComplement(IS, PetscInt, PetscInt, IS *);
126 PETSC_EXTERN PetscErrorCode ISConcatenate(MPI_Comm, PetscInt, const IS[], IS *);
127 PETSC_EXTERN PetscErrorCode ISListToPair(MPI_Comm, PetscInt, IS[], IS *, IS *);
128 PETSC_EXTERN PetscErrorCode ISPairToList(IS, IS, PetscInt *, IS *[]);
129 PETSC_EXTERN PetscErrorCode ISEmbed(IS, IS, PetscBool, IS *);
130 PETSC_EXTERN PetscErrorCode ISSortPermutation(IS, PetscBool, IS *);
131 PETSC_EXTERN PetscErrorCode ISOnComm(IS, MPI_Comm, PetscCopyMode, IS *);
132 PETSC_EXTERN PetscErrorCode ISRenumber(IS, IS, PetscInt *, IS *);
133 PETSC_EXTERN PetscErrorCode ISCreateSubIS(IS, IS, IS *);
134 
135 /* ISGENERAL specific */
136 PETSC_EXTERN PetscErrorCode ISCreateGeneral(MPI_Comm, PetscInt, const PetscInt[], PetscCopyMode, IS *);
137 PETSC_EXTERN PetscErrorCode ISGeneralSetIndices(IS, PetscInt, const PetscInt[], PetscCopyMode);
138 PETSC_EXTERN PetscErrorCode ISGeneralSetIndicesFromMask(IS, PetscInt, PetscInt, const PetscBool[]);
139 PETSC_EXTERN PetscErrorCode ISGeneralFilter(IS, PetscInt, PetscInt);
140 
141 /* ISBLOCK specific */
142 PETSC_EXTERN PetscErrorCode ISCreateBlock(MPI_Comm, PetscInt, PetscInt, const PetscInt[], PetscCopyMode, IS *);
143 PETSC_EXTERN PetscErrorCode ISBlockSetIndices(IS, PetscInt, PetscInt, const PetscInt[], PetscCopyMode);
144 PETSC_EXTERN PetscErrorCode ISBlockGetIndices(IS, const PetscInt *[]);
145 PETSC_EXTERN PetscErrorCode ISBlockRestoreIndices(IS, const PetscInt *[]);
146 PETSC_EXTERN PetscErrorCode ISBlockGetLocalSize(IS, PetscInt *);
147 PETSC_EXTERN PetscErrorCode ISBlockGetSize(IS, PetscInt *);
148 
149 /* ISSTRIDE specific */
150 PETSC_EXTERN PetscErrorCode ISCreateStride(MPI_Comm, PetscInt, PetscInt, PetscInt, IS *);
151 PETSC_EXTERN PetscErrorCode ISStrideSetStride(IS, PetscInt, PetscInt, PetscInt);
152 PETSC_EXTERN PetscErrorCode ISStrideGetInfo(IS, PetscInt *, PetscInt *);
153 
154 PETSC_EXTERN PetscClassId IS_LTOGM_CLASSID;
155 
156 /*E
157     ISGlobalToLocalMappingMode - Indicates mapping behavior if global indices are missing
158 
159    Values:
160 +   `IS_GTOLM_MASK` - missing global indices are masked by mapping them to a local index of -1
161 -   `IS_GTOLM_DROP` - missing global indices are dropped
162 
163    Level: beginner
164 
165 .seealso: `ISGlobalToLocalMappingApplyBlock()`, `ISGlobalToLocalMappingApply()`
166 E*/
167 typedef enum {
168   IS_GTOLM_MASK,
169   IS_GTOLM_DROP
170 } ISGlobalToLocalMappingMode;
171 
172 /*J
173     ISLocalToGlobalMappingType - String with the name of a mapping method
174 
175    Values:
176 +  `ISLOCALTOGLOBALMAPPINGBASIC` - a non-memory scalable way of storing `ISLocalToGlobalMapping` that allows applying `ISGlobalToLocalMappingApply()` efficiently
177 -  `ISLOCALTOGLOBALMAPPINGHASH` - a memory scalable way of storing `ISLocalToGlobalMapping` that allows applying `ISGlobalToLocalMappingApply()` reasonably efficiently
178 
179   Level: beginner
180 
181 .seealso: `ISLocalToGlobalMapping`, `ISLocalToGlobalMappingSetType()`, `ISLocalToGlobalSetFromOptions()`, `ISGlobalToLocalMappingMode`
182 J*/
183 typedef const char *ISLocalToGlobalMappingType;
184 #define ISLOCALTOGLOBALMAPPINGBASIC "basic"
185 #define ISLOCALTOGLOBALMAPPINGHASH  "hash"
186 
187 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingSetType(ISLocalToGlobalMapping, ISLocalToGlobalMappingType);
188 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingGetType(ISLocalToGlobalMapping, ISLocalToGlobalMappingType *);
189 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingRegister(const char[], PetscErrorCode (*)(ISLocalToGlobalMapping));
190 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingRegisterAll(void);
191 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingCreate(MPI_Comm, PetscInt, PetscInt, const PetscInt[], PetscCopyMode, ISLocalToGlobalMapping *);
192 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingCreateIS(IS, ISLocalToGlobalMapping *);
193 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingCreateSF(PetscSF, PetscInt, ISLocalToGlobalMapping *);
194 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingSetFromOptions(ISLocalToGlobalMapping);
195 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingSetUp(ISLocalToGlobalMapping);
196 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingView(ISLocalToGlobalMapping, PetscViewer);
197 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingViewFromOptions(ISLocalToGlobalMapping, PetscObject, const char[]);
198 
199 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingDestroy(ISLocalToGlobalMapping *);
200 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingApply(ISLocalToGlobalMapping, PetscInt, const PetscInt[], PetscInt[]);
201 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingApplyBlock(ISLocalToGlobalMapping, PetscInt, const PetscInt[], PetscInt[]);
202 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingApplyIS(ISLocalToGlobalMapping, IS, IS *);
203 PETSC_EXTERN PetscErrorCode ISGlobalToLocalMappingApply(ISLocalToGlobalMapping, ISGlobalToLocalMappingMode, PetscInt, const PetscInt[], PetscInt *, PetscInt[]);
204 PETSC_EXTERN PetscErrorCode ISGlobalToLocalMappingApplyBlock(ISLocalToGlobalMapping, ISGlobalToLocalMappingMode, PetscInt, const PetscInt[], PetscInt *, PetscInt[]);
205 PETSC_EXTERN PetscErrorCode ISGlobalToLocalMappingApplyIS(ISLocalToGlobalMapping, ISGlobalToLocalMappingMode, IS, IS *);
206 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingGetSize(ISLocalToGlobalMapping, PetscInt *);
207 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingGetNodeInfo(ISLocalToGlobalMapping, PetscInt *, PetscInt *[], PetscInt **[]);
208 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingRestoreNodeInfo(ISLocalToGlobalMapping, PetscInt *, PetscInt *[], PetscInt **[]);
209 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingGetInfo(ISLocalToGlobalMapping, PetscInt *, PetscInt *[], PetscInt *[], PetscInt **[]);
210 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingRestoreInfo(ISLocalToGlobalMapping, PetscInt *, PetscInt *[], PetscInt *[], PetscInt **[]);
211 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingGetBlockInfo(ISLocalToGlobalMapping, PetscInt *, PetscInt *[], PetscInt *[], PetscInt **[]);
212 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingRestoreBlockInfo(ISLocalToGlobalMapping, PetscInt *, PetscInt *[], PetscInt *[], PetscInt **[]);
213 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingGetIndices(ISLocalToGlobalMapping, const PetscInt **);
214 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingRestoreIndices(ISLocalToGlobalMapping, const PetscInt **);
215 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingGetBlockIndices(ISLocalToGlobalMapping, const PetscInt **);
216 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingRestoreBlockIndices(ISLocalToGlobalMapping, const PetscInt **);
217 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingConcatenate(MPI_Comm, PetscInt, const ISLocalToGlobalMapping[], ISLocalToGlobalMapping *);
218 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingGetBlockSize(ISLocalToGlobalMapping, PetscInt *);
219 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingSetBlockSize(ISLocalToGlobalMapping, PetscInt);
220 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingDuplicate(ISLocalToGlobalMapping, ISLocalToGlobalMapping *);
221 
222 /*E
223     ISColoringType - determines if the coloring is for the entire parallel grid/graph/matrix
224                      or for just the local ghosted portion
225 
226     Values:
227 +   `IS_COLORING_GLOBAL` - does not include the colors for ghost points, this is used when the function
228                         is called synchronously in parallel. This requires generating a "parallel coloring".
229 -   `IS_COLORING_LOCAL` - includes colors for ghost points, this is used when the function can be called
230                          separately on individual processes with the ghost points already filled in. Does not
231                          require a "parallel coloring", rather each process colors its local + ghost part.
232                          Using this can result in much less parallel communication. Currently only works
233                          with `DMDA` and if you call `MatFDColoringSetFunction()` with the local function.
234 
235    Level: beginner
236 
237 .seealso: `ISColoring`, `ISColoringSetType()`, `ISColoringGetType()`, `DMCreateColoring()`
238 E*/
239 typedef enum {
240   IS_COLORING_GLOBAL,
241   IS_COLORING_LOCAL
242 } ISColoringType;
243 PETSC_EXTERN const char *const                ISColoringTypes[];
244 typedef unsigned PETSC_IS_COLORING_VALUE_TYPE ISColoringValue;
245 #define IS_COLORING_MAX     PETSC_IS_COLORING_MAX
246 #define MPIU_COLORING_VALUE PETSC_MPIU_IS_COLORING_VALUE_TYPE
247 PETSC_EXTERN PetscErrorCode ISAllGatherColors(MPI_Comm, PetscInt, ISColoringValue *, PetscInt *, ISColoringValue *[]);
248 
249 PETSC_EXTERN PetscErrorCode ISColoringCreate(MPI_Comm, PetscInt, PetscInt, const ISColoringValue[], PetscCopyMode, ISColoring *);
250 PETSC_EXTERN PetscErrorCode ISColoringDestroy(ISColoring *);
251 PETSC_EXTERN PetscErrorCode ISColoringView(ISColoring, PetscViewer);
252 PETSC_EXTERN PetscErrorCode ISColoringViewFromOptions(ISColoring, PetscObject, const char[]);
253 PETSC_EXTERN PetscErrorCode ISColoringGetIS(ISColoring, PetscCopyMode, PetscInt *, IS *[]);
254 PETSC_EXTERN PetscErrorCode ISColoringRestoreIS(ISColoring, PetscCopyMode, IS *[]);
255 PETSC_EXTERN PetscErrorCode ISColoringReference(ISColoring);
256 PETSC_EXTERN PetscErrorCode ISColoringSetType(ISColoring, ISColoringType);
257 PETSC_EXTERN PetscErrorCode ISColoringGetType(ISColoring, ISColoringType *);
258 PETSC_EXTERN PetscErrorCode ISColoringGetColors(ISColoring, PetscInt *, PetscInt *, const ISColoringValue **);
259 
260 PETSC_EXTERN PetscErrorCode ISBuildTwoSided(IS, IS, IS *);
261 PETSC_EXTERN PetscErrorCode ISPartitioningToNumbering(IS, IS *);
262 PETSC_EXTERN PetscErrorCode ISPartitioningCount(IS, PetscInt, PetscInt[]);
263 
264 PETSC_EXTERN PetscErrorCode ISCompressIndicesGeneral(PetscInt, PetscInt, PetscInt, PetscInt, const IS[], IS[]);
265 PETSC_DEPRECATED_FUNCTION(3, 19, 0, "ISCompressIndicesGeneral()", ) static inline PetscErrorCode ISCompressIndicesSorted(PetscInt n, PetscInt bs, PetscInt imax, const IS is_in[], IS is_out[])
266 {
267   return ISCompressIndicesGeneral(n, bs, n, imax, is_in, is_out);
268 }
269 PETSC_EXTERN PetscErrorCode ISExpandIndicesGeneral(PetscInt, PetscInt, PetscInt, PetscInt, const IS[], IS[]);
270 
271 struct _n_PetscLayout {
272   MPI_Comm               comm;
273   PetscMPIInt            size;
274   PetscInt               n, N;         /* local, global vector size */
275   PetscInt               rstart, rend; /* local start, local end + 1 */
276   PetscInt              *range;        /* the offset of each processor */
277   PetscBool              range_alloc;  /* should range be freed in Destroy? */
278   PetscInt               bs;           /* number of elements in each block (generally for multi-component
279                                        * problems). Defaults to -1 and can be arbitrarily lazy so always use
280                                        * PetscAbs(map->bs) when accessing directly and expecting result to be
281                                        * positive. Do NOT multiply above numbers by bs */
282   PetscInt               refcnt;       /* MPI Vecs obtained with VecDuplicate() and from MatCreateVecs() reuse map of input object */
283   ISLocalToGlobalMapping mapping;      /* mapping used in Vec/MatSetValuesLocal() */
284   PetscBool              setupcalled;  /* Forbid setup more than once */
285   PetscInt               oldn, oldN;   /* Checking if setup is allowed */
286   PetscInt               oldbs;        /* And again */
287 };
288 
289 /*@C
290      PetscLayoutFindOwner - Find the owning rank for a global index
291 
292     Not Collective; No Fortran Support
293 
294    Input Parameters:
295 +    map - the layout
296 -    idx - global index to find the owner of
297 
298    Output Parameter:
299 .    owner - the owning rank
300 
301    Level: developer
302 
303 .seealso: `PetscLayout`, `PetscLayoutFindOwnerIndex()`
304 @*/
305 static inline PetscErrorCode PetscLayoutFindOwner(PetscLayout map, PetscInt idx, PetscMPIInt *owner)
306 {
307   PetscMPIInt lo = 0, hi, t;
308 
309   PetscFunctionBegin;
310   *owner = -1; /* GCC erroneously issues warning about possibly uninitialized use when error condition */
311 #if defined(PETSC_USE_DEBUG)
312   PetscCheck((map->n >= 0) && (map->N >= 0) && (map->range), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "PetscLayoutSetUp() must be called first");
313   PetscCheck(idx >= 0 && idx <= map->N, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Index %" PetscInt_FMT " is out of range", idx);
314 #endif
315   hi = map->size;
316   while (hi - lo > 1) {
317     t = lo + (hi - lo) / 2;
318     if (idx < map->range[t]) hi = t;
319     else lo = t;
320   }
321   *owner = lo;
322   PetscFunctionReturn(PETSC_SUCCESS);
323 }
324 
325 /*@C
326      PetscLayoutFindOwnerIndex - Find the owning MPI rank and the local index on that rank for a global index
327 
328     Not Collective; No Fortran Support
329 
330    Input Parameters:
331 +    map   - the layout
332 -    idx   - global index to find the owner of
333 
334    Output Parameters:
335 +    owner - the owning rank
336 -    lidx  - local index used by the owner for `idx`
337 
338    Level: developer
339 
340 .seealso: `PetscLayout`, `PetscLayoutFindOwner()`
341 @*/
342 static inline PetscErrorCode PetscLayoutFindOwnerIndex(PetscLayout map, PetscInt idx, PetscMPIInt *owner, PetscInt *lidx)
343 {
344   PetscMPIInt lo = 0, hi, t;
345 
346   PetscFunctionBegin;
347 #if defined(PETSC_USE_DEBUG)
348   PetscCheck((map->n >= 0) && (map->N >= 0) && (map->range), PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "PetscLayoutSetUp() must be called first");
349   PetscCheck(idx >= 0 && idx <= map->N, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Index %" PetscInt_FMT " is out of range", idx);
350 #endif
351   hi = map->size;
352   while (hi - lo > 1) {
353     t = lo + (hi - lo) / 2;
354     if (idx < map->range[t]) hi = t;
355     else lo = t;
356   }
357   if (owner) *owner = lo;
358   if (lidx) *lidx = idx - map->range[lo];
359   PetscFunctionReturn(PETSC_SUCCESS);
360 }
361 
362 PETSC_EXTERN PetscErrorCode PetscLayoutCreate(MPI_Comm, PetscLayout *);
363 PETSC_EXTERN PetscErrorCode PetscLayoutCreateFromSizes(MPI_Comm, PetscInt, PetscInt, PetscInt, PetscLayout *);
364 PETSC_EXTERN PetscErrorCode PetscLayoutCreateFromRanges(MPI_Comm, const PetscInt[], PetscCopyMode, PetscInt, PetscLayout *);
365 PETSC_EXTERN PetscErrorCode PetscLayoutSetUp(PetscLayout);
366 PETSC_EXTERN PetscErrorCode PetscLayoutDestroy(PetscLayout *);
367 PETSC_EXTERN PetscErrorCode PetscLayoutDuplicate(PetscLayout, PetscLayout *);
368 PETSC_EXTERN PetscErrorCode PetscLayoutReference(PetscLayout, PetscLayout *);
369 PETSC_EXTERN PetscErrorCode PetscLayoutSetLocalSize(PetscLayout, PetscInt);
370 PETSC_EXTERN PetscErrorCode PetscLayoutGetLocalSize(PetscLayout, PetscInt *);
371 PETSC_EXTERN PetscErrorCode PetscLayoutSetSize(PetscLayout, PetscInt);
372 PETSC_EXTERN PetscErrorCode PetscLayoutGetSize(PetscLayout, PetscInt *);
373 PETSC_EXTERN PetscErrorCode PetscLayoutSetBlockSize(PetscLayout, PetscInt);
374 PETSC_EXTERN PetscErrorCode PetscLayoutGetBlockSize(PetscLayout, PetscInt *);
375 PETSC_EXTERN PetscErrorCode PetscLayoutGetRange(PetscLayout, PetscInt *, PetscInt *);
376 PETSC_EXTERN PetscErrorCode PetscLayoutGetRanges(PetscLayout, const PetscInt *[]);
377 PETSC_EXTERN PetscErrorCode PetscLayoutCompare(PetscLayout, PetscLayout, PetscBool *);
378 PETSC_EXTERN PetscErrorCode PetscLayoutSetISLocalToGlobalMapping(PetscLayout, ISLocalToGlobalMapping);
379 PETSC_EXTERN PetscErrorCode PetscLayoutMapLocal(PetscLayout, PetscInt, const PetscInt[], PetscInt *, PetscInt **, PetscInt **);
380 
381 PETSC_EXTERN PetscErrorCode PetscParallelSortInt(PetscLayout, PetscLayout, PetscInt *, PetscInt *);
382 
383 PETSC_EXTERN PetscErrorCode ISGetLayout(IS, PetscLayout *);
384 PETSC_EXTERN PetscErrorCode ISSetLayout(IS, PetscLayout);
385 
386 #endif
387