linux opencl(AMD) Example
2014-04-05 18:07
295 查看
最近对并行计算很感兴趣。不过搞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);
重新编译,运行就没出错了。
当然,首先要安装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);
重新编译,运行就没出错了。
相关文章推荐
- Linux上使用AMD的OpenCL开发运行环境
- Linux上使用AMD的OpenCL开发运行环境 (经验分享)
- VPU?APU?GPU?折腾从AMD-GPU回到Intel核显+OpenCL+Linux环境搭建
- Linux kernel crash analysis example
- example:linux ftp anon_user_upload
- C Socket Programming for Linux with a Server and Client Example Code
- AMD 催化剂的 Linux 驱动打败 Windows 驱动
- Example : Linux kernal file read and write
- 开源的AMDGPU和ATI Linux图形驱动程序更新了新的功能
- 安装OpenCL和AMD驱动程序
- Linux Ubuntu server 15.04 amd-64编译Apache Hadoop 2.7.2源码
- 新电脑Linux-64bit安装OpenCL环境Nvidia
- AMD OpenCL环境配置
- 基于OpenCL的深度学习工具:AMD MLP及其使用详解
- x264:common/opencl.c:154:19: 错误:‘x264_opencl_source_hash’未声明 分类: Linux 2014-11-03 11:10 389人阅读 评论(0) 收藏
- 【原创】Linux环境下的图形系统和AMD R600显卡编程(1)——Linux环境下的图形系统简介
- AMD OpenCL例子阅读笔记系列之BinarySearch
- centos7安装AMD显卡驱动和AMD opencl SDK的过程
- linux下apache启动问题(dummy-host.example.com does not exist, Could not..127.0.0.1 for ServerName)
- Linux compile opencv cuda program ----simple example.