mptensor v0.5.1
Parallel Library for Tensor Network Methods
Loading...
Searching...
No Matches
mpi_wrapper.hpp
Go to the documentation of this file.
1/*
2 mptensor - Parallel Library for Tensor Network Methods
3
4 Copyright 2016 Satoshi Morita
5
6 mptensor is free software: you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation, either version 3 of the
9 License, or (at your option) any later version.
10
11 mptensor is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with mptensor. If not, see
18 <https://www.gnu.org/licenses/>.
19*/
20
31#ifndef _MPI_WRAPPER_HPP_
32#define _MPI_WRAPPER_HPP_
33
34#ifndef _NO_MPI
35
36#include <mpi.h>
37
38#include <vector>
39
40#include "mptensor/complex.hpp"
41
42namespace mptensor {
43namespace mpi {
44
46template <typename C>
48
50// clang-format off
51template <> inline MPI_Datatype mpi_datatype<char> = MPI_CHAR;
54template <> inline MPI_Datatype mpi_datatype<short> = MPI_SHORT;
56template <> inline MPI_Datatype mpi_datatype<int> = MPI_INT;
64// clang-format on
66
68
74template <typename C>
75inline C allreduce_sum(C val, const MPI_Comm& comm) {
76 C recv;
78 return recv;
79};
80
82
88template <typename C>
89inline std::vector<C> allreduce_vec(const std::vector<C>& vec,
90 const MPI_Comm& comm) {
91 size_t n = vec.size();
92 std::vector<C> recv(n);
93 MPI_Allreduce(const_cast<C*>(vec.data()), recv.data(), static_cast<int>(n),
95 return recv;
96};
97
99
106template <typename C>
107inline void allreduce(const C* sendbuf, C* recvbuf, int count, MPI_Op op,
108 const MPI_Comm& comm) {
110 comm);
111};
112
114
125template <typename C>
126inline void sendrecv(const C* sendbuf, int sendcount, int dest, int sendtag,
127 C* recvbuf, int recvcount, int source, int recvtag,
128 const MPI_Comm& comm) {
131 comm, MPI_STATUS_IGNORE);
132};
133
135
144template <typename C>
145inline void sendrecv(const std::vector<C>& send_vec, int dest, int sendtag,
146 std::vector<C>& recv_vec, int source, int recvtag,
147 const MPI_Comm& comm) {
148 MPI_Sendrecv(const_cast<C*>(send_vec.data()),
149 static_cast<int>(send_vec.size()), mpi_datatype<C>, dest,
150 sendtag, recv_vec.data(), static_cast<int>(recv_vec.size()),
152};
153
155
162template <typename C>
163inline void alltoall(const C* sendbuf, int sendcount, C* recvbuf, int recvcount,
164 const MPI_Comm& comm) {
167};
168
170
184template <typename C>
185inline void alltoallv(const C* sendbuf, const int* sendcounts,
186 const int* sdispls, C* recvbuf, const int* recvcounts,
187 const int* rdispls, const MPI_Comm& comm) {
188 MPI_Alltoallv(const_cast<C*>(sendbuf), const_cast<int*>(sendcounts),
189 const_cast<int*>(sdispls), mpi_datatype<C>, recvbuf,
190 const_cast<int*>(recvcounts), const_cast<int*>(rdispls),
191 mpi_datatype<C>, comm);
192};
193
195
201template <typename C>
202inline void bcast(C* buffer, int count, int root, const MPI_Comm& comm) {
204};
205
206} // namespace mpi
207} // namespace mptensor
208
209#endif // _NO_MPI
210#endif // _MPI_WRAPPER_HPP_
Define the type of a complex number.
std::complex< double > complex
Definition complex.hpp:38
void alltoallv(const C *sendbuf, const int *sendcounts, const int *sdispls, C *recvbuf, const int *recvcounts, const int *rdispls, const MPI_Comm &comm)
Wrapper of MPI_Alltoallv.
Definition mpi_wrapper.hpp:185
std::vector< C > allreduce_vec(const std::vector< C > &vec, const MPI_Comm &comm)
Calculate a summation of each element of vector over MPI communicator.
Definition mpi_wrapper.hpp:89
C allreduce_sum(C val, const MPI_Comm &comm)
Calculate a summation over MPI communicator.
Definition mpi_wrapper.hpp:75
void bcast(C *buffer, int count, int root, const MPI_Comm &comm)
Wrapper of MPI_Bcast.
Definition mpi_wrapper.hpp:202
void sendrecv(const C *sendbuf, int sendcount, int dest, int sendtag, C *recvbuf, int recvcount, int source, int recvtag, const MPI_Comm &comm)
Wrapper of MPI_Sendrecv.
Definition mpi_wrapper.hpp:126
MPI_Datatype mpi_datatype
Template variable for MPI Datatype.
Definition mpi_wrapper.hpp:47
void allreduce(const C *sendbuf, C *recvbuf, int count, MPI_Op op, const MPI_Comm &comm)
Wrapper of MPI_Allreduce.
Definition mpi_wrapper.hpp:107
void alltoall(const C *sendbuf, int sendcount, C *recvbuf, int recvcount, const MPI_Comm &comm)
Wrapper of MPI_Alltoall.
Definition mpi_wrapper.hpp:163
Definition complex.hpp:34