source: XIOS/dev/branch_openmp/extern/ep_dev/ep_message.cpp @ 1538

Last change on this file since 1538 was 1538, checked in by yushan, 6 years ago

tests in XIOS OK (client, complete, remap, toy)

File size: 6.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#include "ep_mpi.hpp"
12
13using namespace std;
14
15extern std::list< ep_lib::MPI_Request* > * EP_PendingRequests;
16#pragma omp threadprivate(EP_PendingRequests)
17
18namespace ep_lib
19{
20  int Request_Check()
21  {
22    if(EP_PendingRequests == 0 ) EP_PendingRequests = new std::list< MPI_Request* >;
23   
24    if(EP_PendingRequests->size() == 0) return 0;
25   
26    show_EP_PendingRequests(EP_PendingRequests);
27   
28    MPI_Status status;
29    MPI_Message *message;
30    int probed = false;
31    int recv_count = 0;
32    std::list<MPI_Request* >::iterator it;
33   
34    show_EP_PendingRequests(EP_PendingRequests);
35   
36   
37    for(it = EP_PendingRequests->begin(); it!=EP_PendingRequests->end(); it++)
38    { 
39      if(*(*it) == 0)
40      {
41        EP_PendingRequests->erase(it);
42       
43        memcheck("EP_PendingRequests["<<ep_rank<<"]->size() = " << EP_PendingRequests->size());
44        it = EP_PendingRequests->begin();
45        continue;
46      }
47     
48      if((*(*it))->state == 2)
49      {
50        EP_PendingRequests->erase(it);
51       
52        memcheck("EP_PendingRequests["<<ep_rank<<"]->size() = " << EP_PendingRequests->size());
53        it = EP_PendingRequests->begin();
54        continue;
55      }
56     
57      Message_Check(((*(*it))->comm));
58    }
59   
60   
61
62
63    for(it = EP_PendingRequests->begin(); it!=EP_PendingRequests->end(); )
64    {
65      if(*(*it) == 0)
66      {
67        EP_PendingRequests->erase(it);
68       
69        memcheck("EP_PendingRequests["<<ep_rank<<"]->size() = " << EP_PendingRequests->size());
70        it = EP_PendingRequests->begin();
71        continue;
72      }
73     
74      if((*(*it))->state == 2)
75      {
76        EP_PendingRequests->erase(it);
77       
78        memcheck("EP_PendingRequests["<<ep_rank<<"]->size() = " << EP_PendingRequests->size());
79        it = EP_PendingRequests->begin();
80        continue;
81      }
82     
83      if((*(*it))->probed == false)
84      {
85        #pragma omp critical (_query0)
86        {
87          MPI_Iprobe_endpoint((*(*it))->ep_src, (*(*it))->ep_tag, ((*(*it))->comm), &probed, &status);
88          if(probed)
89          {
90            message = new MPI_Message;
91            *message = new ep_message;
92       
93            memcheck("new "<< message <<" : in ep_lib::Request_Check, message = new MPI_Message");
94            memcheck("new "<< *message <<" : in ep_lib::Request_Check, *message = new ep_message");
95         
96         
97            MPI_Improbe_endpoint((*(*it))->ep_src, (*(*it))->ep_tag, (*(*it))->comm, &probed, message, &status);
98       
99          }
100        }
101     
102       
103        if(probed)
104        {
105          MPI_Get_count(&status, (*(*it))->ep_datatype, &recv_count);
106         
107          MPI_Imrecv((*(*it))->buf, recv_count, (*(*it))->ep_datatype, message, *it);
108          (*(*it))->type = 3;
109          (*(*it))->probed = true;
110
111          memcheck("delete "<< status.mpi_status <<" : in ep_lib::Request_Check, delete status.mpi_status");
112          delete status.mpi_status;         
113
114          memcheck("delete "<< *message <<" : in ep_lib::Request_Check, delete *message");
115          memcheck("delete "<< message <<" : in ep_lib::Request_Check, delete message");
116
117          delete *message;
118          delete message;
119       
120          it++;
121          continue;     
122        }             
123      }
124     
125      it++;
126    }
127   
128    show_EP_PendingRequests(EP_PendingRequests);
129  }
130 
131 
132 
133  int Message_Check(MPI_Comm comm)
134  {
135    if(comm->is_ep) return Message_Check_endpoint(comm);
136  }
137 
138 
139  int Message_Check_endpoint(MPI_Comm comm)
140  {
141   
142    int flag = true;
143    ::MPI_Status status;
144    ::MPI_Message message;
145
146    while(flag) // loop until the end of global queue
147    {
148      Debug("Message probing for intracomm\n");
149     
150      #pragma omp critical (_mpi_call)
151      {
152        ::MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, to_mpi_comm(comm->mpi_comm), &flag, &status);
153        if(flag)
154        {
155          Debug("find message in mpi comm \n");
156          ::MPI_Mprobe(status.MPI_SOURCE, status.MPI_TAG, to_mpi_comm(comm->mpi_comm), &message, &status);
157        }
158      }
159
160     
161      if(flag)
162      {
163        MPI_Message msg = new ep_message; 
164        msg->mpi_message = new ::MPI_Message(message);
165
166        memcheck("new "<< msg <<" : in ep_lib::Message_Check, msg = new ep_message");
167        memcheck("new "<< msg->mpi_message <<" : in ep_lib::Message_Check, msg->mpi_message = new ::MPI_Message");
168             
169
170        msg->ep_tag  = bitset<15>(status.MPI_TAG >> 16).to_ulong(); 
171        int src_loc  = bitset<8> (status.MPI_TAG >> 8) .to_ulong(); 
172        int dest_loc = bitset<8> (status.MPI_TAG)           .to_ulong();
173        int src_mpi  = status.MPI_SOURCE;
174             
175        msg->ep_src  = get_ep_rank(comm, src_loc,  src_mpi); 
176
177#ifdef _showinfo
178        printf("status.MPI_TAG = %d, src_loc = %d, dest_loc = %d, ep_tag = %d\n", status.MPI_TAG, src_loc, dest_loc, msg->ep_tag);
179#endif
180
181        msg->mpi_status = new ::MPI_Status(status); 
182        memcheck("new "<< msg->mpi_status <<" : in ep_lib::Message_Check, msg->mpi_status = new ::MPI_Status");
183
184        #pragma omp critical (_query)
185        {
186          #pragma omp flush
187          comm->ep_comm_ptr->comm_list[dest_loc]->ep_comm_ptr->message_queue->push_back(msg); 
188          int dest_mpi = comm->ep_comm_ptr->size_rank_info[2].first;
189          memcheck("message_queue["<<dest_mpi<<","<<dest_loc<<"]->size = "<<comm->ep_comm_ptr->comm_list[dest_loc]->ep_comm_ptr->message_queue->size());
190          #pragma omp flush
191        }
192      }
193    }
194
195    return MPI_SUCCESS;
196  }
197
198 
199  void show_EP_PendingRequests(std::list< ep_lib::MPI_Request* > * EP_PendingRequest)
200  {
201#ifdef _showinfo
202    std::list<MPI_Request* >::iterator it;
203   
204    int i=0;
205   
206    for(it = EP_PendingRequests->begin(); it!=EP_PendingRequests->end(); it++)
207    { 
208      if(*(*it) == 0)
209      {
210        EP_PendingRequests->erase(it);
211           
212        memcheck("EP_PendingRequests["<<ep_rank<<"]->size() = " << EP_PendingRequests->size());
213        it = EP_PendingRequests->begin();
214        continue;
215      }
216     
217      if((*(*it))->state == 2)
218      {
219        EP_PendingRequests->erase(it);
220       
221        memcheck("EP_PendingRequests["<<ep_rank<<"]->size() = " << EP_PendingRequests->size());
222        it = EP_PendingRequests->begin();
223        continue;
224      }
225         
226      if((*(*it))->ep_src>6)
227        printf("EP_PendingRequests[%d] : ep_src = %d, ep_tag = %d, type = %d, state = %d, comm = %d\n", i, (*(*it))->ep_src, (*(*it))->ep_tag, (*(*it))->type, (*(*it))->state, to_mpi_comm(((*(*it))->comm)->mpi_comm));
228      i++;
229    }
230#endif
231
232  }
233
234 
235
236}
237
Note: See TracBrowser for help on using the repository browser.