首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何正确使用包含stdio.h的NVRTC编译程序?

如何正确使用包含stdio.h的NVRTC编译程序?
EN

Stack Overflow用户
提问于 2020-07-12 11:42:38
回答 2查看 513关注 0票数 0

我编写了一个令人惊叹的内核,它将给我带来名声和财富--如果我能让它与NVRTC一起编译的话:

代码语言:javascript
复制
#include <stdio.h>

__global__ void do_stuff() { }

我本来希望(运行时)编译器能够识别系统头,就像普通的编译器一样,这将“正常工作”(模块化任何特定于打印的机器)。或者,如果它不起作用,我就会期望stdio.h的源代码出现错误消息,因为我将传递NULLNULL作为其最后两个参数,因此“程序创建”API调用(nvrtcCreateProgram())无法提供。

然而,我得到的是:

代码语言:javascript
复制
/usr/include/stdio.h(33): catastrophic error: cannot open source file "stddef.h"

这对我来说很奇怪。这意味着运行时编译器能够在系统头中查找,但无法找到stddef.h,比如nvcc或主机端编译器能够找到。

为什么会发生这种情况,以及习语/推荐的解决方法是什么?

注意:我想要一个跨平台的解决方案,而不仅仅是在我的个人机器上工作。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-07-12 15:59:09

在"JITify“库中采取了另一种方法,这是Robert亲切地提醒我的。虽然这似乎不是很好的文档,Jitify预-包括它认为适合的各种头的处理片段。特别是对于<climits>/<limits.h>

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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";

如果将这些字符串作为标头,并以适当的名称作为键,那么您的内核很可能会编译。

事实上,可以在jitify.hpp中利用这些文件和其他迷你头来形成头文件,用于非NVRTC内核编译。那可能也很有用。

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

票数 0
EN

Stack Overflow用户

发布于 2020-07-12 11:42:38

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

--include-path=).

  • Pass

  • 将特定路径添加到stddef.h作为编译器参数(-Istddef.h的源到nvrtcCreateProgram()调用.

)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62860627

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档