linux opencl(AMD) Example

来源:互联网 发布:linux引导windows 编辑:程序博客网 时间:2024/05/21 06:42
最近对并行计算很感兴趣。不过搞MPI对我来说暂时没什么用,基于GPU的并行计算倒是挺实用。网上的资料都是CUDA的。实质上我对CUDA一点兴趣都没有。无论别人的架构多么先进,我这个只有AMD显卡的小孩都是旁观者而已。在这里记录一下一个opencl程序的编译过程。
    当然,首先要安装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);

重新编译,运行就没出错了。

 

0 0
原创粉丝点击