source: XIOS/dev/branch_openmp/extern/ep_dev/ep_lib.cpp @ 1381

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

add folder for MPI EP-RMA development. Current: MPI_Win, MPI_win_create, MPI_win_fence, MPI_win_free

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