Actual source code: mpimesg.c

petsc-3.3-p7 2013-05-11
  2: #include <petscsys.h>        /*I  "petscsys.h"  I*/


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

 10:   Collective on MPI_Comm

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

 19:   Output Parameters:
 20: . nrecvs    - number of messages received

 22:   Level: developer

 24:   Concepts: mpi utility

 26:   Notes:
 27:   With this info, the correct message lengths can be determined using
 28:   PetscGatherMessageLengths()

 30:   Either iflags or ilengths should be provided.  If iflags is not
 31:   provided (PETSC_NULL) it can be computed from ilengths. If iflags is
 32:   provided, ilengths is not required.

 34: .seealso: PetscGatherMessageLengths()
 35: @*/
 36: PetscErrorCode  PetscGatherNumberOfMessages(MPI_Comm comm,const PetscMPIInt iflags[],const PetscMPIInt ilengths[],PetscMPIInt *nrecvs)
 37: {
 38:   PetscMPIInt    size,rank,*recv_buf,i,*iflags_local = PETSC_NULL,*iflags_localm = PETSC_NULL;


 43:   MPI_Comm_size(comm,&size);
 44:   MPI_Comm_rank(comm,&rank);

 46:   PetscMalloc2(size,PetscMPIInt,&recv_buf,size,PetscMPIInt,&iflags_localm);

 48:   /* If iflags not provided, compute iflags from ilengths */
 49:   if (!iflags) {
 50:     if (!ilengths) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Either iflags or ilengths should be provided");
 51:     iflags_local = iflags_localm;
 52:     for (i=0; i<size; i++) {
 53:       if (ilengths[i])  iflags_local[i] = 1;
 54:       else iflags_local[i] = 0;
 55:     }
 56:   } else {
 57:     iflags_local = (PetscMPIInt *) iflags;
 58:   }

 60:   /* Post an allreduce to determine the numer of messages the current node will receive */
 61:   MPI_Allreduce(iflags_local,recv_buf,size,MPI_INT,MPI_SUM,comm);
 62:   *nrecvs = recv_buf[rank];

 64:   PetscFree2(recv_buf,iflags_localm);
 65:   return(0);
 66: }


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

 75:   Collective on MPI_Comm

 77:   Input Parameters:
 78: + comm      - Communicator
 79: . nsends    - number of messages that are to be sent.
 80: . nrecvs    - number of messages being received
 81: - ilengths  - an array of integers of length sizeof(comm)
 82:               a non zero ilengths[i] represent a message to i of length ilengths[i] 


 85:   Output Parameters:
 86: + onodes    - list of node-ids from which messages are expected
 87: - olengths  - corresponding message lengths

 89:   Level: developer

 91:   Concepts: mpi utility

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

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

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

101: .seealso: PetscGatherNumberOfMessages()
102: @*/
103: PetscErrorCode  PetscGatherMessageLengths(MPI_Comm comm,PetscMPIInt nsends,PetscMPIInt nrecvs,const PetscMPIInt ilengths[],PetscMPIInt **onodes,PetscMPIInt **olengths)
104: {
106:   PetscMPIInt    size,tag,i,j;
107:   MPI_Request    *s_waits = PETSC_NULL,*r_waits = PETSC_NULL;
108:   MPI_Status     *w_status = PETSC_NULL;

111:   MPI_Comm_size(comm,&size);
112:   PetscCommGetNewTag(comm,&tag);

114:   /* cannot use PetscMalloc3() here because in the call to MPI_Waitall() they MUST be contiguous */
115:   PetscMalloc2(nrecvs+nsends,MPI_Request,&r_waits,nrecvs+nsends,MPI_Status,&w_status);
116:   s_waits = r_waits+nrecvs;

118:   /* Post the Irecv to get the message length-info */
119:   PetscMalloc(nrecvs*sizeof(PetscMPIInt),olengths);
120:   for (i=0; i<nrecvs; i++) {
121:     MPI_Irecv((*olengths)+i,1,MPI_INT,MPI_ANY_SOURCE,tag,comm,r_waits+i);
122:   }

124:   /* Post the Isends with the message length-info */
125:   for (i=0,j=0; i<size; ++i) {
126:     if (ilengths[i]) {
127:       MPI_Isend((void*)(ilengths+i),1,MPI_INT,i,tag,comm,s_waits+j);
128:       j++;
129:     }
130:   }

132:   /* Post waits on sends and receivs */
133:   if (nrecvs+nsends) {MPI_Waitall(nrecvs+nsends,r_waits,w_status);}
134: 
135:   /* Pack up the received data */
136:   PetscMalloc(nrecvs*sizeof(PetscMPIInt),onodes);
137:   for (i=0; i<nrecvs; ++i) {
138:     (*onodes)[i] = w_status[i].MPI_SOURCE;
139:   }
140:   PetscFree2(r_waits,w_status);
141:   return(0);
142: }

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

151:   Collective on MPI_Comm

153:   Input Parameters:
154: + comm      - Communicator
155: . nsends    - number of messages that are to be sent.
156: . nrecvs    - number of messages being received
157: - ilengths1, ilengths2 - array of integers of length sizeof(comm)
158:               a non zero ilengths[i] represent a message to i of length ilengths[i] 

160:   Output Parameters:
161: + onodes    - list of node-ids from which messages are expected
162: - olengths1, olengths2 - corresponding message lengths

164:   Level: developer

166:   Concepts: mpi utility

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

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

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

176: .seealso: PetscGatherMessageLengths() and PetscGatherNumberOfMessages()
177: @*/
178: PetscErrorCode  PetscGatherMessageLengths2(MPI_Comm comm,PetscMPIInt nsends,PetscMPIInt nrecvs,const PetscMPIInt ilengths1[],const PetscMPIInt ilengths2[],PetscMPIInt **onodes,PetscMPIInt **olengths1,PetscMPIInt **olengths2)
179: {
181:   PetscMPIInt    size,tag,i,j,*buf_s = PETSC_NULL,*buf_r = PETSC_NULL,*buf_j = PETSC_NULL;
182:   MPI_Request    *s_waits = PETSC_NULL,*r_waits = PETSC_NULL;
183:   MPI_Status     *w_status = PETSC_NULL;

186:   MPI_Comm_size(comm,&size);
187:   PetscCommGetNewTag(comm,&tag);

189:   /* cannot use PetscMalloc5() because r_waits and s_waits must be contiquous for the call to MPI_Waitall() */
190:   PetscMalloc4(nrecvs+nsends,MPI_Request,&r_waits,2*nrecvs,PetscMPIInt,&buf_r,2*nsends,PetscMPIInt,&buf_s,nrecvs+nsends,MPI_Status,&w_status);
191:   s_waits = r_waits + nrecvs;

193:   /* Post the Irecv to get the message length-info */
194:   PetscMalloc((nrecvs+1)*sizeof(PetscMPIInt),olengths1);
195:   PetscMalloc((nrecvs+1)*sizeof(PetscMPIInt),olengths2);
196:   for (i=0; i<nrecvs; i++) {
197:     buf_j = buf_r + (2*i);
198:     MPI_Irecv(buf_j,2,MPI_INT,MPI_ANY_SOURCE,tag,comm,r_waits+i);
199:   }

201:   /* Post the Isends with the message length-info */
202:   for (i=0,j=0; i<size; ++i) {
203:     if (ilengths1[i]) {
204:       buf_j = buf_s + (2*j);
205:       buf_j[0] = *(ilengths1+i);
206:       buf_j[1] = *(ilengths2+i);
207:       MPI_Isend(buf_j,2,MPI_INT,i,tag,comm,s_waits+j);
208:       j++;
209:     }
210:   }
211: 
212:   /* Post waits on sends and receivs */
213:   if (nrecvs+nsends) {MPI_Waitall(nrecvs+nsends,r_waits,w_status);}

215: 
216:   /* Pack up the received data */
217:   PetscMalloc((nrecvs+1)*sizeof(PetscMPIInt),onodes);
218:   for (i=0; i<nrecvs; ++i) {
219:     (*onodes)[i] = w_status[i].MPI_SOURCE;
220:     buf_j = buf_r + (2*i);
221:     (*olengths1)[i] = buf_j[0];
222:     (*olengths2)[i] = buf_j[1];
223:   }

225:   PetscFree4(r_waits,buf_r,buf_s,w_status);
226:   return(0);
227: }

229: /*

231:   Allocate a bufffer sufficient to hold messages of size specified in olengths.
232:   And post Irecvs on these buffers using node info from onodes
233:   
234:  */
237: PetscErrorCode  PetscPostIrecvInt(MPI_Comm comm,PetscMPIInt tag,PetscMPIInt nrecvs,const PetscMPIInt onodes[],const PetscMPIInt olengths[],PetscInt ***rbuf,MPI_Request **r_waits)
238: {
240:   PetscInt       **rbuf_t,i,len = 0;
241:   MPI_Request    *r_waits_t;

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

247:   /* allocate memory for recv buffers */
248:   PetscMalloc((nrecvs+1)*sizeof(PetscInt*),&rbuf_t);
249:   PetscMalloc(len*sizeof(PetscInt),&rbuf_t[0]);
250:   for (i=1; i<nrecvs; ++i) rbuf_t[i] = rbuf_t[i-1] + olengths[i-1];

252:   /* Post the receives */
253:   PetscMalloc(nrecvs*sizeof(MPI_Request),&r_waits_t);
254:   for (i=0; i<nrecvs; ++i) {
255:     MPI_Irecv(rbuf_t[i],olengths[i],MPIU_INT,onodes[i],tag,comm,r_waits_t+i);
256:   }

258:   *rbuf    = rbuf_t;
259:   *r_waits = r_waits_t;
260:   return(0);
261: }

265: PetscErrorCode  PetscPostIrecvScalar(MPI_Comm comm,PetscMPIInt tag,PetscMPIInt nrecvs,const PetscMPIInt onodes[],const PetscMPIInt olengths[],PetscScalar ***rbuf,MPI_Request **r_waits)
266: {
268:   PetscMPIInt    i;
269:   PetscScalar    **rbuf_t;
270:   MPI_Request    *r_waits_t;
271:   PetscInt       len = 0;

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

277:   /* allocate memory for recv buffers */
278:   PetscMalloc((nrecvs+1)*sizeof(PetscScalar*),&rbuf_t);
279:   PetscMalloc(len*sizeof(PetscScalar),&rbuf_t[0]);
280:   for (i=1; i<nrecvs; ++i) rbuf_t[i] = rbuf_t[i-1] + olengths[i-1];

282:   /* Post the receives */
283:   PetscMalloc(nrecvs*sizeof(MPI_Request),&r_waits_t);
284:   for (i=0; i<nrecvs; ++i) {
285:     MPI_Irecv(rbuf_t[i],olengths[i],MPIU_SCALAR,onodes[i],tag,comm,r_waits_t+i);
286:   }

288:   *rbuf    = rbuf_t;
289:   *r_waits = r_waits_t;
290:   return(0);
291: }