? ? 當然,首先要安裝AMD-APP-SDK和AMD的顯卡驅動。我的是linux系統加AMD顯卡。我下APP的地址:http://developer.amd.com/Downloads/AMD-APP-SDK-v2.7-lnx64.tgz。
? ? 搞定以后就可以開始寫程序了。找到apple的一個例程:http://developer.apple.com/library/mac/#samplecode/OpenCL_Hello_World_Example/Listings/hello_c.html。
? ? 當然,我這非蘋果的系統直接用這個是不行的。需要把#include <OpenCL/opencl.h>改為#include <CL/opencl.h>。apple的頭文件目錄和其他的平臺有區別的。
? ? 好了,開始編譯:
? ? ? ? ? g++ -L/opt/AMDAPP/lib/x86_64 -lOpenCL -I/opt/AMDAPP/include /home/delat2/mac_square.cpp -o /home/delat2/mac_square.cpp.o
? ? 編譯成功,運行看看。居然提示:Error: Failed to create a device group!看看代碼,可以發現是clGetDeviceIDs這個函數返回了錯誤。google一下,得知
clGetDeviceIDs returns CL_SUCCESS if the function is executed successfully. Otherwise it returns the following:
CL_INVALID_PLATFORM if platform is not a valid platform.
CL_INVALID_DEVICE_TYPE if device_type is not a valid value.
CL_INVALID_VALUE if num_entries is equal to zero and device_type is not NULL or if both num_devices and device_type are NULL.
CL_DEVICE_NOT_FOUND if no OpenCL devices that matched device_type were found.
?
? ? 用switch case判斷一下錯誤,說是CL_INVALID_PLATFORM。莫非我的APP-SDK還沒裝好?用/opt/AMDAPP/samples/opencl/bin/x86_64下的例程測試一下是可以的。google一下CL_INVALID_PLATFORM,找到了AMD的說明(http://developer.amd.com/support/KnowledgeBase/Lists/KnowledgeBase/DispForm.aspx?ID=71)。原來是新版本的SDK有幾處修改。按照說明,clGetDeviceIDs的cl_platform_id參數不能為NULL。而獲取platform_id的方法
? ? cl_platform_id platform_id=NULL;
? ? err=clGetPlatformIDs(1,&platform_id,NULL);
相應的clGetDeviceIDs改為
? ? ?err = clGetDeviceIDs(platform_id, gpu ? CL_DEVICE_TYPE_GPU : CL_DEVICE_TYPE_CPU, 1, &device_id, NULL);
重新編譯,運行就沒出錯了。
?