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