<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>GPGPU.ORG.CN</title>
	<atom:link href="http://gpgpu.org.cn/feed" rel="self" type="application/rss+xml" />
	<link>http://gpgpu.org.cn</link>
	<description>Another GPGPU Blog...</description>
	<lastBuildDate>Wed, 10 Mar 2010 13:45:55 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>zh</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Matlab下的CUDA编程(二)</title>
		<link>http://gpgpu.org.cn/cuda/matlab_with_cuda_2</link>
		<comments>http://gpgpu.org.cn/cuda/matlab_with_cuda_2#comments</comments>
		<pubDate>Mon, 22 Feb 2010 04:06:56 +0000</pubDate>
		<dc:creator>wy</dc:creator>
				<category><![CDATA[Application]]></category>
		<category><![CDATA[CUDA]]></category>
		<category><![CDATA[matlab]]></category>

		<guid isPermaLink="false">http://gpgpu.org.cn/uncat/matlab%e4%b8%8b%e7%9a%84cuda%e7%bc%96%e7%a8%8b%e4%ba%8c</guid>
		<description><![CDATA[Matlab下通过Mex文件编写C程序
本节参考NVIDIA网站相关资源，点击此处链接：
1、MEX规则
Matlab提供了MEX文件的方式来支持C/C++代码编写的算法。mex文件需要满足如下要求：
(1) 包含mex.h头文件
(2) 函数名称、参数返回值必须为如下形式:

void mexFunction&#40;int nlhs, mxArray *plhs&#91;&#93;,int nrhs, const mxArray *prhs&#91;&#93;&#41;;

其中：
nlhs 为输出数组个数(Left Hand Side)
plhs 为指向输出数组的指针
nrhs 为输入数组个数(Right Hand Side)
prhs 为指向输入数组的指针，且输入数组只读。
以上四个变量，是在C/C++代码中唯一可用到的变量。实际上，由于matlab中所有的变量都是mxArray结构(向量，数组，字符串。。。)，因此常见的数据类型均可放入mxArray传递给程序进行处理。
(3)常用mex函数:
a. mex函数定义在mex.h(./extern/include)中，并以mex为前缀，例如打印输出的mexPrintf()函数、在mex文件中调用matlab函数的mexCallMATLAB()函数等。
b. 在matrix.h(./extern/include)中定义了mxArray结构以及对矩阵操作的函数，如创建双精度矩阵的mxCreateDoubleMatrix()、从mxArray中获取数据指针mxGetPr()等。
(4)编译
首先可以通过mex -setup来选择编译器，此时matlab会提示：
Would you like mex to locate installed compilers [y]/n?
此处如果选y，则会列出系统中安装的编译器，但不一定完整（作者就遇到这样的情况）。如果选n，则会列出matlab支持的所有编译器，
我们按实际情况选取即可（按照CUDA的要求，vs2005以上）。
另外常用的编译选项有：
-I 增加头文件(.h)包含目录
-L 增加库文件(.lib)包含目录
-l 链接引用的库文件名称
其他命令可查看matlab中的帮助文档。
2、MEX例程
在本教程中，举例实现将两个矩阵相加（C = A + B）。

/********************************************************************
filename:     addMatrix.c
file ext:       c
author:        wy@gpgpu.org.cn
purpose:  test matlab with c
*********************************************************************/
#include &#34;mex.h&#34;
void mexFunction&#40;int nlhs, mxArray *plhs&#91;&#93;,int nrhs, const mxArray *prhs&#91;&#93;&#41;
&#123;
  [...]]]></description>
		<wfw:commentRss>http://gpgpu.org.cn/cuda/matlab_with_cuda_2/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Matlab下的CUDA编程(一)</title>
		<link>http://gpgpu.org.cn/cuda/matlab_with_cuda_1</link>
		<comments>http://gpgpu.org.cn/cuda/matlab_with_cuda_1#comments</comments>
		<pubDate>Thu, 04 Feb 2010 07:50:11 +0000</pubDate>
		<dc:creator>wy</dc:creator>
				<category><![CDATA[Application]]></category>
		<category><![CDATA[CUDA]]></category>
		<category><![CDATA[matlab]]></category>

		<guid isPermaLink="false">http://gpgpu.org.cn/cuda/matlab下的cuda编程一</guid>
		<description><![CDATA[引言
Matlab作为科学计算中的重要工具，它提供了丰富的基本函数和工具箱，从而在各个领域得到了广泛的应用。但Matlab的缺点是代码效率较低，在工程应用中只能作为模型验证，而不能得到实际应用。而CUDA作为显卡编程比较成熟的语言，能够充分利用GPU的计算能力，提高执行效率。因此如何能将CUDA的高效与matlab的简便有机的结合是本文要解决的问题，根据解决问题的方式我们将分为两节进行讲解。
Related PostsMatlab下的CUDA编程(二)Matlab下通过Mex文件编写C程序
本节参考NVIDIA网站相关资源，点击此处链接：

1...一种简单的GPU三维图像分割算法一个用于三维灰度图像CCL算法最简单的实现，将算法的过程分为并行和串行两部分，分别放在GPU上和CP...Ubuntu 9.04 安装 CUDA 开发环境1. 下载 CUDA 文件

从 Nvidia CUDA 官方网站（ http://www.nv...CUDA 3.0The CUDA Toolkit 3.0 Beta is now available to GPU ...OpenCL系列讲座(一) 概览OpenCL(Open Computing Language)是由Apple发起，由Khronos负...]]></description>
		<wfw:commentRss>http://gpgpu.org.cn/cuda/matlab_with_cuda_1/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AMD/ATI 发布Stream SDK 2.0正式版</title>
		<link>http://gpgpu.org.cn/opencl/amd-release-stream-sdk2</link>
		<comments>http://gpgpu.org.cn/opencl/amd-release-stream-sdk2#comments</comments>
		<pubDate>Tue, 22 Dec 2009 18:03:42 +0000</pubDate>
		<dc:creator>annkok</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[OpenCL]]></category>
		<category><![CDATA[amd]]></category>

		<guid isPermaLink="false">http://gpgpu.org.cn/?p=241</guid>
		<description><![CDATA[详细报道：
Link1
Link 2
官方下载
大致看了以下，依旧没有提供对image的支持，不过好歹增加了对dx和opengl的互操作。
根据之前测试版的经验，对性能提升不太报希望(ps 之前测试性能竟然和brook+实现在一个级别,比较汗的说）。
基于CT重建的性能测试稍候提供。
Related PostsAMD 发布OpenCL GPU实现近日AMD也发布了其GPU的opencl实现，第一时间去测试了一般，很遗憾，在NV的实现可以运行的代...OpenCL BenchmarkAMD和Sisoft联合在Sisoft的Sandra 2010里面添加了opencl benchma...OpenCL系列(四)-Memory本文将介绍Opencl中的各种存储类型和如何创建和使用它们。...IBM停止CELL开发坑，又少了一个。原文
update:好像是说停止了相关sdk的开发，转向了opencl，这点和前几...OpenCL系列讲座(三) Step By Step  我们已经写好的Kernel函数一般保存在一个字符串内，例如在第二节中的kernel：输入两个一维...]]></description>
		<wfw:commentRss>http://gpgpu.org.cn/opencl/amd-release-stream-sdk2/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Larrabee杯具了</title>
		<link>http://gpgpu.org.cn/news/intel-cancels-larrabee-consumer-graphics-chip</link>
		<comments>http://gpgpu.org.cn/news/intel-cancels-larrabee-consumer-graphics-chip#comments</comments>
		<pubDate>Sun, 06 Dec 2009 04:40:18 +0000</pubDate>
		<dc:creator>annkok</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[Intel]]></category>
		<category><![CDATA[larrabee]]></category>

		<guid isPermaLink="false">http://gpgpu.org.cn/?p=236</guid>
		<description><![CDATA[Intel说larrabee要以软件平台的形式发布了，至于实体&#8230;等吧。
Nvidia和ATI又可以小爽一段了。
话说现在竞争白热，局势却相对明朗了：Nvidia狂推自己的CUDA，AMD和IBM等一干人紧紧抱着Opencl，Intel自己没搞出来东西，又不甘落后，就自己再搞一套软件平台来搅浑水，Microsoft肯定是不屑跟他们这些人玩了，肯定要抱着自己的DX Compute闷头折腾了。
我们这些下游就得屁颠跟着他们晕了。
新闻Link
Random PostsAMD/ATI 发布Stream SDK 2.0正式版详细报道：
Link1
Link 2
官方下载
大致看了以下，依旧没有提供对image的支持...OpenCL系列讲座(二) 基本概念1、计算设备(Compute Device). 是指系统中支持OpenCL的各类处理器，可以是CPU...Matlab下的CUDA编程(二)Matlab下通过Mex文件编写C程序
本节参考NVIDIA网站相关资源，点击此处链接：

1...Matlab下的CUDA编程(一)引言

Matlab作为科学计算中的重要工具，它提供了丰富的基本函数和工具箱，从而在各个领域得到...Ubuntu 9.04 安装 CUDA 开发环境1. 下载 CUDA 文件

从 Nvidia CUDA 官方网站（ http://www.nv...]]></description>
		<wfw:commentRss>http://gpgpu.org.cn/news/intel-cancels-larrabee-consumer-graphics-chip/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>一种简单的GPU三维图像分割算法</title>
		<link>http://gpgpu.org.cn/cuda/a-simple-gpu-based-three-dimensional-image-segmentation-algorithm</link>
		<comments>http://gpgpu.org.cn/cuda/a-simple-gpu-based-three-dimensional-image-segmentation-algorithm#comments</comments>
		<pubDate>Sun, 06 Dec 2009 04:06:35 +0000</pubDate>
		<dc:creator>emaxin</dc:creator>
				<category><![CDATA[CUDA]]></category>
		<category><![CDATA[GPU Image Processing]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[segmentation]]></category>

		<guid isPermaLink="false">http://www.gpgpu.org.cn/?p=229</guid>
		<description><![CDATA[一个用于三维灰度图像CCL算法最简单的实现，将算法的过程分为并行和串行两部分，分别放在GPU上和CPU上运行]]></description>
		<wfw:commentRss>http://gpgpu.org.cn/cuda/a-simple-gpu-based-three-dimensional-image-segmentation-algorithm/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OpenCL Benchmark</title>
		<link>http://gpgpu.org.cn/opencl/opencl-benchmark</link>
		<comments>http://gpgpu.org.cn/opencl/opencl-benchmark#comments</comments>
		<pubDate>Sat, 05 Dec 2009 08:03:45 +0000</pubDate>
		<dc:creator>annkok</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[OpenCL]]></category>
		<category><![CDATA[Benchmark]]></category>

		<guid isPermaLink="false">http://gpgpu.org.cn/?p=226</guid>
		<description><![CDATA[AMD和Sisoft联合在Sisoft的Sandra 2010里面添加了opencl benchmark。支持的硬件有：

ATI GPUs: Radeon HD 5800, 5700 series
AMD CPUs: Athlon Neo, Six (6)/Eight (8)/Twelve (12)-Core Opteron 4100, 6100 server
Intel CPUs: Core i5/i7 desktop/mobile, Xeon Nehalem-EX server
nVidia GPUs: GT215, GT216, GT218 series
VIA CPUs: Nano 3000 series
Hardware monitoring: Analog Devices (ADT), SMSC, Winbond/Nuovoton 

大致是对带宽的测试和计算性能的测试，结果：
GPU结果
CPU结果
比较诡异的是，amd的显卡是5870 ，NVidia的显卡竟然选用的是9500，这个&#8230;有点太&#8230;
Related PostsAMD/ATI 发布Stream SDK 2.0正式版详细报道：
Link1
Link 2
官方下载
大致看了以下，依旧没有提供对image的支持...OpenCL系列(四)-Memory本文将介绍Opencl中的各种存储类型和如何创建和使用它们。...IBM停止CELL开发坑，又少了一个。原文
update:好像是说停止了相关sdk的开发，转向了opencl，这点和前几...OpenCL系列讲座(三) Step By Step  我们已经写好的Kernel函数一般保存在一个字符串内，例如在第二节中的kernel：输入两个一维...OpenCL系列讲座(二) 基本概念1、计算设备(Compute Device). 是指系统中支持OpenCL的各类处理器，可以是CPU...]]></description>
		<wfw:commentRss>http://gpgpu.org.cn/opencl/opencl-benchmark/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OpenCL系列(四)-Memory</title>
		<link>http://gpgpu.org.cn/opencl/opencl-step-by-step-4-memory</link>
		<comments>http://gpgpu.org.cn/opencl/opencl-step-by-step-4-memory#comments</comments>
		<pubDate>Sat, 28 Nov 2009 14:55:20 +0000</pubDate>
		<dc:creator>annkok</dc:creator>
				<category><![CDATA[OpenCL]]></category>
		<category><![CDATA[memory]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://gpgpu.org.cn/?p=172</guid>
		<description><![CDATA[本文将介绍Opencl中的各种存储类型和如何创建和使用它们。]]></description>
		<wfw:commentRss>http://gpgpu.org.cn/opencl/opencl-step-by-step-4-memory/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>IBM停止CELL开发</title>
		<link>http://gpgpu.org.cn/news/ibm_cell_phase_out</link>
		<comments>http://gpgpu.org.cn/news/ibm_cell_phase_out#comments</comments>
		<pubDate>Sat, 21 Nov 2009 17:04:32 +0000</pubDate>
		<dc:creator>annkok</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[CELL]]></category>
		<category><![CDATA[OpenCL]]></category>

		<guid isPermaLink="false">http://igpu.org/news/ibm%e5%81%9c%e6%ad%a2cell%e5%bc%80%e5%8f%91</guid>
		<description><![CDATA[坑，又少了一个。原文
update:好像是说停止了相关sdk的开发，转向了opencl，这点和前几天从amd那边得到的消息一致。
Related PostsAMD/ATI 发布Stream SDK 2.0正式版详细报道：
Link1
Link 2
官方下载
大致看了以下，依旧没有提供对image的支持...OpenCL BenchmarkAMD和Sisoft联合在Sisoft的Sandra 2010里面添加了opencl benchma...OpenCL系列(四)-Memory本文将介绍Opencl中的各种存储类型和如何创建和使用它们。...OpenCL系列讲座(三) Step By Step  我们已经写好的Kernel函数一般保存在一个字符串内，例如在第二节中的kernel：输入两个一维...OpenCL系列讲座(二) 基本概念1、计算设备(Compute Device). 是指系统中支持OpenCL的各类处理器，可以是CPU...]]></description>
		<wfw:commentRss>http://gpgpu.org.cn/news/ibm_cell_phase_out/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Ubuntu 9.04 安装 CUDA 开发环境</title>
		<link>http://gpgpu.org.cn/cuda/develop-cuda-in-ubuntu-9-04</link>
		<comments>http://gpgpu.org.cn/cuda/develop-cuda-in-ubuntu-9-04#comments</comments>
		<pubDate>Fri, 13 Nov 2009 02:49:03 +0000</pubDate>
		<dc:creator>pt</dc:creator>
				<category><![CDATA[CUDA]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://gpgpu.org.cn/?p=151</guid>
		<description><![CDATA[1. 下载 CUDA 文件
从 Nvidia CUDA 官方网站（ http://www.nvidia.com/object/cuda_home.html ）的下载页面可以找到适用于 ubuntu 9.04 的 CUDA 文件，包括：
*  NVIDIA Driver 190.18 Beta for Linux (Ubuntu 9.04) with CUDA Support
*  CUDA Toolkit 2.3 for Linux (Ubuntu 9.04)
*  CUDA SDK 2.3 code samples for Linux (Ubuntu 9.04)
第一个是显卡驱动。第二个是编译工具，包括库文件。这两个是必需的。第三个是一些示例代码，不是必需。
下载后将之放在用户主目录中，赋予可执行属性（ chmod +x 文件名 ），备用。
2. 安装驱动
首先，安装编译环境，因为安装驱动时要编译模块：
sudo apt-get install linux-headers-$(uname -r)$ build-essential
然后，卸载系统自带的所有跟 nvidia 相关的包，否则会跟驱动冲突。可以在“新立得软件包管理器”里搜索“nvidia”，把搜到的已安装的包全部卸载。
卸载后最好重启一下（貌似不重启也没关系）。
重启后可能无法正常进入 X 环境。没关系，安装驱动时还必须退出 [...]]]></description>
		<wfw:commentRss>http://gpgpu.org.cn/cuda/develop-cuda-in-ubuntu-9-04/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>OpenCL系列讲座(三) Step By Step</title>
		<link>http://gpgpu.org.cn/opencl/opencl_step_by_step_3</link>
		<comments>http://gpgpu.org.cn/opencl/opencl_step_by_step_3#comments</comments>
		<pubDate>Sun, 08 Nov 2009 12:22:56 +0000</pubDate>
		<dc:creator>emaxin</dc:creator>
				<category><![CDATA[OpenCL]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://igpu.org/?p=130</guid>
		<description><![CDATA[  我们已经写好的Kernel函数一般保存在一个字符串内，例如在第二节中的kernel：输入两个一维数组，用另外一个一维数组输出两个一维数组求和的结果，如下：

char* chKernelSource = &#34;__kernel sum 
(__global const float* a, __global const float* b,  
__global float* answer)
{
   int xid = get_global_id(0);
    answer[xid] = a[xid] + b[xid];
}&#34;;

如概述中所言，下面我们一步一步来写Host代码Main函数部分；
 
首先包含OpenCL的头文件

#include &#34;cl/opencl.h&#34;;

 Step 1: 定义WorkItem和WorkGroup的大小：

const unsigned int cnGroupItemSize = 512; 
// 表示WorkGroup里的WorkItem以一维形式排列，一个WorkGroup里有512个WorkItem
&#160;
const unsigned int cnGroupSize    = 3;
 // 表示WorkGroup以一维形式排列，一个Kernel里有3个WorkGroup
&#160;
const unsigned int cnItemSize = cnGroupItemSize * cnGroupSize ; 
// 一个Kernel里运行的所有WorkItem的个数

扩展：

如果WorkGroup和一个WorkGroup里的WorkItems以2维或3维形式组织的话，分别写成cnItemSize[i]和cnGroups[i]，i分别等于2和3；则cnItemSize等于 cnGroupItemSize[0] * cnGroupItemSize[1] *(cnGroupItemSize[2] )*cnGroups[0] * cnGroups[1] * (cnGroups[2]) 
cnItemSize是所有WorkItems的个数，和CUDA不同的是，CUDA用另外一个概念——GridDim来间接表示一个Kernel里面将运行多少个WorkItem（和CUDA里Thread的概念相同）。

Step 2: 创建显卡上下文(Device Context)

cl_context hContext; 
hContext = clCreateContextFromType&#40;0, CL_DEVICE_TYPE_GPU, 
                                   0, 0, 0&#41;; // 指定由GPU显卡进行运算
size_t nContextDescriptorSize; 
&#160;
clGetContextInfo&#40;hContext, CL_CONTEXT_DEVICES,  
&#160;
                 0, 0, &#38;amp;nContextDescriptorSize&#41;;  // 获得显卡ID字段的长度
&#160;
cl_device_id * aDevices = malloc&#40;nContextDescriptorSize&#41;; // 分配显卡ID字段的内存
&#160;
clGetContextInfo&#40;hContext, CL_CONTEXT_DEVICES, 
&#160;
                 nContextDescriptorSize, aDevices, 0&#41;;  // 读取显卡ID字段


简言之，显卡上下文就是对于运行Kernel代码的硬件的描述，存储了与硬件自身的参数，这里的hContext相当于一个handle，通过它可以得到安装的所有GPU硬件的信息
由于OpenCL是介于硬件和软件开发人员之间的中间件，支持GPU/CPU/CELL，开发人员可以通过第二个参数，指定用何种计算显卡进行计算；

扩展：

如果我们机器里面插了两块显卡，那么以上代码运行完后，将得到一个aDevices[]数组，存放了两块显卡的ID；
这里通过clGetContextInfo函数，只得到了显卡ID这一个属性，如果我们想得到更多关于显卡的参数，可以照猫画虎，代码如下：

 

clGetContextInfo&#40;hContext, CL_CONTEXT_PROPERTIES,  
&#160;
                 0, 0, &#38;amp;nContextDescriptorSize&#41;;  // 获得显卡属性字段的长度
&#160;
cl_context_properties * aDevicesProperty = malloc&#40;nContextDescriptorSize&#41;; // 分配显卡属性字段的内存
&#160;
clGetContextInfo&#40;hContext, CL_CONTEXT_DEVICES, 
&#160;
                 nContextDescriptorSize, aDevicesProperty , 0&#41;;  // 读取显卡属性字段
&#160;
 

Step 3: 创建一个命令队列(Command Queue)

cl_command_queue hCmdQueue; 
&#160;
hCmdQueue = clCreateCommandQueue&#40;hContext, aDevices&#91;0&#93;, 0, 0&#41;;  // 在查询到的0号显卡上创建命令队列


命令队列用来排列（或存放）在Kernel的显卡上运行的一系列“命令”，如：依次运行不同的Kernel、Host和Device交换数据等。
Command Queue存在时有必要的：Host可以将在显卡上运行的命令依次“放到”这个队列里面，由队列来管理各命令的运行顺序（命令间可以是顺序的，也可以是异步的），而并不需要等待一个操作在显卡运行完毕，才返回；
Host可以进行不会对显卡运算产生冲突的其它操作，相当于在显卡上和在Host上运行不同的线程；
在CUDA里，提供了一种简单的机制，默认在执行下一条命令前需要等待上一条命令执行完毕的；相比较，OpenCL引入的Command Queue机制更为灵活。

 
Step 4: 生成并加载BIN

cl_program hProgram; 
&#160;
hProgram = clCreateProgramWithSource&#40;hContext, 1,  
&#160;
                                     &#38;amp;chKernelSource, 0, 0&#41;; // 从源代码创建一个适合于当前显卡上下文的cl_program对象
clBuildProgram&#40;hProgram, 0, 0, 0, 0, 0&#41;;  // 编译此段代码


chKernelSource是一个字符串，里面包含了一个kernel代码的所有文字；
hProgram是一个句柄，指向加载的从源代码编译好的BIN代码（可执行代码）；
OpenCL本身不提供编译器，因此代码不会事先编译为能载入显卡的BIN代码，而是在运行时通过显卡厂商提供的编译器编译的；当然，OpenCL提供了加载并运行已生成的BIN代码的功能；
OpenCL作为一个中间层，感觉像是一个命令解释器，其实，在相同的显卡上，第二次加载BIN文件并不会重新编译，而是从缓存里面读取；

 
扩展

可以将多段源码编译为BIN代码，只需要传入多个源码char*头指针即可
用clCreateProgramWithBinary函数，可以跳过编译阶段，直接加载符合OpenCL规范的BIN代码

 
Step 5: 创建Kernel

cl_kernel hKernel; 
&#160;
hKernel = clCreateKernel&#40;hProgram, “vectorAdd”, 0&#41;; 
&#160;
 


创建Kernel的过程，可以看做从BIN代码提取Kernel对象的过程，需要指定Kernel函数名，且这个函数名在源代码中必须用__kernel关键字修饰；
clCreateKernel一次只提取一个Kernel对象，如果需要同时提取多个Kernel对象时，调用clCreateKernelsInProgram函数

 
Step 6: 在Host上分配内存，并读入数据

float * pA = new float&#91;cnItemSize&#93;; 
&#160;
float * pB = new float&#91;cnItemSize&#93;; 
&#160;
float * pC = new float&#91;cnItemSize&#93;; 
&#160;
 

Step 7: 分配显卡内存，并传入数据

cl_mem hDeviceMemA, hDeviceMemB, hDeviceMemC; 
&#160;
hDeviceMemA = clCreateBuffer&#40;hContext,  
&#160;
                          CL_MEM_READ_ONLY &#124;   CL_MEM_COPY_HOST_PTR, 
&#160;
                          cnItemSize * sizeof&#40;cl_float&#41;,  
&#160;
                          pA,  
&#160;
                          0&#41;; 
&#160;
hDeviceMemB = clCreateBuffer&#40;hContext,  
&#160;
                          CL_MEM_READ_ONLY &#124;   CL_MEM_COPY_HOST_PTR, 
&#160;
                          cnItemSize * sizeof&#40;cl_float&#41;,  
&#160;
                          pA,  
&#160;
                          0&#41;; 
&#160;
hDeviceMemC = clCreateBuffer&#40;hContext,  
&#160;
                          CL_MEM_WRITE_ONLY, 
&#160;
                          cnItemSize * sizeof&#40;cl_float&#41;,  
&#160;
                          0, 0&#41;; 


OpenCL分配显卡内存，不返单纯返回显卡内存的指针，而是通过“Buffer Object”来管理；和CUDA比起来，优点是可以设置待分配内存的读写属性，还可以设置Host内存的指针，分配后直接从Host拷贝数据，不用再次调用写显卡内存函数。

 
扩展：

读写“Buffer Object”用另外两个函数来实现，分别是：clEnqueueReadBuffer和clEnqueueWriteBuffer。
在“Buffer Object”间拷贝数据用clEnqueueCopyBuffer函数。
clCreateBuffer函数中，第二个参数如果设置为CL_MEM_ALLOC_HOST_PTR，将分配Host内存给Kernel使用；这样可以以减少Host和Device交换数据的时间。Kernel调用完成后，执行clEnqueueMapBuffer函数，然后再从Host内存中读取数据。

 
Step 8: 为Kernel函数指定输入参数

clSetKernelArg&#40;hKernel, 0, sizeof&#40;cl_mem&#41;, &#40;void *&#41;&#38;amp;hDeviceMemA&#41;; 
&#160;
clSetKernelArg&#40;hKernel, 1, sizeof&#40;cl_mem&#41;, &#40;void *&#41;&#38;amp;hDeviceMemB&#41;; 
&#160;
clSetKernelArg&#40;hKernel, 2, sizeof&#40;cl_mem&#41;, &#40;void *&#41;&#38;amp;hDeviceMemC&#41;; 


第二个参数表明输入参数和函数声明中的第几个参数相对应

 
Step 9: 执行Kernel函数

clEnqueueNDRangeKernel&#40;hCmdQueue, hKernel, 1, 0,  
&#160;
                       &#38;amp;cnGroupSize, &#38;amp;cnItemSize, 0&#41;; 

 
Step 10: 从显卡内存拷贝运行结果并释放内存

clEnqueueReadBuffer&#40;hContext, hDeviceC, CL_TRUE, 0,  
&#160;
                    cnItemSize * sizeof&#40;cl_float&#41;,  
&#160;
                    pC, 0, 0, 0&#41;; 
&#160;
delete&#91;&#93; pA; 
&#160;
delete&#91;&#93; pB; 
&#160;
delete&#91;&#93; pC; 
&#160;
clReleaseMemObj&#40;hDeviceMemA&#41;; 
&#160;
clReleaseMemObj&#40;hDeviceMemB&#41;; 
&#160;
clReleaseMemObj&#40;hDeviceMemC&#41;; 

 
 
参考：
1. NVIDIA OpenCL JumpStart Guide 0.9
2. http://developer.apple.com/mac/library/documentation/Performance/Conceptual/
OpenCL_MacProgGuide/
3. http://www.macresearch.org/category/tutorials
4. http://www.khronos.org/message_boards/viewforum.php?f=28&#38;sid=9f376ea1750bb6c84e8f361d5ff64cd0
Related PostsOpenCL系列(四)-Memory本文将介绍Opencl中的各种存储类型和如何创建和使用它们。...OpenCL系列讲座(二) 基本概念1、计算设备(Compute Device). 是指系统中支持OpenCL的各类处理器，可以是CPU...OpenCL系列讲座(一) 概览OpenCL(Open Computing Language)是由Apple发起，由Khronos负...AMD/ATI 发布Stream SDK 2.0正式版详细报道：
Link1
Link 2
官方下载
大致看了以下，依旧没有提供对image的支持...OpenCL BenchmarkAMD和Sisoft联合在Sisoft的Sandra 2010里面添加了opencl benchma...]]></description>
		<wfw:commentRss>http://gpgpu.org.cn/opencl/opencl_step_by_step_3/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
