CUDA驱动程序API和函数名称修饰

9

I have a project that requires C++11, so I separate the files into two categories: those that use C++11, and those that use C++03 and hence are compatible with the nvcc compiler. When I have a kernel that is not a template function, it is easy to load the module and find the function name using cuModuleGetDataEx. However, when the kernel is a template, the function name is mangled after explicit specialization. This makes it difficult to obtain a handle to the function after loading the module using the CUDA Driver API. For example, consider this function.

template <class T, class SizeType>
<strong>global</strong> void
vector_add(const T* a, const T* b, T* c, const SizeType dim)
{
    const SizeType i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < dim) { c[i] = a[i] + b[i]; }
}

在将它编译成PTX代码后,变形的名称是_Z10vector_addIfjEvPKT_S2_PS0_T0_。 我如何轻松地从我的主机代码中查找和加载模板内核函数,而无需手动在文件中查找并复制它们的名称?


3
我想你可以创建包装函数,显式实例化你需要的每个模板版本,并将类型放入包装函数的函数名中。 - Roger Dahl
没错,但这样我不就失去了使用 PTX 代码进行 JIT 编译的能力吗?在使用 cuModuleGetDataEx 后,我仍然需要知道函数的名称才能检索到它的句柄。 - void-pointer
回复自己:不,你不需要。你可以使用--ptxas-options标志在构建过程中指定相同的JIT选项。尽管如此,我仍然想知道是否有更优雅的解决方案。 - void-pointer
@RogerDahl 是的,我知道,但我使用模板的最重要原因是因为我不知道函数将被参数化的类型(它们不会是简单的基元类型)。 - void-pointer
1个回答

1
我有一个需要使用C++11的项目。
这一定是个玩笑吧,你的程序需要原型编译器... 你没有提到你正在使用哪个编译器,但看起来像是gcc。 了解你的编译器 我很确定你的CUDA部分不需要C++11,在C++03文件旁边放置所有内容并按照通常方式进行操作,如果需要使用库与C++11原型编译器生成的可执行文件链接,那就是最先进的技术。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接