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 MPI_UNDEFINED -32766 |
---|
44 | #define MPI_STATUS_IGNORE NULL |
---|
45 | #define MPI_INFO_NULL MPI_Info() |
---|
46 | |
---|
47 | class ep_communicator; |
---|
48 | class ep_intercomm; |
---|
49 | class OMPbarrier; |
---|
50 | typedef ep_communicator* EP_Comm; |
---|
51 | class MPI_Comm; |
---|
52 | |
---|
53 | |
---|
54 | class MPI_Status |
---|
55 | { |
---|
56 | public: |
---|
57 | |
---|
58 | #ifdef _intelmpi |
---|
59 | int ep_datatype; |
---|
60 | #elif _openmpi |
---|
61 | void * ep_datatype; |
---|
62 | #endif |
---|
63 | |
---|
64 | int ep_src; |
---|
65 | int ep_tag; |
---|
66 | |
---|
67 | |
---|
68 | void* mpi_status; |
---|
69 | }; |
---|
70 | |
---|
71 | class MPI_Message |
---|
72 | { |
---|
73 | public: |
---|
74 | |
---|
75 | #ifdef _intelmpi |
---|
76 | int mpi_message; |
---|
77 | #elif _openmpi |
---|
78 | void * mpi_message; |
---|
79 | #endif |
---|
80 | |
---|
81 | int ep_src; |
---|
82 | int ep_tag; |
---|
83 | |
---|
84 | void* mpi_status; |
---|
85 | |
---|
86 | MPI_Message() |
---|
87 | { |
---|
88 | mpi_message = 0; |
---|
89 | mpi_status = 0; |
---|
90 | } |
---|
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 | bool operator == (ep_intercomm right) |
---|
177 | { |
---|
178 | bool a = intercomm_rank_map == right.intercomm_rank_map; |
---|
179 | bool b = local_rank_map == right.local_rank_map; |
---|
180 | bool c = remote_rank_map == right.remote_rank_map; |
---|
181 | bool d = mpi_inter_comm == right.mpi_inter_comm; |
---|
182 | bool e = size_rank_info == right.size_rank_info; |
---|
183 | bool f = intercomm_tag == right.intercomm_tag; |
---|
184 | return a&&b&&c&&d&&e&&f; |
---|
185 | } |
---|
186 | |
---|
187 | bool operator != (ep_intercomm right) |
---|
188 | { |
---|
189 | bool a = intercomm_rank_map != right.intercomm_rank_map; |
---|
190 | bool b = local_rank_map != right.local_rank_map; |
---|
191 | bool c = remote_rank_map != right.remote_rank_map; |
---|
192 | bool d = mpi_inter_comm != right.mpi_inter_comm; |
---|
193 | bool e = size_rank_info != right.size_rank_info; |
---|
194 | bool f = intercomm_tag != right.intercomm_tag; |
---|
195 | return a||b||c||d||e||f; |
---|
196 | } |
---|
197 | }; |
---|
198 | |
---|
199 | |
---|
200 | class ep_communicator |
---|
201 | { |
---|
202 | public: |
---|
203 | |
---|
204 | SIZE_RANK_INFO size_rank_info[3]; // 0: ep_rank, ep_size |
---|
205 | // 1: ep_rank_loc, num_ep |
---|
206 | // 2: mpi_rank, mpi_size |
---|
207 | |
---|
208 | |
---|
209 | MPI_Comm *comm_list; |
---|
210 | |
---|
211 | Message_list *message_queue; |
---|
212 | |
---|
213 | |
---|
214 | int comm_label; |
---|
215 | |
---|
216 | ep_intercomm *intercomm; |
---|
217 | |
---|
218 | ep_communicator() |
---|
219 | { |
---|
220 | comm_list = NULL; |
---|
221 | message_queue = NULL; |
---|
222 | intercomm = NULL; |
---|
223 | } |
---|
224 | |
---|
225 | bool operator == (ep_communicator right) |
---|
226 | { |
---|
227 | bool a = size_rank_info == right.size_rank_info; |
---|
228 | bool b = comm_label == right.comm_label; |
---|
229 | bool c = intercomm == right.intercomm; |
---|
230 | return a&&b&&c; |
---|
231 | } |
---|
232 | |
---|
233 | bool operator != (ep_communicator right) |
---|
234 | { |
---|
235 | bool a = size_rank_info != right.size_rank_info; |
---|
236 | bool b = comm_label != right.comm_label; |
---|
237 | bool c = intercomm != right.intercomm; |
---|
238 | return a||b||c; |
---|
239 | } |
---|
240 | }; |
---|
241 | |
---|
242 | |
---|
243 | struct BUFFER |
---|
244 | { |
---|
245 | double *buf_double; |
---|
246 | float *buf_float; |
---|
247 | int *buf_int; |
---|
248 | long *buf_long; |
---|
249 | unsigned long *buf_ulong; |
---|
250 | char *buf_char; |
---|
251 | }; |
---|
252 | |
---|
253 | |
---|
254 | class MPI_Comm |
---|
255 | { |
---|
256 | public: |
---|
257 | |
---|
258 | #ifdef _intelmpi |
---|
259 | int mpi_comm; |
---|
260 | #elif _openmpi |
---|
261 | void * mpi_comm; |
---|
262 | #endif |
---|
263 | |
---|
264 | bool is_ep; |
---|
265 | bool is_intercomm; |
---|
266 | |
---|
267 | BUFFER *my_buffer; |
---|
268 | OMPbarrier *ep_barrier; |
---|
269 | RANK_MAP *rank_map; |
---|
270 | |
---|
271 | EP_Comm ep_comm_ptr; |
---|
272 | |
---|
273 | MPI_Comm *mem_bridge; |
---|
274 | |
---|
275 | #ifdef _intelmpi |
---|
276 | int mpi_bridge; |
---|
277 | #elif _openmpi |
---|
278 | void * mpi_bridge; |
---|
279 | #endif |
---|
280 | |
---|
281 | MPI_Comm() |
---|
282 | { |
---|
283 | is_ep = false; |
---|
284 | is_intercomm = false; |
---|
285 | my_buffer = NULL; |
---|
286 | ep_barrier = NULL; |
---|
287 | rank_map = NULL; |
---|
288 | ep_comm_ptr = NULL; |
---|
289 | mem_bridge = NULL; |
---|
290 | mpi_bridge = NULL; |
---|
291 | mpi_comm = 0; |
---|
292 | } |
---|
293 | |
---|
294 | MPI_Comm(int comm) |
---|
295 | { |
---|
296 | is_ep = false; |
---|
297 | is_intercomm = false; |
---|
298 | my_buffer = NULL; |
---|
299 | ep_barrier = NULL; |
---|
300 | rank_map = NULL; |
---|
301 | ep_comm_ptr = NULL; |
---|
302 | mem_bridge = NULL; |
---|
303 | mpi_bridge = NULL; |
---|
304 | mpi_comm = comm; |
---|
305 | } |
---|
306 | |
---|
307 | //MPI_Comm(const MPI_Comm &comm); |
---|
308 | |
---|
309 | |
---|
310 | bool operator == (MPI_Comm right) |
---|
311 | { |
---|
312 | bool a = is_ep == right.is_ep; |
---|
313 | bool b = is_intercomm == right.is_intercomm; |
---|
314 | bool c = mpi_comm == right.mpi_comm; |
---|
315 | bool d = is_ep ? ep_comm_ptr == right.ep_comm_ptr : true; |
---|
316 | return a&&b&&c&&d; |
---|
317 | } |
---|
318 | |
---|
319 | bool operator != (MPI_Comm right) |
---|
320 | { |
---|
321 | bool a = is_ep != right.is_ep; |
---|
322 | bool b = is_intercomm != right.is_intercomm; |
---|
323 | bool c = mpi_comm != right.mpi_comm; |
---|
324 | bool d = is_ep ? ep_comm_ptr != right.ep_comm_ptr : true; |
---|
325 | return a||b||c||d; |
---|
326 | } |
---|
327 | }; |
---|
328 | |
---|
329 | |
---|
330 | class MPI_Info |
---|
331 | { |
---|
332 | public: |
---|
333 | |
---|
334 | #ifdef _intelmpi |
---|
335 | int mpi_info; |
---|
336 | #elif _openmpi |
---|
337 | void * mpi_info; |
---|
338 | #endif |
---|
339 | |
---|
340 | MPI_Info() |
---|
341 | { |
---|
342 | mpi_info = 0; |
---|
343 | } |
---|
344 | }; |
---|
345 | |
---|
346 | |
---|
347 | class MPI_Request |
---|
348 | { |
---|
349 | public: |
---|
350 | |
---|
351 | #ifdef _intelmpi |
---|
352 | int mpi_request; |
---|
353 | #elif _openmpi |
---|
354 | void * mpi_request; |
---|
355 | #endif |
---|
356 | |
---|
357 | int type; //! type of the non-blocking communication. 1: Isend; 2:Irecv; 3:Imrecv; 4:Issend |
---|
358 | void* buf; |
---|
359 | |
---|
360 | int ep_src; |
---|
361 | int ep_tag; |
---|
362 | #ifdef _intelmpi |
---|
363 | int ep_datatype; |
---|
364 | #elif _openmpi |
---|
365 | void * ep_datatype; |
---|
366 | #endif |
---|
367 | |
---|
368 | MPI_Comm comm; //! EP communicator related to the communication |
---|
369 | |
---|
370 | MPI_Request() |
---|
371 | { |
---|
372 | mpi_request = 0; |
---|
373 | } |
---|
374 | }; |
---|
375 | |
---|
376 | class MPI_Fint |
---|
377 | { |
---|
378 | public: |
---|
379 | |
---|
380 | int mpi_fint; |
---|
381 | }; |
---|
382 | |
---|
383 | class MPI_Aint |
---|
384 | { |
---|
385 | public: |
---|
386 | |
---|
387 | unsigned long mpi_aint; |
---|
388 | }; |
---|
389 | |
---|
390 | |
---|
391 | static MPI_Comm *passage; |
---|
392 | |
---|
393 | static int TAG = 40000; |
---|
394 | |
---|
395 | static std::list<std::pair<std::pair<int, int>, MPI_Comm * > > tag_list; |
---|
396 | |
---|
397 | static std::map<std::pair<int, int>, MPI_Comm > fc_comm_map; |
---|
398 | // <MPI_Fint,thread_num> EP_Comm |
---|
399 | |
---|
400 | } |
---|
401 | |
---|
402 | |
---|
403 | |
---|
404 | #endif // EP_TYPE_HPP_INCLUDED |
---|