• 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

0.0
/examples/future_reduce/rnd_future_reduce.cpp
1
//  Copyright (c) 2014 John Biddiscombe
2
//
3
//  SPDX-License-Identifier: BSL-1.0
4
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
5
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6

7
#include <hpx/chrono.hpp>
8
#include <hpx/future.hpp>
9
#include <hpx/init.hpp>
10
#include <hpx/runtime.hpp>
11
//
12
#include <iostream>
13
#include <random>
14
#include <utility>
15
#include <vector>
16

17
//
18
// This is a simple example which generates random numbers and returns
19
// pass or fail from a routine.
20
// When called by many threads returning a vector of futures - if the user wants to
21
// reduce the vector of pass/fails into a single pass fail based on a simple
22
// any fail = !pass rule, then this example shows how to do it.
23
// The user can experiment with the failure rate to see if the statistics match
24
// their expectations.
25
// Also. Routine can use either a lambda, or a function under control of USE_LAMBDA
26

27
#define TEST_SUCCESS 1
28
#define TEST_FAIL 0
29
//
30
#define FAILURE_RATE_PERCENT 5
31
#define SAMPLES_PER_LOOP 10
32
#define TEST_LOOPS 1000
33
//
34
std::random_device rseed;
35
std::mt19937 gen(rseed());
36
std::uniform_int_distribution<int> dist(0, 99);    // interval [0,100)
37

38
#define USE_LAMBDA
39

40
//----------------------------------------------------------------------------
41
int reduce(hpx::future<std::vector<hpx::future<int>>>&& futvec)
×
42
{
43
    int res = TEST_SUCCESS;
44
    std::vector<hpx::future<int>> vfs = futvec.get();
×
45
    for (hpx::future<int>& f : vfs)
×
46
    {
47
        if (f.get() == TEST_FAIL)
×
48
            return TEST_FAIL;
49
    }
50
    return res;
51
}
×
52

53
//----------------------------------------------------------------------------
54
int generate_one()
×
55
{
56
    // generate roughly x% fails
57
    int result = TEST_SUCCESS;
58
    if (dist(gen) >= (100 - FAILURE_RATE_PERCENT))
×
59
    {
60
        result = TEST_FAIL;
61
    }
62
    return result;
×
63
}
64

65
//----------------------------------------------------------------------------
66
hpx::future<int> test_reduce()
×
67
{
68
    std::vector<hpx::future<int>> req_futures;
×
69
    //
70
    for (int i = 0; i < SAMPLES_PER_LOOP; i++)
×
71
    {
72
        // generate random sequence of pass/fails using % fail rate per incident
73
        hpx::future<int> result = hpx::async(generate_one);
74
        req_futures.push_back(std::move(result));
75
    }
76

77
    hpx::future<std::vector<hpx::future<int>>> all_ready =
78
        hpx::when_all(req_futures);
79

80
#ifdef USE_LAMBDA
81
    hpx::future<int> result = all_ready.then(
82
        [](hpx::future<std::vector<hpx::future<int>>>&& futvec) -> int {
×
83
            // futvec is ready or the lambda would not be called
84
            std::vector<hpx::future<int>> vfs = futvec.get();
×
85
            // all futures in v are ready as fut is ready
86
            int res = TEST_SUCCESS;
87
            for (hpx::future<int>& f : vfs)
×
88
            {
89
                if (f.get() == TEST_FAIL)
×
90
                    return TEST_FAIL;
91
            }
92
            return res;
93
        });
×
94
#else
95
    hpx::future<int> result = all_ready.then(reduce);
96
#endif
97
    //
98
    return result;
×
99
}
×
100

101
//----------------------------------------------------------------------------
102
int hpx_main()
×
103
{
104
    hpx::chrono::high_resolution_timer htimer;
105
    // run N times and see if we get approximately the right amount of fails
106
    int count = 0;
107
    for (int i = 0; i < TEST_LOOPS; i++)
×
108
    {
109
        int result = test_reduce().get();
×
110
        count += result;
×
111
    }
112
    double pr_pass =
113
        std::pow(1.0 - FAILURE_RATE_PERCENT / 100.0, SAMPLES_PER_LOOP);
114
    double exp_pass = TEST_LOOPS * pr_pass;
115
    std::cout << "From " << TEST_LOOPS << " tests, we got "
116
              << "\n " << count << " passes"
×
117
              << "\n " << exp_pass << " expected \n"
×
118
              << "\n " << htimer.elapsed() << " seconds \n"
119
              << std::flush;
120
    // Initiate shutdown of the runtime system.
121
    return hpx::local::finalize();
×
122
}
123

124
//----------------------------------------------------------------------------
125
int main(int argc, char* argv[])
×
126
{
127
    // Initialize and run HPX.
128
    return hpx::local::init(hpx_main, argc, argv);
×
129
}
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