• 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/balancing/os_thread_num.cpp
1
//  Copyright (c) 2011 Bryce Lelbach
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/barrier.hpp>
8
#include <hpx/format.hpp>
9
#include <hpx/init.hpp>
10
#include <hpx/modules/allocator_support.hpp>
11
#include <hpx/modules/concurrency.hpp>
12
#include <hpx/runtime.hpp>
13
#include <hpx/thread.hpp>
14

15
#include <cstddef>
16
#include <cstdint>
17
#include <functional>
18
#include <iostream>
19
#include <map>
20
#include <memory>
21

22
template <typename T>
23
using queue =
24
    hpx::lockfree::queue<T, hpx::util::aligned_allocator<std::size_t>>;
25

26
using hpx::program_options::options_description;
27
using hpx::program_options::value;
28
using hpx::program_options::variables_map;
29

30
using hpx::barrier;
31

32
using hpx::threads::register_work;
33
using hpx::threads::thread_init_data;
34

35
///////////////////////////////////////////////////////////////////////////////
36
// we use globals here to prevent the delay from being optimized away
37
double global_scratch = 0;
38
std::uint64_t num_iterations = 0;
39

40
///////////////////////////////////////////////////////////////////////////////
41
double delay()
×
42
{
43
    double d = 0.;
44
    for (std::uint64_t i = 0; i < num_iterations; ++i)
×
45
        d += 1 / (2. * static_cast<double>(i + 1));
×
46
    return d;
×
47
}
48

49
///////////////////////////////////////////////////////////////////////////////
50
void get_os_thread_num(
×
51
    std::shared_ptr<barrier<>> barr, queue<std::size_t>& os_threads)
52
{
53
    global_scratch = delay();
×
54
    os_threads.push(hpx::get_worker_thread_num());
×
55
    barr->arrive_and_wait();
×
56
}
×
57

58
///////////////////////////////////////////////////////////////////////////////
59
typedef std::map<std::size_t, std::size_t> result_map;
60

61
typedef std::multimap<std::size_t, std::size_t, std::greater<std::size_t>>
62
    sorter;
63

64
///////////////////////////////////////////////////////////////////////////////
65
int hpx_main(variables_map& vm)
×
66
{
67
    {
68
        num_iterations = vm["delay-iterations"].as<std::uint64_t>();
×
69

70
        bool const csv = vm.count("csv");
×
71

72
        std::size_t const pxthreads = vm["pxthreads"].as<std::size_t>();
×
73

74
        result_map results;
75

76
        {
77
            // Have the queue preallocate the nodes.
78
            queue<std::size_t> os_threads(pxthreads);
×
79

80
            std::shared_ptr<barrier<>> barr =
81
                std::make_shared<barrier<>>(pxthreads + 1);
×
82

83
            for (std::size_t j = 0; j < pxthreads; ++j)
×
84
            {
85
                thread_init_data data(
86
                    hpx::threads::make_thread_function_nullary(hpx::bind(
×
87
                        &get_os_thread_num, barr, std::ref(os_threads))),
88
                    "get_os_thread_num", hpx::threads::thread_priority::normal,
89
                    hpx::threads::thread_schedule_hint(0));
90
                register_work(data);
×
91
            }
92

93
            // wait for all HPX threads to enter the barrier
94
            barr->arrive_and_wait();
×
95

96
            std::size_t shepherd = 0;
×
97

98
            while (os_threads.pop(shepherd))
×
99
                ++results[shepherd];
×
100
        }
×
101

102
        sorter sort;
103

104
        for (result_map::value_type const& result : results)
×
105
        {
106
            sort.insert(sorter::value_type(result.second, result.first));
×
107
        }
108

109
        for (sorter::value_type const& result : sort)
×
110
        {
111
            if (csv)
×
112
                hpx::util::format_to(
×
113
                    std::cout, "{1},{2}\n", result.second, result.first)
×
114
                    << std::flush;
115
            else
116
                hpx::util::format_to(std::cout,
×
117
                    "OS-thread {1} ran {2} PX-threads\n", result.second,
×
118
                    result.first)
×
119
                    << std::flush;
120
        }
121
    }
122

123
    // initiate shutdown of the runtime system
124
    hpx::local::finalize();
×
125
    return 0;
×
126
}
127

128
///////////////////////////////////////////////////////////////////////////////
129
int main(int argc, char* argv[])
×
130
{
131
    // Configure application-specific options
132
    options_description cmdline("Usage: " HPX_APPLICATION_STRING " [options]");
×
133

134
    cmdline.add_options()("pxthreads", value<std::size_t>()->default_value(128),
×
135
        "number of PX-threads to invoke")
136

137
        ("delay-iterations", value<std::uint64_t>()->default_value(65536),
×
138
            "number of iterations in the delay loop")
139

140
            ("csv", "output results as csv (format: OS-thread,PX-threads)");
×
141

142
    // Initialize and run HPX
143
    hpx::local::init_params init_args;
×
144
    init_args.desc_cmdline = cmdline;
×
145

146
    return hpx::local::init(hpx_main, argc, argv, init_args);
×
147
}
×
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