Skip to content

Commit 66fe44c

Browse files
committed
#2302: examples: cleanup/rework perf example
1 parent eed6191 commit 66fe44c

File tree

1 file changed

+10
-152
lines changed

1 file changed

+10
-152
lines changed

examples/collection/do_flops_perf.cc

Lines changed: 10 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,14 @@
4141
//@HEADER
4242
*/
4343

44-
#include <vt/transport.h>
45-
#include <vt/metrics/perf_data.h>
46-
47-
#include <cstdlib>
48-
#include <cassert>
49-
#include <iostream>
50-
5144
/// [Do Flops example]
5245

5346
#include <vt/transport.h>
5447
#include <vt/runnable/invoke.h>
5548

5649
#include <cstdlib>
57-
#include <cassert>
58-
#include <iostream>
5950

60-
static constexpr std::size_t const default_nrow_object = 8;
6151
static constexpr std::size_t const default_num_objs = 100;
62-
static constexpr double const default_tol = 1.0e-02;
6352
static constexpr std::size_t const default_flops_per_iter = 100000;
6453

6554
double pi(uint64_t n) {
@@ -72,55 +61,7 @@ double pi(uint64_t n) {
7261
return 4.0*sum;
7362
}
7463

75-
struct NodeObj {
76-
bool is_finished_ = false;
77-
void workFinishedHandler() { is_finished_ = true; }
78-
bool isWorkFinished() { return is_finished_; }
79-
};
80-
using NodeObjProxy = vt::objgroup::proxy::Proxy<NodeObj>;
81-
8264
struct GenericWork : vt::Collection<GenericWork, vt::Index1D> {
83-
84-
private:
85-
size_t iter_ = 0;
86-
size_t msgReceived_ = 0, totalReceive_ = 0;
87-
size_t numObjs_ = 1;
88-
size_t flopsPerIter_ = default_flops_per_iter;
89-
size_t maxIter_ = 8;
90-
NodeObjProxy objProxy_;
91-
92-
public:
93-
explicit GenericWork() :
94-
iter_(0), msgReceived_(0), totalReceive_(0),
95-
numObjs_(1), flopsPerIter_(default_flops_per_iter), maxIter_(8)
96-
{ }
97-
98-
using BlankMsg = vt::CollectionMessage<GenericWork>;
99-
100-
struct WorkMsg : vt::CollectionMessage<GenericWork> {
101-
size_t numObjects = 0;
102-
size_t flopsPerIter = 0;
103-
size_t iterMax = 0;
104-
NodeObjProxy objProxy;
105-
106-
WorkMsg() = default;
107-
108-
WorkMsg(const size_t nobjs, const size_t flops, const size_t itMax, NodeObjProxy proxy) :
109-
numObjects(nobjs), flopsPerIter(flops), iterMax(itMax), objProxy(proxy)
110-
{ }
111-
};
112-
113-
void checkCompleteCB() {
114-
auto const iter_max_reached = iter_ > maxIter_;
115-
116-
if (iter_max_reached) {
117-
fmt::print("\n Maximum Number of Iterations Reached. \n\n");
118-
objProxy_.broadcast<&NodeObj::workFinishedHandler>();
119-
} else {
120-
fmt::print(" ## ITER {} completed. \n", iter_);
121-
}
122-
}
123-
12465
void doIteration() {
12566
iter_ += 1;
12667
fmt::print("-- Starting Iteration --\n");
@@ -130,17 +71,10 @@ struct GenericWork : vt::Collection<GenericWork, vt::Index1D> {
13071
// ----------------------------------------------------------
13172
// test non packed double precision floating point operations
13273
// should result in ~4*n of these operations
133-
134-
double p;
135-
p = pi(10000000);
74+
double p = pi(10000000);
13675
fmt::print("pi: {}\n", p);
13776
// ----------------------------------------------------------
13877

139-
auto proxy = this->getCollectionProxy();
140-
proxy.reduce<&GenericWork::checkCompleteCB, vt::collective::MaxOp>(
141-
proxy[0]
142-
);
143-
14478
vt::theContext()->getTask()->stopMetrics();
14579
std::unordered_map<std::string, uint64_t> res = vt::theContext()->getTask()->getMetrics();
14680
for (auto [name, value] : res) {
@@ -150,84 +84,14 @@ struct GenericWork : vt::Collection<GenericWork, vt::Index1D> {
15084
fmt::print("-- Stopping Iteration --\n");
15185
}
15286

153-
struct VecMsg : vt::CollectionMessage<GenericWork> {
154-
using MessageParentType = vt::CollectionMessage<GenericWork>;
155-
vt_msg_serialize_if_needed_by_parent_or_type1(vt::IdxBase);
156-
157-
VecMsg() = default;
158-
159-
explicit VecMsg(vt::IdxBase const& in_index) :
160-
vt::CollectionMessage<GenericWork>(),
161-
from_index(in_index)
162-
{ }
163-
164-
template <typename Serializer>
165-
void serialize(Serializer& s) {
166-
MessageParentType::serialize(s);
167-
s | from_index;
168-
}
169-
170-
vt::IdxBase from_index = 0;
171-
};
172-
173-
void exchange(VecMsg *msg) {
174-
msgReceived_ += 1;
175-
176-
if (msgReceived_ == totalReceive_) {
177-
msgReceived_ = 0;
178-
doIteration();
179-
}
180-
}
181-
182-
void doIter([[maybe_unused]] BlankMsg *msg) {
183-
if (numObjs_ == 1) {
184-
doIteration();
185-
return;
186-
}
187-
188-
vt::IdxBase const myIdx = getIndex().x();
189-
auto proxy = this->getCollectionProxy();
190-
191-
if (myIdx > 0) {
192-
proxy[myIdx - 1].send<VecMsg, &GenericWork::exchange>(
193-
VecMsg(myIdx)
194-
);
195-
}
196-
197-
if (size_t(myIdx) < numObjs_ - 1) {
198-
proxy[myIdx + 1].send<VecMsg, &GenericWork::exchange>(
199-
VecMsg(myIdx)
200-
);
201-
}
202-
}
203-
204-
void init() {
205-
totalReceive_ = 2;
206-
207-
if (getIndex().x() == 0) {
208-
totalReceive_ -= 1;
209-
}
210-
211-
if (getIndex().x() == numObjs_ - 1) {
212-
totalReceive_ -= 1;
213-
}
87+
void init(int in_flops_per_iter) {
88+
flopsPerIter_ = in_flops_per_iter;
21489
}
21590

216-
void init(WorkMsg* msg) {
217-
numObjs_ = msg->numObjects;
218-
flopsPerIter_ = msg->flopsPerIter;
219-
maxIter_ = msg->iterMax;
220-
objProxy_ = msg->objProxy;
221-
222-
init();
223-
}
91+
private:
92+
size_t flopsPerIter_ = 0;
22493
};
22594

226-
bool isWorkDone(vt::objgroup::proxy::Proxy<NodeObj> const& proxy) {
227-
auto const this_node = vt::theContext()->getNode();
228-
return proxy[this_node].invoke<&NodeObj::isWorkFinished>();
229-
}
230-
23195
int main(int argc, char** argv) {
23296
size_t num_objs = default_num_objs;
23397
size_t flopsPerIter = default_flops_per_iter;
@@ -259,7 +123,6 @@ int main(int argc, char** argv) {
259123
return 1;
260124
}
261125

262-
auto grp_proxy = vt::theObjGroup()->makeCollective<NodeObj>("examples_generic_work");
263126
using BaseIndexType = typename vt::Index1D::DenseIndexType;
264127
auto range = vt::Index1D(static_cast<BaseIndexType>(num_objs));
265128

@@ -268,19 +131,14 @@ int main(int argc, char** argv) {
268131
.bulkInsert()
269132
.wait();
270133

271-
vt::runInEpochCollective([col_proxy, grp_proxy, num_objs, flopsPerIter, maxIter]{
272-
col_proxy.broadcastCollective<GenericWork::WorkMsg, &GenericWork::init>(
273-
num_objs, flopsPerIter, maxIter, grp_proxy
274-
);
134+
vt::runInEpochCollective([&]{
135+
col_proxy.broadcastCollective<&GenericWork::init>(flopsPerIter);
275136
});
276137

277-
while(!isWorkDone(grp_proxy)) {
278-
vt::runInEpochCollective([col_proxy]{
279-
col_proxy.broadcastCollective<
280-
GenericWork::BlankMsg, &GenericWork::doIter
281-
>();
138+
for (std::size_t i = 0; i < maxIter; i++) {
139+
vt::runInEpochCollective([&]{
140+
col_proxy.broadcastCollective<&GenericWork::doIteration>(flopsPerIter);
282141
});
283-
284142
vt::thePhase()->nextPhaseCollective();
285143
}
286144

0 commit comments

Comments
 (0)