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

Last change on this file since 1287 was 1287, checked in by yushan, 7 years ago

EP updated

File size: 8.9 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
28typedef std::pair< int, int > SIZE_RANK_INFO; // < rank, size>
29
30typedef std::vector< std::pair<int, int> > RANK_MAP;  // at(ep_rank) = <ep_rank_local, mpi_rank>
31
32typedef std::vector<std::pair< std::pair<int, int>, std::pair<int, int> > > INTERCOMM_RANK_MAP;
33
34
35typedef struct
36{
37  int first;
38  int second;
39  int third;
40} Triple_int;
41
42namespace 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    void * void_buffer[12];
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
276    #ifdef _intelmpi
277    int mpi_bridge;
278    #elif _openmpi
279    void * mpi_bridge;
280    #endif
281
282    MPI_Comm()
283    {
284      is_ep = true;
285      is_intercomm = false;
286      my_buffer = NULL;
287      ep_barrier = NULL;
288      rank_map = NULL;
289      ep_comm_ptr = NULL;
290      mem_bridge = NULL;
291      mpi_bridge = NULL;
292    }
293
294    #ifdef _intelmpi
295    MPI_Comm(int comm)
296    {
297      is_ep = false;
298      is_intercomm = false;
299      my_buffer = NULL;
300      ep_barrier = NULL;
301      rank_map = NULL;
302      ep_comm_ptr = NULL;
303      mem_bridge = NULL;
304      mpi_bridge = NULL;
305      mpi_comm = comm;
306    }
307
308    #elif _openmpi
309
310    MPI_Comm(void* comm)
311    {
312      is_ep = false;
313      is_intercomm = false;
314      my_buffer = NULL;
315      ep_barrier = NULL;
316      rank_map = NULL;
317      ep_comm_ptr = NULL;
318      mem_bridge = NULL;
319      mpi_bridge = NULL;
320      mpi_comm = comm;
321    }
322    #endif
323
324
325    bool operator == (MPI_Comm right)
326    {
327      bool a = is_ep == right.is_ep;
328      bool b = is_intercomm == right.is_intercomm;
329      bool c = mpi_comm == right.mpi_comm;
330      bool d = is_ep ? ep_comm_ptr == right.ep_comm_ptr : true;
331      return a&&b&&c&&d;
332    }
333
334    bool operator != (MPI_Comm right)
335    {
336      bool a = is_ep != right.is_ep;
337      bool b = is_intercomm != right.is_intercomm;
338      bool c = mpi_comm != right.mpi_comm;
339      bool d = is_ep ? ep_comm_ptr != right.ep_comm_ptr : true;
340
341      return a||b||c||d;
342    }
343
344  };
345
346
347  class MPI_Info
348  {
349    public:
350
351      #ifdef _intelmpi
352      int mpi_info;
353      #elif _openmpi
354      void * mpi_info;
355      #endif
356
357      MPI_Info(){ }
358     
359      #ifdef _intelmpi
360      MPI_Info(int info): mpi_info(info) {}
361      #elif _openmpi
362      MPI_Info(void* info): mpi_info(info) {}
363      #endif
364  };
365
366
367  class MPI_Request
368  {
369    public:
370
371      #ifdef _intelmpi
372      int mpi_request;
373      #elif _openmpi
374      void * mpi_request;
375      #endif
376
377      int type; //! type of the non-blocking communication. 1: Isend; 2:Irecv; 3:Imrecv; 4:Issend
378      void* buf;
379
380      int ep_src;
381      int ep_tag;
382      #ifdef _intelmpi
383      int ep_datatype;
384      #elif _openmpi
385      void * ep_datatype;
386      #endif
387
388      MPI_Comm comm;    //! EP communicator related to the communication
389
390      MPI_Request() {}
391
392      #ifdef _intelmpi
393      MPI_Request(int request): mpi_request(request) {}
394      #elif _openmpi
395      MPI_Request(void* request): mpi_request(request) {}
396      #endif
397
398
399    bool operator == (MPI_Request right)
400    {
401      //bool a = mpi_request == right.mpi_request;
402      bool b = type == right.type;
403      bool c = buf == right.buf;
404      bool d = ep_src == right.ep_src;
405      bool e = ep_tag == right.ep_tag;
406      bool f = ep_datatype == right.ep_datatype;
407      return b&&c&&d&&e&&f;
408    }
409  };
410
411 
412  class MPI_Aint
413  {
414    public:
415
416    unsigned long mpi_aint;
417
418    MPI_Aint() {}
419    MPI_Aint(int a): mpi_aint(a) {}
420  };
421
422  class MPI_Fint
423  {
424    public:
425
426    int mpi_fint;
427
428    MPI_Fint() {}
429    MPI_Fint(int f): mpi_fint(f) {}
430   
431  };
432
433
434  static MPI_Comm *passage;
435
436  static int TAG = 40000;
437
438  static std::list<std::pair<std::pair<int, int>, MPI_Comm * > > tag_list;
439
440  static std::map<std::pair<int, int>, MPI_Comm >  fc_comm_map;
441
442  static std::map<std::pair<int, int>, MPI_Comm >  *fc_comm_map_ptr;
443  #pragma omp threadprivate(fc_comm_map_ptr)
444            //    <MPI_Fint,thread_num>   EP_Comm
445
446}
447
448
449
450#endif // EP_TYPE_HPP_INCLUDED
451
Note: See TracBrowser for help on using the repository browser.