source: XIOS/dev/branch_openmp/extern/src_ep_dev/ep_lib.cpp @ 1520

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

save dev. TO DO : test with xios

File size: 6.3 KB
Line 
1#include "ep_lib.hpp"
2#include <mpi.h>
3#include "ep_declaration.hpp"
4#include <iostream>
5#include <fstream>
6#include "ep_mpi.hpp"
7
8using namespace std;
9
10std::list< ep_lib::MPI_Request* > * EP_PendingRequests = 0;
11#pragma omp threadprivate(EP_PendingRequests)
12
13
14
15namespace ep_lib
16{ 
17
18  int tag_combine(int real_tag, int src, int dest)
19  {
20    int a = real_tag << 16;
21    int b = src << 8;
22    int c = dest;
23
24    return a+b+c;
25  }
26
27  int get_ep_rank(MPI_Comm comm, int ep_rank_loc, int mpi_rank)
28  {
29    if(comm->is_intercomm)
30    {
31      for(std::map<int, std::pair< int, std::pair<int, int> > >::iterator it = comm->ep_comm_ptr->intercomm->intercomm_rank_map->begin(); it != comm->ep_comm_ptr->intercomm->intercomm_rank_map->end(); it++)
32      {
33        if(   ( it->second.first  == ep_rank_loc )
34           && ( it->second.second.first == mpi_rank ) )
35        {
36          return it->first;
37        }
38      }
39      printf("rank not find for EP_intercomm\n");
40      int err;
41      return MPI_Abort(comm, err);
42    }
43   
44    for(std::map<int, std::pair<int, int> >::iterator it = comm->ep_rank_map->begin(); it != comm->ep_rank_map->end(); it++)
45    {
46      if(   ( it->second.first  == ep_rank_loc )
47         && ( it->second.second == mpi_rank ) )
48      {
49        return it->first;
50      }
51    }
52    printf("rank not find for EP_intracomm\n");
53    int err;
54    return MPI_Abort(comm, err);
55  }
56 
57
58  int MPI_Get_count(const MPI_Status *status, MPI_Datatype datatype, int *count)
59  {
60    return ::MPI_Get_count(to_mpi_status_ptr(*status), to_mpi_type(datatype), count);
61  }
62
63  double MPI_Wtime()
64  {
65    return ::MPI_Wtime();
66
67  }
68
69  void check_sum_send(const void *buf, int count, MPI_Datatype datatype, int dest, int tag, MPI_Comm comm, int type)
70  {
71
72    int src_rank;
73    int int_count;
74    ::MPI_Aint datasize, intsize, charsize, lb;
75   
76    ::MPI_Type_get_extent(*(static_cast< ::MPI_Datatype*>(datatype)), &lb, &datasize);
77    ::MPI_Type_get_extent(*(static_cast< ::MPI_Datatype*>(MPI_CHAR)), &lb, &intsize);
78
79    int_count = count * datasize / intsize ;
80
81    char *buffer = static_cast< char* >(const_cast< void*> (buf));
82   
83    unsigned long sum = 0;
84    for(int i = 0; i<int_count; i++)
85      sum += *(buffer+i); 
86
87
88    MPI_Comm_rank(comm, &src_rank);
89   
90    ofstream myfile;
91    myfile.open ("send_log.txt", ios::app);
92    if (myfile.is_open())
93    {
94      myfile << "type = " << type << " src = "<< src_rank<< " dest = "<< dest <<" tag = "<< tag << "  count = "<< count << " sum = "<< sum << "\n";
95      myfile.close(); 
96    }
97    else printf("Unable to open file\n");
98
99  }
100
101
102  void check_sum_recv(void *buf, int count, MPI_Datatype datatype, int src, int tag, MPI_Comm comm, int type)
103  {
104
105    int dest_rank;
106    int int_count;
107    ::MPI_Aint datasize, intsize, charsize, lb;
108   
109    ::MPI_Type_get_extent(*(static_cast< ::MPI_Datatype*>(datatype)), &lb, &datasize);
110    ::MPI_Type_get_extent(*(static_cast< ::MPI_Datatype*>(MPI_CHAR)), &lb, &intsize);
111
112    int_count = count * datasize / intsize ;
113
114    char *buffer = static_cast< char* >(buf);
115   
116    unsigned long sum = 0;
117    for(int i = 0; i<int_count; i++)
118      sum += *(buffer+i); 
119
120
121    MPI_Comm_rank(comm, &dest_rank);
122   
123    ofstream myfile;
124    myfile.open ("recv_log.txt", ios::app);
125    if (myfile.is_open())
126    {
127      myfile << "type = " << type << " src = "<< src << " dest = "<< dest_rank <<" tag = "<< tag << "  count = "<< count << " sum = "<< sum << "\n";
128      myfile.close(); 
129    }
130    else printf("Unable to open file\n");
131
132
133  }
134
135  int test_sendrecv(MPI_Comm comm)
136  {
137    int myRank;
138    MPI_Comm_rank(comm, &myRank);
139    bool amClient = false;
140    bool amServer = false;
141    if(myRank<=3) amClient = true;
142    else amServer = true;
143
144    if(amServer)
145    {
146      int send_buf[4];
147      MPI_Request send_request[8];
148      MPI_Status send_status[8];
149
150     
151     
152      for(int j=0; j<4; j++)  // 4 buffers
153      {
154        for(int i=0; i<2; i++)
155        {
156          send_buf[j] = (myRank+1)*100 + j;
157          MPI_Isend(&send_buf[j], 1, MPI_INT, i*2, 9999, comm, &send_request[i*4+j]);
158        }
159      }
160     
161
162      MPI_Waitall(8, send_request, send_status);
163    }
164
165
166    if(amClient&&myRank%2==0) // Clients leaders
167    {
168      int recv_buf[8];
169      MPI_Request recv_request[8];
170      MPI_Status recv_status[8];
171
172      for(int i=0; i<2; i++)  // 2 servers
173      {
174        for(int j=0; j<4; j++)
175        {
176          MPI_Irecv(&recv_buf[i*4+j], 1, MPI_INT, i+4, 9999, comm, &recv_request[i*4+j]);
177        }
178      }
179
180      MPI_Waitall(8, recv_request, recv_status);
181      printf("============ client %d, recv_buf = %d, %d, %d, %d, %d, %d, %d, %d ================\n", 
182              myRank, recv_buf[0], recv_buf[1], recv_buf[2], recv_buf[3], recv_buf[4], recv_buf[5], recv_buf[6], recv_buf[7]);
183    }
184
185    MPI_Barrier(comm);
186
187  }
188
189  bool valid_type(MPI_Datatype datatype)
190  {
191    if(   datatype == MPI_INT
192       || datatype == MPI_FLOAT
193       || datatype == MPI_DOUBLE
194       || datatype == MPI_CHAR
195       || datatype == MPI_LONG
196       || datatype == MPI_UNSIGNED_LONG
197       || datatype == MPI_UINT64_T)
198    {
199      return true;
200    }
201
202    return false;
203  }
204
205
206  bool valid_op(MPI_Op op)
207  {
208    if(   op == MPI_MAX
209       || op == MPI_MIN
210       || op == MPI_SUM
211       || op == MPI_LOR)
212    {
213      return true;
214    }
215
216    return false;
217  }
218
219
220}
221
222
223MPI_Datatype to_mpi_type(ep_lib::MPI_Datatype type)
224{
225  return *static_cast< MPI_Datatype* >(type);
226}
227
228MPI_Op to_mpi_op(ep_lib::MPI_Op op)
229{
230  return *static_cast< MPI_Op* >(op);
231}
232
233MPI_Comm to_mpi_comm(void* comm)
234{
235  return *(static_cast< MPI_Comm* >(comm));
236} 
237
238MPI_Comm* to_mpi_comm_ptr(void* comm)
239{
240  return static_cast< MPI_Comm* >(comm);
241} 
242
243MPI_Message to_mpi_message(void* message)
244{
245  return *(static_cast< MPI_Message* >(message));
246}
247
248MPI_Message* to_mpi_message_ptr(ep_lib::MPI_Message message)
249{
250  return static_cast< MPI_Message* >(message->mpi_message);
251}
252
253MPI_Info to_mpi_info(ep_lib::MPI_Info info)
254{
255  return *(static_cast< MPI_Info* >(info->mpi_info));
256}
257
258MPI_Win to_mpi_win(void* win)
259{
260  return *(static_cast< MPI_Win* >(win));
261}
262
263MPI_Aint to_mpi_aint(ep_lib::MPI_Aint aint)
264{
265  return *(static_cast< MPI_Aint* >(aint.mpi_aint));
266}
267
268MPI_Status* to_mpi_status_ptr(ep_lib::MPI_Status status)
269{
270  return static_cast< MPI_Status* >(status.mpi_status);
271}
272
273MPI_Request* to_mpi_request_ptr(ep_lib::MPI_Request request)
274{
275  return static_cast< MPI_Request* >(request->mpi_request);
276}
277
278
279
Note: See TracBrowser for help on using the repository browser.