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_local(const void *sendbuf, int count, MPI_Datatype datatype, void *recvbuf, 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 (write_buffer) |
---|
30 | comm->my_buffer->void_buffer[ep_rank_loc] = const_cast< void* >(sendbuf); |
---|
31 | |
---|
32 | MPI_Barrier_local(comm); |
---|
33 | |
---|
34 | #pragma omp critical (read_buffer) |
---|
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 | |
---|
41 | int MPI_Allgather(const void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, int recvcount, MPI_Datatype recvtype, MPI_Comm comm) |
---|
42 | { |
---|
43 | |
---|
44 | if(!comm->is_ep && comm->mpi_comm) |
---|
45 | { |
---|
46 | return ::MPI_Allgather(const_cast<void*>(sendbuf), sendcount, to_mpi_type(sendtype), recvbuf, recvcount, to_mpi_type(recvtype), to_mpi_comm(comm->mpi_comm)); |
---|
47 | } |
---|
48 | |
---|
49 | assert(sendcount == recvcount); |
---|
50 | |
---|
51 | assert(valid_type(sendtype) && valid_type(recvtype)); |
---|
52 | |
---|
53 | MPI_Datatype datatype = sendtype; |
---|
54 | int count = sendcount; |
---|
55 | |
---|
56 | ::MPI_Aint datasize, lb; |
---|
57 | |
---|
58 | ::MPI_Type_get_extent(to_mpi_type(datatype), &lb, &datasize); |
---|
59 | |
---|
60 | |
---|
61 | int ep_rank = comm->ep_comm_ptr->size_rank_info[0].first; |
---|
62 | int ep_rank_loc = comm->ep_comm_ptr->size_rank_info[1].first; |
---|
63 | int mpi_rank = comm->ep_comm_ptr->size_rank_info[2].first; |
---|
64 | int ep_size = comm->ep_comm_ptr->size_rank_info[0].second; |
---|
65 | int num_ep = comm->ep_comm_ptr->size_rank_info[1].second; |
---|
66 | int mpi_size = comm->ep_comm_ptr->size_rank_info[2].second; |
---|
67 | |
---|
68 | bool is_master = ep_rank_loc==0; |
---|
69 | |
---|
70 | void* local_recvbuf; |
---|
71 | void* tmp_recvbuf; |
---|
72 | |
---|
73 | |
---|
74 | if(is_master) |
---|
75 | { |
---|
76 | local_recvbuf = new void*[datasize * num_ep * count]; |
---|
77 | tmp_recvbuf = new void*[datasize * count * ep_size]; |
---|
78 | } |
---|
79 | |
---|
80 | MPI_Gather_local(sendbuf, count, datatype, local_recvbuf, 0, comm); |
---|
81 | |
---|
82 | |
---|
83 | int* mpi_recvcounts; |
---|
84 | int *mpi_displs; |
---|
85 | |
---|
86 | if(is_master) |
---|
87 | { |
---|
88 | |
---|
89 | mpi_recvcounts = new int[mpi_size]; |
---|
90 | mpi_displs = new int[mpi_size]; |
---|
91 | |
---|
92 | int local_sendcount = num_ep * count; |
---|
93 | |
---|
94 | ::MPI_Allgather(&local_sendcount, 1, to_mpi_type(MPI_INT), mpi_recvcounts, 1, to_mpi_type(MPI_INT), to_mpi_comm(comm->mpi_comm)); |
---|
95 | |
---|
96 | mpi_displs[0] = 0; |
---|
97 | for(int i=1; i<mpi_size; i++) |
---|
98 | { |
---|
99 | mpi_displs[i] = mpi_displs[i-1] + mpi_recvcounts[i-1]; |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | ::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)); |
---|
104 | |
---|
105 | |
---|
106 | // reorder |
---|
107 | int offset; |
---|
108 | for(int i=0; i<ep_size; i++) |
---|
109 | { |
---|
110 | offset = mpi_displs[comm->ep_rank_map->at(i).second] + comm->ep_rank_map->at(i).first * sendcount; |
---|
111 | memcpy(recvbuf + i*sendcount*datasize, tmp_recvbuf+offset*datasize, sendcount*datasize); |
---|
112 | } |
---|
113 | |
---|
114 | delete[] mpi_recvcounts; |
---|
115 | delete[] mpi_displs; |
---|
116 | } |
---|
117 | |
---|
118 | MPI_Bcast_local(recvbuf, count*ep_size, datatype, 0, comm); |
---|
119 | |
---|
120 | MPI_Barrier(comm); |
---|
121 | |
---|
122 | |
---|
123 | if(is_master) |
---|
124 | { |
---|
125 | delete[] local_recvbuf; |
---|
126 | delete[] tmp_recvbuf; |
---|
127 | |
---|
128 | } |
---|
129 | |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | |
---|
134 | } |
---|