• 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
/libs/full/resiliency_distributed/examples/async_replicate_distributed.cpp
1
//  Copyright (c) 2019 National Technology & Engineering Solutions of Sandia,
2
//                     LLC (NTESS).
3
//  Copyright (c) 2018-2019 Hartmut Kaiser
4
//  Copyright (c) 2018-2019 Adrian Serio
5
//  Copyright (c) 2019-2020 Nikunj Gupta
6
//  SPDX-License-Identifier: BSL-1.0
7
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
8
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9

10
#include <hpx/config.hpp>
11
#if !defined(HPX_COMPUTE_DEVICE_CODE)
12

13
#include <hpx/actions_base/plain_action.hpp>
14
#include <hpx/assert.hpp>
15
#include <hpx/hpx_init.hpp>
16
#include <hpx/include/runtime.hpp>
17
#include <hpx/modules/futures.hpp>
18
#include <hpx/modules/resiliency_distributed.hpp>
19
#include <hpx/modules/testing.hpp>
20

21
#include <cstddef>
22
#include <iostream>
23
#include <random>
24
#include <vector>
25

26
int universal_ans(std::vector<hpx::id_type> f_locales, std::size_t size)
×
27
{
28
    // Pretending to do some useful work
29
    std::size_t start = hpx::chrono::high_resolution_clock::now();
30

31
    while ((hpx::chrono::high_resolution_clock::now() - start) < (size * 100))
×
32
    {
33
    }
34

35
    // Check if the node is faulty
36
    for (auto const& locale : f_locales)
×
37
    {
38
        // Throw a runtime error in case the node is faulty
39
        if (locale == hpx::find_here())
×
40
            throw std::runtime_error("runtime error occurred.");
×
41
    }
42

43
    return 42;
×
44
}
45

46
HPX_PLAIN_ACTION(universal_ans, universal_action)
×
47

48
bool validate(int ans)
×
49
{
50
    return ans == 42;
×
51
}
52

53
int vote(std::vector<int>&& results)
×
54
{
55
    return results.at(0);
×
56
}
57

58
int hpx_main(hpx::program_options::variables_map& vm)
×
59
{
60
    std::size_t f_nodes = vm["f-nodes"].as<std::size_t>();
×
61
    std::size_t size = vm["size"].as<std::size_t>();
×
62
    std::size_t num_tasks = vm["num-tasks"].as<std::size_t>();
×
63
    std::size_t num_replications = vm["num-replications"].as<std::size_t>();
×
64

65
    universal_action ac;
66
    std::vector<hpx::id_type> locales = hpx::find_all_localities();
×
67

68
    // Make sure that the number of faulty nodes are less than the number of
69
    // localities we work on.
70
    HPX_ASSERT(f_nodes < locales.size());
71

72
    // List of faulty nodes
73
    std::vector<hpx::id_type> f_locales;
×
74
    std::vector<std::size_t> visited;
×
75

76
    // Mark nodes as faulty
77
    for (std::size_t i = 0; i < f_nodes; ++i)
×
78
    {
79
        std::size_t num = std::rand() % locales.size();
×
80
        while (visited.end() != std::find(visited.begin(), visited.end(), num))
×
81
        {
82
            num = std::rand() % locales.size();
×
83
        }
84

85
        f_locales.push_back(locales.at(num));
×
86
    }
87

88
    {
89
        hpx::chrono::high_resolution_timer t;
90

91
        std::vector<hpx::future<int>> tasks;
×
92
        for (std::size_t i = 0; i < num_tasks; ++i)
×
93
        {
94
            std::vector<hpx::id_type> ids;
×
95
            ids.reserve(num_replications);
×
96
            if (num_replications < locales.size())
×
97
            {
98
                for (auto& locale : locales)
×
99
                    ids.push_back(locale);
×
100
            }
101
            else
102
            {
103
                for (auto& locale : locales)
×
104
                    ids.push_back(locale);
×
105

106
                for (std::size_t i = 0; i < num_replications - ids.size(); ++i)
×
107
                {
108
                    ids.emplace_back(locales.at(i));
×
109
                }
110
            }
111

112
            tasks.push_back(hpx::resiliency::experimental::async_replicate(
×
113
                ids, ac, f_locales, size));
114

115
            std::rotate(locales.begin(), locales.begin() + 1, locales.end());
×
116
        }
×
117

118
        hpx::wait_all(tasks);
119

120
        double elapsed = t.elapsed();
121
        std::cout << "Replicate: " << elapsed << std::endl;
122
    }
×
123

124
    {
125
        hpx::chrono::high_resolution_timer t;
126

127
        std::vector<hpx::future<int>> tasks;
×
128
        for (std::size_t i = 0; i < num_tasks; ++i)
×
129
        {
130
            std::vector<hpx::id_type> ids;
×
131
            ids.reserve(num_replications);
×
132
            if (num_replications < locales.size())
×
133
            {
134
                for (auto& locale : locales)
×
135
                    ids.push_back(locale);
×
136
            }
137
            else
138
            {
139
                for (auto& locale : locales)
×
140
                    ids.push_back(locale);
×
141

142
                for (std::size_t i = 0; i < num_replications - ids.size(); ++i)
×
143
                {
144
                    ids.emplace_back(locales.at(i));
×
145
                }
146
            }
147

148
            tasks.push_back(
149
                hpx::resiliency::experimental::async_replicate_validate(
×
150
                    ids, &validate, ac, f_locales, size));
×
151

152
            std::rotate(locales.begin(), locales.begin() + 1, locales.end());
×
153
        }
×
154

155
        hpx::wait_all(tasks);
156

157
        double elapsed = t.elapsed();
158
        std::cout << "Replicate Validate: " << elapsed << std::endl;
159
    }
×
160

161
    {
162
        hpx::chrono::high_resolution_timer t;
163

164
        std::vector<hpx::future<int>> tasks;
×
165
        for (std::size_t i = 0; i < num_tasks; ++i)
×
166
        {
167
            std::vector<hpx::id_type> ids;
×
168
            ids.reserve(num_replications);
×
169
            if (num_replications < locales.size())
×
170
            {
171
                for (auto& locale : locales)
×
172
                    ids.push_back(locale);
×
173
            }
174
            else
175
            {
176
                for (auto& locale : locales)
×
177
                    ids.push_back(locale);
×
178

179
                for (std::size_t i = 0; i < num_replications - ids.size(); ++i)
×
180
                {
181
                    ids.emplace_back(locales.at(i));
×
182
                }
183
            }
184

185
            tasks.push_back(hpx::resiliency::experimental::async_replicate_vote(
×
186
                ids, &vote, ac, f_locales, size));
×
187

188
            std::rotate(locales.begin(), locales.begin() + 1, locales.end());
×
189
        }
×
190

191
        hpx::wait_all(tasks);
192

193
        double elapsed = t.elapsed();
194
        std::cout << "Replicate Vote: " << elapsed << std::endl;
195
    }
×
196

197
    {
198
        hpx::chrono::high_resolution_timer t;
199

200
        std::vector<hpx::future<int>> tasks;
×
201
        for (std::size_t i = 0; i < num_tasks; ++i)
×
202
        {
203
            std::vector<hpx::id_type> ids;
×
204
            ids.reserve(num_replications);
×
205
            if (num_replications < locales.size())
×
206
            {
207
                for (auto& locale : locales)
×
208
                    ids.push_back(locale);
×
209
            }
210
            else
211
            {
212
                for (auto& locale : locales)
×
213
                    ids.push_back(locale);
×
214

215
                for (std::size_t i = 0; i < num_replications - ids.size(); ++i)
×
216
                {
217
                    ids.emplace_back(locales.at(i));
×
218
                }
219
            }
220

221
            tasks.push_back(
222
                hpx::resiliency::experimental::async_replicate_vote_validate(
×
223
                    ids, &vote, &validate, ac, f_locales, size));
×
224

225
            std::rotate(locales.begin(), locales.begin() + 1, locales.end());
×
226
        }
×
227

228
        hpx::wait_all(tasks);
229

230
        double elapsed = t.elapsed();
231
        std::cout << "Replicate Vote Validate: " << elapsed << std::endl;
232
    }
×
233

234
    return hpx::finalize();
×
235
}
×
236

237
int main(int argc, char* argv[])
×
238
{
239
    // Configure application-specific options
240
    hpx::program_options::options_description desc_commandline;
×
241

242
    namespace po = hpx::program_options;
243

244
    // clang-format off
245
    desc_commandline.add_options()
×
246
        ("f-nodes", po::value<std::size_t>()->default_value(1),
×
247
            "Number of faulty nodes to be injected")
248
        ("size", po::value<std::size_t>()->default_value(200),
×
249
            "Grain size of a task")
250
        ("num-tasks", po::value<std::size_t>()->default_value(1000),
×
251
            "Number of tasks to invoke")
252
        ("num-replications", po::value<std::size_t>()->default_value(3),
×
253
            "Total number of replicates for a task (including the task itself)")
254
    ;
255
    // clang-format on
256

257
    // Initialize and run HPX
258
    hpx::init_params params;
×
259
    params.desc_cmdline = desc_commandline;
×
260
    return hpx::init(argc, argv, params);
×
261
}
×
262

263
#endif
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