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

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

save dev

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