-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrtcx.cpp
More file actions
1399 lines (1152 loc) · 47.9 KB
/
Copy pathrtcx.cpp
File metadata and controls
1399 lines (1152 loc) · 47.9 KB
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
/*
* SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
#include <cuda.h>
#include <cuda_runtime_api.h>
#include <nvtx3/nvtx3.hpp>
#include <dlfcn.h>
#include <fcntl.h>
#include <nvJitLink.h>
#include <nvrtc.h>
#include <rtcx.hpp>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <zstd.h>
#include <atomic>
#include <cerrno>
#include <cstring>
#include <filesystem>
#include <format>
#include <numeric>
#include <source_location>
#ifndef RTCX_STATIC_LINK_LIBNVRTC
#define RTCX_STATIC_LINK_LIBNVRTC 0
#endif
#ifndef RTCX_STATIC_LINK_LIBNVJITLINK
#define RTCX_STATIC_LINK_LIBNVJITLINK 0
#endif
#define RTCX_EXPECTS(condition_, reason_, exception_type_) \
do { \
if (!(condition_)) { \
throw exception_type_{::std::format("RTCX failure at: {}:{}: {}", \
::std::source_location::current().file_name(), \
::std::source_location::current().line(), \
(reason_))}; \
} \
} while (0)
#define RTCX_FAIL(reason_, exception_type_) \
do { \
throw exception_type_{::std::format("RTCX failure at: {}:{}: {}", \
::std::source_location::current().file_name(), \
::std::source_location::current().line(), \
(reason_))}; \
} while (0)
#define RTCX_CHECK_CUDA(...) \
do { \
::CUresult result_ = (__VA_ARGS__); \
if (result_ != ::CUDA_SUCCESS) { \
char const* enum_str_; \
RTCX_EXPECTS(::rtcx::cu->GetErrorString(result_, &enum_str_) == ::CUDA_SUCCESS, \
"Unable to get CUDA error string", \
std::runtime_error); \
auto errstr_ = ::std::format("(cuda) expression `{}` failed, with error ({}): {}", \
#__VA_ARGS__, \
static_cast<::std::int64_t>(result_), \
enum_str_); \
RTCX_FAIL(errstr_, ::std::runtime_error); \
} \
} while (0)
#define RTCX_CHECK_CUDART(...) \
do { \
::cudaError_t result_ = (__VA_ARGS__); \
if (result_ != ::cudaSuccess) { \
char const* enum_name_ = ::cudaGetErrorName(result_); \
char const* enum_msg_ = ::cudaGetErrorString(result_); \
auto errstr_ = ::std::format("(cudart) expression `{}` failed, with error ({}: {}): {}", \
#__VA_ARGS__, \
static_cast<::std::int64_t>(result_), \
enum_name_, \
enum_msg_); \
RTCX_FAIL(errstr_, ::std::runtime_error); \
} \
} while (0)
#define RTCX_CHECK_NVRTC(...) \
do { \
::nvrtcResult result_ = (__VA_ARGS__); \
if (result_ != ::NVRTC_SUCCESS) { \
auto errstr_ = ::std::format("(nvrtc) expression `{}` failed, with error ({}): {}", \
#__VA_ARGS__, \
static_cast<::std::int64_t>(result_), \
::rtcx::nvrtc->GetErrorString(result_)); \
RTCX_FAIL(errstr_, ::std::runtime_error); \
} \
} while (0)
#define RTCX_CHECK_NVJITLINK(...) \
do { \
::nvJitLinkResult result_ = (__VA_ARGS__); \
if (result_ != ::NVJITLINK_SUCCESS) { \
auto errstr_ = ::std::format("(nvJitLink) expression `{}` failed, with error ({}): {}", \
#__VA_ARGS__, \
static_cast<::std::int64_t>(result_), \
::rtcx::nvJitLinkResult_string(result_)); \
RTCX_FAIL(errstr_, ::std::runtime_error); \
} \
} while (0)
#define RTCX_FUNC_RANGE() \
::nvtx3::scoped_range_in<::rtcx::nvtx_domain> rtcx_func_range__ { __func__ }
namespace rtcx {
namespace {
struct nvtx_domain {
static constexpr char const* name [[maybe_unused]] = "rtcx";
};
enum class object_type : std::uint8_t { LIBRARY, BLOB };
std::string_view object_tag(object_type type)
{
switch (type) {
case object_type::LIBRARY: return "cuLibrary";
case object_type::BLOB: return "blob";
default:
RTCX_FAIL(std::format("Unrecognized object type: ({})", static_cast<std::int64_t>(type)),
std::runtime_error);
}
}
template <typename StringType>
std::string join_strings(std::span<StringType> strings, std::string_view separator)
{
if (strings.empty()) { return {}; }
if (strings.size() == 1) { return std::string{strings[0].begin(), strings[0].end()}; }
auto total_size = std::transform_reduce(
strings.begin(),
strings.end(),
size_t{0},
[](size_t total, size_t str_size) { return total + str_size; },
[](auto& str) { return str.size(); });
auto separator_size = separator.size() * (strings.size() - 1);
std::string result;
result.reserve(total_size + separator_size);
for (size_t i = 0; i < strings.size(); ++i) {
result.append(strings[i].begin(), strings[i].end());
if (i != (strings.size() - 1)) { result.append(separator); }
}
return result;
}
} // namespace
void log_warning(std::string_view msg)
{
std::fprintf(stderr, "[rtcx] warn: %.*s\n", static_cast<int>(msg.size()), msg.data());
}
void log_error(std::string_view msg)
{
std::fprintf(stderr, "[rtcx] error: %.*s\n", static_cast<int>(msg.size()), msg.data());
}
#define FOR_EACH_CUDA_FUNC(DO_IT) \
DO_IT(GetErrorString) \
DO_IT(GetErrorName) \
DO_IT(Init) \
DO_IT(OccupancyMaxPotentialBlockSize) \
DO_IT(LaunchKernel) \
DO_IT(LaunchKernelEx) \
DO_IT(LaunchCooperativeKernel) \
DO_IT(KernelGetFunction) \
DO_IT(LibraryLoadData) \
DO_IT(LibraryLoadFromFile) \
DO_IT(LibraryGetKernel) \
DO_IT(LibraryUnload)
#define FOR_EACH_NVRTC_FUNC(DO_IT) \
DO_IT(Version) \
DO_IT(GetErrorString) \
DO_IT(CreateProgram) \
DO_IT(DestroyProgram) \
DO_IT(CompileProgram) \
DO_IT(GetPTXSize) \
DO_IT(GetPTX) \
DO_IT(GetCUBINSize) \
DO_IT(GetCUBIN) \
DO_IT(GetLTOIRSize) \
DO_IT(GetLTOIR) \
DO_IT(GetProgramLogSize) \
DO_IT(GetProgramLog) \
DO_IT(AddNameExpression) \
DO_IT(GetLoweredName)
#define FOR_EACH_NVJITLINK_FUNC(DO_IT) \
DO_IT(Version) \
DO_IT(Create) \
DO_IT(Destroy) \
DO_IT(AddData) \
DO_IT(AddFile) \
DO_IT(Complete) \
DO_IT(GetLinkedCubinSize) \
DO_IT(GetLinkedCubin) \
DO_IT(GetLinkedPtxSize) \
DO_IT(GetLinkedPtx) \
DO_IT(GetErrorLogSize) \
DO_IT(GetErrorLog) \
DO_IT(GetInfoLog) \
DO_IT(GetInfoLogSize)
namespace {
std::string_view nvJitLinkResult_string(nvJitLinkResult result)
{
switch (result) {
case NVJITLINK_SUCCESS: return "NVJITLINK_SUCCESS";
case NVJITLINK_ERROR_UNRECOGNIZED_OPTION: return "NVJITLINK_ERROR_UNRECOGNIZED_OPTION";
case NVJITLINK_ERROR_MISSING_ARCH: return "NVJITLINK_ERROR_MISSING_ARCH";
case NVJITLINK_ERROR_INVALID_INPUT: return "NVJITLINK_ERROR_INVALID_INPUT";
case NVJITLINK_ERROR_PTX_COMPILE: return "NVJITLINK_ERROR_PTX_COMPILE";
case NVJITLINK_ERROR_NVVM_COMPILE: return "NVJITLINK_ERROR_NVVM_COMPILE";
case NVJITLINK_ERROR_INTERNAL: return "NVJITLINK_ERROR_INTERNAL";
case NVJITLINK_ERROR_THREADPOOL: return "NVJITLINK_ERROR_THREADPOOL";
case NVJITLINK_ERROR_UNRECOGNIZED_INPUT: return "NVJITLINK_ERROR_UNRECOGNIZED_INPUT";
case NVJITLINK_ERROR_FINALIZE: return "NVJITLINK_ERROR_FINALIZE";
#if CUDA_VERSION >= 13000
case NVJITLINK_ERROR_NULL_INPUT: return "NVJITLINK_ERROR_NULL_INPUT";
case NVJITLINK_ERROR_INCOMPATIBLE_OPTIONS: return "NVJITLINK_ERROR_INCOMPATIBLE_OPTIONS";
case NVJITLINK_ERROR_INCORRECT_INPUT_TYPE: return "NVJITLINK_ERROR_INCORRECT_INPUT_TYPE";
case NVJITLINK_ERROR_ARCH_MISMATCH: return "NVJITLINK_ERROR_ARCH_MISMATCH";
case NVJITLINK_ERROR_OUTDATED_LIBRARY: return "NVJITLINK_ERROR_OUTDATED_LIBRARY";
case NVJITLINK_ERROR_MISSING_FATBIN: return "NVJITLINK_ERROR_MISSING_FATBIN";
case NVJITLINK_ERROR_UNRECOGNIZED_ARCH: return "NVJITLINK_ERROR_UNRECOGNIZED_ARCH";
case NVJITLINK_ERROR_UNSUPPORTED_ARCH: return "NVJITLINK_ERROR_UNSUPPORTED_ARCH";
case NVJITLINK_ERROR_LTO_NOT_ENABLED: return "NVJITLINK_ERROR_LTO_NOT_ENABLED";
#endif
default:
RTCX_FAIL(
std::format("Unrecognized nvJitLinkResult type: ({})", static_cast<std::int64_t>(result)),
std::runtime_error);
}
}
std::string_view binary_type_string(binary_type type)
{
switch (type) {
case binary_type::LTO_IR: return "LTO_IR";
case binary_type::CUBIN: return "CUBIN";
case binary_type::FATBIN: return "FATBIN";
case binary_type::PTX: return "PTX";
default:
RTCX_FAIL(std::format("Unrecognized binary_type: ({})", static_cast<std::int64_t>(type)),
std::runtime_error);
}
}
nvJitLinkInputType to_nvjitlink_input_type(binary_type bin_type)
{
switch (bin_type) {
case binary_type::LTO_IR: return NVJITLINK_INPUT_LTOIR;
case binary_type::CUBIN: return NVJITLINK_INPUT_CUBIN;
case binary_type::FATBIN: return NVJITLINK_INPUT_FATBIN;
case binary_type::PTX: return NVJITLINK_INPUT_PTX;
default:
RTCX_FAIL(std::format("Unrecognized binary type for linking: ({}) ",
static_cast<std::int64_t>(bin_type)),
std::logic_error);
}
}
[[maybe_unused]] void* load_dso(std::string_view base_name, std::span<std::string const> names)
{
for (auto& name : names) {
void* handle = ::dlopen(name.c_str(), RTLD_NOW | RTLD_LOCAL);
if (handle != nullptr) { return handle; }
}
RTCX_FAIL(
std::format(
"Failed to load dynamic library `{}` (tried: {})", base_name, join_strings(names, ", ")),
std::runtime_error);
}
[[maybe_unused]] void* get_dso_symbol(char const* lib_name, void* handle, char const* sym_name)
{
void* sym = ::dlsym(handle, sym_name);
if (sym == nullptr) {
RTCX_FAIL(
std::format(
"Failed to load symbol `{}` from `{}`, error: `{}`", sym_name, lib_name, ::dlerror()),
std::runtime_error);
}
return sym;
}
inline constexpr std::int32_t major_version(std::int32_t version) { return version / 1000; }
struct LibCuda {
void* _handle = nullptr;
#define DO_IT(func) decltype(::cu##func)* func = nullptr;
FOR_EACH_CUDA_FUNC(DO_IT)
#undef DO_IT
explicit LibCuda(void* handle) : _handle(handle) { _load_symbols(); }
LibCuda(LibCuda const&) = delete;
LibCuda(LibCuda&&) = delete;
LibCuda& operator=(LibCuda const&) = delete;
LibCuda& operator=(LibCuda&&) = delete;
~LibCuda() { ::dlclose(_handle); }
static void* _load()
{
std::string lib_names[] = {"libcuda.so.1"}; // NOLINT(modernize-avoid-c-arrays)
return load_dso("libcuda.so", lib_names);
}
private:
void _load_symbols()
{
#define DO_IT(func) \
this->func = \
reinterpret_cast<decltype(cu##func)*>(get_dso_symbol("libcuda", _handle, "cu" #func));
FOR_EACH_CUDA_FUNC(DO_IT)
#undef DO_IT
}
};
struct LibNVRTC {
void* _handle = nullptr;
#define DO_IT(func) decltype(::nvrtc##func)* func = nullptr;
FOR_EACH_NVRTC_FUNC(DO_IT)
#undef DO_IT
explicit LibNVRTC(void* handle) : _handle(handle) { _load_symbols(); }
LibNVRTC(LibNVRTC const&) = delete;
LibNVRTC(LibNVRTC&&) = delete;
LibNVRTC& operator=(LibNVRTC const&) = delete;
LibNVRTC& operator=(LibNVRTC&&) = delete;
~LibNVRTC()
{
#if !RTCX_STATIC_LINK_LIBNVRTC
::dlclose(_handle);
#endif
}
static void* _load()
{
#if !RTCX_STATIC_LINK_LIBNVRTC
auto expected_major_version = major_version(CUDA_VERSION);
std::int32_t cuda_version;
RTCX_CHECK_CUDART(::cudaRuntimeGetVersion(&cuda_version));
std::int32_t major = major_version(cuda_version);
RTCX_EXPECTS(expected_major_version == major,
std::format("LibNVRTC Compatibility Error: CUDA major version mismatch. Expected "
"major runtime version: {}, got major runtime version: {})",
expected_major_version,
major),
std::runtime_error);
std::string lib_names[] = // NOLINT(modernize-avoid-c-arrays)
{std::format("libnvrtc.so.{}", major)};
return load_dso("libnvrtc.so", lib_names);
#else
return nullptr;
#endif
}
private:
void _load_symbols()
{
#if !RTCX_STATIC_LINK_LIBNVRTC
#define DO_IT(func) \
this->func = \
reinterpret_cast<decltype(nvrtc##func)*>(get_dso_symbol("libnvrtc", _handle, "nvrtc" #func));
#else
#define DO_IT(func) this->func = ::nvrtc##func;
#endif
FOR_EACH_NVRTC_FUNC(DO_IT)
#undef DO_IT
}
};
struct LibNVJitLink {
void* _handle = nullptr;
#define DO_IT(func) decltype(::nvJitLink##func)* func = nullptr;
FOR_EACH_NVJITLINK_FUNC(DO_IT)
#undef DO_IT
explicit LibNVJitLink(void* handle) : _handle(handle) { _load_symbols(); }
LibNVJitLink(LibNVJitLink const&) = delete;
LibNVJitLink(LibNVJitLink&&) = delete;
LibNVJitLink& operator=(LibNVJitLink const&) = delete;
LibNVJitLink& operator=(LibNVJitLink&&) = delete;
~LibNVJitLink()
{
#if !RTCX_STATIC_LINK_LIBNVJITLINK
::dlclose(_handle);
#endif
}
static void* _load()
{
#if !RTCX_STATIC_LINK_LIBNVJITLINK
auto expected_major_version = major_version(CUDA_VERSION);
std::int32_t cuda_version;
RTCX_CHECK_CUDART(::cudaRuntimeGetVersion(&cuda_version));
std::int32_t major = major_version(cuda_version);
RTCX_EXPECTS(
expected_major_version == major,
std::format("LibNVJitLink Compatibility Error: CUDA major version mismatch. Expected "
"major runtime version: {}, got major runtime version: {})",
expected_major_version,
major),
std::runtime_error);
std::string lib_names[] = // NOLINT(modernize-avoid-c-arrays)
{std::format("libnvJitLink.so.{}", major)};
return load_dso("libnvJitLink.so", lib_names);
#else
return nullptr;
#endif
}
private:
void _load_symbols()
{
#if !RTCX_STATIC_LINK_LIBNVJITLINK
#define DO_IT(func) \
this->func = reinterpret_cast<decltype(nvJitLink##func)*>( \
get_dso_symbol("libnvJitLink", _handle, "nvJitLink" #func));
#else
#define DO_IT(func) this->func = ::nvJitLink##func;
#endif
FOR_EACH_NVJITLINK_FUNC(DO_IT)
#undef DO_IT
}
};
static std::optional<LibCuda> cu;
static std::optional<LibNVRTC> nvrtc;
static std::optional<LibNVJitLink> nvjitlink;
static std::optional<std::once_flag> init_libraries_flag{std::in_place};
static std::optional<std::once_flag> teardown_libraries_flag{std::in_place};
} // namespace
void initialize()
{
RTCX_FUNC_RANGE();
std::call_once(init_libraries_flag.value(), [] {
cu.emplace(LibCuda::_load());
RTCX_EXPECTS(
cu->Init(0) == CUDA_SUCCESS, "Failed to initialize CUDA driver API", std::runtime_error);
nvrtc.emplace(LibNVRTC::_load());
nvjitlink.emplace(LibNVJitLink::_load());
});
}
void teardown()
{
RTCX_FUNC_RANGE();
std::call_once(teardown_libraries_flag.value(), [] {
nvjitlink.reset();
nvrtc.reset();
cu.reset();
init_libraries_flag.reset();
teardown_libraries_flag.reset();
init_libraries_flag.emplace();
teardown_libraries_flag.emplace();
});
}
blob_t blob_t::from_buffer(byte_buffer buffer)
{
auto size = buffer.size();
auto data = buffer.release();
return blob_t::from_parts(
data, size, +[](std::uint8_t const* data, std::size_t) {
::free(const_cast<std::uint8_t*>(data));
});
}
blob_t blob_t::from_static_data(std::span<std::uint8_t const> data)
{
return blob_t::from_parts(data.data(), data.size(), blob_t::noop_deallocator);
}
namespace {
void log_nvrtc_result(compile_params const& params,
nvrtcProgram program,
nvrtcResult compile_result)
{
if (program == nullptr) { return; }
std::size_t log_size;
if (auto errc = nvrtc->GetProgramLogSize(program, &log_size); errc != NVRTC_SUCCESS) {
RTCX_FAIL(std::format("Failed to get NVRTC program log size with error ({}): {}",
static_cast<std::int64_t>(errc),
nvrtc->GetErrorString(errc)),
std::runtime_error);
}
std::vector<char> log;
if (log_size > 1) {
log.resize(log_size);
if (auto errc = nvrtc->GetProgramLog(program, log.data()); errc != NVRTC_SUCCESS) {
RTCX_FAIL(std::format("Failed to get NVRTC program log with error ({}): {}",
static_cast<std::int64_t>(errc),
nvrtc->GetErrorString(errc)),
std::runtime_error);
}
}
log.resize(log_size == 0 ? 0 : (log_size - 1));
auto status_str =
(compile_result == NVRTC_SUCCESS && !log.empty()) ? "completed with" : "failed with";
std::string headers_str;
for (auto& header : params.header_include_names) {
headers_str = std::format("{}\t{}\n", headers_str, header);
}
std::string options_str;
for (auto& option : params.options) {
options_str = std::format("{}\t{}\n", options_str, option);
}
if (log.empty()) { return; }
auto msg = std::format(
"NVRTC Compilation for `{}` {} ({}): {}.\nHeaders:\n{}\n\nOptions:\n{}\n\nLog:\n\t{}",
params.name == nullptr ? "<unnamed>" : params.name,
status_str,
static_cast<std::int64_t>(compile_result),
nvrtc->GetErrorString(compile_result),
headers_str,
options_str,
std::string_view{log.data(), log.size()});
if (compile_result != NVRTC_SUCCESS) {
log_error(msg);
} else {
log_warning(msg);
}
}
void log_nvJitLink_result(link_params const& params,
nvJitLinkHandle handle,
nvJitLinkResult link_result)
{
if (handle == nullptr) { return; }
std::size_t info_log_size;
if (auto errc = nvjitlink->GetInfoLogSize(handle, &info_log_size); errc != NVJITLINK_SUCCESS) {
RTCX_FAIL(std::format("Failed to get nvJitLink info log size with error ({}): {}",
static_cast<std::int64_t>(errc),
nvJitLinkResult_string(errc)),
std::runtime_error);
}
std::vector<char> info_log;
if (info_log_size > 1) {
info_log.resize(info_log_size);
if (auto errc = nvjitlink->GetInfoLog(handle, info_log.data()); errc != NVJITLINK_SUCCESS) {
RTCX_FAIL(std::format("Failed to get nvJitLink info log with error ({}): {}",
static_cast<std::int64_t>(errc),
nvJitLinkResult_string(errc)),
std::runtime_error);
}
}
info_log.resize(info_log_size == 0 ? 0 : (info_log_size - 1));
std::size_t error_log_size;
if (auto errc = nvjitlink->GetErrorLogSize(handle, &error_log_size); errc != NVJITLINK_SUCCESS) {
RTCX_FAIL(std::format("Failed to get nvJitLink error log size with error ({}): {}",
static_cast<std::int64_t>(errc),
nvJitLinkResult_string(errc)),
std::runtime_error);
}
std::vector<char> error_log;
if (error_log_size > 1) {
error_log.resize(error_log_size);
if (auto errc = nvjitlink->GetErrorLog(handle, error_log.data()); errc != NVJITLINK_SUCCESS) {
RTCX_FAIL(std::format("Failed to get nvJitLink error log with error ({}): {}",
static_cast<std::int64_t>(errc),
nvJitLinkResult_string(errc)),
std::runtime_error);
}
}
error_log.resize(error_log_size == 0 ? 0 : (error_log_size - 1));
if (info_log.empty() && error_log.empty()) { return; }
std::string fragments_str;
for (auto& frag : params.file_fragments) {
fragments_str = std::format("{}\t{}\n", fragments_str, frag.path);
}
for (auto& frag : params.memory_fragments) {
fragments_str =
std::format("{}\t{}\n", fragments_str, frag.name == nullptr ? "<unnamed>" : frag.name);
}
std::string link_options_str;
for (auto& option : params.link_options) {
link_options_str = std::format("{}\t{}\n", link_options_str, option);
}
auto status_str = link_result == NVJITLINK_SUCCESS ? "completed with" : "failed with";
auto msg = std::format(
"(nvJitLink) Linking for `{}` ({}) {} error code ({}): {}.\nFragments: \n{}\n"
"Link Options: \n{}\n\nInfo Log:\n\t{}\n\nError Log:\n\t{}\n\n",
params.name == nullptr ? "<unnamed>" : params.name,
binary_type_string(params.output_type),
status_str,
static_cast<std::int64_t>(link_result),
nvJitLinkResult_string(link_result),
fragments_str,
link_options_str,
std::string_view{info_log.data(), info_log.size()},
std::string_view{error_log.data(), error_log.size()});
bool needs_info_log =
std::find_if(
params.link_options.begin(), params.link_options.end(), [](std::string_view option) {
return option == "--verbose" || option == "-time";
}) != params.link_options.end();
if (link_result == NVJITLINK_SUCCESS) {
if (needs_info_log) { log_warning(msg); }
} else {
log_error(msg);
}
}
} // namespace
std::int32_t nvrtc_version()
{
RTCX_FUNC_RANGE();
std::int32_t major, minor;
RTCX_CHECK_NVRTC(nvrtc->Version(&major, &minor));
return major * 1000 + minor * 10;
}
std::int32_t nvjitlink_version()
{
RTCX_FUNC_RANGE();
std::uint32_t major, minor;
RTCX_CHECK_NVJITLINK(nvjitlink->Version(&major, &minor));
return static_cast<std::int32_t>(major * 1000 + minor * 10);
}
byte_buffer compile(compile_params const& params)
{
RTCX_FUNC_RANGE();
RTCX_EXPECTS(params.name != nullptr, "Fragment name must not be null", std::logic_error);
RTCX_EXPECTS(params.source != nullptr, "Fragment source must not be null", std::logic_error);
nvrtcProgram program = nullptr;
RTCX_CHECK_NVRTC(nvrtc->CreateProgram(&program,
params.source,
params.name,
static_cast<std::int32_t>(params.headers.size()),
params.headers.data(),
params.header_include_names.data()));
RTCX_DEFER([&] { nvrtc->DestroyProgram(&program); });
for (auto* name_expr : params.name_expressions) {
RTCX_CHECK_NVRTC(nvrtc->AddNameExpression(program, name_expr));
}
auto compile_result = nvrtc->CompileProgram(
program, static_cast<std::int32_t>(params.options.size()), params.options.data());
log_nvrtc_result(params, program, compile_result);
RTCX_CHECK_NVRTC(compile_result);
switch (params.target_type) {
case binary_type::CUBIN: {
std::size_t cubin_size;
RTCX_CHECK_NVRTC(nvrtc->GetCUBINSize(program, &cubin_size));
auto cubin = byte_buffer::make(cubin_size);
RTCX_CHECK_NVRTC(nvrtc->GetCUBIN(program, reinterpret_cast<char*>(cubin.data())));
return cubin;
} break;
case binary_type::LTO_IR: {
std::size_t lto_ir_size;
RTCX_CHECK_NVRTC(nvrtc->GetLTOIRSize(program, <o_ir_size));
auto lto_ir = byte_buffer::make(lto_ir_size);
RTCX_CHECK_NVRTC(nvrtc->GetLTOIR(program, reinterpret_cast<char*>(lto_ir.data())));
return lto_ir;
} break;
case binary_type::PTX: {
std::size_t ptx_size;
RTCX_CHECK_NVRTC(nvrtc->GetPTXSize(program, &ptx_size));
auto ptx = byte_buffer::make(ptx_size);
RTCX_CHECK_NVRTC(nvrtc->GetPTX(program, reinterpret_cast<char*>(ptx.data())));
return ptx;
} break;
default:
RTCX_FAIL(std::format("Unsupported binary type for compiling fragment: {}",
binary_type_string(params.target_type)),
std::logic_error);
}
}
kernel_occupancy_config kernel_ref::max_occupancy_config(std::size_t dynamic_shared_memory_bytes,
std::int32_t block_size_limit) const
{
std::int32_t min_grid_size;
std::int32_t block_size;
RTCX_CHECK_CUDA(cu->OccupancyMaxPotentialBlockSize(&min_grid_size,
&block_size,
reinterpret_cast<CUfunction>(handle_),
nullptr,
dynamic_shared_memory_bytes,
block_size_limit));
return kernel_occupancy_config{.min_grid_size = static_cast<std::uint32_t>(min_grid_size),
.block_size = static_cast<std::uint32_t>(block_size)};
}
void kernel_ref::launch(cuda_dim3 grid_dim,
cuda_dim3 block_dim,
std::uint32_t shared_mem_bytes,
CUstream stream,
void** kernel_params) const
{
RTCX_FUNC_RANGE();
RTCX_EXPECTS(grid_dim.is_valid(), "Grid dimensions must be greater than zero", std::logic_error);
RTCX_EXPECTS(
block_dim.is_valid(), "Block dimensions must be greater than zero", std::logic_error);
RTCX_EXPECTS(
kernel_params != nullptr, "Kernel parameters pointer must not be null", std::logic_error);
CUlaunchConfig cfg{.gridDimX = grid_dim.x,
.gridDimY = grid_dim.y,
.gridDimZ = grid_dim.z,
.blockDimX = block_dim.x,
.blockDimY = block_dim.y,
.blockDimZ = block_dim.z,
.sharedMemBytes = shared_mem_bytes,
.hStream = stream,
.attrs = nullptr,
.numAttrs = 0};
RTCX_CHECK_CUDA(
cu->LaunchKernelEx(&cfg, reinterpret_cast<CUfunction>(handle_), kernel_params, nullptr));
}
void kernel_ref::launch_cooperative(cuda_dim3 grid_dim,
cuda_dim3 block_dim,
std::uint32_t shared_mem_bytes,
CUstream stream,
void** kernel_params) const
{
RTCX_FUNC_RANGE();
RTCX_EXPECTS(grid_dim.is_valid(), "Grid dimensions must be greater than zero", std::logic_error);
RTCX_EXPECTS(
block_dim.is_valid(), "Block dimensions must be greater than zero", std::logic_error);
RTCX_EXPECTS(
kernel_params != nullptr, "Kernel parameters pointer must not be null", std::logic_error);
RTCX_CHECK_CUDA(cu->LaunchCooperativeKernel(reinterpret_cast<CUfunction>(handle_),
grid_dim.x,
grid_dim.y,
grid_dim.z,
block_dim.x,
block_dim.y,
block_dim.z,
shared_mem_bytes,
stream,
kernel_params));
}
library_t::~library_t()
{
if (handle_ != nullptr) { cu->LibraryUnload(handle_); }
}
library load_library(std::span<std::uint8_t const> binary)
{
RTCX_FUNC_RANGE();
CUlibrary handle;
RTCX_CHECK_CUDA(
cu->LibraryLoadData(&handle, binary.data(), nullptr, nullptr, 0, nullptr, nullptr, 0));
RTCX_DEFER([&] {
if (handle != nullptr) { RTCX_CHECK_CUDA(cu->LibraryUnload(handle)); }
});
auto library = std::make_shared<library_t>(handle);
handle = nullptr;
return library;
}
library load_library_from_file(char const* path)
{
RTCX_FUNC_RANGE();
RTCX_EXPECTS(path != nullptr, "Library path must not be null", std::logic_error);
CUlibrary handle;
RTCX_CHECK_CUDA(cu->LibraryLoadFromFile(&handle, path, nullptr, nullptr, 0, nullptr, nullptr, 0));
RTCX_DEFER([&] {
if (handle != nullptr) { RTCX_CHECK_CUDA(cu->LibraryUnload(handle)); }
});
auto library = std::make_shared<library_t>(handle);
handle = nullptr;
return library;
}
byte_buffer link_library(link_params const& params)
{
RTCX_FUNC_RANGE();
RTCX_EXPECTS(params.name != nullptr, "Link output name must not be null", std::logic_error);
RTCX_EXPECTS(params.output_type == binary_type::CUBIN || params.output_type == binary_type::PTX,
"Only CUBIN and PTX output types are supported for linking modules",
std::logic_error);
RTCX_EXPECTS(params.file_fragments.size() != 0 || params.memory_fragments.size() != 0,
"At least one fragment must be provided for linking",
std::logic_error);
for (auto& frag : params.file_fragments) {
RTCX_EXPECTS(frag.path != nullptr, "Fragment file path must not be empty", std::logic_error);
}
for (auto& frag : params.memory_fragments) {
RTCX_EXPECTS(
frag.data.size_bytes() > 0, "Fragment binary data must be non-empty", std::logic_error);
}
nvJitLinkHandle handle = nullptr;
RTCX_CHECK_NVJITLINK(nvjitlink->Create(&handle,
static_cast<std::uint32_t>(params.link_options.size()),
const_cast<char const**>(params.link_options.data())));
RTCX_DEFER([&] { nvjitlink->Destroy(&handle); });
for (auto& frag : params.file_fragments) {
RTCX_CHECK_NVJITLINK(nvjitlink->AddFile(handle, to_nvjitlink_input_type(frag.type), frag.path));
}
for (auto& frag : params.memory_fragments) {
RTCX_CHECK_NVJITLINK(nvjitlink->AddData(handle,
to_nvjitlink_input_type(frag.type),
frag.data.data(),
frag.data.size_bytes(),
frag.name));
}
auto link_result = nvjitlink->Complete(handle);
log_nvJitLink_result(params, handle, link_result);
RTCX_CHECK_NVJITLINK(link_result);
switch (params.output_type) {
case binary_type::CUBIN: {
std::size_t cubin_size;
RTCX_CHECK_NVJITLINK(nvjitlink->GetLinkedCubinSize(handle, &cubin_size));
auto cubin = byte_buffer::make(cubin_size);
RTCX_CHECK_NVJITLINK(nvjitlink->GetLinkedCubin(handle, cubin.data()));
return cubin;
} break;
case binary_type::PTX: {
std::size_t ptx_size;
RTCX_CHECK_NVJITLINK(nvjitlink->GetLinkedPtxSize(handle, &ptx_size));
auto ptx = byte_buffer::make(ptx_size);
RTCX_CHECK_NVJITLINK(nvjitlink->GetLinkedPtx(handle, reinterpret_cast<char*>(ptx.data())));
return ptx;
} break;
default:
RTCX_FAIL(std::format("Unsupported output binary type for linking CUDA libraries: ({})",
binary_type_string(params.output_type)),
std::runtime_error);
}
}
kernel_ref library_t::get_kernel(char const* name) const
{
RTCX_FUNC_RANGE();
CUkernel kernel;
RTCX_CHECK_CUDA(cu->LibraryGetKernel(&kernel, handle_, name));
return kernel_ref{kernel};
}
namespace {
[[noreturn]] void throw_posix(std::string_view message, std::string_view syscall_name)
{
auto errc = errno;
RTCX_FAIL(
std::format("{}. `{}` failed with {} ({})", message, syscall_name, errc, std::strerror(errc)),
std::runtime_error);
}
} // namespace
cache_t::cache_t(std::string cache_dir,
std::string tmp_dir,
cache_limits const& limits,
bool preload,
bool disable)
: enabled_{!disable},
cache_dir_{std::move(cache_dir)},
tmp_dir_{std::move(tmp_dir)},
limits_{limits},
lock_{},
blobs_cache_{limits.num_mem_blobs},
libraries_cache_{limits.num_mem_libraries},
tick_{0}
{
if (preload) { preload_from_disk(); }
}
std::string const& cache_t::get_cache_dir() { return cache_dir_; }
std::string const& cache_t::get_tmp_dir() { return tmp_dir_; }
std::optional<blob_t> blob_t::from_file(char const* path)
{
std::int32_t fd = ::open(path, O_RDONLY);
if (fd == -1) {
if (errno == ENOENT) {
return std::nullopt;
} else {
throw_posix("Failed to open RTCX cache file from disk", "open");
}
}
RTCX_DEFER([&] {
if (::close(fd) == -1) {
throw_posix("Failed to close RTCX cache file after memory-mapping", "close");
}
});
auto file_size = ::lseek(fd, 0, SEEK_END);
if (file_size == -1) { throw_posix("Failed to determine size of RTCX cache file", "lseek"); }
if (file_size == 0) {
// mmap does not support mapping zero-length files, so we return an empty blob in this case
return blob_t::from_static_data({});
}
void* map = ::mmap(nullptr, file_size, PROT_READ, MAP_SHARED, fd, 0);
if (map == MAP_FAILED) { throw_posix("Failed to memory-map RTCX cache file", "mmap"); }
auto deleter = +[](std::uint8_t const* buffer, std::size_t size) {
if (::munmap(static_cast<void*>(const_cast<std::uint8_t*>(buffer)), size) == -1) {
throw_posix("Failed to unmap RTCX cache file from memory", "munmap");
}
};
return blob_t::from_parts(static_cast<std::uint8_t const*>(map), file_size, deleter);
}
namespace {