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