/
hpx_init.cpp
1377 lines (1204 loc) · 54.2 KB
/
hpx_init.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2007-2013 Hartmut Kaiser
// Copyright (c) 2010-2011 Phillip LeBlanc, Dylan Stark
// Copyright (c) 2011 Bryce Lelbach
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <hpx/hpx.hpp>
#include <hpx/config.hpp>
#include <hpx/hpx_init.hpp>
#include <hpx/hpx_start.hpp>
#include <hpx/util/command_line_handling.hpp>
#include <hpx/util/bind_action.hpp>
#include <hpx/include/async.hpp>
#include <hpx/runtime/components/runtime_support.hpp>
#include <hpx/runtime/actions/plain_action.hpp>
#include <hpx/runtime/threads/thread_helpers.hpp>
#include <hpx/runtime/threads/policies/schedulers.hpp>
#include <hpx/runtime/applier/applier.hpp>
#include <hpx/runtime_impl.hpp>
#include <hpx/util/find_prefix.hpp>
#include <hpx/util/query_counters.hpp>
#include <hpx/util/function.hpp>
#include <hpx/util/apex.hpp>
#if !defined(BOOST_WINDOWS)
# include <signal.h>
#endif
#if defined(HPX_NATIVE_MIC) || defined(__bgq__)
# include <cstdlib>
#endif
#include <iostream>
#include <sstream>
#include <vector>
#include <new>
#include <memory>
#include <boost/asio.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/format.hpp>
#include <boost/assign/std/vector.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
///////////////////////////////////////////////////////////////////////////////
namespace hpx
{
void set_error_handlers();
}
///////////////////////////////////////////////////////////////////////////////
namespace hpx { namespace detail
{
// forward declarations only
void console_print(std::string const&);
void list_symbolic_name(std::string const&, naming::gid_type const&);
void list_component_type(std::string const&, components::component_type);
}}
HPX_PLAIN_ACTION_ID(hpx::detail::console_print,
console_print_action, hpx::actions::console_print_action_id)
HPX_PLAIN_ACTION_ID(hpx::detail::list_symbolic_name,
list_symbolic_name_action, hpx::actions::list_symbolic_name_action_id)
HPX_PLAIN_ACTION_ID(hpx::detail::list_component_type,
list_component_type_action, hpx::actions::list_component_type_action_id)
typedef
hpx::util::detail::bound_action<
list_symbolic_name_action
, hpx::util::tuple<
hpx::naming::id_type
, hpx::util::detail::placeholder<1>
, hpx::util::detail::placeholder<2>
>
>
bound_list_symbolic_name_action;
HPX_UTIL_REGISTER_FUNCTION_DECLARATION(
void(std::string const&, const hpx::naming::gid_type&)
, bound_list_symbolic_name_action
, list_symbolic_name_function)
HPX_UTIL_REGISTER_FUNCTION(
void(std::string const&, const hpx::naming::gid_type&)
, bound_list_symbolic_name_action
, list_symbolic_name_function)
typedef
hpx::util::detail::bound_action<
list_component_type_action
, hpx::util::tuple<
hpx::naming::id_type
, hpx::util::detail::placeholder<1>
, hpx::util::detail::placeholder<2>
>
>
bound_list_component_type_action;
HPX_UTIL_REGISTER_FUNCTION_DECLARATION(
void(std::string const&, hpx::components::component_type)
, bound_list_component_type_action
, list_component_type_function)
HPX_UTIL_REGISTER_FUNCTION(
void(std::string const&, hpx::components::component_type)
, bound_list_component_type_action
, list_component_type_function)
namespace hpx { namespace detail
{
///////////////////////////////////////////////////////////////////////////
// print string on the console
void console_print(std::string const& name)
{
std::cout << name << std::endl;
}
inline void print(std::string const& name, error_code& ec = throws)
{
naming::id_type console(agas::get_console_locality(ec));
if (ec) return;
hpx::async<console_print_action>(console, name).get(ec);
if (ec) return;
if (&ec != &throws)
ec = make_success_code();
}
///////////////////////////////////////////////////////////////////////////
// redirect the printing of the given counter name to the console
bool list_counter(performance_counters::counter_info const& info,
error_code& ec)
{
print(info.fullname_, ec);
return true;
}
// List the names of all registered performance counters.
void list_counter_names_header(bool skeleton)
{
// print header
print("List of available counter instances");
if (skeleton)
print("(replace '*' below with the appropriate sequence number)");
print(std::string(78, '-'));
}
void list_counter_names_minimal()
{
// list all counter names
list_counter_names_header(true);
performance_counters::discover_counter_types(&list_counter,
performance_counters::discover_counters_minimal);
}
void list_counter_names_full()
{
// list all counter names
list_counter_names_header(false);
performance_counters::discover_counter_types(&list_counter,
performance_counters::discover_counters_full);
}
///////////////////////////////////////////////////////////////////////////
// redirect the printing of the full counter info to the console
bool list_counter_info(performance_counters::counter_info const& info,
error_code& ec)
{
// compose the information to be printed for each of the counters
std::ostringstream strm;
strm << std::string(78, '-') << '\n';
strm << "fullname: " << info.fullname_ << '\n';
strm << "helptext: " << info.helptext_ << '\n';
strm << "type: "
<< performance_counters::get_counter_type_name(info.type_)
<< '\n';
boost::format fmt("%d.%d.%d\n");
strm << "version: " // 0xMMmmrrrr
<< boost::str(fmt % (info.version_ / 0x1000000) %
(info.version_ / 0x10000 % 0x100) %
(info.version_ % 0x10000));
strm << std::string(78, '-') << '\n';
print(strm.str(), ec);
if (&ec != &throws)
ec = make_success_code();
return true;
}
// List the names of all registered performance counters.
void list_counter_infos_header(bool skeleton)
{
// print header
print("Information about available counter instances");
if (skeleton)
print("(replace '*' below with the appropriate sequence number)");
print("--------------------------------------------------------");
}
void list_counter_infos_minimal()
{
// list all counter information
list_counter_infos_header(true);
performance_counters::discover_counter_types(&list_counter_info,
performance_counters::discover_counters_minimal);
}
void list_counter_infos_full()
{
// list all counter information
list_counter_infos_header(false);
performance_counters::discover_counter_types(&list_counter_info,
performance_counters::discover_counters_full);
}
///////////////////////////////////////////////////////////////////////////
void list_symbolic_name(std::string const& name, naming::gid_type const& gid)
{
std::ostringstream strm;
strm << name << ", " << gid << ", "
<< (naming::detail::has_credits(gid) ? "managed" : "unmanaged");
print(strm.str());
}
void list_symbolic_names()
{
print(std::string("List of all registered symbolic names:"));
print(std::string("--------------------------------------"));
using hpx::util::placeholders::_1;
using hpx::util::placeholders::_2;
naming::id_type console(agas::get_console_locality());
naming::get_agas_client().iterate_ids(
hpx::util::bind<list_symbolic_name_action>(console, _1, _2));
}
///////////////////////////////////////////////////////////////////////////
void list_component_type(std::string const& name,
components::component_type ctype)
{
print(boost::str(boost::format("%1%, %|40t|%2%") %
name % components::get_component_type_name(ctype)));
}
void list_component_types()
{
print(std::string("List of all registered component types:"));
print(std::string("---------------------------------------"));
using hpx::util::placeholders::_1;
using hpx::util::placeholders::_2;
naming::id_type console(agas::get_console_locality());
naming::get_agas_client().iterate_types(
hpx::util::bind<list_component_type_action>(console, _1, _2));
}
///////////////////////////////////////////////////////////////////////////
void print_counters(boost::shared_ptr<util::query_counters> const& qc)
{
try {
HPX_ASSERT(qc);
qc->start();
}
catch (...) {
std::cerr << hpx::diagnostic_information(boost::current_exception())
<< std::flush;
hpx::terminate();
}
}
}}
///////////////////////////////////////////////////////////////////////////////
namespace hpx
{
// Print stack trace and exit.
#if defined(BOOST_WINDOWS)
extern BOOL WINAPI termination_handler(DWORD ctrl_type);
#else
extern void termination_handler(int signum);
#endif
extern void new_handler();
///////////////////////////////////////////////////////////////////////////
namespace detail
{
///////////////////////////////////////////////////////////////////////
struct dump_config
{
dump_config(hpx::runtime const& rt) : rt_(boost::cref(rt)) {}
void operator()() const
{
std::cout << "Configuration after runtime start:\n";
std::cout << "----------------------------------\n";
rt_.get().get_config().dump(0, std::cout);
std::cout << "----------------------------------\n";
}
boost::reference_wrapper<hpx::runtime const> rt_;
};
///////////////////////////////////////////////////////////////////////
void handle_list_and_print_options(hpx::runtime& rt,
boost::program_options::variables_map& vm)
{
if (vm.count("hpx:list-counters")) {
// Print the names of all registered performance counters.
std::string option(vm["hpx:list-counters"].as<std::string>());
if (0 == std::string("minimal").find(option))
rt.add_startup_function(&list_counter_names_minimal);
else if (0 == std::string("full").find(option))
rt.add_startup_function(&list_counter_names_full);
else {
std::string msg ("Invalid command line option value"
"for --hpx:list-counters: ");
msg += option;
msg += ", allowed values are 'minimal' and 'full'";
throw detail::command_line_error(msg.c_str());
}
}
if (vm.count("hpx:list-counter-infos")) {
// Print info about all registered performance counters.
std::string option(vm["hpx:list-counter-infos"].as<std::string>());
if (0 == std::string("minimal").find(option))
rt.add_startup_function(&list_counter_infos_minimal);
else if (0 == std::string("full").find(option))
rt.add_startup_function(&list_counter_infos_full);
else {
std::string msg ("Invalid command line option value"
"for --hpx:list-counter-infos: ");
msg += option;
msg += ", allowed values are 'minimal' and 'full'";
throw detail::command_line_error(msg.c_str());
}
}
if (vm.count("hpx:list-symbolic-names")) {
// Print all registered symbolic names.
rt.add_startup_function(&list_symbolic_names);
}
if (vm.count("hpx:list-component-types")) {
// Print all registered component types.
rt.add_startup_function(&list_component_types);
}
if (vm.count("hpx:print-counter")) {
std::size_t interval = 0;
if (vm.count("hpx:print-counter-interval"))
interval = vm["hpx:print-counter-interval"].as<std::size_t>();
std::vector<std::string> counters =
vm["hpx:print-counter"].as<std::vector<std::string> >();
std::vector<std::string> counter_shortnames;
std::string counter_format("normal");
if (vm.count("hpx:print-counter-format")) {
counter_format = vm["hpx:print-counter-format"].as<std::string>();
if (counter_format == "csv-short"){
for (std::size_t i=0; i< counters.size() ; ++i) {
std::vector<std::string> entry;
boost::algorithm::split(entry, counters[i],
boost::algorithm::is_any_of(","),
boost::algorithm::token_compress_on);
HPX_ASSERT(entry.size() == 2);
counter_shortnames.push_back(entry[0]);
counters[i] = entry[1];
}
}
}
bool csv_header = true;
if(vm.count("hpx:no-csv-header"))
csv_header = false;
std::string destination("cout");
if (vm.count("hpx:print-counter-destination"))
destination = vm["hpx:print-counter-destination"].as<std::string>();
// schedule the query function at startup, which will schedule
// itself to run after the given interval
boost::shared_ptr<util::query_counters> qc =
boost::make_shared<util::query_counters>(
boost::ref(counters), interval, destination, counter_format,
counter_shortnames, csv_header);
// schedule to run at shutdown
rt.add_pre_shutdown_function(
boost::bind(&util::query_counters::evaluate, qc));
// schedule to start all counters
rt.add_startup_function(boost::bind(&print_counters, qc));
// register the query_counters object with the runtime system
rt.register_query_counters(qc);
}
else if (vm.count("hpx:print-counter-interval")) {
throw detail::command_line_error("Invalid command line option "
"--hpx:print-counter-interval, valid in conjunction with "
"--hpx:print-counter only");
}
else if (vm.count("hpx:print-counter-destination")) {
throw detail::command_line_error("Invalid command line option "
"--hpx:print-counter-destination, valid in conjunction with "
"--hpx:print-counter only");
}
else if (vm.count("hpx:print-counter-format")) {
throw detail::command_line_error("Invalid command line option "
"--hpx:print-counter-format, valid in conjunction with "
"--hpx:print-counter only");
}
}
void add_startup_functions(hpx::runtime& rt,
boost::program_options::variables_map& vm, runtime_mode mode,
startup_function_type const& startup,
shutdown_function_type const& shutdown)
{
if (vm.count("hpx:app-config"))
{
std::string config(vm["hpx:app-config"].as<std::string>());
rt.get_config().load_application_configuration(config.c_str());
}
if (!!startup)
rt.add_startup_function(startup);
if (!!shutdown)
rt.add_shutdown_function(shutdown);
// Add startup function related to listing counter names or counter
// infos (on console only).
if (mode == runtime_mode_console)
handle_list_and_print_options(rt, vm);
// Dump the configuration before all components have been loaded.
if (vm.count("hpx:dump-config-initial")) {
std::cout << "Configuration after runtime construction:\n";
std::cout << "-----------------------------------------\n";
rt.get_config().dump(0, std::cout);
std::cout << "-----------------------------------------\n";
}
// Dump the configuration after all components have been loaded.
if (vm.count("hpx:dump-config"))
rt.add_startup_function(dump_config(rt));
}
// helper functions testing option compatibility
void ensure_high_priority_compatibility(
boost::program_options::variables_map const& vm)
{
if (vm.count("hpx:high-priority-threads")) {
throw detail::command_line_error("Invalid command line option "
"--hpx:high-priority-threads, valid for "
"--hpx:queuing=local-priority and "
"--hpx:queuing=abp-priority only");
}
}
void ensure_numa_sensitivity_compatibility(
boost::program_options::variables_map const& vm)
{
if (vm.count("hpx:numa-sensitive")) {
throw detail::command_line_error("Invalid command line option "
"--hpx:numa-sensitive, valid for "
"--hpx:queuing=local, --hpx:queuing=local-priority, or "
"--hpx:queuing=abp-priority only");
}
}
void ensure_hierarchy_arity_compatibility(
boost::program_options::variables_map const& vm)
{
if (vm.count("hpx:hierarchy-arity")) {
throw detail::command_line_error("Invalid command line option "
"--hpx:hierarchy-arity, valid for "
"--hpx:queuing=hierarchy only.");
}
}
void ensure_queuing_option_compatibility(
boost::program_options::variables_map const& vm)
{
ensure_high_priority_compatibility(vm);
ensure_numa_sensitivity_compatibility(vm);
ensure_hierarchy_arity_compatibility(vm);
}
void ensure_hwloc_compatibility(
boost::program_options::variables_map const& vm)
{
#if defined(HPX_HAVE_HWLOC)
// pu control is available for HWLOC only
if (vm.count("hpx:pu-offset")) {
throw detail::command_line_error("Invalid command line option "
"--hpx:pu-offset, valid for --hpx:queuing=priority or "
"--hpx:queuing=local-priority only.");
}
if (vm.count("hpx:pu-step")) {
throw detail::command_line_error("Invalid command line option "
"--hpx:pu-step, valid for --hpx:queuing=abp-priority, "
"--hpx:queuing=periodic-priority, or "
"--hpx:queuing=local-priority only.");
}
#endif
#if defined(HPX_HAVE_HWLOC)
// affinity control is available for HWLOC only
if (vm.count("hpx:affinity")) {
throw detail::command_line_error("Invalid command line option "
"--hpx:affinity, valid for --hpx:queuing=abp-priority, "
"--hpx:queuing=periodic-priority, or "
"--hpx:queuing=local-priority only.");
}
if (vm.count("hpx:bind")) {
throw detail::command_line_error("Invalid command line option "
"--hpx:bind, valid for --hpx:queuing=abp-priority, "
"--hpx:queuing=periodic-priority, or "
"--hpx:queuing=local-priority only.");
}
if (vm.count("hpx:print-bind")) {
throw detail::command_line_error("Invalid command line option "
"--hpx:print-bind, valid for --hpx:queuing=abp-priority, "
"--hpx:queuing=periodic-priority, or "
"--hpx:queuing=local-priority only.");
}
#endif
}
///////////////////////////////////////////////////////////////////////
std::string get_affinity_domain(util::command_line_handling const& cfg)
{
std::string affinity_domain("pu");
#if defined(HPX_HAVE_HWLOC)
if (cfg.affinity_domain_ != "pu")
{
affinity_domain = cfg.affinity_domain_;
if (0 != std::string("pu").find(affinity_domain) &&
0 != std::string("core").find(affinity_domain) &&
0 != std::string("numa").find(affinity_domain) &&
0 != std::string("machine").find(affinity_domain))
{
throw detail::command_line_error("Invalid command line option "
"--hpx:affinity, value must be one of: pu, core, numa, "
"or machine.");
}
}
#endif
return affinity_domain;
}
std::size_t get_affinity_description(
util::command_line_handling const& cfg, std::string& affinity_desc)
{
#if defined(HPX_HAVE_HWLOC)
if (cfg.affinity_bind_.empty())
return cfg.numa_sensitive_;
if (cfg.pu_offset_ != 0 || cfg.pu_step_ != 1 ||
cfg.affinity_domain_ != "pu")
{
throw detail::command_line_error(
"Command line option --hpx:bind "
"should not be used with --hpx:pu-step, --hpx:pu-offset, "
"or --hpx:affinity.");
}
affinity_desc = cfg.affinity_bind_;
#endif
return cfg.numa_sensitive_;
}
std::size_t get_pu_offset(util::command_line_handling const& cfg)
{
std::size_t pu_offset = -1;
#if defined(HPX_HAVE_HWLOC)
if (cfg.pu_offset_ != 0) {
pu_offset = cfg.pu_offset_;
if (pu_offset >= hpx::threads::hardware_concurrency()) {
throw detail::command_line_error(
"Invalid command line option "
"--hpx:pu-offset, value must be smaller than number of "
"available processing units.");
}
}
#endif
return pu_offset;
}
std::size_t get_pu_step(util::command_line_handling const& cfg)
{
std::size_t pu_step = 1;
#if defined(HPX_HAVE_HWLOC)
if (cfg.pu_step_ != 1) {
pu_step = cfg.pu_step_;
if (pu_step == 0 || pu_step >= hpx::threads::hardware_concurrency())
{
throw detail::command_line_error(
"Invalid command line option "
"--hpx:pu-step, value must be non-zero and smaller than "
"number of available processing units.");
}
}
#endif
return pu_step;
}
std::size_t get_num_high_priority_queues(
util::command_line_handling const& cfg)
{
std::size_t num_high_priority_queues = cfg.num_threads_;
if (cfg.vm_.count("hpx:high-priority-threads")) {
num_high_priority_queues =
cfg.vm_["hpx:high-priority-threads"].as<std::size_t>();
if (num_high_priority_queues > cfg.num_threads_)
{
throw detail::command_line_error(
"Invalid command line option: "
"number of high priority threads ("
"--hpx:high-priority-threads), should not be larger "
"than number of threads (--hpx:threads)");
}
}
return num_high_priority_queues;
}
///////////////////////////////////////////////////////////////////////
int run(hpx::runtime& rt,
util::function_nonser<int(boost::program_options::variables_map& vm)>
const& f,
boost::program_options::variables_map& vm, runtime_mode mode,
startup_function_type const& startup,
shutdown_function_type const& shutdown)
{
add_startup_functions(rt, vm, mode, startup, shutdown);
// Run this runtime instance using the given function f.
if (!f.empty())
return rt.run(boost::bind(f, vm));
// Run this runtime instance without an hpx_main
return rt.run();
}
int start(hpx::runtime& rt,
util::function_nonser<int(boost::program_options::variables_map& vm)>
const& f,
boost::program_options::variables_map& vm, runtime_mode mode,
startup_function_type const& startup,
shutdown_function_type const& shutdown)
{
add_startup_functions(rt, vm, mode, startup, shutdown);
if (!f.empty()) {
// Run this runtime instance using the given function f.
return rt.start(boost::bind(f, vm));
}
// Run this runtime instance without an hpx_main
return rt.start();
}
int run_or_start(bool blocking, std::unique_ptr<hpx::runtime> rt,
util::command_line_handling& cfg,
startup_function_type const& startup,
shutdown_function_type const& shutdown)
{
if (blocking) {
return run(*rt, cfg.hpx_main_f_, cfg.vm_, cfg.mode_, startup,
shutdown);
}
// non-blocking version
start(*rt, cfg.hpx_main_f_, cfg.vm_, cfg.mode_, startup, shutdown);
rt.release(); // pointer to runtime is stored in TLS
return 0;
}
///////////////////////////////////////////////////////////////////////
// local scheduler (one queue for each OS threads)
int run_local(startup_function_type const& startup,
shutdown_function_type const& shutdown,
util::command_line_handling& cfg, bool blocking)
{
#if defined(HPX_HAVE_LOCAL_SCHEDULER)
ensure_high_priority_compatibility(cfg.vm_);
ensure_hierarchy_arity_compatibility(cfg.vm_);
std::size_t pu_offset = get_pu_offset(cfg);
std::size_t pu_step = get_pu_step(cfg);
std::string affinity_domain = get_affinity_domain(cfg);
std::string affinity_desc;
std::size_t numa_sensitive =
get_affinity_description(cfg, affinity_desc);
// scheduling policy
typedef hpx::threads::policies::local_queue_scheduler<>
local_queue_policy;
local_queue_policy::init_parameter_type init(
cfg.num_threads_, 1000, numa_sensitive);
threads::policies::init_affinity_data affinity_init(
pu_offset, pu_step, affinity_domain, affinity_desc);
// Build and configure this runtime instance.
typedef hpx::runtime_impl<local_queue_policy> runtime_type;
std::unique_ptr<hpx::runtime> rt(
new runtime_type(cfg.rtcfg_, cfg.mode_, cfg.num_threads_, init,
affinity_init));
return run_or_start(blocking, std::move(rt), cfg, startup, shutdown);
#else
throw detail::command_line_error("Command line option "
"--hpx:queuing=local "
"is not configured in this build. Please rebuild with "
"'cmake -DHPX_THREAD_SCHEDULERS=local'.");
#endif
}
///////////////////////////////////////////////////////////////////////
// local scheduler (one queue for each OS threads)
int run_throttle(startup_function_type const& startup,
shutdown_function_type const& shutdown,
util::command_line_handling& cfg, bool blocking)
{
#if defined(HPX_HAVE_THROTTLE_SCHEDULER)
ensure_high_priority_compatibility(cfg.vm_);
ensure_hierarchy_arity_compatibility(cfg.vm_);
bool numa_sensitive = false;
if (cfg.vm_.count("hpx:numa-sensitive"))
numa_sensitive = true;
std::size_t pu_offset = std::size_t(-1);
std::size_t pu_step = 1;
std::string affinity_domain("pu");
std::string affinity_desc;
#if defined(HPX_HAVE_HWLOC) || defined(BOOST_WINDOWS)
if (cfg.vm_.count("hpx:pu-offset")) {
pu_offset = cfg.vm_["hpx:pu-offset"].as<std::size_t>();
if (pu_offset >= hpx::threads::hardware_concurrency()) {
throw std::logic_error("Invalid command line option "
"--hpx:pu-offset, value must be smaller than number of "
"available processing units.");
}
}
if (cfg.vm_.count("hpx:pu-step")) {
pu_step = cfg.vm_["hpx:pu-step"].as<std::size_t>();
if (pu_step == 0 || pu_step >= hpx::threads::hardware_concurrency()) {
throw std::logic_error("Invalid command line option "
"--hpx:pu-step, value must be non-zero smaller than number of "
"available processing units.");
}
}
#endif
#if defined(HPX_HAVE_HWLOC)
if (cfg.vm_.count("hpx:affinity")) {
affinity_domain = cfg.vm_["hpx:affinity"].as<std::string>();
if (0 != std::string("pu").find(affinity_domain) &&
0 != std::string("core").find(affinity_domain) &&
0 != std::string("numa").find(affinity_domain) &&
0 != std::string("machine").find(affinity_domain))
{
throw std::logic_error("Invalid command line option "
"--hpx:affinity, value must be one of: pu, core, numa, "
"or machine.");
}
}
if (cfg.vm_.count("hpx:bind")) {
if (cfg.vm_.count("hpx:pu-offset") ||
cfg.vm_.count("hpx:pu-step") ||
cfg.vm_.count("hpx:affinity"))
{
throw std::logic_error("Command line option --hpx:bind "
"should not be used with --hpx:pu-step, --hpx:pu-offset, "
"or --hpx:affinity.");
}
std::vector<std::string> bind_affinity =
cfg.vm_["hpx:bind"].as<std::vector<std::string> >();
for(std::string const& s: bind_affinity)
{
if (!affinity_desc.empty())
affinity_desc += ";";
affinity_desc += s;
}
numa_sensitive = true;
}
#endif
// scheduling policy
typedef hpx::threads::policies::throttle_queue_scheduler<>
throttle_queue_policy;
throttle_queue_policy::init_parameter_type init(
cfg.num_threads_, 1000, numa_sensitive);
threads::policies::init_affinity_data affinity_init(
pu_offset, pu_step, affinity_domain, affinity_desc);
// Build and configure this runtime instance.
typedef hpx::runtime_impl<throttle_queue_policy> runtime_type;
std::unique_ptr<hpx::runtime> rt(
new runtime_type(cfg.rtcfg_, cfg.mode_, cfg.num_threads_, init,
affinity_init));
if (blocking) {
return run(*rt, cfg.hpx_main_f_, cfg.vm_, cfg.mode_, startup,
shutdown);
}
// non-blocking version
start(*rt, cfg.hpx_main_f_, cfg.vm_, cfg.mode_, startup, shutdown);
rt.release(); // pointer to runtime is stored in TLS
return 0;
#else
throw detail::command_line_error("Command line option "
"--hpx:queuing=throttle "
"is not configured in this build. Please rebuild with "
"'cmake -DHPX_THREAD_SCHEDULERS=throttle'.");
#endif
}
///////////////////////////////////////////////////////////////////////
// local static scheduler with priority queue (one queue for each OS
// threads plus one separate queue for high priority HPX-threads). Doesn't
// steal.
int run_static_priority(startup_function_type const& startup,
shutdown_function_type const& shutdown,
util::command_line_handling& cfg, bool blocking)
{
#if defined(HPX_HAVE_STATIC_PRIORITY_SCHEDULER)
ensure_hierarchy_arity_compatibility(cfg.vm_);
std::size_t num_high_priority_queues =
get_num_high_priority_queues(cfg);
std::size_t pu_offset = get_pu_offset(cfg);
std::size_t pu_step = get_pu_step(cfg);
std::string affinity_domain = get_affinity_domain(cfg);
std::string affinity_desc;
get_affinity_description(cfg, affinity_desc);
// scheduling policy
typedef hpx::threads::policies::static_priority_queue_scheduler<>
local_queue_policy;
local_queue_policy::init_parameter_type init(
cfg.num_threads_, num_high_priority_queues, 1000);
threads::policies::init_affinity_data affinity_init(
pu_offset, pu_step, affinity_domain, affinity_desc);
// Build and configure this runtime instance.
typedef hpx::runtime_impl<local_queue_policy> runtime_type;
std::unique_ptr<hpx::runtime> rt(
new runtime_type(cfg.rtcfg_, cfg.mode_, cfg.num_threads_, init,
affinity_init));
return run_or_start(blocking, std::move(rt), cfg, startup, shutdown);
#else
throw detail::command_line_error("Command line option "
"--hpx:queuing=static-priority "
"is not configured in this build. Please rebuild with "
"'cmake -DHPX_THREAD_SCHEDULERS=static-priority'.");
#endif
}
///////////////////////////////////////////////////////////////////////
// local static scheduler with priority queue (one queue for each OS
// threads plus one separate queue for high priority HPX-threads). Doesn't
// steal.
int run_static(startup_function_type const& startup,
shutdown_function_type const& shutdown,
util::command_line_handling& cfg, bool blocking)
{
#if defined(HPX_HAVE_STATIC_SCHEDULER)
ensure_high_priority_compatibility(cfg.vm_);
ensure_hierarchy_arity_compatibility(cfg.vm_);
std::size_t pu_offset = get_pu_offset(cfg);
std::size_t pu_step = get_pu_step(cfg);
std::string affinity_domain = get_affinity_domain(cfg);
std::string affinity_desc;
get_affinity_description(cfg, affinity_desc);
// scheduling policy
typedef hpx::threads::policies::static_queue_scheduler<>
local_queue_policy;
local_queue_policy::init_parameter_type init(
cfg.num_threads_, 1000);
threads::policies::init_affinity_data affinity_init(
pu_offset, pu_step, affinity_domain, affinity_desc);
// Build and configure this runtime instance.
typedef hpx::runtime_impl<local_queue_policy> runtime_type;
std::unique_ptr<hpx::runtime> rt(
new runtime_type(cfg.rtcfg_, cfg.mode_, cfg.num_threads_, init,
affinity_init));
return run_or_start(blocking, std::move(rt), cfg, startup, shutdown);
#else
throw detail::command_line_error("Command line option "
"--hpx:queuing=static "
"is not configured in this build. Please rebuild with "
"'cmake -DHPX_THREAD_SCHEDULERS=static'.");
#endif
}
///////////////////////////////////////////////////////////////////////
// local scheduler with priority queue (one queue for each OS threads
// plus one separate queue for high priority HPX-threads)
int run_priority_local(startup_function_type const& startup,
shutdown_function_type const& shutdown,
util::command_line_handling& cfg, bool blocking)
{
ensure_hierarchy_arity_compatibility(cfg.vm_);
std::size_t num_high_priority_queues =
get_num_high_priority_queues(cfg);
std::size_t pu_offset = get_pu_offset(cfg);
std::size_t pu_step = get_pu_step(cfg);
std::string affinity_domain = get_affinity_domain(cfg);
std::string affinity_desc;
std::size_t numa_sensitive =
get_affinity_description(cfg, affinity_desc);
// scheduling policy
typedef hpx::threads::policies:: local_priority_queue_scheduler<>
local_queue_policy;
local_queue_policy::init_parameter_type init(
cfg.num_threads_, num_high_priority_queues, 1000,
numa_sensitive);
threads::policies::init_affinity_data affinity_init(
pu_offset, pu_step, affinity_domain, affinity_desc);
// Build and configure this runtime instance.
typedef hpx::runtime_impl<local_queue_policy> runtime_type;
std::unique_ptr<hpx::runtime> rt(
new runtime_type(cfg.rtcfg_, cfg.mode_, cfg.num_threads_, init,
affinity_init));
return run_or_start(blocking, std::move(rt), cfg, startup, shutdown);
}
///////////////////////////////////////////////////////////////////////
// priority abp scheduler: local priority deques for each OS thread,
// with work stealing from the "bottom" of each.
int run_priority_abp(startup_function_type const& startup,
shutdown_function_type const& shutdown,
util::command_line_handling& cfg, bool blocking)
{
#if defined(HPX_HAVE_ABP_SCHEDULER)
ensure_hierarchy_arity_compatibility(cfg.vm_);
ensure_hwloc_compatibility(cfg.vm_);
std::size_t num_high_priority_queues =
get_num_high_priority_queues(cfg);
// scheduling policy
typedef hpx::threads::policies::abp_fifo_priority_queue_scheduler
abp_priority_queue_policy;
abp_priority_queue_policy::init_parameter_type init(
cfg.num_threads_, num_high_priority_queues, 1000,
cfg.numa_sensitive_);
// Build and configure this runtime instance.
typedef hpx::runtime_impl<abp_priority_queue_policy> runtime_type;
std::unique_ptr<hpx::runtime> rt(
new runtime_type(cfg.rtcfg_, cfg.mode_, cfg.num_threads_, init));
return run_or_start(blocking, std::move(rt), cfg, startup, shutdown);
#else
throw detail::command_line_error("Command line option "
"--hpx:queuing=abp-priority "
"is not configured in this build. Please rebuild with "
"'cmake -DHPX_THREAD_SCHEDULERS=abp-priority'.");
#endif
}
///////////////////////////////////////////////////////////////////////
// hierarchical scheduler: The thread queues are built up hierarchically
// this avoids contention during work stealing
int run_hierarchy(startup_function_type const& startup,
shutdown_function_type const& shutdown,
util::command_line_handling& cfg, bool blocking)
{
#if defined(HPX_HAVE_HIERARCHY_SCHEDULER)