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 PushCallStack(
"mpi::Gatherv");
84 MPI_Comm_rank( comm, &mpirank );
85 MPI_Comm_size( comm, &mpisize );
87 Int localSize = localVec.size()*
sizeof(T);
88 std::vector<Int> localSizeVec( mpisize );
89 MPI_Gather( &localSize, 1, MPI_INT, &localSizeVec[0], 1, MPI_INT,root, comm );
92 std::vector<Int> localSizeDispls( mpisize );
93 localSizeDispls[0] = 0;
94 for( Int ip = 1; ip < mpisize; ip++ ){
95 localSizeDispls[ip] = localSizeDispls[ip-1] + localSizeVec[ip-1]*
sizeof(T);
97 Int totalSize = localSizeDispls[mpisize-1] + localSizeVec[mpisize-1]*
sizeof(T);
100 allVec.resize( totalSize /
sizeof(T) );
102 MPI_Gatherv( &localVec[0], localSize, MPI_BYTE, &allVec[0],
103 &localSizeVec[0], &localSizeDispls[0], MPI_BYTE, root, comm );
106 MPI_Gatherv( &localVec[0], localSize, MPI_BYTE, NULL,
107 NULL, NULL, MPI_INT, root, comm );
119 std::vector<Int>& localVec,
120 std::vector<Int>& allVec,
121 std::vector<Int>& sizes,
122 std::vector<Int>& displs,
130 std::vector<T>& localVec,
131 std::vector<T>& allVec,
132 std::vector<T>& sizes,
133 std::vector<T>& displs,
138 PushCallStack(
"mpi::Gatherv");
140 Int mpirank, mpisize;
141 MPI_Comm_rank( comm, &mpirank );
142 MPI_Comm_size( comm, &mpisize );
144 Int localSize = localVec.size()*
sizeof(T);
147 std::vector<Int> & localSizeVec = sizes;
148 localSizeVec.resize( mpisize );
149 MPI_Gather( &localSize, 1, MPI_INT, &localSizeVec[0], 1, MPI_INT,root, comm );
150 std::vector<Int> & localSizeDispls = displs;
151 localSizeDispls.resize( mpisize );
152 localSizeDispls[0] = 0;
153 for( Int ip = 1; ip < mpisize; ip++ ){
154 localSizeDispls[ip] = localSizeDispls[ip-1] + localSizeVec[ip-1]*
sizeof(T);
156 Int totalSize = localSizeDispls[mpisize-1] + localSizeVec[mpisize-1]*
sizeof(T);
159 allVec.resize( totalSize /
sizeof(T) );
161 MPI_Gatherv( &localVec[0], localSize, MPI_BYTE, &allVec[0],
162 &localSizeVec[0], &localSizeDispls[0], MPI_BYTE, root, comm );
165 MPI_Gather( &localSize, 1, MPI_INT, NULL, 1, MPI_INT,root, comm );
166 MPI_Gatherv( &localVec[0], localSize, MPI_BYTE, NULL,
167 NULL, NULL, MPI_INT, root, comm );
186 std::vector<Int>& localVec,
187 std::vector<Int>& allVec,
194 template <
typename T>
197 std::vector<T>& localVec,
198 std::vector<T>& allVec,
202 PushCallStack(
"mpi::Allgatherv");
204 Int mpirank, mpisize;
205 MPI_Comm_rank( comm, &mpirank );
206 MPI_Comm_size( comm, &mpisize );
208 Int localSize = localVec.size()*
sizeof(T);
209 std::vector<Int> localSizeVec( mpisize );
210 std::vector<Int> localSizeDispls( mpisize );
211 MPI_Allgather( &localSize, 1, MPI_INT, &localSizeVec[0], 1, MPI_INT, comm );
212 localSizeDispls[0] = 0;
213 for( Int ip = 1; ip < mpisize; ip++ ){
214 localSizeDispls[ip] = (localSizeDispls[ip-1] + localSizeVec[ip-1])*
sizeof(T);
216 Int totalSize = (localSizeDispls[mpisize-1] + localSizeVec[mpisize-1])*
sizeof(T);
219 allVec.resize( totalSize/
sizeof(T) );
221 MPI_Allgatherv( &localVec[0], localSize, MPI_BYTE, &allVec[0],
222 &localSizeVec[0], &localSizeDispls[0], MPI_BYTE, comm );
236 template <
typename T>
239 std::vector<T>& dataVec,
244 PushCallStack(
"mpi::Bcast");
246 Int mpirank, mpisize;
247 MPI_Comm_rank( comm, &mpirank );
248 MPI_Comm_size( comm, &mpisize );
250 Int localSize = dataVec.size();
251 MPI_Bcast( &localSize,
sizeof(localSize), MPI_BYTE, root , comm );
255 dataVec.resize(localSize);
258 MPI_Bcast( &dataVec[0], localSize*
sizeof(T), MPI_BYTE, root , comm );
283 void Send( std::stringstream& sstm, Int dest, Int tagSize, Int tagContent,
286 void Recv ( std::stringstream& sstm, Int src, Int tagSize, Int tagContent,
287 MPI_Comm comm, MPI_Status& statSize, MPI_Status& statContent );
289 void Recv ( std::stringstream& sstm, Int src, Int tagSize, Int tagContent,
297 Wait ( MPI_Request& req );
300 Waitall ( std::vector<MPI_Request>& reqs, std::vector<MPI_Status>& stats );
303 Waitall ( std::vector<MPI_Request>& reqs );
310 Reduce ( Real* sendbuf, Real* recvbuf, Int count, MPI_Op op, Int root, MPI_Comm comm );
313 Reduce ( Complex* sendbuf, Complex* recvbuf, Int count, MPI_Op op, Int root, MPI_Comm comm );
318 Ireduce ( Real* sendbuf, Real* recvbuf, Int count, MPI_Op op, Int root, MPI_Comm comm, MPI_Request & request );
321 Ireduce ( Complex* sendbuf, Complex* recvbuf, Int count, MPI_Op op, Int root, MPI_Comm comm, MPI_Request & request );
325 Allreduce ( Int* sendbuf, Int* recvbuf, Int count, MPI_Op op, MPI_Comm comm );
328 Allreduce ( Real* sendbuf, Real* recvbuf, Int count, MPI_Op op, MPI_Comm comm );
331 Allreduce ( Complex* sendbuf, Complex* recvbuf, Int count, MPI_Op op, MPI_Comm comm );
338 Alltoallv ( Int *bufSend, Int *sizeSend, Int *displsSend,
339 Int *bufRecv, Int *sizeRecv,
340 Int *displsRecv, MPI_Comm comm );
343 Alltoallv ( Real *bufSend, Int *sizeSend, Int *displsSend,
344 Real *bufRecv, Int *sizeRecv,
345 Int *displsRecv, MPI_Comm comm );
348 Alltoallv ( Complex *bufSend, Int *sizeSend, Int *displsSend,
349 Complex *bufRecv, Int *sizeRecv,
350 Int *displsRecv, MPI_Comm comm );
359 #endif // _PEXSI_MPI_HPP_