Actual source code: vpscat.h

  2: /*
  3:      Defines the methods VecScatterBegin/End_1,2,......
  4:      This is included by vpscat.c with different values for BS

  6:      This is a terrible way of doing "templates" in C.
  7: */
  8: #define PETSCMAP1_a(a,b)  a ## _ ## b
  9: #define PETSCMAP1_b(a,b)  PETSCMAP1_a(a,b)
 10: #define PETSCMAP1(a)      PETSCMAP1_b(a,BS)

 14: PetscErrorCode PETSCMAP1(VecScatterBegin)(VecScatter ctx,Vec xin,Vec yin,InsertMode addv,ScatterMode mode)
 15: {
 16:   VecScatter_MPI_General *to,*from;
 17:   PetscScalar            *xv,*yv,*svalues;
 18:   MPI_Request            *rwaits,*swaits;
 19:   PetscErrorCode         ierr;
 20:   PetscInt               i,*indices,*sstarts,nrecvs,nsends,bs;

 23:   VecGetArray(xin,&xv);
 24:   if (xin != yin) {VecGetArray(yin,&yv);} else {yv = xv;}
 25:   if (mode & SCATTER_REVERSE) {
 26:     to   = (VecScatter_MPI_General*)ctx->fromdata;
 27:     from = (VecScatter_MPI_General*)ctx->todata;
 28:     rwaits   = from->rev_requests;
 29:     swaits   = to->rev_requests;
 30:   } else {
 31:     to   = (VecScatter_MPI_General*)ctx->todata;
 32:     from = (VecScatter_MPI_General*)ctx->fromdata;
 33:     rwaits   = from->requests;
 34:     swaits   = to->requests;
 35:   }
 36:   bs       = to->bs;
 37:   svalues  = to->values;
 38:   nrecvs   = from->n;
 39:   nsends   = to->n;
 40:   indices  = to->indices;
 41:   sstarts  = to->starts;

 43:   if (!(mode & SCATTER_LOCAL)) {

 45:     if (!from->use_readyreceiver && !to->sendfirst && !to->use_alltoallv  & !to->use_window) {
 46:       /* post receives since they were not previously posted    */
 47:       if (nrecvs) {MPI_Startall_irecv(from->starts[nrecvs]*bs,nrecvs,rwaits);}
 48:     }

 50: #if defined(PETSC_HAVE_MPI_ALLTOALLW)  && !defined(PETSC_USE_64BIT_INDICES)
 51:     if (to->use_alltoallw && addv == INSERT_VALUES) {
 52:       MPI_Alltoallw(xv,to->wcounts,to->wdispls,to->types,yv,from->wcounts,from->wdispls,from->types,((PetscObject)ctx)->comm);
 53:     } else
 54: #endif
 55:     if (ctx->packtogether || to->use_alltoallv) {
 56:       /* this version packs all the messages together and sends, when -vecscatter_packtogether used */
 57:       PETSCMAP1(Pack)(sstarts[nsends],indices,xv,svalues);
 58:       if (to->use_alltoallv) {
 59:         MPI_Alltoallv(to->values,to->counts,to->displs,MPIU_SCALAR,from->values,from->counts,from->displs,MPIU_SCALAR,((PetscObject)ctx)->comm);
 60: #if defined(PETSC_HAVE_MPI_WIN_CREATE)
 61:       } else if (to->use_window) {
 62:         PetscInt cnt;

 64:         MPI_Win_fence(0,from->window);
 65:         for (i=0; i<nsends; i++) {
 66:           cnt  = bs*(to->starts[i+1]-to->starts[i]);
 67:           MPI_Put(to->values+to->starts[i],cnt,MPIU_SCALAR,to->procs[i],0/*disp*/,cnt,MPIU_SCALAR,from->window);
 68:         }
 69:         MPI_Win_fence(0,from->window);
 70: #endif
 71:       } else if (nsends) {
 72:         MPI_Startall_isend(to->starts[to->n],nsends,swaits);
 73:       }
 74:     } else {
 75:       /* this version packs and sends one at a time */
 76:       for (i=0; i<nsends; i++) {
 77:         PETSCMAP1(Pack)(sstarts[i+1]-sstarts[i],indices + sstarts[i],xv,svalues + bs*sstarts[i]);
 78:         MPI_Start_isend(sstarts[i+1]-sstarts[i],swaits+i);
 79:       }
 80:     }

 82:     if (!from->use_readyreceiver && to->sendfirst && !to->use_alltoallv && !to->use_window) {
 83:       /* post receives since they were not previously posted   */
 84:       if (nrecvs) {MPI_Startall_irecv(from->starts[nrecvs]*bs,nrecvs,rwaits);}
 85:     }
 86:   }

 88:   /* take care of local scatters */
 89:   if (to->local.n) {
 90:     if (to->local.is_copy && addv == INSERT_VALUES) {
 91:       PetscMemcpy(yv + from->local.copy_start,xv + to->local.copy_start,to->local.copy_length);
 92:     } else {
 93:       PETSCMAP1(Scatter)(to->local.n,to->local.vslots,xv,from->local.vslots,yv,addv);
 94:     }
 95:   }
 96:   VecRestoreArray(xin,&xv);
 97:   if (xin != yin) {VecRestoreArray(yin,&yv);}
 98:   return(0);
 99: }

101: /* --------------------------------------------------------------------------------------*/

105: PetscErrorCode PETSCMAP1(VecScatterEnd)(VecScatter ctx,Vec xin,Vec yin,InsertMode addv,ScatterMode mode)
106: {
107:   VecScatter_MPI_General *to,*from;
108:   PetscScalar            *rvalues,*yv;
109:   PetscErrorCode         ierr;
110:   PetscInt               nrecvs,nsends,*indices,count,*rstarts,bs;
111:   PetscMPIInt            imdex;
112:   MPI_Request            *rwaits,*swaits;
113:   MPI_Status             xrstatus,*rstatus,*sstatus;

116:   if (mode & SCATTER_LOCAL) return(0);
117:   VecGetArray(yin,&yv);

119:   to       = (VecScatter_MPI_General*)ctx->todata;
120:   from     = (VecScatter_MPI_General*)ctx->fromdata;
121:   rwaits   = from->requests;
122:   swaits   = to->requests;
123:   sstatus  = to->sstatus;   /* sstatus and rstatus are always stored in to */
124:   rstatus  = to->rstatus;
125:   if (mode & SCATTER_REVERSE) {
126:     to       = (VecScatter_MPI_General*)ctx->fromdata;
127:     from     = (VecScatter_MPI_General*)ctx->todata;
128:     rwaits   = from->rev_requests;
129:     swaits   = to->rev_requests;
130:   }
131:   bs       = from->bs;
132:   rvalues  = from->values;
133:   nrecvs   = from->n;
134:   nsends   = to->n;
135:   indices  = from->indices;
136:   rstarts  = from->starts;

138:   if (ctx->packtogether || (to->use_alltoallw && (addv != INSERT_VALUES)) || (to->use_alltoallv && !to->use_alltoallw)) {
139:     if (nrecvs && !to->use_alltoallv && !to->use_window) {MPI_Waitall(nrecvs,rwaits,rstatus);}
140:     PETSCMAP1(UnPack)(from->starts[from->n],from->values,indices,yv,addv);
141:   } else if (!to->use_alltoallw) {
142:     /* unpack one at a time */
143:     count = nrecvs;
144:     while (count) {
145:       if (ctx->reproduce) {
146:         imdex = count - 1;
147:         MPI_Wait(rwaits+imdex,&xrstatus);
148:       } else {
149:         MPI_Waitany(nrecvs,rwaits,&imdex,&xrstatus);
150:       }
151:       /* unpack receives into our local space */
152:       PETSCMAP1(UnPack)(rstarts[imdex+1] - rstarts[imdex],rvalues + bs*rstarts[imdex],indices + rstarts[imdex],yv,addv);
153:       count--;
154:     }
155:   }
156:   if (from->use_readyreceiver) {
157:     if (nrecvs) {MPI_Startall_irecv(from->starts[nrecvs]*bs,nrecvs,rwaits);}
158:     MPI_Barrier(((PetscObject)ctx)->comm);
159:   }

161:   /* wait on sends */
162:   if (nsends  && !to->use_alltoallv  && !to->use_window) {MPI_Waitall(nsends,swaits,sstatus);}
163:   VecRestoreArray(yin,&yv);
164:   return(0);
165: }

167: #undef PETSCMAP1_a
168: #undef PETSCMAP1_b
169: #undef PETSCMAP1
170: #undef BS