xref: /petsc/src/vec/is/utils/isltog.c (revision 45b6f7e9ecd564e8bb535880b270d4a8084ff211)
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 
119f0413b6fSBarry 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);
142f0413b6fSBarry Smith   /*  if (!isblock) { */
143f0413b6fSBarry Smith     ierr = ISGetIndices(is,&indices);CHKERRQ(ierr);
144f0413b6fSBarry Smith     ierr = ISLocalToGlobalMappingCreate(comm,1,n,indices,PETSC_COPY_VALUES,mapping);CHKERRQ(ierr);
1452bdab257SBarry Smith     ierr = ISRestoreIndices(is,&indices);CHKERRQ(ierr);
146f0413b6fSBarry Smith     /*  } else {
147f0413b6fSBarry Smith     ierr = ISBlockGetIndices(is,&indices);CHKERRQ(ierr);
148f0413b6fSBarry Smith     ierr = ISLocalToGlobalMappingCreate(comm,bs,n,indices,PETSC_COPY_VALUES,mapping);CHKERRQ(ierr);
149f0413b6fSBarry Smith     ierr = ISBlockRestoreIndices(is,&indices);CHKERRQ(ierr);
150f0413b6fSBarry 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);
198f0413b6fSBarry 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__
204*45b6f7e9SBarry Smith #define __FUNCT__ "ISLocalToGlobalMappingGetBlockSize"
205*45b6f7e9SBarry Smith /*@
206*45b6f7e9SBarry Smith     ISLocalToGlobalMappingGetBlockSize - Gets the blocksize of the mapping
207*45b6f7e9SBarry Smith     ordering and a global parallel ordering.
208*45b6f7e9SBarry Smith 
209*45b6f7e9SBarry Smith     Not Collective
210*45b6f7e9SBarry Smith 
211*45b6f7e9SBarry Smith     Input Parameters:
212*45b6f7e9SBarry Smith .   mapping - mapping data structure
213*45b6f7e9SBarry Smith 
214*45b6f7e9SBarry Smith     Output Parameter:
215*45b6f7e9SBarry Smith .   bs - the blocksize
216*45b6f7e9SBarry Smith 
217*45b6f7e9SBarry Smith     Level: advanced
218*45b6f7e9SBarry Smith 
219*45b6f7e9SBarry Smith     Concepts: mapping^local to global
220*45b6f7e9SBarry Smith 
221*45b6f7e9SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS()
222*45b6f7e9SBarry Smith @*/
223*45b6f7e9SBarry Smith PetscErrorCode  ISLocalToGlobalMappingGetBlockSize(ISLocalToGlobalMapping mapping,PetscInt *bs)
224*45b6f7e9SBarry Smith {
225*45b6f7e9SBarry Smith   PetscFunctionBegin;
226*45b6f7e9SBarry Smith   *bs = mapping->bs;
227*45b6f7e9SBarry Smith   PetscFunctionReturn(0);
228*45b6f7e9SBarry Smith }
229*45b6f7e9SBarry Smith 
230*45b6f7e9SBarry Smith #undef __FUNCT__
2314a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingCreate"
232ba5bb76aSSatish Balay /*@
23390f02eecSBarry Smith     ISLocalToGlobalMappingCreate - Creates a mapping between a local (0 to n)
23490f02eecSBarry Smith     ordering and a global parallel ordering.
2352362add9SBarry Smith 
23689d82c54SBarry Smith     Not Collective, but communicator may have more than one process
237b9cd556bSLois Curfman McInnes 
2382362add9SBarry Smith     Input Parameters:
23989d82c54SBarry Smith +   comm - MPI communicator
240f0413b6fSBarry Smith .   bs - the block size
24190f02eecSBarry Smith .   n - the number of local elements
242f0413b6fSBarry 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
243d5ad8652SBarry Smith -   mode - see PetscCopyMode
2442362add9SBarry Smith 
245a997ad1aSLois Curfman McInnes     Output Parameter:
24690f02eecSBarry Smith .   mapping - new mapping data structure
2472362add9SBarry Smith 
248f0413b6fSBarry 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
249a997ad1aSLois Curfman McInnes     Level: advanced
250a997ad1aSLois Curfman McInnes 
251273d9f13SBarry Smith     Concepts: mapping^local to global
2522362add9SBarry Smith 
253d5ad8652SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS()
2542362add9SBarry Smith @*/
255f0413b6fSBarry Smith PetscErrorCode  ISLocalToGlobalMappingCreate(MPI_Comm cm,PetscInt bs,PetscInt n,const PetscInt indices[],PetscCopyMode mode,ISLocalToGlobalMapping *mapping)
2562362add9SBarry Smith {
2576849ba73SBarry Smith   PetscErrorCode ierr;
25832dcc486SBarry Smith   PetscInt       *in;
259b46b645bSBarry Smith 
260b46b645bSBarry Smith   PetscFunctionBegin;
26173911063SBarry Smith   if (n) PetscValidIntPointer(indices,3);
2624482741eSBarry Smith   PetscValidPointer(mapping,4);
263b46b645bSBarry Smith 
2640298fd71SBarry Smith   *mapping = NULL;
265607a6623SBarry Smith   ierr = ISInitializePackage();CHKERRQ(ierr);
2662362add9SBarry Smith 
26767c2884eSBarry Smith   ierr = PetscHeaderCreate(*mapping,_p_ISLocalToGlobalMapping,int,IS_LTOGM_CLASSID,"ISLocalToGlobalMapping","Local to global mapping","IS",
26852e6d16bSBarry Smith                            cm,ISLocalToGlobalMappingDestroy,ISLocalToGlobalMappingView);CHKERRQ(ierr);
269d4bb536fSBarry Smith   (*mapping)->n  = n;
270f0413b6fSBarry Smith   (*mapping)->bs = bs;
271d4bb536fSBarry Smith   /*
272d4bb536fSBarry Smith     Do not create the global to local mapping. This is only created if
273d4bb536fSBarry Smith     ISGlobalToLocalMapping() is called
274d4bb536fSBarry Smith   */
275d4bb536fSBarry Smith   (*mapping)->globals = 0;
276d5ad8652SBarry Smith   if (mode == PETSC_COPY_VALUES) {
277785e854fSJed Brown     ierr = PetscMalloc1(n,&in);CHKERRQ(ierr);
278d5ad8652SBarry Smith     ierr = PetscMemcpy(in,indices,n*sizeof(PetscInt));CHKERRQ(ierr);
2793bb1ff40SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)*mapping,n*sizeof(PetscInt));CHKERRQ(ierr);
280d5ad8652SBarry Smith     (*mapping)->indices = in;
281f6e5521dSKarl Rupp   } else if (mode == PETSC_OWN_POINTER) (*mapping)->indices = (PetscInt*)indices;
282f6e5521dSKarl Rupp   else SETERRQ(cm,PETSC_ERR_SUP,"Cannot currently use PETSC_USE_POINTER");
2833a40ed3dSBarry Smith   PetscFunctionReturn(0);
2842362add9SBarry Smith }
2852362add9SBarry Smith 
2864a2ae208SSatish Balay #undef __FUNCT__
2874a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingDestroy"
28890f02eecSBarry Smith /*@
28990f02eecSBarry Smith    ISLocalToGlobalMappingDestroy - Destroys a mapping between a local (0 to n)
29090f02eecSBarry Smith    ordering and a global parallel ordering.
29190f02eecSBarry Smith 
2920f5bd95cSBarry Smith    Note Collective
293b9cd556bSLois Curfman McInnes 
29490f02eecSBarry Smith    Input Parameters:
29590f02eecSBarry Smith .  mapping - mapping data structure
29690f02eecSBarry Smith 
297a997ad1aSLois Curfman McInnes    Level: advanced
298a997ad1aSLois Curfman McInnes 
2993acfe500SLois Curfman McInnes .seealso: ISLocalToGlobalMappingCreate()
30090f02eecSBarry Smith @*/
3016bf464f9SBarry Smith PetscErrorCode  ISLocalToGlobalMappingDestroy(ISLocalToGlobalMapping *mapping)
30290f02eecSBarry Smith {
303dfbe8321SBarry Smith   PetscErrorCode ierr;
3045fd66863SKarl Rupp 
3053a40ed3dSBarry Smith   PetscFunctionBegin;
3066bf464f9SBarry Smith   if (!*mapping) PetscFunctionReturn(0);
3076bf464f9SBarry Smith   PetscValidHeaderSpecific((*mapping),IS_LTOGM_CLASSID,1);
308997056adSBarry Smith   if (--((PetscObject)(*mapping))->refct > 0) {*mapping = 0;PetscFunctionReturn(0);}
3096bf464f9SBarry Smith   ierr     = PetscFree((*mapping)->indices);CHKERRQ(ierr);
3106bf464f9SBarry Smith   ierr     = PetscFree((*mapping)->globals);CHKERRQ(ierr);
311d38fa0fbSBarry Smith   ierr     = PetscHeaderDestroy(mapping);CHKERRQ(ierr);
312992144d0SBarry Smith   *mapping = 0;
3133a40ed3dSBarry Smith   PetscFunctionReturn(0);
31490f02eecSBarry Smith }
31590f02eecSBarry Smith 
3164a2ae208SSatish Balay #undef __FUNCT__
3174a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingApplyIS"
31890f02eecSBarry Smith /*@
3193acfe500SLois Curfman McInnes     ISLocalToGlobalMappingApplyIS - Creates from an IS in the local numbering
3203acfe500SLois Curfman McInnes     a new index set using the global numbering defined in an ISLocalToGlobalMapping
3213acfe500SLois Curfman McInnes     context.
32290f02eecSBarry Smith 
323b9cd556bSLois Curfman McInnes     Not collective
324b9cd556bSLois Curfman McInnes 
32590f02eecSBarry Smith     Input Parameters:
326b9cd556bSLois Curfman McInnes +   mapping - mapping between local and global numbering
327b9cd556bSLois Curfman McInnes -   is - index set in local numbering
32890f02eecSBarry Smith 
32990f02eecSBarry Smith     Output Parameters:
33090f02eecSBarry Smith .   newis - index set in global numbering
33190f02eecSBarry Smith 
332a997ad1aSLois Curfman McInnes     Level: advanced
333a997ad1aSLois Curfman McInnes 
334273d9f13SBarry Smith     Concepts: mapping^local to global
3353acfe500SLois Curfman McInnes 
33690f02eecSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),
337d4bb536fSBarry Smith           ISLocalToGlobalMappingDestroy(), ISGlobalToLocalMappingApply()
33890f02eecSBarry Smith @*/
3397087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingApplyIS(ISLocalToGlobalMapping mapping,IS is,IS *newis)
34090f02eecSBarry Smith {
3416849ba73SBarry Smith   PetscErrorCode ierr;
3425d0c19d7SBarry Smith   PetscInt       n,i,*idxmap,*idxout,Nmax = mapping->n;
3435d0c19d7SBarry Smith   const PetscInt *idxin;
3443a40ed3dSBarry Smith 
3453a40ed3dSBarry Smith   PetscFunctionBegin;
3460700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
3470700a824SBarry Smith   PetscValidHeaderSpecific(is,IS_CLASSID,2);
3484482741eSBarry Smith   PetscValidPointer(newis,3);
34990f02eecSBarry Smith 
3503b9aefa3SBarry Smith   ierr   = ISGetLocalSize(is,&n);CHKERRQ(ierr);
35190f02eecSBarry Smith   ierr   = ISGetIndices(is,&idxin);CHKERRQ(ierr);
35290f02eecSBarry Smith   idxmap = mapping->indices;
35390f02eecSBarry Smith 
354785e854fSJed Brown   ierr = PetscMalloc1(n,&idxout);CHKERRQ(ierr);
35590f02eecSBarry Smith   for (i=0; i<n; i++) {
3567904a332SBarry 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);
35790f02eecSBarry Smith     idxout[i] = idxmap[idxin[i]];
35890f02eecSBarry Smith   }
3593b9aefa3SBarry Smith   ierr = ISRestoreIndices(is,&idxin);CHKERRQ(ierr);
360543f3098SMatthew G. Knepley   ierr = ISCreateGeneral(PetscObjectComm((PetscObject)is),n,idxout,PETSC_OWN_POINTER,newis);CHKERRQ(ierr);
3613a40ed3dSBarry Smith   PetscFunctionReturn(0);
36290f02eecSBarry Smith }
36390f02eecSBarry Smith 
364afcb2eb5SJed Brown #undef __FUNCT__
365afcb2eb5SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingApply"
366b89cb25eSSatish Balay /*@
3673acfe500SLois Curfman McInnes    ISLocalToGlobalMappingApply - Takes a list of integers in a local numbering
3683acfe500SLois Curfman McInnes    and converts them to the global numbering.
36990f02eecSBarry Smith 
370b9cd556bSLois Curfman McInnes    Not collective
371b9cd556bSLois Curfman McInnes 
372bb25748dSBarry Smith    Input Parameters:
373b9cd556bSLois Curfman McInnes +  mapping - the local to global mapping context
374bb25748dSBarry Smith .  N - number of integers
375b9cd556bSLois Curfman McInnes -  in - input indices in local numbering
376bb25748dSBarry Smith 
377bb25748dSBarry Smith    Output Parameter:
378bb25748dSBarry Smith .  out - indices in global numbering
379bb25748dSBarry Smith 
380b9cd556bSLois Curfman McInnes    Notes:
381b9cd556bSLois Curfman McInnes    The in and out array parameters may be identical.
382d4bb536fSBarry Smith 
383a997ad1aSLois Curfman McInnes    Level: advanced
384a997ad1aSLois Curfman McInnes 
385*45b6f7e9SBarry Smith .seealso: ISLocalToGlobalMappingApplyBlock(), ISLocalToGlobalMappingCreate(),ISLocalToGlobalMappingDestroy(),
3860752156aSBarry Smith           ISLocalToGlobalMappingApplyIS(),AOCreateBasic(),AOApplicationToPetsc(),
387d4bb536fSBarry Smith           AOPetscToApplication(), ISGlobalToLocalMappingApply()
388bb25748dSBarry Smith 
389273d9f13SBarry Smith     Concepts: mapping^local to global
390afcb2eb5SJed Brown @*/
391afcb2eb5SJed Brown PetscErrorCode ISLocalToGlobalMappingApply(ISLocalToGlobalMapping mapping,PetscInt N,const PetscInt in[],PetscInt out[])
392afcb2eb5SJed Brown {
393*45b6f7e9SBarry Smith   PetscInt       i,Nmax = mapping->n,bs = mapping->bs;
394*45b6f7e9SBarry Smith   const PetscInt *idx = mapping->indices;
395*45b6f7e9SBarry Smith 
396*45b6f7e9SBarry Smith   PetscFunctionBegin;
397*45b6f7e9SBarry Smith   if (bs == 1) {
398*45b6f7e9SBarry Smith     for (i=0; i<N; i++) {
399*45b6f7e9SBarry Smith       if (in[i] < 0) {
400*45b6f7e9SBarry Smith         out[i] = in[i];
401*45b6f7e9SBarry Smith         continue;
402*45b6f7e9SBarry Smith       }
403*45b6f7e9SBarry Smith       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);
404*45b6f7e9SBarry Smith       out[i] = idx[in[i]];
405*45b6f7e9SBarry Smith     }
406*45b6f7e9SBarry Smith   } else {
407*45b6f7e9SBarry Smith     for (i=0; i<N; i++) {
408*45b6f7e9SBarry Smith       if (in[i] < 0) {
409*45b6f7e9SBarry Smith         out[i] = in[i];
410*45b6f7e9SBarry Smith         continue;
411*45b6f7e9SBarry Smith       }
412*45b6f7e9SBarry Smith       if (in[i] >= bs*Nmax) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local index %D too large %D (max) at %D",in[i],Nmax,i);
413*45b6f7e9SBarry Smith       out[i] = idx[in[i]/bs]*bs + (in[i] % bs);
414*45b6f7e9SBarry Smith     }
415*45b6f7e9SBarry Smith   }
416*45b6f7e9SBarry Smith   PetscFunctionReturn(0);
417*45b6f7e9SBarry Smith }
418*45b6f7e9SBarry Smith 
419*45b6f7e9SBarry Smith #undef __FUNCT__
420*45b6f7e9SBarry Smith #define __FUNCT__ "ISLocalToGlobalMappingApplyBlock"
421*45b6f7e9SBarry Smith /*@
422*45b6f7e9SBarry Smith    ISLocalToGlobalMappingApplyBlock - Takes a list of integers in a local numbering  and converts them to the global numbering.
423*45b6f7e9SBarry Smith 
424*45b6f7e9SBarry Smith    Not collective
425*45b6f7e9SBarry Smith 
426*45b6f7e9SBarry Smith    Input Parameters:
427*45b6f7e9SBarry Smith +  mapping - the local to global mapping context
428*45b6f7e9SBarry Smith .  N - number of integers
429*45b6f7e9SBarry Smith -  in - input indices in local numbering
430*45b6f7e9SBarry Smith 
431*45b6f7e9SBarry Smith    Output Parameter:
432*45b6f7e9SBarry Smith .  out - indices in global numbering
433*45b6f7e9SBarry Smith 
434*45b6f7e9SBarry Smith    Notes:
435*45b6f7e9SBarry Smith    The in and out array parameters may be identical.
436*45b6f7e9SBarry Smith 
437*45b6f7e9SBarry Smith    Level: advanced
438*45b6f7e9SBarry Smith 
439*45b6f7e9SBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),ISLocalToGlobalMappingDestroy(),
440*45b6f7e9SBarry Smith           ISLocalToGlobalMappingApplyIS(),AOCreateBasic(),AOApplicationToPetsc(),
441*45b6f7e9SBarry Smith           AOPetscToApplication(), ISGlobalToLocalMappingApply()
442*45b6f7e9SBarry Smith 
443*45b6f7e9SBarry Smith     Concepts: mapping^local to global
444*45b6f7e9SBarry Smith @*/
445*45b6f7e9SBarry Smith PetscErrorCode ISLocalToGlobalMappingApplyBlock(ISLocalToGlobalMapping mapping,PetscInt N,const PetscInt in[],PetscInt out[])
446*45b6f7e9SBarry Smith {
447afcb2eb5SJed Brown   PetscInt       i,Nmax = mapping->n;
448afcb2eb5SJed Brown   const PetscInt *idx = mapping->indices;
449d4bb536fSBarry Smith 
450afcb2eb5SJed Brown   PetscFunctionBegin;
451afcb2eb5SJed Brown   for (i=0; i<N; i++) {
452afcb2eb5SJed Brown     if (in[i] < 0) {
453afcb2eb5SJed Brown       out[i] = in[i];
454afcb2eb5SJed Brown       continue;
455afcb2eb5SJed Brown     }
456*45b6f7e9SBarry Smith     if (in[i] >= Nmax) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Local block index %D too large %D (max) at %D",in[i]*mapping->bs,Nmax,i);
457afcb2eb5SJed Brown     out[i] = idx[in[i]];
458afcb2eb5SJed Brown   }
459afcb2eb5SJed Brown   PetscFunctionReturn(0);
460afcb2eb5SJed Brown }
461d4bb536fSBarry Smith 
462d4bb536fSBarry Smith /* -----------------------------------------------------------------------------------------*/
463d4bb536fSBarry Smith 
4644a2ae208SSatish Balay #undef __FUNCT__
4654a2ae208SSatish Balay #define __FUNCT__ "ISGlobalToLocalMappingSetUp_Private"
466d4bb536fSBarry Smith /*
467d4bb536fSBarry Smith     Creates the global fields in the ISLocalToGlobalMapping structure
468d4bb536fSBarry Smith */
4696849ba73SBarry Smith static PetscErrorCode ISGlobalToLocalMappingSetUp_Private(ISLocalToGlobalMapping mapping)
470d4bb536fSBarry Smith {
4716849ba73SBarry Smith   PetscErrorCode ierr;
47232dcc486SBarry Smith   PetscInt       i,*idx = mapping->indices,n = mapping->n,end,start,*globals;
473d4bb536fSBarry Smith 
4743a40ed3dSBarry Smith   PetscFunctionBegin;
475d4bb536fSBarry Smith   end   = 0;
476ec268f7cSJed Brown   start = PETSC_MAX_INT;
477d4bb536fSBarry Smith 
478d4bb536fSBarry Smith   for (i=0; i<n; i++) {
479d4bb536fSBarry Smith     if (idx[i] < 0) continue;
480d4bb536fSBarry Smith     if (idx[i] < start) start = idx[i];
481d4bb536fSBarry Smith     if (idx[i] > end)   end   = idx[i];
482d4bb536fSBarry Smith   }
483d4bb536fSBarry Smith   if (start > end) {start = 0; end = -1;}
484d4bb536fSBarry Smith   mapping->globalstart = start;
485d4bb536fSBarry Smith   mapping->globalend   = end;
486d4bb536fSBarry Smith 
487785e854fSJed Brown   ierr             = PetscMalloc1((end-start+2),&globals);CHKERRQ(ierr);
488b0a32e0cSBarry Smith   mapping->globals = globals;
489f6e5521dSKarl Rupp   for (i=0; i<end-start+1; i++) globals[i] = -1;
490d4bb536fSBarry Smith   for (i=0; i<n; i++) {
491d4bb536fSBarry Smith     if (idx[i] < 0) continue;
492d4bb536fSBarry Smith     globals[idx[i] - start] = i;
493d4bb536fSBarry Smith   }
494d4bb536fSBarry Smith 
4953bb1ff40SBarry Smith   ierr = PetscLogObjectMemory((PetscObject)mapping,(end-start+1)*sizeof(PetscInt));CHKERRQ(ierr);
4963a40ed3dSBarry Smith   PetscFunctionReturn(0);
497d4bb536fSBarry Smith }
498d4bb536fSBarry Smith 
4994a2ae208SSatish Balay #undef __FUNCT__
5004a2ae208SSatish Balay #define __FUNCT__ "ISGlobalToLocalMappingApply"
501d4bb536fSBarry Smith /*@
502a997ad1aSLois Curfman McInnes     ISGlobalToLocalMappingApply - Provides the local numbering for a list of integers
503a997ad1aSLois Curfman McInnes     specified with a global numbering.
504d4bb536fSBarry Smith 
505b9cd556bSLois Curfman McInnes     Not collective
506b9cd556bSLois Curfman McInnes 
507d4bb536fSBarry Smith     Input Parameters:
508b9cd556bSLois Curfman McInnes +   mapping - mapping between local and global numbering
509d4bb536fSBarry Smith .   type - IS_GTOLM_MASK - replaces global indices with no local value with -1
510d4bb536fSBarry Smith            IS_GTOLM_DROP - drops the indices with no local value from the output list
511d4bb536fSBarry Smith .   n - number of global indices to map
512b9cd556bSLois Curfman McInnes -   idx - global indices to map
513d4bb536fSBarry Smith 
514d4bb536fSBarry Smith     Output Parameters:
515b9cd556bSLois Curfman McInnes +   nout - number of indices in output array (if type == IS_GTOLM_MASK then nout = n)
516b9cd556bSLois Curfman McInnes -   idxout - local index of each global index, one must pass in an array long enough
517e182c471SBarry Smith              to hold all the indices. You can call ISGlobalToLocalMappingApply() with
5180298fd71SBarry Smith              idxout == NULL to determine the required length (returned in nout)
519e182c471SBarry Smith              and then allocate the required space and call ISGlobalToLocalMappingApply()
520e182c471SBarry Smith              a second time to set the values.
521d4bb536fSBarry Smith 
522b9cd556bSLois Curfman McInnes     Notes:
5230298fd71SBarry Smith     Either nout or idxout may be NULL. idx and idxout may be identical.
524d4bb536fSBarry Smith 
5250f5bd95cSBarry Smith     This is not scalable in memory usage. Each processor requires O(Nglobal) size
5260f5bd95cSBarry Smith     array to compute these.
5270f5bd95cSBarry Smith 
528a997ad1aSLois Curfman McInnes     Level: advanced
529a997ad1aSLois Curfman McInnes 
53032fd6b96SBarry Smith     Developer Note: The manual page states that idx and idxout may be identical but the calling
53132fd6b96SBarry Smith        sequence declares idx as const so it cannot be the same as idxout.
53232fd6b96SBarry Smith 
533273d9f13SBarry Smith     Concepts: mapping^global to local
534d4bb536fSBarry Smith 
535d4bb536fSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),
536d4bb536fSBarry Smith           ISLocalToGlobalMappingDestroy()
537d4bb536fSBarry Smith @*/
5387087cfbeSBarry Smith PetscErrorCode  ISGlobalToLocalMappingApply(ISLocalToGlobalMapping mapping,ISGlobalToLocalMappingType type,
53932dcc486SBarry Smith                                   PetscInt n,const PetscInt idx[],PetscInt *nout,PetscInt idxout[])
540d4bb536fSBarry Smith {
54132dcc486SBarry Smith   PetscInt       i,*globals,nf = 0,tmp,start,end;
5426849ba73SBarry Smith   PetscErrorCode ierr;
543d4bb536fSBarry Smith 
5443a40ed3dSBarry Smith   PetscFunctionBegin;
5450700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
546d4bb536fSBarry Smith   if (!mapping->globals) {
547d4bb536fSBarry Smith     ierr = ISGlobalToLocalMappingSetUp_Private(mapping);CHKERRQ(ierr);
548d4bb536fSBarry Smith   }
549d4bb536fSBarry Smith   globals = mapping->globals;
550d4bb536fSBarry Smith   start   = mapping->globalstart;
551d4bb536fSBarry Smith   end     = mapping->globalend;
552d4bb536fSBarry Smith 
553d4bb536fSBarry Smith   if (type == IS_GTOLM_MASK) {
554d4bb536fSBarry Smith     if (idxout) {
555d4bb536fSBarry Smith       for (i=0; i<n; i++) {
556d4bb536fSBarry Smith         if (idx[i] < 0) idxout[i] = idx[i];
557d4bb536fSBarry Smith         else if (idx[i] < start) idxout[i] = -1;
558d4bb536fSBarry Smith         else if (idx[i] > end)   idxout[i] = -1;
559d4bb536fSBarry Smith         else                     idxout[i] = globals[idx[i] - start];
560d4bb536fSBarry Smith       }
561d4bb536fSBarry Smith     }
562d4bb536fSBarry Smith     if (nout) *nout = n;
563d4bb536fSBarry Smith   } else {
564d4bb536fSBarry Smith     if (idxout) {
565d4bb536fSBarry Smith       for (i=0; i<n; i++) {
566d4bb536fSBarry Smith         if (idx[i] < 0) continue;
567d4bb536fSBarry Smith         if (idx[i] < start) continue;
568d4bb536fSBarry Smith         if (idx[i] > end) continue;
569d4bb536fSBarry Smith         tmp = globals[idx[i] - start];
570d4bb536fSBarry Smith         if (tmp < 0) continue;
571d4bb536fSBarry Smith         idxout[nf++] = tmp;
572d4bb536fSBarry Smith       }
573d4bb536fSBarry Smith     } else {
574d4bb536fSBarry Smith       for (i=0; i<n; i++) {
575d4bb536fSBarry Smith         if (idx[i] < 0) continue;
576d4bb536fSBarry Smith         if (idx[i] < start) continue;
577d4bb536fSBarry Smith         if (idx[i] > end) continue;
578d4bb536fSBarry Smith         tmp = globals[idx[i] - start];
579d4bb536fSBarry Smith         if (tmp < 0) continue;
580d4bb536fSBarry Smith         nf++;
581d4bb536fSBarry Smith       }
582d4bb536fSBarry Smith     }
583d4bb536fSBarry Smith     if (nout) *nout = nf;
584d4bb536fSBarry Smith   }
5853a40ed3dSBarry Smith   PetscFunctionReturn(0);
586d4bb536fSBarry Smith }
58790f02eecSBarry Smith 
5884a2ae208SSatish Balay #undef __FUNCT__
5894a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingGetInfo"
59089d82c54SBarry Smith /*@C
59189d82c54SBarry Smith     ISLocalToGlobalMappingGetInfo - Gets the neighbor information for each processor and
59289d82c54SBarry Smith      each index shared by more than one processor
59389d82c54SBarry Smith 
59489d82c54SBarry Smith     Collective on ISLocalToGlobalMapping
59589d82c54SBarry Smith 
59689d82c54SBarry Smith     Input Parameters:
59789d82c54SBarry Smith .   mapping - the mapping from local to global indexing
59889d82c54SBarry Smith 
59989d82c54SBarry Smith     Output Parameter:
60089d82c54SBarry Smith +   nproc - number of processors that are connected to this one
60189d82c54SBarry Smith .   proc - neighboring processors
60207b52d57SBarry Smith .   numproc - number of indices for each subdomain (processor)
6033463a7baSJed Brown -   indices - indices of nodes (in local numbering) shared with neighbors (sorted by global numbering)
60489d82c54SBarry Smith 
60589d82c54SBarry Smith     Level: advanced
60689d82c54SBarry Smith 
607273d9f13SBarry Smith     Concepts: mapping^local to global
60889d82c54SBarry Smith 
6092cfcea29SBarry Smith     Fortran Usage:
6102cfcea29SBarry Smith $        ISLocalToGlobalMpngGetInfoSize(ISLocalToGlobalMapping,PetscInt nproc,PetscInt numprocmax,ierr) followed by
6112cfcea29SBarry Smith $        ISLocalToGlobalMappingGetInfo(ISLocalToGlobalMapping,PetscInt nproc, PetscInt procs[nproc],PetscInt numprocs[nproc],
6122cfcea29SBarry Smith           PetscInt indices[nproc][numprocmax],ierr)
6132cfcea29SBarry Smith         There is no ISLocalToGlobalMappingRestoreInfo() in Fortran. You must make sure that procs[], numprocs[] and
6142cfcea29SBarry Smith         indices[][] are large enough arrays, either by allocating them dynamically or defining static ones large enough.
6152cfcea29SBarry Smith 
6162cfcea29SBarry Smith 
61707b52d57SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreate(),
61807b52d57SBarry Smith           ISLocalToGlobalMappingRestoreInfo()
61989d82c54SBarry Smith @*/
6207087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingGetInfo(ISLocalToGlobalMapping mapping,PetscInt *nproc,PetscInt *procs[],PetscInt *numprocs[],PetscInt **indices[])
62189d82c54SBarry Smith {
6226849ba73SBarry Smith   PetscErrorCode ierr;
62397f1f81fSBarry Smith   PetscMPIInt    size,rank,tag1,tag2,tag3,*len,*source,imdex;
62432dcc486SBarry Smith   PetscInt       i,n = mapping->n,Ng,ng,max = 0,*lindices = mapping->indices;
62532dcc486SBarry Smith   PetscInt       *nprocs,*owner,nsends,*sends,j,*starts,nmax,nrecvs,*recvs,proc;
62697f1f81fSBarry Smith   PetscInt       cnt,scale,*ownedsenders,*nownedsenders,rstart,nowned;
62732dcc486SBarry Smith   PetscInt       node,nownedm,nt,*sends2,nsends2,*starts2,*lens2,*dest,nrecvs2,*starts3,*recvs2,k,*bprocs,*tmp;
62832dcc486SBarry Smith   PetscInt       first_procs,first_numprocs,*first_indices;
62989d82c54SBarry Smith   MPI_Request    *recv_waits,*send_waits;
63030dcb7c9SBarry Smith   MPI_Status     recv_status,*send_status,*recv_statuses;
631ce94432eSBarry Smith   MPI_Comm       comm;
632ace3abfcSBarry Smith   PetscBool      debug = PETSC_FALSE;
63389d82c54SBarry Smith 
63489d82c54SBarry Smith   PetscFunctionBegin;
6350700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
636ce94432eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)mapping,&comm);CHKERRQ(ierr);
63724cf384cSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
63824cf384cSBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
63924cf384cSBarry Smith   if (size == 1) {
64024cf384cSBarry Smith     *nproc         = 0;
6410298fd71SBarry Smith     *procs         = NULL;
64232dcc486SBarry Smith     ierr           = PetscMalloc(sizeof(PetscInt),numprocs);CHKERRQ(ierr);
6431e2105dcSBarry Smith     (*numprocs)[0] = 0;
64432dcc486SBarry Smith     ierr           = PetscMalloc(sizeof(PetscInt*),indices);CHKERRQ(ierr);
6450298fd71SBarry Smith     (*indices)[0]  = NULL;
64624cf384cSBarry Smith     PetscFunctionReturn(0);
64724cf384cSBarry Smith   }
64824cf384cSBarry Smith 
6490298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-islocaltoglobalmappinggetinfo_debug",&debug,NULL);CHKERRQ(ierr);
65007b52d57SBarry Smith 
6513677ff5aSBarry Smith   /*
6523677ff5aSBarry Smith     Notes on ISLocalToGlobalMappingGetInfo
6533677ff5aSBarry Smith 
6543677ff5aSBarry Smith     globally owned node - the nodes that have been assigned to this processor in global
6553677ff5aSBarry Smith            numbering, just for this routine.
6563677ff5aSBarry Smith 
6573677ff5aSBarry Smith     nontrivial globally owned node - node assigned to this processor that is on a subdomain
6583677ff5aSBarry Smith            boundary (i.e. is has more than one local owner)
6593677ff5aSBarry Smith 
6603677ff5aSBarry Smith     locally owned node - node that exists on this processors subdomain
6613677ff5aSBarry Smith 
6623677ff5aSBarry Smith     nontrivial locally owned node - node that is not in the interior (i.e. has more than one
6633677ff5aSBarry Smith            local subdomain
6643677ff5aSBarry Smith   */
66524cf384cSBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mapping,&tag1);CHKERRQ(ierr);
66624cf384cSBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mapping,&tag2);CHKERRQ(ierr);
66724cf384cSBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mapping,&tag3);CHKERRQ(ierr);
66889d82c54SBarry Smith 
66989d82c54SBarry Smith   for (i=0; i<n; i++) {
67089d82c54SBarry Smith     if (lindices[i] > max) max = lindices[i];
67189d82c54SBarry Smith   }
67232dcc486SBarry Smith   ierr   = MPI_Allreduce(&max,&Ng,1,MPIU_INT,MPI_MAX,comm);CHKERRQ(ierr);
67378058e43SBarry Smith   Ng++;
67489d82c54SBarry Smith   ierr   = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
67589d82c54SBarry Smith   ierr   = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
676bc8ff85bSBarry Smith   scale  = Ng/size + 1;
677a2e34c3dSBarry Smith   ng     = scale; if (rank == size-1) ng = Ng - scale*(size-1); ng = PetscMax(1,ng);
678caba0dd0SBarry Smith   rstart = scale*rank;
67989d82c54SBarry Smith 
68089d82c54SBarry Smith   /* determine ownership ranges of global indices */
681785e854fSJed Brown   ierr = PetscMalloc1(2*size,&nprocs);CHKERRQ(ierr);
68232dcc486SBarry Smith   ierr = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr);
68389d82c54SBarry Smith 
68489d82c54SBarry Smith   /* determine owners of each local node  */
685785e854fSJed Brown   ierr = PetscMalloc1(n,&owner);CHKERRQ(ierr);
68689d82c54SBarry Smith   for (i=0; i<n; i++) {
6873677ff5aSBarry Smith     proc             = lindices[i]/scale; /* processor that globally owns this index */
68827c402fcSBarry Smith     nprocs[2*proc+1] = 1;                 /* processor globally owns at least one of ours */
6893677ff5aSBarry Smith     owner[i]         = proc;
69027c402fcSBarry Smith     nprocs[2*proc]++;                     /* count of how many that processor globally owns of ours */
69189d82c54SBarry Smith   }
69227c402fcSBarry Smith   nsends = 0; for (i=0; i<size; i++) nsends += nprocs[2*i+1];
6937904a332SBarry Smith   ierr = PetscInfo1(mapping,"Number of global owners for my local data %D\n",nsends);CHKERRQ(ierr);
69489d82c54SBarry Smith 
69589d82c54SBarry Smith   /* inform other processors of number of messages and max length*/
69627c402fcSBarry Smith   ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr);
6977904a332SBarry Smith   ierr = PetscInfo1(mapping,"Number of local owners for my global data %D\n",nrecvs);CHKERRQ(ierr);
69889d82c54SBarry Smith 
69989d82c54SBarry Smith   /* post receives for owned rows */
700785e854fSJed Brown   ierr = PetscMalloc1((2*nrecvs+1)*(nmax+1),&recvs);CHKERRQ(ierr);
701785e854fSJed Brown   ierr = PetscMalloc1((nrecvs+1),&recv_waits);CHKERRQ(ierr);
70289d82c54SBarry Smith   for (i=0; i<nrecvs; i++) {
70332dcc486SBarry Smith     ierr = MPI_Irecv(recvs+2*nmax*i,2*nmax,MPIU_INT,MPI_ANY_SOURCE,tag1,comm,recv_waits+i);CHKERRQ(ierr);
70489d82c54SBarry Smith   }
70589d82c54SBarry Smith 
70689d82c54SBarry Smith   /* pack messages containing lists of local nodes to owners */
707785e854fSJed Brown   ierr      = PetscMalloc1((2*n+1),&sends);CHKERRQ(ierr);
708785e854fSJed Brown   ierr      = PetscMalloc1((size+1),&starts);CHKERRQ(ierr);
70989d82c54SBarry Smith   starts[0] = 0;
710f6e5521dSKarl Rupp   for (i=1; i<size; i++) starts[i] = starts[i-1] + 2*nprocs[2*i-2];
71189d82c54SBarry Smith   for (i=0; i<n; i++) {
71289d82c54SBarry Smith     sends[starts[owner[i]]++] = lindices[i];
71330dcb7c9SBarry Smith     sends[starts[owner[i]]++] = i;
71489d82c54SBarry Smith   }
71589d82c54SBarry Smith   ierr = PetscFree(owner);CHKERRQ(ierr);
71689d82c54SBarry Smith   starts[0] = 0;
717f6e5521dSKarl Rupp   for (i=1; i<size; i++) starts[i] = starts[i-1] + 2*nprocs[2*i-2];
71889d82c54SBarry Smith 
71989d82c54SBarry Smith   /* send the messages */
720785e854fSJed Brown   ierr = PetscMalloc1((nsends+1),&send_waits);CHKERRQ(ierr);
721785e854fSJed Brown   ierr = PetscMalloc1((nsends+1),&dest);CHKERRQ(ierr);
72289d82c54SBarry Smith   cnt = 0;
72389d82c54SBarry Smith   for (i=0; i<size; i++) {
72427c402fcSBarry Smith     if (nprocs[2*i]) {
72532dcc486SBarry Smith       ierr      = MPI_Isend(sends+starts[i],2*nprocs[2*i],MPIU_INT,i,tag1,comm,send_waits+cnt);CHKERRQ(ierr);
72630dcb7c9SBarry Smith       dest[cnt] = i;
72789d82c54SBarry Smith       cnt++;
72889d82c54SBarry Smith     }
72989d82c54SBarry Smith   }
73089d82c54SBarry Smith   ierr = PetscFree(starts);CHKERRQ(ierr);
73189d82c54SBarry Smith 
73289d82c54SBarry Smith   /* wait on receives */
733785e854fSJed Brown   ierr = PetscMalloc1((nrecvs+1),&source);CHKERRQ(ierr);
734785e854fSJed Brown   ierr = PetscMalloc1((nrecvs+1),&len);CHKERRQ(ierr);
73589d82c54SBarry Smith   cnt  = nrecvs;
736785e854fSJed Brown   ierr = PetscMalloc1((ng+1),&nownedsenders);CHKERRQ(ierr);
73732dcc486SBarry Smith   ierr = PetscMemzero(nownedsenders,ng*sizeof(PetscInt));CHKERRQ(ierr);
73889d82c54SBarry Smith   while (cnt) {
73989d82c54SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
74089d82c54SBarry Smith     /* unpack receives into our local space */
74132dcc486SBarry Smith     ierr          = MPI_Get_count(&recv_status,MPIU_INT,&len[imdex]);CHKERRQ(ierr);
74289d82c54SBarry Smith     source[imdex] = recv_status.MPI_SOURCE;
74330dcb7c9SBarry Smith     len[imdex]    = len[imdex]/2;
744caba0dd0SBarry Smith     /* count how many local owners for each of my global owned indices */
74530dcb7c9SBarry Smith     for (i=0; i<len[imdex]; i++) nownedsenders[recvs[2*imdex*nmax+2*i]-rstart]++;
74689d82c54SBarry Smith     cnt--;
74789d82c54SBarry Smith   }
74889d82c54SBarry Smith   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
74989d82c54SBarry Smith 
75030dcb7c9SBarry Smith   /* count how many globally owned indices are on an edge multiplied by how many processors own them. */
751bc8ff85bSBarry Smith   nowned  = 0;
752bc8ff85bSBarry Smith   nownedm = 0;
753bc8ff85bSBarry Smith   for (i=0; i<ng; i++) {
754bc8ff85bSBarry Smith     if (nownedsenders[i] > 1) {nownedm += nownedsenders[i]; nowned++;}
755bc8ff85bSBarry Smith   }
756bc8ff85bSBarry Smith 
757bc8ff85bSBarry Smith   /* create single array to contain rank of all local owners of each globally owned index */
758785e854fSJed Brown   ierr      = PetscMalloc1((nownedm+1),&ownedsenders);CHKERRQ(ierr);
759785e854fSJed Brown   ierr      = PetscMalloc1((ng+1),&starts);CHKERRQ(ierr);
760bc8ff85bSBarry Smith   starts[0] = 0;
761bc8ff85bSBarry Smith   for (i=1; i<ng; i++) {
762bc8ff85bSBarry Smith     if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1];
763bc8ff85bSBarry Smith     else starts[i] = starts[i-1];
764bc8ff85bSBarry Smith   }
765bc8ff85bSBarry Smith 
76630dcb7c9SBarry Smith   /* for each nontrival globally owned node list all arriving processors */
767bc8ff85bSBarry Smith   for (i=0; i<nrecvs; i++) {
768bc8ff85bSBarry Smith     for (j=0; j<len[i]; j++) {
76930dcb7c9SBarry Smith       node = recvs[2*i*nmax+2*j]-rstart;
770f6e5521dSKarl Rupp       if (nownedsenders[node] > 1) ownedsenders[starts[node]++] = source[i];
771bc8ff85bSBarry Smith     }
772bc8ff85bSBarry Smith   }
773bc8ff85bSBarry Smith 
77407b52d57SBarry Smith   if (debug) { /* -----------------------------------  */
77530dcb7c9SBarry Smith     starts[0] = 0;
77630dcb7c9SBarry Smith     for (i=1; i<ng; i++) {
77730dcb7c9SBarry Smith       if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1];
77830dcb7c9SBarry Smith       else starts[i] = starts[i-1];
77930dcb7c9SBarry Smith     }
78030dcb7c9SBarry Smith     for (i=0; i<ng; i++) {
78130dcb7c9SBarry Smith       if (nownedsenders[i] > 1) {
7827904a332SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"[%d] global node %D local owner processors: ",rank,i+rstart);CHKERRQ(ierr);
78330dcb7c9SBarry Smith         for (j=0; j<nownedsenders[i]; j++) {
7847904a332SBarry Smith           ierr = PetscSynchronizedPrintf(comm,"%D ",ownedsenders[starts[i]+j]);CHKERRQ(ierr);
78530dcb7c9SBarry Smith         }
78630dcb7c9SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"\n");CHKERRQ(ierr);
78730dcb7c9SBarry Smith       }
78830dcb7c9SBarry Smith     }
7890ec8b6e3SBarry Smith     ierr = PetscSynchronizedFlush(comm,PETSC_STDOUT);CHKERRQ(ierr);
79007b52d57SBarry Smith   } /* -----------------------------------  */
79130dcb7c9SBarry Smith 
7923677ff5aSBarry Smith   /* wait on original sends */
7933a96401aSBarry Smith   if (nsends) {
794785e854fSJed Brown     ierr = PetscMalloc1(nsends,&send_status);CHKERRQ(ierr);
7953a96401aSBarry Smith     ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
7963a96401aSBarry Smith     ierr = PetscFree(send_status);CHKERRQ(ierr);
7973a96401aSBarry Smith   }
79889d82c54SBarry Smith   ierr = PetscFree(send_waits);CHKERRQ(ierr);
7993a96401aSBarry Smith   ierr = PetscFree(sends);CHKERRQ(ierr);
8003677ff5aSBarry Smith   ierr = PetscFree(nprocs);CHKERRQ(ierr);
8013677ff5aSBarry Smith 
8023677ff5aSBarry Smith   /* pack messages to send back to local owners */
80330dcb7c9SBarry Smith   starts[0] = 0;
80430dcb7c9SBarry Smith   for (i=1; i<ng; i++) {
80530dcb7c9SBarry Smith     if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1];
80630dcb7c9SBarry Smith     else starts[i] = starts[i-1];
80730dcb7c9SBarry Smith   }
80830dcb7c9SBarry Smith   nsends2 = nrecvs;
809785e854fSJed Brown   ierr    = PetscMalloc1((nsends2+1),&nprocs);CHKERRQ(ierr); /* length of each message */
81030dcb7c9SBarry Smith   for (i=0; i<nrecvs; i++) {
81130dcb7c9SBarry Smith     nprocs[i] = 1;
81230dcb7c9SBarry Smith     for (j=0; j<len[i]; j++) {
81330dcb7c9SBarry Smith       node = recvs[2*i*nmax+2*j]-rstart;
814f6e5521dSKarl Rupp       if (nownedsenders[node] > 1) nprocs[i] += 2 + nownedsenders[node];
81530dcb7c9SBarry Smith     }
81630dcb7c9SBarry Smith   }
817f6e5521dSKarl Rupp   nt = 0;
818f6e5521dSKarl Rupp   for (i=0; i<nsends2; i++) nt += nprocs[i];
819f6e5521dSKarl Rupp 
820785e854fSJed Brown   ierr = PetscMalloc1((nt+1),&sends2);CHKERRQ(ierr);
821785e854fSJed Brown   ierr = PetscMalloc1((nsends2+1),&starts2);CHKERRQ(ierr);
822f6e5521dSKarl Rupp 
823f6e5521dSKarl Rupp   starts2[0] = 0;
824f6e5521dSKarl Rupp   for (i=1; i<nsends2; i++) starts2[i] = starts2[i-1] + nprocs[i-1];
82530dcb7c9SBarry Smith   /*
82630dcb7c9SBarry Smith      Each message is 1 + nprocs[i] long, and consists of
82730dcb7c9SBarry Smith        (0) the number of nodes being sent back
82830dcb7c9SBarry Smith        (1) the local node number,
82930dcb7c9SBarry Smith        (2) the number of processors sharing it,
83030dcb7c9SBarry Smith        (3) the processors sharing it
83130dcb7c9SBarry Smith   */
83230dcb7c9SBarry Smith   for (i=0; i<nsends2; i++) {
83330dcb7c9SBarry Smith     cnt = 1;
83430dcb7c9SBarry Smith     sends2[starts2[i]] = 0;
83530dcb7c9SBarry Smith     for (j=0; j<len[i]; j++) {
83630dcb7c9SBarry Smith       node = recvs[2*i*nmax+2*j]-rstart;
83730dcb7c9SBarry Smith       if (nownedsenders[node] > 1) {
83830dcb7c9SBarry Smith         sends2[starts2[i]]++;
83930dcb7c9SBarry Smith         sends2[starts2[i]+cnt++] = recvs[2*i*nmax+2*j+1];
84030dcb7c9SBarry Smith         sends2[starts2[i]+cnt++] = nownedsenders[node];
84132dcc486SBarry Smith         ierr = PetscMemcpy(&sends2[starts2[i]+cnt],&ownedsenders[starts[node]],nownedsenders[node]*sizeof(PetscInt));CHKERRQ(ierr);
84230dcb7c9SBarry Smith         cnt += nownedsenders[node];
84330dcb7c9SBarry Smith       }
84430dcb7c9SBarry Smith     }
84530dcb7c9SBarry Smith   }
84630dcb7c9SBarry Smith 
84730dcb7c9SBarry Smith   /* receive the message lengths */
84830dcb7c9SBarry Smith   nrecvs2 = nsends;
849785e854fSJed Brown   ierr    = PetscMalloc1((nrecvs2+1),&lens2);CHKERRQ(ierr);
850785e854fSJed Brown   ierr    = PetscMalloc1((nrecvs2+1),&starts3);CHKERRQ(ierr);
851785e854fSJed Brown   ierr    = PetscMalloc1((nrecvs2+1),&recv_waits);CHKERRQ(ierr);
85230dcb7c9SBarry Smith   for (i=0; i<nrecvs2; i++) {
853d44834fbSBarry Smith     ierr = MPI_Irecv(&lens2[i],1,MPIU_INT,dest[i],tag2,comm,recv_waits+i);CHKERRQ(ierr);
85430dcb7c9SBarry Smith   }
855d44834fbSBarry Smith 
8568a8e0b3aSBarry Smith   /* send the message lengths */
8578a8e0b3aSBarry Smith   for (i=0; i<nsends2; i++) {
8588a8e0b3aSBarry Smith     ierr = MPI_Send(&nprocs[i],1,MPIU_INT,source[i],tag2,comm);CHKERRQ(ierr);
8598a8e0b3aSBarry Smith   }
8608a8e0b3aSBarry Smith 
861d44834fbSBarry Smith   /* wait on receives of lens */
8620c468ba9SBarry Smith   if (nrecvs2) {
863785e854fSJed Brown     ierr = PetscMalloc1(nrecvs2,&recv_statuses);CHKERRQ(ierr);
864d44834fbSBarry Smith     ierr = MPI_Waitall(nrecvs2,recv_waits,recv_statuses);CHKERRQ(ierr);
865d44834fbSBarry Smith     ierr = PetscFree(recv_statuses);CHKERRQ(ierr);
8660c468ba9SBarry Smith   }
867a2ea699eSBarry Smith   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
868d44834fbSBarry Smith 
86930dcb7c9SBarry Smith   starts3[0] = 0;
870d44834fbSBarry Smith   nt         = 0;
87130dcb7c9SBarry Smith   for (i=0; i<nrecvs2-1; i++) {
87230dcb7c9SBarry Smith     starts3[i+1] = starts3[i] + lens2[i];
873d44834fbSBarry Smith     nt          += lens2[i];
87430dcb7c9SBarry Smith   }
87576466f69SStefano Zampini   if (nrecvs2) nt += lens2[nrecvs2-1];
876d44834fbSBarry Smith 
877785e854fSJed Brown   ierr = PetscMalloc1((nt+1),&recvs2);CHKERRQ(ierr);
878785e854fSJed Brown   ierr = PetscMalloc1((nrecvs2+1),&recv_waits);CHKERRQ(ierr);
87952b72c4aSBarry Smith   for (i=0; i<nrecvs2; i++) {
88032dcc486SBarry Smith     ierr = MPI_Irecv(recvs2+starts3[i],lens2[i],MPIU_INT,dest[i],tag3,comm,recv_waits+i);CHKERRQ(ierr);
88130dcb7c9SBarry Smith   }
88230dcb7c9SBarry Smith 
88330dcb7c9SBarry Smith   /* send the messages */
884785e854fSJed Brown   ierr = PetscMalloc1((nsends2+1),&send_waits);CHKERRQ(ierr);
88530dcb7c9SBarry Smith   for (i=0; i<nsends2; i++) {
88632dcc486SBarry Smith     ierr = MPI_Isend(sends2+starts2[i],nprocs[i],MPIU_INT,source[i],tag3,comm,send_waits+i);CHKERRQ(ierr);
88730dcb7c9SBarry Smith   }
88830dcb7c9SBarry Smith 
88930dcb7c9SBarry Smith   /* wait on receives */
8900c468ba9SBarry Smith   if (nrecvs2) {
891785e854fSJed Brown     ierr = PetscMalloc1(nrecvs2,&recv_statuses);CHKERRQ(ierr);
89230dcb7c9SBarry Smith     ierr = MPI_Waitall(nrecvs2,recv_waits,recv_statuses);CHKERRQ(ierr);
89330dcb7c9SBarry Smith     ierr = PetscFree(recv_statuses);CHKERRQ(ierr);
8940c468ba9SBarry Smith   }
89530dcb7c9SBarry Smith   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
89630dcb7c9SBarry Smith   ierr = PetscFree(nprocs);CHKERRQ(ierr);
89730dcb7c9SBarry Smith 
89807b52d57SBarry Smith   if (debug) { /* -----------------------------------  */
89930dcb7c9SBarry Smith     cnt = 0;
90030dcb7c9SBarry Smith     for (i=0; i<nrecvs2; i++) {
90130dcb7c9SBarry Smith       nt = recvs2[cnt++];
90230dcb7c9SBarry Smith       for (j=0; j<nt; j++) {
9037904a332SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"[%d] local node %D number of subdomains %D: ",rank,recvs2[cnt],recvs2[cnt+1]);CHKERRQ(ierr);
90430dcb7c9SBarry Smith         for (k=0; k<recvs2[cnt+1]; k++) {
9057904a332SBarry Smith           ierr = PetscSynchronizedPrintf(comm,"%D ",recvs2[cnt+2+k]);CHKERRQ(ierr);
90630dcb7c9SBarry Smith         }
90730dcb7c9SBarry Smith         cnt += 2 + recvs2[cnt+1];
90830dcb7c9SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"\n");CHKERRQ(ierr);
90930dcb7c9SBarry Smith       }
91030dcb7c9SBarry Smith     }
9110ec8b6e3SBarry Smith     ierr = PetscSynchronizedFlush(comm,PETSC_STDOUT);CHKERRQ(ierr);
91207b52d57SBarry Smith   } /* -----------------------------------  */
91330dcb7c9SBarry Smith 
91430dcb7c9SBarry Smith   /* count number subdomains for each local node */
915785e854fSJed Brown   ierr = PetscMalloc1(size,&nprocs);CHKERRQ(ierr);
91632dcc486SBarry Smith   ierr = PetscMemzero(nprocs,size*sizeof(PetscInt));CHKERRQ(ierr);
91730dcb7c9SBarry Smith   cnt  = 0;
91830dcb7c9SBarry Smith   for (i=0; i<nrecvs2; i++) {
91930dcb7c9SBarry Smith     nt = recvs2[cnt++];
92030dcb7c9SBarry Smith     for (j=0; j<nt; j++) {
921f6e5521dSKarl Rupp       for (k=0; k<recvs2[cnt+1]; k++) nprocs[recvs2[cnt+2+k]]++;
92230dcb7c9SBarry Smith       cnt += 2 + recvs2[cnt+1];
92330dcb7c9SBarry Smith     }
92430dcb7c9SBarry Smith   }
92530dcb7c9SBarry Smith   nt = 0; for (i=0; i<size; i++) nt += (nprocs[i] > 0);
92630dcb7c9SBarry Smith   *nproc    = nt;
927785e854fSJed Brown   ierr = PetscMalloc1((nt+1),procs);CHKERRQ(ierr);
928785e854fSJed Brown   ierr = PetscMalloc1((nt+1),numprocs);CHKERRQ(ierr);
929785e854fSJed Brown   ierr = PetscMalloc1((nt+1),indices);CHKERRQ(ierr);
9300298fd71SBarry Smith   for (i=0;i<nt+1;i++) (*indices)[i]=NULL;
931785e854fSJed Brown   ierr = PetscMalloc1(size,&bprocs);CHKERRQ(ierr);
93230dcb7c9SBarry Smith   cnt       = 0;
93330dcb7c9SBarry Smith   for (i=0; i<size; i++) {
93430dcb7c9SBarry Smith     if (nprocs[i] > 0) {
93530dcb7c9SBarry Smith       bprocs[i]        = cnt;
93630dcb7c9SBarry Smith       (*procs)[cnt]    = i;
93730dcb7c9SBarry Smith       (*numprocs)[cnt] = nprocs[i];
938785e854fSJed Brown       ierr             = PetscMalloc1(nprocs[i],&(*indices)[cnt]);CHKERRQ(ierr);
93930dcb7c9SBarry Smith       cnt++;
94030dcb7c9SBarry Smith     }
94130dcb7c9SBarry Smith   }
94230dcb7c9SBarry Smith 
94330dcb7c9SBarry Smith   /* make the list of subdomains for each nontrivial local node */
94432dcc486SBarry Smith   ierr = PetscMemzero(*numprocs,nt*sizeof(PetscInt));CHKERRQ(ierr);
94530dcb7c9SBarry Smith   cnt  = 0;
94630dcb7c9SBarry Smith   for (i=0; i<nrecvs2; i++) {
94730dcb7c9SBarry Smith     nt = recvs2[cnt++];
94830dcb7c9SBarry Smith     for (j=0; j<nt; j++) {
949f6e5521dSKarl Rupp       for (k=0; k<recvs2[cnt+1]; k++) (*indices)[bprocs[recvs2[cnt+2+k]]][(*numprocs)[bprocs[recvs2[cnt+2+k]]]++] = recvs2[cnt];
95030dcb7c9SBarry Smith       cnt += 2 + recvs2[cnt+1];
95130dcb7c9SBarry Smith     }
95230dcb7c9SBarry Smith   }
95330dcb7c9SBarry Smith   ierr = PetscFree(bprocs);CHKERRQ(ierr);
95407b52d57SBarry Smith   ierr = PetscFree(recvs2);CHKERRQ(ierr);
95530dcb7c9SBarry Smith 
95607b52d57SBarry Smith   /* sort the node indexing by their global numbers */
95707b52d57SBarry Smith   nt = *nproc;
95807b52d57SBarry Smith   for (i=0; i<nt; i++) {
959785e854fSJed Brown     ierr = PetscMalloc1(((*numprocs)[i]),&tmp);CHKERRQ(ierr);
960f6e5521dSKarl Rupp     for (j=0; j<(*numprocs)[i]; j++) tmp[j] = lindices[(*indices)[i][j]];
96107b52d57SBarry Smith     ierr = PetscSortIntWithArray((*numprocs)[i],tmp,(*indices)[i]);CHKERRQ(ierr);
96207b52d57SBarry Smith     ierr = PetscFree(tmp);CHKERRQ(ierr);
96307b52d57SBarry Smith   }
96407b52d57SBarry Smith 
96507b52d57SBarry Smith   if (debug) { /* -----------------------------------  */
96630dcb7c9SBarry Smith     nt = *nproc;
96730dcb7c9SBarry Smith     for (i=0; i<nt; i++) {
9687904a332SBarry Smith       ierr = PetscSynchronizedPrintf(comm,"[%d] subdomain %D number of indices %D: ",rank,(*procs)[i],(*numprocs)[i]);CHKERRQ(ierr);
96930dcb7c9SBarry Smith       for (j=0; j<(*numprocs)[i]; j++) {
9707904a332SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"%D ",(*indices)[i][j]);CHKERRQ(ierr);
97130dcb7c9SBarry Smith       }
97230dcb7c9SBarry Smith       ierr = PetscSynchronizedPrintf(comm,"\n");CHKERRQ(ierr);
97330dcb7c9SBarry Smith     }
9740ec8b6e3SBarry Smith     ierr = PetscSynchronizedFlush(comm,PETSC_STDOUT);CHKERRQ(ierr);
97507b52d57SBarry Smith   } /* -----------------------------------  */
97630dcb7c9SBarry Smith 
97730dcb7c9SBarry Smith   /* wait on sends */
97830dcb7c9SBarry Smith   if (nsends2) {
979785e854fSJed Brown     ierr = PetscMalloc1(nsends2,&send_status);CHKERRQ(ierr);
98030dcb7c9SBarry Smith     ierr = MPI_Waitall(nsends2,send_waits,send_status);CHKERRQ(ierr);
98130dcb7c9SBarry Smith     ierr = PetscFree(send_status);CHKERRQ(ierr);
98230dcb7c9SBarry Smith   }
98330dcb7c9SBarry Smith 
98430dcb7c9SBarry Smith   ierr = PetscFree(starts3);CHKERRQ(ierr);
98530dcb7c9SBarry Smith   ierr = PetscFree(dest);CHKERRQ(ierr);
98630dcb7c9SBarry Smith   ierr = PetscFree(send_waits);CHKERRQ(ierr);
9873677ff5aSBarry Smith 
988bc8ff85bSBarry Smith   ierr = PetscFree(nownedsenders);CHKERRQ(ierr);
989bc8ff85bSBarry Smith   ierr = PetscFree(ownedsenders);CHKERRQ(ierr);
990bc8ff85bSBarry Smith   ierr = PetscFree(starts);CHKERRQ(ierr);
99130dcb7c9SBarry Smith   ierr = PetscFree(starts2);CHKERRQ(ierr);
99230dcb7c9SBarry Smith   ierr = PetscFree(lens2);CHKERRQ(ierr);
99389d82c54SBarry Smith 
99489d82c54SBarry Smith   ierr = PetscFree(source);CHKERRQ(ierr);
99597f1f81fSBarry Smith   ierr = PetscFree(len);CHKERRQ(ierr);
99689d82c54SBarry Smith   ierr = PetscFree(recvs);CHKERRQ(ierr);
9973a96401aSBarry Smith   ierr = PetscFree(nprocs);CHKERRQ(ierr);
99830dcb7c9SBarry Smith   ierr = PetscFree(sends2);CHKERRQ(ierr);
99924cf384cSBarry Smith 
100024cf384cSBarry Smith   /* put the information about myself as the first entry in the list */
100124cf384cSBarry Smith   first_procs    = (*procs)[0];
100224cf384cSBarry Smith   first_numprocs = (*numprocs)[0];
100324cf384cSBarry Smith   first_indices  = (*indices)[0];
100424cf384cSBarry Smith   for (i=0; i<*nproc; i++) {
100524cf384cSBarry Smith     if ((*procs)[i] == rank) {
100624cf384cSBarry Smith       (*procs)[0]    = (*procs)[i];
100724cf384cSBarry Smith       (*numprocs)[0] = (*numprocs)[i];
100824cf384cSBarry Smith       (*indices)[0]  = (*indices)[i];
100924cf384cSBarry Smith       (*procs)[i]    = first_procs;
101024cf384cSBarry Smith       (*numprocs)[i] = first_numprocs;
101124cf384cSBarry Smith       (*indices)[i]  = first_indices;
101224cf384cSBarry Smith       break;
101324cf384cSBarry Smith     }
101424cf384cSBarry Smith   }
101589d82c54SBarry Smith   PetscFunctionReturn(0);
101689d82c54SBarry Smith }
101789d82c54SBarry Smith 
10184a2ae208SSatish Balay #undef __FUNCT__
10194a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingRestoreInfo"
102007b52d57SBarry Smith /*@C
102107b52d57SBarry Smith     ISLocalToGlobalMappingRestoreInfo - Frees the memory allocated by ISLocalToGlobalMappingGetInfo()
102289d82c54SBarry Smith 
102307b52d57SBarry Smith     Collective on ISLocalToGlobalMapping
102407b52d57SBarry Smith 
102507b52d57SBarry Smith     Input Parameters:
102607b52d57SBarry Smith .   mapping - the mapping from local to global indexing
102707b52d57SBarry Smith 
102807b52d57SBarry Smith     Output Parameter:
102907b52d57SBarry Smith +   nproc - number of processors that are connected to this one
103007b52d57SBarry Smith .   proc - neighboring processors
103107b52d57SBarry Smith .   numproc - number of indices for each processor
103207b52d57SBarry Smith -   indices - indices of local nodes shared with neighbor (sorted by global numbering)
103307b52d57SBarry Smith 
103407b52d57SBarry Smith     Level: advanced
103507b52d57SBarry Smith 
103607b52d57SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreate(),
103707b52d57SBarry Smith           ISLocalToGlobalMappingGetInfo()
103807b52d57SBarry Smith @*/
10397087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingRestoreInfo(ISLocalToGlobalMapping mapping,PetscInt *nproc,PetscInt *procs[],PetscInt *numprocs[],PetscInt **indices[])
104007b52d57SBarry Smith {
10416849ba73SBarry Smith   PetscErrorCode ierr;
104232dcc486SBarry Smith   PetscInt       i;
104307b52d57SBarry Smith 
104407b52d57SBarry Smith   PetscFunctionBegin;
104505b42c5fSBarry Smith   ierr = PetscFree(*procs);CHKERRQ(ierr);
104605b42c5fSBarry Smith   ierr = PetscFree(*numprocs);CHKERRQ(ierr);
104700ff320aSBarry Smith   if (*indices) {
104805b42c5fSBarry Smith     ierr = PetscFree((*indices)[0]);CHKERRQ(ierr);
104900ff320aSBarry Smith     for (i=1; i<*nproc; i++) {
105005b42c5fSBarry Smith       ierr = PetscFree((*indices)[i]);CHKERRQ(ierr);
105107b52d57SBarry Smith     }
105207b52d57SBarry Smith     ierr = PetscFree(*indices);CHKERRQ(ierr);
105324cf384cSBarry Smith   }
105407b52d57SBarry Smith   PetscFunctionReturn(0);
105507b52d57SBarry Smith }
105686994e45SJed Brown 
105786994e45SJed Brown #undef __FUNCT__
105886994e45SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingGetIndices"
105986994e45SJed Brown /*@C
106086994e45SJed Brown    ISLocalToGlobalMappingGetIndices - Get global indices for every local point
106186994e45SJed Brown 
106286994e45SJed Brown    Not Collective
106386994e45SJed Brown 
106486994e45SJed Brown    Input Arguments:
106586994e45SJed Brown . ltog - local to global mapping
106686994e45SJed Brown 
106786994e45SJed Brown    Output Arguments:
106886994e45SJed Brown . array - array of indices
106986994e45SJed Brown 
107086994e45SJed Brown    Level: advanced
107186994e45SJed Brown 
107286994e45SJed Brown .seealso: ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingRestoreIndices()
107386994e45SJed Brown @*/
10747087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingGetIndices(ISLocalToGlobalMapping ltog,const PetscInt **array)
107586994e45SJed Brown {
107686994e45SJed Brown   PetscFunctionBegin;
107786994e45SJed Brown   PetscValidHeaderSpecific(ltog,IS_LTOGM_CLASSID,1);
107886994e45SJed Brown   PetscValidPointer(array,2);
1079*45b6f7e9SBarry Smith   if (ltog->bs == 1) {
108086994e45SJed Brown     *array = ltog->indices;
1081*45b6f7e9SBarry Smith   } else {
1082*45b6f7e9SBarry Smith     PetscInt       *jj,k,i,j,n = ltog->n, bs = ltog->bs;
1083*45b6f7e9SBarry Smith     const PetscInt *ii;
1084*45b6f7e9SBarry Smith     PetscErrorCode ierr;
1085*45b6f7e9SBarry Smith 
1086*45b6f7e9SBarry Smith     ierr = PetscMalloc1(bs*n,&jj);CHKERRQ(ierr);
1087*45b6f7e9SBarry Smith     *array = jj;
1088*45b6f7e9SBarry Smith     k    = 0;
1089*45b6f7e9SBarry Smith     ii   = ltog->indices;
1090*45b6f7e9SBarry Smith     for (i=0; i<n; i++)
1091*45b6f7e9SBarry Smith       for (j=0; j<bs; j++)
1092*45b6f7e9SBarry Smith         jj[k++] = bs*ii[i] + j;
1093*45b6f7e9SBarry Smith   }
109486994e45SJed Brown   PetscFunctionReturn(0);
109586994e45SJed Brown }
109686994e45SJed Brown 
109786994e45SJed Brown #undef __FUNCT__
109886994e45SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingRestoreIndices"
109986994e45SJed Brown /*@C
110086994e45SJed Brown    ISLocalToGlobalMappingRestoreIndices - Restore indices obtained with ISLocalToGlobalMappingRestoreIndices()
110186994e45SJed Brown 
110286994e45SJed Brown    Not Collective
110386994e45SJed Brown 
110486994e45SJed Brown    Input Arguments:
110586994e45SJed Brown + ltog - local to global mapping
110686994e45SJed Brown - array - array of indices
110786994e45SJed Brown 
110886994e45SJed Brown    Level: advanced
110986994e45SJed Brown 
111086994e45SJed Brown .seealso: ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingGetIndices()
111186994e45SJed Brown @*/
11127087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingRestoreIndices(ISLocalToGlobalMapping ltog,const PetscInt **array)
111386994e45SJed Brown {
111486994e45SJed Brown   PetscFunctionBegin;
111586994e45SJed Brown   PetscValidHeaderSpecific(ltog,IS_LTOGM_CLASSID,1);
111686994e45SJed Brown   PetscValidPointer(array,2);
1117*45b6f7e9SBarry Smith   if (ltog->bs == 1 && *array != ltog->indices) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_BADPTR,"Trying to return mismatched pointer");
1118*45b6f7e9SBarry Smith 
1119*45b6f7e9SBarry Smith   if (ltog->bs > 1) {
1120*45b6f7e9SBarry Smith     PetscErrorCode ierr;
1121*45b6f7e9SBarry Smith     ierr = PetscFree(*(void**)array);CHKERRQ(ierr);
1122*45b6f7e9SBarry Smith   }
1123*45b6f7e9SBarry Smith   PetscFunctionReturn(0);
1124*45b6f7e9SBarry Smith }
1125*45b6f7e9SBarry Smith 
1126*45b6f7e9SBarry Smith #undef __FUNCT__
1127*45b6f7e9SBarry Smith #define __FUNCT__ "ISLocalToGlobalMappingGetBlockIndices"
1128*45b6f7e9SBarry Smith /*@C
1129*45b6f7e9SBarry Smith    ISLocalToGlobalMappingGetBlockIndices - Get global indices for every local block
1130*45b6f7e9SBarry Smith 
1131*45b6f7e9SBarry Smith    Not Collective
1132*45b6f7e9SBarry Smith 
1133*45b6f7e9SBarry Smith    Input Arguments:
1134*45b6f7e9SBarry Smith . ltog - local to global mapping
1135*45b6f7e9SBarry Smith 
1136*45b6f7e9SBarry Smith    Output Arguments:
1137*45b6f7e9SBarry Smith . array - array of indices
1138*45b6f7e9SBarry Smith 
1139*45b6f7e9SBarry Smith    Level: advanced
1140*45b6f7e9SBarry Smith 
1141*45b6f7e9SBarry Smith .seealso: ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingRestoreBlockIndices()
1142*45b6f7e9SBarry Smith @*/
1143*45b6f7e9SBarry Smith PetscErrorCode  ISLocalToGlobalMappingGetBlockIndices(ISLocalToGlobalMapping ltog,const PetscInt **array)
1144*45b6f7e9SBarry Smith {
1145*45b6f7e9SBarry Smith   PetscFunctionBegin;
1146*45b6f7e9SBarry Smith   PetscValidHeaderSpecific(ltog,IS_LTOGM_CLASSID,1);
1147*45b6f7e9SBarry Smith   PetscValidPointer(array,2);
1148*45b6f7e9SBarry Smith   *array = ltog->indices;
1149*45b6f7e9SBarry Smith   PetscFunctionReturn(0);
1150*45b6f7e9SBarry Smith }
1151*45b6f7e9SBarry Smith 
1152*45b6f7e9SBarry Smith #undef __FUNCT__
1153*45b6f7e9SBarry Smith #define __FUNCT__ "ISLocalToGlobalMappingRestoreBlockIndices"
1154*45b6f7e9SBarry Smith /*@C
1155*45b6f7e9SBarry Smith    ISLocalToGlobalMappingRestoreBlockIndices - Restore indices obtained with ISLocalToGlobalMappingGetBlockIndices()
1156*45b6f7e9SBarry Smith 
1157*45b6f7e9SBarry Smith    Not Collective
1158*45b6f7e9SBarry Smith 
1159*45b6f7e9SBarry Smith    Input Arguments:
1160*45b6f7e9SBarry Smith + ltog - local to global mapping
1161*45b6f7e9SBarry Smith - array - array of indices
1162*45b6f7e9SBarry Smith 
1163*45b6f7e9SBarry Smith    Level: advanced
1164*45b6f7e9SBarry Smith 
1165*45b6f7e9SBarry Smith .seealso: ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingGetIndices()
1166*45b6f7e9SBarry Smith @*/
1167*45b6f7e9SBarry Smith PetscErrorCode  ISLocalToGlobalMappingRestoreBlockIndices(ISLocalToGlobalMapping ltog,const PetscInt **array)
1168*45b6f7e9SBarry Smith {
1169*45b6f7e9SBarry Smith   PetscFunctionBegin;
1170*45b6f7e9SBarry Smith   PetscValidHeaderSpecific(ltog,IS_LTOGM_CLASSID,1);
1171*45b6f7e9SBarry Smith   PetscValidPointer(array,2);
117286994e45SJed Brown   if (*array != ltog->indices) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_BADPTR,"Trying to return mismatched pointer");
11730298fd71SBarry Smith   *array = NULL;
117486994e45SJed Brown   PetscFunctionReturn(0);
117586994e45SJed Brown }
1176f7efa3c7SJed Brown 
1177f7efa3c7SJed Brown #undef __FUNCT__
1178f7efa3c7SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingConcatenate"
1179f7efa3c7SJed Brown /*@C
1180f7efa3c7SJed Brown    ISLocalToGlobalMappingConcatenate - Create a new mapping that concatenates a list of mappings
1181f7efa3c7SJed Brown 
1182f7efa3c7SJed Brown    Not Collective
1183f7efa3c7SJed Brown 
1184f7efa3c7SJed Brown    Input Arguments:
1185f7efa3c7SJed Brown + comm - communicator for the new mapping, must contain the communicator of every mapping to concatenate
1186f7efa3c7SJed Brown . n - number of mappings to concatenate
1187f7efa3c7SJed Brown - ltogs - local to global mappings
1188f7efa3c7SJed Brown 
1189f7efa3c7SJed Brown    Output Arguments:
1190f7efa3c7SJed Brown . ltogcat - new mapping
1191f7efa3c7SJed Brown 
1192f7efa3c7SJed Brown    Level: advanced
1193f7efa3c7SJed Brown 
1194f7efa3c7SJed Brown .seealso: ISLocalToGlobalMappingCreate()
1195f7efa3c7SJed Brown @*/
1196f7efa3c7SJed Brown PetscErrorCode ISLocalToGlobalMappingConcatenate(MPI_Comm comm,PetscInt n,const ISLocalToGlobalMapping ltogs[],ISLocalToGlobalMapping *ltogcat)
1197f7efa3c7SJed Brown {
1198f7efa3c7SJed Brown   PetscInt       i,cnt,m,*idx;
1199f7efa3c7SJed Brown   PetscErrorCode ierr;
1200f7efa3c7SJed Brown 
1201f7efa3c7SJed Brown   PetscFunctionBegin;
1202f7efa3c7SJed Brown   if (n < 0) SETERRQ1(comm,PETSC_ERR_ARG_OUTOFRANGE,"Must have a non-negative number of mappings, given %D",n);
1203f7efa3c7SJed Brown   if (n > 0) PetscValidPointer(ltogs,3);
1204f7efa3c7SJed Brown   for (i=0; i<n; i++) PetscValidHeaderSpecific(ltogs[i],IS_LTOGM_CLASSID,3);
1205f7efa3c7SJed Brown   PetscValidPointer(ltogcat,4);
1206f7efa3c7SJed Brown   for (cnt=0,i=0; i<n; i++) {
1207f7efa3c7SJed Brown     ierr = ISLocalToGlobalMappingGetSize(ltogs[i],&m);CHKERRQ(ierr);
1208f7efa3c7SJed Brown     cnt += m;
1209f7efa3c7SJed Brown   }
1210785e854fSJed Brown   ierr = PetscMalloc1(cnt,&idx);CHKERRQ(ierr);
1211f7efa3c7SJed Brown   for (cnt=0,i=0; i<n; i++) {
1212f7efa3c7SJed Brown     const PetscInt *subidx;
1213f7efa3c7SJed Brown     ierr = ISLocalToGlobalMappingGetSize(ltogs[i],&m);CHKERRQ(ierr);
1214f7efa3c7SJed Brown     ierr = ISLocalToGlobalMappingGetIndices(ltogs[i],&subidx);CHKERRQ(ierr);
1215f7efa3c7SJed Brown     ierr = PetscMemcpy(&idx[cnt],subidx,m*sizeof(PetscInt));CHKERRQ(ierr);
1216f7efa3c7SJed Brown     ierr = ISLocalToGlobalMappingRestoreIndices(ltogs[i],&subidx);CHKERRQ(ierr);
1217f7efa3c7SJed Brown     cnt += m;
1218f7efa3c7SJed Brown   }
1219f0413b6fSBarry Smith   ierr = ISLocalToGlobalMappingCreate(comm,1,cnt,idx,PETSC_OWN_POINTER,ltogcat);CHKERRQ(ierr);
1220f7efa3c7SJed Brown   PetscFunctionReturn(0);
1221f7efa3c7SJed Brown }
122204a59952SBarry Smith 
122304a59952SBarry Smith 
1224