Actual source code: mpimesg.c

petsc-master 2019-06-22
Report Typos and Errors

  2:  #include <petscsys.h>


  5: /*@C
  6:   PetscGatherNumberOfMessages -  Computes the number of messages a node expects to receive

  8:   Collective

 10:   Input Parameters:
 11: + comm     - Communicator
 12: . iflags   - an array of integers of length sizeof(comm). A '1' in ilengths[i] represent a
 13:              message from current node to ith node. Optionally NULL
 14: - ilengths - Non zero ilengths[i] represent a message to i of length ilengths[i].
 15:              Optionally NULL.

 17:   Output Parameters:
 18: . nrecvs    - number of messages received

 20:   Level: developer

 22:   Notes:
 23:   With this info, the correct message lengths can be determined using
 24:   PetscGatherMessageLengths()

 26:   Either iflags or ilengths should be provided.  If iflags is not
 27:   provided (NULL) it can be computed from ilengths. If iflags is
 28:   provided, ilengths is not required.

 30: .seealso: PetscGatherMessageLengths()
 31: @*/
 32: PetscErrorCode  PetscGatherNumberOfMessages(MPI_Comm comm,const PetscMPIInt iflags[],const PetscMPIInt ilengths[],PetscMPIInt *nrecvs)
 33: {
 34:   PetscMPIInt    size,rank,*recv_buf,i,*iflags_local = NULL,*iflags_localm = NULL;

 38:   MPI_Comm_size(comm,&size);
 39:   MPI_Comm_rank(comm,&rank);

 41:   PetscMalloc2(size,&recv_buf,size,&iflags_localm);

 43:   /* If iflags not provided, compute iflags from ilengths */
 44:   if (!iflags) {
 45:     if (!ilengths) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Either iflags or ilengths should be provided");
 46:     iflags_local = iflags_localm;
 47:     for (i=0; i<size; i++) {
 48:       if (ilengths[i]) iflags_local[i] = 1;
 49:       else iflags_local[i] = 0;
 50:     }
 51:   } else iflags_local = (PetscMPIInt*) iflags;

 53:   /* Post an allreduce to determine the numer of messages the current node will receive */
 54:   MPIU_Allreduce(iflags_local,recv_buf,size,MPI_INT,MPI_SUM,comm);
 55:   *nrecvs = recv_buf[rank];

 57:   PetscFree2(recv_buf,iflags_localm);
 58:   return(0);
 59: }


 62: /*@C
 63:   PetscGatherMessageLengths - Computes info about messages that a MPI-node will receive,
 64:   including (from-id,length) pairs for each message.

 66:   Collective

 68:   Input Parameters:
 69: + comm      - Communicator
 70: . nsends    - number of messages that are to be sent.
 71: . nrecvs    - number of messages being received
 72: - ilengths  - an array of integers of length sizeof(comm)
 73:               a non zero ilengths[i] represent a message to i of length ilengths[i]


 76:   Output Parameters:
 77: + onodes    - list of node-ids from which messages are expected
 78: - olengths  - corresponding message lengths

 80:   Level: developer

 82:   Notes:
 83:   With this info, the correct MPI_Irecv() can be posted with the correct
 84:   from-id, with a buffer with the right amount of memory required.

 86:   The calling function deallocates the memory in onodes and olengths

 88:   To determine nrecevs, one can use PetscGatherNumberOfMessages()

 90: .seealso: PetscGatherNumberOfMessages()
 91: @*/
 92: PetscErrorCode  PetscGatherMessageLengths(MPI_Comm comm,PetscMPIInt nsends,PetscMPIInt nrecvs,const PetscMPIInt ilengths[],PetscMPIInt **onodes,PetscMPIInt **olengths)
 93: {
 95:   PetscMPIInt    size,tag,i,j;
 96:   MPI_Request    *s_waits  = NULL,*r_waits = NULL;
 97:   MPI_Status     *w_status = NULL;

100:   MPI_Comm_size(comm,&size);
101:   PetscCommGetNewTag(comm,&tag);

103:   /* cannot use PetscMalloc3() here because in the call to MPI_Waitall() they MUST be contiguous */
104:   PetscMalloc2(nrecvs+nsends,&r_waits,nrecvs+nsends,&w_status);
105:   s_waits = r_waits+nrecvs;

107:   /* Post the Irecv to get the message length-info */
108:   PetscMalloc1(nrecvs,olengths);
109:   for (i=0; i<nrecvs; i++) {
110:     MPI_Irecv((*olengths)+i,1,MPI_INT,MPI_ANY_SOURCE,tag,comm,r_waits+i);
111:   }

113:   /* Post the Isends with the message length-info */
114:   for (i=0,j=0; i<size; ++i) {
115:     if (ilengths[i]) {
116:       MPI_Isend((void*)(ilengths+i),1,MPI_INT,i,tag,comm,s_waits+j);
117:       j++;
118:     }
119:   }

121:   /* Post waits on sends and receivs */
122:   if (nrecvs+nsends) {MPI_Waitall(nrecvs+nsends,r_waits,w_status);}

124:   /* Pack up the received data */
125:   PetscMalloc1(nrecvs,onodes);
126:   for (i=0; i<nrecvs; ++i) (*onodes)[i] = w_status[i].MPI_SOURCE;
127:   PetscFree2(r_waits,w_status);
128:   return(0);
129: }

131: /*@C
132:   PetscGatherMessageLengths2 - Computes info about messages that a MPI-node will receive,
133:   including (from-id,length) pairs for each message. Same functionality as PetscGatherMessageLengths()
134:   except it takes TWO ilenths and output TWO olengths.

136:   Collective

138:   Input Parameters:
139: + comm      - Communicator
140: . nsends    - number of messages that are to be sent.
141: . nrecvs    - number of messages being received
142: - ilengths1, ilengths2 - array of integers of length sizeof(comm)
143:               a non zero ilengths[i] represent a message to i of length ilengths[i]

145:   Output Parameters:
146: + onodes    - list of node-ids from which messages are expected
147: - olengths1, olengths2 - corresponding message lengths

149:   Level: developer

151:   Notes:
152:   With this info, the correct MPI_Irecv() can be posted with the correct
153:   from-id, with a buffer with the right amount of memory required.

155:   The calling function deallocates the memory in onodes and olengths

157:   To determine nrecevs, one can use PetscGatherNumberOfMessages()

159: .seealso: PetscGatherMessageLengths() and PetscGatherNumberOfMessages()
160: @*/
161: PetscErrorCode  PetscGatherMessageLengths2(MPI_Comm comm,PetscMPIInt nsends,PetscMPIInt nrecvs,const PetscMPIInt ilengths1[],const PetscMPIInt ilengths2[],PetscMPIInt **onodes,PetscMPIInt **olengths1,PetscMPIInt **olengths2)
162: {
164:   PetscMPIInt    size,tag,i,j,*buf_s = NULL,*buf_r = NULL,*buf_j = NULL;
165:   MPI_Request    *s_waits  = NULL,*r_waits = NULL;
166:   MPI_Status     *w_status = NULL;

169:   MPI_Comm_size(comm,&size);
170:   PetscCommGetNewTag(comm,&tag);

172:   /* cannot use PetscMalloc5() because r_waits and s_waits must be contiguous for the call to MPI_Waitall() */
173:   PetscMalloc4(nrecvs+nsends,&r_waits,2*nrecvs,&buf_r,2*nsends,&buf_s,nrecvs+nsends,&w_status);
174:   s_waits = r_waits + nrecvs;

176:   /* Post the Irecv to get the message length-info */
177:   PetscMalloc1(nrecvs+1,olengths1);
178:   PetscMalloc1(nrecvs+1,olengths2);
179:   for (i=0; i<nrecvs; i++) {
180:     buf_j = buf_r + (2*i);
181:     MPI_Irecv(buf_j,2,MPI_INT,MPI_ANY_SOURCE,tag,comm,r_waits+i);
182:   }

184:   /* Post the Isends with the message length-info */
185:   for (i=0,j=0; i<size; ++i) {
186:     if (ilengths1[i]) {
187:       buf_j    = buf_s + (2*j);
188:       buf_j[0] = *(ilengths1+i);
189:       buf_j[1] = *(ilengths2+i);
190:       MPI_Isend(buf_j,2,MPI_INT,i,tag,comm,s_waits+j);
191:       j++;
192:     }
193:   }
194:   if (j != nsends) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"j %d not equal to expected number of sends %d\n",j,nsends);

196:   /* Post waits on sends and receivs */
197:   if (nrecvs+nsends) {MPI_Waitall(nrecvs+nsends,r_waits,w_status);}


200:   /* Pack up the received data */
201:   PetscMalloc1(nrecvs+1,onodes);
202:   for (i=0; i<nrecvs; ++i) {
203:     (*onodes)[i]    = w_status[i].MPI_SOURCE;
204:     buf_j           = buf_r + (2*i);
205:     (*olengths1)[i] = buf_j[0];
206:     (*olengths2)[i] = buf_j[1];
207:   }

209:   PetscFree4(r_waits,buf_r,buf_s,w_status);
210:   return(0);
211: }

213: /*

215:   Allocate a bufffer sufficient to hold messages of size specified in olengths.
216:   And post Irecvs on these buffers using node info from onodes

218:  */
219: PetscErrorCode  PetscPostIrecvInt(MPI_Comm comm,PetscMPIInt tag,PetscMPIInt nrecvs,const PetscMPIInt onodes[],const PetscMPIInt olengths[],PetscInt ***rbuf,MPI_Request **r_waits)
220: {
222:   PetscInt       **rbuf_t,i,len = 0;
223:   MPI_Request    *r_waits_t;

226:   /* compute memory required for recv buffers */
227:   for (i=0; i<nrecvs; i++) len += olengths[i];  /* each message length */

229:   /* allocate memory for recv buffers */
230:   PetscMalloc1(nrecvs+1,&rbuf_t);
231:   PetscMalloc1(len,&rbuf_t[0]);
232:   for (i=1; i<nrecvs; ++i) rbuf_t[i] = rbuf_t[i-1] + olengths[i-1];

234:   /* Post the receives */
235:   PetscMalloc1(nrecvs,&r_waits_t);
236:   for (i=0; i<nrecvs; ++i) {
237:     MPI_Irecv(rbuf_t[i],olengths[i],MPIU_INT,onodes[i],tag,comm,r_waits_t+i);
238:   }

240:   *rbuf    = rbuf_t;
241:   *r_waits = r_waits_t;
242:   return(0);
243: }

245: PetscErrorCode  PetscPostIrecvScalar(MPI_Comm comm,PetscMPIInt tag,PetscMPIInt nrecvs,const PetscMPIInt onodes[],const PetscMPIInt olengths[],PetscScalar ***rbuf,MPI_Request **r_waits)
246: {
248:   PetscMPIInt    i;
249:   PetscScalar    **rbuf_t;
250:   MPI_Request    *r_waits_t;
251:   PetscInt       len = 0;

254:   /* compute memory required for recv buffers */
255:   for (i=0; i<nrecvs; i++) len += olengths[i];  /* each message length */

257:   /* allocate memory for recv buffers */
258:   PetscMalloc1(nrecvs+1,&rbuf_t);
259:   PetscMalloc1(len,&rbuf_t[0]);
260:   for (i=1; i<nrecvs; ++i) rbuf_t[i] = rbuf_t[i-1] + olengths[i-1];

262:   /* Post the receives */
263:   PetscMalloc1(nrecvs,&r_waits_t);
264:   for (i=0; i<nrecvs; ++i) {
265:     MPI_Irecv(rbuf_t[i],olengths[i],MPIU_SCALAR,onodes[i],tag,comm,r_waits_t+i);
266:   }

268:   *rbuf    = rbuf_t;
269:   *r_waits = r_waits_t;
270:   return(0);
271: }