1 2 /* 3 The most basic AO application ordering routines. These store the 4 entire orderings on each processor. 5 */ 6 7 #include <../src/vec/is/ao/aoimpl.h> /*I "petscao.h" I*/ 8 9 typedef struct { 10 PetscInt *app; /* app[i] is the partner for the ith PETSc slot */ 11 PetscInt *petsc; /* petsc[j] is the partner for the jth app slot */ 12 } AO_Basic; 13 14 /* 15 All processors have the same data so processor 1 prints it 16 */ 17 #undef __FUNCT__ 18 #define __FUNCT__ "AOView_Basic" 19 PetscErrorCode AOView_Basic(AO ao,PetscViewer viewer) 20 { 21 PetscErrorCode ierr; 22 PetscMPIInt rank; 23 PetscInt i; 24 AO_Basic *aobasic = (AO_Basic*)ao->data; 25 PetscBool iascii; 26 27 PetscFunctionBegin; 28 ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)ao),&rank);CHKERRQ(ierr); 29 if (!rank) { 30 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 31 if (iascii) { 32 ierr = PetscViewerASCIIPrintf(viewer,"Number of elements in ordering %D\n",ao->N);CHKERRQ(ierr); 33 ierr = PetscViewerASCIIPrintf(viewer, "PETSc->App App->PETSc\n");CHKERRQ(ierr); 34 for (i=0; i<ao->N; i++) { 35 ierr = PetscViewerASCIIPrintf(viewer,"%3D %3D %3D %3D\n",i,aobasic->app[i],i,aobasic->petsc[i]);CHKERRQ(ierr); 36 } 37 } 38 } 39 ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 40 PetscFunctionReturn(0); 41 } 42 43 #undef __FUNCT__ 44 #define __FUNCT__ "AODestroy_Basic" 45 PetscErrorCode AODestroy_Basic(AO ao) 46 { 47 AO_Basic *aobasic = (AO_Basic*)ao->data; 48 PetscErrorCode ierr; 49 50 PetscFunctionBegin; 51 ierr = PetscFree2(aobasic->app,aobasic->petsc);CHKERRQ(ierr); 52 ierr = PetscFree(aobasic);CHKERRQ(ierr); 53 PetscFunctionReturn(0); 54 } 55 56 #undef __FUNCT__ 57 #define __FUNCT__ "AOBasicGetIndices_Private" 58 PetscErrorCode AOBasicGetIndices_Private(AO ao,PetscInt **app,PetscInt **petsc) 59 { 60 AO_Basic *basic = (AO_Basic*)ao->data; 61 62 PetscFunctionBegin; 63 if (app) *app = basic->app; 64 if (petsc) *petsc = basic->petsc; 65 PetscFunctionReturn(0); 66 } 67 68 #undef __FUNCT__ 69 #define __FUNCT__ "AOPetscToApplication_Basic" 70 PetscErrorCode AOPetscToApplication_Basic(AO ao,PetscInt n,PetscInt *ia) 71 { 72 PetscInt i,N=ao->N; 73 AO_Basic *aobasic = (AO_Basic*)ao->data; 74 75 PetscFunctionBegin; 76 for (i=0; i<n; i++) { 77 if (ia[i] >= 0 && ia[i] < N) { 78 ia[i] = aobasic->app[ia[i]]; 79 } else { 80 ia[i] = -1; 81 } 82 } 83 PetscFunctionReturn(0); 84 } 85 86 #undef __FUNCT__ 87 #define __FUNCT__ "AOApplicationToPetsc_Basic" 88 PetscErrorCode AOApplicationToPetsc_Basic(AO ao,PetscInt n,PetscInt *ia) 89 { 90 PetscInt i,N=ao->N; 91 AO_Basic *aobasic = (AO_Basic*)ao->data; 92 93 PetscFunctionBegin; 94 for (i=0; i<n; i++) { 95 if (ia[i] >= 0 && ia[i] < N) { 96 ia[i] = aobasic->petsc[ia[i]]; 97 } else { 98 ia[i] = -1; 99 } 100 } 101 PetscFunctionReturn(0); 102 } 103 104 #undef __FUNCT__ 105 #define __FUNCT__ "AOPetscToApplicationPermuteInt_Basic" 106 PetscErrorCode AOPetscToApplicationPermuteInt_Basic(AO ao, PetscInt block, PetscInt *array) 107 { 108 AO_Basic *aobasic = (AO_Basic*) ao->data; 109 PetscInt *temp; 110 PetscInt i, j; 111 PetscErrorCode ierr; 112 113 PetscFunctionBegin; 114 ierr = PetscMalloc(ao->N*block * sizeof(PetscInt), &temp);CHKERRQ(ierr); 115 for (i = 0; i < ao->N; i++) { 116 for (j = 0; j < block; j++) temp[i*block+j] = array[aobasic->petsc[i]*block+j]; 117 } 118 ierr = PetscMemcpy(array, temp, ao->N*block * sizeof(PetscInt));CHKERRQ(ierr); 119 ierr = PetscFree(temp);CHKERRQ(ierr); 120 PetscFunctionReturn(0); 121 } 122 123 #undef __FUNCT__ 124 #define __FUNCT__ "AOApplicationToPetscPermuteInt_Basic" 125 PetscErrorCode AOApplicationToPetscPermuteInt_Basic(AO ao, PetscInt block, PetscInt *array) 126 { 127 AO_Basic *aobasic = (AO_Basic*) ao->data; 128 PetscInt *temp; 129 PetscInt i, j; 130 PetscErrorCode ierr; 131 132 PetscFunctionBegin; 133 ierr = PetscMalloc(ao->N*block * sizeof(PetscInt), &temp);CHKERRQ(ierr); 134 for (i = 0; i < ao->N; i++) { 135 for (j = 0; j < block; j++) temp[i*block+j] = array[aobasic->app[i]*block+j]; 136 } 137 ierr = PetscMemcpy(array, temp, ao->N*block * sizeof(PetscInt));CHKERRQ(ierr); 138 ierr = PetscFree(temp);CHKERRQ(ierr); 139 PetscFunctionReturn(0); 140 } 141 142 #undef __FUNCT__ 143 #define __FUNCT__ "AOPetscToApplicationPermuteReal_Basic" 144 PetscErrorCode AOPetscToApplicationPermuteReal_Basic(AO ao, PetscInt block, PetscReal *array) 145 { 146 AO_Basic *aobasic = (AO_Basic*) ao->data; 147 PetscReal *temp; 148 PetscInt i, j; 149 PetscErrorCode ierr; 150 151 PetscFunctionBegin; 152 ierr = PetscMalloc(ao->N*block * sizeof(PetscReal), &temp);CHKERRQ(ierr); 153 for (i = 0; i < ao->N; i++) { 154 for (j = 0; j < block; j++) temp[i*block+j] = array[aobasic->petsc[i]*block+j]; 155 } 156 ierr = PetscMemcpy(array, temp, ao->N*block * sizeof(PetscReal));CHKERRQ(ierr); 157 ierr = PetscFree(temp);CHKERRQ(ierr); 158 PetscFunctionReturn(0); 159 } 160 161 #undef __FUNCT__ 162 #define __FUNCT__ "AOApplicationToPetscPermuteReal_Basic" 163 PetscErrorCode AOApplicationToPetscPermuteReal_Basic(AO ao, PetscInt block, PetscReal *array) 164 { 165 AO_Basic *aobasic = (AO_Basic*) ao->data; 166 PetscReal *temp; 167 PetscInt i, j; 168 PetscErrorCode ierr; 169 170 PetscFunctionBegin; 171 ierr = PetscMalloc(ao->N*block * sizeof(PetscReal), &temp);CHKERRQ(ierr); 172 for (i = 0; i < ao->N; i++) { 173 for (j = 0; j < block; j++) temp[i*block+j] = array[aobasic->app[i]*block+j]; 174 } 175 ierr = PetscMemcpy(array, temp, ao->N*block * sizeof(PetscReal));CHKERRQ(ierr); 176 ierr = PetscFree(temp);CHKERRQ(ierr); 177 PetscFunctionReturn(0); 178 } 179 180 static struct _AOOps AOOps_Basic = { 181 AOView_Basic, 182 AODestroy_Basic, 183 AOPetscToApplication_Basic, 184 AOApplicationToPetsc_Basic, 185 AOPetscToApplicationPermuteInt_Basic, 186 AOApplicationToPetscPermuteInt_Basic, 187 AOPetscToApplicationPermuteReal_Basic, 188 AOApplicationToPetscPermuteReal_Basic 189 }; 190 191 #undef __FUNCT__ 192 #define __FUNCT__ "AOCreate_Basic" 193 PETSC_EXTERN PetscErrorCode AOCreate_Basic(AO ao) 194 { 195 AO_Basic *aobasic; 196 PetscMPIInt size,rank,count,*lens,*disp; 197 PetscInt napp,*allpetsc,*allapp,ip,ia,N,i,*petsc=NULL,start; 198 PetscErrorCode ierr; 199 IS isapp=ao->isapp,ispetsc=ao->ispetsc; 200 MPI_Comm comm; 201 const PetscInt *myapp,*mypetsc=NULL; 202 203 PetscFunctionBegin; 204 /* create special struct aobasic */ 205 ierr = PetscNewLog(ao, AO_Basic, &aobasic);CHKERRQ(ierr); 206 ao->data = (void*) aobasic; 207 ierr = PetscMemcpy(ao->ops,&AOOps_Basic,sizeof(struct _AOOps));CHKERRQ(ierr); 208 ierr = PetscObjectChangeTypeName((PetscObject)ao,AOBASIC);CHKERRQ(ierr); 209 210 ierr = ISGetLocalSize(isapp,&napp);CHKERRQ(ierr); 211 ierr = ISGetIndices(isapp,&myapp);CHKERRQ(ierr); 212 213 ierr = PetscMPIIntCast(napp,&count);CHKERRQ(ierr); 214 215 /* transmit all lengths to all processors */ 216 ierr = PetscObjectGetComm((PetscObject)isapp,&comm);CHKERRQ(ierr); 217 ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr); 218 ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); 219 ierr = PetscMalloc2(size,PetscMPIInt, &lens,size,PetscMPIInt,&disp);CHKERRQ(ierr); 220 ierr = MPI_Allgather(&count, 1, MPI_INT, lens, 1, MPI_INT, comm);CHKERRQ(ierr); 221 N = 0; 222 for (i = 0; i < size; i++) { 223 ierr = PetscMPIIntCast(N,disp+i);CHKERRQ(ierr); /* = sum(lens[j]), j< i */ 224 N += lens[i]; 225 } 226 ao->N = N; 227 ao->n = N; 228 229 /* If mypetsc is 0 then use "natural" numbering */ 230 if (napp) { 231 if (!ispetsc) { 232 start = disp[rank]; 233 ierr = PetscMalloc((napp+1) * sizeof(PetscInt), &petsc);CHKERRQ(ierr); 234 for (i=0; i<napp; i++) petsc[i] = start + i; 235 } else { 236 ierr = ISGetIndices(ispetsc,&mypetsc);CHKERRQ(ierr); 237 petsc = (PetscInt*)mypetsc; 238 } 239 } 240 241 /* get all indices on all processors */ 242 ierr = PetscMalloc2(N,PetscInt,&allpetsc,N,PetscInt,&allapp);CHKERRQ(ierr); 243 ierr = MPI_Allgatherv(petsc, count, MPIU_INT, allpetsc, lens, disp, MPIU_INT, comm);CHKERRQ(ierr); 244 ierr = MPI_Allgatherv((void*)myapp, count, MPIU_INT, allapp, lens, disp, MPIU_INT, comm);CHKERRQ(ierr); 245 ierr = PetscFree2(lens,disp);CHKERRQ(ierr); 246 247 #if defined(PETSC_USE_DEBUG) 248 { 249 PetscInt *sorted; 250 ierr = PetscMalloc(N*sizeof(PetscInt),&sorted);CHKERRQ(ierr); 251 252 ierr = PetscMemcpy(sorted,allpetsc,N*sizeof(PetscInt));CHKERRQ(ierr); 253 ierr = PetscSortInt(N,sorted);CHKERRQ(ierr); 254 for (i=0; i<N; i++) { 255 if (sorted[i] != i) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"PETSc ordering requires a permutation of numbers 0 to N-1\n it is missing %D has %D",i,sorted[i]); 256 } 257 258 ierr = PetscMemcpy(sorted,allapp,N*sizeof(PetscInt));CHKERRQ(ierr); 259 ierr = PetscSortInt(N,sorted);CHKERRQ(ierr); 260 for (i=0; i<N; i++) { 261 if (sorted[i] != i) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Application ordering requires a permutation of numbers 0 to N-1\n it is missing %D has %D",i,sorted[i]); 262 } 263 264 ierr = PetscFree(sorted);CHKERRQ(ierr); 265 } 266 #endif 267 268 /* generate a list of application and PETSc node numbers */ 269 ierr = PetscMalloc2(N,PetscInt, &aobasic->app,N,PetscInt,&aobasic->petsc);CHKERRQ(ierr); 270 ierr = PetscLogObjectMemory(ao,2*N*sizeof(PetscInt));CHKERRQ(ierr); 271 ierr = PetscMemzero(aobasic->app, N*sizeof(PetscInt));CHKERRQ(ierr); 272 ierr = PetscMemzero(aobasic->petsc, N*sizeof(PetscInt));CHKERRQ(ierr); 273 for (i = 0; i < N; i++) { 274 ip = allpetsc[i]; 275 ia = allapp[i]; 276 /* check there are no duplicates */ 277 if (aobasic->app[ip]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Duplicate in PETSc ordering at position %d. Already mapped to %d, not %d.", i, aobasic->app[ip]-1, ia); 278 aobasic->app[ip] = ia + 1; 279 if (aobasic->petsc[ia]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Duplicate in Application ordering at position %d. Already mapped to %d, not %d.", i, aobasic->petsc[ia]-1, ip); 280 aobasic->petsc[ia] = ip + 1; 281 } 282 if (napp && !mypetsc) { 283 ierr = PetscFree(petsc);CHKERRQ(ierr); 284 } 285 ierr = PetscFree2(allpetsc,allapp);CHKERRQ(ierr); 286 /* shift indices down by one */ 287 for (i = 0; i < N; i++) { 288 aobasic->app[i]--; 289 aobasic->petsc[i]--; 290 } 291 292 ierr = ISRestoreIndices(isapp,&myapp);CHKERRQ(ierr); 293 if (napp) { 294 if (ispetsc) { 295 ierr = ISRestoreIndices(ispetsc,&mypetsc);CHKERRQ(ierr); 296 } else { 297 ierr = PetscFree(petsc);CHKERRQ(ierr); 298 } 299 } 300 PetscFunctionReturn(0); 301 } 302 303 #undef __FUNCT__ 304 #define __FUNCT__ "AOCreateBasic" 305 /*@C 306 AOCreateBasic - Creates a basic application ordering using two integer arrays. 307 308 Collective on MPI_Comm 309 310 Input Parameters: 311 + comm - MPI communicator that is to share AO 312 . napp - size of integer arrays 313 . myapp - integer array that defines an ordering 314 - mypetsc - integer array that defines another ordering (may be NULL to 315 indicate the natural ordering, that is 0,1,2,3,...) 316 317 Output Parameter: 318 . aoout - the new application ordering 319 320 Level: beginner 321 322 Notes: the arrays myapp and mypetsc must contain the all the integers 0 to napp-1 with no duplicates; that is there cannot be any "holes" 323 in the indices. Use AOCreateMapping() or AOCreateMappingIS() if you wish to have "holes" in the indices. 324 325 .keywords: AO, create 326 327 .seealso: AOCreateBasicIS(), AODestroy(), AOPetscToApplication(), AOApplicationToPetsc() 328 @*/ 329 PetscErrorCode AOCreateBasic(MPI_Comm comm,PetscInt napp,const PetscInt myapp[],const PetscInt mypetsc[],AO *aoout) 330 { 331 PetscErrorCode ierr; 332 IS isapp,ispetsc; 333 const PetscInt *app=myapp,*petsc=mypetsc; 334 335 PetscFunctionBegin; 336 ierr = ISCreateGeneral(comm,napp,app,PETSC_USE_POINTER,&isapp);CHKERRQ(ierr); 337 if (mypetsc) { 338 ierr = ISCreateGeneral(comm,napp,petsc,PETSC_USE_POINTER,&ispetsc);CHKERRQ(ierr); 339 } else { 340 ispetsc = NULL; 341 } 342 ierr = AOCreateBasicIS(isapp,ispetsc,aoout);CHKERRQ(ierr); 343 ierr = ISDestroy(&isapp);CHKERRQ(ierr); 344 if (mypetsc) { 345 ierr = ISDestroy(&ispetsc);CHKERRQ(ierr); 346 } 347 PetscFunctionReturn(0); 348 } 349 350 #undef __FUNCT__ 351 #define __FUNCT__ "AOCreateBasicIS" 352 /*@C 353 AOCreateBasicIS - Creates a basic application ordering using two index sets. 354 355 Collective on IS 356 357 Input Parameters: 358 + isapp - index set that defines an ordering 359 - ispetsc - index set that defines another ordering (may be NULL to use the 360 natural ordering) 361 362 Output Parameter: 363 . aoout - the new application ordering 364 365 Level: beginner 366 367 Notes: the index sets isapp and ispetsc must contain the all the integers 0 to napp-1 (where napp is the length of the index sets) with no duplicates; 368 that is there cannot be any "holes" 369 370 .keywords: AO, create 371 372 .seealso: AOCreateBasic(), AODestroy() 373 @*/ 374 PetscErrorCode AOCreateBasicIS(IS isapp,IS ispetsc,AO *aoout) 375 { 376 PetscErrorCode ierr; 377 MPI_Comm comm; 378 AO ao; 379 380 PetscFunctionBegin; 381 ierr = PetscObjectGetComm((PetscObject)isapp,&comm);CHKERRQ(ierr); 382 ierr = AOCreate(comm,&ao);CHKERRQ(ierr); 383 ierr = AOSetIS(ao,isapp,ispetsc);CHKERRQ(ierr); 384 ierr = AOSetType(ao,AOBASIC);CHKERRQ(ierr); 385 *aoout = ao; 386 PetscFunctionReturn(0); 387 } 388 389