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