▶ OpenCL 的环境配置与第一个程序

● CUDA 中自带 OpenCL 需要的头文件和库,直接拉近项目里边去就行;AMD 需要下载 AMD APP SDK(https://community.amd.com/message/2739800 和 http://hc.csdn.net/resources/resource_detail?id=13,感谢大神们提供的方便渠道)

▶ CUDA - OpenCL 在Visual Studio 2015 中的配置。注意 32 位项目和 64 位项目的属性选项不共享,添加的文件也不相同,应该先选定项目的位数,再进行配置。

● VS项目属性右键,属性,C/C++ 目录,包含目录,添加 CUDA 的头文件目录,我的是 "D:\Program\CUDA9.0\include" ,一般代码中使用  #inlcude <CL/cl.h>  来包含子目录中的头文件,也可将其 CL 子目录直接写在这里, "D:\Program\CUDA9.0\include\CL"

● VS项目属性右键,属性,C/C++ 目录,库目录,添加 CUDA 的库目录,注意 32 位和64位是不同的,32位 "D:\Program\CUDA9.0\lib\Win32",64位 "D:\Program\CUDA9.0\lib\x64",使用错误的库文件在编译时会显示类似 “错误 LNK2019 无法解析的外部符号 clGetPlatformInfo,该符号在函数 main 中被引用” 的错误。

  

● VS项目属性右键,属性,链接器,输入,附加依赖项,添加 OpenCL.lib

  

▶ AMD APP SDK - OpenCL 在Visual Studio 2015 中的配置。跟上面 CUDA - OpenCL 的差不多,也需要添加头文件位置,库位置和链接器输入,注意区分 32 位项目和 64 位项目。

● 在办公室的垃圾电脑上完成了配置,相关目录如下:"C:\Program Files %28x86%29\AMD APP SDK\3.0\include","C:\Program Files %28x86%29\AMD APP SDK\3.0\include\CL","C:\Program Files (x86)\AMD APP SDK\3.0\lib\x86_64"

● 我直接按照上面的配置运行时,遇到函数 clGetPlatformIDs() 时报内存错误(0x0FD9F73C (XXX.dll)处(位于 OpenCL.exe 中)引发的异常: 0xC0000005: 读取位置 0x00000001 时发生访问冲突。),与动态库有关。这时需要在 "C:\Windows\SysWOW64" 或 "C:\Windows\System32" 下更新 igdrcl32.dll igdrcl64.dll 才行(随便找了一个较新的版本替换掉它)。

▶ 第一个程序,访问计算平台,输出相关信息

● 代码

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cl.h> // OpenCL 的头文件 int main()
{
cl_platform_id *pPlatform; // 平台数据结构数组,每个结构表示一个在主机上的 OpenCL 执行平台(执行设备)
cl_uint nPlatform;
cl_int i, err;
char* extraInformation; // 额外信息缓冲区
size_t extraSize; // 额外信息缓冲区大小
bool supportICD = false; // 记录主机是否支持 OpenCL Installable Client Driver (ICD) // platform工作的步骤:0、询问主机上有多少计算平台;1、为平台数据结构分配内存空间;2、初始化这些数据结构
if (err = clGetPlatformIDs(, NULL, &nPlatform) < )// 查询平台数,返回值非零说明调用错误,参数:1、需要的平台数上限;2、NULL 表查询平台总数;3、输出平台数的变量指针
{
perror("Couldn't find any pPlatform."); // 输出错误信息用的函数
exit();
}
printf("Platform count: %d\n", nPlatform); pPlatform = (cl_platform_id*)malloc(sizeof(cl_platform_id) * nPlatform);// 创建主机数据结构
clGetPlatformIDs(nPlatform, pPlatform, NULL); // 初始化结构 for (i = ; i < nPlatform; i++) // 循环获取平台信息
{
if (err = clGetPlatformInfo(pPlatform[i], CL_PLATFORM_EXTENSIONS, , NULL, &extraSize) < ) // 获取额外信息,第五个参数为信息长度
{
perror("Couldn't read extension data.");
exit();
}
printf("\nExtension data size: %d\n", extraSize); extraInformation = (char*)malloc(extraSize); // 输出信息缓存
clGetPlatformInfo(pPlatform[i], CL_PLATFORM_EXTENSIONS, extraSize, extraInformation, NULL); // 获取相关信息,第二参数为信息内容,这里是OpenCL支持的扩展功能信息
printf("Platform %d supports extensions: %s\n", i, extraInformation);
if (strstr(extraInformation, "cl_khr_icd") != NULL) // 检查是否支持 ICD,支持则输出平台编号
{
printf("\nPlatform %d supports ICD extension.\n", i);
supportICD = true;
}
clGetPlatformInfo(pPlatform[i], CL_PLATFORM_NAME, extraSize, extraInformation, NULL); // 产商名
printf("Platform %d name: %s\n", i, extraInformation);
clGetPlatformInfo(pPlatform[i], CL_PLATFORM_VENDOR, extraSize, extraInformation, NULL); // 供应商名
printf("Platform %d vendor: %s\n", i, extraInformation);
clGetPlatformInfo(pPlatform[i], CL_PLATFORM_VERSION, extraSize, extraInformation, NULL); // OpenCL版本
printf("Platform %d version: %s\n", i, extraInformation);
clGetPlatformInfo(pPlatform[i], CL_PLATFORM_PROFILE, extraSize, extraInformation, NULL); // 完整模式 / 嵌入式
printf("Profile: %s\n", extraInformation);
free(extraInformation);
}
if (!supportICD)
printf("\nNo platform support ICD extension.\n");
free(pPlatform);
getchar();
return ;
}

● 输出结果,我的电脑。核显被物理屏蔽,仅检测到独立显卡,仅支持到 OpenCL1.2

Platform count: 

Extension data size:
Platform supports extensions: cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_fp64 cl_khr_byte_addressable_store cl_khr_icd cl_khr_gl_sharing cl_nv_compiler_options cl_nv_device_attribute_query cl_nv_pragma_unroll cl_nv_d3d10_sharing cl_khr_d3d10_sharing cl_nv_d3d11_sharing cl_nv_copy_opts cl_nv_create_buffer Platform supports ICD extension.
Platform name: NVIDIA CUDA
Platform vendor: NVIDIA Corporation
Platform version: OpenCL 1.2 CUDA 9.1.
Profile: FULL_PROFILE

● 输出结果,办公室的电脑。检测到独立显卡(支持到 OpenCL2.0)和核显(支持到OpenCL1.2)

Platform count: 

Extension data size:
Platform supports extensions: cl_khr_icd cl_khr_d3d10_sharing cl_khr_d3d11_sharing cl_khr_dx9_media_sharing cl_amd_event_callback cl_amd_offline_devices Platform supports ICD extension.
Platform name: AMD Accelerated Parallel Processing
Platform vendor: Advanced Micro Devices, Inc.
Platform version: OpenCL 2.0 AMD-APP (1800.11)
Profile: FULL_PROFILE Extension data size:
Platform supports extensions: cl_khr_icd cl_khr_global_int32_base_atomics cl_khr_global_int32_extended_atomics cl_khr_local_int32_base_atomics cl_khr_local_int32_extended_atomics cl_khr_byte_addressable_store cl_khr_depth_images cl_khr_3d_image_writes cl_intel_exec_by_local_thread cl_khr_spir cl_khr_dx9_media_sharing cl_intel_dx9_media_sharing cl_khr_d3d11_sharing cl_khr_gl_sharing cl_khr_fp64 Platform supports ICD extension.
Platform name: Intel(R) OpenCL
Platform vendor: Intel(R) Corporation
Platform version: OpenCL 1.2
Profile: FULL_PROFILE

● 用到的函数和定义

 // cl_platform.h
// 标量类型
typedef signed __int8 cl_char;
typedef unsigned __int8 cl_uchar;
typedef signed __int16 cl_short;
typedef unsigned __int16 cl_ushort;
typedef signed __int32 cl_int;
typedef unsigned __int32 cl_uint;
typedef signed __int64 cl_long;
typedef unsigned __int64 cl_ulong;
typedef unsigned __int16 cl_half;
typedef float cl_float;
typedef double cl_double; // 平台数据结构函数
extern CL_API_ENTRY cl_int CL_API_CALL clGetPlatformIDs(// 查询平台数和初始化平台数据结构
cl_uint, // 需要的平台数上限
cl_platform_id *, // 传入指针,NULL 表查询平台总数,非 NULL 表初始化平台数据结构
cl_uint * // 输出指针,指向保存了平台数量的变量
) CL_API_SUFFIX__VERSION_1_0; // 版本号?只是一个宏 extern CL_API_ENTRY cl_int CL_API_CALL clGetPlatformInfo(// 获取平台信息
cl_platform_id, // 平台数据结构
cl_platform_info, // 需要查询的信息名
size_t, // 输出缓冲区大小(Byte),首次调用时采用 0
void *, // 输出信息的缓冲区指针,首次调用时采用 NULL
size_t * // 输出信息需要的缓冲区大小(Byte),首次调用使用该变量来获得所需缓冲区的大小
) CL_API_SUFFIX__VERSION_1_0; // 各种可以查询的信息名,含义见代码中
#define CL_PLATFORM_PROFILE 0x0900
#define CL_PLATFORM_VERSION 0x0901
#define CL_PLATFORM_NAME 0x0902
#define CL_PLATFORM_VENDOR 0x0903
#define CL_PLATFORM_EXTENSIONS 0x0904 // stdio.h
_ACRTIMP void __cdecl perror(_In_opt_z_ char const* _ErrorMessage);// 输出错误信息 // vcruntime.h
typedef unsigned __int64 size_t;// size_t 的定义

OpenCL Hello World的更多相关文章

  1. 基于SoCkit的opencl实验1-基础例程

    基于SoCkit的opencl实验1-基础例程 准备软硬件 Arrow SoCkit Board 4GB or larger microSD Card Quartus II v14.1 SoCEDS ...

  2. OPenCL

    OpenCLhttp://baike.baidu.com/link?url=7uHWCVUYB3Sau_xh3OOKP-A08_IvmT1SJixdAXKezCuCfkzeSQDiSmesGyVGk8 ...

  3. Opencl 并行求和

    上周尝试用opencl求极大值,在网上查到大多是求和,所谓的reduction算法.不过思路是一样的. CPP: ; unsigned ; ; ; int nGroup = nGroupSize / ...

  4. opencl初体验

    总结一下,opencl的步骤差不多是这些 先要获取平台的id clGetPlatformIDs(nPlatforms, platform_id, &num_of_platforms) 然后获取 ...

  5. Altera OpenCL用于计算机领域的13个经典案例(转)

    英文出自:Streamcomputing 转自:http://www.csdn.net/article/2013-10-29/2817319-the-application-areas-opencl- ...

  6. 面向OPENCL的ALTERA SDK

    面向OPENCL的ALTERA SDK 使用面向开放计算语言 (OpenCL™) 的 Altera® SDK,用户可以抽象出传统的硬件 FPGA 开发流程,采用更快.更高层面的软件开发流程.在基于 x ...

  7. OpenCV GPU CUDA OpenCL 配置

    首先,正确安装OpenCV,并且通过测试. 我理解GPU的环境配置由3个主要步骤构成. 1. 生成关联文件,即makefile或工程文件 2. 编译生成与使用硬件相关的库文件,包括动态.静态库文件. ...

  8. CUDA/OpenCL 学习资料

    VS2010 NVIDIA OpenCL 开发环境配置 CUDA 在线课程 [经典培训] 全球首套中文CUDA 教程-胡文美教授主讲

  9. opencl 学习资源

    1.AMD  opencl-optimization-guide http://developer.amd.com/tools-and-sdks/opencl-zone/amd-accelerated ...

  10. opencl gauss filter优化(三)

    1.根据前两次的最终结果: 使用普通buffer,Horizontal 5ms, Vertical 17 ms 使用image buffer:Horizontal 9.4ms, Vertical 6. ...

随机推荐

  1. Java Spring-事务管理

    2017-11-12 16:31:59 Spring的事务管理分为两种: 编程式的事务管理:手动编写代码 声明式的事务管理:只需要配置就可以 一.最初的环境搭建 public interface Ac ...

  2. SQLPLUS的乱码问题

    我的中文系统,把对应非unicode字符时的设置,改成了 日文, 结果控制台使用sqlplus时候,总是出现乱码. 解决方法是,把NLS_LANG环境变量变成跟系统一样,就可以了. american_ ...

  3. linux 环境下 eas console的运行

    1)访问 http://<HOST>:19000/easconsole/ 2)然后下载 jnlp 文件. 3)找个jre, 用javaws 运行 jnlp文件

  4. android------引导页两种实现方式(原生和WebView网页实现)

    有的App当你第一次打开的是和常常会有引导页来描述一些App信息(功能,特点),当然也要做验证,验证第二次进入不进入引导页,直接进入App,此博客借助ViewPager来实现引导页, ViewPage ...

  5. ablout unbuntu default mysql

    http://www.ghostchina.com/how-to-reset-mysqls-root-password/ http://blog.csdn.net/u010603691/article ...

  6. 『转』Bitdefender Internet Security 2013 – 免费1年

    活动中可以选择获取Bitdefender Internet Security 2013+Bitdefender Mobile Security (手机版)各一年激活码申请地址:https://part ...

  7. 关于oceanbase中存储过程的设计与实现

    转自http://www.zhujuncoding.com/index.php/Index/blogview?id=82 这篇文章是关于在淘宝的数据库oceanbase中添加存储过程支持的文章,oce ...

  8. Python3.5 源码安装 Ubuntu16.04环境

    安装源码编译所需的各种依赖库:(Ubuntu16.04环境下) sudo apt-get install zlib1g-dev libbz2-dev libssl-dev libncurses5-de ...

  9. JQ深度手记、源码分析

    1.$.extend() 对象继承操作.浅拷贝操作.深拷贝操作(第一个参数:true) var a = { name:'lisan' }; var b = {}; $.extend(b, a); // ...

  10. 好的框架需要好的 API 设计 —— API 设计的六个原则

    说到框架设计,打心底都会觉得很大很宽泛,而 API 设计是框架设计中的重要组成部分.相比于有很多大佬都认可的面向对象的六大原则.23 种常见的设计模式来说,API 设计确实缺少行业公认的原则或者说设计 ...