xref: /petsc/src/vec/is/utils/isltog.c (revision a997ad1a2eba7846462581b0a04ba1dfb2d5957c)
1d4bb536fSBarry Smith 
2a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
3*a997ad1aSLois Curfman McInnes static char vcid[] = "$Id: isltog.c,v 1.26 1999/01/31 16:03:37 bsmith Exp curfman $";
42362add9SBarry Smith #endif
52362add9SBarry Smith 
62362add9SBarry Smith #include "sys.h"   /*I "sys.h" I*/
783271157SBarry Smith #include "src/vec/is/isimpl.h"    /*I "is.h"  I*/
82362add9SBarry Smith 
95615d1e5SSatish Balay #undef __FUNC__
105a5d4f66SBarry Smith #define __FUNC__ "ISLocalToGlobalMappingView"
115a5d4f66SBarry Smith /*@C
125a5d4f66SBarry Smith     ISLocalToGlobalMappingView - View a local to global mapping
135a5d4f66SBarry Smith 
14b9cd556bSLois Curfman McInnes     Not Collective
15b9cd556bSLois Curfman McInnes 
165a5d4f66SBarry Smith     Input Parameters:
175a5d4f66SBarry Smith .   ltog - local to global mapping
18b9cd556bSLois Curfman McInnes .   viewer - viewer
195a5d4f66SBarry Smith 
20*a997ad1aSLois Curfman McInnes     Level: advanced
21*a997ad1aSLois Curfman McInnes 
225a5d4f66SBarry Smith .keywords: IS, local-to-global mapping, create
235a5d4f66SBarry Smith 
245a5d4f66SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
255a5d4f66SBarry Smith @*/
265a5d4f66SBarry Smith int ISLocalToGlobalMappingView(ISLocalToGlobalMapping mapping,Viewer viewer)
275a5d4f66SBarry Smith {
285a5d4f66SBarry Smith   int      i;
295a5d4f66SBarry Smith 
305a5d4f66SBarry Smith   PetscFunctionBegin;
315a5d4f66SBarry Smith 
325a5d4f66SBarry Smith   for ( i=0; i<mapping->n; i++ ) {
335a5d4f66SBarry Smith     printf("%d %d\n",i,mapping->indices[i]);
345a5d4f66SBarry Smith   }
355a5d4f66SBarry Smith 
365a5d4f66SBarry Smith   PetscFunctionReturn(0);
375a5d4f66SBarry Smith }
385a5d4f66SBarry Smith 
395a5d4f66SBarry Smith #undef __FUNC__
402bdab257SBarry Smith #define __FUNC__ "ISLocalToGlobalMappingCreateIS"
412bdab257SBarry Smith /*@C
422bdab257SBarry Smith     ISLocalToGlobalMappingCreateIS - Creates a mapping between a local (0 to n)
432bdab257SBarry Smith     ordering and a global parallel ordering.
442bdab257SBarry Smith 
45b9cd556bSLois Curfman McInnes     Collective on IS
46b9cd556bSLois Curfman McInnes 
47*a997ad1aSLois Curfman McInnes     Input Parameter:
482bdab257SBarry Smith .   is - index set containing the global numbers for each local
492bdab257SBarry Smith 
50*a997ad1aSLois Curfman McInnes     Output Parameter:
512bdab257SBarry Smith .   mapping - new mapping data structure
522bdab257SBarry Smith 
53*a997ad1aSLois Curfman McInnes     Level: advanced
54*a997ad1aSLois Curfman McInnes 
552bdab257SBarry Smith .keywords: IS, local-to-global mapping, create
562bdab257SBarry Smith 
572bdab257SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
582bdab257SBarry Smith @*/
592bdab257SBarry Smith int ISLocalToGlobalMappingCreateIS(IS is,ISLocalToGlobalMapping *mapping)
602bdab257SBarry Smith {
612bdab257SBarry Smith   int      n,*indices,ierr;
622bdab257SBarry Smith   MPI_Comm comm;
633a40ed3dSBarry Smith 
643a40ed3dSBarry Smith   PetscFunctionBegin;
652bdab257SBarry Smith   PetscValidHeaderSpecific(is,IS_COOKIE);
662bdab257SBarry Smith 
672bdab257SBarry Smith   ierr = PetscObjectGetComm((PetscObject)is,&comm); CHKERRQ(ierr);
682bdab257SBarry Smith   ierr = ISGetSize(is,&n);CHKERRQ(ierr);
692bdab257SBarry Smith   ierr = ISGetIndices(is,&indices);CHKERRQ(ierr);
702bdab257SBarry Smith   ierr = ISLocalToGlobalMappingCreate(comm,n,indices,mapping);CHKERRQ(ierr);
712bdab257SBarry Smith   ierr = ISRestoreIndices(is,&indices);CHKERRQ(ierr);
722bdab257SBarry Smith 
733a40ed3dSBarry Smith   PetscFunctionReturn(0);
742bdab257SBarry Smith }
755a5d4f66SBarry Smith 
762bdab257SBarry Smith #undef __FUNC__
77d4bb536fSBarry Smith #define __FUNC__ "ISLocalToGlobalMappingCreate"
78dd7157adSSatish Balay /*@C
7990f02eecSBarry Smith     ISLocalToGlobalMappingCreate - Creates a mapping between a local (0 to n)
8090f02eecSBarry Smith     ordering and a global parallel ordering.
812362add9SBarry Smith 
82b9cd556bSLois Curfman McInnes     Collective on MPI_Comm
83b9cd556bSLois Curfman McInnes 
842362add9SBarry Smith     Input Parameters:
85b9cd556bSLois Curfman McInnes +   comm - MPI communicator of size 1.
8690f02eecSBarry Smith .   n - the number of local elements
87b9cd556bSLois Curfman McInnes -   indices - the global index for each local element
882362add9SBarry Smith 
89*a997ad1aSLois Curfman McInnes     Output Parameter:
9090f02eecSBarry Smith .   mapping - new mapping data structure
912362add9SBarry Smith 
92*a997ad1aSLois Curfman McInnes     Level: advanced
93*a997ad1aSLois Curfman McInnes 
943acfe500SLois Curfman McInnes .keywords: IS, local-to-global mapping, create
952362add9SBarry Smith 
962bdab257SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS()
972362add9SBarry Smith @*/
98987e4450SSatish Balay int ISLocalToGlobalMappingCreate(MPI_Comm cm,int n,const int indices[],ISLocalToGlobalMapping *mapping)
992362add9SBarry Smith {
1003a40ed3dSBarry Smith   PetscFunctionBegin;
10190f02eecSBarry Smith   PetscValidIntPointer(indices);
10290f02eecSBarry Smith   PetscValidPointer(mapping);
1032362add9SBarry Smith 
1043f1db9ecSBarry Smith   PetscHeaderCreate(*mapping,_p_ISLocalToGlobalMapping,int,IS_LTOGM_COOKIE,0,"ISLocalToGlobalMapping",
1053f1db9ecSBarry Smith                     cm,ISLocalToGlobalMappingDestroy,ISLocalToGlobalMappingView);
106d4bb536fSBarry Smith   PLogObjectCreate(*mapping);
107d4bb536fSBarry Smith   PLogObjectMemory(*mapping,sizeof(struct _p_ISLocalToGlobalMapping)+n*sizeof(int));
108d4bb536fSBarry Smith 
109d4bb536fSBarry Smith   (*mapping)->n       = n;
11090f02eecSBarry Smith   (*mapping)->indices = (int *) PetscMalloc((n+1)*sizeof(int));CHKPTRQ((*mapping)->indices);
11190f02eecSBarry Smith   PetscMemcpy((*mapping)->indices,indices,n*sizeof(int));
112d4bb536fSBarry Smith 
113d4bb536fSBarry Smith   /*
114d4bb536fSBarry Smith       Do not create the global to local mapping. This is only created if
115d4bb536fSBarry Smith      ISGlobalToLocalMapping() is called
116d4bb536fSBarry Smith   */
117d4bb536fSBarry Smith   (*mapping)->globals = 0;
1183a40ed3dSBarry Smith   PetscFunctionReturn(0);
1192362add9SBarry Smith }
1202362add9SBarry Smith 
1215615d1e5SSatish Balay #undef __FUNC__
122d4bb536fSBarry Smith #define __FUNC__ "ISLocalToGlobalMappingDestroy"
12390f02eecSBarry Smith /*@
12490f02eecSBarry Smith    ISLocalToGlobalMappingDestroy - Destroys a mapping between a local (0 to n)
12590f02eecSBarry Smith    ordering and a global parallel ordering.
12690f02eecSBarry Smith 
127b9cd556bSLois Curfman McInnes    Collective on ISLocalToGlobalMapping
128b9cd556bSLois Curfman McInnes 
12990f02eecSBarry Smith    Input Parameters:
13090f02eecSBarry Smith .  mapping - mapping data structure
13190f02eecSBarry Smith 
132*a997ad1aSLois Curfman McInnes    Level: advanced
133*a997ad1aSLois Curfman McInnes 
1343acfe500SLois Curfman McInnes .keywords: IS, local-to-global mapping, destroy
13590f02eecSBarry Smith 
1363acfe500SLois Curfman McInnes .seealso: ISLocalToGlobalMappingCreate()
13790f02eecSBarry Smith @*/
13890f02eecSBarry Smith int ISLocalToGlobalMappingDestroy(ISLocalToGlobalMapping mapping)
13990f02eecSBarry Smith {
1403a40ed3dSBarry Smith   PetscFunctionBegin;
14190f02eecSBarry Smith   PetscValidPointer(mapping);
1423a40ed3dSBarry Smith   if (--mapping->refct > 0) PetscFunctionReturn(0);
14390f02eecSBarry Smith 
14490f02eecSBarry Smith   PetscFree(mapping->indices);
145d4bb536fSBarry Smith   if (mapping->globals) PetscFree(mapping->globals);
146d4bb536fSBarry Smith   PLogObjectDestroy(mapping);
147d4bb536fSBarry Smith   PetscHeaderDestroy(mapping);
1483a40ed3dSBarry Smith   PetscFunctionReturn(0);
14990f02eecSBarry Smith }
15090f02eecSBarry Smith 
1515615d1e5SSatish Balay #undef __FUNC__
152d4bb536fSBarry Smith #define __FUNC__ "ISLocalToGlobalMappingApplyIS"
15390f02eecSBarry Smith /*@
1543acfe500SLois Curfman McInnes     ISLocalToGlobalMappingApplyIS - Creates from an IS in the local numbering
1553acfe500SLois Curfman McInnes     a new index set using the global numbering defined in an ISLocalToGlobalMapping
1563acfe500SLois Curfman McInnes     context.
15790f02eecSBarry Smith 
158b9cd556bSLois Curfman McInnes     Not collective
159b9cd556bSLois Curfman McInnes 
16090f02eecSBarry Smith     Input Parameters:
161b9cd556bSLois Curfman McInnes +   mapping - mapping between local and global numbering
162b9cd556bSLois Curfman McInnes -   is - index set in local numbering
16390f02eecSBarry Smith 
16490f02eecSBarry Smith     Output Parameters:
16590f02eecSBarry Smith .   newis - index set in global numbering
16690f02eecSBarry Smith 
167*a997ad1aSLois Curfman McInnes     Level: advanced
168*a997ad1aSLois Curfman McInnes 
1693acfe500SLois Curfman McInnes .keywords: IS, local-to-global mapping, apply
1703acfe500SLois Curfman McInnes 
17190f02eecSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),
172d4bb536fSBarry Smith           ISLocalToGlobalMappingDestroy(), ISGlobalToLocalMappingApply()
17390f02eecSBarry Smith @*/
17490f02eecSBarry Smith int ISLocalToGlobalMappingApplyIS(ISLocalToGlobalMapping mapping, IS is, IS *newis)
17590f02eecSBarry Smith {
17690f02eecSBarry Smith   int ierr,n,i,*idxin,*idxmap,*idxout;
1773a40ed3dSBarry Smith 
1783a40ed3dSBarry Smith   PetscFunctionBegin;
17990f02eecSBarry Smith   PetscValidPointer(mapping);
18090f02eecSBarry Smith   PetscValidHeaderSpecific(is,IS_COOKIE);
18190f02eecSBarry Smith   PetscValidPointer(newis);
18290f02eecSBarry Smith 
18390f02eecSBarry Smith   ierr   = ISGetSize(is,&n); CHKERRQ(ierr);
18490f02eecSBarry Smith   ierr   = ISGetIndices(is,&idxin); CHKERRQ(ierr);
18590f02eecSBarry Smith   idxmap = mapping->indices;
18690f02eecSBarry Smith 
18790f02eecSBarry Smith   idxout = (int *) PetscMalloc((n+1)*sizeof(int));CHKPTRQ(idxout);
18890f02eecSBarry Smith   for ( i=0; i<n; i++ ) {
18990f02eecSBarry Smith     idxout[i] = idxmap[idxin[i]];
19090f02eecSBarry Smith   }
191029af93fSBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,n,idxout,newis); CHKERRQ(ierr);
19290f02eecSBarry Smith   PetscFree(idxout);
1933a40ed3dSBarry Smith   PetscFunctionReturn(0);
19490f02eecSBarry Smith }
19590f02eecSBarry Smith 
1965615d1e5SSatish Balay #undef __FUNC__
197d4bb536fSBarry Smith #define __FUNC__ "ISLocalToGlobalMappingApply"
198d4bb536fSBarry Smith /*@C
1993acfe500SLois Curfman McInnes    ISLocalToGlobalMappingApply - Takes a list of integers in a local numbering
2003acfe500SLois Curfman McInnes    and converts them to the global numbering.
20190f02eecSBarry Smith 
202b9cd556bSLois Curfman McInnes    Not collective
203b9cd556bSLois Curfman McInnes 
204bb25748dSBarry Smith    Input Parameters:
205b9cd556bSLois Curfman McInnes +  mapping - the local to global mapping context
206bb25748dSBarry Smith .  N - number of integers
207b9cd556bSLois Curfman McInnes -  in - input indices in local numbering
208bb25748dSBarry Smith 
209bb25748dSBarry Smith    Output Parameter:
210bb25748dSBarry Smith .  out - indices in global numbering
211bb25748dSBarry Smith 
212b9cd556bSLois Curfman McInnes    Notes:
213b9cd556bSLois Curfman McInnes    The in and out array parameters may be identical.
214d4bb536fSBarry Smith 
215*a997ad1aSLois Curfman McInnes    Level: advanced
216*a997ad1aSLois Curfman McInnes 
217bb25748dSBarry Smith .seealso: ISLocalToGlobalMappingCreate(),ISLocalToGlobalMappingDestroy(),
2180752156aSBarry Smith           ISLocalToGlobalMappingApplyIS(),AOCreateBasic(),AOApplicationToPetsc(),
219d4bb536fSBarry Smith           AOPetscToApplication(), ISGlobalToLocalMappingApply()
220bb25748dSBarry Smith 
2213acfe500SLois Curfman McInnes .keywords: local-to-global, mapping, apply
222d4bb536fSBarry Smith 
223d4bb536fSBarry Smith @*/
224987e4450SSatish Balay int ISLocalToGlobalMappingApply(ISLocalToGlobalMapping mapping,int N,const int in[],int out[])
225d4bb536fSBarry Smith {
226d4bb536fSBarry Smith   int i,*idx = mapping->indices,Nmax = mapping->n;
2273a40ed3dSBarry Smith 
2283a40ed3dSBarry Smith   PetscFunctionBegin;
229d4bb536fSBarry Smith   for ( i=0; i<N; i++ ) {
230d4bb536fSBarry Smith     if (in[i] < 0) {out[i] = in[i]; continue;}
231596552b5SBarry Smith     if (in[i] >= Nmax) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,1,"Local index %d too large %d (max)",in[i],Nmax);
232d4bb536fSBarry Smith     out[i] = idx[in[i]];
233d4bb536fSBarry Smith   }
2343a40ed3dSBarry Smith   PetscFunctionReturn(0);
235d4bb536fSBarry Smith }
236d4bb536fSBarry Smith 
237d4bb536fSBarry Smith /* -----------------------------------------------------------------------------------------*/
238d4bb536fSBarry Smith 
239d4bb536fSBarry Smith #undef __FUNC__
240d4bb536fSBarry Smith #define __FUNC__ "ISGlobalToLocalMappingSetUp_Private"
241d4bb536fSBarry Smith /*
242d4bb536fSBarry Smith     Creates the global fields in the ISLocalToGlobalMapping structure
243d4bb536fSBarry Smith */
244d4bb536fSBarry Smith static int ISGlobalToLocalMappingSetUp_Private(ISLocalToGlobalMapping mapping)
245d4bb536fSBarry Smith {
246d4bb536fSBarry Smith   int i,*idx = mapping->indices,n = mapping->n,end,start,*globals;
247d4bb536fSBarry Smith 
2483a40ed3dSBarry Smith   PetscFunctionBegin;
249d4bb536fSBarry Smith   end   = 0;
250d4bb536fSBarry Smith   start = 100000000;
251d4bb536fSBarry Smith 
252d4bb536fSBarry Smith   for ( i=0; i<n; i++ ) {
253d4bb536fSBarry Smith     if (idx[i] < 0) continue;
254d4bb536fSBarry Smith     if (idx[i] < start) start = idx[i];
255d4bb536fSBarry Smith     if (idx[i] > end)   end   = idx[i];
256d4bb536fSBarry Smith   }
257d4bb536fSBarry Smith   if (start > end) {start = 0; end = -1;}
258d4bb536fSBarry Smith   mapping->globalstart = start;
259d4bb536fSBarry Smith   mapping->globalend   = end;
260d4bb536fSBarry Smith 
261d4bb536fSBarry Smith   globals = mapping->globals = (int *) PetscMalloc((end-start+2)*sizeof(int));CHKPTRQ(mapping->globals);
262d4bb536fSBarry Smith   for ( i=0; i<end-start+1; i++ ) {
263d4bb536fSBarry Smith     globals[i] = -1;
264d4bb536fSBarry Smith   }
265d4bb536fSBarry Smith   for ( i=0; i<n; i++ ) {
266d4bb536fSBarry Smith     if (idx[i] < 0) continue;
267d4bb536fSBarry Smith     globals[idx[i] - start] = i;
268d4bb536fSBarry Smith   }
269d4bb536fSBarry Smith 
270d4bb536fSBarry Smith   PLogObjectMemory(mapping,(end-start+1)*sizeof(int));
2713a40ed3dSBarry Smith   PetscFunctionReturn(0);
272d4bb536fSBarry Smith }
273d4bb536fSBarry Smith 
274d4bb536fSBarry Smith #undef __FUNC__
275d4bb536fSBarry Smith #define __FUNC__ "ISGlobalToLocalMappingApply"
276d4bb536fSBarry Smith /*@
277*a997ad1aSLois Curfman McInnes     ISGlobalToLocalMappingApply - Provides the local numbering for a list of integers
278*a997ad1aSLois Curfman McInnes     specified with a global numbering.
279d4bb536fSBarry Smith 
280b9cd556bSLois Curfman McInnes     Not collective
281b9cd556bSLois Curfman McInnes 
282d4bb536fSBarry Smith     Input Parameters:
283b9cd556bSLois Curfman McInnes +   mapping - mapping between local and global numbering
284d4bb536fSBarry Smith .   type - IS_GTOLM_MASK - replaces global indices with no local value with -1
285d4bb536fSBarry Smith            IS_GTOLM_DROP - drops the indices with no local value from the output list
286d4bb536fSBarry Smith .   n - number of global indices to map
287b9cd556bSLois Curfman McInnes -   idx - global indices to map
288d4bb536fSBarry Smith 
289d4bb536fSBarry Smith     Output Parameters:
290b9cd556bSLois Curfman McInnes +   nout - number of indices in output array (if type == IS_GTOLM_MASK then nout = n)
291b9cd556bSLois Curfman McInnes -   idxout - local index of each global index, one must pass in an array long enough
292e182c471SBarry Smith              to hold all the indices. You can call ISGlobalToLocalMappingApply() with
293e182c471SBarry Smith              idxout == PETSC_NULL to determine the required length (returned in nout)
294e182c471SBarry Smith              and then allocate the required space and call ISGlobalToLocalMappingApply()
295e182c471SBarry Smith              a second time to set the values.
296d4bb536fSBarry Smith 
297b9cd556bSLois Curfman McInnes     Notes:
298b9cd556bSLois Curfman McInnes     Either nout or idxout may be PETSC_NULL. idx and idxout may be identical.
299d4bb536fSBarry Smith 
300*a997ad1aSLois Curfman McInnes     Level: advanced
301*a997ad1aSLois Curfman McInnes 
302d4bb536fSBarry Smith .keywords: IS, global-to-local mapping, apply
303d4bb536fSBarry Smith 
304d4bb536fSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),
305d4bb536fSBarry Smith           ISLocalToGlobalMappingDestroy()
306d4bb536fSBarry Smith @*/
307d4bb536fSBarry Smith int ISGlobalToLocalMappingApply(ISLocalToGlobalMapping mapping, ISGlobalToLocalMappingType type,
308987e4450SSatish Balay                                   int n, const int idx[],int *nout,int idxout[])
309d4bb536fSBarry Smith {
310d4bb536fSBarry Smith   int i,ierr, *globals,nf = 0,tmp,start,end;
311d4bb536fSBarry Smith 
3123a40ed3dSBarry Smith   PetscFunctionBegin;
313d4bb536fSBarry Smith   if (!mapping->globals) {
314d4bb536fSBarry Smith     ierr = ISGlobalToLocalMappingSetUp_Private(mapping); CHKERRQ(ierr);
315d4bb536fSBarry Smith   }
316d4bb536fSBarry Smith   globals = mapping->globals;
317d4bb536fSBarry Smith   start   = mapping->globalstart;
318d4bb536fSBarry Smith   end     = mapping->globalend;
319d4bb536fSBarry Smith 
320d4bb536fSBarry Smith   if (type == IS_GTOLM_MASK) {
321d4bb536fSBarry Smith     if (idxout) {
322d4bb536fSBarry Smith       for ( i=0; i<n; i++ ) {
323d4bb536fSBarry Smith         if (idx[i] < 0) idxout[i] = idx[i];
324d4bb536fSBarry Smith         else if (idx[i] < start) idxout[i] = -1;
325d4bb536fSBarry Smith         else if (idx[i] > end)   idxout[i] = -1;
326d4bb536fSBarry Smith         else                     idxout[i] = globals[idx[i] - start];
327d4bb536fSBarry Smith       }
328d4bb536fSBarry Smith     }
329d4bb536fSBarry Smith     if (nout) *nout = n;
330d4bb536fSBarry Smith   } else {
331d4bb536fSBarry Smith     if (idxout) {
332d4bb536fSBarry Smith       for ( i=0; i<n; i++ ) {
333d4bb536fSBarry Smith         if (idx[i] < 0) continue;
334d4bb536fSBarry Smith         if (idx[i] < start) continue;
335d4bb536fSBarry Smith         if (idx[i] > end) continue;
336d4bb536fSBarry Smith         tmp = globals[idx[i] - start];
337d4bb536fSBarry Smith         if (tmp < 0) continue;
338d4bb536fSBarry Smith         idxout[nf++] = tmp;
339d4bb536fSBarry Smith       }
340d4bb536fSBarry Smith     } else {
341d4bb536fSBarry Smith       for ( i=0; i<n; i++ ) {
342d4bb536fSBarry Smith         if (idx[i] < 0) continue;
343d4bb536fSBarry Smith         if (idx[i] < start) continue;
344d4bb536fSBarry Smith         if (idx[i] > end) continue;
345d4bb536fSBarry Smith         tmp = globals[idx[i] - start];
346d4bb536fSBarry Smith         if (tmp < 0) continue;
347d4bb536fSBarry Smith         nf++;
348d4bb536fSBarry Smith       }
349d4bb536fSBarry Smith     }
350d4bb536fSBarry Smith     if (nout) *nout = nf;
351d4bb536fSBarry Smith   }
352d4bb536fSBarry Smith 
3533a40ed3dSBarry Smith   PetscFunctionReturn(0);
354d4bb536fSBarry Smith }
35590f02eecSBarry Smith 
356