source: XIOS/dev/branch_openmp/extern/src_ep_dev/ep_type.hpp @ 1354

Last change on this file since 1354 was 1354, checked in by yushan, 6 years ago

omp dev : unify MPI_Comm type

File size: 9.4 KB
Line 
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//#define NUM_THREADS 12
29
30typedef std::pair< int, int > SIZE_RANK_INFO; // < rank, size>
31
32typedef std::vector< std::pair<int, int> > RANK_MAP;  // at(ep_rank) = <ep_rank_local, mpi_rank>
33
34typedef std::vector<std::pair< std::pair<int, int>, std::pair<int, int> > > INTERCOMM_RANK_MAP;
35
36
37typedef struct
38{
39  int first;
40  int second;
41  int third;
42} Triple_int;
43
44namespace 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    void * mpi_inter_comm;
162
163    RANK_MAP *intercomm_rank_map;
164    RANK_MAP *local_rank_map;
165    RANK_MAP *remote_rank_map;
166
167
168    SIZE_RANK_INFO size_rank_info[3];
169
170
171    MPI_Comm *local_comm;
172    int intercomm_tag;
173
174    ep_intercomm()
175    {
176      intercomm_rank_map = NULL;
177      local_rank_map = NULL;
178      remote_rank_map = NULL;
179    }
180
181
182    bool operator == (ep_intercomm right)
183    {
184      bool a = intercomm_rank_map == right.intercomm_rank_map;
185      bool b = local_rank_map == right.local_rank_map;
186      bool c = remote_rank_map == right.remote_rank_map;
187      bool d = mpi_inter_comm == right.mpi_inter_comm;
188      bool e = size_rank_info == right.size_rank_info;
189      bool f = intercomm_tag == right.intercomm_tag;
190      return a&&b&&c&&d&&e&&f;
191    }
192
193    bool operator != (ep_intercomm right)
194    {
195      bool a = intercomm_rank_map != right.intercomm_rank_map;
196      bool b = local_rank_map != right.local_rank_map;
197      bool c = remote_rank_map != right.remote_rank_map;
198      bool d = mpi_inter_comm != right.mpi_inter_comm;
199      bool e = size_rank_info != right.size_rank_info;
200      bool f = intercomm_tag != right.intercomm_tag;
201      return a||b||c||d||e||f;
202    }
203
204
205  };
206
207
208  class ep_communicator
209  {
210    public:
211
212    SIZE_RANK_INFO size_rank_info[3]; // 0: ep_rank,     ep_size
213                                      // 1: ep_rank_loc, num_ep
214                                      // 2: mpi_rank,    mpi_size
215
216
217    MPI_Comm *comm_list;
218
219    Message_list *message_queue;
220
221
222    int comm_label;
223
224    ep_intercomm *intercomm;
225
226    ep_communicator()
227    {
228      comm_list = NULL;
229      message_queue = NULL;
230      intercomm = NULL;
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    bool operator != (ep_communicator right)
242    {
243      bool a = size_rank_info != right.size_rank_info;
244      bool b = comm_label != right.comm_label;
245      bool c = intercomm != right.intercomm;
246      return a||b||c;
247    }
248
249  };
250
251
252  struct BUFFER
253  {
254    void * void_buffer[12];
255  };
256
257
258  class MPI_Comm
259  {
260    public:
261
262    // #ifdef _intelmpi
263    // int mpi_comm;
264    // #elif _openmpi
265    // void * mpi_comm;
266    // #endif
267
268    void * mpi_comm;
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    void * mpi_bridge;
289
290    MPI_Comm()
291    {
292      is_ep = true;
293      is_intercomm = false;
294      my_buffer = NULL;
295      ep_barrier = NULL;
296      rank_map = NULL;
297      ep_comm_ptr = NULL;
298      mem_bridge = NULL;
299      mpi_bridge = NULL;
300    }
301
302    // #ifdef _intelmpi
303    // MPI_Comm(int comm)
304    // {
305    //   is_ep = false;
306    //   is_intercomm = false;
307    //   my_buffer = NULL;
308    //   ep_barrier = NULL;
309    //   rank_map = NULL;
310    //   ep_comm_ptr = NULL;
311    //   mem_bridge = NULL;
312    //   mpi_bridge = NULL;
313    //   mpi_comm = comm;
314    // }
315
316    // #elif _openmpi
317
318    // MPI_Comm(void* comm)
319    // {
320    //   is_ep = false;
321    //   is_intercomm = false;
322    //   my_buffer = NULL;
323    //   ep_barrier = NULL;
324    //   rank_map = NULL;
325    //   ep_comm_ptr = NULL;
326    //   mem_bridge = NULL;
327    //   mpi_bridge = NULL;
328    //   mpi_comm = comm;
329    // }
330    // #endif
331   
332    MPI_Comm(void* comm)
333    {
334      is_ep = false;
335      is_intercomm = false;
336      my_buffer = NULL;
337      ep_barrier = NULL;
338      rank_map = NULL;
339      ep_comm_ptr = NULL;
340      mem_bridge = NULL;
341      mpi_bridge = NULL;
342      mpi_comm = comm;
343    }
344
345    bool operator == (MPI_Comm right)
346    {
347      bool a = is_ep == right.is_ep;
348      bool b = is_intercomm == right.is_intercomm;
349      bool c = mpi_comm == right.mpi_comm;
350      bool d = is_ep ? ep_comm_ptr == right.ep_comm_ptr : true;
351      return a&&b&&c&&d;
352    }
353
354    bool operator != (MPI_Comm right)
355    {
356      bool a = is_ep != right.is_ep;
357      bool b = is_intercomm != right.is_intercomm;
358      bool c = mpi_comm != right.mpi_comm;
359      bool d = is_ep ? ep_comm_ptr != right.ep_comm_ptr : true;
360
361      return a||b||c||d;
362    }
363
364    bool is_null();
365
366  };
367
368
369  class MPI_Info
370  {
371    public:
372
373      #ifdef _intelmpi
374      int mpi_info;
375      #elif _openmpi
376      void * mpi_info;
377      #endif
378
379      MPI_Info(){ }
380     
381      #ifdef _intelmpi
382      MPI_Info(int info): mpi_info(info) {}
383      #elif _openmpi
384      MPI_Info(void* info): mpi_info(info) {}
385      #endif
386  };
387
388
389  class MPI_Request
390  {
391    public:
392
393      #ifdef _intelmpi
394      int mpi_request;
395      #elif _openmpi
396      void * mpi_request;
397      #endif
398
399      int type; //! type of the non-blocking communication. 1: Isend; 2:Irecv; 3:Imrecv; 4:Issend
400      void* buf;
401
402      int ep_src;
403      int ep_tag;
404      #ifdef _intelmpi
405      int ep_datatype;
406      #elif _openmpi
407      void * ep_datatype;
408      #endif
409
410      MPI_Comm comm;    //! EP communicator related to the communication
411
412      MPI_Request() {}
413
414      #ifdef _intelmpi
415      MPI_Request(int request): mpi_request(request) {}
416      #elif _openmpi
417      MPI_Request(void* request): mpi_request(request) {}
418      #endif
419
420
421    bool operator == (MPI_Request right)
422    {
423      //bool a = mpi_request == right.mpi_request;
424      bool b = type == right.type;
425      bool c = buf == right.buf;
426      bool d = ep_src == right.ep_src;
427      bool e = ep_tag == right.ep_tag;
428      bool f = ep_datatype == right.ep_datatype;
429      return b&&c&&d&&e&&f;
430    }
431  };
432
433 
434  class MPI_Aint
435  {
436    public:
437
438    unsigned long mpi_aint;
439
440    MPI_Aint() {}
441    MPI_Aint(int a): mpi_aint(a) {}
442  };
443
444  class MPI_Fint
445  {
446    public:
447
448    int mpi_fint;
449
450    MPI_Fint() {}
451    MPI_Fint(int f): mpi_fint(f) {}
452   
453  };
454
455
456  static MPI_Comm *passage;
457
458  static int TAG = 40000;
459
460  static std::list<std::pair<std::pair<int, int>, MPI_Comm * > > tag_list;
461
462  static std::map<std::pair<int, int>, MPI_Comm >  fc_comm_map;
463
464  static std::map<std::pair<int, int>, MPI_Comm >  *fc_comm_map_ptr;
465  #pragma omp threadprivate(fc_comm_map_ptr)
466            //    <MPI_Fint,thread_num>   EP_Comm
467
468}
469
470
471
472#endif // EP_TYPE_HPP_INCLUDED
473
Note: See TracBrowser for help on using the repository browser.