xref: /petsc/src/vec/is/utils/isltog.c (revision 6a8182857e2a1ca168ce28a153335aa68f3e4e3d)
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"
34565245c5SBarry Smith /*@
35107e9a97SBarry 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:
43107e9a97SBarry Smith .   n - the number of entries in the local mapping, ISLocalToGlobalMappingGetIndices() returns an array of this length
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);
56107e9a97SBarry Smith   *n = mapping->bs*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__
20445b6f7e9SBarry Smith #define __FUNCT__ "ISLocalToGlobalMappingGetBlockSize"
20545b6f7e9SBarry Smith /*@
20645b6f7e9SBarry Smith     ISLocalToGlobalMappingGetBlockSize - Gets the blocksize of the mapping
20745b6f7e9SBarry Smith     ordering and a global parallel ordering.
20845b6f7e9SBarry Smith 
20945b6f7e9SBarry Smith     Not Collective
21045b6f7e9SBarry Smith 
21145b6f7e9SBarry Smith     Input Parameters:
21245b6f7e9SBarry Smith .   mapping - mapping data structure
21345b6f7e9SBarry Smith 
21445b6f7e9SBarry Smith     Output Parameter:
21545b6f7e9SBarry Smith .   bs - the blocksize
21645b6f7e9SBarry Smith 
21745b6f7e9SBarry Smith     Level: advanced
21845b6f7e9SBarry Smith 
21945b6f7e9SBarry Smith     Concepts: mapping^local to global
22045b6f7e9SBarry Smith 
22145b6f7e9SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS()
22245b6f7e9SBarry Smith @*/
22345b6f7e9SBarry Smith PetscErrorCode  ISLocalToGlobalMappingGetBlockSize(ISLocalToGlobalMapping mapping,PetscInt *bs)
22445b6f7e9SBarry Smith {
22545b6f7e9SBarry Smith   PetscFunctionBegin;
22645b6f7e9SBarry Smith   *bs = mapping->bs;
22745b6f7e9SBarry Smith   PetscFunctionReturn(0);
22845b6f7e9SBarry Smith }
22945b6f7e9SBarry Smith 
23045b6f7e9SBarry 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);
279d5ad8652SBarry Smith     (*mapping)->indices = in;
2806389a1a1SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)*mapping,n*sizeof(PetscInt));CHKERRQ(ierr);
2816389a1a1SBarry Smith   } else if (mode == PETSC_OWN_POINTER) {
2826389a1a1SBarry Smith     (*mapping)->indices = (PetscInt*)indices;
2836389a1a1SBarry Smith     ierr = PetscLogObjectMemory((PetscObject)*mapping,n*sizeof(PetscInt));CHKERRQ(ierr);
2846389a1a1SBarry Smith   }
285f6e5521dSKarl Rupp   else SETERRQ(cm,PETSC_ERR_SUP,"Cannot currently use PETSC_USE_POINTER");
2863a40ed3dSBarry Smith   PetscFunctionReturn(0);
2872362add9SBarry Smith }
2882362add9SBarry Smith 
2894a2ae208SSatish Balay #undef __FUNCT__
2904a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingDestroy"
29190f02eecSBarry Smith /*@
29290f02eecSBarry Smith    ISLocalToGlobalMappingDestroy - Destroys a mapping between a local (0 to n)
29390f02eecSBarry Smith    ordering and a global parallel ordering.
29490f02eecSBarry Smith 
2950f5bd95cSBarry Smith    Note Collective
296b9cd556bSLois Curfman McInnes 
29790f02eecSBarry Smith    Input Parameters:
29890f02eecSBarry Smith .  mapping - mapping data structure
29990f02eecSBarry Smith 
300a997ad1aSLois Curfman McInnes    Level: advanced
301a997ad1aSLois Curfman McInnes 
3023acfe500SLois Curfman McInnes .seealso: ISLocalToGlobalMappingCreate()
30390f02eecSBarry Smith @*/
3046bf464f9SBarry Smith PetscErrorCode  ISLocalToGlobalMappingDestroy(ISLocalToGlobalMapping *mapping)
30590f02eecSBarry Smith {
306dfbe8321SBarry Smith   PetscErrorCode ierr;
3075fd66863SKarl Rupp 
3083a40ed3dSBarry Smith   PetscFunctionBegin;
3096bf464f9SBarry Smith   if (!*mapping) PetscFunctionReturn(0);
3106bf464f9SBarry Smith   PetscValidHeaderSpecific((*mapping),IS_LTOGM_CLASSID,1);
311997056adSBarry Smith   if (--((PetscObject)(*mapping))->refct > 0) {*mapping = 0;PetscFunctionReturn(0);}
3126bf464f9SBarry Smith   ierr     = PetscFree((*mapping)->indices);CHKERRQ(ierr);
3136bf464f9SBarry Smith   ierr     = PetscFree((*mapping)->globals);CHKERRQ(ierr);
314d38fa0fbSBarry Smith   ierr     = PetscHeaderDestroy(mapping);CHKERRQ(ierr);
315992144d0SBarry Smith   *mapping = 0;
3163a40ed3dSBarry Smith   PetscFunctionReturn(0);
31790f02eecSBarry Smith }
31890f02eecSBarry Smith 
3194a2ae208SSatish Balay #undef __FUNCT__
3204a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingApplyIS"
32190f02eecSBarry Smith /*@
3223acfe500SLois Curfman McInnes     ISLocalToGlobalMappingApplyIS - Creates from an IS in the local numbering
3233acfe500SLois Curfman McInnes     a new index set using the global numbering defined in an ISLocalToGlobalMapping
3243acfe500SLois Curfman McInnes     context.
32590f02eecSBarry Smith 
326b9cd556bSLois Curfman McInnes     Not collective
327b9cd556bSLois Curfman McInnes 
32890f02eecSBarry Smith     Input Parameters:
329b9cd556bSLois Curfman McInnes +   mapping - mapping between local and global numbering
330b9cd556bSLois Curfman McInnes -   is - index set in local numbering
33190f02eecSBarry Smith 
33290f02eecSBarry Smith     Output Parameters:
33390f02eecSBarry Smith .   newis - index set in global numbering
33490f02eecSBarry Smith 
335a997ad1aSLois Curfman McInnes     Level: advanced
336a997ad1aSLois Curfman McInnes 
337273d9f13SBarry Smith     Concepts: mapping^local to global
3383acfe500SLois Curfman McInnes 
33990f02eecSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),
340d4bb536fSBarry Smith           ISLocalToGlobalMappingDestroy(), ISGlobalToLocalMappingApply()
34190f02eecSBarry Smith @*/
3427087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingApplyIS(ISLocalToGlobalMapping mapping,IS is,IS *newis)
34390f02eecSBarry Smith {
3446849ba73SBarry Smith   PetscErrorCode ierr;
345e24637baSBarry Smith   PetscInt       n,*idxout;
3465d0c19d7SBarry Smith   const PetscInt *idxin;
3473a40ed3dSBarry Smith 
3483a40ed3dSBarry Smith   PetscFunctionBegin;
3490700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
3500700a824SBarry Smith   PetscValidHeaderSpecific(is,IS_CLASSID,2);
3514482741eSBarry Smith   PetscValidPointer(newis,3);
35290f02eecSBarry Smith 
3533b9aefa3SBarry Smith   ierr = ISGetLocalSize(is,&n);CHKERRQ(ierr);
35490f02eecSBarry Smith   ierr = ISGetIndices(is,&idxin);CHKERRQ(ierr);
355785e854fSJed Brown   ierr = PetscMalloc1(n,&idxout);CHKERRQ(ierr);
356e24637baSBarry Smith   ierr = ISLocalToGlobalMappingApply(mapping,n,idxin,idxout);CHKERRQ(ierr);
3573b9aefa3SBarry Smith   ierr = ISRestoreIndices(is,&idxin);CHKERRQ(ierr);
358543f3098SMatthew G. Knepley   ierr = ISCreateGeneral(PetscObjectComm((PetscObject)is),n,idxout,PETSC_OWN_POINTER,newis);CHKERRQ(ierr);
3593a40ed3dSBarry Smith   PetscFunctionReturn(0);
36090f02eecSBarry Smith }
36190f02eecSBarry Smith 
362afcb2eb5SJed Brown #undef __FUNCT__
363afcb2eb5SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingApply"
364b89cb25eSSatish Balay /*@
3653acfe500SLois Curfman McInnes    ISLocalToGlobalMappingApply - Takes a list of integers in a local numbering
3663acfe500SLois Curfman McInnes    and converts them to the global numbering.
36790f02eecSBarry Smith 
368b9cd556bSLois Curfman McInnes    Not collective
369b9cd556bSLois Curfman McInnes 
370bb25748dSBarry Smith    Input Parameters:
371b9cd556bSLois Curfman McInnes +  mapping - the local to global mapping context
372bb25748dSBarry Smith .  N - number of integers
373b9cd556bSLois Curfman McInnes -  in - input indices in local numbering
374bb25748dSBarry Smith 
375bb25748dSBarry Smith    Output Parameter:
376bb25748dSBarry Smith .  out - indices in global numbering
377bb25748dSBarry Smith 
378b9cd556bSLois Curfman McInnes    Notes:
379b9cd556bSLois Curfman McInnes    The in and out array parameters may be identical.
380d4bb536fSBarry Smith 
381a997ad1aSLois Curfman McInnes    Level: advanced
382a997ad1aSLois Curfman McInnes 
38345b6f7e9SBarry Smith .seealso: ISLocalToGlobalMappingApplyBlock(), ISLocalToGlobalMappingCreate(),ISLocalToGlobalMappingDestroy(),
3840752156aSBarry Smith           ISLocalToGlobalMappingApplyIS(),AOCreateBasic(),AOApplicationToPetsc(),
385d4bb536fSBarry Smith           AOPetscToApplication(), ISGlobalToLocalMappingApply()
386bb25748dSBarry Smith 
387273d9f13SBarry Smith     Concepts: mapping^local to global
388afcb2eb5SJed Brown @*/
389afcb2eb5SJed Brown PetscErrorCode ISLocalToGlobalMappingApply(ISLocalToGlobalMapping mapping,PetscInt N,const PetscInt in[],PetscInt out[])
390afcb2eb5SJed Brown {
391e24637baSBarry Smith   PetscInt       i,bs = mapping->bs,Nmax = bs*mapping->n;
39245b6f7e9SBarry Smith   const PetscInt *idx = mapping->indices;
39345b6f7e9SBarry Smith 
39445b6f7e9SBarry Smith   PetscFunctionBegin;
39545b6f7e9SBarry Smith   if (bs == 1) {
39645b6f7e9SBarry Smith     for (i=0; i<N; i++) {
39745b6f7e9SBarry Smith       if (in[i] < 0) {
39845b6f7e9SBarry Smith         out[i] = in[i];
39945b6f7e9SBarry Smith         continue;
40045b6f7e9SBarry Smith       }
401e24637baSBarry 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-1,i);
40245b6f7e9SBarry Smith       out[i] = idx[in[i]];
40345b6f7e9SBarry Smith     }
40445b6f7e9SBarry Smith   } else {
40545b6f7e9SBarry Smith     for (i=0; i<N; i++) {
40645b6f7e9SBarry Smith       if (in[i] < 0) {
40745b6f7e9SBarry Smith         out[i] = in[i];
40845b6f7e9SBarry Smith         continue;
40945b6f7e9SBarry Smith       }
410e24637baSBarry 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-1,i);
41145b6f7e9SBarry Smith       out[i] = idx[in[i]/bs]*bs + (in[i] % bs);
41245b6f7e9SBarry Smith     }
41345b6f7e9SBarry Smith   }
41445b6f7e9SBarry Smith   PetscFunctionReturn(0);
41545b6f7e9SBarry Smith }
41645b6f7e9SBarry Smith 
41745b6f7e9SBarry Smith #undef __FUNCT__
41845b6f7e9SBarry Smith #define __FUNCT__ "ISLocalToGlobalMappingApplyBlock"
41945b6f7e9SBarry Smith /*@
42045b6f7e9SBarry Smith    ISLocalToGlobalMappingApplyBlock - Takes a list of integers in a local numbering  and converts them to the global numbering.
42145b6f7e9SBarry Smith 
42245b6f7e9SBarry Smith    Not collective
42345b6f7e9SBarry Smith 
42445b6f7e9SBarry Smith    Input Parameters:
42545b6f7e9SBarry Smith +  mapping - the local to global mapping context
42645b6f7e9SBarry Smith .  N - number of integers
42745b6f7e9SBarry Smith -  in - input indices in local numbering
42845b6f7e9SBarry Smith 
42945b6f7e9SBarry Smith    Output Parameter:
43045b6f7e9SBarry Smith .  out - indices in global numbering
43145b6f7e9SBarry Smith 
43245b6f7e9SBarry Smith    Notes:
43345b6f7e9SBarry Smith    The in and out array parameters may be identical.
43445b6f7e9SBarry Smith 
43545b6f7e9SBarry Smith    Level: advanced
43645b6f7e9SBarry Smith 
43745b6f7e9SBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),ISLocalToGlobalMappingDestroy(),
43845b6f7e9SBarry Smith           ISLocalToGlobalMappingApplyIS(),AOCreateBasic(),AOApplicationToPetsc(),
43945b6f7e9SBarry Smith           AOPetscToApplication(), ISGlobalToLocalMappingApply()
44045b6f7e9SBarry Smith 
44145b6f7e9SBarry Smith     Concepts: mapping^local to global
44245b6f7e9SBarry Smith @*/
44345b6f7e9SBarry Smith PetscErrorCode ISLocalToGlobalMappingApplyBlock(ISLocalToGlobalMapping mapping,PetscInt N,const PetscInt in[],PetscInt out[])
44445b6f7e9SBarry Smith {
445afcb2eb5SJed Brown   PetscInt       i,Nmax = mapping->n;
446afcb2eb5SJed Brown   const PetscInt *idx = mapping->indices;
447d4bb536fSBarry Smith 
448afcb2eb5SJed Brown   PetscFunctionBegin;
449afcb2eb5SJed Brown   for (i=0; i<N; i++) {
450afcb2eb5SJed Brown     if (in[i] < 0) {
451afcb2eb5SJed Brown       out[i] = in[i];
452afcb2eb5SJed Brown       continue;
453afcb2eb5SJed Brown     }
454e24637baSBarry 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],Nmax-1,i);
455afcb2eb5SJed Brown     out[i] = idx[in[i]];
456afcb2eb5SJed Brown   }
457afcb2eb5SJed Brown   PetscFunctionReturn(0);
458afcb2eb5SJed Brown }
459d4bb536fSBarry Smith 
460d4bb536fSBarry Smith /* -----------------------------------------------------------------------------------------*/
461d4bb536fSBarry Smith 
4624a2ae208SSatish Balay #undef __FUNCT__
4634a2ae208SSatish Balay #define __FUNCT__ "ISGlobalToLocalMappingSetUp_Private"
464d4bb536fSBarry Smith /*
465d4bb536fSBarry Smith     Creates the global fields in the ISLocalToGlobalMapping structure
466d4bb536fSBarry Smith */
4676849ba73SBarry Smith static PetscErrorCode ISGlobalToLocalMappingSetUp_Private(ISLocalToGlobalMapping mapping)
468d4bb536fSBarry Smith {
4696849ba73SBarry Smith   PetscErrorCode ierr;
47032dcc486SBarry Smith   PetscInt       i,*idx = mapping->indices,n = mapping->n,end,start,*globals;
471d4bb536fSBarry Smith 
4723a40ed3dSBarry Smith   PetscFunctionBegin;
473d4bb536fSBarry Smith   end   = 0;
474ec268f7cSJed Brown   start = PETSC_MAX_INT;
475d4bb536fSBarry Smith 
476d4bb536fSBarry Smith   for (i=0; i<n; i++) {
477d4bb536fSBarry Smith     if (idx[i] < 0) continue;
478d4bb536fSBarry Smith     if (idx[i] < start) start = idx[i];
479d4bb536fSBarry Smith     if (idx[i] > end)   end   = idx[i];
480d4bb536fSBarry Smith   }
481d4bb536fSBarry Smith   if (start > end) {start = 0; end = -1;}
482d4bb536fSBarry Smith   mapping->globalstart = start;
483d4bb536fSBarry Smith   mapping->globalend   = end;
484d4bb536fSBarry Smith 
485785e854fSJed Brown   ierr             = PetscMalloc1((end-start+2),&globals);CHKERRQ(ierr);
486b0a32e0cSBarry Smith   mapping->globals = globals;
487f6e5521dSKarl Rupp   for (i=0; i<end-start+1; i++) globals[i] = -1;
488d4bb536fSBarry Smith   for (i=0; i<n; i++) {
489d4bb536fSBarry Smith     if (idx[i] < 0) continue;
490d4bb536fSBarry Smith     globals[idx[i] - start] = i;
491d4bb536fSBarry Smith   }
492d4bb536fSBarry Smith 
4933bb1ff40SBarry Smith   ierr = PetscLogObjectMemory((PetscObject)mapping,(end-start+1)*sizeof(PetscInt));CHKERRQ(ierr);
4943a40ed3dSBarry Smith   PetscFunctionReturn(0);
495d4bb536fSBarry Smith }
496d4bb536fSBarry Smith 
4974a2ae208SSatish Balay #undef __FUNCT__
4984a2ae208SSatish Balay #define __FUNCT__ "ISGlobalToLocalMappingApply"
499d4bb536fSBarry Smith /*@
500a997ad1aSLois Curfman McInnes     ISGlobalToLocalMappingApply - Provides the local numbering for a list of integers
501a997ad1aSLois Curfman McInnes     specified with a global numbering.
502d4bb536fSBarry Smith 
503b9cd556bSLois Curfman McInnes     Not collective
504b9cd556bSLois Curfman McInnes 
505d4bb536fSBarry Smith     Input Parameters:
506b9cd556bSLois Curfman McInnes +   mapping - mapping between local and global numbering
507d4bb536fSBarry Smith .   type - IS_GTOLM_MASK - replaces global indices with no local value with -1
508d4bb536fSBarry Smith            IS_GTOLM_DROP - drops the indices with no local value from the output list
509d4bb536fSBarry Smith .   n - number of global indices to map
510b9cd556bSLois Curfman McInnes -   idx - global indices to map
511d4bb536fSBarry Smith 
512d4bb536fSBarry Smith     Output Parameters:
513b9cd556bSLois Curfman McInnes +   nout - number of indices in output array (if type == IS_GTOLM_MASK then nout = n)
514b9cd556bSLois Curfman McInnes -   idxout - local index of each global index, one must pass in an array long enough
515e182c471SBarry Smith              to hold all the indices. You can call ISGlobalToLocalMappingApply() with
5160298fd71SBarry Smith              idxout == NULL to determine the required length (returned in nout)
517e182c471SBarry Smith              and then allocate the required space and call ISGlobalToLocalMappingApply()
518e182c471SBarry Smith              a second time to set the values.
519d4bb536fSBarry Smith 
520b9cd556bSLois Curfman McInnes     Notes:
5210298fd71SBarry Smith     Either nout or idxout may be NULL. idx and idxout may be identical.
522d4bb536fSBarry Smith 
5230f5bd95cSBarry Smith     This is not scalable in memory usage. Each processor requires O(Nglobal) size
5240f5bd95cSBarry Smith     array to compute these.
5250f5bd95cSBarry Smith 
526a997ad1aSLois Curfman McInnes     Level: advanced
527a997ad1aSLois Curfman McInnes 
52832fd6b96SBarry Smith     Developer Note: The manual page states that idx and idxout may be identical but the calling
52932fd6b96SBarry Smith        sequence declares idx as const so it cannot be the same as idxout.
53032fd6b96SBarry Smith 
531273d9f13SBarry Smith     Concepts: mapping^global to local
532d4bb536fSBarry Smith 
5339d90f715SBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISGlobalToLocalMappingApplyBlock(), ISLocalToGlobalMappingCreate(),
534d4bb536fSBarry Smith           ISLocalToGlobalMappingDestroy()
535d4bb536fSBarry Smith @*/
5367087cfbeSBarry Smith PetscErrorCode  ISGlobalToLocalMappingApply(ISLocalToGlobalMapping mapping,ISGlobalToLocalMappingType type,
53732dcc486SBarry Smith                                             PetscInt n,const PetscInt idx[],PetscInt *nout,PetscInt idxout[])
538d4bb536fSBarry Smith {
5399d90f715SBarry Smith   PetscInt       i,*globals,nf = 0,tmp,start,end,bs;
5409d90f715SBarry Smith   PetscErrorCode ierr;
5419d90f715SBarry Smith 
5429d90f715SBarry Smith   PetscFunctionBegin;
5439d90f715SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
5449d90f715SBarry Smith   if (!mapping->globals) {
5459d90f715SBarry Smith     ierr = ISGlobalToLocalMappingSetUp_Private(mapping);CHKERRQ(ierr);
5469d90f715SBarry Smith   }
5479d90f715SBarry Smith   globals = mapping->globals;
5489d90f715SBarry Smith   start   = mapping->globalstart;
5499d90f715SBarry Smith   end     = mapping->globalend;
5509d90f715SBarry Smith   bs      = mapping->bs;
5519d90f715SBarry Smith 
5529d90f715SBarry Smith   if (type == IS_GTOLM_MASK) {
5539d90f715SBarry Smith     if (idxout) {
5549d90f715SBarry Smith       for (i=0; i<n; i++) {
5559d90f715SBarry Smith         if (idx[i] < 0) idxout[i] = idx[i];
5569d90f715SBarry Smith         else if (idx[i] < bs*start) idxout[i] = -1;
5579d90f715SBarry Smith         else if (idx[i] > bs*end)   idxout[i] = -1;
5589d90f715SBarry Smith         else                        idxout[i] = bs*globals[idx[i]/bs - start] + (idx[i] % bs);
5599d90f715SBarry Smith       }
5609d90f715SBarry Smith     }
5619d90f715SBarry Smith     if (nout) *nout = n;
5629d90f715SBarry Smith   } else {
5639d90f715SBarry Smith     if (idxout) {
5649d90f715SBarry Smith       for (i=0; i<n; i++) {
5659d90f715SBarry Smith         if (idx[i] < 0) continue;
5669d90f715SBarry Smith         if (idx[i] < bs*start) continue;
5679d90f715SBarry Smith         if (idx[i] > bs*end) continue;
5689d90f715SBarry Smith         tmp = bs*globals[idx[i]/bs - start] + (idx[i] % bs);
5699d90f715SBarry Smith         if (tmp < 0) continue;
5709d90f715SBarry Smith         idxout[nf++] = tmp;
5719d90f715SBarry Smith       }
5729d90f715SBarry Smith     } else {
5739d90f715SBarry Smith       for (i=0; i<n; i++) {
5749d90f715SBarry Smith         if (idx[i] < 0) continue;
5759d90f715SBarry Smith         if (idx[i] < bs*start) continue;
5769d90f715SBarry Smith         if (idx[i] > bs*end) continue;
5779d90f715SBarry Smith         tmp = bs*globals[idx[i]/bs - start] + (idx[i] % bs);
5789d90f715SBarry Smith         if (tmp < 0) continue;
5799d90f715SBarry Smith         nf++;
5809d90f715SBarry Smith       }
5819d90f715SBarry Smith     }
5829d90f715SBarry Smith     if (nout) *nout = nf;
5839d90f715SBarry Smith   }
5849d90f715SBarry Smith   PetscFunctionReturn(0);
5859d90f715SBarry Smith }
5869d90f715SBarry Smith 
5879d90f715SBarry Smith #undef __FUNCT__
5889d90f715SBarry Smith #define __FUNCT__ "ISGlobalToLocalMappingApplyBlock"
5899d90f715SBarry Smith /*@
5909d90f715SBarry Smith     ISGlobalToLocalMappingApplyBlock - Provides the local block numbering for a list of integers
5919d90f715SBarry Smith     specified with a block global numbering.
5929d90f715SBarry Smith 
5939d90f715SBarry Smith     Not collective
5949d90f715SBarry Smith 
5959d90f715SBarry Smith     Input Parameters:
5969d90f715SBarry Smith +   mapping - mapping between local and global numbering
5979d90f715SBarry Smith .   type - IS_GTOLM_MASK - replaces global indices with no local value with -1
5989d90f715SBarry Smith            IS_GTOLM_DROP - drops the indices with no local value from the output list
5999d90f715SBarry Smith .   n - number of global indices to map
6009d90f715SBarry Smith -   idx - global indices to map
6019d90f715SBarry Smith 
6029d90f715SBarry Smith     Output Parameters:
6039d90f715SBarry Smith +   nout - number of indices in output array (if type == IS_GTOLM_MASK then nout = n)
6049d90f715SBarry Smith -   idxout - local index of each global index, one must pass in an array long enough
6059d90f715SBarry Smith              to hold all the indices. You can call ISGlobalToLocalMappingApplyBlock() with
6069d90f715SBarry Smith              idxout == NULL to determine the required length (returned in nout)
6079d90f715SBarry Smith              and then allocate the required space and call ISGlobalToLocalMappingApplyBlock()
6089d90f715SBarry Smith              a second time to set the values.
6099d90f715SBarry Smith 
6109d90f715SBarry Smith     Notes:
6119d90f715SBarry Smith     Either nout or idxout may be NULL. idx and idxout may be identical.
6129d90f715SBarry Smith 
6139d90f715SBarry Smith     This is not scalable in memory usage. Each processor requires O(Nglobal) size
6149d90f715SBarry Smith     array to compute these.
6159d90f715SBarry Smith 
6169d90f715SBarry Smith     Level: advanced
6179d90f715SBarry Smith 
6189d90f715SBarry Smith     Developer Note: The manual page states that idx and idxout may be identical but the calling
6199d90f715SBarry Smith        sequence declares idx as const so it cannot be the same as idxout.
6209d90f715SBarry Smith 
6219d90f715SBarry Smith     Concepts: mapping^global to local
6229d90f715SBarry Smith 
6239d90f715SBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISGlobalToLocalMappingApply(), ISLocalToGlobalMappingCreate(),
6249d90f715SBarry Smith           ISLocalToGlobalMappingDestroy()
6259d90f715SBarry Smith @*/
6269d90f715SBarry Smith PetscErrorCode  ISGlobalToLocalMappingApplyBlock(ISLocalToGlobalMapping mapping,ISGlobalToLocalMappingType type,
6279d90f715SBarry Smith                                   PetscInt n,const PetscInt idx[],PetscInt *nout,PetscInt idxout[])
6289d90f715SBarry Smith {
62932dcc486SBarry Smith   PetscInt       i,*globals,nf = 0,tmp,start,end;
6306849ba73SBarry Smith   PetscErrorCode ierr;
631d4bb536fSBarry Smith 
6323a40ed3dSBarry Smith   PetscFunctionBegin;
6330700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
634d4bb536fSBarry Smith   if (!mapping->globals) {
635d4bb536fSBarry Smith     ierr = ISGlobalToLocalMappingSetUp_Private(mapping);CHKERRQ(ierr);
636d4bb536fSBarry Smith   }
637d4bb536fSBarry Smith   globals = mapping->globals;
638d4bb536fSBarry Smith   start   = mapping->globalstart;
639d4bb536fSBarry Smith   end     = mapping->globalend;
640d4bb536fSBarry Smith 
641d4bb536fSBarry Smith   if (type == IS_GTOLM_MASK) {
642d4bb536fSBarry Smith     if (idxout) {
643d4bb536fSBarry Smith       for (i=0; i<n; i++) {
644d4bb536fSBarry Smith         if (idx[i] < 0) idxout[i] = idx[i];
645d4bb536fSBarry Smith         else if (idx[i] < start) idxout[i] = -1;
646d4bb536fSBarry Smith         else if (idx[i] > end)   idxout[i] = -1;
647d4bb536fSBarry Smith         else                     idxout[i] = globals[idx[i] - start];
648d4bb536fSBarry Smith       }
649d4bb536fSBarry Smith     }
650d4bb536fSBarry Smith     if (nout) *nout = n;
651d4bb536fSBarry Smith   } else {
652d4bb536fSBarry Smith     if (idxout) {
653d4bb536fSBarry Smith       for (i=0; i<n; i++) {
654d4bb536fSBarry Smith         if (idx[i] < 0) continue;
655d4bb536fSBarry Smith         if (idx[i] < start) continue;
656d4bb536fSBarry Smith         if (idx[i] > end) continue;
657d4bb536fSBarry Smith         tmp = globals[idx[i] - start];
658d4bb536fSBarry Smith         if (tmp < 0) continue;
659d4bb536fSBarry Smith         idxout[nf++] = tmp;
660d4bb536fSBarry Smith       }
661d4bb536fSBarry Smith     } else {
662d4bb536fSBarry Smith       for (i=0; i<n; i++) {
663d4bb536fSBarry Smith         if (idx[i] < 0) continue;
664d4bb536fSBarry Smith         if (idx[i] < start) continue;
665d4bb536fSBarry Smith         if (idx[i] > end) continue;
666d4bb536fSBarry Smith         tmp = globals[idx[i] - start];
667d4bb536fSBarry Smith         if (tmp < 0) continue;
668d4bb536fSBarry Smith         nf++;
669d4bb536fSBarry Smith       }
670d4bb536fSBarry Smith     }
671d4bb536fSBarry Smith     if (nout) *nout = nf;
672d4bb536fSBarry Smith   }
6733a40ed3dSBarry Smith   PetscFunctionReturn(0);
674d4bb536fSBarry Smith }
67590f02eecSBarry Smith 
6764a2ae208SSatish Balay #undef __FUNCT__
677*6a818285SBarry Smith #define __FUNCT__ "ISLocalToGlobalMappingGetBlockInfo"
67889d82c54SBarry Smith /*@C
679*6a818285SBarry Smith     ISLocalToGlobalMappingGetBlockInfo - Gets the neighbor information for each processor and
68089d82c54SBarry Smith      each index shared by more than one processor
68189d82c54SBarry Smith 
68289d82c54SBarry Smith     Collective on ISLocalToGlobalMapping
68389d82c54SBarry Smith 
68489d82c54SBarry Smith     Input Parameters:
68589d82c54SBarry Smith .   mapping - the mapping from local to global indexing
68689d82c54SBarry Smith 
68789d82c54SBarry Smith     Output Parameter:
68889d82c54SBarry Smith +   nproc - number of processors that are connected to this one
68989d82c54SBarry Smith .   proc - neighboring processors
69007b52d57SBarry Smith .   numproc - number of indices for each subdomain (processor)
6913463a7baSJed Brown -   indices - indices of nodes (in local numbering) shared with neighbors (sorted by global numbering)
69289d82c54SBarry Smith 
69389d82c54SBarry Smith     Level: advanced
69489d82c54SBarry Smith 
695273d9f13SBarry Smith     Concepts: mapping^local to global
69689d82c54SBarry Smith 
6972cfcea29SBarry Smith     Fortran Usage:
6982cfcea29SBarry Smith $        ISLocalToGlobalMpngGetInfoSize(ISLocalToGlobalMapping,PetscInt nproc,PetscInt numprocmax,ierr) followed by
6992cfcea29SBarry Smith $        ISLocalToGlobalMappingGetInfo(ISLocalToGlobalMapping,PetscInt nproc, PetscInt procs[nproc],PetscInt numprocs[nproc],
7002cfcea29SBarry Smith           PetscInt indices[nproc][numprocmax],ierr)
7012cfcea29SBarry Smith         There is no ISLocalToGlobalMappingRestoreInfo() in Fortran. You must make sure that procs[], numprocs[] and
7022cfcea29SBarry Smith         indices[][] are large enough arrays, either by allocating them dynamically or defining static ones large enough.
7032cfcea29SBarry Smith 
7042cfcea29SBarry Smith 
70507b52d57SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreate(),
70607b52d57SBarry Smith           ISLocalToGlobalMappingRestoreInfo()
70789d82c54SBarry Smith @*/
708*6a818285SBarry Smith PetscErrorCode  ISLocalToGlobalMappingGetBlockInfo(ISLocalToGlobalMapping mapping,PetscInt *nproc,PetscInt *procs[],PetscInt *numprocs[],PetscInt **indices[])
70989d82c54SBarry Smith {
7106849ba73SBarry Smith   PetscErrorCode ierr;
71197f1f81fSBarry Smith   PetscMPIInt    size,rank,tag1,tag2,tag3,*len,*source,imdex;
71232dcc486SBarry Smith   PetscInt       i,n = mapping->n,Ng,ng,max = 0,*lindices = mapping->indices;
71332dcc486SBarry Smith   PetscInt       *nprocs,*owner,nsends,*sends,j,*starts,nmax,nrecvs,*recvs,proc;
71497f1f81fSBarry Smith   PetscInt       cnt,scale,*ownedsenders,*nownedsenders,rstart,nowned;
71532dcc486SBarry Smith   PetscInt       node,nownedm,nt,*sends2,nsends2,*starts2,*lens2,*dest,nrecvs2,*starts3,*recvs2,k,*bprocs,*tmp;
71632dcc486SBarry Smith   PetscInt       first_procs,first_numprocs,*first_indices;
71789d82c54SBarry Smith   MPI_Request    *recv_waits,*send_waits;
71830dcb7c9SBarry Smith   MPI_Status     recv_status,*send_status,*recv_statuses;
719ce94432eSBarry Smith   MPI_Comm       comm;
720ace3abfcSBarry Smith   PetscBool      debug = PETSC_FALSE;
72189d82c54SBarry Smith 
72289d82c54SBarry Smith   PetscFunctionBegin;
7230700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
724ce94432eSBarry Smith   ierr = PetscObjectGetComm((PetscObject)mapping,&comm);CHKERRQ(ierr);
72524cf384cSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
72624cf384cSBarry Smith   ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
72724cf384cSBarry Smith   if (size == 1) {
72824cf384cSBarry Smith     *nproc         = 0;
7290298fd71SBarry Smith     *procs         = NULL;
73032dcc486SBarry Smith     ierr           = PetscMalloc(sizeof(PetscInt),numprocs);CHKERRQ(ierr);
7311e2105dcSBarry Smith     (*numprocs)[0] = 0;
73232dcc486SBarry Smith     ierr           = PetscMalloc(sizeof(PetscInt*),indices);CHKERRQ(ierr);
7330298fd71SBarry Smith     (*indices)[0]  = NULL;
73424cf384cSBarry Smith     PetscFunctionReturn(0);
73524cf384cSBarry Smith   }
73624cf384cSBarry Smith 
7370298fd71SBarry Smith   ierr = PetscOptionsGetBool(NULL,"-islocaltoglobalmappinggetinfo_debug",&debug,NULL);CHKERRQ(ierr);
73807b52d57SBarry Smith 
7393677ff5aSBarry Smith   /*
740*6a818285SBarry Smith     Notes on ISLocalToGlobalMappingGetBlockInfo
7413677ff5aSBarry Smith 
7423677ff5aSBarry Smith     globally owned node - the nodes that have been assigned to this processor in global
7433677ff5aSBarry Smith            numbering, just for this routine.
7443677ff5aSBarry Smith 
7453677ff5aSBarry Smith     nontrivial globally owned node - node assigned to this processor that is on a subdomain
7463677ff5aSBarry Smith            boundary (i.e. is has more than one local owner)
7473677ff5aSBarry Smith 
7483677ff5aSBarry Smith     locally owned node - node that exists on this processors subdomain
7493677ff5aSBarry Smith 
7503677ff5aSBarry Smith     nontrivial locally owned node - node that is not in the interior (i.e. has more than one
7513677ff5aSBarry Smith            local subdomain
7523677ff5aSBarry Smith   */
75324cf384cSBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mapping,&tag1);CHKERRQ(ierr);
75424cf384cSBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mapping,&tag2);CHKERRQ(ierr);
75524cf384cSBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mapping,&tag3);CHKERRQ(ierr);
75689d82c54SBarry Smith 
75789d82c54SBarry Smith   for (i=0; i<n; i++) {
75889d82c54SBarry Smith     if (lindices[i] > max) max = lindices[i];
75989d82c54SBarry Smith   }
76032dcc486SBarry Smith   ierr   = MPI_Allreduce(&max,&Ng,1,MPIU_INT,MPI_MAX,comm);CHKERRQ(ierr);
76178058e43SBarry Smith   Ng++;
76289d82c54SBarry Smith   ierr   = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
76389d82c54SBarry Smith   ierr   = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
764bc8ff85bSBarry Smith   scale  = Ng/size + 1;
765a2e34c3dSBarry Smith   ng     = scale; if (rank == size-1) ng = Ng - scale*(size-1); ng = PetscMax(1,ng);
766caba0dd0SBarry Smith   rstart = scale*rank;
76789d82c54SBarry Smith 
76889d82c54SBarry Smith   /* determine ownership ranges of global indices */
769785e854fSJed Brown   ierr = PetscMalloc1(2*size,&nprocs);CHKERRQ(ierr);
77032dcc486SBarry Smith   ierr = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr);
77189d82c54SBarry Smith 
77289d82c54SBarry Smith   /* determine owners of each local node  */
773785e854fSJed Brown   ierr = PetscMalloc1(n,&owner);CHKERRQ(ierr);
77489d82c54SBarry Smith   for (i=0; i<n; i++) {
7753677ff5aSBarry Smith     proc             = lindices[i]/scale; /* processor that globally owns this index */
77627c402fcSBarry Smith     nprocs[2*proc+1] = 1;                 /* processor globally owns at least one of ours */
7773677ff5aSBarry Smith     owner[i]         = proc;
77827c402fcSBarry Smith     nprocs[2*proc]++;                     /* count of how many that processor globally owns of ours */
77989d82c54SBarry Smith   }
78027c402fcSBarry Smith   nsends = 0; for (i=0; i<size; i++) nsends += nprocs[2*i+1];
7817904a332SBarry Smith   ierr = PetscInfo1(mapping,"Number of global owners for my local data %D\n",nsends);CHKERRQ(ierr);
78289d82c54SBarry Smith 
78389d82c54SBarry Smith   /* inform other processors of number of messages and max length*/
78427c402fcSBarry Smith   ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr);
7857904a332SBarry Smith   ierr = PetscInfo1(mapping,"Number of local owners for my global data %D\n",nrecvs);CHKERRQ(ierr);
78689d82c54SBarry Smith 
78789d82c54SBarry Smith   /* post receives for owned rows */
788785e854fSJed Brown   ierr = PetscMalloc1((2*nrecvs+1)*(nmax+1),&recvs);CHKERRQ(ierr);
789785e854fSJed Brown   ierr = PetscMalloc1((nrecvs+1),&recv_waits);CHKERRQ(ierr);
79089d82c54SBarry Smith   for (i=0; i<nrecvs; i++) {
79132dcc486SBarry Smith     ierr = MPI_Irecv(recvs+2*nmax*i,2*nmax,MPIU_INT,MPI_ANY_SOURCE,tag1,comm,recv_waits+i);CHKERRQ(ierr);
79289d82c54SBarry Smith   }
79389d82c54SBarry Smith 
79489d82c54SBarry Smith   /* pack messages containing lists of local nodes to owners */
795785e854fSJed Brown   ierr      = PetscMalloc1((2*n+1),&sends);CHKERRQ(ierr);
796785e854fSJed Brown   ierr      = PetscMalloc1((size+1),&starts);CHKERRQ(ierr);
79789d82c54SBarry Smith   starts[0] = 0;
798f6e5521dSKarl Rupp   for (i=1; i<size; i++) starts[i] = starts[i-1] + 2*nprocs[2*i-2];
79989d82c54SBarry Smith   for (i=0; i<n; i++) {
80089d82c54SBarry Smith     sends[starts[owner[i]]++] = lindices[i];
80130dcb7c9SBarry Smith     sends[starts[owner[i]]++] = i;
80289d82c54SBarry Smith   }
80389d82c54SBarry Smith   ierr = PetscFree(owner);CHKERRQ(ierr);
80489d82c54SBarry Smith   starts[0] = 0;
805f6e5521dSKarl Rupp   for (i=1; i<size; i++) starts[i] = starts[i-1] + 2*nprocs[2*i-2];
80689d82c54SBarry Smith 
80789d82c54SBarry Smith   /* send the messages */
808785e854fSJed Brown   ierr = PetscMalloc1((nsends+1),&send_waits);CHKERRQ(ierr);
809785e854fSJed Brown   ierr = PetscMalloc1((nsends+1),&dest);CHKERRQ(ierr);
81089d82c54SBarry Smith   cnt = 0;
81189d82c54SBarry Smith   for (i=0; i<size; i++) {
81227c402fcSBarry Smith     if (nprocs[2*i]) {
81332dcc486SBarry Smith       ierr      = MPI_Isend(sends+starts[i],2*nprocs[2*i],MPIU_INT,i,tag1,comm,send_waits+cnt);CHKERRQ(ierr);
81430dcb7c9SBarry Smith       dest[cnt] = i;
81589d82c54SBarry Smith       cnt++;
81689d82c54SBarry Smith     }
81789d82c54SBarry Smith   }
81889d82c54SBarry Smith   ierr = PetscFree(starts);CHKERRQ(ierr);
81989d82c54SBarry Smith 
82089d82c54SBarry Smith   /* wait on receives */
821785e854fSJed Brown   ierr = PetscMalloc1((nrecvs+1),&source);CHKERRQ(ierr);
822785e854fSJed Brown   ierr = PetscMalloc1((nrecvs+1),&len);CHKERRQ(ierr);
82389d82c54SBarry Smith   cnt  = nrecvs;
824785e854fSJed Brown   ierr = PetscMalloc1((ng+1),&nownedsenders);CHKERRQ(ierr);
82532dcc486SBarry Smith   ierr = PetscMemzero(nownedsenders,ng*sizeof(PetscInt));CHKERRQ(ierr);
82689d82c54SBarry Smith   while (cnt) {
82789d82c54SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
82889d82c54SBarry Smith     /* unpack receives into our local space */
82932dcc486SBarry Smith     ierr          = MPI_Get_count(&recv_status,MPIU_INT,&len[imdex]);CHKERRQ(ierr);
83089d82c54SBarry Smith     source[imdex] = recv_status.MPI_SOURCE;
83130dcb7c9SBarry Smith     len[imdex]    = len[imdex]/2;
832caba0dd0SBarry Smith     /* count how many local owners for each of my global owned indices */
83330dcb7c9SBarry Smith     for (i=0; i<len[imdex]; i++) nownedsenders[recvs[2*imdex*nmax+2*i]-rstart]++;
83489d82c54SBarry Smith     cnt--;
83589d82c54SBarry Smith   }
83689d82c54SBarry Smith   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
83789d82c54SBarry Smith 
83830dcb7c9SBarry Smith   /* count how many globally owned indices are on an edge multiplied by how many processors own them. */
839bc8ff85bSBarry Smith   nowned  = 0;
840bc8ff85bSBarry Smith   nownedm = 0;
841bc8ff85bSBarry Smith   for (i=0; i<ng; i++) {
842bc8ff85bSBarry Smith     if (nownedsenders[i] > 1) {nownedm += nownedsenders[i]; nowned++;}
843bc8ff85bSBarry Smith   }
844bc8ff85bSBarry Smith 
845bc8ff85bSBarry Smith   /* create single array to contain rank of all local owners of each globally owned index */
846785e854fSJed Brown   ierr      = PetscMalloc1((nownedm+1),&ownedsenders);CHKERRQ(ierr);
847785e854fSJed Brown   ierr      = PetscMalloc1((ng+1),&starts);CHKERRQ(ierr);
848bc8ff85bSBarry Smith   starts[0] = 0;
849bc8ff85bSBarry Smith   for (i=1; i<ng; i++) {
850bc8ff85bSBarry Smith     if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1];
851bc8ff85bSBarry Smith     else starts[i] = starts[i-1];
852bc8ff85bSBarry Smith   }
853bc8ff85bSBarry Smith 
85430dcb7c9SBarry Smith   /* for each nontrival globally owned node list all arriving processors */
855bc8ff85bSBarry Smith   for (i=0; i<nrecvs; i++) {
856bc8ff85bSBarry Smith     for (j=0; j<len[i]; j++) {
85730dcb7c9SBarry Smith       node = recvs[2*i*nmax+2*j]-rstart;
858f6e5521dSKarl Rupp       if (nownedsenders[node] > 1) ownedsenders[starts[node]++] = source[i];
859bc8ff85bSBarry Smith     }
860bc8ff85bSBarry Smith   }
861bc8ff85bSBarry Smith 
86207b52d57SBarry Smith   if (debug) { /* -----------------------------------  */
86330dcb7c9SBarry Smith     starts[0] = 0;
86430dcb7c9SBarry Smith     for (i=1; i<ng; i++) {
86530dcb7c9SBarry Smith       if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1];
86630dcb7c9SBarry Smith       else starts[i] = starts[i-1];
86730dcb7c9SBarry Smith     }
86830dcb7c9SBarry Smith     for (i=0; i<ng; i++) {
86930dcb7c9SBarry Smith       if (nownedsenders[i] > 1) {
8707904a332SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"[%d] global node %D local owner processors: ",rank,i+rstart);CHKERRQ(ierr);
87130dcb7c9SBarry Smith         for (j=0; j<nownedsenders[i]; j++) {
8727904a332SBarry Smith           ierr = PetscSynchronizedPrintf(comm,"%D ",ownedsenders[starts[i]+j]);CHKERRQ(ierr);
87330dcb7c9SBarry Smith         }
87430dcb7c9SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"\n");CHKERRQ(ierr);
87530dcb7c9SBarry Smith       }
87630dcb7c9SBarry Smith     }
8770ec8b6e3SBarry Smith     ierr = PetscSynchronizedFlush(comm,PETSC_STDOUT);CHKERRQ(ierr);
87807b52d57SBarry Smith   } /* -----------------------------------  */
87930dcb7c9SBarry Smith 
8803677ff5aSBarry Smith   /* wait on original sends */
8813a96401aSBarry Smith   if (nsends) {
882785e854fSJed Brown     ierr = PetscMalloc1(nsends,&send_status);CHKERRQ(ierr);
8833a96401aSBarry Smith     ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
8843a96401aSBarry Smith     ierr = PetscFree(send_status);CHKERRQ(ierr);
8853a96401aSBarry Smith   }
88689d82c54SBarry Smith   ierr = PetscFree(send_waits);CHKERRQ(ierr);
8873a96401aSBarry Smith   ierr = PetscFree(sends);CHKERRQ(ierr);
8883677ff5aSBarry Smith   ierr = PetscFree(nprocs);CHKERRQ(ierr);
8893677ff5aSBarry Smith 
8903677ff5aSBarry Smith   /* pack messages to send back to local owners */
89130dcb7c9SBarry Smith   starts[0] = 0;
89230dcb7c9SBarry Smith   for (i=1; i<ng; i++) {
89330dcb7c9SBarry Smith     if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1];
89430dcb7c9SBarry Smith     else starts[i] = starts[i-1];
89530dcb7c9SBarry Smith   }
89630dcb7c9SBarry Smith   nsends2 = nrecvs;
897785e854fSJed Brown   ierr    = PetscMalloc1((nsends2+1),&nprocs);CHKERRQ(ierr); /* length of each message */
89830dcb7c9SBarry Smith   for (i=0; i<nrecvs; i++) {
89930dcb7c9SBarry Smith     nprocs[i] = 1;
90030dcb7c9SBarry Smith     for (j=0; j<len[i]; j++) {
90130dcb7c9SBarry Smith       node = recvs[2*i*nmax+2*j]-rstart;
902f6e5521dSKarl Rupp       if (nownedsenders[node] > 1) nprocs[i] += 2 + nownedsenders[node];
90330dcb7c9SBarry Smith     }
90430dcb7c9SBarry Smith   }
905f6e5521dSKarl Rupp   nt = 0;
906f6e5521dSKarl Rupp   for (i=0; i<nsends2; i++) nt += nprocs[i];
907f6e5521dSKarl Rupp 
908785e854fSJed Brown   ierr = PetscMalloc1((nt+1),&sends2);CHKERRQ(ierr);
909785e854fSJed Brown   ierr = PetscMalloc1((nsends2+1),&starts2);CHKERRQ(ierr);
910f6e5521dSKarl Rupp 
911f6e5521dSKarl Rupp   starts2[0] = 0;
912f6e5521dSKarl Rupp   for (i=1; i<nsends2; i++) starts2[i] = starts2[i-1] + nprocs[i-1];
91330dcb7c9SBarry Smith   /*
91430dcb7c9SBarry Smith      Each message is 1 + nprocs[i] long, and consists of
91530dcb7c9SBarry Smith        (0) the number of nodes being sent back
91630dcb7c9SBarry Smith        (1) the local node number,
91730dcb7c9SBarry Smith        (2) the number of processors sharing it,
91830dcb7c9SBarry Smith        (3) the processors sharing it
91930dcb7c9SBarry Smith   */
92030dcb7c9SBarry Smith   for (i=0; i<nsends2; i++) {
92130dcb7c9SBarry Smith     cnt = 1;
92230dcb7c9SBarry Smith     sends2[starts2[i]] = 0;
92330dcb7c9SBarry Smith     for (j=0; j<len[i]; j++) {
92430dcb7c9SBarry Smith       node = recvs[2*i*nmax+2*j]-rstart;
92530dcb7c9SBarry Smith       if (nownedsenders[node] > 1) {
92630dcb7c9SBarry Smith         sends2[starts2[i]]++;
92730dcb7c9SBarry Smith         sends2[starts2[i]+cnt++] = recvs[2*i*nmax+2*j+1];
92830dcb7c9SBarry Smith         sends2[starts2[i]+cnt++] = nownedsenders[node];
92932dcc486SBarry Smith         ierr = PetscMemcpy(&sends2[starts2[i]+cnt],&ownedsenders[starts[node]],nownedsenders[node]*sizeof(PetscInt));CHKERRQ(ierr);
93030dcb7c9SBarry Smith         cnt += nownedsenders[node];
93130dcb7c9SBarry Smith       }
93230dcb7c9SBarry Smith     }
93330dcb7c9SBarry Smith   }
93430dcb7c9SBarry Smith 
93530dcb7c9SBarry Smith   /* receive the message lengths */
93630dcb7c9SBarry Smith   nrecvs2 = nsends;
937785e854fSJed Brown   ierr    = PetscMalloc1((nrecvs2+1),&lens2);CHKERRQ(ierr);
938785e854fSJed Brown   ierr    = PetscMalloc1((nrecvs2+1),&starts3);CHKERRQ(ierr);
939785e854fSJed Brown   ierr    = PetscMalloc1((nrecvs2+1),&recv_waits);CHKERRQ(ierr);
94030dcb7c9SBarry Smith   for (i=0; i<nrecvs2; i++) {
941d44834fbSBarry Smith     ierr = MPI_Irecv(&lens2[i],1,MPIU_INT,dest[i],tag2,comm,recv_waits+i);CHKERRQ(ierr);
94230dcb7c9SBarry Smith   }
943d44834fbSBarry Smith 
9448a8e0b3aSBarry Smith   /* send the message lengths */
9458a8e0b3aSBarry Smith   for (i=0; i<nsends2; i++) {
9468a8e0b3aSBarry Smith     ierr = MPI_Send(&nprocs[i],1,MPIU_INT,source[i],tag2,comm);CHKERRQ(ierr);
9478a8e0b3aSBarry Smith   }
9488a8e0b3aSBarry Smith 
949d44834fbSBarry Smith   /* wait on receives of lens */
9500c468ba9SBarry Smith   if (nrecvs2) {
951785e854fSJed Brown     ierr = PetscMalloc1(nrecvs2,&recv_statuses);CHKERRQ(ierr);
952d44834fbSBarry Smith     ierr = MPI_Waitall(nrecvs2,recv_waits,recv_statuses);CHKERRQ(ierr);
953d44834fbSBarry Smith     ierr = PetscFree(recv_statuses);CHKERRQ(ierr);
9540c468ba9SBarry Smith   }
955a2ea699eSBarry Smith   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
956d44834fbSBarry Smith 
95730dcb7c9SBarry Smith   starts3[0] = 0;
958d44834fbSBarry Smith   nt         = 0;
95930dcb7c9SBarry Smith   for (i=0; i<nrecvs2-1; i++) {
96030dcb7c9SBarry Smith     starts3[i+1] = starts3[i] + lens2[i];
961d44834fbSBarry Smith     nt          += lens2[i];
96230dcb7c9SBarry Smith   }
96376466f69SStefano Zampini   if (nrecvs2) nt += lens2[nrecvs2-1];
964d44834fbSBarry Smith 
965785e854fSJed Brown   ierr = PetscMalloc1((nt+1),&recvs2);CHKERRQ(ierr);
966785e854fSJed Brown   ierr = PetscMalloc1((nrecvs2+1),&recv_waits);CHKERRQ(ierr);
96752b72c4aSBarry Smith   for (i=0; i<nrecvs2; i++) {
96832dcc486SBarry Smith     ierr = MPI_Irecv(recvs2+starts3[i],lens2[i],MPIU_INT,dest[i],tag3,comm,recv_waits+i);CHKERRQ(ierr);
96930dcb7c9SBarry Smith   }
97030dcb7c9SBarry Smith 
97130dcb7c9SBarry Smith   /* send the messages */
972785e854fSJed Brown   ierr = PetscMalloc1((nsends2+1),&send_waits);CHKERRQ(ierr);
97330dcb7c9SBarry Smith   for (i=0; i<nsends2; i++) {
97432dcc486SBarry Smith     ierr = MPI_Isend(sends2+starts2[i],nprocs[i],MPIU_INT,source[i],tag3,comm,send_waits+i);CHKERRQ(ierr);
97530dcb7c9SBarry Smith   }
97630dcb7c9SBarry Smith 
97730dcb7c9SBarry Smith   /* wait on receives */
9780c468ba9SBarry Smith   if (nrecvs2) {
979785e854fSJed Brown     ierr = PetscMalloc1(nrecvs2,&recv_statuses);CHKERRQ(ierr);
98030dcb7c9SBarry Smith     ierr = MPI_Waitall(nrecvs2,recv_waits,recv_statuses);CHKERRQ(ierr);
98130dcb7c9SBarry Smith     ierr = PetscFree(recv_statuses);CHKERRQ(ierr);
9820c468ba9SBarry Smith   }
98330dcb7c9SBarry Smith   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
98430dcb7c9SBarry Smith   ierr = PetscFree(nprocs);CHKERRQ(ierr);
98530dcb7c9SBarry Smith 
98607b52d57SBarry Smith   if (debug) { /* -----------------------------------  */
98730dcb7c9SBarry Smith     cnt = 0;
98830dcb7c9SBarry Smith     for (i=0; i<nrecvs2; i++) {
98930dcb7c9SBarry Smith       nt = recvs2[cnt++];
99030dcb7c9SBarry Smith       for (j=0; j<nt; j++) {
9917904a332SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"[%d] local node %D number of subdomains %D: ",rank,recvs2[cnt],recvs2[cnt+1]);CHKERRQ(ierr);
99230dcb7c9SBarry Smith         for (k=0; k<recvs2[cnt+1]; k++) {
9937904a332SBarry Smith           ierr = PetscSynchronizedPrintf(comm,"%D ",recvs2[cnt+2+k]);CHKERRQ(ierr);
99430dcb7c9SBarry Smith         }
99530dcb7c9SBarry Smith         cnt += 2 + recvs2[cnt+1];
99630dcb7c9SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"\n");CHKERRQ(ierr);
99730dcb7c9SBarry Smith       }
99830dcb7c9SBarry Smith     }
9990ec8b6e3SBarry Smith     ierr = PetscSynchronizedFlush(comm,PETSC_STDOUT);CHKERRQ(ierr);
100007b52d57SBarry Smith   } /* -----------------------------------  */
100130dcb7c9SBarry Smith 
100230dcb7c9SBarry Smith   /* count number subdomains for each local node */
1003785e854fSJed Brown   ierr = PetscMalloc1(size,&nprocs);CHKERRQ(ierr);
100432dcc486SBarry Smith   ierr = PetscMemzero(nprocs,size*sizeof(PetscInt));CHKERRQ(ierr);
100530dcb7c9SBarry Smith   cnt  = 0;
100630dcb7c9SBarry Smith   for (i=0; i<nrecvs2; i++) {
100730dcb7c9SBarry Smith     nt = recvs2[cnt++];
100830dcb7c9SBarry Smith     for (j=0; j<nt; j++) {
1009f6e5521dSKarl Rupp       for (k=0; k<recvs2[cnt+1]; k++) nprocs[recvs2[cnt+2+k]]++;
101030dcb7c9SBarry Smith       cnt += 2 + recvs2[cnt+1];
101130dcb7c9SBarry Smith     }
101230dcb7c9SBarry Smith   }
101330dcb7c9SBarry Smith   nt = 0; for (i=0; i<size; i++) nt += (nprocs[i] > 0);
101430dcb7c9SBarry Smith   *nproc    = nt;
1015785e854fSJed Brown   ierr = PetscMalloc1((nt+1),procs);CHKERRQ(ierr);
1016785e854fSJed Brown   ierr = PetscMalloc1((nt+1),numprocs);CHKERRQ(ierr);
1017785e854fSJed Brown   ierr = PetscMalloc1((nt+1),indices);CHKERRQ(ierr);
10180298fd71SBarry Smith   for (i=0;i<nt+1;i++) (*indices)[i]=NULL;
1019785e854fSJed Brown   ierr = PetscMalloc1(size,&bprocs);CHKERRQ(ierr);
102030dcb7c9SBarry Smith   cnt       = 0;
102130dcb7c9SBarry Smith   for (i=0; i<size; i++) {
102230dcb7c9SBarry Smith     if (nprocs[i] > 0) {
102330dcb7c9SBarry Smith       bprocs[i]        = cnt;
102430dcb7c9SBarry Smith       (*procs)[cnt]    = i;
102530dcb7c9SBarry Smith       (*numprocs)[cnt] = nprocs[i];
1026785e854fSJed Brown       ierr             = PetscMalloc1(nprocs[i],&(*indices)[cnt]);CHKERRQ(ierr);
102730dcb7c9SBarry Smith       cnt++;
102830dcb7c9SBarry Smith     }
102930dcb7c9SBarry Smith   }
103030dcb7c9SBarry Smith 
103130dcb7c9SBarry Smith   /* make the list of subdomains for each nontrivial local node */
103232dcc486SBarry Smith   ierr = PetscMemzero(*numprocs,nt*sizeof(PetscInt));CHKERRQ(ierr);
103330dcb7c9SBarry Smith   cnt  = 0;
103430dcb7c9SBarry Smith   for (i=0; i<nrecvs2; i++) {
103530dcb7c9SBarry Smith     nt = recvs2[cnt++];
103630dcb7c9SBarry Smith     for (j=0; j<nt; j++) {
1037f6e5521dSKarl Rupp       for (k=0; k<recvs2[cnt+1]; k++) (*indices)[bprocs[recvs2[cnt+2+k]]][(*numprocs)[bprocs[recvs2[cnt+2+k]]]++] = recvs2[cnt];
103830dcb7c9SBarry Smith       cnt += 2 + recvs2[cnt+1];
103930dcb7c9SBarry Smith     }
104030dcb7c9SBarry Smith   }
104130dcb7c9SBarry Smith   ierr = PetscFree(bprocs);CHKERRQ(ierr);
104207b52d57SBarry Smith   ierr = PetscFree(recvs2);CHKERRQ(ierr);
104330dcb7c9SBarry Smith 
104407b52d57SBarry Smith   /* sort the node indexing by their global numbers */
104507b52d57SBarry Smith   nt = *nproc;
104607b52d57SBarry Smith   for (i=0; i<nt; i++) {
1047785e854fSJed Brown     ierr = PetscMalloc1(((*numprocs)[i]),&tmp);CHKERRQ(ierr);
1048f6e5521dSKarl Rupp     for (j=0; j<(*numprocs)[i]; j++) tmp[j] = lindices[(*indices)[i][j]];
104907b52d57SBarry Smith     ierr = PetscSortIntWithArray((*numprocs)[i],tmp,(*indices)[i]);CHKERRQ(ierr);
105007b52d57SBarry Smith     ierr = PetscFree(tmp);CHKERRQ(ierr);
105107b52d57SBarry Smith   }
105207b52d57SBarry Smith 
105307b52d57SBarry Smith   if (debug) { /* -----------------------------------  */
105430dcb7c9SBarry Smith     nt = *nproc;
105530dcb7c9SBarry Smith     for (i=0; i<nt; i++) {
10567904a332SBarry Smith       ierr = PetscSynchronizedPrintf(comm,"[%d] subdomain %D number of indices %D: ",rank,(*procs)[i],(*numprocs)[i]);CHKERRQ(ierr);
105730dcb7c9SBarry Smith       for (j=0; j<(*numprocs)[i]; j++) {
10587904a332SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"%D ",(*indices)[i][j]);CHKERRQ(ierr);
105930dcb7c9SBarry Smith       }
106030dcb7c9SBarry Smith       ierr = PetscSynchronizedPrintf(comm,"\n");CHKERRQ(ierr);
106130dcb7c9SBarry Smith     }
10620ec8b6e3SBarry Smith     ierr = PetscSynchronizedFlush(comm,PETSC_STDOUT);CHKERRQ(ierr);
106307b52d57SBarry Smith   } /* -----------------------------------  */
106430dcb7c9SBarry Smith 
106530dcb7c9SBarry Smith   /* wait on sends */
106630dcb7c9SBarry Smith   if (nsends2) {
1067785e854fSJed Brown     ierr = PetscMalloc1(nsends2,&send_status);CHKERRQ(ierr);
106830dcb7c9SBarry Smith     ierr = MPI_Waitall(nsends2,send_waits,send_status);CHKERRQ(ierr);
106930dcb7c9SBarry Smith     ierr = PetscFree(send_status);CHKERRQ(ierr);
107030dcb7c9SBarry Smith   }
107130dcb7c9SBarry Smith 
107230dcb7c9SBarry Smith   ierr = PetscFree(starts3);CHKERRQ(ierr);
107330dcb7c9SBarry Smith   ierr = PetscFree(dest);CHKERRQ(ierr);
107430dcb7c9SBarry Smith   ierr = PetscFree(send_waits);CHKERRQ(ierr);
10753677ff5aSBarry Smith 
1076bc8ff85bSBarry Smith   ierr = PetscFree(nownedsenders);CHKERRQ(ierr);
1077bc8ff85bSBarry Smith   ierr = PetscFree(ownedsenders);CHKERRQ(ierr);
1078bc8ff85bSBarry Smith   ierr = PetscFree(starts);CHKERRQ(ierr);
107930dcb7c9SBarry Smith   ierr = PetscFree(starts2);CHKERRQ(ierr);
108030dcb7c9SBarry Smith   ierr = PetscFree(lens2);CHKERRQ(ierr);
108189d82c54SBarry Smith 
108289d82c54SBarry Smith   ierr = PetscFree(source);CHKERRQ(ierr);
108397f1f81fSBarry Smith   ierr = PetscFree(len);CHKERRQ(ierr);
108489d82c54SBarry Smith   ierr = PetscFree(recvs);CHKERRQ(ierr);
10853a96401aSBarry Smith   ierr = PetscFree(nprocs);CHKERRQ(ierr);
108630dcb7c9SBarry Smith   ierr = PetscFree(sends2);CHKERRQ(ierr);
108724cf384cSBarry Smith 
108824cf384cSBarry Smith   /* put the information about myself as the first entry in the list */
108924cf384cSBarry Smith   first_procs    = (*procs)[0];
109024cf384cSBarry Smith   first_numprocs = (*numprocs)[0];
109124cf384cSBarry Smith   first_indices  = (*indices)[0];
109224cf384cSBarry Smith   for (i=0; i<*nproc; i++) {
109324cf384cSBarry Smith     if ((*procs)[i] == rank) {
109424cf384cSBarry Smith       (*procs)[0]    = (*procs)[i];
109524cf384cSBarry Smith       (*numprocs)[0] = (*numprocs)[i];
109624cf384cSBarry Smith       (*indices)[0]  = (*indices)[i];
109724cf384cSBarry Smith       (*procs)[i]    = first_procs;
109824cf384cSBarry Smith       (*numprocs)[i] = first_numprocs;
109924cf384cSBarry Smith       (*indices)[i]  = first_indices;
110024cf384cSBarry Smith       break;
110124cf384cSBarry Smith     }
110224cf384cSBarry Smith   }
110389d82c54SBarry Smith   PetscFunctionReturn(0);
110489d82c54SBarry Smith }
110589d82c54SBarry Smith 
11064a2ae208SSatish Balay #undef __FUNCT__
1107*6a818285SBarry Smith #define __FUNCT__ "ISLocalToGlobalMappingRestoreBlockInfo"
1108*6a818285SBarry Smith /*@C
1109*6a818285SBarry Smith     ISLocalToGlobalMappingRestoreBlockInfo - Frees the memory allocated by ISLocalToGlobalMappingGetBlockInfo()
1110*6a818285SBarry Smith 
1111*6a818285SBarry Smith     Collective on ISLocalToGlobalMapping
1112*6a818285SBarry Smith 
1113*6a818285SBarry Smith     Input Parameters:
1114*6a818285SBarry Smith .   mapping - the mapping from local to global indexing
1115*6a818285SBarry Smith 
1116*6a818285SBarry Smith     Output Parameter:
1117*6a818285SBarry Smith +   nproc - number of processors that are connected to this one
1118*6a818285SBarry Smith .   proc - neighboring processors
1119*6a818285SBarry Smith .   numproc - number of indices for each processor
1120*6a818285SBarry Smith -   indices - indices of local nodes shared with neighbor (sorted by global numbering)
1121*6a818285SBarry Smith 
1122*6a818285SBarry Smith     Level: advanced
1123*6a818285SBarry Smith 
1124*6a818285SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreate(),
1125*6a818285SBarry Smith           ISLocalToGlobalMappingGetInfo()
1126*6a818285SBarry Smith @*/
1127*6a818285SBarry Smith PetscErrorCode  ISLocalToGlobalMappingRestoreBlockInfo(ISLocalToGlobalMapping mapping,PetscInt *nproc,PetscInt *procs[],PetscInt *numprocs[],PetscInt **indices[])
1128*6a818285SBarry Smith {
1129*6a818285SBarry Smith   PetscErrorCode ierr;
1130*6a818285SBarry Smith   PetscInt       i;
1131*6a818285SBarry Smith 
1132*6a818285SBarry Smith   PetscFunctionBegin;
1133*6a818285SBarry Smith   ierr = PetscFree(*procs);CHKERRQ(ierr);
1134*6a818285SBarry Smith   ierr = PetscFree(*numprocs);CHKERRQ(ierr);
1135*6a818285SBarry Smith   if (*indices) {
1136*6a818285SBarry Smith     ierr = PetscFree((*indices)[0]);CHKERRQ(ierr);
1137*6a818285SBarry Smith     for (i=1; i<*nproc; i++) {
1138*6a818285SBarry Smith       ierr = PetscFree((*indices)[i]);CHKERRQ(ierr);
1139*6a818285SBarry Smith     }
1140*6a818285SBarry Smith     ierr = PetscFree(*indices);CHKERRQ(ierr);
1141*6a818285SBarry Smith   }
1142*6a818285SBarry Smith   PetscFunctionReturn(0);
1143*6a818285SBarry Smith }
1144*6a818285SBarry Smith 
1145*6a818285SBarry Smith #undef __FUNCT__
1146*6a818285SBarry Smith #define __FUNCT__ "ISLocalToGlobalMappingGetInfo"
1147*6a818285SBarry Smith /*@C
1148*6a818285SBarry Smith     ISLocalToGlobalMappingGetInfo - Gets the neighbor information for each processor and
1149*6a818285SBarry Smith      each index shared by more than one processor
1150*6a818285SBarry Smith 
1151*6a818285SBarry Smith     Collective on ISLocalToGlobalMapping
1152*6a818285SBarry Smith 
1153*6a818285SBarry Smith     Input Parameters:
1154*6a818285SBarry Smith .   mapping - the mapping from local to global indexing
1155*6a818285SBarry Smith 
1156*6a818285SBarry Smith     Output Parameter:
1157*6a818285SBarry Smith +   nproc - number of processors that are connected to this one
1158*6a818285SBarry Smith .   proc - neighboring processors
1159*6a818285SBarry Smith .   numproc - number of indices for each subdomain (processor)
1160*6a818285SBarry Smith -   indices - indices of nodes (in local numbering) shared with neighbors (sorted by global numbering)
1161*6a818285SBarry Smith 
1162*6a818285SBarry Smith     Level: advanced
1163*6a818285SBarry Smith 
1164*6a818285SBarry Smith     Concepts: mapping^local to global
1165*6a818285SBarry Smith 
1166*6a818285SBarry Smith     Fortran Usage:
1167*6a818285SBarry Smith $        ISLocalToGlobalMpngGetInfoSize(ISLocalToGlobalMapping,PetscInt nproc,PetscInt numprocmax,ierr) followed by
1168*6a818285SBarry Smith $        ISLocalToGlobalMappingGetInfo(ISLocalToGlobalMapping,PetscInt nproc, PetscInt procs[nproc],PetscInt numprocs[nproc],
1169*6a818285SBarry Smith           PetscInt indices[nproc][numprocmax],ierr)
1170*6a818285SBarry Smith         There is no ISLocalToGlobalMappingRestoreInfo() in Fortran. You must make sure that procs[], numprocs[] and
1171*6a818285SBarry Smith         indices[][] are large enough arrays, either by allocating them dynamically or defining static ones large enough.
1172*6a818285SBarry Smith 
1173*6a818285SBarry Smith 
1174*6a818285SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreate(),
1175*6a818285SBarry Smith           ISLocalToGlobalMappingRestoreInfo()
1176*6a818285SBarry Smith @*/
1177*6a818285SBarry Smith PetscErrorCode  ISLocalToGlobalMappingGetInfo(ISLocalToGlobalMapping mapping,PetscInt *nproc,PetscInt *procs[],PetscInt *numprocs[],PetscInt **indices[])
1178*6a818285SBarry Smith {
1179*6a818285SBarry Smith   PetscErrorCode ierr;
1180*6a818285SBarry Smith   PetscInt       **bindices = NULL,bs = mapping->bs,i,j,k;
1181*6a818285SBarry Smith 
1182*6a818285SBarry Smith   PetscFunctionBegin;
1183*6a818285SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
1184*6a818285SBarry Smith   ierr = ISLocalToGlobalMappingGetBlockInfo(mapping,nproc,procs,numprocs,&bindices);CHKERRQ(ierr);
1185*6a818285SBarry Smith   ierr = PetscCalloc1(1+*nproc,&*indices);CHKERRQ(ierr);
1186*6a818285SBarry Smith   for (i=0; i<*nproc; i++) {
1187*6a818285SBarry Smith     ierr = PetscMalloc1(bs*(*numprocs)[i],&(*indices)[i]);CHKERRQ(ierr);
1188*6a818285SBarry Smith     for (j=0; j<(*numprocs)[i]; j++) {
1189*6a818285SBarry Smith       for (k=0; k<bs; k++) {
1190*6a818285SBarry Smith         (*indices)[i][j*bs+k] = bs*bindices[i][j] + k;
1191*6a818285SBarry Smith       }
1192*6a818285SBarry Smith     }
1193*6a818285SBarry Smith     (*numprocs)[i] *= bs;
1194*6a818285SBarry Smith   }
1195*6a818285SBarry Smith   if (bindices) {
1196*6a818285SBarry Smith     ierr = PetscFree(bindices[0]);CHKERRQ(ierr);
1197*6a818285SBarry Smith     for (i=1; i<*nproc; i++) {
1198*6a818285SBarry Smith       ierr = PetscFree(bindices[i]);CHKERRQ(ierr);
1199*6a818285SBarry Smith     }
1200*6a818285SBarry Smith     ierr = PetscFree(bindices);CHKERRQ(ierr);
1201*6a818285SBarry Smith   }
1202*6a818285SBarry Smith   PetscFunctionReturn(0);
1203*6a818285SBarry Smith }
1204*6a818285SBarry Smith 
1205*6a818285SBarry Smith #undef __FUNCT__
12064a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingRestoreInfo"
120707b52d57SBarry Smith /*@C
120807b52d57SBarry Smith     ISLocalToGlobalMappingRestoreInfo - Frees the memory allocated by ISLocalToGlobalMappingGetInfo()
120989d82c54SBarry Smith 
121007b52d57SBarry Smith     Collective on ISLocalToGlobalMapping
121107b52d57SBarry Smith 
121207b52d57SBarry Smith     Input Parameters:
121307b52d57SBarry Smith .   mapping - the mapping from local to global indexing
121407b52d57SBarry Smith 
121507b52d57SBarry Smith     Output Parameter:
121607b52d57SBarry Smith +   nproc - number of processors that are connected to this one
121707b52d57SBarry Smith .   proc - neighboring processors
121807b52d57SBarry Smith .   numproc - number of indices for each processor
121907b52d57SBarry Smith -   indices - indices of local nodes shared with neighbor (sorted by global numbering)
122007b52d57SBarry Smith 
122107b52d57SBarry Smith     Level: advanced
122207b52d57SBarry Smith 
122307b52d57SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreate(),
122407b52d57SBarry Smith           ISLocalToGlobalMappingGetInfo()
122507b52d57SBarry Smith @*/
12267087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingRestoreInfo(ISLocalToGlobalMapping mapping,PetscInt *nproc,PetscInt *procs[],PetscInt *numprocs[],PetscInt **indices[])
122707b52d57SBarry Smith {
12286849ba73SBarry Smith   PetscErrorCode ierr;
122907b52d57SBarry Smith 
123007b52d57SBarry Smith   PetscFunctionBegin;
1231*6a818285SBarry Smith   ierr = ISLocalToGlobalMappingRestoreBlockInfo(mapping,nproc,procs,numprocs,indices);CHKERRQ(ierr);
123207b52d57SBarry Smith   PetscFunctionReturn(0);
123307b52d57SBarry Smith }
123486994e45SJed Brown 
123586994e45SJed Brown #undef __FUNCT__
123686994e45SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingGetIndices"
123786994e45SJed Brown /*@C
1238107e9a97SBarry Smith    ISLocalToGlobalMappingGetIndices - Get global indices for every local point that is mapped
123986994e45SJed Brown 
124086994e45SJed Brown    Not Collective
124186994e45SJed Brown 
124286994e45SJed Brown    Input Arguments:
124386994e45SJed Brown . ltog - local to global mapping
124486994e45SJed Brown 
124586994e45SJed Brown    Output Arguments:
1246565245c5SBarry Smith . array - array of indices, the length of this array may be obtained with ISLocalToGlobalMappingGetSize()
124786994e45SJed Brown 
124886994e45SJed Brown    Level: advanced
124986994e45SJed Brown 
1250107e9a97SBarry Smith    Notes: ISLocalToGlobalMappingGetSize() returns the length the this array
1251107e9a97SBarry Smith 
1252107e9a97SBarry Smith .seealso: ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingRestoreIndices(), ISLocalToGlobalMappingGetBlockIndices(), ISLocalToGlobalMappingRestoreBlockIndices()
125386994e45SJed Brown @*/
12547087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingGetIndices(ISLocalToGlobalMapping ltog,const PetscInt **array)
125586994e45SJed Brown {
125686994e45SJed Brown   PetscFunctionBegin;
125786994e45SJed Brown   PetscValidHeaderSpecific(ltog,IS_LTOGM_CLASSID,1);
125886994e45SJed Brown   PetscValidPointer(array,2);
125945b6f7e9SBarry Smith   if (ltog->bs == 1) {
126086994e45SJed Brown     *array = ltog->indices;
126145b6f7e9SBarry Smith   } else {
126245b6f7e9SBarry Smith     PetscInt       *jj,k,i,j,n = ltog->n, bs = ltog->bs;
126345b6f7e9SBarry Smith     const PetscInt *ii;
126445b6f7e9SBarry Smith     PetscErrorCode ierr;
126545b6f7e9SBarry Smith 
126645b6f7e9SBarry Smith     ierr = PetscMalloc1(bs*n,&jj);CHKERRQ(ierr);
126745b6f7e9SBarry Smith     *array = jj;
126845b6f7e9SBarry Smith     k    = 0;
126945b6f7e9SBarry Smith     ii   = ltog->indices;
127045b6f7e9SBarry Smith     for (i=0; i<n; i++)
127145b6f7e9SBarry Smith       for (j=0; j<bs; j++)
127245b6f7e9SBarry Smith         jj[k++] = bs*ii[i] + j;
127345b6f7e9SBarry Smith   }
127486994e45SJed Brown   PetscFunctionReturn(0);
127586994e45SJed Brown }
127686994e45SJed Brown 
127786994e45SJed Brown #undef __FUNCT__
127886994e45SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingRestoreIndices"
127986994e45SJed Brown /*@C
128086994e45SJed Brown    ISLocalToGlobalMappingRestoreIndices - Restore indices obtained with ISLocalToGlobalMappingRestoreIndices()
128186994e45SJed Brown 
128286994e45SJed Brown    Not Collective
128386994e45SJed Brown 
128486994e45SJed Brown    Input Arguments:
128586994e45SJed Brown + ltog - local to global mapping
128686994e45SJed Brown - array - array of indices
128786994e45SJed Brown 
128886994e45SJed Brown    Level: advanced
128986994e45SJed Brown 
129086994e45SJed Brown .seealso: ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingGetIndices()
129186994e45SJed Brown @*/
12927087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingRestoreIndices(ISLocalToGlobalMapping ltog,const PetscInt **array)
129386994e45SJed Brown {
129486994e45SJed Brown   PetscFunctionBegin;
129586994e45SJed Brown   PetscValidHeaderSpecific(ltog,IS_LTOGM_CLASSID,1);
129686994e45SJed Brown   PetscValidPointer(array,2);
129745b6f7e9SBarry Smith   if (ltog->bs == 1 && *array != ltog->indices) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_BADPTR,"Trying to return mismatched pointer");
129845b6f7e9SBarry Smith 
129945b6f7e9SBarry Smith   if (ltog->bs > 1) {
130045b6f7e9SBarry Smith     PetscErrorCode ierr;
130145b6f7e9SBarry Smith     ierr = PetscFree(*(void**)array);CHKERRQ(ierr);
130245b6f7e9SBarry Smith   }
130345b6f7e9SBarry Smith   PetscFunctionReturn(0);
130445b6f7e9SBarry Smith }
130545b6f7e9SBarry Smith 
130645b6f7e9SBarry Smith #undef __FUNCT__
130745b6f7e9SBarry Smith #define __FUNCT__ "ISLocalToGlobalMappingGetBlockIndices"
130845b6f7e9SBarry Smith /*@C
130945b6f7e9SBarry Smith    ISLocalToGlobalMappingGetBlockIndices - Get global indices for every local block
131045b6f7e9SBarry Smith 
131145b6f7e9SBarry Smith    Not Collective
131245b6f7e9SBarry Smith 
131345b6f7e9SBarry Smith    Input Arguments:
131445b6f7e9SBarry Smith . ltog - local to global mapping
131545b6f7e9SBarry Smith 
131645b6f7e9SBarry Smith    Output Arguments:
131745b6f7e9SBarry Smith . array - array of indices
131845b6f7e9SBarry Smith 
131945b6f7e9SBarry Smith    Level: advanced
132045b6f7e9SBarry Smith 
132145b6f7e9SBarry Smith .seealso: ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingRestoreBlockIndices()
132245b6f7e9SBarry Smith @*/
132345b6f7e9SBarry Smith PetscErrorCode  ISLocalToGlobalMappingGetBlockIndices(ISLocalToGlobalMapping ltog,const PetscInt **array)
132445b6f7e9SBarry Smith {
132545b6f7e9SBarry Smith   PetscFunctionBegin;
132645b6f7e9SBarry Smith   PetscValidHeaderSpecific(ltog,IS_LTOGM_CLASSID,1);
132745b6f7e9SBarry Smith   PetscValidPointer(array,2);
132845b6f7e9SBarry Smith   *array = ltog->indices;
132945b6f7e9SBarry Smith   PetscFunctionReturn(0);
133045b6f7e9SBarry Smith }
133145b6f7e9SBarry Smith 
133245b6f7e9SBarry Smith #undef __FUNCT__
133345b6f7e9SBarry Smith #define __FUNCT__ "ISLocalToGlobalMappingRestoreBlockIndices"
133445b6f7e9SBarry Smith /*@C
133545b6f7e9SBarry Smith    ISLocalToGlobalMappingRestoreBlockIndices - Restore indices obtained with ISLocalToGlobalMappingGetBlockIndices()
133645b6f7e9SBarry Smith 
133745b6f7e9SBarry Smith    Not Collective
133845b6f7e9SBarry Smith 
133945b6f7e9SBarry Smith    Input Arguments:
134045b6f7e9SBarry Smith + ltog - local to global mapping
134145b6f7e9SBarry Smith - array - array of indices
134245b6f7e9SBarry Smith 
134345b6f7e9SBarry Smith    Level: advanced
134445b6f7e9SBarry Smith 
134545b6f7e9SBarry Smith .seealso: ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingGetIndices()
134645b6f7e9SBarry Smith @*/
134745b6f7e9SBarry Smith PetscErrorCode  ISLocalToGlobalMappingRestoreBlockIndices(ISLocalToGlobalMapping ltog,const PetscInt **array)
134845b6f7e9SBarry Smith {
134945b6f7e9SBarry Smith   PetscFunctionBegin;
135045b6f7e9SBarry Smith   PetscValidHeaderSpecific(ltog,IS_LTOGM_CLASSID,1);
135145b6f7e9SBarry Smith   PetscValidPointer(array,2);
135286994e45SJed Brown   if (*array != ltog->indices) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_BADPTR,"Trying to return mismatched pointer");
13530298fd71SBarry Smith   *array = NULL;
135486994e45SJed Brown   PetscFunctionReturn(0);
135586994e45SJed Brown }
1356f7efa3c7SJed Brown 
1357f7efa3c7SJed Brown #undef __FUNCT__
1358f7efa3c7SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingConcatenate"
1359f7efa3c7SJed Brown /*@C
1360f7efa3c7SJed Brown    ISLocalToGlobalMappingConcatenate - Create a new mapping that concatenates a list of mappings
1361f7efa3c7SJed Brown 
1362f7efa3c7SJed Brown    Not Collective
1363f7efa3c7SJed Brown 
1364f7efa3c7SJed Brown    Input Arguments:
1365f7efa3c7SJed Brown + comm - communicator for the new mapping, must contain the communicator of every mapping to concatenate
1366f7efa3c7SJed Brown . n - number of mappings to concatenate
1367f7efa3c7SJed Brown - ltogs - local to global mappings
1368f7efa3c7SJed Brown 
1369f7efa3c7SJed Brown    Output Arguments:
1370f7efa3c7SJed Brown . ltogcat - new mapping
1371f7efa3c7SJed Brown 
13729d90f715SBarry Smith    Note: this currently always returns a mapping with block size of 1
13739d90f715SBarry Smith 
13749d90f715SBarry Smith    Developer Note: If all the input mapping have the same block size we could easily handle that as a special case
13759d90f715SBarry Smith 
1376f7efa3c7SJed Brown    Level: advanced
1377f7efa3c7SJed Brown 
1378f7efa3c7SJed Brown .seealso: ISLocalToGlobalMappingCreate()
1379f7efa3c7SJed Brown @*/
1380f7efa3c7SJed Brown PetscErrorCode ISLocalToGlobalMappingConcatenate(MPI_Comm comm,PetscInt n,const ISLocalToGlobalMapping ltogs[],ISLocalToGlobalMapping *ltogcat)
1381f7efa3c7SJed Brown {
1382f7efa3c7SJed Brown   PetscInt       i,cnt,m,*idx;
1383f7efa3c7SJed Brown   PetscErrorCode ierr;
1384f7efa3c7SJed Brown 
1385f7efa3c7SJed Brown   PetscFunctionBegin;
1386f7efa3c7SJed Brown   if (n < 0) SETERRQ1(comm,PETSC_ERR_ARG_OUTOFRANGE,"Must have a non-negative number of mappings, given %D",n);
1387f7efa3c7SJed Brown   if (n > 0) PetscValidPointer(ltogs,3);
1388f7efa3c7SJed Brown   for (i=0; i<n; i++) PetscValidHeaderSpecific(ltogs[i],IS_LTOGM_CLASSID,3);
1389f7efa3c7SJed Brown   PetscValidPointer(ltogcat,4);
1390f7efa3c7SJed Brown   for (cnt=0,i=0; i<n; i++) {
1391f7efa3c7SJed Brown     ierr = ISLocalToGlobalMappingGetSize(ltogs[i],&m);CHKERRQ(ierr);
1392f7efa3c7SJed Brown     cnt += m;
1393f7efa3c7SJed Brown   }
1394785e854fSJed Brown   ierr = PetscMalloc1(cnt,&idx);CHKERRQ(ierr);
1395f7efa3c7SJed Brown   for (cnt=0,i=0; i<n; i++) {
1396f7efa3c7SJed Brown     const PetscInt *subidx;
1397f7efa3c7SJed Brown     ierr = ISLocalToGlobalMappingGetSize(ltogs[i],&m);CHKERRQ(ierr);
1398f7efa3c7SJed Brown     ierr = ISLocalToGlobalMappingGetIndices(ltogs[i],&subidx);CHKERRQ(ierr);
1399f7efa3c7SJed Brown     ierr = PetscMemcpy(&idx[cnt],subidx,m*sizeof(PetscInt));CHKERRQ(ierr);
1400f7efa3c7SJed Brown     ierr = ISLocalToGlobalMappingRestoreIndices(ltogs[i],&subidx);CHKERRQ(ierr);
1401f7efa3c7SJed Brown     cnt += m;
1402f7efa3c7SJed Brown   }
1403f0413b6fSBarry Smith   ierr = ISLocalToGlobalMappingCreate(comm,1,cnt,idx,PETSC_OWN_POINTER,ltogcat);CHKERRQ(ierr);
1404f7efa3c7SJed Brown   PetscFunctionReturn(0);
1405f7efa3c7SJed Brown }
140604a59952SBarry Smith 
140704a59952SBarry Smith 
1408