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