Skip to content

Commit 1900e26

Browse files
committed
balancing of mpi-based filtering
1 parent 9360f8e commit 1900e26

File tree

5 files changed

+418
-173
lines changed

5 files changed

+418
-173
lines changed

__dependency_graph.dot

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ digraph {
1111
}
1212
FctHelper -> iostream [color=blue]
1313
FctHelper -> math [color=blue]
14-
FctHelper -> mpi [color=blue]
1514
FctHelper -> algorithm [color=red]
1615
FctHelper -> array [color=red]
1716
FctHelper -> charconv [color=red]
@@ -22,6 +21,7 @@ digraph {
2221
FctHelper -> filesystem [color=red]
2322
FctHelper -> fstream [color=red]
2423
FctHelper -> map [color=red]
24+
FctHelper -> mpi [color=red]
2525
FctHelper -> set [color=red]
2626
FctHelper -> sstream [color=red]
2727
FctHelper -> string [color=red]
@@ -69,7 +69,6 @@ digraph {
6969
main -> cstring [color=blue]
7070
main -> ctime [color=blue]
7171
main -> iostream [color=blue]
72-
main -> mpi [color=blue]
7372
main -> unistd [color=blue]
7473
subgraph "cluster_D:/Dropbox/eclipse/pmGenerator" {
7574
main
@@ -149,7 +148,6 @@ digraph {
149148
DlProofEnumerator -> parallel_sort [color=blue]
150149
DlProofEnumerator -> cstring [color=blue]
151150
DlProofEnumerator -> iostream [color=blue]
152-
DlProofEnumerator -> mpi [color=blue]
153151
DlProofEnumerator -> FwdTbb [color=red]
154152
DlProofEnumerator -> ProgressData [color=red]
155153
DlProofEnumerator -> array [color=red]

helper/FctHelper.cpp

Lines changed: 73 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include <iostream>
44
#include <math.h>
5-
#include <mpi.h>
65

76
using namespace std;
87

@@ -33,98 +32,132 @@ template<typename T> void mpi_send(int rank, int count, MPI_Datatype type, const
3332
cout << rank << "->" << dest << ": Sending " << printer(val) << "." << endl;
3433
MPI_Send(val, count, type, dest, tag, MPI_COMM_WORLD);
3534
}
36-
template<typename T> T mpi_recvValue(int rank, MPI_Datatype type, int source, int tag, bool debug, auto printer) {
35+
template<typename T> T mpi_recvValue(int rank, MPI_Datatype type, int source, int tag, MPI_Status* status, bool debug, auto printer) {
3736
T val;
38-
MPI_Recv(&val, 1, type, source, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
37+
MPI_Recv(&val, 1, type, source, tag, MPI_COMM_WORLD, status);
3938
if (debug)
4039
cout << source << "->" << rank << ": Received " << printer(val) << "." << endl;
4140
return val;
4241
}
43-
template<typename T> bool mpi_tryRecvValue(int rank, MPI_Datatype type, int source, int tag, T& result, bool debug, auto printer) {
42+
template<typename T> bool mpi_tryRecvValue(int rank, MPI_Datatype type, int source, int tag, MPI_Status* status, T& result, bool debug, auto printer) {
4443
int flag;
45-
MPI_Iprobe(source, tag, MPI_COMM_WORLD, &flag, MPI_STATUS_IGNORE);
44+
MPI_Iprobe(source, tag, MPI_COMM_WORLD, &flag, status);
4645
if (flag) {
47-
MPI_Recv(&result, 1, type, source, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
46+
MPI_Recv(&result, 1, type, source, tag, MPI_COMM_WORLD, status);
4847
if (debug)
4948
cout << source << "->" << rank << ": Received " << printer(result) << "." << endl;
5049
}
5150
return flag;
5251
}
53-
template<typename T, size_t N> array<T, N> mpi_recvArray(int rank, MPI_Datatype type, int source, int tag, bool debug, auto printer) {
52+
template<typename T, size_t N> array<T, N> mpi_recvArray(int rank, MPI_Datatype type, int source, int tag, MPI_Status* status, bool debug, auto printer) {
5453
array<T, N> arr;
55-
MPI_Recv(arr.data(), N, type, source, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
54+
MPI_Recv(arr.data(), N, type, source, tag, MPI_COMM_WORLD, status);
5655
if (debug)
5756
cout << source << "->" << rank << ": Received " << printer(arr) << "." << endl;
5857
return arr;
5958
}
60-
template<typename T, size_t N> bool mpi_tryRecvArray(int rank, MPI_Datatype type, int source, int tag, array<T, N>& result, bool debug, auto printer) {
59+
template<typename T, size_t N> bool mpi_tryRecvArray(int rank, MPI_Datatype type, int source, int tag, MPI_Status* status, array<T, N>& result, bool debug, auto printer) {
6160
int flag;
62-
MPI_Iprobe(source, tag, MPI_COMM_WORLD, &flag, MPI_STATUS_IGNORE);
61+
MPI_Iprobe(source, tag, MPI_COMM_WORLD, &flag, status);
6362
if (flag) {
64-
MPI_Recv(result.data(), N, type, source, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
63+
MPI_Recv(result.data(), N, type, source, tag, MPI_COMM_WORLD, status);
6564
if (debug)
6665
cout << source << "->" << rank << ": Received " << printer(result) << "." << endl;
6766
}
6867
return flag;
6968
}
70-
template<typename T> ManagedArray<T> mpi_recvDynArray(int rank, MPI_Datatype type, int source, int tag, bool debug, auto printer) {
71-
MPI_Status status_recv;
72-
MPI_Probe(source, tag, MPI_COMM_WORLD, &status_recv);
69+
template<typename T> ManagedArray<T> mpi_recvDynArray(int rank, MPI_Datatype type, int source, int tag, MPI_Status* status, bool debug, auto printer) {
70+
MPI_Status status_probe;
71+
MPI_Probe(source, tag, MPI_COMM_WORLD, &status_probe);
7372
int size;
74-
MPI_Get_count(&status_recv, type, &size);
73+
MPI_Get_count(&status_probe, type, &size);
7574
if (debug)
7675
cout << source << "->" << rank << ": Will receive " << size << " elements." << endl;
7776
ManagedArray<T> arr(size);
78-
MPI_Recv(arr.data, size, type, source, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
77+
MPI_Recv(arr.data, size, type, source, tag, MPI_COMM_WORLD, status);
7978
if (debug)
8079
cout << source << "->" << rank << ": Received " << printer(arr) << "." << endl;
8180
return arr;
8281
}
83-
template<typename T> bool mpi_tryRecvDynArray(int rank, MPI_Datatype type, int source, int tag, ManagedArray<T>& result, bool debug, auto printer) {
82+
template<typename T> bool mpi_tryRecvDynArray(int rank, MPI_Datatype type, int source, int tag, MPI_Status* status, ManagedArray<T>& result, bool debug, auto printer) {
8483
int flag;
85-
MPI_Status status_recv;
86-
MPI_Iprobe(source, tag, MPI_COMM_WORLD, &flag, &status_recv);
84+
MPI_Status status_probe;
85+
MPI_Iprobe(source, tag, MPI_COMM_WORLD, &flag, &status_probe);
8786
if (flag) {
8887
int size;
89-
MPI_Get_count(&status_recv, type, &size);
88+
MPI_Get_count(&status_probe, type, &size);
9089
if (debug)
9190
cout << source << "->" << rank << ": Will receive " << size << " elements." << endl;
9291
result.alloc(size);
93-
MPI_Recv(result.data, size, type, source, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
92+
MPI_Recv(result.data, size, type, source, tag, MPI_COMM_WORLD, status);
9493
if (debug)
9594
cout << source << "->" << rank << ": Received " << printer(result) << "." << endl;
9695
}
9796
return flag;
9897
}
9998

100-
enum mpi_tag : int {
101-
mpi_tag_unspecified = 0, // not used by any helper function
102-
mpi_tag_bool,
103-
mpi_tag_string,
104-
mpi_tag_uint64,
105-
mpi_tag_uint64_pair
106-
};
99+
void FctHelper::mpi_sendBool(int rank, const bool b, int dest, int tag, bool debug) {
100+
char c(b);
101+
mpi_send(rank, 1, MPI_CHAR, &c, dest, tag, debug, [](const char* x) { return *x ? "true" : "false"; });
102+
}
103+
104+
bool FctHelper::mpi_recvBool(int rank, int source, int tag, MPI_Status* optOut_status, bool debug) {
105+
return mpi_recvValue<char>(rank, MPI_CHAR, source, tag, optOut_status, debug, [](const char x) { return x ? "true" : "false"; });
106+
}
107+
108+
bool FctHelper::mpi_tryRecvBool(int rank, int source, bool& result, int tag, MPI_Status* optOut_status, bool debug) {
109+
char c;
110+
if (mpi_tryRecvValue(rank, MPI_CHAR, source, tag, optOut_status, c, debug, [](const char x) { return x ? "true" : "false"; })) {
111+
result = c;
112+
return true;
113+
}
114+
return false;
115+
}
116+
117+
void FctHelper::mpi_sendInt(int rank, const int num, int dest, int tag, bool debug) {
118+
mpi_send(rank, 1, MPI_INT, &num, dest, tag, debug, [](const int* x) { return to_string(*x); });
119+
}
120+
121+
int FctHelper::mpi_recvInt(int rank, int source, int tag, MPI_Status* optOut_status, bool debug) {
122+
return mpi_recvValue<int>(rank, MPI_INT, source, tag, optOut_status, debug, [](const int x) { return to_string(x); });
123+
}
124+
125+
bool FctHelper::mpi_tryRecvInt(int rank, int source, int& result, int tag, MPI_Status* optOut_status, bool debug) {
126+
return mpi_tryRecvValue(rank, MPI_INT, source, tag, optOut_status, result, debug, [](const int x) { return to_string(x); });
127+
}
128+
129+
void FctHelper::mpi_sendUint64(int rank, const uint64_t num, int dest, int tag, bool debug) {
130+
mpi_send(rank, 1, MPI_UNSIGNED_LONG_LONG, &num, dest, tag, debug, [](const uint64_t* x) { return to_string(*x); });
131+
}
132+
133+
uint64_t FctHelper::mpi_recvUint64(int rank, int source, int tag, MPI_Status* optOut_status, bool debug) {
134+
return mpi_recvValue<uint64_t>(rank, MPI_UNSIGNED_LONG_LONG, source, tag, optOut_status, debug, [](const uint64_t x) { return to_string(x); });
135+
}
107136

108-
void FctHelper::mpi_sendString(int rank, const string& s, int dest, bool debug) {
137+
bool FctHelper::mpi_tryRecvUint64(int rank, int source, uint64_t& result, int tag, MPI_Status* optOut_status, bool debug) {
138+
return mpi_tryRecvValue(rank, MPI_UNSIGNED_LONG_LONG, source, tag, optOut_status, result, debug, [](const uint64_t x) { return to_string(x); });
139+
}
140+
141+
void FctHelper::mpi_sendString(int rank, const string& s, int dest, int tag, bool debug) {
109142
// Actually send s.size() + 1 chars, since s.c_str() is null-terminated.
110-
mpi_send(rank, static_cast<int>(s.size() + 1), MPI_CHAR, s.c_str(), dest, mpi_tag_string, debug, [&](const char* x) {
143+
mpi_send(rank, static_cast<int>(s.size() + 1), MPI_CHAR, s.c_str(), dest, tag, debug, [&](const char* x) {
111144
stringstream ss;
112145
ss << "\"" << x << "\" (length " << s.length() << ")";
113146
return ss.str();
114147
});
115148
}
116149

117-
string FctHelper::mpi_recvString(int rank, int source, bool debug) {
118-
return mpi_recvDynArray<char>(rank, MPI_CHAR, source, mpi_tag_string, debug, [&](const ManagedArray<char>& x) {
150+
string FctHelper::mpi_recvString(int rank, int source, int tag, MPI_Status* optOut_status, bool debug) {
151+
return mpi_recvDynArray<char>(rank, MPI_CHAR, source, tag, optOut_status, debug, [&](const ManagedArray<char>& x) {
119152
stringstream ss;
120153
ss << "\"" << x.data << "\" (length " << string(x.data).length() << ")";
121154
return ss.str();
122155
}).data;
123156
}
124157

125-
bool FctHelper::mpi_tryRecvString(int rank, int source, string& result, bool debug) {
158+
bool FctHelper::mpi_tryRecvString(int rank, int source, string& result, int tag, MPI_Status* optOut_status, bool debug) {
126159
ManagedArray<char> arr;
127-
if (mpi_tryRecvDynArray(rank, MPI_CHAR, source, mpi_tag_string, arr, debug, [&](const ManagedArray<char>& x) {
160+
if (mpi_tryRecvDynArray(rank, MPI_CHAR, source, tag, optOut_status, arr, debug, [&](const ManagedArray<char>& x) {
128161
stringstream ss;
129162
ss << "\"" << x.data << "\" (length " << string(x.data).length() << ")";
130163
return ss.str();
@@ -135,50 +168,20 @@ bool FctHelper::mpi_tryRecvString(int rank, int source, string& result, bool deb
135168
return false;
136169
}
137170

138-
void FctHelper::mpi_sendBool(int rank, const bool b, int dest, bool debug) {
139-
char c(b);
140-
mpi_send(rank, 1, MPI_CHAR, &c, dest, mpi_tag_bool, debug, [](const char* x) { return *x ? "true" : "false"; });
141-
}
142-
143-
bool FctHelper::mpi_recvBool(int rank, int source, bool debug) {
144-
return mpi_recvValue<char>(rank, MPI_CHAR, source, mpi_tag_bool, debug, [](const char x) { return x ? "true" : "false"; });
145-
}
146-
147-
bool FctHelper::mpi_tryRecvBool(int rank, int source, bool& result, bool debug) {
148-
char c;
149-
if (mpi_tryRecvValue(rank, MPI_CHAR, source, mpi_tag_bool, c, debug, [](const char x) { return x ? "true" : "false"; })) {
150-
result = c;
151-
return true;
152-
}
153-
return false;
154-
}
155-
156-
void FctHelper::mpi_sendUint64(int rank, const uint64_t num, int dest, bool debug) {
157-
mpi_send(rank, 1, MPI_LONG_LONG_INT, &num, dest, mpi_tag_uint64, debug, [](const uint64_t* x) { return to_string(*x); });
158-
}
159-
160-
uint64_t FctHelper::mpi_recvUint64(int rank, int source, bool debug) {
161-
return mpi_recvValue<uint64_t>(rank, MPI_LONG_LONG_INT, source, mpi_tag_uint64, debug, [](const uint64_t x) { return to_string(x); });
162-
}
163-
164-
bool FctHelper::mpi_tryRecvUint64(int rank, int source, uint64_t& result, bool debug) {
165-
return mpi_tryRecvValue(rank, MPI_LONG_LONG_INT, source, mpi_tag_uint64, result, debug, [](const uint64_t x) { return to_string(x); });
166-
}
167-
168-
void FctHelper::mpi_sendUint64Pair(int rank, const array<uint64_t, 2>& arr, int dest, bool debug) {
169-
mpi_send(rank, 2, MPI_LONG_LONG_INT, arr.data(), dest, mpi_tag_uint64_pair, debug, [](const uint64_t* x) {
171+
void FctHelper::mpi_sendPairUint64(int rank, const array<uint64_t, 2>& arr, int dest, int tag, bool debug) {
172+
mpi_send(rank, 2, MPI_UNSIGNED_LONG_LONG, arr.data(), dest, tag, debug, [](const uint64_t* x) {
170173
return "(" + to_string(x[0]) + ", " + to_string(x[1]) + ")";
171174
});
172175
}
173176

174-
array<uint64_t, 2> FctHelper::mpi_recvUint64Pair(int rank, int source, bool debug) {
175-
return mpi_recvArray<uint64_t, 2>(rank, MPI_LONG_LONG_INT, source, mpi_tag_uint64_pair, debug, [](const array<uint64_t, 2>& x) {
177+
array<uint64_t, 2> FctHelper::mpi_recvPairUint64(int rank, int source, int tag, MPI_Status* optOut_status, bool debug) {
178+
return mpi_recvArray<uint64_t, 2>(rank, MPI_UNSIGNED_LONG_LONG, source, tag, optOut_status, debug, [](const array<uint64_t, 2>& x) {
176179
return "(" + to_string(x[0]) + ", " + to_string(x[1]) + ")";
177180
});
178181
}
179182

180-
bool FctHelper::mpi_tryRecvUint64Pair(int rank, int source, array<uint64_t, 2>& result, bool debug) {
181-
return mpi_tryRecvArray(rank, MPI_LONG_LONG_INT, source, mpi_tag_uint64_pair, result, debug, [](const array<uint64_t, 2>& x) {
183+
bool FctHelper::mpi_tryRecvPairUint64(int rank, int source, array<uint64_t, 2>& result, int tag, MPI_Status* optOut_status, bool debug) {
184+
return mpi_tryRecvArray(rank, MPI_UNSIGNED_LONG_LONG, source, tag, optOut_status, result, debug, [](const array<uint64_t, 2>& x) {
182185
return "(" + to_string(x[0]) + ", " + to_string(x[1]) + ")";
183186
});
184187
}

helper/FctHelper.h

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <filesystem>
1212
#include <fstream>
1313
#include <map>
14+
#include <mpi.h>
1415
#include <set>
1516
#include <sstream>
1617
#include <string>
@@ -40,21 +41,35 @@ struct ManagedArray { // for RAII on dynamic arrays
4041
};
4142

4243
struct FctHelper {
43-
static void mpi_sendString(int rank, const std::string& s, int dest, bool debug = false);
44-
static std::string mpi_recvString(int rank, int source, bool debug = false);
45-
static bool mpi_tryRecvString(int rank, int source, std::string& result, bool debug = false);
46-
47-
static void mpi_sendBool(int rank, const bool num, int dest, bool debug = false);
48-
static bool mpi_recvBool(int rank, int source, bool debug = false);
49-
static bool mpi_tryRecvBool(int rank, int source, bool& result, bool debug = false);
50-
51-
static void mpi_sendUint64(int rank, const std::uint64_t num, int dest, bool debug = false);
52-
static std::uint64_t mpi_recvUint64(int rank, int source, bool debug = false);
53-
static bool mpi_tryRecvUint64(int rank, int source, std::uint64_t& result, bool debug = false);
54-
55-
static void mpi_sendUint64Pair(int rank, const std::array<std::uint64_t, 2>& arr, int dest, bool debug = false);
56-
static std::array<std::uint64_t, 2> mpi_recvUint64Pair(int rank, int source, bool debug = false);
57-
static bool mpi_tryRecvUint64Pair(int rank, int source, std::array<std::uint64_t, 2>& result, bool debug = false);
44+
enum mpi_tag : int {
45+
mpi_tag_unspecified = 0, // not used by any helper function
46+
mpi_tag_bool = 1,
47+
mpi_tag_int = 2,
48+
mpi_tag_uint64 = 3,
49+
mpi_tag_string = 4,
50+
mpi_tag_pair_uint64 = 5,
51+
mpi_tag_custom = 6 // highest value ; to be added upon for custom tags
52+
};
53+
54+
static void mpi_sendBool(int rank, const bool num, int dest, int tag = mpi_tag_bool, bool debug = false);
55+
static bool mpi_recvBool(int rank, int source, int tag = mpi_tag_bool, MPI_Status* optOut_status = MPI_STATUS_IGNORE, bool debug = false);
56+
static bool mpi_tryRecvBool(int rank, int source, bool& result, int tag = mpi_tag_bool, MPI_Status* optOut_status = MPI_STATUS_IGNORE, bool debug = false);
57+
58+
static void mpi_sendInt(int rank, const int num, int dest, int tag = mpi_tag_int, bool debug = false);
59+
static int mpi_recvInt(int rank, int source, int tag = mpi_tag_int, MPI_Status* optOut_status = MPI_STATUS_IGNORE, bool debug = false);
60+
static bool mpi_tryRecvInt(int rank, int source, int& result, int tag = mpi_tag_int, MPI_Status* optOut_status = MPI_STATUS_IGNORE, bool debug = false);
61+
62+
static void mpi_sendUint64(int rank, const std::uint64_t num, int dest, int tag = mpi_tag_uint64, bool debug = false);
63+
static std::uint64_t mpi_recvUint64(int rank, int source, int tag = mpi_tag_uint64, MPI_Status* optOut_status = MPI_STATUS_IGNORE, bool debug = false);
64+
static bool mpi_tryRecvUint64(int rank, int source, std::uint64_t& result, int tag = mpi_tag_uint64, MPI_Status* optOut_status = MPI_STATUS_IGNORE, bool debug = false);
65+
66+
static void mpi_sendString(int rank, const std::string& s, int dest, int tag = mpi_tag_string, bool debug = false);
67+
static std::string mpi_recvString(int rank, int source, int tag = mpi_tag_string, MPI_Status* optOut_status = MPI_STATUS_IGNORE, bool debug = false);
68+
static bool mpi_tryRecvString(int rank, int source, std::string& result, int tag = mpi_tag_string, MPI_Status* optOut_status = MPI_STATUS_IGNORE, bool debug = false);
69+
70+
static void mpi_sendPairUint64(int rank, const std::array<std::uint64_t, 2>& arr, int dest, int tag = mpi_tag_pair_uint64, bool debug = false);
71+
static std::array<std::uint64_t, 2> mpi_recvPairUint64(int rank, int source, int tag = mpi_tag_pair_uint64, MPI_Status* optOut_status = MPI_STATUS_IGNORE, bool debug = false);
72+
static bool mpi_tryRecvPairUint64(int rank, int source, std::array<std::uint64_t, 2>& result, int tag = mpi_tag_pair_uint64, MPI_Status* optOut_status = MPI_STATUS_IGNORE, bool debug = false);
5873

5974
// from_chars does not work as desired (i.e. accepts invalid inputs), so we use it with some additional argument checks
6075
template<typename T>

main.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include <cstring>
66
#include <ctime>
77
#include <iostream>
8-
#include <mpi.h>
98
#include <unistd.h>
109

1110
using namespace std;

0 commit comments

Comments
 (0)