xref: /petsc/src/vec/is/utils/isltog.c (revision 549d3d68a6ae470532d58d544870024f02ff2d7c) !
1 
2 #ifdef PETSC_RCS_HEADER
3 static char vcid[] = "$Id: isltog.c,v 1.27 1999/02/01 21:43:29 curfman Exp balay $";
4 #endif
5 
6 #include "sys.h"   /*I "sys.h" I*/
7 #include "src/vec/is/isimpl.h"    /*I "is.h"  I*/
8 
9 #undef __FUNC__
10 #define __FUNC__ "ISLocalToGlobalMappingView"
11 /*@C
12     ISLocalToGlobalMappingView - View a local to global mapping
13 
14     Not Collective
15 
16     Input Parameters:
17 .   ltog - local to global mapping
18 .   viewer - viewer
19 
20     Level: advanced
21 
22 .keywords: IS, local-to-global mapping, create
23 
24 .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
25 @*/
26 int ISLocalToGlobalMappingView(ISLocalToGlobalMapping mapping,Viewer viewer)
27 {
28   int      i;
29 
30   PetscFunctionBegin;
31 
32   for ( i=0; i<mapping->n; i++ ) {
33     printf("%d %d\n",i,mapping->indices[i]);
34   }
35 
36   PetscFunctionReturn(0);
37 }
38 
39 #undef __FUNC__
40 #define __FUNC__ "ISLocalToGlobalMappingCreateIS"
41 /*@C
42     ISLocalToGlobalMappingCreateIS - Creates a mapping between a local (0 to n)
43     ordering and a global parallel ordering.
44 
45     Collective on IS
46 
47     Input Parameter:
48 .   is - index set containing the global numbers for each local
49 
50     Output Parameter:
51 .   mapping - new mapping data structure
52 
53     Level: advanced
54 
55 .keywords: IS, local-to-global mapping, create
56 
57 .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
58 @*/
59 int ISLocalToGlobalMappingCreateIS(IS is,ISLocalToGlobalMapping *mapping)
60 {
61   int      n,*indices,ierr;
62   MPI_Comm comm;
63 
64   PetscFunctionBegin;
65   PetscValidHeaderSpecific(is,IS_COOKIE);
66 
67   ierr = PetscObjectGetComm((PetscObject)is,&comm);CHKERRQ(ierr);
68   ierr = ISGetSize(is,&n);CHKERRQ(ierr);
69   ierr = ISGetIndices(is,&indices);CHKERRQ(ierr);
70   ierr = ISLocalToGlobalMappingCreate(comm,n,indices,mapping);CHKERRQ(ierr);
71   ierr = ISRestoreIndices(is,&indices);CHKERRQ(ierr);
72 
73   PetscFunctionReturn(0);
74 }
75 
76 #undef __FUNC__
77 #define __FUNC__ "ISLocalToGlobalMappingCreate"
78 /*@C
79     ISLocalToGlobalMappingCreate - Creates a mapping between a local (0 to n)
80     ordering and a global parallel ordering.
81 
82     Collective on MPI_Comm
83 
84     Input Parameters:
85 +   comm - MPI communicator of size 1.
86 .   n - the number of local elements
87 -   indices - the global index for each local element
88 
89     Output Parameter:
90 .   mapping - new mapping data structure
91 
92     Level: advanced
93 
94 .keywords: IS, local-to-global mapping, create
95 
96 .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS()
97 @*/
98 int ISLocalToGlobalMappingCreate(MPI_Comm cm,int n,const int indices[],ISLocalToGlobalMapping *mapping)
99 {
100   int ierr;
101 
102   PetscFunctionBegin;
103   PetscValidIntPointer(indices);
104   PetscValidPointer(mapping);
105 
106   PetscHeaderCreate(*mapping,_p_ISLocalToGlobalMapping,int,IS_LTOGM_COOKIE,0,"ISLocalToGlobalMapping",
107                     cm,ISLocalToGlobalMappingDestroy,ISLocalToGlobalMappingView);
108   PLogObjectCreate(*mapping);
109   PLogObjectMemory(*mapping,sizeof(struct _p_ISLocalToGlobalMapping)+n*sizeof(int));
110 
111   (*mapping)->n       = n;
112   (*mapping)->indices = (int *) PetscMalloc((n+1)*sizeof(int));CHKPTRQ((*mapping)->indices);
113   ierr = PetscMemcpy((*mapping)->indices,indices,n*sizeof(int));CHKERRQ(ierr);
114 
115   /*
116       Do not create the global to local mapping. This is only created if
117      ISGlobalToLocalMapping() is called
118   */
119   (*mapping)->globals = 0;
120   PetscFunctionReturn(0);
121 }
122 
123 #undef __FUNC__
124 #define __FUNC__ "ISLocalToGlobalMappingDestroy"
125 /*@
126    ISLocalToGlobalMappingDestroy - Destroys a mapping between a local (0 to n)
127    ordering and a global parallel ordering.
128 
129    Collective on ISLocalToGlobalMapping
130 
131    Input Parameters:
132 .  mapping - mapping data structure
133 
134    Level: advanced
135 
136 .keywords: IS, local-to-global mapping, destroy
137 
138 .seealso: ISLocalToGlobalMappingCreate()
139 @*/
140 int ISLocalToGlobalMappingDestroy(ISLocalToGlobalMapping mapping)
141 {
142   PetscFunctionBegin;
143   PetscValidPointer(mapping);
144   if (--mapping->refct > 0) PetscFunctionReturn(0);
145 
146   PetscFree(mapping->indices);
147   if (mapping->globals) PetscFree(mapping->globals);
148   PLogObjectDestroy(mapping);
149   PetscHeaderDestroy(mapping);
150   PetscFunctionReturn(0);
151 }
152 
153 #undef __FUNC__
154 #define __FUNC__ "ISLocalToGlobalMappingApplyIS"
155 /*@
156     ISLocalToGlobalMappingApplyIS - Creates from an IS in the local numbering
157     a new index set using the global numbering defined in an ISLocalToGlobalMapping
158     context.
159 
160     Not collective
161 
162     Input Parameters:
163 +   mapping - mapping between local and global numbering
164 -   is - index set in local numbering
165 
166     Output Parameters:
167 .   newis - index set in global numbering
168 
169     Level: advanced
170 
171 .keywords: IS, local-to-global mapping, apply
172 
173 .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),
174           ISLocalToGlobalMappingDestroy(), ISGlobalToLocalMappingApply()
175 @*/
176 int ISLocalToGlobalMappingApplyIS(ISLocalToGlobalMapping mapping, IS is, IS *newis)
177 {
178   int ierr,n,i,*idxin,*idxmap,*idxout;
179 
180   PetscFunctionBegin;
181   PetscValidPointer(mapping);
182   PetscValidHeaderSpecific(is,IS_COOKIE);
183   PetscValidPointer(newis);
184 
185   ierr   = ISGetSize(is,&n);CHKERRQ(ierr);
186   ierr   = ISGetIndices(is,&idxin);CHKERRQ(ierr);
187   idxmap = mapping->indices;
188 
189   idxout = (int *) PetscMalloc((n+1)*sizeof(int));CHKPTRQ(idxout);
190   for ( i=0; i<n; i++ ) {
191     idxout[i] = idxmap[idxin[i]];
192   }
193   ierr = ISCreateGeneral(PETSC_COMM_SELF,n,idxout,newis);CHKERRQ(ierr);
194   PetscFree(idxout);
195   PetscFunctionReturn(0);
196 }
197 
198 #undef __FUNC__
199 #define __FUNC__ "ISLocalToGlobalMappingApply"
200 /*@C
201    ISLocalToGlobalMappingApply - Takes a list of integers in a local numbering
202    and converts them to the global numbering.
203 
204    Not collective
205 
206    Input Parameters:
207 +  mapping - the local to global mapping context
208 .  N - number of integers
209 -  in - input indices in local numbering
210 
211    Output Parameter:
212 .  out - indices in global numbering
213 
214    Notes:
215    The in and out array parameters may be identical.
216 
217    Level: advanced
218 
219 .seealso: ISLocalToGlobalMappingCreate(),ISLocalToGlobalMappingDestroy(),
220           ISLocalToGlobalMappingApplyIS(),AOCreateBasic(),AOApplicationToPetsc(),
221           AOPetscToApplication(), ISGlobalToLocalMappingApply()
222 
223 .keywords: local-to-global, mapping, apply
224 
225 @*/
226 int ISLocalToGlobalMappingApply(ISLocalToGlobalMapping mapping,int N,const int in[],int out[])
227 {
228   int i,*idx = mapping->indices,Nmax = mapping->n;
229 
230   PetscFunctionBegin;
231   for ( i=0; i<N; i++ ) {
232     if (in[i] < 0) {out[i] = in[i]; continue;}
233     if (in[i] >= Nmax) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,1,"Local index %d too large %d (max)",in[i],Nmax);
234     out[i] = idx[in[i]];
235   }
236   PetscFunctionReturn(0);
237 }
238 
239 /* -----------------------------------------------------------------------------------------*/
240 
241 #undef __FUNC__
242 #define __FUNC__ "ISGlobalToLocalMappingSetUp_Private"
243 /*
244     Creates the global fields in the ISLocalToGlobalMapping structure
245 */
246 static int ISGlobalToLocalMappingSetUp_Private(ISLocalToGlobalMapping mapping)
247 {
248   int i,*idx = mapping->indices,n = mapping->n,end,start,*globals;
249 
250   PetscFunctionBegin;
251   end   = 0;
252   start = 100000000;
253 
254   for ( i=0; i<n; i++ ) {
255     if (idx[i] < 0) continue;
256     if (idx[i] < start) start = idx[i];
257     if (idx[i] > end)   end   = idx[i];
258   }
259   if (start > end) {start = 0; end = -1;}
260   mapping->globalstart = start;
261   mapping->globalend   = end;
262 
263   globals = mapping->globals = (int *) PetscMalloc((end-start+2)*sizeof(int));CHKPTRQ(mapping->globals);
264   for ( i=0; i<end-start+1; i++ ) {
265     globals[i] = -1;
266   }
267   for ( i=0; i<n; i++ ) {
268     if (idx[i] < 0) continue;
269     globals[idx[i] - start] = i;
270   }
271 
272   PLogObjectMemory(mapping,(end-start+1)*sizeof(int));
273   PetscFunctionReturn(0);
274 }
275 
276 #undef __FUNC__
277 #define __FUNC__ "ISGlobalToLocalMappingApply"
278 /*@
279     ISGlobalToLocalMappingApply - Provides the local numbering for a list of integers
280     specified with a global numbering.
281 
282     Not collective
283 
284     Input Parameters:
285 +   mapping - mapping between local and global numbering
286 .   type - IS_GTOLM_MASK - replaces global indices with no local value with -1
287            IS_GTOLM_DROP - drops the indices with no local value from the output list
288 .   n - number of global indices to map
289 -   idx - global indices to map
290 
291     Output Parameters:
292 +   nout - number of indices in output array (if type == IS_GTOLM_MASK then nout = n)
293 -   idxout - local index of each global index, one must pass in an array long enough
294              to hold all the indices. You can call ISGlobalToLocalMappingApply() with
295              idxout == PETSC_NULL to determine the required length (returned in nout)
296              and then allocate the required space and call ISGlobalToLocalMappingApply()
297              a second time to set the values.
298 
299     Notes:
300     Either nout or idxout may be PETSC_NULL. idx and idxout may be identical.
301 
302     Level: advanced
303 
304 .keywords: IS, global-to-local mapping, apply
305 
306 .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),
307           ISLocalToGlobalMappingDestroy()
308 @*/
309 int ISGlobalToLocalMappingApply(ISLocalToGlobalMapping mapping, ISGlobalToLocalMappingType type,
310                                   int n, const int idx[],int *nout,int idxout[])
311 {
312   int i,ierr, *globals,nf = 0,tmp,start,end;
313 
314   PetscFunctionBegin;
315   if (!mapping->globals) {
316     ierr = ISGlobalToLocalMappingSetUp_Private(mapping);CHKERRQ(ierr);
317   }
318   globals = mapping->globals;
319   start   = mapping->globalstart;
320   end     = mapping->globalend;
321 
322   if (type == IS_GTOLM_MASK) {
323     if (idxout) {
324       for ( i=0; i<n; i++ ) {
325         if (idx[i] < 0) idxout[i] = idx[i];
326         else if (idx[i] < start) idxout[i] = -1;
327         else if (idx[i] > end)   idxout[i] = -1;
328         else                     idxout[i] = globals[idx[i] - start];
329       }
330     }
331     if (nout) *nout = n;
332   } else {
333     if (idxout) {
334       for ( i=0; i<n; i++ ) {
335         if (idx[i] < 0) continue;
336         if (idx[i] < start) continue;
337         if (idx[i] > end) continue;
338         tmp = globals[idx[i] - start];
339         if (tmp < 0) continue;
340         idxout[nf++] = tmp;
341       }
342     } else {
343       for ( i=0; i<n; i++ ) {
344         if (idx[i] < 0) continue;
345         if (idx[i] < start) continue;
346         if (idx[i] > end) continue;
347         tmp = globals[idx[i] - start];
348         if (tmp < 0) continue;
349         nf++;
350       }
351     }
352     if (nout) *nout = nf;
353   }
354 
355   PetscFunctionReturn(0);
356 }
357 
358