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