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

273 views Asked by At

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).

Using the CUDA Driver API for modules, I can try loading the data into a module within the current context; and this would fail if compiled code is not available for a relevant target (and there's no PTX which could be JITed instead). 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?

2

There are 2 answers

1
Abator Abetor On

You could call cuobjdump from within your program and parse its 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;
}
3
einpoklum On

You may be able do this using an ELF parser.

It seems that cubin files are actually slightly-non-standard ELF files. Specifically, they have an .nv_fatbin ELF section, containing regions with compiled code for different targets; 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.

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).

Now it's just the "simple matter of coding", adapting an ELF parser to collect and expose what you need. 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.