如何在 NVRTC-compiled 程序中正确使用 include stdio.h?

How to properly use include stdio.h in an NVRTC-compiled program?

我写了一个很棒的内核,它将给我带来名望和财富——如果我只能用 NVRTC 编译它的话:

#include <stdio.h>

__global__ void do_stuff() { }

我希望系统 headers 应该被(运行时)编译器识别,就像常规编译器一样,并且这将“正常工作”(对任何 printf-specific 机器取模) .或者,如果它不起作用,我会预料到关于“程序创建”API 调用 (nvrtcCreateProgram()) 的 stdio.h 源代码不可用的错误消息,因为我我正在传递 NULLNULL 作为它的最后两个参数。

然而,我得到的是:

/usr/include/stdio.h(33): catastrophic error: cannot open source file "stddef.h"

这对我来说似乎很奇怪。这意味着运行时编译器能够查看系统内部headers,但不能能够找到stddef.h,像 nvcc 或主机端编译器都可以。

为什么会这样,idiomatic/recommended 解决方法是什么?

注意:我想要一个 cross-platform 的解决方法,而不仅仅是在我个人的机器上工作。

这里有两个可能有效的解决方案,但我宁愿避免。如果它们毕竟是唯一合理的行动方案 - 请发表评论并这样说:

  1. 将特定路径添加到 stddef.h 作为编译器参数(-I--include-path=)。
  2. stddef.h 的来源传递给 nvrtcCreateProgram() 调用。

“JITify”库中采用了另一种方法,Robert Crovella 亲切地提醒我。虽然这似乎不是很好的文档,但 Jitify pre-includes 处理了它认为合适的各种 headers 的片段。特别是 <climits>/<limits.h>:

static const char* jitsafe_header_limits_h = R"(
#pragma once
#if defined _WIN32 || defined _WIN64
 #define __WORDSIZE 32
#else
 #if defined __x86_64__ && !defined __ILP32__
  #define __WORDSIZE 64
 #else
  #define __WORDSIZE 32
 #endif
#endif
#define MB_LEN_MAX  16
#define CHAR_BIT    8
#define SCHAR_MIN   (-128)
#define SCHAR_MAX   127
#define UCHAR_MAX   255
enum {
  _JITIFY_CHAR_IS_UNSIGNED = (char)-1 >= 0,
  CHAR_MIN = _JITIFY_CHAR_IS_UNSIGNED ? 0 : SCHAR_MIN,
  CHAR_MAX = _JITIFY_CHAR_IS_UNSIGNED ? UCHAR_MAX : SCHAR_MAX,
};
#define SHRT_MIN    (-32768)
#define SHRT_MAX    32767
#define USHRT_MAX   65535
#define INT_MIN     (-INT_MAX - 1)
#define INT_MAX     2147483647
#define UINT_MAX    4294967295U
#if __WORDSIZE == 64
 # define LONG_MAX  9223372036854775807L
#else
 # define LONG_MAX  2147483647L
#endif
#define LONG_MIN    (-LONG_MAX - 1L)
#if __WORDSIZE == 64
 #define ULONG_MAX  18446744073709551615UL
#else
 #define ULONG_MAX  4294967295UL
#endif
#define LLONG_MAX  9223372036854775807LL
#define LLONG_MIN  (-LLONG_MAX - 1LL)
#define ULLONG_MAX 18446744073709551615ULL
)";

对于 stddef.h:

static const char* jitsafe_header_stddef_h =
    "#pragma once\n"
    "#include <climits>\n"
    "namespace __jitify_stddef_ns {\n"
    "#if __cplusplus >= 201103L\n"
    "typedef decltype(nullptr) nullptr_t;\n"
    "#if defined(_MSC_VER)\n"
    "  typedef double max_align_t;\n"
    "#elif defined(__APPLE__)\n"
    "  typedef long double max_align_t;\n"
    "#else\n"
    "  // Define max_align_t to match the GCC definition.\n"
    "  typedef struct {\n"
    "    long long __jitify_max_align_nonce1\n"
    "        __attribute__((__aligned__(__alignof__(long long))));\n"
    "    long double __jitify_max_align_nonce2\n"
    "        __attribute__((__aligned__(__alignof__(long double))));\n"
    "  } max_align_t;\n"
    "#endif\n"
    "#endif  // __cplusplus >= 201103L\n"
    "#if __cplusplus >= 201703L\n"
    "enum class byte : unsigned char {};\n"
    "#endif  // __cplusplus >= 201703L\n"
    "} // namespace __jitify_stddef_ns\n"
    "namespace std {\n"
    "  // NVRTC provides built-in definitions of ::size_t and ::ptrdiff_t.\n"
    "  using ::size_t;\n"
    "  using ::ptrdiff_t;\n"
    "  using namespace __jitify_stddef_ns;\n"
    "} // namespace std\n"
    "using namespace __jitify_stddef_ns;\n";

stdio.h:

static const char* jitsafe_header_stdio_h =
    "#pragma once\n"
    "#include <stddef.h>\n"
    "#define FILE int\n"
    "int fflush ( FILE * stream );\n"
    "int fprintf ( FILE * stream, const char * format, ... );\n";

如果您将这些字符串包含为 headers,并使用适当的名称作为键,您的内核很可能会编译。

事实上,可以从这些文件和 jitify.hpp 中的其他 mini-headers 中形成 header 文件,以用于 non-NVRTC 内核编译。这也可能有用。

最后一点:上面的常量没有指定__device__执行space。因此,除非另有说明,否则您要么在其中添加 __device__ ,要么告诉编译器假定函数仅用于在设备上执行;那是 --device-as-default-execution-space NVRTC 编译器选项。