• 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

75.29
/libs/full/init_runtime/src/pre_main.cpp
1
//  Copyright (c) 2007-2022 Hartmut Kaiser
2
//  Copyright (c)      2011 Bryce Lelbach
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
#include <hpx/config.hpp>
9

10
#if defined(HPX_HAVE_DISTRIBUTED_RUNTIME)
11

12
#include <hpx/agas/addressing_service.hpp>
13
#include <hpx/collectives/barrier.hpp>
14
#include <hpx/collectives/channel_communicator.hpp>
15
#include <hpx/collectives/create_communicator.hpp>
16
#include <hpx/collectives/detail/barrier_node.hpp>
17
#include <hpx/collectives/latch.hpp>
18
#include <hpx/components_base/agas_interface.hpp>
19
#include <hpx/init_runtime/pre_main.hpp>
20
#include <hpx/modules/datastructures.hpp>
21
#include <hpx/modules/errors.hpp>
22
#include <hpx/modules/logging.hpp>
23
#include <hpx/modules/runtime_configuration.hpp>
24
#include <hpx/modules/runtime_local.hpp>
25
#include <hpx/parcelset/message_handler_fwd.hpp>
26
#include <hpx/performance_counters/agas_counter_types.hpp>
27
#include <hpx/performance_counters/parcelhandler_counter_types.hpp>
28
#include <hpx/performance_counters/threadmanager_counter_types.hpp>
29
#include <hpx/runtime_components/console_logging.hpp>
30
#include <hpx/runtime_distributed.hpp>
31
#include <hpx/runtime_distributed/applier.hpp>
32
#include <hpx/runtime_distributed/runtime_fwd.hpp>
33
#include <hpx/runtime_distributed/runtime_support.hpp>
34

35
#include <string>
36
#include <vector>
37

38
#include <hpx/config/warnings_prefix.hpp>
39

32✔
40
namespace hpx { namespace detail {
41

32✔
42
    static void garbage_collect_non_blocking()
43
    {
44
        return ::hpx::agas::garbage_collect_non_blocking();
32✔
45
    }
46

32✔
47
    static void garbage_collect()
48
    {
49
        return ::hpx::agas::garbage_collect();
50
    }
51

32✔
52
    ///////////////////////////////////////////////////////////////////////////
53
    // Install performance counter startup functions for core subsystems.
32✔
54
    static void register_counter_types()
32✔
55
    {
32✔
56
        auto& agas_client = naming::get_agas_client();
57
        performance_counters::register_agas_counter_types(agas_client);
32✔
58
        agas_client.register_server_instances();
59
        lbt_ << "(2nd stage) pre_main: registered AGAS client-side "
32✔
60
                "performance counter types";
61

32✔
62
        get_runtime_distributed().register_counter_types();
63
        lbt_ << "(2nd stage) pre_main: registered runtime performance "
32✔
64
                "counter types";
65

66
        performance_counters::register_threadmanager_counter_types(
32✔
67
            threads::get_thread_manager());
68
        lbt_ << "(2nd stage) pre_main: registered thread-manager performance "
69
                "counter types";
32✔
70

32✔
71
#if defined(HPX_HAVE_NETWORKING)
72
        performance_counters::register_parcelhandler_counter_types(
32✔
73
            applier::get_applier().get_parcel_handler());
74
        lbt_ << "(2nd stage) pre_main: registered parcelset performance "
32✔
75
                "counter types";
76
#endif
77
    }
78

32✔
79
    ///////////////////////////////////////////////////////////////////////////
80
#if defined(HPX_HAVE_NETWORKING)
32✔
81
    static void register_message_handlers()
32✔
82
    {
1,152✔
83
        runtime_distributed& rtd = get_runtime_distributed();
84
        for (auto const& t :
85
            parcelset::detail::get_message_handler_registrations())
1,088✔
86
        {
87
            error_code ec(throwmode::lightweight);
32✔
88
            rtd.register_message_handler(hpx::get<0>(t), hpx::get<1>(t), ec);
32✔
89
        }
90
        lbt_ << "(3rd stage) pre_main: registered message handlers";
91
    }
92
#endif
93

32✔
94
    ///////////////////////////////////////////////////////////////////////////
95
    // Implements second and third stage bootstrapping.
96
    int pre_main(runtime_mode mode)
97
    {
32✔
98
        // Register pre-shutdown and shutdown functions to flush pending
32✔
99
        // reference counting operations.
100
        register_pre_shutdown_function(&garbage_collect_non_blocking);
101
        register_shutdown_function(&garbage_collect);
102

32✔
103
        using components::stubs::runtime_support;
32✔
104

105
        agas::addressing_service& agas_client = naming::get_agas_client();
106
        runtime& rt = get_runtime();
32✔
107

108
        int exit_code = 0;
109
        if (runtime_mode::connect == mode)
×
110
        {
×
111
            lbt_ << "(2nd stage) pre_main: locality is in connect mode, "
112
                    "skipping 2nd and 3rd stage startup synchronization";
113
            lbt_ << "(2nd stage) pre_main: addressing services enabled";
×
114

115
            // Load components, so that we can use the barrier LCO.
×
116
            exit_code = runtime_support::load_components(find_here());
117
            lbt_ << "(2nd stage) pre_main: loaded components"
118
                 << (exit_code ? ", application exit has been requested" : "");
119

×
120
            // Work on registration requests for message handler plugins
121
#if defined(HPX_HAVE_NETWORKING)
122
            register_message_handlers();
123
#endif
×
124
            // Register all counter types before the startup functions are being
125
            // executed.
×
126
            register_counter_types();
×
127

×
128
            rt.set_state(hpx::state::pre_startup);
129
            runtime_support::call_startup_functions(find_here(), true);
×
130
            lbt_ << "(3rd stage) pre_main: ran pre-startup functions";
×
131

×
132
            rt.set_state(hpx::state::startup);
133
            runtime_support::call_startup_functions(find_here(), false);
134
            lbt_ << "(4th stage) pre_main: ran startup functions";
135
        }
32✔
136
        else
137
        {
138
            lbt_ << "(2nd stage) pre_main: addressing services enabled";
32✔
139

140
            // Load components, so that we can use the barrier LCO.
64✔
141
            exit_code = runtime_support::load_components(find_here());
142
            lbt_ << "(2nd stage) pre_main: loaded components"
143
                 << (exit_code ? ", application exit has been requested" : "");
32✔
144

145
            // Second and third stage barrier creation.
29✔
146
            if (agas_client.is_bootstrap())
29✔
147
            {
148
                naming::gid_type console_;
×
149
                if (HPX_UNLIKELY(!agas_client.get_console_locality(console_)))
150
                {
151
                    HPX_THROW_EXCEPTION(hpx::error::network_error, "pre_main",
152
                        "no console locality registered");
153
                }
29✔
154

155
                lbt_ << "(2nd stage) pre_main: creating 2nd and 3rd stage boot "
156
                        "barriers";
157
            }
158
            else    // Hosted.
3✔
159
            {
160
                lbt_ << "(2nd stage) pre_main: finding 2nd and 3rd stage boot "
161
                        "barriers";
162
            }
32✔
163

32✔
164
#if !defined(HPX_COMPUTE_DEVICE_CODE)
165
            // create predefined communicator, but only if locality is not
166
            // connecting late
167
            if (hpx::get_config_entry("hpx.runtime_mode",
168
                    get_runtime_mode_name(runtime_mode::console)) !=
32✔
169
                get_runtime_mode_name(runtime_mode::connect))
32✔
170
            {
171
                hpx::collectives::detail::create_global_communicator();
172
            }
173
#endif
32✔
174

175
            // create our global barrier...
176
            hpx::distributed::barrier::get_global_barrier() =
177
                hpx::distributed::barrier::create_global_barrier();
32✔
178

179
            // Second stage bootstrap synchronizes component loading across all
180
            // localities, ensuring that the component namespace tables are fully
181
            // populated before user code is executed.
32✔
182
            distributed::barrier::synchronize();
32✔
183
            lbt_ << "(2nd stage) pre_main: passed 2nd stage boot barrier";
184

32✔
185
            // Work on registration requests for message handler plugins
32✔
186
#if defined(HPX_HAVE_NETWORKING)
187
            register_message_handlers();
188
#endif
32✔
189
            // Register all counter types before the startup functions are being
32✔
190
            // executed.
191
            register_counter_types();
32✔
192

32✔
193
            // Second stage bootstrap synchronizes performance counter loading
194
            // across all localities.
195
            distributed::barrier::synchronize();
196
            lbt_ << "(3rd stage) pre_main: passed 3rd stage boot barrier";
197

198
            runtime_support::call_startup_functions(find_here(), true);
32✔
199
            lbt_ << "(3rd stage) pre_main: ran pre-startup functions";
32✔
200

201
            // Third stage separates pre-startup and startup function phase.
202
            distributed::barrier::synchronize();
203
            lbt_ << "(4th stage) pre_main: passed 4th stage boot barrier";
204

32✔
205
            runtime_support::call_startup_functions(find_here(), false);
32✔
206
            lbt_ << "(4th stage) pre_main: ran startup functions";
207

208
            // Forth stage bootstrap synchronizes startup functions across all
209
            // localities. This is done after component loading to guarantee that
32✔
210
            // all user code, including startup functions, are only run after the
211
            // component tables are populated.
212
            distributed::barrier::synchronize();
213
            lbt_ << "(5th stage) pre_main: passed 5th stage boot barrier";
×
214
        }
×
215

×
216
        // Enable logging. Even if we terminate at this point we will see all
217
        // pending log messages so far.
218
        components::activate_logging();
219
        lbt_ << "(last stage) pre_main: activated logging";
220

64✔
221
        // Any error in post-command line handling or any explicit --exit command
32✔
222
        // line option will cause the application to terminate at this point.
223
        if (exit_code)
224
        {
225
            // If load_components returns false, shutdown the system. This
×
226
            // essentially only happens if the command line contained --exit.
227
            runtime_support::shutdown_all(
228
                naming::get_id_from_locality_id(agas::booststrap_prefix), -1.0);
×
229
            return exit_code;
×
230
        }
×
231

232
        // Connect back to given latch if specified
233
        std::string connect_back_to(
234
            get_config_entry("hpx.on_startup.wait_on_latch", ""));
×
235
        if (!connect_back_to.empty())
236
        {
237
            lbt_ << "(6th stage) runtime::run_helper: about to "
238
                    "synchronize with latch: "
239
                 << connect_back_to;
240

32✔
241
            // inform launching process that this locality is up and running
242
            hpx::distributed::latch l;
243
            l.connect_to(connect_back_to);
32✔
244
            l.arrive_and_wait();
32✔
245

32✔
246
            lbt_ << "(6th stage) runtime::run_helper: "
32✔
247
                    "synchronized with latch: "
248
                 << connect_back_to;
249
        }
250

251
        return 0;
252
    }
253

254
    void post_main()
255
    {
256
#if !defined(HPX_COMPUTE_DEVICE_CODE)
257
        // destroy predefined communicators
258
        hpx::collectives::detail::reset_global_communicator();
259
        hpx::collectives::detail::reset_local_communicator();
260
        hpx::collectives::detail::reset_world_channel_communicator();
261
#endif
262

263
        // simply destroy global barrier
264
        auto& b = hpx::distributed::barrier::get_global_barrier();
265
        b[0].detach();
266
        b[1].detach();
267
    }
268
}}    // namespace hpx::detail
269

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