1 | #include "ep_lib.hpp" |
---|
2 | #include <mpi.h> |
---|
3 | #include "ep_declaration.hpp" |
---|
4 | #include <iostream> |
---|
5 | #include <fstream> |
---|
6 | |
---|
7 | using namespace std; |
---|
8 | |
---|
9 | std::list< ep_lib::MPI_Request* > * EP_PendingRequests = 0; |
---|
10 | #pragma omp threadprivate(EP_PendingRequests) |
---|
11 | |
---|
12 | |
---|
13 | |
---|
14 | namespace ep_lib |
---|
15 | { |
---|
16 | bool MPI_Comm::is_null() |
---|
17 | { |
---|
18 | if(!this->is_intercomm) |
---|
19 | return this->mpi_comm == MPI_COMM_NULL.mpi_comm; |
---|
20 | else |
---|
21 | return this->ep_comm_ptr->intercomm->mpi_inter_comm == MPI_COMM_NULL.mpi_comm; |
---|
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 | for(int i=0; i<comm.rank_map->size(); i++) |
---|
36 | { |
---|
37 | if( ( comm.rank_map->at(i).first == ep_rank_loc ) |
---|
38 | && ( comm.rank_map->at(i).second == mpi_rank ) ) |
---|
39 | { |
---|
40 | return i; |
---|
41 | } |
---|
42 | } |
---|
43 | printf("rank not find\n"); |
---|
44 | } |
---|
45 | |
---|
46 | int get_ep_rank_intercomm(MPI_Comm comm, int ep_rank_loc, int mpi_rank) |
---|
47 | { |
---|
48 | // intercomm |
---|
49 | int inter_rank; |
---|
50 | for(int i=0; i<comm.ep_comm_ptr->intercomm->intercomm_rank_map->size(); i++) |
---|
51 | { |
---|
52 | if( ( comm.ep_comm_ptr->intercomm->intercomm_rank_map->at(i).first == ep_rank_loc ) |
---|
53 | && ( comm.ep_comm_ptr->intercomm->intercomm_rank_map->at(i).second == mpi_rank ) ) |
---|
54 | { |
---|
55 | inter_rank = i; |
---|
56 | break; |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | for(int i=0; i<comm.ep_comm_ptr->intercomm->remote_rank_map->size(); i++) |
---|
61 | { |
---|
62 | if( comm.ep_comm_ptr->intercomm->remote_rank_map->at(i).first == inter_rank ) |
---|
63 | { |
---|
64 | //printf("get_ep_rank for intercomm, ep_rank_loc = %d, mpi_rank = %d => ep_src = %d\n", ep_rank_loc, mpi_rank, i); |
---|
65 | return i; |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | printf("rank not find\n"); |
---|
70 | |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | int MPI_Get_count(const MPI_Status *status, MPI_Datatype datatype, int *count) |
---|
75 | { |
---|
76 | |
---|
77 | ::MPI_Status *mpi_status = static_cast< ::MPI_Status* >(status->mpi_status); |
---|
78 | ::MPI_Datatype mpi_datatype = static_cast< ::MPI_Datatype >(datatype); |
---|
79 | |
---|
80 | ::MPI_Get_count(mpi_status, mpi_datatype, count); |
---|
81 | } |
---|
82 | |
---|
83 | double MPI_Wtime() |
---|
84 | { |
---|
85 | return ::MPI_Wtime(); |
---|
86 | |
---|
87 | } |
---|
88 | |
---|
89 | void check_sum_send(const void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm, int type) |
---|
90 | { |
---|
91 | int src_rank; |
---|
92 | int int_count; |
---|
93 | ::MPI_Aint datasize, intsize, charsize, lb; |
---|
94 | |
---|
95 | ::MPI_Type_get_extent(static_cast< ::MPI_Datatype>(datatype), &lb, &datasize); |
---|
96 | ::MPI_Type_get_extent(static_cast< ::MPI_Datatype> (MPI_CHAR), &lb, &intsize); |
---|
97 | |
---|
98 | int_count = count * datasize / intsize ; |
---|
99 | |
---|
100 | char *buffer = static_cast< char* >(const_cast< void*> (buf)); |
---|
101 | |
---|
102 | unsigned long sum = 0; |
---|
103 | for(int i = 0; i<int_count; i++) |
---|
104 | sum += *(buffer+i); |
---|
105 | |
---|
106 | |
---|
107 | MPI_Comm_rank(comm, &src_rank); |
---|
108 | |
---|
109 | ofstream myfile; |
---|
110 | myfile.open ("send_log.txt", ios::app); |
---|
111 | if (myfile.is_open()) |
---|
112 | { |
---|
113 | myfile << "type = " << type << " src = "<< src_rank<< " dest = "<< dest <<" tag = "<< tag << " count = "<< count << " sum = "<< sum << "\n"; |
---|
114 | myfile.close(); |
---|
115 | } |
---|
116 | else printf("Unable to open file\n"); |
---|
117 | |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | void check_sum_recv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, int type) |
---|
122 | { |
---|
123 | int dest_rank; |
---|
124 | int int_count; |
---|
125 | ::MPI_Aint datasize, intsize, charsize, lb; |
---|
126 | |
---|
127 | ::MPI_Type_get_extent(static_cast< ::MPI_Datatype>(datatype), &lb, &datasize); |
---|
128 | ::MPI_Type_get_extent(static_cast< ::MPI_Datatype> (MPI_CHAR), &lb, &intsize); |
---|
129 | |
---|
130 | int_count = count * datasize / intsize ; |
---|
131 | |
---|
132 | char *buffer = static_cast< char* >(buf); |
---|
133 | |
---|
134 | unsigned long sum = 0; |
---|
135 | for(int i = 0; i<int_count; i++) |
---|
136 | sum += *(buffer+i); |
---|
137 | |
---|
138 | |
---|
139 | MPI_Comm_rank(comm, &dest_rank); |
---|
140 | |
---|
141 | ofstream myfile; |
---|
142 | myfile.open ("recv_log.txt", ios::app); |
---|
143 | if (myfile.is_open()) |
---|
144 | { |
---|
145 | myfile << "type = " << type << " src = "<< src << " dest = "<< dest_rank <<" tag = "<< tag << " count = "<< count << " sum = "<< sum << "\n"; |
---|
146 | myfile.close(); |
---|
147 | } |
---|
148 | else printf("Unable to open file\n"); |
---|
149 | |
---|
150 | } |
---|
151 | |
---|
152 | int test_sendrecv(MPI_Comm comm) |
---|
153 | { |
---|
154 | int myRank; |
---|
155 | MPI_Comm_rank(comm, &myRank); |
---|
156 | bool amClient = false; |
---|
157 | bool amServer = false; |
---|
158 | if(myRank<=3) amClient = true; |
---|
159 | else amServer = true; |
---|
160 | |
---|
161 | if(amServer) |
---|
162 | { |
---|
163 | int send_buf[4]; |
---|
164 | MPI_Request send_request[8]; |
---|
165 | MPI_Status send_status[8]; |
---|
166 | |
---|
167 | |
---|
168 | |
---|
169 | for(int j=0; j<4; j++) // 4 buffers |
---|
170 | { |
---|
171 | for(int i=0; i<2; i++) |
---|
172 | { |
---|
173 | send_buf[j] = (myRank+1)*100 + j; |
---|
174 | MPI_Isend(&send_buf[j], 1, MPI_INT, i*2, 9999, comm, &send_request[i*4+j]); |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | |
---|
179 | MPI_Waitall(8, send_request, send_status); |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | if(amClient&&myRank%2==0) // Clients leaders |
---|
184 | { |
---|
185 | int recv_buf[8]; |
---|
186 | MPI_Request recv_request[8]; |
---|
187 | MPI_Status recv_status[8]; |
---|
188 | |
---|
189 | for(int i=0; i<2; i++) // 2 servers |
---|
190 | { |
---|
191 | for(int j=0; j<4; j++) |
---|
192 | { |
---|
193 | MPI_Irecv(&recv_buf[i*4+j], 1, MPI_INT, i+4, 9999, comm, &recv_request[i*4+j]); |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | MPI_Waitall(8, recv_request, recv_status); |
---|
198 | printf("============ client %d, recv_buf = %d, %d, %d, %d, %d, %d, %d, %d ================\n", |
---|
199 | 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]); |
---|
200 | } |
---|
201 | |
---|
202 | MPI_Barrier(comm); |
---|
203 | |
---|
204 | } |
---|
205 | |
---|
206 | bool valid_type(MPI_Datatype datatype) |
---|
207 | { |
---|
208 | if( datatype == MPI_INT |
---|
209 | || datatype == MPI_FLOAT |
---|
210 | || datatype == MPI_DOUBLE |
---|
211 | || datatype == MPI_CHAR |
---|
212 | || datatype == MPI_LONG |
---|
213 | || datatype == MPI_UNSIGNED_LONG) |
---|
214 | { |
---|
215 | return true; |
---|
216 | } |
---|
217 | |
---|
218 | return false; |
---|
219 | } |
---|
220 | |
---|
221 | |
---|
222 | bool valid_op(MPI_Op op) |
---|
223 | { |
---|
224 | if( op == MPI_MAX |
---|
225 | || op == MPI_MIN |
---|
226 | || op == MPI_SUM) |
---|
227 | { |
---|
228 | return true; |
---|
229 | } |
---|
230 | |
---|
231 | return false; |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | } |
---|
236 | |
---|
237 | |
---|
238 | MPI_Datatype to_mpi_type(ep_lib::MPI_Datatype type) |
---|
239 | { |
---|
240 | return static_cast< MPI_Datatype >(type); |
---|
241 | } |
---|
242 | |
---|
243 | MPI_Op to_mpi_op(ep_lib::MPI_Op op) |
---|
244 | { |
---|
245 | return static_cast< MPI_Op >(op); |
---|
246 | } |
---|
247 | |
---|
248 | MPI_Comm to_mpi_comm(int* comm) |
---|
249 | { |
---|
250 | return static_cast< MPI_Comm >(*comm); |
---|
251 | } |
---|
252 | |
---|
253 | |
---|
254 | |
---|
255 | |
---|
256 | |
---|
257 | |
---|