user2390724
user2390724

Reputation: 15

Compile kernel to DLL and use it

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.

Upvotes: 0

Views: 541

Answers (2)

Ivan Solntsev
Ivan Solntsev

Reputation: 2120

You could compile your kernel code to PTX and run it using CUDA driver API, see CUDA C Programming Guide / Driver Api / Module.

If you invoke nvcc with -ptx option instead of --compile, it will generate ptx files. It is not linked with your exe program, and you can change ptx files at any time.

Upvotes: 1

Adam
Adam

Reputation: 4174

The whole code doesn't make sense.

First, you are not checking the cudaStatus.

Second you are copying from constant memory, but why? surely you didn't update the constant memory in your kernel. You are probably looking for cudaMemcpy not cudaMemcpyFromSymbol

Have a Google on "Pinned Memory", it might be useful in your case.

Upvotes: 0

Related Questions