简体   繁体   English

如何以编程方式检查 cubin 中可用的目标?

[英]How do I check, programmatically, which targets are available in a cubin?

Suppose I have a cubin file, or perhaps to make it easier, a cubin file I loaded into memory (so that I have a void* to the data).假设我有一个 cubin 文件,或者为了更容易,我将一个 cubin 文件加载到 memory(这样我就有了一个void*数据)。

Using the CUDA Driver API for modules , I can try loading the data into a module within the current context;使用CUDA Driver API for modules ,我可以尝试将数据加载到当前上下文中的模块中; and this would fail if compiled code is not available for a relevant target (and there's no PTX which could be JITed instead).如果编译后的代码不可用于相关目标(并且没有 PTX 可以改为 JIT),这将失败。 But - what I actually want to do is check which targets have code in the module data (or module file)?但是-我真正想做的是检查哪些目标在模块数据(或模块文件)中有代码?

Non-programmatically, I know I can invoke:非编程方式,我知道我可以调用:

cuobjdump my.fatbin

and get a listing of what's in there.并获取其中内容的列表。 But I want to do it from within my application's code.但我想从我的应用程序代码中进行。

Is this possible?这可能吗?

You could call cuobjdump from within your program and parse its output.您可以从程序中调用 cuobjdump 并解析其 output。

#include <cstdlib>
#include <string>

__global__
void kernel(){

}

int main(int argc, char** argv){
    std::string command{};
    command += "cuobjdump ";
    command += argv[0];
    command += " > out.txt";

    int sysret = system(command.c_str());
    kernel<<<1,1>>>();
    cudaDeviceSynchronize();

    return sysret;
}

You may be able do this using an ELF parser.您可以使用 ELF 解析器来执行此操作。

It seems that cubin files are actually slightly-non-standard ELF files.看起来 cubin 文件实际上是稍微不标准的ELF文件。 Specifically, they have an .nv_fatbin ELF section, containing regions with compiled code for different targets;具体来说,它们有一个.nv_fatbin ELF 部分,包含针对不同目标编译代码的区域; see this analysis .看到这个分析

If you used an ELF library, and made it accept some invalid/different magic numbers / version numbers, it would probably parse the cubin file in a way you could then easily extract your meta-data of interest from, including the target architecture for each region.如果您使用 ELF 库,并让它接受一些无效/不同的幻数/版本号,它可能会以一种您可以轻松提取您感兴趣的元数据的方式解析 cubin 文件,包括每个目标架构地区。

See also how a cubin file is generated , using an ELF library, here (but note that's Python code with a lot of magic numbers and somewhat difficult to follow).另请参阅如何使用 ELF 库在此处生成cubin 文件(但请注意,这是 Python 代码,其中包含很多幻数,有点难以理解)。

Now it's just the "simple matter of coding", adapting an ELF parser to collect and expose what you need.现在只是“简单的编码问题”,调整 ELF 解析器来收集和公开您需要的内容。 The remaining question is whether all of the relevant information is in the ELF meta-data or whether you also need to do further parsing of the contents.剩下的问题是所有相关信息是否都在 ELF 元数据中,或者您是否还需要对内容进行进一步解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM