xref: /petsc/src/vec/is/utils/isltog.c (revision 3bbf0e9209c918da710d8f50ca5c48af17a42e60)
12362add9SBarry Smith 
2b45d2f2cSJed Brown #include <petsc-private/isimpl.h>    /*I "petscis.h"  I*/
30c312b8eSJed Brown #include <petscsf.h>
4665c2dedSJed Brown #include <petscviewer.h>
52362add9SBarry Smith 
67087cfbeSBarry Smith PetscClassId IS_LTOGM_CLASSID;
78e58c17dSMatthew Knepley 
84a2ae208SSatish Balay #undef __FUNCT__
9743c9b42SStefano Zampini #define __FUNCT__ "ISG2LMapApply"
10186d4ecdSBarry Smith PetscErrorCode ISG2LMapApply(ISLocalToGlobalMapping mapping,PetscInt n,const PetscInt in[],PetscInt out[])
11186d4ecdSBarry Smith {
12186d4ecdSBarry Smith   PetscErrorCode ierr;
13743c9b42SStefano Zampini   PetscInt       i,start,end;
14186d4ecdSBarry Smith 
15186d4ecdSBarry Smith   PetscFunctionBegin;
16186d4ecdSBarry Smith   if (!mapping->globals) {
17186d4ecdSBarry Smith     ierr = ISGlobalToLocalMappingApply(mapping,IS_GTOLM_MASK,0,0,0,0);CHKERRQ(ierr);
18186d4ecdSBarry Smith   }
19743c9b42SStefano Zampini   start = mapping->globalstart;
20743c9b42SStefano Zampini   end = mapping->globalend;
21186d4ecdSBarry Smith   for (i=0; i<n; i++) {
22186d4ecdSBarry Smith     if (in[i] < 0)          out[i] = in[i];
23186d4ecdSBarry Smith     else if (in[i] < start) out[i] = -1;
24186d4ecdSBarry Smith     else if (in[i] > end)   out[i] = -1;
25743c9b42SStefano Zampini     else                    out[i] = mapping->globals[in[i] - start];
26186d4ecdSBarry Smith   }
27186d4ecdSBarry Smith   PetscFunctionReturn(0);
28186d4ecdSBarry Smith }
29186d4ecdSBarry Smith 
30186d4ecdSBarry Smith 
31186d4ecdSBarry Smith #undef __FUNCT__
324a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingGetSize"
333b9aefa3SBarry Smith /*@C
343b9aefa3SBarry Smith     ISLocalToGlobalMappingGetSize - Gets the local size of a local to global mapping.
353b9aefa3SBarry Smith 
363b9aefa3SBarry Smith     Not Collective
373b9aefa3SBarry Smith 
383b9aefa3SBarry Smith     Input Parameter:
393b9aefa3SBarry Smith .   ltog - local to global mapping
403b9aefa3SBarry Smith 
413b9aefa3SBarry Smith     Output Parameter:
423b9aefa3SBarry Smith .   n - the number of entries in the local mapping
433b9aefa3SBarry Smith 
443b9aefa3SBarry Smith     Level: advanced
453b9aefa3SBarry Smith 
46273d9f13SBarry Smith     Concepts: mapping^local to global
473b9aefa3SBarry Smith 
483b9aefa3SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
493b9aefa3SBarry Smith @*/
507087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingGetSize(ISLocalToGlobalMapping mapping,PetscInt *n)
513b9aefa3SBarry Smith {
523b9aefa3SBarry Smith   PetscFunctionBegin;
530700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
544482741eSBarry Smith   PetscValidIntPointer(n,2);
553b9aefa3SBarry Smith   *n = mapping->n;
563b9aefa3SBarry Smith   PetscFunctionReturn(0);
573b9aefa3SBarry Smith }
583b9aefa3SBarry Smith 
594a2ae208SSatish Balay #undef __FUNCT__
604a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingView"
615a5d4f66SBarry Smith /*@C
625a5d4f66SBarry Smith     ISLocalToGlobalMappingView - View a local to global mapping
635a5d4f66SBarry Smith 
64b9cd556bSLois Curfman McInnes     Not Collective
65b9cd556bSLois Curfman McInnes 
665a5d4f66SBarry Smith     Input Parameters:
673b9aefa3SBarry Smith +   ltog - local to global mapping
683b9aefa3SBarry Smith -   viewer - viewer
695a5d4f66SBarry Smith 
70a997ad1aSLois Curfman McInnes     Level: advanced
71a997ad1aSLois Curfman McInnes 
72273d9f13SBarry Smith     Concepts: mapping^local to global
735a5d4f66SBarry Smith 
745a5d4f66SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
755a5d4f66SBarry Smith @*/
767087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingView(ISLocalToGlobalMapping mapping,PetscViewer viewer)
775a5d4f66SBarry Smith {
7832dcc486SBarry Smith   PetscInt       i;
7932dcc486SBarry Smith   PetscMPIInt    rank;
80ace3abfcSBarry Smith   PetscBool      iascii;
816849ba73SBarry Smith   PetscErrorCode ierr;
825a5d4f66SBarry Smith 
835a5d4f66SBarry Smith   PetscFunctionBegin;
840700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
853050cee2SBarry Smith   if (!viewer) {
86ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)mapping),&viewer);CHKERRQ(ierr);
873050cee2SBarry Smith   }
880700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
895a5d4f66SBarry Smith 
90ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)mapping),&rank);CHKERRQ(ierr);
91251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
9232077d6dSBarry Smith   if (iascii) {
937b23a99aSBarry Smith     ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_TRUE);CHKERRQ(ierr);
945a5d4f66SBarry Smith     for (i=0; i<mapping->n; i++) {
95b0a32e0cSBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %d %d\n",rank,i,mapping->indices[i]);CHKERRQ(ierr);
966831982aSBarry Smith     }
97b0a32e0cSBarry Smith     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
987b23a99aSBarry Smith     ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_FALSE);CHKERRQ(ierr);
997b23a99aSBarry Smith   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported for ISLocalToGlobalMapping",((PetscObject)viewer)->type_name);
1005a5d4f66SBarry Smith   PetscFunctionReturn(0);
1015a5d4f66SBarry Smith }
1025a5d4f66SBarry Smith 
1034a2ae208SSatish Balay #undef __FUNCT__
1044a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingCreateIS"
1051f428162SBarry Smith /*@
1062bdab257SBarry Smith     ISLocalToGlobalMappingCreateIS - Creates a mapping between a local (0 to n)
1072bdab257SBarry Smith     ordering and a global parallel ordering.
1082bdab257SBarry Smith 
1090f5bd95cSBarry Smith     Not collective
110b9cd556bSLois Curfman McInnes 
111a997ad1aSLois Curfman McInnes     Input Parameter:
1128c03b21aSDmitry Karpeev .   is - index set containing the global numbers for each local number
1132bdab257SBarry Smith 
114a997ad1aSLois Curfman McInnes     Output Parameter:
1152bdab257SBarry Smith .   mapping - new mapping data structure
1162bdab257SBarry Smith 
117a997ad1aSLois Curfman McInnes     Level: advanced
118a997ad1aSLois Curfman McInnes 
119273d9f13SBarry Smith     Concepts: mapping^local to global
1202bdab257SBarry Smith 
1212bdab257SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
1222bdab257SBarry Smith @*/
1237087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingCreateIS(IS is,ISLocalToGlobalMapping *mapping)
1242bdab257SBarry Smith {
1256849ba73SBarry Smith   PetscErrorCode ierr;
126*3bbf0e92SBarry Smith   PetscInt       n,bs;
1275d0c19d7SBarry Smith   const PetscInt *indices;
1282bdab257SBarry Smith   MPI_Comm       comm;
129*3bbf0e92SBarry Smith   PetscBool      isblock;
1303a40ed3dSBarry Smith 
1313a40ed3dSBarry Smith   PetscFunctionBegin;
1320700a824SBarry Smith   PetscValidHeaderSpecific(is,IS_CLASSID,1);
1334482741eSBarry Smith   PetscValidPointer(mapping,2);
1342bdab257SBarry Smith 
1352bdab257SBarry Smith   ierr = PetscObjectGetComm((PetscObject)is,&comm);CHKERRQ(ierr);
1363b9aefa3SBarry Smith   ierr = ISGetLocalSize(is,&n);CHKERRQ(ierr);
1372bdab257SBarry Smith   ierr = ISGetIndices(is,&indices);CHKERRQ(ierr);
138*3bbf0e92SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)is,ISBLOCK,&isblock);CHKERRQ(ierr);
139*3bbf0e92SBarry Smith   ierr = ISGetBlockSize(is,&bs);CHKERRQ(ierr);
140*3bbf0e92SBarry Smith   if (!isblock || bs == 1) {
141d5ad8652SBarry Smith     ierr = ISLocalToGlobalMappingCreate(comm,n,indices,PETSC_COPY_VALUES,mapping);CHKERRQ(ierr);
1422bdab257SBarry Smith     ierr = ISRestoreIndices(is,&indices);CHKERRQ(ierr);
143*3bbf0e92SBarry Smith   } else {
144*3bbf0e92SBarry Smith     ierr = ISLocalToGlobalMappingCreate(comm,n,indices,PETSC_OWN_POINTER,mapping);CHKERRQ(ierr);
145*3bbf0e92SBarry Smith   }
1463a40ed3dSBarry Smith   PetscFunctionReturn(0);
1472bdab257SBarry Smith }
1485a5d4f66SBarry Smith 
149a4d96a55SJed Brown #undef __FUNCT__
150a4d96a55SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingCreateSF"
151a4d96a55SJed Brown /*@C
152a4d96a55SJed Brown     ISLocalToGlobalMappingCreateSF - Creates a mapping between a local (0 to n)
153a4d96a55SJed Brown     ordering and a global parallel ordering.
154a4d96a55SJed Brown 
155a4d96a55SJed Brown     Collective
156a4d96a55SJed Brown 
157a4d96a55SJed Brown     Input Parameter:
158a4d96a55SJed Brown +   sf - star forest mapping contiguous local indices to (rank, offset)
159a4d96a55SJed Brown -   start - first global index on this process
160a4d96a55SJed Brown 
161a4d96a55SJed Brown     Output Parameter:
162a4d96a55SJed Brown .   mapping - new mapping data structure
163a4d96a55SJed Brown 
164a4d96a55SJed Brown     Level: advanced
165a4d96a55SJed Brown 
166a4d96a55SJed Brown     Concepts: mapping^local to global
167a4d96a55SJed Brown 
168a4d96a55SJed Brown .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingCreateIS()
169a4d96a55SJed Brown @*/
170a4d96a55SJed Brown PetscErrorCode ISLocalToGlobalMappingCreateSF(PetscSF sf,PetscInt start,ISLocalToGlobalMapping *mapping)
171a4d96a55SJed Brown {
172a4d96a55SJed Brown   PetscErrorCode ierr;
173a4d96a55SJed Brown   PetscInt       i,maxlocal,nroots,nleaves,*globals,*ltog;
174a4d96a55SJed Brown   const PetscInt *ilocal;
175a4d96a55SJed Brown   MPI_Comm       comm;
176a4d96a55SJed Brown 
177a4d96a55SJed Brown   PetscFunctionBegin;
178a4d96a55SJed Brown   PetscValidHeaderSpecific(sf,PETSCSF_CLASSID,1);
179a4d96a55SJed Brown   PetscValidPointer(mapping,3);
180a4d96a55SJed Brown 
181a4d96a55SJed Brown   ierr = PetscObjectGetComm((PetscObject)sf,&comm);CHKERRQ(ierr);
1820298fd71SBarry Smith   ierr = PetscSFGetGraph(sf,&nroots,&nleaves,&ilocal,NULL);CHKERRQ(ierr);
183f6e5521dSKarl Rupp   if (ilocal) {
184f6e5521dSKarl Rupp     for (i=0,maxlocal=0; i<nleaves; i++) maxlocal = PetscMax(maxlocal,ilocal[i]+1);
185f6e5521dSKarl Rupp   }
186a4d96a55SJed Brown   else maxlocal = nleaves;
187a4d96a55SJed Brown   ierr = PetscMalloc(nroots*sizeof(PetscInt),&globals);CHKERRQ(ierr);
188a4d96a55SJed Brown   ierr = PetscMalloc(maxlocal*sizeof(PetscInt),&ltog);CHKERRQ(ierr);
189a4d96a55SJed Brown   for (i=0; i<nroots; i++) globals[i] = start + i;
190a4d96a55SJed Brown   for (i=0; i<maxlocal; i++) ltog[i] = -1;
191a4d96a55SJed Brown   ierr = PetscSFBcastBegin(sf,MPIU_INT,globals,ltog);CHKERRQ(ierr);
192a4d96a55SJed Brown   ierr = PetscSFBcastEnd(sf,MPIU_INT,globals,ltog);CHKERRQ(ierr);
193a4d96a55SJed Brown   ierr = ISLocalToGlobalMappingCreate(comm,maxlocal,ltog,PETSC_OWN_POINTER,mapping);CHKERRQ(ierr);
194a4d96a55SJed Brown   ierr = PetscFree(globals);CHKERRQ(ierr);
195a4d96a55SJed Brown   PetscFunctionReturn(0);
196a4d96a55SJed Brown }
197b46b645bSBarry Smith 
1984a2ae208SSatish Balay #undef __FUNCT__
1994a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingCreate"
200ba5bb76aSSatish Balay /*@
20190f02eecSBarry Smith     ISLocalToGlobalMappingCreate - Creates a mapping between a local (0 to n)
20290f02eecSBarry Smith     ordering and a global parallel ordering.
2032362add9SBarry Smith 
20489d82c54SBarry Smith     Not Collective, but communicator may have more than one process
205b9cd556bSLois Curfman McInnes 
2062362add9SBarry Smith     Input Parameters:
20789d82c54SBarry Smith +   comm - MPI communicator
20890f02eecSBarry Smith .   n - the number of local elements
2099669e4d8SBarry Smith .   indices - the global index for each local element, these do not need to be in increasing order (sorted)
210d5ad8652SBarry Smith -   mode - see PetscCopyMode
2112362add9SBarry Smith 
212a997ad1aSLois Curfman McInnes     Output Parameter:
21390f02eecSBarry Smith .   mapping - new mapping data structure
2142362add9SBarry Smith 
215a997ad1aSLois Curfman McInnes     Level: advanced
216a997ad1aSLois Curfman McInnes 
217273d9f13SBarry Smith     Concepts: mapping^local to global
2182362add9SBarry Smith 
219d5ad8652SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS()
2202362add9SBarry Smith @*/
2217087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingCreate(MPI_Comm cm,PetscInt n,const PetscInt indices[],PetscCopyMode mode,ISLocalToGlobalMapping *mapping)
2222362add9SBarry Smith {
2236849ba73SBarry Smith   PetscErrorCode ierr;
22432dcc486SBarry Smith   PetscInt       *in;
225b46b645bSBarry Smith 
226b46b645bSBarry Smith   PetscFunctionBegin;
22773911063SBarry Smith   if (n) PetscValidIntPointer(indices,3);
2284482741eSBarry Smith   PetscValidPointer(mapping,4);
229b46b645bSBarry Smith 
2300298fd71SBarry Smith   *mapping = NULL;
231607a6623SBarry Smith   ierr = ISInitializePackage();CHKERRQ(ierr);
2322362add9SBarry Smith 
23367c2884eSBarry Smith   ierr = PetscHeaderCreate(*mapping,_p_ISLocalToGlobalMapping,int,IS_LTOGM_CLASSID,"ISLocalToGlobalMapping","Local to global mapping","IS",
23452e6d16bSBarry Smith                            cm,ISLocalToGlobalMappingDestroy,ISLocalToGlobalMappingView);CHKERRQ(ierr);
235d4bb536fSBarry Smith   (*mapping)->n = n;
236d4bb536fSBarry Smith   /*
237d4bb536fSBarry Smith     Do not create the global to local mapping. This is only created if
238d4bb536fSBarry Smith     ISGlobalToLocalMapping() is called
239d4bb536fSBarry Smith   */
240d4bb536fSBarry Smith   (*mapping)->globals = 0;
241d5ad8652SBarry Smith   if (mode == PETSC_COPY_VALUES) {
242d5ad8652SBarry Smith     ierr = PetscMalloc(n*sizeof(PetscInt),&in);CHKERRQ(ierr);
243d5ad8652SBarry Smith     ierr = PetscMemcpy(in,indices,n*sizeof(PetscInt));CHKERRQ(ierr);
2443bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)*mapping,n*sizeof(PetscInt));CHKERRQ(ierr);
245d5ad8652SBarry Smith     (*mapping)->indices = in;
246f6e5521dSKarl Rupp   } else if (mode == PETSC_OWN_POINTER) (*mapping)->indices = (PetscInt*)indices;
247f6e5521dSKarl Rupp   else SETERRQ(cm,PETSC_ERR_SUP,"Cannot currently use PETSC_USE_POINTER");
2483a40ed3dSBarry Smith   PetscFunctionReturn(0);
2492362add9SBarry Smith }
2502362add9SBarry Smith 
2514a2ae208SSatish Balay #undef __FUNCT__
2524a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingBlock"
253bce096a4SSatish Balay /*@
254323b833fSBarry Smith     ISLocalToGlobalMappingBlock - Creates a blocked index version of an
255323b833fSBarry Smith        ISLocalToGlobalMapping that is appropriate for MatSetLocalToGlobalMappingBlock()
256323b833fSBarry Smith        and VecSetLocalToGlobalMappingBlock().
257323b833fSBarry Smith 
258323b833fSBarry Smith     Not Collective, but communicator may have more than one process
259323b833fSBarry Smith 
260323b833fSBarry Smith     Input Parameters:
261323b833fSBarry Smith +    inmap - original point-wise mapping
262323b833fSBarry Smith -    bs - block size
263323b833fSBarry Smith 
264323b833fSBarry Smith     Output Parameter:
26569eb54c3SBarry Smith .   outmap - block based mapping; the indices are relative to BLOCKS, not individual vector or matrix entries.
266323b833fSBarry Smith 
267323b833fSBarry Smith     Level: advanced
268323b833fSBarry Smith 
269323b833fSBarry Smith     Concepts: mapping^local to global
270323b833fSBarry Smith 
271323b833fSBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingCreateIS()
272323b833fSBarry Smith @*/
2737087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingBlock(ISLocalToGlobalMapping inmap,PetscInt bs,ISLocalToGlobalMapping *outmap)
274323b833fSBarry Smith {
2756849ba73SBarry Smith   PetscErrorCode ierr;
27632dcc486SBarry Smith   PetscInt       *ii,i,n;
277323b833fSBarry Smith 
278323b833fSBarry Smith   PetscFunctionBegin;
2790700a824SBarry Smith   PetscValidHeaderSpecific(inmap,IS_LTOGM_CLASSID,1);
280b2beed0aSJed Brown   PetscValidPointer(outmap,3);
281323b833fSBarry Smith   if (bs > 1) {
282323b833fSBarry Smith     n = inmap->n/bs;
283e32f2f54SBarry Smith     if (n*bs != inmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Pointwise mapping length is not divisible by block size");
28432dcc486SBarry Smith     ierr = PetscMalloc(n*sizeof(PetscInt),&ii);CHKERRQ(ierr);
285f6e5521dSKarl Rupp     for (i=0; i<n; i++) ii[i] = inmap->indices[bs*i]/bs;
286ce94432eSBarry Smith     ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)inmap),n,ii,PETSC_OWN_POINTER,outmap);CHKERRQ(ierr);
287323b833fSBarry Smith   } else {
288323b833fSBarry Smith     ierr    = PetscObjectReference((PetscObject)inmap);CHKERRQ(ierr);
289c3122656SLisandro Dalcin     *outmap = inmap;
290323b833fSBarry Smith   }
291323b833fSBarry Smith   PetscFunctionReturn(0);
292323b833fSBarry Smith }
293323b833fSBarry Smith 
2944a2ae208SSatish Balay #undef __FUNCT__
2958ab951edSJed Brown #define __FUNCT__ "ISLocalToGlobalMappingUnBlock"
296b2beed0aSJed Brown /*@
297b2beed0aSJed Brown     ISLocalToGlobalMappingUnBlock - Creates a scalar index version of a blocked
298b2beed0aSJed Brown        ISLocalToGlobalMapping
299b2beed0aSJed Brown 
300b2beed0aSJed Brown     Not Collective, but communicator may have more than one process
301b2beed0aSJed Brown 
302b2beed0aSJed Brown     Input Parameter:
303b2beed0aSJed Brown + inmap - block based mapping; the indices are relative to BLOCKS, not individual vector or matrix entries.
304b2beed0aSJed Brown - bs - block size
305b2beed0aSJed Brown 
306b2beed0aSJed Brown     Output Parameter:
307b2beed0aSJed Brown .   outmap - pointwise mapping
308b2beed0aSJed Brown 
309b2beed0aSJed Brown     Level: advanced
310b2beed0aSJed Brown 
311b2beed0aSJed Brown     Concepts: mapping^local to global
312b2beed0aSJed Brown 
313b2beed0aSJed Brown .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingBlock()
314b2beed0aSJed Brown @*/
3157087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingUnBlock(ISLocalToGlobalMapping inmap,PetscInt bs,ISLocalToGlobalMapping *outmap)
316b2beed0aSJed Brown {
317b2beed0aSJed Brown   PetscErrorCode ierr;
318b2beed0aSJed Brown   PetscInt       *ii,i,n;
319b2beed0aSJed Brown 
320b2beed0aSJed Brown   PetscFunctionBegin;
321b2beed0aSJed Brown   PetscValidHeaderSpecific(inmap,IS_LTOGM_CLASSID,1);
322b2beed0aSJed Brown   PetscValidPointer(outmap,2);
323b2beed0aSJed Brown   if (bs > 1) {
324b2beed0aSJed Brown     n    = inmap->n*bs;
325b2beed0aSJed Brown     ierr = PetscMalloc(n*sizeof(PetscInt),&ii);CHKERRQ(ierr);
326f6e5521dSKarl Rupp     for (i=0; i<n; i++) ii[i] = inmap->indices[i/bs]*bs + (i%bs);
327ce94432eSBarry Smith     ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)inmap),n,ii,PETSC_OWN_POINTER,outmap);CHKERRQ(ierr);
328b2beed0aSJed Brown   } else {
329b2beed0aSJed Brown     ierr    = PetscObjectReference((PetscObject)inmap);CHKERRQ(ierr);
330b2beed0aSJed Brown     *outmap = inmap;
331b2beed0aSJed Brown   }
332b2beed0aSJed Brown   PetscFunctionReturn(0);
333b2beed0aSJed Brown }
334b2beed0aSJed Brown 
335b2beed0aSJed Brown #undef __FUNCT__
3364a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingDestroy"
33790f02eecSBarry Smith /*@
33890f02eecSBarry Smith    ISLocalToGlobalMappingDestroy - Destroys a mapping between a local (0 to n)
33990f02eecSBarry Smith    ordering and a global parallel ordering.
34090f02eecSBarry Smith 
3410f5bd95cSBarry Smith    Note Collective
342b9cd556bSLois Curfman McInnes 
34390f02eecSBarry Smith    Input Parameters:
34490f02eecSBarry Smith .  mapping - mapping data structure
34590f02eecSBarry Smith 
346a997ad1aSLois Curfman McInnes    Level: advanced
347a997ad1aSLois Curfman McInnes 
3483acfe500SLois Curfman McInnes .seealso: ISLocalToGlobalMappingCreate()
34990f02eecSBarry Smith @*/
3506bf464f9SBarry Smith PetscErrorCode  ISLocalToGlobalMappingDestroy(ISLocalToGlobalMapping *mapping)
35190f02eecSBarry Smith {
352dfbe8321SBarry Smith   PetscErrorCode ierr;
3535fd66863SKarl Rupp 
3543a40ed3dSBarry Smith   PetscFunctionBegin;
3556bf464f9SBarry Smith   if (!*mapping) PetscFunctionReturn(0);
3566bf464f9SBarry Smith   PetscValidHeaderSpecific((*mapping),IS_LTOGM_CLASSID,1);
357997056adSBarry Smith   if (--((PetscObject)(*mapping))->refct > 0) {*mapping = 0;PetscFunctionReturn(0);}
3586bf464f9SBarry Smith   ierr     = PetscFree((*mapping)->indices);CHKERRQ(ierr);
3596bf464f9SBarry Smith   ierr     = PetscFree((*mapping)->globals);CHKERRQ(ierr);
360d38fa0fbSBarry Smith   ierr     = PetscHeaderDestroy(mapping);CHKERRQ(ierr);
361992144d0SBarry Smith   *mapping = 0;
3623a40ed3dSBarry Smith   PetscFunctionReturn(0);
36390f02eecSBarry Smith }
36490f02eecSBarry Smith 
3654a2ae208SSatish Balay #undef __FUNCT__
3664a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingApplyIS"
36790f02eecSBarry Smith /*@
3683acfe500SLois Curfman McInnes     ISLocalToGlobalMappingApplyIS - Creates from an IS in the local numbering
3693acfe500SLois Curfman McInnes     a new index set using the global numbering defined in an ISLocalToGlobalMapping
3703acfe500SLois Curfman McInnes     context.
37190f02eecSBarry Smith 
372b9cd556bSLois Curfman McInnes     Not collective
373b9cd556bSLois Curfman McInnes 
37490f02eecSBarry Smith     Input Parameters:
375b9cd556bSLois Curfman McInnes +   mapping - mapping between local and global numbering
376b9cd556bSLois Curfman McInnes -   is - index set in local numbering
37790f02eecSBarry Smith 
37890f02eecSBarry Smith     Output Parameters:
37990f02eecSBarry Smith .   newis - index set in global numbering
38090f02eecSBarry Smith 
381a997ad1aSLois Curfman McInnes     Level: advanced
382a997ad1aSLois Curfman McInnes 
383273d9f13SBarry Smith     Concepts: mapping^local to global
3843acfe500SLois Curfman McInnes 
38590f02eecSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),
386d4bb536fSBarry Smith           ISLocalToGlobalMappingDestroy(), ISGlobalToLocalMappingApply()
38790f02eecSBarry Smith @*/
3887087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingApplyIS(ISLocalToGlobalMapping mapping,IS is,IS *newis)
38990f02eecSBarry Smith {
3906849ba73SBarry Smith   PetscErrorCode ierr;
3915d0c19d7SBarry Smith   PetscInt       n,i,*idxmap,*idxout,Nmax = mapping->n;
3925d0c19d7SBarry Smith   const PetscInt *idxin;
3933a40ed3dSBarry Smith 
3943a40ed3dSBarry Smith   PetscFunctionBegin;
3950700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
3960700a824SBarry Smith   PetscValidHeaderSpecific(is,IS_CLASSID,2);
3974482741eSBarry Smith   PetscValidPointer(newis,3);
39890f02eecSBarry Smith 
3993b9aefa3SBarry Smith   ierr   = ISGetLocalSize(is,&n);CHKERRQ(ierr);
40090f02eecSBarry Smith   ierr   = ISGetIndices(is,&idxin);CHKERRQ(ierr);
40190f02eecSBarry Smith   idxmap = mapping->indices;
40290f02eecSBarry Smith 
4037c334f02SBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&idxout);CHKERRQ(ierr);
40490f02eecSBarry Smith   for (i=0; i<n; i++) {
405e32f2f54SBarry Smith     if (idxin[i] >= Nmax) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local index %d too large %d (max) at %d",idxin[i],Nmax-1,i);
40690f02eecSBarry Smith     idxout[i] = idxmap[idxin[i]];
40790f02eecSBarry Smith   }
4083b9aefa3SBarry Smith   ierr = ISRestoreIndices(is,&idxin);CHKERRQ(ierr);
40970b3c8c7SBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,n,idxout,PETSC_OWN_POINTER,newis);CHKERRQ(ierr);
4103a40ed3dSBarry Smith   PetscFunctionReturn(0);
41190f02eecSBarry Smith }
41290f02eecSBarry Smith 
413afcb2eb5SJed Brown #undef __FUNCT__
414afcb2eb5SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingApply"
415b89cb25eSSatish Balay /*@
4163acfe500SLois Curfman McInnes    ISLocalToGlobalMappingApply - Takes a list of integers in a local numbering
4173acfe500SLois Curfman McInnes    and converts them to the global numbering.
41890f02eecSBarry Smith 
419b9cd556bSLois Curfman McInnes    Not collective
420b9cd556bSLois Curfman McInnes 
421bb25748dSBarry Smith    Input Parameters:
422b9cd556bSLois Curfman McInnes +  mapping - the local to global mapping context
423bb25748dSBarry Smith .  N - number of integers
424b9cd556bSLois Curfman McInnes -  in - input indices in local numbering
425bb25748dSBarry Smith 
426bb25748dSBarry Smith    Output Parameter:
427bb25748dSBarry Smith .  out - indices in global numbering
428bb25748dSBarry Smith 
429b9cd556bSLois Curfman McInnes    Notes:
430b9cd556bSLois Curfman McInnes    The in and out array parameters may be identical.
431d4bb536fSBarry Smith 
432a997ad1aSLois Curfman McInnes    Level: advanced
433a997ad1aSLois Curfman McInnes 
434bb25748dSBarry Smith .seealso: ISLocalToGlobalMappingCreate(),ISLocalToGlobalMappingDestroy(),
4350752156aSBarry Smith           ISLocalToGlobalMappingApplyIS(),AOCreateBasic(),AOApplicationToPetsc(),
436d4bb536fSBarry Smith           AOPetscToApplication(), ISGlobalToLocalMappingApply()
437bb25748dSBarry Smith 
438273d9f13SBarry Smith     Concepts: mapping^local to global
439afcb2eb5SJed Brown @*/
440afcb2eb5SJed Brown PetscErrorCode ISLocalToGlobalMappingApply(ISLocalToGlobalMapping mapping,PetscInt N,const PetscInt in[],PetscInt out[])
441afcb2eb5SJed Brown {
442afcb2eb5SJed Brown   PetscInt       i,Nmax = mapping->n;
443afcb2eb5SJed Brown   const PetscInt *idx = mapping->indices;
444d4bb536fSBarry Smith 
445afcb2eb5SJed Brown   PetscFunctionBegin;
446afcb2eb5SJed Brown   for (i=0; i<N; i++) {
447afcb2eb5SJed Brown     if (in[i] < 0) {
448afcb2eb5SJed Brown       out[i] = in[i];
449afcb2eb5SJed Brown       continue;
450afcb2eb5SJed Brown     }
451afcb2eb5SJed Brown     if (in[i] >= Nmax) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local index %D too large %D (max) at %D",in[i],Nmax,i);
452afcb2eb5SJed Brown     out[i] = idx[in[i]];
453afcb2eb5SJed Brown   }
454afcb2eb5SJed Brown   PetscFunctionReturn(0);
455afcb2eb5SJed Brown }
456d4bb536fSBarry Smith 
457d4bb536fSBarry Smith /* -----------------------------------------------------------------------------------------*/
458d4bb536fSBarry Smith 
4594a2ae208SSatish Balay #undef __FUNCT__
4604a2ae208SSatish Balay #define __FUNCT__ "ISGlobalToLocalMappingSetUp_Private"
461d4bb536fSBarry Smith /*
462d4bb536fSBarry Smith     Creates the global fields in the ISLocalToGlobalMapping structure
463d4bb536fSBarry Smith */
4646849ba73SBarry Smith static PetscErrorCode ISGlobalToLocalMappingSetUp_Private(ISLocalToGlobalMapping mapping)
465d4bb536fSBarry Smith {
4666849ba73SBarry Smith   PetscErrorCode ierr;
46732dcc486SBarry Smith   PetscInt       i,*idx = mapping->indices,n = mapping->n,end,start,*globals;
468d4bb536fSBarry Smith 
4693a40ed3dSBarry Smith   PetscFunctionBegin;
470d4bb536fSBarry Smith   end   = 0;
471ec268f7cSJed Brown   start = PETSC_MAX_INT;
472d4bb536fSBarry Smith 
473d4bb536fSBarry Smith   for (i=0; i<n; i++) {
474d4bb536fSBarry Smith     if (idx[i] < 0) continue;
475d4bb536fSBarry Smith     if (idx[i] < start) start = idx[i];
476d4bb536fSBarry Smith     if (idx[i] > end)   end   = idx[i];
477d4bb536fSBarry Smith   }
478d4bb536fSBarry Smith   if (start > end) {start = 0; end = -1;}
479d4bb536fSBarry Smith   mapping->globalstart = start;
480d4bb536fSBarry Smith   mapping->globalend   = end;
481d4bb536fSBarry Smith 
48232dcc486SBarry Smith   ierr             = PetscMalloc((end-start+2)*sizeof(PetscInt),&globals);CHKERRQ(ierr);
483b0a32e0cSBarry Smith   mapping->globals = globals;
484f6e5521dSKarl Rupp   for (i=0; i<end-start+1; i++) globals[i] = -1;
485d4bb536fSBarry Smith   for (i=0; i<n; i++) {
486d4bb536fSBarry Smith     if (idx[i] < 0) continue;
487d4bb536fSBarry Smith     globals[idx[i] - start] = i;
488d4bb536fSBarry Smith   }
489d4bb536fSBarry Smith 
4903bb1ff40SBarry Smith   ierr = PetscLogObjectMemory((PetscObject)mapping,(end-start+1)*sizeof(PetscInt));CHKERRQ(ierr);
4913a40ed3dSBarry Smith   PetscFunctionReturn(0);
492d4bb536fSBarry Smith }
493d4bb536fSBarry Smith 
4944a2ae208SSatish Balay #undef __FUNCT__
4954a2ae208SSatish Balay #define __FUNCT__ "ISGlobalToLocalMappingApply"
496d4bb536fSBarry Smith /*@
497a997ad1aSLois Curfman McInnes     ISGlobalToLocalMappingApply - Provides the local numbering for a list of integers
498a997ad1aSLois Curfman McInnes     specified with a global numbering.
499d4bb536fSBarry Smith 
500b9cd556bSLois Curfman McInnes     Not collective
501b9cd556bSLois Curfman McInnes 
502d4bb536fSBarry Smith     Input Parameters:
503b9cd556bSLois Curfman McInnes +   mapping - mapping between local and global numbering
504d4bb536fSBarry Smith .   type - IS_GTOLM_MASK - replaces global indices with no local value with -1
505d4bb536fSBarry Smith            IS_GTOLM_DROP - drops the indices with no local value from the output list
506d4bb536fSBarry Smith .   n - number of global indices to map
507b9cd556bSLois Curfman McInnes -   idx - global indices to map
508d4bb536fSBarry Smith 
509d4bb536fSBarry Smith     Output Parameters:
510b9cd556bSLois Curfman McInnes +   nout - number of indices in output array (if type == IS_GTOLM_MASK then nout = n)
511b9cd556bSLois Curfman McInnes -   idxout - local index of each global index, one must pass in an array long enough
512e182c471SBarry Smith              to hold all the indices. You can call ISGlobalToLocalMappingApply() with
5130298fd71SBarry Smith              idxout == NULL to determine the required length (returned in nout)
514e182c471SBarry Smith              and then allocate the required space and call ISGlobalToLocalMappingApply()
515e182c471SBarry Smith              a second time to set the values.
516d4bb536fSBarry Smith 
517b9cd556bSLois Curfman McInnes     Notes:
5180298fd71SBarry Smith     Either nout or idxout may be NULL. idx and idxout may be identical.
519d4bb536fSBarry Smith 
5200f5bd95cSBarry Smith     This is not scalable in memory usage. Each processor requires O(Nglobal) size
5210f5bd95cSBarry Smith     array to compute these.
5220f5bd95cSBarry Smith 
523a997ad1aSLois Curfman McInnes     Level: advanced
524a997ad1aSLois Curfman McInnes 
52532fd6b96SBarry Smith     Developer Note: The manual page states that idx and idxout may be identical but the calling
52632fd6b96SBarry Smith        sequence declares idx as const so it cannot be the same as idxout.
52732fd6b96SBarry Smith 
528273d9f13SBarry Smith     Concepts: mapping^global to local
529d4bb536fSBarry Smith 
530d4bb536fSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),
531d4bb536fSBarry Smith           ISLocalToGlobalMappingDestroy()
532d4bb536fSBarry Smith @*/
5337087cfbeSBarry Smith PetscErrorCode  ISGlobalToLocalMappingApply(ISLocalToGlobalMapping mapping,ISGlobalToLocalMappingType type,
53432dcc486SBarry Smith                                   PetscInt n,const PetscInt idx[],PetscInt *nout,PetscInt idxout[])
535d4bb536fSBarry Smith {
53632dcc486SBarry Smith   PetscInt       i,*globals,nf = 0,tmp,start,end;
5376849ba73SBarry Smith   PetscErrorCode ierr;
538d4bb536fSBarry Smith 
5393a40ed3dSBarry Smith   PetscFunctionBegin;
5400700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
541d4bb536fSBarry Smith   if (!mapping->globals) {
542d4bb536fSBarry Smith     ierr = ISGlobalToLocalMappingSetUp_Private(mapping);CHKERRQ(ierr);
543d4bb536fSBarry Smith   }
544d4bb536fSBarry Smith   globals = mapping->globals;
545d4bb536fSBarry Smith   start   = mapping->globalstart;
546d4bb536fSBarry Smith   end     = mapping->globalend;
547d4bb536fSBarry Smith 
548d4bb536fSBarry Smith   if (type == IS_GTOLM_MASK) {
549d4bb536fSBarry Smith     if (idxout) {
550d4bb536fSBarry Smith       for (i=0; i<n; i++) {
551d4bb536fSBarry Smith         if (idx[i] < 0) idxout[i] = idx[i];
552d4bb536fSBarry Smith         else if (idx[i] < start) idxout[i] = -1;
553d4bb536fSBarry Smith         else if (idx[i] > end)   idxout[i] = -1;
554d4bb536fSBarry Smith         else                     idxout[i] = globals[idx[i] - start];
555d4bb536fSBarry Smith       }
556d4bb536fSBarry Smith     }
557d4bb536fSBarry Smith     if (nout) *nout = n;
558d4bb536fSBarry Smith   } else {
559d4bb536fSBarry Smith     if (idxout) {
560d4bb536fSBarry Smith       for (i=0; i<n; i++) {
561d4bb536fSBarry Smith         if (idx[i] < 0) continue;
562d4bb536fSBarry Smith         if (idx[i] < start) continue;
563d4bb536fSBarry Smith         if (idx[i] > end) continue;
564d4bb536fSBarry Smith         tmp = globals[idx[i] - start];
565d4bb536fSBarry Smith         if (tmp < 0) continue;
566d4bb536fSBarry Smith         idxout[nf++] = tmp;
567d4bb536fSBarry Smith       }
568d4bb536fSBarry Smith     } else {
569d4bb536fSBarry Smith       for (i=0; i<n; i++) {
570d4bb536fSBarry Smith         if (idx[i] < 0) continue;
571d4bb536fSBarry Smith         if (idx[i] < start) continue;
572d4bb536fSBarry Smith         if (idx[i] > end) continue;
573d4bb536fSBarry Smith         tmp = globals[idx[i] - start];
574d4bb536fSBarry Smith         if (tmp < 0) continue;
575d4bb536fSBarry Smith         nf++;
576d4bb536fSBarry Smith       }
577d4bb536fSBarry Smith     }
578d4bb536fSBarry Smith     if (nout) *nout = nf;
579d4bb536fSBarry Smith   }
5803a40ed3dSBarry Smith   PetscFunctionReturn(0);
581d4bb536fSBarry Smith }
58290f02eecSBarry Smith 
5834a2ae208SSatish Balay #undef __FUNCT__
5844a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingGetInfo"
58589d82c54SBarry Smith /*@C
58689d82c54SBarry Smith     ISLocalToGlobalMappingGetInfo - Gets the neighbor information for each processor and
58789d82c54SBarry Smith      each index shared by more than one processor
58889d82c54SBarry Smith 
58989d82c54SBarry Smith     Collective on ISLocalToGlobalMapping
59089d82c54SBarry Smith 
59189d82c54SBarry Smith     Input Parameters:
59289d82c54SBarry Smith .   mapping - the mapping from local to global indexing
59389d82c54SBarry Smith 
59489d82c54SBarry Smith     Output Parameter:
59589d82c54SBarry Smith +   nproc - number of processors that are connected to this one
59689d82c54SBarry Smith .   proc - neighboring processors
59707b52d57SBarry Smith .   numproc - number of indices for each subdomain (processor)
5983463a7baSJed Brown -   indices - indices of nodes (in local numbering) shared with neighbors (sorted by global numbering)
59989d82c54SBarry Smith 
60089d82c54SBarry Smith     Level: advanced
60189d82c54SBarry Smith 
602273d9f13SBarry Smith     Concepts: mapping^local to global
60389d82c54SBarry Smith 
6042cfcea29SBarry Smith     Fortran Usage:
6052cfcea29SBarry Smith $        ISLocalToGlobalMpngGetInfoSize(ISLocalToGlobalMapping,PetscInt nproc,PetscInt numprocmax,ierr) followed by
6062cfcea29SBarry Smith $        ISLocalToGlobalMappingGetInfo(ISLocalToGlobalMapping,PetscInt nproc, PetscInt procs[nproc],PetscInt numprocs[nproc],
6072cfcea29SBarry Smith           PetscInt indices[nproc][numprocmax],ierr)
6082cfcea29SBarry Smith         There is no ISLocalToGlobalMappingRestoreInfo() in Fortran. You must make sure that procs[], numprocs[] and
6092cfcea29SBarry Smith         indices[][] are large enough arrays, either by allocating them dynamically or defining static ones large enough.
6102cfcea29SBarry Smith 
6112cfcea29SBarry Smith 
61207b52d57SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreate(),
61307b52d57SBarry Smith           ISLocalToGlobalMappingRestoreInfo()
61489d82c54SBarry Smith @*/
6157087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingGetInfo(ISLocalToGlobalMapping mapping,PetscInt *nproc,PetscInt *procs[],PetscInt *numprocs[],PetscInt **indices[])
61689d82c54SBarry Smith {
6176849ba73SBarry Smith   PetscErrorCode ierr;
61897f1f81fSBarry Smith   PetscMPIInt    size,rank,tag1,tag2,tag3,*len,*source,imdex;
61932dcc486SBarry Smith   PetscInt       i,n = mapping->n,Ng,ng,max = 0,*lindices = mapping->indices;
62032dcc486SBarry Smith   PetscInt       *nprocs,*owner,nsends,*sends,j,*starts,nmax,nrecvs,*recvs,proc;
62197f1f81fSBarry Smith   PetscInt       cnt,scale,*ownedsenders,*nownedsenders,rstart,nowned;
62232dcc486SBarry Smith   PetscInt       node,nownedm,nt,*sends2,nsends2,*starts2,*lens2,*dest,nrecvs2,*starts3,*recvs2,k,*bprocs,*tmp;
62332dcc486SBarry Smith   PetscInt       first_procs,first_numprocs,*first_indices;
62489d82c54SBarry Smith   MPI_Request    *recv_waits,*send_waits;
62530dcb7c9SBarry Smith   MPI_Status     recv_status,*send_status,*recv_statuses;
626ce94432eSBarry Smith   MPI_Comm       comm;
627ace3abfcSBarry Smith   PetscBool      debug = PETSC_FALSE;
62889d82c54SBarry Smith 
62989d82c54SBarry Smith   PetscFunctionBegin;
6300700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
631ce94432eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)mapping,&comm);CHKERRQ(ierr);
63224cf384cSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
63324cf384cSBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
63424cf384cSBarry Smith   if (size == 1) {
63524cf384cSBarry Smith     *nproc         = 0;
6360298fd71SBarry Smith     *procs         = NULL;
63732dcc486SBarry Smith     ierr           = PetscMalloc(sizeof(PetscInt),numprocs);CHKERRQ(ierr);
6381e2105dcSBarry Smith     (*numprocs)[0] = 0;
63932dcc486SBarry Smith     ierr           = PetscMalloc(sizeof(PetscInt*),indices);CHKERRQ(ierr);
6400298fd71SBarry Smith     (*indices)[0]  = NULL;
64124cf384cSBarry Smith     PetscFunctionReturn(0);
64224cf384cSBarry Smith   }
64324cf384cSBarry Smith 
6440298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-islocaltoglobalmappinggetinfo_debug",&debug,NULL);CHKERRQ(ierr);
64507b52d57SBarry Smith 
6463677ff5aSBarry Smith   /*
6473677ff5aSBarry Smith     Notes on ISLocalToGlobalMappingGetInfo
6483677ff5aSBarry Smith 
6493677ff5aSBarry Smith     globally owned node - the nodes that have been assigned to this processor in global
6503677ff5aSBarry Smith            numbering, just for this routine.
6513677ff5aSBarry Smith 
6523677ff5aSBarry Smith     nontrivial globally owned node - node assigned to this processor that is on a subdomain
6533677ff5aSBarry Smith            boundary (i.e. is has more than one local owner)
6543677ff5aSBarry Smith 
6553677ff5aSBarry Smith     locally owned node - node that exists on this processors subdomain
6563677ff5aSBarry Smith 
6573677ff5aSBarry Smith     nontrivial locally owned node - node that is not in the interior (i.e. has more than one
6583677ff5aSBarry Smith            local subdomain
6593677ff5aSBarry Smith   */
66024cf384cSBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mapping,&tag1);CHKERRQ(ierr);
66124cf384cSBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mapping,&tag2);CHKERRQ(ierr);
66224cf384cSBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mapping,&tag3);CHKERRQ(ierr);
66389d82c54SBarry Smith 
66489d82c54SBarry Smith   for (i=0; i<n; i++) {
66589d82c54SBarry Smith     if (lindices[i] > max) max = lindices[i];
66689d82c54SBarry Smith   }
66732dcc486SBarry Smith   ierr   = MPI_Allreduce(&max,&Ng,1,MPIU_INT,MPI_MAX,comm);CHKERRQ(ierr);
66878058e43SBarry Smith   Ng++;
66989d82c54SBarry Smith   ierr   = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
67089d82c54SBarry Smith   ierr   = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
671bc8ff85bSBarry Smith   scale  = Ng/size + 1;
672a2e34c3dSBarry Smith   ng     = scale; if (rank == size-1) ng = Ng - scale*(size-1); ng = PetscMax(1,ng);
673caba0dd0SBarry Smith   rstart = scale*rank;
67489d82c54SBarry Smith 
67589d82c54SBarry Smith   /* determine ownership ranges of global indices */
6767c334f02SBarry Smith   ierr = PetscMalloc(2*size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr);
67732dcc486SBarry Smith   ierr = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr);
67889d82c54SBarry Smith 
67989d82c54SBarry Smith   /* determine owners of each local node  */
6807c334f02SBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&owner);CHKERRQ(ierr);
68189d82c54SBarry Smith   for (i=0; i<n; i++) {
6823677ff5aSBarry Smith     proc             = lindices[i]/scale; /* processor that globally owns this index */
68327c402fcSBarry Smith     nprocs[2*proc+1] = 1;                 /* processor globally owns at least one of ours */
6843677ff5aSBarry Smith     owner[i]         = proc;
68527c402fcSBarry Smith     nprocs[2*proc]++;                     /* count of how many that processor globally owns of ours */
68689d82c54SBarry Smith   }
68727c402fcSBarry Smith   nsends = 0; for (i=0; i<size; i++) nsends += nprocs[2*i+1];
6881e2582c4SBarry Smith   ierr = PetscInfo1(mapping,"Number of global owners for my local data %d\n",nsends);CHKERRQ(ierr);
68989d82c54SBarry Smith 
69089d82c54SBarry Smith   /* inform other processors of number of messages and max length*/
69127c402fcSBarry Smith   ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr);
6921e2582c4SBarry Smith   ierr = PetscInfo1(mapping,"Number of local owners for my global data %d\n",nrecvs);CHKERRQ(ierr);
69389d82c54SBarry Smith 
69489d82c54SBarry Smith   /* post receives for owned rows */
69532dcc486SBarry Smith   ierr = PetscMalloc((2*nrecvs+1)*(nmax+1)*sizeof(PetscInt),&recvs);CHKERRQ(ierr);
696b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
69789d82c54SBarry Smith   for (i=0; i<nrecvs; i++) {
69832dcc486SBarry Smith     ierr = MPI_Irecv(recvs+2*nmax*i,2*nmax,MPIU_INT,MPI_ANY_SOURCE,tag1,comm,recv_waits+i);CHKERRQ(ierr);
69989d82c54SBarry Smith   }
70089d82c54SBarry Smith 
70189d82c54SBarry Smith   /* pack messages containing lists of local nodes to owners */
70232dcc486SBarry Smith   ierr      = PetscMalloc((2*n+1)*sizeof(PetscInt),&sends);CHKERRQ(ierr);
70332dcc486SBarry Smith   ierr      = PetscMalloc((size+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr);
70489d82c54SBarry Smith   starts[0] = 0;
705f6e5521dSKarl Rupp   for (i=1; i<size; i++) starts[i] = starts[i-1] + 2*nprocs[2*i-2];
70689d82c54SBarry Smith   for (i=0; i<n; i++) {
70789d82c54SBarry Smith     sends[starts[owner[i]]++] = lindices[i];
70830dcb7c9SBarry Smith     sends[starts[owner[i]]++] = i;
70989d82c54SBarry Smith   }
71089d82c54SBarry Smith   ierr = PetscFree(owner);CHKERRQ(ierr);
71189d82c54SBarry Smith   starts[0] = 0;
712f6e5521dSKarl Rupp   for (i=1; i<size; i++) starts[i] = starts[i-1] + 2*nprocs[2*i-2];
71389d82c54SBarry Smith 
71489d82c54SBarry Smith   /* send the messages */
715b0a32e0cSBarry Smith   ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr);
71632dcc486SBarry Smith   ierr = PetscMalloc((nsends+1)*sizeof(PetscInt),&dest);CHKERRQ(ierr);
71789d82c54SBarry Smith   cnt = 0;
71889d82c54SBarry Smith   for (i=0; i<size; i++) {
71927c402fcSBarry Smith     if (nprocs[2*i]) {
72032dcc486SBarry Smith       ierr      = MPI_Isend(sends+starts[i],2*nprocs[2*i],MPIU_INT,i,tag1,comm,send_waits+cnt);CHKERRQ(ierr);
72130dcb7c9SBarry Smith       dest[cnt] = i;
72289d82c54SBarry Smith       cnt++;
72389d82c54SBarry Smith     }
72489d82c54SBarry Smith   }
72589d82c54SBarry Smith   ierr = PetscFree(starts);CHKERRQ(ierr);
72689d82c54SBarry Smith 
72789d82c54SBarry Smith   /* wait on receives */
72897f1f81fSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(PetscMPIInt),&source);CHKERRQ(ierr);
72997f1f81fSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(PetscMPIInt),&len);CHKERRQ(ierr);
73089d82c54SBarry Smith   cnt  = nrecvs;
73132dcc486SBarry Smith   ierr = PetscMalloc((ng+1)*sizeof(PetscInt),&nownedsenders);CHKERRQ(ierr);
73232dcc486SBarry Smith   ierr = PetscMemzero(nownedsenders,ng*sizeof(PetscInt));CHKERRQ(ierr);
73389d82c54SBarry Smith   while (cnt) {
73489d82c54SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
73589d82c54SBarry Smith     /* unpack receives into our local space */
73632dcc486SBarry Smith     ierr          = MPI_Get_count(&recv_status,MPIU_INT,&len[imdex]);CHKERRQ(ierr);
73789d82c54SBarry Smith     source[imdex] = recv_status.MPI_SOURCE;
73830dcb7c9SBarry Smith     len[imdex]    = len[imdex]/2;
739caba0dd0SBarry Smith     /* count how many local owners for each of my global owned indices */
74030dcb7c9SBarry Smith     for (i=0; i<len[imdex]; i++) nownedsenders[recvs[2*imdex*nmax+2*i]-rstart]++;
74189d82c54SBarry Smith     cnt--;
74289d82c54SBarry Smith   }
74389d82c54SBarry Smith   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
74489d82c54SBarry Smith 
74530dcb7c9SBarry Smith   /* count how many globally owned indices are on an edge multiplied by how many processors own them. */
746bc8ff85bSBarry Smith   nowned  = 0;
747bc8ff85bSBarry Smith   nownedm = 0;
748bc8ff85bSBarry Smith   for (i=0; i<ng; i++) {
749bc8ff85bSBarry Smith     if (nownedsenders[i] > 1) {nownedm += nownedsenders[i]; nowned++;}
750bc8ff85bSBarry Smith   }
751bc8ff85bSBarry Smith 
752bc8ff85bSBarry Smith   /* create single array to contain rank of all local owners of each globally owned index */
75332dcc486SBarry Smith   ierr      = PetscMalloc((nownedm+1)*sizeof(PetscInt),&ownedsenders);CHKERRQ(ierr);
75432dcc486SBarry Smith   ierr      = PetscMalloc((ng+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr);
755bc8ff85bSBarry Smith   starts[0] = 0;
756bc8ff85bSBarry Smith   for (i=1; i<ng; i++) {
757bc8ff85bSBarry Smith     if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1];
758bc8ff85bSBarry Smith     else starts[i] = starts[i-1];
759bc8ff85bSBarry Smith   }
760bc8ff85bSBarry Smith 
76130dcb7c9SBarry Smith   /* for each nontrival globally owned node list all arriving processors */
762bc8ff85bSBarry Smith   for (i=0; i<nrecvs; i++) {
763bc8ff85bSBarry Smith     for (j=0; j<len[i]; j++) {
76430dcb7c9SBarry Smith       node = recvs[2*i*nmax+2*j]-rstart;
765f6e5521dSKarl Rupp       if (nownedsenders[node] > 1) ownedsenders[starts[node]++] = source[i];
766bc8ff85bSBarry Smith     }
767bc8ff85bSBarry Smith   }
768bc8ff85bSBarry Smith 
76907b52d57SBarry Smith   if (debug) { /* -----------------------------------  */
77030dcb7c9SBarry Smith     starts[0] = 0;
77130dcb7c9SBarry Smith     for (i=1; i<ng; i++) {
77230dcb7c9SBarry Smith       if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1];
77330dcb7c9SBarry Smith       else starts[i] = starts[i-1];
77430dcb7c9SBarry Smith     }
77530dcb7c9SBarry Smith     for (i=0; i<ng; i++) {
77630dcb7c9SBarry Smith       if (nownedsenders[i] > 1) {
77730dcb7c9SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"[%d] global node %d local owner processors: ",rank,i+rstart);CHKERRQ(ierr);
77830dcb7c9SBarry Smith         for (j=0; j<nownedsenders[i]; j++) {
77930dcb7c9SBarry Smith           ierr = PetscSynchronizedPrintf(comm,"%d ",ownedsenders[starts[i]+j]);CHKERRQ(ierr);
78030dcb7c9SBarry Smith         }
78130dcb7c9SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"\n");CHKERRQ(ierr);
78230dcb7c9SBarry Smith       }
78330dcb7c9SBarry Smith     }
78430dcb7c9SBarry Smith     ierr = PetscSynchronizedFlush(comm);CHKERRQ(ierr);
78507b52d57SBarry Smith   } /* -----------------------------------  */
78630dcb7c9SBarry Smith 
7873677ff5aSBarry Smith   /* wait on original sends */
7883a96401aSBarry Smith   if (nsends) {
789b0a32e0cSBarry Smith     ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
7903a96401aSBarry Smith     ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
7913a96401aSBarry Smith     ierr = PetscFree(send_status);CHKERRQ(ierr);
7923a96401aSBarry Smith   }
79389d82c54SBarry Smith   ierr = PetscFree(send_waits);CHKERRQ(ierr);
7943a96401aSBarry Smith   ierr = PetscFree(sends);CHKERRQ(ierr);
7953677ff5aSBarry Smith   ierr = PetscFree(nprocs);CHKERRQ(ierr);
7963677ff5aSBarry Smith 
7973677ff5aSBarry Smith   /* pack messages to send back to local owners */
79830dcb7c9SBarry Smith   starts[0] = 0;
79930dcb7c9SBarry Smith   for (i=1; i<ng; i++) {
80030dcb7c9SBarry Smith     if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1];
80130dcb7c9SBarry Smith     else starts[i] = starts[i-1];
80230dcb7c9SBarry Smith   }
80330dcb7c9SBarry Smith   nsends2 = nrecvs;
80432dcc486SBarry Smith   ierr    = PetscMalloc((nsends2+1)*sizeof(PetscInt),&nprocs);CHKERRQ(ierr); /* length of each message */
80530dcb7c9SBarry Smith   for (i=0; i<nrecvs; i++) {
80630dcb7c9SBarry Smith     nprocs[i] = 1;
80730dcb7c9SBarry Smith     for (j=0; j<len[i]; j++) {
80830dcb7c9SBarry Smith       node = recvs[2*i*nmax+2*j]-rstart;
809f6e5521dSKarl Rupp       if (nownedsenders[node] > 1) nprocs[i] += 2 + nownedsenders[node];
81030dcb7c9SBarry Smith     }
81130dcb7c9SBarry Smith   }
812f6e5521dSKarl Rupp   nt = 0;
813f6e5521dSKarl Rupp   for (i=0; i<nsends2; i++) nt += nprocs[i];
814f6e5521dSKarl Rupp 
81532dcc486SBarry Smith   ierr = PetscMalloc((nt+1)*sizeof(PetscInt),&sends2);CHKERRQ(ierr);
81632dcc486SBarry Smith   ierr = PetscMalloc((nsends2+1)*sizeof(PetscInt),&starts2);CHKERRQ(ierr);
817f6e5521dSKarl Rupp 
818f6e5521dSKarl Rupp   starts2[0] = 0;
819f6e5521dSKarl Rupp   for (i=1; i<nsends2; i++) starts2[i] = starts2[i-1] + nprocs[i-1];
82030dcb7c9SBarry Smith   /*
82130dcb7c9SBarry Smith      Each message is 1 + nprocs[i] long, and consists of
82230dcb7c9SBarry Smith        (0) the number of nodes being sent back
82330dcb7c9SBarry Smith        (1) the local node number,
82430dcb7c9SBarry Smith        (2) the number of processors sharing it,
82530dcb7c9SBarry Smith        (3) the processors sharing it
82630dcb7c9SBarry Smith   */
82730dcb7c9SBarry Smith   for (i=0; i<nsends2; i++) {
82830dcb7c9SBarry Smith     cnt = 1;
82930dcb7c9SBarry Smith     sends2[starts2[i]] = 0;
83030dcb7c9SBarry Smith     for (j=0; j<len[i]; j++) {
83130dcb7c9SBarry Smith       node = recvs[2*i*nmax+2*j]-rstart;
83230dcb7c9SBarry Smith       if (nownedsenders[node] > 1) {
83330dcb7c9SBarry Smith         sends2[starts2[i]]++;
83430dcb7c9SBarry Smith         sends2[starts2[i]+cnt++] = recvs[2*i*nmax+2*j+1];
83530dcb7c9SBarry Smith         sends2[starts2[i]+cnt++] = nownedsenders[node];
83632dcc486SBarry Smith         ierr = PetscMemcpy(&sends2[starts2[i]+cnt],&ownedsenders[starts[node]],nownedsenders[node]*sizeof(PetscInt));CHKERRQ(ierr);
83730dcb7c9SBarry Smith         cnt += nownedsenders[node];
83830dcb7c9SBarry Smith       }
83930dcb7c9SBarry Smith     }
84030dcb7c9SBarry Smith   }
84130dcb7c9SBarry Smith 
84230dcb7c9SBarry Smith   /* receive the message lengths */
84330dcb7c9SBarry Smith   nrecvs2 = nsends;
84432dcc486SBarry Smith   ierr    = PetscMalloc((nrecvs2+1)*sizeof(PetscInt),&lens2);CHKERRQ(ierr);
84532dcc486SBarry Smith   ierr    = PetscMalloc((nrecvs2+1)*sizeof(PetscInt),&starts3);CHKERRQ(ierr);
846d44834fbSBarry Smith   ierr    = PetscMalloc((nrecvs2+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
84730dcb7c9SBarry Smith   for (i=0; i<nrecvs2; i++) {
848d44834fbSBarry Smith     ierr = MPI_Irecv(&lens2[i],1,MPIU_INT,dest[i],tag2,comm,recv_waits+i);CHKERRQ(ierr);
84930dcb7c9SBarry Smith   }
850d44834fbSBarry Smith 
8518a8e0b3aSBarry Smith   /* send the message lengths */
8528a8e0b3aSBarry Smith   for (i=0; i<nsends2; i++) {
8538a8e0b3aSBarry Smith     ierr = MPI_Send(&nprocs[i],1,MPIU_INT,source[i],tag2,comm);CHKERRQ(ierr);
8548a8e0b3aSBarry Smith   }
8558a8e0b3aSBarry Smith 
856d44834fbSBarry Smith   /* wait on receives of lens */
8570c468ba9SBarry Smith   if (nrecvs2) {
8580c468ba9SBarry Smith     ierr = PetscMalloc(nrecvs2*sizeof(MPI_Status),&recv_statuses);CHKERRQ(ierr);
859d44834fbSBarry Smith     ierr = MPI_Waitall(nrecvs2,recv_waits,recv_statuses);CHKERRQ(ierr);
860d44834fbSBarry Smith     ierr = PetscFree(recv_statuses);CHKERRQ(ierr);
8610c468ba9SBarry Smith   }
862a2ea699eSBarry Smith   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
863d44834fbSBarry Smith 
86430dcb7c9SBarry Smith   starts3[0] = 0;
865d44834fbSBarry Smith   nt         = 0;
86630dcb7c9SBarry Smith   for (i=0; i<nrecvs2-1; i++) {
86730dcb7c9SBarry Smith     starts3[i+1] = starts3[i] + lens2[i];
868d44834fbSBarry Smith     nt          += lens2[i];
86930dcb7c9SBarry Smith   }
87076466f69SStefano Zampini   if (nrecvs2) nt += lens2[nrecvs2-1];
871d44834fbSBarry Smith 
87232dcc486SBarry Smith   ierr = PetscMalloc((nt+1)*sizeof(PetscInt),&recvs2);CHKERRQ(ierr);
873b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs2+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
87452b72c4aSBarry Smith   for (i=0; i<nrecvs2; i++) {
87532dcc486SBarry Smith     ierr = MPI_Irecv(recvs2+starts3[i],lens2[i],MPIU_INT,dest[i],tag3,comm,recv_waits+i);CHKERRQ(ierr);
87630dcb7c9SBarry Smith   }
87730dcb7c9SBarry Smith 
87830dcb7c9SBarry Smith   /* send the messages */
879b0a32e0cSBarry Smith   ierr = PetscMalloc((nsends2+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr);
88030dcb7c9SBarry Smith   for (i=0; i<nsends2; i++) {
88132dcc486SBarry Smith     ierr = MPI_Isend(sends2+starts2[i],nprocs[i],MPIU_INT,source[i],tag3,comm,send_waits+i);CHKERRQ(ierr);
88230dcb7c9SBarry Smith   }
88330dcb7c9SBarry Smith 
88430dcb7c9SBarry Smith   /* wait on receives */
8850c468ba9SBarry Smith   if (nrecvs2) {
8860c468ba9SBarry Smith     ierr = PetscMalloc(nrecvs2*sizeof(MPI_Status),&recv_statuses);CHKERRQ(ierr);
88730dcb7c9SBarry Smith     ierr = MPI_Waitall(nrecvs2,recv_waits,recv_statuses);CHKERRQ(ierr);
88830dcb7c9SBarry Smith     ierr = PetscFree(recv_statuses);CHKERRQ(ierr);
8890c468ba9SBarry Smith   }
89030dcb7c9SBarry Smith   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
89130dcb7c9SBarry Smith   ierr = PetscFree(nprocs);CHKERRQ(ierr);
89230dcb7c9SBarry Smith 
89307b52d57SBarry Smith   if (debug) { /* -----------------------------------  */
89430dcb7c9SBarry Smith     cnt = 0;
89530dcb7c9SBarry Smith     for (i=0; i<nrecvs2; i++) {
89630dcb7c9SBarry Smith       nt = recvs2[cnt++];
89730dcb7c9SBarry Smith       for (j=0; j<nt; j++) {
89830dcb7c9SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"[%d] local node %d number of subdomains %d: ",rank,recvs2[cnt],recvs2[cnt+1]);CHKERRQ(ierr);
89930dcb7c9SBarry Smith         for (k=0; k<recvs2[cnt+1]; k++) {
90030dcb7c9SBarry Smith           ierr = PetscSynchronizedPrintf(comm,"%d ",recvs2[cnt+2+k]);CHKERRQ(ierr);
90130dcb7c9SBarry Smith         }
90230dcb7c9SBarry Smith         cnt += 2 + recvs2[cnt+1];
90330dcb7c9SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"\n");CHKERRQ(ierr);
90430dcb7c9SBarry Smith       }
90530dcb7c9SBarry Smith     }
90630dcb7c9SBarry Smith     ierr = PetscSynchronizedFlush(comm);CHKERRQ(ierr);
90707b52d57SBarry Smith   } /* -----------------------------------  */
90830dcb7c9SBarry Smith 
90930dcb7c9SBarry Smith   /* count number subdomains for each local node */
91032dcc486SBarry Smith   ierr = PetscMalloc(size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr);
91132dcc486SBarry Smith   ierr = PetscMemzero(nprocs,size*sizeof(PetscInt));CHKERRQ(ierr);
91230dcb7c9SBarry Smith   cnt  = 0;
91330dcb7c9SBarry Smith   for (i=0; i<nrecvs2; i++) {
91430dcb7c9SBarry Smith     nt = recvs2[cnt++];
91530dcb7c9SBarry Smith     for (j=0; j<nt; j++) {
916f6e5521dSKarl Rupp       for (k=0; k<recvs2[cnt+1]; k++) nprocs[recvs2[cnt+2+k]]++;
91730dcb7c9SBarry Smith       cnt += 2 + recvs2[cnt+1];
91830dcb7c9SBarry Smith     }
91930dcb7c9SBarry Smith   }
92030dcb7c9SBarry Smith   nt = 0; for (i=0; i<size; i++) nt += (nprocs[i] > 0);
92130dcb7c9SBarry Smith   *nproc    = nt;
92232dcc486SBarry Smith   ierr = PetscMalloc((nt+1)*sizeof(PetscInt),procs);CHKERRQ(ierr);
92332dcc486SBarry Smith   ierr = PetscMalloc((nt+1)*sizeof(PetscInt),numprocs);CHKERRQ(ierr);
92432dcc486SBarry Smith   ierr = PetscMalloc((nt+1)*sizeof(PetscInt*),indices);CHKERRQ(ierr);
9250298fd71SBarry Smith   for (i=0;i<nt+1;i++) (*indices)[i]=NULL;
92632dcc486SBarry Smith   ierr = PetscMalloc(size*sizeof(PetscInt),&bprocs);CHKERRQ(ierr);
92730dcb7c9SBarry Smith   cnt       = 0;
92830dcb7c9SBarry Smith   for (i=0; i<size; i++) {
92930dcb7c9SBarry Smith     if (nprocs[i] > 0) {
93030dcb7c9SBarry Smith       bprocs[i]        = cnt;
93130dcb7c9SBarry Smith       (*procs)[cnt]    = i;
93230dcb7c9SBarry Smith       (*numprocs)[cnt] = nprocs[i];
93332dcc486SBarry Smith       ierr             = PetscMalloc(nprocs[i]*sizeof(PetscInt),&(*indices)[cnt]);CHKERRQ(ierr);
93430dcb7c9SBarry Smith       cnt++;
93530dcb7c9SBarry Smith     }
93630dcb7c9SBarry Smith   }
93730dcb7c9SBarry Smith 
93830dcb7c9SBarry Smith   /* make the list of subdomains for each nontrivial local node */
93932dcc486SBarry Smith   ierr = PetscMemzero(*numprocs,nt*sizeof(PetscInt));CHKERRQ(ierr);
94030dcb7c9SBarry Smith   cnt  = 0;
94130dcb7c9SBarry Smith   for (i=0; i<nrecvs2; i++) {
94230dcb7c9SBarry Smith     nt = recvs2[cnt++];
94330dcb7c9SBarry Smith     for (j=0; j<nt; j++) {
944f6e5521dSKarl Rupp       for (k=0; k<recvs2[cnt+1]; k++) (*indices)[bprocs[recvs2[cnt+2+k]]][(*numprocs)[bprocs[recvs2[cnt+2+k]]]++] = recvs2[cnt];
94530dcb7c9SBarry Smith       cnt += 2 + recvs2[cnt+1];
94630dcb7c9SBarry Smith     }
94730dcb7c9SBarry Smith   }
94830dcb7c9SBarry Smith   ierr = PetscFree(bprocs);CHKERRQ(ierr);
94907b52d57SBarry Smith   ierr = PetscFree(recvs2);CHKERRQ(ierr);
95030dcb7c9SBarry Smith 
95107b52d57SBarry Smith   /* sort the node indexing by their global numbers */
95207b52d57SBarry Smith   nt = *nproc;
95307b52d57SBarry Smith   for (i=0; i<nt; i++) {
95432dcc486SBarry Smith     ierr = PetscMalloc(((*numprocs)[i])*sizeof(PetscInt),&tmp);CHKERRQ(ierr);
955f6e5521dSKarl Rupp     for (j=0; j<(*numprocs)[i]; j++) tmp[j] = lindices[(*indices)[i][j]];
95607b52d57SBarry Smith     ierr = PetscSortIntWithArray((*numprocs)[i],tmp,(*indices)[i]);CHKERRQ(ierr);
95707b52d57SBarry Smith     ierr = PetscFree(tmp);CHKERRQ(ierr);
95807b52d57SBarry Smith   }
95907b52d57SBarry Smith 
96007b52d57SBarry Smith   if (debug) { /* -----------------------------------  */
96130dcb7c9SBarry Smith     nt = *nproc;
96230dcb7c9SBarry Smith     for (i=0; i<nt; i++) {
96330dcb7c9SBarry Smith       ierr = PetscSynchronizedPrintf(comm,"[%d] subdomain %d number of indices %d: ",rank,(*procs)[i],(*numprocs)[i]);CHKERRQ(ierr);
96430dcb7c9SBarry Smith       for (j=0; j<(*numprocs)[i]; j++) {
96530dcb7c9SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"%d ",(*indices)[i][j]);CHKERRQ(ierr);
96630dcb7c9SBarry Smith       }
96730dcb7c9SBarry Smith       ierr = PetscSynchronizedPrintf(comm,"\n");CHKERRQ(ierr);
96830dcb7c9SBarry Smith     }
96930dcb7c9SBarry Smith     ierr = PetscSynchronizedFlush(comm);CHKERRQ(ierr);
97007b52d57SBarry Smith   } /* -----------------------------------  */
97130dcb7c9SBarry Smith 
97230dcb7c9SBarry Smith   /* wait on sends */
97330dcb7c9SBarry Smith   if (nsends2) {
974b0a32e0cSBarry Smith     ierr = PetscMalloc(nsends2*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
97530dcb7c9SBarry Smith     ierr = MPI_Waitall(nsends2,send_waits,send_status);CHKERRQ(ierr);
97630dcb7c9SBarry Smith     ierr = PetscFree(send_status);CHKERRQ(ierr);
97730dcb7c9SBarry Smith   }
97830dcb7c9SBarry Smith 
97930dcb7c9SBarry Smith   ierr = PetscFree(starts3);CHKERRQ(ierr);
98030dcb7c9SBarry Smith   ierr = PetscFree(dest);CHKERRQ(ierr);
98130dcb7c9SBarry Smith   ierr = PetscFree(send_waits);CHKERRQ(ierr);
9823677ff5aSBarry Smith 
983bc8ff85bSBarry Smith   ierr = PetscFree(nownedsenders);CHKERRQ(ierr);
984bc8ff85bSBarry Smith   ierr = PetscFree(ownedsenders);CHKERRQ(ierr);
985bc8ff85bSBarry Smith   ierr = PetscFree(starts);CHKERRQ(ierr);
98630dcb7c9SBarry Smith   ierr = PetscFree(starts2);CHKERRQ(ierr);
98730dcb7c9SBarry Smith   ierr = PetscFree(lens2);CHKERRQ(ierr);
98889d82c54SBarry Smith 
98989d82c54SBarry Smith   ierr = PetscFree(source);CHKERRQ(ierr);
99097f1f81fSBarry Smith   ierr = PetscFree(len);CHKERRQ(ierr);
99189d82c54SBarry Smith   ierr = PetscFree(recvs);CHKERRQ(ierr);
9923a96401aSBarry Smith   ierr = PetscFree(nprocs);CHKERRQ(ierr);
99330dcb7c9SBarry Smith   ierr = PetscFree(sends2);CHKERRQ(ierr);
99424cf384cSBarry Smith 
99524cf384cSBarry Smith   /* put the information about myself as the first entry in the list */
99624cf384cSBarry Smith   first_procs    = (*procs)[0];
99724cf384cSBarry Smith   first_numprocs = (*numprocs)[0];
99824cf384cSBarry Smith   first_indices  = (*indices)[0];
99924cf384cSBarry Smith   for (i=0; i<*nproc; i++) {
100024cf384cSBarry Smith     if ((*procs)[i] == rank) {
100124cf384cSBarry Smith       (*procs)[0]    = (*procs)[i];
100224cf384cSBarry Smith       (*numprocs)[0] = (*numprocs)[i];
100324cf384cSBarry Smith       (*indices)[0]  = (*indices)[i];
100424cf384cSBarry Smith       (*procs)[i]    = first_procs;
100524cf384cSBarry Smith       (*numprocs)[i] = first_numprocs;
100624cf384cSBarry Smith       (*indices)[i]  = first_indices;
100724cf384cSBarry Smith       break;
100824cf384cSBarry Smith     }
100924cf384cSBarry Smith   }
101089d82c54SBarry Smith   PetscFunctionReturn(0);
101189d82c54SBarry Smith }
101289d82c54SBarry Smith 
10134a2ae208SSatish Balay #undef __FUNCT__
10144a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingRestoreInfo"
101507b52d57SBarry Smith /*@C
101607b52d57SBarry Smith     ISLocalToGlobalMappingRestoreInfo - Frees the memory allocated by ISLocalToGlobalMappingGetInfo()
101789d82c54SBarry Smith 
101807b52d57SBarry Smith     Collective on ISLocalToGlobalMapping
101907b52d57SBarry Smith 
102007b52d57SBarry Smith     Input Parameters:
102107b52d57SBarry Smith .   mapping - the mapping from local to global indexing
102207b52d57SBarry Smith 
102307b52d57SBarry Smith     Output Parameter:
102407b52d57SBarry Smith +   nproc - number of processors that are connected to this one
102507b52d57SBarry Smith .   proc - neighboring processors
102607b52d57SBarry Smith .   numproc - number of indices for each processor
102707b52d57SBarry Smith -   indices - indices of local nodes shared with neighbor (sorted by global numbering)
102807b52d57SBarry Smith 
102907b52d57SBarry Smith     Level: advanced
103007b52d57SBarry Smith 
103107b52d57SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreate(),
103207b52d57SBarry Smith           ISLocalToGlobalMappingGetInfo()
103307b52d57SBarry Smith @*/
10347087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingRestoreInfo(ISLocalToGlobalMapping mapping,PetscInt *nproc,PetscInt *procs[],PetscInt *numprocs[],PetscInt **indices[])
103507b52d57SBarry Smith {
10366849ba73SBarry Smith   PetscErrorCode ierr;
103732dcc486SBarry Smith   PetscInt       i;
103807b52d57SBarry Smith 
103907b52d57SBarry Smith   PetscFunctionBegin;
104005b42c5fSBarry Smith   ierr = PetscFree(*procs);CHKERRQ(ierr);
104105b42c5fSBarry Smith   ierr = PetscFree(*numprocs);CHKERRQ(ierr);
104200ff320aSBarry Smith   if (*indices) {
104305b42c5fSBarry Smith     ierr = PetscFree((*indices)[0]);CHKERRQ(ierr);
104400ff320aSBarry Smith     for (i=1; i<*nproc; i++) {
104505b42c5fSBarry Smith       ierr = PetscFree((*indices)[i]);CHKERRQ(ierr);
104607b52d57SBarry Smith     }
104707b52d57SBarry Smith     ierr = PetscFree(*indices);CHKERRQ(ierr);
104824cf384cSBarry Smith   }
104907b52d57SBarry Smith   PetscFunctionReturn(0);
105007b52d57SBarry Smith }
105186994e45SJed Brown 
105286994e45SJed Brown #undef __FUNCT__
105386994e45SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingGetIndices"
105486994e45SJed Brown /*@C
105586994e45SJed Brown    ISLocalToGlobalMappingGetIndices - Get global indices for every local point
105686994e45SJed Brown 
105786994e45SJed Brown    Not Collective
105886994e45SJed Brown 
105986994e45SJed Brown    Input Arguments:
106086994e45SJed Brown . ltog - local to global mapping
106186994e45SJed Brown 
106286994e45SJed Brown    Output Arguments:
106386994e45SJed Brown . array - array of indices
106486994e45SJed Brown 
106586994e45SJed Brown    Level: advanced
106686994e45SJed Brown 
106786994e45SJed Brown .seealso: ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingRestoreIndices()
106886994e45SJed Brown @*/
10697087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingGetIndices(ISLocalToGlobalMapping ltog,const PetscInt **array)
107086994e45SJed Brown {
107186994e45SJed Brown   PetscFunctionBegin;
107286994e45SJed Brown   PetscValidHeaderSpecific(ltog,IS_LTOGM_CLASSID,1);
107386994e45SJed Brown   PetscValidPointer(array,2);
107486994e45SJed Brown   *array = ltog->indices;
107586994e45SJed Brown   PetscFunctionReturn(0);
107686994e45SJed Brown }
107786994e45SJed Brown 
107886994e45SJed Brown #undef __FUNCT__
107986994e45SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingRestoreIndices"
108086994e45SJed Brown /*@C
108186994e45SJed Brown    ISLocalToGlobalMappingRestoreIndices - Restore indices obtained with ISLocalToGlobalMappingRestoreIndices()
108286994e45SJed Brown 
108386994e45SJed Brown    Not Collective
108486994e45SJed Brown 
108586994e45SJed Brown    Input Arguments:
108686994e45SJed Brown + ltog - local to global mapping
108786994e45SJed Brown - array - array of indices
108886994e45SJed Brown 
108986994e45SJed Brown    Level: advanced
109086994e45SJed Brown 
109186994e45SJed Brown .seealso: ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingGetIndices()
109286994e45SJed Brown @*/
10937087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingRestoreIndices(ISLocalToGlobalMapping ltog,const PetscInt **array)
109486994e45SJed Brown {
109586994e45SJed Brown   PetscFunctionBegin;
109686994e45SJed Brown   PetscValidHeaderSpecific(ltog,IS_LTOGM_CLASSID,1);
109786994e45SJed Brown   PetscValidPointer(array,2);
109886994e45SJed Brown   if (*array != ltog->indices) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_BADPTR,"Trying to return mismatched pointer");
10990298fd71SBarry Smith   *array = NULL;
110086994e45SJed Brown   PetscFunctionReturn(0);
110186994e45SJed Brown }
1102f7efa3c7SJed Brown 
1103f7efa3c7SJed Brown #undef __FUNCT__
1104f7efa3c7SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingConcatenate"
1105f7efa3c7SJed Brown /*@C
1106f7efa3c7SJed Brown    ISLocalToGlobalMappingConcatenate - Create a new mapping that concatenates a list of mappings
1107f7efa3c7SJed Brown 
1108f7efa3c7SJed Brown    Not Collective
1109f7efa3c7SJed Brown 
1110f7efa3c7SJed Brown    Input Arguments:
1111f7efa3c7SJed Brown + comm - communicator for the new mapping, must contain the communicator of every mapping to concatenate
1112f7efa3c7SJed Brown . n - number of mappings to concatenate
1113f7efa3c7SJed Brown - ltogs - local to global mappings
1114f7efa3c7SJed Brown 
1115f7efa3c7SJed Brown    Output Arguments:
1116f7efa3c7SJed Brown . ltogcat - new mapping
1117f7efa3c7SJed Brown 
1118f7efa3c7SJed Brown    Level: advanced
1119f7efa3c7SJed Brown 
1120f7efa3c7SJed Brown .seealso: ISLocalToGlobalMappingCreate()
1121f7efa3c7SJed Brown @*/
1122f7efa3c7SJed Brown PetscErrorCode ISLocalToGlobalMappingConcatenate(MPI_Comm comm,PetscInt n,const ISLocalToGlobalMapping ltogs[],ISLocalToGlobalMapping *ltogcat)
1123f7efa3c7SJed Brown {
1124f7efa3c7SJed Brown   PetscInt       i,cnt,m,*idx;
1125f7efa3c7SJed Brown   PetscErrorCode ierr;
1126f7efa3c7SJed Brown 
1127f7efa3c7SJed Brown   PetscFunctionBegin;
1128f7efa3c7SJed Brown   if (n < 0) SETERRQ1(comm,PETSC_ERR_ARG_OUTOFRANGE,"Must have a non-negative number of mappings, given %D",n);
1129f7efa3c7SJed Brown   if (n > 0) PetscValidPointer(ltogs,3);
1130f7efa3c7SJed Brown   for (i=0; i<n; i++) PetscValidHeaderSpecific(ltogs[i],IS_LTOGM_CLASSID,3);
1131f7efa3c7SJed Brown   PetscValidPointer(ltogcat,4);
1132f7efa3c7SJed Brown   for (cnt=0,i=0; i<n; i++) {
1133f7efa3c7SJed Brown     ierr = ISLocalToGlobalMappingGetSize(ltogs[i],&m);CHKERRQ(ierr);
1134f7efa3c7SJed Brown     cnt += m;
1135f7efa3c7SJed Brown   }
1136f7efa3c7SJed Brown   ierr = PetscMalloc(cnt*sizeof(PetscInt),&idx);CHKERRQ(ierr);
1137f7efa3c7SJed Brown   for (cnt=0,i=0; i<n; i++) {
1138f7efa3c7SJed Brown     const PetscInt *subidx;
1139f7efa3c7SJed Brown     ierr = ISLocalToGlobalMappingGetSize(ltogs[i],&m);CHKERRQ(ierr);
1140f7efa3c7SJed Brown     ierr = ISLocalToGlobalMappingGetIndices(ltogs[i],&subidx);CHKERRQ(ierr);
1141f7efa3c7SJed Brown     ierr = PetscMemcpy(&idx[cnt],subidx,m*sizeof(PetscInt));CHKERRQ(ierr);
1142f7efa3c7SJed Brown     ierr = ISLocalToGlobalMappingRestoreIndices(ltogs[i],&subidx);CHKERRQ(ierr);
1143f7efa3c7SJed Brown     cnt += m;
1144f7efa3c7SJed Brown   }
1145f7efa3c7SJed Brown   ierr = ISLocalToGlobalMappingCreate(comm,cnt,idx,PETSC_OWN_POINTER,ltogcat);CHKERRQ(ierr);
1146f7efa3c7SJed Brown   PetscFunctionReturn(0);
1147f7efa3c7SJed Brown }
1148