xref: /petsc/src/vec/is/utils/isltog.c (revision ce94432eddcd14845bc7e8083b7f8ea723b9bf7d)
12362add9SBarry Smith 
2b45d2f2cSJed Brown #include <petsc-private/isimpl.h>    /*I "petscis.h"  I*/
32362add9SBarry Smith 
47087cfbeSBarry Smith PetscClassId IS_LTOGM_CLASSID;
58e58c17dSMatthew Knepley 
64a2ae208SSatish Balay #undef __FUNCT__
74a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingGetSize"
83b9aefa3SBarry Smith /*@C
93b9aefa3SBarry Smith     ISLocalToGlobalMappingGetSize - Gets the local size of a local to global mapping.
103b9aefa3SBarry Smith 
113b9aefa3SBarry Smith     Not Collective
123b9aefa3SBarry Smith 
133b9aefa3SBarry Smith     Input Parameter:
143b9aefa3SBarry Smith .   ltog - local to global mapping
153b9aefa3SBarry Smith 
163b9aefa3SBarry Smith     Output Parameter:
173b9aefa3SBarry Smith .   n - the number of entries in the local mapping
183b9aefa3SBarry Smith 
193b9aefa3SBarry Smith     Level: advanced
203b9aefa3SBarry Smith 
21273d9f13SBarry Smith     Concepts: mapping^local to global
223b9aefa3SBarry Smith 
233b9aefa3SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
243b9aefa3SBarry Smith @*/
257087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingGetSize(ISLocalToGlobalMapping mapping,PetscInt *n)
263b9aefa3SBarry Smith {
273b9aefa3SBarry Smith   PetscFunctionBegin;
280700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
294482741eSBarry Smith   PetscValidIntPointer(n,2);
303b9aefa3SBarry Smith   *n = mapping->n;
313b9aefa3SBarry Smith   PetscFunctionReturn(0);
323b9aefa3SBarry Smith }
333b9aefa3SBarry Smith 
344a2ae208SSatish Balay #undef __FUNCT__
354a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingView"
365a5d4f66SBarry Smith /*@C
375a5d4f66SBarry Smith     ISLocalToGlobalMappingView - View a local to global mapping
385a5d4f66SBarry Smith 
39b9cd556bSLois Curfman McInnes     Not Collective
40b9cd556bSLois Curfman McInnes 
415a5d4f66SBarry Smith     Input Parameters:
423b9aefa3SBarry Smith +   ltog - local to global mapping
433b9aefa3SBarry Smith -   viewer - viewer
445a5d4f66SBarry Smith 
45a997ad1aSLois Curfman McInnes     Level: advanced
46a997ad1aSLois Curfman McInnes 
47273d9f13SBarry Smith     Concepts: mapping^local to global
485a5d4f66SBarry Smith 
495a5d4f66SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
505a5d4f66SBarry Smith @*/
517087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingView(ISLocalToGlobalMapping mapping,PetscViewer viewer)
525a5d4f66SBarry Smith {
5332dcc486SBarry Smith   PetscInt       i;
5432dcc486SBarry Smith   PetscMPIInt    rank;
55ace3abfcSBarry Smith   PetscBool      iascii;
566849ba73SBarry Smith   PetscErrorCode ierr;
575a5d4f66SBarry Smith 
585a5d4f66SBarry Smith   PetscFunctionBegin;
590700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
603050cee2SBarry Smith   if (!viewer) {
61*ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)mapping),&viewer);CHKERRQ(ierr);
623050cee2SBarry Smith   }
630700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
645a5d4f66SBarry Smith 
65*ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)mapping),&rank);CHKERRQ(ierr);
66251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
6732077d6dSBarry Smith   if (iascii) {
687b23a99aSBarry Smith     ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_TRUE);CHKERRQ(ierr);
695a5d4f66SBarry Smith     for (i=0; i<mapping->n; i++) {
70b0a32e0cSBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %d %d\n",rank,i,mapping->indices[i]);CHKERRQ(ierr);
716831982aSBarry Smith     }
72b0a32e0cSBarry Smith     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
737b23a99aSBarry Smith     ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_FALSE);CHKERRQ(ierr);
747b23a99aSBarry Smith   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported for ISLocalToGlobalMapping",((PetscObject)viewer)->type_name);
755a5d4f66SBarry Smith   PetscFunctionReturn(0);
765a5d4f66SBarry Smith }
775a5d4f66SBarry Smith 
784a2ae208SSatish Balay #undef __FUNCT__
794a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingCreateIS"
801f428162SBarry Smith /*@
812bdab257SBarry Smith     ISLocalToGlobalMappingCreateIS - Creates a mapping between a local (0 to n)
822bdab257SBarry Smith     ordering and a global parallel ordering.
832bdab257SBarry Smith 
840f5bd95cSBarry Smith     Not collective
85b9cd556bSLois Curfman McInnes 
86a997ad1aSLois Curfman McInnes     Input Parameter:
878c03b21aSDmitry Karpeev .   is - index set containing the global numbers for each local number
882bdab257SBarry Smith 
89a997ad1aSLois Curfman McInnes     Output Parameter:
902bdab257SBarry Smith .   mapping - new mapping data structure
912bdab257SBarry Smith 
92a997ad1aSLois Curfman McInnes     Level: advanced
93a997ad1aSLois Curfman McInnes 
94273d9f13SBarry Smith     Concepts: mapping^local to global
952bdab257SBarry Smith 
962bdab257SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
972bdab257SBarry Smith @*/
987087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingCreateIS(IS is,ISLocalToGlobalMapping *mapping)
992bdab257SBarry Smith {
1006849ba73SBarry Smith   PetscErrorCode ierr;
1015d0c19d7SBarry Smith   PetscInt       n;
1025d0c19d7SBarry Smith   const PetscInt *indices;
1032bdab257SBarry Smith   MPI_Comm       comm;
1043a40ed3dSBarry Smith 
1053a40ed3dSBarry Smith   PetscFunctionBegin;
1060700a824SBarry Smith   PetscValidHeaderSpecific(is,IS_CLASSID,1);
1074482741eSBarry Smith   PetscValidPointer(mapping,2);
1082bdab257SBarry Smith 
1092bdab257SBarry Smith   ierr = PetscObjectGetComm((PetscObject)is,&comm);CHKERRQ(ierr);
1103b9aefa3SBarry Smith   ierr = ISGetLocalSize(is,&n);CHKERRQ(ierr);
1112bdab257SBarry Smith   ierr = ISGetIndices(is,&indices);CHKERRQ(ierr);
112d5ad8652SBarry Smith   ierr = ISLocalToGlobalMappingCreate(comm,n,indices,PETSC_COPY_VALUES,mapping);CHKERRQ(ierr);
1132bdab257SBarry Smith   ierr = ISRestoreIndices(is,&indices);CHKERRQ(ierr);
1143a40ed3dSBarry Smith   PetscFunctionReturn(0);
1152bdab257SBarry Smith }
1165a5d4f66SBarry Smith 
117a4d96a55SJed Brown #undef __FUNCT__
118a4d96a55SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingCreateSF"
119a4d96a55SJed Brown /*@C
120a4d96a55SJed Brown     ISLocalToGlobalMappingCreateSF - Creates a mapping between a local (0 to n)
121a4d96a55SJed Brown     ordering and a global parallel ordering.
122a4d96a55SJed Brown 
123a4d96a55SJed Brown     Collective
124a4d96a55SJed Brown 
125a4d96a55SJed Brown     Input Parameter:
126a4d96a55SJed Brown +   sf - star forest mapping contiguous local indices to (rank, offset)
127a4d96a55SJed Brown -   start - first global index on this process
128a4d96a55SJed Brown 
129a4d96a55SJed Brown     Output Parameter:
130a4d96a55SJed Brown .   mapping - new mapping data structure
131a4d96a55SJed Brown 
132a4d96a55SJed Brown     Level: advanced
133a4d96a55SJed Brown 
134a4d96a55SJed Brown     Concepts: mapping^local to global
135a4d96a55SJed Brown 
136a4d96a55SJed Brown .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingCreateIS()
137a4d96a55SJed Brown @*/
138a4d96a55SJed Brown PetscErrorCode ISLocalToGlobalMappingCreateSF(PetscSF sf,PetscInt start,ISLocalToGlobalMapping *mapping)
139a4d96a55SJed Brown {
140a4d96a55SJed Brown   PetscErrorCode ierr;
141a4d96a55SJed Brown   PetscInt       i,maxlocal,nroots,nleaves,*globals,*ltog;
142a4d96a55SJed Brown   const PetscInt *ilocal;
143a4d96a55SJed Brown   MPI_Comm       comm;
144a4d96a55SJed Brown 
145a4d96a55SJed Brown   PetscFunctionBegin;
146a4d96a55SJed Brown   PetscValidHeaderSpecific(sf,PETSCSF_CLASSID,1);
147a4d96a55SJed Brown   PetscValidPointer(mapping,3);
148a4d96a55SJed Brown 
149a4d96a55SJed Brown   ierr = PetscObjectGetComm((PetscObject)sf,&comm);CHKERRQ(ierr);
1500298fd71SBarry Smith   ierr = PetscSFGetGraph(sf,&nroots,&nleaves,&ilocal,NULL);CHKERRQ(ierr);
151f6e5521dSKarl Rupp   if (ilocal) {
152f6e5521dSKarl Rupp     for (i=0,maxlocal=0; i<nleaves; i++) maxlocal = PetscMax(maxlocal,ilocal[i]+1);
153f6e5521dSKarl Rupp   }
154a4d96a55SJed Brown   else maxlocal = nleaves;
155a4d96a55SJed Brown   ierr = PetscMalloc(nroots*sizeof(PetscInt),&globals);CHKERRQ(ierr);
156a4d96a55SJed Brown   ierr = PetscMalloc(maxlocal*sizeof(PetscInt),&ltog);CHKERRQ(ierr);
157a4d96a55SJed Brown   for (i=0; i<nroots; i++) globals[i] = start + i;
158a4d96a55SJed Brown   for (i=0; i<maxlocal; i++) ltog[i] = -1;
159a4d96a55SJed Brown   ierr = PetscSFBcastBegin(sf,MPIU_INT,globals,ltog);CHKERRQ(ierr);
160a4d96a55SJed Brown   ierr = PetscSFBcastEnd(sf,MPIU_INT,globals,ltog);CHKERRQ(ierr);
161a4d96a55SJed Brown   ierr = ISLocalToGlobalMappingCreate(comm,maxlocal,ltog,PETSC_OWN_POINTER,mapping);CHKERRQ(ierr);
162a4d96a55SJed Brown   ierr = PetscFree(globals);CHKERRQ(ierr);
163a4d96a55SJed Brown   PetscFunctionReturn(0);
164a4d96a55SJed Brown }
165b46b645bSBarry Smith 
1664a2ae208SSatish Balay #undef __FUNCT__
1674a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingCreate"
168ba5bb76aSSatish Balay /*@
16990f02eecSBarry Smith     ISLocalToGlobalMappingCreate - Creates a mapping between a local (0 to n)
17090f02eecSBarry Smith     ordering and a global parallel ordering.
1712362add9SBarry Smith 
17289d82c54SBarry Smith     Not Collective, but communicator may have more than one process
173b9cd556bSLois Curfman McInnes 
1742362add9SBarry Smith     Input Parameters:
17589d82c54SBarry Smith +   comm - MPI communicator
17690f02eecSBarry Smith .   n - the number of local elements
1779669e4d8SBarry Smith .   indices - the global index for each local element, these do not need to be in increasing order (sorted)
178d5ad8652SBarry Smith -   mode - see PetscCopyMode
1792362add9SBarry Smith 
180a997ad1aSLois Curfman McInnes     Output Parameter:
18190f02eecSBarry Smith .   mapping - new mapping data structure
1822362add9SBarry Smith 
183a997ad1aSLois Curfman McInnes     Level: advanced
184a997ad1aSLois Curfman McInnes 
185273d9f13SBarry Smith     Concepts: mapping^local to global
1862362add9SBarry Smith 
187d5ad8652SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS()
1882362add9SBarry Smith @*/
1897087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingCreate(MPI_Comm cm,PetscInt n,const PetscInt indices[],PetscCopyMode mode,ISLocalToGlobalMapping *mapping)
1902362add9SBarry Smith {
1916849ba73SBarry Smith   PetscErrorCode ierr;
19232dcc486SBarry Smith   PetscInt       *in;
193b46b645bSBarry Smith 
194b46b645bSBarry Smith   PetscFunctionBegin;
19573911063SBarry Smith   if (n) PetscValidIntPointer(indices,3);
1964482741eSBarry Smith   PetscValidPointer(mapping,4);
197b46b645bSBarry Smith 
1980298fd71SBarry Smith   *mapping = NULL;
199519f805aSKarl Rupp #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
2000298fd71SBarry Smith   ierr = ISInitializePackage(NULL);CHKERRQ(ierr);
2018e58c17dSMatthew Knepley #endif
2022362add9SBarry Smith 
20367c2884eSBarry Smith   ierr = PetscHeaderCreate(*mapping,_p_ISLocalToGlobalMapping,int,IS_LTOGM_CLASSID,"ISLocalToGlobalMapping","Local to global mapping","IS",
20452e6d16bSBarry Smith                            cm,ISLocalToGlobalMappingDestroy,ISLocalToGlobalMappingView);CHKERRQ(ierr);
205d4bb536fSBarry Smith   (*mapping)->n = n;
206d4bb536fSBarry Smith   /*
207d4bb536fSBarry Smith     Do not create the global to local mapping. This is only created if
208d4bb536fSBarry Smith     ISGlobalToLocalMapping() is called
209d4bb536fSBarry Smith   */
210d4bb536fSBarry Smith   (*mapping)->globals = 0;
211d5ad8652SBarry Smith   if (mode == PETSC_COPY_VALUES) {
212d5ad8652SBarry Smith     ierr = PetscMalloc(n*sizeof(PetscInt),&in);CHKERRQ(ierr);
213d5ad8652SBarry Smith     ierr = PetscMemcpy(in,indices,n*sizeof(PetscInt));CHKERRQ(ierr);
214d5ad8652SBarry Smith     ierr = PetscLogObjectMemory(*mapping,n*sizeof(PetscInt));CHKERRQ(ierr);
215d5ad8652SBarry Smith     (*mapping)->indices = in;
216f6e5521dSKarl Rupp   } else if (mode == PETSC_OWN_POINTER) (*mapping)->indices = (PetscInt*)indices;
217f6e5521dSKarl Rupp   else SETERRQ(cm,PETSC_ERR_SUP,"Cannot currently use PETSC_USE_POINTER");
2183a40ed3dSBarry Smith   PetscFunctionReturn(0);
2192362add9SBarry Smith }
2202362add9SBarry Smith 
2214a2ae208SSatish Balay #undef __FUNCT__
2224a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingBlock"
223bce096a4SSatish Balay /*@
224323b833fSBarry Smith     ISLocalToGlobalMappingBlock - Creates a blocked index version of an
225323b833fSBarry Smith        ISLocalToGlobalMapping that is appropriate for MatSetLocalToGlobalMappingBlock()
226323b833fSBarry Smith        and VecSetLocalToGlobalMappingBlock().
227323b833fSBarry Smith 
228323b833fSBarry Smith     Not Collective, but communicator may have more than one process
229323b833fSBarry Smith 
230323b833fSBarry Smith     Input Parameters:
231323b833fSBarry Smith +    inmap - original point-wise mapping
232323b833fSBarry Smith -    bs - block size
233323b833fSBarry Smith 
234323b833fSBarry Smith     Output Parameter:
23569eb54c3SBarry Smith .   outmap - block based mapping; the indices are relative to BLOCKS, not individual vector or matrix entries.
236323b833fSBarry Smith 
237323b833fSBarry Smith     Level: advanced
238323b833fSBarry Smith 
239323b833fSBarry Smith     Concepts: mapping^local to global
240323b833fSBarry Smith 
241323b833fSBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingCreateIS()
242323b833fSBarry Smith @*/
2437087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingBlock(ISLocalToGlobalMapping inmap,PetscInt bs,ISLocalToGlobalMapping *outmap)
244323b833fSBarry Smith {
2456849ba73SBarry Smith   PetscErrorCode ierr;
24632dcc486SBarry Smith   PetscInt       *ii,i,n;
247323b833fSBarry Smith 
248323b833fSBarry Smith   PetscFunctionBegin;
2490700a824SBarry Smith   PetscValidHeaderSpecific(inmap,IS_LTOGM_CLASSID,1);
250b2beed0aSJed Brown   PetscValidPointer(outmap,3);
251323b833fSBarry Smith   if (bs > 1) {
252323b833fSBarry Smith     n = inmap->n/bs;
253e32f2f54SBarry Smith     if (n*bs != inmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Pointwise mapping length is not divisible by block size");
25432dcc486SBarry Smith     ierr = PetscMalloc(n*sizeof(PetscInt),&ii);CHKERRQ(ierr);
255f6e5521dSKarl Rupp     for (i=0; i<n; i++) ii[i] = inmap->indices[bs*i]/bs;
256*ce94432eSBarry Smith     ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)inmap),n,ii,PETSC_OWN_POINTER,outmap);CHKERRQ(ierr);
257323b833fSBarry Smith   } else {
258323b833fSBarry Smith     ierr    = PetscObjectReference((PetscObject)inmap);CHKERRQ(ierr);
259c3122656SLisandro Dalcin     *outmap = inmap;
260323b833fSBarry Smith   }
261323b833fSBarry Smith   PetscFunctionReturn(0);
262323b833fSBarry Smith }
263323b833fSBarry Smith 
2644a2ae208SSatish Balay #undef __FUNCT__
2658ab951edSJed Brown #define __FUNCT__ "ISLocalToGlobalMappingUnBlock"
266b2beed0aSJed Brown /*@
267b2beed0aSJed Brown     ISLocalToGlobalMappingUnBlock - Creates a scalar index version of a blocked
268b2beed0aSJed Brown        ISLocalToGlobalMapping
269b2beed0aSJed Brown 
270b2beed0aSJed Brown     Not Collective, but communicator may have more than one process
271b2beed0aSJed Brown 
272b2beed0aSJed Brown     Input Parameter:
273b2beed0aSJed Brown + inmap - block based mapping; the indices are relative to BLOCKS, not individual vector or matrix entries.
274b2beed0aSJed Brown - bs - block size
275b2beed0aSJed Brown 
276b2beed0aSJed Brown     Output Parameter:
277b2beed0aSJed Brown .   outmap - pointwise mapping
278b2beed0aSJed Brown 
279b2beed0aSJed Brown     Level: advanced
280b2beed0aSJed Brown 
281b2beed0aSJed Brown     Concepts: mapping^local to global
282b2beed0aSJed Brown 
283b2beed0aSJed Brown .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingBlock()
284b2beed0aSJed Brown @*/
2857087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingUnBlock(ISLocalToGlobalMapping inmap,PetscInt bs,ISLocalToGlobalMapping *outmap)
286b2beed0aSJed Brown {
287b2beed0aSJed Brown   PetscErrorCode ierr;
288b2beed0aSJed Brown   PetscInt       *ii,i,n;
289b2beed0aSJed Brown 
290b2beed0aSJed Brown   PetscFunctionBegin;
291b2beed0aSJed Brown   PetscValidHeaderSpecific(inmap,IS_LTOGM_CLASSID,1);
292b2beed0aSJed Brown   PetscValidPointer(outmap,2);
293b2beed0aSJed Brown   if (bs > 1) {
294b2beed0aSJed Brown     n    = inmap->n*bs;
295b2beed0aSJed Brown     ierr = PetscMalloc(n*sizeof(PetscInt),&ii);CHKERRQ(ierr);
296f6e5521dSKarl Rupp     for (i=0; i<n; i++) ii[i] = inmap->indices[i/bs]*bs + (i%bs);
297*ce94432eSBarry Smith     ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)inmap),n,ii,PETSC_OWN_POINTER,outmap);CHKERRQ(ierr);
298b2beed0aSJed Brown   } else {
299b2beed0aSJed Brown     ierr    = PetscObjectReference((PetscObject)inmap);CHKERRQ(ierr);
300b2beed0aSJed Brown     *outmap = inmap;
301b2beed0aSJed Brown   }
302b2beed0aSJed Brown   PetscFunctionReturn(0);
303b2beed0aSJed Brown }
304b2beed0aSJed Brown 
305b2beed0aSJed Brown #undef __FUNCT__
3064a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingDestroy"
30790f02eecSBarry Smith /*@
30890f02eecSBarry Smith    ISLocalToGlobalMappingDestroy - Destroys a mapping between a local (0 to n)
30990f02eecSBarry Smith    ordering and a global parallel ordering.
31090f02eecSBarry Smith 
3110f5bd95cSBarry Smith    Note Collective
312b9cd556bSLois Curfman McInnes 
31390f02eecSBarry Smith    Input Parameters:
31490f02eecSBarry Smith .  mapping - mapping data structure
31590f02eecSBarry Smith 
316a997ad1aSLois Curfman McInnes    Level: advanced
317a997ad1aSLois Curfman McInnes 
3183acfe500SLois Curfman McInnes .seealso: ISLocalToGlobalMappingCreate()
31990f02eecSBarry Smith @*/
3206bf464f9SBarry Smith PetscErrorCode  ISLocalToGlobalMappingDestroy(ISLocalToGlobalMapping *mapping)
32190f02eecSBarry Smith {
322dfbe8321SBarry Smith   PetscErrorCode ierr;
3235fd66863SKarl Rupp 
3243a40ed3dSBarry Smith   PetscFunctionBegin;
3256bf464f9SBarry Smith   if (!*mapping) PetscFunctionReturn(0);
3266bf464f9SBarry Smith   PetscValidHeaderSpecific((*mapping),IS_LTOGM_CLASSID,1);
327997056adSBarry Smith   if (--((PetscObject)(*mapping))->refct > 0) {*mapping = 0;PetscFunctionReturn(0);}
3286bf464f9SBarry Smith   ierr     = PetscFree((*mapping)->indices);CHKERRQ(ierr);
3296bf464f9SBarry Smith   ierr     = PetscFree((*mapping)->globals);CHKERRQ(ierr);
330d38fa0fbSBarry Smith   ierr     = PetscHeaderDestroy(mapping);CHKERRQ(ierr);
331992144d0SBarry Smith   *mapping = 0;
3323a40ed3dSBarry Smith   PetscFunctionReturn(0);
33390f02eecSBarry Smith }
33490f02eecSBarry Smith 
3354a2ae208SSatish Balay #undef __FUNCT__
3364a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingApplyIS"
33790f02eecSBarry Smith /*@
3383acfe500SLois Curfman McInnes     ISLocalToGlobalMappingApplyIS - Creates from an IS in the local numbering
3393acfe500SLois Curfman McInnes     a new index set using the global numbering defined in an ISLocalToGlobalMapping
3403acfe500SLois Curfman McInnes     context.
34190f02eecSBarry Smith 
342b9cd556bSLois Curfman McInnes     Not collective
343b9cd556bSLois Curfman McInnes 
34490f02eecSBarry Smith     Input Parameters:
345b9cd556bSLois Curfman McInnes +   mapping - mapping between local and global numbering
346b9cd556bSLois Curfman McInnes -   is - index set in local numbering
34790f02eecSBarry Smith 
34890f02eecSBarry Smith     Output Parameters:
34990f02eecSBarry Smith .   newis - index set in global numbering
35090f02eecSBarry Smith 
351a997ad1aSLois Curfman McInnes     Level: advanced
352a997ad1aSLois Curfman McInnes 
353273d9f13SBarry Smith     Concepts: mapping^local to global
3543acfe500SLois Curfman McInnes 
35590f02eecSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),
356d4bb536fSBarry Smith           ISLocalToGlobalMappingDestroy(), ISGlobalToLocalMappingApply()
35790f02eecSBarry Smith @*/
3587087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingApplyIS(ISLocalToGlobalMapping mapping,IS is,IS *newis)
35990f02eecSBarry Smith {
3606849ba73SBarry Smith   PetscErrorCode ierr;
3615d0c19d7SBarry Smith   PetscInt       n,i,*idxmap,*idxout,Nmax = mapping->n;
3625d0c19d7SBarry Smith   const PetscInt *idxin;
3633a40ed3dSBarry Smith 
3643a40ed3dSBarry Smith   PetscFunctionBegin;
3650700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
3660700a824SBarry Smith   PetscValidHeaderSpecific(is,IS_CLASSID,2);
3674482741eSBarry Smith   PetscValidPointer(newis,3);
36890f02eecSBarry Smith 
3693b9aefa3SBarry Smith   ierr   = ISGetLocalSize(is,&n);CHKERRQ(ierr);
37090f02eecSBarry Smith   ierr   = ISGetIndices(is,&idxin);CHKERRQ(ierr);
37190f02eecSBarry Smith   idxmap = mapping->indices;
37290f02eecSBarry Smith 
3737c334f02SBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&idxout);CHKERRQ(ierr);
37490f02eecSBarry Smith   for (i=0; i<n; i++) {
375e32f2f54SBarry 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);
37690f02eecSBarry Smith     idxout[i] = idxmap[idxin[i]];
37790f02eecSBarry Smith   }
3783b9aefa3SBarry Smith   ierr = ISRestoreIndices(is,&idxin);CHKERRQ(ierr);
37970b3c8c7SBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,n,idxout,PETSC_OWN_POINTER,newis);CHKERRQ(ierr);
3803a40ed3dSBarry Smith   PetscFunctionReturn(0);
38190f02eecSBarry Smith }
38290f02eecSBarry Smith 
383afcb2eb5SJed Brown #undef __FUNCT__
384afcb2eb5SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingApply"
385afcb2eb5SJed Brown /*@C
3863acfe500SLois Curfman McInnes    ISLocalToGlobalMappingApply - Takes a list of integers in a local numbering
3873acfe500SLois Curfman McInnes    and converts them to the global numbering.
38890f02eecSBarry Smith 
389b9cd556bSLois Curfman McInnes    Not collective
390b9cd556bSLois Curfman McInnes 
391bb25748dSBarry Smith    Input Parameters:
392b9cd556bSLois Curfman McInnes +  mapping - the local to global mapping context
393bb25748dSBarry Smith .  N - number of integers
394b9cd556bSLois Curfman McInnes -  in - input indices in local numbering
395bb25748dSBarry Smith 
396bb25748dSBarry Smith    Output Parameter:
397bb25748dSBarry Smith .  out - indices in global numbering
398bb25748dSBarry Smith 
399b9cd556bSLois Curfman McInnes    Notes:
400b9cd556bSLois Curfman McInnes    The in and out array parameters may be identical.
401d4bb536fSBarry Smith 
402a997ad1aSLois Curfman McInnes    Level: advanced
403a997ad1aSLois Curfman McInnes 
404bb25748dSBarry Smith .seealso: ISLocalToGlobalMappingCreate(),ISLocalToGlobalMappingDestroy(),
4050752156aSBarry Smith           ISLocalToGlobalMappingApplyIS(),AOCreateBasic(),AOApplicationToPetsc(),
406d4bb536fSBarry Smith           AOPetscToApplication(), ISGlobalToLocalMappingApply()
407bb25748dSBarry Smith 
408273d9f13SBarry Smith     Concepts: mapping^local to global
409afcb2eb5SJed Brown @*/
410afcb2eb5SJed Brown PetscErrorCode ISLocalToGlobalMappingApply(ISLocalToGlobalMapping mapping,PetscInt N,const PetscInt in[],PetscInt out[])
411afcb2eb5SJed Brown {
412afcb2eb5SJed Brown   PetscInt       i,Nmax = mapping->n;
413afcb2eb5SJed Brown   const PetscInt *idx = mapping->indices;
414d4bb536fSBarry Smith 
415afcb2eb5SJed Brown   PetscFunctionBegin;
416afcb2eb5SJed Brown   for (i=0; i<N; i++) {
417afcb2eb5SJed Brown     if (in[i] < 0) {
418afcb2eb5SJed Brown       out[i] = in[i];
419afcb2eb5SJed Brown       continue;
420afcb2eb5SJed Brown     }
421afcb2eb5SJed 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);
422afcb2eb5SJed Brown     out[i] = idx[in[i]];
423afcb2eb5SJed Brown   }
424afcb2eb5SJed Brown   PetscFunctionReturn(0);
425afcb2eb5SJed Brown }
426d4bb536fSBarry Smith 
427d4bb536fSBarry Smith /* -----------------------------------------------------------------------------------------*/
428d4bb536fSBarry Smith 
4294a2ae208SSatish Balay #undef __FUNCT__
4304a2ae208SSatish Balay #define __FUNCT__ "ISGlobalToLocalMappingSetUp_Private"
431d4bb536fSBarry Smith /*
432d4bb536fSBarry Smith     Creates the global fields in the ISLocalToGlobalMapping structure
433d4bb536fSBarry Smith */
4346849ba73SBarry Smith static PetscErrorCode ISGlobalToLocalMappingSetUp_Private(ISLocalToGlobalMapping mapping)
435d4bb536fSBarry Smith {
4366849ba73SBarry Smith   PetscErrorCode ierr;
43732dcc486SBarry Smith   PetscInt       i,*idx = mapping->indices,n = mapping->n,end,start,*globals;
438d4bb536fSBarry Smith 
4393a40ed3dSBarry Smith   PetscFunctionBegin;
440d4bb536fSBarry Smith   end   = 0;
441ec268f7cSJed Brown   start = PETSC_MAX_INT;
442d4bb536fSBarry Smith 
443d4bb536fSBarry Smith   for (i=0; i<n; i++) {
444d4bb536fSBarry Smith     if (idx[i] < 0) continue;
445d4bb536fSBarry Smith     if (idx[i] < start) start = idx[i];
446d4bb536fSBarry Smith     if (idx[i] > end)   end   = idx[i];
447d4bb536fSBarry Smith   }
448d4bb536fSBarry Smith   if (start > end) {start = 0; end = -1;}
449d4bb536fSBarry Smith   mapping->globalstart = start;
450d4bb536fSBarry Smith   mapping->globalend   = end;
451d4bb536fSBarry Smith 
45232dcc486SBarry Smith   ierr             = PetscMalloc((end-start+2)*sizeof(PetscInt),&globals);CHKERRQ(ierr);
453b0a32e0cSBarry Smith   mapping->globals = globals;
454f6e5521dSKarl Rupp   for (i=0; i<end-start+1; i++) globals[i] = -1;
455d4bb536fSBarry Smith   for (i=0; i<n; i++) {
456d4bb536fSBarry Smith     if (idx[i] < 0) continue;
457d4bb536fSBarry Smith     globals[idx[i] - start] = i;
458d4bb536fSBarry Smith   }
459d4bb536fSBarry Smith 
46052e6d16bSBarry Smith   ierr = PetscLogObjectMemory(mapping,(end-start+1)*sizeof(PetscInt));CHKERRQ(ierr);
4613a40ed3dSBarry Smith   PetscFunctionReturn(0);
462d4bb536fSBarry Smith }
463d4bb536fSBarry Smith 
4644a2ae208SSatish Balay #undef __FUNCT__
4654a2ae208SSatish Balay #define __FUNCT__ "ISGlobalToLocalMappingApply"
466d4bb536fSBarry Smith /*@
467a997ad1aSLois Curfman McInnes     ISGlobalToLocalMappingApply - Provides the local numbering for a list of integers
468a997ad1aSLois Curfman McInnes     specified with a global numbering.
469d4bb536fSBarry Smith 
470b9cd556bSLois Curfman McInnes     Not collective
471b9cd556bSLois Curfman McInnes 
472d4bb536fSBarry Smith     Input Parameters:
473b9cd556bSLois Curfman McInnes +   mapping - mapping between local and global numbering
474d4bb536fSBarry Smith .   type - IS_GTOLM_MASK - replaces global indices with no local value with -1
475d4bb536fSBarry Smith            IS_GTOLM_DROP - drops the indices with no local value from the output list
476d4bb536fSBarry Smith .   n - number of global indices to map
477b9cd556bSLois Curfman McInnes -   idx - global indices to map
478d4bb536fSBarry Smith 
479d4bb536fSBarry Smith     Output Parameters:
480b9cd556bSLois Curfman McInnes +   nout - number of indices in output array (if type == IS_GTOLM_MASK then nout = n)
481b9cd556bSLois Curfman McInnes -   idxout - local index of each global index, one must pass in an array long enough
482e182c471SBarry Smith              to hold all the indices. You can call ISGlobalToLocalMappingApply() with
4830298fd71SBarry Smith              idxout == NULL to determine the required length (returned in nout)
484e182c471SBarry Smith              and then allocate the required space and call ISGlobalToLocalMappingApply()
485e182c471SBarry Smith              a second time to set the values.
486d4bb536fSBarry Smith 
487b9cd556bSLois Curfman McInnes     Notes:
4880298fd71SBarry Smith     Either nout or idxout may be NULL. idx and idxout may be identical.
489d4bb536fSBarry Smith 
4900f5bd95cSBarry Smith     This is not scalable in memory usage. Each processor requires O(Nglobal) size
4910f5bd95cSBarry Smith     array to compute these.
4920f5bd95cSBarry Smith 
493a997ad1aSLois Curfman McInnes     Level: advanced
494a997ad1aSLois Curfman McInnes 
49532fd6b96SBarry Smith     Developer Note: The manual page states that idx and idxout may be identical but the calling
49632fd6b96SBarry Smith        sequence declares idx as const so it cannot be the same as idxout.
49732fd6b96SBarry Smith 
498273d9f13SBarry Smith     Concepts: mapping^global to local
499d4bb536fSBarry Smith 
500d4bb536fSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),
501d4bb536fSBarry Smith           ISLocalToGlobalMappingDestroy()
502d4bb536fSBarry Smith @*/
5037087cfbeSBarry Smith PetscErrorCode  ISGlobalToLocalMappingApply(ISLocalToGlobalMapping mapping,ISGlobalToLocalMappingType type,
50432dcc486SBarry Smith                                   PetscInt n,const PetscInt idx[],PetscInt *nout,PetscInt idxout[])
505d4bb536fSBarry Smith {
50632dcc486SBarry Smith   PetscInt       i,*globals,nf = 0,tmp,start,end;
5076849ba73SBarry Smith   PetscErrorCode ierr;
508d4bb536fSBarry Smith 
5093a40ed3dSBarry Smith   PetscFunctionBegin;
5100700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
511d4bb536fSBarry Smith   if (!mapping->globals) {
512d4bb536fSBarry Smith     ierr = ISGlobalToLocalMappingSetUp_Private(mapping);CHKERRQ(ierr);
513d4bb536fSBarry Smith   }
514d4bb536fSBarry Smith   globals = mapping->globals;
515d4bb536fSBarry Smith   start   = mapping->globalstart;
516d4bb536fSBarry Smith   end     = mapping->globalend;
517d4bb536fSBarry Smith 
518d4bb536fSBarry Smith   if (type == IS_GTOLM_MASK) {
519d4bb536fSBarry Smith     if (idxout) {
520d4bb536fSBarry Smith       for (i=0; i<n; i++) {
521d4bb536fSBarry Smith         if (idx[i] < 0) idxout[i] = idx[i];
522d4bb536fSBarry Smith         else if (idx[i] < start) idxout[i] = -1;
523d4bb536fSBarry Smith         else if (idx[i] > end)   idxout[i] = -1;
524d4bb536fSBarry Smith         else                     idxout[i] = globals[idx[i] - start];
525d4bb536fSBarry Smith       }
526d4bb536fSBarry Smith     }
527d4bb536fSBarry Smith     if (nout) *nout = n;
528d4bb536fSBarry Smith   } else {
529d4bb536fSBarry Smith     if (idxout) {
530d4bb536fSBarry Smith       for (i=0; i<n; i++) {
531d4bb536fSBarry Smith         if (idx[i] < 0) continue;
532d4bb536fSBarry Smith         if (idx[i] < start) continue;
533d4bb536fSBarry Smith         if (idx[i] > end) continue;
534d4bb536fSBarry Smith         tmp = globals[idx[i] - start];
535d4bb536fSBarry Smith         if (tmp < 0) continue;
536d4bb536fSBarry Smith         idxout[nf++] = tmp;
537d4bb536fSBarry Smith       }
538d4bb536fSBarry Smith     } else {
539d4bb536fSBarry Smith       for (i=0; i<n; i++) {
540d4bb536fSBarry Smith         if (idx[i] < 0) continue;
541d4bb536fSBarry Smith         if (idx[i] < start) continue;
542d4bb536fSBarry Smith         if (idx[i] > end) continue;
543d4bb536fSBarry Smith         tmp = globals[idx[i] - start];
544d4bb536fSBarry Smith         if (tmp < 0) continue;
545d4bb536fSBarry Smith         nf++;
546d4bb536fSBarry Smith       }
547d4bb536fSBarry Smith     }
548d4bb536fSBarry Smith     if (nout) *nout = nf;
549d4bb536fSBarry Smith   }
5503a40ed3dSBarry Smith   PetscFunctionReturn(0);
551d4bb536fSBarry Smith }
55290f02eecSBarry Smith 
5534a2ae208SSatish Balay #undef __FUNCT__
5544a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingGetInfo"
55589d82c54SBarry Smith /*@C
55689d82c54SBarry Smith     ISLocalToGlobalMappingGetInfo - Gets the neighbor information for each processor and
55789d82c54SBarry Smith      each index shared by more than one processor
55889d82c54SBarry Smith 
55989d82c54SBarry Smith     Collective on ISLocalToGlobalMapping
56089d82c54SBarry Smith 
56189d82c54SBarry Smith     Input Parameters:
56289d82c54SBarry Smith .   mapping - the mapping from local to global indexing
56389d82c54SBarry Smith 
56489d82c54SBarry Smith     Output Parameter:
56589d82c54SBarry Smith +   nproc - number of processors that are connected to this one
56689d82c54SBarry Smith .   proc - neighboring processors
56707b52d57SBarry Smith .   numproc - number of indices for each subdomain (processor)
5683463a7baSJed Brown -   indices - indices of nodes (in local numbering) shared with neighbors (sorted by global numbering)
56989d82c54SBarry Smith 
57089d82c54SBarry Smith     Level: advanced
57189d82c54SBarry Smith 
572273d9f13SBarry Smith     Concepts: mapping^local to global
57389d82c54SBarry Smith 
5742cfcea29SBarry Smith     Fortran Usage:
5752cfcea29SBarry Smith $        ISLocalToGlobalMpngGetInfoSize(ISLocalToGlobalMapping,PetscInt nproc,PetscInt numprocmax,ierr) followed by
5762cfcea29SBarry Smith $        ISLocalToGlobalMappingGetInfo(ISLocalToGlobalMapping,PetscInt nproc, PetscInt procs[nproc],PetscInt numprocs[nproc],
5772cfcea29SBarry Smith           PetscInt indices[nproc][numprocmax],ierr)
5782cfcea29SBarry Smith         There is no ISLocalToGlobalMappingRestoreInfo() in Fortran. You must make sure that procs[], numprocs[] and
5792cfcea29SBarry Smith         indices[][] are large enough arrays, either by allocating them dynamically or defining static ones large enough.
5802cfcea29SBarry Smith 
5812cfcea29SBarry Smith 
58207b52d57SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreate(),
58307b52d57SBarry Smith           ISLocalToGlobalMappingRestoreInfo()
58489d82c54SBarry Smith @*/
5857087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingGetInfo(ISLocalToGlobalMapping mapping,PetscInt *nproc,PetscInt *procs[],PetscInt *numprocs[],PetscInt **indices[])
58689d82c54SBarry Smith {
5876849ba73SBarry Smith   PetscErrorCode ierr;
58897f1f81fSBarry Smith   PetscMPIInt    size,rank,tag1,tag2,tag3,*len,*source,imdex;
58932dcc486SBarry Smith   PetscInt       i,n = mapping->n,Ng,ng,max = 0,*lindices = mapping->indices;
59032dcc486SBarry Smith   PetscInt       *nprocs,*owner,nsends,*sends,j,*starts,nmax,nrecvs,*recvs,proc;
59197f1f81fSBarry Smith   PetscInt       cnt,scale,*ownedsenders,*nownedsenders,rstart,nowned;
59232dcc486SBarry Smith   PetscInt       node,nownedm,nt,*sends2,nsends2,*starts2,*lens2,*dest,nrecvs2,*starts3,*recvs2,k,*bprocs,*tmp;
59332dcc486SBarry Smith   PetscInt       first_procs,first_numprocs,*first_indices;
59489d82c54SBarry Smith   MPI_Request    *recv_waits,*send_waits;
59530dcb7c9SBarry Smith   MPI_Status     recv_status,*send_status,*recv_statuses;
596*ce94432eSBarry Smith   MPI_Comm       comm;
597ace3abfcSBarry Smith   PetscBool      debug = PETSC_FALSE;
59889d82c54SBarry Smith 
59989d82c54SBarry Smith   PetscFunctionBegin;
6000700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
601*ce94432eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)mapping,&comm);CHKERRQ(ierr);
60224cf384cSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
60324cf384cSBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
60424cf384cSBarry Smith   if (size == 1) {
60524cf384cSBarry Smith     *nproc         = 0;
6060298fd71SBarry Smith     *procs         = NULL;
60732dcc486SBarry Smith     ierr           = PetscMalloc(sizeof(PetscInt),numprocs);CHKERRQ(ierr);
6081e2105dcSBarry Smith     (*numprocs)[0] = 0;
60932dcc486SBarry Smith     ierr           = PetscMalloc(sizeof(PetscInt*),indices);CHKERRQ(ierr);
6100298fd71SBarry Smith     (*indices)[0]  = NULL;
61124cf384cSBarry Smith     PetscFunctionReturn(0);
61224cf384cSBarry Smith   }
61324cf384cSBarry Smith 
6140298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-islocaltoglobalmappinggetinfo_debug",&debug,NULL);CHKERRQ(ierr);
61507b52d57SBarry Smith 
6163677ff5aSBarry Smith   /*
6173677ff5aSBarry Smith     Notes on ISLocalToGlobalMappingGetInfo
6183677ff5aSBarry Smith 
6193677ff5aSBarry Smith     globally owned node - the nodes that have been assigned to this processor in global
6203677ff5aSBarry Smith            numbering, just for this routine.
6213677ff5aSBarry Smith 
6223677ff5aSBarry Smith     nontrivial globally owned node - node assigned to this processor that is on a subdomain
6233677ff5aSBarry Smith            boundary (i.e. is has more than one local owner)
6243677ff5aSBarry Smith 
6253677ff5aSBarry Smith     locally owned node - node that exists on this processors subdomain
6263677ff5aSBarry Smith 
6273677ff5aSBarry Smith     nontrivial locally owned node - node that is not in the interior (i.e. has more than one
6283677ff5aSBarry Smith            local subdomain
6293677ff5aSBarry Smith   */
63024cf384cSBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mapping,&tag1);CHKERRQ(ierr);
63124cf384cSBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mapping,&tag2);CHKERRQ(ierr);
63224cf384cSBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mapping,&tag3);CHKERRQ(ierr);
63389d82c54SBarry Smith 
63489d82c54SBarry Smith   for (i=0; i<n; i++) {
63589d82c54SBarry Smith     if (lindices[i] > max) max = lindices[i];
63689d82c54SBarry Smith   }
63732dcc486SBarry Smith   ierr   = MPI_Allreduce(&max,&Ng,1,MPIU_INT,MPI_MAX,comm);CHKERRQ(ierr);
63878058e43SBarry Smith   Ng++;
63989d82c54SBarry Smith   ierr   = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
64089d82c54SBarry Smith   ierr   = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
641bc8ff85bSBarry Smith   scale  = Ng/size + 1;
642a2e34c3dSBarry Smith   ng     = scale; if (rank == size-1) ng = Ng - scale*(size-1); ng = PetscMax(1,ng);
643caba0dd0SBarry Smith   rstart = scale*rank;
64489d82c54SBarry Smith 
64589d82c54SBarry Smith   /* determine ownership ranges of global indices */
6467c334f02SBarry Smith   ierr = PetscMalloc(2*size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr);
64732dcc486SBarry Smith   ierr = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr);
64889d82c54SBarry Smith 
64989d82c54SBarry Smith   /* determine owners of each local node  */
6507c334f02SBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&owner);CHKERRQ(ierr);
65189d82c54SBarry Smith   for (i=0; i<n; i++) {
6523677ff5aSBarry Smith     proc             = lindices[i]/scale; /* processor that globally owns this index */
65327c402fcSBarry Smith     nprocs[2*proc+1] = 1;                 /* processor globally owns at least one of ours */
6543677ff5aSBarry Smith     owner[i]         = proc;
65527c402fcSBarry Smith     nprocs[2*proc]++;                     /* count of how many that processor globally owns of ours */
65689d82c54SBarry Smith   }
65727c402fcSBarry Smith   nsends = 0; for (i=0; i<size; i++) nsends += nprocs[2*i+1];
6581e2582c4SBarry Smith   ierr = PetscInfo1(mapping,"Number of global owners for my local data %d\n",nsends);CHKERRQ(ierr);
65989d82c54SBarry Smith 
66089d82c54SBarry Smith   /* inform other processors of number of messages and max length*/
66127c402fcSBarry Smith   ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr);
6621e2582c4SBarry Smith   ierr = PetscInfo1(mapping,"Number of local owners for my global data %d\n",nrecvs);CHKERRQ(ierr);
66389d82c54SBarry Smith 
66489d82c54SBarry Smith   /* post receives for owned rows */
66532dcc486SBarry Smith   ierr = PetscMalloc((2*nrecvs+1)*(nmax+1)*sizeof(PetscInt),&recvs);CHKERRQ(ierr);
666b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
66789d82c54SBarry Smith   for (i=0; i<nrecvs; i++) {
66832dcc486SBarry Smith     ierr = MPI_Irecv(recvs+2*nmax*i,2*nmax,MPIU_INT,MPI_ANY_SOURCE,tag1,comm,recv_waits+i);CHKERRQ(ierr);
66989d82c54SBarry Smith   }
67089d82c54SBarry Smith 
67189d82c54SBarry Smith   /* pack messages containing lists of local nodes to owners */
67232dcc486SBarry Smith   ierr      = PetscMalloc((2*n+1)*sizeof(PetscInt),&sends);CHKERRQ(ierr);
67332dcc486SBarry Smith   ierr      = PetscMalloc((size+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr);
67489d82c54SBarry Smith   starts[0] = 0;
675f6e5521dSKarl Rupp   for (i=1; i<size; i++) starts[i] = starts[i-1] + 2*nprocs[2*i-2];
67689d82c54SBarry Smith   for (i=0; i<n; i++) {
67789d82c54SBarry Smith     sends[starts[owner[i]]++] = lindices[i];
67830dcb7c9SBarry Smith     sends[starts[owner[i]]++] = i;
67989d82c54SBarry Smith   }
68089d82c54SBarry Smith   ierr = PetscFree(owner);CHKERRQ(ierr);
68189d82c54SBarry Smith   starts[0] = 0;
682f6e5521dSKarl Rupp   for (i=1; i<size; i++) starts[i] = starts[i-1] + 2*nprocs[2*i-2];
68389d82c54SBarry Smith 
68489d82c54SBarry Smith   /* send the messages */
685b0a32e0cSBarry Smith   ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr);
68632dcc486SBarry Smith   ierr = PetscMalloc((nsends+1)*sizeof(PetscInt),&dest);CHKERRQ(ierr);
68789d82c54SBarry Smith   cnt = 0;
68889d82c54SBarry Smith   for (i=0; i<size; i++) {
68927c402fcSBarry Smith     if (nprocs[2*i]) {
69032dcc486SBarry Smith       ierr      = MPI_Isend(sends+starts[i],2*nprocs[2*i],MPIU_INT,i,tag1,comm,send_waits+cnt);CHKERRQ(ierr);
69130dcb7c9SBarry Smith       dest[cnt] = i;
69289d82c54SBarry Smith       cnt++;
69389d82c54SBarry Smith     }
69489d82c54SBarry Smith   }
69589d82c54SBarry Smith   ierr = PetscFree(starts);CHKERRQ(ierr);
69689d82c54SBarry Smith 
69789d82c54SBarry Smith   /* wait on receives */
69897f1f81fSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(PetscMPIInt),&source);CHKERRQ(ierr);
69997f1f81fSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(PetscMPIInt),&len);CHKERRQ(ierr);
70089d82c54SBarry Smith   cnt  = nrecvs;
70132dcc486SBarry Smith   ierr = PetscMalloc((ng+1)*sizeof(PetscInt),&nownedsenders);CHKERRQ(ierr);
70232dcc486SBarry Smith   ierr = PetscMemzero(nownedsenders,ng*sizeof(PetscInt));CHKERRQ(ierr);
70389d82c54SBarry Smith   while (cnt) {
70489d82c54SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
70589d82c54SBarry Smith     /* unpack receives into our local space */
70632dcc486SBarry Smith     ierr          = MPI_Get_count(&recv_status,MPIU_INT,&len[imdex]);CHKERRQ(ierr);
70789d82c54SBarry Smith     source[imdex] = recv_status.MPI_SOURCE;
70830dcb7c9SBarry Smith     len[imdex]    = len[imdex]/2;
709caba0dd0SBarry Smith     /* count how many local owners for each of my global owned indices */
71030dcb7c9SBarry Smith     for (i=0; i<len[imdex]; i++) nownedsenders[recvs[2*imdex*nmax+2*i]-rstart]++;
71189d82c54SBarry Smith     cnt--;
71289d82c54SBarry Smith   }
71389d82c54SBarry Smith   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
71489d82c54SBarry Smith 
71530dcb7c9SBarry Smith   /* count how many globally owned indices are on an edge multiplied by how many processors own them. */
716bc8ff85bSBarry Smith   nowned  = 0;
717bc8ff85bSBarry Smith   nownedm = 0;
718bc8ff85bSBarry Smith   for (i=0; i<ng; i++) {
719bc8ff85bSBarry Smith     if (nownedsenders[i] > 1) {nownedm += nownedsenders[i]; nowned++;}
720bc8ff85bSBarry Smith   }
721bc8ff85bSBarry Smith 
722bc8ff85bSBarry Smith   /* create single array to contain rank of all local owners of each globally owned index */
72332dcc486SBarry Smith   ierr      = PetscMalloc((nownedm+1)*sizeof(PetscInt),&ownedsenders);CHKERRQ(ierr);
72432dcc486SBarry Smith   ierr      = PetscMalloc((ng+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr);
725bc8ff85bSBarry Smith   starts[0] = 0;
726bc8ff85bSBarry Smith   for (i=1; i<ng; i++) {
727bc8ff85bSBarry Smith     if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1];
728bc8ff85bSBarry Smith     else starts[i] = starts[i-1];
729bc8ff85bSBarry Smith   }
730bc8ff85bSBarry Smith 
73130dcb7c9SBarry Smith   /* for each nontrival globally owned node list all arriving processors */
732bc8ff85bSBarry Smith   for (i=0; i<nrecvs; i++) {
733bc8ff85bSBarry Smith     for (j=0; j<len[i]; j++) {
73430dcb7c9SBarry Smith       node = recvs[2*i*nmax+2*j]-rstart;
735f6e5521dSKarl Rupp       if (nownedsenders[node] > 1) ownedsenders[starts[node]++] = source[i];
736bc8ff85bSBarry Smith     }
737bc8ff85bSBarry Smith   }
738bc8ff85bSBarry Smith 
73907b52d57SBarry Smith   if (debug) { /* -----------------------------------  */
74030dcb7c9SBarry Smith     starts[0] = 0;
74130dcb7c9SBarry Smith     for (i=1; i<ng; i++) {
74230dcb7c9SBarry Smith       if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1];
74330dcb7c9SBarry Smith       else starts[i] = starts[i-1];
74430dcb7c9SBarry Smith     }
74530dcb7c9SBarry Smith     for (i=0; i<ng; i++) {
74630dcb7c9SBarry Smith       if (nownedsenders[i] > 1) {
74730dcb7c9SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"[%d] global node %d local owner processors: ",rank,i+rstart);CHKERRQ(ierr);
74830dcb7c9SBarry Smith         for (j=0; j<nownedsenders[i]; j++) {
74930dcb7c9SBarry Smith           ierr = PetscSynchronizedPrintf(comm,"%d ",ownedsenders[starts[i]+j]);CHKERRQ(ierr);
75030dcb7c9SBarry Smith         }
75130dcb7c9SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"\n");CHKERRQ(ierr);
75230dcb7c9SBarry Smith       }
75330dcb7c9SBarry Smith     }
75430dcb7c9SBarry Smith     ierr = PetscSynchronizedFlush(comm);CHKERRQ(ierr);
75507b52d57SBarry Smith   } /* -----------------------------------  */
75630dcb7c9SBarry Smith 
7573677ff5aSBarry Smith   /* wait on original sends */
7583a96401aSBarry Smith   if (nsends) {
759b0a32e0cSBarry Smith     ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
7603a96401aSBarry Smith     ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
7613a96401aSBarry Smith     ierr = PetscFree(send_status);CHKERRQ(ierr);
7623a96401aSBarry Smith   }
76389d82c54SBarry Smith   ierr = PetscFree(send_waits);CHKERRQ(ierr);
7643a96401aSBarry Smith   ierr = PetscFree(sends);CHKERRQ(ierr);
7653677ff5aSBarry Smith   ierr = PetscFree(nprocs);CHKERRQ(ierr);
7663677ff5aSBarry Smith 
7673677ff5aSBarry Smith   /* pack messages to send back to local owners */
76830dcb7c9SBarry Smith   starts[0] = 0;
76930dcb7c9SBarry Smith   for (i=1; i<ng; i++) {
77030dcb7c9SBarry Smith     if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1];
77130dcb7c9SBarry Smith     else starts[i] = starts[i-1];
77230dcb7c9SBarry Smith   }
77330dcb7c9SBarry Smith   nsends2 = nrecvs;
77432dcc486SBarry Smith   ierr    = PetscMalloc((nsends2+1)*sizeof(PetscInt),&nprocs);CHKERRQ(ierr); /* length of each message */
77530dcb7c9SBarry Smith   for (i=0; i<nrecvs; i++) {
77630dcb7c9SBarry Smith     nprocs[i] = 1;
77730dcb7c9SBarry Smith     for (j=0; j<len[i]; j++) {
77830dcb7c9SBarry Smith       node = recvs[2*i*nmax+2*j]-rstart;
779f6e5521dSKarl Rupp       if (nownedsenders[node] > 1) nprocs[i] += 2 + nownedsenders[node];
78030dcb7c9SBarry Smith     }
78130dcb7c9SBarry Smith   }
782f6e5521dSKarl Rupp   nt = 0;
783f6e5521dSKarl Rupp   for (i=0; i<nsends2; i++) nt += nprocs[i];
784f6e5521dSKarl Rupp 
78532dcc486SBarry Smith   ierr = PetscMalloc((nt+1)*sizeof(PetscInt),&sends2);CHKERRQ(ierr);
78632dcc486SBarry Smith   ierr = PetscMalloc((nsends2+1)*sizeof(PetscInt),&starts2);CHKERRQ(ierr);
787f6e5521dSKarl Rupp 
788f6e5521dSKarl Rupp   starts2[0] = 0;
789f6e5521dSKarl Rupp   for (i=1; i<nsends2; i++) starts2[i] = starts2[i-1] + nprocs[i-1];
79030dcb7c9SBarry Smith   /*
79130dcb7c9SBarry Smith      Each message is 1 + nprocs[i] long, and consists of
79230dcb7c9SBarry Smith        (0) the number of nodes being sent back
79330dcb7c9SBarry Smith        (1) the local node number,
79430dcb7c9SBarry Smith        (2) the number of processors sharing it,
79530dcb7c9SBarry Smith        (3) the processors sharing it
79630dcb7c9SBarry Smith   */
79730dcb7c9SBarry Smith   for (i=0; i<nsends2; i++) {
79830dcb7c9SBarry Smith     cnt = 1;
79930dcb7c9SBarry Smith     sends2[starts2[i]] = 0;
80030dcb7c9SBarry Smith     for (j=0; j<len[i]; j++) {
80130dcb7c9SBarry Smith       node = recvs[2*i*nmax+2*j]-rstart;
80230dcb7c9SBarry Smith       if (nownedsenders[node] > 1) {
80330dcb7c9SBarry Smith         sends2[starts2[i]]++;
80430dcb7c9SBarry Smith         sends2[starts2[i]+cnt++] = recvs[2*i*nmax+2*j+1];
80530dcb7c9SBarry Smith         sends2[starts2[i]+cnt++] = nownedsenders[node];
80632dcc486SBarry Smith         ierr = PetscMemcpy(&sends2[starts2[i]+cnt],&ownedsenders[starts[node]],nownedsenders[node]*sizeof(PetscInt));CHKERRQ(ierr);
80730dcb7c9SBarry Smith         cnt += nownedsenders[node];
80830dcb7c9SBarry Smith       }
80930dcb7c9SBarry Smith     }
81030dcb7c9SBarry Smith   }
81130dcb7c9SBarry Smith 
81230dcb7c9SBarry Smith   /* receive the message lengths */
81330dcb7c9SBarry Smith   nrecvs2 = nsends;
81432dcc486SBarry Smith   ierr    = PetscMalloc((nrecvs2+1)*sizeof(PetscInt),&lens2);CHKERRQ(ierr);
81532dcc486SBarry Smith   ierr    = PetscMalloc((nrecvs2+1)*sizeof(PetscInt),&starts3);CHKERRQ(ierr);
816d44834fbSBarry Smith   ierr    = PetscMalloc((nrecvs2+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
81730dcb7c9SBarry Smith   for (i=0; i<nrecvs2; i++) {
818d44834fbSBarry Smith     ierr = MPI_Irecv(&lens2[i],1,MPIU_INT,dest[i],tag2,comm,recv_waits+i);CHKERRQ(ierr);
81930dcb7c9SBarry Smith   }
820d44834fbSBarry Smith 
8218a8e0b3aSBarry Smith   /* send the message lengths */
8228a8e0b3aSBarry Smith   for (i=0; i<nsends2; i++) {
8238a8e0b3aSBarry Smith     ierr = MPI_Send(&nprocs[i],1,MPIU_INT,source[i],tag2,comm);CHKERRQ(ierr);
8248a8e0b3aSBarry Smith   }
8258a8e0b3aSBarry Smith 
826d44834fbSBarry Smith   /* wait on receives of lens */
8270c468ba9SBarry Smith   if (nrecvs2) {
8280c468ba9SBarry Smith     ierr = PetscMalloc(nrecvs2*sizeof(MPI_Status),&recv_statuses);CHKERRQ(ierr);
829d44834fbSBarry Smith     ierr = MPI_Waitall(nrecvs2,recv_waits,recv_statuses);CHKERRQ(ierr);
830d44834fbSBarry Smith     ierr = PetscFree(recv_statuses);CHKERRQ(ierr);
8310c468ba9SBarry Smith   }
832a2ea699eSBarry Smith   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
833d44834fbSBarry Smith 
83430dcb7c9SBarry Smith   starts3[0] = 0;
835d44834fbSBarry Smith   nt         = 0;
83630dcb7c9SBarry Smith   for (i=0; i<nrecvs2-1; i++) {
83730dcb7c9SBarry Smith     starts3[i+1] = starts3[i] + lens2[i];
838d44834fbSBarry Smith     nt          += lens2[i];
83930dcb7c9SBarry Smith   }
84076466f69SStefano Zampini   if (nrecvs2) nt += lens2[nrecvs2-1];
841d44834fbSBarry Smith 
84232dcc486SBarry Smith   ierr = PetscMalloc((nt+1)*sizeof(PetscInt),&recvs2);CHKERRQ(ierr);
843b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs2+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
84452b72c4aSBarry Smith   for (i=0; i<nrecvs2; i++) {
84532dcc486SBarry Smith     ierr = MPI_Irecv(recvs2+starts3[i],lens2[i],MPIU_INT,dest[i],tag3,comm,recv_waits+i);CHKERRQ(ierr);
84630dcb7c9SBarry Smith   }
84730dcb7c9SBarry Smith 
84830dcb7c9SBarry Smith   /* send the messages */
849b0a32e0cSBarry Smith   ierr = PetscMalloc((nsends2+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr);
85030dcb7c9SBarry Smith   for (i=0; i<nsends2; i++) {
85132dcc486SBarry Smith     ierr = MPI_Isend(sends2+starts2[i],nprocs[i],MPIU_INT,source[i],tag3,comm,send_waits+i);CHKERRQ(ierr);
85230dcb7c9SBarry Smith   }
85330dcb7c9SBarry Smith 
85430dcb7c9SBarry Smith   /* wait on receives */
8550c468ba9SBarry Smith   if (nrecvs2) {
8560c468ba9SBarry Smith     ierr = PetscMalloc(nrecvs2*sizeof(MPI_Status),&recv_statuses);CHKERRQ(ierr);
85730dcb7c9SBarry Smith     ierr = MPI_Waitall(nrecvs2,recv_waits,recv_statuses);CHKERRQ(ierr);
85830dcb7c9SBarry Smith     ierr = PetscFree(recv_statuses);CHKERRQ(ierr);
8590c468ba9SBarry Smith   }
86030dcb7c9SBarry Smith   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
86130dcb7c9SBarry Smith   ierr = PetscFree(nprocs);CHKERRQ(ierr);
86230dcb7c9SBarry Smith 
86307b52d57SBarry Smith   if (debug) { /* -----------------------------------  */
86430dcb7c9SBarry Smith     cnt = 0;
86530dcb7c9SBarry Smith     for (i=0; i<nrecvs2; i++) {
86630dcb7c9SBarry Smith       nt = recvs2[cnt++];
86730dcb7c9SBarry Smith       for (j=0; j<nt; j++) {
86830dcb7c9SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"[%d] local node %d number of subdomains %d: ",rank,recvs2[cnt],recvs2[cnt+1]);CHKERRQ(ierr);
86930dcb7c9SBarry Smith         for (k=0; k<recvs2[cnt+1]; k++) {
87030dcb7c9SBarry Smith           ierr = PetscSynchronizedPrintf(comm,"%d ",recvs2[cnt+2+k]);CHKERRQ(ierr);
87130dcb7c9SBarry Smith         }
87230dcb7c9SBarry Smith         cnt += 2 + recvs2[cnt+1];
87330dcb7c9SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"\n");CHKERRQ(ierr);
87430dcb7c9SBarry Smith       }
87530dcb7c9SBarry Smith     }
87630dcb7c9SBarry Smith     ierr = PetscSynchronizedFlush(comm);CHKERRQ(ierr);
87707b52d57SBarry Smith   } /* -----------------------------------  */
87830dcb7c9SBarry Smith 
87930dcb7c9SBarry Smith   /* count number subdomains for each local node */
88032dcc486SBarry Smith   ierr = PetscMalloc(size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr);
88132dcc486SBarry Smith   ierr = PetscMemzero(nprocs,size*sizeof(PetscInt));CHKERRQ(ierr);
88230dcb7c9SBarry Smith   cnt  = 0;
88330dcb7c9SBarry Smith   for (i=0; i<nrecvs2; i++) {
88430dcb7c9SBarry Smith     nt = recvs2[cnt++];
88530dcb7c9SBarry Smith     for (j=0; j<nt; j++) {
886f6e5521dSKarl Rupp       for (k=0; k<recvs2[cnt+1]; k++) nprocs[recvs2[cnt+2+k]]++;
88730dcb7c9SBarry Smith       cnt += 2 + recvs2[cnt+1];
88830dcb7c9SBarry Smith     }
88930dcb7c9SBarry Smith   }
89030dcb7c9SBarry Smith   nt = 0; for (i=0; i<size; i++) nt += (nprocs[i] > 0);
89130dcb7c9SBarry Smith   *nproc    = nt;
89232dcc486SBarry Smith   ierr = PetscMalloc((nt+1)*sizeof(PetscInt),procs);CHKERRQ(ierr);
89332dcc486SBarry Smith   ierr = PetscMalloc((nt+1)*sizeof(PetscInt),numprocs);CHKERRQ(ierr);
89432dcc486SBarry Smith   ierr = PetscMalloc((nt+1)*sizeof(PetscInt*),indices);CHKERRQ(ierr);
8950298fd71SBarry Smith   for (i=0;i<nt+1;i++) (*indices)[i]=NULL;
89632dcc486SBarry Smith   ierr = PetscMalloc(size*sizeof(PetscInt),&bprocs);CHKERRQ(ierr);
89730dcb7c9SBarry Smith   cnt       = 0;
89830dcb7c9SBarry Smith   for (i=0; i<size; i++) {
89930dcb7c9SBarry Smith     if (nprocs[i] > 0) {
90030dcb7c9SBarry Smith       bprocs[i]        = cnt;
90130dcb7c9SBarry Smith       (*procs)[cnt]    = i;
90230dcb7c9SBarry Smith       (*numprocs)[cnt] = nprocs[i];
90332dcc486SBarry Smith       ierr             = PetscMalloc(nprocs[i]*sizeof(PetscInt),&(*indices)[cnt]);CHKERRQ(ierr);
90430dcb7c9SBarry Smith       cnt++;
90530dcb7c9SBarry Smith     }
90630dcb7c9SBarry Smith   }
90730dcb7c9SBarry Smith 
90830dcb7c9SBarry Smith   /* make the list of subdomains for each nontrivial local node */
90932dcc486SBarry Smith   ierr = PetscMemzero(*numprocs,nt*sizeof(PetscInt));CHKERRQ(ierr);
91030dcb7c9SBarry Smith   cnt  = 0;
91130dcb7c9SBarry Smith   for (i=0; i<nrecvs2; i++) {
91230dcb7c9SBarry Smith     nt = recvs2[cnt++];
91330dcb7c9SBarry Smith     for (j=0; j<nt; j++) {
914f6e5521dSKarl Rupp       for (k=0; k<recvs2[cnt+1]; k++) (*indices)[bprocs[recvs2[cnt+2+k]]][(*numprocs)[bprocs[recvs2[cnt+2+k]]]++] = recvs2[cnt];
91530dcb7c9SBarry Smith       cnt += 2 + recvs2[cnt+1];
91630dcb7c9SBarry Smith     }
91730dcb7c9SBarry Smith   }
91830dcb7c9SBarry Smith   ierr = PetscFree(bprocs);CHKERRQ(ierr);
91907b52d57SBarry Smith   ierr = PetscFree(recvs2);CHKERRQ(ierr);
92030dcb7c9SBarry Smith 
92107b52d57SBarry Smith   /* sort the node indexing by their global numbers */
92207b52d57SBarry Smith   nt = *nproc;
92307b52d57SBarry Smith   for (i=0; i<nt; i++) {
92432dcc486SBarry Smith     ierr = PetscMalloc(((*numprocs)[i])*sizeof(PetscInt),&tmp);CHKERRQ(ierr);
925f6e5521dSKarl Rupp     for (j=0; j<(*numprocs)[i]; j++) tmp[j] = lindices[(*indices)[i][j]];
92607b52d57SBarry Smith     ierr = PetscSortIntWithArray((*numprocs)[i],tmp,(*indices)[i]);CHKERRQ(ierr);
92707b52d57SBarry Smith     ierr = PetscFree(tmp);CHKERRQ(ierr);
92807b52d57SBarry Smith   }
92907b52d57SBarry Smith 
93007b52d57SBarry Smith   if (debug) { /* -----------------------------------  */
93130dcb7c9SBarry Smith     nt = *nproc;
93230dcb7c9SBarry Smith     for (i=0; i<nt; i++) {
93330dcb7c9SBarry Smith       ierr = PetscSynchronizedPrintf(comm,"[%d] subdomain %d number of indices %d: ",rank,(*procs)[i],(*numprocs)[i]);CHKERRQ(ierr);
93430dcb7c9SBarry Smith       for (j=0; j<(*numprocs)[i]; j++) {
93530dcb7c9SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"%d ",(*indices)[i][j]);CHKERRQ(ierr);
93630dcb7c9SBarry Smith       }
93730dcb7c9SBarry Smith       ierr = PetscSynchronizedPrintf(comm,"\n");CHKERRQ(ierr);
93830dcb7c9SBarry Smith     }
93930dcb7c9SBarry Smith     ierr = PetscSynchronizedFlush(comm);CHKERRQ(ierr);
94007b52d57SBarry Smith   } /* -----------------------------------  */
94130dcb7c9SBarry Smith 
94230dcb7c9SBarry Smith   /* wait on sends */
94330dcb7c9SBarry Smith   if (nsends2) {
944b0a32e0cSBarry Smith     ierr = PetscMalloc(nsends2*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
94530dcb7c9SBarry Smith     ierr = MPI_Waitall(nsends2,send_waits,send_status);CHKERRQ(ierr);
94630dcb7c9SBarry Smith     ierr = PetscFree(send_status);CHKERRQ(ierr);
94730dcb7c9SBarry Smith   }
94830dcb7c9SBarry Smith 
94930dcb7c9SBarry Smith   ierr = PetscFree(starts3);CHKERRQ(ierr);
95030dcb7c9SBarry Smith   ierr = PetscFree(dest);CHKERRQ(ierr);
95130dcb7c9SBarry Smith   ierr = PetscFree(send_waits);CHKERRQ(ierr);
9523677ff5aSBarry Smith 
953bc8ff85bSBarry Smith   ierr = PetscFree(nownedsenders);CHKERRQ(ierr);
954bc8ff85bSBarry Smith   ierr = PetscFree(ownedsenders);CHKERRQ(ierr);
955bc8ff85bSBarry Smith   ierr = PetscFree(starts);CHKERRQ(ierr);
95630dcb7c9SBarry Smith   ierr = PetscFree(starts2);CHKERRQ(ierr);
95730dcb7c9SBarry Smith   ierr = PetscFree(lens2);CHKERRQ(ierr);
95889d82c54SBarry Smith 
95989d82c54SBarry Smith   ierr = PetscFree(source);CHKERRQ(ierr);
96097f1f81fSBarry Smith   ierr = PetscFree(len);CHKERRQ(ierr);
96189d82c54SBarry Smith   ierr = PetscFree(recvs);CHKERRQ(ierr);
9623a96401aSBarry Smith   ierr = PetscFree(nprocs);CHKERRQ(ierr);
96330dcb7c9SBarry Smith   ierr = PetscFree(sends2);CHKERRQ(ierr);
96424cf384cSBarry Smith 
96524cf384cSBarry Smith   /* put the information about myself as the first entry in the list */
96624cf384cSBarry Smith   first_procs    = (*procs)[0];
96724cf384cSBarry Smith   first_numprocs = (*numprocs)[0];
96824cf384cSBarry Smith   first_indices  = (*indices)[0];
96924cf384cSBarry Smith   for (i=0; i<*nproc; i++) {
97024cf384cSBarry Smith     if ((*procs)[i] == rank) {
97124cf384cSBarry Smith       (*procs)[0]    = (*procs)[i];
97224cf384cSBarry Smith       (*numprocs)[0] = (*numprocs)[i];
97324cf384cSBarry Smith       (*indices)[0]  = (*indices)[i];
97424cf384cSBarry Smith       (*procs)[i]    = first_procs;
97524cf384cSBarry Smith       (*numprocs)[i] = first_numprocs;
97624cf384cSBarry Smith       (*indices)[i]  = first_indices;
97724cf384cSBarry Smith       break;
97824cf384cSBarry Smith     }
97924cf384cSBarry Smith   }
98089d82c54SBarry Smith   PetscFunctionReturn(0);
98189d82c54SBarry Smith }
98289d82c54SBarry Smith 
9834a2ae208SSatish Balay #undef __FUNCT__
9844a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingRestoreInfo"
98507b52d57SBarry Smith /*@C
98607b52d57SBarry Smith     ISLocalToGlobalMappingRestoreInfo - Frees the memory allocated by ISLocalToGlobalMappingGetInfo()
98789d82c54SBarry Smith 
98807b52d57SBarry Smith     Collective on ISLocalToGlobalMapping
98907b52d57SBarry Smith 
99007b52d57SBarry Smith     Input Parameters:
99107b52d57SBarry Smith .   mapping - the mapping from local to global indexing
99207b52d57SBarry Smith 
99307b52d57SBarry Smith     Output Parameter:
99407b52d57SBarry Smith +   nproc - number of processors that are connected to this one
99507b52d57SBarry Smith .   proc - neighboring processors
99607b52d57SBarry Smith .   numproc - number of indices for each processor
99707b52d57SBarry Smith -   indices - indices of local nodes shared with neighbor (sorted by global numbering)
99807b52d57SBarry Smith 
99907b52d57SBarry Smith     Level: advanced
100007b52d57SBarry Smith 
100107b52d57SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreate(),
100207b52d57SBarry Smith           ISLocalToGlobalMappingGetInfo()
100307b52d57SBarry Smith @*/
10047087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingRestoreInfo(ISLocalToGlobalMapping mapping,PetscInt *nproc,PetscInt *procs[],PetscInt *numprocs[],PetscInt **indices[])
100507b52d57SBarry Smith {
10066849ba73SBarry Smith   PetscErrorCode ierr;
100732dcc486SBarry Smith   PetscInt       i;
100807b52d57SBarry Smith 
100907b52d57SBarry Smith   PetscFunctionBegin;
101005b42c5fSBarry Smith   ierr = PetscFree(*procs);CHKERRQ(ierr);
101105b42c5fSBarry Smith   ierr = PetscFree(*numprocs);CHKERRQ(ierr);
101200ff320aSBarry Smith   if (*indices) {
101305b42c5fSBarry Smith     ierr = PetscFree((*indices)[0]);CHKERRQ(ierr);
101400ff320aSBarry Smith     for (i=1; i<*nproc; i++) {
101505b42c5fSBarry Smith       ierr = PetscFree((*indices)[i]);CHKERRQ(ierr);
101607b52d57SBarry Smith     }
101707b52d57SBarry Smith     ierr = PetscFree(*indices);CHKERRQ(ierr);
101824cf384cSBarry Smith   }
101907b52d57SBarry Smith   PetscFunctionReturn(0);
102007b52d57SBarry Smith }
102186994e45SJed Brown 
102286994e45SJed Brown #undef __FUNCT__
102386994e45SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingGetIndices"
102486994e45SJed Brown /*@C
102586994e45SJed Brown    ISLocalToGlobalMappingGetIndices - Get global indices for every local point
102686994e45SJed Brown 
102786994e45SJed Brown    Not Collective
102886994e45SJed Brown 
102986994e45SJed Brown    Input Arguments:
103086994e45SJed Brown . ltog - local to global mapping
103186994e45SJed Brown 
103286994e45SJed Brown    Output Arguments:
103386994e45SJed Brown . array - array of indices
103486994e45SJed Brown 
103586994e45SJed Brown    Level: advanced
103686994e45SJed Brown 
103786994e45SJed Brown .seealso: ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingRestoreIndices()
103886994e45SJed Brown @*/
10397087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingGetIndices(ISLocalToGlobalMapping ltog,const PetscInt **array)
104086994e45SJed Brown {
104186994e45SJed Brown   PetscFunctionBegin;
104286994e45SJed Brown   PetscValidHeaderSpecific(ltog,IS_LTOGM_CLASSID,1);
104386994e45SJed Brown   PetscValidPointer(array,2);
104486994e45SJed Brown   *array = ltog->indices;
104586994e45SJed Brown   PetscFunctionReturn(0);
104686994e45SJed Brown }
104786994e45SJed Brown 
104886994e45SJed Brown #undef __FUNCT__
104986994e45SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingRestoreIndices"
105086994e45SJed Brown /*@C
105186994e45SJed Brown    ISLocalToGlobalMappingRestoreIndices - Restore indices obtained with ISLocalToGlobalMappingRestoreIndices()
105286994e45SJed Brown 
105386994e45SJed Brown    Not Collective
105486994e45SJed Brown 
105586994e45SJed Brown    Input Arguments:
105686994e45SJed Brown + ltog - local to global mapping
105786994e45SJed Brown - array - array of indices
105886994e45SJed Brown 
105986994e45SJed Brown    Level: advanced
106086994e45SJed Brown 
106186994e45SJed Brown .seealso: ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingGetIndices()
106286994e45SJed Brown @*/
10637087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingRestoreIndices(ISLocalToGlobalMapping ltog,const PetscInt **array)
106486994e45SJed Brown {
106586994e45SJed Brown   PetscFunctionBegin;
106686994e45SJed Brown   PetscValidHeaderSpecific(ltog,IS_LTOGM_CLASSID,1);
106786994e45SJed Brown   PetscValidPointer(array,2);
106886994e45SJed Brown   if (*array != ltog->indices) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_BADPTR,"Trying to return mismatched pointer");
10690298fd71SBarry Smith   *array = NULL;
107086994e45SJed Brown   PetscFunctionReturn(0);
107186994e45SJed Brown }
1072f7efa3c7SJed Brown 
1073f7efa3c7SJed Brown #undef __FUNCT__
1074f7efa3c7SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingConcatenate"
1075f7efa3c7SJed Brown /*@C
1076f7efa3c7SJed Brown    ISLocalToGlobalMappingConcatenate - Create a new mapping that concatenates a list of mappings
1077f7efa3c7SJed Brown 
1078f7efa3c7SJed Brown    Not Collective
1079f7efa3c7SJed Brown 
1080f7efa3c7SJed Brown    Input Arguments:
1081f7efa3c7SJed Brown + comm - communicator for the new mapping, must contain the communicator of every mapping to concatenate
1082f7efa3c7SJed Brown . n - number of mappings to concatenate
1083f7efa3c7SJed Brown - ltogs - local to global mappings
1084f7efa3c7SJed Brown 
1085f7efa3c7SJed Brown    Output Arguments:
1086f7efa3c7SJed Brown . ltogcat - new mapping
1087f7efa3c7SJed Brown 
1088f7efa3c7SJed Brown    Level: advanced
1089f7efa3c7SJed Brown 
1090f7efa3c7SJed Brown .seealso: ISLocalToGlobalMappingCreate()
1091f7efa3c7SJed Brown @*/
1092f7efa3c7SJed Brown PetscErrorCode ISLocalToGlobalMappingConcatenate(MPI_Comm comm,PetscInt n,const ISLocalToGlobalMapping ltogs[],ISLocalToGlobalMapping *ltogcat)
1093f7efa3c7SJed Brown {
1094f7efa3c7SJed Brown   PetscInt       i,cnt,m,*idx;
1095f7efa3c7SJed Brown   PetscErrorCode ierr;
1096f7efa3c7SJed Brown 
1097f7efa3c7SJed Brown   PetscFunctionBegin;
1098f7efa3c7SJed Brown   if (n < 0) SETERRQ1(comm,PETSC_ERR_ARG_OUTOFRANGE,"Must have a non-negative number of mappings, given %D",n);
1099f7efa3c7SJed Brown   if (n > 0) PetscValidPointer(ltogs,3);
1100f7efa3c7SJed Brown   for (i=0; i<n; i++) PetscValidHeaderSpecific(ltogs[i],IS_LTOGM_CLASSID,3);
1101f7efa3c7SJed Brown   PetscValidPointer(ltogcat,4);
1102f7efa3c7SJed Brown   for (cnt=0,i=0; i<n; i++) {
1103f7efa3c7SJed Brown     ierr = ISLocalToGlobalMappingGetSize(ltogs[i],&m);CHKERRQ(ierr);
1104f7efa3c7SJed Brown     cnt += m;
1105f7efa3c7SJed Brown   }
1106f7efa3c7SJed Brown   ierr = PetscMalloc(cnt*sizeof(PetscInt),&idx);CHKERRQ(ierr);
1107f7efa3c7SJed Brown   for (cnt=0,i=0; i<n; i++) {
1108f7efa3c7SJed Brown     const PetscInt *subidx;
1109f7efa3c7SJed Brown     ierr = ISLocalToGlobalMappingGetSize(ltogs[i],&m);CHKERRQ(ierr);
1110f7efa3c7SJed Brown     ierr = ISLocalToGlobalMappingGetIndices(ltogs[i],&subidx);CHKERRQ(ierr);
1111f7efa3c7SJed Brown     ierr = PetscMemcpy(&idx[cnt],subidx,m*sizeof(PetscInt));CHKERRQ(ierr);
1112f7efa3c7SJed Brown     ierr = ISLocalToGlobalMappingRestoreIndices(ltogs[i],&subidx);CHKERRQ(ierr);
1113f7efa3c7SJed Brown     cnt += m;
1114f7efa3c7SJed Brown   }
1115f7efa3c7SJed Brown   ierr = ISLocalToGlobalMappingCreate(comm,cnt,idx,PETSC_OWN_POINTER,ltogcat);CHKERRQ(ierr);
1116f7efa3c7SJed Brown   PetscFunctionReturn(0);
1117f7efa3c7SJed Brown }
1118