source: XIOS/dev/branch_yushan_merged/extern/src_ep_dev/ep_message.cpp @ 1187

Last change on this file since 1187 was 1187, checked in by yushan, 7 years ago

add pending list for irecv

File size: 8.7 KB
Line 
1/*!
2   \file ep_message.cpp
3   \since 2 may 2016
4
5   \brief Definitions of MPI endpoint function: Message_Check
6 */
7
8#include "ep_lib.hpp"
9#include <mpi.h>
10#include "ep_declaration.hpp"
11
12using namespace std;
13
14namespace ep_lib
15{
16
17  int Message_Check(MPI_Comm comm)
18  {
19    int myRank;
20    MPI_Comm_rank(comm, &myRank);
21
22    if(!comm.is_ep) return 0;
23
24    if(comm.is_intercomm)
25    {
26      return  Message_Check_intercomm(comm);
27    }
28
29    int flag = true;
30    ::MPI_Message message;
31    ::MPI_Status status;
32    int mpi_source;
33
34    while(flag) // loop until the end of global queue
35    {
36      Debug("Message probing for intracomm\n");
37      ::MPI_Comm mpi_comm = static_cast< ::MPI_Comm> (comm.mpi_comm);
38      #ifdef _openmpi
39      #pragma omp critical (_mpi_call)
40      {
41        ::MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, mpi_comm, &flag, &status);
42        if(flag)
43        {
44          Debug("find message in mpi comm \n");
45          mpi_source = status.MPI_SOURCE;
46          int tag = status.MPI_TAG;
47          ::MPI_Mprobe(mpi_source, tag, mpi_comm, &message, &status);
48
49        }
50      }
51      #elif _intelmpi
52      //#pragma omp critical (_mpi_call)
53      //::MPI_Improbe(MPI_ANY_SOURCE, MPI_ANY_TAG, mpi_comm, &flag, &message, &status);
54      #pragma omp critical (_mpi_call)
55      {
56        ::MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, mpi_comm, &flag, &status);
57        if(flag)
58        {
59          Debug("find message in mpi comm \n");
60          mpi_source = status.MPI_SOURCE;
61          int tag = status.MPI_TAG;
62          ::MPI_Mprobe(mpi_source, tag, mpi_comm, &message, &status);
63
64        }
65      }
66      #endif
67     
68      if(flag)
69      {
70
71        MPI_Message *msg_block = new MPI_Message; //printf("myRank = %d, new ok\n", myRank);
72        msg_block->mpi_message = message;  //printf("myRank = %d, msg_block->mpi_message = %d\n", myRank, msg_block->mpi_message);
73        msg_block->ep_tag = bitset<15>(status.MPI_TAG >> 16).to_ulong();  //printf("myRank = %d, msg_block->ep_tag = %d\n", myRank, msg_block->ep_tag);
74        int src_loc       = bitset<8> (status.MPI_TAG >> 8) .to_ulong();  //printf("myRank = %d, src_loc = %d\n",           myRank, src_loc);
75        int dest_loc      = bitset<8> (status.MPI_TAG)      .to_ulong();  //printf("myRank = %d, dest_loc = %d\n",          myRank, dest_loc);
76        int src_mpi       = status.MPI_SOURCE;                            //printf("myRank = %d, src_mpi = %d\n",           myRank, src_mpi);
77             
78        msg_block->ep_src  = get_ep_rank(comm, src_loc,  src_mpi);        //printf("myRank = %d, msg_block->ep_src = %d\n",  myRank, msg_block->ep_src);
79        int dest_mpi = comm.ep_comm_ptr->size_rank_info[2].first;
80        int ep_dest = get_ep_rank(comm, dest_loc, dest_mpi);
81        //printf("myRank = %d, probed one message, ep_src = %d, ep_dest = %d, tag = %d, message = %d\n", myRank, msg_block->ep_src, ep_dest, msg_block->ep_tag, msg_block->mpi_message);
82        msg_block->mpi_status = new ::MPI_Status(status);
83
84        MPI_Comm* ptr_comm_list = comm.ep_comm_ptr->comm_list;
85        MPI_Comm* ptr_comm_target = &ptr_comm_list[dest_loc];
86
87
88        #pragma omp critical (_query)
89        {
90          #pragma omp flush
91          ptr_comm_target->ep_comm_ptr->message_queue->push_back(*msg_block); 
92          //printf("myRank = %d, push_back OK, ep_src = %d, ep_tag = %d, dest = %d(%d)\n", myRank,
93          //                                                                                   ptr_comm_target->ep_comm_ptr->message_queue->back().ep_src,
94          //                                                                                   ptr_comm_target->ep_comm_ptr->message_queue->back().ep_tag,
95          //                                                                                   ep_dest, dest_loc);
96   
97          #pragma omp flush
98        }
99       
100        delete msg_block;
101        //printf("myRank = %d, delete msg_block, queue size = %lu\n", myRank, ptr_comm_target->ep_comm_ptr->message_queue->size());
102       
103      }
104
105    }
106
107    return MPI_SUCCESS;
108  }
109
110
111
112  int Message_Check_intercomm(MPI_Comm comm)
113  {
114    if(!comm.ep_comm_ptr->intercomm->mpi_inter_comm) return 0;
115
116    Debug("Message probing for intercomm\n");
117
118    int flag = true;
119    ::MPI_Message message;
120    ::MPI_Status status;
121    int mpi_source;
122    int current_ep_rank;
123    MPI_Comm_rank(comm, &current_ep_rank);
124
125    while(flag) // loop until the end of global queue "comm.ep_comm_ptr->intercomm->mpi_inter_comm"
126    {
127      Debug("Message probing for intracomm\n");
128      ::MPI_Comm mpi_comm = static_cast< ::MPI_Comm> (comm.ep_comm_ptr->intercomm->mpi_inter_comm);  // => mpi_intercomm
129     
130      #ifdef _openmpi
131      #pragma omp critical (_mpi_call)
132      {
133        ::MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, mpi_comm, &flag, &status);
134        if(flag)
135        {
136          Debug("find message in mpi comm \n");
137          mpi_source = status.MPI_SOURCE;
138          int tag = status.MPI_TAG;
139          ::MPI_Mprobe(mpi_source, tag, mpi_comm, &message, &status);
140
141        }
142      }
143      #elif _intelmpi
144      #pragma omp critical (_mpi_call)
145      {
146        ::MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, mpi_comm, &flag, &status);
147        if(flag)
148        {
149          Debug("find message in mpi comm \n");
150          mpi_source = status.MPI_SOURCE;
151          int tag = status.MPI_TAG;
152          ::MPI_Mprobe(mpi_source, tag, mpi_comm, &message, &status);
153
154        }
155      }
156      //::MPI_Improbe(MPI_ANY_SOURCE, MPI_ANY_TAG, mpi_comm, &flag, &message, &status);       
157      #endif
158
159      if(flag)
160      {
161
162        MPI_Message *msg_block = new MPI_Message;
163
164        msg_block->mpi_message = message;
165        msg_block->ep_tag = bitset<15>(status.MPI_TAG >> 16).to_ulong();
166        int src_loc       = bitset<8> (status.MPI_TAG >> 8) .to_ulong();
167        int dest_loc      = bitset<8> (status.MPI_TAG)      .to_ulong();
168        int src_mpi       = status.MPI_SOURCE;
169        int current_inter = comm.ep_comm_ptr->intercomm->local_rank_map->at(current_ep_rank).first;
170             
171        msg_block->ep_src  = get_ep_rank_intercomm(comm, src_loc,  src_mpi);
172        msg_block->mpi_status = new ::MPI_Status(status);
173
174
175        MPI_Comm* ptr_comm_list = comm.ep_comm_ptr->comm_list;
176        MPI_Comm* ptr_comm_target = &ptr_comm_list[dest_loc];
177
178
179        #pragma omp critical (_query)
180        {
181          #pragma omp flush
182          ptr_comm_target->ep_comm_ptr->message_queue->push_back(*msg_block);
183          #pragma omp flush
184        }
185       
186        delete msg_block;
187       
188      }
189
190    }
191
192    flag = true;
193    while(flag) // loop until the end of global queue "comm.mpi_comm"
194    {
195      Debug("Message probing for intracomm\n");
196      ::MPI_Comm mpi_comm = static_cast< ::MPI_Comm> (comm.mpi_comm);
197      #ifdef _openmpi
198      #pragma omp critical (_mpi_call)
199      {
200        ::MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, mpi_comm, &flag, &status);
201        if(flag)
202        {
203          Debug("find message in mpi comm \n");
204          mpi_source = status.MPI_SOURCE;
205          int tag = status.MPI_TAG;
206          ::MPI_Mprobe(mpi_source, tag, mpi_comm, &message, &status);
207
208        }
209      }
210      #elif _intelmpi
211      #pragma omp critical (_mpi_call)
212      {
213        ::MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, mpi_comm, &flag, &status);
214        if(flag)
215        {
216          Debug("find message in mpi comm \n");
217          mpi_source = status.MPI_SOURCE;
218          int tag = status.MPI_TAG;
219          ::MPI_Mprobe(mpi_source, tag, mpi_comm, &message, &status);
220
221        }
222      }
223      //::MPI_Improbe(MPI_ANY_SOURCE, MPI_ANY_TAG, mpi_comm, &flag, &message, &status);       
224      #endif
225
226      if(flag)
227      {
228
229        MPI_Message *msg_block = new MPI_Message;
230       
231        msg_block->mpi_message = message;
232        msg_block->ep_tag = bitset<15>(status.MPI_TAG >> 16).to_ulong();
233        int src_loc       = bitset<8> (status.MPI_TAG >> 8) .to_ulong();
234        int dest_loc      = bitset<8> (status.MPI_TAG)      .to_ulong();
235        int src_mpi       = status.MPI_SOURCE;
236        int current_inter = comm.ep_comm_ptr->intercomm->local_rank_map->at(current_ep_rank).first;
237       
238        msg_block->ep_src  = get_ep_rank_intercomm(comm, src_loc, src_mpi);
239        msg_block->mpi_status = new ::MPI_Status(status);
240       
241
242        MPI_Comm* ptr_comm_list = comm.ep_comm_ptr->comm_list;
243        MPI_Comm* ptr_comm_target = &ptr_comm_list[dest_loc];
244
245
246        #pragma omp critical (_query)
247        {
248          #pragma omp flush
249          ptr_comm_target->ep_comm_ptr->message_queue->push_back(*msg_block);
250          printf("probed one message, ep_src = %d, tag = %d, mpi_status = %p (%p), message = %d\n", msg_block->ep_src, msg_block->ep_tag, msg_block->mpi_status, &status, msg_block->mpi_message);
251          #pragma omp flush
252        }
253       
254        delete msg_block;
255       
256      }
257
258    }
259
260    return MPI_SUCCESS;
261  }
262
263}
Note: See TracBrowser for help on using the repository browser.