1 /*******************************************************************************
2  * benchmarks/core/duplicates_speedup_benchmark.cpp
3  *
4  * Part of Project Thrill - http://project-thrill.org
5  *
6  * Copyright (C) 2016 Alexander Noe <aleexnoe@gmail.com>
7  *
8  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
9  ******************************************************************************/
10 
11 #include <thrill/api/all_gather.hpp>
12 #include <thrill/api/dia.hpp>
13 #include <thrill/api/generate.hpp>
14 #include <thrill/api/reduce_by_key.hpp>
15 #include <thrill/api/size.hpp>
16 #include <thrill/common/logger.hpp>
17 #include <thrill/common/stats_timer.hpp>
18 #include <tlx/cmdline_parser.hpp>
19 #include <tlx/die.hpp>
20 
21 #include <algorithm>
22 #include <array>
23 #include <string>
24 #include <utility>
25 
26 using namespace thrill; // NOLINT
27 
SpeedupTest(api::Context & ctx,size_t equal,size_t elements)28 void SpeedupTest(api::Context& ctx, size_t equal, size_t elements) {
29 
30     static constexpr bool debug = false;
31 
32     auto in = api::Generate(
33         ctx, elements,
34         [&equal](size_t n) {
35             std::array<size_t, 128> value;
36             for (size_t i = 0; i < 128; ++i) {
37                 value[i] = i + n;
38             }
39             return std::make_pair(n / equal, value);
40         }).Keep().Execute();
41 
42     ctx.net.Barrier();
43     common::StatsTimerStart timer;
44     auto out = in.ReducePair(
45         [](const std::array<size_t, 128>& in1, const std::array<size_t, 128>& in2) {
46             std::array<size_t, 128> value_out;
47             for (size_t i = 0; i < 128; ++i) {
48                 value_out[i] = in1[i] + in2[i];
49             }
50             return value_out;
51         });
52     out.Size();
53     ctx.net.Barrier();
54     timer.Stop();
55 
56     if (debug) {
57         auto vec = out.AllGather();
58         std::sort(vec.begin(), vec.end(), [](auto i1, auto i2) {
59                       return i1.first < i2.first;
60                   });
61         if (ctx.my_rank() == 0) {
62             LOG1 << "Checking results!";
63             die_unequal(elements / equal, vec.size());
64             for (size_t i = 0; i < vec.size(); ++i) {
65                 for (size_t j = 0; j < 128; ++j) {
66                     die_unequal(vec[i].second[j],
67                                 equal * (equal - 1) / 2
68                                 + equal * j
69                                 + equal * equal * i);
70                 }
71             }
72             LOG1 << "Result checking successful.";
73         }
74     }
75     else {
76         LOG1 << "RESULT" << " benchmark=duplicates detection=ON"
77              << " elements=" << elements
78              << " time=" << timer
79              << " traffic=" << ctx.net_manager().Traffic()
80              << " hosts=" << ctx.num_hosts();
81     }
82 }
83 
main(int argc,char * argv[])84 int main(int argc, char* argv[]) {
85 
86     tlx::CmdlineParser clp;
87 
88     size_t equal = 5;
89     clp.add_opt_param_size_t("e", equal, "Number of equal elements reduced together");
90 
91     size_t elements = 1000;
92     clp.add_opt_param_size_t("n", elements, "Number of elements in total.");
93 
94     if (!clp.process(argc, argv)) {
95         return -1;
96     }
97 
98     clp.print_result();
99 
100     api::Run([&](api::Context& ctx) {
101                  return SpeedupTest(ctx, equal, elements);
102              });
103 }
104 
105 /******************************************************************************/
106