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