1d4bb536fSBarry Smith 2a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER 3*3f1db9ecSBarry Smith static char vcid[] = "$Id: isltog.c,v 1.23 1998/07/22 20:18:50 balay Exp bsmith $"; 42362add9SBarry Smith #endif 52362add9SBarry Smith 62362add9SBarry Smith #include "sys.h" /*I "sys.h" I*/ 7d4bb536fSBarry Smith #include "src/is/isimpl.h" /*I "is.h" I*/ 82362add9SBarry Smith 95615d1e5SSatish Balay #undef __FUNC__ 105a5d4f66SBarry Smith #define __FUNC__ "ISLocalToGlobalMappingView" 115a5d4f66SBarry Smith /*@C 125a5d4f66SBarry Smith ISLocalToGlobalMappingView - View a local to global mapping 135a5d4f66SBarry Smith 14b9cd556bSLois Curfman McInnes Not Collective 15b9cd556bSLois Curfman McInnes 165a5d4f66SBarry Smith Input Parameters: 175a5d4f66SBarry Smith . ltog - local to global mapping 18b9cd556bSLois Curfman McInnes . viewer - viewer 195a5d4f66SBarry Smith 205a5d4f66SBarry Smith .keywords: IS, local-to-global mapping, create 215a5d4f66SBarry Smith 225a5d4f66SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate() 235a5d4f66SBarry Smith @*/ 245a5d4f66SBarry Smith int ISLocalToGlobalMappingView(ISLocalToGlobalMapping mapping,Viewer viewer) 255a5d4f66SBarry Smith { 265a5d4f66SBarry Smith int i; 275a5d4f66SBarry Smith 285a5d4f66SBarry Smith PetscFunctionBegin; 295a5d4f66SBarry Smith 305a5d4f66SBarry Smith for ( i=0; i<mapping->n; i++ ) { 315a5d4f66SBarry Smith printf("%d %d\n",i,mapping->indices[i]); 325a5d4f66SBarry Smith } 335a5d4f66SBarry Smith 345a5d4f66SBarry Smith PetscFunctionReturn(0); 355a5d4f66SBarry Smith } 365a5d4f66SBarry Smith 375a5d4f66SBarry Smith #undef __FUNC__ 382bdab257SBarry Smith #define __FUNC__ "ISLocalToGlobalMappingCreateIS" 392bdab257SBarry Smith /*@C 402bdab257SBarry Smith ISLocalToGlobalMappingCreateIS - Creates a mapping between a local (0 to n) 412bdab257SBarry Smith ordering and a global parallel ordering. 422bdab257SBarry Smith 43b9cd556bSLois Curfman McInnes Collective on IS 44b9cd556bSLois Curfman McInnes 452bdab257SBarry Smith Input Parameters: 462bdab257SBarry Smith . is - index set containing the global numbers for each local 472bdab257SBarry Smith 482bdab257SBarry Smith Output Parameters: 492bdab257SBarry Smith . mapping - new mapping data structure 502bdab257SBarry Smith 512bdab257SBarry Smith .keywords: IS, local-to-global mapping, create 522bdab257SBarry Smith 532bdab257SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate() 542bdab257SBarry Smith @*/ 552bdab257SBarry Smith int ISLocalToGlobalMappingCreateIS(IS is,ISLocalToGlobalMapping *mapping) 562bdab257SBarry Smith { 572bdab257SBarry Smith int n,*indices,ierr; 582bdab257SBarry Smith MPI_Comm comm; 593a40ed3dSBarry Smith 603a40ed3dSBarry Smith PetscFunctionBegin; 612bdab257SBarry Smith PetscValidHeaderSpecific(is,IS_COOKIE); 622bdab257SBarry Smith 632bdab257SBarry Smith ierr = PetscObjectGetComm((PetscObject)is,&comm); CHKERRQ(ierr); 642bdab257SBarry Smith ierr = ISGetSize(is,&n);CHKERRQ(ierr); 652bdab257SBarry Smith ierr = ISGetIndices(is,&indices);CHKERRQ(ierr); 662bdab257SBarry Smith ierr = ISLocalToGlobalMappingCreate(comm,n,indices,mapping);CHKERRQ(ierr); 672bdab257SBarry Smith ierr = ISRestoreIndices(is,&indices);CHKERRQ(ierr); 682bdab257SBarry Smith 693a40ed3dSBarry Smith PetscFunctionReturn(0); 702bdab257SBarry Smith } 715a5d4f66SBarry Smith 722bdab257SBarry Smith #undef __FUNC__ 73d4bb536fSBarry Smith #define __FUNC__ "ISLocalToGlobalMappingCreate" 74dd7157adSSatish Balay /*@C 7590f02eecSBarry Smith ISLocalToGlobalMappingCreate - Creates a mapping between a local (0 to n) 7690f02eecSBarry Smith ordering and a global parallel ordering. 772362add9SBarry Smith 78b9cd556bSLois Curfman McInnes Collective on MPI_Comm 79b9cd556bSLois Curfman McInnes 802362add9SBarry Smith Input Parameters: 81b9cd556bSLois Curfman McInnes + comm - MPI communicator of size 1. 8290f02eecSBarry Smith . n - the number of local elements 83b9cd556bSLois Curfman McInnes - indices - the global index for each local element 842362add9SBarry Smith 852362add9SBarry Smith Output Parameters: 8690f02eecSBarry Smith . mapping - new mapping data structure 872362add9SBarry Smith 883acfe500SLois Curfman McInnes .keywords: IS, local-to-global mapping, create 892362add9SBarry Smith 902bdab257SBarry Smith .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS() 912362add9SBarry Smith @*/ 92987e4450SSatish Balay int ISLocalToGlobalMappingCreate(MPI_Comm cm,int n,const int indices[],ISLocalToGlobalMapping *mapping) 932362add9SBarry Smith { 943a40ed3dSBarry Smith PetscFunctionBegin; 9590f02eecSBarry Smith PetscValidIntPointer(indices); 9690f02eecSBarry Smith PetscValidPointer(mapping); 972362add9SBarry Smith 98*3f1db9ecSBarry Smith PetscHeaderCreate(*mapping,_p_ISLocalToGlobalMapping,int,IS_LTOGM_COOKIE,0,"ISLocalToGlobalMapping", 99*3f1db9ecSBarry Smith cm,ISLocalToGlobalMappingDestroy,ISLocalToGlobalMappingView); 100d4bb536fSBarry Smith PLogObjectCreate(*mapping); 101d4bb536fSBarry Smith PLogObjectMemory(*mapping,sizeof(struct _p_ISLocalToGlobalMapping)+n*sizeof(int)); 102d4bb536fSBarry Smith 103d4bb536fSBarry Smith (*mapping)->n = n; 10490f02eecSBarry Smith (*mapping)->indices = (int *) PetscMalloc((n+1)*sizeof(int));CHKPTRQ((*mapping)->indices); 10590f02eecSBarry Smith PetscMemcpy((*mapping)->indices,indices,n*sizeof(int)); 106d4bb536fSBarry Smith 107d4bb536fSBarry Smith /* 108d4bb536fSBarry Smith Do not create the global to local mapping. This is only created if 109d4bb536fSBarry Smith ISGlobalToLocalMapping() is called 110d4bb536fSBarry Smith */ 111d4bb536fSBarry Smith (*mapping)->globals = 0; 1123a40ed3dSBarry Smith PetscFunctionReturn(0); 1132362add9SBarry Smith } 1142362add9SBarry Smith 1155615d1e5SSatish Balay #undef __FUNC__ 116d4bb536fSBarry Smith #define __FUNC__ "ISLocalToGlobalMappingDestroy" 11790f02eecSBarry Smith /*@ 11890f02eecSBarry Smith ISLocalToGlobalMappingDestroy - Destroys a mapping between a local (0 to n) 11990f02eecSBarry Smith ordering and a global parallel ordering. 12090f02eecSBarry Smith 121b9cd556bSLois Curfman McInnes Collective on ISLocalToGlobalMapping 122b9cd556bSLois Curfman McInnes 12390f02eecSBarry Smith Input Parameters: 12490f02eecSBarry Smith . mapping - mapping data structure 12590f02eecSBarry Smith 1263acfe500SLois Curfman McInnes .keywords: IS, local-to-global mapping, destroy 12790f02eecSBarry Smith 1283acfe500SLois Curfman McInnes .seealso: ISLocalToGlobalMappingCreate() 12990f02eecSBarry Smith @*/ 13090f02eecSBarry Smith int ISLocalToGlobalMappingDestroy(ISLocalToGlobalMapping mapping) 13190f02eecSBarry Smith { 1323a40ed3dSBarry Smith PetscFunctionBegin; 13390f02eecSBarry Smith PetscValidPointer(mapping); 1343a40ed3dSBarry Smith if (--mapping->refct > 0) PetscFunctionReturn(0); 13590f02eecSBarry Smith 13690f02eecSBarry Smith PetscFree(mapping->indices); 137d4bb536fSBarry Smith if (mapping->globals) PetscFree(mapping->globals); 138d4bb536fSBarry Smith PLogObjectDestroy(mapping); 139d4bb536fSBarry Smith PetscHeaderDestroy(mapping); 1403a40ed3dSBarry Smith PetscFunctionReturn(0); 14190f02eecSBarry Smith } 14290f02eecSBarry Smith 1435615d1e5SSatish Balay #undef __FUNC__ 144d4bb536fSBarry Smith #define __FUNC__ "ISLocalToGlobalMappingApplyIS" 14590f02eecSBarry Smith /*@ 1463acfe500SLois Curfman McInnes ISLocalToGlobalMappingApplyIS - Creates from an IS in the local numbering 1473acfe500SLois Curfman McInnes a new index set using the global numbering defined in an ISLocalToGlobalMapping 1483acfe500SLois Curfman McInnes context. 14990f02eecSBarry Smith 150b9cd556bSLois Curfman McInnes Not collective 151b9cd556bSLois Curfman McInnes 15290f02eecSBarry Smith Input Parameters: 153b9cd556bSLois Curfman McInnes + mapping - mapping between local and global numbering 154b9cd556bSLois Curfman McInnes - is - index set in local numbering 15590f02eecSBarry Smith 15690f02eecSBarry Smith Output Parameters: 15790f02eecSBarry Smith . newis - index set in global numbering 15890f02eecSBarry Smith 1593acfe500SLois Curfman McInnes .keywords: IS, local-to-global mapping, apply 1603acfe500SLois Curfman McInnes 16190f02eecSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(), 162d4bb536fSBarry Smith ISLocalToGlobalMappingDestroy(), ISGlobalToLocalMappingApply() 16390f02eecSBarry Smith @*/ 16490f02eecSBarry Smith int ISLocalToGlobalMappingApplyIS(ISLocalToGlobalMapping mapping, IS is, IS *newis) 16590f02eecSBarry Smith { 16690f02eecSBarry Smith int ierr,n,i,*idxin,*idxmap,*idxout; 1673a40ed3dSBarry Smith 1683a40ed3dSBarry Smith PetscFunctionBegin; 16990f02eecSBarry Smith PetscValidPointer(mapping); 17090f02eecSBarry Smith PetscValidHeaderSpecific(is,IS_COOKIE); 17190f02eecSBarry Smith PetscValidPointer(newis); 17290f02eecSBarry Smith 17390f02eecSBarry Smith ierr = ISGetSize(is,&n); CHKERRQ(ierr); 17490f02eecSBarry Smith ierr = ISGetIndices(is,&idxin); CHKERRQ(ierr); 17590f02eecSBarry Smith idxmap = mapping->indices; 17690f02eecSBarry Smith 17790f02eecSBarry Smith idxout = (int *) PetscMalloc((n+1)*sizeof(int));CHKPTRQ(idxout); 17890f02eecSBarry Smith for ( i=0; i<n; i++ ) { 17990f02eecSBarry Smith idxout[i] = idxmap[idxin[i]]; 18090f02eecSBarry Smith } 181029af93fSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,n,idxout,newis); CHKERRQ(ierr); 18290f02eecSBarry Smith PetscFree(idxout); 1833a40ed3dSBarry Smith PetscFunctionReturn(0); 18490f02eecSBarry Smith } 18590f02eecSBarry Smith 1865615d1e5SSatish Balay #undef __FUNC__ 187d4bb536fSBarry Smith #define __FUNC__ "ISLocalToGlobalMappingApply" 188d4bb536fSBarry Smith /*@C 1893acfe500SLois Curfman McInnes ISLocalToGlobalMappingApply - Takes a list of integers in a local numbering 1903acfe500SLois Curfman McInnes and converts them to the global numbering. 19190f02eecSBarry Smith 192b9cd556bSLois Curfman McInnes Not collective 193b9cd556bSLois Curfman McInnes 194bb25748dSBarry Smith Input Parameters: 195b9cd556bSLois Curfman McInnes + mapping - the local to global mapping context 196bb25748dSBarry Smith . N - number of integers 197b9cd556bSLois Curfman McInnes - in - input indices in local numbering 198bb25748dSBarry Smith 199bb25748dSBarry Smith Output Parameter: 200bb25748dSBarry Smith . out - indices in global numbering 201bb25748dSBarry Smith 202b9cd556bSLois Curfman McInnes Notes: 203b9cd556bSLois Curfman McInnes The in and out array parameters may be identical. 204d4bb536fSBarry Smith 205bb25748dSBarry Smith .seealso: ISLocalToGlobalMappingCreate(),ISLocalToGlobalMappingDestroy(), 2060752156aSBarry Smith ISLocalToGlobalMappingApplyIS(),AOCreateBasic(),AOApplicationToPetsc(), 207d4bb536fSBarry Smith AOPetscToApplication(), ISGlobalToLocalMappingApply() 208bb25748dSBarry Smith 2093acfe500SLois Curfman McInnes .keywords: local-to-global, mapping, apply 210d4bb536fSBarry Smith 211d4bb536fSBarry Smith @*/ 212987e4450SSatish Balay int ISLocalToGlobalMappingApply(ISLocalToGlobalMapping mapping,int N,const int in[],int out[]) 213d4bb536fSBarry Smith { 214d4bb536fSBarry Smith int i,*idx = mapping->indices,Nmax = mapping->n; 2153a40ed3dSBarry Smith 2163a40ed3dSBarry Smith PetscFunctionBegin; 217d4bb536fSBarry Smith for ( i=0; i<N; i++ ) { 218d4bb536fSBarry Smith if (in[i] < 0) {out[i] = in[i]; continue;} 219a8c6a408SBarry Smith if (in[i] >= Nmax) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,1,"Local index too large"); 220d4bb536fSBarry Smith out[i] = idx[in[i]]; 221d4bb536fSBarry Smith } 2223a40ed3dSBarry Smith PetscFunctionReturn(0); 223d4bb536fSBarry Smith } 224d4bb536fSBarry Smith 225d4bb536fSBarry Smith /* -----------------------------------------------------------------------------------------*/ 226d4bb536fSBarry Smith 227d4bb536fSBarry Smith #undef __FUNC__ 228d4bb536fSBarry Smith #define __FUNC__ "ISGlobalToLocalMappingSetUp_Private" 229d4bb536fSBarry Smith /* 230d4bb536fSBarry Smith Creates the global fields in the ISLocalToGlobalMapping structure 231d4bb536fSBarry Smith */ 232d4bb536fSBarry Smith static int ISGlobalToLocalMappingSetUp_Private(ISLocalToGlobalMapping mapping) 233d4bb536fSBarry Smith { 234d4bb536fSBarry Smith int i,*idx = mapping->indices,n = mapping->n,end,start,*globals; 235d4bb536fSBarry Smith 2363a40ed3dSBarry Smith PetscFunctionBegin; 237d4bb536fSBarry Smith end = 0; 238d4bb536fSBarry Smith start = 100000000; 239d4bb536fSBarry Smith 240d4bb536fSBarry Smith for ( i=0; i<n; i++ ) { 241d4bb536fSBarry Smith if (idx[i] < 0) continue; 242d4bb536fSBarry Smith if (idx[i] < start) start = idx[i]; 243d4bb536fSBarry Smith if (idx[i] > end) end = idx[i]; 244d4bb536fSBarry Smith } 245d4bb536fSBarry Smith if (start > end) {start = 0; end = -1;} 246d4bb536fSBarry Smith mapping->globalstart = start; 247d4bb536fSBarry Smith mapping->globalend = end; 248d4bb536fSBarry Smith 249d4bb536fSBarry Smith globals = mapping->globals = (int *) PetscMalloc((end-start+2)*sizeof(int));CHKPTRQ(mapping->globals); 250d4bb536fSBarry Smith for ( i=0; i<end-start+1; i++ ) { 251d4bb536fSBarry Smith globals[i] = -1; 252d4bb536fSBarry Smith } 253d4bb536fSBarry Smith for ( i=0; i<n; i++ ) { 254d4bb536fSBarry Smith if (idx[i] < 0) continue; 255d4bb536fSBarry Smith globals[idx[i] - start] = i; 256d4bb536fSBarry Smith } 257d4bb536fSBarry Smith 258d4bb536fSBarry Smith PLogObjectMemory(mapping,(end-start+1)*sizeof(int)); 2593a40ed3dSBarry Smith PetscFunctionReturn(0); 260d4bb536fSBarry Smith } 261d4bb536fSBarry Smith 262d4bb536fSBarry Smith #undef __FUNC__ 263d4bb536fSBarry Smith #define __FUNC__ "ISGlobalToLocalMappingApply" 264d4bb536fSBarry Smith /*@ 265d4bb536fSBarry Smith ISGlobalToLocalMappingApply - Takes a list of integers in global numbering 266d4bb536fSBarry Smith and returns the local numbering. 267d4bb536fSBarry Smith 268b9cd556bSLois Curfman McInnes Not collective 269b9cd556bSLois Curfman McInnes 270d4bb536fSBarry Smith Input Parameters: 271b9cd556bSLois Curfman McInnes + mapping - mapping between local and global numbering 272d4bb536fSBarry Smith . type - IS_GTOLM_MASK - replaces global indices with no local value with -1 273d4bb536fSBarry Smith IS_GTOLM_DROP - drops the indices with no local value from the output list 274d4bb536fSBarry Smith . n - number of global indices to map 275b9cd556bSLois Curfman McInnes - idx - global indices to map 276d4bb536fSBarry Smith 277d4bb536fSBarry Smith Output Parameters: 278b9cd556bSLois Curfman McInnes + nout - number of indices in output array (if type == IS_GTOLM_MASK then nout = n) 279b9cd556bSLois Curfman McInnes - idxout - local index of each global index, one must pass in an array long enough 280e182c471SBarry Smith to hold all the indices. You can call ISGlobalToLocalMappingApply() with 281e182c471SBarry Smith idxout == PETSC_NULL to determine the required length (returned in nout) 282e182c471SBarry Smith and then allocate the required space and call ISGlobalToLocalMappingApply() 283e182c471SBarry Smith a second time to set the values. 284d4bb536fSBarry Smith 285b9cd556bSLois Curfman McInnes Notes: 286b9cd556bSLois Curfman McInnes Either nout or idxout may be PETSC_NULL. idx and idxout may be identical. 287d4bb536fSBarry Smith 288d4bb536fSBarry Smith .keywords: IS, global-to-local mapping, apply 289d4bb536fSBarry Smith 290d4bb536fSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(), 291d4bb536fSBarry Smith ISLocalToGlobalMappingDestroy() 292d4bb536fSBarry Smith @*/ 293d4bb536fSBarry Smith int ISGlobalToLocalMappingApply(ISLocalToGlobalMapping mapping, ISGlobalToLocalMappingType type, 294987e4450SSatish Balay int n, const int idx[],int *nout,int idxout[]) 295d4bb536fSBarry Smith { 296d4bb536fSBarry Smith int i,ierr, *globals,nf = 0,tmp,start,end; 297d4bb536fSBarry Smith 2983a40ed3dSBarry Smith PetscFunctionBegin; 299d4bb536fSBarry Smith if (!mapping->globals) { 300d4bb536fSBarry Smith ierr = ISGlobalToLocalMappingSetUp_Private(mapping); CHKERRQ(ierr); 301d4bb536fSBarry Smith } 302d4bb536fSBarry Smith globals = mapping->globals; 303d4bb536fSBarry Smith start = mapping->globalstart; 304d4bb536fSBarry Smith end = mapping->globalend; 305d4bb536fSBarry Smith 306d4bb536fSBarry Smith if (type == IS_GTOLM_MASK) { 307d4bb536fSBarry Smith if (idxout) { 308d4bb536fSBarry Smith for ( i=0; i<n; i++ ) { 309d4bb536fSBarry Smith if (idx[i] < 0) idxout[i] = idx[i]; 310d4bb536fSBarry Smith else if (idx[i] < start) idxout[i] = -1; 311d4bb536fSBarry Smith else if (idx[i] > end) idxout[i] = -1; 312d4bb536fSBarry Smith else idxout[i] = globals[idx[i] - start]; 313d4bb536fSBarry Smith } 314d4bb536fSBarry Smith } 315d4bb536fSBarry Smith if (nout) *nout = n; 316d4bb536fSBarry Smith } else { 317d4bb536fSBarry Smith if (idxout) { 318d4bb536fSBarry Smith for ( i=0; i<n; i++ ) { 319d4bb536fSBarry Smith if (idx[i] < 0) continue; 320d4bb536fSBarry Smith if (idx[i] < start) continue; 321d4bb536fSBarry Smith if (idx[i] > end) continue; 322d4bb536fSBarry Smith tmp = globals[idx[i] - start]; 323d4bb536fSBarry Smith if (tmp < 0) continue; 324d4bb536fSBarry Smith idxout[nf++] = tmp; 325d4bb536fSBarry Smith } 326d4bb536fSBarry Smith } else { 327d4bb536fSBarry Smith for ( i=0; i<n; i++ ) { 328d4bb536fSBarry Smith if (idx[i] < 0) continue; 329d4bb536fSBarry Smith if (idx[i] < start) continue; 330d4bb536fSBarry Smith if (idx[i] > end) continue; 331d4bb536fSBarry Smith tmp = globals[idx[i] - start]; 332d4bb536fSBarry Smith if (tmp < 0) continue; 333d4bb536fSBarry Smith nf++; 334d4bb536fSBarry Smith } 335d4bb536fSBarry Smith } 336d4bb536fSBarry Smith if (nout) *nout = nf; 337d4bb536fSBarry Smith } 338d4bb536fSBarry Smith 3393a40ed3dSBarry Smith PetscFunctionReturn(0); 340d4bb536fSBarry Smith } 34190f02eecSBarry Smith 342