-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathredev_bidirectional_comm.h
More file actions
206 lines (194 loc) · 7.34 KB
/
redev_bidirectional_comm.h
File metadata and controls
206 lines (194 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#ifndef REDEV_REDEV_BIDIRECTIONAL_COMM_H
#define REDEV_REDEV_BIDIRECTIONAL_COMM_H
#include "redev_assert.h"
#include "redev_comm.h"
#include <memory>
namespace redev {
/**
* A BidirectionalComm is a communicator that can send and receive data
* If you are on a client rank sending sends data to the server and receiving
* retrieves data from server.
* If you are on a server rank sending sends data to the client and receiving
* retrieves data from client.
*/
template <class T> class BidirectionalComm {
public:
BidirectionalComm() = default;
BidirectionalComm(std::unique_ptr<Communicator<T>> sender_,
std::unique_ptr<Communicator<T>> receiver_)
: sender(std::move(sender_)), receiver(std::move(receiver_)) {
REDEV_FUNCTION_TIMER;
REDEV_ALWAYS_ASSERT(sender != nullptr);
REDEV_ALWAYS_ASSERT(receiver != nullptr);
}
void SetOutMessageLayout(LOs &dest, LOs &offsets) {
REDEV_FUNCTION_TIMER;
REDEV_ALWAYS_ASSERT(sender != nullptr);
sender->SetOutMessageLayout(dest, offsets);
}
InMessageLayout GetInMessageLayout() {
REDEV_FUNCTION_TIMER;
REDEV_ALWAYS_ASSERT(receiver != nullptr);
return receiver->GetInMessageLayout();
}
void Send(T *msgs, Mode mode = Mode::Deferred) {
REDEV_FUNCTION_TIMER;
REDEV_ALWAYS_ASSERT(sender != nullptr);
sender->Send(msgs, mode);
}
std::vector<T> Recv(Mode mode = Mode::Deferred) {
REDEV_FUNCTION_TIMER;
REDEV_ALWAYS_ASSERT(receiver != nullptr);
return receiver->Recv(mode);
}
private:
std::unique_ptr<Communicator<T>> sender;
std::unique_ptr<Communicator<T>> receiver;
};
enum class CommunicatorDataType {
INT8,
INT16,
INT32,
INT64,
UINT8,
UINT16,
UINT32,
UINT64,
LONG_INT,
FLOAT,
DOUBLE,
LONG_DOUBLE,
COMPLEX_DOUBLE
};
namespace detail {
// we need to not include BidirectionalComm<long int> if long int == int64_t
// there may be a more sophisticated way to do this with a type list and
// appending, but it will require some boiler plate, so the quick and dirty
// method works reasonably well for now. We can revisit this if other types
// end up being aliased on other compilers
template <bool=false>
struct CommVTypes;
template <>
struct CommVTypes <false> {
using type = std::variant<BidirectionalComm<int8_t>, BidirectionalComm<int16_t>,
BidirectionalComm<int32_t>, BidirectionalComm<int64_t>,
BidirectionalComm<uint8_t>, BidirectionalComm<uint16_t>,
BidirectionalComm<uint32_t>, BidirectionalComm<uint64_t>,
BidirectionalComm<long int>, BidirectionalComm<float>,
BidirectionalComm<double>, BidirectionalComm<long double>,
BidirectionalComm<std::complex<double>>>;
};
template <>
struct CommVTypes <true> {
using type = std::variant<BidirectionalComm<int8_t>, BidirectionalComm<int16_t>,
BidirectionalComm<int32_t>, BidirectionalComm<int64_t>,
BidirectionalComm<uint8_t>, BidirectionalComm<uint16_t>,
BidirectionalComm<uint32_t>, BidirectionalComm<uint64_t>,
BidirectionalComm<float>,
BidirectionalComm<double>, BidirectionalComm<long double>,
BidirectionalComm<std::complex<double>>>;
};
}
// support types that adios supports.
// See: ADIOS2/bindings/C/adios2/c/adios2_c_internal.inl
using CommV = typename detail::CommVTypes<std::is_same_v<int64_t, long int>>::type;
template <CommunicatorDataType> struct CommunicatorTypeMap;
template <> struct CommunicatorTypeMap<CommunicatorDataType::INT8> {
using type = int8_t;
};
template <> struct CommunicatorTypeMap<CommunicatorDataType::INT16> {
using type = int16_t;
};
template <> struct CommunicatorTypeMap<CommunicatorDataType::INT32> {
using type = int32_t;
};
template <> struct CommunicatorTypeMap<CommunicatorDataType::INT64> {
using type = int64_t;
};
template <> struct CommunicatorTypeMap<CommunicatorDataType::UINT8> {
using type = uint8_t;
};
template <> struct CommunicatorTypeMap<CommunicatorDataType::UINT16> {
using type = uint16_t;
};
template <> struct CommunicatorTypeMap<CommunicatorDataType::UINT32> {
using type = uint32_t;
};
template <> struct CommunicatorTypeMap<CommunicatorDataType::UINT64> {
using type = uint64_t;
};
template <> struct CommunicatorTypeMap<CommunicatorDataType::LONG_INT> {
using type = long int;
};
template <> struct CommunicatorTypeMap<CommunicatorDataType::FLOAT> {
using type = float;
};
template <> struct CommunicatorTypeMap<CommunicatorDataType::DOUBLE> {
using type = double;
};
template <> struct CommunicatorTypeMap<CommunicatorDataType::LONG_DOUBLE> {
using type = long double;
};
template <> struct CommunicatorTypeMap<CommunicatorDataType::COMPLEX_DOUBLE> {
using type = std::complex<double>;
};
template <typename T, bool=false> struct InvCommunicatorTypeMap;
template <>
struct InvCommunicatorTypeMap<int8_t>
: std::integral_constant<CommunicatorDataType, CommunicatorDataType::INT8> {
};
template <>
struct InvCommunicatorTypeMap<int16_t>
: std::integral_constant<CommunicatorDataType,
CommunicatorDataType::INT16> {};
template <>
struct InvCommunicatorTypeMap<int32_t>
: std::integral_constant<CommunicatorDataType,
CommunicatorDataType::INT32> {};
template <>
struct InvCommunicatorTypeMap<int64_t>
: std::integral_constant<CommunicatorDataType,
CommunicatorDataType::INT64> {};
template <>
struct InvCommunicatorTypeMap<uint8_t>
: std::integral_constant<CommunicatorDataType,
CommunicatorDataType::UINT8> {};
template <>
struct InvCommunicatorTypeMap<uint16_t>
: std::integral_constant<CommunicatorDataType,
CommunicatorDataType::UINT16> {};
template <>
struct InvCommunicatorTypeMap<uint32_t>
: std::integral_constant<CommunicatorDataType,
CommunicatorDataType::UINT32> {};
template <>
struct InvCommunicatorTypeMap<uint64_t>
: std::integral_constant<CommunicatorDataType,
CommunicatorDataType::UINT64> {};
// on some compilers, int64_t is the smae as long int. In that case,
// the second template argument will be true and these functions
// will have different signatures. InvCommunicatorTypeMap<int64_t,true>,
// and InvCommunicatorTypeMap<int64_t,false>. By default the user will
// get the false one which corresponds to CommunicatorDataType::INT64
template <>
struct InvCommunicatorTypeMap<long int, std::is_same_v<int64_t, long int>>
: std::integral_constant<CommunicatorDataType,
CommunicatorDataType::LONG_INT> {};
template <>
struct InvCommunicatorTypeMap<float>
: std::integral_constant<CommunicatorDataType,
CommunicatorDataType::FLOAT> {};
template <>
struct InvCommunicatorTypeMap<double>
: std::integral_constant<CommunicatorDataType,
CommunicatorDataType::DOUBLE> {};
template <>
struct InvCommunicatorTypeMap<long double>
: std::integral_constant<CommunicatorDataType,
CommunicatorDataType::LONG_DOUBLE> {};
template <>
struct InvCommunicatorTypeMap<std::complex<double>>
: std::integral_constant<CommunicatorDataType,
CommunicatorDataType::COMPLEX_DOUBLE> {};
} // namespace redev
#endif // REDEV_REDEV_BIDIRECTIONAL_COMM_H