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

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

save modif

File size: 8.6 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
80        //printf("myRank = %d, probed one message, ep_src = %d, ep_dest = %d, tag = %d, message = %d\n", myRank, msg_block->ep_src, msg_block->ep_dest, msg_block->ep_tag, msg_block->mpi_message);
81        msg_block->mpi_status = new ::MPI_Status(status);
82
83        MPI_Comm* ptr_comm_list = comm.ep_comm_ptr->comm_list;
84        MPI_Comm* ptr_comm_target = &ptr_comm_list[dest_loc];
85
86
87        #pragma omp critical (_query)
88        {
89          #pragma omp flush
90          ptr_comm_target->ep_comm_ptr->message_queue->push_back(*msg_block); 
91          // printf("myRank = %d, push_back OK, ep_src = %d, ep_tag = %d, mpi_status = %p (%p)\n", myRank,
92          //                                                                                   ptr_comm_target->ep_comm_ptr->message_queue->back().ep_src,
93          //                                                                                   ptr_comm_target->ep_comm_ptr->message_queue->back().ep_tag,
94          //                                                                                   msg_block->mpi_status, &status);
95   
96          #pragma omp flush
97        }
98       
99        delete msg_block;
100        //printf("myRank = %d, delete msg_block, queue size = %lu\n", myRank, ptr_comm_target->ep_comm_ptr->message_queue->size());
101       
102      }
103
104    }
105
106    return MPI_SUCCESS;
107  }
108
109
110
111  int Message_Check_intercomm(MPI_Comm comm)
112  {
113    if(!comm.ep_comm_ptr->intercomm->mpi_inter_comm) return 0;
114
115    Debug("Message probing for intercomm\n");
116
117    int flag = true;
118    ::MPI_Message message;
119    ::MPI_Status status;
120    int mpi_source;
121    int current_ep_rank;
122    MPI_Comm_rank(comm, &current_ep_rank);
123
124    while(flag) // loop until the end of global queue "comm.ep_comm_ptr->intercomm->mpi_inter_comm"
125    {
126      Debug("Message probing for intracomm\n");
127      ::MPI_Comm mpi_comm = static_cast< ::MPI_Comm> (comm.ep_comm_ptr->intercomm->mpi_inter_comm);  // => mpi_intercomm
128     
129      #ifdef _openmpi
130      #pragma omp critical (_mpi_call)
131      {
132        ::MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, mpi_comm, &flag, &status);
133        if(flag)
134        {
135          Debug("find message in mpi comm \n");
136          mpi_source = status.MPI_SOURCE;
137          int tag = status.MPI_TAG;
138          ::MPI_Mprobe(mpi_source, tag, mpi_comm, &message, &status);
139
140        }
141      }
142      #elif _intelmpi
143      #pragma omp critical (_mpi_call)
144      {
145        ::MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, mpi_comm, &flag, &status);
146        if(flag)
147        {
148          Debug("find message in mpi comm \n");
149          mpi_source = status.MPI_SOURCE;
150          int tag = status.MPI_TAG;
151          ::MPI_Mprobe(mpi_source, tag, mpi_comm, &message, &status);
152
153        }
154      }
155      //::MPI_Improbe(MPI_ANY_SOURCE, MPI_ANY_TAG, mpi_comm, &flag, &message, &status);       
156      #endif
157
158      if(flag)
159      {
160
161        MPI_Message *msg_block = new MPI_Message;
162
163        msg_block->mpi_message = message;
164        msg_block->ep_tag = bitset<15>(status.MPI_TAG >> 16).to_ulong();
165        int src_loc       = bitset<8> (status.MPI_TAG >> 8) .to_ulong();
166        int dest_loc      = bitset<8> (status.MPI_TAG)      .to_ulong();
167        int src_mpi       = status.MPI_SOURCE;
168        int current_inter = comm.ep_comm_ptr->intercomm->local_rank_map->at(current_ep_rank).first;
169             
170        msg_block->ep_src  = get_ep_rank_intercomm(comm, src_loc,  src_mpi);
171        msg_block->mpi_status = new ::MPI_Status(status);
172
173
174        MPI_Comm* ptr_comm_list = comm.ep_comm_ptr->comm_list;
175        MPI_Comm* ptr_comm_target = &ptr_comm_list[dest_loc];
176
177
178        #pragma omp critical (_query)
179        {
180          #pragma omp flush
181          ptr_comm_target->ep_comm_ptr->message_queue->push_back(*msg_block);
182          #pragma omp flush
183        }
184       
185        delete msg_block;
186       
187      }
188
189    }
190
191    flag = true;
192    while(flag) // loop until the end of global queue "comm.mpi_comm"
193    {
194      Debug("Message probing for intracomm\n");
195      ::MPI_Comm mpi_comm = static_cast< ::MPI_Comm> (comm.mpi_comm);
196      #ifdef _openmpi
197      #pragma omp critical (_mpi_call)
198      {
199        ::MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, mpi_comm, &flag, &status);
200        if(flag)
201        {
202          Debug("find message in mpi comm \n");
203          mpi_source = status.MPI_SOURCE;
204          int tag = status.MPI_TAG;
205          ::MPI_Mprobe(mpi_source, tag, mpi_comm, &message, &status);
206
207        }
208      }
209      #elif _intelmpi
210      #pragma omp critical (_mpi_call)
211      {
212        ::MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, mpi_comm, &flag, &status);
213        if(flag)
214        {
215          Debug("find message in mpi comm \n");
216          mpi_source = status.MPI_SOURCE;
217          int tag = status.MPI_TAG;
218          ::MPI_Mprobe(mpi_source, tag, mpi_comm, &message, &status);
219
220        }
221      }
222      //::MPI_Improbe(MPI_ANY_SOURCE, MPI_ANY_TAG, mpi_comm, &flag, &message, &status);       
223      #endif
224
225      if(flag)
226      {
227
228        MPI_Message *msg_block = new MPI_Message;
229       
230        msg_block->mpi_message = message;
231        msg_block->ep_tag = bitset<15>(status.MPI_TAG >> 16).to_ulong();
232        int src_loc       = bitset<8> (status.MPI_TAG >> 8) .to_ulong();
233        int dest_loc      = bitset<8> (status.MPI_TAG)      .to_ulong();
234        int src_mpi       = status.MPI_SOURCE;
235        int current_inter = comm.ep_comm_ptr->intercomm->local_rank_map->at(current_ep_rank).first;
236       
237        msg_block->ep_src  = get_ep_rank_intercomm(comm, src_loc, src_mpi);
238        msg_block->mpi_status = new ::MPI_Status(status);
239       
240
241        MPI_Comm* ptr_comm_list = comm.ep_comm_ptr->comm_list;
242        MPI_Comm* ptr_comm_target = &ptr_comm_list[dest_loc];
243
244
245        #pragma omp critical (_query)
246        {
247          #pragma omp flush
248          ptr_comm_target->ep_comm_ptr->message_queue->push_back(*msg_block);
249          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);
250          #pragma omp flush
251        }
252       
253        delete msg_block;
254       
255      }
256
257    }
258
259    return MPI_SUCCESS;
260  }
261
262}
Note: See TracBrowser for help on using the repository browser.