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

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

test_remap_omp tested on ADA except two fields

File size: 9.0 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
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
27typedef std::pair< int, int > SIZE_RANK_INFO; // < rank, size>
28
29typedef std::vector< std::pair<int, int> > RANK_MAP;  // at(ep_rank) = <ep_rank_local, mpi_rank>
30
31typedef std::vector<std::pair< std::pair<int, int>, std::pair<int, int> > > INTERCOMM_RANK_MAP;
32
33
34typedef struct
35{
36  int first;
37  int second;
38  int third;
39} Triple_int;
40
41namespace 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
177    bool operator == (ep_intercomm right)
178    {
179      bool a = intercomm_rank_map == right.intercomm_rank_map;
180      bool b = local_rank_map == right.local_rank_map;
181      bool c = remote_rank_map == right.remote_rank_map;
182      bool d = mpi_inter_comm == right.mpi_inter_comm;
183      bool e = size_rank_info == right.size_rank_info;
184      bool f = intercomm_tag == right.intercomm_tag;
185      return a&&b&&c&&d&&e&&f;
186    }
187
188    bool operator != (ep_intercomm right)
189    {
190      bool a = intercomm_rank_map != right.intercomm_rank_map;
191      bool b = local_rank_map != right.local_rank_map;
192      bool c = remote_rank_map != right.remote_rank_map;
193      bool d = mpi_inter_comm != right.mpi_inter_comm;
194      bool e = size_rank_info != right.size_rank_info;
195      bool f = intercomm_tag != right.intercomm_tag;
196      return a||b||c||d||e||f;
197    }
198
199
200  };
201
202
203  class ep_communicator
204  {
205    public:
206
207    SIZE_RANK_INFO size_rank_info[3]; // 0: ep_rank,     ep_size
208                                      // 1: ep_rank_loc, num_ep
209                                      // 2: mpi_rank,    mpi_size
210
211
212    MPI_Comm *comm_list;
213
214    Message_list *message_queue;
215
216
217    int comm_label;
218
219    ep_intercomm *intercomm;
220
221    ep_communicator()
222    {
223      comm_list = NULL;
224      message_queue = NULL;
225      intercomm = NULL;
226    }
227
228    bool operator == (ep_communicator right)
229    {
230      bool a = size_rank_info == right.size_rank_info;
231      bool b = comm_label == right.comm_label;
232      bool c = intercomm == right.intercomm;
233      return a&&b&&c;
234    }
235
236    bool operator != (ep_communicator right)
237    {
238      bool a = size_rank_info != right.size_rank_info;
239      bool b = comm_label != right.comm_label;
240      bool c = intercomm != right.intercomm;
241      return a||b||c;
242    }
243
244  };
245
246
247  struct BUFFER
248  {
249    double *buf_double;
250    float  *buf_float;
251    int    *buf_int;
252    long    *buf_long;
253    unsigned long    *buf_ulong;
254    char    *buf_char;
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    bool is_ep;
269    bool is_intercomm;
270
271    BUFFER     *my_buffer;
272    OMPbarrier *ep_barrier;
273    RANK_MAP   *rank_map;
274
275    EP_Comm ep_comm_ptr;
276
277    MPI_Comm *mem_bridge;
278
279    #ifdef _intelmpi
280    int mpi_bridge;
281    #elif _openmpi
282    void * mpi_bridge;
283    #endif
284
285    MPI_Comm()
286    {
287      is_ep = true;
288      is_intercomm = false;
289      my_buffer = NULL;
290      ep_barrier = NULL;
291      rank_map = NULL;
292      ep_comm_ptr = NULL;
293      mem_bridge = NULL;
294      mpi_bridge = NULL;
295    }
296
297    #ifdef _intelmpi
298    MPI_Comm(int comm)
299    {
300      is_ep = false;
301      is_intercomm = false;
302      my_buffer = NULL;
303      ep_barrier = NULL;
304      rank_map = NULL;
305      ep_comm_ptr = NULL;
306      mem_bridge = NULL;
307      mpi_bridge = NULL;
308      mpi_comm = comm;
309    }
310
311    #elif _openmpi
312
313    MPI_Comm(void* comm)
314    {
315      is_ep = false;
316      is_intercomm = false;
317      my_buffer = NULL;
318      ep_barrier = NULL;
319      rank_map = NULL;
320      ep_comm_ptr = NULL;
321      mem_bridge = NULL;
322      mpi_bridge = NULL;
323      mpi_comm = comm;
324    }
325    #endif
326
327
328    bool operator == (MPI_Comm right)
329    {
330      bool a = is_ep == right.is_ep;
331      bool b = is_intercomm == right.is_intercomm;
332      bool c = mpi_comm == right.mpi_comm;
333      bool d = is_ep ? ep_comm_ptr == right.ep_comm_ptr : true;
334      return a&&b&&c&&d;
335    }
336
337    bool operator != (MPI_Comm right)
338    {
339      bool a = is_ep != right.is_ep;
340      bool b = is_intercomm != right.is_intercomm;
341      bool c = mpi_comm != right.mpi_comm;
342      bool d = is_ep ? ep_comm_ptr != right.ep_comm_ptr : true;
343
344      return a||b||c||d;
345    }
346
347  };
348
349
350  class MPI_Info
351  {
352    public:
353
354      #ifdef _intelmpi
355      int mpi_info;
356      #elif _openmpi
357      void * mpi_info;
358      #endif
359
360      MPI_Info(){ }
361     
362      #ifdef _intelmpi
363      MPI_Info(int info): mpi_info(info) {}
364      #elif _openmpi
365      MPI_Info(void* info): mpi_info(info) {}
366      #endif
367  };
368
369
370  class MPI_Request
371  {
372    public:
373
374      #ifdef _intelmpi
375      int mpi_request;
376      #elif _openmpi
377      void * mpi_request;
378      #endif
379
380      int type; //! type of the non-blocking communication. 1: Isend; 2:Irecv; 3:Imrecv; 4:Issend
381      void* buf;
382
383      int ep_src;
384      int ep_tag;
385      #ifdef _intelmpi
386      int ep_datatype;
387      #elif _openmpi
388      void * ep_datatype;
389      #endif
390
391      MPI_Comm comm;    //! EP communicator related to the communication
392
393      MPI_Request() {}
394
395      #ifdef _intelmpi
396      MPI_Request(int request): mpi_request(request) {}
397      #elif _openmpi
398      MPI_Request(void* request): mpi_request(request) {}
399      #endif
400
401
402    bool operator == (MPI_Request right)
403    {
404      //bool a = mpi_request == right.mpi_request;
405      bool b = type == right.type;
406      bool c = buf == right.buf;
407      bool d = ep_src == right.ep_src;
408      bool e = ep_tag == right.ep_tag;
409      bool f = ep_datatype == right.ep_datatype;
410      return b&&c&&d&&e&&f;
411    }
412  };
413
414 
415  class MPI_Aint
416  {
417    public:
418
419    unsigned long mpi_aint;
420
421    MPI_Aint() {}
422    MPI_Aint(int a): mpi_aint(a) {}
423  };
424
425  class MPI_Fint
426  {
427    public:
428
429    int mpi_fint;
430
431    MPI_Fint() {}
432    MPI_Fint(int f): mpi_fint(f) {}
433   
434  };
435
436
437  static MPI_Comm *passage;
438
439  static int TAG = 40000;
440
441  static std::list<std::pair<std::pair<int, int>, MPI_Comm * > > tag_list;
442
443  static std::map<std::pair<int, int>, MPI_Comm >  fc_comm_map;
444
445  static std::map<std::pair<int, int>, MPI_Comm >  *fc_comm_map_ptr;
446  #pragma omp threadprivate(fc_comm_map_ptr)
447            //    <MPI_Fint,thread_num>   EP_Comm
448
449}
450
451
452
453#endif // EP_TYPE_HPP_INCLUDED
454
Note: See TracBrowser for help on using the repository browser.