sisi4s
Loading...
Searching...
No Matches
MpiCommunicator.hpp
Go to the documentation of this file.
1#ifndef MPI_COMMUNICATOR_DEFINED
2#define MPI_COMMUNICATOR_DEFINED
3
4#include "mpi.h"
5#include <math/Complex.hpp>
6#include <math/Vector.hpp>
7#include <util/Tensor.hpp>
8
9namespace sisi4s {
10// base template for type traits
11template <typename F>
13
15public:
16 MpiCommunicator(int rank_, int processes_, MPI_Comm comm_ = MPI_COMM_WORLD)
17 : rank(rank_)
18 , processes(processes_)
19 , comm(comm_) {}
20 MpiCommunicator(const CTF::World &world)
21 : rank(world.rank)
22 , processes(world.np)
23 , comm(world.comm) {}
25
26 void barrier() { MPI_Barrier(comm); }
27
28 template <typename F>
29 void reduce(const F &src, F &dst, int rootRank = 0) {
30 MPI_Reduce(&src,
31 &dst,
34 MPI_SUM,
35 rootRank,
36 comm);
37 }
38
39 template <typename F>
40 void allReduce(const F &src, F &dst) {
41 MPI_Allreduce(&src,
42 &dst,
45 MPI_SUM,
46 comm);
47 }
48
53 template <typename F>
54 void
55 gather(const std::vector<F> &src, std::vector<F> &dst, int rootRank = 0) {
56 if (rank == rootRank) {
57 dst.resize(src.size() * processes);
58 } else {
59 dst.resize(0);
60 }
61 MPI_Gather(src.data(),
62 src.size() * MpiTypeTraits<F>::elementCount(),
64 dst.data(),
65 src.size() * MpiTypeTraits<F>::elementCount(),
67 rootRank,
68 comm);
69 }
70
71 int getRank() const { return rank; }
72 int getProcesses() const { return processes; }
73
74protected:
76 MPI_Comm comm;
77};
78
79template <>
80class MpiTypeTraits<int> {
81public:
82 static MPI_Datatype elementType() { return MPI_INT; }
83 static int elementCount() { return 1; }
84};
85
86template <>
87class MpiTypeTraits<int64_t> {
88public:
89 static MPI_Datatype elementType() { return MPI_INTEGER8; }
90 static int elementCount() { return 1; }
91};
92
93template <>
94class MpiTypeTraits<uint64_t> {
95public:
96 static MPI_Datatype elementType() { return MPI_INTEGER8; }
97 static int elementCount() { return 1; }
98};
99
100template <>
101class MpiTypeTraits<double> {
102public:
103 static MPI_Datatype elementType() { return MPI_REAL8; }
104 static int elementCount() { return 1; }
105};
106
107template <>
109public:
110 static MPI_Datatype elementType() { return MPI_DOUBLE_COMPLEX; }
111 static int elementCount() { return 1; }
112};
113
114template <typename F, int D>
115class MpiTypeTraits<Vector<F, D>> {
116public:
117 static MPI_Datatype elementType() { return MpiTypeTraits<F>::elementType(); }
118 static int elementCount() { return D; }
119};
120} // namespace sisi4s
121
122#endif
Definition MpiCommunicator.hpp:14
int getRank() const
Definition MpiCommunicator.hpp:71
void reduce(const F &src, F &dst, int rootRank=0)
Definition MpiCommunicator.hpp:29
MpiCommunicator(const CTF::World &world)
Definition MpiCommunicator.hpp:20
void gather(const std::vector< F > &src, std::vector< F > &dst, int rootRank=0)
Definition MpiCommunicator.hpp:55
MPI_Comm comm
Definition MpiCommunicator.hpp:76
~MpiCommunicator()
Definition MpiCommunicator.hpp:24
int getProcesses() const
Definition MpiCommunicator.hpp:72
MpiCommunicator(int rank_, int processes_, MPI_Comm comm_=MPI_COMM_WORLD)
Definition MpiCommunicator.hpp:16
void barrier()
Definition MpiCommunicator.hpp:26
int rank
Definition MpiCommunicator.hpp:75
int processes
Definition MpiCommunicator.hpp:75
void allReduce(const F &src, F &dst)
Definition MpiCommunicator.hpp:40
static MPI_Datatype elementType()
Definition MpiCommunicator.hpp:117
static int elementCount()
Definition MpiCommunicator.hpp:118
static int elementCount()
Definition MpiCommunicator.hpp:111
static MPI_Datatype elementType()
Definition MpiCommunicator.hpp:110
static MPI_Datatype elementType()
Definition MpiCommunicator.hpp:103
static int elementCount()
Definition MpiCommunicator.hpp:104
static int elementCount()
Definition MpiCommunicator.hpp:90
static MPI_Datatype elementType()
Definition MpiCommunicator.hpp:89
static MPI_Datatype elementType()
Definition MpiCommunicator.hpp:82
static int elementCount()
Definition MpiCommunicator.hpp:83
static int elementCount()
Definition MpiCommunicator.hpp:97
static MPI_Datatype elementType()
Definition MpiCommunicator.hpp:96
Definition MpiCommunicator.hpp:12
Definition Vector.hpp:9
Definition Algorithm.hpp:10
Complex< real > complex
Definition Complex.hpp:17