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