• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

STEllAR-GROUP / hpx / #882

31 Aug 2023 07:44PM UTC coverage: 41.798% (-44.7%) from 86.546%
#882

push

19442 of 46514 relevant lines covered (41.8%)

126375.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.89
/examples/quickstart/fibonacci_dataflow.cpp
1
//  Copyright (c)      2013 Thomas Heller
2
//  Copyright (c) 2007-2013 Hartmut Kaiser
3
//
4
//  SPDX-License-Identifier: BSL-1.0
5
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
6
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7

8
// This is a purely local version demonstrating the proposed extension to
9
// C++ implementing resumable functions (see N3564,
10
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3564.pdf). The
11
// necessary transformations are performed by hand.
12

13
#include <hpx/chrono.hpp>
14
#include <hpx/format.hpp>
15
#include <hpx/future.hpp>
16
#include <hpx/init.hpp>
17

18
#include <cstdint>
19
#include <iostream>
20
#include <string>
21
#include <utility>
22

23
///////////////////////////////////////////////////////////////////////////////
24
std::uint64_t threshold = 2;
25

26
///////////////////////////////////////////////////////////////////////////////
27
HPX_NOINLINE std::uint64_t fibonacci_serial(std::uint64_t n)
89✔
28
{
29
    if (n < 2)
177✔
30
        return n;
31
    return fibonacci_serial(n - 1) + fibonacci_serial(n - 2);
88✔
32
}
33

34
///////////////////////////////////////////////////////////////////////////////
35
hpx::future<std::uint64_t> fibonacci(std::uint64_t n)
177✔
36
{
37
    if (n < 2)
177✔
38
        return hpx::make_ready_future(n);
39
    if (n < threshold)
88✔
40
        return hpx::make_ready_future(fibonacci_serial(n));
×
41

42
    hpx::future<std::uint64_t> lhs_future = hpx::async(&fibonacci, n - 1);
88✔
43
    hpx::future<std::uint64_t> rhs_future = fibonacci(n - 2);
88✔
44

45
    return hpx::dataflow(
46
        hpx::unwrapping(
88✔
47
            [](std::uint64_t lhs, std::uint64_t rhs) { return lhs + rhs; }),
88✔
48
        std::move(lhs_future), std::move(rhs_future));
49
}
50

51
///////////////////////////////////////////////////////////////////////////////
52
int hpx_main(hpx::program_options::variables_map& vm)
1✔
53
{
54
    // extract command line argument, i.e. fib(N)
55
    std::uint64_t n = vm["n-value"].as<std::uint64_t>();
2✔
56
    std::string test = vm["test"].as<std::string>();
1✔
57
    std::uint64_t max_runs = vm["n-runs"].as<std::uint64_t>();
2✔
58

59
    if (max_runs == 0)
1✔
60
    {
61
        std::cerr
62
            << "fibonacci_dataflow: wrong command line argument value for "
63
               "option 'n-runs', should not be zero"
64
            << std::endl;
65
        return hpx::local::finalize();    // Handles HPX shutdown
×
66
    }
67

68
    threshold = vm["threshold"].as<unsigned int>();
2✔
69
    if (threshold < 2 || threshold > n)
1✔
70
    {
71
        std::cerr
72
            << "fibonacci_dataflow: wrong command line argument value for "
73
               "option 'threshold', should be in between 2 and n-value"
74
               ", value specified: "
75
            << threshold << std::endl;
×
76
        return hpx::local::finalize();    // Handles HPX shutdown
×
77
    }
78

79
    bool executed_one = false;
80
    std::uint64_t r = 0;
1✔
81

82
    if (test == "all" || test == "0")
1✔
83
    {
84
        // Keep track of the time required to execute.
85
        std::uint64_t start = hpx::chrono::high_resolution_clock::now();
86

87
        for (std::uint64_t i = 0; i != max_runs; ++i)
2✔
88
        {
89
            // serial execution
90
            r = fibonacci_serial(n);
1✔
91
        }
92

93
        std::uint64_t d = hpx::chrono::high_resolution_clock::now() - start;
1✔
94
        char const* fmt = "fibonacci_serial({1}) == {2},"
95
                          "elapsed time:,{3},[s]\n";
96
        hpx::util::format_to(std::cout, fmt, n, r, d / max_runs);
1✔
97

98
        executed_one = true;
99
    }
100

101
    if (test == "all" || test == "1")
1✔
102
    {
103
        // Keep track of the time required to execute.
104
        std::uint64_t start = hpx::chrono::high_resolution_clock::now();
105

106
        for (std::uint64_t i = 0; i != max_runs; ++i)
2✔
107
        {
108
            // Create a future for the whole calculation, execute it locally,
109
            // and wait for it.
110
            r = fibonacci(n).get();
1✔
111
        }
112

113
        std::uint64_t d = hpx::chrono::high_resolution_clock::now() - start;
1✔
114
        char const* fmt = "fibonacci_await({1}) == {2},"
115
                          "elapsed time:,{3},[s]\n";
116
        hpx::util::format_to(std::cout, fmt, n, r, d / max_runs);
1✔
117

118
        executed_one = true;
119
    }
120

121
    if (!executed_one)
×
122
    {
123
        std::cerr
124
            << "fibonacci_dataflow: wrong command line argument value for "
125
               "option 'tests', should be either 'all' or a number between "
126
               "zero "
127
               "and 1, value specified: "
128
            << test << std::endl;
129
    }
130

131
    return hpx::local::finalize();    // Handles HPX shutdown
1✔
132
}
133

134
///////////////////////////////////////////////////////////////////////////////
135
int main(int argc, char* argv[])
1✔
136
{
137
    // Configure application-specific options
138
    hpx::program_options::options_description desc_commandline(
139
        "Usage: " HPX_APPLICATION_STRING " [options]");
1✔
140

141
    using hpx::program_options::value;
142
    // clang-format off
143
    desc_commandline.add_options()
1✔
144
        ("n-value", value<std::uint64_t>()->default_value(10),
1✔
145
         "n value for the Fibonacci function")
146
        ("n-runs", value<std::uint64_t>()->default_value(1),
1✔
147
         "number of runs to perform")
148
        ("threshold", value<unsigned int>()->default_value(2),
1✔
149
         "threshold for switching to serial code")
150
        ("test", value<std::string>()->default_value("all"),
1✔
151
        "select tests to execute (0-1, default: all)");
152
    // clang-format on
153

154
    // Initialize and run HPX
155
    hpx::local::init_params init_args;
1✔
156
    init_args.desc_cmdline = desc_commandline;
1✔
157

158
    return hpx::local::init(hpx_main, argc, argv, init_args);
1✔
159
}
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc