1 | #include "ep_lib.hpp" |
---|
2 | #include <mpi.h> |
---|
3 | #include "ep_declaration.hpp" |
---|
4 | #include "ep_mpi.hpp" |
---|
5 | |
---|
6 | using namespace std; |
---|
7 | |
---|
8 | |
---|
9 | namespace ep_lib { |
---|
10 | |
---|
11 | int MPI_Intercomm_merge_unique_leader(MPI_Comm inter_comm, bool high, MPI_Comm *newintracomm) |
---|
12 | { |
---|
13 | Debug("intercomm_merge with unique leader\n"); |
---|
14 | |
---|
15 | |
---|
16 | |
---|
17 | int ep_rank, ep_rank_loc, mpi_rank; |
---|
18 | int ep_size, num_ep, mpi_size; |
---|
19 | |
---|
20 | ep_rank = inter_comm->ep_comm_ptr->size_rank_info[0].first; |
---|
21 | ep_rank_loc = inter_comm->ep_comm_ptr->size_rank_info[1].first; |
---|
22 | mpi_rank = inter_comm->ep_comm_ptr->size_rank_info[2].first; |
---|
23 | ep_size = inter_comm->ep_comm_ptr->size_rank_info[0].second; |
---|
24 | num_ep = inter_comm->ep_comm_ptr->size_rank_info[1].second; |
---|
25 | mpi_size = inter_comm->ep_comm_ptr->size_rank_info[2].second; |
---|
26 | |
---|
27 | int local_high = high; |
---|
28 | int remote_high; |
---|
29 | |
---|
30 | int remote_ep_size = inter_comm->ep_comm_ptr->intercomm->remote_rank_map->size(); |
---|
31 | |
---|
32 | int local_ep_rank, local_ep_rank_loc, local_mpi_rank; |
---|
33 | int local_ep_size, local_num_ep, local_mpi_size; |
---|
34 | |
---|
35 | local_ep_rank = inter_comm->ep_comm_ptr->intercomm->local_comm->ep_comm_ptr->size_rank_info[0].first; |
---|
36 | local_ep_rank_loc = inter_comm->ep_comm_ptr->intercomm->local_comm->ep_comm_ptr->size_rank_info[1].first; |
---|
37 | local_mpi_rank = inter_comm->ep_comm_ptr->intercomm->local_comm->ep_comm_ptr->size_rank_info[2].first; |
---|
38 | local_ep_size = inter_comm->ep_comm_ptr->intercomm->local_comm->ep_comm_ptr->size_rank_info[0].second; |
---|
39 | local_num_ep = inter_comm->ep_comm_ptr->intercomm->local_comm->ep_comm_ptr->size_rank_info[1].second; |
---|
40 | local_mpi_size = inter_comm->ep_comm_ptr->intercomm->local_comm->ep_comm_ptr->size_rank_info[2].second; |
---|
41 | |
---|
42 | |
---|
43 | if(local_ep_rank == 0) |
---|
44 | { |
---|
45 | MPI_Status status[2]; |
---|
46 | MPI_Request request[2]; |
---|
47 | MPI_Isend(&local_high, 1, MPI_INT, 0, inter_comm->ep_comm_ptr->intercomm->intercomm_tag, inter_comm, &request[0]); |
---|
48 | MPI_Irecv(&remote_high, 1, MPI_INT, 0, inter_comm->ep_comm_ptr->intercomm->intercomm_tag, inter_comm, &request[1]); |
---|
49 | |
---|
50 | MPI_Waitall(2, request, status); |
---|
51 | } |
---|
52 | |
---|
53 | |
---|
54 | MPI_Bcast(&remote_high, 1, MPI_INT, 0, inter_comm->ep_comm_ptr->intercomm->local_comm); |
---|
55 | |
---|
56 | |
---|
57 | |
---|
58 | MPI_Comm_dup(inter_comm, newintracomm); |
---|
59 | |
---|
60 | int my_ep_rank = local_high<remote_high? local_ep_rank: local_ep_rank+remote_ep_size; |
---|
61 | |
---|
62 | |
---|
63 | int intra_ep_rank, intra_ep_rank_loc, intra_mpi_rank; |
---|
64 | int intra_ep_size, intra_num_ep, intra_mpi_size; |
---|
65 | |
---|
66 | intra_ep_rank = (*newintracomm)->ep_comm_ptr->size_rank_info[0].first; |
---|
67 | intra_ep_rank_loc = (*newintracomm)->ep_comm_ptr->size_rank_info[1].first; |
---|
68 | intra_mpi_rank = (*newintracomm)->ep_comm_ptr->size_rank_info[2].first; |
---|
69 | intra_ep_size = (*newintracomm)->ep_comm_ptr->size_rank_info[0].second; |
---|
70 | intra_num_ep = (*newintracomm)->ep_comm_ptr->size_rank_info[1].second; |
---|
71 | intra_mpi_size = (*newintracomm)->ep_comm_ptr->size_rank_info[2].second; |
---|
72 | |
---|
73 | |
---|
74 | MPI_Barrier_local(*newintracomm); |
---|
75 | |
---|
76 | |
---|
77 | int *reorder; |
---|
78 | if(intra_ep_rank_loc == 0) |
---|
79 | { |
---|
80 | reorder = new int[intra_ep_size]; |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | MPI_Gather(&my_ep_rank, 1, MPI_INT, reorder, 1, MPI_INT, 0, *newintracomm); |
---|
85 | if(intra_ep_rank_loc == 0) |
---|
86 | { |
---|
87 | ::MPI_Bcast(reorder, intra_ep_size, to_mpi_type(MPI_INT), 0, to_mpi_comm((*newintracomm)->mpi_comm)); |
---|
88 | |
---|
89 | vector< pair<int, int> > tmp_rank_map(intra_ep_size); |
---|
90 | |
---|
91 | |
---|
92 | for(int i=0; i<intra_ep_size; i++) |
---|
93 | { |
---|
94 | tmp_rank_map[reorder[i]] = (*newintracomm)->ep_rank_map->at(i) ; |
---|
95 | } |
---|
96 | |
---|
97 | //(*newintracomm)->rank_map->swap(tmp_rank_map); |
---|
98 | (*newintracomm)->ep_rank_map->clear(); |
---|
99 | for(int i=0; i<tmp_rank_map.size(); i++) |
---|
100 | { |
---|
101 | (*newintracomm)->ep_rank_map->insert(std::pair< int, std::pair<int,int> >(i, tmp_rank_map[i].first, tmp_rank_map[i].second)); |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | tmp_rank_map.clear(); |
---|
106 | } |
---|
107 | |
---|
108 | MPI_Barrier_local(*newintracomm); |
---|
109 | |
---|
110 | (*newintracomm)->ep_comm_ptr->size_rank_info[0].first = my_ep_rank; |
---|
111 | |
---|
112 | if(intra_ep_rank_loc == 0) |
---|
113 | { |
---|
114 | delete[] reorder; |
---|
115 | } |
---|
116 | |
---|
117 | return MPI_SUCCESS; |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | |
---|
122 | |
---|
123 | |
---|
124 | int MPI_Intercomm_merge(MPI_Comm inter_comm, bool high, MPI_Comm *newintracomm) |
---|
125 | { |
---|
126 | |
---|
127 | assert(inter_comm->is_intercomm); |
---|
128 | |
---|
129 | if(inter_comm->ep_comm_ptr->intercomm->local_comm->ep_comm_ptr->comm_label == -99) |
---|
130 | { |
---|
131 | return MPI_Intercomm_merge_unique_leader(inter_comm, high, newintracomm); |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | Debug("intercomm_merge kernel\n"); |
---|
136 | |
---|
137 | int ep_rank_loc; |
---|
138 | int num_ep; |
---|
139 | |
---|
140 | ep_rank_loc = inter_comm->ep_comm_ptr->size_rank_info[1].first; |
---|
141 | num_ep = inter_comm->ep_comm_ptr->size_rank_info[1].second; |
---|
142 | |
---|
143 | |
---|
144 | |
---|
145 | int remote_ep_size = inter_comm->ep_comm_ptr->intercomm->remote_rank_map->size(); |
---|
146 | |
---|
147 | |
---|
148 | MPI_Barrier(inter_comm); |
---|
149 | |
---|
150 | |
---|
151 | ::MPI_Comm *mpi_intracomm = new ::MPI_Comm; |
---|
152 | MPI_Comm *ep_intracomm; |
---|
153 | |
---|
154 | if(ep_rank_loc == 0) |
---|
155 | { |
---|
156 | |
---|
157 | ::MPI_Comm mpi_comm = to_mpi_comm(inter_comm->ep_comm_ptr->intercomm->mpi_inter_comm); |
---|
158 | |
---|
159 | ::MPI_Intercomm_merge(mpi_comm, high, mpi_intracomm); |
---|
160 | MPI_Info info; |
---|
161 | MPI_Comm_create_endpoints(mpi_intracomm, num_ep, info, ep_intracomm); |
---|
162 | |
---|
163 | inter_comm->ep_comm_ptr->comm_list[0]->mem_bridge = ep_intracomm; |
---|
164 | |
---|
165 | } |
---|
166 | |
---|
167 | MPI_Barrier_local(inter_comm); |
---|
168 | |
---|
169 | int inter_rank; |
---|
170 | MPI_Comm_rank(inter_comm, &inter_rank); |
---|
171 | |
---|
172 | int my_ep_rank = high? inter_rank+remote_ep_size : inter_rank; |
---|
173 | int my_ep_rank_loc = inter_comm->ep_comm_ptr->intercomm->local_comm->ep_comm_ptr->size_rank_info[1].first; |
---|
174 | int my_num_ep_loc = inter_comm->ep_comm_ptr->intercomm->local_comm->ep_comm_ptr->size_rank_info[1].second; |
---|
175 | int my_num_ep_total = inter_comm->ep_comm_ptr->comm_list[0]->mem_bridge[0]->ep_comm_ptr->size_rank_info[1].second; |
---|
176 | int my_ep_size = inter_comm->ep_comm_ptr->comm_list[0]->mem_bridge[0]->ep_comm_ptr->size_rank_info[0].second; |
---|
177 | |
---|
178 | int tmp_intra_ep_rank_loc = high?my_ep_rank_loc+my_num_ep_total-my_num_ep_loc: my_ep_rank_loc; |
---|
179 | |
---|
180 | |
---|
181 | *newintracomm = inter_comm->ep_comm_ptr->comm_list[0]->mem_bridge[tmp_intra_ep_rank_loc]; |
---|
182 | |
---|
183 | int newintracomm_ep_rank = (*newintracomm)->ep_comm_ptr->size_rank_info[0].first; |
---|
184 | int newintracomm_ep_rank_loc = (*newintracomm)->ep_comm_ptr->size_rank_info[1].first; |
---|
185 | int newintracomm_mpi_rank = (*newintracomm)->ep_comm_ptr->size_rank_info[2].first; |
---|
186 | int newintracomm_ep_size = (*newintracomm)->ep_comm_ptr->size_rank_info[0].second; |
---|
187 | int newintracomm_num_ep = (*newintracomm)->ep_comm_ptr->size_rank_info[1].second; |
---|
188 | int newintracomm_mpi_size = (*newintracomm)->ep_comm_ptr->size_rank_info[2].second; |
---|
189 | |
---|
190 | |
---|
191 | int buf[3]; |
---|
192 | buf[0] = my_ep_rank; |
---|
193 | buf[1] = tmp_intra_ep_rank_loc; |
---|
194 | buf[2] = (*newintracomm)->ep_comm_ptr->size_rank_info[2].first; |
---|
195 | |
---|
196 | // printf("my_ep_rank = %d, tmp_intra_ep_rank_loc = %d, mpi_rank = %d\n", my_ep_rank, tmp_intra_ep_rank_loc, newintracomm->ep_comm_ptr->size_rank_info[2].first); |
---|
197 | |
---|
198 | int *rankmap_buf; |
---|
199 | rankmap_buf = new int [3*my_ep_size]; |
---|
200 | |
---|
201 | MPI_Allgather(buf, 3, MPI_INT, rankmap_buf, 3, MPI_INT, *newintracomm); |
---|
202 | |
---|
203 | |
---|
204 | // printf(" ID = %d : rankmap_buf = (%d %d %d), (%d %d %d), (%d %d %d), (%d %d %d), (%d %d %d), (%d %d %d), (%d %d %d), (%d %d %d), (%d %d %d), (%d %d %d), (%d %d %d), (%d %d %d), (%d %d %d), (%d %d %d), (%d %d %d), (%d %d %d)\n", newintracomm_ep_rank, |
---|
205 | // rankmap_buf[0], rankmap_buf[1], rankmap_buf[2], rankmap_buf[3], rankmap_buf[4], rankmap_buf[5], rankmap_buf[6], rankmap_buf[7], rankmap_buf[8], rankmap_buf[9], |
---|
206 | // rankmap_buf[10], rankmap_buf[11], rankmap_buf[12], rankmap_buf[13], rankmap_buf[14], rankmap_buf[15], rankmap_buf[16], rankmap_buf[17], rankmap_buf[18], rankmap_buf[19], |
---|
207 | // rankmap_buf[20], rankmap_buf[21], rankmap_buf[22], rankmap_buf[23], rankmap_buf[24], rankmap_buf[25], rankmap_buf[26], rankmap_buf[27], rankmap_buf[28], rankmap_buf[29], |
---|
208 | // rankmap_buf[30], rankmap_buf[31], rankmap_buf[32], rankmap_buf[33], rankmap_buf[34], rankmap_buf[35], rankmap_buf[36], rankmap_buf[37], rankmap_buf[38], rankmap_buf[39], |
---|
209 | // rankmap_buf[40], rankmap_buf[41], rankmap_buf[42], rankmap_buf[43], rankmap_buf[44], rankmap_buf[45], rankmap_buf[46], rankmap_buf[47]); |
---|
210 | |
---|
211 | |
---|
212 | for(int i=0; i<newintracomm_ep_size; i++) |
---|
213 | { |
---|
214 | (*newintracomm)->ep_rank_map->at(rankmap_buf[3*i]).first = rankmap_buf[3*i+1]; |
---|
215 | (*newintracomm)->ep_rank_map->at(rankmap_buf[3*i]).second = rankmap_buf[3*i+2]; |
---|
216 | } |
---|
217 | |
---|
218 | |
---|
219 | (*newintracomm)->ep_comm_ptr->size_rank_info[0].first = my_ep_rank; |
---|
220 | (*newintracomm)->ep_comm_ptr->size_rank_info[1].first = tmp_intra_ep_rank_loc; |
---|
221 | |
---|
222 | |
---|
223 | delete[] rankmap_buf; |
---|
224 | |
---|
225 | |
---|
226 | return MPI_SUCCESS; |
---|
227 | |
---|
228 | } |
---|
229 | |
---|
230 | |
---|
231 | } |
---|