xref: /petsc/src/vec/is/utils/isltog.c (revision 3bb1ff401821b9e2ae019d3e61bc8ab4bd4e59d5)
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__
9186d4ecdSBarry Smith #define __FUNCT__ "ISL2GMapApply"
10186d4ecdSBarry Smith PetscErrorCode ISG2LMapApply(ISLocalToGlobalMapping mapping,PetscInt n,const PetscInt in[],PetscInt out[])
11186d4ecdSBarry Smith {
12186d4ecdSBarry Smith   PetscErrorCode ierr;
13186d4ecdSBarry Smith   PetscInt       i,*globals = mapping->globals,start = mapping->globalstart,end = mapping->globalend;
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   }
19186d4ecdSBarry Smith   for (i=0; i<n; i++) {
20186d4ecdSBarry Smith     if (in[i] < 0)          out[i] = in[i];
21186d4ecdSBarry Smith     else if (in[i] < start) out[i] = -1;
22186d4ecdSBarry Smith     else if (in[i] > end)   out[i] = -1;
23186d4ecdSBarry Smith     else                    out[i] = globals[in[i] - start];
24186d4ecdSBarry Smith   }
25186d4ecdSBarry Smith   PetscFunctionReturn(0);
26186d4ecdSBarry Smith }
27186d4ecdSBarry Smith 
28186d4ecdSBarry Smith 
29186d4ecdSBarry Smith #undef __FUNCT__
304a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingGetSize"
313b9aefa3SBarry Smith /*@C
323b9aefa3SBarry Smith     ISLocalToGlobalMappingGetSize - Gets the local size of a local to global mapping.
333b9aefa3SBarry Smith 
343b9aefa3SBarry Smith     Not Collective
353b9aefa3SBarry Smith 
363b9aefa3SBarry Smith     Input Parameter:
373b9aefa3SBarry Smith .   ltog - local to global mapping
383b9aefa3SBarry Smith 
393b9aefa3SBarry Smith     Output Parameter:
403b9aefa3SBarry Smith .   n - the number of entries in the local mapping
413b9aefa3SBarry Smith 
423b9aefa3SBarry Smith     Level: advanced
433b9aefa3SBarry Smith 
44273d9f13SBarry Smith     Concepts: mapping^local to global
453b9aefa3SBarry Smith 
463b9aefa3SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
473b9aefa3SBarry Smith @*/
487087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingGetSize(ISLocalToGlobalMapping mapping,PetscInt *n)
493b9aefa3SBarry Smith {
503b9aefa3SBarry Smith   PetscFunctionBegin;
510700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
524482741eSBarry Smith   PetscValidIntPointer(n,2);
533b9aefa3SBarry Smith   *n = mapping->n;
543b9aefa3SBarry Smith   PetscFunctionReturn(0);
553b9aefa3SBarry Smith }
563b9aefa3SBarry Smith 
574a2ae208SSatish Balay #undef __FUNCT__
584a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingView"
595a5d4f66SBarry Smith /*@C
605a5d4f66SBarry Smith     ISLocalToGlobalMappingView - View a local to global mapping
615a5d4f66SBarry Smith 
62b9cd556bSLois Curfman McInnes     Not Collective
63b9cd556bSLois Curfman McInnes 
645a5d4f66SBarry Smith     Input Parameters:
653b9aefa3SBarry Smith +   ltog - local to global mapping
663b9aefa3SBarry Smith -   viewer - viewer
675a5d4f66SBarry Smith 
68a997ad1aSLois Curfman McInnes     Level: advanced
69a997ad1aSLois Curfman McInnes 
70273d9f13SBarry Smith     Concepts: mapping^local to global
715a5d4f66SBarry Smith 
725a5d4f66SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
735a5d4f66SBarry Smith @*/
747087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingView(ISLocalToGlobalMapping mapping,PetscViewer viewer)
755a5d4f66SBarry Smith {
7632dcc486SBarry Smith   PetscInt       i;
7732dcc486SBarry Smith   PetscMPIInt    rank;
78ace3abfcSBarry Smith   PetscBool      iascii;
796849ba73SBarry Smith   PetscErrorCode ierr;
805a5d4f66SBarry Smith 
815a5d4f66SBarry Smith   PetscFunctionBegin;
820700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
833050cee2SBarry Smith   if (!viewer) {
84ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)mapping),&viewer);CHKERRQ(ierr);
853050cee2SBarry Smith   }
860700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
875a5d4f66SBarry Smith 
88ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)mapping),&rank);CHKERRQ(ierr);
89251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
9032077d6dSBarry Smith   if (iascii) {
917b23a99aSBarry Smith     ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_TRUE);CHKERRQ(ierr);
925a5d4f66SBarry Smith     for (i=0; i<mapping->n; i++) {
93b0a32e0cSBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %d %d\n",rank,i,mapping->indices[i]);CHKERRQ(ierr);
946831982aSBarry Smith     }
95b0a32e0cSBarry Smith     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
967b23a99aSBarry Smith     ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_FALSE);CHKERRQ(ierr);
977b23a99aSBarry Smith   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported for ISLocalToGlobalMapping",((PetscObject)viewer)->type_name);
985a5d4f66SBarry Smith   PetscFunctionReturn(0);
995a5d4f66SBarry Smith }
1005a5d4f66SBarry Smith 
1014a2ae208SSatish Balay #undef __FUNCT__
1024a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingCreateIS"
1031f428162SBarry Smith /*@
1042bdab257SBarry Smith     ISLocalToGlobalMappingCreateIS - Creates a mapping between a local (0 to n)
1052bdab257SBarry Smith     ordering and a global parallel ordering.
1062bdab257SBarry Smith 
1070f5bd95cSBarry Smith     Not collective
108b9cd556bSLois Curfman McInnes 
109a997ad1aSLois Curfman McInnes     Input Parameter:
1108c03b21aSDmitry Karpeev .   is - index set containing the global numbers for each local number
1112bdab257SBarry Smith 
112a997ad1aSLois Curfman McInnes     Output Parameter:
1132bdab257SBarry Smith .   mapping - new mapping data structure
1142bdab257SBarry Smith 
115a997ad1aSLois Curfman McInnes     Level: advanced
116a997ad1aSLois Curfman McInnes 
117273d9f13SBarry Smith     Concepts: mapping^local to global
1182bdab257SBarry Smith 
1192bdab257SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
1202bdab257SBarry Smith @*/
1217087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingCreateIS(IS is,ISLocalToGlobalMapping *mapping)
1222bdab257SBarry Smith {
1236849ba73SBarry Smith   PetscErrorCode ierr;
1245d0c19d7SBarry Smith   PetscInt       n;
1255d0c19d7SBarry Smith   const PetscInt *indices;
1262bdab257SBarry Smith   MPI_Comm       comm;
1273a40ed3dSBarry Smith 
1283a40ed3dSBarry Smith   PetscFunctionBegin;
1290700a824SBarry Smith   PetscValidHeaderSpecific(is,IS_CLASSID,1);
1304482741eSBarry Smith   PetscValidPointer(mapping,2);
1312bdab257SBarry Smith 
1322bdab257SBarry Smith   ierr = PetscObjectGetComm((PetscObject)is,&comm);CHKERRQ(ierr);
1333b9aefa3SBarry Smith   ierr = ISGetLocalSize(is,&n);CHKERRQ(ierr);
1342bdab257SBarry Smith   ierr = ISGetIndices(is,&indices);CHKERRQ(ierr);
135d5ad8652SBarry Smith   ierr = ISLocalToGlobalMappingCreate(comm,n,indices,PETSC_COPY_VALUES,mapping);CHKERRQ(ierr);
1362bdab257SBarry Smith   ierr = ISRestoreIndices(is,&indices);CHKERRQ(ierr);
1373a40ed3dSBarry Smith   PetscFunctionReturn(0);
1382bdab257SBarry Smith }
1395a5d4f66SBarry Smith 
140a4d96a55SJed Brown #undef __FUNCT__
141a4d96a55SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingCreateSF"
142a4d96a55SJed Brown /*@C
143a4d96a55SJed Brown     ISLocalToGlobalMappingCreateSF - Creates a mapping between a local (0 to n)
144a4d96a55SJed Brown     ordering and a global parallel ordering.
145a4d96a55SJed Brown 
146a4d96a55SJed Brown     Collective
147a4d96a55SJed Brown 
148a4d96a55SJed Brown     Input Parameter:
149a4d96a55SJed Brown +   sf - star forest mapping contiguous local indices to (rank, offset)
150a4d96a55SJed Brown -   start - first global index on this process
151a4d96a55SJed Brown 
152a4d96a55SJed Brown     Output Parameter:
153a4d96a55SJed Brown .   mapping - new mapping data structure
154a4d96a55SJed Brown 
155a4d96a55SJed Brown     Level: advanced
156a4d96a55SJed Brown 
157a4d96a55SJed Brown     Concepts: mapping^local to global
158a4d96a55SJed Brown 
159a4d96a55SJed Brown .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingCreateIS()
160a4d96a55SJed Brown @*/
161a4d96a55SJed Brown PetscErrorCode ISLocalToGlobalMappingCreateSF(PetscSF sf,PetscInt start,ISLocalToGlobalMapping *mapping)
162a4d96a55SJed Brown {
163a4d96a55SJed Brown   PetscErrorCode ierr;
164a4d96a55SJed Brown   PetscInt       i,maxlocal,nroots,nleaves,*globals,*ltog;
165a4d96a55SJed Brown   const PetscInt *ilocal;
166a4d96a55SJed Brown   MPI_Comm       comm;
167a4d96a55SJed Brown 
168a4d96a55SJed Brown   PetscFunctionBegin;
169a4d96a55SJed Brown   PetscValidHeaderSpecific(sf,PETSCSF_CLASSID,1);
170a4d96a55SJed Brown   PetscValidPointer(mapping,3);
171a4d96a55SJed Brown 
172a4d96a55SJed Brown   ierr = PetscObjectGetComm((PetscObject)sf,&comm);CHKERRQ(ierr);
1730298fd71SBarry Smith   ierr = PetscSFGetGraph(sf,&nroots,&nleaves,&ilocal,NULL);CHKERRQ(ierr);
174f6e5521dSKarl Rupp   if (ilocal) {
175f6e5521dSKarl Rupp     for (i=0,maxlocal=0; i<nleaves; i++) maxlocal = PetscMax(maxlocal,ilocal[i]+1);
176f6e5521dSKarl Rupp   }
177a4d96a55SJed Brown   else maxlocal = nleaves;
178a4d96a55SJed Brown   ierr = PetscMalloc(nroots*sizeof(PetscInt),&globals);CHKERRQ(ierr);
179a4d96a55SJed Brown   ierr = PetscMalloc(maxlocal*sizeof(PetscInt),&ltog);CHKERRQ(ierr);
180a4d96a55SJed Brown   for (i=0; i<nroots; i++) globals[i] = start + i;
181a4d96a55SJed Brown   for (i=0; i<maxlocal; i++) ltog[i] = -1;
182a4d96a55SJed Brown   ierr = PetscSFBcastBegin(sf,MPIU_INT,globals,ltog);CHKERRQ(ierr);
183a4d96a55SJed Brown   ierr = PetscSFBcastEnd(sf,MPIU_INT,globals,ltog);CHKERRQ(ierr);
184a4d96a55SJed Brown   ierr = ISLocalToGlobalMappingCreate(comm,maxlocal,ltog,PETSC_OWN_POINTER,mapping);CHKERRQ(ierr);
185a4d96a55SJed Brown   ierr = PetscFree(globals);CHKERRQ(ierr);
186a4d96a55SJed Brown   PetscFunctionReturn(0);
187a4d96a55SJed Brown }
188b46b645bSBarry Smith 
1894a2ae208SSatish Balay #undef __FUNCT__
1904a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingCreate"
191ba5bb76aSSatish Balay /*@
19290f02eecSBarry Smith     ISLocalToGlobalMappingCreate - Creates a mapping between a local (0 to n)
19390f02eecSBarry Smith     ordering and a global parallel ordering.
1942362add9SBarry Smith 
19589d82c54SBarry Smith     Not Collective, but communicator may have more than one process
196b9cd556bSLois Curfman McInnes 
1972362add9SBarry Smith     Input Parameters:
19889d82c54SBarry Smith +   comm - MPI communicator
19990f02eecSBarry Smith .   n - the number of local elements
2009669e4d8SBarry Smith .   indices - the global index for each local element, these do not need to be in increasing order (sorted)
201d5ad8652SBarry Smith -   mode - see PetscCopyMode
2022362add9SBarry Smith 
203a997ad1aSLois Curfman McInnes     Output Parameter:
20490f02eecSBarry Smith .   mapping - new mapping data structure
2052362add9SBarry Smith 
206a997ad1aSLois Curfman McInnes     Level: advanced
207a997ad1aSLois Curfman McInnes 
208273d9f13SBarry Smith     Concepts: mapping^local to global
2092362add9SBarry Smith 
210d5ad8652SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS()
2112362add9SBarry Smith @*/
2127087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingCreate(MPI_Comm cm,PetscInt n,const PetscInt indices[],PetscCopyMode mode,ISLocalToGlobalMapping *mapping)
2132362add9SBarry Smith {
2146849ba73SBarry Smith   PetscErrorCode ierr;
21532dcc486SBarry Smith   PetscInt       *in;
216b46b645bSBarry Smith 
217b46b645bSBarry Smith   PetscFunctionBegin;
21873911063SBarry Smith   if (n) PetscValidIntPointer(indices,3);
2194482741eSBarry Smith   PetscValidPointer(mapping,4);
220b46b645bSBarry Smith 
2210298fd71SBarry Smith   *mapping = NULL;
222519f805aSKarl Rupp #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
223607a6623SBarry Smith   ierr = ISInitializePackage();CHKERRQ(ierr);
2248e58c17dSMatthew Knepley #endif
2252362add9SBarry Smith 
22667c2884eSBarry Smith   ierr = PetscHeaderCreate(*mapping,_p_ISLocalToGlobalMapping,int,IS_LTOGM_CLASSID,"ISLocalToGlobalMapping","Local to global mapping","IS",
22752e6d16bSBarry Smith                            cm,ISLocalToGlobalMappingDestroy,ISLocalToGlobalMappingView);CHKERRQ(ierr);
228d4bb536fSBarry Smith   (*mapping)->n = n;
229d4bb536fSBarry Smith   /*
230d4bb536fSBarry Smith     Do not create the global to local mapping. This is only created if
231d4bb536fSBarry Smith     ISGlobalToLocalMapping() is called
232d4bb536fSBarry Smith   */
233d4bb536fSBarry Smith   (*mapping)->globals = 0;
234d5ad8652SBarry Smith   if (mode == PETSC_COPY_VALUES) {
235d5ad8652SBarry Smith     ierr = PetscMalloc(n*sizeof(PetscInt),&in);CHKERRQ(ierr);
236d5ad8652SBarry Smith     ierr = PetscMemcpy(in,indices,n*sizeof(PetscInt));CHKERRQ(ierr);
237*3bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)*mapping,n*sizeof(PetscInt));CHKERRQ(ierr);
238d5ad8652SBarry Smith     (*mapping)->indices = in;
239f6e5521dSKarl Rupp   } else if (mode == PETSC_OWN_POINTER) (*mapping)->indices = (PetscInt*)indices;
240f6e5521dSKarl Rupp   else SETERRQ(cm,PETSC_ERR_SUP,"Cannot currently use PETSC_USE_POINTER");
2413a40ed3dSBarry Smith   PetscFunctionReturn(0);
2422362add9SBarry Smith }
2432362add9SBarry Smith 
2444a2ae208SSatish Balay #undef __FUNCT__
2454a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingBlock"
246bce096a4SSatish Balay /*@
247323b833fSBarry Smith     ISLocalToGlobalMappingBlock - Creates a blocked index version of an
248323b833fSBarry Smith        ISLocalToGlobalMapping that is appropriate for MatSetLocalToGlobalMappingBlock()
249323b833fSBarry Smith        and VecSetLocalToGlobalMappingBlock().
250323b833fSBarry Smith 
251323b833fSBarry Smith     Not Collective, but communicator may have more than one process
252323b833fSBarry Smith 
253323b833fSBarry Smith     Input Parameters:
254323b833fSBarry Smith +    inmap - original point-wise mapping
255323b833fSBarry Smith -    bs - block size
256323b833fSBarry Smith 
257323b833fSBarry Smith     Output Parameter:
25869eb54c3SBarry Smith .   outmap - block based mapping; the indices are relative to BLOCKS, not individual vector or matrix entries.
259323b833fSBarry Smith 
260323b833fSBarry Smith     Level: advanced
261323b833fSBarry Smith 
262323b833fSBarry Smith     Concepts: mapping^local to global
263323b833fSBarry Smith 
264323b833fSBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingCreateIS()
265323b833fSBarry Smith @*/
2667087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingBlock(ISLocalToGlobalMapping inmap,PetscInt bs,ISLocalToGlobalMapping *outmap)
267323b833fSBarry Smith {
2686849ba73SBarry Smith   PetscErrorCode ierr;
26932dcc486SBarry Smith   PetscInt       *ii,i,n;
270323b833fSBarry Smith 
271323b833fSBarry Smith   PetscFunctionBegin;
2720700a824SBarry Smith   PetscValidHeaderSpecific(inmap,IS_LTOGM_CLASSID,1);
273b2beed0aSJed Brown   PetscValidPointer(outmap,3);
274323b833fSBarry Smith   if (bs > 1) {
275323b833fSBarry Smith     n = inmap->n/bs;
276e32f2f54SBarry Smith     if (n*bs != inmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Pointwise mapping length is not divisible by block size");
27732dcc486SBarry Smith     ierr = PetscMalloc(n*sizeof(PetscInt),&ii);CHKERRQ(ierr);
278f6e5521dSKarl Rupp     for (i=0; i<n; i++) ii[i] = inmap->indices[bs*i]/bs;
279ce94432eSBarry Smith     ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)inmap),n,ii,PETSC_OWN_POINTER,outmap);CHKERRQ(ierr);
280323b833fSBarry Smith   } else {
281323b833fSBarry Smith     ierr    = PetscObjectReference((PetscObject)inmap);CHKERRQ(ierr);
282c3122656SLisandro Dalcin     *outmap = inmap;
283323b833fSBarry Smith   }
284323b833fSBarry Smith   PetscFunctionReturn(0);
285323b833fSBarry Smith }
286323b833fSBarry Smith 
2874a2ae208SSatish Balay #undef __FUNCT__
2888ab951edSJed Brown #define __FUNCT__ "ISLocalToGlobalMappingUnBlock"
289b2beed0aSJed Brown /*@
290b2beed0aSJed Brown     ISLocalToGlobalMappingUnBlock - Creates a scalar index version of a blocked
291b2beed0aSJed Brown        ISLocalToGlobalMapping
292b2beed0aSJed Brown 
293b2beed0aSJed Brown     Not Collective, but communicator may have more than one process
294b2beed0aSJed Brown 
295b2beed0aSJed Brown     Input Parameter:
296b2beed0aSJed Brown + inmap - block based mapping; the indices are relative to BLOCKS, not individual vector or matrix entries.
297b2beed0aSJed Brown - bs - block size
298b2beed0aSJed Brown 
299b2beed0aSJed Brown     Output Parameter:
300b2beed0aSJed Brown .   outmap - pointwise mapping
301b2beed0aSJed Brown 
302b2beed0aSJed Brown     Level: advanced
303b2beed0aSJed Brown 
304b2beed0aSJed Brown     Concepts: mapping^local to global
305b2beed0aSJed Brown 
306b2beed0aSJed Brown .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingBlock()
307b2beed0aSJed Brown @*/
3087087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingUnBlock(ISLocalToGlobalMapping inmap,PetscInt bs,ISLocalToGlobalMapping *outmap)
309b2beed0aSJed Brown {
310b2beed0aSJed Brown   PetscErrorCode ierr;
311b2beed0aSJed Brown   PetscInt       *ii,i,n;
312b2beed0aSJed Brown 
313b2beed0aSJed Brown   PetscFunctionBegin;
314b2beed0aSJed Brown   PetscValidHeaderSpecific(inmap,IS_LTOGM_CLASSID,1);
315b2beed0aSJed Brown   PetscValidPointer(outmap,2);
316b2beed0aSJed Brown   if (bs > 1) {
317b2beed0aSJed Brown     n    = inmap->n*bs;
318b2beed0aSJed Brown     ierr = PetscMalloc(n*sizeof(PetscInt),&ii);CHKERRQ(ierr);
319f6e5521dSKarl Rupp     for (i=0; i<n; i++) ii[i] = inmap->indices[i/bs]*bs + (i%bs);
320ce94432eSBarry Smith     ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)inmap),n,ii,PETSC_OWN_POINTER,outmap);CHKERRQ(ierr);
321b2beed0aSJed Brown   } else {
322b2beed0aSJed Brown     ierr    = PetscObjectReference((PetscObject)inmap);CHKERRQ(ierr);
323b2beed0aSJed Brown     *outmap = inmap;
324b2beed0aSJed Brown   }
325b2beed0aSJed Brown   PetscFunctionReturn(0);
326b2beed0aSJed Brown }
327b2beed0aSJed Brown 
328b2beed0aSJed Brown #undef __FUNCT__
3294a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingDestroy"
33090f02eecSBarry Smith /*@
33190f02eecSBarry Smith    ISLocalToGlobalMappingDestroy - Destroys a mapping between a local (0 to n)
33290f02eecSBarry Smith    ordering and a global parallel ordering.
33390f02eecSBarry Smith 
3340f5bd95cSBarry Smith    Note Collective
335b9cd556bSLois Curfman McInnes 
33690f02eecSBarry Smith    Input Parameters:
33790f02eecSBarry Smith .  mapping - mapping data structure
33890f02eecSBarry Smith 
339a997ad1aSLois Curfman McInnes    Level: advanced
340a997ad1aSLois Curfman McInnes 
3413acfe500SLois Curfman McInnes .seealso: ISLocalToGlobalMappingCreate()
34290f02eecSBarry Smith @*/
3436bf464f9SBarry Smith PetscErrorCode  ISLocalToGlobalMappingDestroy(ISLocalToGlobalMapping *mapping)
34490f02eecSBarry Smith {
345dfbe8321SBarry Smith   PetscErrorCode ierr;
3465fd66863SKarl Rupp 
3473a40ed3dSBarry Smith   PetscFunctionBegin;
3486bf464f9SBarry Smith   if (!*mapping) PetscFunctionReturn(0);
3496bf464f9SBarry Smith   PetscValidHeaderSpecific((*mapping),IS_LTOGM_CLASSID,1);
350997056adSBarry Smith   if (--((PetscObject)(*mapping))->refct > 0) {*mapping = 0;PetscFunctionReturn(0);}
3516bf464f9SBarry Smith   ierr     = PetscFree((*mapping)->indices);CHKERRQ(ierr);
3526bf464f9SBarry Smith   ierr     = PetscFree((*mapping)->globals);CHKERRQ(ierr);
353d38fa0fbSBarry Smith   ierr     = PetscHeaderDestroy(mapping);CHKERRQ(ierr);
354992144d0SBarry Smith   *mapping = 0;
3553a40ed3dSBarry Smith   PetscFunctionReturn(0);
35690f02eecSBarry Smith }
35790f02eecSBarry Smith 
3584a2ae208SSatish Balay #undef __FUNCT__
3594a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingApplyIS"
36090f02eecSBarry Smith /*@
3613acfe500SLois Curfman McInnes     ISLocalToGlobalMappingApplyIS - Creates from an IS in the local numbering
3623acfe500SLois Curfman McInnes     a new index set using the global numbering defined in an ISLocalToGlobalMapping
3633acfe500SLois Curfman McInnes     context.
36490f02eecSBarry Smith 
365b9cd556bSLois Curfman McInnes     Not collective
366b9cd556bSLois Curfman McInnes 
36790f02eecSBarry Smith     Input Parameters:
368b9cd556bSLois Curfman McInnes +   mapping - mapping between local and global numbering
369b9cd556bSLois Curfman McInnes -   is - index set in local numbering
37090f02eecSBarry Smith 
37190f02eecSBarry Smith     Output Parameters:
37290f02eecSBarry Smith .   newis - index set in global numbering
37390f02eecSBarry Smith 
374a997ad1aSLois Curfman McInnes     Level: advanced
375a997ad1aSLois Curfman McInnes 
376273d9f13SBarry Smith     Concepts: mapping^local to global
3773acfe500SLois Curfman McInnes 
37890f02eecSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),
379d4bb536fSBarry Smith           ISLocalToGlobalMappingDestroy(), ISGlobalToLocalMappingApply()
38090f02eecSBarry Smith @*/
3817087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingApplyIS(ISLocalToGlobalMapping mapping,IS is,IS *newis)
38290f02eecSBarry Smith {
3836849ba73SBarry Smith   PetscErrorCode ierr;
3845d0c19d7SBarry Smith   PetscInt       n,i,*idxmap,*idxout,Nmax = mapping->n;
3855d0c19d7SBarry Smith   const PetscInt *idxin;
3863a40ed3dSBarry Smith 
3873a40ed3dSBarry Smith   PetscFunctionBegin;
3880700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
3890700a824SBarry Smith   PetscValidHeaderSpecific(is,IS_CLASSID,2);
3904482741eSBarry Smith   PetscValidPointer(newis,3);
39190f02eecSBarry Smith 
3923b9aefa3SBarry Smith   ierr   = ISGetLocalSize(is,&n);CHKERRQ(ierr);
39390f02eecSBarry Smith   ierr   = ISGetIndices(is,&idxin);CHKERRQ(ierr);
39490f02eecSBarry Smith   idxmap = mapping->indices;
39590f02eecSBarry Smith 
3967c334f02SBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&idxout);CHKERRQ(ierr);
39790f02eecSBarry Smith   for (i=0; i<n; i++) {
398e32f2f54SBarry 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);
39990f02eecSBarry Smith     idxout[i] = idxmap[idxin[i]];
40090f02eecSBarry Smith   }
4013b9aefa3SBarry Smith   ierr = ISRestoreIndices(is,&idxin);CHKERRQ(ierr);
40270b3c8c7SBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,n,idxout,PETSC_OWN_POINTER,newis);CHKERRQ(ierr);
4033a40ed3dSBarry Smith   PetscFunctionReturn(0);
40490f02eecSBarry Smith }
40590f02eecSBarry Smith 
406afcb2eb5SJed Brown #undef __FUNCT__
407afcb2eb5SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingApply"
408b89cb25eSSatish Balay /*@
4093acfe500SLois Curfman McInnes    ISLocalToGlobalMappingApply - Takes a list of integers in a local numbering
4103acfe500SLois Curfman McInnes    and converts them to the global numbering.
41190f02eecSBarry Smith 
412b9cd556bSLois Curfman McInnes    Not collective
413b9cd556bSLois Curfman McInnes 
414bb25748dSBarry Smith    Input Parameters:
415b9cd556bSLois Curfman McInnes +  mapping - the local to global mapping context
416bb25748dSBarry Smith .  N - number of integers
417b9cd556bSLois Curfman McInnes -  in - input indices in local numbering
418bb25748dSBarry Smith 
419bb25748dSBarry Smith    Output Parameter:
420bb25748dSBarry Smith .  out - indices in global numbering
421bb25748dSBarry Smith 
422b9cd556bSLois Curfman McInnes    Notes:
423b9cd556bSLois Curfman McInnes    The in and out array parameters may be identical.
424d4bb536fSBarry Smith 
425a997ad1aSLois Curfman McInnes    Level: advanced
426a997ad1aSLois Curfman McInnes 
427bb25748dSBarry Smith .seealso: ISLocalToGlobalMappingCreate(),ISLocalToGlobalMappingDestroy(),
4280752156aSBarry Smith           ISLocalToGlobalMappingApplyIS(),AOCreateBasic(),AOApplicationToPetsc(),
429d4bb536fSBarry Smith           AOPetscToApplication(), ISGlobalToLocalMappingApply()
430bb25748dSBarry Smith 
431273d9f13SBarry Smith     Concepts: mapping^local to global
432afcb2eb5SJed Brown @*/
433afcb2eb5SJed Brown PetscErrorCode ISLocalToGlobalMappingApply(ISLocalToGlobalMapping mapping,PetscInt N,const PetscInt in[],PetscInt out[])
434afcb2eb5SJed Brown {
435afcb2eb5SJed Brown   PetscInt       i,Nmax = mapping->n;
436afcb2eb5SJed Brown   const PetscInt *idx = mapping->indices;
437d4bb536fSBarry Smith 
438afcb2eb5SJed Brown   PetscFunctionBegin;
439afcb2eb5SJed Brown   for (i=0; i<N; i++) {
440afcb2eb5SJed Brown     if (in[i] < 0) {
441afcb2eb5SJed Brown       out[i] = in[i];
442afcb2eb5SJed Brown       continue;
443afcb2eb5SJed Brown     }
444afcb2eb5SJed 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);
445afcb2eb5SJed Brown     out[i] = idx[in[i]];
446afcb2eb5SJed Brown   }
447afcb2eb5SJed Brown   PetscFunctionReturn(0);
448afcb2eb5SJed Brown }
449d4bb536fSBarry Smith 
450d4bb536fSBarry Smith /* -----------------------------------------------------------------------------------------*/
451d4bb536fSBarry Smith 
4524a2ae208SSatish Balay #undef __FUNCT__
4534a2ae208SSatish Balay #define __FUNCT__ "ISGlobalToLocalMappingSetUp_Private"
454d4bb536fSBarry Smith /*
455d4bb536fSBarry Smith     Creates the global fields in the ISLocalToGlobalMapping structure
456d4bb536fSBarry Smith */
4576849ba73SBarry Smith static PetscErrorCode ISGlobalToLocalMappingSetUp_Private(ISLocalToGlobalMapping mapping)
458d4bb536fSBarry Smith {
4596849ba73SBarry Smith   PetscErrorCode ierr;
46032dcc486SBarry Smith   PetscInt       i,*idx = mapping->indices,n = mapping->n,end,start,*globals;
461d4bb536fSBarry Smith 
4623a40ed3dSBarry Smith   PetscFunctionBegin;
463d4bb536fSBarry Smith   end   = 0;
464ec268f7cSJed Brown   start = PETSC_MAX_INT;
465d4bb536fSBarry Smith 
466d4bb536fSBarry Smith   for (i=0; i<n; i++) {
467d4bb536fSBarry Smith     if (idx[i] < 0) continue;
468d4bb536fSBarry Smith     if (idx[i] < start) start = idx[i];
469d4bb536fSBarry Smith     if (idx[i] > end)   end   = idx[i];
470d4bb536fSBarry Smith   }
471d4bb536fSBarry Smith   if (start > end) {start = 0; end = -1;}
472d4bb536fSBarry Smith   mapping->globalstart = start;
473d4bb536fSBarry Smith   mapping->globalend   = end;
474d4bb536fSBarry Smith 
47532dcc486SBarry Smith   ierr             = PetscMalloc((end-start+2)*sizeof(PetscInt),&globals);CHKERRQ(ierr);
476b0a32e0cSBarry Smith   mapping->globals = globals;
477f6e5521dSKarl Rupp   for (i=0; i<end-start+1; i++) globals[i] = -1;
478d4bb536fSBarry Smith   for (i=0; i<n; i++) {
479d4bb536fSBarry Smith     if (idx[i] < 0) continue;
480d4bb536fSBarry Smith     globals[idx[i] - start] = i;
481d4bb536fSBarry Smith   }
482d4bb536fSBarry Smith 
483*3bb1ff40SBarry Smith   ierr = PetscLogObjectMemory((PetscObject)mapping,(end-start+1)*sizeof(PetscInt));CHKERRQ(ierr);
4843a40ed3dSBarry Smith   PetscFunctionReturn(0);
485d4bb536fSBarry Smith }
486d4bb536fSBarry Smith 
4874a2ae208SSatish Balay #undef __FUNCT__
4884a2ae208SSatish Balay #define __FUNCT__ "ISGlobalToLocalMappingApply"
489d4bb536fSBarry Smith /*@
490a997ad1aSLois Curfman McInnes     ISGlobalToLocalMappingApply - Provides the local numbering for a list of integers
491a997ad1aSLois Curfman McInnes     specified with a global numbering.
492d4bb536fSBarry Smith 
493b9cd556bSLois Curfman McInnes     Not collective
494b9cd556bSLois Curfman McInnes 
495d4bb536fSBarry Smith     Input Parameters:
496b9cd556bSLois Curfman McInnes +   mapping - mapping between local and global numbering
497d4bb536fSBarry Smith .   type - IS_GTOLM_MASK - replaces global indices with no local value with -1
498d4bb536fSBarry Smith            IS_GTOLM_DROP - drops the indices with no local value from the output list
499d4bb536fSBarry Smith .   n - number of global indices to map
500b9cd556bSLois Curfman McInnes -   idx - global indices to map
501d4bb536fSBarry Smith 
502d4bb536fSBarry Smith     Output Parameters:
503b9cd556bSLois Curfman McInnes +   nout - number of indices in output array (if type == IS_GTOLM_MASK then nout = n)
504b9cd556bSLois Curfman McInnes -   idxout - local index of each global index, one must pass in an array long enough
505e182c471SBarry Smith              to hold all the indices. You can call ISGlobalToLocalMappingApply() with
5060298fd71SBarry Smith              idxout == NULL to determine the required length (returned in nout)
507e182c471SBarry Smith              and then allocate the required space and call ISGlobalToLocalMappingApply()
508e182c471SBarry Smith              a second time to set the values.
509d4bb536fSBarry Smith 
510b9cd556bSLois Curfman McInnes     Notes:
5110298fd71SBarry Smith     Either nout or idxout may be NULL. idx and idxout may be identical.
512d4bb536fSBarry Smith 
5130f5bd95cSBarry Smith     This is not scalable in memory usage. Each processor requires O(Nglobal) size
5140f5bd95cSBarry Smith     array to compute these.
5150f5bd95cSBarry Smith 
516a997ad1aSLois Curfman McInnes     Level: advanced
517a997ad1aSLois Curfman McInnes 
51832fd6b96SBarry Smith     Developer Note: The manual page states that idx and idxout may be identical but the calling
51932fd6b96SBarry Smith        sequence declares idx as const so it cannot be the same as idxout.
52032fd6b96SBarry Smith 
521273d9f13SBarry Smith     Concepts: mapping^global to local
522d4bb536fSBarry Smith 
523d4bb536fSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),
524d4bb536fSBarry Smith           ISLocalToGlobalMappingDestroy()
525d4bb536fSBarry Smith @*/
5267087cfbeSBarry Smith PetscErrorCode  ISGlobalToLocalMappingApply(ISLocalToGlobalMapping mapping,ISGlobalToLocalMappingType type,
52732dcc486SBarry Smith                                   PetscInt n,const PetscInt idx[],PetscInt *nout,PetscInt idxout[])
528d4bb536fSBarry Smith {
52932dcc486SBarry Smith   PetscInt       i,*globals,nf = 0,tmp,start,end;
5306849ba73SBarry Smith   PetscErrorCode ierr;
531d4bb536fSBarry Smith 
5323a40ed3dSBarry Smith   PetscFunctionBegin;
5330700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
534d4bb536fSBarry Smith   if (!mapping->globals) {
535d4bb536fSBarry Smith     ierr = ISGlobalToLocalMappingSetUp_Private(mapping);CHKERRQ(ierr);
536d4bb536fSBarry Smith   }
537d4bb536fSBarry Smith   globals = mapping->globals;
538d4bb536fSBarry Smith   start   = mapping->globalstart;
539d4bb536fSBarry Smith   end     = mapping->globalend;
540d4bb536fSBarry Smith 
541d4bb536fSBarry Smith   if (type == IS_GTOLM_MASK) {
542d4bb536fSBarry Smith     if (idxout) {
543d4bb536fSBarry Smith       for (i=0; i<n; i++) {
544d4bb536fSBarry Smith         if (idx[i] < 0) idxout[i] = idx[i];
545d4bb536fSBarry Smith         else if (idx[i] < start) idxout[i] = -1;
546d4bb536fSBarry Smith         else if (idx[i] > end)   idxout[i] = -1;
547d4bb536fSBarry Smith         else                     idxout[i] = globals[idx[i] - start];
548d4bb536fSBarry Smith       }
549d4bb536fSBarry Smith     }
550d4bb536fSBarry Smith     if (nout) *nout = n;
551d4bb536fSBarry Smith   } else {
552d4bb536fSBarry Smith     if (idxout) {
553d4bb536fSBarry Smith       for (i=0; i<n; i++) {
554d4bb536fSBarry Smith         if (idx[i] < 0) continue;
555d4bb536fSBarry Smith         if (idx[i] < start) continue;
556d4bb536fSBarry Smith         if (idx[i] > end) continue;
557d4bb536fSBarry Smith         tmp = globals[idx[i] - start];
558d4bb536fSBarry Smith         if (tmp < 0) continue;
559d4bb536fSBarry Smith         idxout[nf++] = tmp;
560d4bb536fSBarry Smith       }
561d4bb536fSBarry Smith     } else {
562d4bb536fSBarry Smith       for (i=0; i<n; i++) {
563d4bb536fSBarry Smith         if (idx[i] < 0) continue;
564d4bb536fSBarry Smith         if (idx[i] < start) continue;
565d4bb536fSBarry Smith         if (idx[i] > end) continue;
566d4bb536fSBarry Smith         tmp = globals[idx[i] - start];
567d4bb536fSBarry Smith         if (tmp < 0) continue;
568d4bb536fSBarry Smith         nf++;
569d4bb536fSBarry Smith       }
570d4bb536fSBarry Smith     }
571d4bb536fSBarry Smith     if (nout) *nout = nf;
572d4bb536fSBarry Smith   }
5733a40ed3dSBarry Smith   PetscFunctionReturn(0);
574d4bb536fSBarry Smith }
57590f02eecSBarry Smith 
5764a2ae208SSatish Balay #undef __FUNCT__
5774a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingGetInfo"
57889d82c54SBarry Smith /*@C
57989d82c54SBarry Smith     ISLocalToGlobalMappingGetInfo - Gets the neighbor information for each processor and
58089d82c54SBarry Smith      each index shared by more than one processor
58189d82c54SBarry Smith 
58289d82c54SBarry Smith     Collective on ISLocalToGlobalMapping
58389d82c54SBarry Smith 
58489d82c54SBarry Smith     Input Parameters:
58589d82c54SBarry Smith .   mapping - the mapping from local to global indexing
58689d82c54SBarry Smith 
58789d82c54SBarry Smith     Output Parameter:
58889d82c54SBarry Smith +   nproc - number of processors that are connected to this one
58989d82c54SBarry Smith .   proc - neighboring processors
59007b52d57SBarry Smith .   numproc - number of indices for each subdomain (processor)
5913463a7baSJed Brown -   indices - indices of nodes (in local numbering) shared with neighbors (sorted by global numbering)
59289d82c54SBarry Smith 
59389d82c54SBarry Smith     Level: advanced
59489d82c54SBarry Smith 
595273d9f13SBarry Smith     Concepts: mapping^local to global
59689d82c54SBarry Smith 
5972cfcea29SBarry Smith     Fortran Usage:
5982cfcea29SBarry Smith $        ISLocalToGlobalMpngGetInfoSize(ISLocalToGlobalMapping,PetscInt nproc,PetscInt numprocmax,ierr) followed by
5992cfcea29SBarry Smith $        ISLocalToGlobalMappingGetInfo(ISLocalToGlobalMapping,PetscInt nproc, PetscInt procs[nproc],PetscInt numprocs[nproc],
6002cfcea29SBarry Smith           PetscInt indices[nproc][numprocmax],ierr)
6012cfcea29SBarry Smith         There is no ISLocalToGlobalMappingRestoreInfo() in Fortran. You must make sure that procs[], numprocs[] and
6022cfcea29SBarry Smith         indices[][] are large enough arrays, either by allocating them dynamically or defining static ones large enough.
6032cfcea29SBarry Smith 
6042cfcea29SBarry Smith 
60507b52d57SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreate(),
60607b52d57SBarry Smith           ISLocalToGlobalMappingRestoreInfo()
60789d82c54SBarry Smith @*/
6087087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingGetInfo(ISLocalToGlobalMapping mapping,PetscInt *nproc,PetscInt *procs[],PetscInt *numprocs[],PetscInt **indices[])
60989d82c54SBarry Smith {
6106849ba73SBarry Smith   PetscErrorCode ierr;
61197f1f81fSBarry Smith   PetscMPIInt    size,rank,tag1,tag2,tag3,*len,*source,imdex;
61232dcc486SBarry Smith   PetscInt       i,n = mapping->n,Ng,ng,max = 0,*lindices = mapping->indices;
61332dcc486SBarry Smith   PetscInt       *nprocs,*owner,nsends,*sends,j,*starts,nmax,nrecvs,*recvs,proc;
61497f1f81fSBarry Smith   PetscInt       cnt,scale,*ownedsenders,*nownedsenders,rstart,nowned;
61532dcc486SBarry Smith   PetscInt       node,nownedm,nt,*sends2,nsends2,*starts2,*lens2,*dest,nrecvs2,*starts3,*recvs2,k,*bprocs,*tmp;
61632dcc486SBarry Smith   PetscInt       first_procs,first_numprocs,*first_indices;
61789d82c54SBarry Smith   MPI_Request    *recv_waits,*send_waits;
61830dcb7c9SBarry Smith   MPI_Status     recv_status,*send_status,*recv_statuses;
619ce94432eSBarry Smith   MPI_Comm       comm;
620ace3abfcSBarry Smith   PetscBool      debug = PETSC_FALSE;
62189d82c54SBarry Smith 
62289d82c54SBarry Smith   PetscFunctionBegin;
6230700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
624ce94432eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)mapping,&comm);CHKERRQ(ierr);
62524cf384cSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
62624cf384cSBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
62724cf384cSBarry Smith   if (size == 1) {
62824cf384cSBarry Smith     *nproc         = 0;
6290298fd71SBarry Smith     *procs         = NULL;
63032dcc486SBarry Smith     ierr           = PetscMalloc(sizeof(PetscInt),numprocs);CHKERRQ(ierr);
6311e2105dcSBarry Smith     (*numprocs)[0] = 0;
63232dcc486SBarry Smith     ierr           = PetscMalloc(sizeof(PetscInt*),indices);CHKERRQ(ierr);
6330298fd71SBarry Smith     (*indices)[0]  = NULL;
63424cf384cSBarry Smith     PetscFunctionReturn(0);
63524cf384cSBarry Smith   }
63624cf384cSBarry Smith 
6370298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-islocaltoglobalmappinggetinfo_debug",&debug,NULL);CHKERRQ(ierr);
63807b52d57SBarry Smith 
6393677ff5aSBarry Smith   /*
6403677ff5aSBarry Smith     Notes on ISLocalToGlobalMappingGetInfo
6413677ff5aSBarry Smith 
6423677ff5aSBarry Smith     globally owned node - the nodes that have been assigned to this processor in global
6433677ff5aSBarry Smith            numbering, just for this routine.
6443677ff5aSBarry Smith 
6453677ff5aSBarry Smith     nontrivial globally owned node - node assigned to this processor that is on a subdomain
6463677ff5aSBarry Smith            boundary (i.e. is has more than one local owner)
6473677ff5aSBarry Smith 
6483677ff5aSBarry Smith     locally owned node - node that exists on this processors subdomain
6493677ff5aSBarry Smith 
6503677ff5aSBarry Smith     nontrivial locally owned node - node that is not in the interior (i.e. has more than one
6513677ff5aSBarry Smith            local subdomain
6523677ff5aSBarry Smith   */
65324cf384cSBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mapping,&tag1);CHKERRQ(ierr);
65424cf384cSBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mapping,&tag2);CHKERRQ(ierr);
65524cf384cSBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mapping,&tag3);CHKERRQ(ierr);
65689d82c54SBarry Smith 
65789d82c54SBarry Smith   for (i=0; i<n; i++) {
65889d82c54SBarry Smith     if (lindices[i] > max) max = lindices[i];
65989d82c54SBarry Smith   }
66032dcc486SBarry Smith   ierr   = MPI_Allreduce(&max,&Ng,1,MPIU_INT,MPI_MAX,comm);CHKERRQ(ierr);
66178058e43SBarry Smith   Ng++;
66289d82c54SBarry Smith   ierr   = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
66389d82c54SBarry Smith   ierr   = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
664bc8ff85bSBarry Smith   scale  = Ng/size + 1;
665a2e34c3dSBarry Smith   ng     = scale; if (rank == size-1) ng = Ng - scale*(size-1); ng = PetscMax(1,ng);
666caba0dd0SBarry Smith   rstart = scale*rank;
66789d82c54SBarry Smith 
66889d82c54SBarry Smith   /* determine ownership ranges of global indices */
6697c334f02SBarry Smith   ierr = PetscMalloc(2*size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr);
67032dcc486SBarry Smith   ierr = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr);
67189d82c54SBarry Smith 
67289d82c54SBarry Smith   /* determine owners of each local node  */
6737c334f02SBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&owner);CHKERRQ(ierr);
67489d82c54SBarry Smith   for (i=0; i<n; i++) {
6753677ff5aSBarry Smith     proc             = lindices[i]/scale; /* processor that globally owns this index */
67627c402fcSBarry Smith     nprocs[2*proc+1] = 1;                 /* processor globally owns at least one of ours */
6773677ff5aSBarry Smith     owner[i]         = proc;
67827c402fcSBarry Smith     nprocs[2*proc]++;                     /* count of how many that processor globally owns of ours */
67989d82c54SBarry Smith   }
68027c402fcSBarry Smith   nsends = 0; for (i=0; i<size; i++) nsends += nprocs[2*i+1];
6811e2582c4SBarry Smith   ierr = PetscInfo1(mapping,"Number of global owners for my local data %d\n",nsends);CHKERRQ(ierr);
68289d82c54SBarry Smith 
68389d82c54SBarry Smith   /* inform other processors of number of messages and max length*/
68427c402fcSBarry Smith   ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr);
6851e2582c4SBarry Smith   ierr = PetscInfo1(mapping,"Number of local owners for my global data %d\n",nrecvs);CHKERRQ(ierr);
68689d82c54SBarry Smith 
68789d82c54SBarry Smith   /* post receives for owned rows */
68832dcc486SBarry Smith   ierr = PetscMalloc((2*nrecvs+1)*(nmax+1)*sizeof(PetscInt),&recvs);CHKERRQ(ierr);
689b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
69089d82c54SBarry Smith   for (i=0; i<nrecvs; i++) {
69132dcc486SBarry Smith     ierr = MPI_Irecv(recvs+2*nmax*i,2*nmax,MPIU_INT,MPI_ANY_SOURCE,tag1,comm,recv_waits+i);CHKERRQ(ierr);
69289d82c54SBarry Smith   }
69389d82c54SBarry Smith 
69489d82c54SBarry Smith   /* pack messages containing lists of local nodes to owners */
69532dcc486SBarry Smith   ierr      = PetscMalloc((2*n+1)*sizeof(PetscInt),&sends);CHKERRQ(ierr);
69632dcc486SBarry Smith   ierr      = PetscMalloc((size+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr);
69789d82c54SBarry Smith   starts[0] = 0;
698f6e5521dSKarl Rupp   for (i=1; i<size; i++) starts[i] = starts[i-1] + 2*nprocs[2*i-2];
69989d82c54SBarry Smith   for (i=0; i<n; i++) {
70089d82c54SBarry Smith     sends[starts[owner[i]]++] = lindices[i];
70130dcb7c9SBarry Smith     sends[starts[owner[i]]++] = i;
70289d82c54SBarry Smith   }
70389d82c54SBarry Smith   ierr = PetscFree(owner);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 
70789d82c54SBarry Smith   /* send the messages */
708b0a32e0cSBarry Smith   ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr);
70932dcc486SBarry Smith   ierr = PetscMalloc((nsends+1)*sizeof(PetscInt),&dest);CHKERRQ(ierr);
71089d82c54SBarry Smith   cnt = 0;
71189d82c54SBarry Smith   for (i=0; i<size; i++) {
71227c402fcSBarry Smith     if (nprocs[2*i]) {
71332dcc486SBarry Smith       ierr      = MPI_Isend(sends+starts[i],2*nprocs[2*i],MPIU_INT,i,tag1,comm,send_waits+cnt);CHKERRQ(ierr);
71430dcb7c9SBarry Smith       dest[cnt] = i;
71589d82c54SBarry Smith       cnt++;
71689d82c54SBarry Smith     }
71789d82c54SBarry Smith   }
71889d82c54SBarry Smith   ierr = PetscFree(starts);CHKERRQ(ierr);
71989d82c54SBarry Smith 
72089d82c54SBarry Smith   /* wait on receives */
72197f1f81fSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(PetscMPIInt),&source);CHKERRQ(ierr);
72297f1f81fSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(PetscMPIInt),&len);CHKERRQ(ierr);
72389d82c54SBarry Smith   cnt  = nrecvs;
72432dcc486SBarry Smith   ierr = PetscMalloc((ng+1)*sizeof(PetscInt),&nownedsenders);CHKERRQ(ierr);
72532dcc486SBarry Smith   ierr = PetscMemzero(nownedsenders,ng*sizeof(PetscInt));CHKERRQ(ierr);
72689d82c54SBarry Smith   while (cnt) {
72789d82c54SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
72889d82c54SBarry Smith     /* unpack receives into our local space */
72932dcc486SBarry Smith     ierr          = MPI_Get_count(&recv_status,MPIU_INT,&len[imdex]);CHKERRQ(ierr);
73089d82c54SBarry Smith     source[imdex] = recv_status.MPI_SOURCE;
73130dcb7c9SBarry Smith     len[imdex]    = len[imdex]/2;
732caba0dd0SBarry Smith     /* count how many local owners for each of my global owned indices */
73330dcb7c9SBarry Smith     for (i=0; i<len[imdex]; i++) nownedsenders[recvs[2*imdex*nmax+2*i]-rstart]++;
73489d82c54SBarry Smith     cnt--;
73589d82c54SBarry Smith   }
73689d82c54SBarry Smith   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
73789d82c54SBarry Smith 
73830dcb7c9SBarry Smith   /* count how many globally owned indices are on an edge multiplied by how many processors own them. */
739bc8ff85bSBarry Smith   nowned  = 0;
740bc8ff85bSBarry Smith   nownedm = 0;
741bc8ff85bSBarry Smith   for (i=0; i<ng; i++) {
742bc8ff85bSBarry Smith     if (nownedsenders[i] > 1) {nownedm += nownedsenders[i]; nowned++;}
743bc8ff85bSBarry Smith   }
744bc8ff85bSBarry Smith 
745bc8ff85bSBarry Smith   /* create single array to contain rank of all local owners of each globally owned index */
74632dcc486SBarry Smith   ierr      = PetscMalloc((nownedm+1)*sizeof(PetscInt),&ownedsenders);CHKERRQ(ierr);
74732dcc486SBarry Smith   ierr      = PetscMalloc((ng+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr);
748bc8ff85bSBarry Smith   starts[0] = 0;
749bc8ff85bSBarry Smith   for (i=1; i<ng; i++) {
750bc8ff85bSBarry Smith     if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1];
751bc8ff85bSBarry Smith     else starts[i] = starts[i-1];
752bc8ff85bSBarry Smith   }
753bc8ff85bSBarry Smith 
75430dcb7c9SBarry Smith   /* for each nontrival globally owned node list all arriving processors */
755bc8ff85bSBarry Smith   for (i=0; i<nrecvs; i++) {
756bc8ff85bSBarry Smith     for (j=0; j<len[i]; j++) {
75730dcb7c9SBarry Smith       node = recvs[2*i*nmax+2*j]-rstart;
758f6e5521dSKarl Rupp       if (nownedsenders[node] > 1) ownedsenders[starts[node]++] = source[i];
759bc8ff85bSBarry Smith     }
760bc8ff85bSBarry Smith   }
761bc8ff85bSBarry Smith 
76207b52d57SBarry Smith   if (debug) { /* -----------------------------------  */
76330dcb7c9SBarry Smith     starts[0] = 0;
76430dcb7c9SBarry Smith     for (i=1; i<ng; i++) {
76530dcb7c9SBarry Smith       if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1];
76630dcb7c9SBarry Smith       else starts[i] = starts[i-1];
76730dcb7c9SBarry Smith     }
76830dcb7c9SBarry Smith     for (i=0; i<ng; i++) {
76930dcb7c9SBarry Smith       if (nownedsenders[i] > 1) {
77030dcb7c9SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"[%d] global node %d local owner processors: ",rank,i+rstart);CHKERRQ(ierr);
77130dcb7c9SBarry Smith         for (j=0; j<nownedsenders[i]; j++) {
77230dcb7c9SBarry Smith           ierr = PetscSynchronizedPrintf(comm,"%d ",ownedsenders[starts[i]+j]);CHKERRQ(ierr);
77330dcb7c9SBarry Smith         }
77430dcb7c9SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"\n");CHKERRQ(ierr);
77530dcb7c9SBarry Smith       }
77630dcb7c9SBarry Smith     }
77730dcb7c9SBarry Smith     ierr = PetscSynchronizedFlush(comm);CHKERRQ(ierr);
77807b52d57SBarry Smith   } /* -----------------------------------  */
77930dcb7c9SBarry Smith 
7803677ff5aSBarry Smith   /* wait on original sends */
7813a96401aSBarry Smith   if (nsends) {
782b0a32e0cSBarry Smith     ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
7833a96401aSBarry Smith     ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
7843a96401aSBarry Smith     ierr = PetscFree(send_status);CHKERRQ(ierr);
7853a96401aSBarry Smith   }
78689d82c54SBarry Smith   ierr = PetscFree(send_waits);CHKERRQ(ierr);
7873a96401aSBarry Smith   ierr = PetscFree(sends);CHKERRQ(ierr);
7883677ff5aSBarry Smith   ierr = PetscFree(nprocs);CHKERRQ(ierr);
7893677ff5aSBarry Smith 
7903677ff5aSBarry Smith   /* pack messages to send back to local owners */
79130dcb7c9SBarry Smith   starts[0] = 0;
79230dcb7c9SBarry Smith   for (i=1; i<ng; i++) {
79330dcb7c9SBarry Smith     if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1];
79430dcb7c9SBarry Smith     else starts[i] = starts[i-1];
79530dcb7c9SBarry Smith   }
79630dcb7c9SBarry Smith   nsends2 = nrecvs;
79732dcc486SBarry Smith   ierr    = PetscMalloc((nsends2+1)*sizeof(PetscInt),&nprocs);CHKERRQ(ierr); /* length of each message */
79830dcb7c9SBarry Smith   for (i=0; i<nrecvs; i++) {
79930dcb7c9SBarry Smith     nprocs[i] = 1;
80030dcb7c9SBarry Smith     for (j=0; j<len[i]; j++) {
80130dcb7c9SBarry Smith       node = recvs[2*i*nmax+2*j]-rstart;
802f6e5521dSKarl Rupp       if (nownedsenders[node] > 1) nprocs[i] += 2 + nownedsenders[node];
80330dcb7c9SBarry Smith     }
80430dcb7c9SBarry Smith   }
805f6e5521dSKarl Rupp   nt = 0;
806f6e5521dSKarl Rupp   for (i=0; i<nsends2; i++) nt += nprocs[i];
807f6e5521dSKarl Rupp 
80832dcc486SBarry Smith   ierr = PetscMalloc((nt+1)*sizeof(PetscInt),&sends2);CHKERRQ(ierr);
80932dcc486SBarry Smith   ierr = PetscMalloc((nsends2+1)*sizeof(PetscInt),&starts2);CHKERRQ(ierr);
810f6e5521dSKarl Rupp 
811f6e5521dSKarl Rupp   starts2[0] = 0;
812f6e5521dSKarl Rupp   for (i=1; i<nsends2; i++) starts2[i] = starts2[i-1] + nprocs[i-1];
81330dcb7c9SBarry Smith   /*
81430dcb7c9SBarry Smith      Each message is 1 + nprocs[i] long, and consists of
81530dcb7c9SBarry Smith        (0) the number of nodes being sent back
81630dcb7c9SBarry Smith        (1) the local node number,
81730dcb7c9SBarry Smith        (2) the number of processors sharing it,
81830dcb7c9SBarry Smith        (3) the processors sharing it
81930dcb7c9SBarry Smith   */
82030dcb7c9SBarry Smith   for (i=0; i<nsends2; i++) {
82130dcb7c9SBarry Smith     cnt = 1;
82230dcb7c9SBarry Smith     sends2[starts2[i]] = 0;
82330dcb7c9SBarry Smith     for (j=0; j<len[i]; j++) {
82430dcb7c9SBarry Smith       node = recvs[2*i*nmax+2*j]-rstart;
82530dcb7c9SBarry Smith       if (nownedsenders[node] > 1) {
82630dcb7c9SBarry Smith         sends2[starts2[i]]++;
82730dcb7c9SBarry Smith         sends2[starts2[i]+cnt++] = recvs[2*i*nmax+2*j+1];
82830dcb7c9SBarry Smith         sends2[starts2[i]+cnt++] = nownedsenders[node];
82932dcc486SBarry Smith         ierr = PetscMemcpy(&sends2[starts2[i]+cnt],&ownedsenders[starts[node]],nownedsenders[node]*sizeof(PetscInt));CHKERRQ(ierr);
83030dcb7c9SBarry Smith         cnt += nownedsenders[node];
83130dcb7c9SBarry Smith       }
83230dcb7c9SBarry Smith     }
83330dcb7c9SBarry Smith   }
83430dcb7c9SBarry Smith 
83530dcb7c9SBarry Smith   /* receive the message lengths */
83630dcb7c9SBarry Smith   nrecvs2 = nsends;
83732dcc486SBarry Smith   ierr    = PetscMalloc((nrecvs2+1)*sizeof(PetscInt),&lens2);CHKERRQ(ierr);
83832dcc486SBarry Smith   ierr    = PetscMalloc((nrecvs2+1)*sizeof(PetscInt),&starts3);CHKERRQ(ierr);
839d44834fbSBarry Smith   ierr    = PetscMalloc((nrecvs2+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
84030dcb7c9SBarry Smith   for (i=0; i<nrecvs2; i++) {
841d44834fbSBarry Smith     ierr = MPI_Irecv(&lens2[i],1,MPIU_INT,dest[i],tag2,comm,recv_waits+i);CHKERRQ(ierr);
84230dcb7c9SBarry Smith   }
843d44834fbSBarry Smith 
8448a8e0b3aSBarry Smith   /* send the message lengths */
8458a8e0b3aSBarry Smith   for (i=0; i<nsends2; i++) {
8468a8e0b3aSBarry Smith     ierr = MPI_Send(&nprocs[i],1,MPIU_INT,source[i],tag2,comm);CHKERRQ(ierr);
8478a8e0b3aSBarry Smith   }
8488a8e0b3aSBarry Smith 
849d44834fbSBarry Smith   /* wait on receives of lens */
8500c468ba9SBarry Smith   if (nrecvs2) {
8510c468ba9SBarry Smith     ierr = PetscMalloc(nrecvs2*sizeof(MPI_Status),&recv_statuses);CHKERRQ(ierr);
852d44834fbSBarry Smith     ierr = MPI_Waitall(nrecvs2,recv_waits,recv_statuses);CHKERRQ(ierr);
853d44834fbSBarry Smith     ierr = PetscFree(recv_statuses);CHKERRQ(ierr);
8540c468ba9SBarry Smith   }
855a2ea699eSBarry Smith   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
856d44834fbSBarry Smith 
85730dcb7c9SBarry Smith   starts3[0] = 0;
858d44834fbSBarry Smith   nt         = 0;
85930dcb7c9SBarry Smith   for (i=0; i<nrecvs2-1; i++) {
86030dcb7c9SBarry Smith     starts3[i+1] = starts3[i] + lens2[i];
861d44834fbSBarry Smith     nt          += lens2[i];
86230dcb7c9SBarry Smith   }
86376466f69SStefano Zampini   if (nrecvs2) nt += lens2[nrecvs2-1];
864d44834fbSBarry Smith 
86532dcc486SBarry Smith   ierr = PetscMalloc((nt+1)*sizeof(PetscInt),&recvs2);CHKERRQ(ierr);
866b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs2+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
86752b72c4aSBarry Smith   for (i=0; i<nrecvs2; i++) {
86832dcc486SBarry Smith     ierr = MPI_Irecv(recvs2+starts3[i],lens2[i],MPIU_INT,dest[i],tag3,comm,recv_waits+i);CHKERRQ(ierr);
86930dcb7c9SBarry Smith   }
87030dcb7c9SBarry Smith 
87130dcb7c9SBarry Smith   /* send the messages */
872b0a32e0cSBarry Smith   ierr = PetscMalloc((nsends2+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr);
87330dcb7c9SBarry Smith   for (i=0; i<nsends2; i++) {
87432dcc486SBarry Smith     ierr = MPI_Isend(sends2+starts2[i],nprocs[i],MPIU_INT,source[i],tag3,comm,send_waits+i);CHKERRQ(ierr);
87530dcb7c9SBarry Smith   }
87630dcb7c9SBarry Smith 
87730dcb7c9SBarry Smith   /* wait on receives */
8780c468ba9SBarry Smith   if (nrecvs2) {
8790c468ba9SBarry Smith     ierr = PetscMalloc(nrecvs2*sizeof(MPI_Status),&recv_statuses);CHKERRQ(ierr);
88030dcb7c9SBarry Smith     ierr = MPI_Waitall(nrecvs2,recv_waits,recv_statuses);CHKERRQ(ierr);
88130dcb7c9SBarry Smith     ierr = PetscFree(recv_statuses);CHKERRQ(ierr);
8820c468ba9SBarry Smith   }
88330dcb7c9SBarry Smith   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
88430dcb7c9SBarry Smith   ierr = PetscFree(nprocs);CHKERRQ(ierr);
88530dcb7c9SBarry Smith 
88607b52d57SBarry Smith   if (debug) { /* -----------------------------------  */
88730dcb7c9SBarry Smith     cnt = 0;
88830dcb7c9SBarry Smith     for (i=0; i<nrecvs2; i++) {
88930dcb7c9SBarry Smith       nt = recvs2[cnt++];
89030dcb7c9SBarry Smith       for (j=0; j<nt; j++) {
89130dcb7c9SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"[%d] local node %d number of subdomains %d: ",rank,recvs2[cnt],recvs2[cnt+1]);CHKERRQ(ierr);
89230dcb7c9SBarry Smith         for (k=0; k<recvs2[cnt+1]; k++) {
89330dcb7c9SBarry Smith           ierr = PetscSynchronizedPrintf(comm,"%d ",recvs2[cnt+2+k]);CHKERRQ(ierr);
89430dcb7c9SBarry Smith         }
89530dcb7c9SBarry Smith         cnt += 2 + recvs2[cnt+1];
89630dcb7c9SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"\n");CHKERRQ(ierr);
89730dcb7c9SBarry Smith       }
89830dcb7c9SBarry Smith     }
89930dcb7c9SBarry Smith     ierr = PetscSynchronizedFlush(comm);CHKERRQ(ierr);
90007b52d57SBarry Smith   } /* -----------------------------------  */
90130dcb7c9SBarry Smith 
90230dcb7c9SBarry Smith   /* count number subdomains for each local node */
90332dcc486SBarry Smith   ierr = PetscMalloc(size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr);
90432dcc486SBarry Smith   ierr = PetscMemzero(nprocs,size*sizeof(PetscInt));CHKERRQ(ierr);
90530dcb7c9SBarry Smith   cnt  = 0;
90630dcb7c9SBarry Smith   for (i=0; i<nrecvs2; i++) {
90730dcb7c9SBarry Smith     nt = recvs2[cnt++];
90830dcb7c9SBarry Smith     for (j=0; j<nt; j++) {
909f6e5521dSKarl Rupp       for (k=0; k<recvs2[cnt+1]; k++) nprocs[recvs2[cnt+2+k]]++;
91030dcb7c9SBarry Smith       cnt += 2 + recvs2[cnt+1];
91130dcb7c9SBarry Smith     }
91230dcb7c9SBarry Smith   }
91330dcb7c9SBarry Smith   nt = 0; for (i=0; i<size; i++) nt += (nprocs[i] > 0);
91430dcb7c9SBarry Smith   *nproc    = nt;
91532dcc486SBarry Smith   ierr = PetscMalloc((nt+1)*sizeof(PetscInt),procs);CHKERRQ(ierr);
91632dcc486SBarry Smith   ierr = PetscMalloc((nt+1)*sizeof(PetscInt),numprocs);CHKERRQ(ierr);
91732dcc486SBarry Smith   ierr = PetscMalloc((nt+1)*sizeof(PetscInt*),indices);CHKERRQ(ierr);
9180298fd71SBarry Smith   for (i=0;i<nt+1;i++) (*indices)[i]=NULL;
91932dcc486SBarry Smith   ierr = PetscMalloc(size*sizeof(PetscInt),&bprocs);CHKERRQ(ierr);
92030dcb7c9SBarry Smith   cnt       = 0;
92130dcb7c9SBarry Smith   for (i=0; i<size; i++) {
92230dcb7c9SBarry Smith     if (nprocs[i] > 0) {
92330dcb7c9SBarry Smith       bprocs[i]        = cnt;
92430dcb7c9SBarry Smith       (*procs)[cnt]    = i;
92530dcb7c9SBarry Smith       (*numprocs)[cnt] = nprocs[i];
92632dcc486SBarry Smith       ierr             = PetscMalloc(nprocs[i]*sizeof(PetscInt),&(*indices)[cnt]);CHKERRQ(ierr);
92730dcb7c9SBarry Smith       cnt++;
92830dcb7c9SBarry Smith     }
92930dcb7c9SBarry Smith   }
93030dcb7c9SBarry Smith 
93130dcb7c9SBarry Smith   /* make the list of subdomains for each nontrivial local node */
93232dcc486SBarry Smith   ierr = PetscMemzero(*numprocs,nt*sizeof(PetscInt));CHKERRQ(ierr);
93330dcb7c9SBarry Smith   cnt  = 0;
93430dcb7c9SBarry Smith   for (i=0; i<nrecvs2; i++) {
93530dcb7c9SBarry Smith     nt = recvs2[cnt++];
93630dcb7c9SBarry Smith     for (j=0; j<nt; j++) {
937f6e5521dSKarl Rupp       for (k=0; k<recvs2[cnt+1]; k++) (*indices)[bprocs[recvs2[cnt+2+k]]][(*numprocs)[bprocs[recvs2[cnt+2+k]]]++] = recvs2[cnt];
93830dcb7c9SBarry Smith       cnt += 2 + recvs2[cnt+1];
93930dcb7c9SBarry Smith     }
94030dcb7c9SBarry Smith   }
94130dcb7c9SBarry Smith   ierr = PetscFree(bprocs);CHKERRQ(ierr);
94207b52d57SBarry Smith   ierr = PetscFree(recvs2);CHKERRQ(ierr);
94330dcb7c9SBarry Smith 
94407b52d57SBarry Smith   /* sort the node indexing by their global numbers */
94507b52d57SBarry Smith   nt = *nproc;
94607b52d57SBarry Smith   for (i=0; i<nt; i++) {
94732dcc486SBarry Smith     ierr = PetscMalloc(((*numprocs)[i])*sizeof(PetscInt),&tmp);CHKERRQ(ierr);
948f6e5521dSKarl Rupp     for (j=0; j<(*numprocs)[i]; j++) tmp[j] = lindices[(*indices)[i][j]];
94907b52d57SBarry Smith     ierr = PetscSortIntWithArray((*numprocs)[i],tmp,(*indices)[i]);CHKERRQ(ierr);
95007b52d57SBarry Smith     ierr = PetscFree(tmp);CHKERRQ(ierr);
95107b52d57SBarry Smith   }
95207b52d57SBarry Smith 
95307b52d57SBarry Smith   if (debug) { /* -----------------------------------  */
95430dcb7c9SBarry Smith     nt = *nproc;
95530dcb7c9SBarry Smith     for (i=0; i<nt; i++) {
95630dcb7c9SBarry Smith       ierr = PetscSynchronizedPrintf(comm,"[%d] subdomain %d number of indices %d: ",rank,(*procs)[i],(*numprocs)[i]);CHKERRQ(ierr);
95730dcb7c9SBarry Smith       for (j=0; j<(*numprocs)[i]; j++) {
95830dcb7c9SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"%d ",(*indices)[i][j]);CHKERRQ(ierr);
95930dcb7c9SBarry Smith       }
96030dcb7c9SBarry Smith       ierr = PetscSynchronizedPrintf(comm,"\n");CHKERRQ(ierr);
96130dcb7c9SBarry Smith     }
96230dcb7c9SBarry Smith     ierr = PetscSynchronizedFlush(comm);CHKERRQ(ierr);
96307b52d57SBarry Smith   } /* -----------------------------------  */
96430dcb7c9SBarry Smith 
96530dcb7c9SBarry Smith   /* wait on sends */
96630dcb7c9SBarry Smith   if (nsends2) {
967b0a32e0cSBarry Smith     ierr = PetscMalloc(nsends2*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
96830dcb7c9SBarry Smith     ierr = MPI_Waitall(nsends2,send_waits,send_status);CHKERRQ(ierr);
96930dcb7c9SBarry Smith     ierr = PetscFree(send_status);CHKERRQ(ierr);
97030dcb7c9SBarry Smith   }
97130dcb7c9SBarry Smith 
97230dcb7c9SBarry Smith   ierr = PetscFree(starts3);CHKERRQ(ierr);
97330dcb7c9SBarry Smith   ierr = PetscFree(dest);CHKERRQ(ierr);
97430dcb7c9SBarry Smith   ierr = PetscFree(send_waits);CHKERRQ(ierr);
9753677ff5aSBarry Smith 
976bc8ff85bSBarry Smith   ierr = PetscFree(nownedsenders);CHKERRQ(ierr);
977bc8ff85bSBarry Smith   ierr = PetscFree(ownedsenders);CHKERRQ(ierr);
978bc8ff85bSBarry Smith   ierr = PetscFree(starts);CHKERRQ(ierr);
97930dcb7c9SBarry Smith   ierr = PetscFree(starts2);CHKERRQ(ierr);
98030dcb7c9SBarry Smith   ierr = PetscFree(lens2);CHKERRQ(ierr);
98189d82c54SBarry Smith 
98289d82c54SBarry Smith   ierr = PetscFree(source);CHKERRQ(ierr);
98397f1f81fSBarry Smith   ierr = PetscFree(len);CHKERRQ(ierr);
98489d82c54SBarry Smith   ierr = PetscFree(recvs);CHKERRQ(ierr);
9853a96401aSBarry Smith   ierr = PetscFree(nprocs);CHKERRQ(ierr);
98630dcb7c9SBarry Smith   ierr = PetscFree(sends2);CHKERRQ(ierr);
98724cf384cSBarry Smith 
98824cf384cSBarry Smith   /* put the information about myself as the first entry in the list */
98924cf384cSBarry Smith   first_procs    = (*procs)[0];
99024cf384cSBarry Smith   first_numprocs = (*numprocs)[0];
99124cf384cSBarry Smith   first_indices  = (*indices)[0];
99224cf384cSBarry Smith   for (i=0; i<*nproc; i++) {
99324cf384cSBarry Smith     if ((*procs)[i] == rank) {
99424cf384cSBarry Smith       (*procs)[0]    = (*procs)[i];
99524cf384cSBarry Smith       (*numprocs)[0] = (*numprocs)[i];
99624cf384cSBarry Smith       (*indices)[0]  = (*indices)[i];
99724cf384cSBarry Smith       (*procs)[i]    = first_procs;
99824cf384cSBarry Smith       (*numprocs)[i] = first_numprocs;
99924cf384cSBarry Smith       (*indices)[i]  = first_indices;
100024cf384cSBarry Smith       break;
100124cf384cSBarry Smith     }
100224cf384cSBarry Smith   }
100389d82c54SBarry Smith   PetscFunctionReturn(0);
100489d82c54SBarry Smith }
100589d82c54SBarry Smith 
10064a2ae208SSatish Balay #undef __FUNCT__
10074a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingRestoreInfo"
100807b52d57SBarry Smith /*@C
100907b52d57SBarry Smith     ISLocalToGlobalMappingRestoreInfo - Frees the memory allocated by ISLocalToGlobalMappingGetInfo()
101089d82c54SBarry Smith 
101107b52d57SBarry Smith     Collective on ISLocalToGlobalMapping
101207b52d57SBarry Smith 
101307b52d57SBarry Smith     Input Parameters:
101407b52d57SBarry Smith .   mapping - the mapping from local to global indexing
101507b52d57SBarry Smith 
101607b52d57SBarry Smith     Output Parameter:
101707b52d57SBarry Smith +   nproc - number of processors that are connected to this one
101807b52d57SBarry Smith .   proc - neighboring processors
101907b52d57SBarry Smith .   numproc - number of indices for each processor
102007b52d57SBarry Smith -   indices - indices of local nodes shared with neighbor (sorted by global numbering)
102107b52d57SBarry Smith 
102207b52d57SBarry Smith     Level: advanced
102307b52d57SBarry Smith 
102407b52d57SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreate(),
102507b52d57SBarry Smith           ISLocalToGlobalMappingGetInfo()
102607b52d57SBarry Smith @*/
10277087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingRestoreInfo(ISLocalToGlobalMapping mapping,PetscInt *nproc,PetscInt *procs[],PetscInt *numprocs[],PetscInt **indices[])
102807b52d57SBarry Smith {
10296849ba73SBarry Smith   PetscErrorCode ierr;
103032dcc486SBarry Smith   PetscInt       i;
103107b52d57SBarry Smith 
103207b52d57SBarry Smith   PetscFunctionBegin;
103305b42c5fSBarry Smith   ierr = PetscFree(*procs);CHKERRQ(ierr);
103405b42c5fSBarry Smith   ierr = PetscFree(*numprocs);CHKERRQ(ierr);
103500ff320aSBarry Smith   if (*indices) {
103605b42c5fSBarry Smith     ierr = PetscFree((*indices)[0]);CHKERRQ(ierr);
103700ff320aSBarry Smith     for (i=1; i<*nproc; i++) {
103805b42c5fSBarry Smith       ierr = PetscFree((*indices)[i]);CHKERRQ(ierr);
103907b52d57SBarry Smith     }
104007b52d57SBarry Smith     ierr = PetscFree(*indices);CHKERRQ(ierr);
104124cf384cSBarry Smith   }
104207b52d57SBarry Smith   PetscFunctionReturn(0);
104307b52d57SBarry Smith }
104486994e45SJed Brown 
104586994e45SJed Brown #undef __FUNCT__
104686994e45SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingGetIndices"
104786994e45SJed Brown /*@C
104886994e45SJed Brown    ISLocalToGlobalMappingGetIndices - Get global indices for every local point
104986994e45SJed Brown 
105086994e45SJed Brown    Not Collective
105186994e45SJed Brown 
105286994e45SJed Brown    Input Arguments:
105386994e45SJed Brown . ltog - local to global mapping
105486994e45SJed Brown 
105586994e45SJed Brown    Output Arguments:
105686994e45SJed Brown . array - array of indices
105786994e45SJed Brown 
105886994e45SJed Brown    Level: advanced
105986994e45SJed Brown 
106086994e45SJed Brown .seealso: ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingRestoreIndices()
106186994e45SJed Brown @*/
10627087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingGetIndices(ISLocalToGlobalMapping ltog,const PetscInt **array)
106386994e45SJed Brown {
106486994e45SJed Brown   PetscFunctionBegin;
106586994e45SJed Brown   PetscValidHeaderSpecific(ltog,IS_LTOGM_CLASSID,1);
106686994e45SJed Brown   PetscValidPointer(array,2);
106786994e45SJed Brown   *array = ltog->indices;
106886994e45SJed Brown   PetscFunctionReturn(0);
106986994e45SJed Brown }
107086994e45SJed Brown 
107186994e45SJed Brown #undef __FUNCT__
107286994e45SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingRestoreIndices"
107386994e45SJed Brown /*@C
107486994e45SJed Brown    ISLocalToGlobalMappingRestoreIndices - Restore indices obtained with ISLocalToGlobalMappingRestoreIndices()
107586994e45SJed Brown 
107686994e45SJed Brown    Not Collective
107786994e45SJed Brown 
107886994e45SJed Brown    Input Arguments:
107986994e45SJed Brown + ltog - local to global mapping
108086994e45SJed Brown - array - array of indices
108186994e45SJed Brown 
108286994e45SJed Brown    Level: advanced
108386994e45SJed Brown 
108486994e45SJed Brown .seealso: ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingGetIndices()
108586994e45SJed Brown @*/
10867087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingRestoreIndices(ISLocalToGlobalMapping ltog,const PetscInt **array)
108786994e45SJed Brown {
108886994e45SJed Brown   PetscFunctionBegin;
108986994e45SJed Brown   PetscValidHeaderSpecific(ltog,IS_LTOGM_CLASSID,1);
109086994e45SJed Brown   PetscValidPointer(array,2);
109186994e45SJed Brown   if (*array != ltog->indices) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_BADPTR,"Trying to return mismatched pointer");
10920298fd71SBarry Smith   *array = NULL;
109386994e45SJed Brown   PetscFunctionReturn(0);
109486994e45SJed Brown }
1095f7efa3c7SJed Brown 
1096f7efa3c7SJed Brown #undef __FUNCT__
1097f7efa3c7SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingConcatenate"
1098f7efa3c7SJed Brown /*@C
1099f7efa3c7SJed Brown    ISLocalToGlobalMappingConcatenate - Create a new mapping that concatenates a list of mappings
1100f7efa3c7SJed Brown 
1101f7efa3c7SJed Brown    Not Collective
1102f7efa3c7SJed Brown 
1103f7efa3c7SJed Brown    Input Arguments:
1104f7efa3c7SJed Brown + comm - communicator for the new mapping, must contain the communicator of every mapping to concatenate
1105f7efa3c7SJed Brown . n - number of mappings to concatenate
1106f7efa3c7SJed Brown - ltogs - local to global mappings
1107f7efa3c7SJed Brown 
1108f7efa3c7SJed Brown    Output Arguments:
1109f7efa3c7SJed Brown . ltogcat - new mapping
1110f7efa3c7SJed Brown 
1111f7efa3c7SJed Brown    Level: advanced
1112f7efa3c7SJed Brown 
1113f7efa3c7SJed Brown .seealso: ISLocalToGlobalMappingCreate()
1114f7efa3c7SJed Brown @*/
1115f7efa3c7SJed Brown PetscErrorCode ISLocalToGlobalMappingConcatenate(MPI_Comm comm,PetscInt n,const ISLocalToGlobalMapping ltogs[],ISLocalToGlobalMapping *ltogcat)
1116f7efa3c7SJed Brown {
1117f7efa3c7SJed Brown   PetscInt       i,cnt,m,*idx;
1118f7efa3c7SJed Brown   PetscErrorCode ierr;
1119f7efa3c7SJed Brown 
1120f7efa3c7SJed Brown   PetscFunctionBegin;
1121f7efa3c7SJed Brown   if (n < 0) SETERRQ1(comm,PETSC_ERR_ARG_OUTOFRANGE,"Must have a non-negative number of mappings, given %D",n);
1122f7efa3c7SJed Brown   if (n > 0) PetscValidPointer(ltogs,3);
1123f7efa3c7SJed Brown   for (i=0; i<n; i++) PetscValidHeaderSpecific(ltogs[i],IS_LTOGM_CLASSID,3);
1124f7efa3c7SJed Brown   PetscValidPointer(ltogcat,4);
1125f7efa3c7SJed Brown   for (cnt=0,i=0; i<n; i++) {
1126f7efa3c7SJed Brown     ierr = ISLocalToGlobalMappingGetSize(ltogs[i],&m);CHKERRQ(ierr);
1127f7efa3c7SJed Brown     cnt += m;
1128f7efa3c7SJed Brown   }
1129f7efa3c7SJed Brown   ierr = PetscMalloc(cnt*sizeof(PetscInt),&idx);CHKERRQ(ierr);
1130f7efa3c7SJed Brown   for (cnt=0,i=0; i<n; i++) {
1131f7efa3c7SJed Brown     const PetscInt *subidx;
1132f7efa3c7SJed Brown     ierr = ISLocalToGlobalMappingGetSize(ltogs[i],&m);CHKERRQ(ierr);
1133f7efa3c7SJed Brown     ierr = ISLocalToGlobalMappingGetIndices(ltogs[i],&subidx);CHKERRQ(ierr);
1134f7efa3c7SJed Brown     ierr = PetscMemcpy(&idx[cnt],subidx,m*sizeof(PetscInt));CHKERRQ(ierr);
1135f7efa3c7SJed Brown     ierr = ISLocalToGlobalMappingRestoreIndices(ltogs[i],&subidx);CHKERRQ(ierr);
1136f7efa3c7SJed Brown     cnt += m;
1137f7efa3c7SJed Brown   }
1138f7efa3c7SJed Brown   ierr = ISLocalToGlobalMappingCreate(comm,cnt,idx,PETSC_OWN_POINTER,ltogcat);CHKERRQ(ierr);
1139f7efa3c7SJed Brown   PetscFunctionReturn(0);
1140f7efa3c7SJed Brown }
1141