cuModuleGetFunction返回未找到

cuModuleGetFunction returns not found

本文关键字:返回 cuModuleGetFunction      更新时间:2023-10-16

我想用nvrtc JIT编译器编译CUDA内核,以提高我的应用程序的性能(所以我有增加的指令提取量,但我节省了多个数组访问)。

函数看起来像这样,是由我的函数生成器生成的(不那么重要):

extern "C" __device__ void GetSumOfBranches(double* branches, double* outSum)
{
    double sum = (branches[38])+(-branches[334])+(-branches[398])+(-branches[411]);
    *outSum = sum;
}

我正在用以下函数编译上面的代码:

CUfunction* FunctionGenerator::CreateFunction(const char* programText)
{
        // When I comment this statement out the output of the PTX file is changing
        // what is the reson?!
        // Bug?
        std::string savedString = std::string(programText);

        nvrtcProgram prog;
        nvrtcCreateProgram(&prog, programText, "GetSumOfBranches.cu", 0, NULL, NULL);
        const char *opts[] = {"--gpu-architecture=compute_52", "--fmad=false"};
        nvrtcCompileProgram(prog, 2, opts);
        // Obtain compilation log from the program.
        size_t logSize;
        nvrtcGetProgramLogSize(prog, &logSize);
        char *log = new char[logSize];
        nvrtcGetProgramLog(prog, log);
        // Obtain PTX from the program.
        size_t ptxSize;
        nvrtcGetPTXSize(prog, &ptxSize);
        char *ptx = new char[ptxSize];
        nvrtcGetPTX(prog, ptx);
        printf("%s", ptx);
        CUdevice cuDevice;
        CUcontext context;
        CUmodule module;
        CUfunction* kernel;
        kernel = (CUfunction*)malloc(sizeof(CUfunction));
        cuInit(0);
        cuDeviceGet(&cuDevice, 0);
        cuCtxCreate(&context, 0, cuDevice);
        auto resultLoad = cuModuleLoadDataEx(&module, ptx, 0, 0, 0);
        auto resultGetF = cuModuleGetFunction(kernel, module, "GetSumOfBranches");
        return kernel;
}

除了cuModuleGetFunction返回CUDA_ERROR_NOT_FOUND之外,一切正常。出现这个错误是因为在PTX文件中找不到GetSumOfBranches

然而printf("%s", ptx);的输出是:

// Generated by NVIDIA NVVM Compiler
//
// Compiler Build ID: CL-19856038
// Cuda compilation tools, release 7.5, V7.5.17
// Based on LLVM 3.4svn
//
.version 4.3
.target sm_52
.address_size 64
    // .globl   GetSumOfBranches
.visible .func GetSumOfBranches(
    .param .b64 GetSumOfBranches_param_0,
    .param .b64 GetSumOfBranches_param_1
)
{
    .reg .f64   %fd<8>;
    .reg .b64   %rd<3>;

    ld.param.u64    %rd1, [GetSumOfBranches_param_0];
    ld.param.u64    %rd2, [GetSumOfBranches_param_1];
    ld.f64  %fd1, [%rd1+304];
    ld.f64  %fd2, [%rd1+2672];
    sub.rn.f64  %fd3, %fd1, %fd2;
    ld.f64  %fd4, [%rd1+3184];
    sub.rn.f64  %fd5, %fd3, %fd4;
    ld.f64  %fd6, [%rd1+3288];
    sub.rn.f64  %fd7, %fd5, %fd6;
    st.f64  [%rd2], %fd7;
    ret;
}

在我的选择一切都很好,GetSumOfBranches应该被cuModuleGetFunction找到。你能给我解释一下吗?

第二个问题

当我输出std::string savedString = std::string(programText);时,PTX的输出只是:

// Generated by NVIDIA NVVM Compiler
//
// Compiler Build ID: CL-19856038
// Cuda compilation tools, release 7.5, V7.5.17
// Based on LLVM 3.4svn
//
.version 4.3
.target sm_52
.address_size 64

,这很奇怪,因为savedString根本没用…

不支持您要做的操作。主机端模块管理api和设备ELF格式不公开__device__函数,只有__global__函数可以通过内核启动api调用。

你可以预先编译设备函数或在运行时编译,并以JIT方式将它们与内核链接,你可以检索这些内核并调用它们。但这是你所能做的。