[1646] | 1 | #ifdef _usingEP |
---|
[1603] | 2 | /*! |
---|
| 3 | \file ep_gather.cpp |
---|
| 4 | \since 2 may 2016 |
---|
| 5 | |
---|
| 6 | \brief Definitions of MPI collective function: MPI_Gather, MPI_Allgather |
---|
| 7 | */ |
---|
| 8 | |
---|
| 9 | #include "ep_lib.hpp" |
---|
| 10 | #include <mpi.h> |
---|
| 11 | #include "ep_declaration.hpp" |
---|
| 12 | #include "ep_mpi.hpp" |
---|
| 13 | |
---|
| 14 | using namespace std; |
---|
| 15 | |
---|
| 16 | namespace ep_lib |
---|
| 17 | { |
---|
| 18 | |
---|
| 19 | int MPI_Gather_local(const void *sendbuf, int count, MPI_Datatype datatype, void *recvbuf, int local_root, MPI_Comm comm) |
---|
| 20 | { |
---|
| 21 | assert(valid_type(datatype)); |
---|
| 22 | |
---|
| 23 | ::MPI_Aint datasize, lb; |
---|
| 24 | ::MPI_Type_get_extent(to_mpi_type(datatype), &lb, &datasize); |
---|
| 25 | |
---|
| 26 | int ep_rank_loc = comm->ep_comm_ptr->size_rank_info[1].first; |
---|
| 27 | int num_ep = comm->ep_comm_ptr->size_rank_info[1].second; |
---|
| 28 | |
---|
| 29 | #pragma omp critical (_gather) |
---|
| 30 | comm->my_buffer->void_buffer[ep_rank_loc] = const_cast< void* >(sendbuf); |
---|
| 31 | |
---|
| 32 | MPI_Barrier_local(comm); |
---|
| 33 | |
---|
| 34 | if(ep_rank_loc == local_root) |
---|
| 35 | { |
---|
| 36 | for(int i=0; i<num_ep; i++) |
---|
| 37 | memcpy(recvbuf + datasize * i * count, comm->my_buffer->void_buffer[i], datasize * count); |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | MPI_Barrier_local(comm); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | int MPI_Gather(const void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm) |
---|
| 44 | { |
---|
| 45 | if(!comm->is_ep) return ::MPI_Gather(const_cast<void*>(sendbuf), sendcount, to_mpi_type(sendtype), recvbuf, recvcount, to_mpi_type(recvtype), |
---|
| 46 | root, to_mpi_comm(comm->mpi_comm)); |
---|
| 47 | if(comm->is_intercomm) return MPI_Gather_intercomm(sendbuf, sendcount, sendtype, recvbuf, recvcount, recvtype, root, comm); |
---|
| 48 | |
---|
| 49 | assert(sendcount == recvcount && sendtype == recvtype); |
---|
| 50 | |
---|
| 51 | int ep_rank = comm->ep_comm_ptr->size_rank_info[0].first; |
---|
| 52 | int ep_rank_loc = comm->ep_comm_ptr->size_rank_info[1].first; |
---|
| 53 | int mpi_rank = comm->ep_comm_ptr->size_rank_info[2].first; |
---|
| 54 | int ep_size = comm->ep_comm_ptr->size_rank_info[0].second; |
---|
| 55 | int num_ep = comm->ep_comm_ptr->size_rank_info[1].second; |
---|
| 56 | int mpi_size = comm->ep_comm_ptr->size_rank_info[2].second; |
---|
| 57 | |
---|
| 58 | int root_mpi_rank = comm->ep_rank_map->at(root).second; |
---|
| 59 | int root_ep_loc = comm->ep_rank_map->at(root).first; |
---|
| 60 | |
---|
| 61 | ::MPI_Aint datasize, lb; |
---|
| 62 | ::MPI_Type_get_extent(to_mpi_type(sendtype), &lb, &datasize); |
---|
| 63 | |
---|
| 64 | bool is_master = (ep_rank_loc==0 && mpi_rank != root_mpi_rank ) || ep_rank == root; |
---|
| 65 | bool is_root = ep_rank == root; |
---|
| 66 | |
---|
| 67 | void* local_recvbuf; |
---|
| 68 | |
---|
| 69 | if(is_master) |
---|
| 70 | { |
---|
| 71 | local_recvbuf = new void*[datasize * num_ep * sendcount]; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | void* tmp_recvbuf; |
---|
| 75 | if(is_root) tmp_recvbuf = new void*[datasize * recvcount * ep_size]; |
---|
| 76 | |
---|
| 77 | |
---|
| 78 | if(mpi_rank == root_mpi_rank) MPI_Gather_local(sendbuf, sendcount, sendtype, local_recvbuf, root_ep_loc, comm); |
---|
| 79 | else MPI_Gather_local(sendbuf, sendcount, sendtype, local_recvbuf, 0, comm); |
---|
| 80 | |
---|
| 81 | std::vector<int> recvcounts(mpi_size, 0); |
---|
| 82 | std::vector<int> displs(mpi_size, 0); |
---|
| 83 | |
---|
| 84 | |
---|
| 85 | if(is_master) |
---|
| 86 | { |
---|
| 87 | for(int i=0; i<ep_size; i++) |
---|
| 88 | { |
---|
| 89 | recvcounts[comm->ep_rank_map->at(i).second]+=sendcount; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | for(int i=1; i<mpi_size; i++) |
---|
| 93 | displs[i] = displs[i-1] + recvcounts[i-1]; |
---|
| 94 | |
---|
| 95 | ::MPI_Gatherv(local_recvbuf, sendcount*num_ep, to_mpi_type(sendtype), tmp_recvbuf, recvcounts.data(), displs.data(), to_mpi_type(recvtype), root_mpi_rank, to_mpi_comm(comm->mpi_comm)); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | // reorder data |
---|
| 100 | if(is_root) |
---|
| 101 | { |
---|
| 102 | int offset; |
---|
| 103 | for(int i=0; i<ep_size; i++) |
---|
| 104 | { |
---|
| 105 | offset = displs[comm->ep_rank_map->at(i).second] + comm->ep_rank_map->at(i).first * sendcount; |
---|
| 106 | memcpy(recvbuf + i*sendcount*datasize, tmp_recvbuf+offset*datasize, sendcount*datasize); |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | if(is_master) |
---|
| 115 | { |
---|
| 116 | delete[] local_recvbuf; |
---|
| 117 | } |
---|
| 118 | if(is_root) delete[] tmp_recvbuf; |
---|
| 119 | |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | int MPI_Gather_intercomm(const void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm) |
---|
| 124 | { |
---|
| 125 | printf("MPI_Gather_intercomm not yet implemented\n"); |
---|
| 126 | MPI_Abort(comm, 0); |
---|
| 127 | } |
---|
| 128 | } |
---|
[1646] | 129 | #endif |
---|