xref: /petsc/include/petscis.h (revision 7087cfbefd1a42b179f217f9994fb6cb0d0c1824)
12eac72dbSBarry Smith /*
2f8256253SLois Curfman McInnes    An index set is a generalization of a subset of integers.  Index sets
3f8256253SLois Curfman McInnes    are used for defining scatters and gathers.
42eac72dbSBarry Smith */
50a835dfdSSatish Balay #if !defined(__PETSCIS_H)
60a835dfdSSatish Balay #define __PETSCIS_H
7d382aafbSBarry Smith #include "petscsys.h"
8e9fa29b7SSatish Balay PETSC_EXTERN_CXX_BEGIN
92eac72dbSBarry Smith 
1097b48c8fSBarry Smith #define IS_FILE_CLASSID 1211218
11*7087cfbeSBarry Smith extern  PetscClassId IS_CLASSID;
12f0479e8cSBarry Smith 
13*7087cfbeSBarry Smith extern PetscErrorCode  ISInitializePackage(const char[]);
142b6de112SBarry Smith 
155c20da3cSBarry Smith /*S
16f22f69f0SBarry Smith      IS - Abstract PETSc object that allows indexing.
175c20da3cSBarry Smith 
185c20da3cSBarry Smith    Level: beginner
195c20da3cSBarry Smith 
205c20da3cSBarry Smith   Concepts: indexing, stride
215c20da3cSBarry Smith 
225c20da3cSBarry Smith .seealso:  ISCreateGeneral(), ISCreateBlock(), ISCreateStride(), ISGetIndices(), ISDestroy()
235c20da3cSBarry Smith S*/
24f09e8eb9SSatish Balay typedef struct _p_IS* IS;
252eac72dbSBarry Smith 
2627bdab1eSBarry Smith /*E
2727bdab1eSBarry Smith     ISType - String with the name of a PETSc vector or the creation function
2827bdab1eSBarry Smith        with an optional dynamic library name, for example
2927bdab1eSBarry Smith        http://www.mcs.anl.gov/petsc/lib.a:myveccreate()
3027bdab1eSBarry Smith 
3127bdab1eSBarry Smith    Level: beginner
3227bdab1eSBarry Smith 
3327bdab1eSBarry Smith .seealso: ISSetType(), IS
3427bdab1eSBarry Smith E*/
3527bdab1eSBarry Smith #define ISType char*
3627bdab1eSBarry Smith #define ISGENERAL      "general"
3727bdab1eSBarry Smith #define ISSTRIDE       "stride"
3827bdab1eSBarry Smith #define ISBLOCK        "block"
3927bdab1eSBarry Smith 
4027bdab1eSBarry Smith /* Dynamic creation and loading functions */
4127bdab1eSBarry Smith extern PetscFList ISList;
4227bdab1eSBarry Smith extern PetscBool  ISRegisterAllCalled;
43*7087cfbeSBarry Smith extern PetscErrorCode  ISSetType(IS, const ISType);
44*7087cfbeSBarry Smith extern PetscErrorCode  ISGetType(IS, const ISType *);
45*7087cfbeSBarry Smith extern PetscErrorCode  ISRegister(const char[],const char[],const char[],PetscErrorCode (*)(IS));
46*7087cfbeSBarry Smith extern PetscErrorCode  ISRegisterAll(const char []);
47*7087cfbeSBarry Smith extern PetscErrorCode  ISRegisterDestroy(void);
48*7087cfbeSBarry Smith extern PetscErrorCode  ISCreate(MPI_Comm,IS*);
4927bdab1eSBarry Smith 
5027bdab1eSBarry Smith /*MC
5127bdab1eSBarry Smith   ISRegisterDynamic - Adds a new vector component implementation
5227bdab1eSBarry Smith 
5327bdab1eSBarry Smith   Synopsis:
5427bdab1eSBarry Smith   PetscErrorCode ISRegisterDynamic(const char *name, const char *path, const char *func_name, PetscErrorCode (*create_func)(IS))
5527bdab1eSBarry Smith 
5627bdab1eSBarry Smith   Not Collective
5727bdab1eSBarry Smith 
5827bdab1eSBarry Smith   Input Parameters:
5927bdab1eSBarry Smith + name        - The name of a new user-defined creation routine
6027bdab1eSBarry Smith . path        - The path (either absolute or relative) of the library containing this routine
6127bdab1eSBarry Smith . func_name   - The name of routine to create method context
6227bdab1eSBarry Smith - create_func - The creation routine itself
6327bdab1eSBarry Smith 
6427bdab1eSBarry Smith   Notes:
6527bdab1eSBarry Smith   ISRegisterDynamic() may be called multiple times to add several user-defined vectors
6627bdab1eSBarry Smith 
6727bdab1eSBarry Smith   If dynamic libraries are used, then the fourth input argument (routine_create) is ignored.
6827bdab1eSBarry Smith 
6927bdab1eSBarry Smith   Sample usage:
7027bdab1eSBarry Smith .vb
7127bdab1eSBarry Smith     ISRegisterDynamic("my_is","/home/username/my_lib/lib/libO/solaris/libmy.a", "MyIStorCreate", MyIStorCreate);
7227bdab1eSBarry Smith .ve
7327bdab1eSBarry Smith 
7427bdab1eSBarry Smith   Then, your vector type can be chosen with the procedural interface via
7527bdab1eSBarry Smith .vb
7627bdab1eSBarry Smith     ISCreate(MPI_Comm, IS *);
7727bdab1eSBarry Smith     ISSetType(IS,"my_vector_name");
7827bdab1eSBarry Smith .ve
7927bdab1eSBarry Smith    or at runtime via the option
8027bdab1eSBarry Smith .vb
8127bdab1eSBarry Smith     -vec_type my_vector_name
8227bdab1eSBarry Smith .ve
8327bdab1eSBarry Smith 
8427bdab1eSBarry Smith   Notes: $PETSC_ARCH occuring in pathname will be replaced with appropriate values.
8527bdab1eSBarry Smith          If your function is not being put into a shared library then use ISRegister() instead
8627bdab1eSBarry Smith 
8727bdab1eSBarry Smith   Level: advanced
8827bdab1eSBarry Smith 
8927bdab1eSBarry Smith .keywords: IS, register
9027bdab1eSBarry Smith .seealso: ISRegisterAll(), ISRegisterDestroy(), ISRegister()
9127bdab1eSBarry Smith M*/
9227bdab1eSBarry Smith #if defined(PETSC_USE_DYNAMIC_LIBRARIES)
9327bdab1eSBarry Smith #define ISRegisterDynamic(a,b,c,d) ISRegister(a,b,c,0)
9427bdab1eSBarry Smith #else
9527bdab1eSBarry Smith #define ISRegisterDynamic(a,b,c,d) ISRegister(a,b,c,d)
9627bdab1eSBarry Smith #endif
9727bdab1eSBarry Smith 
98639f9d9dSBarry Smith /*
99639f9d9dSBarry Smith     Default index set data structures that PETSc provides.
100639f9d9dSBarry Smith */
101*7087cfbeSBarry Smith extern PetscErrorCode    ISCreateGeneral(MPI_Comm,PetscInt,const PetscInt[],PetscCopyMode,IS *);
102*7087cfbeSBarry Smith extern PetscErrorCode    ISGeneralSetIndices(IS,PetscInt,const PetscInt[],PetscCopyMode);
103*7087cfbeSBarry Smith extern PetscErrorCode    ISCreateBlock(MPI_Comm,PetscInt,PetscInt,const PetscInt[],PetscCopyMode,IS *);
104*7087cfbeSBarry Smith extern PetscErrorCode    ISBlockSetIndices(IS,PetscInt,PetscInt,const PetscInt[],PetscCopyMode);
105*7087cfbeSBarry Smith extern PetscErrorCode    ISCreateStride(MPI_Comm,PetscInt,PetscInt,PetscInt,IS *);
106*7087cfbeSBarry Smith extern PetscErrorCode    ISStrideSetStride(IS,PetscInt,PetscInt,PetscInt);
1074b0e389bSBarry Smith 
108*7087cfbeSBarry Smith extern PetscErrorCode    ISDestroy(IS);
1094b0e389bSBarry Smith 
110*7087cfbeSBarry Smith extern PetscErrorCode    ISSetPermutation(IS);
111*7087cfbeSBarry Smith extern PetscErrorCode    ISPermutation(IS,PetscBool *);
112*7087cfbeSBarry Smith extern PetscErrorCode    ISSetIdentity(IS);
113*7087cfbeSBarry Smith extern PetscErrorCode    ISIdentity(IS,PetscBool *);
114*7087cfbeSBarry Smith extern PetscErrorCode    ISContiguousLocal(IS,PetscInt,PetscInt,PetscInt*,PetscBool*);
11508480c60SBarry Smith 
116*7087cfbeSBarry Smith extern PetscErrorCode    ISGetIndices(IS,const PetscInt *[]);
117*7087cfbeSBarry Smith extern PetscErrorCode    ISRestoreIndices(IS,const PetscInt *[]);
118*7087cfbeSBarry Smith extern PetscErrorCode    ISGetTotalIndices(IS,const PetscInt *[]);
119*7087cfbeSBarry Smith extern PetscErrorCode    ISRestoreTotalIndices(IS,const PetscInt *[]);
120*7087cfbeSBarry Smith extern PetscErrorCode    ISGetNonlocalIndices(IS,const PetscInt *[]);
121*7087cfbeSBarry Smith extern PetscErrorCode    ISRestoreNonlocalIndices(IS,const PetscInt *[]);
122*7087cfbeSBarry Smith extern PetscErrorCode    ISGetNonlocalIS(IS, IS *is);
123*7087cfbeSBarry Smith extern PetscErrorCode    ISRestoreNonlocalIS(IS, IS *is);
124*7087cfbeSBarry Smith extern PetscErrorCode    ISGetSize(IS,PetscInt *);
125*7087cfbeSBarry Smith extern PetscErrorCode    ISGetLocalSize(IS,PetscInt *);
126*7087cfbeSBarry Smith extern PetscErrorCode    ISInvertPermutation(IS,PetscInt,IS*);
127*7087cfbeSBarry Smith extern PetscErrorCode    ISView(IS,PetscViewer);
128*7087cfbeSBarry Smith extern PetscErrorCode    ISEqual(IS,IS,PetscBool  *);
129*7087cfbeSBarry Smith extern PetscErrorCode    ISSort(IS);
130*7087cfbeSBarry Smith extern PetscErrorCode    ISSorted(IS,PetscBool  *);
131*7087cfbeSBarry Smith extern PetscErrorCode    ISDifference(IS,IS,IS*);
132*7087cfbeSBarry Smith extern PetscErrorCode    ISSum(IS,IS,IS*);
133*7087cfbeSBarry Smith extern PetscErrorCode    ISExpand(IS,IS,IS*);
134612dd529SBarry Smith 
135*7087cfbeSBarry Smith extern PetscErrorCode    ISBlockGetIndices(IS,const PetscInt *[]);
136*7087cfbeSBarry Smith extern PetscErrorCode    ISBlockRestoreIndices(IS,const PetscInt *[]);
137*7087cfbeSBarry Smith extern PetscErrorCode    ISBlockGetLocalSize(IS,PetscInt *);
138*7087cfbeSBarry Smith extern PetscErrorCode    ISBlockGetSize(IS,PetscInt *);
139*7087cfbeSBarry Smith extern PetscErrorCode    ISGetBlockSize(IS,PetscInt*);
140*7087cfbeSBarry Smith extern PetscErrorCode    ISSetBlockSize(IS,PetscInt);
141c16cb8f2SBarry Smith 
142*7087cfbeSBarry Smith extern PetscErrorCode    ISStrideGetInfo(IS,PetscInt *,PetscInt*);
143c16cb8f2SBarry Smith 
144*7087cfbeSBarry Smith extern PetscErrorCode    ISToGeneral(IS);
14538f40f24SLois Curfman McInnes 
146*7087cfbeSBarry Smith extern PetscErrorCode    ISDuplicate(IS,IS*);
147*7087cfbeSBarry Smith extern PetscErrorCode    ISCopy(IS,IS);
148*7087cfbeSBarry Smith extern PetscErrorCode    ISAllGather(IS,IS*);
149*7087cfbeSBarry Smith extern PetscErrorCode    ISComplement(IS,PetscInt,PetscInt,IS*);
150*7087cfbeSBarry Smith extern PetscErrorCode    ISOnComm(IS,MPI_Comm,PetscCopyMode,IS*);
151d64ed03dSBarry Smith 
15256cd22aeSBarry Smith /* --------------------------------------------------------------------------*/
153*7087cfbeSBarry Smith extern  PetscClassId IS_LTOGM_CLASSID;
15456cd22aeSBarry Smith 
1555c20da3cSBarry Smith /*S
156d9ffb7b8SBarry Smith    ISLocalToGlobalMapping - mappings from an arbitrary
15790f02eecSBarry Smith       local ordering from 0 to n-1 to a global PETSc ordering
158d4bb536fSBarry Smith       used by a vector or matrix.
159d4bb536fSBarry Smith 
1605c20da3cSBarry Smith    Level: intermediate
1615c20da3cSBarry Smith 
162d4bb536fSBarry Smith    Note: mapping from Local to Global is scalable; but Global
163eec0b4cfSBarry Smith   to Local may not be if the range of global values represented locally
164d4bb536fSBarry Smith   is very large.
16574637425SBarry Smith 
16674637425SBarry Smith    Note: the ISLocalToGlobalMapping is actually a private object; it is included
16774637425SBarry Smith   here for the MACRO ISLocalToGlobalMappingApply() to allow it to be inlined since
16874637425SBarry Smith   it is used so often.
16974637425SBarry Smith 
1705c20da3cSBarry Smith .seealso:  ISLocalToGlobalMappingCreate()
1715c20da3cSBarry Smith S*/
17274637425SBarry Smith struct _p_ISLocalToGlobalMapping{
173011f5a45SSatish Balay   PETSCHEADER(int);
17432dcc486SBarry Smith   PetscInt n;                  /* number of local indices */
17532dcc486SBarry Smith   PetscInt *indices;           /* global index of each local index */
17632dcc486SBarry Smith   PetscInt globalstart;        /* first global referenced in indices */
17732dcc486SBarry Smith   PetscInt globalend;          /* last + 1 global referenced in indices */
17832dcc486SBarry Smith   PetscInt *globals;           /* local index for each global index between start and end */
17974637425SBarry Smith };
180f09e8eb9SSatish Balay typedef struct _p_ISLocalToGlobalMapping* ISLocalToGlobalMapping;
1815c20da3cSBarry Smith 
1825c20da3cSBarry Smith /*E
1835c20da3cSBarry Smith     ISGlobalToLocalMappingType - Indicates if missing global indices are
1845c20da3cSBarry Smith 
1855c20da3cSBarry Smith    IS_GTOLM_MASK - missing global indices are replaced with -1
1865c20da3cSBarry Smith    IS_GTOLM_DROP - missing global indices are dropped
1875c20da3cSBarry Smith 
1885c20da3cSBarry Smith    Level: beginner
1895c20da3cSBarry Smith 
1905c20da3cSBarry Smith .seealso: ISGlobalToLocalMappingApply()
1915c20da3cSBarry Smith 
1925c20da3cSBarry Smith E*/
193987e4450SSatish Balay typedef enum {IS_GTOLM_MASK,IS_GTOLM_DROP} ISGlobalToLocalMappingType;
19490f02eecSBarry Smith 
195*7087cfbeSBarry Smith extern PetscErrorCode  ISLocalToGlobalMappingCreate(MPI_Comm,PetscInt,const PetscInt[],PetscCopyMode,ISLocalToGlobalMapping*);
196*7087cfbeSBarry Smith extern PetscErrorCode  ISLocalToGlobalMappingCreateIS(IS,ISLocalToGlobalMapping *);
197*7087cfbeSBarry Smith extern PetscErrorCode  ISLocalToGlobalMappingView(ISLocalToGlobalMapping,PetscViewer);
198*7087cfbeSBarry Smith extern PetscErrorCode  ISLocalToGlobalMappingDestroy(ISLocalToGlobalMapping);
199*7087cfbeSBarry Smith extern PetscErrorCode  ISLocalToGlobalMappingApplyIS(ISLocalToGlobalMapping,IS,IS*);
200*7087cfbeSBarry Smith extern PetscErrorCode  ISGlobalToLocalMappingApply(ISLocalToGlobalMapping,ISGlobalToLocalMappingType,PetscInt,const PetscInt[],PetscInt*,PetscInt[]);
201*7087cfbeSBarry Smith extern PetscErrorCode  ISLocalToGlobalMappingGetSize(ISLocalToGlobalMapping,PetscInt*);
202*7087cfbeSBarry Smith extern PetscErrorCode  ISLocalToGlobalMappingGetInfo(ISLocalToGlobalMapping,PetscInt*,PetscInt*[],PetscInt*[],PetscInt**[]);
203*7087cfbeSBarry Smith extern PetscErrorCode  ISLocalToGlobalMappingRestoreInfo(ISLocalToGlobalMapping,PetscInt*,PetscInt*[],PetscInt*[],PetscInt**[]);
204*7087cfbeSBarry Smith extern PetscErrorCode  ISLocalToGlobalMappingGetIndices(ISLocalToGlobalMapping,const PetscInt**);
205*7087cfbeSBarry Smith extern PetscErrorCode  ISLocalToGlobalMappingRestoreIndices(ISLocalToGlobalMapping,const PetscInt**);
206*7087cfbeSBarry Smith extern PetscErrorCode  ISLocalToGlobalMappingBlock(ISLocalToGlobalMapping,PetscInt,ISLocalToGlobalMapping*);
207*7087cfbeSBarry Smith extern PetscErrorCode  ISLocalToGlobalMappingUnBlock(ISLocalToGlobalMapping,PetscInt,ISLocalToGlobalMapping*);
20874637425SBarry Smith 
209d0f46423SBarry Smith PETSC_STATIC_INLINE PetscErrorCode ISLocalToGlobalMappingApply(ISLocalToGlobalMapping mapping,PetscInt N,const PetscInt in[],PetscInt out[])
210d0f46423SBarry Smith {
21153ef36baSBarry Smith   PetscInt       i,Nmax = mapping->n;
21253ef36baSBarry Smith   const PetscInt *idx = mapping->indices;
213b7827b44SJed Brown   PetscFunctionBegin;
2141620fd73SBarry Smith   for (i=0; i<N; i++) {
2151620fd73SBarry Smith     if (in[i] < 0) {out[i] = in[i]; continue;}
216e32f2f54SBarry Smith     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);
2171620fd73SBarry Smith     out[i] = idx[in[i]];
2181620fd73SBarry Smith   }
2191620fd73SBarry Smith   PetscFunctionReturn(0);
22074637425SBarry Smith }
22190f02eecSBarry Smith 
22256cd22aeSBarry Smith /* --------------------------------------------------------------------------*/
223b9617806SBarry Smith /*E
224b9617806SBarry Smith     ISColoringType - determines if the coloring is for the entire parallel grid/graph/matrix
225b9617806SBarry Smith                      or for just the local ghosted portion
226b9617806SBarry Smith 
227b9617806SBarry Smith     Level: beginner
228b9617806SBarry Smith 
2298ee2e534SBarry Smith $   IS_COLORING_GLOBAL - does not include the colors for ghost points, this is used when the function
2308ee2e534SBarry Smith $                        is called synchronously in parallel. This requires generating a "parallel coloring".
2318ee2e534SBarry Smith $   IS_COLORING_GHOSTED - includes colors for ghost points, this is used when the function can be called
2328ee2e534SBarry Smith $                         seperately on individual processes with the ghost points already filled in. Does not
2338ee2e534SBarry Smith $                         require a "parallel coloring", rather each process colors its local + ghost part.
2348ee2e534SBarry Smith $                         Using this can result in much less parallel communication. In the paradigm of
2359a42bb27SBarry Smith $                         DMGetLocalVector() and DMGetGlobalVector() this could be called IS_COLORING_LOCAL
23673d7d85fSBarry Smith 
23794013140SBarry Smith .seealso: DMGetColoring()
238b9617806SBarry Smith E*/
2398ee2e534SBarry Smith typedef enum {IS_COLORING_GLOBAL,IS_COLORING_GHOSTED} ISColoringType;
240a34d58ebSBarry Smith extern const char *ISColoringTypes[];
2416c09f170SSatish Balay typedef unsigned PETSC_IS_COLOR_VALUE_TYPE ISColoringValue;
242*7087cfbeSBarry Smith extern PetscErrorCode  ISAllGatherColors(MPI_Comm,PetscInt,ISColoringValue*,PetscInt*,ISColoringValue*[]);
243dde82324SBarry Smith 
2445c20da3cSBarry Smith /*S
245dde82324SBarry Smith      ISColoring - sets of IS's that define a coloring
246639f9d9dSBarry Smith               of the underlying indices
2475c20da3cSBarry Smith 
2485c20da3cSBarry Smith    Level: intermediate
2495c20da3cSBarry Smith 
2505c20da3cSBarry Smith     Notes:
251b9617806SBarry Smith         One should not access the *is records below directly because they may not yet
2525c20da3cSBarry Smith     have been created. One should use ISColoringGetIS() to make sure they are
2535c20da3cSBarry Smith     created when needed.
2545c20da3cSBarry Smith 
2555c20da3cSBarry Smith .seealso:  ISColoringCreate(), ISColoringGetIS(), ISColoringView(), ISColoringGetIS()
2565c20da3cSBarry Smith S*/
25795fbd943SSatish Balay struct _n_ISColoring {
258a7cc72afSBarry Smith   PetscInt        refct;
259a7cc72afSBarry Smith   PetscInt        n;                /* number of colors */
2605c20da3cSBarry Smith   IS              *is;              /* for each color indicates columns */
261639f9d9dSBarry Smith   MPI_Comm        comm;
26208b6dcc0SBarry Smith   ISColoringValue *colors;          /* for each column indicates color */
26332dcc486SBarry Smith   PetscInt        N;                /* number of columns */
264b9617806SBarry Smith   ISColoringType  ctype;
265639f9d9dSBarry Smith };
26695fbd943SSatish Balay typedef struct _n_ISColoring* ISColoring;
267639f9d9dSBarry Smith 
268*7087cfbeSBarry Smith extern PetscErrorCode  ISColoringCreate(MPI_Comm,PetscInt,PetscInt,const ISColoringValue[],ISColoring*);
269*7087cfbeSBarry Smith extern PetscErrorCode  ISColoringDestroy(ISColoring);
270*7087cfbeSBarry Smith extern PetscErrorCode  ISColoringView(ISColoring,PetscViewer);
271*7087cfbeSBarry Smith extern PetscErrorCode  ISColoringGetIS(ISColoring,PetscInt*,IS*[]);
272*7087cfbeSBarry Smith extern PetscErrorCode  ISColoringRestoreIS(ISColoring,IS*[]);
2733a7fca6bSBarry Smith #define ISColoringReference(coloring) ((coloring)->refct++,0)
2743a7fca6bSBarry Smith #define ISColoringSetType(coloring,type) ((coloring)->ctype = type,0)
2753a7fca6bSBarry Smith 
276dbef8a1cSBarry Smith /* --------------------------------------------------------------------------*/
277dbef8a1cSBarry Smith 
278*7087cfbeSBarry Smith extern PetscErrorCode  ISPartitioningToNumbering(IS,IS*);
279*7087cfbeSBarry Smith extern PetscErrorCode  ISPartitioningCount(IS,PetscInt,PetscInt[]);
280dbef8a1cSBarry Smith 
281*7087cfbeSBarry Smith extern PetscErrorCode  ISCompressIndicesGeneral(PetscInt,PetscInt,PetscInt,const IS[],IS[]);
282*7087cfbeSBarry Smith extern PetscErrorCode  ISCompressIndicesSorted(PetscInt,PetscInt,PetscInt,const IS[],IS[]);
283*7087cfbeSBarry Smith extern PetscErrorCode  ISExpandIndicesGeneral(PetscInt,PetscInt,PetscInt,const IS[],IS[]);
284d9489beaSHong Zhang 
285a5891931SBarry Smith 
286e9fa29b7SSatish Balay PETSC_EXTERN_CXX_END
287a2ce50c7SBarry Smith #endif
288