recv.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "mpiP.h"
  2. /*
  3. * RECEIVING
  4. *
  5. */
  6. static int mpi_match_send(void *r, void *tag)
  7. {
  8. return( *((int *)tag) == MPI_ANY_TAG ||
  9. *((int *)tag) == ((Req *)r)->tag );
  10. }
  11. /*
  12. *
  13. */
  14. FC_FUNC( mpi_irecv , MPI_IRECV )(void *buf, int *count, int *datatype,
  15. int *source, int *tag, int *comm,
  16. int *request, int *ierror)
  17. {
  18. *ierror=MPI_Irecv(buf,*count,*datatype,*source,*tag,
  19. *comm, (void *)request);
  20. }
  21. int MPI_Irecv(void *buf, int count, MPI_Datatype datatype,
  22. int source, int tag, MPI_Comm comm, MPI_Request *request)
  23. {
  24. pListitem match;
  25. Comm *mycomm;
  26. Req *rreq, *sreq;
  27. mycomm=mpi_handle_to_ptr(comm); /* mycomm=(Comm *)comm; */
  28. #ifdef INFO
  29. fflush(stdout);
  30. fprintf(stderr,"MPI_Irecv: Comm=%d tag=%d count=%d type=%d\n",
  31. mycomm->num,tag,count,datatype);
  32. #endif
  33. if (source!=0 && source!=MPI_ANY_SOURCE && source!=MPI_PROC_NULL)
  34. {
  35. fprintf(stderr,"MPI_Irecv: bad source %d\n",source);
  36. abort();
  37. }
  38. mpi_alloc_handle(request,(void **)&rreq);
  39. if (source==MPI_PROC_NULL)
  40. {
  41. rreq->complete=1;
  42. rreq->source=MPI_PROC_NULL;
  43. rreq->tag=MPI_ANY_TAG;
  44. return(MPI_SUCCESS);
  45. }
  46. if ( match=AP_list_search_func(mycomm->sendlist,mpi_match_send,&tag) )
  47. {
  48. sreq=(Req *)AP_listitem_data(match);
  49. AP_list_delete_item(mycomm->sendlist,match);
  50. memcpy(buf,sreq->buf,count * datatype);
  51. rreq->complete=1;
  52. rreq->source=0;
  53. rreq->tag=sreq->tag; /* in case tag was MPI_ANY_TAG */
  54. sreq->complete=1;
  55. #ifdef DEBUG
  56. printf("Completion(recv) value=%d tag=%d\n",
  57. *((int *)buf),rreq->tag);
  58. #endif
  59. return(MPI_SUCCESS);
  60. }
  61. rreq->buf=buf;
  62. rreq->tag=tag;
  63. rreq->complete=0;
  64. rreq->listitem=AP_list_append(mycomm->recvlist,rreq);
  65. #ifdef INFO
  66. print_list(mycomm->recvlist,"recvlist for comm ",mycomm->num);
  67. #endif
  68. return(MPI_SUCCESS);
  69. }
  70. /*********/
  71. FC_FUNC( mpi_recv , MPI_RECV )(void *buf, int *count, int *datatype,
  72. int *source, int *tag, int *comm,
  73. int *status, int *ierror)
  74. {
  75. *ierror=MPI_Recv(buf,*count,*datatype,*source,*tag,*comm,
  76. (MPI_Status *)status);
  77. }
  78. int MPI_Recv(void *buf, int count, MPI_Datatype datatype, int source,
  79. int tag, MPI_Comm comm, MPI_Status *status)
  80. {
  81. MPI_Request request;
  82. #ifdef INFO
  83. fflush(stdout);
  84. fprintf(stderr,"MPI_Recv: ");
  85. #endif
  86. MPI_Irecv(buf,count,datatype,source,tag,comm,&request);
  87. MPI_Wait(&request,status);
  88. return(MPI_SUCCESS);
  89. }
  90. #ifdef INFO
  91. int print_item(void *item, void *data)
  92. {
  93. fprintf(stderr,"%d ", ((Req *)item)->tag);
  94. return(0);
  95. }
  96. int print_list(pList list, char *msg, int num)
  97. {
  98. fflush(stdout);
  99. fprintf(stderr,"%s %d: ",msg,num);
  100. AP_list_apply(list,print_item,NULL);
  101. fprintf(stderr,"\n");
  102. return(0);
  103. }
  104. #endif