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