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

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

EP updated

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