1d4bb536fSBarry Smith 2a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER 3*0752156aSBarry Smith static char vcid[] = "$Id: isltog.c,v 1.14 1997/09/11 20:38:10 bsmith 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__ 10d4bb536fSBarry Smith #define __FUNC__ "ISLocalToGlobalMappingCreate" 11dd7157adSSatish Balay /*@C 1290f02eecSBarry Smith ISLocalToGlobalMappingCreate - Creates a mapping between a local (0 to n) 1390f02eecSBarry Smith ordering and a global parallel ordering. 142362add9SBarry Smith 152362add9SBarry Smith Input Parameters: 16d4bb536fSBarry Smith . comm - MPI communicator of size 1. 1790f02eecSBarry Smith . n - the number of local elements 1890f02eecSBarry Smith . indices - the global index for each local element 192362add9SBarry Smith 202362add9SBarry Smith Output Parameters: 2190f02eecSBarry Smith . mapping - new mapping data structure 222362add9SBarry Smith 233acfe500SLois Curfman McInnes .keywords: IS, local-to-global mapping, create 242362add9SBarry Smith 253acfe500SLois Curfman McInnes .seealso: ISLocalToGlobalMappingDestroy() 262362add9SBarry Smith @*/ 27d4bb536fSBarry Smith int ISLocalToGlobalMappingCreate(MPI_Comm cm,int n, int *indices,ISLocalToGlobalMapping *mapping) 282362add9SBarry Smith { 2990f02eecSBarry Smith PetscValidIntPointer(indices); 3090f02eecSBarry Smith PetscValidPointer(mapping); 312362add9SBarry Smith 32d4bb536fSBarry Smith PetscHeaderCreate(*mapping,_p_ISLocalToGlobalMapping,IS_LTOGM_COOKIE,0,cm,ISLocalToGlobalMappingDestroy,0); 33d4bb536fSBarry Smith PLogObjectCreate(*mapping); 34d4bb536fSBarry Smith PLogObjectMemory(*mapping,sizeof(struct _p_ISLocalToGlobalMapping)+n*sizeof(int)); 35d4bb536fSBarry Smith 36d4bb536fSBarry Smith (*mapping)->n = n; 3790f02eecSBarry Smith (*mapping)->indices = (int *) PetscMalloc((n+1)*sizeof(int));CHKPTRQ((*mapping)->indices); 3890f02eecSBarry Smith PetscMemcpy((*mapping)->indices,indices,n*sizeof(int)); 39d4bb536fSBarry Smith 40d4bb536fSBarry Smith /* 41d4bb536fSBarry Smith Do not create the global to local mapping. This is only created if 42d4bb536fSBarry Smith ISGlobalToLocalMapping() is called 43d4bb536fSBarry Smith */ 44d4bb536fSBarry Smith (*mapping)->globals = 0; 452362add9SBarry Smith return 0; 462362add9SBarry Smith } 472362add9SBarry Smith 485615d1e5SSatish Balay #undef __FUNC__ 49d4bb536fSBarry Smith #define __FUNC__ "ISLocalToGlobalMappingDestroy" 5090f02eecSBarry Smith /*@ 5190f02eecSBarry Smith ISLocalToGlobalMappingDestroy - Destroys a mapping between a local (0 to n) 5290f02eecSBarry Smith ordering and a global parallel ordering. 5390f02eecSBarry Smith 5490f02eecSBarry Smith Input Parameters: 5590f02eecSBarry Smith . mapping - mapping data structure 5690f02eecSBarry Smith 573acfe500SLois Curfman McInnes .keywords: IS, local-to-global mapping, destroy 5890f02eecSBarry Smith 593acfe500SLois Curfman McInnes .seealso: ISLocalToGlobalMappingCreate() 6090f02eecSBarry Smith @*/ 6190f02eecSBarry Smith int ISLocalToGlobalMappingDestroy(ISLocalToGlobalMapping mapping) 6290f02eecSBarry Smith { 6390f02eecSBarry Smith PetscValidPointer(mapping); 64d4bb536fSBarry Smith if (--mapping->refct > 0) return 0; 6590f02eecSBarry Smith 6690f02eecSBarry Smith PetscFree(mapping->indices); 67d4bb536fSBarry Smith if (mapping->globals) PetscFree(mapping->globals); 68d4bb536fSBarry Smith PLogObjectDestroy(mapping); 69d4bb536fSBarry Smith PetscHeaderDestroy(mapping); 7090f02eecSBarry Smith return 0; 7190f02eecSBarry Smith } 7290f02eecSBarry Smith 735615d1e5SSatish Balay #undef __FUNC__ 74d4bb536fSBarry Smith #define __FUNC__ "ISLocalToGlobalMappingApplyIS" 7590f02eecSBarry Smith /*@ 763acfe500SLois Curfman McInnes ISLocalToGlobalMappingApplyIS - Creates from an IS in the local numbering 773acfe500SLois Curfman McInnes a new index set using the global numbering defined in an ISLocalToGlobalMapping 783acfe500SLois Curfman McInnes context. 7990f02eecSBarry Smith 8090f02eecSBarry Smith Input Parameters: 81d4bb536fSBarry Smith . mapping - mapping between local and global numbering 8290f02eecSBarry Smith . is - index set in local numbering 8390f02eecSBarry Smith 8490f02eecSBarry Smith Output Parameters: 8590f02eecSBarry Smith . newis - index set in global numbering 8690f02eecSBarry Smith 873acfe500SLois Curfman McInnes .keywords: IS, local-to-global mapping, apply 883acfe500SLois Curfman McInnes 8990f02eecSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(), 90d4bb536fSBarry Smith ISLocalToGlobalMappingDestroy(), ISGlobalToLocalMappingApply() 9190f02eecSBarry Smith @*/ 9290f02eecSBarry Smith int ISLocalToGlobalMappingApplyIS(ISLocalToGlobalMapping mapping, IS is, IS *newis) 9390f02eecSBarry Smith { 9490f02eecSBarry Smith int ierr,n,i,*idxin,*idxmap,*idxout; 9590f02eecSBarry Smith PetscValidPointer(mapping); 9690f02eecSBarry Smith PetscValidHeaderSpecific(is,IS_COOKIE); 9790f02eecSBarry Smith PetscValidPointer(newis); 9890f02eecSBarry Smith 9990f02eecSBarry Smith ierr = ISGetSize(is,&n); CHKERRQ(ierr); 10090f02eecSBarry Smith ierr = ISGetIndices(is,&idxin); CHKERRQ(ierr); 10190f02eecSBarry Smith idxmap = mapping->indices; 10290f02eecSBarry Smith 10390f02eecSBarry Smith idxout = (int *) PetscMalloc((n+1)*sizeof(int));CHKPTRQ(idxout); 10490f02eecSBarry Smith for ( i=0; i<n; i++ ) { 10590f02eecSBarry Smith idxout[i] = idxmap[idxin[i]]; 10690f02eecSBarry Smith } 107029af93fSBarry Smith ierr = ISCreateGeneral(PETSC_COMM_SELF,n,idxout,newis); CHKERRQ(ierr); 10890f02eecSBarry Smith PetscFree(idxout); 10990f02eecSBarry Smith return 0; 11090f02eecSBarry Smith } 11190f02eecSBarry Smith 1125615d1e5SSatish Balay #undef __FUNC__ 113d4bb536fSBarry Smith #define __FUNC__ "ISLocalToGlobalMappingApply" 114d4bb536fSBarry Smith /*@C 1153acfe500SLois Curfman McInnes ISLocalToGlobalMappingApply - Takes a list of integers in a local numbering 1163acfe500SLois Curfman McInnes and converts them to the global numbering. 11790f02eecSBarry Smith 118bb25748dSBarry Smith Input Parameters: 119bb25748dSBarry Smith . mapping - the local to global mapping context 120bb25748dSBarry Smith . N - number of integers 121bb25748dSBarry Smith . in - input indices in local numbering 122bb25748dSBarry Smith 123bb25748dSBarry Smith Output Parameter: 124bb25748dSBarry Smith . out - indices in global numbering 125bb25748dSBarry Smith 126d4bb536fSBarry Smith Notes: The in and out array may be identical 127d4bb536fSBarry Smith 128bb25748dSBarry Smith .seealso: ISLocalToGlobalMappingCreate(),ISLocalToGlobalMappingDestroy(), 129*0752156aSBarry Smith ISLocalToGlobalMappingApplyIS(),AOCreateBasic(),AOApplicationToPetsc(), 130d4bb536fSBarry Smith AOPetscToApplication(), ISGlobalToLocalMappingApply() 131bb25748dSBarry Smith 1323acfe500SLois Curfman McInnes .keywords: local-to-global, mapping, apply 133d4bb536fSBarry Smith 134d4bb536fSBarry Smith @*/ 135d4bb536fSBarry Smith int ISLocalToGlobalMappingApply(ISLocalToGlobalMapping mapping,int N,int *in,int *out) 136d4bb536fSBarry Smith { 137d4bb536fSBarry Smith int i,*idx = mapping->indices,Nmax = mapping->n; 138d4bb536fSBarry Smith for ( i=0; i<N; i++ ) { 139d4bb536fSBarry Smith if (in[i] < 0) {out[i] = in[i]; continue;} 140d4bb536fSBarry Smith if (in[i] >= Nmax) SETERRQ(1,1,"Local index too large"); 141d4bb536fSBarry Smith out[i] = idx[in[i]]; 142d4bb536fSBarry Smith } 143d4bb536fSBarry Smith return 0; 144d4bb536fSBarry Smith } 145d4bb536fSBarry Smith 146d4bb536fSBarry Smith /* -----------------------------------------------------------------------------------------*/ 147d4bb536fSBarry Smith 148d4bb536fSBarry Smith #undef __FUNC__ 149d4bb536fSBarry Smith #define __FUNC__ "ISGlobalToLocalMappingSetUp_Private" 150d4bb536fSBarry Smith /* 151d4bb536fSBarry Smith Creates the global fields in the ISLocalToGlobalMapping structure 152d4bb536fSBarry Smith */ 153d4bb536fSBarry Smith static int ISGlobalToLocalMappingSetUp_Private(ISLocalToGlobalMapping mapping) 154d4bb536fSBarry Smith { 155d4bb536fSBarry Smith int i,*idx = mapping->indices,n = mapping->n,end,start,*globals; 156d4bb536fSBarry Smith 157d4bb536fSBarry Smith end = 0; 158d4bb536fSBarry Smith start = 100000000; 159d4bb536fSBarry Smith 160d4bb536fSBarry Smith for ( i=0; i<n; i++ ) { 161d4bb536fSBarry Smith if (idx[i] < 0) continue; 162d4bb536fSBarry Smith if (idx[i] < start) start = idx[i]; 163d4bb536fSBarry Smith if (idx[i] > end) end = idx[i]; 164d4bb536fSBarry Smith } 165d4bb536fSBarry Smith if (start > end) {start = 0; end = -1;} 166d4bb536fSBarry Smith mapping->globalstart = start; 167d4bb536fSBarry Smith mapping->globalend = end; 168d4bb536fSBarry Smith 169d4bb536fSBarry Smith globals = mapping->globals = (int *) PetscMalloc((end-start+2)*sizeof(int));CHKPTRQ(mapping->globals); 170d4bb536fSBarry Smith for ( i=0; i<end-start+1; i++ ) { 171d4bb536fSBarry Smith globals[i] = -1; 172d4bb536fSBarry Smith } 173d4bb536fSBarry Smith for ( i=0; i<n; i++ ) { 174d4bb536fSBarry Smith if (idx[i] < 0) continue; 175d4bb536fSBarry Smith globals[idx[i] - start] = i; 176d4bb536fSBarry Smith } 177d4bb536fSBarry Smith 178d4bb536fSBarry Smith PLogObjectMemory(mapping,(end-start+1)*sizeof(int)); 179d4bb536fSBarry Smith return 0; 180d4bb536fSBarry Smith } 181d4bb536fSBarry Smith 182d4bb536fSBarry Smith #undef __FUNC__ 183d4bb536fSBarry Smith #define __FUNC__ "ISGlobalToLocalMappingApply" 184d4bb536fSBarry Smith /*@ 185d4bb536fSBarry Smith ISGlobalToLocalMappingApply - Takes a list of integers in global numbering 186d4bb536fSBarry Smith and returns the local numbering. 187d4bb536fSBarry Smith 188d4bb536fSBarry Smith Input Parameters: 189d4bb536fSBarry Smith . mapping - mapping between local and global numbering 190d4bb536fSBarry Smith . type - IS_GTOLM_MASK - replaces global indices with no local value with -1 191d4bb536fSBarry Smith IS_GTOLM_DROP - drops the indices with no local value from the output list 192d4bb536fSBarry Smith . n - number of global indices to map 193d4bb536fSBarry Smith . idx - global indices to map 194d4bb536fSBarry Smith 195d4bb536fSBarry Smith Output Parameters: 196d4bb536fSBarry Smith . nout - number of indices in output array (if type == IS_GTOLM_MASK then nout = n) 197e182c471SBarry Smith . idxout - local index of each global index, one must pass in an array long enough 198e182c471SBarry Smith to hold all the indices. You can call ISGlobalToLocalMappingApply() with 199e182c471SBarry Smith idxout == PETSC_NULL to determine the required length (returned in nout) 200e182c471SBarry Smith and then allocate the required space and call ISGlobalToLocalMappingApply() 201e182c471SBarry Smith a second time to set the values. 202d4bb536fSBarry Smith 203d4bb536fSBarry Smith Notes: Either nout or idxout may be PETSC_NULL. idx and idxout may be identical. 204d4bb536fSBarry Smith 205d4bb536fSBarry Smith .keywords: IS, global-to-local mapping, apply 206d4bb536fSBarry Smith 207d4bb536fSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(), 208d4bb536fSBarry Smith ISLocalToGlobalMappingDestroy() 209d4bb536fSBarry Smith @*/ 210d4bb536fSBarry Smith int ISGlobalToLocalMappingApply(ISLocalToGlobalMapping mapping, ISGlobalToLocalMappingType type, 211d4bb536fSBarry Smith int n, int *idx,int *nout,int *idxout) 212d4bb536fSBarry Smith { 213d4bb536fSBarry Smith int i,ierr, *globals,nf = 0,tmp,start,end; 214d4bb536fSBarry Smith 215d4bb536fSBarry Smith if (!mapping->globals) { 216d4bb536fSBarry Smith ierr = ISGlobalToLocalMappingSetUp_Private(mapping); CHKERRQ(ierr); 217d4bb536fSBarry Smith } 218d4bb536fSBarry Smith globals = mapping->globals; 219d4bb536fSBarry Smith start = mapping->globalstart; 220d4bb536fSBarry Smith end = mapping->globalend; 221d4bb536fSBarry Smith 222d4bb536fSBarry Smith if (type == IS_GTOLM_MASK) { 223d4bb536fSBarry Smith if (idxout) { 224d4bb536fSBarry Smith for ( i=0; i<n; i++ ) { 225d4bb536fSBarry Smith if (idx[i] < 0) idxout[i] = idx[i]; 226d4bb536fSBarry Smith else if (idx[i] < start) idxout[i] = -1; 227d4bb536fSBarry Smith else if (idx[i] > end) idxout[i] = -1; 228d4bb536fSBarry Smith else idxout[i] = globals[idx[i] - start]; 229d4bb536fSBarry Smith } 230d4bb536fSBarry Smith } 231d4bb536fSBarry Smith if (nout) *nout = n; 232d4bb536fSBarry Smith } else { 233d4bb536fSBarry Smith if (idxout) { 234d4bb536fSBarry Smith for ( i=0; i<n; i++ ) { 235d4bb536fSBarry Smith if (idx[i] < 0) continue; 236d4bb536fSBarry Smith if (idx[i] < start) continue; 237d4bb536fSBarry Smith if (idx[i] > end) continue; 238d4bb536fSBarry Smith tmp = globals[idx[i] - start]; 239d4bb536fSBarry Smith if (tmp < 0) continue; 240d4bb536fSBarry Smith idxout[nf++] = tmp; 241d4bb536fSBarry Smith } 242d4bb536fSBarry Smith } else { 243d4bb536fSBarry Smith for ( i=0; i<n; i++ ) { 244d4bb536fSBarry Smith if (idx[i] < 0) continue; 245d4bb536fSBarry Smith if (idx[i] < start) continue; 246d4bb536fSBarry Smith if (idx[i] > end) continue; 247d4bb536fSBarry Smith tmp = globals[idx[i] - start]; 248d4bb536fSBarry Smith if (tmp < 0) continue; 249d4bb536fSBarry Smith nf++; 250d4bb536fSBarry Smith } 251d4bb536fSBarry Smith } 252d4bb536fSBarry Smith if (nout) *nout = nf; 253d4bb536fSBarry Smith } 254d4bb536fSBarry Smith 255d4bb536fSBarry Smith return 0; 256d4bb536fSBarry Smith } 25790f02eecSBarry Smith 258