xref: /petsc/include/petscis.h (revision 17efb62674e29b38b1362c0da2a8a70541c25d97)
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 "petsc.h"
8 PETSC_EXTERN_CXX_BEGIN
9 
10 extern PETSCVEC_DLLEXPORT PetscCookie IS_COOKIE;
11 
12 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT ISInitializePackage(const char[]);
13 
14 /*S
15      IS - Abstract PETSc object that 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 /*
26     Default index set data structures that PETSc provides.
27 */
28 typedef enum {IS_GENERAL=0,IS_STRIDE=1,IS_BLOCK = 2} ISType;
29 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISCreateGeneral(MPI_Comm,PetscInt,const PetscInt[],IS *);
30 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISCreateGeneralNC(MPI_Comm,PetscInt,const PetscInt[],IS *);
31 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISCreateGeneralWithArray(MPI_Comm,PetscInt,PetscInt[],IS *);
32 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISCreateBlock(MPI_Comm,PetscInt,PetscInt,const PetscInt[],IS *);
33 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISCreateStride(MPI_Comm,PetscInt,PetscInt,PetscInt,IS *);
34 
35 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISDestroy(IS);
36 
37 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISSetPermutation(IS);
38 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISPermutation(IS,PetscTruth*);
39 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISSetIdentity(IS);
40 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISIdentity(IS,PetscTruth*);
41 
42 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISGetIndices(IS,PetscInt *[]);
43 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISRestoreIndices(IS,PetscInt *[]);
44 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISGetSize(IS,PetscInt *);
45 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISGetLocalSize(IS,PetscInt *);
46 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISInvertPermutation(IS,PetscInt,IS*);
47 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISView(IS,PetscViewer);
48 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISEqual(IS,IS,PetscTruth *);
49 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISSort(IS);
50 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISSorted(IS,PetscTruth *);
51 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISDifference(IS,IS,IS*);
52 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISSum(IS,IS,IS*);
53 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISExpand(IS,IS,IS*);
54 
55 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISBlock(IS,PetscTruth*);
56 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISBlockGetIndices(IS,PetscInt *[]);
57 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISBlockRestoreIndices(IS,PetscInt *[]);
58 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISBlockGetSize(IS,PetscInt *);
59 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISBlockGetBlockSize(IS,PetscInt *);
60 
61 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISStride(IS,PetscTruth*);
62 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISStrideGetInfo(IS,PetscInt *,PetscInt*);
63 
64 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISStrideToGeneral(IS);
65 
66 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISDuplicate(IS,IS*);
67 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISAllGather(IS,IS*);
68 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISComplement(IS,PetscInt,IS*);
69 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT   ISAllGatherIndices(MPI_Comm,PetscInt,const PetscInt[],PetscInt*,PetscInt*[]);
70 
71 /* --------------------------------------------------------------------------*/
72 extern PETSCVEC_DLLEXPORT PetscCookie IS_LTOGM_COOKIE;
73 
74 /*S
75    ISLocalToGlobalMapping - mappings from an arbitrary
76       local ordering from 0 to n-1 to a global PETSc ordering
77       used by a vector or matrix.
78 
79    Level: intermediate
80 
81    Note: mapping from Local to Global is scalable; but Global
82   to Local may not be if the range of global values represented locally
83   is very large.
84 
85    Note: the ISLocalToGlobalMapping is actually a private object; it is included
86   here for the MACRO ISLocalToGlobalMappingApply() to allow it to be inlined since
87   it is used so often.
88 
89 .seealso:  ISLocalToGlobalMappingCreate()
90 S*/
91 struct _p_ISLocalToGlobalMapping{
92   PETSCHEADER(int);
93   PetscInt n;                  /* number of local indices */
94   PetscInt *indices;           /* global index of each local index */
95   PetscInt globalstart;        /* first global referenced in indices */
96   PetscInt globalend;          /* last + 1 global referenced in indices */
97   PetscInt *globals;           /* local index for each global index between start and end */
98 };
99 typedef struct _p_ISLocalToGlobalMapping* ISLocalToGlobalMapping;
100 
101 /*E
102     ISGlobalToLocalMappingType - Indicates if missing global indices are
103 
104    IS_GTOLM_MASK - missing global indices are replaced with -1
105    IS_GTOLM_DROP - missing global indices are dropped
106 
107    Level: beginner
108 
109 .seealso: ISGlobalToLocalMappingApply()
110 
111 E*/
112 typedef enum {IS_GTOLM_MASK,IS_GTOLM_DROP} ISGlobalToLocalMappingType;
113 
114 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT ISLocalToGlobalMappingCreate(MPI_Comm,PetscInt,const PetscInt[],ISLocalToGlobalMapping*);
115 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT ISLocalToGlobalMappingCreateNC(MPI_Comm,PetscInt,const PetscInt[],ISLocalToGlobalMapping*);
116 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT ISLocalToGlobalMappingCreateIS(IS,ISLocalToGlobalMapping *);
117 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT ISLocalToGlobalMappingView(ISLocalToGlobalMapping,PetscViewer);
118 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT ISLocalToGlobalMappingDestroy(ISLocalToGlobalMapping);
119 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT ISLocalToGlobalMappingApplyIS(ISLocalToGlobalMapping,IS,IS*);
120 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT ISGlobalToLocalMappingApply(ISLocalToGlobalMapping,ISGlobalToLocalMappingType,PetscInt,const PetscInt[],PetscInt*,PetscInt[]);
121 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT ISLocalToGlobalMappingGetSize(ISLocalToGlobalMapping,PetscInt*);
122 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT ISLocalToGlobalMappingGetInfo(ISLocalToGlobalMapping,PetscInt*,PetscInt*[],PetscInt*[],PetscInt**[]);
123 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT ISLocalToGlobalMappingRestoreInfo(ISLocalToGlobalMapping,PetscInt*,PetscInt*[],PetscInt*[],PetscInt**[]);
124 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT ISLocalToGlobalMappingBlock(ISLocalToGlobalMapping,PetscInt,ISLocalToGlobalMapping*);
125 
126 PETSC_STATIC_INLINE PetscErrorCode ISLocalToGlobalMappingApply(ISLocalToGlobalMapping mapping,PetscInt N,const PetscInt in[],PetscInt out[]){
127   PetscInt i,*idx = mapping->indices,Nmax = mapping->n;
128   for (i=0; i<N; i++) {
129     if (in[i] < 0) {out[i] = in[i]; continue;}
130     if (in[i] >= Nmax) SETERRQ3(PETSC_ERR_ARG_OUTOFRANGE,"Local index %D too large %D (max) at %D",in[i],Nmax,i);
131     out[i] = idx[in[i]];
132   }
133   PetscFunctionReturn(0);
134 }
135 
136 /* --------------------------------------------------------------------------*/
137 /*E
138     ISColoringType - determines if the coloring is for the entire parallel grid/graph/matrix
139                      or for just the local ghosted portion
140 
141     Level: beginner
142 
143 $   IS_COLORING_GLOBAL - does not include the colors for ghost points, this is used when the function
144 $                        is called synchronously in parallel. This requires generating a "parallel coloring".
145 $   IS_COLORING_GHOSTED - includes colors for ghost points, this is used when the function can be called
146 $                         seperately on individual processes with the ghost points already filled in. Does not
147 $                         require a "parallel coloring", rather each process colors its local + ghost part.
148 $                         Using this can result in much less parallel communication. In the paradigm of
149 $                         DAGetLocalVector() and DAGetGlobalVector() this could be called IS_COLORING_LOCAL
150 
151 .seealso: DAGetColoring()
152 E*/
153 typedef enum {IS_COLORING_GLOBAL,IS_COLORING_GHOSTED} ISColoringType;
154 extern const char *ISColoringTypes[];
155 typedef unsigned PETSC_IS_COLOR_VALUE_TYPE ISColoringValue;
156 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT ISAllGatherColors(MPI_Comm,PetscInt,ISColoringValue*,PetscInt*,ISColoringValue*[]);
157 
158 /*S
159      ISColoring - sets of IS's that define a coloring
160               of the underlying indices
161 
162    Level: intermediate
163 
164     Notes:
165         One should not access the *is records below directly because they may not yet
166     have been created. One should use ISColoringGetIS() to make sure they are
167     created when needed.
168 
169 .seealso:  ISColoringCreate(), ISColoringGetIS(), ISColoringView(), ISColoringGetIS()
170 S*/
171 struct _n_ISColoring {
172   PetscInt        refct;
173   PetscInt        n;                /* number of colors */
174   IS              *is;              /* for each color indicates columns */
175   MPI_Comm        comm;
176   ISColoringValue *colors;          /* for each column indicates color */
177   PetscInt        N;                /* number of columns */
178   ISColoringType  ctype;
179 };
180 typedef struct _n_ISColoring* ISColoring;
181 
182 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT ISColoringCreate(MPI_Comm,PetscInt,PetscInt,const ISColoringValue[],ISColoring*);
183 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT ISColoringDestroy(ISColoring);
184 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT ISColoringView(ISColoring,PetscViewer);
185 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT ISColoringGetIS(ISColoring,PetscInt*,IS*[]);
186 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT ISColoringRestoreIS(ISColoring,IS*[]);
187 #define ISColoringReference(coloring) ((coloring)->refct++,0)
188 #define ISColoringSetType(coloring,type) ((coloring)->ctype = type,0)
189 
190 /* --------------------------------------------------------------------------*/
191 
192 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT ISPartitioningToNumbering(IS,IS*);
193 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT ISPartitioningCount(IS,PetscInt,PetscInt[]);
194 
195 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT ISCompressIndicesGeneral(PetscInt,PetscInt,PetscInt,const IS[],IS[]);
196 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT ISCompressIndicesSorted(PetscInt,PetscInt,PetscInt,const IS[],IS[]);
197 EXTERN PetscErrorCode PETSCVEC_DLLEXPORT ISExpandIndicesGeneral(PetscInt,PetscInt,PetscInt,const IS[],IS[]);
198 
199 PETSC_EXTERN_CXX_END
200 #endif
201