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

STEllAR-GROUP / hpx / #868

16 Jan 2023 08:21PM UTC coverage: 86.487%. Remained the same
#868

push

StellarBot
Merge #6137

6137: Adding example of a simple master/slave distributed application r=hkaiser a=hkaiser

The purpose of this example is to demonstrate how HPX actions can be used to build a simple master-slave application. The master (locality 0) assigns work to the slaves (all other localities). Note that if this application is run on one locality only it uses the same locality for the master and the slave functionalities.

The slaves receive a message that encodes how many sub-tasks of a certain type they should spawn locally.


Co-authored-by: Hartmut Kaiser <hartmut.kaiser@gmail.com>

72 of 72 new or added lines in 1 file covered. (100.0%)

174663 of 201952 relevant lines covered (86.49%)

1849169.69 hits per line

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

67.16
/libs/full/runtime_distributed/src/locality_interface.cpp
1
//  Copyright (c) 2021 Hartmut Kaiser
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/config.hpp>
8

9
#if defined(HPX_HAVE_NETWORKING)
10
#include <hpx/assert.hpp>
11
#include <hpx/modules/datastructures.hpp>
12
#include <hpx/modules/errors.hpp>
13

14
#include <hpx/parcelset/detail/message_handler_interface_functions.hpp>
15
#include <hpx/parcelset/message_handler_fwd.hpp>
16
#include <hpx/parcelset/parcel.hpp>
17
#include <hpx/parcelset/parcelhandler.hpp>
18
#include <hpx/parcelset/parcelset_fwd.hpp>
19
#include <hpx/parcelset_base/detail/locality_interface_functions.hpp>
20
#include <hpx/parcelset_base/locality.hpp>
21
#include <hpx/runtime_distributed.hpp>
22
#include <hpx/runtime_distributed/runtime_fwd.hpp>
23

24
#include <cstddef>
25
#include <string>
26
#include <system_error>
27
#include <vector>
28

29
///////////////////////////////////////////////////////////////////////////////
30
namespace hpx::detail {
31

32
    // forward declaration only
33
    void dijkstra_make_black();
34
}    // namespace hpx::detail
35

36
///////////////////////////////////////////////////////////////////////////////
37
namespace hpx::parcelset {
38

39
    namespace detail::impl {
40

41
        parcelset::parcel create_parcel()
475,095✔
42
        {
43
            return parcelset::parcel(new detail::parcel());
475,095✔
44
        }
×
45

46
        locality create_locality(std::string const& name)
706✔
47
        {
48
            HPX_ASSERT(get_runtime_ptr());
706✔
49
            return get_runtime_distributed()
1,412✔
50
                .get_parcel_handler()
706✔
51
                .create_locality(name);
706✔
52
        }
53

54
        parcel_write_handler_type set_parcel_write_handler(
2✔
55
            parcel_write_handler_type const& f)
56
        {
57
            runtime_distributed* rt = get_runtime_distributed_ptr();
2✔
58
            if (nullptr != rt)
2✔
59
                return rt->get_parcel_handler().set_write_handler(f);
2✔
60

61
            HPX_THROW_EXCEPTION(hpx::error::invalid_status,
×
62
                "hpx::set_default_parcel_write_handler",
63
                "the runtime system is not operational at this point");
64
        }
×
65

66
        ///////////////////////////////////////////////////////////////////////////
67
        policies::message_handler* get_message_handler(char const* action,
159,549✔
68
            char const* type, std::size_t num, std::size_t interval,
69
            locality const& loc, error_code& ec)
70
        {
71
            return get_runtime_distributed()
319,098✔
72
                .get_parcel_handler()
159,549✔
73
                .get_message_handler(action, type, num, interval, loc, ec);
159,549✔
74
        }
75

76
        void register_message_handler(char const* message_handler_type,
×
77
            char const* action, error_code& ec)
78
        {
79
            runtime_distributed* rtd = get_runtime_distributed_ptr();
×
80
            if (nullptr != rtd)
×
81
            {
82
                return rtd->register_message_handler(
×
83
                    message_handler_type, action, ec);
×
84
            }
85

86
            // store the request for later
87
            get_message_handler_registrations().push_back(
×
88
                hpx::make_tuple(message_handler_type, action));
×
89
        }
×
90

91
        parcelset::policies::message_handler* create_message_handler(
284✔
92
            char const* message_handler_type, char const* action,
93
            parcelset::parcelport* pp, std::size_t num_messages,
94
            std::size_t interval, error_code& ec)
95
        {
96
            runtime_distributed* rtd = get_runtime_distributed_ptr();
284✔
97
            if (nullptr != rtd)
284✔
98
            {
99
                return rtd->create_message_handler(message_handler_type, action,
568✔
100
                    pp, num_messages, interval, ec);
284✔
101
            }
102

103
            HPX_THROWS_IF(ec, hpx::error::invalid_status,
×
104
                "create_message_handler",
105
                "the runtime system is not available at this time");
106
            return nullptr;
×
107
        }
284✔
108

109
        ///////////////////////////////////////////////////////////////////////
110
        void put_parcel(parcelset::parcel&& p, write_handler_type&& f)
473,471✔
111
        {
112
            parcelset::parcelhandler& ph =
473,465✔
113
                hpx::get_runtime_distributed().get_parcel_handler();
473,465✔
114
            ph.put_parcel(HPX_MOVE(p), HPX_MOVE(f));
473,471✔
115
        }
473,465✔
116

117
        void sync_put_parcel(parcelset::parcel&& p)
140✔
118
        {
119
            parcelset::parcelhandler& ph =
140✔
120
                hpx::get_runtime_distributed().get_parcel_handler();
140✔
121
            ph.sync_put_parcel(HPX_MOVE(p));
140✔
122
        }
140✔
123

124
        ///////////////////////////////////////////////////////////////////////
125
        void parcel_route_handler(
×
126
            std::error_code const& ec, parcelset::parcel const& p)
127
        {
128
            parcelhandler& ph =
×
129
                hpx::get_runtime_distributed().get_parcel_handler();
×
130

131
            // invoke the original handler
132
            ph.invoke_write_handler(ec, p);
×
133

134
            // inform termination detection of a sent message
135
            if (!p.does_termination_detection())
×
136
            {
137
                hpx::detail::dijkstra_make_black();
×
138
            }
×
139
        }
×
140
    }    // namespace detail::impl
141

142
    // initialize locality interface function pointers in parcelset modules
143
    struct HPX_EXPORT locality_interface_functions
144
    {
145
        locality_interface_functions()
488✔
146
        {
147
            detail::create_parcel = &detail::impl::create_parcel;
488✔
148
            detail::create_locality = &detail::impl::create_locality;
488✔
149
            detail::set_parcel_write_handler =
488✔
150
                &detail::impl::set_parcel_write_handler;
151

152
            detail::get_message_handler = &detail::impl::get_message_handler;
488✔
153
            detail::register_message_handler =
488✔
154
                &detail::impl::register_message_handler;
155
            detail::create_message_handler =
488✔
156
                &detail::impl::create_message_handler;
157

158
            detail::put_parcel = &detail::impl::put_parcel;
488✔
159
            detail::sync_put_parcel = &detail::impl::sync_put_parcel;
488✔
160

161
            detail::parcel_route_handler_func =
488✔
162
                &detail::impl::parcel_route_handler;
163
        }
488✔
164
    };
165

166
    locality_interface_functions& locality_init()
597✔
167
    {
168
        static locality_interface_functions locality_init_;
597✔
169
        return locality_init_;
597✔
170
    }
×
171
}    // namespace hpx::parcelset
172

173
#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