【问题标题】:Compile kernel to DLL and use it将内核编译为 DLL 并使用它
【发布时间】:2013-06-10 22:48:31
【问题描述】:

我设法用函数指针使整个事情工作,现在我想动态加载这样的内核。 我的代码:

cuh:

ifndef customkernel_cuh
define customkernel_cuh

extern "C" pfunctionWhere __declspec(dllexport) getHostPointer();

endif

cu:

__device__
    bool myWhere2(PapayaColumnValue *values)
{
    return ((int)values[1]) == 1 || ((int)values[1]) == 3;
}
__device__ pfunctionWhere pMyWhere2 = myWhere2;

pfunctionWhere __declspec(dllexport) getHostPointer()
{
    cudaError_t cudaStatus;
    pfunctionWhere h_pMyWhere2;
    cudaStatus = cudaMemcpyFromSymbol(&h_pMyWhere2, pMyWhere2, sizeof(pfunctionWhere));
    cudaDeviceSynchronize();
    return h_pMyWhere2;
}

main.cpp:

HINSTANCE hGetProcIDDLL = LoadLibrary("xxx.dll");
    if (hGetProcIDDLL == NULL) {
        std::cout << "could not load the dynamic library" << std::endl;
    }
    dll_func dll_getHostPointer = (dll_func)GetProcAddress(hGetProcIDDLL, "getHostPointer");
    DWORD dw = GetLastError(); 
    if (!dll_getHostPointer) {
        std::cout << "could not locate the function" << std::endl;
    }
    pfunctionWhere h_pMyWhere2 = (*dll_getHostPointer)();

如果我调试到 dll cudaStatus = cudaSuccess,但指向函数的指针为空,并且它是从 dll 调用返回的。我的问题是:是否可以在 DLL 中编写内核函数,然后获取指向此类内核的指针并将其传递给主程序?我需要它能够在主程序运行时更改内核。

【问题讨论】:

    标签: pointers dll cuda


    【解决方案1】:

    您可以将内核代码编译为 PTX 并使用 CUDA 驱动程序 API 运行它,请参阅 CUDA C Programming Guide / Driver Api / Module

    如果您使用-ptx 选项而不是--compile 调用nvcc,它将生成ptx 文件。它不与你的exe程序链接,你可以随时更改ptx文件。

    【讨论】:

    • 我正在尝试加载一些可以在编译主程序后编译的代码,然后可以更改并重新加载。 PTX 可以吗?
    • 是的,你将ptx文件与cpu程序分开编译。
    【解决方案2】:

    整个代码没有意义。

    首先,您没有检查cudaStatus

    第二次你从常量内存中复制,但为什么呢?您肯定没有更新内核中的常量内存。您可能正在寻找 cudaMemcpy 而不是 cudaMemcpyFromSymbol

    在“Pinned Memory”上有一个谷歌,它可能对你有用。

    【讨论】:

    猜你喜欢
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-04
    • 2014-09-30
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    相关资源
    最近更新 更多