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

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

save dev

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