如何包含标准CUDA库以与NVRTC代码链接? [英] How do you include standard CUDA libraries to link with NVRTC code?

查看:257
本文介绍了如何包含标准CUDA库以与NVRTC代码链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具体地说,我的问题是我有需要运行<curand_kernel.h>的CUDA代码. NVRTC默认不包括此功能.大概然后在创建程序上下文(即对nvrtcCreateProgram的调用)时,我必须发送文件名(curand_kernel.h)以及curand_kernel.h的源代码?我觉得我不必这样做.

很难说;我还没有从NVIDIA找到一个例子,有人需要像这样的标准CUDA文件作为源,所以我真的不明白语法是什么.一些问题:curand_kernel.h还包括...我是否必须对每个问题都做同样的事情?我什至不确定NVRTC编译器是否可以在curand_kernel.h上正确运行,因为它不支持某些语言功能,不是吗?

下一步:如果您已将头文件的源代码发送到nvrtcCreateProgram,我是否还必须在要执行的代码中将它保存在#include中?如果这样做,是否会导致错误?/p>

一个指向执行此操作或类似操作的示例代码的链接将比一个简单的答案更受赞赏;我真的没有找到任何东西.

解决方案

您必须分别发送文件名"和每个标头的源.

预处理器执行其操作时,它将根据您提供的集合使用任何#include文件名作为键来查找标头的源.

我怀疑在这种情况下,编译器(驱动程序)没有文件系统访问权限,因此您必须以与在OpenGL中包含的着色器几乎相同的方式为其提供源.

所以:

  • 在调用nvrtcCreateProgram时包括标题的名称.

  • 在内核源代码中,照常使用#include "foo.cuh".

  • 编译器将使用foo.cuh作为其内部映射的索引或键(在您调用nvrtcCreateProgram时创建),并将从该集合中检索标头源

  • 编译正常进行.

nvrtc仅提供功能子集"的原因之一是,编译器在有点沙盒化的环境中运行,而不必拥有脱机编译所具有的所有支持工具和实用程序.因此,您必须手动处理普通nvcc + (gcc | MSVC| clang)组合提供的许多内容.

一个可能但非理想的解决方案是对IDE中所需的文件进行预处理,保存结果,然后#include保存.但是,我敢打赌,有更好的方法可以做到这一点.如果您只想curand,请考虑进入该库并提取所需的部分(出错)或使用另一种GPU友好的rand实现.在较旧的CUDA版本上,我只是在主机上生成了大量随机浮点数,将其上传到GPU,并在内核中进行了采样.

此相关链接可能会有所帮助.

Specifically, my issue is that I have CUDA code that needs <curand_kernel.h> to run. This isn't included by default in NVRTC. Presumably then when creating the program context (i.e. the call to nvrtcCreateProgram), I have to send in the name of the file (curand_kernel.h) and also the source code of curand_kernel.h? I feel like I shouldn't have to do that.

It's hard to tell; I haven't managed to find an example from NVIDIA of someone needing standard CUDA files like this as a source, so I really don't understand what the syntax is. Some issues: curand_kernel.h also has includes... Do I have to do the same for each of these? I am not even sure the NVRTC compiler will even run correctly on curand_kernel.h, because there are some language features it doesn't support, aren't there?

Next: if you've sent in the source code of a header file to nvrtcCreateProgram, do I still have to #include it in the code to be executed / will it cause an error if I do so?

A link to example code that does this or something like it would be appreciated much more than a straightforward answer; I really haven't managed to find any.

解决方案

You have to send the "filename" and the source of each header separately.

When the preprocessor does its thing, it'll use any #include filenames as a key to find the source for the header, based on the collection that you provide.

I suspect that, in this case, the compiler (driver) doesn't have file system access, so you have to give it the source in much the same way that you would for shader includes in OpenGL.

So:

  • Include your header's name when calling nvrtcCreateProgram. The compiler will, internally, generate the equivalent of a std::map<string,string> containing the source of each header indexed by the given name.

  • In your kernel source, use #include "foo.cuh" as usual.

  • The compiler will use foo.cuh as an index or key into its internal map (created when you called nvrtcCreateProgram), and will retrieve the header source from that collection

  • Compilation proceeds as normal.

One of the reasons that nvrtc provides only a "subset" of features is that the compiler plays in a somewhat sandboxed environment, without necessarily having all of the supporting tools and utilities lying around that you have with offline compilation. So, you have to manually handle a lot of the stuff that the normal nvcc + (gcc | MSVC| clang) combination provides.

A possible, but non-ideal, solution would be to preprocess the file that you need in your IDE, save the result and then #include that. However, I bet there is a better way to do that. if you just want curand, consider diving into the library and extracting the part you need (blech) or using another GPU-friendly rand implementation. On older CUDA versions, I just generated a big array of random floats on the host, uploaded it to the GPU, and sampled it in the kernels.

This related link may be helpful.

这篇关于如何包含标准CUDA库以与NVRTC代码链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆