5

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?

4

1 回答 1

9

您将类方法传递给cudaStreamAddCallback,但它应该是非成员函数(全局或静态)。如果你想使用类方法,你应该实现将调用该方法的包装函数:

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);
于 2013-07-30T10:43:08.583 回答