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