1 #ifndef MPQC_MPI_COMM_HPP
2 #define MPQC_MPI_COMM_HPP
4 #include "mpqc/mpi.hpp"
5 #include "mpqc/utility/string.hpp"
25 this->rank_ = comm.rank();
27 std::ostream& stream()
const {
28 std::cout << rank_ <<
": ";
37 void printf(std::string fmt, ...)
const {
41 vfprintf(stdout, fmt.c_str(), args);
50 static MPI::Comm dup(MPI::Comm comm) {
return Comm(); }
51 bool operator==(
const Comm &comm)
const {
return true; }
53 int rank()
const {
return 0; }
54 int size()
const {
return 1; }
55 void barrier()
const {}
57 Comm() : cout(*this) {}
65 static MPI::Comm Self() {
66 return Comm(MPI_COMM_SELF);
69 static MPI::Comm World() {
70 return Comm(MPI_COMM_WORLD);
74 static MPI::Comm dup(MPI::Comm comm) {
76 MPI_Comm_dup(comm, &dup);
83 MPI_Comm_free(&this->comm_);
86 explicit Comm(MPI_Comm comm)
87 : comm_(comm), cout(*this) {}
89 operator MPI_Comm()
const {
93 bool operator==(
const Comm &comm)
const {
94 return this->comm_ == comm.comm_;
100 MPI_Comm_rank(comm_, &rank);
107 MPI_Comm_size(comm_, &size);
111 void barrier()
const {
113 MPI_Barrier(this->comm_);
116 MPI_Status recv(
void *data,
int count, MPI_Datatype type,
117 int src,
int tag)
const {
122 MPI_Recv(data, count, type, src, tag, comm_, &status);
126 template <
typename T>
127 T recv(
int src,
int tag)
const {
129 MPI::Comm::recv(&data,
sizeof(T), MPI_BYTE, src, tag);
133 void send(
const void *data,
int count, MPI_Datatype type,
134 int dst,
int tag)
const {
138 MPI_Send((
void*)data, count, type, dst, tag, comm_);
141 template <
typename T>
142 void send(
const T &data,
int dst,
int tag)
const {
143 MPI::Comm::send((
void*)&data,
sizeof(T), MPI_BYTE, dst, tag);
147 void ssend(
const void *data,
int count, MPI_Datatype type,
148 int dst,
int tag)
const {
152 MPI_Ssend((
void*)data, count, type, dst, tag, comm_);
155 template <
typename T>
156 void ssend(
const T &data,
int dst,
int tag)
const {
157 MPI::Comm::ssend((
void*)&data,
sizeof(T), MPI_BYTE, dst, tag);
160 MPI_Request irecv(
void *data,
int count, MPI_Datatype type,
161 int src,
int tag)
const {
165 MPI_Irecv(data, count, type, src, tag, comm_, &request);
169 MPI_Request isend(
const void *data,
int count, MPI_Datatype type,
170 int src,
int tag)
const {
174 MPI_Isend((
void*)data, count, type, src, tag, comm_, &request);
184 template <
typename T>
185 void broadcast(T &value,
int root)
const {
187 MPI_Bcast(&value,
sizeof(T), MPI_BYTE, root, comm_);
191 template <
typename T>
192 void broadcast(T *data,
int count,
int root)
const {
194 MPI_Bcast(data,
sizeof(T)*count, MPI_BYTE, root, comm_);
198 bool any(
const bool &value)
const {
201 MPI_Allreduce(MPI_IN_PLACE, &result, 1, MPI_INT, MPI_LOR, comm_);
206 template <
typename T>
207 void sum(T *value,
int count)
const {
209 MPI_Allreduce(MPI_IN_PLACE, value, count, MPI::type<T>(), MPI_SUM, comm_);
213 template <
typename T>
214 void sum(T &value)
const {
218 template <
typename T>
219 std::vector<T> allgather(T value)
const {
220 int bytes =
sizeof(T);
221 std::vector<T> data(this->size(), T());
223 MPI_Allgather(&value, bytes, MPI_BYTE, &data[0], bytes, MPI_BYTE, comm_);
233 template <
typename T>
234 std::ostream&
operator<<(
const MPI::Comm::OStream &s,
const T &t) {
243 #endif // MPQC_MPI_COMM_HPP