1 #define PETSCKSP_DLL 2 3 /* 4 This file defines a "solve the problem redistributely on each subgroup of processor" preconditioner. 5 */ 6 #include "private/pcimpl.h" /*I "petscpc.h" I*/ 7 #include "petscksp.h" 8 9 typedef struct { 10 KSP ksp; 11 Vec x,b; 12 Mat mat; 13 VecScatter scatter; 14 IS is; 15 } PC_Redistribute; 16 17 #undef __FUNCT__ 18 #define __FUNCT__ "PCView_Redistribute" 19 static PetscErrorCode PCView_Redistribute(PC pc,PetscViewer viewer) 20 { 21 PC_Redistribute *red = (PC_Redistribute*)pc->data; 22 PetscErrorCode ierr; 23 PetscTruth iascii,isstring; 24 25 PetscFunctionBegin; 26 ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr); 27 ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);CHKERRQ(ierr); 28 if (iascii) { 29 ierr = PetscViewerASCIIPrintf(viewer," Redistribute preconditioner: \n");CHKERRQ(ierr); 30 ierr = KSPView(red->ksp,viewer);CHKERRQ(ierr); 31 } else if (isstring) { 32 ierr = PetscViewerStringSPrintf(viewer," Redistribute preconditioner");CHKERRQ(ierr); 33 ierr = KSPView(red->ksp,viewer);CHKERRQ(ierr); 34 } else { 35 SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for PC redistribute",((PetscObject)viewer)->type_name); 36 } 37 PetscFunctionReturn(0); 38 } 39 40 #include "private/matimpl.h" /*I "petscmat.h" I*/ 41 #undef __FUNCT__ 42 #define __FUNCT__ "PCSetUp_Redistribute" 43 static PetscErrorCode PCSetUp_Redistribute(PC pc) 44 { 45 PC_Redistribute *red = (PC_Redistribute*)pc->data; 46 PetscErrorCode ierr; 47 MPI_Comm comm; 48 PetscInt rstart,rend,i,nz,cnt,*rows,ncnt; 49 PetscMap *map,*nmap; 50 PetscMPIInt size,rank,imdex,tag,n; 51 PetscInt *source = PETSC_NULL; 52 PetscMPIInt *nprocs = PETSC_NULL,nrecvs; 53 PetscInt j,nsends; 54 PetscInt *owner = PETSC_NULL,*starts = PETSC_NULL,count,slen; 55 PetscInt *rvalues,*svalues,recvtotal; 56 PetscMPIInt *onodes1,*olengths1; 57 MPI_Request *send_waits = PETSC_NULL,*recv_waits = PETSC_NULL; 58 MPI_Status recv_status,*send_status; 59 Vec tvec; 60 Mat tmat; 61 62 PetscFunctionBegin; 63 ierr = PetscObjectGetComm((PetscObject)pc,&comm);CHKERRQ(ierr); 64 ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr); 65 ierr = MPI_Comm_rank(comm,&rank);CHKERRQ(ierr); 66 ierr = PetscObjectGetNewTag((PetscObject)pc,&tag);CHKERRQ(ierr); 67 68 if (!pc->setupcalled) { 69 } else SETERRQ(PETSC_ERR_SUP,"Cannot yet re-setup a redistribute KSP"); 70 71 /* count non-diagonal rows on process */ 72 ierr = MatGetOwnershipRange(pc->mat,&rstart,&rend);CHKERRQ(ierr); 73 cnt = 0; 74 for (i=rstart; i<rend; i++) { 75 ierr = MatGetRow(pc->mat,i,&nz,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 76 if (nz > 1) cnt++; 77 ierr = MatRestoreRow(pc->mat,i,&nz,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 78 } 79 ierr = PetscMalloc(cnt*sizeof(PetscInt),&rows);CHKERRQ(ierr); 80 81 /* list non-diagonal rows on process */ 82 cnt = 0; 83 for (i=rstart; i<rend; i++) { 84 ierr = MatGetRow(pc->mat,i,&nz,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 85 if (nz > 1) rows[cnt++] = i; 86 ierr = MatRestoreRow(pc->mat,i,&nz,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 87 } 88 89 /* create PetscMap for non-diagonal rows on each process */ 90 ierr = PetscMalloc(sizeof(PetscMap),&map);CHKERRQ(ierr); 91 ierr = PetscMapInitialize(comm,map);CHKERRQ(ierr); 92 ierr = PetscMapSetLocalSize(map,cnt);CHKERRQ(ierr); 93 ierr = PetscMapSetBlockSize(map,1);CHKERRQ(ierr); 94 ierr = PetscMapSetUp(map);CHKERRQ(ierr); 95 rstart = map->rstart; 96 rend = map->rend; 97 98 /* create PetscMap for load-balanced non-diagonal rows on each process */ 99 ierr = PetscMalloc(sizeof(PetscMap),&nmap);CHKERRQ(ierr); 100 ierr = PetscMapInitialize(comm,nmap);CHKERRQ(ierr); 101 ierr = MPI_Allreduce(&cnt,&ncnt,1,MPIU_INT,MPI_SUM,comm);CHKERRQ(ierr); 102 ierr = PetscMapSetSize(nmap,ncnt);CHKERRQ(ierr); 103 ierr = PetscMapSetBlockSize(nmap,1);CHKERRQ(ierr); 104 ierr = PetscMapSetUp(nmap);CHKERRQ(ierr); 105 106 /* 107 this code is taken from VecScatterCreate_PtoS() 108 Determines what rows need to be moved where to 109 load balance the non-diagonal rows 110 */ 111 /* count number of contributors to each processor */ 112 ierr = PetscMalloc2(size,PetscMPIInt,&nprocs,cnt,PetscInt,&owner);CHKERRQ(ierr); 113 ierr = PetscMemzero(nprocs,size*sizeof(PetscMPIInt));CHKERRQ(ierr); 114 j = 0; 115 nsends = 0; 116 for (i=rstart; i<rend; i++) { 117 if (i < nmap->range[j]) j = 0; 118 for (; j<size; j++) { 119 if (i < nmap->range[j+1]) { 120 if (!nprocs[j]++) nsends++; 121 owner[i-rstart] = j; 122 break; 123 } 124 } 125 } 126 /* inform other processors of number of messages and max length*/ 127 ierr = PetscGatherNumberOfMessages(comm,PETSC_NULL,nprocs,&nrecvs);CHKERRQ(ierr); 128 ierr = PetscGatherMessageLengths(comm,nsends,nrecvs,nprocs,&onodes1,&olengths1);CHKERRQ(ierr); 129 ierr = PetscSortMPIIntWithArray(nrecvs,onodes1,olengths1);CHKERRQ(ierr); 130 recvtotal = 0; for (i=0; i<nrecvs; i++) recvtotal += olengths1[i]; 131 132 /* post receives: rvalues - rows I will own; count - nu */ 133 ierr = PetscMalloc3(recvtotal,PetscInt,&rvalues,nrecvs,PetscInt,&source,nrecvs,MPI_Request,&recv_waits);CHKERRQ(ierr); 134 count = 0; 135 for (i=0; i<nrecvs; i++) { 136 ierr = MPI_Irecv((rvalues+count),olengths1[i],MPIU_INT,onodes1[i],tag,comm,recv_waits+i);CHKERRQ(ierr); 137 count += olengths1[i]; 138 } 139 140 /* do sends: 141 1) starts[i] gives the starting index in svalues for stuff going to 142 the ith processor 143 */ 144 ierr = PetscMalloc3(cnt,PetscInt,&svalues,nsends,MPI_Request,&send_waits,size,PetscInt,&starts);CHKERRQ(ierr); 145 starts[0] = 0; 146 for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[i-1];} 147 for (i=0; i<cnt; i++) { 148 svalues[starts[owner[i]]++] = rows[i]; 149 } 150 ierr = PetscFree(rows);CHKERRQ(ierr); 151 starts[0] = 0; 152 for (i=1; i<size; i++) { starts[i] = starts[i-1] + nprocs[i-1];} 153 count = 0; 154 for (i=0; i<size; i++) { 155 if (nprocs[i]) { 156 ierr = MPI_Isend(svalues+starts[i],nprocs[i],MPIU_INT,i,tag,comm,send_waits+count++);CHKERRQ(ierr); 157 } 158 } 159 160 /* wait on receives */ 161 count = nrecvs; 162 slen = 0; 163 while (count) { 164 ierr = MPI_Waitany(nrecvs,recv_waits,&imdex,&recv_status);CHKERRQ(ierr); 165 /* unpack receives into our local space */ 166 ierr = MPI_Get_count(&recv_status,MPIU_INT,&n);CHKERRQ(ierr); 167 slen += n; 168 count--; 169 } 170 if (slen != recvtotal) SETERRQ2(PETSC_ERR_PLIB,"Total message lengths %D not expected %D",slen,recvtotal); 171 172 ierr = ISCreateGeneral(comm,slen,rvalues,&red->is);CHKERRQ(ierr); 173 174 /* free up all work space */ 175 ierr = PetscFree(olengths1);CHKERRQ(ierr); 176 ierr = PetscFree(onodes1);CHKERRQ(ierr); 177 ierr = PetscFree3(rvalues,source,recv_waits);CHKERRQ(ierr); 178 ierr = PetscFree2(nprocs,owner);CHKERRQ(ierr); 179 if (nsends) { /* wait on sends */ 180 ierr = PetscMalloc(nsends*sizeof(MPI_Status),&send_status);CHKERRQ(ierr); 181 ierr = MPI_Waitall(nsends,send_waits,send_status);CHKERRQ(ierr); 182 ierr = PetscFree(send_status);CHKERRQ(ierr); 183 } 184 ierr = PetscFree3(svalues,send_waits,starts);CHKERRQ(ierr); 185 ierr = PetscFree(map->range);CHKERRQ(ierr); 186 ierr = PetscFree(map);CHKERRQ(ierr); 187 ierr = PetscFree(nmap->range);CHKERRQ(ierr); 188 ierr = PetscFree(nmap);CHKERRQ(ierr); 189 190 ierr = ISView(red->is,PETSC_VIEWER_STDOUT_(comm));CHKERRQ(ierr); 191 192 ierr = VecCreateMPI(comm,slen,PETSC_DETERMINE,&red->b);CHKERRQ(ierr); 193 ierr = VecDuplicate(red->b,&red->x);CHKERRQ(ierr); 194 ierr = MatGetVecs(pc->pmat,&tvec,PETSC_NULL);CHKERRQ(ierr); 195 ierr = VecScatterCreate(tvec,red->is,red->b,PETSC_NULL,&red->scatter);CHKERRQ(ierr); 196 ierr = VecDestroy(tvec);CHKERRQ(ierr); 197 ierr = MatGetSubMatrix(pc->pmat,red->is,red->is,MAT_INITIAL_MATRIX,&tmat);CHKERRQ(ierr); 198 ierr = KSPSetOperators(red->ksp,tmat,tmat,SAME_NONZERO_PATTERN);CHKERRQ(ierr); 199 ierr = MatDestroy(tmat);CHKERRQ(ierr); 200 201 ierr = KSPSetUp(red->ksp);CHKERRQ(ierr); 202 PetscFunctionReturn(0); 203 } 204 205 #undef __FUNCT__ 206 #define __FUNCT__ "PCApply_Redistribute" 207 static PetscErrorCode PCApply_Redistribute(PC pc,Vec b,Vec x) 208 { 209 PC_Redistribute *red = (PC_Redistribute*)pc->data; 210 PetscErrorCode ierr; 211 212 PetscFunctionBegin; 213 ierr = VecScatterBegin(red->scatter,b,red->b,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 214 ierr = VecScatterEnd(red->scatter,b,red->b,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 215 ierr = KSPSolve(red->ksp,red->b,red->x);CHKERRQ(ierr); 216 ierr = VecScatterBegin(red->scatter,red->x,x,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 217 ierr = VecScatterEnd(red->scatter,red->x,x,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); 218 PetscFunctionReturn(0); 219 } 220 221 #undef __FUNCT__ 222 #define __FUNCT__ "PCDestroy_Redistribute" 223 static PetscErrorCode PCDestroy_Redistribute(PC pc) 224 { 225 PC_Redistribute *red = (PC_Redistribute*)pc->data; 226 PetscErrorCode ierr; 227 228 PetscFunctionBegin; 229 if (red->scatter) {ierr = VecScatterDestroy(red->scatter);CHKERRQ(ierr);} 230 if (red->is) {ierr = ISDestroy(red->is);CHKERRQ(ierr);} 231 if (red->b) {ierr = VecDestroy(red->b);CHKERRQ(ierr);} 232 if (red->x) {ierr = VecDestroy(red->x);CHKERRQ(ierr);} 233 if (red->mat) {ierr = MatDestroy(red->mat);CHKERRQ(ierr);} 234 if (red->ksp) {ierr = KSPDestroy(red->ksp);CHKERRQ(ierr);} 235 ierr = PetscFree(red);CHKERRQ(ierr); 236 PetscFunctionReturn(0); 237 } 238 239 #undef __FUNCT__ 240 #define __FUNCT__ "PCSetFromOptions_Redistribute" 241 static PetscErrorCode PCSetFromOptions_Redistribute(PC pc) 242 { 243 PetscErrorCode ierr; 244 PC_Redistribute *red = (PC_Redistribute*)pc->data; 245 246 PetscFunctionBegin; 247 ierr = KSPSetFromOptions(red->ksp);CHKERRQ(ierr); 248 PetscFunctionReturn(0); 249 } 250 251 /* -------------------------------------------------------------------------------------*/ 252 /*MC 253 PCREDISTRIBUTE - Redistributes a matrix for load balancing and then applys a KSP to that new matrix 254 255 Options for the redistribute preconditioners can be set with -redistribute_ksp_xxx <values> and -redistribute_pc_xxx <values> 256 257 Level: intermediate 258 259 .seealso: PCCreate(), PCSetType(), PCType (for list of available types) 260 M*/ 261 262 EXTERN_C_BEGIN 263 #undef __FUNCT__ 264 #define __FUNCT__ "PCCreate_Redistribute" 265 PetscErrorCode PETSCKSP_DLLEXPORT PCCreate_Redistribute(PC pc) 266 { 267 PetscErrorCode ierr; 268 PC_Redistribute *red; 269 const char *prefix; 270 271 PetscFunctionBegin; 272 ierr = PetscNewLog(pc,PC_Redistribute,&red);CHKERRQ(ierr); 273 pc->data = (void*)red; 274 275 pc->ops->apply = PCApply_Redistribute; 276 pc->ops->applytranspose = 0; 277 pc->ops->setup = PCSetUp_Redistribute; 278 pc->ops->destroy = PCDestroy_Redistribute; 279 pc->ops->setfromoptions = PCSetFromOptions_Redistribute; 280 pc->ops->view = PCView_Redistribute; 281 282 ierr = KSPCreate(((PetscObject)pc)->comm,&red->ksp);CHKERRQ(ierr); 283 ierr = PetscObjectIncrementTabLevel((PetscObject)red->ksp,(PetscObject)pc,1);CHKERRQ(ierr); 284 ierr = PetscLogObjectParent(pc,red->ksp);CHKERRQ(ierr); 285 ierr = PCGetOptionsPrefix(pc,&prefix);CHKERRQ(ierr); 286 ierr = KSPSetOptionsPrefix(red->ksp,prefix);CHKERRQ(ierr); 287 ierr = KSPAppendOptionsPrefix(red->ksp,"redistribute_");CHKERRQ(ierr); 288 PetscFunctionReturn(0); 289 } 290 EXTERN_C_END 291