xref: /petsc/src/vec/is/utils/isltog.c (revision c6db04a5321582041def2b1e244c75985478b3ef)
12362add9SBarry Smith 
2*c6db04a5SJed Brown #include <petscvec.h>   /*I "petscvec.h" I*/
3*c6db04a5SJed Brown #include <private/isimpl.h>    /*I "petscis.h"  I*/
42362add9SBarry Smith 
57087cfbeSBarry Smith PetscClassId  IS_LTOGM_CLASSID;
68e58c17dSMatthew Knepley 
74a2ae208SSatish Balay #undef __FUNCT__
84a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingGetSize"
93b9aefa3SBarry Smith /*@C
103b9aefa3SBarry Smith     ISLocalToGlobalMappingGetSize - Gets the local size of a local to global mapping.
113b9aefa3SBarry Smith 
123b9aefa3SBarry Smith     Not Collective
133b9aefa3SBarry Smith 
143b9aefa3SBarry Smith     Input Parameter:
153b9aefa3SBarry Smith .   ltog - local to global mapping
163b9aefa3SBarry Smith 
173b9aefa3SBarry Smith     Output Parameter:
183b9aefa3SBarry Smith .   n - the number of entries in the local mapping
193b9aefa3SBarry Smith 
203b9aefa3SBarry Smith     Level: advanced
213b9aefa3SBarry Smith 
22273d9f13SBarry Smith     Concepts: mapping^local to global
233b9aefa3SBarry Smith 
243b9aefa3SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
253b9aefa3SBarry Smith @*/
267087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingGetSize(ISLocalToGlobalMapping mapping,PetscInt *n)
273b9aefa3SBarry Smith {
283b9aefa3SBarry Smith   PetscFunctionBegin;
290700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
304482741eSBarry Smith   PetscValidIntPointer(n,2);
313b9aefa3SBarry Smith   *n = mapping->n;
323b9aefa3SBarry Smith   PetscFunctionReturn(0);
333b9aefa3SBarry Smith }
343b9aefa3SBarry Smith 
354a2ae208SSatish Balay #undef __FUNCT__
364a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingView"
375a5d4f66SBarry Smith /*@C
385a5d4f66SBarry Smith     ISLocalToGlobalMappingView - View a local to global mapping
395a5d4f66SBarry Smith 
40b9cd556bSLois Curfman McInnes     Not Collective
41b9cd556bSLois Curfman McInnes 
425a5d4f66SBarry Smith     Input Parameters:
433b9aefa3SBarry Smith +   ltog - local to global mapping
443b9aefa3SBarry Smith -   viewer - viewer
455a5d4f66SBarry Smith 
46a997ad1aSLois Curfman McInnes     Level: advanced
47a997ad1aSLois Curfman McInnes 
48273d9f13SBarry Smith     Concepts: mapping^local to global
495a5d4f66SBarry Smith 
505a5d4f66SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
515a5d4f66SBarry Smith @*/
527087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingView(ISLocalToGlobalMapping mapping,PetscViewer viewer)
535a5d4f66SBarry Smith {
5432dcc486SBarry Smith   PetscInt        i;
5532dcc486SBarry Smith   PetscMPIInt     rank;
56ace3abfcSBarry Smith   PetscBool       iascii;
576849ba73SBarry Smith   PetscErrorCode  ierr;
585a5d4f66SBarry Smith 
595a5d4f66SBarry Smith   PetscFunctionBegin;
600700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
613050cee2SBarry Smith   if (!viewer) {
627adad957SLisandro Dalcin     ierr = PetscViewerASCIIGetStdout(((PetscObject)mapping)->comm,&viewer);CHKERRQ(ierr);
633050cee2SBarry Smith   }
640700a824SBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
655a5d4f66SBarry Smith 
667adad957SLisandro Dalcin   ierr = MPI_Comm_rank(((PetscObject)mapping)->comm,&rank);CHKERRQ(ierr);
672692d6eeSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
6832077d6dSBarry Smith   if (iascii) {
695a5d4f66SBarry Smith     for (i=0; i<mapping->n; i++) {
70b0a32e0cSBarry Smith       ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %d %d\n",rank,i,mapping->indices[i]);CHKERRQ(ierr);
716831982aSBarry Smith     }
72b0a32e0cSBarry Smith     ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
736831982aSBarry Smith   } else {
74e32f2f54SBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported for ISLocalToGlobalMapping",((PetscObject)viewer)->type_name);
755a5d4f66SBarry Smith   }
765a5d4f66SBarry Smith 
775a5d4f66SBarry Smith   PetscFunctionReturn(0);
785a5d4f66SBarry Smith }
795a5d4f66SBarry Smith 
804a2ae208SSatish Balay #undef __FUNCT__
814a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingCreateIS"
821f428162SBarry Smith /*@
832bdab257SBarry Smith     ISLocalToGlobalMappingCreateIS - Creates a mapping between a local (0 to n)
842bdab257SBarry Smith     ordering and a global parallel ordering.
852bdab257SBarry Smith 
860f5bd95cSBarry Smith     Not collective
87b9cd556bSLois Curfman McInnes 
88a997ad1aSLois Curfman McInnes     Input Parameter:
898c03b21aSDmitry Karpeev .   is - index set containing the global numbers for each local number
902bdab257SBarry Smith 
91a997ad1aSLois Curfman McInnes     Output Parameter:
922bdab257SBarry Smith .   mapping - new mapping data structure
932bdab257SBarry Smith 
94a997ad1aSLois Curfman McInnes     Level: advanced
95a997ad1aSLois Curfman McInnes 
96273d9f13SBarry Smith     Concepts: mapping^local to global
972bdab257SBarry Smith 
982bdab257SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate()
992bdab257SBarry Smith @*/
1007087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingCreateIS(IS is,ISLocalToGlobalMapping *mapping)
1012bdab257SBarry Smith {
1026849ba73SBarry Smith   PetscErrorCode ierr;
1035d0c19d7SBarry Smith   PetscInt       n;
1045d0c19d7SBarry Smith   const PetscInt *indices;
1052bdab257SBarry Smith   MPI_Comm       comm;
1063a40ed3dSBarry Smith 
1073a40ed3dSBarry Smith   PetscFunctionBegin;
1080700a824SBarry Smith   PetscValidHeaderSpecific(is,IS_CLASSID,1);
1094482741eSBarry Smith   PetscValidPointer(mapping,2);
1102bdab257SBarry Smith 
1112bdab257SBarry Smith   ierr = PetscObjectGetComm((PetscObject)is,&comm);CHKERRQ(ierr);
1123b9aefa3SBarry Smith   ierr = ISGetLocalSize(is,&n);CHKERRQ(ierr);
1132bdab257SBarry Smith   ierr = ISGetIndices(is,&indices);CHKERRQ(ierr);
114d5ad8652SBarry Smith   ierr = ISLocalToGlobalMappingCreate(comm,n,indices,PETSC_COPY_VALUES,mapping);CHKERRQ(ierr);
1152bdab257SBarry Smith   ierr = ISRestoreIndices(is,&indices);CHKERRQ(ierr);
1163a40ed3dSBarry Smith   PetscFunctionReturn(0);
1172bdab257SBarry Smith }
1185a5d4f66SBarry Smith 
119b46b645bSBarry Smith 
1204a2ae208SSatish Balay #undef __FUNCT__
1214a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingCreate"
122ba5bb76aSSatish Balay /*@
12390f02eecSBarry Smith     ISLocalToGlobalMappingCreate - Creates a mapping between a local (0 to n)
12490f02eecSBarry Smith     ordering and a global parallel ordering.
1252362add9SBarry Smith 
12689d82c54SBarry Smith     Not Collective, but communicator may have more than one process
127b9cd556bSLois Curfman McInnes 
1282362add9SBarry Smith     Input Parameters:
12989d82c54SBarry Smith +   comm - MPI communicator
13090f02eecSBarry Smith .   n - the number of local elements
131d5ad8652SBarry Smith .   indices - the global index for each local element
132d5ad8652SBarry Smith -   mode - see PetscCopyMode
1332362add9SBarry Smith 
134a997ad1aSLois Curfman McInnes     Output Parameter:
13590f02eecSBarry Smith .   mapping - new mapping data structure
1362362add9SBarry Smith 
137a997ad1aSLois Curfman McInnes     Level: advanced
138a997ad1aSLois Curfman McInnes 
139273d9f13SBarry Smith     Concepts: mapping^local to global
1402362add9SBarry Smith 
141d5ad8652SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS()
1422362add9SBarry Smith @*/
1437087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingCreate(MPI_Comm cm,PetscInt n,const PetscInt indices[],PetscCopyMode mode,ISLocalToGlobalMapping *mapping)
1442362add9SBarry Smith {
1456849ba73SBarry Smith   PetscErrorCode ierr;
14632dcc486SBarry Smith   PetscInt       *in;
147b46b645bSBarry Smith 
148b46b645bSBarry Smith   PetscFunctionBegin;
14973911063SBarry Smith   if (n) PetscValidIntPointer(indices,3);
1504482741eSBarry Smith   PetscValidPointer(mapping,4);
151b46b645bSBarry Smith 
1528e58c17dSMatthew Knepley   *mapping = PETSC_NULL;
1538e58c17dSMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES
1542b6de112SBarry Smith   ierr = ISInitializePackage(PETSC_NULL);CHKERRQ(ierr);
1558e58c17dSMatthew Knepley #endif
1562362add9SBarry Smith 
1570700a824SBarry Smith   ierr = PetscHeaderCreate(*mapping,_p_ISLocalToGlobalMapping,int,IS_LTOGM_CLASSID,0,"ISLocalToGlobalMapping",
15852e6d16bSBarry Smith 			   cm,ISLocalToGlobalMappingDestroy,ISLocalToGlobalMappingView);CHKERRQ(ierr);
159d4bb536fSBarry Smith   (*mapping)->n       = n;
160d4bb536fSBarry Smith   /*
161d4bb536fSBarry Smith     Do not create the global to local mapping. This is only created if
162d4bb536fSBarry Smith     ISGlobalToLocalMapping() is called
163d4bb536fSBarry Smith   */
164d4bb536fSBarry Smith   (*mapping)->globals = 0;
165d5ad8652SBarry Smith   if (mode == PETSC_COPY_VALUES) {
166d5ad8652SBarry Smith     ierr = PetscMalloc(n*sizeof(PetscInt),&in);CHKERRQ(ierr);
167d5ad8652SBarry Smith     ierr = PetscMemcpy(in,indices,n*sizeof(PetscInt));CHKERRQ(ierr);
168d5ad8652SBarry Smith     ierr = PetscLogObjectMemory(*mapping,n*sizeof(PetscInt));CHKERRQ(ierr);
169d5ad8652SBarry Smith     (*mapping)->indices = in;
170d5ad8652SBarry Smith   } else if (mode == PETSC_OWN_POINTER) {
171d5ad8652SBarry Smith     (*mapping)->indices = (PetscInt*)indices;
172d5ad8652SBarry Smith   } else SETERRQ(cm,PETSC_ERR_SUP,"Cannot currently use PETSC_USE_POINTER");
1733a40ed3dSBarry Smith   PetscFunctionReturn(0);
1742362add9SBarry Smith }
1752362add9SBarry Smith 
1764a2ae208SSatish Balay #undef __FUNCT__
1774a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingBlock"
178bce096a4SSatish Balay /*@
179323b833fSBarry Smith     ISLocalToGlobalMappingBlock - Creates a blocked index version of an
180323b833fSBarry Smith        ISLocalToGlobalMapping that is appropriate for MatSetLocalToGlobalMappingBlock()
181323b833fSBarry Smith        and VecSetLocalToGlobalMappingBlock().
182323b833fSBarry Smith 
183323b833fSBarry Smith     Not Collective, but communicator may have more than one process
184323b833fSBarry Smith 
185323b833fSBarry Smith     Input Parameters:
186323b833fSBarry Smith +    inmap - original point-wise mapping
187323b833fSBarry Smith -    bs - block size
188323b833fSBarry Smith 
189323b833fSBarry Smith     Output Parameter:
19069eb54c3SBarry Smith .   outmap - block based mapping; the indices are relative to BLOCKS, not individual vector or matrix entries.
191323b833fSBarry Smith 
192323b833fSBarry Smith     Level: advanced
193323b833fSBarry Smith 
194323b833fSBarry Smith     Concepts: mapping^local to global
195323b833fSBarry Smith 
196323b833fSBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingCreateIS()
197323b833fSBarry Smith @*/
1987087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingBlock(ISLocalToGlobalMapping inmap,PetscInt bs,ISLocalToGlobalMapping *outmap)
199323b833fSBarry Smith {
2006849ba73SBarry Smith   PetscErrorCode ierr;
20132dcc486SBarry Smith   PetscInt       *ii,i,n;
202323b833fSBarry Smith 
203323b833fSBarry Smith   PetscFunctionBegin;
2040700a824SBarry Smith   PetscValidHeaderSpecific(inmap,IS_LTOGM_CLASSID,1);
205b2beed0aSJed Brown   PetscValidPointer(outmap,3);
206323b833fSBarry Smith   if (bs > 1) {
207323b833fSBarry Smith     n    = inmap->n/bs;
208e32f2f54SBarry Smith     if (n*bs != inmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Pointwise mapping length is not divisible by block size");
20932dcc486SBarry Smith     ierr = PetscMalloc(n*sizeof(PetscInt),&ii);CHKERRQ(ierr);
210323b833fSBarry Smith     for (i=0; i<n; i++) {
211db032c9dSBarry Smith       ii[i] = inmap->indices[bs*i]/bs;
212323b833fSBarry Smith     }
213d5ad8652SBarry Smith     ierr = ISLocalToGlobalMappingCreate(((PetscObject)inmap)->comm,n,ii,PETSC_OWN_POINTER,outmap);CHKERRQ(ierr);
214323b833fSBarry Smith   } else {
215323b833fSBarry Smith     ierr    = PetscObjectReference((PetscObject)inmap);CHKERRQ(ierr);
216c3122656SLisandro Dalcin     *outmap = inmap;
217323b833fSBarry Smith   }
218323b833fSBarry Smith   PetscFunctionReturn(0);
219323b833fSBarry Smith }
220323b833fSBarry Smith 
2214a2ae208SSatish Balay #undef __FUNCT__
2228ab951edSJed Brown #define __FUNCT__ "ISLocalToGlobalMappingUnBlock"
223b2beed0aSJed Brown /*@
224b2beed0aSJed Brown     ISLocalToGlobalMappingUnBlock - Creates a scalar index version of a blocked
225b2beed0aSJed Brown        ISLocalToGlobalMapping
226b2beed0aSJed Brown 
227b2beed0aSJed Brown     Not Collective, but communicator may have more than one process
228b2beed0aSJed Brown 
229b2beed0aSJed Brown     Input Parameter:
230b2beed0aSJed Brown + inmap - block based mapping; the indices are relative to BLOCKS, not individual vector or matrix entries.
231b2beed0aSJed Brown - bs - block size
232b2beed0aSJed Brown 
233b2beed0aSJed Brown     Output Parameter:
234b2beed0aSJed Brown .   outmap - pointwise mapping
235b2beed0aSJed Brown 
236b2beed0aSJed Brown     Level: advanced
237b2beed0aSJed Brown 
238b2beed0aSJed Brown     Concepts: mapping^local to global
239b2beed0aSJed Brown 
240b2beed0aSJed Brown .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingBlock()
241b2beed0aSJed Brown @*/
2427087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingUnBlock(ISLocalToGlobalMapping inmap,PetscInt bs,ISLocalToGlobalMapping *outmap)
243b2beed0aSJed Brown {
244b2beed0aSJed Brown   PetscErrorCode ierr;
245b2beed0aSJed Brown   PetscInt       *ii,i,n;
246b2beed0aSJed Brown 
247b2beed0aSJed Brown   PetscFunctionBegin;
248b2beed0aSJed Brown   PetscValidHeaderSpecific(inmap,IS_LTOGM_CLASSID,1);
249b2beed0aSJed Brown   PetscValidPointer(outmap,2);
250b2beed0aSJed Brown   if (bs > 1) {
251b2beed0aSJed Brown     n    = inmap->n*bs;
252b2beed0aSJed Brown     ierr = PetscMalloc(n*sizeof(PetscInt),&ii);CHKERRQ(ierr);
253b2beed0aSJed Brown     for (i=0; i<n; i++) {
25494f4c46bSJed Brown       ii[i] = inmap->indices[i/bs]*bs + (i%bs);
255b2beed0aSJed Brown     }
256b2beed0aSJed Brown     ierr = ISLocalToGlobalMappingCreate(((PetscObject)inmap)->comm,n,ii,PETSC_OWN_POINTER,outmap);CHKERRQ(ierr);
257b2beed0aSJed Brown   } else {
258b2beed0aSJed Brown     ierr    = PetscObjectReference((PetscObject)inmap);CHKERRQ(ierr);
259b2beed0aSJed Brown     *outmap = inmap;
260b2beed0aSJed Brown   }
261b2beed0aSJed Brown   PetscFunctionReturn(0);
262b2beed0aSJed Brown }
263b2beed0aSJed Brown 
264b2beed0aSJed Brown #undef __FUNCT__
2654a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingDestroy"
26690f02eecSBarry Smith /*@
26790f02eecSBarry Smith    ISLocalToGlobalMappingDestroy - Destroys a mapping between a local (0 to n)
26890f02eecSBarry Smith    ordering and a global parallel ordering.
26990f02eecSBarry Smith 
2700f5bd95cSBarry Smith    Note Collective
271b9cd556bSLois Curfman McInnes 
27290f02eecSBarry Smith    Input Parameters:
27390f02eecSBarry Smith .  mapping - mapping data structure
27490f02eecSBarry Smith 
275a997ad1aSLois Curfman McInnes    Level: advanced
276a997ad1aSLois Curfman McInnes 
2773acfe500SLois Curfman McInnes .seealso: ISLocalToGlobalMappingCreate()
27890f02eecSBarry Smith @*/
2797087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingDestroy(ISLocalToGlobalMapping mapping)
28090f02eecSBarry Smith {
281dfbe8321SBarry Smith   PetscErrorCode ierr;
2823a40ed3dSBarry Smith   PetscFunctionBegin;
2830700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
2847adad957SLisandro Dalcin   if (--((PetscObject)mapping)->refct > 0) PetscFunctionReturn(0);
285606d414cSSatish Balay   ierr = PetscFree(mapping->indices);CHKERRQ(ierr);
28605b42c5fSBarry Smith   ierr = PetscFree(mapping->globals);CHKERRQ(ierr);
287d38fa0fbSBarry Smith   ierr = PetscHeaderDestroy(mapping);CHKERRQ(ierr);
2883a40ed3dSBarry Smith   PetscFunctionReturn(0);
28990f02eecSBarry Smith }
29090f02eecSBarry Smith 
2914a2ae208SSatish Balay #undef __FUNCT__
2924a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingApplyIS"
29390f02eecSBarry Smith /*@
2943acfe500SLois Curfman McInnes     ISLocalToGlobalMappingApplyIS - Creates from an IS in the local numbering
2953acfe500SLois Curfman McInnes     a new index set using the global numbering defined in an ISLocalToGlobalMapping
2963acfe500SLois Curfman McInnes     context.
29790f02eecSBarry Smith 
298b9cd556bSLois Curfman McInnes     Not collective
299b9cd556bSLois Curfman McInnes 
30090f02eecSBarry Smith     Input Parameters:
301b9cd556bSLois Curfman McInnes +   mapping - mapping between local and global numbering
302b9cd556bSLois Curfman McInnes -   is - index set in local numbering
30390f02eecSBarry Smith 
30490f02eecSBarry Smith     Output Parameters:
30590f02eecSBarry Smith .   newis - index set in global numbering
30690f02eecSBarry Smith 
307a997ad1aSLois Curfman McInnes     Level: advanced
308a997ad1aSLois Curfman McInnes 
309273d9f13SBarry Smith     Concepts: mapping^local to global
3103acfe500SLois Curfman McInnes 
31190f02eecSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),
312d4bb536fSBarry Smith           ISLocalToGlobalMappingDestroy(), ISGlobalToLocalMappingApply()
31390f02eecSBarry Smith @*/
3147087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingApplyIS(ISLocalToGlobalMapping mapping,IS is,IS *newis)
31590f02eecSBarry Smith {
3166849ba73SBarry Smith   PetscErrorCode ierr;
3175d0c19d7SBarry Smith   PetscInt       n,i,*idxmap,*idxout,Nmax = mapping->n;
3185d0c19d7SBarry Smith   const PetscInt *idxin;
3193a40ed3dSBarry Smith 
3203a40ed3dSBarry Smith   PetscFunctionBegin;
3210700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
3220700a824SBarry Smith   PetscValidHeaderSpecific(is,IS_CLASSID,2);
3234482741eSBarry Smith   PetscValidPointer(newis,3);
32490f02eecSBarry Smith 
3253b9aefa3SBarry Smith   ierr   = ISGetLocalSize(is,&n);CHKERRQ(ierr);
32690f02eecSBarry Smith   ierr   = ISGetIndices(is,&idxin);CHKERRQ(ierr);
32790f02eecSBarry Smith   idxmap = mapping->indices;
32890f02eecSBarry Smith 
3297c334f02SBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&idxout);CHKERRQ(ierr);
33090f02eecSBarry Smith   for (i=0; i<n; i++) {
331e32f2f54SBarry 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);
33290f02eecSBarry Smith     idxout[i] = idxmap[idxin[i]];
33390f02eecSBarry Smith   }
3343b9aefa3SBarry Smith   ierr = ISRestoreIndices(is,&idxin);CHKERRQ(ierr);
33570b3c8c7SBarry Smith   ierr = ISCreateGeneral(PETSC_COMM_SELF,n,idxout,PETSC_OWN_POINTER,newis);CHKERRQ(ierr);
3363a40ed3dSBarry Smith   PetscFunctionReturn(0);
33790f02eecSBarry Smith }
33890f02eecSBarry Smith 
33989d82c54SBarry Smith /*MC
3403acfe500SLois Curfman McInnes    ISLocalToGlobalMappingApply - Takes a list of integers in a local numbering
3413acfe500SLois Curfman McInnes    and converts them to the global numbering.
34290f02eecSBarry Smith 
343eca87e8dSBarry Smith    Synopsis:
344eca87e8dSBarry Smith    PetscErrorCode ISLocalToGlobalMappingApply(ISLocalToGlobalMapping mapping,int N,int in[],int out[])
345eca87e8dSBarry Smith 
346b9cd556bSLois Curfman McInnes    Not collective
347b9cd556bSLois Curfman McInnes 
348bb25748dSBarry Smith    Input Parameters:
349b9cd556bSLois Curfman McInnes +  mapping - the local to global mapping context
350bb25748dSBarry Smith .  N - number of integers
351b9cd556bSLois Curfman McInnes -  in - input indices in local numbering
352bb25748dSBarry Smith 
353bb25748dSBarry Smith    Output Parameter:
354bb25748dSBarry Smith .  out - indices in global numbering
355bb25748dSBarry Smith 
356b9cd556bSLois Curfman McInnes    Notes:
357b9cd556bSLois Curfman McInnes    The in and out array parameters may be identical.
358d4bb536fSBarry Smith 
359a997ad1aSLois Curfman McInnes    Level: advanced
360a997ad1aSLois Curfman McInnes 
361bb25748dSBarry Smith .seealso: ISLocalToGlobalMappingCreate(),ISLocalToGlobalMappingDestroy(),
3620752156aSBarry Smith           ISLocalToGlobalMappingApplyIS(),AOCreateBasic(),AOApplicationToPetsc(),
363d4bb536fSBarry Smith           AOPetscToApplication(), ISGlobalToLocalMappingApply()
364bb25748dSBarry Smith 
365273d9f13SBarry Smith     Concepts: mapping^local to global
366d4bb536fSBarry Smith 
36789d82c54SBarry Smith M*/
368d4bb536fSBarry Smith 
369d4bb536fSBarry Smith /* -----------------------------------------------------------------------------------------*/
370d4bb536fSBarry Smith 
3714a2ae208SSatish Balay #undef __FUNCT__
3724a2ae208SSatish Balay #define __FUNCT__ "ISGlobalToLocalMappingSetUp_Private"
373d4bb536fSBarry Smith /*
374d4bb536fSBarry Smith     Creates the global fields in the ISLocalToGlobalMapping structure
375d4bb536fSBarry Smith */
3766849ba73SBarry Smith static PetscErrorCode ISGlobalToLocalMappingSetUp_Private(ISLocalToGlobalMapping mapping)
377d4bb536fSBarry Smith {
3786849ba73SBarry Smith   PetscErrorCode ierr;
37932dcc486SBarry Smith   PetscInt       i,*idx = mapping->indices,n = mapping->n,end,start,*globals;
380d4bb536fSBarry Smith 
3813a40ed3dSBarry Smith   PetscFunctionBegin;
382d4bb536fSBarry Smith   end   = 0;
383d4bb536fSBarry Smith   start = 100000000;
384d4bb536fSBarry Smith 
385d4bb536fSBarry Smith   for (i=0; i<n; i++) {
386d4bb536fSBarry Smith     if (idx[i] < 0) continue;
387d4bb536fSBarry Smith     if (idx[i] < start) start = idx[i];
388d4bb536fSBarry Smith     if (idx[i] > end)   end   = idx[i];
389d4bb536fSBarry Smith   }
390d4bb536fSBarry Smith   if (start > end) {start = 0; end = -1;}
391d4bb536fSBarry Smith   mapping->globalstart = start;
392d4bb536fSBarry Smith   mapping->globalend   = end;
393d4bb536fSBarry Smith 
39432dcc486SBarry Smith   ierr             = PetscMalloc((end-start+2)*sizeof(PetscInt),&globals);CHKERRQ(ierr);
395b0a32e0cSBarry Smith   mapping->globals = globals;
396d4bb536fSBarry Smith   for (i=0; i<end-start+1; i++) {
397d4bb536fSBarry Smith     globals[i] = -1;
398d4bb536fSBarry Smith   }
399d4bb536fSBarry Smith   for (i=0; i<n; i++) {
400d4bb536fSBarry Smith     if (idx[i] < 0) continue;
401d4bb536fSBarry Smith     globals[idx[i] - start] = i;
402d4bb536fSBarry Smith   }
403d4bb536fSBarry Smith 
40452e6d16bSBarry Smith   ierr = PetscLogObjectMemory(mapping,(end-start+1)*sizeof(PetscInt));CHKERRQ(ierr);
4053a40ed3dSBarry Smith   PetscFunctionReturn(0);
406d4bb536fSBarry Smith }
407d4bb536fSBarry Smith 
4084a2ae208SSatish Balay #undef __FUNCT__
4094a2ae208SSatish Balay #define __FUNCT__ "ISGlobalToLocalMappingApply"
410d4bb536fSBarry Smith /*@
411a997ad1aSLois Curfman McInnes     ISGlobalToLocalMappingApply - Provides the local numbering for a list of integers
412a997ad1aSLois Curfman McInnes     specified with a global numbering.
413d4bb536fSBarry Smith 
414b9cd556bSLois Curfman McInnes     Not collective
415b9cd556bSLois Curfman McInnes 
416d4bb536fSBarry Smith     Input Parameters:
417b9cd556bSLois Curfman McInnes +   mapping - mapping between local and global numbering
418d4bb536fSBarry Smith .   type - IS_GTOLM_MASK - replaces global indices with no local value with -1
419d4bb536fSBarry Smith            IS_GTOLM_DROP - drops the indices with no local value from the output list
420d4bb536fSBarry Smith .   n - number of global indices to map
421b9cd556bSLois Curfman McInnes -   idx - global indices to map
422d4bb536fSBarry Smith 
423d4bb536fSBarry Smith     Output Parameters:
424b9cd556bSLois Curfman McInnes +   nout - number of indices in output array (if type == IS_GTOLM_MASK then nout = n)
425b9cd556bSLois Curfman McInnes -   idxout - local index of each global index, one must pass in an array long enough
426e182c471SBarry Smith              to hold all the indices. You can call ISGlobalToLocalMappingApply() with
427e182c471SBarry Smith              idxout == PETSC_NULL to determine the required length (returned in nout)
428e182c471SBarry Smith              and then allocate the required space and call ISGlobalToLocalMappingApply()
429e182c471SBarry Smith              a second time to set the values.
430d4bb536fSBarry Smith 
431b9cd556bSLois Curfman McInnes     Notes:
432b9cd556bSLois Curfman McInnes     Either nout or idxout may be PETSC_NULL. idx and idxout may be identical.
433d4bb536fSBarry Smith 
4340f5bd95cSBarry Smith     This is not scalable in memory usage. Each processor requires O(Nglobal) size
4350f5bd95cSBarry Smith     array to compute these.
4360f5bd95cSBarry Smith 
437a997ad1aSLois Curfman McInnes     Level: advanced
438a997ad1aSLois Curfman McInnes 
439273d9f13SBarry Smith     Concepts: mapping^global to local
440d4bb536fSBarry Smith 
441d4bb536fSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(),
442d4bb536fSBarry Smith           ISLocalToGlobalMappingDestroy()
443d4bb536fSBarry Smith @*/
4447087cfbeSBarry Smith PetscErrorCode  ISGlobalToLocalMappingApply(ISLocalToGlobalMapping mapping,ISGlobalToLocalMappingType type,
44532dcc486SBarry Smith                                   PetscInt n,const PetscInt idx[],PetscInt *nout,PetscInt idxout[])
446d4bb536fSBarry Smith {
44732dcc486SBarry Smith   PetscInt       i,*globals,nf = 0,tmp,start,end;
4486849ba73SBarry Smith   PetscErrorCode ierr;
449d4bb536fSBarry Smith 
4503a40ed3dSBarry Smith   PetscFunctionBegin;
4510700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
452d4bb536fSBarry Smith   if (!mapping->globals) {
453d4bb536fSBarry Smith     ierr = ISGlobalToLocalMappingSetUp_Private(mapping);CHKERRQ(ierr);
454d4bb536fSBarry Smith   }
455d4bb536fSBarry Smith   globals = mapping->globals;
456d4bb536fSBarry Smith   start   = mapping->globalstart;
457d4bb536fSBarry Smith   end     = mapping->globalend;
458d4bb536fSBarry Smith 
459d4bb536fSBarry Smith   if (type == IS_GTOLM_MASK) {
460d4bb536fSBarry Smith     if (idxout) {
461d4bb536fSBarry Smith       for (i=0; i<n; i++) {
462d4bb536fSBarry Smith         if (idx[i] < 0) idxout[i] = idx[i];
463d4bb536fSBarry Smith         else if (idx[i] < start) idxout[i] = -1;
464d4bb536fSBarry Smith         else if (idx[i] > end)   idxout[i] = -1;
465d4bb536fSBarry Smith         else                     idxout[i] = globals[idx[i] - start];
466d4bb536fSBarry Smith       }
467d4bb536fSBarry Smith     }
468d4bb536fSBarry Smith     if (nout) *nout = n;
469d4bb536fSBarry Smith   } else {
470d4bb536fSBarry Smith     if (idxout) {
471d4bb536fSBarry Smith       for (i=0; i<n; i++) {
472d4bb536fSBarry Smith         if (idx[i] < 0) continue;
473d4bb536fSBarry Smith         if (idx[i] < start) continue;
474d4bb536fSBarry Smith         if (idx[i] > end) continue;
475d4bb536fSBarry Smith         tmp = globals[idx[i] - start];
476d4bb536fSBarry Smith         if (tmp < 0) continue;
477d4bb536fSBarry Smith         idxout[nf++] = tmp;
478d4bb536fSBarry Smith       }
479d4bb536fSBarry Smith     } else {
480d4bb536fSBarry Smith       for (i=0; i<n; i++) {
481d4bb536fSBarry Smith         if (idx[i] < 0) continue;
482d4bb536fSBarry Smith         if (idx[i] < start) continue;
483d4bb536fSBarry Smith         if (idx[i] > end) continue;
484d4bb536fSBarry Smith         tmp = globals[idx[i] - start];
485d4bb536fSBarry Smith         if (tmp < 0) continue;
486d4bb536fSBarry Smith         nf++;
487d4bb536fSBarry Smith       }
488d4bb536fSBarry Smith     }
489d4bb536fSBarry Smith     if (nout) *nout = nf;
490d4bb536fSBarry Smith   }
491d4bb536fSBarry Smith 
4923a40ed3dSBarry Smith   PetscFunctionReturn(0);
493d4bb536fSBarry Smith }
49490f02eecSBarry Smith 
4954a2ae208SSatish Balay #undef __FUNCT__
4964a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingGetInfo"
49789d82c54SBarry Smith /*@C
49889d82c54SBarry Smith     ISLocalToGlobalMappingGetInfo - Gets the neighbor information for each processor and
49989d82c54SBarry Smith      each index shared by more than one processor
50089d82c54SBarry Smith 
50189d82c54SBarry Smith     Collective on ISLocalToGlobalMapping
50289d82c54SBarry Smith 
50389d82c54SBarry Smith     Input Parameters:
50489d82c54SBarry Smith .   mapping - the mapping from local to global indexing
50589d82c54SBarry Smith 
50689d82c54SBarry Smith     Output Parameter:
50789d82c54SBarry Smith +   nproc - number of processors that are connected to this one
50889d82c54SBarry Smith .   proc - neighboring processors
50907b52d57SBarry Smith .   numproc - number of indices for each subdomain (processor)
5103463a7baSJed Brown -   indices - indices of nodes (in local numbering) shared with neighbors (sorted by global numbering)
51189d82c54SBarry Smith 
51289d82c54SBarry Smith     Level: advanced
51389d82c54SBarry Smith 
514273d9f13SBarry Smith     Concepts: mapping^local to global
51589d82c54SBarry Smith 
5162cfcea29SBarry Smith     Fortran Usage:
5172cfcea29SBarry Smith $        ISLocalToGlobalMpngGetInfoSize(ISLocalToGlobalMapping,PetscInt nproc,PetscInt numprocmax,ierr) followed by
5182cfcea29SBarry Smith $        ISLocalToGlobalMappingGetInfo(ISLocalToGlobalMapping,PetscInt nproc, PetscInt procs[nproc],PetscInt numprocs[nproc],
5192cfcea29SBarry Smith           PetscInt indices[nproc][numprocmax],ierr)
5202cfcea29SBarry Smith         There is no ISLocalToGlobalMappingRestoreInfo() in Fortran. You must make sure that procs[], numprocs[] and
5212cfcea29SBarry Smith         indices[][] are large enough arrays, either by allocating them dynamically or defining static ones large enough.
5222cfcea29SBarry Smith 
5232cfcea29SBarry Smith 
52407b52d57SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreate(),
52507b52d57SBarry Smith           ISLocalToGlobalMappingRestoreInfo()
52689d82c54SBarry Smith @*/
5277087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingGetInfo(ISLocalToGlobalMapping mapping,PetscInt *nproc,PetscInt *procs[],PetscInt *numprocs[],PetscInt **indices[])
52889d82c54SBarry Smith {
5296849ba73SBarry Smith   PetscErrorCode ierr;
53097f1f81fSBarry Smith   PetscMPIInt    size,rank,tag1,tag2,tag3,*len,*source,imdex;
53132dcc486SBarry Smith   PetscInt       i,n = mapping->n,Ng,ng,max = 0,*lindices = mapping->indices;
53232dcc486SBarry Smith   PetscInt       *nprocs,*owner,nsends,*sends,j,*starts,nmax,nrecvs,*recvs,proc;
53397f1f81fSBarry Smith   PetscInt       cnt,scale,*ownedsenders,*nownedsenders,rstart,nowned;
53432dcc486SBarry Smith   PetscInt       node,nownedm,nt,*sends2,nsends2,*starts2,*lens2,*dest,nrecvs2,*starts3,*recvs2,k,*bprocs,*tmp;
53532dcc486SBarry Smith   PetscInt       first_procs,first_numprocs,*first_indices;
53689d82c54SBarry Smith   MPI_Request    *recv_waits,*send_waits;
53730dcb7c9SBarry Smith   MPI_Status     recv_status,*send_status,*recv_statuses;
5387adad957SLisandro Dalcin   MPI_Comm       comm = ((PetscObject)mapping)->comm;
539ace3abfcSBarry Smith   PetscBool      debug = PETSC_FALSE;
54089d82c54SBarry Smith 
54189d82c54SBarry Smith   PetscFunctionBegin;
5420700a824SBarry Smith   PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1);
54324cf384cSBarry Smith   ierr   = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
54424cf384cSBarry Smith   ierr   = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
54524cf384cSBarry Smith   if (size == 1) {
54624cf384cSBarry Smith     *nproc         = 0;
54724cf384cSBarry Smith     *procs         = PETSC_NULL;
54832dcc486SBarry Smith     ierr           = PetscMalloc(sizeof(PetscInt),numprocs);CHKERRQ(ierr);
5491e2105dcSBarry Smith     (*numprocs)[0] = 0;
55032dcc486SBarry Smith     ierr           = PetscMalloc(sizeof(PetscInt*),indices);CHKERRQ(ierr);
5511e2105dcSBarry Smith     (*indices)[0]  = PETSC_NULL;
55224cf384cSBarry Smith     PetscFunctionReturn(0);
55324cf384cSBarry Smith   }
55424cf384cSBarry Smith 
555acfcf0e5SJed Brown   ierr = PetscOptionsGetBool(PETSC_NULL,"-islocaltoglobalmappinggetinfo_debug",&debug,PETSC_NULL);CHKERRQ(ierr);
55607b52d57SBarry Smith 
5573677ff5aSBarry Smith   /*
5583677ff5aSBarry Smith     Notes on ISLocalToGlobalMappingGetInfo
5593677ff5aSBarry Smith 
5603677ff5aSBarry Smith     globally owned node - the nodes that have been assigned to this processor in global
5613677ff5aSBarry Smith            numbering, just for this routine.
5623677ff5aSBarry Smith 
5633677ff5aSBarry Smith     nontrivial globally owned node - node assigned to this processor that is on a subdomain
5643677ff5aSBarry Smith            boundary (i.e. is has more than one local owner)
5653677ff5aSBarry Smith 
5663677ff5aSBarry Smith     locally owned node - node that exists on this processors subdomain
5673677ff5aSBarry Smith 
5683677ff5aSBarry Smith     nontrivial locally owned node - node that is not in the interior (i.e. has more than one
5693677ff5aSBarry Smith            local subdomain
5703677ff5aSBarry Smith   */
57124cf384cSBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mapping,&tag1);CHKERRQ(ierr);
57224cf384cSBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mapping,&tag2);CHKERRQ(ierr);
57324cf384cSBarry Smith   ierr = PetscObjectGetNewTag((PetscObject)mapping,&tag3);CHKERRQ(ierr);
57489d82c54SBarry Smith 
57589d82c54SBarry Smith   for (i=0; i<n; i++) {
57689d82c54SBarry Smith     if (lindices[i] > max) max = lindices[i];
57789d82c54SBarry Smith   }
57832dcc486SBarry Smith   ierr   = MPI_Allreduce(&max,&Ng,1,MPIU_INT,MPI_MAX,comm);CHKERRQ(ierr);
57978058e43SBarry Smith   Ng++;
58089d82c54SBarry Smith   ierr   = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
58189d82c54SBarry Smith   ierr   = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr);
582bc8ff85bSBarry Smith   scale  = Ng/size + 1;
583a2e34c3dSBarry Smith   ng     = scale; if (rank == size-1) ng = Ng - scale*(size-1); ng = PetscMax(1,ng);
584caba0dd0SBarry Smith   rstart = scale*rank;
58589d82c54SBarry Smith 
58689d82c54SBarry Smith   /* determine ownership ranges of global indices */
5877c334f02SBarry Smith   ierr = PetscMalloc(2*size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr);
58832dcc486SBarry Smith   ierr = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr);
58989d82c54SBarry Smith 
59089d82c54SBarry Smith   /* determine owners of each local node  */
5917c334f02SBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&owner);CHKERRQ(ierr);
59289d82c54SBarry Smith   for (i=0; i<n; i++) {
5933677ff5aSBarry Smith     proc             = lindices[i]/scale; /* processor that globally owns this index */
59427c402fcSBarry Smith     nprocs[2*proc+1] = 1;                 /* processor globally owns at least one of ours */
5953677ff5aSBarry Smith     owner[i]         = proc;
59627c402fcSBarry Smith     nprocs[2*proc]++;                     /* count of how many that processor globally owns of ours */
59789d82c54SBarry Smith   }
59827c402fcSBarry Smith   nsends = 0; for (i=0; i<size; i++) nsends += nprocs[2*i+1];
5991e2582c4SBarry Smith   ierr = PetscInfo1(mapping,"Number of global owners for my local data %d\n",nsends);CHKERRQ(ierr);
60089d82c54SBarry Smith 
60189d82c54SBarry Smith   /* inform other processors of number of messages and max length*/
60227c402fcSBarry Smith   ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr);
6031e2582c4SBarry Smith   ierr = PetscInfo1(mapping,"Number of local owners for my global data %d\n",nrecvs);CHKERRQ(ierr);
60489d82c54SBarry Smith 
60589d82c54SBarry Smith   /* post receives for owned rows */
60632dcc486SBarry Smith   ierr = PetscMalloc((2*nrecvs+1)*(nmax+1)*sizeof(PetscInt),&recvs);CHKERRQ(ierr);
607b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
60889d82c54SBarry Smith   for (i=0; i<nrecvs; i++) {
60932dcc486SBarry Smith     ierr = MPI_Irecv(recvs+2*nmax*i,2*nmax,MPIU_INT,MPI_ANY_SOURCE,tag1,comm,recv_waits+i);CHKERRQ(ierr);
61089d82c54SBarry Smith   }
61189d82c54SBarry Smith 
61289d82c54SBarry Smith   /* pack messages containing lists of local nodes to owners */
61332dcc486SBarry Smith   ierr       = PetscMalloc((2*n+1)*sizeof(PetscInt),&sends);CHKERRQ(ierr);
61432dcc486SBarry Smith   ierr       = PetscMalloc((size+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr);
61589d82c54SBarry Smith   starts[0]  = 0;
61627c402fcSBarry Smith   for (i=1; i<size; i++) { starts[i] = starts[i-1] + 2*nprocs[2*i-2];}
61789d82c54SBarry Smith   for (i=0; i<n; i++) {
61889d82c54SBarry Smith     sends[starts[owner[i]]++] = lindices[i];
61930dcb7c9SBarry Smith     sends[starts[owner[i]]++] = i;
62089d82c54SBarry Smith   }
62189d82c54SBarry Smith   ierr = PetscFree(owner);CHKERRQ(ierr);
62289d82c54SBarry Smith   starts[0]  = 0;
62327c402fcSBarry Smith   for (i=1; i<size; i++) { starts[i] = starts[i-1] + 2*nprocs[2*i-2];}
62489d82c54SBarry Smith 
62589d82c54SBarry Smith   /* send the messages */
626b0a32e0cSBarry Smith   ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr);
62732dcc486SBarry Smith   ierr = PetscMalloc((nsends+1)*sizeof(PetscInt),&dest);CHKERRQ(ierr);
62889d82c54SBarry Smith   cnt = 0;
62989d82c54SBarry Smith   for (i=0; i<size; i++) {
63027c402fcSBarry Smith     if (nprocs[2*i]) {
63132dcc486SBarry Smith       ierr      = MPI_Isend(sends+starts[i],2*nprocs[2*i],MPIU_INT,i,tag1,comm,send_waits+cnt);CHKERRQ(ierr);
63230dcb7c9SBarry Smith       dest[cnt] = i;
63389d82c54SBarry Smith       cnt++;
63489d82c54SBarry Smith     }
63589d82c54SBarry Smith   }
63689d82c54SBarry Smith   ierr = PetscFree(starts);CHKERRQ(ierr);
63789d82c54SBarry Smith 
63889d82c54SBarry Smith   /* wait on receives */
63997f1f81fSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(PetscMPIInt),&source);CHKERRQ(ierr);
64097f1f81fSBarry Smith   ierr = PetscMalloc((nrecvs+1)*sizeof(PetscMPIInt),&len);CHKERRQ(ierr);
64189d82c54SBarry Smith   cnt  = nrecvs;
64232dcc486SBarry Smith   ierr = PetscMalloc((ng+1)*sizeof(PetscInt),&nownedsenders);CHKERRQ(ierr);
64332dcc486SBarry Smith   ierr = PetscMemzero(nownedsenders,ng*sizeof(PetscInt));CHKERRQ(ierr);
64489d82c54SBarry Smith   while (cnt) {
64589d82c54SBarry Smith     ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr);
64689d82c54SBarry Smith     /* unpack receives into our local space */
64732dcc486SBarry Smith     ierr           = MPI_Get_count(&recv_status,MPIU_INT,&len[imdex]);CHKERRQ(ierr);
64889d82c54SBarry Smith     source[imdex]  = recv_status.MPI_SOURCE;
64930dcb7c9SBarry Smith     len[imdex]     = len[imdex]/2;
650caba0dd0SBarry Smith     /* count how many local owners for each of my global owned indices */
65130dcb7c9SBarry Smith     for (i=0; i<len[imdex]; i++) nownedsenders[recvs[2*imdex*nmax+2*i]-rstart]++;
65289d82c54SBarry Smith     cnt--;
65389d82c54SBarry Smith   }
65489d82c54SBarry Smith   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
65589d82c54SBarry Smith 
65630dcb7c9SBarry Smith   /* count how many globally owned indices are on an edge multiplied by how many processors own them. */
657bc8ff85bSBarry Smith   nowned  = 0;
658bc8ff85bSBarry Smith   nownedm = 0;
659bc8ff85bSBarry Smith   for (i=0; i<ng; i++) {
660bc8ff85bSBarry Smith     if (nownedsenders[i] > 1) {nownedm += nownedsenders[i]; nowned++;}
661bc8ff85bSBarry Smith   }
662bc8ff85bSBarry Smith 
663bc8ff85bSBarry Smith   /* create single array to contain rank of all local owners of each globally owned index */
66432dcc486SBarry Smith   ierr      = PetscMalloc((nownedm+1)*sizeof(PetscInt),&ownedsenders);CHKERRQ(ierr);
66532dcc486SBarry Smith   ierr      = PetscMalloc((ng+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr);
666bc8ff85bSBarry Smith   starts[0] = 0;
667bc8ff85bSBarry Smith   for (i=1; i<ng; i++) {
668bc8ff85bSBarry Smith     if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1];
669bc8ff85bSBarry Smith     else starts[i] = starts[i-1];
670bc8ff85bSBarry Smith   }
671bc8ff85bSBarry Smith 
67230dcb7c9SBarry Smith   /* for each nontrival globally owned node list all arriving processors */
673bc8ff85bSBarry Smith   for (i=0; i<nrecvs; i++) {
674bc8ff85bSBarry Smith     for (j=0; j<len[i]; j++) {
67530dcb7c9SBarry Smith       node = recvs[2*i*nmax+2*j]-rstart;
676bc8ff85bSBarry Smith       if (nownedsenders[node] > 1) {
677bc8ff85bSBarry Smith         ownedsenders[starts[node]++] = source[i];
678bc8ff85bSBarry Smith       }
679bc8ff85bSBarry Smith     }
680bc8ff85bSBarry Smith   }
681bc8ff85bSBarry Smith 
68207b52d57SBarry Smith   if (debug) { /* -----------------------------------  */
68330dcb7c9SBarry Smith     starts[0]    = 0;
68430dcb7c9SBarry Smith     for (i=1; i<ng; i++) {
68530dcb7c9SBarry Smith       if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1];
68630dcb7c9SBarry Smith       else starts[i] = starts[i-1];
68730dcb7c9SBarry Smith     }
68830dcb7c9SBarry Smith     for (i=0; i<ng; i++) {
68930dcb7c9SBarry Smith       if (nownedsenders[i] > 1) {
69030dcb7c9SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"[%d] global node %d local owner processors: ",rank,i+rstart);CHKERRQ(ierr);
69130dcb7c9SBarry Smith         for (j=0; j<nownedsenders[i]; j++) {
69230dcb7c9SBarry Smith           ierr = PetscSynchronizedPrintf(comm,"%d ",ownedsenders[starts[i]+j]);CHKERRQ(ierr);
69330dcb7c9SBarry Smith         }
69430dcb7c9SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"\n");CHKERRQ(ierr);
69530dcb7c9SBarry Smith       }
69630dcb7c9SBarry Smith     }
69730dcb7c9SBarry Smith     ierr = PetscSynchronizedFlush(comm);CHKERRQ(ierr);
69807b52d57SBarry Smith   }/* -----------------------------------  */
69930dcb7c9SBarry Smith 
7003677ff5aSBarry Smith   /* wait on original sends */
7013a96401aSBarry Smith   if (nsends) {
702b0a32e0cSBarry Smith     ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
7033a96401aSBarry Smith     ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr);
7043a96401aSBarry Smith     ierr = PetscFree(send_status);CHKERRQ(ierr);
7053a96401aSBarry Smith   }
70689d82c54SBarry Smith   ierr = PetscFree(send_waits);CHKERRQ(ierr);
7073a96401aSBarry Smith   ierr = PetscFree(sends);CHKERRQ(ierr);
7083677ff5aSBarry Smith   ierr = PetscFree(nprocs);CHKERRQ(ierr);
7093677ff5aSBarry Smith 
7103677ff5aSBarry Smith   /* pack messages to send back to local owners */
71130dcb7c9SBarry Smith   starts[0]    = 0;
71230dcb7c9SBarry Smith   for (i=1; i<ng; i++) {
71330dcb7c9SBarry Smith     if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1];
71430dcb7c9SBarry Smith     else starts[i] = starts[i-1];
71530dcb7c9SBarry Smith   }
71630dcb7c9SBarry Smith   nsends2 = nrecvs;
71732dcc486SBarry Smith   ierr    = PetscMalloc((nsends2+1)*sizeof(PetscInt),&nprocs);CHKERRQ(ierr); /* length of each message */
71830dcb7c9SBarry Smith   for (i=0; i<nrecvs; i++) {
71930dcb7c9SBarry Smith     nprocs[i] = 1;
72030dcb7c9SBarry Smith     for (j=0; j<len[i]; j++) {
72130dcb7c9SBarry Smith       node = recvs[2*i*nmax+2*j]-rstart;
72230dcb7c9SBarry Smith       if (nownedsenders[node] > 1) {
72330dcb7c9SBarry Smith         nprocs[i] += 2 + nownedsenders[node];
72430dcb7c9SBarry Smith       }
72530dcb7c9SBarry Smith     }
72630dcb7c9SBarry Smith   }
72730dcb7c9SBarry Smith   nt = 0; for (i=0; i<nsends2; i++) nt += nprocs[i];
72832dcc486SBarry Smith   ierr = PetscMalloc((nt+1)*sizeof(PetscInt),&sends2);CHKERRQ(ierr);
72932dcc486SBarry Smith   ierr = PetscMalloc((nsends2+1)*sizeof(PetscInt),&starts2);CHKERRQ(ierr);
73030dcb7c9SBarry Smith   starts2[0] = 0; for (i=1; i<nsends2; i++) starts2[i] = starts2[i-1] + nprocs[i-1];
73130dcb7c9SBarry Smith   /*
73230dcb7c9SBarry Smith      Each message is 1 + nprocs[i] long, and consists of
73330dcb7c9SBarry Smith        (0) the number of nodes being sent back
73430dcb7c9SBarry Smith        (1) the local node number,
73530dcb7c9SBarry Smith        (2) the number of processors sharing it,
73630dcb7c9SBarry Smith        (3) the processors sharing it
73730dcb7c9SBarry Smith   */
73830dcb7c9SBarry Smith   for (i=0; i<nsends2; i++) {
73930dcb7c9SBarry Smith     cnt = 1;
74030dcb7c9SBarry Smith     sends2[starts2[i]] = 0;
74130dcb7c9SBarry Smith     for (j=0; j<len[i]; j++) {
74230dcb7c9SBarry Smith       node = recvs[2*i*nmax+2*j]-rstart;
74330dcb7c9SBarry Smith       if (nownedsenders[node] > 1) {
74430dcb7c9SBarry Smith         sends2[starts2[i]]++;
74530dcb7c9SBarry Smith         sends2[starts2[i]+cnt++] = recvs[2*i*nmax+2*j+1];
74630dcb7c9SBarry Smith         sends2[starts2[i]+cnt++] = nownedsenders[node];
74732dcc486SBarry Smith         ierr = PetscMemcpy(&sends2[starts2[i]+cnt],&ownedsenders[starts[node]],nownedsenders[node]*sizeof(PetscInt));CHKERRQ(ierr);
74830dcb7c9SBarry Smith         cnt += nownedsenders[node];
74930dcb7c9SBarry Smith       }
75030dcb7c9SBarry Smith     }
75130dcb7c9SBarry Smith   }
75230dcb7c9SBarry Smith 
75330dcb7c9SBarry Smith   /* receive the message lengths */
75430dcb7c9SBarry Smith   nrecvs2 = nsends;
75532dcc486SBarry Smith   ierr = PetscMalloc((nrecvs2+1)*sizeof(PetscInt),&lens2);CHKERRQ(ierr);
75632dcc486SBarry Smith   ierr = PetscMalloc((nrecvs2+1)*sizeof(PetscInt),&starts3);CHKERRQ(ierr);
757d44834fbSBarry Smith   ierr = PetscMalloc((nrecvs2+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
75830dcb7c9SBarry Smith   for (i=0; i<nrecvs2; i++) {
759d44834fbSBarry Smith     ierr = MPI_Irecv(&lens2[i],1,MPIU_INT,dest[i],tag2,comm,recv_waits+i);CHKERRQ(ierr);
76030dcb7c9SBarry Smith   }
761d44834fbSBarry Smith 
7628a8e0b3aSBarry Smith   /* send the message lengths */
7638a8e0b3aSBarry Smith   for (i=0; i<nsends2; i++) {
7648a8e0b3aSBarry Smith     ierr = MPI_Send(&nprocs[i],1,MPIU_INT,source[i],tag2,comm);CHKERRQ(ierr);
7658a8e0b3aSBarry Smith   }
7668a8e0b3aSBarry Smith 
767d44834fbSBarry Smith   /* wait on receives of lens */
7680c468ba9SBarry Smith   if (nrecvs2) {
7690c468ba9SBarry Smith     ierr = PetscMalloc(nrecvs2*sizeof(MPI_Status),&recv_statuses);CHKERRQ(ierr);
770d44834fbSBarry Smith     ierr = MPI_Waitall(nrecvs2,recv_waits,recv_statuses);CHKERRQ(ierr);
771d44834fbSBarry Smith     ierr = PetscFree(recv_statuses);CHKERRQ(ierr);
7720c468ba9SBarry Smith   }
773d44834fbSBarry Smith   ierr = PetscFree(recv_waits);
774d44834fbSBarry Smith 
77530dcb7c9SBarry Smith   starts3[0] = 0;
776d44834fbSBarry Smith   nt         = 0;
77730dcb7c9SBarry Smith   for (i=0; i<nrecvs2-1; i++) {
77830dcb7c9SBarry Smith     starts3[i+1] = starts3[i] + lens2[i];
779d44834fbSBarry Smith     nt          += lens2[i];
78030dcb7c9SBarry Smith   }
781d44834fbSBarry Smith   nt += lens2[nrecvs2-1];
782d44834fbSBarry Smith 
78332dcc486SBarry Smith   ierr = PetscMalloc((nt+1)*sizeof(PetscInt),&recvs2);CHKERRQ(ierr);
784b0a32e0cSBarry Smith   ierr = PetscMalloc((nrecvs2+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr);
78552b72c4aSBarry Smith   for (i=0; i<nrecvs2; i++) {
78632dcc486SBarry Smith     ierr = MPI_Irecv(recvs2+starts3[i],lens2[i],MPIU_INT,dest[i],tag3,comm,recv_waits+i);CHKERRQ(ierr);
78730dcb7c9SBarry Smith   }
78830dcb7c9SBarry Smith 
78930dcb7c9SBarry Smith   /* send the messages */
790b0a32e0cSBarry Smith   ierr = PetscMalloc((nsends2+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr);
79130dcb7c9SBarry Smith   for (i=0; i<nsends2; i++) {
79232dcc486SBarry Smith     ierr = MPI_Isend(sends2+starts2[i],nprocs[i],MPIU_INT,source[i],tag3,comm,send_waits+i);CHKERRQ(ierr);
79330dcb7c9SBarry Smith   }
79430dcb7c9SBarry Smith 
79530dcb7c9SBarry Smith   /* wait on receives */
7960c468ba9SBarry Smith   if (nrecvs2) {
7970c468ba9SBarry Smith     ierr = PetscMalloc(nrecvs2*sizeof(MPI_Status),&recv_statuses);CHKERRQ(ierr);
79830dcb7c9SBarry Smith     ierr = MPI_Waitall(nrecvs2,recv_waits,recv_statuses);CHKERRQ(ierr);
79930dcb7c9SBarry Smith     ierr = PetscFree(recv_statuses);CHKERRQ(ierr);
8000c468ba9SBarry Smith   }
80130dcb7c9SBarry Smith   ierr = PetscFree(recv_waits);CHKERRQ(ierr);
80230dcb7c9SBarry Smith   ierr = PetscFree(nprocs);CHKERRQ(ierr);
80330dcb7c9SBarry Smith 
80407b52d57SBarry Smith   if (debug) { /* -----------------------------------  */
80530dcb7c9SBarry Smith     cnt = 0;
80630dcb7c9SBarry Smith     for (i=0; i<nrecvs2; i++) {
80730dcb7c9SBarry Smith       nt = recvs2[cnt++];
80830dcb7c9SBarry Smith       for (j=0; j<nt; j++) {
80930dcb7c9SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"[%d] local node %d number of subdomains %d: ",rank,recvs2[cnt],recvs2[cnt+1]);CHKERRQ(ierr);
81030dcb7c9SBarry Smith         for (k=0; k<recvs2[cnt+1]; k++) {
81130dcb7c9SBarry Smith           ierr = PetscSynchronizedPrintf(comm,"%d ",recvs2[cnt+2+k]);CHKERRQ(ierr);
81230dcb7c9SBarry Smith         }
81330dcb7c9SBarry Smith         cnt += 2 + recvs2[cnt+1];
81430dcb7c9SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"\n");CHKERRQ(ierr);
81530dcb7c9SBarry Smith       }
81630dcb7c9SBarry Smith     }
81730dcb7c9SBarry Smith     ierr = PetscSynchronizedFlush(comm);CHKERRQ(ierr);
81807b52d57SBarry Smith   } /* -----------------------------------  */
81930dcb7c9SBarry Smith 
82030dcb7c9SBarry Smith   /* count number subdomains for each local node */
82132dcc486SBarry Smith   ierr = PetscMalloc(size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr);
82232dcc486SBarry Smith   ierr = PetscMemzero(nprocs,size*sizeof(PetscInt));CHKERRQ(ierr);
82330dcb7c9SBarry Smith   cnt  = 0;
82430dcb7c9SBarry Smith   for (i=0; i<nrecvs2; i++) {
82530dcb7c9SBarry Smith     nt = recvs2[cnt++];
82630dcb7c9SBarry Smith     for (j=0; j<nt; j++) {
82730dcb7c9SBarry Smith       for (k=0; k<recvs2[cnt+1]; k++) {
82830dcb7c9SBarry Smith         nprocs[recvs2[cnt+2+k]]++;
82930dcb7c9SBarry Smith       }
83030dcb7c9SBarry Smith       cnt += 2 + recvs2[cnt+1];
83130dcb7c9SBarry Smith     }
83230dcb7c9SBarry Smith   }
83330dcb7c9SBarry Smith   nt = 0; for (i=0; i<size; i++) nt += (nprocs[i] > 0);
83430dcb7c9SBarry Smith   *nproc    = nt;
83532dcc486SBarry Smith   ierr = PetscMalloc((nt+1)*sizeof(PetscInt),procs);CHKERRQ(ierr);
83632dcc486SBarry Smith   ierr = PetscMalloc((nt+1)*sizeof(PetscInt),numprocs);CHKERRQ(ierr);
83732dcc486SBarry Smith   ierr = PetscMalloc((nt+1)*sizeof(PetscInt*),indices);CHKERRQ(ierr);
83832dcc486SBarry Smith   ierr = PetscMalloc(size*sizeof(PetscInt),&bprocs);CHKERRQ(ierr);
83930dcb7c9SBarry Smith   cnt       = 0;
84030dcb7c9SBarry Smith   for (i=0; i<size; i++) {
84130dcb7c9SBarry Smith     if (nprocs[i] > 0) {
84230dcb7c9SBarry Smith       bprocs[i]        = cnt;
84330dcb7c9SBarry Smith       (*procs)[cnt]    = i;
84430dcb7c9SBarry Smith       (*numprocs)[cnt] = nprocs[i];
84532dcc486SBarry Smith       ierr             = PetscMalloc(nprocs[i]*sizeof(PetscInt),&(*indices)[cnt]);CHKERRQ(ierr);
84630dcb7c9SBarry Smith       cnt++;
84730dcb7c9SBarry Smith     }
84830dcb7c9SBarry Smith   }
84930dcb7c9SBarry Smith 
85030dcb7c9SBarry Smith   /* make the list of subdomains for each nontrivial local node */
85132dcc486SBarry Smith   ierr = PetscMemzero(*numprocs,nt*sizeof(PetscInt));CHKERRQ(ierr);
85230dcb7c9SBarry Smith   cnt  = 0;
85330dcb7c9SBarry Smith   for (i=0; i<nrecvs2; i++) {
85430dcb7c9SBarry Smith     nt = recvs2[cnt++];
85530dcb7c9SBarry Smith     for (j=0; j<nt; j++) {
85630dcb7c9SBarry Smith       for (k=0; k<recvs2[cnt+1]; k++) {
85730dcb7c9SBarry Smith         (*indices)[bprocs[recvs2[cnt+2+k]]][(*numprocs)[bprocs[recvs2[cnt+2+k]]]++] = recvs2[cnt];
85830dcb7c9SBarry Smith       }
85930dcb7c9SBarry Smith       cnt += 2 + recvs2[cnt+1];
86030dcb7c9SBarry Smith     }
86130dcb7c9SBarry Smith   }
86230dcb7c9SBarry Smith   ierr = PetscFree(bprocs);CHKERRQ(ierr);
86307b52d57SBarry Smith   ierr = PetscFree(recvs2);CHKERRQ(ierr);
86430dcb7c9SBarry Smith 
86507b52d57SBarry Smith   /* sort the node indexing by their global numbers */
86607b52d57SBarry Smith   nt = *nproc;
86707b52d57SBarry Smith   for (i=0; i<nt; i++) {
86832dcc486SBarry Smith     ierr = PetscMalloc(((*numprocs)[i])*sizeof(PetscInt),&tmp);CHKERRQ(ierr);
86907b52d57SBarry Smith     for (j=0; j<(*numprocs)[i]; j++) {
87007b52d57SBarry Smith       tmp[j] = lindices[(*indices)[i][j]];
87107b52d57SBarry Smith     }
87207b52d57SBarry Smith     ierr = PetscSortIntWithArray((*numprocs)[i],tmp,(*indices)[i]);CHKERRQ(ierr);
87307b52d57SBarry Smith     ierr = PetscFree(tmp);CHKERRQ(ierr);
87407b52d57SBarry Smith   }
87507b52d57SBarry Smith 
87607b52d57SBarry Smith   if (debug) { /* -----------------------------------  */
87730dcb7c9SBarry Smith     nt = *nproc;
87830dcb7c9SBarry Smith     for (i=0; i<nt; i++) {
87930dcb7c9SBarry Smith       ierr = PetscSynchronizedPrintf(comm,"[%d] subdomain %d number of indices %d: ",rank,(*procs)[i],(*numprocs)[i]);CHKERRQ(ierr);
88030dcb7c9SBarry Smith       for (j=0; j<(*numprocs)[i]; j++) {
88130dcb7c9SBarry Smith         ierr = PetscSynchronizedPrintf(comm,"%d ",(*indices)[i][j]);CHKERRQ(ierr);
88230dcb7c9SBarry Smith       }
88330dcb7c9SBarry Smith       ierr = PetscSynchronizedPrintf(comm,"\n");CHKERRQ(ierr);
88430dcb7c9SBarry Smith     }
88530dcb7c9SBarry Smith     ierr = PetscSynchronizedFlush(comm);CHKERRQ(ierr);
88607b52d57SBarry Smith   } /* -----------------------------------  */
88730dcb7c9SBarry Smith 
88830dcb7c9SBarry Smith   /* wait on sends */
88930dcb7c9SBarry Smith   if (nsends2) {
890b0a32e0cSBarry Smith     ierr = PetscMalloc(nsends2*sizeof(MPI_Status),&send_status);CHKERRQ(ierr);
89130dcb7c9SBarry Smith     ierr = MPI_Waitall(nsends2,send_waits,send_status);CHKERRQ(ierr);
89230dcb7c9SBarry Smith     ierr = PetscFree(send_status);CHKERRQ(ierr);
89330dcb7c9SBarry Smith   }
89430dcb7c9SBarry Smith 
89530dcb7c9SBarry Smith   ierr = PetscFree(starts3);CHKERRQ(ierr);
89630dcb7c9SBarry Smith   ierr = PetscFree(dest);CHKERRQ(ierr);
89730dcb7c9SBarry Smith   ierr = PetscFree(send_waits);CHKERRQ(ierr);
8983677ff5aSBarry Smith 
899bc8ff85bSBarry Smith   ierr = PetscFree(nownedsenders);CHKERRQ(ierr);
900bc8ff85bSBarry Smith   ierr = PetscFree(ownedsenders);CHKERRQ(ierr);
901bc8ff85bSBarry Smith   ierr = PetscFree(starts);CHKERRQ(ierr);
90230dcb7c9SBarry Smith   ierr = PetscFree(starts2);CHKERRQ(ierr);
90330dcb7c9SBarry Smith   ierr = PetscFree(lens2);CHKERRQ(ierr);
90489d82c54SBarry Smith 
90589d82c54SBarry Smith   ierr = PetscFree(source);CHKERRQ(ierr);
90697f1f81fSBarry Smith   ierr = PetscFree(len);CHKERRQ(ierr);
90789d82c54SBarry Smith   ierr = PetscFree(recvs);CHKERRQ(ierr);
9083a96401aSBarry Smith   ierr = PetscFree(nprocs);CHKERRQ(ierr);
90930dcb7c9SBarry Smith   ierr = PetscFree(sends2);CHKERRQ(ierr);
91024cf384cSBarry Smith 
91124cf384cSBarry Smith   /* put the information about myself as the first entry in the list */
91224cf384cSBarry Smith   first_procs    = (*procs)[0];
91324cf384cSBarry Smith   first_numprocs = (*numprocs)[0];
91424cf384cSBarry Smith   first_indices  = (*indices)[0];
91524cf384cSBarry Smith   for (i=0; i<*nproc; i++) {
91624cf384cSBarry Smith     if ((*procs)[i] == rank) {
91724cf384cSBarry Smith       (*procs)[0]    = (*procs)[i];
91824cf384cSBarry Smith       (*numprocs)[0] = (*numprocs)[i];
91924cf384cSBarry Smith       (*indices)[0]  = (*indices)[i];
92024cf384cSBarry Smith       (*procs)[i]    = first_procs;
92124cf384cSBarry Smith       (*numprocs)[i] = first_numprocs;
92224cf384cSBarry Smith       (*indices)[i]  = first_indices;
92324cf384cSBarry Smith       break;
92424cf384cSBarry Smith     }
92524cf384cSBarry Smith   }
92689d82c54SBarry Smith   PetscFunctionReturn(0);
92789d82c54SBarry Smith }
92889d82c54SBarry Smith 
9294a2ae208SSatish Balay #undef __FUNCT__
9304a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingRestoreInfo"
93107b52d57SBarry Smith /*@C
93207b52d57SBarry Smith     ISLocalToGlobalMappingRestoreInfo - Frees the memory allocated by ISLocalToGlobalMappingGetInfo()
93389d82c54SBarry Smith 
93407b52d57SBarry Smith     Collective on ISLocalToGlobalMapping
93507b52d57SBarry Smith 
93607b52d57SBarry Smith     Input Parameters:
93707b52d57SBarry Smith .   mapping - the mapping from local to global indexing
93807b52d57SBarry Smith 
93907b52d57SBarry Smith     Output Parameter:
94007b52d57SBarry Smith +   nproc - number of processors that are connected to this one
94107b52d57SBarry Smith .   proc - neighboring processors
94207b52d57SBarry Smith .   numproc - number of indices for each processor
94307b52d57SBarry Smith -   indices - indices of local nodes shared with neighbor (sorted by global numbering)
94407b52d57SBarry Smith 
94507b52d57SBarry Smith     Level: advanced
94607b52d57SBarry Smith 
94707b52d57SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreate(),
94807b52d57SBarry Smith           ISLocalToGlobalMappingGetInfo()
94907b52d57SBarry Smith @*/
9507087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingRestoreInfo(ISLocalToGlobalMapping mapping,PetscInt *nproc,PetscInt *procs[],PetscInt *numprocs[],PetscInt **indices[])
95107b52d57SBarry Smith {
9526849ba73SBarry Smith   PetscErrorCode ierr;
95332dcc486SBarry Smith   PetscInt       i;
95407b52d57SBarry Smith 
95507b52d57SBarry Smith   PetscFunctionBegin;
95605b42c5fSBarry Smith   ierr = PetscFree(*procs);CHKERRQ(ierr);
95705b42c5fSBarry Smith   ierr = PetscFree(*numprocs);CHKERRQ(ierr);
95800ff320aSBarry Smith   if (*indices) {
95905b42c5fSBarry Smith     ierr = PetscFree((*indices)[0]);CHKERRQ(ierr);
96000ff320aSBarry Smith     for (i=1; i<*nproc; i++) {
96105b42c5fSBarry Smith       ierr = PetscFree((*indices)[i]);CHKERRQ(ierr);
96207b52d57SBarry Smith     }
96307b52d57SBarry Smith     ierr = PetscFree(*indices);CHKERRQ(ierr);
96424cf384cSBarry Smith   }
96507b52d57SBarry Smith   PetscFunctionReturn(0);
96607b52d57SBarry Smith }
96786994e45SJed Brown 
96886994e45SJed Brown #undef __FUNCT__
96986994e45SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingGetIndices"
97086994e45SJed Brown /*@C
97186994e45SJed Brown    ISLocalToGlobalMappingGetIndices - Get global indices for every local point
97286994e45SJed Brown 
97386994e45SJed Brown    Not Collective
97486994e45SJed Brown 
97586994e45SJed Brown    Input Arguments:
97686994e45SJed Brown . ltog - local to global mapping
97786994e45SJed Brown 
97886994e45SJed Brown    Output Arguments:
97986994e45SJed Brown . array - array of indices
98086994e45SJed Brown 
98186994e45SJed Brown    Level: advanced
98286994e45SJed Brown 
98386994e45SJed Brown .seealso: ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingRestoreIndices()
98486994e45SJed Brown @*/
9857087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingGetIndices(ISLocalToGlobalMapping ltog,const PetscInt **array)
98686994e45SJed Brown {
98786994e45SJed Brown 
98886994e45SJed Brown   PetscFunctionBegin;
98986994e45SJed Brown   PetscValidHeaderSpecific(ltog,IS_LTOGM_CLASSID,1);
99086994e45SJed Brown   PetscValidPointer(array,2);
99186994e45SJed Brown   *array = ltog->indices;
99286994e45SJed Brown   PetscFunctionReturn(0);
99386994e45SJed Brown }
99486994e45SJed Brown 
99586994e45SJed Brown #undef __FUNCT__
99686994e45SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingRestoreIndices"
99786994e45SJed Brown /*@C
99886994e45SJed Brown    ISLocalToGlobalMappingRestoreIndices - Restore indices obtained with ISLocalToGlobalMappingRestoreIndices()
99986994e45SJed Brown 
100086994e45SJed Brown    Not Collective
100186994e45SJed Brown 
100286994e45SJed Brown    Input Arguments:
100386994e45SJed Brown + ltog - local to global mapping
100486994e45SJed Brown - array - array of indices
100586994e45SJed Brown 
100686994e45SJed Brown    Level: advanced
100786994e45SJed Brown 
100886994e45SJed Brown .seealso: ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingGetIndices()
100986994e45SJed Brown @*/
10107087cfbeSBarry Smith PetscErrorCode  ISLocalToGlobalMappingRestoreIndices(ISLocalToGlobalMapping ltog,const PetscInt **array)
101186994e45SJed Brown {
101286994e45SJed Brown 
101386994e45SJed Brown   PetscFunctionBegin;
101486994e45SJed Brown   PetscValidHeaderSpecific(ltog,IS_LTOGM_CLASSID,1);
101586994e45SJed Brown   PetscValidPointer(array,2);
101686994e45SJed Brown   if (*array != ltog->indices) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_BADPTR,"Trying to return mismatched pointer");
101786994e45SJed Brown   *array = PETSC_NULL;
101886994e45SJed Brown   PetscFunctionReturn(0);
101986994e45SJed Brown }
1020