1 | /*! |
---|
2 | \file ep_gather.cpp |
---|
3 | \since 2 may 2016 |
---|
4 | |
---|
5 | \brief Definitions of MPI collective function: MPI_Gather, MPI_Allgather |
---|
6 | */ |
---|
7 | |
---|
8 | #include "ep_lib.hpp" |
---|
9 | #include <mpi.h> |
---|
10 | #include "ep_declaration.hpp" |
---|
11 | #include <string.h> |
---|
12 | #include "ep_mpi.hpp" |
---|
13 | |
---|
14 | using namespace std; |
---|
15 | |
---|
16 | namespace ep_lib |
---|
17 | { |
---|
18 | |
---|
19 | int MPI_Allgather(const void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm) |
---|
20 | { |
---|
21 | |
---|
22 | if(!comm.is_ep && comm.mpi_comm) |
---|
23 | { |
---|
24 | ::MPI_Allgather(const_cast<void*>(sendbuf), sendcount, static_cast< ::MPI_Datatype>(sendtype), recvbuf, recvcount, static_cast< ::MPI_Datatype>(recvtype), |
---|
25 | static_cast< ::MPI_Comm>(comm.mpi_comm)); |
---|
26 | return 0; |
---|
27 | } |
---|
28 | |
---|
29 | if(!comm.mpi_comm) return 0; |
---|
30 | |
---|
31 | assert(sendcount == recvcount); |
---|
32 | |
---|
33 | assert(valid_type(sendtype) && valid_type(recvtype)); |
---|
34 | |
---|
35 | MPI_Datatype datatype = sendtype; |
---|
36 | int count = sendcount; |
---|
37 | |
---|
38 | ::MPI_Aint datasize, lb; |
---|
39 | |
---|
40 | ::MPI_Type_get_extent(to_mpi_type(datatype), &lb, &datasize); |
---|
41 | |
---|
42 | |
---|
43 | int ep_rank = comm.ep_comm_ptr->size_rank_info[0].first; |
---|
44 | int ep_rank_loc = comm.ep_comm_ptr->size_rank_info[1].first; |
---|
45 | int mpi_rank = comm.ep_comm_ptr->size_rank_info[2].first; |
---|
46 | int ep_size = comm.ep_comm_ptr->size_rank_info[0].second; |
---|
47 | int num_ep = comm.ep_comm_ptr->size_rank_info[1].second; |
---|
48 | int mpi_size = comm.ep_comm_ptr->size_rank_info[2].second; |
---|
49 | |
---|
50 | bool is_master = ep_rank_loc==0; |
---|
51 | |
---|
52 | void* local_recvbuf; |
---|
53 | void* tmp_recvbuf; |
---|
54 | |
---|
55 | |
---|
56 | if(is_master) |
---|
57 | { |
---|
58 | local_recvbuf = new void*[datasize * num_ep * count]; |
---|
59 | tmp_recvbuf = new void*[datasize * count * ep_size]; |
---|
60 | } |
---|
61 | |
---|
62 | MPI_Gather_local(sendbuf, count, datatype, local_recvbuf, 0, comm); |
---|
63 | |
---|
64 | |
---|
65 | int* mpi_recvcounts; |
---|
66 | int *mpi_displs; |
---|
67 | |
---|
68 | if(is_master) |
---|
69 | { |
---|
70 | |
---|
71 | mpi_recvcounts = new int[mpi_size]; |
---|
72 | mpi_displs = new int[mpi_size]; |
---|
73 | |
---|
74 | int local_sendcount = num_ep * count; |
---|
75 | |
---|
76 | ::MPI_Allgather(&local_sendcount, 1, to_mpi_type(MPI_INT), mpi_recvcounts, 1, to_mpi_type(MPI_INT), to_mpi_comm(comm.mpi_comm)); |
---|
77 | |
---|
78 | mpi_displs[0] = 0; |
---|
79 | for(int i=1; i<mpi_size; i++) |
---|
80 | { |
---|
81 | mpi_displs[i] = mpi_displs[i-1] + mpi_recvcounts[i-1]; |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | ::MPI_Allgatherv(local_recvbuf, num_ep * count, to_mpi_type(datatype), tmp_recvbuf, mpi_recvcounts, mpi_displs, to_mpi_type(datatype), to_mpi_comm(comm.mpi_comm)); |
---|
86 | |
---|
87 | |
---|
88 | // reorder |
---|
89 | int offset; |
---|
90 | for(int i=0; i<ep_size; i++) |
---|
91 | { |
---|
92 | offset = mpi_displs[comm.rank_map->at(i).second] + comm.rank_map->at(i).first * sendcount; |
---|
93 | memcpy(recvbuf + i*sendcount*datasize, tmp_recvbuf+offset*datasize, sendcount*datasize); |
---|
94 | } |
---|
95 | |
---|
96 | delete[] mpi_recvcounts; |
---|
97 | delete[] mpi_displs; |
---|
98 | } |
---|
99 | |
---|
100 | MPI_Bcast_local(recvbuf, count*ep_size, datatype, 0, comm); |
---|
101 | |
---|
102 | MPI_Barrier(comm); |
---|
103 | |
---|
104 | |
---|
105 | if(is_master) |
---|
106 | { |
---|
107 | delete[] local_recvbuf; |
---|
108 | delete[] tmp_recvbuf; |
---|
109 | |
---|
110 | } |
---|
111 | |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | |
---|
116 | } |
---|