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 | namespace ep_lib |
---|
9 | { |
---|
10 | int MPI_Intercomm_create(MPI_Comm local_comm, int local_leader, MPI_Comm peer_comm, int remote_leader, int tag, MPI_Comm *newintercomm) |
---|
11 | { |
---|
12 | assert(local_comm->is_ep); |
---|
13 | |
---|
14 | int ep_rank, ep_rank_loc, mpi_rank; |
---|
15 | int ep_size, num_ep, mpi_size; |
---|
16 | |
---|
17 | ep_rank = local_comm->ep_comm_ptr->size_rank_info[0].first; |
---|
18 | ep_rank_loc = local_comm->ep_comm_ptr->size_rank_info[1].first; |
---|
19 | mpi_rank = local_comm->ep_comm_ptr->size_rank_info[2].first; |
---|
20 | ep_size = local_comm->ep_comm_ptr->size_rank_info[0].second; |
---|
21 | num_ep = local_comm->ep_comm_ptr->size_rank_info[1].second; |
---|
22 | mpi_size = local_comm->ep_comm_ptr->size_rank_info[2].second; |
---|
23 | |
---|
24 | |
---|
25 | MPI_Barrier(local_comm); |
---|
26 | |
---|
27 | int leader_ranks_in_peer[3]; // local_leader_rank_in_peer |
---|
28 | // remote_leader_rank_in_peer |
---|
29 | // size of peer |
---|
30 | |
---|
31 | if(ep_rank == local_leader) |
---|
32 | { |
---|
33 | MPI_Comm_rank(peer_comm, &leader_ranks_in_peer[0]); |
---|
34 | leader_ranks_in_peer[1] = remote_leader; |
---|
35 | MPI_Comm_size(peer_comm, &leader_ranks_in_peer[2]); |
---|
36 | } |
---|
37 | |
---|
38 | MPI_Bcast(leader_ranks_in_peer, 3, MPI_INT, local_leader, local_comm); |
---|
39 | |
---|
40 | if(leader_ranks_in_peer[0] != leader_ranks_in_peer[2]) |
---|
41 | { |
---|
42 | Debug("calling MPI_Intercomm_create_kernel\n"); |
---|
43 | return MPI_Intercomm_create_kernel(local_comm, local_leader, peer_comm, remote_leader, tag, newintercomm); |
---|
44 | } |
---|
45 | |
---|
46 | else |
---|
47 | { |
---|
48 | if(leader_ranks_in_peer[2] == 1) |
---|
49 | { |
---|
50 | Debug("calling MPI_Intercomm_create_unique\n"); |
---|
51 | return MPI_Intercomm_create_unique_leader(local_comm, local_leader, peer_comm, remote_leader, tag, newintercomm); |
---|
52 | |
---|
53 | } |
---|
54 | else |
---|
55 | { |
---|
56 | Debug("calling MPI_Intercomm_create_world\n"); |
---|
57 | return MPI_Intercomm_create_from_world(local_comm, local_leader, peer_comm, remote_leader, tag, newintercomm); |
---|
58 | } |
---|
59 | |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | |
---|
64 | |
---|
65 | |
---|
66 | int leader_ranks[6]; //! 0: rank in world, 1: mpi_size, 2: rank_in_peer. |
---|
67 | //! 3, 4, 5 : remote |
---|
68 | |
---|
69 | bool is_decider = false; |
---|
70 | |
---|
71 | |
---|
72 | if(ep_rank == local_leader) |
---|
73 | { |
---|
74 | MPI_Comm_rank(MPI_COMM_WORLD, &leader_ranks[0]); |
---|
75 | |
---|
76 | leader_ranks[1] = mpi_size; |
---|
77 | MPI_Comm_rank(peer_comm, &leader_ranks[2]); |
---|
78 | |
---|
79 | MPI_Request request[2]; |
---|
80 | MPI_Status status[2]; |
---|
81 | |
---|
82 | MPI_Isend(&leader_ranks[0], 3, MPI_INT, remote_leader, tag, peer_comm, &request[0]); |
---|
83 | MPI_Irecv(&leader_ranks[3], 3, MPI_INT, remote_leader, tag, peer_comm, &request[1]); |
---|
84 | |
---|
85 | MPI_Waitall(2, request, status); |
---|
86 | |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | MPI_Bcast(leader_ranks, 6, MPI_INT, local_leader, local_comm); |
---|
91 | |
---|
92 | |
---|
93 | MPI_Barrier(local_comm); |
---|
94 | |
---|
95 | |
---|
96 | if(leader_ranks[0] == leader_ranks[3]) |
---|
97 | { |
---|
98 | if( leader_ranks[1] * leader_ranks[4] == 1) |
---|
99 | { |
---|
100 | if(ep_rank == local_leader) printf("calling MPI_Intercomm_create_unique_leader\n"); |
---|
101 | local_comm->ep_comm_ptr->comm_label = -99; |
---|
102 | |
---|
103 | return MPI_Intercomm_create_unique_leader(local_comm, local_leader, peer_comm, remote_leader, tag, newintercomm); |
---|
104 | } |
---|
105 | else // leader_ranks[1] * leader_ranks[4] != 1 |
---|
106 | { |
---|
107 | // change leader |
---|
108 | int new_local_leader; |
---|
109 | |
---|
110 | if(leader_ranks[2] < leader_ranks[5]) |
---|
111 | { |
---|
112 | if(leader_ranks[1] > 1) //! change leader |
---|
113 | { |
---|
114 | // change leader |
---|
115 | is_decider = true; |
---|
116 | int target = local_comm->ep_rank_map->at(local_leader).second; |
---|
117 | { |
---|
118 | for(int i=0; i<ep_size; i++) |
---|
119 | { |
---|
120 | if(local_comm->ep_rank_map->at(i).second != target && local_comm->ep_rank_map->at(i).first == 0) |
---|
121 | { |
---|
122 | new_local_leader = i; |
---|
123 | break; |
---|
124 | } |
---|
125 | } |
---|
126 | } |
---|
127 | } |
---|
128 | else |
---|
129 | { |
---|
130 | new_local_leader = local_leader; |
---|
131 | } |
---|
132 | } |
---|
133 | else |
---|
134 | { |
---|
135 | if(leader_ranks[4] == 1) |
---|
136 | { |
---|
137 | // change leader |
---|
138 | is_decider = true; |
---|
139 | int target = local_comm->ep_rank_map->at(local_leader).second; |
---|
140 | { |
---|
141 | for(int i=0; i<ep_size; i++) |
---|
142 | { |
---|
143 | if(local_comm->ep_rank_map->at(i).second != target && local_comm->ep_rank_map->at(i).first == 0) |
---|
144 | { |
---|
145 | new_local_leader = i; |
---|
146 | break; |
---|
147 | } |
---|
148 | } |
---|
149 | } |
---|
150 | } |
---|
151 | else |
---|
152 | { |
---|
153 | new_local_leader = local_leader; |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | int new_tag_in_world; |
---|
159 | |
---|
160 | int leader_in_world[2]; |
---|
161 | |
---|
162 | |
---|
163 | if(is_decider) |
---|
164 | { |
---|
165 | if(ep_rank == new_local_leader) |
---|
166 | { |
---|
167 | new_tag_in_world = TAG++; |
---|
168 | } |
---|
169 | MPI_Bcast(&new_tag_in_world, 1, MPI_INT, new_local_leader, local_comm); |
---|
170 | if(ep_rank == local_leader) MPI_Send(&new_tag_in_world, 1, MPI_INT, remote_leader, tag, peer_comm); |
---|
171 | } |
---|
172 | else |
---|
173 | { |
---|
174 | if(ep_rank == local_leader) |
---|
175 | { |
---|
176 | MPI_Status status; |
---|
177 | MPI_Recv(&new_tag_in_world, 1, MPI_INT, remote_leader, tag, peer_comm, &status); |
---|
178 | } |
---|
179 | MPI_Bcast(&new_tag_in_world, 1, MPI_INT, new_local_leader, local_comm); |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | if(ep_rank == new_local_leader) |
---|
184 | { |
---|
185 | ::MPI_Comm_rank(to_mpi_comm(MPI_COMM_WORLD->mpi_comm), &leader_in_world[0]); |
---|
186 | } |
---|
187 | |
---|
188 | MPI_Bcast(&leader_in_world[0], 1, MPI_INT, new_local_leader, local_comm); |
---|
189 | |
---|
190 | |
---|
191 | if(ep_rank == local_leader) |
---|
192 | { |
---|
193 | MPI_Request request[2]; |
---|
194 | MPI_Status status[2]; |
---|
195 | |
---|
196 | MPI_Isend(&leader_in_world[0], 1, MPI_INT, remote_leader, tag, peer_comm, &request[0]); |
---|
197 | MPI_Irecv(&leader_in_world[1], 1, MPI_INT, remote_leader, tag, peer_comm, &request[1]); |
---|
198 | |
---|
199 | MPI_Waitall(2, request, status); |
---|
200 | } |
---|
201 | |
---|
202 | MPI_Bcast(&leader_in_world[1], 1, MPI_INT, local_leader, local_comm); |
---|
203 | |
---|
204 | local_comm->ep_comm_ptr->comm_label = tag; |
---|
205 | |
---|
206 | if(ep_rank == local_leader) printf("calling MPI_Intercomm_create_from_world\n"); |
---|
207 | |
---|
208 | return MPI_Intercomm_create_from_world(local_comm, new_local_leader, MPI_COMM_WORLD->mpi_comm, leader_in_world[1], new_tag_in_world, newintercomm); |
---|
209 | |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | |
---|
214 | |
---|
215 | } |
---|
216 | |
---|
217 | int MPI_Comm_test_inter(MPI_Comm comm, int *flag) |
---|
218 | { |
---|
219 | *flag = false; |
---|
220 | if(comm->is_ep) |
---|
221 | { |
---|
222 | *flag = comm->is_intercomm; |
---|
223 | return 0; |
---|
224 | } |
---|
225 | else if(comm->mpi_comm != static_cast< ::MPI_Comm*>(MPI_COMM_NULL->mpi_comm)) |
---|
226 | { |
---|
227 | ::MPI_Comm mpi_comm = to_mpi_comm(comm->mpi_comm); |
---|
228 | |
---|
229 | ::MPI_Comm_test_inter(mpi_comm, flag); |
---|
230 | return 0; |
---|
231 | } |
---|
232 | return 0; |
---|
233 | } |
---|
234 | |
---|
235 | |
---|
236 | } |
---|