46 #ifndef _PEXSI_MPI_HPP_
47 #define _PEXSI_MPI_HPP_
65 std::vector<Int>& localVec,
66 std::vector<Int>& allVec,
75 std::vector<T>& localVec,
76 std::vector<T>& allVec,
81 MPI_Comm_rank( comm, &mpirank );
82 MPI_Comm_size( comm, &mpisize );
84 Int localSize = localVec.size()*
sizeof(T);
85 std::vector<Int> localSizeVec( mpisize );
86 MPI_Gather( &localSize, 1, MPI_INT, &localSizeVec[0], 1, MPI_INT,root, comm );
89 std::vector<Int> localSizeDispls( mpisize );
90 localSizeDispls[0] = 0;
91 for( Int ip = 1; ip < mpisize; ip++ ){
92 localSizeDispls[ip] = localSizeDispls[ip-1] + localSizeVec[ip-1]*
sizeof(T);
94 Int totalSize = localSizeDispls[mpisize-1] + localSizeVec[mpisize-1]*
sizeof(T);
97 allVec.resize( totalSize /
sizeof(T) );
99 MPI_Gatherv( &localVec[0], localSize, MPI_BYTE, &allVec[0],
100 &localSizeVec[0], &localSizeDispls[0], MPI_BYTE, root, comm );
103 MPI_Gatherv( &localVec[0], localSize, MPI_BYTE, NULL,
104 NULL, NULL, MPI_INT, root, comm );
113 std::vector<Int>& localVec,
114 std::vector<Int>& allVec,
115 std::vector<Int>& sizes,
116 std::vector<Int>& displs,
124 std::vector<T>& localVec,
125 std::vector<T>& allVec,
126 std::vector<T>& sizes,
127 std::vector<T>& displs,
131 Int mpirank, mpisize;
132 MPI_Comm_rank( comm, &mpirank );
133 MPI_Comm_size( comm, &mpisize );
135 Int localSize = localVec.size()*
sizeof(T);
138 std::vector<Int> & localSizeVec = sizes;
139 localSizeVec.resize( mpisize );
140 MPI_Gather( &localSize, 1, MPI_INT, &localSizeVec[0], 1, MPI_INT,root, comm );
141 std::vector<Int> & localSizeDispls = displs;
142 localSizeDispls.resize( mpisize );
143 localSizeDispls[0] = 0;
144 for( Int ip = 1; ip < mpisize; ip++ ){
145 localSizeDispls[ip] = localSizeDispls[ip-1] + localSizeVec[ip-1]*
sizeof(T);
147 Int totalSize = localSizeDispls[mpisize-1] + localSizeVec[mpisize-1]*
sizeof(T);
150 allVec.resize( totalSize /
sizeof(T) );
152 MPI_Gatherv( &localVec[0], localSize, MPI_BYTE, &allVec[0],
153 &localSizeVec[0], &localSizeDispls[0], MPI_BYTE, root, comm );
156 MPI_Gather( &localSize, 1, MPI_INT, NULL, 1, MPI_INT,root, comm );
157 MPI_Gatherv( &localVec[0], localSize, MPI_BYTE, NULL,
158 NULL, NULL, MPI_INT, root, comm );
174 std::vector<Int>& localVec,
175 std::vector<Int>& allVec,
182 template <
typename T>
185 std::vector<T>& localVec,
186 std::vector<T>& allVec,
189 Int mpirank, mpisize;
190 MPI_Comm_rank( comm, &mpirank );
191 MPI_Comm_size( comm, &mpisize );
193 Int localSize = localVec.size()*
sizeof(T);
194 std::vector<Int> localSizeVec( mpisize );
195 std::vector<Int> localSizeDispls( mpisize );
196 MPI_Allgather( &localSize, 1, MPI_INT, &localSizeVec[0], 1, MPI_INT, comm );
197 localSizeDispls[0] = 0;
198 for( Int ip = 1; ip < mpisize; ip++ ){
199 localSizeDispls[ip] = (localSizeDispls[ip-1] + localSizeVec[ip-1])*
sizeof(T);
201 Int totalSize = (localSizeDispls[mpisize-1] + localSizeVec[mpisize-1])*
sizeof(T);
204 allVec.resize( totalSize/
sizeof(T) );
206 MPI_Allgatherv( &localVec[0], localSize, MPI_BYTE, &allVec[0],
207 &localSizeVec[0], &localSizeDispls[0], MPI_BYTE, comm );
218 template <
typename T>
221 std::vector<T>& dataVec,
225 Int mpirank, mpisize;
226 MPI_Comm_rank( comm, &mpirank );
227 MPI_Comm_size( comm, &mpisize );
229 Int localSize = dataVec.size();
230 MPI_Bcast( &localSize,
sizeof(localSize), MPI_BYTE, root , comm );
234 dataVec.resize(localSize);
237 MPI_Bcast( &dataVec[0], localSize*
sizeof(T), MPI_BYTE, root , comm );
259 void Send( std::stringstream& sstm, Int dest, Int tagSize, Int tagContent,
262 void Recv ( std::stringstream& sstm, Int src, Int tagSize, Int tagContent,
263 MPI_Comm comm, MPI_Status& statSize, MPI_Status& statContent );
265 void Recv ( std::stringstream& sstm, Int src, Int tagSize, Int tagContent,
273 Wait ( MPI_Request& req );
276 Waitall ( std::vector<MPI_Request>& reqs, std::vector<MPI_Status>& stats );
279 Waitall ( std::vector<MPI_Request>& reqs );
286 Reduce ( Real* sendbuf, Real* recvbuf, Int count, MPI_Op op, Int root, MPI_Comm comm );
289 Reduce ( Complex* sendbuf, Complex* recvbuf, Int count, MPI_Op op, Int root, MPI_Comm comm );
294 Ireduce ( Real* sendbuf, Real* recvbuf, Int count, MPI_Op op, Int root, MPI_Comm comm, MPI_Request & request );
297 Ireduce ( Complex* sendbuf, Complex* recvbuf, Int count, MPI_Op op, Int root, MPI_Comm comm, MPI_Request & request );
301 Allreduce ( Int* sendbuf, Int* recvbuf, Int count, MPI_Op op, MPI_Comm comm );
304 Allreduce ( Real* sendbuf, Real* recvbuf, Int count, MPI_Op op, MPI_Comm comm );
307 Allreduce ( Complex* sendbuf, Complex* recvbuf, Int count, MPI_Op op, MPI_Comm comm );
314 Alltoallv ( Int *bufSend, Int *sizeSend, Int *displsSend,
315 Int *bufRecv, Int *sizeRecv,
316 Int *displsRecv, MPI_Comm comm );
319 Alltoallv ( Real *bufSend, Int *sizeSend, Int *displsSend,
320 Real *bufRecv, Int *sizeRecv,
321 Int *displsRecv, MPI_Comm comm );
324 Alltoallv ( Complex *bufSend, Int *sizeSend, Int *displsSend,
325 Complex *bufRecv, Int *sizeRecv,
326 Int *displsRecv, MPI_Comm comm );
335 #endif // _PEXSI_MPI_HPP_