2010-08-01 98 views
4

我如何在OpenCL中定義函數?我試圖爲每個函數構建一個程序。它沒有奏效。如何在OpenCL中定義函數?

float AddVectors(float a, float b) 
{ 
    return a + b; 
} 

kernel void VectorAdd(
    global read_only float* a, 
    global read_only float* b, 
    global write_only float* c) 
{ 
    int index = get_global_id(0); 
    //c[index] = a[index] + b[index]; 
    c[index] = AddVectors(a[index], b[index]); 
} 
+1

可能複製http://stackoverflow.com/questions/2924619/opencl-user-defined-inline-functions的? – 2010-08-01 23:29:06

+0

是的,搜索,但沒有發現之前。 – Kayhano 2010-08-01 23:35:30

回答

5

你不使用需要爲每個函數創建一個程序,而不是你的一組功能,標有__kernel(或kernel)和潛在的輔助功能(如您的AddVectors功能)創建一個程序例如撥打clCreateProgramWithSource

退房來自蘋果,AMD,NVIDIA的基本教程..

+0

我剛測試過這個。沒有收到任何錯誤,所以它可能正在工作。還發現clBuildProgram不是線程安全的。所以我們把所有的程序放在一個單獨的cl文件中是有原因的。 – 2014-01-23 14:17:04