-
Notifications
You must be signed in to change notification settings - Fork 407
/
Kokkos_Core.cpp
1303 lines (1197 loc) · 50.3 KB
/
Kokkos_Core.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
//@HEADER
// ************************************************************************
//
// Kokkos v. 4.0
// Copyright (2022) National Technology & Engineering
// Solutions of Sandia, LLC (NTESS).
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
// See https://kokkos.org/LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//@HEADER
#ifndef KOKKOS_IMPL_PUBLIC_INCLUDE
#define KOKKOS_IMPL_PUBLIC_INCLUDE
#endif
#include <Kokkos_Core.hpp>
#include <impl/Kokkos_Error.hpp>
#include <impl/Kokkos_Command_Line_Parsing.hpp>
#include <impl/Kokkos_ParseCommandLineArgumentsAndEnvironmentVariables.hpp>
#include <impl/Kokkos_DeviceManagement.hpp>
#include <impl/Kokkos_ExecSpaceManager.hpp>
#include <impl/Kokkos_CPUDiscovery.hpp>
#include <algorithm>
#include <cctype>
#include <cstring>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <stack>
#include <functional>
#include <list>
#include <cerrno>
#include <random>
#include <regex>
#ifndef _WIN32
#include <unistd.h>
#else
#include <windows.h>
#endif
//----------------------------------------------------------------------------
namespace {
bool g_is_initialized = false;
bool g_is_finalized = false;
bool g_show_warnings = true;
bool g_tune_internals = false;
// When compiling with clang/LLVM and using the GNU (GCC) C++ Standard Library
// (any recent version between GCC 7.3 and GCC 9.2), std::deque SEGV's during
// the unwinding of the atexit(3C) handlers at program termination. However,
// this bug is not observable when building with GCC.
// As an added bonus, std::list<T> provides constant insertion and
// deletion time complexity, which translates to better run-time performance. As
// opposed to std::deque<T> which does not provide the same constant time
// complexity for inserts/removals, since std::deque<T> is implemented as a
// segmented array.
using hook_function_type = std::function<void()>;
std::stack<hook_function_type, std::list<hook_function_type>> finalize_hooks;
/**
* The category is only used in printing, tools
* get all metadata free of category
*/
using metadata_category_type = std::string;
using metadata_key_type = std::string;
using metadata_value_type = std::string;
std::map<metadata_category_type,
std::map<metadata_key_type, metadata_value_type>>
metadata_map;
void declare_configuration_metadata(const std::string& category,
const std::string& key,
const std::string& value) {
metadata_map[category][key] = value;
}
void combine(Kokkos::InitializationSettings& out,
Kokkos::InitializationSettings const& in) {
#define KOKKOS_IMPL_COMBINE_SETTING(NAME) \
if (in.has_##NAME()) { \
out.set_##NAME(in.get_##NAME()); \
} \
static_assert(true, "no-op to require trailing semicolon")
KOKKOS_IMPL_COMBINE_SETTING(num_threads);
KOKKOS_IMPL_COMBINE_SETTING(map_device_id_by);
KOKKOS_IMPL_COMBINE_SETTING(device_id);
KOKKOS_IMPL_COMBINE_SETTING(num_devices);
KOKKOS_IMPL_COMBINE_SETTING(skip_device);
KOKKOS_IMPL_COMBINE_SETTING(disable_warnings);
KOKKOS_IMPL_COMBINE_SETTING(tune_internals);
KOKKOS_IMPL_COMBINE_SETTING(tools_help);
KOKKOS_IMPL_COMBINE_SETTING(tools_libs);
KOKKOS_IMPL_COMBINE_SETTING(tools_args);
#undef KOKKOS_IMPL_COMBINE_SETTING
}
void combine(Kokkos::InitializationSettings& out,
Kokkos::Tools::InitArguments const& in) {
using Kokkos::Tools::InitArguments;
if (in.help != InitArguments::PossiblyUnsetOption::unset) {
out.set_tools_help(in.help == InitArguments::PossiblyUnsetOption::on);
}
if (in.lib != InitArguments::unset_string_option) {
out.set_tools_libs(in.lib);
}
if (in.args != InitArguments::unset_string_option) {
out.set_tools_args(in.args);
}
}
void combine(Kokkos::Tools::InitArguments& out,
Kokkos::InitializationSettings const& in) {
using Kokkos::Tools::InitArguments;
if (in.has_tools_help()) {
out.help = in.get_tools_help() ? InitArguments::PossiblyUnsetOption::on
: InitArguments::PossiblyUnsetOption::off;
}
if (in.has_tools_libs()) {
out.lib = in.get_tools_libs();
}
if (in.has_tools_args()) {
out.args = in.get_tools_args();
}
}
int get_device_count() {
#if defined(KOKKOS_ENABLE_CUDA)
int count;
KOKKOS_IMPL_CUDA_SAFE_CALL(cudaGetDeviceCount(&count));
return count;
#elif defined(KOKKOS_ENABLE_HIP)
int count;
KOKKOS_IMPL_HIP_SAFE_CALL(hipGetDeviceCount(&count));
return count;
#elif defined(KOKKOS_ENABLE_SYCL)
return sycl::device::get_devices(sycl::info::device_type::gpu).size();
#elif defined(KOKKOS_ENABLE_OPENACC)
return acc_get_num_devices(
Kokkos::Experimental::Impl::OpenACC_Traits::dev_type);
#elif defined(KOKKOS_ENABLE_OPENMPTARGET)
return omp_get_num_devices();
#else
Kokkos::abort("implementation bug");
return -1;
#endif
}
unsigned get_process_id() {
#ifdef _WIN32
return unsigned(GetCurrentProcessId());
#else
return unsigned(getpid());
#endif
}
bool is_valid_num_threads(int x) { return x > 0; }
bool is_valid_device_id(int x) { return x >= 0; }
bool is_valid_map_device_id_by(std::string const& x) {
return x == "mpi_rank" || x == "random";
}
} // namespace
[[nodiscard]] int Kokkos::device_id() noexcept {
#if defined(KOKKOS_ENABLE_CUDA)
return Cuda().cuda_device();
#elif defined(KOKKOS_ENABLE_HIP)
return HIP().hip_device();
#elif defined(KOKKOS_ENABLE_OPENACC)
return Experimental::OpenACC().acc_device_number();
#elif defined(KOKKOS_ENABLE_OPENMPTARGET)
return omp_get_default_device(); // FIXME_OPENMPTARGET
#elif defined(KOKKOS_ENABLE_SYCL)
return Experimental::Impl::SYCLInternal::m_syclDev;
#else
return -1;
#endif
}
[[nodiscard]] int Kokkos::num_threads() noexcept {
return DefaultHostExecutionSpace().concurrency();
}
Kokkos::Impl::ExecSpaceManager& Kokkos::Impl::ExecSpaceManager::get_instance() {
static ExecSpaceManager space_initializer = {};
return space_initializer;
}
void Kokkos::Impl::ExecSpaceManager::register_space_factory(
const std::string name, std::unique_ptr<ExecSpaceBase> space) {
exec_space_factory_list[name] = std::move(space);
}
void Kokkos::Impl::ExecSpaceManager::initialize_spaces(
const InitializationSettings& settings) {
// Note: the names of the execution spaces, used as keys in the map, encode
// the ordering of the initialization code from the old initialization stuff.
// Eventually, we may want to do something less brittle than this, but for now
// we're just preserving compatibility with the old implementation.
for (auto& to_init : exec_space_factory_list) {
to_init.second->initialize(settings);
}
}
void Kokkos::Impl::ExecSpaceManager::finalize_spaces() {
for (auto& to_finalize : exec_space_factory_list) {
to_finalize.second->finalize();
}
}
void Kokkos::Impl::ExecSpaceManager::static_fence(const std::string& name) {
for (auto& to_fence : exec_space_factory_list) {
to_fence.second->static_fence(name);
}
}
void Kokkos::Impl::ExecSpaceManager::print_configuration(std::ostream& os,
bool verbose) {
for (auto const& to_print : exec_space_factory_list) {
to_print.second->print_configuration(os, verbose);
}
}
int Kokkos::Impl::get_ctest_gpu(int local_rank) {
auto const* ctest_kokkos_device_type =
std::getenv("CTEST_KOKKOS_DEVICE_TYPE");
if (!ctest_kokkos_device_type) {
return 0;
}
auto const* ctest_resource_group_count_str =
std::getenv("CTEST_RESOURCE_GROUP_COUNT");
if (!ctest_resource_group_count_str) {
return 0;
}
// Make sure rank is within bounds of resource groups specified by CTest
auto resource_group_count = std::stoi(ctest_resource_group_count_str);
assert(local_rank >= 0);
if (local_rank >= resource_group_count) {
std::ostringstream ss;
ss << "Error: local rank " << local_rank
<< " is outside the bounds of resource groups provided by CTest. Raised"
<< " by Kokkos::Impl::get_ctest_gpu().";
throw_runtime_exception(ss.str());
}
// Get the resource types allocated to this resource group
std::ostringstream ctest_resource_group;
ctest_resource_group << "CTEST_RESOURCE_GROUP_" << local_rank;
std::string ctest_resource_group_name = ctest_resource_group.str();
auto const* ctest_resource_group_str =
std::getenv(ctest_resource_group_name.c_str());
if (!ctest_resource_group_str) {
std::ostringstream ss;
ss << "Error: " << ctest_resource_group_name << " is not specified. Raised"
<< " by Kokkos::Impl::get_ctest_gpu().";
throw_runtime_exception(ss.str());
}
// Look for the device type specified in CTEST_KOKKOS_DEVICE_TYPE
bool found_device = false;
std::string ctest_resource_group_cxx_str = ctest_resource_group_str;
std::istringstream instream(ctest_resource_group_cxx_str);
while (true) {
std::string devName;
std::getline(instream, devName, ',');
if (devName == ctest_kokkos_device_type) {
found_device = true;
break;
}
if (instream.eof() || devName.length() == 0) {
break;
}
}
if (!found_device) {
std::ostringstream ss;
ss << "Error: device type '" << ctest_kokkos_device_type
<< "' not included in " << ctest_resource_group_name
<< ". Raised by Kokkos::Impl::get_ctest_gpu().";
throw_runtime_exception(ss.str());
}
// Get the device ID
std::string ctest_device_type_upper = ctest_kokkos_device_type;
for (auto& c : ctest_device_type_upper) {
c = std::toupper(c);
}
ctest_resource_group << "_" << ctest_device_type_upper;
std::string ctest_resource_group_id_name = ctest_resource_group.str();
auto resource_str = std::getenv(ctest_resource_group_id_name.c_str());
if (!resource_str) {
std::ostringstream ss;
ss << "Error: " << ctest_resource_group_id_name
<< " is not specified. Raised by Kokkos::Impl::get_ctest_gpu().";
throw_runtime_exception(ss.str());
}
auto const* comma = std::strchr(resource_str, ',');
if (!comma || strncmp(resource_str, "id:", 3)) {
std::ostringstream ss;
ss << "Error: invalid value of " << ctest_resource_group_id_name << ": '"
<< resource_str << "'. Raised by Kokkos::Impl::get_ctest_gpu().";
throw_runtime_exception(ss.str());
}
std::string id(resource_str + 3, comma - resource_str - 3);
return std::stoi(id.c_str());
}
std::vector<int> Kokkos::Impl::get_visible_devices(
Kokkos::InitializationSettings const& settings, int device_count) {
std::vector<int> visible_devices;
char* env_visible_devices = std::getenv("KOKKOS_VISIBLE_DEVICES");
if (env_visible_devices) {
std::stringstream ss(env_visible_devices);
for (int i; ss >> i;) {
visible_devices.push_back(i);
if (ss.peek() == ',') ss.ignore();
}
for (auto id : visible_devices) {
if (id < 0) {
ss << "Error: Invalid device id '" << id
<< "' in environment variable 'KOKKOS_VISIBLE_DEVICES="
<< env_visible_devices << "'."
<< " Device id cannot be negative!"
<< " Raised by Kokkos::initialize().\n";
}
if (id >= device_count) {
ss << "Error: Invalid device id '" << id
<< "' in environment variable 'KOKKOS_VISIBLE_DEVICES="
<< env_visible_devices << "'."
<< " Device id must be smaller than the number of GPUs available"
<< " for execution '" << device_count << "'!"
<< " Raised by Kokkos::initialize().\n";
}
}
} else {
int num_devices =
settings.has_num_devices() ? settings.get_num_devices() : device_count;
if (num_devices > device_count) {
std::stringstream ss;
ss << "Error: Specified number of devices '" << num_devices
<< "' exceeds the actual number of GPUs available for execution '"
<< device_count << "'."
<< " Raised by Kokkos::initialize().\n";
Kokkos::abort(ss.str().c_str());
}
for (int i = 0; i < num_devices; ++i) {
visible_devices.push_back(i);
}
if (settings.has_skip_device()) {
if (visible_devices.size() == 1 && settings.get_skip_device() == 0) {
Kokkos::abort(
"Error: skipping the only GPU available for execution.\n"
" Raised by Kokkos::initialize().\n");
}
visible_devices.erase(
std::remove(visible_devices.begin(), visible_devices.end(),
settings.get_skip_device()),
visible_devices.end());
}
}
if (visible_devices.empty()) {
Kokkos::abort(
"Error: no GPU available for execution.\n"
" Raised by Kokkos::initialize().\n");
}
return visible_devices;
}
int Kokkos::Impl::get_gpu(const InitializationSettings& settings) {
std::vector<int> visible_devices =
get_visible_devices(settings, get_device_count());
int const num_devices = visible_devices.size();
// device_id is provided
if (settings.has_device_id()) {
int const id = settings.get_device_id();
if (id < 0) {
std::stringstream ss;
ss << "Error: Requested GPU with invalid id '" << id << "'."
<< " Device id cannot be negative!"
<< " Raised by Kokkos::initialize().\n";
Kokkos::abort(ss.str().c_str());
}
if (id >= num_devices) {
std::stringstream ss;
ss << "Error: Requested GPU with id '" << id << "' but only "
<< num_devices << "GPU(s) available!"
<< " Raised by Kokkos::initialize().\n";
Kokkos::abort(ss.str().c_str());
}
return visible_devices[settings.get_device_id()];
}
// either random or round-robin assignment based on local MPI rank
if (settings.has_map_device_id_by() &&
!is_valid_map_device_id_by(settings.get_map_device_id_by())) {
std::stringstream ss;
ss << "Error: map_device_id_by setting '" << settings.get_map_device_id_by()
<< "' is not recognized."
<< " Raised by Kokkos::initialize().\n";
Kokkos::abort(ss.str().c_str());
}
if (settings.has_map_device_id_by() &&
settings.get_map_device_id_by() == "random") {
std::default_random_engine gen(get_process_id());
std::uniform_int_distribution<int> distribution(0, num_devices - 1);
return visible_devices[distribution(gen)];
}
// either map_device_id_by is not specified or it is mpi_rank
if (settings.has_map_device_id_by() &&
settings.get_map_device_id_by() != "mpi_rank") {
Kokkos::abort("implementation bug");
}
int const mpi_local_rank = mpi_local_rank_on_node();
// use first GPU available for execution if unable to detect local MPI rank
if (mpi_local_rank < 0) {
if (settings.has_map_device_id_by()) {
std::cerr << "Warning: unable to detect local MPI rank."
<< " Falling back to the first GPU available for execution."
<< " Raised by Kokkos::initialize()." << std::endl;
}
return visible_devices[0];
}
// use device assigned by CTest when resource allocation is activated
if (std::getenv("CTEST_KOKKOS_DEVICE_TYPE") &&
std::getenv("CTEST_RESOURCE_GROUP_COUNT")) {
return get_ctest_gpu(mpi_local_rank);
}
return visible_devices[mpi_local_rank % visible_devices.size()];
}
namespace {
void initialize_backends(const Kokkos::InitializationSettings& settings) {
// This is an experimental setting
// For KNL in Flat mode this variable should be set, so that
// memkind allocates high bandwidth memory correctly.
#ifdef KOKKOS_ENABLE_HBWSPACE
setenv("MEMKIND_HBW_NODES", "1", 0);
#endif
Kokkos::Impl::ExecSpaceManager::get_instance().initialize_spaces(settings);
}
void initialize_profiling(const Kokkos::Tools::InitArguments& args) {
auto initialization_status =
Kokkos::Tools::Impl::initialize_tools_subsystem(args);
if (initialization_status.result ==
Kokkos::Tools::Impl::InitializationStatus::InitializationResult::
help_request) {
g_is_initialized = true;
::Kokkos::finalize();
std::exit(EXIT_SUCCESS);
} else if (initialization_status.result ==
Kokkos::Tools::Impl::InitializationStatus::InitializationResult::
success) {
Kokkos::Tools::parseArgs(args.args);
for (const auto& category_value : metadata_map) {
for (const auto& key_value : category_value.second) {
Kokkos::Tools::declareMetadata(key_value.first, key_value.second);
}
}
} else {
std::cerr << "Error initializing Kokkos Tools subsystem" << std::endl;
g_is_initialized = true;
::Kokkos::finalize();
std::exit(EXIT_FAILURE);
}
}
std::string version_string_from_int(int version_number) {
std::stringstream str_builder;
str_builder << version_number / 10000 << "." << (version_number % 10000) / 100
<< "." << version_number % 100;
return str_builder.str();
}
void pre_initialize_internal(const Kokkos::InitializationSettings& settings) {
if (settings.has_disable_warnings() && settings.get_disable_warnings())
g_show_warnings = false;
if (settings.has_tune_internals() && settings.get_tune_internals())
g_tune_internals = true;
declare_configuration_metadata("version_info", "Kokkos Version",
version_string_from_int(KOKKOS_VERSION));
#ifdef KOKKOS_COMPILER_APPLECC
declare_configuration_metadata("compiler_version", "KOKKOS_COMPILER_APPLECC",
std::to_string(KOKKOS_COMPILER_APPLECC));
declare_configuration_metadata("tools_only", "compiler_family", "apple");
#endif
#ifdef KOKKOS_COMPILER_CLANG
declare_configuration_metadata("compiler_version", "KOKKOS_COMPILER_CLANG",
std::to_string(KOKKOS_COMPILER_CLANG));
declare_configuration_metadata("tools_only", "compiler_family", "clang");
#endif
#ifdef KOKKOS_COMPILER_CRAYC
declare_configuration_metadata("compiler_version", "KOKKOS_COMPILER_CRAYC",
std::to_string(KOKKOS_COMPILER_CRAYC));
declare_configuration_metadata("tools_only", "compiler_family", "cray");
#endif
#ifdef KOKKOS_COMPILER_GNU
declare_configuration_metadata("compiler_version", "KOKKOS_COMPILER_GNU",
std::to_string(KOKKOS_COMPILER_GNU));
declare_configuration_metadata("tools_only", "compiler_family", "gnu");
#endif
#ifdef KOKKOS_COMPILER_INTEL
declare_configuration_metadata("compiler_version", "KOKKOS_COMPILER_INTEL",
std::to_string(KOKKOS_COMPILER_INTEL));
declare_configuration_metadata("tools_only", "compiler_family", "intel");
#endif
#ifdef KOKKOS_COMPILER_INTEL_LLVM
declare_configuration_metadata("compiler_version",
"KOKKOS_COMPILER_INTEL_LLVM",
std::to_string(KOKKOS_COMPILER_INTEL_LLVM));
declare_configuration_metadata("tools_only", "compiler_family", "intel_llvm");
#endif
#ifdef KOKKOS_COMPILER_NVCC
declare_configuration_metadata("compiler_version", "KOKKOS_COMPILER_NVCC",
std::to_string(KOKKOS_COMPILER_NVCC));
declare_configuration_metadata("tools_only", "compiler_family", "nvcc");
#endif
#ifdef KOKKOS_COMPILER_NVHPC
declare_configuration_metadata("compiler_version", "KOKKOS_COMPILER_NVHPC",
std::to_string(KOKKOS_COMPILER_NVHPC));
declare_configuration_metadata("tools_only", "compiler_family", "pgi");
#endif
#ifdef KOKKOS_COMPILER_MSVC
declare_configuration_metadata("compiler_version", "KOKKOS_COMPILER_MSVC",
std::to_string(KOKKOS_COMPILER_MSVC));
declare_configuration_metadata("tools_only", "compiler_family", "msvc");
#endif
#ifdef KOKKOS_ENABLE_PRAGMA_IVDEP
declare_configuration_metadata("vectorization", "KOKKOS_ENABLE_PRAGMA_IVDEP",
"yes");
#else
declare_configuration_metadata("vectorization", "KOKKOS_ENABLE_PRAGMA_IVDEP",
"no");
#endif
#ifdef KOKKOS_ENABLE_PRAGMA_LOOPCOUNT
declare_configuration_metadata("vectorization",
"KOKKOS_ENABLE_PRAGMA_LOOPCOUNT", "yes");
#else
declare_configuration_metadata("vectorization",
"KOKKOS_ENABLE_PRAGMA_LOOPCOUNT", "no");
#endif
#ifdef KOKKOS_ENABLE_PRAGMA_UNROLL
declare_configuration_metadata("vectorization", "KOKKOS_ENABLE_PRAGMA_UNROLL",
"yes");
#else
declare_configuration_metadata("vectorization", "KOKKOS_ENABLE_PRAGMA_UNROLL",
"no");
#endif
#ifdef KOKKOS_ENABLE_PRAGMA_VECTOR
declare_configuration_metadata("vectorization", "KOKKOS_ENABLE_PRAGMA_VECTOR",
"yes");
#else
declare_configuration_metadata("vectorization", "KOKKOS_ENABLE_PRAGMA_VECTOR",
"no");
#endif
#ifdef KOKKOS_ENABLE_HBWSPACE
declare_configuration_metadata("memory", "KOKKOS_ENABLE_HBWSPACE", "yes");
#else
declare_configuration_metadata("memory", "KOKKOS_ENABLE_HBWSPACE", "no");
#endif
#ifdef KOKKOS_ENABLE_INTEL_MM_ALLOC
declare_configuration_metadata("memory", "KOKKOS_ENABLE_INTEL_MM_ALLOC",
"yes");
#else
declare_configuration_metadata("memory", "KOKKOS_ENABLE_INTEL_MM_ALLOC",
"no");
#endif
#ifdef KOKKOS_ENABLE_ASM
declare_configuration_metadata("options", "KOKKOS_ENABLE_ASM", "yes");
#else
declare_configuration_metadata("options", "KOKKOS_ENABLE_ASM", "no");
#endif
#ifdef KOKKOS_ENABLE_CXX17
declare_configuration_metadata("options", "KOKKOS_ENABLE_CXX17", "yes");
#else
declare_configuration_metadata("options", "KOKKOS_ENABLE_CXX17", "no");
#endif
#ifdef KOKKOS_ENABLE_CXX20
declare_configuration_metadata("options", "KOKKOS_ENABLE_CXX20", "yes");
#else
declare_configuration_metadata("options", "KOKKOS_ENABLE_CXX20", "no");
#endif
#ifdef KOKKOS_ENABLE_CXX23
declare_configuration_metadata("options", "KOKKOS_ENABLE_CXX23", "yes");
#else
declare_configuration_metadata("options", "KOKKOS_ENABLE_CXX23", "no");
#endif
#ifdef KOKKOS_ENABLE_CXX26
declare_configuration_metadata("options", "KOKKOS_ENABLE_CXX26", "yes");
#else
declare_configuration_metadata("options", "KOKKOS_ENABLE_CXX26", "no");
#endif
#ifdef KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK
declare_configuration_metadata("options", "KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK",
"yes");
#else
declare_configuration_metadata("options", "KOKKOS_ENABLE_DEBUG_BOUNDS_CHECK",
"no");
#endif
#ifdef KOKKOS_ENABLE_HWLOC
declare_configuration_metadata("options", "KOKKOS_ENABLE_HWLOC", "yes");
#else
declare_configuration_metadata("options", "KOKKOS_ENABLE_HWLOC", "no");
#endif
#ifdef KOKKOS_ENABLE_LIBRT
declare_configuration_metadata("options", "KOKKOS_ENABLE_LIBRT", "yes");
#else
declare_configuration_metadata("options", "KOKKOS_ENABLE_LIBRT", "no");
#endif
#ifdef KOKKOS_ENABLE_LIBDL
declare_configuration_metadata("options", "KOKKOS_ENABLE_LIBDL", "yes");
#else
declare_configuration_metadata("options", "KOKKOS_ENABLE_LIBDL", "no");
#endif
declare_configuration_metadata("architecture", "Default Device",
typeid(Kokkos::DefaultExecutionSpace).name());
#if defined(KOKKOS_ARCH_A64FX)
declare_configuration_metadata("architecture", "CPU architecture", "A64FX");
#elif defined(KOKKOS_ARCH_AMDAVX)
declare_configuration_metadata("architecture", "CPU architecture", "AMDAVX");
#elif defined(KOKKOS_ARCH_ARMV80)
declare_configuration_metadata("architecture", "CPU architecture", "ARMV80");
#elif defined(KOKKOS_ARCH_ARMV81)
declare_configuration_metadata("architecture", "CPU architecture", "ARMV81");
#elif defined(KOKKOS_ARCH_ARMV8_THUNDERX)
declare_configuration_metadata("architecture", "CPU architecture",
"ARMV8_THUNDERX");
#elif defined(KOKKOS_ARCH_ARMV8_THUNDERX2)
declare_configuration_metadata("architecture", "CPU architecture",
"ARMV8_THUNDERX2");
#elif defined(KOKKOS_ARCH_BDW)
declare_configuration_metadata("architecture", "CPU architecture", "BDW");
#elif defined(KOKKOS_ARCH_BGQ)
declare_configuration_metadata("architecture", "CPU architecture", "BGQ");
#elif defined(KOKKOS_ARCH_HSW)
declare_configuration_metadata("architecture", "CPU architecture", "HSW");
#elif defined(KOKKOS_ARCH_ICL)
declare_configuration_metadata("architecture", "CPU architecture", "ICL");
#elif defined(KOKKOS_ARCH_ICX)
declare_configuration_metadata("architecture", "CPU architecture", "ICX");
#elif defined(KOKKOS_ARCH_KNC)
declare_configuration_metadata("architecture", "CPU architecture", "KNC");
#elif defined(KOKKOS_ARCH_KNL)
declare_configuration_metadata("architecture", "CPU architecture", "KNL");
#elif defined(KOKKOS_ARCH_NATIVE)
declare_configuration_metadata("architecture", "CPU architecture", "NATIVE");
#elif defined(KOKKOS_ARCH_POWER7)
declare_configuration_metadata("architecture", "CPU architecture", "POWER7");
#elif defined(KOKKOS_ARCH_POWER8)
declare_configuration_metadata("architecture", "CPU architecture", "POWER8");
#elif defined(KOKKOS_ARCH_POWER9)
declare_configuration_metadata("architecture", "CPU architecture", "POWER9");
#elif defined(KOKKOS_ARCH_SKL)
declare_configuration_metadata("architecture", "CPU architecture", "SKL");
#elif defined(KOKKOS_ARCH_SKX)
declare_configuration_metadata("architecture", "CPU architecture", "SKX");
#elif defined(KOKKOS_ARCH_SNB)
declare_configuration_metadata("architecture", "CPU architecture", "SNB");
#elif defined(KOKKOS_ARCH_SPR)
declare_configuration_metadata("architecture", "CPU architecture", "SPR");
#elif defined(KOKKOS_ARCH_WSM)
declare_configuration_metadata("architecture", "CPU architecture", "WSM");
#elif defined(KOKKOS_ARCH_AMD_ZEN)
declare_configuration_metadata("architecture", "CPU architecture", "AMD_ZEN");
#elif defined(KOKKOS_ARCH_AMD_ZEN2)
declare_configuration_metadata("architecture", "CPU architecture",
"AMD_ZEN2");
#elif defined(KOKKOS_ARCH_AMD_ZEN3)
declare_configuration_metadata("architecture", "CPU architecture",
"AMD_ZEN3");
#else
declare_configuration_metadata("architecture", "CPU architecture", "none");
#endif
#if defined(KOKKOS_ARCH_INTEL_GEN)
declare_configuration_metadata("architecture", "GPU architecture",
"INTEL_GEN");
#elif defined(KOKKOS_ARCH_INTEL_DG1)
declare_configuration_metadata("architecture", "GPU architecture",
"INTEL_DG1");
#elif defined(KOKKOS_ARCH_INTEL_GEN9)
declare_configuration_metadata("architecture", "GPU architecture",
"INTEL_GEN9");
#elif defined(KOKKOS_ARCH_INTEL_GEN11)
declare_configuration_metadata("architecture", "GPU architecture",
"INTEL_GEN11");
#elif defined(KOKKOS_ARCH_INTEL_GEN12LP)
declare_configuration_metadata("architecture", "GPU architecture",
"INTEL_GEN12LP");
#elif defined(KOKKOS_ARCH_INTEL_XEHP)
declare_configuration_metadata("architecture", "GPU architecture",
"INTEL_XEHP");
#elif defined(KOKKOS_ARCH_INTEL_PVC)
declare_configuration_metadata("architecture", "GPU architecture",
"INTEL_PVC");
#elif defined(KOKKOS_ARCH_KEPLER30)
declare_configuration_metadata("architecture", "GPU architecture",
"KEPLER30");
#elif defined(KOKKOS_ARCH_KEPLER32)
declare_configuration_metadata("architecture", "GPU architecture",
"KEPLER32");
#elif defined(KOKKOS_ARCH_KEPLER35)
declare_configuration_metadata("architecture", "GPU architecture",
"KEPLER35");
#elif defined(KOKKOS_ARCH_KEPLER37)
declare_configuration_metadata("architecture", "GPU architecture",
"KELPER37");
#elif defined(KOKKOS_ARCH_MAXWELL50)
declare_configuration_metadata("architecture", "GPU architecture",
"MAXWELL50");
#elif defined(KOKKOS_ARCH_MAXWELL52)
declare_configuration_metadata("architecture", "GPU architecture",
"MAXWELL52");
#elif defined(KOKKOS_ARCH_MAXWELL53)
declare_configuration_metadata("architecture", "GPU architecture",
"MAXWELL53");
#elif defined(KOKKOS_ARCH_PASCAL60)
declare_configuration_metadata("architecture", "GPU architecture",
"PASCAL60");
#elif defined(KOKKOS_ARCH_PASCAL61)
declare_configuration_metadata("architecture", "GPU architecture",
"PASCAL61");
#elif defined(KOKKOS_ARCH_VOLTA70)
declare_configuration_metadata("architecture", "GPU architecture", "VOLTA70");
#elif defined(KOKKOS_ARCH_VOLTA72)
declare_configuration_metadata("architecture", "GPU architecture", "VOLTA72");
#elif defined(KOKKOS_ARCH_TURING75)
declare_configuration_metadata("architecture", "GPU architecture",
"TURING75");
#elif defined(KOKKOS_ARCH_AMPERE80)
declare_configuration_metadata("architecture", "GPU architecture",
"AMPERE80");
#elif defined(KOKKOS_ARCH_AMPERE86)
declare_configuration_metadata("architecture", "GPU architecture",
"AMPERE86");
#elif defined(KOKKOS_ARCH_ADA89)
declare_configuration_metadata("architecture", "GPU architecture", "ADA89");
#elif defined(KOKKOS_ARCH_HOPPER90)
declare_configuration_metadata("architecture", "GPU architecture",
"HOPPER90");
#elif defined(KOKKOS_ARCH_AMD_GFX906)
declare_configuration_metadata("architecture", "GPU architecture",
"AMD_GFX906");
#elif defined(KOKKOS_ARCH_AMD_GFX908)
declare_configuration_metadata("architecture", "GPU architecture",
"AMD_GFX908");
#elif defined(KOKKOS_ARCH_AMD_GFX90A)
declare_configuration_metadata("architecture", "GPU architecture",
"AMD_GFX90A");
#elif defined(KOKKOS_ARCH_AMD_GFX1030)
declare_configuration_metadata("architecture", "GPU architecture",
"AMD_GFX1030");
#elif defined(KOKKOS_ARCH_AMD_GFX1100)
declare_configuration_metadata("architecture", "GPU architecture",
"AMD_GFX1100");
#else
declare_configuration_metadata("architecture", "GPU architecture", "none");
#endif
#ifdef KOKKOS_IMPL_32BIT
declare_configuration_metadata("architecture", "platform", "32bit");
#else
declare_configuration_metadata("architecture", "platform", "64bit");
#endif
}
void post_initialize_internal(const Kokkos::InitializationSettings& settings) {
Kokkos::Tools::InitArguments tools_init_arguments;
combine(tools_init_arguments, settings);
initialize_profiling(tools_init_arguments);
g_is_initialized = true;
if (settings.has_print_configuration() &&
settings.get_print_configuration()) {
::Kokkos::print_configuration(std::cout);
}
}
void initialize_internal(const Kokkos::InitializationSettings& settings) {
// The tool initialization is only called in post_initialize_internal.
// Pausing tools here, so that if someone has set callbacks programmatically
// these callbacks are not called inside the backend initialization, before
// the tool initialization happened.
Kokkos::Tools::Experimental::pause_tools();
pre_initialize_internal(settings);
initialize_backends(settings);
Kokkos::Tools::Experimental::resume_tools();
post_initialize_internal(settings);
}
void pre_finalize_internal() {
typename decltype(finalize_hooks)::size_type numSuccessfulCalls = 0;
while (!finalize_hooks.empty()) {
auto f = finalize_hooks.top();
try {
f();
} catch (...) {
std::cerr << "Kokkos::finalize: A finalize hook (set via "
"Kokkos::push_finalize_hook) threw an exception that it did "
"not catch."
" Per std::atexit rules, this results in std::terminate. "
"This is "
"finalize hook number "
<< numSuccessfulCalls
<< " (1-based indexing) "
"out of "
<< finalize_hooks.size()
<< " to call. Remember that "
"Kokkos::finalize calls finalize hooks in reverse order "
"from how they "
"were pushed."
<< std::endl;
std::terminate();
}
finalize_hooks.pop();
++numSuccessfulCalls;
}
Kokkos::Profiling::finalize();
}
void post_finalize_internal() {
g_is_initialized = false;
g_is_finalized = true;
g_show_warnings = true;
g_tune_internals = false;
}
void fence_internal(const std::string& name) {
Kokkos::Impl::ExecSpaceManager::get_instance().static_fence(name);
}
void print_help_message() {
auto const help_message = R"(
--------------------------------------------------------------------------------
-------------Kokkos command line arguments--------------------------------------
--------------------------------------------------------------------------------
This program is using Kokkos. You can use the following command line flags to
control its behavior:
Kokkos Core Options:
--kokkos-help : print this message
--kokkos-disable-warnings : disable kokkos warning messages
--kokkos-print-configuration : print configuration
--kokkos-tune-internals : allow Kokkos to autotune policies and declare
tuning features through the tuning system. If
left off, Kokkos uses heuristics
--kokkos-num-threads=INT : specify total number of threads to use for
parallel regions on the host.
--kokkos-device-id=INT : specify device id to be used by Kokkos.
--kokkos-map-device-id-by=(random|mpi_rank)
: strategy to select device-id automatically from
available devices.
- random: choose a random device from available.
- mpi_rank: choose device-id based on a round robin
assignment of local MPI ranks.
Works with OpenMPI, MVAPICH, SLURM, and
derived implementations.
Kokkos Tools Options:
--kokkos-tools-libs=STR : Specify which of the tools to use. Must either
be full path to library or name of library if the
path is present in the runtime library search path
(e.g. LD_LIBRARY_PATH)
--kokkos-tools-help : Query the (loaded) kokkos-tool for its command-line
option support (which should then be passed via
--kokkos-tools-args="...")
--kokkos-tools-args=STR : A single (quoted) string of options which will be
whitespace delimited and passed to the loaded
kokkos-tool as command-line arguments. E.g.
`<EXE> --kokkos-tools-args="-c input.txt"` will
pass `<EXE> -c input.txt` as argc/argv to tool
Except for --kokkos[-tools]-help, you can alternatively set the corresponding
environment variable of a flag (all letters in upper-case and underscores
instead of hyphens). For example, to disable warning messages, you can either
specify --kokkos-disable-warnings or set the KOKKOS_DISABLE_WARNINGS
environment variable to yes.
Join us on Slack, visit https://kokkosteam.slack.com
Report bugs to https://github.com/kokkos/kokkos/issues
--------------------------------------------------------------------------------
)";
std::cout << help_message << std::endl;
}
} // namespace
void Kokkos::Impl::parse_command_line_arguments(
int& argc, char* argv[], InitializationSettings& settings) {
Tools::InitArguments tools_init_arguments;
combine(tools_init_arguments, settings);
Tools::Impl::parse_command_line_arguments(argc, argv, tools_init_arguments);
combine(settings, tools_init_arguments);
int num_threads;
int device_id;
int num_devices; // deprecated
int skip_device; // deprecated
std::string map_device_id_by;
bool disable_warnings;
bool print_configuration;
bool tune_internals;
auto get_flag = [](std::string s) -> std::string {
return s.erase(s.find('='));
};
bool help_flag = false;
int iarg = 0;
while (iarg < argc) {
bool remove_flag = false;
if (check_arg(argv[iarg], "--kokkos-numa") ||
check_arg(argv[iarg], "--numa")) {
warn_deprecated_command_line_argument(get_flag(argv[iarg]));
// remove flag if prefixed with '--kokkos-'
remove_flag = std::string(argv[iarg]).find("--kokkos-") == 0;
} else if (check_arg_int(argv[iarg], "--kokkos-num-threads", num_threads) ||
check_arg_int(argv[iarg], "--num-threads", num_threads) ||
check_arg_int(argv[iarg], "--kokkos-threads", num_threads) ||
check_arg_int(argv[iarg], "--threads", num_threads)) {
if (get_flag(argv[iarg]) != "--kokkos-num-threads") {
warn_deprecated_command_line_argument(get_flag(argv[iarg]),
"--kokkos-num-threads");
}
if (!is_valid_num_threads(num_threads)) {
std::stringstream ss;
ss << "Error: command line argument '" << argv[iarg] << "' is invalid."
<< " The number of threads must be greater than or equal to one."
<< " Raised by Kokkos::initialize().\n";
Kokkos::abort(ss.str().c_str());
}
settings.set_num_threads(num_threads);
remove_flag = std::string(argv[iarg]).find("--kokkos-") == 0;
} else if (check_arg_int(argv[iarg], "--kokkos-device-id", device_id) ||
check_arg_int(argv[iarg], "--device-id", device_id) ||
check_arg_int(argv[iarg], "--kokkos-device", device_id) ||
check_arg_int(argv[iarg], "--device", device_id)) {
if (get_flag(argv[iarg]) != "--kokkos-device-id") {
warn_deprecated_command_line_argument(get_flag(argv[iarg]),
"--kokkos-device-id");
}
if (!is_valid_device_id(device_id)) {
std::stringstream ss;
ss << "Error: command line argument '" << argv[iarg] << "' is invalid."
<< " The device id must be greater than or equal to zero."
<< " Raised by Kokkos::initialize().\n";
Kokkos::abort(ss.str().c_str());
}
settings.set_device_id(device_id);
remove_flag = std::string(argv[iarg]).find("--kokkos-") == 0;
} else if (check_arg(argv[iarg], "--kokkos-num-devices") ||
check_arg(argv[iarg], "--num-devices") ||
check_arg(argv[iarg], "--kokkos-ndevices") ||
check_arg(argv[iarg], "--ndevices")) {
if (check_arg(argv[iarg], "--num-devices")) {
warn_deprecated_command_line_argument("--num-devices",
"--kokkos-num-devices");
}
if (check_arg(argv[iarg], "--ndevices")) {
warn_deprecated_command_line_argument("--ndevices",
"--kokkos-num-devices");
}
if (check_arg(argv[iarg], "--kokkos-ndevices")) {
warn_deprecated_command_line_argument("--kokkos-ndevices",
"--kokkos-num-devices");
}
warn_deprecated_command_line_argument(
"--kokkos-num-devices", "--kokkos-map-device-id-by=mpi_rank");
// Find the number of device (expecting --device=XX)
if (!((strncmp(argv[iarg], "--kokkos-num-devices=", 21) == 0) ||
(strncmp(argv[iarg], "--num-devices=", 14) == 0) ||
(strncmp(argv[iarg], "--kokkos-ndevices=", 18) == 0) ||