1 2 /* 3 Tools to help solve the coarse grid problem redundantly. 4 Provides two scatter contexts that (1) map from the usual global vector 5 to all processors the entire vector in NATURAL numbering and (2) 6 from the entire vector on each processor in natural numbering extracts 7 out this processors piece in GLOBAL numbering 8 */ 9 10 #include <petsc/private/dmdaimpl.h> /*I "petscdmda.h" I*/ 11 12 #undef __FUNCT__ 13 #define __FUNCT__ "DMDAGlobalToNaturalAllCreate" 14 /*@ 15 DMDAGlobalToNaturalAllCreate - Creates a scatter context that maps from the 16 global vector the entire vector to each processor in natural numbering 17 18 Collective on DMDA 19 20 Input Parameter: 21 . da - the distributed array context 22 23 Output Parameter: 24 . scatter - the scatter context 25 26 Level: advanced 27 28 .keywords: distributed array, global to local, begin, coarse problem 29 30 .seealso: DMDAGlobalToNaturalEnd(), DMLocalToGlobalBegin(), DMDACreate2d(), 31 DMGlobalToLocalBegin(), DMGlobalToLocalEnd(), DMDACreateNaturalVector() 32 @*/ 33 PetscErrorCode DMDAGlobalToNaturalAllCreate(DM da,VecScatter *scatter) 34 { 35 PetscErrorCode ierr; 36 PetscInt N; 37 IS from,to; 38 Vec tmplocal,global; 39 AO ao; 40 DM_DA *dd = (DM_DA*)da->data; 41 42 PetscFunctionBegin; 43 PetscValidHeaderSpecific(da,DM_CLASSID,1); 44 PetscValidPointer(scatter,2); 45 ierr = DMDAGetAO(da,&ao);CHKERRQ(ierr); 46 47 /* create the scatter context */ 48 ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject)da),dd->w,dd->Nlocal,PETSC_DETERMINE,0,&global);CHKERRQ(ierr); 49 ierr = VecGetSize(global,&N);CHKERRQ(ierr); 50 ierr = ISCreateStride(PetscObjectComm((PetscObject)da),N,0,1,&to);CHKERRQ(ierr); 51 ierr = AOPetscToApplicationIS(ao,to);CHKERRQ(ierr); 52 ierr = ISCreateStride(PetscObjectComm((PetscObject)da),N,0,1,&from);CHKERRQ(ierr); 53 ierr = VecCreateSeqWithArray(PETSC_COMM_SELF,dd->w,N,0,&tmplocal);CHKERRQ(ierr); 54 ierr = VecScatterCreate(global,from,tmplocal,to,scatter);CHKERRQ(ierr); 55 ierr = VecDestroy(&tmplocal);CHKERRQ(ierr); 56 ierr = VecDestroy(&global);CHKERRQ(ierr); 57 ierr = ISDestroy(&from);CHKERRQ(ierr); 58 ierr = ISDestroy(&to);CHKERRQ(ierr); 59 PetscFunctionReturn(0); 60 } 61 62 #undef __FUNCT__ 63 #define __FUNCT__ "DMDANaturalAllToGlobalCreate" 64 /*@ 65 DMDANaturalAllToGlobalCreate - Creates a scatter context that maps from a copy 66 of the entire vector on each processor to its local part in the global vector. 67 68 Collective on DMDA 69 70 Input Parameter: 71 . da - the distributed array context 72 73 Output Parameter: 74 . scatter - the scatter context 75 76 Level: advanced 77 78 .keywords: distributed array, global to local, begin, coarse problem 79 80 .seealso: DMDAGlobalToNaturalEnd(), DMLocalToGlobalBegin(), DMDACreate2d(), 81 DMGlobalToLocalBegin(), DMGlobalToLocalEnd(), DMDACreateNaturalVector() 82 @*/ 83 PetscErrorCode DMDANaturalAllToGlobalCreate(DM da,VecScatter *scatter) 84 { 85 PetscErrorCode ierr; 86 DM_DA *dd = (DM_DA*)da->data; 87 PetscInt M,m = dd->Nlocal,start; 88 IS from,to; 89 Vec tmplocal,global; 90 AO ao; 91 92 PetscFunctionBegin; 93 PetscValidHeaderSpecific(da,DM_CLASSID,1); 94 PetscValidPointer(scatter,2); 95 ierr = DMDAGetAO(da,&ao);CHKERRQ(ierr); 96 97 /* create the scatter context */ 98 ierr = MPI_Allreduce(&m,&M,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)da));CHKERRQ(ierr); 99 ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject)da),dd->w,m,PETSC_DETERMINE,0,&global);CHKERRQ(ierr); 100 ierr = VecGetOwnershipRange(global,&start,NULL);CHKERRQ(ierr); 101 ierr = ISCreateStride(PetscObjectComm((PetscObject)da),m,start,1,&from);CHKERRQ(ierr); 102 ierr = AOPetscToApplicationIS(ao,from);CHKERRQ(ierr); 103 ierr = ISCreateStride(PetscObjectComm((PetscObject)da),m,start,1,&to);CHKERRQ(ierr); 104 ierr = VecCreateSeqWithArray(PETSC_COMM_SELF,dd->w,M,0,&tmplocal);CHKERRQ(ierr); 105 ierr = VecScatterCreate(tmplocal,from,global,to,scatter);CHKERRQ(ierr); 106 ierr = VecDestroy(&tmplocal);CHKERRQ(ierr); 107 ierr = VecDestroy(&global);CHKERRQ(ierr); 108 ierr = ISDestroy(&from);CHKERRQ(ierr); 109 ierr = ISDestroy(&to);CHKERRQ(ierr); 110 PetscFunctionReturn(0); 111 } 112 113