-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.hpp
195 lines (170 loc) · 4.48 KB
/
common.hpp
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
#pragma once
#include <chrono>
#include <thread>
#include <vector>
#include <fstream>
#include <mpi.h>
#include <boost/filesystem.hpp>
#include "logging.hpp"
namespace mp {
enum ParticipantType {
A,
B
};
enum CommunicatorType {
single,
many
};
enum PublishingType {
file,
server
};
void sleep(int ms) {
std::this_thread::sleep_for(std::chrono::milliseconds(ms));
}
int getCommSize(MPI_Comm comm = MPI_COMM_WORLD)
{
int size = -1;
MPI_Comm_size(comm, &size);
return size;
}
int getRemoteCommSize(MPI_Comm comm)
{
int size = -1;
MPI_Comm_remote_size(comm, &size);
return size;
}
int getCommRank(MPI_Comm comm = MPI_COMM_WORLD)
{
int rank = -1;
MPI_Comm_rank(comm, &rank);
return rank;
}
void removeDir(boost::filesystem::path path, MPI_Comm comm = MPI_COMM_WORLD)
{
if (getCommRank(comm) == 0) {
boost::filesystem::remove_all(path);
}
MPI_Barrier(comm);
}
/// Aquires a port name from MPI
std::string openPort()
{
std::string p = std::string(MPI_MAX_PORT_NAME, '\0');
MPI_Open_port(MPI_INFO_NULL, const_cast<char *>(p.data()));
DEBUG << "Opened port: " << p;
return p;
}
/// Writes port to file
void writePort(boost::filesystem::path path, std::string const & portName)
{
DEBUG << "Writing portname " << portName << " to " << path;
create_directory(path.parent_path());
std::ofstream ofs(path.string(), std::ofstream::out);
ofs << portName;
ofs.flush();
}
/// Writes port to nameserver
void writePort(std::string const & serviceName, std::string const & portName)
{
DEBUG << "Publishing portname " << portName << " as name " << serviceName;
MPI_Publish_name(serviceName.c_str(), MPI_INFO_NULL, portName.c_str());
}
/// Reads port from a file
std::string readPort(boost::filesystem::path path)
{
std::ifstream ifs;
do {
ifs.open(path.string(), std::ifstream::in);
std::this_thread::yield();
} while (not ifs);
// sleep(10);
std::string portName;
ifs >> portName;
std::getline(ifs, portName);
if (portName == "")
ERROR << "READ EMPTY PORT NAME FROM " << path.string();
portName.resize(MPI_MAX_PORT_NAME, '\0');
DEBUG << "Read address " << portName << " from " << path;
return portName;
}
/// Reads port from a nameserver
std::string readPort(std::string const & name)
{
char p[MPI_MAX_PORT_NAME];
DEBUG << "Looking up address at service name " << name;
MPI_Lookup_name(name.c_str(), MPI_INFO_NULL, p);
DEBUG << "Looked up address " << p;
return p;
}
/// Ranks to connect to
/*
* @param[in] peers Number of peers
* @param[in] size Size of participant we connect to
* @param[in] rank My own rank
* @returns Sorted list of ranks
*/
std::vector<int> getRanks(int peers, int size, int rank)
{
std::vector<int> ranks;
ranks.push_back(rank);
int acc1 = rank, acc2 = rank;
for (int i = 0; i < peers; ++i) {
if (acc1 > 0)
ranks.push_back(--acc1);
if (acc2 < size - 1)
ranks.push_back(++acc2);
}
ranks.resize( std::min(peers, size) );
std::sort(ranks.begin(), ranks.end());
return ranks;
}
/// Ranks to connect to
/*
* @param[in] peers Ratio or number of peers to connect
* @param[in] size Size of participant we connect to
* @param[in] rank My own rank
* @returns Sorted list of ranks
*/
std::vector<int> getRanks(double peers, int size, int rank)
{
if (peers < 1)
return getRanks(static_cast<int>(std::round(size * peers)), size, rank);
else
return getRanks(static_cast<int>(peers), size, rank);
}
/// Inverts getRanks, i.e. who connects to me (rank)?
std::vector<int> invertGetRanks(double peers, int size, int rank)
{
std::vector<int> igr;
for (int r = 0; r < size; ++r) {
auto rs = getRanks(peers, size, r);
if (std::find(rs.begin(), rs.end(), rank) != rs.end())
igr.push_back(r);
}
return igr;
}
/// Creates an intercom on rank 0 that can be used to synchronize both particpants
MPI_Comm createSyncIcomm(ParticipantType p,
boost::filesystem::path publishDirectory,
MPI_Comm comm)
{
int rank;
MPI_Comm_rank(comm, &rank);
if (rank != 0)
return MPI_COMM_NULL;
MPI_Comm syncComm;
if (p == A) {
auto port = openPort();
writePort(publishDirectory / "sync-intercomm.address", port);
MPI_Comm_accept(port.c_str(), MPI_INFO_NULL, 0, MPI_COMM_SELF, &syncComm);
}
if (p == B) {
sleep(100);
auto port = readPort(publishDirectory / "sync-intercomm.address");
MPI_Comm_connect(port.c_str(), MPI_INFO_NULL, 0, MPI_COMM_SELF, &syncComm);
}
DEBUG << "Created sync intercomm";
return syncComm;
}
}