xref: /petsc/include/petscis.h (revision 009bbdc485cd9ad46be9940d3549e2dde9cdc322)
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 #if !defined(__PETSCIS_H)
6 #define __PETSCIS_H
7 #include <petscsys.h>
8 #include <petscsf.h>
9 
10 #define IS_FILE_CLASSID 1211218
11 PETSC_EXTERN PetscClassId IS_CLASSID;
12 
13 PETSC_EXTERN PetscErrorCode ISInitializePackage(const char[]);
14 
15 /*S
16      IS - Abstract PETSc object that allows indexing.
17 
18    Level: beginner
19 
20   Concepts: indexing, stride
21 
22 .seealso:  ISCreateGeneral(), ISCreateBlock(), ISCreateStride(), ISGetIndices(), ISDestroy()
23 S*/
24 typedef struct _p_IS* IS;
25 
26 /*J
27     ISType - String with the name of a PETSc vector or the creation function
28        with an optional dynamic library name, for example
29        http://www.mcs.anl.gov/petsc/lib.a:myveccreate()
30 
31    Level: beginner
32 
33 .seealso: ISSetType(), IS
34 J*/
35 typedef const char* ISType;
36 #define ISGENERAL      "general"
37 #define ISSTRIDE       "stride"
38 #define ISBLOCK        "block"
39 
40 /* Dynamic creation and loading functions */
41 PETSC_EXTERN PetscFunctionList ISList;
42 PETSC_EXTERN PetscBool         ISRegisterAllCalled;
43 PETSC_EXTERN PetscErrorCode ISSetType(IS, ISType);
44 PETSC_EXTERN PetscErrorCode ISGetType(IS, ISType *);
45 PETSC_EXTERN PetscErrorCode ISRegister(const char[],const char[],const char[],PetscErrorCode (*)(IS));
46 PETSC_EXTERN PetscErrorCode ISRegisterAll(const char []);
47 PETSC_EXTERN PetscErrorCode ISRegisterDestroy(void);
48 PETSC_EXTERN PetscErrorCode ISCreate(MPI_Comm,IS*);
49 
50 /*MC
51   ISRegisterDynamic - Adds a new index set implementation
52 
53   Synopsis:
54   #include "petscis.h"
55   PetscErrorCode ISRegisterDynamic(const char *name, const char *path, const char *func_name, PetscErrorCode (*create_func)(IS))
56 
57   Not Collective
58 
59   Input Parameters:
60 + name        - The name of a new user-defined creation routine
61 . path        - The path (either absolute or relative) of the library containing this routine
62 . func_name   - The name of routine to create method context
63 - create_func - The creation routine itself
64 
65   Notes:
66   ISRegisterDynamic() may be called multiple times to add several user-defined vectors
67 
68   If dynamic libraries are used, then the fourth input argument (routine_create) is ignored.
69 
70   Sample usage:
71 .vb
72     ISRegisterDynamic("my_is_name","/home/username/my_lib/lib/libO/solaris/libmy.a", "MyISCreate", MyISCreate);
73 .ve
74 
75   Then, your vector type can be chosen with the procedural interface via
76 .vb
77     ISCreate(MPI_Comm, IS *);
78     ISSetType(IS,"my_is_name");
79 .ve
80    or at runtime via the option
81 .vb
82     -is_type my_is_name
83 .ve
84 
85   Notes: $PETSC_ARCH occuring in pathname will be replaced with appropriate values.
86          If your function is not being put into a shared library then use ISRegister() instead
87 
88   This is no ISSetFromOptions() and the current implementations do not have a way to dynamically determine type, so
89   dynamic registration of custom IS types will be of limited use to users.
90 
91   Level: developer
92 
93 .keywords: IS, register
94 .seealso: ISRegisterAll(), ISRegisterDestroy(), ISRegister()
95 M*/
96 #if defined(PETSC_USE_DYNAMIC_LIBRARIES)
97 #define ISRegisterDynamic(a,b,c,d) ISRegister(a,b,c,0)
98 #else
99 #define ISRegisterDynamic(a,b,c,d) ISRegister(a,b,c,d)
100 #endif
101 
102 /*
103     Default index set data structures that PETSc provides.
104 */
105 PETSC_EXTERN PetscErrorCode ISCreateGeneral(MPI_Comm,PetscInt,const PetscInt[],PetscCopyMode,IS *);
106 PETSC_EXTERN PetscErrorCode ISGeneralSetIndices(IS,PetscInt,const PetscInt[],PetscCopyMode);
107 PETSC_EXTERN PetscErrorCode ISCreateBlock(MPI_Comm,PetscInt,PetscInt,const PetscInt[],PetscCopyMode,IS *);
108 PETSC_EXTERN PetscErrorCode ISBlockSetIndices(IS,PetscInt,PetscInt,const PetscInt[],PetscCopyMode);
109 PETSC_EXTERN PetscErrorCode ISCreateStride(MPI_Comm,PetscInt,PetscInt,PetscInt,IS *);
110 PETSC_EXTERN PetscErrorCode ISStrideSetStride(IS,PetscInt,PetscInt,PetscInt);
111 
112 PETSC_EXTERN PetscErrorCode ISDestroy(IS*);
113 PETSC_EXTERN PetscErrorCode ISSetPermutation(IS);
114 PETSC_EXTERN PetscErrorCode ISPermutation(IS,PetscBool *);
115 PETSC_EXTERN PetscErrorCode ISSetIdentity(IS);
116 PETSC_EXTERN PetscErrorCode ISIdentity(IS,PetscBool *);
117 PETSC_EXTERN PetscErrorCode ISContiguousLocal(IS,PetscInt,PetscInt,PetscInt*,PetscBool*);
118 
119 PETSC_EXTERN PetscErrorCode ISGetIndices(IS,const PetscInt *[]);
120 PETSC_EXTERN PetscErrorCode ISRestoreIndices(IS,const PetscInt *[]);
121 PETSC_EXTERN PetscErrorCode ISGetTotalIndices(IS,const PetscInt *[]);
122 PETSC_EXTERN PetscErrorCode ISRestoreTotalIndices(IS,const PetscInt *[]);
123 PETSC_EXTERN PetscErrorCode ISGetNonlocalIndices(IS,const PetscInt *[]);
124 PETSC_EXTERN PetscErrorCode ISRestoreNonlocalIndices(IS,const PetscInt *[]);
125 PETSC_EXTERN PetscErrorCode ISGetNonlocalIS(IS, IS *is);
126 PETSC_EXTERN PetscErrorCode ISRestoreNonlocalIS(IS, IS *is);
127 PETSC_EXTERN PetscErrorCode ISGetSize(IS,PetscInt *);
128 PETSC_EXTERN PetscErrorCode ISGetLocalSize(IS,PetscInt *);
129 PETSC_EXTERN PetscErrorCode ISInvertPermutation(IS,PetscInt,IS*);
130 PETSC_EXTERN PetscErrorCode ISView(IS,PetscViewer);
131 PETSC_EXTERN PetscErrorCode ISEqual(IS,IS,PetscBool  *);
132 PETSC_EXTERN PetscErrorCode ISSort(IS);
133 PETSC_EXTERN PetscErrorCode ISSorted(IS,PetscBool  *);
134 PETSC_EXTERN PetscErrorCode ISDifference(IS,IS,IS*);
135 PETSC_EXTERN PetscErrorCode ISSum(IS,IS,IS*);
136 PETSC_EXTERN PetscErrorCode ISExpand(IS,IS,IS*);
137 
138 PETSC_EXTERN PetscErrorCode ISBlockGetIndices(IS,const PetscInt *[]);
139 PETSC_EXTERN PetscErrorCode ISBlockRestoreIndices(IS,const PetscInt *[]);
140 PETSC_EXTERN PetscErrorCode ISBlockGetLocalSize(IS,PetscInt *);
141 PETSC_EXTERN PetscErrorCode ISBlockGetSize(IS,PetscInt *);
142 PETSC_EXTERN PetscErrorCode ISGetBlockSize(IS,PetscInt*);
143 PETSC_EXTERN PetscErrorCode ISSetBlockSize(IS,PetscInt);
144 
145 PETSC_EXTERN PetscErrorCode ISStrideGetInfo(IS,PetscInt *,PetscInt*);
146 
147 PETSC_EXTERN PetscErrorCode ISToGeneral(IS);
148 
149 PETSC_EXTERN PetscErrorCode ISDuplicate(IS,IS*);
150 PETSC_EXTERN PetscErrorCode ISCopy(IS,IS);
151 PETSC_EXTERN PetscErrorCode ISAllGather(IS,IS*);
152 PETSC_EXTERN PetscErrorCode ISComplement(IS,PetscInt,PetscInt,IS*);
153 PETSC_EXTERN PetscErrorCode ISConcatenate(MPI_Comm,PetscInt,const IS[],IS*);
154 PETSC_EXTERN PetscErrorCode ISListToMap(MPI_Comm,PetscInt, IS[],IS*,IS*);
155 PETSC_EXTERN PetscErrorCode ISMapToList(IS,IS,PetscInt*, IS *[]);
156 PETSC_EXTERN PetscErrorCode ISMapFactorRight(IS,IS,PetscBool,IS*);
157 PETSC_EXTERN PetscErrorCode ISOnComm(IS,MPI_Comm,PetscCopyMode,IS*);
158 
159 /* --------------------------------------------------------------------------*/
160 PETSC_EXTERN PetscClassId IS_LTOGM_CLASSID;
161 
162 /*S
163    ISLocalToGlobalMapping - mappings from an arbitrary
164       local ordering from 0 to n-1 to a global PETSc ordering
165       used by a vector or matrix.
166 
167    Level: intermediate
168 
169    Note: mapping from Local to Global is scalable; but Global
170   to Local may not be if the range of global values represented locally
171   is very large.
172 
173    Note: the ISLocalToGlobalMapping is actually a private object; it is included
174   here for the inline function ISLocalToGlobalMappingApply() to allow it to be inlined since
175   it is used so often.
176 
177 .seealso:  ISLocalToGlobalMappingCreate()
178 S*/
179 struct _p_ISLocalToGlobalMapping{
180   PETSCHEADER(int);
181   PetscInt n;                  /* number of local indices */
182   PetscInt *indices;           /* global index of each local index */
183   PetscInt globalstart;        /* first global referenced in indices */
184   PetscInt globalend;          /* last + 1 global referenced in indices */
185   PetscInt *globals;           /* local index for each global index between start and end */
186 };
187 typedef struct _p_ISLocalToGlobalMapping* ISLocalToGlobalMapping;
188 
189 /*E
190     ISGlobalToLocalMappingType - Indicates if missing global indices are
191 
192    IS_GTOLM_MASK - missing global indices are replaced with -1
193    IS_GTOLM_DROP - missing global indices are dropped
194 
195    Level: beginner
196 
197 .seealso: ISGlobalToLocalMappingApply()
198 
199 E*/
200 typedef enum {IS_GTOLM_MASK,IS_GTOLM_DROP} ISGlobalToLocalMappingType;
201 
202 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingCreate(MPI_Comm,PetscInt,const PetscInt[],PetscCopyMode,ISLocalToGlobalMapping*);
203 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingCreateIS(IS,ISLocalToGlobalMapping *);
204 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingCreateSF(PetscSF,PetscInt,ISLocalToGlobalMapping*);
205 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingView(ISLocalToGlobalMapping,PetscViewer);
206 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingDestroy(ISLocalToGlobalMapping*);
207 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingApplyIS(ISLocalToGlobalMapping,IS,IS*);
208 PETSC_EXTERN PetscErrorCode ISGlobalToLocalMappingApply(ISLocalToGlobalMapping,ISGlobalToLocalMappingType,PetscInt,const PetscInt[],PetscInt*,PetscInt[]);
209 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingGetSize(ISLocalToGlobalMapping,PetscInt*);
210 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingGetInfo(ISLocalToGlobalMapping,PetscInt*,PetscInt*[],PetscInt*[],PetscInt**[]);
211 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingRestoreInfo(ISLocalToGlobalMapping,PetscInt*,PetscInt*[],PetscInt*[],PetscInt**[]);
212 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingGetIndices(ISLocalToGlobalMapping,const PetscInt**);
213 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingRestoreIndices(ISLocalToGlobalMapping,const PetscInt**);
214 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingBlock(ISLocalToGlobalMapping,PetscInt,ISLocalToGlobalMapping*);
215 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingUnBlock(ISLocalToGlobalMapping,PetscInt,ISLocalToGlobalMapping*);
216 PETSC_EXTERN PetscErrorCode ISLocalToGlobalMappingConcatenate(MPI_Comm,PetscInt,const ISLocalToGlobalMapping[],ISLocalToGlobalMapping*);
217 
218 #undef __FUNCT__
219 #define __FUNCT__ "ISLocalToGlobalMappingApply"
220 PETSC_STATIC_INLINE PetscErrorCode ISLocalToGlobalMappingApply(ISLocalToGlobalMapping mapping,PetscInt N,const PetscInt in[],PetscInt out[])
221 {
222   PetscInt       i,Nmax = mapping->n;
223   const PetscInt *idx = mapping->indices;
224 
225   PetscFunctionBegin;
226   for (i=0; i<N; i++) {
227     if (in[i] < 0) {
228       out[i] = in[i];
229       continue;
230     }
231     if (in[i] >= Nmax) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local index %D too large %D (max) at %D",in[i],Nmax,i);
232     out[i] = idx[in[i]];
233   }
234   PetscFunctionReturn(0);
235 }
236 
237 /* --------------------------------------------------------------------------*/
238 /*E
239     ISColoringType - determines if the coloring is for the entire parallel grid/graph/matrix
240                      or for just the local ghosted portion
241 
242     Level: beginner
243 
244 $   IS_COLORING_GLOBAL - does not include the colors for ghost points, this is used when the function
245 $                        is called synchronously in parallel. This requires generating a "parallel coloring".
246 $   IS_COLORING_GHOSTED - includes colors for ghost points, this is used when the function can be called
247 $                         seperately on individual processes with the ghost points already filled in. Does not
248 $                         require a "parallel coloring", rather each process colors its local + ghost part.
249 $                         Using this can result in much less parallel communication. In the paradigm of
250 $                         DMGetLocalVector() and DMGetGlobalVector() this could be called IS_COLORING_LOCAL
251 
252 .seealso: DMCreateColoring()
253 E*/
254 typedef enum {IS_COLORING_GLOBAL,IS_COLORING_GHOSTED} ISColoringType;
255 PETSC_EXTERN const char *const ISColoringTypes[];
256 typedef unsigned PETSC_IS_COLOR_VALUE_TYPE ISColoringValue;
257 PETSC_EXTERN PetscErrorCode ISAllGatherColors(MPI_Comm,PetscInt,ISColoringValue*,PetscInt*,ISColoringValue*[]);
258 
259 /*S
260      ISColoring - sets of IS's that define a coloring
261               of the underlying indices
262 
263    Level: intermediate
264 
265     Notes:
266         One should not access the *is records below directly because they may not yet
267     have been created. One should use ISColoringGetIS() to make sure they are
268     created when needed.
269 
270     Developer Note: this is not a PetscObject
271 
272 .seealso:  ISColoringCreate(), ISColoringGetIS(), ISColoringView(), ISColoringGetIS()
273 S*/
274 struct _n_ISColoring {
275   PetscInt        refct;
276   PetscInt        n;                /* number of colors */
277   IS              *is;              /* for each color indicates columns */
278   MPI_Comm        comm;
279   ISColoringValue *colors;          /* for each column indicates color */
280   PetscInt        N;                /* number of columns */
281   ISColoringType  ctype;
282 };
283 typedef struct _n_ISColoring* ISColoring;
284 
285 PETSC_EXTERN PetscErrorCode ISColoringCreate(MPI_Comm,PetscInt,PetscInt,const ISColoringValue[],ISColoring*);
286 PETSC_EXTERN PetscErrorCode ISColoringDestroy(ISColoring*);
287 PETSC_EXTERN PetscErrorCode ISColoringView(ISColoring,PetscViewer);
288 PETSC_EXTERN PetscErrorCode ISColoringGetIS(ISColoring,PetscInt*,IS*[]);
289 PETSC_EXTERN PetscErrorCode ISColoringRestoreIS(ISColoring,IS*[]);
290 PETSC_EXTERN PetscErrorCode ISColoringReference(ISColoring);
291 PETSC_EXTERN PetscErrorCode ISColoringSetType(ISColoring,ISColoringType);
292 
293 
294 /* --------------------------------------------------------------------------*/
295 
296 PETSC_EXTERN PetscErrorCode ISPartitioningToNumbering(IS,IS*);
297 PETSC_EXTERN PetscErrorCode ISPartitioningCount(IS,PetscInt,PetscInt[]);
298 
299 PETSC_EXTERN PetscErrorCode ISCompressIndicesGeneral(PetscInt,PetscInt,PetscInt,PetscInt,const IS[],IS[]);
300 PETSC_EXTERN PetscErrorCode ISCompressIndicesSorted(PetscInt,PetscInt,PetscInt,const IS[],IS[]);
301 PETSC_EXTERN PetscErrorCode ISExpandIndicesGeneral(PetscInt,PetscInt,PetscInt,PetscInt,const IS[],IS[]);
302 
303 
304 /* Reset __FUNCT__ in case the user does not define it themselves */
305 #undef __FUNCT__
306 #define __FUNCT__ "User provided function"
307 
308 #endif
309