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

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

save dev

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