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->rank_map->at(i) ; |
---|
95 | } |
---|
96 | |
---|
97 | newintracomm->rank_map->swap(tmp_rank_map); |
---|
98 | |
---|
99 | tmp_rank_map.clear(); |
---|
100 | } |
---|
101 | |
---|
102 | MPI_Barrier_local(*newintracomm); |
---|
103 | |
---|
104 | (*newintracomm).ep_comm_ptr->size_rank_info[0].first = my_ep_rank; |
---|
105 | |
---|
106 | if(intra_ep_rank_loc == 0) |
---|
107 | { |
---|
108 | delete[] reorder; |
---|
109 | } |
---|
110 | |
---|
111 | return MPI_SUCCESS; |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | |
---|
116 | |
---|
117 | |
---|
118 | int MPI_Intercomm_merge(MPI_Comm inter_comm, bool high, MPI_Comm *newintracomm) |
---|
119 | { |
---|
120 | |
---|
121 | assert(inter_comm.is_intercomm); |
---|
122 | |
---|
123 | if(inter_comm.ep_comm_ptr->intercomm->local_comm->ep_comm_ptr->comm_label == -99) |
---|
124 | { |
---|
125 | return MPI_Intercomm_merge_unique_leader(inter_comm, high, newintracomm); |
---|
126 | } |
---|
127 | |
---|
128 | |
---|
129 | Debug("intercomm_merge kernel\n"); |
---|
130 | |
---|
131 | int ep_rank_loc; |
---|
132 | int num_ep; |
---|
133 | |
---|
134 | ep_rank_loc = inter_comm.ep_comm_ptr->size_rank_info[1].first; |
---|
135 | num_ep = inter_comm.ep_comm_ptr->size_rank_info[1].second; |
---|
136 | |
---|
137 | |
---|
138 | |
---|
139 | int remote_ep_size = inter_comm.ep_comm_ptr->intercomm->remote_rank_map->size(); |
---|
140 | |
---|
141 | |
---|
142 | MPI_Barrier(inter_comm); |
---|
143 | |
---|
144 | |
---|
145 | ::MPI_Comm *mpi_intracomm = new ::MPI_Comm; |
---|
146 | MPI_Comm *ep_intracomm; |
---|
147 | |
---|
148 | if(ep_rank_loc == 0) |
---|
149 | { |
---|
150 | |
---|
151 | ::MPI_Comm mpi_comm = to_mpi_comm(inter_comm.ep_comm_ptr->intercomm->mpi_inter_comm); |
---|
152 | |
---|
153 | ::MPI_Intercomm_merge(mpi_comm, high, mpi_intracomm); |
---|
154 | MPI_Info info; |
---|
155 | MPI_Comm_create_endpoints(mpi_intracomm, num_ep, info, ep_intracomm); |
---|
156 | |
---|
157 | inter_comm.ep_comm_ptr->comm_list->mem_bridge = ep_intracomm; |
---|
158 | |
---|
159 | } |
---|
160 | |
---|
161 | MPI_Barrier_local(inter_comm); |
---|
162 | |
---|
163 | int inter_rank; |
---|
164 | MPI_Comm_rank(inter_comm, &inter_rank); |
---|
165 | |
---|
166 | int my_ep_rank = high? inter_rank+remote_ep_size : inter_rank; |
---|
167 | int my_ep_rank_loc = inter_comm.ep_comm_ptr->intercomm->local_comm->ep_comm_ptr->size_rank_info[1].first; |
---|
168 | int my_num_ep_loc = inter_comm.ep_comm_ptr->intercomm->local_comm->ep_comm_ptr->size_rank_info[1].second; |
---|
169 | int my_num_ep_total = inter_comm.ep_comm_ptr->comm_list->mem_bridge[0].ep_comm_ptr->size_rank_info[1].second; |
---|
170 | int my_ep_size = inter_comm.ep_comm_ptr->comm_list->mem_bridge[0].ep_comm_ptr->size_rank_info[0].second; |
---|
171 | |
---|
172 | int tmp_intra_ep_rank_loc = high?my_ep_rank_loc+my_num_ep_total-my_num_ep_loc: my_ep_rank_loc; |
---|
173 | |
---|
174 | |
---|
175 | *newintracomm = inter_comm.ep_comm_ptr->comm_list->mem_bridge[tmp_intra_ep_rank_loc]; |
---|
176 | |
---|
177 | int newintracomm_ep_rank = (*newintracomm).ep_comm_ptr->size_rank_info[0].first; |
---|
178 | int newintracomm_ep_rank_loc = (*newintracomm).ep_comm_ptr->size_rank_info[1].first; |
---|
179 | int newintracomm_mpi_rank = (*newintracomm).ep_comm_ptr->size_rank_info[2].first; |
---|
180 | int newintracomm_ep_size = (*newintracomm).ep_comm_ptr->size_rank_info[0].second; |
---|
181 | int newintracomm_num_ep = (*newintracomm).ep_comm_ptr->size_rank_info[1].second; |
---|
182 | int newintracomm_mpi_size = (*newintracomm).ep_comm_ptr->size_rank_info[2].second; |
---|
183 | |
---|
184 | |
---|
185 | int buf[3]; |
---|
186 | buf[0] = my_ep_rank; |
---|
187 | buf[1] = tmp_intra_ep_rank_loc; |
---|
188 | buf[2] = newintracomm->ep_comm_ptr->size_rank_info[2].first; |
---|
189 | |
---|
190 | // 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); |
---|
191 | |
---|
192 | int *rankmap_buf; |
---|
193 | rankmap_buf = new int [3*my_ep_size]; |
---|
194 | |
---|
195 | MPI_Allgather(buf, 3, MPI_INT, rankmap_buf, 3, MPI_INT, *newintracomm); |
---|
196 | |
---|
197 | |
---|
198 | // 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, |
---|
199 | // 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], |
---|
200 | // 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], |
---|
201 | // 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], |
---|
202 | // 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], |
---|
203 | // rankmap_buf[40], rankmap_buf[41], rankmap_buf[42], rankmap_buf[43], rankmap_buf[44], rankmap_buf[45], rankmap_buf[46], rankmap_buf[47]); |
---|
204 | |
---|
205 | |
---|
206 | for(int i=0; i<newintracomm_ep_size; i++) |
---|
207 | { |
---|
208 | (*newintracomm).rank_map->at(rankmap_buf[3*i]).first = rankmap_buf[3*i+1]; |
---|
209 | (*newintracomm).rank_map->at(rankmap_buf[3*i]).second = rankmap_buf[3*i+2]; |
---|
210 | } |
---|
211 | |
---|
212 | |
---|
213 | (*newintracomm).ep_comm_ptr->size_rank_info[0].first = my_ep_rank; |
---|
214 | (*newintracomm).ep_comm_ptr->size_rank_info[1].first = tmp_intra_ep_rank_loc; |
---|
215 | |
---|
216 | |
---|
217 | delete[] rankmap_buf; |
---|
218 | |
---|
219 | |
---|
220 | return MPI_SUCCESS; |
---|
221 | |
---|
222 | } |
---|
223 | |
---|
224 | |
---|
225 | } |
---|