12362add9SBarry Smith 2c6db04a5SJed Brown #include <petscvec.h> /*I "petscvec.h" I*/ 3*b45d2f2cSJed Brown #include <petsc-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) { 697b23a99aSBarry Smith ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_TRUE);CHKERRQ(ierr); 705a5d4f66SBarry Smith for (i=0; i<mapping->n; i++) { 71b0a32e0cSBarry Smith ierr = PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %d %d\n",rank,i,mapping->indices[i]);CHKERRQ(ierr); 726831982aSBarry Smith } 73b0a32e0cSBarry Smith ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 747b23a99aSBarry Smith ierr = PetscViewerASCIISynchronizedAllow(viewer,PETSC_FALSE);CHKERRQ(ierr); 757b23a99aSBarry Smith } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Viewer type %s not supported for ISLocalToGlobalMapping",((PetscObject)viewer)->type_name); 765a5d4f66SBarry Smith PetscFunctionReturn(0); 775a5d4f66SBarry Smith } 785a5d4f66SBarry Smith 794a2ae208SSatish Balay #undef __FUNCT__ 804a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingCreateIS" 811f428162SBarry Smith /*@ 822bdab257SBarry Smith ISLocalToGlobalMappingCreateIS - Creates a mapping between a local (0 to n) 832bdab257SBarry Smith ordering and a global parallel ordering. 842bdab257SBarry Smith 850f5bd95cSBarry Smith Not collective 86b9cd556bSLois Curfman McInnes 87a997ad1aSLois Curfman McInnes Input Parameter: 888c03b21aSDmitry Karpeev . is - index set containing the global numbers for each local number 892bdab257SBarry Smith 90a997ad1aSLois Curfman McInnes Output Parameter: 912bdab257SBarry Smith . mapping - new mapping data structure 922bdab257SBarry Smith 93a997ad1aSLois Curfman McInnes Level: advanced 94a997ad1aSLois Curfman McInnes 95273d9f13SBarry Smith Concepts: mapping^local to global 962bdab257SBarry Smith 972bdab257SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate() 982bdab257SBarry Smith @*/ 997087cfbeSBarry Smith PetscErrorCode ISLocalToGlobalMappingCreateIS(IS is,ISLocalToGlobalMapping *mapping) 1002bdab257SBarry Smith { 1016849ba73SBarry Smith PetscErrorCode ierr; 1025d0c19d7SBarry Smith PetscInt n; 1035d0c19d7SBarry Smith const PetscInt *indices; 1042bdab257SBarry Smith MPI_Comm comm; 1053a40ed3dSBarry Smith 1063a40ed3dSBarry Smith PetscFunctionBegin; 1070700a824SBarry Smith PetscValidHeaderSpecific(is,IS_CLASSID,1); 1084482741eSBarry Smith PetscValidPointer(mapping,2); 1092bdab257SBarry Smith 1102bdab257SBarry Smith ierr = PetscObjectGetComm((PetscObject)is,&comm);CHKERRQ(ierr); 1113b9aefa3SBarry Smith ierr = ISGetLocalSize(is,&n);CHKERRQ(ierr); 1122bdab257SBarry Smith ierr = ISGetIndices(is,&indices);CHKERRQ(ierr); 113d5ad8652SBarry Smith ierr = ISLocalToGlobalMappingCreate(comm,n,indices,PETSC_COPY_VALUES,mapping);CHKERRQ(ierr); 1142bdab257SBarry Smith ierr = ISRestoreIndices(is,&indices);CHKERRQ(ierr); 1153a40ed3dSBarry Smith PetscFunctionReturn(0); 1162bdab257SBarry Smith } 1175a5d4f66SBarry Smith 118a4d96a55SJed Brown #undef __FUNCT__ 119a4d96a55SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingCreateSF" 120a4d96a55SJed Brown /*@C 121a4d96a55SJed Brown ISLocalToGlobalMappingCreateSF - Creates a mapping between a local (0 to n) 122a4d96a55SJed Brown ordering and a global parallel ordering. 123a4d96a55SJed Brown 124a4d96a55SJed Brown Collective 125a4d96a55SJed Brown 126a4d96a55SJed Brown Input Parameter: 127a4d96a55SJed Brown + sf - star forest mapping contiguous local indices to (rank, offset) 128a4d96a55SJed Brown - start - first global index on this process 129a4d96a55SJed Brown 130a4d96a55SJed Brown Output Parameter: 131a4d96a55SJed Brown . mapping - new mapping data structure 132a4d96a55SJed Brown 133a4d96a55SJed Brown Level: advanced 134a4d96a55SJed Brown 135a4d96a55SJed Brown Concepts: mapping^local to global 136a4d96a55SJed Brown 137a4d96a55SJed Brown .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingCreateIS() 138a4d96a55SJed Brown @*/ 139a4d96a55SJed Brown PetscErrorCode ISLocalToGlobalMappingCreateSF(PetscSF sf,PetscInt start,ISLocalToGlobalMapping *mapping) 140a4d96a55SJed Brown { 141a4d96a55SJed Brown PetscErrorCode ierr; 142a4d96a55SJed Brown PetscInt i,maxlocal,nroots,nleaves,*globals,*ltog; 143a4d96a55SJed Brown const PetscInt *ilocal; 144a4d96a55SJed Brown MPI_Comm comm; 145a4d96a55SJed Brown 146a4d96a55SJed Brown PetscFunctionBegin; 147a4d96a55SJed Brown PetscValidHeaderSpecific(sf,PETSCSF_CLASSID,1); 148a4d96a55SJed Brown PetscValidPointer(mapping,3); 149a4d96a55SJed Brown 150a4d96a55SJed Brown ierr = PetscObjectGetComm((PetscObject)sf,&comm);CHKERRQ(ierr); 151a4d96a55SJed Brown ierr = PetscSFGetGraph(sf,&nroots,&nleaves,&ilocal,PETSC_NULL);CHKERRQ(ierr); 152a4d96a55SJed Brown if (ilocal) {for (i=0,maxlocal=0; i<nleaves; i++) maxlocal = PetscMax(maxlocal,ilocal[i]+1);} 153a4d96a55SJed Brown else maxlocal = nleaves; 154a4d96a55SJed Brown ierr = PetscMalloc(nroots*sizeof(PetscInt),&globals);CHKERRQ(ierr); 155a4d96a55SJed Brown ierr = PetscMalloc(maxlocal*sizeof(PetscInt),<og);CHKERRQ(ierr); 156a4d96a55SJed Brown for (i=0; i<nroots; i++) globals[i] = start + i; 157a4d96a55SJed Brown for (i=0; i<maxlocal; i++) ltog[i] = -1; 158a4d96a55SJed Brown ierr = PetscSFBcastBegin(sf,MPIU_INT,globals,ltog);CHKERRQ(ierr); 159a4d96a55SJed Brown ierr = PetscSFBcastEnd(sf,MPIU_INT,globals,ltog);CHKERRQ(ierr); 160a4d96a55SJed Brown ierr = ISLocalToGlobalMappingCreate(comm,maxlocal,ltog,PETSC_OWN_POINTER,mapping);CHKERRQ(ierr); 161a4d96a55SJed Brown ierr = PetscFree(globals);CHKERRQ(ierr); 162a4d96a55SJed Brown PetscFunctionReturn(0); 163a4d96a55SJed Brown } 164b46b645bSBarry Smith 1654a2ae208SSatish Balay #undef __FUNCT__ 1664a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingCreate" 167ba5bb76aSSatish Balay /*@ 16890f02eecSBarry Smith ISLocalToGlobalMappingCreate - Creates a mapping between a local (0 to n) 16990f02eecSBarry Smith ordering and a global parallel ordering. 1702362add9SBarry Smith 17189d82c54SBarry Smith Not Collective, but communicator may have more than one process 172b9cd556bSLois Curfman McInnes 1732362add9SBarry Smith Input Parameters: 17489d82c54SBarry Smith + comm - MPI communicator 17590f02eecSBarry Smith . n - the number of local elements 176d5ad8652SBarry Smith . indices - the global index for each local element 177d5ad8652SBarry Smith - mode - see PetscCopyMode 1782362add9SBarry Smith 179a997ad1aSLois Curfman McInnes Output Parameter: 18090f02eecSBarry Smith . mapping - new mapping data structure 1812362add9SBarry Smith 182a997ad1aSLois Curfman McInnes Level: advanced 183a997ad1aSLois Curfman McInnes 184273d9f13SBarry Smith Concepts: mapping^local to global 1852362add9SBarry Smith 186d5ad8652SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS() 1872362add9SBarry Smith @*/ 1887087cfbeSBarry Smith PetscErrorCode ISLocalToGlobalMappingCreate(MPI_Comm cm,PetscInt n,const PetscInt indices[],PetscCopyMode mode,ISLocalToGlobalMapping *mapping) 1892362add9SBarry Smith { 1906849ba73SBarry Smith PetscErrorCode ierr; 19132dcc486SBarry Smith PetscInt *in; 192b46b645bSBarry Smith 193b46b645bSBarry Smith PetscFunctionBegin; 19473911063SBarry Smith if (n) PetscValidIntPointer(indices,3); 1954482741eSBarry Smith PetscValidPointer(mapping,4); 196b46b645bSBarry Smith 1978e58c17dSMatthew Knepley *mapping = PETSC_NULL; 1988e58c17dSMatthew Knepley #ifndef PETSC_USE_DYNAMIC_LIBRARIES 1992b6de112SBarry Smith ierr = ISInitializePackage(PETSC_NULL);CHKERRQ(ierr); 2008e58c17dSMatthew Knepley #endif 2012362add9SBarry Smith 2023194b578SJed Brown ierr = PetscHeaderCreate(*mapping,_p_ISLocalToGlobalMapping,int,IS_LTOGM_CLASSID,0,"ISLocalToGlobalMapping","Local to global mapping","IS", 20352e6d16bSBarry Smith cm,ISLocalToGlobalMappingDestroy,ISLocalToGlobalMappingView);CHKERRQ(ierr); 204d4bb536fSBarry Smith (*mapping)->n = n; 205d4bb536fSBarry Smith /* 206d4bb536fSBarry Smith Do not create the global to local mapping. This is only created if 207d4bb536fSBarry Smith ISGlobalToLocalMapping() is called 208d4bb536fSBarry Smith */ 209d4bb536fSBarry Smith (*mapping)->globals = 0; 210d5ad8652SBarry Smith if (mode == PETSC_COPY_VALUES) { 211d5ad8652SBarry Smith ierr = PetscMalloc(n*sizeof(PetscInt),&in);CHKERRQ(ierr); 212d5ad8652SBarry Smith ierr = PetscMemcpy(in,indices,n*sizeof(PetscInt));CHKERRQ(ierr); 213d5ad8652SBarry Smith ierr = PetscLogObjectMemory(*mapping,n*sizeof(PetscInt));CHKERRQ(ierr); 214d5ad8652SBarry Smith (*mapping)->indices = in; 215d5ad8652SBarry Smith } else if (mode == PETSC_OWN_POINTER) { 216d5ad8652SBarry Smith (*mapping)->indices = (PetscInt*)indices; 217d5ad8652SBarry Smith } else SETERRQ(cm,PETSC_ERR_SUP,"Cannot currently use PETSC_USE_POINTER"); 2183a40ed3dSBarry Smith PetscFunctionReturn(0); 2192362add9SBarry Smith } 2202362add9SBarry Smith 2214a2ae208SSatish Balay #undef __FUNCT__ 2224a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingBlock" 223bce096a4SSatish Balay /*@ 224323b833fSBarry Smith ISLocalToGlobalMappingBlock - Creates a blocked index version of an 225323b833fSBarry Smith ISLocalToGlobalMapping that is appropriate for MatSetLocalToGlobalMappingBlock() 226323b833fSBarry Smith and VecSetLocalToGlobalMappingBlock(). 227323b833fSBarry Smith 228323b833fSBarry Smith Not Collective, but communicator may have more than one process 229323b833fSBarry Smith 230323b833fSBarry Smith Input Parameters: 231323b833fSBarry Smith + inmap - original point-wise mapping 232323b833fSBarry Smith - bs - block size 233323b833fSBarry Smith 234323b833fSBarry Smith Output Parameter: 23569eb54c3SBarry Smith . outmap - block based mapping; the indices are relative to BLOCKS, not individual vector or matrix entries. 236323b833fSBarry Smith 237323b833fSBarry Smith Level: advanced 238323b833fSBarry Smith 239323b833fSBarry Smith Concepts: mapping^local to global 240323b833fSBarry Smith 241323b833fSBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingCreateIS() 242323b833fSBarry Smith @*/ 2437087cfbeSBarry Smith PetscErrorCode ISLocalToGlobalMappingBlock(ISLocalToGlobalMapping inmap,PetscInt bs,ISLocalToGlobalMapping *outmap) 244323b833fSBarry Smith { 2456849ba73SBarry Smith PetscErrorCode ierr; 24632dcc486SBarry Smith PetscInt *ii,i,n; 247323b833fSBarry Smith 248323b833fSBarry Smith PetscFunctionBegin; 2490700a824SBarry Smith PetscValidHeaderSpecific(inmap,IS_LTOGM_CLASSID,1); 250b2beed0aSJed Brown PetscValidPointer(outmap,3); 251323b833fSBarry Smith if (bs > 1) { 252323b833fSBarry Smith n = inmap->n/bs; 253e32f2f54SBarry Smith if (n*bs != inmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Pointwise mapping length is not divisible by block size"); 25432dcc486SBarry Smith ierr = PetscMalloc(n*sizeof(PetscInt),&ii);CHKERRQ(ierr); 255323b833fSBarry Smith for (i=0; i<n; i++) { 256db032c9dSBarry Smith ii[i] = inmap->indices[bs*i]/bs; 257323b833fSBarry Smith } 258d5ad8652SBarry Smith ierr = ISLocalToGlobalMappingCreate(((PetscObject)inmap)->comm,n,ii,PETSC_OWN_POINTER,outmap);CHKERRQ(ierr); 259323b833fSBarry Smith } else { 260323b833fSBarry Smith ierr = PetscObjectReference((PetscObject)inmap);CHKERRQ(ierr); 261c3122656SLisandro Dalcin *outmap = inmap; 262323b833fSBarry Smith } 263323b833fSBarry Smith PetscFunctionReturn(0); 264323b833fSBarry Smith } 265323b833fSBarry Smith 2664a2ae208SSatish Balay #undef __FUNCT__ 2678ab951edSJed Brown #define __FUNCT__ "ISLocalToGlobalMappingUnBlock" 268b2beed0aSJed Brown /*@ 269b2beed0aSJed Brown ISLocalToGlobalMappingUnBlock - Creates a scalar index version of a blocked 270b2beed0aSJed Brown ISLocalToGlobalMapping 271b2beed0aSJed Brown 272b2beed0aSJed Brown Not Collective, but communicator may have more than one process 273b2beed0aSJed Brown 274b2beed0aSJed Brown Input Parameter: 275b2beed0aSJed Brown + inmap - block based mapping; the indices are relative to BLOCKS, not individual vector or matrix entries. 276b2beed0aSJed Brown - bs - block size 277b2beed0aSJed Brown 278b2beed0aSJed Brown Output Parameter: 279b2beed0aSJed Brown . outmap - pointwise mapping 280b2beed0aSJed Brown 281b2beed0aSJed Brown Level: advanced 282b2beed0aSJed Brown 283b2beed0aSJed Brown Concepts: mapping^local to global 284b2beed0aSJed Brown 285b2beed0aSJed Brown .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingBlock() 286b2beed0aSJed Brown @*/ 2877087cfbeSBarry Smith PetscErrorCode ISLocalToGlobalMappingUnBlock(ISLocalToGlobalMapping inmap,PetscInt bs,ISLocalToGlobalMapping *outmap) 288b2beed0aSJed Brown { 289b2beed0aSJed Brown PetscErrorCode ierr; 290b2beed0aSJed Brown PetscInt *ii,i,n; 291b2beed0aSJed Brown 292b2beed0aSJed Brown PetscFunctionBegin; 293b2beed0aSJed Brown PetscValidHeaderSpecific(inmap,IS_LTOGM_CLASSID,1); 294b2beed0aSJed Brown PetscValidPointer(outmap,2); 295b2beed0aSJed Brown if (bs > 1) { 296b2beed0aSJed Brown n = inmap->n*bs; 297b2beed0aSJed Brown ierr = PetscMalloc(n*sizeof(PetscInt),&ii);CHKERRQ(ierr); 298b2beed0aSJed Brown for (i=0; i<n; i++) { 29994f4c46bSJed Brown ii[i] = inmap->indices[i/bs]*bs + (i%bs); 300b2beed0aSJed Brown } 301b2beed0aSJed Brown ierr = ISLocalToGlobalMappingCreate(((PetscObject)inmap)->comm,n,ii,PETSC_OWN_POINTER,outmap);CHKERRQ(ierr); 302b2beed0aSJed Brown } else { 303b2beed0aSJed Brown ierr = PetscObjectReference((PetscObject)inmap);CHKERRQ(ierr); 304b2beed0aSJed Brown *outmap = inmap; 305b2beed0aSJed Brown } 306b2beed0aSJed Brown PetscFunctionReturn(0); 307b2beed0aSJed Brown } 308b2beed0aSJed Brown 309b2beed0aSJed Brown #undef __FUNCT__ 3104a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingDestroy" 31190f02eecSBarry Smith /*@ 31290f02eecSBarry Smith ISLocalToGlobalMappingDestroy - Destroys a mapping between a local (0 to n) 31390f02eecSBarry Smith ordering and a global parallel ordering. 31490f02eecSBarry Smith 3150f5bd95cSBarry Smith Note Collective 316b9cd556bSLois Curfman McInnes 31790f02eecSBarry Smith Input Parameters: 31890f02eecSBarry Smith . mapping - mapping data structure 31990f02eecSBarry Smith 320a997ad1aSLois Curfman McInnes Level: advanced 321a997ad1aSLois Curfman McInnes 3223acfe500SLois Curfman McInnes .seealso: ISLocalToGlobalMappingCreate() 32390f02eecSBarry Smith @*/ 3246bf464f9SBarry Smith PetscErrorCode ISLocalToGlobalMappingDestroy(ISLocalToGlobalMapping *mapping) 32590f02eecSBarry Smith { 326dfbe8321SBarry Smith PetscErrorCode ierr; 3273a40ed3dSBarry Smith PetscFunctionBegin; 3286bf464f9SBarry Smith if (!*mapping) PetscFunctionReturn(0); 3296bf464f9SBarry Smith PetscValidHeaderSpecific((*mapping),IS_LTOGM_CLASSID,1); 330997056adSBarry Smith if (--((PetscObject)(*mapping))->refct > 0) {*mapping = 0;PetscFunctionReturn(0);} 3316bf464f9SBarry Smith ierr = PetscFree((*mapping)->indices);CHKERRQ(ierr); 3326bf464f9SBarry Smith ierr = PetscFree((*mapping)->globals);CHKERRQ(ierr); 333d38fa0fbSBarry Smith ierr = PetscHeaderDestroy(mapping);CHKERRQ(ierr); 334992144d0SBarry Smith *mapping = 0; 3353a40ed3dSBarry Smith PetscFunctionReturn(0); 33690f02eecSBarry Smith } 33790f02eecSBarry Smith 3384a2ae208SSatish Balay #undef __FUNCT__ 3394a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingApplyIS" 34090f02eecSBarry Smith /*@ 3413acfe500SLois Curfman McInnes ISLocalToGlobalMappingApplyIS - Creates from an IS in the local numbering 3423acfe500SLois Curfman McInnes a new index set using the global numbering defined in an ISLocalToGlobalMapping 3433acfe500SLois Curfman McInnes context. 34490f02eecSBarry Smith 345b9cd556bSLois Curfman McInnes Not collective 346b9cd556bSLois Curfman McInnes 34790f02eecSBarry Smith Input Parameters: 348b9cd556bSLois Curfman McInnes + mapping - mapping between local and global numbering 349b9cd556bSLois Curfman McInnes - is - index set in local numbering 35090f02eecSBarry Smith 35190f02eecSBarry Smith Output Parameters: 35290f02eecSBarry Smith . newis - index set in global numbering 35390f02eecSBarry Smith 354a997ad1aSLois Curfman McInnes Level: advanced 355a997ad1aSLois Curfman McInnes 356273d9f13SBarry Smith Concepts: mapping^local to global 3573acfe500SLois Curfman McInnes 35890f02eecSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(), 359d4bb536fSBarry Smith ISLocalToGlobalMappingDestroy(), ISGlobalToLocalMappingApply() 36090f02eecSBarry Smith @*/ 3617087cfbeSBarry Smith PetscErrorCode ISLocalToGlobalMappingApplyIS(ISLocalToGlobalMapping mapping,IS is,IS *newis) 36290f02eecSBarry Smith { 3636849ba73SBarry Smith PetscErrorCode ierr; 3645d0c19d7SBarry Smith PetscInt n,i,*idxmap,*idxout,Nmax = mapping->n; 3655d0c19d7SBarry Smith const PetscInt *idxin; 3663a40ed3dSBarry Smith 3673a40ed3dSBarry Smith PetscFunctionBegin; 3680700a824SBarry Smith PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1); 3690700a824SBarry Smith PetscValidHeaderSpecific(is,IS_CLASSID,2); 3704482741eSBarry Smith PetscValidPointer(newis,3); 37190f02eecSBarry Smith 3723b9aefa3SBarry Smith ierr = ISGetLocalSize(is,&n);CHKERRQ(ierr); 37390f02eecSBarry Smith ierr = ISGetIndices(is,&idxin);CHKERRQ(ierr); 37490f02eecSBarry Smith idxmap = mapping->indices; 37590f02eecSBarry Smith 3767c334f02SBarry Smith ierr = PetscMalloc(n*sizeof(PetscInt),&idxout);CHKERRQ(ierr); 37790f02eecSBarry Smith for (i=0; i<n; i++) { 378e32f2f54SBarry 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); 37990f02eecSBarry Smith idxout[i] = idxmap[idxin[i]]; 38090f02eecSBarry Smith } 3813b9aefa3SBarry Smith ierr = ISRestoreIndices(is,&idxin);CHKERRQ(ierr); 38270b3c8c7SBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,n,idxout,PETSC_OWN_POINTER,newis);CHKERRQ(ierr); 3833a40ed3dSBarry Smith PetscFunctionReturn(0); 38490f02eecSBarry Smith } 38590f02eecSBarry Smith 38689d82c54SBarry Smith /*MC 3873acfe500SLois Curfman McInnes ISLocalToGlobalMappingApply - Takes a list of integers in a local numbering 3883acfe500SLois Curfman McInnes and converts them to the global numbering. 38990f02eecSBarry Smith 390eca87e8dSBarry Smith Synopsis: 391eca87e8dSBarry Smith PetscErrorCode ISLocalToGlobalMappingApply(ISLocalToGlobalMapping mapping,int N,int in[],int out[]) 392eca87e8dSBarry Smith 393b9cd556bSLois Curfman McInnes Not collective 394b9cd556bSLois Curfman McInnes 395bb25748dSBarry Smith Input Parameters: 396b9cd556bSLois Curfman McInnes + mapping - the local to global mapping context 397bb25748dSBarry Smith . N - number of integers 398b9cd556bSLois Curfman McInnes - in - input indices in local numbering 399bb25748dSBarry Smith 400bb25748dSBarry Smith Output Parameter: 401bb25748dSBarry Smith . out - indices in global numbering 402bb25748dSBarry Smith 403b9cd556bSLois Curfman McInnes Notes: 404b9cd556bSLois Curfman McInnes The in and out array parameters may be identical. 405d4bb536fSBarry Smith 406a997ad1aSLois Curfman McInnes Level: advanced 407a997ad1aSLois Curfman McInnes 408bb25748dSBarry Smith .seealso: ISLocalToGlobalMappingCreate(),ISLocalToGlobalMappingDestroy(), 4090752156aSBarry Smith ISLocalToGlobalMappingApplyIS(),AOCreateBasic(),AOApplicationToPetsc(), 410d4bb536fSBarry Smith AOPetscToApplication(), ISGlobalToLocalMappingApply() 411bb25748dSBarry Smith 412273d9f13SBarry Smith Concepts: mapping^local to global 413d4bb536fSBarry Smith 41489d82c54SBarry Smith M*/ 415d4bb536fSBarry Smith 416d4bb536fSBarry Smith /* -----------------------------------------------------------------------------------------*/ 417d4bb536fSBarry Smith 4184a2ae208SSatish Balay #undef __FUNCT__ 4194a2ae208SSatish Balay #define __FUNCT__ "ISGlobalToLocalMappingSetUp_Private" 420d4bb536fSBarry Smith /* 421d4bb536fSBarry Smith Creates the global fields in the ISLocalToGlobalMapping structure 422d4bb536fSBarry Smith */ 4236849ba73SBarry Smith static PetscErrorCode ISGlobalToLocalMappingSetUp_Private(ISLocalToGlobalMapping mapping) 424d4bb536fSBarry Smith { 4256849ba73SBarry Smith PetscErrorCode ierr; 42632dcc486SBarry Smith PetscInt i,*idx = mapping->indices,n = mapping->n,end,start,*globals; 427d4bb536fSBarry Smith 4283a40ed3dSBarry Smith PetscFunctionBegin; 429d4bb536fSBarry Smith end = 0; 430ec268f7cSJed Brown start = PETSC_MAX_INT; 431d4bb536fSBarry Smith 432d4bb536fSBarry Smith for (i=0; i<n; i++) { 433d4bb536fSBarry Smith if (idx[i] < 0) continue; 434d4bb536fSBarry Smith if (idx[i] < start) start = idx[i]; 435d4bb536fSBarry Smith if (idx[i] > end) end = idx[i]; 436d4bb536fSBarry Smith } 437d4bb536fSBarry Smith if (start > end) {start = 0; end = -1;} 438d4bb536fSBarry Smith mapping->globalstart = start; 439d4bb536fSBarry Smith mapping->globalend = end; 440d4bb536fSBarry Smith 44132dcc486SBarry Smith ierr = PetscMalloc((end-start+2)*sizeof(PetscInt),&globals);CHKERRQ(ierr); 442b0a32e0cSBarry Smith mapping->globals = globals; 443d4bb536fSBarry Smith for (i=0; i<end-start+1; i++) { 444d4bb536fSBarry Smith globals[i] = -1; 445d4bb536fSBarry Smith } 446d4bb536fSBarry Smith for (i=0; i<n; i++) { 447d4bb536fSBarry Smith if (idx[i] < 0) continue; 448d4bb536fSBarry Smith globals[idx[i] - start] = i; 449d4bb536fSBarry Smith } 450d4bb536fSBarry Smith 45152e6d16bSBarry Smith ierr = PetscLogObjectMemory(mapping,(end-start+1)*sizeof(PetscInt));CHKERRQ(ierr); 4523a40ed3dSBarry Smith PetscFunctionReturn(0); 453d4bb536fSBarry Smith } 454d4bb536fSBarry Smith 4554a2ae208SSatish Balay #undef __FUNCT__ 4564a2ae208SSatish Balay #define __FUNCT__ "ISGlobalToLocalMappingApply" 457d4bb536fSBarry Smith /*@ 458a997ad1aSLois Curfman McInnes ISGlobalToLocalMappingApply - Provides the local numbering for a list of integers 459a997ad1aSLois Curfman McInnes specified with a global numbering. 460d4bb536fSBarry Smith 461b9cd556bSLois Curfman McInnes Not collective 462b9cd556bSLois Curfman McInnes 463d4bb536fSBarry Smith Input Parameters: 464b9cd556bSLois Curfman McInnes + mapping - mapping between local and global numbering 465d4bb536fSBarry Smith . type - IS_GTOLM_MASK - replaces global indices with no local value with -1 466d4bb536fSBarry Smith IS_GTOLM_DROP - drops the indices with no local value from the output list 467d4bb536fSBarry Smith . n - number of global indices to map 468b9cd556bSLois Curfman McInnes - idx - global indices to map 469d4bb536fSBarry Smith 470d4bb536fSBarry Smith Output Parameters: 471b9cd556bSLois Curfman McInnes + nout - number of indices in output array (if type == IS_GTOLM_MASK then nout = n) 472b9cd556bSLois Curfman McInnes - idxout - local index of each global index, one must pass in an array long enough 473e182c471SBarry Smith to hold all the indices. You can call ISGlobalToLocalMappingApply() with 474e182c471SBarry Smith idxout == PETSC_NULL to determine the required length (returned in nout) 475e182c471SBarry Smith and then allocate the required space and call ISGlobalToLocalMappingApply() 476e182c471SBarry Smith a second time to set the values. 477d4bb536fSBarry Smith 478b9cd556bSLois Curfman McInnes Notes: 479b9cd556bSLois Curfman McInnes Either nout or idxout may be PETSC_NULL. idx and idxout may be identical. 480d4bb536fSBarry Smith 4810f5bd95cSBarry Smith This is not scalable in memory usage. Each processor requires O(Nglobal) size 4820f5bd95cSBarry Smith array to compute these. 4830f5bd95cSBarry Smith 484a997ad1aSLois Curfman McInnes Level: advanced 485a997ad1aSLois Curfman McInnes 48632fd6b96SBarry Smith Developer Note: The manual page states that idx and idxout may be identical but the calling 48732fd6b96SBarry Smith sequence declares idx as const so it cannot be the same as idxout. 48832fd6b96SBarry Smith 489273d9f13SBarry Smith Concepts: mapping^global to local 490d4bb536fSBarry Smith 491d4bb536fSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(), 492d4bb536fSBarry Smith ISLocalToGlobalMappingDestroy() 493d4bb536fSBarry Smith @*/ 4947087cfbeSBarry Smith PetscErrorCode ISGlobalToLocalMappingApply(ISLocalToGlobalMapping mapping,ISGlobalToLocalMappingType type, 49532dcc486SBarry Smith PetscInt n,const PetscInt idx[],PetscInt *nout,PetscInt idxout[]) 496d4bb536fSBarry Smith { 49732dcc486SBarry Smith PetscInt i,*globals,nf = 0,tmp,start,end; 4986849ba73SBarry Smith PetscErrorCode ierr; 499d4bb536fSBarry Smith 5003a40ed3dSBarry Smith PetscFunctionBegin; 5010700a824SBarry Smith PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1); 502d4bb536fSBarry Smith if (!mapping->globals) { 503d4bb536fSBarry Smith ierr = ISGlobalToLocalMappingSetUp_Private(mapping);CHKERRQ(ierr); 504d4bb536fSBarry Smith } 505d4bb536fSBarry Smith globals = mapping->globals; 506d4bb536fSBarry Smith start = mapping->globalstart; 507d4bb536fSBarry Smith end = mapping->globalend; 508d4bb536fSBarry Smith 509d4bb536fSBarry Smith if (type == IS_GTOLM_MASK) { 510d4bb536fSBarry Smith if (idxout) { 511d4bb536fSBarry Smith for (i=0; i<n; i++) { 512d4bb536fSBarry Smith if (idx[i] < 0) idxout[i] = idx[i]; 513d4bb536fSBarry Smith else if (idx[i] < start) idxout[i] = -1; 514d4bb536fSBarry Smith else if (idx[i] > end) idxout[i] = -1; 515d4bb536fSBarry Smith else idxout[i] = globals[idx[i] - start]; 516d4bb536fSBarry Smith } 517d4bb536fSBarry Smith } 518d4bb536fSBarry Smith if (nout) *nout = n; 519d4bb536fSBarry Smith } else { 520d4bb536fSBarry Smith if (idxout) { 521d4bb536fSBarry Smith for (i=0; i<n; i++) { 522d4bb536fSBarry Smith if (idx[i] < 0) continue; 523d4bb536fSBarry Smith if (idx[i] < start) continue; 524d4bb536fSBarry Smith if (idx[i] > end) continue; 525d4bb536fSBarry Smith tmp = globals[idx[i] - start]; 526d4bb536fSBarry Smith if (tmp < 0) continue; 527d4bb536fSBarry Smith idxout[nf++] = tmp; 528d4bb536fSBarry Smith } 529d4bb536fSBarry Smith } else { 530d4bb536fSBarry Smith for (i=0; i<n; i++) { 531d4bb536fSBarry Smith if (idx[i] < 0) continue; 532d4bb536fSBarry Smith if (idx[i] < start) continue; 533d4bb536fSBarry Smith if (idx[i] > end) continue; 534d4bb536fSBarry Smith tmp = globals[idx[i] - start]; 535d4bb536fSBarry Smith if (tmp < 0) continue; 536d4bb536fSBarry Smith nf++; 537d4bb536fSBarry Smith } 538d4bb536fSBarry Smith } 539d4bb536fSBarry Smith if (nout) *nout = nf; 540d4bb536fSBarry Smith } 541d4bb536fSBarry Smith 5423a40ed3dSBarry Smith PetscFunctionReturn(0); 543d4bb536fSBarry Smith } 54490f02eecSBarry Smith 5454a2ae208SSatish Balay #undef __FUNCT__ 5464a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingGetInfo" 54789d82c54SBarry Smith /*@C 54889d82c54SBarry Smith ISLocalToGlobalMappingGetInfo - Gets the neighbor information for each processor and 54989d82c54SBarry Smith each index shared by more than one processor 55089d82c54SBarry Smith 55189d82c54SBarry Smith Collective on ISLocalToGlobalMapping 55289d82c54SBarry Smith 55389d82c54SBarry Smith Input Parameters: 55489d82c54SBarry Smith . mapping - the mapping from local to global indexing 55589d82c54SBarry Smith 55689d82c54SBarry Smith Output Parameter: 55789d82c54SBarry Smith + nproc - number of processors that are connected to this one 55889d82c54SBarry Smith . proc - neighboring processors 55907b52d57SBarry Smith . numproc - number of indices for each subdomain (processor) 5603463a7baSJed Brown - indices - indices of nodes (in local numbering) shared with neighbors (sorted by global numbering) 56189d82c54SBarry Smith 56289d82c54SBarry Smith Level: advanced 56389d82c54SBarry Smith 564273d9f13SBarry Smith Concepts: mapping^local to global 56589d82c54SBarry Smith 5662cfcea29SBarry Smith Fortran Usage: 5672cfcea29SBarry Smith $ ISLocalToGlobalMpngGetInfoSize(ISLocalToGlobalMapping,PetscInt nproc,PetscInt numprocmax,ierr) followed by 5682cfcea29SBarry Smith $ ISLocalToGlobalMappingGetInfo(ISLocalToGlobalMapping,PetscInt nproc, PetscInt procs[nproc],PetscInt numprocs[nproc], 5692cfcea29SBarry Smith PetscInt indices[nproc][numprocmax],ierr) 5702cfcea29SBarry Smith There is no ISLocalToGlobalMappingRestoreInfo() in Fortran. You must make sure that procs[], numprocs[] and 5712cfcea29SBarry Smith indices[][] are large enough arrays, either by allocating them dynamically or defining static ones large enough. 5722cfcea29SBarry Smith 5732cfcea29SBarry Smith 57407b52d57SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreate(), 57507b52d57SBarry Smith ISLocalToGlobalMappingRestoreInfo() 57689d82c54SBarry Smith @*/ 5777087cfbeSBarry Smith PetscErrorCode ISLocalToGlobalMappingGetInfo(ISLocalToGlobalMapping mapping,PetscInt *nproc,PetscInt *procs[],PetscInt *numprocs[],PetscInt **indices[]) 57889d82c54SBarry Smith { 5796849ba73SBarry Smith PetscErrorCode ierr; 58097f1f81fSBarry Smith PetscMPIInt size,rank,tag1,tag2,tag3,*len,*source,imdex; 58132dcc486SBarry Smith PetscInt i,n = mapping->n,Ng,ng,max = 0,*lindices = mapping->indices; 58232dcc486SBarry Smith PetscInt *nprocs,*owner,nsends,*sends,j,*starts,nmax,nrecvs,*recvs,proc; 58397f1f81fSBarry Smith PetscInt cnt,scale,*ownedsenders,*nownedsenders,rstart,nowned; 58432dcc486SBarry Smith PetscInt node,nownedm,nt,*sends2,nsends2,*starts2,*lens2,*dest,nrecvs2,*starts3,*recvs2,k,*bprocs,*tmp; 58532dcc486SBarry Smith PetscInt first_procs,first_numprocs,*first_indices; 58689d82c54SBarry Smith MPI_Request *recv_waits,*send_waits; 58730dcb7c9SBarry Smith MPI_Status recv_status,*send_status,*recv_statuses; 5887adad957SLisandro Dalcin MPI_Comm comm = ((PetscObject)mapping)->comm; 589ace3abfcSBarry Smith PetscBool debug = PETSC_FALSE; 59089d82c54SBarry Smith 59189d82c54SBarry Smith PetscFunctionBegin; 5920700a824SBarry Smith PetscValidHeaderSpecific(mapping,IS_LTOGM_CLASSID,1); 59324cf384cSBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 59424cf384cSBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 59524cf384cSBarry Smith if (size == 1) { 59624cf384cSBarry Smith *nproc = 0; 59724cf384cSBarry Smith *procs = PETSC_NULL; 59832dcc486SBarry Smith ierr = PetscMalloc(sizeof(PetscInt),numprocs);CHKERRQ(ierr); 5991e2105dcSBarry Smith (*numprocs)[0] = 0; 60032dcc486SBarry Smith ierr = PetscMalloc(sizeof(PetscInt*),indices);CHKERRQ(ierr); 6011e2105dcSBarry Smith (*indices)[0] = PETSC_NULL; 60224cf384cSBarry Smith PetscFunctionReturn(0); 60324cf384cSBarry Smith } 60424cf384cSBarry Smith 605acfcf0e5SJed Brown ierr = PetscOptionsGetBool(PETSC_NULL,"-islocaltoglobalmappinggetinfo_debug",&debug,PETSC_NULL);CHKERRQ(ierr); 60607b52d57SBarry Smith 6073677ff5aSBarry Smith /* 6083677ff5aSBarry Smith Notes on ISLocalToGlobalMappingGetInfo 6093677ff5aSBarry Smith 6103677ff5aSBarry Smith globally owned node - the nodes that have been assigned to this processor in global 6113677ff5aSBarry Smith numbering, just for this routine. 6123677ff5aSBarry Smith 6133677ff5aSBarry Smith nontrivial globally owned node - node assigned to this processor that is on a subdomain 6143677ff5aSBarry Smith boundary (i.e. is has more than one local owner) 6153677ff5aSBarry Smith 6163677ff5aSBarry Smith locally owned node - node that exists on this processors subdomain 6173677ff5aSBarry Smith 6183677ff5aSBarry Smith nontrivial locally owned node - node that is not in the interior (i.e. has more than one 6193677ff5aSBarry Smith local subdomain 6203677ff5aSBarry Smith */ 62124cf384cSBarry Smith ierr = PetscObjectGetNewTag((PetscObject)mapping,&tag1);CHKERRQ(ierr); 62224cf384cSBarry Smith ierr = PetscObjectGetNewTag((PetscObject)mapping,&tag2);CHKERRQ(ierr); 62324cf384cSBarry Smith ierr = PetscObjectGetNewTag((PetscObject)mapping,&tag3);CHKERRQ(ierr); 62489d82c54SBarry Smith 62589d82c54SBarry Smith for (i=0; i<n; i++) { 62689d82c54SBarry Smith if (lindices[i] > max) max = lindices[i]; 62789d82c54SBarry Smith } 62832dcc486SBarry Smith ierr = MPI_Allreduce(&max,&Ng,1,MPIU_INT,MPI_MAX,comm);CHKERRQ(ierr); 62978058e43SBarry Smith Ng++; 63089d82c54SBarry Smith ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 63189d82c54SBarry Smith ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 632bc8ff85bSBarry Smith scale = Ng/size + 1; 633a2e34c3dSBarry Smith ng = scale; if (rank == size-1) ng = Ng - scale*(size-1); ng = PetscMax(1,ng); 634caba0dd0SBarry Smith rstart = scale*rank; 63589d82c54SBarry Smith 63689d82c54SBarry Smith /* determine ownership ranges of global indices */ 6377c334f02SBarry Smith ierr = PetscMalloc(2*size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr); 63832dcc486SBarry Smith ierr = PetscMemzero(nprocs,2*size*sizeof(PetscInt));CHKERRQ(ierr); 63989d82c54SBarry Smith 64089d82c54SBarry Smith /* determine owners of each local node */ 6417c334f02SBarry Smith ierr = PetscMalloc(n*sizeof(PetscInt),&owner);CHKERRQ(ierr); 64289d82c54SBarry Smith for (i=0; i<n; i++) { 6433677ff5aSBarry Smith proc = lindices[i]/scale; /* processor that globally owns this index */ 64427c402fcSBarry Smith nprocs[2*proc+1] = 1; /* processor globally owns at least one of ours */ 6453677ff5aSBarry Smith owner[i] = proc; 64627c402fcSBarry Smith nprocs[2*proc]++; /* count of how many that processor globally owns of ours */ 64789d82c54SBarry Smith } 64827c402fcSBarry Smith nsends = 0; for (i=0; i<size; i++) nsends += nprocs[2*i+1]; 6491e2582c4SBarry Smith ierr = PetscInfo1(mapping,"Number of global owners for my local data %d\n",nsends);CHKERRQ(ierr); 65089d82c54SBarry Smith 65189d82c54SBarry Smith /* inform other processors of number of messages and max length*/ 65227c402fcSBarry Smith ierr = PetscMaxSum(comm,nprocs,&nmax,&nrecvs);CHKERRQ(ierr); 6531e2582c4SBarry Smith ierr = PetscInfo1(mapping,"Number of local owners for my global data %d\n",nrecvs);CHKERRQ(ierr); 65489d82c54SBarry Smith 65589d82c54SBarry Smith /* post receives for owned rows */ 65632dcc486SBarry Smith ierr = PetscMalloc((2*nrecvs+1)*(nmax+1)*sizeof(PetscInt),&recvs);CHKERRQ(ierr); 657b0a32e0cSBarry Smith ierr = PetscMalloc((nrecvs+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr); 65889d82c54SBarry Smith for (i=0; i<nrecvs; i++) { 65932dcc486SBarry Smith ierr = MPI_Irecv(recvs+2*nmax*i,2*nmax,MPIU_INT,MPI_ANY_SOURCE,tag1,comm,recv_waits+i);CHKERRQ(ierr); 66089d82c54SBarry Smith } 66189d82c54SBarry Smith 66289d82c54SBarry Smith /* pack messages containing lists of local nodes to owners */ 66332dcc486SBarry Smith ierr = PetscMalloc((2*n+1)*sizeof(PetscInt),&sends);CHKERRQ(ierr); 66432dcc486SBarry Smith ierr = PetscMalloc((size+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr); 66589d82c54SBarry Smith starts[0] = 0; 66627c402fcSBarry Smith for (i=1; i<size; i++) { starts[i] = starts[i-1] + 2*nprocs[2*i-2];} 66789d82c54SBarry Smith for (i=0; i<n; i++) { 66889d82c54SBarry Smith sends[starts[owner[i]]++] = lindices[i]; 66930dcb7c9SBarry Smith sends[starts[owner[i]]++] = i; 67089d82c54SBarry Smith } 67189d82c54SBarry Smith ierr = PetscFree(owner);CHKERRQ(ierr); 67289d82c54SBarry Smith starts[0] = 0; 67327c402fcSBarry Smith for (i=1; i<size; i++) { starts[i] = starts[i-1] + 2*nprocs[2*i-2];} 67489d82c54SBarry Smith 67589d82c54SBarry Smith /* send the messages */ 676b0a32e0cSBarry Smith ierr = PetscMalloc((nsends+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr); 67732dcc486SBarry Smith ierr = PetscMalloc((nsends+1)*sizeof(PetscInt),&dest);CHKERRQ(ierr); 67889d82c54SBarry Smith cnt = 0; 67989d82c54SBarry Smith for (i=0; i<size; i++) { 68027c402fcSBarry Smith if (nprocs[2*i]) { 68132dcc486SBarry Smith ierr = MPI_Isend(sends+starts[i],2*nprocs[2*i],MPIU_INT,i,tag1,comm,send_waits+cnt);CHKERRQ(ierr); 68230dcb7c9SBarry Smith dest[cnt] = i; 68389d82c54SBarry Smith cnt++; 68489d82c54SBarry Smith } 68589d82c54SBarry Smith } 68689d82c54SBarry Smith ierr = PetscFree(starts);CHKERRQ(ierr); 68789d82c54SBarry Smith 68889d82c54SBarry Smith /* wait on receives */ 68997f1f81fSBarry Smith ierr = PetscMalloc((nrecvs+1)*sizeof(PetscMPIInt),&source);CHKERRQ(ierr); 69097f1f81fSBarry Smith ierr = PetscMalloc((nrecvs+1)*sizeof(PetscMPIInt),&len);CHKERRQ(ierr); 69189d82c54SBarry Smith cnt = nrecvs; 69232dcc486SBarry Smith ierr = PetscMalloc((ng+1)*sizeof(PetscInt),&nownedsenders);CHKERRQ(ierr); 69332dcc486SBarry Smith ierr = PetscMemzero(nownedsenders,ng*sizeof(PetscInt));CHKERRQ(ierr); 69489d82c54SBarry Smith while (cnt) { 69589d82c54SBarry Smith ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr); 69689d82c54SBarry Smith /* unpack receives into our local space */ 69732dcc486SBarry Smith ierr = MPI_Get_count(&recv_status,MPIU_INT,&len[imdex]);CHKERRQ(ierr); 69889d82c54SBarry Smith source[imdex] = recv_status.MPI_SOURCE; 69930dcb7c9SBarry Smith len[imdex] = len[imdex]/2; 700caba0dd0SBarry Smith /* count how many local owners for each of my global owned indices */ 70130dcb7c9SBarry Smith for (i=0; i<len[imdex]; i++) nownedsenders[recvs[2*imdex*nmax+2*i]-rstart]++; 70289d82c54SBarry Smith cnt--; 70389d82c54SBarry Smith } 70489d82c54SBarry Smith ierr = PetscFree(recv_waits);CHKERRQ(ierr); 70589d82c54SBarry Smith 70630dcb7c9SBarry Smith /* count how many globally owned indices are on an edge multiplied by how many processors own them. */ 707bc8ff85bSBarry Smith nowned = 0; 708bc8ff85bSBarry Smith nownedm = 0; 709bc8ff85bSBarry Smith for (i=0; i<ng; i++) { 710bc8ff85bSBarry Smith if (nownedsenders[i] > 1) {nownedm += nownedsenders[i]; nowned++;} 711bc8ff85bSBarry Smith } 712bc8ff85bSBarry Smith 713bc8ff85bSBarry Smith /* create single array to contain rank of all local owners of each globally owned index */ 71432dcc486SBarry Smith ierr = PetscMalloc((nownedm+1)*sizeof(PetscInt),&ownedsenders);CHKERRQ(ierr); 71532dcc486SBarry Smith ierr = PetscMalloc((ng+1)*sizeof(PetscInt),&starts);CHKERRQ(ierr); 716bc8ff85bSBarry Smith starts[0] = 0; 717bc8ff85bSBarry Smith for (i=1; i<ng; i++) { 718bc8ff85bSBarry Smith if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1]; 719bc8ff85bSBarry Smith else starts[i] = starts[i-1]; 720bc8ff85bSBarry Smith } 721bc8ff85bSBarry Smith 72230dcb7c9SBarry Smith /* for each nontrival globally owned node list all arriving processors */ 723bc8ff85bSBarry Smith for (i=0; i<nrecvs; i++) { 724bc8ff85bSBarry Smith for (j=0; j<len[i]; j++) { 72530dcb7c9SBarry Smith node = recvs[2*i*nmax+2*j]-rstart; 726bc8ff85bSBarry Smith if (nownedsenders[node] > 1) { 727bc8ff85bSBarry Smith ownedsenders[starts[node]++] = source[i]; 728bc8ff85bSBarry Smith } 729bc8ff85bSBarry Smith } 730bc8ff85bSBarry Smith } 731bc8ff85bSBarry Smith 73207b52d57SBarry Smith if (debug) { /* ----------------------------------- */ 73330dcb7c9SBarry Smith starts[0] = 0; 73430dcb7c9SBarry Smith for (i=1; i<ng; i++) { 73530dcb7c9SBarry Smith if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1]; 73630dcb7c9SBarry Smith else starts[i] = starts[i-1]; 73730dcb7c9SBarry Smith } 73830dcb7c9SBarry Smith for (i=0; i<ng; i++) { 73930dcb7c9SBarry Smith if (nownedsenders[i] > 1) { 74030dcb7c9SBarry Smith ierr = PetscSynchronizedPrintf(comm,"[%d] global node %d local owner processors: ",rank,i+rstart);CHKERRQ(ierr); 74130dcb7c9SBarry Smith for (j=0; j<nownedsenders[i]; j++) { 74230dcb7c9SBarry Smith ierr = PetscSynchronizedPrintf(comm,"%d ",ownedsenders[starts[i]+j]);CHKERRQ(ierr); 74330dcb7c9SBarry Smith } 74430dcb7c9SBarry Smith ierr = PetscSynchronizedPrintf(comm,"\n");CHKERRQ(ierr); 74530dcb7c9SBarry Smith } 74630dcb7c9SBarry Smith } 74730dcb7c9SBarry Smith ierr = PetscSynchronizedFlush(comm);CHKERRQ(ierr); 74807b52d57SBarry Smith }/* ----------------------------------- */ 74930dcb7c9SBarry Smith 7503677ff5aSBarry Smith /* wait on original sends */ 7513a96401aSBarry Smith if (nsends) { 752b0a32e0cSBarry Smith ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr); 7533a96401aSBarry Smith ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr); 7543a96401aSBarry Smith ierr = PetscFree(send_status);CHKERRQ(ierr); 7553a96401aSBarry Smith } 75689d82c54SBarry Smith ierr = PetscFree(send_waits);CHKERRQ(ierr); 7573a96401aSBarry Smith ierr = PetscFree(sends);CHKERRQ(ierr); 7583677ff5aSBarry Smith ierr = PetscFree(nprocs);CHKERRQ(ierr); 7593677ff5aSBarry Smith 7603677ff5aSBarry Smith /* pack messages to send back to local owners */ 76130dcb7c9SBarry Smith starts[0] = 0; 76230dcb7c9SBarry Smith for (i=1; i<ng; i++) { 76330dcb7c9SBarry Smith if (nownedsenders[i-1] > 1) starts[i] = starts[i-1] + nownedsenders[i-1]; 76430dcb7c9SBarry Smith else starts[i] = starts[i-1]; 76530dcb7c9SBarry Smith } 76630dcb7c9SBarry Smith nsends2 = nrecvs; 76732dcc486SBarry Smith ierr = PetscMalloc((nsends2+1)*sizeof(PetscInt),&nprocs);CHKERRQ(ierr); /* length of each message */ 76830dcb7c9SBarry Smith for (i=0; i<nrecvs; i++) { 76930dcb7c9SBarry Smith nprocs[i] = 1; 77030dcb7c9SBarry Smith for (j=0; j<len[i]; j++) { 77130dcb7c9SBarry Smith node = recvs[2*i*nmax+2*j]-rstart; 77230dcb7c9SBarry Smith if (nownedsenders[node] > 1) { 77330dcb7c9SBarry Smith nprocs[i] += 2 + nownedsenders[node]; 77430dcb7c9SBarry Smith } 77530dcb7c9SBarry Smith } 77630dcb7c9SBarry Smith } 77730dcb7c9SBarry Smith nt = 0; for (i=0; i<nsends2; i++) nt += nprocs[i]; 77832dcc486SBarry Smith ierr = PetscMalloc((nt+1)*sizeof(PetscInt),&sends2);CHKERRQ(ierr); 77932dcc486SBarry Smith ierr = PetscMalloc((nsends2+1)*sizeof(PetscInt),&starts2);CHKERRQ(ierr); 78030dcb7c9SBarry Smith starts2[0] = 0; for (i=1; i<nsends2; i++) starts2[i] = starts2[i-1] + nprocs[i-1]; 78130dcb7c9SBarry Smith /* 78230dcb7c9SBarry Smith Each message is 1 + nprocs[i] long, and consists of 78330dcb7c9SBarry Smith (0) the number of nodes being sent back 78430dcb7c9SBarry Smith (1) the local node number, 78530dcb7c9SBarry Smith (2) the number of processors sharing it, 78630dcb7c9SBarry Smith (3) the processors sharing it 78730dcb7c9SBarry Smith */ 78830dcb7c9SBarry Smith for (i=0; i<nsends2; i++) { 78930dcb7c9SBarry Smith cnt = 1; 79030dcb7c9SBarry Smith sends2[starts2[i]] = 0; 79130dcb7c9SBarry Smith for (j=0; j<len[i]; j++) { 79230dcb7c9SBarry Smith node = recvs[2*i*nmax+2*j]-rstart; 79330dcb7c9SBarry Smith if (nownedsenders[node] > 1) { 79430dcb7c9SBarry Smith sends2[starts2[i]]++; 79530dcb7c9SBarry Smith sends2[starts2[i]+cnt++] = recvs[2*i*nmax+2*j+1]; 79630dcb7c9SBarry Smith sends2[starts2[i]+cnt++] = nownedsenders[node]; 79732dcc486SBarry Smith ierr = PetscMemcpy(&sends2[starts2[i]+cnt],&ownedsenders[starts[node]],nownedsenders[node]*sizeof(PetscInt));CHKERRQ(ierr); 79830dcb7c9SBarry Smith cnt += nownedsenders[node]; 79930dcb7c9SBarry Smith } 80030dcb7c9SBarry Smith } 80130dcb7c9SBarry Smith } 80230dcb7c9SBarry Smith 80330dcb7c9SBarry Smith /* receive the message lengths */ 80430dcb7c9SBarry Smith nrecvs2 = nsends; 80532dcc486SBarry Smith ierr = PetscMalloc((nrecvs2+1)*sizeof(PetscInt),&lens2);CHKERRQ(ierr); 80632dcc486SBarry Smith ierr = PetscMalloc((nrecvs2+1)*sizeof(PetscInt),&starts3);CHKERRQ(ierr); 807d44834fbSBarry Smith ierr = PetscMalloc((nrecvs2+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr); 80830dcb7c9SBarry Smith for (i=0; i<nrecvs2; i++) { 809d44834fbSBarry Smith ierr = MPI_Irecv(&lens2[i],1,MPIU_INT,dest[i],tag2,comm,recv_waits+i);CHKERRQ(ierr); 81030dcb7c9SBarry Smith } 811d44834fbSBarry Smith 8128a8e0b3aSBarry Smith /* send the message lengths */ 8138a8e0b3aSBarry Smith for (i=0; i<nsends2; i++) { 8148a8e0b3aSBarry Smith ierr = MPI_Send(&nprocs[i],1,MPIU_INT,source[i],tag2,comm);CHKERRQ(ierr); 8158a8e0b3aSBarry Smith } 8168a8e0b3aSBarry Smith 817d44834fbSBarry Smith /* wait on receives of lens */ 8180c468ba9SBarry Smith if (nrecvs2) { 8190c468ba9SBarry Smith ierr = PetscMalloc(nrecvs2*sizeof(MPI_Status),&recv_statuses);CHKERRQ(ierr); 820d44834fbSBarry Smith ierr = MPI_Waitall(nrecvs2,recv_waits,recv_statuses);CHKERRQ(ierr); 821d44834fbSBarry Smith ierr = PetscFree(recv_statuses);CHKERRQ(ierr); 8220c468ba9SBarry Smith } 823d44834fbSBarry Smith ierr = PetscFree(recv_waits); 824d44834fbSBarry Smith 82530dcb7c9SBarry Smith starts3[0] = 0; 826d44834fbSBarry Smith nt = 0; 82730dcb7c9SBarry Smith for (i=0; i<nrecvs2-1; i++) { 82830dcb7c9SBarry Smith starts3[i+1] = starts3[i] + lens2[i]; 829d44834fbSBarry Smith nt += lens2[i]; 83030dcb7c9SBarry Smith } 831d44834fbSBarry Smith nt += lens2[nrecvs2-1]; 832d44834fbSBarry Smith 83332dcc486SBarry Smith ierr = PetscMalloc((nt+1)*sizeof(PetscInt),&recvs2);CHKERRQ(ierr); 834b0a32e0cSBarry Smith ierr = PetscMalloc((nrecvs2+1)*sizeof(MPI_Request),&recv_waits);CHKERRQ(ierr); 83552b72c4aSBarry Smith for (i=0; i<nrecvs2; i++) { 83632dcc486SBarry Smith ierr = MPI_Irecv(recvs2+starts3[i],lens2[i],MPIU_INT,dest[i],tag3,comm,recv_waits+i);CHKERRQ(ierr); 83730dcb7c9SBarry Smith } 83830dcb7c9SBarry Smith 83930dcb7c9SBarry Smith /* send the messages */ 840b0a32e0cSBarry Smith ierr = PetscMalloc((nsends2+1)*sizeof(MPI_Request),&send_waits);CHKERRQ(ierr); 84130dcb7c9SBarry Smith for (i=0; i<nsends2; i++) { 84232dcc486SBarry Smith ierr = MPI_Isend(sends2+starts2[i],nprocs[i],MPIU_INT,source[i],tag3,comm,send_waits+i);CHKERRQ(ierr); 84330dcb7c9SBarry Smith } 84430dcb7c9SBarry Smith 84530dcb7c9SBarry Smith /* wait on receives */ 8460c468ba9SBarry Smith if (nrecvs2) { 8470c468ba9SBarry Smith ierr = PetscMalloc(nrecvs2*sizeof(MPI_Status),&recv_statuses);CHKERRQ(ierr); 84830dcb7c9SBarry Smith ierr = MPI_Waitall(nrecvs2,recv_waits,recv_statuses);CHKERRQ(ierr); 84930dcb7c9SBarry Smith ierr = PetscFree(recv_statuses);CHKERRQ(ierr); 8500c468ba9SBarry Smith } 85130dcb7c9SBarry Smith ierr = PetscFree(recv_waits);CHKERRQ(ierr); 85230dcb7c9SBarry Smith ierr = PetscFree(nprocs);CHKERRQ(ierr); 85330dcb7c9SBarry Smith 85407b52d57SBarry Smith if (debug) { /* ----------------------------------- */ 85530dcb7c9SBarry Smith cnt = 0; 85630dcb7c9SBarry Smith for (i=0; i<nrecvs2; i++) { 85730dcb7c9SBarry Smith nt = recvs2[cnt++]; 85830dcb7c9SBarry Smith for (j=0; j<nt; j++) { 85930dcb7c9SBarry Smith ierr = PetscSynchronizedPrintf(comm,"[%d] local node %d number of subdomains %d: ",rank,recvs2[cnt],recvs2[cnt+1]);CHKERRQ(ierr); 86030dcb7c9SBarry Smith for (k=0; k<recvs2[cnt+1]; k++) { 86130dcb7c9SBarry Smith ierr = PetscSynchronizedPrintf(comm,"%d ",recvs2[cnt+2+k]);CHKERRQ(ierr); 86230dcb7c9SBarry Smith } 86330dcb7c9SBarry Smith cnt += 2 + recvs2[cnt+1]; 86430dcb7c9SBarry Smith ierr = PetscSynchronizedPrintf(comm,"\n");CHKERRQ(ierr); 86530dcb7c9SBarry Smith } 86630dcb7c9SBarry Smith } 86730dcb7c9SBarry Smith ierr = PetscSynchronizedFlush(comm);CHKERRQ(ierr); 86807b52d57SBarry Smith } /* ----------------------------------- */ 86930dcb7c9SBarry Smith 87030dcb7c9SBarry Smith /* count number subdomains for each local node */ 87132dcc486SBarry Smith ierr = PetscMalloc(size*sizeof(PetscInt),&nprocs);CHKERRQ(ierr); 87232dcc486SBarry Smith ierr = PetscMemzero(nprocs,size*sizeof(PetscInt));CHKERRQ(ierr); 87330dcb7c9SBarry Smith cnt = 0; 87430dcb7c9SBarry Smith for (i=0; i<nrecvs2; i++) { 87530dcb7c9SBarry Smith nt = recvs2[cnt++]; 87630dcb7c9SBarry Smith for (j=0; j<nt; j++) { 87730dcb7c9SBarry Smith for (k=0; k<recvs2[cnt+1]; k++) { 87830dcb7c9SBarry Smith nprocs[recvs2[cnt+2+k]]++; 87930dcb7c9SBarry Smith } 88030dcb7c9SBarry Smith cnt += 2 + recvs2[cnt+1]; 88130dcb7c9SBarry Smith } 88230dcb7c9SBarry Smith } 88330dcb7c9SBarry Smith nt = 0; for (i=0; i<size; i++) nt += (nprocs[i] > 0); 88430dcb7c9SBarry Smith *nproc = nt; 88532dcc486SBarry Smith ierr = PetscMalloc((nt+1)*sizeof(PetscInt),procs);CHKERRQ(ierr); 88632dcc486SBarry Smith ierr = PetscMalloc((nt+1)*sizeof(PetscInt),numprocs);CHKERRQ(ierr); 88732dcc486SBarry Smith ierr = PetscMalloc((nt+1)*sizeof(PetscInt*),indices);CHKERRQ(ierr); 88832dcc486SBarry Smith ierr = PetscMalloc(size*sizeof(PetscInt),&bprocs);CHKERRQ(ierr); 88930dcb7c9SBarry Smith cnt = 0; 89030dcb7c9SBarry Smith for (i=0; i<size; i++) { 89130dcb7c9SBarry Smith if (nprocs[i] > 0) { 89230dcb7c9SBarry Smith bprocs[i] = cnt; 89330dcb7c9SBarry Smith (*procs)[cnt] = i; 89430dcb7c9SBarry Smith (*numprocs)[cnt] = nprocs[i]; 89532dcc486SBarry Smith ierr = PetscMalloc(nprocs[i]*sizeof(PetscInt),&(*indices)[cnt]);CHKERRQ(ierr); 89630dcb7c9SBarry Smith cnt++; 89730dcb7c9SBarry Smith } 89830dcb7c9SBarry Smith } 89930dcb7c9SBarry Smith 90030dcb7c9SBarry Smith /* make the list of subdomains for each nontrivial local node */ 90132dcc486SBarry Smith ierr = PetscMemzero(*numprocs,nt*sizeof(PetscInt));CHKERRQ(ierr); 90230dcb7c9SBarry Smith cnt = 0; 90330dcb7c9SBarry Smith for (i=0; i<nrecvs2; i++) { 90430dcb7c9SBarry Smith nt = recvs2[cnt++]; 90530dcb7c9SBarry Smith for (j=0; j<nt; j++) { 90630dcb7c9SBarry Smith for (k=0; k<recvs2[cnt+1]; k++) { 90730dcb7c9SBarry Smith (*indices)[bprocs[recvs2[cnt+2+k]]][(*numprocs)[bprocs[recvs2[cnt+2+k]]]++] = recvs2[cnt]; 90830dcb7c9SBarry Smith } 90930dcb7c9SBarry Smith cnt += 2 + recvs2[cnt+1]; 91030dcb7c9SBarry Smith } 91130dcb7c9SBarry Smith } 91230dcb7c9SBarry Smith ierr = PetscFree(bprocs);CHKERRQ(ierr); 91307b52d57SBarry Smith ierr = PetscFree(recvs2);CHKERRQ(ierr); 91430dcb7c9SBarry Smith 91507b52d57SBarry Smith /* sort the node indexing by their global numbers */ 91607b52d57SBarry Smith nt = *nproc; 91707b52d57SBarry Smith for (i=0; i<nt; i++) { 91832dcc486SBarry Smith ierr = PetscMalloc(((*numprocs)[i])*sizeof(PetscInt),&tmp);CHKERRQ(ierr); 91907b52d57SBarry Smith for (j=0; j<(*numprocs)[i]; j++) { 92007b52d57SBarry Smith tmp[j] = lindices[(*indices)[i][j]]; 92107b52d57SBarry Smith } 92207b52d57SBarry Smith ierr = PetscSortIntWithArray((*numprocs)[i],tmp,(*indices)[i]);CHKERRQ(ierr); 92307b52d57SBarry Smith ierr = PetscFree(tmp);CHKERRQ(ierr); 92407b52d57SBarry Smith } 92507b52d57SBarry Smith 92607b52d57SBarry Smith if (debug) { /* ----------------------------------- */ 92730dcb7c9SBarry Smith nt = *nproc; 92830dcb7c9SBarry Smith for (i=0; i<nt; i++) { 92930dcb7c9SBarry Smith ierr = PetscSynchronizedPrintf(comm,"[%d] subdomain %d number of indices %d: ",rank,(*procs)[i],(*numprocs)[i]);CHKERRQ(ierr); 93030dcb7c9SBarry Smith for (j=0; j<(*numprocs)[i]; j++) { 93130dcb7c9SBarry Smith ierr = PetscSynchronizedPrintf(comm,"%d ",(*indices)[i][j]);CHKERRQ(ierr); 93230dcb7c9SBarry Smith } 93330dcb7c9SBarry Smith ierr = PetscSynchronizedPrintf(comm,"\n");CHKERRQ(ierr); 93430dcb7c9SBarry Smith } 93530dcb7c9SBarry Smith ierr = PetscSynchronizedFlush(comm);CHKERRQ(ierr); 93607b52d57SBarry Smith } /* ----------------------------------- */ 93730dcb7c9SBarry Smith 93830dcb7c9SBarry Smith /* wait on sends */ 93930dcb7c9SBarry Smith if (nsends2) { 940b0a32e0cSBarry Smith ierr = PetscMalloc(nsends2*sizeof(MPI_Status),&send_status);CHKERRQ(ierr); 94130dcb7c9SBarry Smith ierr = MPI_Waitall(nsends2,send_waits,send_status);CHKERRQ(ierr); 94230dcb7c9SBarry Smith ierr = PetscFree(send_status);CHKERRQ(ierr); 94330dcb7c9SBarry Smith } 94430dcb7c9SBarry Smith 94530dcb7c9SBarry Smith ierr = PetscFree(starts3);CHKERRQ(ierr); 94630dcb7c9SBarry Smith ierr = PetscFree(dest);CHKERRQ(ierr); 94730dcb7c9SBarry Smith ierr = PetscFree(send_waits);CHKERRQ(ierr); 9483677ff5aSBarry Smith 949bc8ff85bSBarry Smith ierr = PetscFree(nownedsenders);CHKERRQ(ierr); 950bc8ff85bSBarry Smith ierr = PetscFree(ownedsenders);CHKERRQ(ierr); 951bc8ff85bSBarry Smith ierr = PetscFree(starts);CHKERRQ(ierr); 95230dcb7c9SBarry Smith ierr = PetscFree(starts2);CHKERRQ(ierr); 95330dcb7c9SBarry Smith ierr = PetscFree(lens2);CHKERRQ(ierr); 95489d82c54SBarry Smith 95589d82c54SBarry Smith ierr = PetscFree(source);CHKERRQ(ierr); 95697f1f81fSBarry Smith ierr = PetscFree(len);CHKERRQ(ierr); 95789d82c54SBarry Smith ierr = PetscFree(recvs);CHKERRQ(ierr); 9583a96401aSBarry Smith ierr = PetscFree(nprocs);CHKERRQ(ierr); 95930dcb7c9SBarry Smith ierr = PetscFree(sends2);CHKERRQ(ierr); 96024cf384cSBarry Smith 96124cf384cSBarry Smith /* put the information about myself as the first entry in the list */ 96224cf384cSBarry Smith first_procs = (*procs)[0]; 96324cf384cSBarry Smith first_numprocs = (*numprocs)[0]; 96424cf384cSBarry Smith first_indices = (*indices)[0]; 96524cf384cSBarry Smith for (i=0; i<*nproc; i++) { 96624cf384cSBarry Smith if ((*procs)[i] == rank) { 96724cf384cSBarry Smith (*procs)[0] = (*procs)[i]; 96824cf384cSBarry Smith (*numprocs)[0] = (*numprocs)[i]; 96924cf384cSBarry Smith (*indices)[0] = (*indices)[i]; 97024cf384cSBarry Smith (*procs)[i] = first_procs; 97124cf384cSBarry Smith (*numprocs)[i] = first_numprocs; 97224cf384cSBarry Smith (*indices)[i] = first_indices; 97324cf384cSBarry Smith break; 97424cf384cSBarry Smith } 97524cf384cSBarry Smith } 97689d82c54SBarry Smith PetscFunctionReturn(0); 97789d82c54SBarry Smith } 97889d82c54SBarry Smith 9794a2ae208SSatish Balay #undef __FUNCT__ 9804a2ae208SSatish Balay #define __FUNCT__ "ISLocalToGlobalMappingRestoreInfo" 98107b52d57SBarry Smith /*@C 98207b52d57SBarry Smith ISLocalToGlobalMappingRestoreInfo - Frees the memory allocated by ISLocalToGlobalMappingGetInfo() 98389d82c54SBarry Smith 98407b52d57SBarry Smith Collective on ISLocalToGlobalMapping 98507b52d57SBarry Smith 98607b52d57SBarry Smith Input Parameters: 98707b52d57SBarry Smith . mapping - the mapping from local to global indexing 98807b52d57SBarry Smith 98907b52d57SBarry Smith Output Parameter: 99007b52d57SBarry Smith + nproc - number of processors that are connected to this one 99107b52d57SBarry Smith . proc - neighboring processors 99207b52d57SBarry Smith . numproc - number of indices for each processor 99307b52d57SBarry Smith - indices - indices of local nodes shared with neighbor (sorted by global numbering) 99407b52d57SBarry Smith 99507b52d57SBarry Smith Level: advanced 99607b52d57SBarry Smith 99707b52d57SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS(), ISLocalToGlobalMappingCreate(), 99807b52d57SBarry Smith ISLocalToGlobalMappingGetInfo() 99907b52d57SBarry Smith @*/ 10007087cfbeSBarry Smith PetscErrorCode ISLocalToGlobalMappingRestoreInfo(ISLocalToGlobalMapping mapping,PetscInt *nproc,PetscInt *procs[],PetscInt *numprocs[],PetscInt **indices[]) 100107b52d57SBarry Smith { 10026849ba73SBarry Smith PetscErrorCode ierr; 100332dcc486SBarry Smith PetscInt i; 100407b52d57SBarry Smith 100507b52d57SBarry Smith PetscFunctionBegin; 100605b42c5fSBarry Smith ierr = PetscFree(*procs);CHKERRQ(ierr); 100705b42c5fSBarry Smith ierr = PetscFree(*numprocs);CHKERRQ(ierr); 100800ff320aSBarry Smith if (*indices) { 100905b42c5fSBarry Smith ierr = PetscFree((*indices)[0]);CHKERRQ(ierr); 101000ff320aSBarry Smith for (i=1; i<*nproc; i++) { 101105b42c5fSBarry Smith ierr = PetscFree((*indices)[i]);CHKERRQ(ierr); 101207b52d57SBarry Smith } 101307b52d57SBarry Smith ierr = PetscFree(*indices);CHKERRQ(ierr); 101424cf384cSBarry Smith } 101507b52d57SBarry Smith PetscFunctionReturn(0); 101607b52d57SBarry Smith } 101786994e45SJed Brown 101886994e45SJed Brown #undef __FUNCT__ 101986994e45SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingGetIndices" 102086994e45SJed Brown /*@C 102186994e45SJed Brown ISLocalToGlobalMappingGetIndices - Get global indices for every local point 102286994e45SJed Brown 102386994e45SJed Brown Not Collective 102486994e45SJed Brown 102586994e45SJed Brown Input Arguments: 102686994e45SJed Brown . ltog - local to global mapping 102786994e45SJed Brown 102886994e45SJed Brown Output Arguments: 102986994e45SJed Brown . array - array of indices 103086994e45SJed Brown 103186994e45SJed Brown Level: advanced 103286994e45SJed Brown 103386994e45SJed Brown .seealso: ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingRestoreIndices() 103486994e45SJed Brown @*/ 10357087cfbeSBarry Smith PetscErrorCode ISLocalToGlobalMappingGetIndices(ISLocalToGlobalMapping ltog,const PetscInt **array) 103686994e45SJed Brown { 103786994e45SJed Brown 103886994e45SJed Brown PetscFunctionBegin; 103986994e45SJed Brown PetscValidHeaderSpecific(ltog,IS_LTOGM_CLASSID,1); 104086994e45SJed Brown PetscValidPointer(array,2); 104186994e45SJed Brown *array = ltog->indices; 104286994e45SJed Brown PetscFunctionReturn(0); 104386994e45SJed Brown } 104486994e45SJed Brown 104586994e45SJed Brown #undef __FUNCT__ 104686994e45SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingRestoreIndices" 104786994e45SJed Brown /*@C 104886994e45SJed Brown ISLocalToGlobalMappingRestoreIndices - Restore indices obtained with ISLocalToGlobalMappingRestoreIndices() 104986994e45SJed Brown 105086994e45SJed Brown Not Collective 105186994e45SJed Brown 105286994e45SJed Brown Input Arguments: 105386994e45SJed Brown + ltog - local to global mapping 105486994e45SJed Brown - array - array of indices 105586994e45SJed Brown 105686994e45SJed Brown Level: advanced 105786994e45SJed Brown 105886994e45SJed Brown .seealso: ISLocalToGlobalMappingCreate(), ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingGetIndices() 105986994e45SJed Brown @*/ 10607087cfbeSBarry Smith PetscErrorCode ISLocalToGlobalMappingRestoreIndices(ISLocalToGlobalMapping ltog,const PetscInt **array) 106186994e45SJed Brown { 106286994e45SJed Brown 106386994e45SJed Brown PetscFunctionBegin; 106486994e45SJed Brown PetscValidHeaderSpecific(ltog,IS_LTOGM_CLASSID,1); 106586994e45SJed Brown PetscValidPointer(array,2); 106686994e45SJed Brown if (*array != ltog->indices) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_BADPTR,"Trying to return mismatched pointer"); 106786994e45SJed Brown *array = PETSC_NULL; 106886994e45SJed Brown PetscFunctionReturn(0); 106986994e45SJed Brown } 1070f7efa3c7SJed Brown 1071f7efa3c7SJed Brown #undef __FUNCT__ 1072f7efa3c7SJed Brown #define __FUNCT__ "ISLocalToGlobalMappingConcatenate" 1073f7efa3c7SJed Brown /*@C 1074f7efa3c7SJed Brown ISLocalToGlobalMappingConcatenate - Create a new mapping that concatenates a list of mappings 1075f7efa3c7SJed Brown 1076f7efa3c7SJed Brown Not Collective 1077f7efa3c7SJed Brown 1078f7efa3c7SJed Brown Input Arguments: 1079f7efa3c7SJed Brown + comm - communicator for the new mapping, must contain the communicator of every mapping to concatenate 1080f7efa3c7SJed Brown . n - number of mappings to concatenate 1081f7efa3c7SJed Brown - ltogs - local to global mappings 1082f7efa3c7SJed Brown 1083f7efa3c7SJed Brown Output Arguments: 1084f7efa3c7SJed Brown . ltogcat - new mapping 1085f7efa3c7SJed Brown 1086f7efa3c7SJed Brown Level: advanced 1087f7efa3c7SJed Brown 1088f7efa3c7SJed Brown .seealso: ISLocalToGlobalMappingCreate() 1089f7efa3c7SJed Brown @*/ 1090f7efa3c7SJed Brown PetscErrorCode ISLocalToGlobalMappingConcatenate(MPI_Comm comm,PetscInt n,const ISLocalToGlobalMapping ltogs[],ISLocalToGlobalMapping *ltogcat) 1091f7efa3c7SJed Brown { 1092f7efa3c7SJed Brown PetscInt i,cnt,m,*idx; 1093f7efa3c7SJed Brown PetscErrorCode ierr; 1094f7efa3c7SJed Brown 1095f7efa3c7SJed Brown PetscFunctionBegin; 1096f7efa3c7SJed Brown if (n < 0) SETERRQ1(comm,PETSC_ERR_ARG_OUTOFRANGE,"Must have a non-negative number of mappings, given %D",n); 1097f7efa3c7SJed Brown if (n > 0) PetscValidPointer(ltogs,3); 1098f7efa3c7SJed Brown for (i=0; i<n; i++) PetscValidHeaderSpecific(ltogs[i],IS_LTOGM_CLASSID,3); 1099f7efa3c7SJed Brown PetscValidPointer(ltogcat,4); 1100f7efa3c7SJed Brown for (cnt=0,i=0; i<n; i++) { 1101f7efa3c7SJed Brown ierr = ISLocalToGlobalMappingGetSize(ltogs[i],&m);CHKERRQ(ierr); 1102f7efa3c7SJed Brown cnt += m; 1103f7efa3c7SJed Brown } 1104f7efa3c7SJed Brown ierr = PetscMalloc(cnt*sizeof(PetscInt),&idx);CHKERRQ(ierr); 1105f7efa3c7SJed Brown for (cnt=0,i=0; i<n; i++) { 1106f7efa3c7SJed Brown const PetscInt *subidx; 1107f7efa3c7SJed Brown ierr = ISLocalToGlobalMappingGetSize(ltogs[i],&m);CHKERRQ(ierr); 1108f7efa3c7SJed Brown ierr = ISLocalToGlobalMappingGetIndices(ltogs[i],&subidx);CHKERRQ(ierr); 1109f7efa3c7SJed Brown ierr = PetscMemcpy(&idx[cnt],subidx,m*sizeof(PetscInt));CHKERRQ(ierr); 1110f7efa3c7SJed Brown ierr = ISLocalToGlobalMappingRestoreIndices(ltogs[i],&subidx);CHKERRQ(ierr); 1111f7efa3c7SJed Brown cnt += m; 1112f7efa3c7SJed Brown } 1113f7efa3c7SJed Brown ierr = ISLocalToGlobalMappingCreate(comm,cnt,idx,PETSC_OWN_POINTER,ltogcat);CHKERRQ(ierr); 1114f7efa3c7SJed Brown PetscFunctionReturn(0); 1115f7efa3c7SJed Brown } 1116