1 2 #ifdef PETSC_RCS_HEADER 3 static char vcid[] = "$Id: isltog.c,v 1.26 1999/01/31 16:03:37 bsmith Exp curfman $"; 4 #endif 5 6 #include "sys.h" /*I "sys.h" I*/ 7 #include "src/vec/is/isimpl.h" /*I "is.h" I*/ 8 9 #undef __FUNC__ 10 #define __FUNC__ "ISLocalToGlobalMappingView" 11 /*@C 12 ISLocalToGlobalMappingView - View a local to global mapping 13 14 Not Collective 15 16 Input Parameters: 17 . ltog - local to global mapping 18 . viewer - viewer 19 20 Level: advanced 21 22 .keywords: IS, local-to-global mapping, create 23 24 .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate() 25 @*/ 26 int ISLocalToGlobalMappingView(ISLocalToGlobalMapping mapping,Viewer viewer) 27 { 28 int i; 29 30 PetscFunctionBegin; 31 32 for ( i=0; i<mapping->n; i++ ) { 33 printf("%d %d\n",i,mapping->indices[i]); 34 } 35 36 PetscFunctionReturn(0); 37 } 38 39 #undef __FUNC__ 40 #define __FUNC__ "ISLocalToGlobalMappingCreateIS" 41 /*@C 42 ISLocalToGlobalMappingCreateIS - Creates a mapping between a local (0 to n) 43 ordering and a global parallel ordering. 44 45 Collective on IS 46 47 Input Parameter: 48 . is - index set containing the global numbers for each local 49 50 Output Parameter: 51 . mapping - new mapping data structure 52 53 Level: advanced 54 55 .keywords: IS, local-to-global mapping, create 56 57 .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreate() 58 @*/ 59 int ISLocalToGlobalMappingCreateIS(IS is,ISLocalToGlobalMapping *mapping) 60 { 61 int n,*indices,ierr; 62 MPI_Comm comm; 63 64 PetscFunctionBegin; 65 PetscValidHeaderSpecific(is,IS_COOKIE); 66 67 ierr = PetscObjectGetComm((PetscObject)is,&comm); CHKERRQ(ierr); 68 ierr = ISGetSize(is,&n);CHKERRQ(ierr); 69 ierr = ISGetIndices(is,&indices);CHKERRQ(ierr); 70 ierr = ISLocalToGlobalMappingCreate(comm,n,indices,mapping);CHKERRQ(ierr); 71 ierr = ISRestoreIndices(is,&indices);CHKERRQ(ierr); 72 73 PetscFunctionReturn(0); 74 } 75 76 #undef __FUNC__ 77 #define __FUNC__ "ISLocalToGlobalMappingCreate" 78 /*@C 79 ISLocalToGlobalMappingCreate - Creates a mapping between a local (0 to n) 80 ordering and a global parallel ordering. 81 82 Collective on MPI_Comm 83 84 Input Parameters: 85 + comm - MPI communicator of size 1. 86 . n - the number of local elements 87 - indices - the global index for each local element 88 89 Output Parameter: 90 . mapping - new mapping data structure 91 92 Level: advanced 93 94 .keywords: IS, local-to-global mapping, create 95 96 .seealso: ISLocalToGlobalMappingDestroy(), ISLocalToGlobalMappingCreateIS() 97 @*/ 98 int ISLocalToGlobalMappingCreate(MPI_Comm cm,int n,const int indices[],ISLocalToGlobalMapping *mapping) 99 { 100 PetscFunctionBegin; 101 PetscValidIntPointer(indices); 102 PetscValidPointer(mapping); 103 104 PetscHeaderCreate(*mapping,_p_ISLocalToGlobalMapping,int,IS_LTOGM_COOKIE,0,"ISLocalToGlobalMapping", 105 cm,ISLocalToGlobalMappingDestroy,ISLocalToGlobalMappingView); 106 PLogObjectCreate(*mapping); 107 PLogObjectMemory(*mapping,sizeof(struct _p_ISLocalToGlobalMapping)+n*sizeof(int)); 108 109 (*mapping)->n = n; 110 (*mapping)->indices = (int *) PetscMalloc((n+1)*sizeof(int));CHKPTRQ((*mapping)->indices); 111 PetscMemcpy((*mapping)->indices,indices,n*sizeof(int)); 112 113 /* 114 Do not create the global to local mapping. This is only created if 115 ISGlobalToLocalMapping() is called 116 */ 117 (*mapping)->globals = 0; 118 PetscFunctionReturn(0); 119 } 120 121 #undef __FUNC__ 122 #define __FUNC__ "ISLocalToGlobalMappingDestroy" 123 /*@ 124 ISLocalToGlobalMappingDestroy - Destroys a mapping between a local (0 to n) 125 ordering and a global parallel ordering. 126 127 Collective on ISLocalToGlobalMapping 128 129 Input Parameters: 130 . mapping - mapping data structure 131 132 Level: advanced 133 134 .keywords: IS, local-to-global mapping, destroy 135 136 .seealso: ISLocalToGlobalMappingCreate() 137 @*/ 138 int ISLocalToGlobalMappingDestroy(ISLocalToGlobalMapping mapping) 139 { 140 PetscFunctionBegin; 141 PetscValidPointer(mapping); 142 if (--mapping->refct > 0) PetscFunctionReturn(0); 143 144 PetscFree(mapping->indices); 145 if (mapping->globals) PetscFree(mapping->globals); 146 PLogObjectDestroy(mapping); 147 PetscHeaderDestroy(mapping); 148 PetscFunctionReturn(0); 149 } 150 151 #undef __FUNC__ 152 #define __FUNC__ "ISLocalToGlobalMappingApplyIS" 153 /*@ 154 ISLocalToGlobalMappingApplyIS - Creates from an IS in the local numbering 155 a new index set using the global numbering defined in an ISLocalToGlobalMapping 156 context. 157 158 Not collective 159 160 Input Parameters: 161 + mapping - mapping between local and global numbering 162 - is - index set in local numbering 163 164 Output Parameters: 165 . newis - index set in global numbering 166 167 Level: advanced 168 169 .keywords: IS, local-to-global mapping, apply 170 171 .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(), 172 ISLocalToGlobalMappingDestroy(), ISGlobalToLocalMappingApply() 173 @*/ 174 int ISLocalToGlobalMappingApplyIS(ISLocalToGlobalMapping mapping, IS is, IS *newis) 175 { 176 int ierr,n,i,*idxin,*idxmap,*idxout; 177 178 PetscFunctionBegin; 179 PetscValidPointer(mapping); 180 PetscValidHeaderSpecific(is,IS_COOKIE); 181 PetscValidPointer(newis); 182 183 ierr = ISGetSize(is,&n); CHKERRQ(ierr); 184 ierr = ISGetIndices(is,&idxin); CHKERRQ(ierr); 185 idxmap = mapping->indices; 186 187 idxout = (int *) PetscMalloc((n+1)*sizeof(int));CHKPTRQ(idxout); 188 for ( i=0; i<n; i++ ) { 189 idxout[i] = idxmap[idxin[i]]; 190 } 191 ierr = ISCreateGeneral(PETSC_COMM_SELF,n,idxout,newis); CHKERRQ(ierr); 192 PetscFree(idxout); 193 PetscFunctionReturn(0); 194 } 195 196 #undef __FUNC__ 197 #define __FUNC__ "ISLocalToGlobalMappingApply" 198 /*@C 199 ISLocalToGlobalMappingApply - Takes a list of integers in a local numbering 200 and converts them to the global numbering. 201 202 Not collective 203 204 Input Parameters: 205 + mapping - the local to global mapping context 206 . N - number of integers 207 - in - input indices in local numbering 208 209 Output Parameter: 210 . out - indices in global numbering 211 212 Notes: 213 The in and out array parameters may be identical. 214 215 Level: advanced 216 217 .seealso: ISLocalToGlobalMappingCreate(),ISLocalToGlobalMappingDestroy(), 218 ISLocalToGlobalMappingApplyIS(),AOCreateBasic(),AOApplicationToPetsc(), 219 AOPetscToApplication(), ISGlobalToLocalMappingApply() 220 221 .keywords: local-to-global, mapping, apply 222 223 @*/ 224 int ISLocalToGlobalMappingApply(ISLocalToGlobalMapping mapping,int N,const int in[],int out[]) 225 { 226 int i,*idx = mapping->indices,Nmax = mapping->n; 227 228 PetscFunctionBegin; 229 for ( i=0; i<N; i++ ) { 230 if (in[i] < 0) {out[i] = in[i]; continue;} 231 if (in[i] >= Nmax) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,1,"Local index %d too large %d (max)",in[i],Nmax); 232 out[i] = idx[in[i]]; 233 } 234 PetscFunctionReturn(0); 235 } 236 237 /* -----------------------------------------------------------------------------------------*/ 238 239 #undef __FUNC__ 240 #define __FUNC__ "ISGlobalToLocalMappingSetUp_Private" 241 /* 242 Creates the global fields in the ISLocalToGlobalMapping structure 243 */ 244 static int ISGlobalToLocalMappingSetUp_Private(ISLocalToGlobalMapping mapping) 245 { 246 int i,*idx = mapping->indices,n = mapping->n,end,start,*globals; 247 248 PetscFunctionBegin; 249 end = 0; 250 start = 100000000; 251 252 for ( i=0; i<n; i++ ) { 253 if (idx[i] < 0) continue; 254 if (idx[i] < start) start = idx[i]; 255 if (idx[i] > end) end = idx[i]; 256 } 257 if (start > end) {start = 0; end = -1;} 258 mapping->globalstart = start; 259 mapping->globalend = end; 260 261 globals = mapping->globals = (int *) PetscMalloc((end-start+2)*sizeof(int));CHKPTRQ(mapping->globals); 262 for ( i=0; i<end-start+1; i++ ) { 263 globals[i] = -1; 264 } 265 for ( i=0; i<n; i++ ) { 266 if (idx[i] < 0) continue; 267 globals[idx[i] - start] = i; 268 } 269 270 PLogObjectMemory(mapping,(end-start+1)*sizeof(int)); 271 PetscFunctionReturn(0); 272 } 273 274 #undef __FUNC__ 275 #define __FUNC__ "ISGlobalToLocalMappingApply" 276 /*@ 277 ISGlobalToLocalMappingApply - Provides the local numbering for a list of integers 278 specified with a global numbering. 279 280 Not collective 281 282 Input Parameters: 283 + mapping - mapping between local and global numbering 284 . type - IS_GTOLM_MASK - replaces global indices with no local value with -1 285 IS_GTOLM_DROP - drops the indices with no local value from the output list 286 . n - number of global indices to map 287 - idx - global indices to map 288 289 Output Parameters: 290 + nout - number of indices in output array (if type == IS_GTOLM_MASK then nout = n) 291 - idxout - local index of each global index, one must pass in an array long enough 292 to hold all the indices. You can call ISGlobalToLocalMappingApply() with 293 idxout == PETSC_NULL to determine the required length (returned in nout) 294 and then allocate the required space and call ISGlobalToLocalMappingApply() 295 a second time to set the values. 296 297 Notes: 298 Either nout or idxout may be PETSC_NULL. idx and idxout may be identical. 299 300 Level: advanced 301 302 .keywords: IS, global-to-local mapping, apply 303 304 .seealso: ISLocalToGlobalMappingApply(), ISLocalToGlobalMappingCreate(), 305 ISLocalToGlobalMappingDestroy() 306 @*/ 307 int ISGlobalToLocalMappingApply(ISLocalToGlobalMapping mapping, ISGlobalToLocalMappingType type, 308 int n, const int idx[],int *nout,int idxout[]) 309 { 310 int i,ierr, *globals,nf = 0,tmp,start,end; 311 312 PetscFunctionBegin; 313 if (!mapping->globals) { 314 ierr = ISGlobalToLocalMappingSetUp_Private(mapping); CHKERRQ(ierr); 315 } 316 globals = mapping->globals; 317 start = mapping->globalstart; 318 end = mapping->globalend; 319 320 if (type == IS_GTOLM_MASK) { 321 if (idxout) { 322 for ( i=0; i<n; i++ ) { 323 if (idx[i] < 0) idxout[i] = idx[i]; 324 else if (idx[i] < start) idxout[i] = -1; 325 else if (idx[i] > end) idxout[i] = -1; 326 else idxout[i] = globals[idx[i] - start]; 327 } 328 } 329 if (nout) *nout = n; 330 } else { 331 if (idxout) { 332 for ( i=0; i<n; i++ ) { 333 if (idx[i] < 0) continue; 334 if (idx[i] < start) continue; 335 if (idx[i] > end) continue; 336 tmp = globals[idx[i] - start]; 337 if (tmp < 0) continue; 338 idxout[nf++] = tmp; 339 } 340 } else { 341 for ( i=0; i<n; i++ ) { 342 if (idx[i] < 0) continue; 343 if (idx[i] < start) continue; 344 if (idx[i] > end) continue; 345 tmp = globals[idx[i] - start]; 346 if (tmp < 0) continue; 347 nf++; 348 } 349 } 350 if (nout) *nout = nf; 351 } 352 353 PetscFunctionReturn(0); 354 } 355 356