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