source: XIOS/dev/branch_yushan/extern/src_ep_dev/ep_message.cpp @ 1126

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

test_complete OK with openmp. Missing : arithmetic filter

File size: 7.5 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      ::MPI_Improbe(MPI_ANY_SOURCE, MPI_ANY_TAG, mpi_comm, &flag, &message, &status); 
53      #endif
54
55      if(flag)
56      {
57
58        MPI_Message *msg_block = new MPI_Message; //printf("myRank = %d, new ok\n", myRank);
59        msg_block->mpi_message = message;  //printf("myRank = %d, msg_block->mpi_message = %d\n", myRank, msg_block->mpi_message);
60        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);
61        int src_loc       = bitset<8> (status.MPI_TAG >> 8) .to_ulong();  //printf("myRank = %d, src_loc = %d\n",           myRank, src_loc);
62        int dest_loc      = bitset<8> (status.MPI_TAG)      .to_ulong();  //printf("myRank = %d, dest_loc = %d\n",          myRank, dest_loc);
63        int src_mpi       = status.MPI_SOURCE;                            //printf("myRank = %d, src_mpi = %d\n",           myRank, src_mpi);
64             
65        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);
66
67        //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);
68        msg_block->mpi_status = new ::MPI_Status(status);
69
70        MPI_Comm* ptr_comm_list = comm.ep_comm_ptr->comm_list;
71        MPI_Comm* ptr_comm_target = &ptr_comm_list[dest_loc];
72
73
74        #pragma omp critical (_query)
75        {
76          #pragma omp flush
77          ptr_comm_target->ep_comm_ptr->message_queue->push_back(*msg_block); 
78          // printf("myRank = %d, push_back OK, ep_src = %d, ep_tag = %d, mpi_status = %p (%p)\n", myRank,
79          //                                                                                   ptr_comm_target->ep_comm_ptr->message_queue->back().ep_src,
80          //                                                                                   ptr_comm_target->ep_comm_ptr->message_queue->back().ep_tag,
81          //                                                                                   msg_block->mpi_status, &status);
82   
83          #pragma omp flush
84        }
85       
86        delete msg_block;
87        //printf("myRank = %d, delete msg_block, queue size = %lu\n", myRank, ptr_comm_target->ep_comm_ptr->message_queue->size());
88       
89      }
90
91    }
92
93    return MPI_SUCCESS;
94  }
95
96
97
98  int Message_Check_intercomm(MPI_Comm comm)
99  {
100    if(!comm.ep_comm_ptr->intercomm->mpi_inter_comm) return 0;
101
102    Debug("Message probing for intercomm\n");
103
104    int flag = true;
105    ::MPI_Message message;
106    ::MPI_Status status;
107    int mpi_source;
108    int current_ep_rank;
109    MPI_Comm_rank(comm, &current_ep_rank);
110
111    while(flag) // loop until the end of global queue "comm.ep_comm_ptr->intercomm->mpi_inter_comm"
112    {
113      Debug("Message probing for intracomm\n");
114      ::MPI_Comm mpi_comm = static_cast< ::MPI_Comm> (comm.ep_comm_ptr->intercomm->mpi_inter_comm);  // => mpi_intercomm
115     
116      #ifdef _openmpi
117      #pragma omp critical (_mpi_call)
118      {
119        ::MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, mpi_comm, &flag, &status);
120        if(flag)
121        {
122          Debug("find message in mpi comm \n");
123          mpi_source = status.MPI_SOURCE;
124          int tag = status.MPI_TAG;
125          ::MPI_Mprobe(mpi_source, tag, mpi_comm, &message, &status);
126
127        }
128      }
129      #elif _intelmpi
130      ::MPI_Improbe(MPI_ANY_SOURCE, MPI_ANY_TAG, mpi_comm, &flag, &message, &status);       
131      #endif
132
133      if(flag)
134      {
135
136        MPI_Message *msg_block = new MPI_Message;
137
138        msg_block->mpi_message = message;
139        msg_block->ep_tag = bitset<15>(status.MPI_TAG >> 16).to_ulong();
140        int src_loc       = bitset<8> (status.MPI_TAG >> 8) .to_ulong();
141        int dest_loc      = bitset<8> (status.MPI_TAG)      .to_ulong();
142        int src_mpi       = status.MPI_SOURCE;
143        int current_inter = comm.ep_comm_ptr->intercomm->local_rank_map->at(current_ep_rank).first;
144             
145        msg_block->ep_src  = get_ep_rank_intercomm(comm, src_loc,  src_mpi);
146        msg_block->mpi_status = new ::MPI_Status(status);
147
148
149        MPI_Comm* ptr_comm_list = comm.ep_comm_ptr->comm_list;
150        MPI_Comm* ptr_comm_target = &ptr_comm_list[dest_loc];
151
152
153        #pragma omp critical (_query)
154        {
155          #pragma omp flush
156          ptr_comm_target->ep_comm_ptr->message_queue->push_back(*msg_block);
157          #pragma omp flush
158        }
159       
160        delete msg_block;
161       
162      }
163
164    }
165
166    flag = true;
167    while(flag) // loop until the end of global queue "comm.mpi_comm"
168    {
169      Debug("Message probing for intracomm\n");
170      ::MPI_Comm mpi_comm = static_cast< ::MPI_Comm> (comm.mpi_comm);
171      #ifdef _openmpi
172      #pragma omp critical (_mpi_call)
173      {
174        ::MPI_Iprobe(MPI_ANY_SOURCE, MPI_ANY_TAG, mpi_comm, &flag, &status);
175        if(flag)
176        {
177          Debug("find message in mpi comm \n");
178          mpi_source = status.MPI_SOURCE;
179          int tag = status.MPI_TAG;
180          ::MPI_Mprobe(mpi_source, tag, mpi_comm, &message, &status);
181
182        }
183      }
184      #elif _intelmpi
185      ::MPI_Improbe(MPI_ANY_SOURCE, MPI_ANY_TAG, mpi_comm, &flag, &message, &status);       
186      #endif
187
188      if(flag)
189      {
190
191        MPI_Message *msg_block = new MPI_Message;
192       
193        msg_block->mpi_message = message;
194        msg_block->ep_tag = bitset<15>(status.MPI_TAG >> 16).to_ulong();
195        int src_loc       = bitset<8> (status.MPI_TAG >> 8) .to_ulong();
196        int dest_loc      = bitset<8> (status.MPI_TAG)      .to_ulong();
197        int src_mpi       = status.MPI_SOURCE;
198        int current_inter = comm.ep_comm_ptr->intercomm->local_rank_map->at(current_ep_rank).first;
199       
200        msg_block->ep_src  = get_ep_rank_intercomm(comm, src_loc, src_mpi);
201        msg_block->mpi_status = new ::MPI_Status(status);
202       
203
204        MPI_Comm* ptr_comm_list = comm.ep_comm_ptr->comm_list;
205        MPI_Comm* ptr_comm_target = &ptr_comm_list[dest_loc];
206
207
208        #pragma omp critical (_query)
209        {
210          #pragma omp flush
211          ptr_comm_target->ep_comm_ptr->message_queue->push_back(*msg_block);
212          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);
213          #pragma omp flush
214        }
215       
216        delete msg_block;
217       
218      }
219
220    }
221
222    return MPI_SUCCESS;
223  }
224
225}
Note: See TracBrowser for help on using the repository browser.