source: XIOS/dev/branch_openmp/extern/src_ep_dev/ep_create.cpp @ 1461

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

unify MPI_Comm type

File size: 8.1 KB
RevLine 
[1134]1/*!
2   \file ep_create.cpp
3   \since 2 may 2016
4
5   \brief Definitions of MPI endpoint function: MPI_Comm_create_endpoints
6 */
7
8#include "ep_lib.hpp"
9#include <mpi.h>
10#include "ep_declaration.hpp"
[1354]11#include "ep_mpi.hpp"
[1134]12
13using namespace std;
14
15namespace ep_lib {
16
17  /*!
18    Dynamic creation of endpoints for each MPI process.
19    The output of this function is an array of communicator handles of length num_ep, where each handle
20    corresponds to a nez local randk in the output communicator.
21    Once created, endpoints behave as MPI processes.
22    \param [in] mpi_comm Parent MPI communicator.
23    \param [in] num_ep Number of endpoints per process.
24    \param [out] info Information of the EP creation.
25    \param [out] out_comm_hdls Handles of EP communicators.
26  */
[1354]27  // #ifdef _intelmpi
28  // int MPI_Comm_create_endpoints(int base_comm_ptr, int num_ep, MPI_Info info, MPI_Comm *& out_comm_hdls)
29  // {
30  //   int base_rank;
31  //   int base_size;
[1134]32
[1354]33  //   ::MPI_Comm mpi_base_comm = static_cast< ::MPI_Comm> (base_comm_ptr);
[1134]34
[1354]35  //   ::MPI_Comm_size(mpi_base_comm, &base_size);  // ep_lib::mpi_comm_size
36  //   ::MPI_Comm_rank(mpi_base_comm, &base_rank);  // ep_lib::mpi_comm_rank
37                //                                                        // parent_comm can also be endpoints communicators
[1134]38
[1354]39  //   std::vector<int> recv_num_ep(base_size);
[1134]40
[1354]41  //   out_comm_hdls = new MPI_Comm[num_ep];
[1134]42
[1354]43  //   for (int idx = 0; idx < num_ep; ++idx)
44  //   {
45  //     out_comm_hdls[idx].is_ep = true;
46  //     out_comm_hdls[idx].is_intercomm = false;
47  //     out_comm_hdls[idx].ep_comm_ptr = new ep_communicator;
48  //     out_comm_hdls[idx].mpi_comm = base_comm_ptr;
49  //     out_comm_hdls[idx].ep_comm_ptr->comm_list = out_comm_hdls;
50  //     out_comm_hdls[idx].ep_comm_ptr->comm_label = 0;
51  //   }
[1134]52
[1354]53  //   ::MPI_Allgather(&num_ep, 1, static_cast< ::MPI_Datatype>(MPI_INT), &recv_num_ep[0], 1, static_cast< ::MPI_Datatype>(MPI_INT), mpi_base_comm);
[1134]54
55
[1354]56  //   int sum = 0;  // representing total ep number of process with smaller rank
57  //   for (int i = 0; i < base_rank; ++i) {sum += recv_num_ep[i]; }
[1134]58
[1354]59  //   int ep_size = std::accumulate(recv_num_ep.begin(), recv_num_ep.end(), 0);
[1134]60
[1354]61  //   out_comm_hdls[0].ep_barrier = new OMPbarrier(num_ep);
[1134]62
[1354]63  //   out_comm_hdls[0].my_buffer = new BUFFER;
[1134]64
[1354]65  //   out_comm_hdls[0].rank_map = new RANK_MAP;
66  //   out_comm_hdls[0].rank_map->resize(ep_size);
[1134]67
68
[1354]69  //   for (int i = 1; i < num_ep; i++)
70  //   {
71  //     out_comm_hdls[i].ep_barrier = out_comm_hdls[0].ep_barrier;
72  //     out_comm_hdls[i].my_buffer  = out_comm_hdls[0].my_buffer;
73  //     out_comm_hdls[i].rank_map   = out_comm_hdls[0].rank_map;
74  //   }
[1134]75
76
[1354]77  //   for (int i = 0; i < num_ep; i++)
78  //   {
79  //     out_comm_hdls[i].ep_comm_ptr->size_rank_info[0] = std::make_pair(sum+i, ep_size);
80  //     out_comm_hdls[i].ep_comm_ptr->size_rank_info[1] = std::make_pair(i, num_ep);
81  //     out_comm_hdls[i].ep_comm_ptr->size_rank_info[2] = std::make_pair(base_rank, base_size);
[1134]82
[1354]83  //     out_comm_hdls[i].ep_comm_ptr->message_queue = new Message_list;
84  //   }
[1134]85
86
[1354]87  //   int ind = 0;
[1134]88
[1354]89  //   for(int i=0; i<base_size; i++)
90  //   {
91  //     for(int j=0; j<recv_num_ep[i]; j++)
92  //     {
93  //       out_comm_hdls[0].rank_map->at(ind) = make_pair(j, i);
94  //       ind++;
95  //     }
96  //   }
[1134]97
98
[1295]99
[1354]100  //   return 0;
[1134]101
[1354]102  // } //MPI_Comm_create_endpoints
[1134]103
[1354]104  // #elif _openmpi
105  // int MPI_Comm_create_endpoints(void* base_comm_ptr, int num_ep, MPI_Info info, MPI_Comm *& out_comm_hdls)
106  // {
[1134]107
[1354]108  //   int base_rank;
109  //   int base_size;
[1134]110
[1354]111  //   ::MPI_Comm mpi_base_comm = static_cast< ::MPI_Comm> (base_comm_ptr);
[1134]112
[1354]113  //   ::MPI_Comm_size(mpi_base_comm, &base_size);  // ep_lib::mpi_comm_size
114  //   ::MPI_Comm_rank(mpi_base_comm, &base_rank);  // ep_lib::mpi_comm_rank
115  //                                                // parent_comm can also be endpoints communicators ?
116  //   std::vector<int> recv_num_ep(base_size);
[1134]117
[1354]118  //   out_comm_hdls = new MPI_Comm[num_ep];
[1134]119
[1354]120  //   for (int idx = 0; idx < num_ep; ++idx)
121  //   {
122  //     out_comm_hdls[idx].is_ep = true;
123  //     out_comm_hdls[idx].is_intercomm = false;
124  //     out_comm_hdls[idx].ep_comm_ptr = new ep_communicator;
125  //     out_comm_hdls[idx].mpi_comm = base_comm_ptr;
126  //     out_comm_hdls[idx].ep_comm_ptr->comm_list = out_comm_hdls;
127  //     out_comm_hdls[idx].ep_comm_ptr->comm_label = 0;
128  //   }
[1134]129
[1354]130  //   ::MPI_Allgather(&num_ep, 1, static_cast< ::MPI_Datatype> (MPI_INT),
131  //                  &recv_num_ep[0], 1, static_cast< ::MPI_Datatype> (MPI_INT), mpi_base_comm);
[1134]132
[1354]133  //   int sum = 0;  // representing total ep number of process with smaller rank
134  //   for (int i = 0; i < base_rank; ++i) {sum += recv_num_ep[i]; }
[1134]135
[1354]136  //   int ep_size = std::accumulate(recv_num_ep.begin(), recv_num_ep.end(), 0);
[1134]137
[1354]138  //   out_comm_hdls[0].ep_barrier = new OMPbarrier(num_ep);
139  //   out_comm_hdls[0].my_buffer = new BUFFER;
[1134]140
[1354]141  //   out_comm_hdls[0].rank_map = new RANK_MAP;
142  //   out_comm_hdls[0].rank_map->resize(ep_size);
[1134]143
144
[1354]145  //   for (int i = 1; i < num_ep; i++)
146  //   {
147  //     out_comm_hdls[i].ep_barrier = out_comm_hdls[0].ep_barrier;
148  //     out_comm_hdls[i].my_buffer  = out_comm_hdls[0].my_buffer;
149  //     out_comm_hdls[i].rank_map   = out_comm_hdls[0].rank_map;
150  //   }
[1134]151
152
[1354]153  //   for (int i = 0; i < num_ep; i++)
154  //   {
155  //     out_comm_hdls[i].ep_comm_ptr->size_rank_info[0] = std::make_pair(sum+i, ep_size);
156  //     out_comm_hdls[i].ep_comm_ptr->size_rank_info[1] = std::make_pair(i, num_ep);
157  //     out_comm_hdls[i].ep_comm_ptr->size_rank_info[2] = std::make_pair(base_rank, base_size);
[1134]158
[1354]159  //     out_comm_hdls[i].ep_comm_ptr->message_queue = new Message_list;
160  //   }
[1134]161
162
[1354]163  //   int ind = 0;
[1134]164
[1354]165  //   for(int i=0; i<base_size; i++)
166  //   {
167  //     for(int j=0; j<recv_num_ep[i]; j++)
168  //     {
169  //       out_comm_hdls[0].rank_map->at(ind) = make_pair(j, i);
170  //       ind++;
171  //     }
172  //   }
[1134]173
[1354]174  //   return 0;
[1134]175
[1354]176  // } //MPI_Comm_create_endpoints
[1134]177
[1354]178  // #endif
[1134]179 
180 
[1356]181  int MPI_Comm_create_endpoints(void* base_comm_ptr, int num_ep, MPI_Info info, MPI_Comm *& out_comm_hdls)
[1134]182  {
183
184    int base_rank;
185    int base_size;
186   
[1354]187    ::MPI_Comm mpi_base_comm = to_mpi_comm(base_comm_ptr);
[1134]188
189    ::MPI_Comm_size(mpi_base_comm, &base_size);  // ep_lib::mpi_comm_size
190    ::MPI_Comm_rank(mpi_base_comm, &base_rank);  // ep_lib::mpi_comm_rank
191                    // parent_comm can also be endpoints communicators
192
193    std::vector<int> recv_num_ep(base_size);
194
195    out_comm_hdls = new MPI_Comm[num_ep];
196
197    for (int idx = 0; idx < num_ep; ++idx)
198    {
199      out_comm_hdls[idx].is_ep = true;
200      out_comm_hdls[idx].is_intercomm = false;
[1355]201      out_comm_hdls[idx].ep_comm_ptr = new ep_communicator;     
[1356]202      *(static_cast< ::MPI_Comm*>(out_comm_hdls[idx].mpi_comm)) = *(static_cast< ::MPI_Comm*>(base_comm_ptr));
[1134]203      out_comm_hdls[idx].ep_comm_ptr->comm_list = out_comm_hdls;
204      out_comm_hdls[idx].ep_comm_ptr->comm_label = 0;
205    }
206
[1354]207    ::MPI_Allgather(&num_ep, 1, to_mpi_type(MPI_INT), &recv_num_ep[0], 1, to_mpi_type(MPI_INT), mpi_base_comm);
[1134]208
209
210    int sum = 0;  // representing total ep number of process with smaller rank
211    for (int i = 0; i < base_rank; ++i) {sum += recv_num_ep[i]; }
212
213    int ep_size = std::accumulate(recv_num_ep.begin(), recv_num_ep.end(), 0);
214
215    out_comm_hdls[0].ep_barrier = new OMPbarrier(num_ep);
216    out_comm_hdls[0].my_buffer = new BUFFER;
217
218    out_comm_hdls[0].rank_map = new RANK_MAP;
219    out_comm_hdls[0].rank_map->resize(ep_size);
220
221
222    for (int i = 1; i < num_ep; i++)
223    {
224      out_comm_hdls[i].ep_barrier = out_comm_hdls[0].ep_barrier;
225      out_comm_hdls[i].my_buffer  = out_comm_hdls[0].my_buffer;
226      out_comm_hdls[i].rank_map   = out_comm_hdls[0].rank_map;
227    }
228
229
230    for (int i = 0; i < num_ep; i++)
231    {
232      out_comm_hdls[i].ep_comm_ptr->size_rank_info[0] = std::make_pair(sum+i, ep_size);
233      out_comm_hdls[i].ep_comm_ptr->size_rank_info[1] = std::make_pair(i, num_ep);
234      out_comm_hdls[i].ep_comm_ptr->size_rank_info[2] = std::make_pair(base_rank, base_size);
235
236      out_comm_hdls[i].ep_comm_ptr->message_queue = new Message_list;
237    }
238
239
240    int ind = 0;
241
242    for(int i=0; i<base_size; i++)
243    {
244      for(int j=0; j<recv_num_ep[i]; j++)
245      {
246        out_comm_hdls[0].rank_map->at(ind) = make_pair(j, i);
247        ind++;
248      }
249    }
250
251    return 0;
252
253  } //MPI_Comm_create_endpoints
254
255
256} //namespace ep_lib
Note: See TracBrowser for help on using the repository browser.