xref: /petsc/src/vec/is/utils/isltog.c (revision 5a5d4f665b3034485fbcdfbe42345f740a4bd170)
1d4bb536fSBarry Smith 
2a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
3*5a5d4f66SBarry Smith static char vcid[] = "$Id: isltog.c,v 1.17 1997/10/19 03:22:23 bsmith Exp bsmith $";
42362add9SBarry Smith #endif
52362add9SBarry Smith 
62362add9SBarry Smith #include "sys.h"   /*I "sys.h" I*/
7d4bb536fSBarry Smith #include "src/is/isimpl.h"    /*I "is.h"  I*/
82362add9SBarry Smith 
95615d1e5SSatish Balay #undef __FUNC__
10*5a5d4f66SBarry Smith #define __FUNC__ "ISLocalToGlobalMappingView"
11*5a5d4f66SBarry Smith /*@C
12*5a5d4f66SBarry Smith     ISLocalToGlobalMappingView - View a local to global mapping
13*5a5d4f66SBarry Smith 
14*5a5d4f66SBarry Smith     Input Parameters:
15*5a5d4f66SBarry Smith .   ltog - local to global mapping
16*5a5d4f66SBarry Smith 
17*5a5d4f66SBarry Smith    Collective on ISLocalToGlobalMapping and Viewer
18*5a5d4f66SBarry Smith 
19*5a5d4f66SBarry Smith .keywords: IS, local-to-global mapping, create
20*5a5d4f66SBarry Smith 
21*5a5d4f66SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
22*5a5d4f66SBarry Smith @*/
23*5a5d4f66SBarry Smith int ISLocalToGlobalMappingView(ISLocalToGlobalMapping mapping,Viewer viewer)
24*5a5d4f66SBarry Smith {
25*5a5d4f66SBarry Smith   int      i;
26*5a5d4f66SBarry Smith 
27*5a5d4f66SBarry Smith   PetscFunctionBegin;
28*5a5d4f66SBarry Smith 
29*5a5d4f66SBarry Smith   for ( i=0; i<mapping->n; i++ ) {
30*5a5d4f66SBarry Smith     printf("%d %d\n",i,mapping->indices[i]);
31*5a5d4f66SBarry Smith   }
32*5a5d4f66SBarry Smith 
33*5a5d4f66SBarry Smith   PetscFunctionReturn(0);
34*5a5d4f66SBarry Smith }
35*5a5d4f66SBarry Smith 
36*5a5d4f66SBarry Smith #undef __FUNC__
372bdab257SBarry Smith #define __FUNC__ "ISLocalToGlobalMappingCreateIS"
382bdab257SBarry Smith /*@C
392bdab257SBarry Smith     ISLocalToGlobalMappingCreateIS - Creates a mapping between a local (0 to n)
402bdab257SBarry Smith     ordering and a global parallel ordering.
412bdab257SBarry Smith 
422bdab257SBarry Smith     Input Parameters:
432bdab257SBarry Smith .   is - index set containing the global numbers for each local
442bdab257SBarry Smith 
452bdab257SBarry Smith     Output Parameters:
462bdab257SBarry Smith .   mapping - new mapping data structure
472bdab257SBarry Smith 
48*5a5d4f66SBarry Smith    Collective on IS
49*5a5d4f66SBarry Smith 
502bdab257SBarry Smith .keywords: IS, local-to-global mapping, create
512bdab257SBarry Smith 
522bdab257SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
532bdab257SBarry Smith @*/
542bdab257SBarry Smith int ISLocalToGlobalMappingCreateIS(IS is,ISLocalToGlobalMapping *mapping)
552bdab257SBarry Smith {
562bdab257SBarry Smith   int      n,*indices,ierr;
572bdab257SBarry Smith   MPI_Comm comm;
583a40ed3dSBarry Smith 
593a40ed3dSBarry Smith   PetscFunctionBegin;
602bdab257SBarry Smith   PetscValidHeaderSpecific(is,IS_COOKIE);
612bdab257SBarry Smith 
622bdab257SBarry Smith   ierr = PetscObjectGetComm((PetscObject)is,&comm); CHKERRQ(ierr);
632bdab257SBarry Smith   ierr = ISGetSize(is,&n);CHKERRQ(ierr);
642bdab257SBarry Smith   ierr = ISGetIndices(is,&indices);CHKERRQ(ierr);
652bdab257SBarry Smith   ierr = ISLocalToGlobalMappingCreate(comm,n,indices,mapping);CHKERRQ(ierr);
662bdab257SBarry Smith   ierr = ISRestoreIndices(is,&indices);CHKERRQ(ierr);
672bdab257SBarry Smith 
683a40ed3dSBarry Smith   PetscFunctionReturn(0);
692bdab257SBarry Smith }
70*5a5d4f66SBarry Smith 
712bdab257SBarry Smith #undef __FUNC__
72d4bb536fSBarry Smith #define __FUNC__ "ISLocalToGlobalMappingCreate"
73dd7157adSSatish Balay /*@C
7490f02eecSBarry Smith     ISLocalToGlobalMappingCreate - Creates a mapping between a local (0 to n)
7590f02eecSBarry Smith     ordering and a global parallel ordering.
762362add9SBarry Smith 
772362add9SBarry Smith     Input Parameters:
78d4bb536fSBarry Smith .   comm - MPI communicator of size 1.
7990f02eecSBarry Smith .   n - the number of local elements
8090f02eecSBarry Smith .   indices - the global index for each local element
812362add9SBarry Smith 
822362add9SBarry Smith     Output Parameters:
8390f02eecSBarry Smith .   mapping - new mapping data structure
842362add9SBarry Smith 
85*5a5d4f66SBarry Smith     Collective on MPI_Comm
86*5a5d4f66SBarry Smith 
873acfe500SLois Curfman McInnes .keywords: IS, local-to-global mapping, create
882362add9SBarry Smith 
892bdab257SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS()
902362add9SBarry Smith @*/
91d4bb536fSBarry Smith int ISLocalToGlobalMappingCreate(MPI_Comm cm,int n, int *indices,ISLocalToGlobalMapping *mapping)
922362add9SBarry Smith {
933a40ed3dSBarry Smith   PetscFunctionBegin;
9490f02eecSBarry Smith   PetscValidIntPointer(indices);
9590f02eecSBarry Smith   PetscValidPointer(mapping);
962362add9SBarry Smith 
97*5a5d4f66SBarry Smith   PetscHeaderCreate(*mapping,_p_ISLocalToGlobalMapping,IS_LTOGM_COOKIE,0,cm,ISLocalToGlobalMappingDestroy,
98*5a5d4f66SBarry Smith                     ISLocalToGlobalMappingView);
99d4bb536fSBarry Smith   PLogObjectCreate(*mapping);
100d4bb536fSBarry Smith   PLogObjectMemory(*mapping,sizeof(struct _p_ISLocalToGlobalMapping)+n*sizeof(int));
101d4bb536fSBarry Smith 
102d4bb536fSBarry Smith   (*mapping)->n       = n;
10390f02eecSBarry Smith   (*mapping)->indices = (int *) PetscMalloc((n+1)*sizeof(int));CHKPTRQ((*mapping)->indices);
10490f02eecSBarry Smith   PetscMemcpy((*mapping)->indices,indices,n*sizeof(int));
105d4bb536fSBarry Smith 
106d4bb536fSBarry Smith   /*
107d4bb536fSBarry Smith       Do not create the global to local mapping. This is only created if
108d4bb536fSBarry Smith      ISGlobalToLocalMapping() is called
109d4bb536fSBarry Smith   */
110d4bb536fSBarry Smith   (*mapping)->globals = 0;
1113a40ed3dSBarry Smith   PetscFunctionReturn(0);
1122362add9SBarry Smith }
1132362add9SBarry Smith 
1145615d1e5SSatish Balay #undef __FUNC__
115d4bb536fSBarry Smith #define __FUNC__ "ISLocalToGlobalMappingDestroy"
11690f02eecSBarry Smith /*@
11790f02eecSBarry Smith    ISLocalToGlobalMappingDestroy - Destroys a mapping between a local (0 to n)
11890f02eecSBarry Smith    ordering and a global parallel ordering.
11990f02eecSBarry Smith 
12090f02eecSBarry Smith    Input Parameters:
12190f02eecSBarry Smith .  mapping - mapping data structure
12290f02eecSBarry Smith 
123*5a5d4f66SBarry Smith    Collective on ISLocalToGlobalMapping
124*5a5d4f66SBarry Smith 
1253acfe500SLois Curfman McInnes .keywords: IS, local-to-global mapping, destroy
12690f02eecSBarry Smith 
1273acfe500SLois Curfman McInnes .seealso: ISLocalToGlobalMappingCreate()
12890f02eecSBarry Smith @*/
12990f02eecSBarry Smith int ISLocalToGlobalMappingDestroy(ISLocalToGlobalMapping mapping)
13090f02eecSBarry Smith {
1313a40ed3dSBarry Smith   PetscFunctionBegin;
13290f02eecSBarry Smith   PetscValidPointer(mapping);
1333a40ed3dSBarry Smith   if (--mapping->refct > 0) PetscFunctionReturn(0);
13490f02eecSBarry Smith 
13590f02eecSBarry Smith   PetscFree(mapping->indices);
136d4bb536fSBarry Smith   if (mapping->globals) PetscFree(mapping->globals);
137d4bb536fSBarry Smith   PLogObjectDestroy(mapping);
138d4bb536fSBarry Smith   PetscHeaderDestroy(mapping);
1393a40ed3dSBarry Smith   PetscFunctionReturn(0);
14090f02eecSBarry Smith }
14190f02eecSBarry Smith 
1425615d1e5SSatish Balay #undef __FUNC__
143d4bb536fSBarry Smith #define __FUNC__ "ISLocalToGlobalMappingApplyIS"
14490f02eecSBarry Smith /*@
1453acfe500SLois Curfman McInnes     ISLocalToGlobalMappingApplyIS - Creates from an IS in the local numbering
1463acfe500SLois Curfman McInnes     a new index set using the global numbering defined in an ISLocalToGlobalMapping
1473acfe500SLois Curfman McInnes     context.
14890f02eecSBarry Smith 
14990f02eecSBarry Smith     Input Parameters:
150d4bb536fSBarry Smith .   mapping - mapping between local and global numbering
15190f02eecSBarry Smith .   is - index set in local numbering
15290f02eecSBarry Smith 
15390f02eecSBarry Smith     Output Parameters:
15490f02eecSBarry Smith .   newis - index set in global numbering
15590f02eecSBarry Smith 
156*5a5d4f66SBarry Smith     Not collective
157*5a5d4f66SBarry Smith 
1583acfe500SLois Curfman McInnes .keywords: IS, local-to-global mapping, apply
1593acfe500SLois Curfman McInnes 
16090f02eecSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),
161d4bb536fSBarry Smith           ISLocalToGlobalMappingDestroy(), ISGlobalToLocalMappingApply()
16290f02eecSBarry Smith @*/
16390f02eecSBarry Smith int ISLocalToGlobalMappingApplyIS(ISLocalToGlobalMapping mapping, IS is, IS *newis)
16490f02eecSBarry Smith {
16590f02eecSBarry Smith   int ierr,n,i,*idxin,*idxmap,*idxout;
1663a40ed3dSBarry Smith 
1673a40ed3dSBarry Smith   PetscFunctionBegin;
16890f02eecSBarry Smith   PetscValidPointer(mapping);
16990f02eecSBarry Smith   PetscValidHeaderSpecific(is,IS_COOKIE);
17090f02eecSBarry Smith   PetscValidPointer(newis);
17190f02eecSBarry Smith 
17290f02eecSBarry Smith   ierr   = ISGetSize(is,&n); CHKERRQ(ierr);
17390f02eecSBarry Smith   ierr   = ISGetIndices(is,&idxin); CHKERRQ(ierr);
17490f02eecSBarry Smith   idxmap = mapping->indices;
17590f02eecSBarry Smith 
17690f02eecSBarry Smith   idxout = (int *) PetscMalloc((n+1)*sizeof(int));CHKPTRQ(idxout);
17790f02eecSBarry Smith   for ( i=0; i<n; i++ ) {
17890f02eecSBarry Smith     idxout[i] = idxmap[idxin[i]];
17990f02eecSBarry Smith   }
180029af93fSBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,n,idxout,newis); CHKERRQ(ierr);
18190f02eecSBarry Smith   PetscFree(idxout);
1823a40ed3dSBarry Smith   PetscFunctionReturn(0);
18390f02eecSBarry Smith }
18490f02eecSBarry Smith 
1855615d1e5SSatish Balay #undef __FUNC__
186d4bb536fSBarry Smith #define __FUNC__ "ISLocalToGlobalMappingApply"
187d4bb536fSBarry Smith /*@C
1883acfe500SLois Curfman McInnes    ISLocalToGlobalMappingApply - Takes a list of integers in a local numbering
1893acfe500SLois Curfman McInnes    and converts them to the global numbering.
19090f02eecSBarry Smith 
191bb25748dSBarry Smith    Input Parameters:
192bb25748dSBarry Smith .  mapping - the local to global mapping context
193bb25748dSBarry Smith .  N - number of integers
194bb25748dSBarry Smith .  in - input indices in local numbering
195bb25748dSBarry Smith 
196bb25748dSBarry Smith    Output Parameter:
197bb25748dSBarry Smith .  out - indices in global numbering
198bb25748dSBarry Smith 
199*5a5d4f66SBarry Smith    Not collective
200*5a5d4f66SBarry Smith 
201d4bb536fSBarry Smith    Notes: The in and out array may be identical
202d4bb536fSBarry Smith 
203bb25748dSBarry Smith .seealso: ISLocalToGlobalMappingCreate(),ISLocalToGlobalMappingDestroy(),
2040752156aSBarry Smith           ISLocalToGlobalMappingApplyIS(),AOCreateBasic(),AOApplicationToPetsc(),
205d4bb536fSBarry Smith           AOPetscToApplication(), ISGlobalToLocalMappingApply()
206bb25748dSBarry Smith 
2073acfe500SLois Curfman McInnes .keywords: local-to-global, mapping, apply
208d4bb536fSBarry Smith 
209d4bb536fSBarry Smith @*/
210d4bb536fSBarry Smith int ISLocalToGlobalMappingApply(ISLocalToGlobalMapping mapping,int N,int *in,int *out)
211d4bb536fSBarry Smith {
212d4bb536fSBarry Smith   int i,*idx = mapping->indices,Nmax = mapping->n;
2133a40ed3dSBarry Smith 
2143a40ed3dSBarry Smith   PetscFunctionBegin;
215d4bb536fSBarry Smith   for ( i=0; i<N; i++ ) {
216d4bb536fSBarry Smith     if (in[i] < 0) {out[i] = in[i]; continue;}
217d4bb536fSBarry Smith     if (in[i] >= Nmax) SETERRQ(1,1,"Local index too large");
218d4bb536fSBarry Smith     out[i] = idx[in[i]];
219d4bb536fSBarry Smith   }
2203a40ed3dSBarry Smith   PetscFunctionReturn(0);
221d4bb536fSBarry Smith }
222d4bb536fSBarry Smith 
223d4bb536fSBarry Smith /* -----------------------------------------------------------------------------------------*/
224d4bb536fSBarry Smith 
225d4bb536fSBarry Smith #undef __FUNC__
226d4bb536fSBarry Smith #define __FUNC__ "ISGlobalToLocalMappingSetUp_Private"
227d4bb536fSBarry Smith /*
228d4bb536fSBarry Smith     Creates the global fields in the ISLocalToGlobalMapping structure
229d4bb536fSBarry Smith */
230d4bb536fSBarry Smith static int ISGlobalToLocalMappingSetUp_Private(ISLocalToGlobalMapping mapping)
231d4bb536fSBarry Smith {
232d4bb536fSBarry Smith   int i,*idx = mapping->indices,n = mapping->n,end,start,*globals;
233d4bb536fSBarry Smith 
2343a40ed3dSBarry Smith   PetscFunctionBegin;
235d4bb536fSBarry Smith   end   = 0;
236d4bb536fSBarry Smith   start = 100000000;
237d4bb536fSBarry Smith 
238d4bb536fSBarry Smith   for ( i=0; i<n; i++ ) {
239d4bb536fSBarry Smith     if (idx[i] < 0) continue;
240d4bb536fSBarry Smith     if (idx[i] < start) start = idx[i];
241d4bb536fSBarry Smith     if (idx[i] > end)   end   = idx[i];
242d4bb536fSBarry Smith   }
243d4bb536fSBarry Smith   if (start > end) {start = 0; end = -1;}
244d4bb536fSBarry Smith   mapping->globalstart = start;
245d4bb536fSBarry Smith   mapping->globalend   = end;
246d4bb536fSBarry Smith 
247d4bb536fSBarry Smith   globals = mapping->globals = (int *) PetscMalloc((end-start+2)*sizeof(int));CHKPTRQ(mapping->globals);
248d4bb536fSBarry Smith   for ( i=0; i<end-start+1; i++ ) {
249d4bb536fSBarry Smith     globals[i] = -1;
250d4bb536fSBarry Smith   }
251d4bb536fSBarry Smith   for ( i=0; i<n; i++ ) {
252d4bb536fSBarry Smith     if (idx[i] < 0) continue;
253d4bb536fSBarry Smith     globals[idx[i] - start] = i;
254d4bb536fSBarry Smith   }
255d4bb536fSBarry Smith 
256d4bb536fSBarry Smith   PLogObjectMemory(mapping,(end-start+1)*sizeof(int));
2573a40ed3dSBarry Smith   PetscFunctionReturn(0);
258d4bb536fSBarry Smith }
259d4bb536fSBarry Smith 
260d4bb536fSBarry Smith #undef __FUNC__
261d4bb536fSBarry Smith #define __FUNC__ "ISGlobalToLocalMappingApply"
262d4bb536fSBarry Smith /*@
263d4bb536fSBarry Smith     ISGlobalToLocalMappingApply - Takes a list of integers in global numbering
264d4bb536fSBarry Smith       and returns the local numbering.
265d4bb536fSBarry Smith 
266d4bb536fSBarry Smith     Input Parameters:
267d4bb536fSBarry Smith .   mapping - mapping between local and global numbering
268d4bb536fSBarry Smith .   type - IS_GTOLM_MASK - replaces global indices with no local value with -1
269d4bb536fSBarry Smith            IS_GTOLM_DROP - drops the indices with no local value from the output list
270d4bb536fSBarry Smith .   n - number of global indices to map
271d4bb536fSBarry Smith .   idx - global indices to map
272d4bb536fSBarry Smith 
273d4bb536fSBarry Smith     Output Parameters:
274d4bb536fSBarry Smith .   nout - number of indices in output array (if type == IS_GTOLM_MASK then nout = n)
275e182c471SBarry Smith .   idxout - local index of each global index, one must pass in an array long enough
276e182c471SBarry Smith              to hold all the indices. You can call ISGlobalToLocalMappingApply() with
277e182c471SBarry Smith              idxout == PETSC_NULL to determine the required length (returned in nout)
278e182c471SBarry Smith              and then allocate the required space and call ISGlobalToLocalMappingApply()
279e182c471SBarry Smith              a second time to set the values.
280d4bb536fSBarry Smith 
281*5a5d4f66SBarry Smith     Not collective
282*5a5d4f66SBarry Smith 
283d4bb536fSBarry Smith     Notes: Either nout or idxout may be PETSC_NULL. idx and idxout may be identical.
284d4bb536fSBarry Smith 
285d4bb536fSBarry Smith .keywords: IS, global-to-local mapping, apply
286d4bb536fSBarry Smith 
287d4bb536fSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),
288d4bb536fSBarry Smith           ISLocalToGlobalMappingDestroy()
289d4bb536fSBarry Smith @*/
290d4bb536fSBarry Smith int ISGlobalToLocalMappingApply(ISLocalToGlobalMapping mapping, ISGlobalToLocalMappingType type,
291d4bb536fSBarry Smith                                   int n, int *idx,int *nout,int *idxout)
292d4bb536fSBarry Smith {
293d4bb536fSBarry Smith   int i,ierr, *globals,nf = 0,tmp,start,end;
294d4bb536fSBarry Smith 
2953a40ed3dSBarry Smith   PetscFunctionBegin;
296d4bb536fSBarry Smith   if (!mapping->globals) {
297d4bb536fSBarry Smith     ierr = ISGlobalToLocalMappingSetUp_Private(mapping); CHKERRQ(ierr);
298d4bb536fSBarry Smith   }
299d4bb536fSBarry Smith   globals = mapping->globals;
300d4bb536fSBarry Smith   start   = mapping->globalstart;
301d4bb536fSBarry Smith   end     = mapping->globalend;
302d4bb536fSBarry Smith 
303d4bb536fSBarry Smith   if (type == IS_GTOLM_MASK) {
304d4bb536fSBarry Smith     if (idxout) {
305d4bb536fSBarry Smith       for ( i=0; i<n; i++ ) {
306d4bb536fSBarry Smith         if (idx[i] < 0) idxout[i] = idx[i];
307d4bb536fSBarry Smith         else if (idx[i] < start) idxout[i] = -1;
308d4bb536fSBarry Smith         else if (idx[i] > end)   idxout[i] = -1;
309d4bb536fSBarry Smith         else                     idxout[i] = globals[idx[i] - start];
310d4bb536fSBarry Smith       }
311d4bb536fSBarry Smith     }
312d4bb536fSBarry Smith     if (nout) *nout = n;
313d4bb536fSBarry Smith   } else {
314d4bb536fSBarry Smith     if (idxout) {
315d4bb536fSBarry Smith       for ( i=0; i<n; i++ ) {
316d4bb536fSBarry Smith         if (idx[i] < 0) continue;
317d4bb536fSBarry Smith         if (idx[i] < start) continue;
318d4bb536fSBarry Smith         if (idx[i] > end) continue;
319d4bb536fSBarry Smith         tmp = globals[idx[i] - start];
320d4bb536fSBarry Smith         if (tmp < 0) continue;
321d4bb536fSBarry Smith         idxout[nf++] = tmp;
322d4bb536fSBarry Smith       }
323d4bb536fSBarry Smith     } else {
324d4bb536fSBarry Smith       for ( i=0; i<n; i++ ) {
325d4bb536fSBarry Smith         if (idx[i] < 0) continue;
326d4bb536fSBarry Smith         if (idx[i] < start) continue;
327d4bb536fSBarry Smith         if (idx[i] > end) continue;
328d4bb536fSBarry Smith         tmp = globals[idx[i] - start];
329d4bb536fSBarry Smith         if (tmp < 0) continue;
330d4bb536fSBarry Smith         nf++;
331d4bb536fSBarry Smith       }
332d4bb536fSBarry Smith     }
333d4bb536fSBarry Smith     if (nout) *nout = nf;
334d4bb536fSBarry Smith   }
335d4bb536fSBarry Smith 
3363a40ed3dSBarry Smith   PetscFunctionReturn(0);
337d4bb536fSBarry Smith }
33890f02eecSBarry Smith 
339