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 | |
---|
28 | |
---|
29 | int leader_ranks[6]; //! 0: rank in world, 1: mpi_size, 2: rank_in_peer. |
---|
30 | //! 3, 4, 5 : remote |
---|
31 | |
---|
32 | bool is_decider = false; |
---|
33 | |
---|
34 | |
---|
35 | if(ep_rank == local_leader) |
---|
36 | { |
---|
37 | MPI_Comm_rank(MPI_COMM_WORLD, &leader_ranks[0]); |
---|
38 | |
---|
39 | leader_ranks[1] = mpi_size; |
---|
40 | MPI_Comm_rank(peer_comm, &leader_ranks[2]); |
---|
41 | |
---|
42 | MPI_Request request[2]; |
---|
43 | MPI_Status status[2]; |
---|
44 | |
---|
45 | MPI_Isend(&leader_ranks[0], 3, MPI_INT, remote_leader, tag, peer_comm, &request[0]); |
---|
46 | MPI_Irecv(&leader_ranks[3], 3, MPI_INT, remote_leader, tag, peer_comm, &request[1]); |
---|
47 | |
---|
48 | MPI_Waitall(2, request, status); |
---|
49 | |
---|
50 | } |
---|
51 | |
---|
52 | |
---|
53 | MPI_Bcast(leader_ranks, 6, MPI_INT, local_leader, local_comm); |
---|
54 | |
---|
55 | |
---|
56 | MPI_Barrier(local_comm); |
---|
57 | |
---|
58 | |
---|
59 | if(leader_ranks[0] == leader_ranks[3]) |
---|
60 | { |
---|
61 | if( leader_ranks[1] * leader_ranks[4] == 1) |
---|
62 | { |
---|
63 | if(ep_rank == local_leader) printf("calling MPI_Intercomm_create_unique_leader\n"); |
---|
64 | local_comm->ep_comm_ptr->comm_label = -99; |
---|
65 | |
---|
66 | return MPI_Intercomm_create_unique_leader(local_comm, local_leader, peer_comm, remote_leader, tag, newintercomm); |
---|
67 | } |
---|
68 | else // leader_ranks[1] * leader_ranks[4] != 1 |
---|
69 | { |
---|
70 | // change leader |
---|
71 | int new_local_leader; |
---|
72 | |
---|
73 | if(leader_ranks[2] < leader_ranks[5]) |
---|
74 | { |
---|
75 | if(leader_ranks[1] > 1) //! change leader |
---|
76 | { |
---|
77 | // change leader |
---|
78 | is_decider = true; |
---|
79 | int target = local_comm->ep_rank_map->at(local_leader).second; |
---|
80 | { |
---|
81 | for(int i=0; i<ep_size; i++) |
---|
82 | { |
---|
83 | if(local_comm->ep_rank_map->at(i).second != target && local_comm->ep_rank_map->at(i).first == 0) |
---|
84 | { |
---|
85 | new_local_leader = i; |
---|
86 | break; |
---|
87 | } |
---|
88 | } |
---|
89 | } |
---|
90 | } |
---|
91 | else |
---|
92 | { |
---|
93 | new_local_leader = local_leader; |
---|
94 | } |
---|
95 | } |
---|
96 | else |
---|
97 | { |
---|
98 | if(leader_ranks[4] == 1) |
---|
99 | { |
---|
100 | // change leader |
---|
101 | is_decider = true; |
---|
102 | int target = local_comm->ep_rank_map->at(local_leader).second; |
---|
103 | { |
---|
104 | for(int i=0; i<ep_size; i++) |
---|
105 | { |
---|
106 | if(local_comm->ep_rank_map->at(i).second != target && local_comm->ep_rank_map->at(i).first == 0) |
---|
107 | { |
---|
108 | new_local_leader = i; |
---|
109 | break; |
---|
110 | } |
---|
111 | } |
---|
112 | } |
---|
113 | } |
---|
114 | else |
---|
115 | { |
---|
116 | new_local_leader = local_leader; |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | int new_tag_in_world; |
---|
122 | |
---|
123 | int leader_in_world[2]; |
---|
124 | |
---|
125 | |
---|
126 | if(is_decider) |
---|
127 | { |
---|
128 | if(ep_rank == new_local_leader) |
---|
129 | { |
---|
130 | new_tag_in_world = TAG++; |
---|
131 | } |
---|
132 | MPI_Bcast(&new_tag_in_world, 1, MPI_INT, new_local_leader, local_comm); |
---|
133 | if(ep_rank == local_leader) MPI_Send(&new_tag_in_world, 1, MPI_INT, remote_leader, tag, peer_comm); |
---|
134 | } |
---|
135 | else |
---|
136 | { |
---|
137 | if(ep_rank == local_leader) |
---|
138 | { |
---|
139 | MPI_Status status; |
---|
140 | MPI_Recv(&new_tag_in_world, 1, MPI_INT, remote_leader, tag, peer_comm, &status); |
---|
141 | } |
---|
142 | MPI_Bcast(&new_tag_in_world, 1, MPI_INT, new_local_leader, local_comm); |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | if(ep_rank == new_local_leader) |
---|
147 | { |
---|
148 | ::MPI_Comm_rank(to_mpi_comm(MPI_COMM_WORLD->mpi_comm), &leader_in_world[0]); |
---|
149 | } |
---|
150 | |
---|
151 | MPI_Bcast(&leader_in_world[0], 1, MPI_INT, new_local_leader, local_comm); |
---|
152 | |
---|
153 | |
---|
154 | if(ep_rank == local_leader) |
---|
155 | { |
---|
156 | MPI_Request request[2]; |
---|
157 | MPI_Status status[2]; |
---|
158 | |
---|
159 | MPI_Isend(&leader_in_world[0], 1, MPI_INT, remote_leader, tag, peer_comm, &request[0]); |
---|
160 | MPI_Irecv(&leader_in_world[1], 1, MPI_INT, remote_leader, tag, peer_comm, &request[1]); |
---|
161 | |
---|
162 | MPI_Waitall(2, request, status); |
---|
163 | } |
---|
164 | |
---|
165 | MPI_Bcast(&leader_in_world[1], 1, MPI_INT, local_leader, local_comm); |
---|
166 | |
---|
167 | local_comm->ep_comm_ptr->comm_label = tag; |
---|
168 | |
---|
169 | if(ep_rank == local_leader) printf("calling MPI_Intercomm_create_from_world\n"); |
---|
170 | |
---|
171 | 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); |
---|
172 | |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | if(ep_rank == local_leader) printf("calling MPI_Intercomm_create_kernel\n"); |
---|
177 | |
---|
178 | return MPI_Intercomm_create_kernel(local_comm, local_leader, peer_comm, remote_leader, tag, newintercomm); |
---|
179 | |
---|
180 | } |
---|
181 | |
---|
182 | int MPI_Comm_test_inter(MPI_Comm comm, int *flag) |
---|
183 | { |
---|
184 | *flag = false; |
---|
185 | if(comm->is_ep) |
---|
186 | { |
---|
187 | *flag = comm->is_intercomm; |
---|
188 | return 0; |
---|
189 | } |
---|
190 | else if(comm->mpi_comm != static_cast< ::MPI_Comm*>(MPI_COMM_NULL->mpi_comm)) |
---|
191 | { |
---|
192 | ::MPI_Comm mpi_comm = to_mpi_comm(comm->mpi_comm); |
---|
193 | |
---|
194 | ::MPI_Comm_test_inter(mpi_comm, flag); |
---|
195 | return 0; |
---|
196 | } |
---|
197 | return 0; |
---|
198 | } |
---|
199 | |
---|
200 | |
---|
201 | } |
---|