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