1*d4bb536fSBarry Smith 2a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER 3*d4bb536fSBarry Smith static char vcid[] = "$Id: isltog.c,v 1.11 1997/07/09 20:49:26 balay Exp bsmith $"; 42362add9SBarry Smith #endif 52362add9SBarry Smith 62362add9SBarry Smith #include "sys.h" /*I "sys.h" I*/ 7*d4bb536fSBarry Smith #include "src/is/isimpl.h" /*I "is.h" I*/ 82362add9SBarry Smith 95615d1e5SSatish Balay #undef __FUNC__ 10*d4bb536fSBarry Smith #define __FUNC__ "ISLocalToGlobalMappingCreate" 1190f02eecSBarry Smith /*@ 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: 16*d4bb536fSBarry 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 @*/ 27*d4bb536fSBarry Smith int ISLocalToGlobalMappingCreate(MPI_Comm cm,int n, int *indices,ISLocalToGlobalMapping *mapping) 282362add9SBarry Smith { 2990f02eecSBarry Smith PetscValidIntPointer(indices); 3090f02eecSBarry Smith PetscValidPointer(mapping); 312362add9SBarry Smith 32*d4bb536fSBarry Smith PetscHeaderCreate(*mapping,_p_ISLocalToGlobalMapping,IS_LTOGM_COOKIE,0,cm,ISLocalToGlobalMappingDestroy,0); 33*d4bb536fSBarry Smith PLogObjectCreate(*mapping); 34*d4bb536fSBarry Smith PLogObjectMemory(*mapping,sizeof(struct _p_ISLocalToGlobalMapping)+n*sizeof(int)); 35*d4bb536fSBarry Smith 36*d4bb536fSBarry 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)); 39*d4bb536fSBarry Smith 40*d4bb536fSBarry Smith /* 41*d4bb536fSBarry Smith Do not create the global to local mapping. This is only created if 42*d4bb536fSBarry Smith ISGlobalToLocalMapping() is called 43*d4bb536fSBarry Smith */ 44*d4bb536fSBarry Smith (*mapping)->globals = 0; 452362add9SBarry Smith return 0; 462362add9SBarry Smith } 472362add9SBarry Smith 485615d1e5SSatish Balay #undef __FUNC__ 49*d4bb536fSBarry 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); 64*d4bb536fSBarry Smith if (--mapping->refct > 0) return 0; 6590f02eecSBarry Smith 6690f02eecSBarry Smith PetscFree(mapping->indices); 67*d4bb536fSBarry Smith if (mapping->globals) PetscFree(mapping->globals); 68*d4bb536fSBarry Smith PLogObjectDestroy(mapping); 69*d4bb536fSBarry Smith PetscHeaderDestroy(mapping); 7090f02eecSBarry Smith return 0; 7190f02eecSBarry Smith } 7290f02eecSBarry Smith 735615d1e5SSatish Balay #undef __FUNC__ 74*d4bb536fSBarry 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: 81*d4bb536fSBarry 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(), 90*d4bb536fSBarry 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__ 113*d4bb536fSBarry Smith #define __FUNC__ "ISLocalToGlobalMappingApply" 114*d4bb536fSBarry 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 126*d4bb536fSBarry Smith Notes: The in and out array may be identical 127*d4bb536fSBarry Smith 128bb25748dSBarry Smith .seealso: ISLocalToGlobalMappingCreate(),ISLocalToGlobalMappingDestroy(), 129bb25748dSBarry Smith ISLocalToGlobalMappingApplyIS(),AOCreateDebug(),AOApplicationToPetsc(), 130*d4bb536fSBarry Smith AOPetscToApplication(), ISGlobalToLocalMappingApply() 131bb25748dSBarry Smith 1323acfe500SLois Curfman McInnes .keywords: local-to-global, mapping, apply 133*d4bb536fSBarry Smith 134*d4bb536fSBarry Smith @*/ 135*d4bb536fSBarry Smith int ISLocalToGlobalMappingApply(ISLocalToGlobalMapping mapping,int N,int *in,int *out) 136*d4bb536fSBarry Smith { 137*d4bb536fSBarry Smith int i,*idx = mapping->indices,Nmax = mapping->n; 138*d4bb536fSBarry Smith for ( i=0; i<N; i++ ) { 139*d4bb536fSBarry Smith if (in[i] < 0) {out[i] = in[i]; continue;} 140*d4bb536fSBarry Smith if (in[i] >= Nmax) SETERRQ(1,1,"Local index too large"); 141*d4bb536fSBarry Smith out[i] = idx[in[i]]; 142*d4bb536fSBarry Smith } 143*d4bb536fSBarry Smith return 0; 144*d4bb536fSBarry Smith } 145*d4bb536fSBarry Smith 146*d4bb536fSBarry Smith /* -----------------------------------------------------------------------------------------*/ 147*d4bb536fSBarry Smith 148*d4bb536fSBarry Smith #undef __FUNC__ 149*d4bb536fSBarry Smith #define __FUNC__ "ISGlobalToLocalMappingSetUp_Private" 150*d4bb536fSBarry Smith /* 151*d4bb536fSBarry Smith Creates the global fields in the ISLocalToGlobalMapping structure 152*d4bb536fSBarry Smith */ 153*d4bb536fSBarry Smith static int ISGlobalToLocalMappingSetUp_Private(ISLocalToGlobalMapping mapping) 154*d4bb536fSBarry Smith { 155*d4bb536fSBarry Smith int i,*idx = mapping->indices,n = mapping->n,end,start,*globals; 156*d4bb536fSBarry Smith 157*d4bb536fSBarry Smith end = 0; 158*d4bb536fSBarry Smith start = 100000000; 159*d4bb536fSBarry Smith 160*d4bb536fSBarry Smith for ( i=0; i<n; i++ ) { 161*d4bb536fSBarry Smith if (idx[i] < 0) continue; 162*d4bb536fSBarry Smith if (idx[i] < start) start = idx[i]; 163*d4bb536fSBarry Smith if (idx[i] > end) end = idx[i]; 164*d4bb536fSBarry Smith } 165*d4bb536fSBarry Smith if (start > end) {start = 0; end = -1;} 166*d4bb536fSBarry Smith mapping->globalstart = start; 167*d4bb536fSBarry Smith mapping->globalend = end; 168*d4bb536fSBarry Smith 169*d4bb536fSBarry Smith globals = mapping->globals = (int *) PetscMalloc((end-start+2)*sizeof(int));CHKPTRQ(mapping->globals); 170*d4bb536fSBarry Smith for ( i=0; i<end-start+1; i++ ) { 171*d4bb536fSBarry Smith globals[i] = -1; 172*d4bb536fSBarry Smith } 173*d4bb536fSBarry Smith for ( i=0; i<n; i++ ) { 174*d4bb536fSBarry Smith if (idx[i] < 0) continue; 175*d4bb536fSBarry Smith globals[idx[i] - start] = i; 176*d4bb536fSBarry Smith } 177*d4bb536fSBarry Smith 178*d4bb536fSBarry Smith PLogObjectMemory(mapping,(end-start+1)*sizeof(int)); 179*d4bb536fSBarry Smith return 0; 180*d4bb536fSBarry Smith } 181*d4bb536fSBarry Smith 182*d4bb536fSBarry Smith #undef __FUNC__ 183*d4bb536fSBarry Smith #define __FUNC__ "ISGlobalToLocalMappingApply" 184*d4bb536fSBarry Smith /*@ 185*d4bb536fSBarry Smith ISGlobalToLocalMappingApply - Takes a list of integers in global numbering 186*d4bb536fSBarry Smith and returns the local numbering. 187*d4bb536fSBarry Smith 188*d4bb536fSBarry Smith Input Parameters: 189*d4bb536fSBarry Smith . mapping - mapping between local and global numbering 190*d4bb536fSBarry Smith . type - IS_GTOLM_MASK - replaces global indices with no local value with -1 191*d4bb536fSBarry Smith IS_GTOLM_DROP - drops the indices with no local value from the output list 192*d4bb536fSBarry Smith . n - number of global indices to map 193*d4bb536fSBarry Smith . idx - global indices to map 194*d4bb536fSBarry Smith 195*d4bb536fSBarry Smith Output Parameters: 196*d4bb536fSBarry Smith . nout - number of indices in output array (if type == IS_GTOLM_MASK then nout = n) 197*d4bb536fSBarry Smith . idxout - local index of each global index 198*d4bb536fSBarry Smith 199*d4bb536fSBarry Smith 200*d4bb536fSBarry Smith Notes: Either nout or idxout may be PETSC_NULL. idx and idxout may be identical. 201*d4bb536fSBarry Smith 202*d4bb536fSBarry Smith .keywords: IS, global-to-local mapping, apply 203*d4bb536fSBarry Smith 204*d4bb536fSBarry Smith .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(), 205*d4bb536fSBarry Smith ISLocalToGlobalMappingDestroy() 206*d4bb536fSBarry Smith @*/ 207*d4bb536fSBarry Smith int ISGlobalToLocalMappingApply(ISLocalToGlobalMapping mapping, ISGlobalToLocalMappingType type, 208*d4bb536fSBarry Smith int n, int *idx,int *nout,int *idxout) 209*d4bb536fSBarry Smith { 210*d4bb536fSBarry Smith int i,ierr, *globals,nf = 0,tmp,start,end; 211*d4bb536fSBarry Smith 212*d4bb536fSBarry Smith if (!mapping->globals) { 213*d4bb536fSBarry Smith ierr = ISGlobalToLocalMappingSetUp_Private(mapping); CHKERRQ(ierr); 214*d4bb536fSBarry Smith } 215*d4bb536fSBarry Smith globals = mapping->globals; 216*d4bb536fSBarry Smith start = mapping->globalstart; 217*d4bb536fSBarry Smith end = mapping->globalend; 218*d4bb536fSBarry Smith 219*d4bb536fSBarry Smith if (type == IS_GTOLM_MASK) { 220*d4bb536fSBarry Smith if (idxout) { 221*d4bb536fSBarry Smith for ( i=0; i<n; i++ ) { 222*d4bb536fSBarry Smith if (idx[i] < 0) idxout[i] = idx[i]; 223*d4bb536fSBarry Smith else if (idx[i] < start) idxout[i] = -1; 224*d4bb536fSBarry Smith else if (idx[i] > end) idxout[i] = -1; 225*d4bb536fSBarry Smith else idxout[i] = globals[idx[i] - start]; 226*d4bb536fSBarry Smith } 227*d4bb536fSBarry Smith } 228*d4bb536fSBarry Smith if (nout) *nout = n; 229*d4bb536fSBarry Smith } else { 230*d4bb536fSBarry Smith if (idxout) { 231*d4bb536fSBarry Smith for ( i=0; i<n; i++ ) { 232*d4bb536fSBarry Smith if (idx[i] < 0) continue; 233*d4bb536fSBarry Smith if (idx[i] < start) continue; 234*d4bb536fSBarry Smith if (idx[i] > end) continue; 235*d4bb536fSBarry Smith tmp = globals[idx[i] - start]; 236*d4bb536fSBarry Smith if (tmp < 0) continue; 237*d4bb536fSBarry Smith idxout[nf++] = tmp; 238*d4bb536fSBarry Smith } 239*d4bb536fSBarry Smith } else { 240*d4bb536fSBarry Smith for ( i=0; i<n; i++ ) { 241*d4bb536fSBarry Smith if (idx[i] < 0) continue; 242*d4bb536fSBarry Smith if (idx[i] < start) continue; 243*d4bb536fSBarry Smith if (idx[i] > end) continue; 244*d4bb536fSBarry Smith tmp = globals[idx[i] - start]; 245*d4bb536fSBarry Smith if (tmp < 0) continue; 246*d4bb536fSBarry Smith nf++; 247*d4bb536fSBarry Smith } 248*d4bb536fSBarry Smith } 249*d4bb536fSBarry Smith if (nout) *nout = nf; 250*d4bb536fSBarry Smith } 251*d4bb536fSBarry Smith 252*d4bb536fSBarry Smith return 0; 253*d4bb536fSBarry Smith } 25490f02eecSBarry Smith 255