[1134] | 1 | #ifndef EP_TYPE_HPP_INCLUDED |
---|
| 2 | #define EP_TYPE_HPP_INCLUDED |
---|
| 3 | |
---|
| 4 | |
---|
| 5 | #include <iostream> |
---|
| 6 | #include <stdlib.h> |
---|
| 7 | #include <stdio.h> |
---|
| 8 | #include <list> |
---|
| 9 | #include <map> |
---|
| 10 | #include <omp.h> |
---|
| 11 | #include <vector> |
---|
| 12 | #include <numeric> |
---|
| 13 | #include <bitset> |
---|
| 14 | //#include <memory.h> |
---|
| 15 | #include <algorithm> |
---|
| 16 | #include <assert.h> |
---|
| 17 | #include <math.h> |
---|
[1287] | 18 | #include <string.h> |
---|
[1134] | 19 | |
---|
| 20 | #ifdef _Debug |
---|
| 21 | #define Debug(x) std::cout << x << std::endl |
---|
| 22 | #else |
---|
| 23 | #define Debug(x) |
---|
| 24 | #endif |
---|
| 25 | |
---|
| 26 | #define BUFFER_SIZE 10000 |
---|
| 27 | |
---|
[1295] | 28 | //#define NUM_THREADS 12 |
---|
| 29 | |
---|
[1134] | 30 | typedef std::pair< int, int > SIZE_RANK_INFO; // < rank, size> |
---|
| 31 | |
---|
| 32 | typedef std::vector< std::pair<int, int> > RANK_MAP; // at(ep_rank) = <ep_rank_local, mpi_rank> |
---|
| 33 | |
---|
| 34 | typedef std::vector<std::pair< std::pair<int, int>, std::pair<int, int> > > INTERCOMM_RANK_MAP; |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | typedef struct |
---|
| 38 | { |
---|
| 39 | int first; |
---|
| 40 | int second; |
---|
| 41 | int third; |
---|
| 42 | } Triple_int; |
---|
| 43 | |
---|
| 44 | namespace ep_lib |
---|
| 45 | { |
---|
| 46 | //#define EP_UNDEFINED -32766 |
---|
| 47 | |
---|
| 48 | class ep_communicator; |
---|
| 49 | class ep_intercomm; |
---|
| 50 | class OMPbarrier; |
---|
| 51 | typedef ep_communicator* EP_Comm; |
---|
| 52 | class MPI_Comm; |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | class MPI_Status |
---|
| 56 | { |
---|
| 57 | public: |
---|
| 58 | |
---|
| 59 | #ifdef _intelmpi |
---|
| 60 | int ep_datatype; |
---|
| 61 | #elif _openmpi |
---|
| 62 | void * ep_datatype; |
---|
| 63 | #endif |
---|
| 64 | |
---|
| 65 | int ep_src; |
---|
| 66 | int ep_tag; |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | void* mpi_status; |
---|
| 70 | }; |
---|
| 71 | |
---|
| 72 | class MPI_Message |
---|
| 73 | { |
---|
| 74 | public: |
---|
| 75 | |
---|
| 76 | #ifdef _intelmpi |
---|
| 77 | int mpi_message; |
---|
| 78 | #elif _openmpi |
---|
| 79 | void * mpi_message; |
---|
| 80 | #endif |
---|
| 81 | |
---|
| 82 | int ep_src; |
---|
| 83 | int ep_tag; |
---|
| 84 | |
---|
| 85 | void* mpi_status; |
---|
| 86 | |
---|
| 87 | MPI_Message() {} |
---|
| 88 | |
---|
| 89 | #ifdef _intelmpi |
---|
| 90 | MPI_Message(int message): mpi_message(message) {} |
---|
| 91 | #elif _openmpi |
---|
| 92 | MPI_Message(void* message): mpi_message(message) {} |
---|
| 93 | #endif |
---|
| 94 | }; |
---|
| 95 | |
---|
| 96 | typedef std::list<MPI_Message > Message_list; |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | class OMPbarrier |
---|
| 100 | { |
---|
| 101 | private: |
---|
| 102 | int nbThreads; //<The number of threads for this barrier |
---|
| 103 | int currentNbThread; //<The current number of threads waiting |
---|
| 104 | bool sense; //<Direct barrier feedback protection |
---|
| 105 | omp_lock_t mutex; //<To have an atomic int |
---|
| 106 | |
---|
| 107 | OMPbarrier(OMPbarrier&){} |
---|
| 108 | OMPbarrier& operator=(OMPbarrier&){return *this;} |
---|
| 109 | |
---|
| 110 | public: |
---|
| 111 | /** Constructor with the number of threads */ |
---|
| 112 | explicit OMPbarrier(const int inNbThreads) |
---|
| 113 | : nbThreads(inNbThreads), currentNbThread(0), sense(false) { |
---|
| 114 | omp_init_lock( &mutex ); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /** Destructor, release the omp lock */ |
---|
| 118 | ~OMPbarrier(){ |
---|
| 119 | omp_destroy_lock( &mutex ); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | /** Perform a barrier */ |
---|
| 123 | void wait(){ |
---|
| 124 | const bool mySense = sense; |
---|
| 125 | omp_set_lock( &mutex ); |
---|
| 126 | const int nbThreadsArrived = (++currentNbThread); |
---|
| 127 | omp_unset_lock( &mutex ); |
---|
| 128 | |
---|
| 129 | if(nbThreadsArrived == nbThreads) { |
---|
| 130 | currentNbThread = 0; |
---|
| 131 | sense = !sense; |
---|
| 132 | #pragma omp flush |
---|
| 133 | } |
---|
| 134 | else { |
---|
| 135 | volatile const bool* const ptSense = &sense; |
---|
| 136 | while( (*ptSense) == mySense){ |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | |
---|
| 142 | /** Change the number of threads */ |
---|
| 143 | void setNbThreads(const int inNbThread){ |
---|
| 144 | omp_set_lock( &mutex ); |
---|
| 145 | nbThreads = inNbThread; |
---|
| 146 | omp_unset_lock( &mutex ); |
---|
| 147 | } |
---|
| 148 | }; |
---|
| 149 | |
---|
| 150 | class ep_intercomm |
---|
| 151 | { |
---|
| 152 | public: |
---|
| 153 | |
---|
| 154 | |
---|
| 155 | #ifdef _intelmpi |
---|
| 156 | int mpi_inter_comm; |
---|
| 157 | #elif _openmpi |
---|
| 158 | void * mpi_inter_comm; |
---|
| 159 | #endif |
---|
| 160 | |
---|
| 161 | RANK_MAP *intercomm_rank_map; |
---|
| 162 | RANK_MAP *local_rank_map; |
---|
| 163 | RANK_MAP *remote_rank_map; |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | SIZE_RANK_INFO size_rank_info[3]; |
---|
| 167 | |
---|
| 168 | |
---|
| 169 | MPI_Comm *local_comm; |
---|
| 170 | int intercomm_tag; |
---|
| 171 | |
---|
| 172 | ep_intercomm() |
---|
| 173 | { |
---|
| 174 | intercomm_rank_map = NULL; |
---|
| 175 | local_rank_map = NULL; |
---|
| 176 | remote_rank_map = NULL; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | |
---|
| 180 | bool operator == (ep_intercomm right) |
---|
| 181 | { |
---|
| 182 | bool a = intercomm_rank_map == right.intercomm_rank_map; |
---|
| 183 | bool b = local_rank_map == right.local_rank_map; |
---|
| 184 | bool c = remote_rank_map == right.remote_rank_map; |
---|
| 185 | bool d = mpi_inter_comm == right.mpi_inter_comm; |
---|
| 186 | bool e = size_rank_info == right.size_rank_info; |
---|
| 187 | bool f = intercomm_tag == right.intercomm_tag; |
---|
| 188 | return a&&b&&c&&d&&e&&f; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | bool operator != (ep_intercomm right) |
---|
| 192 | { |
---|
| 193 | bool a = intercomm_rank_map != right.intercomm_rank_map; |
---|
| 194 | bool b = local_rank_map != right.local_rank_map; |
---|
| 195 | bool c = remote_rank_map != right.remote_rank_map; |
---|
| 196 | bool d = mpi_inter_comm != right.mpi_inter_comm; |
---|
| 197 | bool e = size_rank_info != right.size_rank_info; |
---|
| 198 | bool f = intercomm_tag != right.intercomm_tag; |
---|
| 199 | return a||b||c||d||e||f; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | |
---|
| 203 | }; |
---|
| 204 | |
---|
| 205 | |
---|
| 206 | class ep_communicator |
---|
| 207 | { |
---|
| 208 | public: |
---|
| 209 | |
---|
| 210 | SIZE_RANK_INFO size_rank_info[3]; // 0: ep_rank, ep_size |
---|
| 211 | // 1: ep_rank_loc, num_ep |
---|
| 212 | // 2: mpi_rank, mpi_size |
---|
| 213 | |
---|
| 214 | |
---|
| 215 | MPI_Comm *comm_list; |
---|
| 216 | |
---|
| 217 | Message_list *message_queue; |
---|
| 218 | |
---|
| 219 | |
---|
| 220 | int comm_label; |
---|
| 221 | |
---|
| 222 | ep_intercomm *intercomm; |
---|
| 223 | |
---|
| 224 | ep_communicator() |
---|
| 225 | { |
---|
| 226 | comm_list = NULL; |
---|
| 227 | message_queue = NULL; |
---|
| 228 | intercomm = NULL; |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | bool operator == (ep_communicator right) |
---|
| 232 | { |
---|
| 233 | bool a = size_rank_info == right.size_rank_info; |
---|
| 234 | bool b = comm_label == right.comm_label; |
---|
| 235 | bool c = intercomm == right.intercomm; |
---|
| 236 | return a&&b&&c; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | bool operator != (ep_communicator right) |
---|
| 240 | { |
---|
| 241 | bool a = size_rank_info != right.size_rank_info; |
---|
| 242 | bool b = comm_label != right.comm_label; |
---|
| 243 | bool c = intercomm != right.intercomm; |
---|
| 244 | return a||b||c; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | }; |
---|
| 248 | |
---|
| 249 | |
---|
| 250 | struct BUFFER |
---|
| 251 | { |
---|
[1287] | 252 | void * void_buffer[12]; |
---|
[1134] | 253 | }; |
---|
| 254 | |
---|
| 255 | |
---|
| 256 | class MPI_Comm |
---|
| 257 | { |
---|
| 258 | public: |
---|
| 259 | |
---|
| 260 | #ifdef _intelmpi |
---|
| 261 | int mpi_comm; |
---|
| 262 | #elif _openmpi |
---|
| 263 | void * mpi_comm; |
---|
| 264 | #endif |
---|
| 265 | |
---|
| 266 | bool is_ep; |
---|
| 267 | bool is_intercomm; |
---|
| 268 | |
---|
| 269 | BUFFER *my_buffer; |
---|
| 270 | OMPbarrier *ep_barrier; |
---|
| 271 | RANK_MAP *rank_map; |
---|
| 272 | |
---|
| 273 | EP_Comm ep_comm_ptr; |
---|
| 274 | |
---|
| 275 | MPI_Comm *mem_bridge; |
---|
| 276 | |
---|
[1287] | 277 | |
---|
[1134] | 278 | #ifdef _intelmpi |
---|
| 279 | int mpi_bridge; |
---|
| 280 | #elif _openmpi |
---|
| 281 | void * mpi_bridge; |
---|
| 282 | #endif |
---|
| 283 | |
---|
| 284 | MPI_Comm() |
---|
| 285 | { |
---|
| 286 | is_ep = true; |
---|
| 287 | is_intercomm = false; |
---|
| 288 | my_buffer = NULL; |
---|
| 289 | ep_barrier = NULL; |
---|
| 290 | rank_map = NULL; |
---|
| 291 | ep_comm_ptr = NULL; |
---|
| 292 | mem_bridge = NULL; |
---|
| 293 | mpi_bridge = NULL; |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | #ifdef _intelmpi |
---|
| 297 | MPI_Comm(int comm) |
---|
| 298 | { |
---|
| 299 | is_ep = false; |
---|
| 300 | is_intercomm = false; |
---|
| 301 | my_buffer = NULL; |
---|
| 302 | ep_barrier = NULL; |
---|
| 303 | rank_map = NULL; |
---|
| 304 | ep_comm_ptr = NULL; |
---|
| 305 | mem_bridge = NULL; |
---|
| 306 | mpi_bridge = NULL; |
---|
| 307 | mpi_comm = comm; |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | #elif _openmpi |
---|
| 311 | |
---|
| 312 | MPI_Comm(void* comm) |
---|
| 313 | { |
---|
| 314 | is_ep = false; |
---|
| 315 | is_intercomm = false; |
---|
| 316 | my_buffer = NULL; |
---|
| 317 | ep_barrier = NULL; |
---|
| 318 | rank_map = NULL; |
---|
| 319 | ep_comm_ptr = NULL; |
---|
| 320 | mem_bridge = NULL; |
---|
| 321 | mpi_bridge = NULL; |
---|
| 322 | mpi_comm = comm; |
---|
| 323 | } |
---|
| 324 | #endif |
---|
| 325 | |
---|
| 326 | |
---|
| 327 | bool operator == (MPI_Comm right) |
---|
| 328 | { |
---|
| 329 | bool a = is_ep == right.is_ep; |
---|
| 330 | bool b = is_intercomm == right.is_intercomm; |
---|
| 331 | bool c = mpi_comm == right.mpi_comm; |
---|
| 332 | bool d = is_ep ? ep_comm_ptr == right.ep_comm_ptr : true; |
---|
| 333 | return a&&b&&c&&d; |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | bool operator != (MPI_Comm right) |
---|
| 337 | { |
---|
| 338 | bool a = is_ep != right.is_ep; |
---|
| 339 | bool b = is_intercomm != right.is_intercomm; |
---|
| 340 | bool c = mpi_comm != right.mpi_comm; |
---|
| 341 | bool d = is_ep ? ep_comm_ptr != right.ep_comm_ptr : true; |
---|
| 342 | |
---|
| 343 | return a||b||c||d; |
---|
| 344 | } |
---|
| 345 | |
---|
[1328] | 346 | bool is_null(); |
---|
| 347 | |
---|
[1134] | 348 | }; |
---|
| 349 | |
---|
| 350 | |
---|
| 351 | class MPI_Info |
---|
| 352 | { |
---|
| 353 | public: |
---|
| 354 | |
---|
| 355 | #ifdef _intelmpi |
---|
| 356 | int mpi_info; |
---|
| 357 | #elif _openmpi |
---|
| 358 | void * mpi_info; |
---|
| 359 | #endif |
---|
| 360 | |
---|
| 361 | MPI_Info(){ } |
---|
| 362 | |
---|
| 363 | #ifdef _intelmpi |
---|
| 364 | MPI_Info(int info): mpi_info(info) {} |
---|
| 365 | #elif _openmpi |
---|
| 366 | MPI_Info(void* info): mpi_info(info) {} |
---|
| 367 | #endif |
---|
| 368 | }; |
---|
| 369 | |
---|
| 370 | |
---|
| 371 | class MPI_Request |
---|
| 372 | { |
---|
| 373 | public: |
---|
| 374 | |
---|
| 375 | #ifdef _intelmpi |
---|
| 376 | int mpi_request; |
---|
| 377 | #elif _openmpi |
---|
| 378 | void * mpi_request; |
---|
| 379 | #endif |
---|
| 380 | |
---|
| 381 | int type; //! type of the non-blocking communication. 1: Isend; 2:Irecv; 3:Imrecv; 4:Issend |
---|
| 382 | void* buf; |
---|
| 383 | |
---|
| 384 | int ep_src; |
---|
| 385 | int ep_tag; |
---|
| 386 | #ifdef _intelmpi |
---|
| 387 | int ep_datatype; |
---|
| 388 | #elif _openmpi |
---|
| 389 | void * ep_datatype; |
---|
| 390 | #endif |
---|
| 391 | |
---|
| 392 | MPI_Comm comm; //! EP communicator related to the communication |
---|
| 393 | |
---|
| 394 | MPI_Request() {} |
---|
| 395 | |
---|
| 396 | #ifdef _intelmpi |
---|
| 397 | MPI_Request(int request): mpi_request(request) {} |
---|
| 398 | #elif _openmpi |
---|
| 399 | MPI_Request(void* request): mpi_request(request) {} |
---|
| 400 | #endif |
---|
[1187] | 401 | |
---|
[1196] | 402 | |
---|
| 403 | bool operator == (MPI_Request right) |
---|
| 404 | { |
---|
| 405 | //bool a = mpi_request == right.mpi_request; |
---|
| 406 | bool b = type == right.type; |
---|
| 407 | bool c = buf == right.buf; |
---|
| 408 | bool d = ep_src == right.ep_src; |
---|
| 409 | bool e = ep_tag == right.ep_tag; |
---|
| 410 | bool f = ep_datatype == right.ep_datatype; |
---|
| 411 | return b&&c&&d&&e&&f; |
---|
| 412 | } |
---|
[1134] | 413 | }; |
---|
| 414 | |
---|
| 415 | |
---|
| 416 | class MPI_Aint |
---|
| 417 | { |
---|
| 418 | public: |
---|
| 419 | |
---|
| 420 | unsigned long mpi_aint; |
---|
| 421 | |
---|
| 422 | MPI_Aint() {} |
---|
| 423 | MPI_Aint(int a): mpi_aint(a) {} |
---|
| 424 | }; |
---|
| 425 | |
---|
| 426 | class MPI_Fint |
---|
| 427 | { |
---|
| 428 | public: |
---|
| 429 | |
---|
| 430 | int mpi_fint; |
---|
| 431 | |
---|
| 432 | MPI_Fint() {} |
---|
| 433 | MPI_Fint(int f): mpi_fint(f) {} |
---|
| 434 | |
---|
| 435 | }; |
---|
| 436 | |
---|
| 437 | |
---|
| 438 | static MPI_Comm *passage; |
---|
| 439 | |
---|
| 440 | static int TAG = 40000; |
---|
| 441 | |
---|
| 442 | static std::list<std::pair<std::pair<int, int>, MPI_Comm * > > tag_list; |
---|
| 443 | |
---|
| 444 | static std::map<std::pair<int, int>, MPI_Comm > fc_comm_map; |
---|
| 445 | |
---|
| 446 | static std::map<std::pair<int, int>, MPI_Comm > *fc_comm_map_ptr; |
---|
| 447 | #pragma omp threadprivate(fc_comm_map_ptr) |
---|
| 448 | // <MPI_Fint,thread_num> EP_Comm |
---|
| 449 | |
---|
| 450 | } |
---|
| 451 | |
---|
| 452 | |
---|
| 453 | |
---|
| 454 | #endif // EP_TYPE_HPP_INCLUDED |
---|
[1196] | 455 | |
---|