xref: /petsc/src/vec/is/utils/isltog.c (revision f0413b6f5d7c9354ff89613ff0ab87d5c3d1b2ee)
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 
804a59952SBarry Smith 
94a2ae208SSatish Balay #undef __FUNCT__
10743c9b42SStefano Zampini #define __FUNCT__ "ISG2LMapApply"
11186d4ecdSBarry Smith PetscErrorCode ISG2LMapApply(ISLocalToGlobalMapping mapping,PetscInt n,const PetscInt in[],PetscInt out[])
12186d4ecdSBarry Smith {
13186d4ecdSBarry Smith   PetscErrorCode ierr;
14743c9b42SStefano Zampini   PetscInt       i,start,end;
15186d4ecdSBarry Smith 
16186d4ecdSBarry Smith   PetscFunctionBegin;
17186d4ecdSBarry Smith   if (!mapping->globals) {
18186d4ecdSBarry Smith     ierr = ISGlobalToLocalMappingApply(mapping,IS_GTOLM_MASK,0,0,0,0);CHKERRQ(ierr);
19186d4ecdSBarry Smith   }
20743c9b42SStefano Zampini   start = mapping->globalstart;
21743c9b42SStefano Zampini   end = mapping->globalend;
22186d4ecdSBarry Smith   for (i=0; i<n; i++) {
23186d4ecdSBarry Smith     if (in[i] < 0)          out[i] = in[i];
24186d4ecdSBarry Smith     else if (in[i] < start) out[i] = -1;
25186d4ecdSBarry Smith     else if (in[i] > end)   out[i] = -1;
26743c9b42SStefano Zampini     else                    out[i] = mapping->globals[in[i] - start];
27186d4ecdSBarry Smith   }
28186d4ecdSBarry Smith   PetscFunctionReturn(0);
29186d4ecdSBarry Smith }
30186d4ecdSBarry Smith 
31186d4ecdSBarry Smith 
32186d4ecdSBarry Smith #undef __FUNCT__
334a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingGetSize"
343b9aefa3SBarry Smith /*@C
353b9aefa3SBarry Smith     ISLocalToGlobalMappingGetSize - Gets the local size of a local to global mapping.
363b9aefa3SBarry Smith 
373b9aefa3SBarry Smith     Not Collective
383b9aefa3SBarry Smith 
393b9aefa3SBarry Smith     Input Parameter:
403b9aefa3SBarry Smith .   ltog - local to global mapping
413b9aefa3SBarry Smith 
423b9aefa3SBarry Smith     Output Parameter:
433b9aefa3SBarry Smith .   n - the number of entries in the local mapping
443b9aefa3SBarry Smith 
453b9aefa3SBarry Smith     Level: advanced
463b9aefa3SBarry Smith 
47273d9f13SBarry Smith     Concepts: mapping^local to global
483b9aefa3SBarry Smith 
493b9aefa3SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
503b9aefa3SBarry Smith @*/
517087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingGetSize(ISLocalToGlobalMapping mapping,PetscInt *n)
523b9aefa3SBarry Smith {
533b9aefa3SBarry Smith   PetscFunctionBegin;
540700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
554482741eSBarry Smith   PetscValidIntPointer(n,2);
563b9aefa3SBarry Smith   *n = mapping->n;
573b9aefa3SBarry Smith   PetscFunctionReturn(0);
583b9aefa3SBarry Smith }
593b9aefa3SBarry Smith 
604a2ae208SSatish Balay #undef __FUNCT__
614a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingView"
625a5d4f66SBarry Smith /*@C
635a5d4f66SBarry Smith     ISLocalToGlobalMappingView - View a local to global mapping
645a5d4f66SBarry Smith 
65b9cd556bSLois Curfman McInnes     Not Collective
66b9cd556bSLois Curfman McInnes 
675a5d4f66SBarry Smith     Input Parameters:
683b9aefa3SBarry Smith +   ltog - local to global mapping
693b9aefa3SBarry Smith -   viewer - viewer
705a5d4f66SBarry Smith 
71a997ad1aSLois Curfman McInnes     Level: advanced
72a997ad1aSLois Curfman McInnes 
73273d9f13SBarry Smith     Concepts: mapping^local to global
745a5d4f66SBarry Smith 
755a5d4f66SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
765a5d4f66SBarry Smith @*/
777087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingView(ISLocalToGlobalMapping mapping,PetscViewer viewer)
785a5d4f66SBarry Smith {
7932dcc486SBarry Smith   PetscInt       i;
8032dcc486SBarry Smith   PetscMPIInt    rank;
81ace3abfcSBarry Smith   PetscBool      iascii;
826849ba73SBarry Smith   PetscErrorCode ierr;
835a5d4f66SBarry Smith 
845a5d4f66SBarry Smith   PetscFunctionBegin;
850700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
863050cee2SBarry Smith   if (!viewer) {
87ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)mapping),&viewer);CHKERRQ(ierr);
883050cee2SBarry Smith   }
890700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
905a5d4f66SBarry Smith 
91ce94432eSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)mapping),&rank);CHKERRQ(ierr);
92251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
9332077d6dSBarry Smith   if (iascii) {
9498c3331eSBarry Smith     ierr = PetscObjectPrintClassNamePrefixType((PetscObject)mapping,viewer);CHKERRQ(ierr);
957b23a99aSBarry Smith     ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_TRUE);CHKERRQ(ierr);
965a5d4f66SBarry Smith     for (i=0; i<mapping->n; i++) {
977904a332SBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %D %D\n",rank,i,mapping->indices[i]);CHKERRQ(ierr);
986831982aSBarry Smith     }
99b0a32e0cSBarry Smith     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
1007b23a99aSBarry Smith     ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_FALSE);CHKERRQ(ierr);
1017b23a99aSBarry Smith   } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported for ISLocalToGlobalMapping",((PetscObject)viewer)->type_name);
1025a5d4f66SBarry Smith   PetscFunctionReturn(0);
1035a5d4f66SBarry Smith }
1045a5d4f66SBarry Smith 
1054a2ae208SSatish Balay #undef __FUNCT__
1064a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingCreateIS"
1071f428162SBarry Smith /*@
1082bdab257SBarry Smith     ISLocalToGlobalMappingCreateIS - Creates a mapping between a local (0 to n)
1092bdab257SBarry Smith     ordering and a global parallel ordering.
1102bdab257SBarry Smith 
1110f5bd95cSBarry Smith     Not collective
112b9cd556bSLois Curfman McInnes 
113a997ad1aSLois Curfman McInnes     Input Parameter:
1148c03b21aSDmitry Karpeev .   is - index set containing the global numbers for each local number
1152bdab257SBarry Smith 
116a997ad1aSLois Curfman McInnes     Output Parameter:
1172bdab257SBarry Smith .   mapping - new mapping data structure
1182bdab257SBarry Smith 
119*f0413b6fSBarry Smith     Notes: the block size of the IS determines the block size of the mapping
120a997ad1aSLois Curfman McInnes     Level: advanced
121a997ad1aSLois Curfman McInnes 
122273d9f13SBarry Smith     Concepts: mapping^local to global
1232bdab257SBarry Smith 
1242bdab257SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
1252bdab257SBarry Smith @*/
1267087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingCreateIS(IS is,ISLocalToGlobalMapping *mapping)
1272bdab257SBarry Smith {
1286849ba73SBarry Smith   PetscErrorCode ierr;
1293bbf0e92SBarry Smith   PetscInt       n,bs;
1305d0c19d7SBarry Smith   const PetscInt *indices;
1312bdab257SBarry Smith   MPI_Comm       comm;
1323bbf0e92SBarry Smith   PetscBool      isblock;
1333a40ed3dSBarry Smith 
1343a40ed3dSBarry Smith   PetscFunctionBegin;
1350700a824SBarry Smith   PetscValidHeaderSpecific(is,IS_CLASSID,1);
1364482741eSBarry Smith   PetscValidPointer(mapping,2);
1372bdab257SBarry Smith 
1382bdab257SBarry Smith   ierr = PetscObjectGetComm((PetscObject)is,&comm);CHKERRQ(ierr);
1393b9aefa3SBarry Smith   ierr = ISGetLocalSize(is,&n);CHKERRQ(ierr);
1403bbf0e92SBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)is,ISBLOCK,&isblock);CHKERRQ(ierr);
1413bbf0e92SBarry Smith   ierr = ISGetBlockSize(is,&bs);CHKERRQ(ierr);
142*f0413b6fSBarry Smith   /*  if (!isblock) { */
143*f0413b6fSBarry Smith     ierr = ISGetIndices(is,&indices);CHKERRQ(ierr);
144*f0413b6fSBarry Smith     ierr = ISLocalToGlobalMappingCreate(comm,1,n,indices,PETSC_COPY_VALUES,mapping);CHKERRQ(ierr);
1452bdab257SBarry Smith     ierr = ISRestoreIndices(is,&indices);CHKERRQ(ierr);
146*f0413b6fSBarry Smith     /*  } else {
147*f0413b6fSBarry Smith     ierr = ISBlockGetIndices(is,&indices);CHKERRQ(ierr);
148*f0413b6fSBarry Smith     ierr = ISLocalToGlobalMappingCreate(comm,bs,n,indices,PETSC_COPY_VALUES,mapping);CHKERRQ(ierr);
149*f0413b6fSBarry Smith     ierr = ISBlockRestoreIndices(is,&indices);CHKERRQ(ierr);
150*f0413b6fSBarry Smith      }*/
1513a40ed3dSBarry Smith   PetscFunctionReturn(0);
1522bdab257SBarry Smith }
1535a5d4f66SBarry Smith 
154a4d96a55SJed Brown #undef __FUNCT__
155a4d96a55SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingCreateSF"
156a4d96a55SJed Brown /*@C
157a4d96a55SJed Brown     ISLocalToGlobalMappingCreateSF - Creates a mapping between a local (0 to n)
158a4d96a55SJed Brown     ordering and a global parallel ordering.
159a4d96a55SJed Brown 
160a4d96a55SJed Brown     Collective
161a4d96a55SJed Brown 
162a4d96a55SJed Brown     Input Parameter:
163a4d96a55SJed Brown +   sf - star forest mapping contiguous local indices to (rank, offset)
164a4d96a55SJed Brown -   start - first global index on this process
165a4d96a55SJed Brown 
166a4d96a55SJed Brown     Output Parameter:
167a4d96a55SJed Brown .   mapping - new mapping data structure
168a4d96a55SJed Brown 
169a4d96a55SJed Brown     Level: advanced
170a4d96a55SJed Brown 
171a4d96a55SJed Brown     Concepts: mapping^local to global
172a4d96a55SJed Brown 
173a4d96a55SJed Brown .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingCreateIS()
174a4d96a55SJed Brown @*/
175a4d96a55SJed Brown PetscErrorCode ISLocalToGlobalMappingCreateSF(PetscSF sf,PetscInt start,ISLocalToGlobalMapping *mapping)
176a4d96a55SJed Brown {
177a4d96a55SJed Brown   PetscErrorCode ierr;
178a4d96a55SJed Brown   PetscInt       i,maxlocal,nroots,nleaves,*globals,*ltog;
179a4d96a55SJed Brown   const PetscInt *ilocal;
180a4d96a55SJed Brown   MPI_Comm       comm;
181a4d96a55SJed Brown 
182a4d96a55SJed Brown   PetscFunctionBegin;
183a4d96a55SJed Brown   PetscValidHeaderSpecific(sf,PETSCSF_CLASSID,1);
184a4d96a55SJed Brown   PetscValidPointer(mapping,3);
185a4d96a55SJed Brown 
186a4d96a55SJed Brown   ierr = PetscObjectGetComm((PetscObject)sf,&comm);CHKERRQ(ierr);
1870298fd71SBarry Smith   ierr = PetscSFGetGraph(sf,&nroots,&nleaves,&ilocal,NULL);CHKERRQ(ierr);
188f6e5521dSKarl Rupp   if (ilocal) {
189f6e5521dSKarl Rupp     for (i=0,maxlocal=0; i<nleaves; i++) maxlocal = PetscMax(maxlocal,ilocal[i]+1);
190f6e5521dSKarl Rupp   }
191a4d96a55SJed Brown   else maxlocal = nleaves;
192785e854fSJed Brown   ierr = PetscMalloc1(nroots,&globals);CHKERRQ(ierr);
193785e854fSJed Brown   ierr = PetscMalloc1(maxlocal,&ltog);CHKERRQ(ierr);
194a4d96a55SJed Brown   for (i=0; i<nroots; i++) globals[i] = start + i;
195a4d96a55SJed Brown   for (i=0; i<maxlocal; i++) ltog[i] = -1;
196a4d96a55SJed Brown   ierr = PetscSFBcastBegin(sf,MPIU_INT,globals,ltog);CHKERRQ(ierr);
197a4d96a55SJed Brown   ierr = PetscSFBcastEnd(sf,MPIU_INT,globals,ltog);CHKERRQ(ierr);
198*f0413b6fSBarry Smith   ierr = ISLocalToGlobalMappingCreate(comm,1,maxlocal,ltog,PETSC_OWN_POINTER,mapping);CHKERRQ(ierr);
199a4d96a55SJed Brown   ierr = PetscFree(globals);CHKERRQ(ierr);
200a4d96a55SJed Brown   PetscFunctionReturn(0);
201a4d96a55SJed Brown }
202b46b645bSBarry Smith 
2034a2ae208SSatish Balay #undef __FUNCT__
2044a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingCreate"
205ba5bb76aSSatish Balay /*@
20690f02eecSBarry Smith     ISLocalToGlobalMappingCreate - Creates a mapping between a local (0 to n)
20790f02eecSBarry Smith     ordering and a global parallel ordering.
2082362add9SBarry Smith 
20989d82c54SBarry Smith     Not Collective, but communicator may have more than one process
210b9cd556bSLois Curfman McInnes 
2112362add9SBarry Smith     Input Parameters:
21289d82c54SBarry Smith +   comm - MPI communicator
213*f0413b6fSBarry Smith .   bs - the block size
21490f02eecSBarry Smith .   n - the number of local elements
215*f0413b6fSBarry Smith .   indices - the global index for each local element, these do not need to be in increasing order (sorted), these values should not be scaled by the blocksize bs
216d5ad8652SBarry Smith -   mode - see PetscCopyMode
2172362add9SBarry Smith 
218a997ad1aSLois Curfman McInnes     Output Parameter:
21990f02eecSBarry Smith .   mapping - new mapping data structure
2202362add9SBarry Smith 
221*f0413b6fSBarry Smith     Notes: There is one integer value in indices per block and it represents the actual indices bs*idx + j, where j=0,..,bs-1
222a997ad1aSLois Curfman McInnes     Level: advanced
223a997ad1aSLois Curfman McInnes 
224273d9f13SBarry Smith     Concepts: mapping^local to global
2252362add9SBarry Smith 
226d5ad8652SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS()
2272362add9SBarry Smith @*/
228*f0413b6fSBarry Smith PetscErrorCode  ISLocalToGlobalMappingCreate(MPI_Comm cm,PetscInt bs,PetscInt n,const PetscInt indices[],PetscCopyMode mode,ISLocalToGlobalMapping *mapping)
2292362add9SBarry Smith {
2306849ba73SBarry Smith   PetscErrorCode ierr;
23132dcc486SBarry Smith   PetscInt       *in;
232b46b645bSBarry Smith 
233b46b645bSBarry Smith   PetscFunctionBegin;
23473911063SBarry Smith   if (n) PetscValidIntPointer(indices,3);
2354482741eSBarry Smith   PetscValidPointer(mapping,4);
236b46b645bSBarry Smith 
2370298fd71SBarry Smith   *mapping = NULL;
238607a6623SBarry Smith   ierr = ISInitializePackage();CHKERRQ(ierr);
2392362add9SBarry Smith 
24067c2884eSBarry Smith   ierr = PetscHeaderCreate(*mapping,_p_ISLocalToGlobalMapping,int,IS_LTOGM_CLASSID,"ISLocalToGlobalMapping","Local to global mapping","IS",
24152e6d16bSBarry Smith                            cm,ISLocalToGlobalMappingDestroy,ISLocalToGlobalMappingView);CHKERRQ(ierr);
242d4bb536fSBarry Smith   (*mapping)->n  = n;
243*f0413b6fSBarry Smith   (*mapping)->bs = bs;
244d4bb536fSBarry Smith   /*
245d4bb536fSBarry Smith     Do not create the global to local mapping. This is only created if
246d4bb536fSBarry Smith     ISGlobalToLocalMapping() is called
247d4bb536fSBarry Smith   */
248d4bb536fSBarry Smith   (*mapping)->globals = 0;
249d5ad8652SBarry Smith   if (mode == PETSC_COPY_VALUES) {
250785e854fSJed Brown     ierr = PetscMalloc1(n,&in);CHKERRQ(ierr);
251d5ad8652SBarry Smith     ierr = PetscMemcpy(in,indices,n*sizeof(PetscInt));CHKERRQ(ierr);
2523bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)*mapping,n*sizeof(PetscInt));CHKERRQ(ierr);
253d5ad8652SBarry Smith     (*mapping)->indices = in;
254f6e5521dSKarl Rupp   } else if (mode == PETSC_OWN_POINTER) (*mapping)->indices = (PetscInt*)indices;
255f6e5521dSKarl Rupp   else SETERRQ(cm,PETSC_ERR_SUP,"Cannot currently use PETSC_USE_POINTER");
2563a40ed3dSBarry Smith   PetscFunctionReturn(0);
2572362add9SBarry Smith }
2582362add9SBarry Smith 
2594a2ae208SSatish Balay #undef __FUNCT__
2604a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingBlock"
261bce096a4SSatish Balay /*@
262323b833fSBarry Smith     ISLocalToGlobalMappingBlock - Creates a blocked index version of an
263323b833fSBarry Smith        ISLocalToGlobalMapping that is appropriate for MatSetLocalToGlobalMappingBlock()
264323b833fSBarry Smith        and VecSetLocalToGlobalMappingBlock().
265323b833fSBarry Smith 
266323b833fSBarry Smith     Not Collective, but communicator may have more than one process
267323b833fSBarry Smith 
268323b833fSBarry Smith     Input Parameters:
269323b833fSBarry Smith +    inmap - original point-wise mapping
270323b833fSBarry Smith -    bs - block size
271323b833fSBarry Smith 
272323b833fSBarry Smith     Output Parameter:
27369eb54c3SBarry Smith .   outmap - block based mapping; the indices are relative to BLOCKS, not individual vector or matrix entries.
274323b833fSBarry Smith 
275323b833fSBarry Smith     Level: advanced
276323b833fSBarry Smith 
277323b833fSBarry Smith     Concepts: mapping^local to global
278323b833fSBarry Smith 
279323b833fSBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingCreateIS()
280323b833fSBarry Smith @*/
2817087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingBlock(ISLocalToGlobalMapping inmap,PetscInt bs,ISLocalToGlobalMapping *outmap)
282323b833fSBarry Smith {
2836849ba73SBarry Smith   PetscErrorCode ierr;
28432dcc486SBarry Smith   PetscInt       *ii,i,n;
285323b833fSBarry Smith 
286323b833fSBarry Smith   PetscFunctionBegin;
2870700a824SBarry Smith   PetscValidHeaderSpecific(inmap,IS_LTOGM_CLASSID,1);
288b2beed0aSJed Brown   PetscValidPointer(outmap,3);
289323b833fSBarry Smith   if (bs > 1) {
290323b833fSBarry Smith     n = inmap->n/bs;
291e32f2f54SBarry Smith     if (n*bs != inmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Pointwise mapping length is not divisible by block size");
292785e854fSJed Brown     ierr = PetscMalloc1(n,&ii);CHKERRQ(ierr);
293f6e5521dSKarl Rupp     for (i=0; i<n; i++) ii[i] = inmap->indices[bs*i]/bs;
294*f0413b6fSBarry Smith     ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)inmap),1,n,ii,PETSC_OWN_POINTER,outmap);CHKERRQ(ierr);
295323b833fSBarry Smith   } else {
296323b833fSBarry Smith     ierr    = PetscObjectReference((PetscObject)inmap);CHKERRQ(ierr);
297c3122656SLisandro Dalcin     *outmap = inmap;
298323b833fSBarry Smith   }
299323b833fSBarry Smith   PetscFunctionReturn(0);
300323b833fSBarry Smith }
301323b833fSBarry Smith 
3024a2ae208SSatish Balay #undef __FUNCT__
3038ab951edSJed Brown #define __FUNCT__ "ISLocalToGlobalMappingUnBlock"
304b2beed0aSJed Brown /*@
305b2beed0aSJed Brown     ISLocalToGlobalMappingUnBlock - Creates a scalar index version of a blocked
306b2beed0aSJed Brown        ISLocalToGlobalMapping
307b2beed0aSJed Brown 
308b2beed0aSJed Brown     Not Collective, but communicator may have more than one process
309b2beed0aSJed Brown 
310b2beed0aSJed Brown     Input Parameter:
311b2beed0aSJed Brown + inmap - block based mapping; the indices are relative to BLOCKS, not individual vector or matrix entries.
312b2beed0aSJed Brown - bs - block size
313b2beed0aSJed Brown 
314b2beed0aSJed Brown     Output Parameter:
315b2beed0aSJed Brown .   outmap - pointwise mapping
316b2beed0aSJed Brown 
317b2beed0aSJed Brown     Level: advanced
318b2beed0aSJed Brown 
319b2beed0aSJed Brown     Concepts: mapping^local to global
320b2beed0aSJed Brown 
321b2beed0aSJed Brown .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingBlock()
322b2beed0aSJed Brown @*/
3237087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingUnBlock(ISLocalToGlobalMapping inmap,PetscInt bs,ISLocalToGlobalMapping *outmap)
324b2beed0aSJed Brown {
325b2beed0aSJed Brown   PetscErrorCode ierr;
326b2beed0aSJed Brown   PetscInt       *ii,i,n;
327b2beed0aSJed Brown 
328b2beed0aSJed Brown   PetscFunctionBegin;
329b2beed0aSJed Brown   PetscValidHeaderSpecific(inmap,IS_LTOGM_CLASSID,1);
330b2beed0aSJed Brown   PetscValidPointer(outmap,2);
331b2beed0aSJed Brown   if (bs > 1) {
332b2beed0aSJed Brown     n    = inmap->n*bs;
333785e854fSJed Brown     ierr = PetscMalloc1(n,&ii);CHKERRQ(ierr);
334f6e5521dSKarl Rupp     for (i=0; i<n; i++) ii[i] = inmap->indices[i/bs]*bs + (i%bs);
335*f0413b6fSBarry Smith     ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)inmap),1,n,ii,PETSC_OWN_POINTER,outmap);CHKERRQ(ierr);
336b2beed0aSJed Brown   } else {
337b2beed0aSJed Brown     ierr    = PetscObjectReference((PetscObject)inmap);CHKERRQ(ierr);
338b2beed0aSJed Brown     *outmap = inmap;
339b2beed0aSJed Brown   }
340b2beed0aSJed Brown   PetscFunctionReturn(0);
341b2beed0aSJed Brown }
342b2beed0aSJed Brown 
343b2beed0aSJed Brown #undef __FUNCT__
3444a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingDestroy"
34590f02eecSBarry Smith /*@
34690f02eecSBarry Smith    ISLocalToGlobalMappingDestroy - Destroys a mapping between a local (0 to n)
34790f02eecSBarry Smith    ordering and a global parallel ordering.
34890f02eecSBarry Smith 
3490f5bd95cSBarry Smith    Note Collective
350b9cd556bSLois Curfman McInnes 
35190f02eecSBarry Smith    Input Parameters:
35290f02eecSBarry Smith .  mapping - mapping data structure
35390f02eecSBarry Smith 
354a997ad1aSLois Curfman McInnes    Level: advanced
355a997ad1aSLois Curfman McInnes 
3563acfe500SLois Curfman McInnes .seealso: ISLocalToGlobalMappingCreate()
35790f02eecSBarry Smith @*/
3586bf464f9SBarry Smith PetscErrorCode  ISLocalToGlobalMappingDestroy(ISLocalToGlobalMapping *mapping)
35990f02eecSBarry Smith {
360dfbe8321SBarry Smith   PetscErrorCode ierr;
3615fd66863SKarl Rupp 
3623a40ed3dSBarry Smith   PetscFunctionBegin;
3636bf464f9SBarry Smith   if (!*mapping) PetscFunctionReturn(0);
3646bf464f9SBarry Smith   PetscValidHeaderSpecific((*mapping),IS_LTOGM_CLASSID,1);
365997056adSBarry Smith   if (--((PetscObject)(*mapping))->refct > 0) {*mapping = 0;PetscFunctionReturn(0);}
3666bf464f9SBarry Smith   ierr     = PetscFree((*mapping)->indices);CHKERRQ(ierr);
3676bf464f9SBarry Smith   ierr     = PetscFree((*mapping)->globals);CHKERRQ(ierr);
368d38fa0fbSBarry Smith   ierr     = PetscHeaderDestroy(mapping);CHKERRQ(ierr);
369992144d0SBarry Smith   *mapping = 0;
3703a40ed3dSBarry Smith   PetscFunctionReturn(0);
37190f02eecSBarry Smith }
37290f02eecSBarry Smith 
3734a2ae208SSatish Balay #undef __FUNCT__
3744a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingApplyIS"
37590f02eecSBarry Smith /*@
3763acfe500SLois Curfman McInnes     ISLocalToGlobalMappingApplyIS - Creates from an IS in the local numbering
3773acfe500SLois Curfman McInnes     a new index set using the global numbering defined in an ISLocalToGlobalMapping
3783acfe500SLois Curfman McInnes     context.
37990f02eecSBarry Smith 
380b9cd556bSLois Curfman McInnes     Not collective
381b9cd556bSLois Curfman McInnes 
38290f02eecSBarry Smith     Input Parameters:
383b9cd556bSLois Curfman McInnes +   mapping - mapping between local and global numbering
384b9cd556bSLois Curfman McInnes -   is - index set in local numbering
38590f02eecSBarry Smith 
38690f02eecSBarry Smith     Output Parameters:
38790f02eecSBarry Smith .   newis - index set in global numbering
38890f02eecSBarry Smith 
389a997ad1aSLois Curfman McInnes     Level: advanced
390a997ad1aSLois Curfman McInnes 
391273d9f13SBarry Smith     Concepts: mapping^local to global
3923acfe500SLois Curfman McInnes 
39390f02eecSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),
394d4bb536fSBarry Smith           ISLocalToGlobalMappingDestroy(), ISGlobalToLocalMappingApply()
39590f02eecSBarry Smith @*/
3967087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingApplyIS(ISLocalToGlobalMapping mapping,IS is,IS *newis)
39790f02eecSBarry Smith {
3986849ba73SBarry Smith   PetscErrorCode ierr;
3995d0c19d7SBarry Smith   PetscInt       n,i,*idxmap,*idxout,Nmax = mapping->n;
4005d0c19d7SBarry Smith   const PetscInt *idxin;
4013a40ed3dSBarry Smith 
4023a40ed3dSBarry Smith   PetscFunctionBegin;
4030700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
4040700a824SBarry Smith   PetscValidHeaderSpecific(is,IS_CLASSID,2);
4054482741eSBarry Smith   PetscValidPointer(newis,3);
40690f02eecSBarry Smith 
4073b9aefa3SBarry Smith   ierr   = ISGetLocalSize(is,&n);CHKERRQ(ierr);
40890f02eecSBarry Smith   ierr   = ISGetIndices(is,&idxin);CHKERRQ(ierr);
40990f02eecSBarry Smith   idxmap = mapping->indices;
41090f02eecSBarry Smith 
411785e854fSJed Brown   ierr = PetscMalloc1(n,&idxout);CHKERRQ(ierr);
41290f02eecSBarry Smith   for (i=0; i<n; i++) {
4137904a332SBarry 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);
41490f02eecSBarry Smith     idxout[i] = idxmap[idxin[i]];
41590f02eecSBarry Smith   }
4163b9aefa3SBarry Smith   ierr = ISRestoreIndices(is,&idxin);CHKERRQ(ierr);
417543f3098SMatthew G. Knepley   ierr = ISCreateGeneral(PetscObjectComm((PetscObject)is),n,idxout,PETSC_OWN_POINTER,newis);CHKERRQ(ierr);
4183a40ed3dSBarry Smith   PetscFunctionReturn(0);
41990f02eecSBarry Smith }
42090f02eecSBarry Smith 
421afcb2eb5SJed Brown #undef __FUNCT__
422afcb2eb5SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingApply"
423b89cb25eSSatish Balay /*@
4243acfe500SLois Curfman McInnes    ISLocalToGlobalMappingApply - Takes a list of integers in a local numbering
4253acfe500SLois Curfman McInnes    and converts them to the global numbering.
42690f02eecSBarry Smith 
427b9cd556bSLois Curfman McInnes    Not collective
428b9cd556bSLois Curfman McInnes 
429bb25748dSBarry Smith    Input Parameters:
430b9cd556bSLois Curfman McInnes +  mapping - the local to global mapping context
431bb25748dSBarry Smith .  N - number of integers
432b9cd556bSLois Curfman McInnes -  in - input indices in local numbering
433bb25748dSBarry Smith 
434bb25748dSBarry Smith    Output Parameter:
435bb25748dSBarry Smith .  out - indices in global numbering
436bb25748dSBarry Smith 
437b9cd556bSLois Curfman McInnes    Notes:
438b9cd556bSLois Curfman McInnes    The in and out array parameters may be identical.
439d4bb536fSBarry Smith 
440a997ad1aSLois Curfman McInnes    Level: advanced
441a997ad1aSLois Curfman McInnes 
442bb25748dSBarry Smith .seealso: ISLocalToGlobalMappingCreate(),ISLocalToGlobalMappingDestroy(),
4430752156aSBarry Smith           ISLocalToGlobalMappingApplyIS(),AOCreateBasic(),AOApplicationToPetsc(),
444d4bb536fSBarry Smith           AOPetscToApplication(), ISGlobalToLocalMappingApply()
445bb25748dSBarry Smith 
446273d9f13SBarry Smith     Concepts: mapping^local to global
447afcb2eb5SJed Brown @*/
448afcb2eb5SJed Brown PetscErrorCode ISLocalToGlobalMappingApply(ISLocalToGlobalMapping mapping,PetscInt N,const PetscInt in[],PetscInt out[])
449afcb2eb5SJed Brown {
450afcb2eb5SJed Brown   PetscInt       i,Nmax = mapping->n;
451afcb2eb5SJed Brown   const PetscInt *idx = mapping->indices;
452d4bb536fSBarry Smith 
453afcb2eb5SJed Brown   PetscFunctionBegin;
454afcb2eb5SJed Brown   for (i=0; i<N; i++) {
455afcb2eb5SJed Brown     if (in[i] < 0) {
456afcb2eb5SJed Brown       out[i] = in[i];
457afcb2eb5SJed Brown       continue;
458afcb2eb5SJed Brown     }
459afcb2eb5SJed 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);
460afcb2eb5SJed Brown     out[i] = idx[in[i]];
461afcb2eb5SJed Brown   }
462afcb2eb5SJed Brown   PetscFunctionReturn(0);
463afcb2eb5SJed Brown }
464d4bb536fSBarry Smith 
465d4bb536fSBarry Smith /* -----------------------------------------------------------------------------------------*/
466d4bb536fSBarry Smith 
4674a2ae208SSatish Balay #undef __FUNCT__
4684a2ae208SSatish Balay #define __FUNCT__ "ISGlobalToLocalMappingSetUp_Private"
469d4bb536fSBarry Smith /*
470d4bb536fSBarry Smith     Creates the global fields in the ISLocalToGlobalMapping structure
471d4bb536fSBarry Smith */
4726849ba73SBarry Smith static PetscErrorCode ISGlobalToLocalMappingSetUp_Private(ISLocalToGlobalMapping mapping)
473d4bb536fSBarry Smith {
4746849ba73SBarry Smith   PetscErrorCode ierr;
47532dcc486SBarry Smith   PetscInt       i,*idx = mapping->indices,n = mapping->n,end,start,*globals;
476d4bb536fSBarry Smith 
4773a40ed3dSBarry Smith   PetscFunctionBegin;
478d4bb536fSBarry Smith   end   = 0;
479ec268f7cSJed Brown   start = PETSC_MAX_INT;
480d4bb536fSBarry Smith 
481d4bb536fSBarry Smith   for (i=0; i<n; i++) {
482d4bb536fSBarry Smith     if (idx[i] < 0) continue;
483d4bb536fSBarry Smith     if (idx[i] < start) start = idx[i];
484d4bb536fSBarry Smith     if (idx[i] > end)   end   = idx[i];
485d4bb536fSBarry Smith   }
486d4bb536fSBarry Smith   if (start > end) {start = 0; end = -1;}
487d4bb536fSBarry Smith   mapping->globalstart = start;
488d4bb536fSBarry Smith   mapping->globalend   = end;
489d4bb536fSBarry Smith 
490785e854fSJed Brown   ierr             = PetscMalloc1((end-start+2),&globals);CHKERRQ(ierr);
491b0a32e0cSBarry Smith   mapping->globals = globals;
492f6e5521dSKarl Rupp   for (i=0; i<end-start+1; i++) globals[i] = -1;
493d4bb536fSBarry Smith   for (i=0; i<n; i++) {
494d4bb536fSBarry Smith     if (idx[i] < 0) continue;
495d4bb536fSBarry Smith     globals[idx[i] - start] = i;
496d4bb536fSBarry Smith   }
497d4bb536fSBarry Smith 
4983bb1ff40SBarry Smith   ierr = PetscLogObjectMemory((PetscObject)mapping,(end-start+1)*sizeof(PetscInt));CHKERRQ(ierr);
4993a40ed3dSBarry Smith   PetscFunctionReturn(0);
500d4bb536fSBarry Smith }
501d4bb536fSBarry Smith 
5024a2ae208SSatish Balay #undef __FUNCT__
5034a2ae208SSatish Balay #define __FUNCT__ "ISGlobalToLocalMappingApply"
504d4bb536fSBarry Smith /*@
505a997ad1aSLois Curfman McInnes     ISGlobalToLocalMappingApply - Provides the local numbering for a list of integers
506a997ad1aSLois Curfman McInnes     specified with a global numbering.
507d4bb536fSBarry Smith 
508b9cd556bSLois Curfman McInnes     Not collective
509b9cd556bSLois Curfman McInnes 
510d4bb536fSBarry Smith     Input Parameters:
511b9cd556bSLois Curfman McInnes +   mapping - mapping between local and global numbering
512d4bb536fSBarry Smith .   type - IS_GTOLM_MASK - replaces global indices with no local value with -1
513d4bb536fSBarry Smith            IS_GTOLM_DROP - drops the indices with no local value from the output list
514d4bb536fSBarry Smith .   n - number of global indices to map
515b9cd556bSLois Curfman McInnes -   idx - global indices to map
516d4bb536fSBarry Smith 
517d4bb536fSBarry Smith     Output Parameters:
518b9cd556bSLois Curfman McInnes +   nout - number of indices in output array (if type == IS_GTOLM_MASK then nout = n)
519b9cd556bSLois Curfman McInnes -   idxout - local index of each global index, one must pass in an array long enough
520e182c471SBarry Smith              to hold all the indices. You can call ISGlobalToLocalMappingApply() with
5210298fd71SBarry Smith              idxout == NULL to determine the required length (returned in nout)
522e182c471SBarry Smith              and then allocate the required space and call ISGlobalToLocalMappingApply()
523e182c471SBarry Smith              a second time to set the values.
524d4bb536fSBarry Smith 
525b9cd556bSLois Curfman McInnes     Notes:
5260298fd71SBarry Smith     Either nout or idxout may be NULL. idx and idxout may be identical.
527d4bb536fSBarry Smith 
5280f5bd95cSBarry Smith     This is not scalable in memory usage. Each processor requires O(Nglobal) size
5290f5bd95cSBarry Smith     array to compute these.
5300f5bd95cSBarry Smith 
531a997ad1aSLois Curfman McInnes     Level: advanced
532a997ad1aSLois Curfman McInnes 
53332fd6b96SBarry Smith     Developer Note: The manual page states that idx and idxout may be identical but the calling
53432fd6b96SBarry Smith        sequence declares idx as const so it cannot be the same as idxout.
53532fd6b96SBarry Smith 
536273d9f13SBarry Smith     Concepts: mapping^global to local
537d4bb536fSBarry Smith 
538d4bb536fSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),
539d4bb536fSBarry Smith           ISLocalToGlobalMappingDestroy()
540d4bb536fSBarry Smith @*/
5417087cfbeSBarry Smith PetscErrorCode  ISGlobalToLocalMappingApply(ISLocalToGlobalMapping mapping,ISGlobalToLocalMappingType type,
54232dcc486SBarry Smith                                   PetscInt n,const PetscInt idx[],PetscInt *nout,PetscInt idxout[])
543d4bb536fSBarry Smith {
54432dcc486SBarry Smith   PetscInt       i,*globals,nf = 0,tmp,start,end;
5456849ba73SBarry Smith   PetscErrorCode ierr;
546d4bb536fSBarry Smith 
5473a40ed3dSBarry Smith   PetscFunctionBegin;
5480700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
549d4bb536fSBarry Smith   if (!mapping->globals) {
550d4bb536fSBarry Smith     ierr = ISGlobalToLocalMappingSetUp_Private(mapping);CHKERRQ(ierr);
551d4bb536fSBarry Smith   }
552d4bb536fSBarry Smith   globals = mapping->globals;
553d4bb536fSBarry Smith   start   = mapping->globalstart;
554d4bb536fSBarry Smith   end     = mapping->globalend;
555d4bb536fSBarry Smith 
556d4bb536fSBarry Smith   if (type == IS_GTOLM_MASK) {
557d4bb536fSBarry Smith     if (idxout) {
558d4bb536fSBarry Smith       for (i=0; i<n; i++) {
559d4bb536fSBarry Smith         if (idx[i] < 0) idxout[i] = idx[i];
560d4bb536fSBarry Smith         else if (idx[i] < start) idxout[i] = -1;
561d4bb536fSBarry Smith         else if (idx[i] > end)   idxout[i] = -1;
562d4bb536fSBarry Smith         else                     idxout[i] = globals[idx[i] - start];
563d4bb536fSBarry Smith       }
564d4bb536fSBarry Smith     }
565d4bb536fSBarry Smith     if (nout) *nout = n;
566d4bb536fSBarry Smith   } else {
567d4bb536fSBarry Smith     if (idxout) {
568d4bb536fSBarry Smith       for (i=0; i<n; i++) {
569d4bb536fSBarry Smith         if (idx[i] < 0) continue;
570d4bb536fSBarry Smith         if (idx[i] < start) continue;
571d4bb536fSBarry Smith         if (idx[i] > end) continue;
572d4bb536fSBarry Smith         tmp = globals[idx[i] - start];
573d4bb536fSBarry Smith         if (tmp < 0) continue;
574d4bb536fSBarry Smith         idxout[nf++] = tmp;
575d4bb536fSBarry Smith       }
576d4bb536fSBarry Smith     } else {
577d4bb536fSBarry Smith       for (i=0; i<n; i++) {
578d4bb536fSBarry Smith         if (idx[i] < 0) continue;
579d4bb536fSBarry Smith         if (idx[i] < start) continue;
580d4bb536fSBarry Smith         if (idx[i] > end) continue;
581d4bb536fSBarry Smith         tmp = globals[idx[i] - start];
582d4bb536fSBarry Smith         if (tmp < 0) continue;
583d4bb536fSBarry Smith         nf++;
584d4bb536fSBarry Smith       }
585d4bb536fSBarry Smith     }
586d4bb536fSBarry Smith     if (nout) *nout = nf;
587d4bb536fSBarry Smith   }
5883a40ed3dSBarry Smith   PetscFunctionReturn(0);
589d4bb536fSBarry Smith }
59090f02eecSBarry Smith 
5914a2ae208SSatish Balay #undef __FUNCT__
5924a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingGetInfo"
59389d82c54SBarry Smith /*@C
59489d82c54SBarry Smith     ISLocalToGlobalMappingGetInfo - Gets the neighbor information for each processor and
59589d82c54SBarry Smith      each index shared by more than one processor
59689d82c54SBarry Smith 
59789d82c54SBarry Smith     Collective on ISLocalToGlobalMapping
59889d82c54SBarry Smith 
59989d82c54SBarry Smith     Input Parameters:
60089d82c54SBarry Smith .   mapping - the mapping from local to global indexing
60189d82c54SBarry Smith 
60289d82c54SBarry Smith     Output Parameter:
60389d82c54SBarry Smith +   nproc - number of processors that are connected to this one
60489d82c54SBarry Smith .   proc - neighboring processors
60507b52d57SBarry Smith .   numproc - number of indices for each subdomain (processor)
6063463a7baSJed Brown -   indices - indices of nodes (in local numbering) shared with neighbors (sorted by global numbering)
60789d82c54SBarry Smith 
60889d82c54SBarry Smith     Level: advanced
60989d82c54SBarry Smith 
610273d9f13SBarry Smith     Concepts: mapping^local to global
61189d82c54SBarry Smith 
6122cfcea29SBarry Smith     Fortran Usage:
6132cfcea29SBarry Smith $        ISLocalToGlobalMpngGetInfoSize(ISLocalToGlobalMapping,PetscInt nproc,PetscInt numprocmax,ierr) followed by
6142cfcea29SBarry Smith $        ISLocalToGlobalMappingGetInfo(ISLocalToGlobalMapping,PetscInt nproc, PetscInt procs[nproc],PetscInt numprocs[nproc],
6152cfcea29SBarry Smith           PetscInt indices[nproc][numprocmax],ierr)
6162cfcea29SBarry Smith         There is no ISLocalToGlobalMappingRestoreInfo() in Fortran. You must make sure that procs[], numprocs[] and
6172cfcea29SBarry Smith         indices[][] are large enough arrays, either by allocating them dynamically or defining static ones large enough.
6182cfcea29SBarry Smith 
6192cfcea29SBarry Smith 
62007b52d57SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreate(),
62107b52d57SBarry Smith           ISLocalToGlobalMappingRestoreInfo()
62289d82c54SBarry Smith @*/
6237087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingGetInfo(ISLocalToGlobalMapping mapping,PetscInt *nproc,PetscInt *procs[],PetscInt *numprocs[],PetscInt **indices[])
62489d82c54SBarry Smith {
6256849ba73SBarry Smith   PetscErrorCode ierr;
62697f1f81fSBarry Smith   PetscMPIInt    size,rank,tag1,tag2,tag3,*len,*source,imdex;
62732dcc486SBarry Smith   PetscInt       i,n = mapping->n,Ng,ng,max = 0,*lindices = mapping->indices;
62832dcc486SBarry Smith   PetscInt       *nprocs,*owner,nsends,*sends,j,*starts,nmax,nrecvs,*recvs,proc;
62997f1f81fSBarry Smith   PetscInt       cnt,scale,*ownedsenders,*nownedsenders,rstart,nowned;
63032dcc486SBarry Smith   PetscInt       node,nownedm,nt,*sends2,nsends2,*starts2,*lens2,*dest,nrecvs2,*starts3,*recvs2,k,*bprocs,*tmp;
63132dcc486SBarry Smith   PetscInt       first_procs,first_numprocs,*first_indices;
63289d82c54SBarry Smith   MPI_Request    *recv_waits,*send_waits;
63330dcb7c9SBarry Smith   MPI_Status     recv_status,*send_status,*recv_statuses;
634ce94432eSBarry Smith   MPI_Comm       comm;
635ace3abfcSBarry Smith   PetscBool      debug = PETSC_FALSE;
63689d82c54SBarry Smith 
63789d82c54SBarry Smith   PetscFunctionBegin;
6380700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
639ce94432eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)mapping,&comm);CHKERRQ(ierr);
64024cf384cSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
64124cf384cSBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
64224cf384cSBarry Smith   if (size == 1) {
64324cf384cSBarry Smith     *nproc         = 0;
6440298fd71SBarry Smith     *procs         = NULL;
64532dcc486SBarry Smith     ierr           = PetscMalloc(sizeof(PetscInt),numprocs);CHKERRQ(ierr);
6461e2105dcSBarry Smith     (*numprocs)[0] = 0;
64732dcc486SBarry Smith     ierr           = PetscMalloc(sizeof(PetscInt*),indices);CHKERRQ(ierr);
6480298fd71SBarry Smith     (*indices)[0]  = NULL;
64924cf384cSBarry Smith     PetscFunctionReturn(0);
65024cf384cSBarry Smith   }
65124cf384cSBarry Smith 
6520298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-islocaltoglobalmappinggetinfo_debug",&debug,NULL);CHKERRQ(ierr);
65307b52d57SBarry Smith 
6543677ff5aSBarry Smith   /*
6553677ff5aSBarry Smith     Notes on ISLocalToGlobalMappingGetInfo
6563677ff5aSBarry Smith 
6573677ff5aSBarry Smith     globally owned node - the nodes that have been assigned to this processor in global
6583677ff5aSBarry Smith            numbering, just for this routine.
6593677ff5aSBarry Smith 
6603677ff5aSBarry Smith     nontrivial globally owned node - node assigned to this processor that is on a subdomain
6613677ff5aSBarry Smith            boundary (i.e. is has more than one local owner)
6623677ff5aSBarry Smith 
6633677ff5aSBarry Smith     locally owned node - node that exists on this processors subdomain
6643677ff5aSBarry Smith 
6653677ff5aSBarry Smith     nontrivial locally owned node - node that is not in the interior (i.e. has more than one
6663677ff5aSBarry Smith            local subdomain
6673677ff5aSBarry Smith   */
66824cf384cSBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mapping,&tag1);CHKERRQ(ierr);
66924cf384cSBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mapping,&tag2);CHKERRQ(ierr);
67024cf384cSBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mapping,&tag3);CHKERRQ(ierr);
67189d82c54SBarry Smith 
67289d82c54SBarry Smith   for (i=0; i<n; i++) {
67389d82c54SBarry Smith     if (lindices[i] > max) max = lindices[i];
67489d82c54SBarry Smith   }
67532dcc486SBarry Smith   ierr   = MPI_Allreduce(&max,&Ng,1,MPIU_INT,MPI_MAX,comm);CHKERRQ(ierr);
67678058e43SBarry Smith   Ng++;
67789d82c54SBarry Smith   ierr   = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
67889d82c54SBarry Smith   ierr   = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
679bc8ff85bSBarry Smith   scale  = Ng/size + 1;
680a2e34c3dSBarry Smith   ng     = scale; if (rank == size-1) ng = Ng - scale*(size-1); ng = PetscMax(1,ng);
681caba0dd0SBarry Smith   rstart = scale*rank;
68289d82c54SBarry Smith 
68389d82c54SBarry Smith   /* determine ownership ranges of global indices */
684785e854fSJed Brown   ierr = PetscMalloc1(2*size,&nprocs);CHKERRQ(ierr);
68532dcc486SBarry Smith   ierr = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr);
68689d82c54SBarry Smith 
68789d82c54SBarry Smith   /* determine owners of each local node  */
688785e854fSJed Brown   ierr = PetscMalloc1(n,&owner);CHKERRQ(ierr);
68989d82c54SBarry Smith   for (i=0; i<n; i++) {
6903677ff5aSBarry Smith     proc             = lindices[i]/scale; /* processor that globally owns this index */
69127c402fcSBarry Smith     nprocs[2*proc+1] = 1;                 /* processor globally owns at least one of ours */
6923677ff5aSBarry Smith     owner[i]         = proc;
69327c402fcSBarry Smith     nprocs[2*proc]++;                     /* count of how many that processor globally owns of ours */
69489d82c54SBarry Smith   }
69527c402fcSBarry Smith   nsends = 0; for (i=0; i<size; i++) nsends += nprocs[2*i+1];
6967904a332SBarry Smith   ierr = PetscInfo1(mapping,"Number of global owners for my local data %D\n",nsends);CHKERRQ(ierr);
69789d82c54SBarry Smith 
69889d82c54SBarry Smith   /* inform other processors of number of messages and max length*/
69927c402fcSBarry Smith   ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr);
7007904a332SBarry Smith   ierr = PetscInfo1(mapping,"Number of local owners for my global data %D\n",nrecvs);CHKERRQ(ierr);
70189d82c54SBarry Smith 
70289d82c54SBarry Smith   /* post receives for owned rows */
703785e854fSJed Brown   ierr = PetscMalloc1((2*nrecvs+1)*(nmax+1),&recvs);CHKERRQ(ierr);
704785e854fSJed Brown   ierr = PetscMalloc1((nrecvs+1),&recv_waits);CHKERRQ(ierr);
70589d82c54SBarry Smith   for (i=0; i<nrecvs; i++) {
70632dcc486SBarry Smith     ierr = MPI_Irecv(recvs+2*nmax*i,2*nmax,MPIU_INT,MPI_ANY_SOURCE,tag1,comm,recv_waits+i);CHKERRQ(ierr);
70789d82c54SBarry Smith   }
70889d82c54SBarry Smith 
70989d82c54SBarry Smith   /* pack messages containing lists of local nodes to owners */
710785e854fSJed Brown   ierr      = PetscMalloc1((2*n+1),&sends);CHKERRQ(ierr);
711785e854fSJed Brown   ierr      = PetscMalloc1((size+1),&starts);CHKERRQ(ierr);
71289d82c54SBarry Smith   starts[0] = 0;
713f6e5521dSKarl Rupp   for (i=1; i<size; i++) starts[i] = starts[i-1] + 2*nprocs[2*i-2];
71489d82c54SBarry Smith   for (i=0; i<n; i++) {
71589d82c54SBarry Smith     sends[starts[owner[i]]++] = lindices[i];
71630dcb7c9SBarry Smith     sends[starts[owner[i]]++] = i;
71789d82c54SBarry Smith   }
71889d82c54SBarry Smith   ierr = PetscFree(owner);CHKERRQ(ierr);
71989d82c54SBarry Smith   starts[0] = 0;
720f6e5521dSKarl Rupp   for (i=1; i<size; i++) starts[i] = starts[i-1] + 2*nprocs[2*i-2];
72189d82c54SBarry Smith 
72289d82c54SBarry Smith   /* send the messages */
723785e854fSJed Brown   ierr = PetscMalloc1((nsends+1),&send_waits);CHKERRQ(ierr);
724785e854fSJed Brown   ierr = PetscMalloc1((nsends+1),&dest);CHKERRQ(ierr);
72589d82c54SBarry Smith   cnt = 0;
72689d82c54SBarry Smith   for (i=0; i<size; i++) {
72727c402fcSBarry Smith     if (nprocs[2*i]) {
72832dcc486SBarry Smith       ierr      = MPI_Isend(sends+starts[i],2*nprocs[2*i],MPIU_INT,i,tag1,comm,send_waits+cnt);CHKERRQ(ierr);
72930dcb7c9SBarry Smith       dest[cnt] = i;
73089d82c54SBarry Smith       cnt++;
73189d82c54SBarry Smith     }
73289d82c54SBarry Smith   }
73389d82c54SBarry Smith   ierr = PetscFree(starts);CHKERRQ(ierr);
73489d82c54SBarry Smith 
73589d82c54SBarry Smith   /* wait on receives */
736785e854fSJed Brown   ierr = PetscMalloc1((nrecvs+1),&source);CHKERRQ(ierr);
737785e854fSJed Brown   ierr = PetscMalloc1((nrecvs+1),&len);CHKERRQ(ierr);
73889d82c54SBarry Smith   cnt  = nrecvs;
739785e854fSJed Brown   ierr = PetscMalloc1((ng+1),&nownedsenders);CHKERRQ(ierr);
74032dcc486SBarry Smith   ierr = PetscMemzero(nownedsenders,ng*sizeof(PetscInt));CHKERRQ(ierr);
74189d82c54SBarry Smith   while (cnt) {
74289d82c54SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
74389d82c54SBarry Smith     /* unpack receives into our local space */
74432dcc486SBarry Smith     ierr          = MPI_Get_count(&recv_status,MPIU_INT,&len[imdex]);CHKERRQ(ierr);
74589d82c54SBarry Smith     source[imdex] = recv_status.MPI_SOURCE;
74630dcb7c9SBarry Smith     len[imdex]    = len[imdex]/2;
747caba0dd0SBarry Smith     /* count how many local owners for each of my global owned indices */
74830dcb7c9SBarry Smith     for (i=0; i<len[imdex]; i++) nownedsenders[recvs[2*imdex*nmax+2*i]-rstart]++;
74989d82c54SBarry Smith     cnt--;
75089d82c54SBarry Smith   }
75189d82c54SBarry Smith   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
75289d82c54SBarry Smith 
75330dcb7c9SBarry Smith   /* count how many globally owned indices are on an edge multiplied by how many processors own them. */
754bc8ff85bSBarry Smith   nowned  = 0;
755bc8ff85bSBarry Smith   nownedm = 0;
756bc8ff85bSBarry Smith   for (i=0; i<ng; i++) {
757bc8ff85bSBarry Smith     if (nownedsenders[i] > 1) {nownedm += nownedsenders[i]; nowned++;}
758bc8ff85bSBarry Smith   }
759bc8ff85bSBarry Smith 
760bc8ff85bSBarry Smith   /* create single array to contain rank of all local owners of each globally owned index */
761785e854fSJed Brown   ierr      = PetscMalloc1((nownedm+1),&ownedsenders);CHKERRQ(ierr);
762785e854fSJed Brown   ierr      = PetscMalloc1((ng+1),&starts);CHKERRQ(ierr);
763bc8ff85bSBarry Smith   starts[0] = 0;
764bc8ff85bSBarry Smith   for (i=1; i<ng; i++) {
765bc8ff85bSBarry Smith     if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1];
766bc8ff85bSBarry Smith     else starts[i] = starts[i-1];
767bc8ff85bSBarry Smith   }
768bc8ff85bSBarry Smith 
76930dcb7c9SBarry Smith   /* for each nontrival globally owned node list all arriving processors */
770bc8ff85bSBarry Smith   for (i=0; i<nrecvs; i++) {
771bc8ff85bSBarry Smith     for (j=0; j<len[i]; j++) {
77230dcb7c9SBarry Smith       node = recvs[2*i*nmax+2*j]-rstart;
773f6e5521dSKarl Rupp       if (nownedsenders[node] > 1) ownedsenders[starts[node]++] = source[i];
774bc8ff85bSBarry Smith     }
775bc8ff85bSBarry Smith   }
776bc8ff85bSBarry Smith 
77707b52d57SBarry Smith   if (debug) { /* -----------------------------------  */
77830dcb7c9SBarry Smith     starts[0] = 0;
77930dcb7c9SBarry Smith     for (i=1; i<ng; i++) {
78030dcb7c9SBarry Smith       if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1];
78130dcb7c9SBarry Smith       else starts[i] = starts[i-1];
78230dcb7c9SBarry Smith     }
78330dcb7c9SBarry Smith     for (i=0; i<ng; i++) {
78430dcb7c9SBarry Smith       if (nownedsenders[i] > 1) {
7857904a332SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"[%d] global node %D local owner processors: ",rank,i+rstart);CHKERRQ(ierr);
78630dcb7c9SBarry Smith         for (j=0; j<nownedsenders[i]; j++) {
7877904a332SBarry Smith           ierr = PetscSynchronizedPrintf(comm,"%D ",ownedsenders[starts[i]+j]);CHKERRQ(ierr);
78830dcb7c9SBarry Smith         }
78930dcb7c9SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"\n");CHKERRQ(ierr);
79030dcb7c9SBarry Smith       }
79130dcb7c9SBarry Smith     }
7920ec8b6e3SBarry Smith     ierr = PetscSynchronizedFlush(comm,PETSC_STDOUT);CHKERRQ(ierr);
79307b52d57SBarry Smith   } /* -----------------------------------  */
79430dcb7c9SBarry Smith 
7953677ff5aSBarry Smith   /* wait on original sends */
7963a96401aSBarry Smith   if (nsends) {
797785e854fSJed Brown     ierr = PetscMalloc1(nsends,&send_status);CHKERRQ(ierr);
7983a96401aSBarry Smith     ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
7993a96401aSBarry Smith     ierr = PetscFree(send_status);CHKERRQ(ierr);
8003a96401aSBarry Smith   }
80189d82c54SBarry Smith   ierr = PetscFree(send_waits);CHKERRQ(ierr);
8023a96401aSBarry Smith   ierr = PetscFree(sends);CHKERRQ(ierr);
8033677ff5aSBarry Smith   ierr = PetscFree(nprocs);CHKERRQ(ierr);
8043677ff5aSBarry Smith 
8053677ff5aSBarry Smith   /* pack messages to send back to local owners */
80630dcb7c9SBarry Smith   starts[0] = 0;
80730dcb7c9SBarry Smith   for (i=1; i<ng; i++) {
80830dcb7c9SBarry Smith     if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1];
80930dcb7c9SBarry Smith     else starts[i] = starts[i-1];
81030dcb7c9SBarry Smith   }
81130dcb7c9SBarry Smith   nsends2 = nrecvs;
812785e854fSJed Brown   ierr    = PetscMalloc1((nsends2+1),&nprocs);CHKERRQ(ierr); /* length of each message */
81330dcb7c9SBarry Smith   for (i=0; i<nrecvs; i++) {
81430dcb7c9SBarry Smith     nprocs[i] = 1;
81530dcb7c9SBarry Smith     for (j=0; j<len[i]; j++) {
81630dcb7c9SBarry Smith       node = recvs[2*i*nmax+2*j]-rstart;
817f6e5521dSKarl Rupp       if (nownedsenders[node] > 1) nprocs[i] += 2 + nownedsenders[node];
81830dcb7c9SBarry Smith     }
81930dcb7c9SBarry Smith   }
820f6e5521dSKarl Rupp   nt = 0;
821f6e5521dSKarl Rupp   for (i=0; i<nsends2; i++) nt += nprocs[i];
822f6e5521dSKarl Rupp 
823785e854fSJed Brown   ierr = PetscMalloc1((nt+1),&sends2);CHKERRQ(ierr);
824785e854fSJed Brown   ierr = PetscMalloc1((nsends2+1),&starts2);CHKERRQ(ierr);
825f6e5521dSKarl Rupp 
826f6e5521dSKarl Rupp   starts2[0] = 0;
827f6e5521dSKarl Rupp   for (i=1; i<nsends2; i++) starts2[i] = starts2[i-1] + nprocs[i-1];
82830dcb7c9SBarry Smith   /*
82930dcb7c9SBarry Smith      Each message is 1 + nprocs[i] long, and consists of
83030dcb7c9SBarry Smith        (0) the number of nodes being sent back
83130dcb7c9SBarry Smith        (1) the local node number,
83230dcb7c9SBarry Smith        (2) the number of processors sharing it,
83330dcb7c9SBarry Smith        (3) the processors sharing it
83430dcb7c9SBarry Smith   */
83530dcb7c9SBarry Smith   for (i=0; i<nsends2; i++) {
83630dcb7c9SBarry Smith     cnt = 1;
83730dcb7c9SBarry Smith     sends2[starts2[i]] = 0;
83830dcb7c9SBarry Smith     for (j=0; j<len[i]; j++) {
83930dcb7c9SBarry Smith       node = recvs[2*i*nmax+2*j]-rstart;
84030dcb7c9SBarry Smith       if (nownedsenders[node] > 1) {
84130dcb7c9SBarry Smith         sends2[starts2[i]]++;
84230dcb7c9SBarry Smith         sends2[starts2[i]+cnt++] = recvs[2*i*nmax+2*j+1];
84330dcb7c9SBarry Smith         sends2[starts2[i]+cnt++] = nownedsenders[node];
84432dcc486SBarry Smith         ierr = PetscMemcpy(&sends2[starts2[i]+cnt],&ownedsenders[starts[node]],nownedsenders[node]*sizeof(PetscInt));CHKERRQ(ierr);
84530dcb7c9SBarry Smith         cnt += nownedsenders[node];
84630dcb7c9SBarry Smith       }
84730dcb7c9SBarry Smith     }
84830dcb7c9SBarry Smith   }
84930dcb7c9SBarry Smith 
85030dcb7c9SBarry Smith   /* receive the message lengths */
85130dcb7c9SBarry Smith   nrecvs2 = nsends;
852785e854fSJed Brown   ierr    = PetscMalloc1((nrecvs2+1),&lens2);CHKERRQ(ierr);
853785e854fSJed Brown   ierr    = PetscMalloc1((nrecvs2+1),&starts3);CHKERRQ(ierr);
854785e854fSJed Brown   ierr    = PetscMalloc1((nrecvs2+1),&recv_waits);CHKERRQ(ierr);
85530dcb7c9SBarry Smith   for (i=0; i<nrecvs2; i++) {
856d44834fbSBarry Smith     ierr = MPI_Irecv(&lens2[i],1,MPIU_INT,dest[i],tag2,comm,recv_waits+i);CHKERRQ(ierr);
85730dcb7c9SBarry Smith   }
858d44834fbSBarry Smith 
8598a8e0b3aSBarry Smith   /* send the message lengths */
8608a8e0b3aSBarry Smith   for (i=0; i<nsends2; i++) {
8618a8e0b3aSBarry Smith     ierr = MPI_Send(&nprocs[i],1,MPIU_INT,source[i],tag2,comm);CHKERRQ(ierr);
8628a8e0b3aSBarry Smith   }
8638a8e0b3aSBarry Smith 
864d44834fbSBarry Smith   /* wait on receives of lens */
8650c468ba9SBarry Smith   if (nrecvs2) {
866785e854fSJed Brown     ierr = PetscMalloc1(nrecvs2,&recv_statuses);CHKERRQ(ierr);
867d44834fbSBarry Smith     ierr = MPI_Waitall(nrecvs2,recv_waits,recv_statuses);CHKERRQ(ierr);
868d44834fbSBarry Smith     ierr = PetscFree(recv_statuses);CHKERRQ(ierr);
8690c468ba9SBarry Smith   }
870a2ea699eSBarry Smith   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
871d44834fbSBarry Smith 
87230dcb7c9SBarry Smith   starts3[0] = 0;
873d44834fbSBarry Smith   nt         = 0;
87430dcb7c9SBarry Smith   for (i=0; i<nrecvs2-1; i++) {
87530dcb7c9SBarry Smith     starts3[i+1] = starts3[i] + lens2[i];
876d44834fbSBarry Smith     nt          += lens2[i];
87730dcb7c9SBarry Smith   }
87876466f69SStefano Zampini   if (nrecvs2) nt += lens2[nrecvs2-1];
879d44834fbSBarry Smith 
880785e854fSJed Brown   ierr = PetscMalloc1((nt+1),&recvs2);CHKERRQ(ierr);
881785e854fSJed Brown   ierr = PetscMalloc1((nrecvs2+1),&recv_waits);CHKERRQ(ierr);
88252b72c4aSBarry Smith   for (i=0; i<nrecvs2; i++) {
88332dcc486SBarry Smith     ierr = MPI_Irecv(recvs2+starts3[i],lens2[i],MPIU_INT,dest[i],tag3,comm,recv_waits+i);CHKERRQ(ierr);
88430dcb7c9SBarry Smith   }
88530dcb7c9SBarry Smith 
88630dcb7c9SBarry Smith   /* send the messages */
887785e854fSJed Brown   ierr = PetscMalloc1((nsends2+1),&send_waits);CHKERRQ(ierr);
88830dcb7c9SBarry Smith   for (i=0; i<nsends2; i++) {
88932dcc486SBarry Smith     ierr = MPI_Isend(sends2+starts2[i],nprocs[i],MPIU_INT,source[i],tag3,comm,send_waits+i);CHKERRQ(ierr);
89030dcb7c9SBarry Smith   }
89130dcb7c9SBarry Smith 
89230dcb7c9SBarry Smith   /* wait on receives */
8930c468ba9SBarry Smith   if (nrecvs2) {
894785e854fSJed Brown     ierr = PetscMalloc1(nrecvs2,&recv_statuses);CHKERRQ(ierr);
89530dcb7c9SBarry Smith     ierr = MPI_Waitall(nrecvs2,recv_waits,recv_statuses);CHKERRQ(ierr);
89630dcb7c9SBarry Smith     ierr = PetscFree(recv_statuses);CHKERRQ(ierr);
8970c468ba9SBarry Smith   }
89830dcb7c9SBarry Smith   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
89930dcb7c9SBarry Smith   ierr = PetscFree(nprocs);CHKERRQ(ierr);
90030dcb7c9SBarry Smith 
90107b52d57SBarry Smith   if (debug) { /* -----------------------------------  */
90230dcb7c9SBarry Smith     cnt = 0;
90330dcb7c9SBarry Smith     for (i=0; i<nrecvs2; i++) {
90430dcb7c9SBarry Smith       nt = recvs2[cnt++];
90530dcb7c9SBarry Smith       for (j=0; j<nt; j++) {
9067904a332SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"[%d] local node %D number of subdomains %D: ",rank,recvs2[cnt],recvs2[cnt+1]);CHKERRQ(ierr);
90730dcb7c9SBarry Smith         for (k=0; k<recvs2[cnt+1]; k++) {
9087904a332SBarry Smith           ierr = PetscSynchronizedPrintf(comm,"%D ",recvs2[cnt+2+k]);CHKERRQ(ierr);
90930dcb7c9SBarry Smith         }
91030dcb7c9SBarry Smith         cnt += 2 + recvs2[cnt+1];
91130dcb7c9SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"\n");CHKERRQ(ierr);
91230dcb7c9SBarry Smith       }
91330dcb7c9SBarry Smith     }
9140ec8b6e3SBarry Smith     ierr = PetscSynchronizedFlush(comm,PETSC_STDOUT);CHKERRQ(ierr);
91507b52d57SBarry Smith   } /* -----------------------------------  */
91630dcb7c9SBarry Smith 
91730dcb7c9SBarry Smith   /* count number subdomains for each local node */
918785e854fSJed Brown   ierr = PetscMalloc1(size,&nprocs);CHKERRQ(ierr);
91932dcc486SBarry Smith   ierr = PetscMemzero(nprocs,size*sizeof(PetscInt));CHKERRQ(ierr);
92030dcb7c9SBarry Smith   cnt  = 0;
92130dcb7c9SBarry Smith   for (i=0; i<nrecvs2; i++) {
92230dcb7c9SBarry Smith     nt = recvs2[cnt++];
92330dcb7c9SBarry Smith     for (j=0; j<nt; j++) {
924f6e5521dSKarl Rupp       for (k=0; k<recvs2[cnt+1]; k++) nprocs[recvs2[cnt+2+k]]++;
92530dcb7c9SBarry Smith       cnt += 2 + recvs2[cnt+1];
92630dcb7c9SBarry Smith     }
92730dcb7c9SBarry Smith   }
92830dcb7c9SBarry Smith   nt = 0; for (i=0; i<size; i++) nt += (nprocs[i] > 0);
92930dcb7c9SBarry Smith   *nproc    = nt;
930785e854fSJed Brown   ierr = PetscMalloc1((nt+1),procs);CHKERRQ(ierr);
931785e854fSJed Brown   ierr = PetscMalloc1((nt+1),numprocs);CHKERRQ(ierr);
932785e854fSJed Brown   ierr = PetscMalloc1((nt+1),indices);CHKERRQ(ierr);
9330298fd71SBarry Smith   for (i=0;i<nt+1;i++) (*indices)[i]=NULL;
934785e854fSJed Brown   ierr = PetscMalloc1(size,&bprocs);CHKERRQ(ierr);
93530dcb7c9SBarry Smith   cnt       = 0;
93630dcb7c9SBarry Smith   for (i=0; i<size; i++) {
93730dcb7c9SBarry Smith     if (nprocs[i] > 0) {
93830dcb7c9SBarry Smith       bprocs[i]        = cnt;
93930dcb7c9SBarry Smith       (*procs)[cnt]    = i;
94030dcb7c9SBarry Smith       (*numprocs)[cnt] = nprocs[i];
941785e854fSJed Brown       ierr             = PetscMalloc1(nprocs[i],&(*indices)[cnt]);CHKERRQ(ierr);
94230dcb7c9SBarry Smith       cnt++;
94330dcb7c9SBarry Smith     }
94430dcb7c9SBarry Smith   }
94530dcb7c9SBarry Smith 
94630dcb7c9SBarry Smith   /* make the list of subdomains for each nontrivial local node */
94732dcc486SBarry Smith   ierr = PetscMemzero(*numprocs,nt*sizeof(PetscInt));CHKERRQ(ierr);
94830dcb7c9SBarry Smith   cnt  = 0;
94930dcb7c9SBarry Smith   for (i=0; i<nrecvs2; i++) {
95030dcb7c9SBarry Smith     nt = recvs2[cnt++];
95130dcb7c9SBarry Smith     for (j=0; j<nt; j++) {
952f6e5521dSKarl Rupp       for (k=0; k<recvs2[cnt+1]; k++) (*indices)[bprocs[recvs2[cnt+2+k]]][(*numprocs)[bprocs[recvs2[cnt+2+k]]]++] = recvs2[cnt];
95330dcb7c9SBarry Smith       cnt += 2 + recvs2[cnt+1];
95430dcb7c9SBarry Smith     }
95530dcb7c9SBarry Smith   }
95630dcb7c9SBarry Smith   ierr = PetscFree(bprocs);CHKERRQ(ierr);
95707b52d57SBarry Smith   ierr = PetscFree(recvs2);CHKERRQ(ierr);
95830dcb7c9SBarry Smith 
95907b52d57SBarry Smith   /* sort the node indexing by their global numbers */
96007b52d57SBarry Smith   nt = *nproc;
96107b52d57SBarry Smith   for (i=0; i<nt; i++) {
962785e854fSJed Brown     ierr = PetscMalloc1(((*numprocs)[i]),&tmp);CHKERRQ(ierr);
963f6e5521dSKarl Rupp     for (j=0; j<(*numprocs)[i]; j++) tmp[j] = lindices[(*indices)[i][j]];
96407b52d57SBarry Smith     ierr = PetscSortIntWithArray((*numprocs)[i],tmp,(*indices)[i]);CHKERRQ(ierr);
96507b52d57SBarry Smith     ierr = PetscFree(tmp);CHKERRQ(ierr);
96607b52d57SBarry Smith   }
96707b52d57SBarry Smith 
96807b52d57SBarry Smith   if (debug) { /* -----------------------------------  */
96930dcb7c9SBarry Smith     nt = *nproc;
97030dcb7c9SBarry Smith     for (i=0; i<nt; i++) {
9717904a332SBarry Smith       ierr = PetscSynchronizedPrintf(comm,"[%d] subdomain %D number of indices %D: ",rank,(*procs)[i],(*numprocs)[i]);CHKERRQ(ierr);
97230dcb7c9SBarry Smith       for (j=0; j<(*numprocs)[i]; j++) {
9737904a332SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"%D ",(*indices)[i][j]);CHKERRQ(ierr);
97430dcb7c9SBarry Smith       }
97530dcb7c9SBarry Smith       ierr = PetscSynchronizedPrintf(comm,"\n");CHKERRQ(ierr);
97630dcb7c9SBarry Smith     }
9770ec8b6e3SBarry Smith     ierr = PetscSynchronizedFlush(comm,PETSC_STDOUT);CHKERRQ(ierr);
97807b52d57SBarry Smith   } /* -----------------------------------  */
97930dcb7c9SBarry Smith 
98030dcb7c9SBarry Smith   /* wait on sends */
98130dcb7c9SBarry Smith   if (nsends2) {
982785e854fSJed Brown     ierr = PetscMalloc1(nsends2,&send_status);CHKERRQ(ierr);
98330dcb7c9SBarry Smith     ierr = MPI_Waitall(nsends2,send_waits,send_status);CHKERRQ(ierr);
98430dcb7c9SBarry Smith     ierr = PetscFree(send_status);CHKERRQ(ierr);
98530dcb7c9SBarry Smith   }
98630dcb7c9SBarry Smith 
98730dcb7c9SBarry Smith   ierr = PetscFree(starts3);CHKERRQ(ierr);
98830dcb7c9SBarry Smith   ierr = PetscFree(dest);CHKERRQ(ierr);
98930dcb7c9SBarry Smith   ierr = PetscFree(send_waits);CHKERRQ(ierr);
9903677ff5aSBarry Smith 
991bc8ff85bSBarry Smith   ierr = PetscFree(nownedsenders);CHKERRQ(ierr);
992bc8ff85bSBarry Smith   ierr = PetscFree(ownedsenders);CHKERRQ(ierr);
993bc8ff85bSBarry Smith   ierr = PetscFree(starts);CHKERRQ(ierr);
99430dcb7c9SBarry Smith   ierr = PetscFree(starts2);CHKERRQ(ierr);
99530dcb7c9SBarry Smith   ierr = PetscFree(lens2);CHKERRQ(ierr);
99689d82c54SBarry Smith 
99789d82c54SBarry Smith   ierr = PetscFree(source);CHKERRQ(ierr);
99897f1f81fSBarry Smith   ierr = PetscFree(len);CHKERRQ(ierr);
99989d82c54SBarry Smith   ierr = PetscFree(recvs);CHKERRQ(ierr);
10003a96401aSBarry Smith   ierr = PetscFree(nprocs);CHKERRQ(ierr);
100130dcb7c9SBarry Smith   ierr = PetscFree(sends2);CHKERRQ(ierr);
100224cf384cSBarry Smith 
100324cf384cSBarry Smith   /* put the information about myself as the first entry in the list */
100424cf384cSBarry Smith   first_procs    = (*procs)[0];
100524cf384cSBarry Smith   first_numprocs = (*numprocs)[0];
100624cf384cSBarry Smith   first_indices  = (*indices)[0];
100724cf384cSBarry Smith   for (i=0; i<*nproc; i++) {
100824cf384cSBarry Smith     if ((*procs)[i] == rank) {
100924cf384cSBarry Smith       (*procs)[0]    = (*procs)[i];
101024cf384cSBarry Smith       (*numprocs)[0] = (*numprocs)[i];
101124cf384cSBarry Smith       (*indices)[0]  = (*indices)[i];
101224cf384cSBarry Smith       (*procs)[i]    = first_procs;
101324cf384cSBarry Smith       (*numprocs)[i] = first_numprocs;
101424cf384cSBarry Smith       (*indices)[i]  = first_indices;
101524cf384cSBarry Smith       break;
101624cf384cSBarry Smith     }
101724cf384cSBarry Smith   }
101889d82c54SBarry Smith   PetscFunctionReturn(0);
101989d82c54SBarry Smith }
102089d82c54SBarry Smith 
10214a2ae208SSatish Balay #undef __FUNCT__
10224a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingRestoreInfo"
102307b52d57SBarry Smith /*@C
102407b52d57SBarry Smith     ISLocalToGlobalMappingRestoreInfo - Frees the memory allocated by ISLocalToGlobalMappingGetInfo()
102589d82c54SBarry Smith 
102607b52d57SBarry Smith     Collective on ISLocalToGlobalMapping
102707b52d57SBarry Smith 
102807b52d57SBarry Smith     Input Parameters:
102907b52d57SBarry Smith .   mapping - the mapping from local to global indexing
103007b52d57SBarry Smith 
103107b52d57SBarry Smith     Output Parameter:
103207b52d57SBarry Smith +   nproc - number of processors that are connected to this one
103307b52d57SBarry Smith .   proc - neighboring processors
103407b52d57SBarry Smith .   numproc - number of indices for each processor
103507b52d57SBarry Smith -   indices - indices of local nodes shared with neighbor (sorted by global numbering)
103607b52d57SBarry Smith 
103707b52d57SBarry Smith     Level: advanced
103807b52d57SBarry Smith 
103907b52d57SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreate(),
104007b52d57SBarry Smith           ISLocalToGlobalMappingGetInfo()
104107b52d57SBarry Smith @*/
10427087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingRestoreInfo(ISLocalToGlobalMapping mapping,PetscInt *nproc,PetscInt *procs[],PetscInt *numprocs[],PetscInt **indices[])
104307b52d57SBarry Smith {
10446849ba73SBarry Smith   PetscErrorCode ierr;
104532dcc486SBarry Smith   PetscInt       i;
104607b52d57SBarry Smith 
104707b52d57SBarry Smith   PetscFunctionBegin;
104805b42c5fSBarry Smith   ierr = PetscFree(*procs);CHKERRQ(ierr);
104905b42c5fSBarry Smith   ierr = PetscFree(*numprocs);CHKERRQ(ierr);
105000ff320aSBarry Smith   if (*indices) {
105105b42c5fSBarry Smith     ierr = PetscFree((*indices)[0]);CHKERRQ(ierr);
105200ff320aSBarry Smith     for (i=1; i<*nproc; i++) {
105305b42c5fSBarry Smith       ierr = PetscFree((*indices)[i]);CHKERRQ(ierr);
105407b52d57SBarry Smith     }
105507b52d57SBarry Smith     ierr = PetscFree(*indices);CHKERRQ(ierr);
105624cf384cSBarry Smith   }
105707b52d57SBarry Smith   PetscFunctionReturn(0);
105807b52d57SBarry Smith }
105986994e45SJed Brown 
106086994e45SJed Brown #undef __FUNCT__
106186994e45SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingGetIndices"
106286994e45SJed Brown /*@C
106386994e45SJed Brown    ISLocalToGlobalMappingGetIndices - Get global indices for every local point
106486994e45SJed Brown 
106586994e45SJed Brown    Not Collective
106686994e45SJed Brown 
106786994e45SJed Brown    Input Arguments:
106886994e45SJed Brown . ltog - local to global mapping
106986994e45SJed Brown 
107086994e45SJed Brown    Output Arguments:
107186994e45SJed Brown . array - array of indices
107286994e45SJed Brown 
107386994e45SJed Brown    Level: advanced
107486994e45SJed Brown 
107586994e45SJed Brown .seealso: ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingRestoreIndices()
107686994e45SJed Brown @*/
10777087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingGetIndices(ISLocalToGlobalMapping ltog,const PetscInt **array)
107886994e45SJed Brown {
107986994e45SJed Brown   PetscFunctionBegin;
108086994e45SJed Brown   PetscValidHeaderSpecific(ltog,IS_LTOGM_CLASSID,1);
108186994e45SJed Brown   PetscValidPointer(array,2);
108286994e45SJed Brown   *array = ltog->indices;
108386994e45SJed Brown   PetscFunctionReturn(0);
108486994e45SJed Brown }
108586994e45SJed Brown 
108686994e45SJed Brown #undef __FUNCT__
108786994e45SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingRestoreIndices"
108886994e45SJed Brown /*@C
108986994e45SJed Brown    ISLocalToGlobalMappingRestoreIndices - Restore indices obtained with ISLocalToGlobalMappingRestoreIndices()
109086994e45SJed Brown 
109186994e45SJed Brown    Not Collective
109286994e45SJed Brown 
109386994e45SJed Brown    Input Arguments:
109486994e45SJed Brown + ltog - local to global mapping
109586994e45SJed Brown - array - array of indices
109686994e45SJed Brown 
109786994e45SJed Brown    Level: advanced
109886994e45SJed Brown 
109986994e45SJed Brown .seealso: ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingGetIndices()
110086994e45SJed Brown @*/
11017087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingRestoreIndices(ISLocalToGlobalMapping ltog,const PetscInt **array)
110286994e45SJed Brown {
110386994e45SJed Brown   PetscFunctionBegin;
110486994e45SJed Brown   PetscValidHeaderSpecific(ltog,IS_LTOGM_CLASSID,1);
110586994e45SJed Brown   PetscValidPointer(array,2);
110686994e45SJed Brown   if (*array != ltog->indices) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_BADPTR,"Trying to return mismatched pointer");
11070298fd71SBarry Smith   *array = NULL;
110886994e45SJed Brown   PetscFunctionReturn(0);
110986994e45SJed Brown }
1110f7efa3c7SJed Brown 
1111f7efa3c7SJed Brown #undef __FUNCT__
1112f7efa3c7SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingConcatenate"
1113f7efa3c7SJed Brown /*@C
1114f7efa3c7SJed Brown    ISLocalToGlobalMappingConcatenate - Create a new mapping that concatenates a list of mappings
1115f7efa3c7SJed Brown 
1116f7efa3c7SJed Brown    Not Collective
1117f7efa3c7SJed Brown 
1118f7efa3c7SJed Brown    Input Arguments:
1119f7efa3c7SJed Brown + comm - communicator for the new mapping, must contain the communicator of every mapping to concatenate
1120f7efa3c7SJed Brown . n - number of mappings to concatenate
1121f7efa3c7SJed Brown - ltogs - local to global mappings
1122f7efa3c7SJed Brown 
1123f7efa3c7SJed Brown    Output Arguments:
1124f7efa3c7SJed Brown . ltogcat - new mapping
1125f7efa3c7SJed Brown 
1126f7efa3c7SJed Brown    Level: advanced
1127f7efa3c7SJed Brown 
1128f7efa3c7SJed Brown .seealso: ISLocalToGlobalMappingCreate()
1129f7efa3c7SJed Brown @*/
1130f7efa3c7SJed Brown PetscErrorCode ISLocalToGlobalMappingConcatenate(MPI_Comm comm,PetscInt n,const ISLocalToGlobalMapping ltogs[],ISLocalToGlobalMapping *ltogcat)
1131f7efa3c7SJed Brown {
1132f7efa3c7SJed Brown   PetscInt       i,cnt,m,*idx;
1133f7efa3c7SJed Brown   PetscErrorCode ierr;
1134f7efa3c7SJed Brown 
1135f7efa3c7SJed Brown   PetscFunctionBegin;
1136f7efa3c7SJed Brown   if (n < 0) SETERRQ1(comm,PETSC_ERR_ARG_OUTOFRANGE,"Must have a non-negative number of mappings, given %D",n);
1137f7efa3c7SJed Brown   if (n > 0) PetscValidPointer(ltogs,3);
1138f7efa3c7SJed Brown   for (i=0; i<n; i++) PetscValidHeaderSpecific(ltogs[i],IS_LTOGM_CLASSID,3);
1139f7efa3c7SJed Brown   PetscValidPointer(ltogcat,4);
1140f7efa3c7SJed Brown   for (cnt=0,i=0; i<n; i++) {
1141f7efa3c7SJed Brown     ierr = ISLocalToGlobalMappingGetSize(ltogs[i],&m);CHKERRQ(ierr);
1142f7efa3c7SJed Brown     cnt += m;
1143f7efa3c7SJed Brown   }
1144785e854fSJed Brown   ierr = PetscMalloc1(cnt,&idx);CHKERRQ(ierr);
1145f7efa3c7SJed Brown   for (cnt=0,i=0; i<n; i++) {
1146f7efa3c7SJed Brown     const PetscInt *subidx;
1147f7efa3c7SJed Brown     ierr = ISLocalToGlobalMappingGetSize(ltogs[i],&m);CHKERRQ(ierr);
1148f7efa3c7SJed Brown     ierr = ISLocalToGlobalMappingGetIndices(ltogs[i],&subidx);CHKERRQ(ierr);
1149f7efa3c7SJed Brown     ierr = PetscMemcpy(&idx[cnt],subidx,m*sizeof(PetscInt));CHKERRQ(ierr);
1150f7efa3c7SJed Brown     ierr = ISLocalToGlobalMappingRestoreIndices(ltogs[i],&subidx);CHKERRQ(ierr);
1151f7efa3c7SJed Brown     cnt += m;
1152f7efa3c7SJed Brown   }
1153*f0413b6fSBarry Smith   ierr = ISLocalToGlobalMappingCreate(comm,1,cnt,idx,PETSC_OWN_POINTER,ltogcat);CHKERRQ(ierr);
1154f7efa3c7SJed Brown   PetscFunctionReturn(0);
1155f7efa3c7SJed Brown }
115604a59952SBarry Smith 
115704a59952SBarry Smith 
1158