1 | #include "ep_lib.hpp" |
---|
2 | #include <mpi.h> |
---|
3 | #include "ep_declaration.hpp" |
---|
4 | #include <iostream> |
---|
5 | #include <fstream> |
---|
6 | #include "ep_mpi.hpp" |
---|
7 | |
---|
8 | using namespace std; |
---|
9 | |
---|
10 | std::list< ep_lib::MPI_Request* > * EP_PendingRequests = 0; |
---|
11 | #pragma omp threadprivate(EP_PendingRequests) |
---|
12 | |
---|
13 | |
---|
14 | std::map<std::pair<int, int>, MPI_Group* > * tag_group_map = 0; |
---|
15 | |
---|
16 | std::map<int, std::pair<ep_lib::MPI_Comm*, std::pair<int, int> > > * tag_comm_map = 0; |
---|
17 | |
---|
18 | |
---|
19 | MPI_Group MPI_GROUP_WORLD; |
---|
20 | |
---|
21 | namespace ep_lib |
---|
22 | { |
---|
23 | |
---|
24 | int tag_combine(int real_tag, int src, int dest) |
---|
25 | { |
---|
26 | int a = real_tag << 16; |
---|
27 | int b = src << 8; |
---|
28 | int c = dest; |
---|
29 | |
---|
30 | return a+b+c; |
---|
31 | } |
---|
32 | |
---|
33 | int get_ep_rank(MPI_Comm comm, int ep_rank_loc, int mpi_rank) |
---|
34 | { |
---|
35 | /*if(comm->is_intercomm) |
---|
36 | { |
---|
37 | for(std::map<int, std::pair< int, std::pair<int, int> > >::iterator it = comm->ep_comm_ptr->intercomm->intercomm_rank_map->begin(); it != comm->ep_comm_ptr->intercomm->intercomm_rank_map->end(); it++) |
---|
38 | { |
---|
39 | if( ( it->second.first == ep_rank_loc ) |
---|
40 | && ( it->second.second.first == mpi_rank ) ) |
---|
41 | { |
---|
42 | return it->first; |
---|
43 | } |
---|
44 | } |
---|
45 | printf("rank not find for EP_intercomm\n"); |
---|
46 | int err; |
---|
47 | return MPI_Abort(comm, err); |
---|
48 | }*/ |
---|
49 | |
---|
50 | for(std::map<int, std::pair<int, int> >::iterator it = comm->ep_rank_map->begin(); it != comm->ep_rank_map->end(); it++) |
---|
51 | { |
---|
52 | if( ( it->second.first == ep_rank_loc ) |
---|
53 | && ( it->second.second == mpi_rank ) ) |
---|
54 | { |
---|
55 | return it->first; |
---|
56 | } |
---|
57 | } |
---|
58 | printf("rank not find for EP_intracomm\n"); |
---|
59 | int err; |
---|
60 | return MPI_Abort(comm, err); |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | int MPI_Get_count(const MPI_Status *status, MPI_Datatype datatype, int *count) |
---|
65 | { |
---|
66 | return ::MPI_Get_count(to_mpi_status_ptr(*status), to_mpi_type(datatype), count); |
---|
67 | } |
---|
68 | |
---|
69 | double MPI_Wtime() |
---|
70 | { |
---|
71 | return ::MPI_Wtime(); |
---|
72 | |
---|
73 | } |
---|
74 | |
---|
75 | void check_sum_send(const void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm, int type) |
---|
76 | { |
---|
77 | |
---|
78 | int src_rank; |
---|
79 | int int_count; |
---|
80 | ::MPI_Aint datasize, intsize, charsize, lb; |
---|
81 | |
---|
82 | ::MPI_Type_get_extent(*(static_cast< ::MPI_Datatype*>(datatype)), &lb, &datasize); |
---|
83 | ::MPI_Type_get_extent(*(static_cast< ::MPI_Datatype*>(MPI_CHAR)), &lb, &intsize); |
---|
84 | |
---|
85 | int_count = count * datasize / intsize ; |
---|
86 | |
---|
87 | char *buffer = static_cast< char* >(const_cast< void*> (buf)); |
---|
88 | |
---|
89 | unsigned long sum = 0; |
---|
90 | for(int i = 0; i<int_count; i++) |
---|
91 | sum += *(buffer+i); |
---|
92 | |
---|
93 | |
---|
94 | MPI_Comm_rank(comm, &src_rank); |
---|
95 | |
---|
96 | ofstream myfile; |
---|
97 | myfile.open ("send_log.txt", ios::app); |
---|
98 | if (myfile.is_open()) |
---|
99 | { |
---|
100 | myfile << "type = " << type << " src = "<< src_rank<< " dest = "<< dest <<" tag = "<< tag << " count = "<< count << " sum = "<< sum << "\n"; |
---|
101 | myfile.close(); |
---|
102 | } |
---|
103 | else printf("Unable to open file\n"); |
---|
104 | |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | void check_sum_recv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, int type) |
---|
109 | { |
---|
110 | |
---|
111 | int dest_rank; |
---|
112 | int int_count; |
---|
113 | ::MPI_Aint datasize, intsize, charsize, lb; |
---|
114 | |
---|
115 | ::MPI_Type_get_extent(*(static_cast< ::MPI_Datatype*>(datatype)), &lb, &datasize); |
---|
116 | ::MPI_Type_get_extent(*(static_cast< ::MPI_Datatype*>(MPI_CHAR)), &lb, &intsize); |
---|
117 | |
---|
118 | int_count = count * datasize / intsize ; |
---|
119 | |
---|
120 | char *buffer = static_cast< char* >(buf); |
---|
121 | |
---|
122 | unsigned long sum = 0; |
---|
123 | for(int i = 0; i<int_count; i++) |
---|
124 | sum += *(buffer+i); |
---|
125 | |
---|
126 | |
---|
127 | MPI_Comm_rank(comm, &dest_rank); |
---|
128 | |
---|
129 | ofstream myfile; |
---|
130 | myfile.open ("recv_log.txt", ios::app); |
---|
131 | if (myfile.is_open()) |
---|
132 | { |
---|
133 | myfile << "type = " << type << " src = "<< src << " dest = "<< dest_rank <<" tag = "<< tag << " count = "<< count << " sum = "<< sum << "\n"; |
---|
134 | myfile.close(); |
---|
135 | } |
---|
136 | else printf("Unable to open file\n"); |
---|
137 | |
---|
138 | |
---|
139 | } |
---|
140 | |
---|
141 | int test_sendrecv(MPI_Comm comm) |
---|
142 | { |
---|
143 | int myRank; |
---|
144 | MPI_Comm_rank(comm, &myRank); |
---|
145 | bool amClient = false; |
---|
146 | bool amServer = false; |
---|
147 | if(myRank<=3) amClient = true; |
---|
148 | else amServer = true; |
---|
149 | |
---|
150 | if(amServer) |
---|
151 | { |
---|
152 | int send_buf[4]; |
---|
153 | MPI_Request send_request[8]; |
---|
154 | MPI_Status send_status[8]; |
---|
155 | |
---|
156 | |
---|
157 | |
---|
158 | for(int j=0; j<4; j++) // 4 buffers |
---|
159 | { |
---|
160 | for(int i=0; i<2; i++) |
---|
161 | { |
---|
162 | send_buf[j] = (myRank+1)*100 + j; |
---|
163 | MPI_Isend(&send_buf[j], 1, MPI_INT, i*2, 9999, comm, &send_request[i*4+j]); |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | |
---|
168 | MPI_Waitall(8, send_request, send_status); |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | if(amClient&&myRank%2==0) // Clients leaders |
---|
173 | { |
---|
174 | int recv_buf[8]; |
---|
175 | MPI_Request recv_request[8]; |
---|
176 | MPI_Status recv_status[8]; |
---|
177 | |
---|
178 | for(int i=0; i<2; i++) // 2 servers |
---|
179 | { |
---|
180 | for(int j=0; j<4; j++) |
---|
181 | { |
---|
182 | MPI_Irecv(&recv_buf[i*4+j], 1, MPI_INT, i+4, 9999, comm, &recv_request[i*4+j]); |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | MPI_Waitall(8, recv_request, recv_status); |
---|
187 | printf("============ client %d, recv_buf = %d, %d, %d, %d, %d, %d, %d, %d ================\n", |
---|
188 | myRank, recv_buf[0], recv_buf[1], recv_buf[2], recv_buf[3], recv_buf[4], recv_buf[5], recv_buf[6], recv_buf[7]); |
---|
189 | } |
---|
190 | |
---|
191 | MPI_Barrier(comm); |
---|
192 | |
---|
193 | } |
---|
194 | |
---|
195 | bool valid_type(MPI_Datatype datatype) |
---|
196 | { |
---|
197 | if( datatype == MPI_INT |
---|
198 | || datatype == MPI_FLOAT |
---|
199 | || datatype == MPI_DOUBLE |
---|
200 | || datatype == MPI_CHAR |
---|
201 | || datatype == MPI_LONG |
---|
202 | || datatype == MPI_UNSIGNED_LONG |
---|
203 | || datatype == MPI_UINT64_T) |
---|
204 | { |
---|
205 | return true; |
---|
206 | } |
---|
207 | |
---|
208 | return false; |
---|
209 | } |
---|
210 | |
---|
211 | |
---|
212 | bool valid_op(MPI_Op op) |
---|
213 | { |
---|
214 | if( op == MPI_MAX |
---|
215 | || op == MPI_MIN |
---|
216 | || op == MPI_SUM |
---|
217 | || op == MPI_LOR) |
---|
218 | { |
---|
219 | return true; |
---|
220 | } |
---|
221 | |
---|
222 | return false; |
---|
223 | } |
---|
224 | |
---|
225 | |
---|
226 | } |
---|
227 | |
---|
228 | |
---|
229 | MPI_Datatype to_mpi_type(ep_lib::MPI_Datatype type) |
---|
230 | { |
---|
231 | return *static_cast< MPI_Datatype* >(type); |
---|
232 | } |
---|
233 | |
---|
234 | MPI_Op to_mpi_op(ep_lib::MPI_Op op) |
---|
235 | { |
---|
236 | return *static_cast< MPI_Op* >(op); |
---|
237 | } |
---|
238 | |
---|
239 | MPI_Comm to_mpi_comm(void* comm) |
---|
240 | { |
---|
241 | return *(static_cast< MPI_Comm* >(comm)); |
---|
242 | } |
---|
243 | |
---|
244 | MPI_Comm* to_mpi_comm_ptr(void* comm) |
---|
245 | { |
---|
246 | return static_cast< MPI_Comm* >(comm); |
---|
247 | } |
---|
248 | |
---|
249 | MPI_Message to_mpi_message(void* message) |
---|
250 | { |
---|
251 | return *(static_cast< MPI_Message* >(message)); |
---|
252 | } |
---|
253 | |
---|
254 | MPI_Message* to_mpi_message_ptr(ep_lib::MPI_Message message) |
---|
255 | { |
---|
256 | return static_cast< MPI_Message* >(message->mpi_message); |
---|
257 | } |
---|
258 | |
---|
259 | MPI_Info to_mpi_info(ep_lib::MPI_Info info) |
---|
260 | { |
---|
261 | return *(static_cast< MPI_Info* >(info->mpi_info)); |
---|
262 | } |
---|
263 | |
---|
264 | MPI_Win to_mpi_win(void* win) |
---|
265 | { |
---|
266 | return *(static_cast< MPI_Win* >(win)); |
---|
267 | } |
---|
268 | |
---|
269 | MPI_Aint to_mpi_aint(ep_lib::MPI_Aint aint) |
---|
270 | { |
---|
271 | return *(static_cast< MPI_Aint* >(aint.mpi_aint)); |
---|
272 | } |
---|
273 | |
---|
274 | MPI_Status* to_mpi_status_ptr(ep_lib::MPI_Status status) |
---|
275 | { |
---|
276 | return static_cast< MPI_Status* >(status.mpi_status); |
---|
277 | } |
---|
278 | |
---|
279 | MPI_Request* to_mpi_request_ptr(ep_lib::MPI_Request request) |
---|
280 | { |
---|
281 | return static_cast< MPI_Request* >(request->mpi_request); |
---|
282 | } |
---|
283 | |
---|
284 | |
---|
285 | |
---|