2012-04-13 68 views
1

在OpenCL中有使用clEnqueueNativeKernel的例子嗎?通過這種方式,可以用c或C++語言編寫內核。其他命令是否保持不變?在OpenCL中使用clEnqueueNativeKernel

+0

你能指定你想達到的目標。現在的問題太籠統了。 – Kentzo 2012-04-20 12:37:49

+0

我想寫一個本地C++內核。因此我應該使用clEnqueueNativeKernel命令。我正在尋找源代碼來了解Native Kernels的優點和缺點,以及人們可以使用它們的方式。看起來它們在所有平臺上都不被支持。 – Damoon 2012-04-23 09:12:16

回答

4

本機C++「內核」實質上就是您想要在命令隊列中執行以保持命令順序的函數。

AFAIK它們在GPU上不受支持。如果你想在所有設備上執行C++函數,你應該考慮使用cl_event回調函數(當status == CL_COMPLETE時)。

假設你有一個緩衝區對象,你想從設備讀取並傳遞給你的C++函數。你也想通過一些整數值 (我使用C++ OpenCL包裝):

// First of all, we should define a struct which describes our arguments list. 
struct Arguments 
{ 
    int integer; 
    void* buffer_host; 
}; 

// Define C/C++ function you want to call. 
void CFunction(void *args) 
{ 
    Arguments args = reinterpret_cast<Arguments*>(args); 
    // Do something with args->integer and args->buffer_host. 
} 

// ... 
Arguments args = {.integer = 0, .buffer_host = NULL}; 

// First, we should define Buffer objects in arguments. 
std::vector<cl::Memory> buffers_dev; 
buffers_dev.push_back(a_buffer); 

// Then we should define pointers to *pointer in args* which will be set 
// when OpenCL read data from buffers_dev to the host memory. 
std::vector<const void*> buffers_host; 
buffers_host.push_back(&args.buffer_host); 

// Finally, set integer 
args.integer = 10; 

queue.enqueueNativeKernel(CFunction, 
          std::make_pair(&args, siezof(Arguments)), 
          &buffers_dev, 
          &buffers_host); 

// At this point args were copied by OpenCL and you may reuse or delete it.