Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I use cudaStreamAddCallback() with a class member method?

I'm trying to synchronise my cuda routine by using cudaStreamAddCallback(), but I can't implement it, also because the documentation is not unambiguous. The cuda-C-programming-guide says that the callback has to be defined as:

void CUDART_CB MyCallback(void *data){}

and is talking about flags like the cudaStreamCallbackBlocking that needs to be set; while the Cuda_Toolhit_Reference_Manual and the cuda_runtime_api.h requiring an other implementation of the callback:

void CUDART_CB MyCallback (cudaStream_t stream, cudaError_t status, void *userData){}

and mentioning that the flag is for future use and require a 0 as argument. Furthermore, calling the function as follow:

cudaStreamAddCallback(GpuStream, MyCallback, &BufSwitchParams, 0);

and working using VS 2010 trying to compile for 64bit I'm getting the message: argument of type “ void(__stdcall CMyClass::*)(cudaStream_t stream, cudaError_t status, void *userData)” is incompatible with parameter of type "cudaStreamCallback_t".

Does someone has already implemented this function and would be able to help me out of my dilemma, while posting a snippet here?

like image 802
GregPhil Avatar asked Jul 30 '13 09:07

GregPhil


1 Answers

You pass a class method to cudaStreamAddCallback, but it should be a non-member function (global or static). If you want to use class method you should implement wrapper function that will call the method:

class MyClass
{
public:
    static void CUDART_CB Callback(cudaStream_t stream, cudaError_t status, void *userData);

private:
    void callbackFunc();
};

void CUDART_CB MyClass::Callback(cudaStream_t stream, cudaError_t status, void *userData)
{
    MyClass* thiz = (MyClass*) userData;
    thiz->callbackFunc();
}

void MyClass::callbackFunc()
{
    // implementation here
}

MyClass* obj = new MyClass;
cudaStreamAddCallback(GpuStream, MyClass::Callback, obj, 0);
like image 101
jet47 Avatar answered Oct 04 '22 17:10

jet47