0

I managed to make whole thing with function pointers work and now I want to dynamically load such a kernel. My code:

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)();

And if I debug into dll cudaStatus = cudaSuccess, but pointer to function is null and it is returned from dll invocation. My question is: is it possible to write kernel functions in DLL and then get pointer to such kernels and pass it to main program? I need it to be able to change the kernel while main program is working.

4

2 回答 2

1

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

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

于 2013-06-11T15:07:02.053 回答
0

整个代码没有意义。

首先,您没有检查cudaStatus.

其次,您正在从常量内存中复制,但为什么呢?您肯定没有更新内核中的常量内存。您可能正在寻找cudaMemcpycudaMemcpyFromSymbol

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

于 2013-06-11T11:17:47.120 回答