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