xref: /petsc/src/vec/is/utils/isltog.c (revision 3a40ed3dce77c081171d005ae1a6ff4bb9d13b6f)
1d4bb536fSBarry Smith 
2a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
3*3a40ed3dSBarry Smith static char vcid[] = "$Id: isltog.c,v 1.16 1997/10/10 04:01:58 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__
102bdab257SBarry Smith #define __FUNC__ "ISLocalToGlobalMappingCreateIS"
112bdab257SBarry Smith /*@C
122bdab257SBarry Smith     ISLocalToGlobalMappingCreateIS - Creates a mapping between a local (0 to n)
132bdab257SBarry Smith     ordering and a global parallel ordering.
142bdab257SBarry Smith 
152bdab257SBarry Smith     Input Parameters:
162bdab257SBarry Smith .   is - index set containing the global numbers for each local
172bdab257SBarry Smith 
182bdab257SBarry Smith     Output Parameters:
192bdab257SBarry Smith .   mapping - new mapping data structure
202bdab257SBarry Smith 
212bdab257SBarry Smith .keywords: IS, local-to-global mapping, create
222bdab257SBarry Smith 
232bdab257SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
242bdab257SBarry Smith @*/
252bdab257SBarry Smith int ISLocalToGlobalMappingCreateIS(IS is,ISLocalToGlobalMapping *mapping)
262bdab257SBarry Smith {
272bdab257SBarry Smith   int      n,*indices,ierr;
282bdab257SBarry Smith   MPI_Comm comm;
29*3a40ed3dSBarry Smith 
30*3a40ed3dSBarry Smith   PetscFunctionBegin;
312bdab257SBarry Smith   PetscValidHeaderSpecific(is,IS_COOKIE);
322bdab257SBarry Smith 
332bdab257SBarry Smith   ierr = PetscObjectGetComm((PetscObject)is,&comm); CHKERRQ(ierr);
342bdab257SBarry Smith   ierr = ISGetSize(is,&n);CHKERRQ(ierr);
352bdab257SBarry Smith   ierr = ISGetIndices(is,&indices);CHKERRQ(ierr);
362bdab257SBarry Smith   ierr = ISLocalToGlobalMappingCreate(comm,n,indices,mapping);CHKERRQ(ierr);
372bdab257SBarry Smith   ierr = ISRestoreIndices(is,&indices);CHKERRQ(ierr);
382bdab257SBarry Smith 
39*3a40ed3dSBarry Smith   PetscFunctionReturn(0);
402bdab257SBarry Smith }
412bdab257SBarry Smith #undef __FUNC__
42d4bb536fSBarry Smith #define __FUNC__ "ISLocalToGlobalMappingCreate"
43dd7157adSSatish Balay /*@C
4490f02eecSBarry Smith     ISLocalToGlobalMappingCreate - Creates a mapping between a local (0 to n)
4590f02eecSBarry Smith     ordering and a global parallel ordering.
462362add9SBarry Smith 
472362add9SBarry Smith     Input Parameters:
48d4bb536fSBarry Smith .   comm - MPI communicator of size 1.
4990f02eecSBarry Smith .   n - the number of local elements
5090f02eecSBarry Smith .   indices - the global index for each local element
512362add9SBarry Smith 
522362add9SBarry Smith     Output Parameters:
5390f02eecSBarry Smith .   mapping - new mapping data structure
542362add9SBarry Smith 
553acfe500SLois Curfman McInnes .keywords: IS, local-to-global mapping, create
562362add9SBarry Smith 
572bdab257SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS()
582362add9SBarry Smith @*/
59d4bb536fSBarry Smith int ISLocalToGlobalMappingCreate(MPI_Comm cm,int n, int *indices,ISLocalToGlobalMapping *mapping)
602362add9SBarry Smith {
61*3a40ed3dSBarry Smith   PetscFunctionBegin;
6290f02eecSBarry Smith   PetscValidIntPointer(indices);
6390f02eecSBarry Smith   PetscValidPointer(mapping);
642362add9SBarry Smith 
65d4bb536fSBarry Smith   PetscHeaderCreate(*mapping,_p_ISLocalToGlobalMapping,IS_LTOGM_COOKIE,0,cm,ISLocalToGlobalMappingDestroy,0);
66d4bb536fSBarry Smith   PLogObjectCreate(*mapping);
67d4bb536fSBarry Smith   PLogObjectMemory(*mapping,sizeof(struct _p_ISLocalToGlobalMapping)+n*sizeof(int));
68d4bb536fSBarry Smith 
69d4bb536fSBarry Smith   (*mapping)->n       = n;
7090f02eecSBarry Smith   (*mapping)->indices = (int *) PetscMalloc((n+1)*sizeof(int));CHKPTRQ((*mapping)->indices);
7190f02eecSBarry Smith   PetscMemcpy((*mapping)->indices,indices,n*sizeof(int));
72d4bb536fSBarry Smith 
73d4bb536fSBarry Smith   /*
74d4bb536fSBarry Smith       Do not create the global to local mapping. This is only created if
75d4bb536fSBarry Smith      ISGlobalToLocalMapping() is called
76d4bb536fSBarry Smith   */
77d4bb536fSBarry Smith   (*mapping)->globals = 0;
78*3a40ed3dSBarry Smith   PetscFunctionReturn(0);
792362add9SBarry Smith }
802362add9SBarry Smith 
815615d1e5SSatish Balay #undef __FUNC__
82d4bb536fSBarry Smith #define __FUNC__ "ISLocalToGlobalMappingDestroy"
8390f02eecSBarry Smith /*@
8490f02eecSBarry Smith    ISLocalToGlobalMappingDestroy - Destroys a mapping between a local (0 to n)
8590f02eecSBarry Smith    ordering and a global parallel ordering.
8690f02eecSBarry Smith 
8790f02eecSBarry Smith    Input Parameters:
8890f02eecSBarry Smith .  mapping - mapping data structure
8990f02eecSBarry Smith 
903acfe500SLois Curfman McInnes .keywords: IS, local-to-global mapping, destroy
9190f02eecSBarry Smith 
923acfe500SLois Curfman McInnes .seealso: ISLocalToGlobalMappingCreate()
9390f02eecSBarry Smith @*/
9490f02eecSBarry Smith int ISLocalToGlobalMappingDestroy(ISLocalToGlobalMapping mapping)
9590f02eecSBarry Smith {
96*3a40ed3dSBarry Smith   PetscFunctionBegin;
9790f02eecSBarry Smith   PetscValidPointer(mapping);
98*3a40ed3dSBarry Smith   if (--mapping->refct > 0) PetscFunctionReturn(0);
9990f02eecSBarry Smith 
10090f02eecSBarry Smith   PetscFree(mapping->indices);
101d4bb536fSBarry Smith   if (mapping->globals) PetscFree(mapping->globals);
102d4bb536fSBarry Smith   PLogObjectDestroy(mapping);
103d4bb536fSBarry Smith   PetscHeaderDestroy(mapping);
104*3a40ed3dSBarry Smith   PetscFunctionReturn(0);
10590f02eecSBarry Smith }
10690f02eecSBarry Smith 
1075615d1e5SSatish Balay #undef __FUNC__
108d4bb536fSBarry Smith #define __FUNC__ "ISLocalToGlobalMappingApplyIS"
10990f02eecSBarry Smith /*@
1103acfe500SLois Curfman McInnes     ISLocalToGlobalMappingApplyIS - Creates from an IS in the local numbering
1113acfe500SLois Curfman McInnes     a new index set using the global numbering defined in an ISLocalToGlobalMapping
1123acfe500SLois Curfman McInnes     context.
11390f02eecSBarry Smith 
11490f02eecSBarry Smith     Input Parameters:
115d4bb536fSBarry Smith .   mapping - mapping between local and global numbering
11690f02eecSBarry Smith .   is - index set in local numbering
11790f02eecSBarry Smith 
11890f02eecSBarry Smith     Output Parameters:
11990f02eecSBarry Smith .   newis - index set in global numbering
12090f02eecSBarry Smith 
1213acfe500SLois Curfman McInnes .keywords: IS, local-to-global mapping, apply
1223acfe500SLois Curfman McInnes 
12390f02eecSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),
124d4bb536fSBarry Smith           ISLocalToGlobalMappingDestroy(), ISGlobalToLocalMappingApply()
12590f02eecSBarry Smith @*/
12690f02eecSBarry Smith int ISLocalToGlobalMappingApplyIS(ISLocalToGlobalMapping mapping, IS is, IS *newis)
12790f02eecSBarry Smith {
12890f02eecSBarry Smith   int ierr,n,i,*idxin,*idxmap,*idxout;
129*3a40ed3dSBarry Smith 
130*3a40ed3dSBarry Smith   PetscFunctionBegin;
13190f02eecSBarry Smith   PetscValidPointer(mapping);
13290f02eecSBarry Smith   PetscValidHeaderSpecific(is,IS_COOKIE);
13390f02eecSBarry Smith   PetscValidPointer(newis);
13490f02eecSBarry Smith 
13590f02eecSBarry Smith   ierr   = ISGetSize(is,&n); CHKERRQ(ierr);
13690f02eecSBarry Smith   ierr   = ISGetIndices(is,&idxin); CHKERRQ(ierr);
13790f02eecSBarry Smith   idxmap = mapping->indices;
13890f02eecSBarry Smith 
13990f02eecSBarry Smith   idxout = (int *) PetscMalloc((n+1)*sizeof(int));CHKPTRQ(idxout);
14090f02eecSBarry Smith   for ( i=0; i<n; i++ ) {
14190f02eecSBarry Smith     idxout[i] = idxmap[idxin[i]];
14290f02eecSBarry Smith   }
143029af93fSBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,n,idxout,newis); CHKERRQ(ierr);
14490f02eecSBarry Smith   PetscFree(idxout);
145*3a40ed3dSBarry Smith   PetscFunctionReturn(0);
14690f02eecSBarry Smith }
14790f02eecSBarry Smith 
1485615d1e5SSatish Balay #undef __FUNC__
149d4bb536fSBarry Smith #define __FUNC__ "ISLocalToGlobalMappingApply"
150d4bb536fSBarry Smith /*@C
1513acfe500SLois Curfman McInnes    ISLocalToGlobalMappingApply - Takes a list of integers in a local numbering
1523acfe500SLois Curfman McInnes    and converts them to the global numbering.
15390f02eecSBarry Smith 
154bb25748dSBarry Smith    Input Parameters:
155bb25748dSBarry Smith .  mapping - the local to global mapping context
156bb25748dSBarry Smith .  N - number of integers
157bb25748dSBarry Smith .  in - input indices in local numbering
158bb25748dSBarry Smith 
159bb25748dSBarry Smith    Output Parameter:
160bb25748dSBarry Smith .  out - indices in global numbering
161bb25748dSBarry Smith 
162d4bb536fSBarry Smith    Notes: The in and out array may be identical
163d4bb536fSBarry Smith 
164bb25748dSBarry Smith .seealso: ISLocalToGlobalMappingCreate(),ISLocalToGlobalMappingDestroy(),
1650752156aSBarry Smith           ISLocalToGlobalMappingApplyIS(),AOCreateBasic(),AOApplicationToPetsc(),
166d4bb536fSBarry Smith           AOPetscToApplication(), ISGlobalToLocalMappingApply()
167bb25748dSBarry Smith 
1683acfe500SLois Curfman McInnes .keywords: local-to-global, mapping, apply
169d4bb536fSBarry Smith 
170d4bb536fSBarry Smith @*/
171d4bb536fSBarry Smith int ISLocalToGlobalMappingApply(ISLocalToGlobalMapping mapping,int N,int *in,int *out)
172d4bb536fSBarry Smith {
173d4bb536fSBarry Smith   int i,*idx = mapping->indices,Nmax = mapping->n;
174*3a40ed3dSBarry Smith 
175*3a40ed3dSBarry Smith   PetscFunctionBegin;
176d4bb536fSBarry Smith   for ( i=0; i<N; i++ ) {
177d4bb536fSBarry Smith     if (in[i] < 0) {out[i] = in[i]; continue;}
178d4bb536fSBarry Smith     if (in[i] >= Nmax) SETERRQ(1,1,"Local index too large");
179d4bb536fSBarry Smith     out[i] = idx[in[i]];
180d4bb536fSBarry Smith   }
181*3a40ed3dSBarry Smith   PetscFunctionReturn(0);
182d4bb536fSBarry Smith }
183d4bb536fSBarry Smith 
184d4bb536fSBarry Smith /* -----------------------------------------------------------------------------------------*/
185d4bb536fSBarry Smith 
186d4bb536fSBarry Smith #undef __FUNC__
187d4bb536fSBarry Smith #define __FUNC__ "ISGlobalToLocalMappingSetUp_Private"
188d4bb536fSBarry Smith /*
189d4bb536fSBarry Smith     Creates the global fields in the ISLocalToGlobalMapping structure
190d4bb536fSBarry Smith */
191d4bb536fSBarry Smith static int ISGlobalToLocalMappingSetUp_Private(ISLocalToGlobalMapping mapping)
192d4bb536fSBarry Smith {
193d4bb536fSBarry Smith   int i,*idx = mapping->indices,n = mapping->n,end,start,*globals;
194d4bb536fSBarry Smith 
195*3a40ed3dSBarry Smith   PetscFunctionBegin;
196d4bb536fSBarry Smith   end   = 0;
197d4bb536fSBarry Smith   start = 100000000;
198d4bb536fSBarry Smith 
199d4bb536fSBarry Smith   for ( i=0; i<n; i++ ) {
200d4bb536fSBarry Smith     if (idx[i] < 0) continue;
201d4bb536fSBarry Smith     if (idx[i] < start) start = idx[i];
202d4bb536fSBarry Smith     if (idx[i] > end)   end   = idx[i];
203d4bb536fSBarry Smith   }
204d4bb536fSBarry Smith   if (start > end) {start = 0; end = -1;}
205d4bb536fSBarry Smith   mapping->globalstart = start;
206d4bb536fSBarry Smith   mapping->globalend   = end;
207d4bb536fSBarry Smith 
208d4bb536fSBarry Smith   globals = mapping->globals = (int *) PetscMalloc((end-start+2)*sizeof(int));CHKPTRQ(mapping->globals);
209d4bb536fSBarry Smith   for ( i=0; i<end-start+1; i++ ) {
210d4bb536fSBarry Smith     globals[i] = -1;
211d4bb536fSBarry Smith   }
212d4bb536fSBarry Smith   for ( i=0; i<n; i++ ) {
213d4bb536fSBarry Smith     if (idx[i] < 0) continue;
214d4bb536fSBarry Smith     globals[idx[i] - start] = i;
215d4bb536fSBarry Smith   }
216d4bb536fSBarry Smith 
217d4bb536fSBarry Smith   PLogObjectMemory(mapping,(end-start+1)*sizeof(int));
218*3a40ed3dSBarry Smith   PetscFunctionReturn(0);
219d4bb536fSBarry Smith }
220d4bb536fSBarry Smith 
221d4bb536fSBarry Smith #undef __FUNC__
222d4bb536fSBarry Smith #define __FUNC__ "ISGlobalToLocalMappingApply"
223d4bb536fSBarry Smith /*@
224d4bb536fSBarry Smith     ISGlobalToLocalMappingApply - Takes a list of integers in global numbering
225d4bb536fSBarry Smith       and returns the local numbering.
226d4bb536fSBarry Smith 
227d4bb536fSBarry Smith     Input Parameters:
228d4bb536fSBarry Smith .   mapping - mapping between local and global numbering
229d4bb536fSBarry Smith .   type - IS_GTOLM_MASK - replaces global indices with no local value with -1
230d4bb536fSBarry Smith            IS_GTOLM_DROP - drops the indices with no local value from the output list
231d4bb536fSBarry Smith .   n - number of global indices to map
232d4bb536fSBarry Smith .   idx - global indices to map
233d4bb536fSBarry Smith 
234d4bb536fSBarry Smith     Output Parameters:
235d4bb536fSBarry Smith .   nout - number of indices in output array (if type == IS_GTOLM_MASK then nout = n)
236e182c471SBarry Smith .   idxout - local index of each global index, one must pass in an array long enough
237e182c471SBarry Smith              to hold all the indices. You can call ISGlobalToLocalMappingApply() with
238e182c471SBarry Smith              idxout == PETSC_NULL to determine the required length (returned in nout)
239e182c471SBarry Smith              and then allocate the required space and call ISGlobalToLocalMappingApply()
240e182c471SBarry Smith              a second time to set the values.
241d4bb536fSBarry Smith 
242d4bb536fSBarry Smith     Notes: Either nout or idxout may be PETSC_NULL. idx and idxout may be identical.
243d4bb536fSBarry Smith 
244d4bb536fSBarry Smith .keywords: IS, global-to-local mapping, apply
245d4bb536fSBarry Smith 
246d4bb536fSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),
247d4bb536fSBarry Smith           ISLocalToGlobalMappingDestroy()
248d4bb536fSBarry Smith @*/
249d4bb536fSBarry Smith int ISGlobalToLocalMappingApply(ISLocalToGlobalMapping mapping, ISGlobalToLocalMappingType type,
250d4bb536fSBarry Smith                                   int n, int *idx,int *nout,int *idxout)
251d4bb536fSBarry Smith {
252d4bb536fSBarry Smith   int i,ierr, *globals,nf = 0,tmp,start,end;
253d4bb536fSBarry Smith 
254*3a40ed3dSBarry Smith   PetscFunctionBegin;
255d4bb536fSBarry Smith   if (!mapping->globals) {
256d4bb536fSBarry Smith     ierr = ISGlobalToLocalMappingSetUp_Private(mapping); CHKERRQ(ierr);
257d4bb536fSBarry Smith   }
258d4bb536fSBarry Smith   globals = mapping->globals;
259d4bb536fSBarry Smith   start   = mapping->globalstart;
260d4bb536fSBarry Smith   end     = mapping->globalend;
261d4bb536fSBarry Smith 
262d4bb536fSBarry Smith   if (type == IS_GTOLM_MASK) {
263d4bb536fSBarry Smith     if (idxout) {
264d4bb536fSBarry Smith       for ( i=0; i<n; i++ ) {
265d4bb536fSBarry Smith         if (idx[i] < 0) idxout[i] = idx[i];
266d4bb536fSBarry Smith         else if (idx[i] < start) idxout[i] = -1;
267d4bb536fSBarry Smith         else if (idx[i] > end)   idxout[i] = -1;
268d4bb536fSBarry Smith         else                     idxout[i] = globals[idx[i] - start];
269d4bb536fSBarry Smith       }
270d4bb536fSBarry Smith     }
271d4bb536fSBarry Smith     if (nout) *nout = n;
272d4bb536fSBarry Smith   } else {
273d4bb536fSBarry Smith     if (idxout) {
274d4bb536fSBarry Smith       for ( i=0; i<n; i++ ) {
275d4bb536fSBarry Smith         if (idx[i] < 0) continue;
276d4bb536fSBarry Smith         if (idx[i] < start) continue;
277d4bb536fSBarry Smith         if (idx[i] > end) continue;
278d4bb536fSBarry Smith         tmp = globals[idx[i] - start];
279d4bb536fSBarry Smith         if (tmp < 0) continue;
280d4bb536fSBarry Smith         idxout[nf++] = tmp;
281d4bb536fSBarry Smith       }
282d4bb536fSBarry Smith     } else {
283d4bb536fSBarry Smith       for ( i=0; i<n; i++ ) {
284d4bb536fSBarry Smith         if (idx[i] < 0) continue;
285d4bb536fSBarry Smith         if (idx[i] < start) continue;
286d4bb536fSBarry Smith         if (idx[i] > end) continue;
287d4bb536fSBarry Smith         tmp = globals[idx[i] - start];
288d4bb536fSBarry Smith         if (tmp < 0) continue;
289d4bb536fSBarry Smith         nf++;
290d4bb536fSBarry Smith       }
291d4bb536fSBarry Smith     }
292d4bb536fSBarry Smith     if (nout) *nout = nf;
293d4bb536fSBarry Smith   }
294d4bb536fSBarry Smith 
295*3a40ed3dSBarry Smith   PetscFunctionReturn(0);
296d4bb536fSBarry Smith }
29790f02eecSBarry Smith 
298