xref: /petsc/src/vec/is/utils/isltog.c (revision 2bdab257d34f9d407eac123c854c8b30dbb6719c)
1d4bb536fSBarry Smith 
2a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
3*2bdab257SBarry Smith static char vcid[] = "$Id: isltog.c,v 1.15 1997/09/26 02:17:37 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*2bdab257SBarry Smith #define __FUNC__ "ISLocalToGlobalMappingCreateIS"
11*2bdab257SBarry Smith /*@C
12*2bdab257SBarry Smith     ISLocalToGlobalMappingCreateIS - Creates a mapping between a local (0 to n)
13*2bdab257SBarry Smith     ordering and a global parallel ordering.
14*2bdab257SBarry Smith 
15*2bdab257SBarry Smith     Input Parameters:
16*2bdab257SBarry Smith .   is - index set containing the global numbers for each local
17*2bdab257SBarry Smith 
18*2bdab257SBarry Smith     Output Parameters:
19*2bdab257SBarry Smith .   mapping - new mapping data structure
20*2bdab257SBarry Smith 
21*2bdab257SBarry Smith .keywords: IS, local-to-global mapping, create
22*2bdab257SBarry Smith 
23*2bdab257SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
24*2bdab257SBarry Smith @*/
25*2bdab257SBarry Smith int ISLocalToGlobalMappingCreateIS(IS is,ISLocalToGlobalMapping *mapping)
26*2bdab257SBarry Smith {
27*2bdab257SBarry Smith   int      n,*indices,ierr;
28*2bdab257SBarry Smith   MPI_Comm comm;
29*2bdab257SBarry Smith   PetscValidHeaderSpecific(is,IS_COOKIE);
30*2bdab257SBarry Smith 
31*2bdab257SBarry Smith   ierr = PetscObjectGetComm((PetscObject)is,&comm); CHKERRQ(ierr);
32*2bdab257SBarry Smith   ierr = ISGetSize(is,&n);CHKERRQ(ierr);
33*2bdab257SBarry Smith   ierr = ISGetIndices(is,&indices);CHKERRQ(ierr);
34*2bdab257SBarry Smith   ierr = ISLocalToGlobalMappingCreate(comm,n,indices,mapping);CHKERRQ(ierr);
35*2bdab257SBarry Smith   ierr = ISRestoreIndices(is,&indices);CHKERRQ(ierr);
36*2bdab257SBarry Smith 
37*2bdab257SBarry Smith   return 0;
38*2bdab257SBarry Smith }
39*2bdab257SBarry Smith #undef __FUNC__
40d4bb536fSBarry Smith #define __FUNC__ "ISLocalToGlobalMappingCreate"
41dd7157adSSatish Balay /*@C
4290f02eecSBarry Smith     ISLocalToGlobalMappingCreate - Creates a mapping between a local (0 to n)
4390f02eecSBarry Smith     ordering and a global parallel ordering.
442362add9SBarry Smith 
452362add9SBarry Smith     Input Parameters:
46d4bb536fSBarry Smith .   comm - MPI communicator of size 1.
4790f02eecSBarry Smith .   n - the number of local elements
4890f02eecSBarry Smith .   indices - the global index for each local element
492362add9SBarry Smith 
502362add9SBarry Smith     Output Parameters:
5190f02eecSBarry Smith .   mapping - new mapping data structure
522362add9SBarry Smith 
533acfe500SLois Curfman McInnes .keywords: IS, local-to-global mapping, create
542362add9SBarry Smith 
55*2bdab257SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS()
562362add9SBarry Smith @*/
57d4bb536fSBarry Smith int ISLocalToGlobalMappingCreate(MPI_Comm cm,int n, int *indices,ISLocalToGlobalMapping *mapping)
582362add9SBarry Smith {
5990f02eecSBarry Smith   PetscValidIntPointer(indices);
6090f02eecSBarry Smith   PetscValidPointer(mapping);
612362add9SBarry Smith 
62d4bb536fSBarry Smith   PetscHeaderCreate(*mapping,_p_ISLocalToGlobalMapping,IS_LTOGM_COOKIE,0,cm,ISLocalToGlobalMappingDestroy,0);
63d4bb536fSBarry Smith   PLogObjectCreate(*mapping);
64d4bb536fSBarry Smith   PLogObjectMemory(*mapping,sizeof(struct _p_ISLocalToGlobalMapping)+n*sizeof(int));
65d4bb536fSBarry Smith 
66d4bb536fSBarry Smith   (*mapping)->n       = n;
6790f02eecSBarry Smith   (*mapping)->indices = (int *) PetscMalloc((n+1)*sizeof(int));CHKPTRQ((*mapping)->indices);
6890f02eecSBarry Smith   PetscMemcpy((*mapping)->indices,indices,n*sizeof(int));
69d4bb536fSBarry Smith 
70d4bb536fSBarry Smith   /*
71d4bb536fSBarry Smith       Do not create the global to local mapping. This is only created if
72d4bb536fSBarry Smith      ISGlobalToLocalMapping() is called
73d4bb536fSBarry Smith   */
74d4bb536fSBarry Smith   (*mapping)->globals = 0;
752362add9SBarry Smith   return 0;
762362add9SBarry Smith }
772362add9SBarry Smith 
785615d1e5SSatish Balay #undef __FUNC__
79d4bb536fSBarry Smith #define __FUNC__ "ISLocalToGlobalMappingDestroy"
8090f02eecSBarry Smith /*@
8190f02eecSBarry Smith    ISLocalToGlobalMappingDestroy - Destroys a mapping between a local (0 to n)
8290f02eecSBarry Smith    ordering and a global parallel ordering.
8390f02eecSBarry Smith 
8490f02eecSBarry Smith    Input Parameters:
8590f02eecSBarry Smith .  mapping - mapping data structure
8690f02eecSBarry Smith 
873acfe500SLois Curfman McInnes .keywords: IS, local-to-global mapping, destroy
8890f02eecSBarry Smith 
893acfe500SLois Curfman McInnes .seealso: ISLocalToGlobalMappingCreate()
9090f02eecSBarry Smith @*/
9190f02eecSBarry Smith int ISLocalToGlobalMappingDestroy(ISLocalToGlobalMapping mapping)
9290f02eecSBarry Smith {
9390f02eecSBarry Smith   PetscValidPointer(mapping);
94d4bb536fSBarry Smith   if (--mapping->refct > 0) return 0;
9590f02eecSBarry Smith 
9690f02eecSBarry Smith   PetscFree(mapping->indices);
97d4bb536fSBarry Smith   if (mapping->globals) PetscFree(mapping->globals);
98d4bb536fSBarry Smith   PLogObjectDestroy(mapping);
99d4bb536fSBarry Smith   PetscHeaderDestroy(mapping);
10090f02eecSBarry Smith   return 0;
10190f02eecSBarry Smith }
10290f02eecSBarry Smith 
1035615d1e5SSatish Balay #undef __FUNC__
104d4bb536fSBarry Smith #define __FUNC__ "ISLocalToGlobalMappingApplyIS"
10590f02eecSBarry Smith /*@
1063acfe500SLois Curfman McInnes     ISLocalToGlobalMappingApplyIS - Creates from an IS in the local numbering
1073acfe500SLois Curfman McInnes     a new index set using the global numbering defined in an ISLocalToGlobalMapping
1083acfe500SLois Curfman McInnes     context.
10990f02eecSBarry Smith 
11090f02eecSBarry Smith     Input Parameters:
111d4bb536fSBarry Smith .   mapping - mapping between local and global numbering
11290f02eecSBarry Smith .   is - index set in local numbering
11390f02eecSBarry Smith 
11490f02eecSBarry Smith     Output Parameters:
11590f02eecSBarry Smith .   newis - index set in global numbering
11690f02eecSBarry Smith 
1173acfe500SLois Curfman McInnes .keywords: IS, local-to-global mapping, apply
1183acfe500SLois Curfman McInnes 
11990f02eecSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),
120d4bb536fSBarry Smith           ISLocalToGlobalMappingDestroy(), ISGlobalToLocalMappingApply()
12190f02eecSBarry Smith @*/
12290f02eecSBarry Smith int ISLocalToGlobalMappingApplyIS(ISLocalToGlobalMapping mapping, IS is, IS *newis)
12390f02eecSBarry Smith {
12490f02eecSBarry Smith   int ierr,n,i,*idxin,*idxmap,*idxout;
12590f02eecSBarry Smith   PetscValidPointer(mapping);
12690f02eecSBarry Smith   PetscValidHeaderSpecific(is,IS_COOKIE);
12790f02eecSBarry Smith   PetscValidPointer(newis);
12890f02eecSBarry Smith 
12990f02eecSBarry Smith   ierr   = ISGetSize(is,&n); CHKERRQ(ierr);
13090f02eecSBarry Smith   ierr   = ISGetIndices(is,&idxin); CHKERRQ(ierr);
13190f02eecSBarry Smith   idxmap = mapping->indices;
13290f02eecSBarry Smith 
13390f02eecSBarry Smith   idxout = (int *) PetscMalloc((n+1)*sizeof(int));CHKPTRQ(idxout);
13490f02eecSBarry Smith   for ( i=0; i<n; i++ ) {
13590f02eecSBarry Smith     idxout[i] = idxmap[idxin[i]];
13690f02eecSBarry Smith   }
137029af93fSBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,n,idxout,newis); CHKERRQ(ierr);
13890f02eecSBarry Smith   PetscFree(idxout);
13990f02eecSBarry Smith   return 0;
14090f02eecSBarry Smith }
14190f02eecSBarry Smith 
1425615d1e5SSatish Balay #undef __FUNC__
143d4bb536fSBarry Smith #define __FUNC__ "ISLocalToGlobalMappingApply"
144d4bb536fSBarry Smith /*@C
1453acfe500SLois Curfman McInnes    ISLocalToGlobalMappingApply - Takes a list of integers in a local numbering
1463acfe500SLois Curfman McInnes    and converts them to the global numbering.
14790f02eecSBarry Smith 
148bb25748dSBarry Smith    Input Parameters:
149bb25748dSBarry Smith .  mapping - the local to global mapping context
150bb25748dSBarry Smith .  N - number of integers
151bb25748dSBarry Smith .  in - input indices in local numbering
152bb25748dSBarry Smith 
153bb25748dSBarry Smith    Output Parameter:
154bb25748dSBarry Smith .  out - indices in global numbering
155bb25748dSBarry Smith 
156d4bb536fSBarry Smith    Notes: The in and out array may be identical
157d4bb536fSBarry Smith 
158bb25748dSBarry Smith .seealso: ISLocalToGlobalMappingCreate(),ISLocalToGlobalMappingDestroy(),
1590752156aSBarry Smith           ISLocalToGlobalMappingApplyIS(),AOCreateBasic(),AOApplicationToPetsc(),
160d4bb536fSBarry Smith           AOPetscToApplication(), ISGlobalToLocalMappingApply()
161bb25748dSBarry Smith 
1623acfe500SLois Curfman McInnes .keywords: local-to-global, mapping, apply
163d4bb536fSBarry Smith 
164d4bb536fSBarry Smith @*/
165d4bb536fSBarry Smith int ISLocalToGlobalMappingApply(ISLocalToGlobalMapping mapping,int N,int *in,int *out)
166d4bb536fSBarry Smith {
167d4bb536fSBarry Smith   int i,*idx = mapping->indices,Nmax = mapping->n;
168d4bb536fSBarry Smith   for ( i=0; i<N; i++ ) {
169d4bb536fSBarry Smith     if (in[i] < 0) {out[i] = in[i]; continue;}
170d4bb536fSBarry Smith     if (in[i] >= Nmax) SETERRQ(1,1,"Local index too large");
171d4bb536fSBarry Smith     out[i] = idx[in[i]];
172d4bb536fSBarry Smith   }
173d4bb536fSBarry Smith   return 0;
174d4bb536fSBarry Smith }
175d4bb536fSBarry Smith 
176d4bb536fSBarry Smith /* -----------------------------------------------------------------------------------------*/
177d4bb536fSBarry Smith 
178d4bb536fSBarry Smith #undef __FUNC__
179d4bb536fSBarry Smith #define __FUNC__ "ISGlobalToLocalMappingSetUp_Private"
180d4bb536fSBarry Smith /*
181d4bb536fSBarry Smith     Creates the global fields in the ISLocalToGlobalMapping structure
182d4bb536fSBarry Smith */
183d4bb536fSBarry Smith static int ISGlobalToLocalMappingSetUp_Private(ISLocalToGlobalMapping mapping)
184d4bb536fSBarry Smith {
185d4bb536fSBarry Smith   int i,*idx = mapping->indices,n = mapping->n,end,start,*globals;
186d4bb536fSBarry Smith 
187d4bb536fSBarry Smith   end   = 0;
188d4bb536fSBarry Smith   start = 100000000;
189d4bb536fSBarry Smith 
190d4bb536fSBarry Smith   for ( i=0; i<n; i++ ) {
191d4bb536fSBarry Smith     if (idx[i] < 0) continue;
192d4bb536fSBarry Smith     if (idx[i] < start) start = idx[i];
193d4bb536fSBarry Smith     if (idx[i] > end)   end   = idx[i];
194d4bb536fSBarry Smith   }
195d4bb536fSBarry Smith   if (start > end) {start = 0; end = -1;}
196d4bb536fSBarry Smith   mapping->globalstart = start;
197d4bb536fSBarry Smith   mapping->globalend   = end;
198d4bb536fSBarry Smith 
199d4bb536fSBarry Smith   globals = mapping->globals = (int *) PetscMalloc((end-start+2)*sizeof(int));CHKPTRQ(mapping->globals);
200d4bb536fSBarry Smith   for ( i=0; i<end-start+1; i++ ) {
201d4bb536fSBarry Smith     globals[i] = -1;
202d4bb536fSBarry Smith   }
203d4bb536fSBarry Smith   for ( i=0; i<n; i++ ) {
204d4bb536fSBarry Smith     if (idx[i] < 0) continue;
205d4bb536fSBarry Smith     globals[idx[i] - start] = i;
206d4bb536fSBarry Smith   }
207d4bb536fSBarry Smith 
208d4bb536fSBarry Smith   PLogObjectMemory(mapping,(end-start+1)*sizeof(int));
209d4bb536fSBarry Smith   return 0;
210d4bb536fSBarry Smith }
211d4bb536fSBarry Smith 
212d4bb536fSBarry Smith #undef __FUNC__
213d4bb536fSBarry Smith #define __FUNC__ "ISGlobalToLocalMappingApply"
214d4bb536fSBarry Smith /*@
215d4bb536fSBarry Smith     ISGlobalToLocalMappingApply - Takes a list of integers in global numbering
216d4bb536fSBarry Smith       and returns the local numbering.
217d4bb536fSBarry Smith 
218d4bb536fSBarry Smith     Input Parameters:
219d4bb536fSBarry Smith .   mapping - mapping between local and global numbering
220d4bb536fSBarry Smith .   type - IS_GTOLM_MASK - replaces global indices with no local value with -1
221d4bb536fSBarry Smith            IS_GTOLM_DROP - drops the indices with no local value from the output list
222d4bb536fSBarry Smith .   n - number of global indices to map
223d4bb536fSBarry Smith .   idx - global indices to map
224d4bb536fSBarry Smith 
225d4bb536fSBarry Smith     Output Parameters:
226d4bb536fSBarry Smith .   nout - number of indices in output array (if type == IS_GTOLM_MASK then nout = n)
227e182c471SBarry Smith .   idxout - local index of each global index, one must pass in an array long enough
228e182c471SBarry Smith              to hold all the indices. You can call ISGlobalToLocalMappingApply() with
229e182c471SBarry Smith              idxout == PETSC_NULL to determine the required length (returned in nout)
230e182c471SBarry Smith              and then allocate the required space and call ISGlobalToLocalMappingApply()
231e182c471SBarry Smith              a second time to set the values.
232d4bb536fSBarry Smith 
233d4bb536fSBarry Smith     Notes: Either nout or idxout may be PETSC_NULL. idx and idxout may be identical.
234d4bb536fSBarry Smith 
235d4bb536fSBarry Smith .keywords: IS, global-to-local mapping, apply
236d4bb536fSBarry Smith 
237d4bb536fSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),
238d4bb536fSBarry Smith           ISLocalToGlobalMappingDestroy()
239d4bb536fSBarry Smith @*/
240d4bb536fSBarry Smith int ISGlobalToLocalMappingApply(ISLocalToGlobalMapping mapping, ISGlobalToLocalMappingType type,
241d4bb536fSBarry Smith                                   int n, int *idx,int *nout,int *idxout)
242d4bb536fSBarry Smith {
243d4bb536fSBarry Smith   int i,ierr, *globals,nf = 0,tmp,start,end;
244d4bb536fSBarry Smith 
245d4bb536fSBarry Smith   if (!mapping->globals) {
246d4bb536fSBarry Smith     ierr = ISGlobalToLocalMappingSetUp_Private(mapping); CHKERRQ(ierr);
247d4bb536fSBarry Smith   }
248d4bb536fSBarry Smith   globals = mapping->globals;
249d4bb536fSBarry Smith   start   = mapping->globalstart;
250d4bb536fSBarry Smith   end     = mapping->globalend;
251d4bb536fSBarry Smith 
252d4bb536fSBarry Smith   if (type == IS_GTOLM_MASK) {
253d4bb536fSBarry Smith     if (idxout) {
254d4bb536fSBarry Smith       for ( i=0; i<n; i++ ) {
255d4bb536fSBarry Smith         if (idx[i] < 0) idxout[i] = idx[i];
256d4bb536fSBarry Smith         else if (idx[i] < start) idxout[i] = -1;
257d4bb536fSBarry Smith         else if (idx[i] > end)   idxout[i] = -1;
258d4bb536fSBarry Smith         else                     idxout[i] = globals[idx[i] - start];
259d4bb536fSBarry Smith       }
260d4bb536fSBarry Smith     }
261d4bb536fSBarry Smith     if (nout) *nout = n;
262d4bb536fSBarry Smith   } else {
263d4bb536fSBarry Smith     if (idxout) {
264d4bb536fSBarry Smith       for ( i=0; i<n; i++ ) {
265d4bb536fSBarry Smith         if (idx[i] < 0) continue;
266d4bb536fSBarry Smith         if (idx[i] < start) continue;
267d4bb536fSBarry Smith         if (idx[i] > end) continue;
268d4bb536fSBarry Smith         tmp = globals[idx[i] - start];
269d4bb536fSBarry Smith         if (tmp < 0) continue;
270d4bb536fSBarry Smith         idxout[nf++] = tmp;
271d4bb536fSBarry Smith       }
272d4bb536fSBarry Smith     } else {
273d4bb536fSBarry Smith       for ( i=0; i<n; i++ ) {
274d4bb536fSBarry Smith         if (idx[i] < 0) continue;
275d4bb536fSBarry Smith         if (idx[i] < start) continue;
276d4bb536fSBarry Smith         if (idx[i] > end) continue;
277d4bb536fSBarry Smith         tmp = globals[idx[i] - start];
278d4bb536fSBarry Smith         if (tmp < 0) continue;
279d4bb536fSBarry Smith         nf++;
280d4bb536fSBarry Smith       }
281d4bb536fSBarry Smith     }
282d4bb536fSBarry Smith     if (nout) *nout = nf;
283d4bb536fSBarry Smith   }
284d4bb536fSBarry Smith 
285d4bb536fSBarry Smith   return 0;
286d4bb536fSBarry Smith }
28790f02eecSBarry Smith 
288