OpenCL编程入门(一)
OpenCL简介
OpenCL标准
<span style="font-size:10px;">_kernel void vecadd(_global int *C, _global int *A, _global int *C)
{int tid = get_global_id(0); //OpenCL intrisic functionC[tid] = A[tid] + B[tid];
}</span>
OpenCL规范
- 平台模型:定义了一个抽象的硬件模型,描述了宿主机和设备。供编程人员在上面编写在这些设备上执行的Kernel。
- 执行模型:定义了如何在宿主机上配置OpenCL环境以及如何在设备上执行Kernel。
- 内存模型:定义了Kernel中所使用的内存层次,无需考虑实际的底层架构。
- 编程模型:定义了如何将并发模型映射到物理硬件上。
平台模型
执行模型
上下文与命令队列
- 设备:平台上可用的设备。
- Kernels:运行在设备上的OpenCL C程序。
- 程序对象:负责执行和实现Kernels的程序资源。
- 内存对象:Kernel和宿主机可见的内存对象的集合,内存对象中包含kernel实例中用于计算的数据。
cl_context
clCreateContext(const cl_context_properties *properties,cl_unit num_devices,void (CL_CALLBACK *pfn_notify)(const char *errinfo,const void *private_info,size_t cb,void *user_data),void *user_data,cl_int *errcode_ret)
API函数clCreateCommandQueue()用来建立命令队列并将其关联到某个设备。
cl_command_queue
clCreateCommandQueue(cl_context contextcl_device_id device,cl_command_queue_properties properties,cl_int *errcode_ret)
clCreateCommandQueue()中的属性参数properties是一个位域,用于开启程序剖析(profiling)命令(CL_QUEUE_PROFILING_ENABLE)、乱序执行命令(CL_QUEUE_OUT_OF_ORDER_EXEC_ENABLE)。
内存对象
cl_mem clCreateBuffer(cl_context context,cl_mem_flags flags,size_t size,void *host_ptr,cl_int *errcode_ret)
将宿主机内存传入或传出设备的API分别为clEnqueueWriteBuffer和clEnqueueReadBuffer。可通过设置参数选项blocking_write为CL_TRUE,使写函数工作在阻塞模式——数据完成到OpenCL buffer的传输后才返回。
程序对象
- OpenCL源代码以字符串形式存储。如果代码存放在文件中,则必须读取到内存中,在内存中以字符串数组的形式存放。
- 源代码通过调用clCreateProgramWithSource()转化为cl_program对象。
- 调用clBuildProgram()在支持OpenCL的设备上编译程序对象。
向量加代码演示
#include<stdio.h>
#include<stdlib.h>
#include<CL/cl.h>const char *programSource =
"_kernel void vecadd(_global int *A, _global int *B, _global int *C) \n"
"{ \n"
" int idx = get_global_id(0); \n"
" C[idx] = A[idx] + B[idx]; \n"
"} \n" int main()
{int *A = NULL;int *B = NULL;int *C = NULL;const int elements = 2048;size_t datasize = sizeof(int) * elements;A = (int*)malloc(datasize);B = (int*)malloc(datasize);C = (int*)malloc(datasize);int i;for(i=0; i<elements; i++){A[i] = i;B[i] = i;}//Check return valuecl_int status;//Retrieve the number of platforms cl_uint numPlatforms = 0;status = clGetPlatformIDs(0, NULL, &numPlatforms);//Allocate space for each platformcl_plarform_id *platforms = NULL;platforms = (cl_platform_id*)malloc(numPlatforms * sizeof(cl_platform_id));//Fill in the platformsstatus = clGetPlatformIDs(numPlatforms, platforms, NULL);//Retrieve the number of devicescl_unit numDevicdes = 0;status = clGetDeviceIDs(platform[0], CL_DEVICE_TYPE_ALL, 0, NULL, &numDevices);//Allocate space for devicescl_device_id *devices;devices = (cl_device_id*)malloc(numDevices * sizeof(cl_device_id));//Fill in the devicesstatus = clGetDeviceIDs(platform[0], CL_DEVICE_TYPE_ALL, numDevices, deivces, NULL);//Create a context and associate it with devicescl_context context;context = clCreateContext(NULL, numDevices, devices, NULL, NULL, &status);//Create a command queue and associate it with the devicecl_command_queue cmdQueue;cmdQueue = clCreateCommandQueue(context, devices[0], 0, &status);//Create buffer objectcl_mem bufA = clCreateBuffer(context, CL_MEM_READ_ONLY, datasize, NULL, &status);cl_mem bufB = clCreateBuffer(context, CL_MEM_READ_ONLY, datasize, NULL, &status);cl_mem bufC = clCreateBuffer(context, CL_MEM_READ_ONLY, datasize, NULL, &status);//Write to the device bufferstatus = clEnqueueWriteBuffer(cmdQueue, bufA, CL_FALSE, 0, datasize, A, 0, NULL, NULL);status = clEnqueueWriteBuffer(cmdQueue, bufB, CL_FALSE, 0, datasize, B, 0, NULL, NULL);status = clEnqueueWriteBuffer(cmdQueue, bufC, CL_FALSE, 0, datasize, C, 0, NULL, NULL);//Create program from source codecl_program program = clCreateProgramWithSource(context, 1, (const char**)&programSource, NULL, &status);//Compile the programstatus = clBuildProgram(program, "vacadd", &status);//Associate buffers with the kernelstatus = clSetKernelArg(kernel, 0, sizeof(cl_mem), &bufA);status = clSetKernelArg(kernel, 1, sizeof(cl_mem), &bufB);status = clSetKernelArg(kernel, 2, sizeof(cl_mem), &bufC);//Define index spacesize_t globalWorkSize[1];globalWorkSize[0] = elements;//execute the kernelstatus = clEnqueueNDRangeKernel(cmdQueue, kernle, 1, NULL, globalWorkSize, NULL, 0, NULL, NULL);//Read the device output buffer to the host output array clEnqueueReadBuffer(cmdQueue, bufC, CL_TRUE, 0, datasize, C, 0, NULL, NULL);//Verify the outputint result = 1;for(i=0;i<elements;i++){if(C[i] != i+i){result = 0;break;}}if(result){printf("correct!\n");}else{printf("incorrect!\n");}//Release resourcesclReleaseKernel(kernel);clReleaseProgram(program);clReleaseCommandQueue(cmdQueue);clReleaseMemObject(bufA);clReleaseMemObject(bufB);clReleaseMemObject(bufC);clReleaseContext(context);//Free host resourcesfree(A);free(B);free(C);free(platform);free(devices);
}
OpenCL编程入门(一)相关推荐
- 【浅墨著作】 OpenCV3编程入门 内容简介 勘误 配套源代码下载
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 经过近一 ...
- 【OpenCV学习】 《OpenCV3编程入门》--毛星云 01 邂逅OpenCV(OpenCV基本概念与基本架构) ROS系统上的运用(python实现)
对 <OpenCV3编程入门>第一章的学习笔记:理解什么是计算机视觉,什么是OpenCV,以及其中的联系等等. PS:此书为2014年出版,opencv的版本和接口也与现在有些不一致了,作 ...
- 【《OpenCV3编程入门》内容简介勘误配套源代码下载
录 (?) [+] 一前言 二内容安排 三适合阅读本书的读者 四书本目录 第一部分 快速上手OpenCV 1 第1章 邂逅OpenCV 3 1 OpenCV周边概念认知 4 2 OpenCV基本 ...
- OpenCL编程详细解析与实例
OpenCL编程详细解析与实例 C语言与OpenCL的编程示例比较 参考链接: https://www.zhihu.com/people/wujianming_110117/posts 先以图像旋转的 ...
- python编程求圆的面积案例_Python实用案例编程入门:第七章 调式手段
本章的主题为调试手段,这是程序开发必不可少的步骤,也是占用时间最多的环节.在程序员的正常开发工作中,调试工作至少占据1/3的时间,而实际编码工作相对占用实际比较少.因此,无论您是初学者,还是编程兴趣爱 ...
- 《C++游戏编程入门(第4版)》——1.12 习题
本节书摘来自异步社区出版社<C++游戏编程入门(第4版)>一书中的第1章,第1.1节,作者:[美]Michael Dawson(道森),更多章节内容可以访问云栖社区"异步社区&q ...
- 编程入门到进大厂,你需要这套学习架构
我相信大多数学习编程的同学都有着对大公司的憧憬.技术.声望.薪资.福利,这些都足以成为吸引你进入大厂的理由. 但是,如何进入大厂呢? 对于很多同学来说,通往大厂的道路并不明朗,不知道是否有希望,也不知 ...
- 《C++游戏编程入门(第4版)》——1.8 Lost Fortune简介
本节书摘来自异步社区出版社<C++游戏编程入门(第4版)>一书中的第1章,第1.8节,作者:[美]Michael Dawson(道森),更多章节内容可以访问云栖社区"异步社区&q ...
- [译]函数式响应编程入门指南
原文地址:An Introduction to Functional Reactive Programming 原文作者:Daniel Lew 译文出自:掘金翻译计划 本文永久链接:github.co ...
- 《C++游戏编程入门(第4版)》——2.4 使用带else子句的if语句序列
本节书摘来自异步社区出版社<C++游戏编程入门(第4版)>一书中的第2章,第2.4节,作者:[美]Michael Dawson(道森),更多章节内容可以访问云栖社区"异步社区&q ...
最新文章
- 2018 年将打响 AI 战争,7 条实战经验帮你战胜恐惧
- Java入门系列-16-继承
- MySQL------MySQL与SQLServer数据类型的转换
- python web flask开发框架_Python Web 开发框架,Flask 与 Django那个更好
- 转: 通过Servlet生成验证码图片
- 深度学习(二十六)——VAE
- 产品原型设计的参考步骤
- 作者:廖小飞,博士,华中科技大学计算机科学与技术学院教授、博士生导师。...
- PHP内核通用网站后台权限管理系统源码
- OutOfMemoryError(内存溢出)解决办法
- 内部存储_Mongodb存储特性与内部原理
- 深入探讨apply()方法的作用
- 为什么图灵奖获得者戴克斯特拉痛恨 BASIC 语言
- 如何免费搭建个人网站
- 最新图片交替闪现效果代码
- 如何拉取钉钉的外出、出差审批单
- Excel笔记(持续更新)
- 数据结构(C语言) 实验---图及其应用
- 海康IPC摄像头通过ONVIF协议接入VCN离线
- linux下socket编程带来的尴尬