<?xml version="1.0" encoding="GBK" ?>
<rss version="2.0" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dcterms="http://purl.org/dc/terms/">
 <channel>
  	  <title><![CDATA[Pan Ant]]></title>
	  <link>http://pannsp.blog.163.com</link>
	  <description><![CDATA[ 人生就象小蚂蚁一样的不懈的努力，奋勇向前]]></description>
	  <language>zh-CN</language>
	  <pubDate>Wed, 25 Nov 2009 16:57:08 +0800</pubDate>
	  <lastBuildDate>Wed, 25 Nov 2009 16:57:08 +0800</lastBuildDate>
	  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
	  <generator><![CDATA[NetEase Space]]></generator>
	  <managingEditor><![CDATA[pannsp]]></managingEditor>
	  <webMaster><![CDATA[pannsp]]></webMaster>
		  <ttl>120</ttl>
	  <image>
	  	<title><![CDATA[Pan Ant]]></title>
	  	<url>http://ava.bimg.126.net/photo/Sbc5DySyW3ltbkvc-Vv9mA==/213076557370107277.jpg</url>
	  	<link>http://pannsp.blog.163.com</link>
	  </image>
  <item>
  	<title><![CDATA[VS制作安装程序 ]]></title>	
    <link>http://pannsp.blog.163.com/blog/static/92023200910254578241</link>
    <description><![CDATA[<div><P style="TEXT-INDENT: 2em"></P>
<P style="TEXT-INDENT: 2em">(一)，安装程序</P>
<P style="TEXT-INDENT: 2em">以前用vs制作过安装程序，现在把步骤写出来，有帮助的大家一定要顶哦</P>
<P style="TEXT-INDENT: 2em">第一步:建立工程</P>
<P style="TEXT-INDENT: 2em">1.打开vs，新建项目-&gt;其他项目类型-&gt;安装和部署(這個子项下面有安装项目和Web安装项目等,安装项目就是普通的桌面程序安装,Web安装就是安装网站,通常安装到IIS下,这里以普通桌面程序安装为例),新建安装项目，命名为SetupTest。</P>
<P style="TEXT-INDENT: 2em">2.新建工程后，可以在“解决方案资源管理器”里面看到子项：文件系统编辑器，注册表编辑器，文件类型编辑器，用户界面编辑器，自定义操作编辑器，启动条件编辑器。下面将会举例来说明各个地方怎么使用。</P>
<P style="TEXT-INDENT: 2em">3.最简单的一个安装程序：点击“文件类型编辑器”，找到“应用程序文件夹”，添加-&gt;文件，选择你要打包的程序文件（譬如我的程序有run.exe和lib.dll组成），确定。在项目“SetupTest”上点击生成，一个简单的安装包就做好了。生成后，点击安装，如果没有错误的话，你现在可以看到你的安装包的效果了。</P>
<P style="TEXT-INDENT: 2em">4.如何自定义代码？在制作过程中，这些简单的功能不能满足你的要求，那么就自己写代码来完成吧。</P>
<P style="TEXT-INDENT: 2em">解决方案-&gt;添加-&gt;新建项目-&gt;类库,命名为Library，在项目Library下面，删除Class1.cs，添加-&gt;新建项目-&gt;安装程序类（名称为InstallerTest.cs），打开InstallerTest.cs，你会发现这个class InstallerTest继承自Installer，里面就一个构造函数。好了，现在安装程序不能完成的功能，你可以在这个类写代码来自己实现了。</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; 举例：在构造函数里面添上this.BeforeInstall += new InstallEventHandler(InstallerTest_BeforeInstall)，实现方法InstallerTest_BeforeInstall(object sender, InstallEventArgs e)，就可以完成程序在安装之前要做的事情了，Installer主要要用到的事件不外乎这几个：BeforeInstall，AfterInstall，AfterRollback，AfterUninstall，BeforeRollback，BeforeUninstall，很明显的意义，就不用解释了吧</P>
<P style="TEXT-INDENT: 2em">5.完成了4中的InstallerTest_BeforeInstall方法，是不是在安装之前就要执行InstallerTest_BeforeInstall中的代码呢，答案是不行。还要用到“自定义操作编辑器”。项目SetupTest-&gt;添加-&gt;项目输出-&gt;选择Library后确定，你会发现，在项目SetupTest下面多了一项“主输出来自Library(活动)”的字样。好，到正题了，来到“自定义操作”处，可以看到四个子项：安装，提交，回滚，卸载。在安装上面点击右键-&gt;添加自定义操作-&gt;选择“主输出来自Library(活动)”。这样，重新生成后，安装之前就会执行InstallerTest_BeforeInstall代码了。（当然，如果你在要在卸载前后做一些事情，就必须在“卸载”处也把自定义操作添加进去，否则即使监听了事件AfterUninstall，也不会执行的，完全之策是在安装，提交，回滚，卸载里面都把主输出来自Library(活动)添加进去就不怕了，嘿嘿）</P>
<P style="TEXT-INDENT: 2em">6.如何得到用户所选安装路径？在实现代码的时候，用户所选安装路径大多数情况下都要用到，怎么得到他的值呢？在自定义操作-&gt;安装-&gt;“主输出来自Library(活动)”上面右键属性，在CustomActionData中填入/targetdir="[TARGETDIR]""， 然后在类InstallerTest中用这句话this.Context.Parameters["targetdir"] 就可以取得安装的目录了。</P>
<P style="TEXT-INDENT: 2em">7.如何得到用户在安装的时候输入的更多的信息？用户在安装的时候，还有可能塡很多其他的信息，比如我以前做的一个安装文件中包含安装数据库，安装的时候用户就要填写数据库密码，用户名等信息。实现如下：用户界面-&gt;安装-&gt;启动-&gt;右键-&gt;添加对话框-&gt;选择"文本框(A)",可以看到文本框(A)有很多属性，如：Edit1Label，Edit1Property，Edit1Value，Edit1Visible等，Edit1Label就是這個输入框所要输入的内容的提示语，Edit1Value就是所输入的内容。</P>
<P style="TEXT-INDENT: 2em">例如：将Edit1Label内容填上“请输入数据库用户名:”，将Edit1Property内容填上DBUSER,Edit1Visible选问true，另外在6中所提到的CustomActionData中，添加上 /dbuser="[DBUSER]",再在InstallerTest中用这句话this.Context.Parameters["dbuser"] 就可以或得用户在安装的时候所填写的用户名了（其他的单选框，复选框，button按钮框都差不多获取值的方法，不再累赘）.总之，添加了一些对话框后，把属性好，还要在CustomActionData添加一个/parm="[属性值]",才能在代码中获取改值。</P>
<P style="TEXT-INDENT: 2em">8.其他:创建快捷方式,你可能在程序中要创建多个快捷方式，这里展示一种写代码的方式来创建快方式（各个选项意义就不说了，其中有些选项是可以不需要的，读者可自己测试选择使用）</P>
<P style="TEXT-INDENT: 2em">添加引用：com名称为Windows Script Host Object Model</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; using IWshRuntimeLibrary; </P>
<P style="TEXT-INDENT: 2em">string dk = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);//得到桌面文件夹 </P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShellClass(); </P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(@dk + """快捷方式到数据中心SiteDataCenter.lnk"); </P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P style="TEXT-INDENT: 2em">shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(@dk + """互联网公共上网服务场所信息安全管理系统.lnk"); </P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; shortcut.TargetPath = @"%HOMEDRIVE%/Program Files"Internet Explorer"IEXPLORE.EXE";</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; shortcut.Arguments = "<A href="http://localhost/web/Main.html%22;//">http://localhost/web/Main.html";//</A>参数 </P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; shortcut.Description = "火星网络安全有限责任公司"; </P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; shortcut.WorkingDirectory = pathWeb;//程序所在文件夹，在快捷方式图标点击右键可以看到此属性 </P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; shortcut.IconLocation = "notepad.exe, 0";//图标 </P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; shortcut.Hotkey = "CTRL+SHIFT+N";//热键 </P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; shortcut.WindowStyle = 1; </P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; shortcut.Save(); </P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P style="TEXT-INDENT: 2em">9.如何在安装完成后自动运行某个程序呢（如run.exe）？</P>
<P style="TEXT-INDENT: 2em">将这句代码写在AfterInstall里面：</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Diagnostics.ProcessStartInfo psiConfig = new System.Diagnostics.ProcessStartInfo(path + "<A href="file://run.exe%22);//path">""run.exe");//path</A>即是安装的目录</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.Diagnostics.Process pConfig = System.Diagnostics.Process.Start(psiConfig);</P>
<P style="TEXT-INDENT: 2em">(二)卸载（以下是转的，看客自己提取有用信息）</P>
<P style="TEXT-INDENT: 2em">1.新建一C# Windows From项目，完成项目的工作</P>
<P style="TEXT-INDENT: 2em">2.菜单-》文件-》添加项目-》新建项-》安装部署项目-》安装向导</P>
<P style="TEXT-INDENT: 2em">选择上面那个项目的主输出。</P>
<P style="TEXT-INDENT: 2em">在安装项目的属性栏中会显示出"XXX部署项目属性"，在第一个AddRemoveProgramsIcon属性后"浏览"到你的ICO图标，接着在下面Author输入作者名，也就是你的名字，其他Localization是语言属性，Manufacturer,ProductName的属性将是用户在安装时默认设置的文件夹路径一部分了，设置Title为你的应用程序标题名称。</P>
<P style="TEXT-INDENT: 2em">3.右击"安装项目"上，选择文件系统视图</P>
<P style="TEXT-INDENT: 2em">在应用程序文件夹，添加一个文件，如果是WIN98下的安装，请选择WIN98操作系统目录下的msiexec.exe文件（一般在c:"windows"system目录下）；如果是其他，比如WIN2K系列，请选择该操作系统目录下的msiexec.exe文件(一般在"winnt"system32目录下)。</P>
<P style="TEXT-INDENT: 2em">4.在文件系统视图的"用户程序菜单"里，添加一个目录，命名为项目的名字，</P>
<P style="TEXT-INDENT: 2em">并且，在这个目录里，添加一个快捷方式，指向项目的主输出，</P>
<P style="TEXT-INDENT: 2em">再添加一个快捷方式，指向加入的文件Msiexec.exe，命名为："卸载"，</P>
<P style="TEXT-INDENT: 2em">5.在解决方案里，选择安装项目，再打开属性视图，打开安装项目的属性面板，里面有一项：</P>
<P style="TEXT-INDENT: 2em">ProductCode {5284694C-24C1-4FCF-A705-D4B9A0081274}</P>
<P style="TEXT-INDENT: 2em">把后面的{5284694C-24C1-4FCF-A705-D4B9A0081274}，复制一下。</P>
<P style="TEXT-INDENT: 2em">6.在"卸载"快捷方式的属性里，有一个叫Arguments的属性栏，在属性栏中首先填入"/x"后加一个空格，再"粘贴"，将ProductCode的属性值COPY至此。</P>
<P style="TEXT-INDENT: 2em">7、再在VS.Net的顶部菜单栏中"生成"-"生成 XXXX"(XXXX为你的项目名称)</P>
<P style="TEXT-INDENT: 2em">补充：合并模块项目的使用</P>
<P style="TEXT-INDENT: 2em">若在某个大型的安装程序中，模块飞分为模块1，模块2等，每次都把模块N的文件加进来重新编译就很麻烦，这是就可用“合并模块项目(MergeModule)”。</P>
<P style="TEXT-INDENT: 2em">1.新建一个合并模块项目，名字为MergeModule1，添加需要的文件，生成后，将看到MergeModule1.msm</P>
<P style="TEXT-INDENT: 2em">2.在這個大型的安装程序中，添加“合并模块”，将MergeModule1.msm添加进去</P>
<P style="TEXT-INDENT: 2em">3.生成，安装，你会发现MergeModule1的内容也被安装到了安装目录下</P>
<P style="TEXT-INDENT: 2em"></P></div>]]></description>
	    <author><![CDATA[pannsp]]></author>
	    <comments>http://pannsp.blog.163.com/blog/static/92023200910254578241</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://pannsp.blog.163.com/blog/static/92023200910254578241</guid>
    <pubDate>Wed, 25 Nov 2009 16:57:08 +0800</pubDate>
    <dcterms:modified>2009-11-25T16:57:08+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[经史子集（的分类）和四书五经]]></title>	
    <link>http://pannsp.blog.163.com/blog/static/920232009102410924899</link>
    <description><![CDATA[<div><P style="TEXT-INDENT: 2em">经史子集”是我国古代读书人对经典的分类法。 </P>
<P style="TEXT-INDENT: 2em">　　“经”是指古代社会中的政教、纲常伦理、道德规范的教条，主要是儒家的典籍，有儒学十三经：《周易》、《尚书》、《周礼》、《礼记》、《仪礼》、《诗经》、《春秋左传》、《春秋公羊传》、《春秋谷梁传》、《论语》、《孝经》、《尔雅》、《孟子》。</P>
<P style="TEXT-INDENT: 2em">　　“史”是各种体裁历史著作，分为正史、编年、纪事本末、别史杂史、诏令奏议、传记、史钞、载记、时令、地理、职官、政书、目录、史评十五类。 </P>
<P style="TEXT-INDENT: 2em">　　“子”是诸子百家及释道宗教著作，分为儒家、兵家、法家、农家、医家、天文算法、术数、艺术、诸录、杂家、类书、小说家、释家、道家十四类。 </P>
<P style="TEXT-INDENT: 2em">　　“集”是收历代作家一人或多人的散文、骈文、诗、词、散曲等的集子和文学评论、戏曲等著作，分为楚辞、别集、诗文评、诗词五类。 </P>
<P style="TEXT-INDENT: 2em">　　注：以上分类依照　清《四库全书》。</P>
<P style="TEXT-INDENT: 2em">-------------------------------------------------------</P>
<P style="TEXT-INDENT: 2em">四书：亦称四子书，即《大学》、《中庸》、《论语》、《孟子》；五经：《诗》、《书》、《礼》、《易》、《春秋》。指儒家经典。</P></div>]]></description>
	    <author><![CDATA[pannsp]]></author>
	    <comments>http://pannsp.blog.163.com/blog/static/920232009102410924899</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://pannsp.blog.163.com/blog/static/920232009102410924899</guid>
    <pubDate>Tue, 24 Nov 2009 10:09:24 +0800</pubDate>
    <dcterms:modified>2009-11-24T10:17:18+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[matlab中各类对话框和窗口(转)]]></title>	
    <link>http://pannsp.blog.163.com/blog/static/920232009102332122895</link>
    <description><![CDATA[<div><P style="TEXT-INDENT: 2em">*对话框设计：在图形用户界面程序设计中，对话框是重要的信息显示和获取输入数据的用户界面对象。</P>
<P style="TEXT-INDENT: 2em">1、公共对话框:</P>
<P style="TEXT-INDENT: 2em">&nbsp;公共对话框是利用windows资源的对话框，包括文件打开、文件保存、颜色设置、字体设置、打印设置等。</P>
<P style="TEXT-INDENT: 2em">1) 文件打开对话框：用于打开文件</P>
<P style="TEXT-INDENT: 2em">uigetfile</P>
<P style="TEXT-INDENT: 2em">uigetfile(‘FilterSpec’)</P>
<P style="TEXT-INDENT: 2em">uigetfile(‘FilterSpec’,’DialogTitle’)</P>
<P style="TEXT-INDENT: 2em">uigetfile(‘FilterSpec’,’DialogTitle’,x,y)</P>
<P style="TEXT-INDENT: 2em">[fname,pname]=uigetfile(…)</P>
<P style="TEXT-INDENT: 2em">2) 文件保存对话框：用于保存文件</P>
<P style="TEXT-INDENT: 2em">uiputfile</P>
<P style="TEXT-INDENT: 2em">uiputfile(‘InitFile’)</P>
<P style="TEXT-INDENT: 2em">uiputfile(‘InitFile’,’DialogTitle’)</P>
<P style="TEXT-INDENT: 2em">uiputfile(‘InitFile’,’DialogTitle’,x,y)</P>
<P style="TEXT-INDENT: 2em">[fname,pname]=uiputfile(…)</P>
<P style="TEXT-INDENT: 2em">3) 颜色设置对话框：用于图形对象颜色的交互设置</P>
<P style="TEXT-INDENT: 2em">c=uisetcolor(‘h_or_c,’DialogTitle’)</P>
<P style="TEXT-INDENT: 2em">4) 字体设置对话框：用于字体属性的交互式设置</P>
<P style="TEXT-INDENT: 2em">uisetfont</P>
<P style="TEXT-INDENT: 2em">uisetfont(h)</P>
<P style="TEXT-INDENT: 2em">uisetfont(S)</P>
<P style="TEXT-INDENT: 2em">uisetfont(h,’DialogTitle’)</P>
<P style="TEXT-INDENT: 2em">uisetfont(S,’DialogTitle’)</P>
<P style="TEXT-INDENT: 2em">S=uisetfont(…)</P>
<P style="TEXT-INDENT: 2em">5） 打印设置对话框：用于打印页面的交互式设置</P>
<P style="TEXT-INDENT: 2em">dlg=pagesetupdlg(fig)</P>
<P style="TEXT-INDENT: 2em">pagedlg</P>
<P style="TEXT-INDENT: 2em">pagedlg(fig)</P>
<P style="TEXT-INDENT: 2em">6) 打印预览对话框：用于对打印页面进行预览</P>
<P style="TEXT-INDENT: 2em">printpreview</P>
<P style="TEXT-INDENT: 2em">printpreview(f)</P>
<P style="TEXT-INDENT: 2em">7) 打印对话框：</P>
<P style="TEXT-INDENT: 2em">printdlg</P>
<P style="TEXT-INDENT: 2em">printdlg(fig)</P>
<P style="TEXT-INDENT: 2em">printdlg(‘-crossplatform’,fig)</P>
<P style="TEXT-INDENT: 2em">printdlg(-‘setup’,fig)</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">2、MATLAB专用对话框</P>
<P style="TEXT-INDENT: 2em">1)错误信息对话框：用于提示错误信息</P>
<P style="TEXT-INDENT: 2em">errordlg &nbsp;打开默认的错误信息对话框</P>
<P style="TEXT-INDENT: 2em">errordlg(‘errorstring’) 打开显示’errorstring’信息的错误信息对话框</P>
<P style="TEXT-INDENT: 2em">errordlg(‘errorstring’,’dlgname’) 打开显示’errorstring’信息的错误信息对话框,对话框的标题由‘dlgname’指定</P>
<P style="TEXT-INDENT: 2em">erordlg(‘errorstring’,’dlgname’,’on’) 打开显示’errorstring’信息的错误信息对话框,对话框的标题由‘dlgname’指定. 如果对话框已存在，’on’参数将对话框显示在最前端。</P>
<P style="TEXT-INDENT: 2em">h=errodlg(…)&nbsp; 返回对话框句柄</P>
<P style="TEXT-INDENT: 2em">例：errordlg('输入错误,请重新输入','错误信息')</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">&nbsp;2) 帮助对话框：用于帮助提示信息</P>
<P style="TEXT-INDENT: 2em">&nbsp;helpdlg&nbsp;&nbsp;&nbsp; 打开默认的帮助对话框</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">helpdlg(‘helpstring’)&nbsp; 打开显示’errorstring’信息的帮助对话框,</P>
<P style="TEXT-INDENT: 2em">helpdlg(‘helpstring’,’dlgname’) 打开显示’errorstring’信息的帮助对话框, 对话框的标题由‘dlgname’指定</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">h=helpdlg(…) &nbsp;返回对话框句柄</P>
<P style="TEXT-INDENT: 2em">例：helpdlg('矩阵尺寸必须相等','在线帮助')</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">3) 输入对话框：用于输入信息</P>
<P style="TEXT-INDENT: 2em">answer=inputdlg(prompt) &nbsp;打开输入对话框，prompt为单元数组，用于定义输入数据窗口的个数和显示提示信息，answer为用于存储输入数据的单元数组。</P>
<P style="TEXT-INDENT: 2em">answer=inputdlg(prompt,title)&nbsp; 与上者相同，title确定对话框的标题。</P>
<P style="TEXT-INDENT: 2em">answer=inputdlg(prompt,title,lineNo)&nbsp; 参数lineNo可以是标量、列矢量或m×2阶矩阵，若为标量，表示每个输入窗口的行数均为lineNo；若为列矢量，则每个输入窗口的行数由列矢量lineNo的每个元素确定；若为矩阵，每个元素对应一个输入窗口，每行的第一列为输入窗口的行数，第二列为输入窗口的宽度。</P>
<P style="TEXT-INDENT: 2em">answer=inputdlg(prompt,title,lineNo,de<I>f</I>Ans) 参数defans为一个单元数组，存储每个输入数据的默认值，元素个数必须与prompt 所定义的输入窗口数相同，所有元素必须是字符串。</P>
<P style="TEXT-INDENT: 2em">answer=inputdlg(prompt,title,lineNo,de<I>f</I>Ans,Sesize) 参数resize决定输入对话框的大小能否被调整，可选值为on或off.</P>
<P style="TEXT-INDENT: 2em">例：prompt={'Input Name','Input Age'};</P>
<P style="TEXT-INDENT: 2em">title='Input Name and Age';</P>
<P style="TEXT-INDENT: 2em">lines=[2 1]';</P>
<P style="TEXT-INDENT: 2em">def={'John Smith','35'};</P>
<P style="TEXT-INDENT: 2em">answer=inputdlg(prompt,title,lines,def);</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">4) 列表选择对话框：用于在多个选项中选择需要的值</P>
<P style="TEXT-INDENT: 2em">[selection,ok]=listdlg(‘Liststring’,S,…) 输出参数selection为一个矢量，存储所选择的列表项的索引号，</P>
<P style="TEXT-INDENT: 2em">输入参数为可选项’Liststring’（单元数组）, ’SelectionMode’（’single’或’multiple’,&nbsp; ’ListSize’([wight,height]),&nbsp; ’Name’ (对话框标题）等</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">5) 信息提示对话框：用于显示提示信息</P>
<P style="TEXT-INDENT: 2em">msgbox(message)&nbsp; 打开信息提示对话框，显示message信息。</P>
<P style="TEXT-INDENT: 2em">msgbox(message,title)&nbsp; title确定对话框标题。</P>
<P style="TEXT-INDENT: 2em">msgbox(message,title,’icon’) icon用于显示图标，可选图标包括：none(无图标)/error/help/warn/custom（用户定义）</P>
<P style="TEXT-INDENT: 2em">msgbox(message,title,’custom’,icondata,iconcmap) 当使用用户定义图标时，icondata为定义图标的图像数据，iconcmap为图像的色彩图。</P>
<P style="TEXT-INDENT: 2em">msgbox(…,’creatmode’)&nbsp; 选择模式creatmode,选项为：modal, non_modal, 和replace。</P>
<P style="TEXT-INDENT: 2em">h=msgbox(…)&nbsp;&nbsp; 返回对话框句柄</P>
<P style="TEXT-INDENT: 2em">6) 问题提示对话框：用于回答问题的多种选择</P>
<P style="TEXT-INDENT: 2em">button=questdlg(‘qstring’) 打开问题提示对话框，有三个按钮，分别为：yes,no和cancel,’questdlg’确定提示信息。</P>
<P style="TEXT-INDENT: 2em">button=questdlg(‘qstring’,’title’) title确定对话框标题。</P>
<P style="TEXT-INDENT: 2em">button=questdlg(‘qstring’’title’,’default’) 当按回车键时，返回default值default 必须是yes,no或cancel 之一。</P>
<P style="TEXT-INDENT: 2em">button=questdlg(‘qstring’,’title’,’str1’,’str2’,’default’) 打开问题提示对话框，有两个按钮，分别由str1和str2确定，’qstdlg’确定提示信息，default必须是str1或str2之一。</P>
<P style="TEXT-INDENT: 2em">button=questdlg(‘qstring’, ’title’,’str1’,’str2’,’str3’,’default’) 打开问题提示对话框，有三个按钮，分别由str1， str2和str3确定，’qstdlg’确定提示信息，default必须是str1, str2或str3之一。</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">7) 进程条：以图形方式显示运算或处理的进程</P>
<P style="TEXT-INDENT: 2em">h=waitbar(x,’title’)&nbsp; 显示以title为标题的进程条，x为进程条的比例长度，其值必须在0到1之间，h为返回的进程条对象的句柄。</P>
<P style="TEXT-INDENT: 2em">waitbar(x,’title’,’creatcancelbtn’,’button_callback’) 在进程条上使用creatcancelbtn参数创建一个撤销按钮，在进程中按下撤销按钮将调用button_callback函数。</P>
<P style="TEXT-INDENT: 2em">waitbar(…,property_name,property_value,…) 选择其它由prompt_name定义的参数，参数值由prompt_value指定。</P>
<P style="TEXT-INDENT: 2em">例：</P>
<P style="TEXT-INDENT: 2em">h=waitbar(0,'pleas wait...');</P>
<P style="TEXT-INDENT: 2em">for i=1:10000</P>
<P style="TEXT-INDENT: 2em">&nbsp; waitbar(i/10000,h)</P>
<P style="TEXT-INDENT: 2em">end</P>
<P style="TEXT-INDENT: 2em">&nbsp; close(h)</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">8）警告信息对话框：用于提示警告信息</P>
<P style="TEXT-INDENT: 2em">&nbsp;h=warndlg(‘warningstring’,’dlgname’) 打开警告信息对话框，显示warningstring信息，dlgname确定对话框标题，h为返回对话句柄。</P></div>]]></description>
	    <author><![CDATA[pannsp]]></author>
	    <comments>http://pannsp.blog.163.com/blog/static/920232009102332122895</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://pannsp.blog.163.com/blog/static/920232009102332122895</guid>
    <pubDate>Mon, 23 Nov 2009 15:21:22 +0800</pubDate>
    <dcterms:modified>2009-11-23T15:21:22+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[快速开发CUDA windows 程序]]></title>	
    <link>http://pannsp.blog.163.com/blog/static/920232009916111354228</link>
    <description><![CDATA[<div><P><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">建立一个简单的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">CDUA</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">程序，需要设置的参数有很多，这样对于初学者来说增加了难度。在这里作者开发了一个针对</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">VS2005</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">CUDA wizard </SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">《</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial"><A href="http://blog.csdn.net/OpenHero/archive/2008/04/18/2305856.aspx"><SPAN style="COLOR: rgb(51,102,153)">CUDA VS2005 Wizard</SPAN></A></SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">》，这</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">样就可以很方便的在</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">VS2005</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">的环境中进行</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">CUDA</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">的开发。</SPAN></P>
<P><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">下面是快速入门的流程：</SPAN></P>
<P><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">1</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">。安装</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: Arial"> </SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">《</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial"><A href="http://blog.csdn.net/OpenHero/archive/2008/04/18/2305856.aspx"><SPAN style="COLOR: rgb(51,102,153)">CUDA VS2005 Wizard</SPAN></A></SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">》就可以可以在</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">VS</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">的工程里面看到一个</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">CUDAWin32App</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">的工程向导。</SPAN></P>
<P>&nbsp;</P>
<P><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体"><IMG title="快速开发CUDA windows 程序 - pannsp - Pan Ant" alt="" src="http://www.hpctech.com/uploadfile/2009/0819/20090819120403467.jpg">点击这个就可以创建一个简单的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">CUDA</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">工程，里面已经就有一个简单的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">example</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">，可以进行</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">CUDA</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">开发了。</SPAN></P>
<P><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">2.</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">我这里创建的是一个测试</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">Thread </SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">demo</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">，所以生成的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">cu</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">文件名字就是</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">threaddemo1<IMG title="快速开发CUDA windows 程序 - pannsp - Pan Ant" alt="" src="http://www.hpctech.com/uploadfile/2009/0819/20090819120403644.jpg"></SPAN></P>
<P><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">可以看到这里已经由</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">VS wizard</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">向导自动生成了一些简单的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">hello cuda</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">的程序。</SPAN></P>
<P><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">3.</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">我们再来看工程的环境配置，就可以发现</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">Debug Release EmuDebug EmuRelease</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">已经在系统设置里面了，已经由</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">Wizard</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">自动生成了工程的各个环节配置。</SPAN></P>
<P>&nbsp;</P>
<P><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial"><IMG title="快速开发CUDA windows 程序 - pannsp - Pan Ant" style="WIDTH: 700px" alt="" src="http://www.hpctech.com/uploadfile/2009/0819/20090819120403580.jpg">4.</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">工程环境变量的查看和配置，右键点击工程，选择</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">“</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">属性</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">” </SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">或者</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial"> “properties" </SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">就可以看到</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">project</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">的环境配置里面就多了一个</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">CUDA</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">选项，这里就可以对</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">CUDA</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">进行环境配置。</SPAN></P>
<P>&nbsp;</P>
<P><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial"><IMG title="快速开发CUDA windows 程序 - pannsp - Pan Ant" alt="" src="http://www.hpctech.com/uploadfile/2009/0819/20090819120403322.jpg">&nbsp;<IMG title="快速开发CUDA windows 程序 - pannsp - Pan Ant" style="WIDTH: 700px" alt="" src="http://www.hpctech.com/uploadfile/2009/0819/20090819120403254.jpg"></SPAN></P>
<P><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">5.</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">编译程序</SPAN></P>
<P>&nbsp;</P>
<P><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial"><IMG title="快速开发CUDA windows 程序 - pannsp - Pan Ant" alt="" src="http://www.hpctech.com/uploadfile/2009/0819/20090819120403412.jpg"><BR>6.</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">运行</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: Arial"> </SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">输出</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">CUDA initialized. Hello CUDA</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">time</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">！</SPAN></P>
<P>&nbsp;</P>
<P><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial"><IMG title="快速开发CUDA windows 程序 - pannsp - Pan Ant" style="WIDTH: 700px" alt="" src="http://www.hpctech.com/uploadfile/2009/0819/20090819120403114.jpg">7.</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">调试，需要把</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">cu</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">文件关联到</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">VC</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">project</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">里面，不然调试的时候不能断点跟踪进去，所以必须在系统的</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">VC++project </SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">环境中加入</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">*.cu</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">文件，</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">VS</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">才能把你的程序和源文件关联起来，进行调试。</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial"><BR></SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">如果没找到关联</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">cu</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">，在调试的时候就会发现找不到源文件。</SPAN></P>
<P>&nbsp;</P>
<P><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial"><IMG title="快速开发CUDA windows 程序 - pannsp - Pan Ant" alt="" src="http://www.hpctech.com/uploadfile/2009/0819/20090819120403192.jpg">8.</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">简单的快速开发就讲到这里了。我的字体这里显示高亮度，或许有些朋友会问，怎么才能让</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">CU</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">文件显示高亮度，可以查看我的另一篇翻译，《</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial"><A href="http://blog.csdn.net/OpenHero/archive/2008/04/17/2301617.aspx"><SPAN style="COLOR: rgb(51,102,153)">syntax highlighting when editing your .cu files in Visual Studio</SPAN></A></SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">》</SPAN></P>
<P><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">9.</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">再下一节中，准备对</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">CUDA</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">的线程模型做一个简单的实用讲解</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">;</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">欢迎再来</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">……hoho &gt;_&lt;!</SPAN></P>
<P><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">ps</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 宋体">：你的留言是对我的最大鼓励，</SPAN><SPAN lang=EN-US style="FONT-SIZE: 11pt; FONT-FAMILY: Arial">Open Heart Breavly fly!</SPAN></P></div>]]></description>
	    <author><![CDATA[pannsp]]></author>
	    <comments>http://pannsp.blog.163.com/blog/static/920232009916111354228</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://pannsp.blog.163.com/blog/static/920232009916111354228</guid>
    <pubDate>Fri, 16 Oct 2009 23:13:54 +0800</pubDate>
    <dcterms:modified>2009-10-16T23:13:54+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[(CUDA 编程10).CUDA cosnstant使用（一）------GPU的革命 （转）]]></title>	
    <link>http://pannsp.blog.163.com/blog/static/920232009916111149208</link>
    <description><![CDATA[<div><P style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 22.7pt; TEXT-ALIGN: center; mso-margin-top-alt: auto" align=center><FONT size=3><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-FAMILY: 新宋体">10. CUDA cosnstant</SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-FAMILY: 新宋体">使用（一）<SPAN lang=EN-US>------GPU</SPAN>的革命<SPAN lang=EN-US></SPAN></SPAN></B></FONT></P>
<H1 style="MARGIN: 5pt 0cm 0pt 36pt"><SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">书接上回</SPAN><SPAN lang=EN-US><A href="http://www.hpctech.com/2009/0818/207.html"><FONT color=#1f3a87><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN lang=EN-US>《9.CUDA shared mem</SPAN></SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN lang=EN-US>使用》</SPAN></SPAN></FONT></A></SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">讲了<SPAN lang=EN-US>shared memory</SPAN>的使用，最近有几个朋友都在问我<SPAN lang=EN-US>cosntant</SPAN>的使用的问题，这次首先先讲一下<SPAN lang=EN-US>cosntant</SPAN>的使用，下一章节才讲一下<SPAN lang=EN-US>cosntant</SPAN>的使用中性能的体现；<SPAN lang=EN-US></SPAN></SPAN></H1>
<H1 style="MARGIN: 5pt 0cm 0pt 36pt"><SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">下面是一个简单的代码<SPAN lang=EN-US>:</SPAN></SPAN></H1>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/********************************************************************</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">*<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>cosntant_test.cu</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">*<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>This is a example of the CUDA program.</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">*<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>author: zhao.kaiyong(at)gmail.com</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">*********************************************************************/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">#include</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="COLOR: #a31515">&lt;stdio.h&gt;</SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">#include</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="COLOR: #a31515">&lt;stdlib.h&gt;</SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">#include</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="COLOR: #a31515">&lt;cuda_runtime.h&gt;</SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">#include</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="COLOR: #a31515">&lt;cutil.h&gt;</SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: #a31515; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/************************************************************************/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/* Init CUDA<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>*/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/************************************************************************/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">#if</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">__DEVICE_EMULATION__</SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #020002; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes; mso-highlight: white">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">bool</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">InitCUDA</SPAN>(<SPAN style="COLOR: blue">void</SPAN>){<SPAN style="COLOR: blue">return</SPAN> <SPAN style="COLOR: blue">true</SPAN>;}</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">#else</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">bool</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">InitCUDA</SPAN>(<SPAN style="COLOR: blue">void</SPAN>)</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">{</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">count</SPAN> = 0;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">i</SPAN> = 0;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">cudaGetDeviceCount</SPAN>(&amp;<SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">count</SPAN>);</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">if</SPAN>(<SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">count</SPAN> == 0) {</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">fprintf</SPAN>(<SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">stderr</SPAN>, <SPAN style="COLOR: #a31515">"There is no device. "</SPAN>);</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">return</SPAN> <SPAN style="COLOR: blue">false</SPAN>;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>}</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">for</SPAN>(<SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">i</SPAN> = 0; <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">i</SPAN> &lt; <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">count</SPAN>; <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">i</SPAN>++) {</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">cudaDeviceProp</SPAN> <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">prop</SPAN>;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">if</SPAN>(<SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">cudaGetDeviceProperties</SPAN>(&amp;<SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">prop</SPAN>, <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">i</SPAN>) == <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">cudaSuccess</SPAN>) {</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">if</SPAN>(<SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">prop</SPAN>.<SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">major</SPAN> &gt;= 1) {</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">break</SPAN>;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>}</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>}</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">if</SPAN>(<SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">i</SPAN> == <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">count</SPAN>) {</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">fprintf</SPAN>(<SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">stderr</SPAN>, <SPAN style="COLOR: #a31515">"There is no device supporting CUDA. "</SPAN>);</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">return</SPAN> <SPAN style="COLOR: blue">false</SPAN>;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>}</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">cudaSetDevice</SPAN>(<SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">i</SPAN>);</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">printf</SPAN>(<SPAN style="COLOR: #a31515">"CUDA initialized. "</SPAN>);</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">return</SPAN> <SPAN style="COLOR: blue">true</SPAN>;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">}</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">#endif</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/************************************************************************/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/* Example<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>*/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/************************************************************************/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">__constant__</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="COLOR: blue">char</SPAN> <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">p_HelloCUDA</SPAN>[11];<SPAN style="COLOR: green">// = "Hello CUDA!";</SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">__constant__</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">t_HelloCUDA</SPAN>[11]={0,1,2,3,4,5,6,7,8,9,10};</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">__constant__</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">num</SPAN> = 11;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">__global__</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="COLOR: blue">static</SPAN> <SPAN style="COLOR: blue">void</SPAN> <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">HelloCUDA</SPAN>(<SPAN style="COLOR: blue">char</SPAN>* <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">result</SPAN>)</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">{</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">i</SPAN> = 0;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">for</SPAN>(<SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">i</SPAN> = 0; <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">i</SPAN> &lt; <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">num</SPAN>; <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">i</SPAN>++) {</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">result</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">i</SPAN>] = <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">p_HelloCUDA</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">i</SPAN>]+<SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">t_HelloCUDA</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">i</SPAN>];</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>}</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">}</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/************************************************************************/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/* HelloCUDA<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>*/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/************************************************************************/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">int</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">main</SPAN>(<SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">argc</SPAN>, <SPAN style="COLOR: blue">char</SPAN>* <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">argv</SPAN>[])</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">{</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">if</SPAN>(!<SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">InitCUDA</SPAN>()) {</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">return</SPAN> 0;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>}</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">char</SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">helloCUDA</SPAN>[] = <SPAN style="COLOR: #a31515">"Hdjik CUDA!"</SPAN>;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">char</SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>*<SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">device_result</SPAN><SPAN style="mso-tab-count: 1">&nbsp; </SPAN>= 0;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">char</SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">host_result</SPAN>[12]<SPAN style="mso-tab-count: 1"> </SPAN>={0};</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">CUDA_SAFE_CALL</SPAN>( <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">cudaMalloc</SPAN>((<SPAN style="COLOR: blue">void</SPAN>**) &amp;<SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">device_result</SPAN>, <SPAN style="COLOR: blue">sizeof</SPAN>(<SPAN style="COLOR: blue">char</SPAN>) * 11));</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">CUDA_SAFE_CALL</SPAN>( <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">cudaMemcpyToSymbol</SPAN>(<SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">p_HelloCUDA</SPAN>, <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">helloCUDA</SPAN>, <SPAN style="COLOR: blue">sizeof</SPAN>(<SPAN style="COLOR: blue">char</SPAN>)*11) );</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">unsigned</SPAN> <SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">timer</SPAN> = 0;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">CUT_SAFE_CALL</SPAN>( <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">cutCreateTimer</SPAN>( &amp;<SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">timer</SPAN>));</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">CUT_SAFE_CALL</SPAN>( <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">cutStartTimer</SPAN>( <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">timer</SPAN>));</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">HelloCUDA</SPAN>&lt;&lt;&lt;1, 1, 0&gt;&gt;&gt;(<SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">device_result</SPAN>);</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">CUT_CHECK_ERROR</SPAN>(<SPAN style="COLOR: #a31515">"Kernel execution failed "</SPAN>);</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">CUDA_SAFE_CALL</SPAN>( <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">cudaThreadSynchronize</SPAN>() );</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">CUT_SAFE_CALL</SPAN>( <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">cutStopTimer</SPAN>( <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">timer</SPAN>));</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">printf</SPAN>(<SPAN style="COLOR: #a31515">"Processing time: %f (ms) "</SPAN>, <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">cutGetTimerValue</SPAN>( <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">timer</SPAN>));</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">CUT_SAFE_CALL</SPAN>( <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">cutDeleteTimer</SPAN>( <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">timer</SPAN>));</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">CUDA_SAFE_CALL</SPAN>( <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">cudaMemcpy</SPAN>(&amp;<SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">host_result</SPAN>, <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">device_result</SPAN>, <SPAN style="COLOR: blue">sizeof</SPAN>(<SPAN style="COLOR: blue">char</SPAN>) * 11, <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">cudaMemcpyDeviceToHost</SPAN>));</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">printf</SPAN>(<SPAN style="COLOR: #a31515">"%s "</SPAN>, <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">host_result</SPAN>);</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">CUDA_SAFE_CALL</SPAN>( <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">cudaFree</SPAN>(<SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">device_result</SPAN>));</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">CUT_EXIT</SPAN>(<SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">argc</SPAN>, <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">argv</SPAN>);</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">return</SPAN> 0;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">}</SPAN></P>
<P style="MARGIN: 0cm 0cm 10pt"><SPAN lang=EN-US style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 10pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">这里写了两种使用<SPAN lang=EN-US>cosntant</SPAN>的方法：<SPAN lang=EN-US></SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1"><SPAN lang=EN-US style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes; mso-bidi-font-family: 新宋体"><SPAN style="mso-list: Ignore">1.<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">一种方法是直接在定义的时候初始化<SPAN lang=EN-US>constant</SPAN>：<SPAN lang=EN-US></SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none; mso-add-space: auto"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">__constant__</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">t_HelloCUDA</SPAN>[11]={0,1,2,3,4,5,6,7,8,9,10};</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none; mso-add-space: auto"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">__constant__</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">num</SPAN> = 11;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt"><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">在<SPAN lang=EN-US>kernel</SPAN>里面直接使用就可以了；<SPAN lang=EN-US></SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt"><SPAN lang=EN-US style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1"><SPAN lang=EN-US style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes; mso-bidi-font-family: 新宋体"><SPAN style="mso-list: Ignore">2.<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; LINE-HEIGHT: 115%; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">第二种方法是定义一个<SPAN lang=EN-US>cosntant</SPAN>数组，然后使用函数初始化它；<SPAN lang=EN-US></SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none; mso-add-space: auto"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">__constant__</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="COLOR: blue">char</SPAN> <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">p_HelloCUDA</SPAN>[11];<SPAN style="COLOR: green">// = "Hello CUDA!";</SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: #020002; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes; mso-highlight: white">CUDA_SAFE_CALL</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">( <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">cudaMemcpyToSymbol</SPAN>(<SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">p_HelloCUDA</SPAN>, <SPAN style="BACKGROUND: white; COLOR: #020002; mso-highlight: white">helloCUDA</SPAN>, <SPAN style="COLOR: blue">sizeof</SPAN>(<SPAN style="COLOR: blue">char</SPAN>)*11) );</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none; mso-add-space: auto"><SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">前面一句是定义<SPAN lang=EN-US>constant</SPAN>，后面一句是初始化这个常量，在<SPAN lang=EN-US>kernel</SPAN>里面使用的时候，就按照定义的方式使用就可以了；<SPAN lang=EN-US></SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none; mso-add-space: auto"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none; mso-add-space: auto"><SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">当然也有朋友想定义自己的结构体，当然是可以的，只是在初始化的时候，<SPAN lang=EN-US>copy</SPAN>相应的结构体就可以了。这个只是一个初步的使用方法，希望对家有用。<SPAN lang=EN-US></SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none; mso-add-space: auto"><SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">下一次会对<SPAN lang=EN-US>cosntant</SPAN>的使用中体现出来的性能优势做一个简单的分析。<SPAN lang=EN-US></SPAN></SPAN></P></div>]]></description>
	    <author><![CDATA[pannsp]]></author>
	    <comments>http://pannsp.blog.163.com/blog/static/920232009916111149208</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://pannsp.blog.163.com/blog/static/920232009916111149208</guid>
    <pubDate>Fri, 16 Oct 2009 23:11:49 +0800</pubDate>
    <dcterms:modified>2009-10-16T23:11:49+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[CUDA DLL 开发流程 （转）]]></title>	
    <link>http://pannsp.blog.163.com/blog/static/92023200991611936839</link>
    <description><![CDATA[<div><P style="TEXT-INDENT: 2em">CUDA的DLL开发其实和一般的C/C++的DLL开发是一个原理，当然，DLL的开发就有几种方式，这里就讲最容易理解的，也最直接的方式，然后把代码放出来。大家自己可以琢磨一下其它的方式。</P>
<P style="TEXT-INDENT: 2em">1.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 创建DLL</P>
<P style="TEXT-INDENT: 2em">用我的<A href="http://blog.csdn.net/OpenHero/archive/2009/03/10/3975962.aspx">Wizard</A> 可以创建一个DLL项目工程：</P>
<P style="TEXT-INDENT: 2em"><IMG title="CUDA DLL 开发流程 （转） - pannsp - Pan Ant" alt="CUDA DLL 开发流程 （转） - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0819/20090819121821568.png" border=0>选择DLL项目，然后可以看到下面的项目文件结构：</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em"><IMG title="CUDA DLL 开发流程 （转） - pannsp - Pan Ant" alt="CUDA DLL 开发流程 （转） - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0819/20090819121821105.png" border=0>其中sampe_cu.h文件为头文件，stdafx.h和stdafx.cpp是windows的加载的相关文件，这里可以不用理会，DLL_Test.cpp是其中的一个导出函数文件，也是声明了DkkMain的入口函数，这里是在DLL创建，加载，卸载的时候需要处理的一些内容，这里如果不熟悉DLL也不用管它。</P>
<P style="TEXT-INDENT: 2em">针对CUDA，这里我们就来看sample.cu文件：</P>
<P style="TEXT-INDENT: 2em">/************************************************************************/</P>
<P style="TEXT-INDENT: 2em">/* HelloCUDA&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */</P>
<P style="TEXT-INDENT: 2em">/************************************************************************/</P>
<P style="TEXT-INDENT: 2em">int TestHelloCUDA(void)</P>
<P style="TEXT-INDENT: 2em">{</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; if(!InitCUDA()) {</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return 0;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; }</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; char&nbsp;&nbsp;&nbsp; *device_result&nbsp; = 0;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; char&nbsp;&nbsp;&nbsp; host_result[12] ={0};</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; CUDA_SAFE_CALL( cudaMalloc((void**) device_result, sizeof(char) * 11));</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; unsigned int timer = 0;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; CUT_SAFE_CALL( cutCreateTimer( &amp;timer));</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; CUT_SAFE_CALL( cutStartTimer( timer));</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; HelloCUDA&lt;&lt;&lt;1, 1, 0&gt;&gt;&gt;(device_result, 11);</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; CUT_CHECK_ERROR("Kernel execution failed ");</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; CUDA_SAFE_CALL( cudaThreadSynchronize() );</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; CUT_SAFE_CALL( cutStopTimer( timer));</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; printf("Processing time: %f (ms) ", cutGetTimerValue( timer));</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; CUT_SAFE_CALL( cutDeleteTimer( timer));</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; CUDA_SAFE_CALL( cudaMemcpy(&amp;host_result, device_result, sizeof(char) * 11, cudaMemcpyDeviceToHost));</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; printf("%s ", host_result);</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; CUDA_SAFE_CALL( cudaFree(device_result));</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; //CUT_EXIT(argc, argv);</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; return 0;</P>
<P style="TEXT-INDENT: 2em">}</P>
<P style="TEXT-INDENT: 2em">这个就是sample.cu文件里面的实现的dll的接口函数，当然还有一个def文件，这个文件是用来做动态导出用的，看看def文件：</P>
<P style="TEXT-INDENT: 2em">LIBRARY "DLL_Test"</P>
<P style="TEXT-INDENT: 2em">EXPORTS</P>
<P style="TEXT-INDENT: 2em">TestHelloCUDA</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">这个是sample.def文件的内容，这个文件是为了能动态导出DLL里面的函数接口而用的。编译器通过这个文件把声明的TestHelloCUDA，接口导出。</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">编译，就可以在相应的目录里面得到一个xxxx.dll文件.</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">2.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 调用DLL</P>
<P style="TEXT-INDENT: 2em">这里就是测试DLL文件，当然我在同一个工程里面创建一个调用的测试工程：</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em"><IMG title="CUDA DLL 开发流程 （转） - pannsp - Pan Ant" alt="CUDA DLL 开发流程 （转） - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0819/20090819121821983.png" border=0>这里添加一个测试工程，当然你也可以再另外的地方创建一个测试工程，或者加载到自己的工程里面；</P>
<P style="TEXT-INDENT: 2em">选择一个win32工作作为测试例子；</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em"><IMG title="CUDA DLL 开发流程 （转） - pannsp - Pan Ant" alt="CUDA DLL 开发流程 （转） - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0819/20090819121821852.png" border=0>这个例子就是一个简单的测试项目，都选默认得就可以；</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em"><IMG title="CUDA DLL 开发流程 （转） - pannsp - Pan Ant" alt="CUDA DLL 开发流程 （转） - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0819/20090819121821361.png" border=0>这里默认的就可以了，其他的自己去查找意思J不能偷懒到什么都告诉你……</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em"><IMG title="CUDA DLL 开发流程 （转） - pannsp - Pan Ant" alt="CUDA DLL 开发流程 （转） - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0819/20090819121821720.png" border=0>这个时候就可以看到两个工程，一个是dll工程，一个是测试工程，然后在测试工程里面，我们采用动态加载DLL的方法来调用DLL；</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em"><IMG title="CUDA DLL 开发流程 （转） - pannsp - Pan Ant" alt="CUDA DLL 开发流程 （转） - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0819/20090819121822624.png" border=0>上面就是调用DLL的代码，这里为什么加__T()自己去找unicode的调用方法J</P>
<P style="TEXT-INDENT: 2em">里面的__T()里面的dll的文件名就是我们要加载的DLL，当然这里是路径，入下图，我们的dll和exe在同一个文件夹里面，如果是不同的文件夹，那就用相对路径来找就okJ</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em"><IMG title="CUDA DLL 开发流程 （转） - pannsp - Pan Ant" alt="CUDA DLL 开发流程 （转） - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0819/20090819121822107.png" border=0>然后运行例子，就可以得到下图结果:</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em"><IMG title="CUDA DLL 开发流程 （转） - pannsp - Pan Ant" alt="CUDA DLL 开发流程 （转） - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0819/20090819121822761.png" border=0>这里的DLL就ok了~~~</P></div>]]></description>
	    <author><![CDATA[pannsp]]></author>
	    <comments>http://pannsp.blog.163.com/blog/static/92023200991611936839</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://pannsp.blog.163.com/blog/static/92023200991611936839</guid>
    <pubDate>Fri, 16 Oct 2009 23:09:36 +0800</pubDate>
    <dcterms:modified>2009-10-16T23:09:36+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[(CUDA 编程9).CUDA shared memory使用------GPU的革命 (转)]]></title>	
    <link>http://pannsp.blog.163.com/blog/static/92023200991611740395</link>
    <description><![CDATA[<div><P style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 22.7pt; TEXT-ALIGN: center; mso-margin-top-alt: auto" align=center><FONT size=3><B style="mso-bidi-font-weight: normal"><SPAN lang=EN-US style="FONT-FAMILY: 新宋体">9. CUDA shared memory</SPAN></B><B style="mso-bidi-font-weight: normal"><SPAN style="FONT-FAMILY: 新宋体">使用<SPAN lang=EN-US>------GPU</SPAN>的革命<SPAN lang=EN-US></SPAN></SPAN></B></FONT></P>
<H1 style="MARGIN: 5pt 0cm 0pt 36pt"><SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-font-kerning: 0pt">书接上文</SPAN><SPAN style="FONT-SIZE: 11pt; BACKGROUND: #d9d9d9; COLOR: red; FONT-FAMILY: 新宋体; mso-font-kerning: 0pt; mso-shading: white; mso-pattern: gray-15 auto">《<SPAN lang=EN-US><A href="http://blog.csdn.net/OpenHero/archive/2008/12/15/3520578.aspx"><SPAN style="COLOR: red; TEXT-DECORATION: none; text-underline: none">8. CUDA </SPAN><SPAN lang=EN-US style="COLOR: red; TEXT-DECORATION: none; text-underline: none"><SPAN lang=EN-US>内存使用 global </SPAN></SPAN><SPAN lang=EN-US style="COLOR: red; TEXT-DECORATION: none; text-underline: none"><SPAN lang=EN-US>二------GPU</SPAN></SPAN><SPAN lang=EN-US style="COLOR: red; TEXT-DECORATION: none; text-underline: none"><SPAN lang=EN-US>的革命</SPAN></SPAN></A></SPAN>》</SPAN><SPAN style="FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-font-kerning: 0pt"> </SPAN><SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt">讲了<SPAN lang=EN-US>global</SPAN>内存访问的时候，需要对齐的问题，只有在对齐的情况下才能保证<SPAN lang=EN-US>global</SPAN>内存的高效访问。这一章节准备写一下<SPAN lang=EN-US>shared memory</SPAN>的访问的问题，首先是讲一下<SPAN lang=EN-US>shared</SPAN>的<SPAN lang=EN-US>memory</SPAN>的两种使用方法，然后讲解一下<SPAN lang=EN-US>shared memory</SPAN>的<SPAN lang=EN-US>bank conflict</SPAN>的问题，这个是<SPAN lang=EN-US>shared memory</SPAN>访问能否高效的问题所在；<SPAN lang=EN-US></SPAN></SPAN></H1>
<H1 style="MARGIN: 5pt 0cm 0pt 36pt"><SPAN lang=EN-US style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt">Shared memory</SPAN><SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt">的常规使用：<SPAN lang=EN-US></SPAN></SPAN></H1>
<H1 style="MARGIN: 5pt 0cm 0pt 54pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1"><SPAN lang=EN-US style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-bidi-font-family: 新宋体"><SPAN style="mso-list: Ignore">1.<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt">使用固定大小的数组：<SPAN lang=EN-US></SPAN></SPAN></H1>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/************************************************************************/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/* Example<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>*/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/************************************************************************/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">__global__</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="COLOR: blue">void</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">shared_memory_1</SPAN>(<SPAN style="COLOR: blue">float</SPAN>* <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">result</SPAN>, <SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">num</SPAN>, <SPAN style="COLOR: blue">float</SPAN>* <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">table_1</SPAN>)</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">{</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="COLOR: blue">__shared__</SPAN> <SPAN style="COLOR: blue">float</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">sh_da<wbr>ta</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">THREAD_SIZE</SPAN>];</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN> = <SPAN style="COLOR: blue">threadIdx</SPAN>.<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">x</SPAN>;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="COLOR: blue">float</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">ret</SPAN> = 0.0f;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">sh_da<wbr>ta</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN>] = <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">table_1</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN>];</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="COLOR: blue">for</SPAN> (<SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">i</SPAN> = 0; <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">i</SPAN> &lt; <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">num</SPAN>; <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">i</SPAN>++)</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">{</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">ret</SPAN> += <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">sh_da<wbr>ta</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN> %<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">BANK_CONFLICT</SPAN>];</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">}</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">result</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN>]<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>= <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">ret</SPAN>;</SPAN></P>
<H1 style="MARGIN: 5pt 0cm 0pt 36pt"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">}</SPAN><SPAN lang=EN-US style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt"></SPAN></H1>
<H1 style="MARGIN: 5pt 0cm 0pt 36pt"><SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt">这里的<SPAN lang=EN-US>sh_da<wbr>ta</SPAN>就是固定大小的数组；<SPAN lang=EN-US></SPAN></SPAN></H1>
<H1 style="MARGIN: 5pt 0cm 0pt 36pt"><SPAN lang=EN-US style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt"></SPAN></H1>
<H1 style="MARGIN: 5pt 0cm 0pt 36pt"><SPAN lang=EN-US style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt"></SPAN></H1>
<H1 style="MARGIN: 5pt 0cm 0pt 36pt"><SPAN lang=EN-US style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt"></SPAN></H1>
<H1 style="MARGIN: 5pt 0cm 0pt 54pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1"><SPAN lang=EN-US style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-bidi-font-family: 新宋体"><SPAN style="mso-list: Ignore"><BR>2.<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt">使用动态分配的数组：<SPAN lang=EN-US></SPAN></SPAN></H1>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">extern</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="COLOR: blue">__shared__</SPAN> <SPAN style="COLOR: blue">char</SPAN> <SPAN style="COLOR: blue">array</SPAN>[];</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">__global__</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="COLOR: blue">void</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">shared_memory_1</SPAN>(<SPAN style="COLOR: blue">float</SPAN>* <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">result</SPAN>, <SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">num</SPAN>, <SPAN style="COLOR: blue">float</SPAN>* <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">table_1</SPAN>, <SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">shared_size</SPAN>)</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">{</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="COLOR: blue">float</SPAN>* <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">sh_da<wbr>ta</SPAN> = (<SPAN style="COLOR: blue">float</SPAN>*)<SPAN style="COLOR: blue">array</SPAN>; <SPAN style="COLOR: green">// </SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">这里就让<SPAN lang=EN-US>sh_da<wbr>ta</SPAN>指向了<SPAN lang=EN-US>shared memory</SPAN>的第一个地址，就可以动态分配空间<SPAN lang=EN-US></SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><A></A><A><SPAN style="mso-bookmark: OLE_LINK2"><U><SPAN style="COLOR: blue">float</SPAN><FONT color=#1f3a87>* </FONT><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">sh_da<wbr>ta2</SPAN><FONT color=#1f3a87> = (</FONT><SPAN style="COLOR: blue">float</SPAN><FONT color=#1f3a87>*)&amp;</FONT><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">sh_da<wbr>ta</SPAN><FONT color=#1f3a87>[</FONT><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">shared_size</SPAN><FONT color=#1f3a87>]; </FONT></U></SPAN></A><SPAN style="COLOR: green">// </SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">这里的<SPAN lang=EN-US>shared_size</SPAN>的大小为<SPAN lang=EN-US>sh_da<wbr>ta</SPAN>的大小；<SPAN lang=EN-US></SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN> = <SPAN style="COLOR: blue">threadIdx</SPAN>.<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">x</SPAN>;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="COLOR: blue">float</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">ret</SPAN> = 0.0f;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">sh_da<wbr>ta</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN>] = <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">table_1</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN>];</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="COLOR: blue">for</SPAN> (<SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">i</SPAN> = 0; <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">i</SPAN> &lt; <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">num</SPAN>; <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">i</SPAN>++)</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">{</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">ret</SPAN> += <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">sh_da<wbr>ta</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN> %<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">BANK_CONFLICT</SPAN>];</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">}</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">result</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN>]<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>= <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">ret</SPAN>;</SPAN></P>
<H1 style="MARGIN: 5pt 0cm 0pt 36pt"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">}</SPAN></H1>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: Calibri; mso-no-proof: yes">这里是动态分配的空间，</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">extern</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="COLOR: blue">__shared__</SPAN> <SPAN style="COLOR: blue">char</SPAN> <SPAN style="COLOR: blue">array</SPAN>[];</SPAN><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">指定了<SPAN lang=EN-US>shared</SPAN>的第一个变量的地址，这里其实是指向<SPAN lang=EN-US>shared memory</SPAN>空间地址；后面的动态分配<SPAN lang=EN-US style="COLOR: blue">float</SPAN><SPAN lang=EN-US>* <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">sh_da<wbr>ta</SPAN> = (<SPAN style="COLOR: blue">float</SPAN>*)<SPAN style="COLOR: blue">array</SPAN>;</SPAN>让<SPAN lang=EN-US>sh_da<wbr>ta</SPAN>指向<SPAN lang=EN-US>array</SPAN>其实就是指向<SPAN lang=EN-US>shared memory</SPAN>上的第一个地址；<SPAN lang=EN-US></SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">后面的<SPAN lang=EN-US style="COLOR: blue">float</SPAN><SPAN lang=EN-US>* <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">sh_da<wbr>ta2</SPAN> = (<SPAN style="COLOR: blue">float</SPAN>*)&amp;<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">sh_da<wbr>ta</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">shared_size</SPAN>];</SPAN>这里的<SPAN lang=EN-US>sh_da<wbr>ta2</SPAN>是指向的第一个<SPAN lang=EN-US>sh_da<wbr>ta</SPAN>的<SPAN lang=EN-US>shared_size</SPAN>的地址，就是<SPAN lang=EN-US>sh_da<wbr>ta</SPAN>就是有了<SPAN lang=EN-US>shared_size</SPAN>的动态分配的空间；<SPAN lang=EN-US></SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">入下图：<SPAN lang=EN-US></SPAN></SPAN></P>
<H1 style="MARGIN: 5pt 0cm 0pt 36pt"><SPAN lang=EN-US><IMG title="(CUDA 编程9).CUDA shared memory使用------GPU的革命 (转) - pannsp - Pan Ant" alt="" src="http://www.hpctech.com/uploadfile/2009/0819/20090819121646861.gif"></SPAN><SPAN lang=EN-US style="mso-fareast-font-family: 宋体"></SPAN></H1>
<H1 style="MARGIN: 5pt 0cm 0pt 36pt"><SPAN lang=EN-US style="mso-fareast-font-family: 宋体"><FONT face="Times New Roman">&nbsp;</FONT></SPAN></H1>
<H1 style="MARGIN: 5pt 0cm 0pt 54pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1"><SPAN lang=EN-US style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-bidi-font-family: 新宋体"><SPAN style="mso-list: Ignore">3.<SPAN style="FONT: 7pt 'Times New Roman'">&nbsp;&nbsp;&nbsp; </SPAN></SPAN></SPAN><SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-hansi-font-family: 新宋体; mso-ascii-font-family: 新宋体">下面是讲解</SPAN><SPAN lang=EN-US style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-fareast-font-family: 宋体">bank conflict</SPAN></H1>
<H1 style="MARGIN: 5pt 0cm 0pt 54pt"><SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-hansi-font-family: 新宋体; mso-ascii-font-family: 新宋体">我们知道有每一个</SPAN><SPAN lang=EN-US style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-fareast-font-family: 宋体">half-warp</SPAN><SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-hansi-font-family: 新宋体; mso-ascii-font-family: 新宋体">是</SPAN><SPAN lang=EN-US style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-fareast-font-family: 宋体">16</SPAN><SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-hansi-font-family: 新宋体; mso-ascii-font-family: 新宋体">个</SPAN><SPAN lang=EN-US style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-fareast-font-family: 宋体">thread</SPAN><SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-hansi-font-family: 新宋体; mso-ascii-font-family: 新宋体">，然后</SPAN><SPAN lang=EN-US style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-fareast-font-family: 宋体">shared memory</SPAN><SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-hansi-font-family: 新宋体; mso-ascii-font-family: 新宋体">有</SPAN><SPAN lang=EN-US style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-fareast-font-family: 宋体">16</SPAN><SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-hansi-font-family: 新宋体; mso-ascii-font-family: 新宋体">个</SPAN><SPAN lang=EN-US style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-fareast-font-family: 宋体">bank</SPAN><SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-hansi-font-family: 新宋体; mso-ascii-font-family: 新宋体">，怎么分配这</SPAN><SPAN lang=EN-US style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-fareast-font-family: 宋体">16</SPAN><SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-hansi-font-family: 新宋体; mso-ascii-font-family: 新宋体">个</SPAN><SPAN lang=EN-US style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-fareast-font-family: 宋体">thread</SPAN><SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-hansi-font-family: 新宋体; mso-ascii-font-family: 新宋体">，分别到各自的</SPAN><SPAN lang=EN-US style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-fareast-font-family: 宋体">bank</SPAN><SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-hansi-font-family: 新宋体; mso-ascii-font-family: 新宋体">去取</SPAN><SPAN lang=EN-US style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-fareast-font-family: 宋体">shared memory</SPAN><SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-hansi-font-family: 新宋体; mso-ascii-font-family: 新宋体">，如果大家都到同一个</SPAN><SPAN lang=EN-US style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-fareast-font-family: 宋体">bank</SPAN><SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-hansi-font-family: 新宋体; mso-ascii-font-family: 新宋体">取款，就会排队，这就造成了</SPAN><SPAN lang=EN-US style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-fareast-font-family: 宋体">bank conflict</SPAN><SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-hansi-font-family: 新宋体; mso-ascii-font-family: 新宋体">，上面的代码可以用来验证一下</SPAN><SPAN lang=EN-US style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-fareast-font-family: 宋体">bank conflict</SPAN><SPAN style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-hansi-font-family: 新宋体; mso-ascii-font-family: 新宋体">对代码性能造成的影响：</SPAN><SPAN lang=EN-US style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt; mso-fareast-font-family: 宋体"></SPAN></H1>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/************************************************************************/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/* Example<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>*/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/************************************************************************/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">__global__</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="COLOR: blue">void</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">shared_memory_1</SPAN>(<SPAN style="COLOR: blue">float</SPAN>* <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">result</SPAN>, <SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">num</SPAN>, <SPAN style="COLOR: blue">float</SPAN>* <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">table_1</SPAN>)</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">{</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="COLOR: blue">__shared__</SPAN> <SPAN style="COLOR: blue">float</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">sh_da<wbr>ta</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">THREAD_SIZE</SPAN>];</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN> = <SPAN style="COLOR: blue">threadIdx</SPAN>.<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">x</SPAN>;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="COLOR: blue">float</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">ret</SPAN> = 0.0f;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">sh_da<wbr>ta</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN>] = <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">table_1</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN>];</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="COLOR: blue">for</SPAN> (<SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">i</SPAN> = 0; <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">i</SPAN> &lt; <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">num</SPAN>; <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">i</SPAN>++)</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">{</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">ret</SPAN> += <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">sh_da<wbr>ta</SPAN>[<A></A><A><SPAN style="mso-bookmark: OLE_LINK4"><U><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN><FONT color=#1f3a87> </FONT></U></SPAN></A>%<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">BANK_CONFLICT</SPAN>];</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">}</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt 36pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">result</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN>]<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>= <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">ret</SPAN>;</SPAN></P>
<H1 style="MARGIN: 5pt 0cm 0pt 36pt"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">}</SPAN><SPAN lang=EN-US style="FONT-WEIGHT: normal; FONT-SIZE: 11pt; FONT-FAMILY: 新宋体; mso-bidi-font-weight: bold; mso-font-kerning: 0pt"></SPAN></H1>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">// 1,2,3,4,5,6,7.....16</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">#define</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">BANK_CONFLICT</SPAN> 16</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">这里的<SPAN lang=EN-US style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">BANK_CONFLICT</SPAN><SPAN lang=EN-US style="COLOR: #010001"> </SPAN><SPAN style="COLOR: #010001">定义为从<SPAN lang=EN-US>1</SPAN>到<SPAN lang=EN-US>16</SPAN>的大小，可以自己修改，来看看<SPAN lang=EN-US>bank conflict</SPAN>对性能的影响；当<SPAN lang=EN-US style="BACKGROUND: white; mso-highlight: white">BANK_CONFLICT</SPAN>为<SPAN lang=EN-US>2</SPAN>的时候，就会通用有<SPAN lang=EN-US>8</SPAN>个<SPAN lang=EN-US>thread</SPAN>同时访问同一个<SPAN lang=EN-US>bank</SPAN>，因为<SPAN lang=EN-US>idx%2</SPAN>的取值只有<SPAN lang=EN-US>2</SPAN>个<SPAN lang=EN-US>0</SPAN>和<SPAN lang=EN-US>1</SPAN>，所以<SPAN lang=EN-US>16</SPAN>个都会访问<SPAN lang=EN-US>bank0</SPAN>和<SPAN lang=EN-US>bank1</SPAN>，以此类推，就可以测试整个的性能；<SPAN lang=EN-US></SPAN></SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: #010001; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: #010001; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">下面为示意图：<SPAN lang=EN-US></SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none">&nbsp;</P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US><FONT face=宋体 size=3><IMG title="(CUDA 编程9).CUDA shared memory使用------GPU的革命 (转) - pannsp - Pan Ant" alt="" src="http://www.hpctech.com/uploadfile/2009/0819/20090819121646395.gif">&nbsp;</FONT></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><FONT size=3><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: Calibri; mso-ascii-font-family: Calibri">当然我们还可以利用</SPAN><FONT face=宋体><SPAN lang=EN-US>16bank conflict</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: Calibri; mso-ascii-font-family: Calibri">，大家都访问同一个</SPAN><SPAN lang=EN-US>bank</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: Calibri; mso-ascii-font-family: Calibri">的同一个数据的时候，就可以形成一个</SPAN><SPAN lang=EN-US>broadcast</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: Calibri; mso-ascii-font-family: Calibri">，那样就会把数据同时广播给</SPAN><SPAN lang=EN-US>16</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: Calibri; mso-ascii-font-family: Calibri">个</SPAN><SPAN lang=EN-US>thread</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: Calibri; mso-ascii-font-family: Calibri">，这样就可以合理利用</SPAN><SPAN lang=EN-US>shared memory</SPAN><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: Calibri; mso-ascii-font-family: Calibri">的</SPAN><SPAN lang=EN-US>broadcast</SPAN></FONT><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: Calibri; mso-ascii-font-family: Calibri">的机会。</SPAN></FONT></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US><FONT face=宋体 size=3>&nbsp;</FONT></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-FAMILY: 宋体; mso-hansi-font-family: Calibri; mso-ascii-font-family: Calibri"><FONT size=3>下面贴出代码，最好自己测试一下；</FONT></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US><FONT face=宋体 size=3>&nbsp;</FONT></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/********************************************************************</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">*<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>shared_memory_test.cu</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">*<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>This is a example of the CUDA program.</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">*<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>Author: zhao.kaiyong(at)gmail.com</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">*<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>http://blog.csdn.net/openhero</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">*<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>http://www.comp.hkbu.edu.hk/~kyzhao/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">*********************************************************************/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">#include</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="COLOR: #a31515">&lt;stdio.h&gt;</SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">#include</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="COLOR: #a31515">&lt;stdlib.h&gt;</SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">#include</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="COLOR: #a31515">&lt;cutil.h&gt;</SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">#include</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="COLOR: #a31515">&lt;cutil_inline.h&gt;</SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: #a31515; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">// 1,2,3,4,5,6,7.....16</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">#define</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">BANK_CONFLICT</SPAN> 16</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">#define</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">THREAD_SIZE</SPAN> 16</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/************************************************************************/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/* static<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>*/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/************************************************************************/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">__global__</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="COLOR: blue">void</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">shared_memory_static</SPAN>(<SPAN style="COLOR: blue">float</SPAN>* <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">result</SPAN>, <SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">num</SPAN>, <SPAN style="COLOR: blue">float</SPAN>* <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">table_1</SPAN>)</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">{</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">__shared__</SPAN> <SPAN style="COLOR: blue">float</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">sh_da<wbr>ta</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">THREAD_SIZE</SPAN>];</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN> = <SPAN style="COLOR: blue">threadIdx</SPAN>.<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">x</SPAN>;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">float</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">ret</SPAN> = 0.0f;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">sh_da<wbr>ta</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN>] = <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">table_1</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN>];</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">for</SPAN> (<SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">i</SPAN> = 0; <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">i</SPAN> &lt; <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">num</SPAN>; <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">i</SPAN>++)</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>{</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">ret</SPAN> += <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">sh_da<wbr>ta</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN>%<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">BANK_CONFLICT</SPAN>];</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>}</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">result</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN>]<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>= <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">ret</SPAN>;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">}</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/************************************************************************/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/* dynamic<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>*/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/************************************************************************/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">extern</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="COLOR: blue">__shared__</SPAN> <SPAN style="COLOR: blue">char</SPAN> <SPAN style="COLOR: blue">array</SPAN>[];</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">__global__</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="COLOR: blue">void</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">shared_memory_dynamic</SPAN>(<SPAN style="COLOR: blue">float</SPAN>* <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">result</SPAN>, <SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">num</SPAN>, <SPAN style="COLOR: blue">float</SPAN>* <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">table_1</SPAN>, <SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">shared_size</SPAN>)</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">{</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">float</SPAN>* <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">sh_da<wbr>ta</SPAN> = (<SPAN style="COLOR: blue">float</SPAN>*)<SPAN style="COLOR: blue">array</SPAN>; <SPAN style="COLOR: green">// </SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">这里就让<SPAN lang=EN-US>sh_da<wbr>ta</SPAN>指向了<SPAN lang=EN-US>shared memory</SPAN>的第一个地址，就可以动态分配空间<SPAN lang=EN-US></SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">float</SPAN>* <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">sh_da<wbr>ta2</SPAN> = (<SPAN style="COLOR: blue">float</SPAN>*)&amp;<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">sh_da<wbr>ta</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">shared_size</SPAN>]; <SPAN style="COLOR: green">// </SPAN></SPAN><SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">这里的<SPAN lang=EN-US>shared_size</SPAN>的大小为<SPAN lang=EN-US>sh_da<wbr>ta</SPAN>的大小；<SPAN lang=EN-US></SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN> = <SPAN style="COLOR: blue">threadIdx</SPAN>.<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">x</SPAN>;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">float</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">ret</SPAN> = 0.0f;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">sh_da<wbr>ta</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN>] = <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">table_1</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN>];</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">for</SPAN> (<SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">i</SPAN> = 0; <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">i</SPAN> &lt; <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">num</SPAN>; <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">i</SPAN>++)</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>{</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">ret</SPAN> += <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">sh_da<wbr>ta</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN>%<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">BANK_CONFLICT</SPAN>];</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>}</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">result</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN>]<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>= <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">ret</SPAN>;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">}</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/************************************************************************/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/* Bank conflict<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>*/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/************************************************************************/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">__global__</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="COLOR: blue">void</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">shared_memory_bankconflict</SPAN>(<SPAN style="COLOR: blue">float</SPAN>* <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">result</SPAN>, <SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">num</SPAN>, <SPAN style="COLOR: blue">float</SPAN>* <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">table_1</SPAN>)</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">{</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">__shared__</SPAN> <SPAN style="COLOR: blue">float</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">sh_da<wbr>ta</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">THREAD_SIZE</SPAN>];</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN> = <SPAN style="COLOR: blue">threadIdx</SPAN>.<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">x</SPAN>;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">float</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">ret</SPAN> = 0.0f;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">sh_da<wbr>ta</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN>] = <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">table_1</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN>];</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">for</SPAN> (<SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">i</SPAN> = 0; <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">i</SPAN> &lt; <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">num</SPAN>; <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">i</SPAN>++)</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>{</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">ret</SPAN> += <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">sh_da<wbr>ta</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN> % <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">BANK_CONFLICT</SPAN>];</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>}</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">result</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">idx</SPAN>]<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>= <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">ret</SPAN>;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">}</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/************************************************************************/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/* HelloCUDA<SPAN style="mso-spacerun: yes">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN>*/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">/************************************************************************/</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">int</SPAN><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">main</SPAN>(<SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">argc</SPAN>, <SPAN style="COLOR: blue">char</SPAN>* <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">argv</SPAN>[])</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">{</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">if</SPAN> ( <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">cutCheckCmdLineFlag</SPAN>(<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">argc</SPAN>, (<SPAN style="COLOR: blue">const</SPAN> <SPAN style="COLOR: blue">char</SPAN>**) <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">argv</SPAN>, <SPAN style="COLOR: #a31515">"device"</SPAN>))</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>{</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">cutilDeviceInit</SPAN>(<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">argc</SPAN>, <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">argv</SPAN>);</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>}<SPAN style="COLOR: blue">else</SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>{</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">id</SPAN> = <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">cutGetMaxGflopsDeviceId</SPAN>();</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">cudaSetDevice</SPAN>(<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">id</SPAN>);</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>}</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">float</SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp; </SPAN>*<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">device_result</SPAN><SPAN style="mso-tab-count: 1">&nbsp; </SPAN>= <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">NULL</SPAN>;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">float</SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">host_result</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">THREAD_SIZE</SPAN>]<SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>={0};</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">CUDA_SAFE_CALL</SPAN>( <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">cudaMalloc</SPAN>((<SPAN style="COLOR: blue">void</SPAN>**) &amp;<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">device_result</SPAN>, <SPAN style="COLOR: blue">sizeof</SPAN>(<SPAN style="COLOR: blue">float</SPAN>) * <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">THREAD_SIZE</SPAN>));</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">float</SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp; </SPAN>*<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">device_table_1</SPAN> = <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">NULL</SPAN>;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">float</SPAN><SPAN style="mso-tab-count: 1">&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">host_table1</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">THREAD_SIZE</SPAN>] = {0};</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">for</SPAN> (<SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">i</SPAN> = 0; <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">i</SPAN> &lt; <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">THREAD_SIZE</SPAN>; <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">i</SPAN>++ )</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>{</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">host_table1</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">i</SPAN>] = <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">rand</SPAN>()%<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">RAND_MAX</SPAN>;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>}</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">CUDA_SAFE_CALL</SPAN>( <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">cudaMalloc</SPAN>((<SPAN style="COLOR: blue">void</SPAN>**) &amp;<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">device_table_1</SPAN>, <SPAN style="COLOR: blue">sizeof</SPAN>(<SPAN style="COLOR: blue">float</SPAN>) * <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">THREAD_SIZE</SPAN>));</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">CUDA_SAFE_CALL</SPAN>( <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">cudaMemcpy</SPAN>(<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">device_table_1</SPAN>, <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">host_table1</SPAN>, <SPAN style="COLOR: blue">sizeof</SPAN>(<SPAN style="COLOR: blue">float</SPAN>) * <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">THREAD_SIZE</SPAN>, <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">cudaMemcpyHostToDevice</SPAN>));</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">unsigned</SPAN> <SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">timer</SPAN> = 0;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">CUT_SAFE_CALL</SPAN>( <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">cutCreateTimer</SPAN>( &amp;<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">timer</SPAN>));</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">CUT_SAFE_CALL</SPAN>( <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">cutStartTimer</SPAN>( <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">timer</SPAN>));</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">shared_memory_static</SPAN>&lt;&lt;&lt;1, <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">THREAD_SIZE</SPAN>&gt;&gt;&gt;(<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">device_result</SPAN>, 1000, <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">device_table_1</SPAN>);</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: green">//shared_memory_dynamic&lt;&lt;&lt;1, THREAD_SIZE&gt;&gt;&gt;(device_result, 1000, device_table_1, 16);</SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: green">//shared_memory_bankconflict&lt;&lt;&lt;1, THREAD_SIZE&gt;&gt;&gt;(device_result, 1000, device_table_1);</SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">CUT_CHECK_ERROR</SPAN>(<SPAN style="COLOR: #a31515">"Kernel execution failed "</SPAN>);</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">CUDA_SAFE_CALL</SPAN>( <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">cudaMemcpy</SPAN>(<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">host_result</SPAN>, <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">device_result</SPAN>, <SPAN style="COLOR: blue">sizeof</SPAN>(<SPAN style="COLOR: blue">float</SPAN>) * <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">THREAD_SIZE</SPAN>, <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">cudaMemcpyDeviceToHost</SPAN>));</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">CUT_SAFE_CALL</SPAN>( <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">cutStopTimer</SPAN>( <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">timer</SPAN>));</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">printf</SPAN>(<SPAN style="COLOR: #a31515">"Processing time: %f (ms) "</SPAN>, <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">cutGetTimerValue</SPAN>( <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">timer</SPAN>));</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">CUT_SAFE_CALL</SPAN>( <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">cutDeleteTimer</SPAN>( <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">timer</SPAN>));</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="COLOR: blue">for</SPAN> (<SPAN style="COLOR: blue">int</SPAN> <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">i</SPAN> = 0; <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">i</SPAN> &lt; <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">THREAD_SIZE</SPAN>; <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">i</SPAN>++)</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>{</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">printf</SPAN>(<SPAN style="COLOR: #a31515">"%f "</SPAN>, <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">host_result</SPAN>[<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">i</SPAN>]);</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN>}</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">CUDA_SAFE_CALL</SPAN>( <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">cudaFree</SPAN>(<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">device_result</SPAN>));</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">CUDA_SAFE_CALL</SPAN>( <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">cudaFree</SPAN>(<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">device_table_1</SPAN>));</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes"><SPAN style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp; </SPAN><SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">cutilExit</SPAN>(<SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">argc</SPAN>, <SPAN style="BACKGROUND: white; COLOR: #010001; mso-highlight: white">argv</SPAN>);</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">}</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">这里只是一个简单的<SPAN lang=EN-US>demo</SPAN>，大家可以测试一下。下一章节会将一些<SPAN lang=EN-US>shared memory</SPAN>的更多的特性，更深入的讲解<SPAN lang=EN-US>shared memory</SPAN>的一些隐藏的性质；<SPAN lang=EN-US></SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">再在接下来的章节会讲一些<SPAN lang=EN-US>constant</SPAN>和<SPAN lang=EN-US>texture</SPAN>的使用；<SPAN lang=EN-US></SPAN></SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN lang=EN-US style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">&nbsp;</SPAN></P>
<P style="MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: normal; mso-layout-grid-align: none"><SPAN style="FONT-SIZE: 10pt; COLOR: green; FONT-FAMILY: 新宋体; mso-hansi-font-family: 'Times New Roman'; mso-no-proof: yes">写的内容一直都是文字比较多，代码比较少，其实学习的过程更重要的思想，实践的代码，最好是自己写，唯一可以学习的是思想，学习更重要的也是思想的交流，知识的传播，最好的是思想的传播，代码，方法，都是只是一些工具而已。但是工具的熟练层度，就得靠自己下来多练习。</SPAN></P></div>]]></description>
	    <author><![CDATA[pannsp]]></author>
	    <comments>http://pannsp.blog.163.com/blog/static/92023200991611740395</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://pannsp.blog.163.com/blog/static/92023200991611740395</guid>
    <pubDate>Fri, 16 Oct 2009 23:07:40 +0800</pubDate>
    <dcterms:modified>2009-10-16T23:07:40+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[(CUDA 编程8).CUDA 内存使用 global 二------GPU的革命 （转）]]></title>	
    <link>http://pannsp.blog.163.com/blog/static/9202320099161152681</link>
    <description><![CDATA[<div><P style="TEXT-INDENT: 2em">8. CUDA 内存使用 global 二------GPU的革命</P>
<P style="TEXT-INDENT: 2em">前面一章节已经写到了内存访问的问题，内存对齐的问题，不过在看到编程手册第五章的时候，还是会有很多朋友问到我关于CUDA的global内存访问的问题，怎么是访问的冲突，怎样才能更好的访问内存，达到更高的速度。下面先看几张图，这些图都是CUDA编程手册上的图，然后分别对这些图做解释，来理解硬件1.0，1.1 以及现在最新的硬件的访问内存的区别。</P>
<P style="TEXT-INDENT: 2em">我们在这里再深入的讲解一下global内存对齐的问题，每次执行一条明命令的时候，都是会按照32个thread为一个warp，一起来执行，但是在执行的时候，又会按照硬件的条件（这里有两个限制条件，一个是内存访问的时钟和执行core的时钟不一样，第二个是为了细粒度的分支的问题）然后就会把16个thread组成的half-warp来一次访问global内存才能让访问内存的性能高一些，这个可以理解；</P>
<P style="TEXT-INDENT: 2em">就像手册上说的那样，如果16个thread（half-warp）访问内存的时候，如果每一个thread访问32bits就是4个字节，那么就可以合并为一个64bytes的访问，手册上这点写得有点让人咋一看不太明白~4bytes（32bits）*16 = 64bytes，就是这么来的，如果每一个thread访问64bits（8个bytes），那么就可以合并为128bytes的访问；这里啦，由于合并访问的最大限制是128bytes，所以最大也按照128bytes一次访问来合并，如果超过，就得多次访问，或者如果没有按照这样的方式对齐访问，也会多次访问；下面是1.2device之前的访问的几个图，这里要把1.2device以前和以后的分开，是因为这里在对齐访问的方式的时候，有不同的策略；先看1.2device以前的能合并为一次访问的情况：</P>
<P style="TEXT-INDENT: 2em">下图是编程手册上的图：</P>
<P style="TEXT-INDENT: 2em"><IMG title="(CUDA 编程8).CUDA 内存使用 global 二------GPU的革命 （转） - pannsp - Pan Ant" alt="(CUDA 编程8).CUDA 内存使用 global 二------GPU的革命 （转） - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0819/20090819121312719.jpg" border=0>这里的每一个thread都是访问的对应的地址，是对齐的，所以可以合并为一个存储event；</P>
<P style="TEXT-INDENT: 2em">下面这个图是没有对齐访问，就造成了non-coalesced访问的问题，下面可以看图说话：</P>
<P style="TEXT-INDENT: 2em"><IMG title="(CUDA 编程8).CUDA 内存使用 global 二------GPU的革命 （转） - pannsp - Pan Ant" alt="(CUDA 编程8).CUDA 内存使用 global 二------GPU的革命 （转） - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0819/20090819121312797.jpg" border=0>左边的那个好理解，中间对应的thread访问的地址交叉了，thread3和thread4交叉访问了，在硬件1.2版本之前的都会造成Non-Coalesced访问；</P>
<P style="TEXT-INDENT: 2em">详细的需要说明的是右边的为什么也造成了Non-Coalesced（非对齐）访问，这个是基础问题，大家理解的内存对齐是怎么样的？按照固定思路，或者教材上强调的都是中间过程的内存访问的对齐，但是内存是从offset 0x00000000位置开始的，就是偏移量0开始的，如果要真的满足内存对齐，严格的说起来就需要从内存的0地址开始算起，再加上我们知道的global内存的对齐方式有几种，4位bytes，8bytes，16bytes，这里说的对齐方式，注意区别关系；再来看看右边的那个图：thread0开始，从address128的位置开始向下便宜的位置是？132-128=4 偏移了4，16个threads整体访问的是16*4=64，是从132开始的，从0算起来，132-0 =132； 132/16 = 8…4,从整体上讲，从0偏移位置开始，偏移了4个位置，这里的就造成了访问的未对齐，这个是从整体角度上讲的，和左边的图比较一下，那个是按照局部对齐来说的，注意理解；</P>
<P style="TEXT-INDENT: 2em">继续看图说话：</P>
<P style="TEXT-INDENT: 2em"><IMG title="(CUDA 编程8).CUDA 内存使用 global 二------GPU的革命 （转） - pannsp - Pan Ant" alt="(CUDA 编程8).CUDA 内存使用 global 二------GPU的革命 （转） - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0819/20090819121312813.jpg" border=0>左边的图看看，算一下，局部的时候偏移了，从局部和整体来说，都会引起未对齐访问；</P>
<P style="TEXT-INDENT: 2em">右边的图自己算一下，是不是超出了刚才我说的范围；所以造成了内存访问的未对齐情况；</P>
<P style="TEXT-INDENT: 2em">前面我们看的图都是1.2版本前的硬件的情况下的内存访问情况，现在看看1.2版本以后的硬件；</P>
<P style="TEXT-INDENT: 2em">这里解释一下，什么叫1.2版本的硬件，或许有些朋友也不太了解，g80架构的都是1.0或者1.1的硬件架构，现在的gtx200系列的都是1.3的架构，其实1.2的硬件架构，或许是Nvidia的一个内部的，没有推出产品，可能准备提供给低端的产品，但是我想没有推出低端的产品，直接就上1.3device了，市场需求吧~~如果下一步GTX的架构还是按照老路子，不改进的话，或许Intel的Lrb上来以后，对Nvidia的产品，就是一个很大的竞争了；</P>
<P style="TEXT-INDENT: 2em">不说废话了，先看图：</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em"><IMG title="(CUDA 编程8).CUDA 内存使用 global 二------GPU的革命 （转） - pannsp - Pan Ant" alt="(CUDA 编程8).CUDA 内存使用 global 二------GPU的革命 （转） - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0819/20090819121312810.jpg" border=0>1.2以后的硬件版本，弱化了threads之间交叉访问的时候，没对齐的情况，只要大家都在一次访问的64bytes的一个段里面，或者128bytes的一个段里面面，这样的段访问，那就可以不用多次访问，当然如果你16个threads分别跨过了16个段，那就得产生16个存储event~记住几个段的定义，这里说的段，就是我们常常理解的对齐的方式，全局的内存访问对齐方式，8个bits的是按照32bytes对齐，16bits的是按照64bytes对齐，32bits和64bits都是按照128bytes对齐；</P>
<P style="TEXT-INDENT: 2em">在优化代码的时候，这个地方是一个值得注意的部分；</P>
<P style="TEXT-INDENT: 2em">API函数里面有对齐访问的接口，会按照对齐的方式分配global内存给你，不过注意其中的一个offset值的使用，这个是为了解决全局情况下的对齐偏移的问题：）cudaMallocPitch，这个函数，注意使用~</P>
<P style="TEXT-INDENT: 2em"></P></div>]]></description>
	    <author><![CDATA[pannsp]]></author>
	    <comments>http://pannsp.blog.163.com/blog/static/9202320099161152681</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://pannsp.blog.163.com/blog/static/9202320099161152681</guid>
    <pubDate>Fri, 16 Oct 2009 23:05:26 +0800</pubDate>
    <dcterms:modified>2009-10-16T23:05:26+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[(CUDA 编程7).CUDA内存访问（一）提高篇------按部就班 (转)]]></title>	
    <link>http://pannsp.blog.163.com/blog/static/9202320099161147799</link>
    <description><![CDATA[<div><P style="TEXT-INDENT: 2em">(CUDA 编程7).CUDA内存访问（一）提高篇------按部就班</P>
<P style="TEXT-INDENT: 2em">------GPU的革命</P>
<P style="TEXT-INDENT: 2em">谈到内存的访问，其实也就是几个API函数的调用，感觉也没什么好讲的，知道cudaMalloc，知道cudaMemcpy，还有cudaFree，就应该可以对device上的内存进行分配，然后就可以在device上使用内存。但是就像我们看运动会一样，只看100米的那不到10秒的时间，也许你只会说，他跑得真快。又有多少人能清楚其过程中的磨炼啦？要真正理解device上内存的调度访问，让我们的程序达到更快的速度，就得对内存访问的情况做更深入的了解，明白其中的过程。</P>
<P style="TEXT-INDENT: 2em">我记得大学刚开始上编程语言的课程的时候，都会讲到内存的分配和内存的释放，但是一般都是在书的最后面才会讲内存的对齐，内存的空间布局。就像C++一样，或许你学习了好几年，用了它好多年，但是你是否清楚class的内存布局？知道虚表是怎么一个内存访问过程？就像做网络，当做到一定地步的时候，才会发现数据从一段传递到另一段的时候数据发生了变化，内容不对了，定义的结构体传递到另一边的时候就错位了。当在使用SSE对数据处理加速的时候，是否为内存对齐的问题头疼？说了这么多，或许有人在烦了~~~讲正题吧，这些部分或许只有自己真正在实际运用中遇到问题，才会去考虑的，不过还是希望大家能在遇到问题之前，就能掌握解决问题的能力。不是在遇到美女的时候才开始刮胡子，平时的习惯就要养好。</P>
<P style="TEXT-INDENT: 2em">前面这一段话或许只是生活的一些经历，其实可以跳过，当遇到的时候再回来看看：）我们还是按部就班的来讲解device上内存的访问吧。这么多年的发展，内存的价格是越来越便宜，但是有谁知道当你做编织内存的时候，很多人的眼睛都被弄瞎过啦，为她们当年为计算机作出的贡献bless一下。</P>
<P style="TEXT-INDENT: 2em">还记得小时候经常看枪战片，看到小马哥端着枪一个劲的扫射，哪个帅啊~~不过后来稍微大一些以后，心理面一直有一个疑问，子弹用不完？一次能装多少子弹啊~就那么小的弹夹。8发子弹的左轮手枪在帅哥哥手里可以搞定十几个人~还不装弹- 囧！一般的自动手枪一般都是8发，14发，最多的驳壳枪（毛瑟枪）也才能装20发子弹。你要说人家是《第一滴血4》里面的史泰龙可以开着战车上面的M2HB12.7mm重机枪，子弹不用弹夹，一次可以装几千发的弹；对，人家是DMA直接内存访问，不是通过弹夹访问的，不需要处理器来中转数据。G80支持的内存访问的能力是一次访问4bytes，8byets或者16bytes，G80有三种弹夹，一种可以一次装4发子弹，一种是8发子弹，还有16发的。</P>
<P style="TEXT-INDENT: 2em">Global Memory 在访问过程中没有Cache，就像以前的火枪一样，打一枪以后，装药，然后才能打下一枪。每次的访问时间是400-600个clock（core 跑的时钟）延迟。所以在CUDA编程中，其中一个瓶颈就是内存访问。利用SDK提供的bandwidthTest可以测试到host到device，device到host，device到device的传输性能。虽然PCIE有3.2G/s的理论值，但实际达不到这么多。Device to Device的传输能达到89G/s（GTX260）左右，理论值是90G/s（GTX260）也达到差不多了。这个地方每个人显卡不一样，主板不一样，设置环境不同，也不一定一样。</P>
<P style="TEXT-INDENT: 2em">Device上active的一个warp有32个线程，但是实际是16个thread同时在运行，就是half warp。当half warp的16个threads访问内存的时候，最好让16个thread依次对着相对应的内存地址，这样的时候才能保证Coalesced Access。如下图<A href="http://www.isi.edu/~ddavis/GPU/Course/Slides/GPU+CUDA.pdf">http://www.isi.edu/~ddavis/GPU/Course/Slides/GPU+CUDA.pdf</A>:</P>
<P style="TEXT-INDENT: 2em"><IMG title="(CUDA 编程7).CUDA内存访问（一）提高篇------按部就班 (转) - pannsp - Pan Ant" alt="(CUDA 编程7).CUDA内存访问（一）提高篇------按部就班 (转) - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0819/20090819121158671.jpg" border=0>如果还没明白，看解释，每一个thread一次访问对应的内存地址，不互相交错，这样就不会卡壳了~。如果有交错了，就会出现下面的情况 Uncoalesced Access，</P>
<P style="TEXT-INDENT: 2em"><IMG title="(CUDA 编程7).CUDA内存访问（一）提高篇------按部就班 (转) - pannsp - Pan Ant" alt="(CUDA 编程7).CUDA内存访问（一）提高篇------按部就班 (转) - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0819/20090819121158923.jpg" border=0>小时候玩过仿照左轮手枪的，自动的转动肯定会很快把子弹打完，没看到谁打完第1颗，再去打第3颗，再回来打第2颗子弹的~……这里也不多做解释了~</P>
<P style="TEXT-INDENT: 2em">下面是一个例子，也是刚才那个连接上的：</P>
<P style="TEXT-INDENT: 2em">把一个Uncoalesced float3 Co<wbr>de的代码优化为Coalesced的代码的过程：</P>
<P style="TEXT-INDENT: 2em"><IMG title="(CUDA 编程7).CUDA内存访问（一）提高篇------按部就班 (转) - pannsp - Pan Ant" alt="(CUDA 编程7).CUDA内存访问（一）提高篇------按部就班 (转) - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0819/20090819121158172.jpg" border=0>Float3是12bytes，每一个thread就会读取3个float。还记得我们前面说过的每次换一个弹夹是4，8，或者16，但是明显float3不等于这些。还记得active thread是怎么工作的吧？如果不记得了最好参看前面的thread工作的章节。Warp工作的时候是16个threads一起运作的，所以同时就有16个threads访问内存，加起来就是16*3*4（16个threds，float3是3个float，每个float有4个byte）, 192个bytes。所以就造成了Uncoalesced Access。</P>
<P style="TEXT-INDENT: 2em">下面怎么来解决这个问题，一个图：</P>
<P style="TEXT-INDENT: 2em"><IMG title="(CUDA 编程7).CUDA内存访问（一）提高篇------按部就班 (转) - pannsp - Pan Ant" alt="(CUDA 编程7).CUDA内存访问（一）提高篇------按部就班 (转) - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0819/20090819121158895.jpg" border=0>看看这个图，然后再看一段代码：</P>
<P style="TEXT-INDENT: 2em"><IMG title="(CUDA 编程7).CUDA内存访问（一）提高篇------按部就班 (转) - pannsp - Pan Ant" alt="(CUDA 编程7).CUDA内存访问（一）提高篇------按部就班 (转) - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0819/20090819121158537.jpg" border=0>自己先算一下，这里是怎么一个访问过程。</P>
<P style="TEXT-INDENT: 2em">下面解释：我们设置block的thread为256，然后当大家都执行第一条存储命令s_da<wbr>ta[threadIdx.x] = g_in[index]，还记得block内的thread的执行模型吗？SIMP，一条指令，同时256个threads都要执行完，才会执行下一个指令。</P>
<P style="TEXT-INDENT: 2em">这里其实用到了shared memory来作为中转，避免了global内存访问的Uncoalesced。如果结构体不是size（4，8，16）还有内存强制对齐的方式来使得global访问的coalesced。例如用__align(x)来强制对齐内存，但是这里就会浪费一些空间，如果float3，的结构体，用__align(16)，对齐，就会有一个float的空白空间用来做对齐了~。</P>
<P style="TEXT-INDENT: 2em">总结一下，就讲了一个global内存访问对齐的东东，如果访问不能连续，就采用两种方法来使得其对齐~其实看完前面的，理解到最后就这一句管用~</P>
<P style="TEXT-INDENT: 2em">下一章节应该会讲到shared memory里面访问的bank conflict的部分，其实也很简单，画一个图就出来解决了~~</P>
<P style="TEXT-INDENT: 2em">Ps：多画图，不明白的时候就画图出来：）</P>
<P style="TEXT-INDENT: 2em"></P></div>]]></description>
	    <author><![CDATA[pannsp]]></author>
	    <comments>http://pannsp.blog.163.com/blog/static/9202320099161147799</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://pannsp.blog.163.com/blog/static/9202320099161147799</guid>
    <pubDate>Fri, 16 Oct 2009 23:04:07 +0800</pubDate>
    <dcterms:modified>2009-10-16T23:04:07+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[(CUDA 编程6).CUDA编程接口（二）------一十八般武器 （转）]]></title>	
    <link>http://pannsp.blog.163.com/blog/static/920232009916105718317</link>
    <description><![CDATA[<div><P style="TEXT-INDENT: 2em">(CUDA 编程6)CUDA编程接口（二）------一十八般武器</P>
<P style="TEXT-INDENT: 2em">------GPU的革命</P>
<P style="TEXT-INDENT: 2em">4.&nbsp;&nbsp;&nbsp;&nbsp; 程序运行控制：像Stream，Event，Context, Module, Execution control这样的咱都把归类到运行管理的。这里也得分清楚有Runtime级别的，也有Driver级别的。</P>
<P style="TEXT-INDENT: 2em">Stream：如果了解AGP时代的显卡，就知道在Device和Host之间进行数据交换的时候有作为中转数据的部分，叫着stream；发展到G8X以后就有了新的专门针对GPGPU统一设计的方便数据传输的Stream out的硬件设备。这个层的功能是将Vertex Shader和Pixel Shader（在G8X的设计中已经差不多不用把他们俩再分开，差不多统一起来）处理完成的数据输出给用户，由用户进行处理后再反馈给流水线继续处理。它可以直接读写本地显存。如果是内存对齐的，速度会更快，这个部分可以参考simpleStream的代码。</P>
<P style="TEXT-INDENT: 2em">Event：和平时的大家在编程中使用的Event一个道理，都是起到通知的作用，创建一个事件，然后同步；Event在多线程编程中会经常遇到。</P>
<P style="TEXT-INDENT: 2em">Context：上下文，什么叫叫上下文- -！（中文翻译叫上下文，实在的，俺觉得这个翻译不恰当，但是很多传统的书都这么翻译了，咱们也只能叫上下文）这里的Context和CPU里面使用的Context差不多，都是一个process里面需要用到的一些“资源”（系统资源：堆栈，内存……等等）。我倒觉得翻译为process包含的东东更为恰当。</P>
<P style="TEXT-INDENT: 2em">Module：可以理解为linux里面的module意思；如果你不了解linux也不用担心，module就是一个个的专门针对Device的程序。知道很久很久以前的dos时代就有的.com文件吗？那个东东就像直接在CPU上可以调用的一个文件，加载到内存中就可以运行的。这里的module就是加载到Device上就可以运行。</P>
<P style="TEXT-INDENT: 2em">Execution control：这个就是从驱动层面来控制线程怎么在Device上运行。</P>
<P style="TEXT-INDENT: 2em">Stream和Event在Runtime API和Driver API中都有，函数接口也差不多。</P>
<P style="TEXT-INDENT: 2em">Context, Module, Execution control就只有在Driver API层面才有。</P>
<P style="TEXT-INDENT: 2em">上面的这些都没多少难点，其实API的讲解，一般都没多少难点，难点在于怎么能灵活的运用API，那就得通过多次的练习，多用API实践；其实有的时候也能发现API的不足，才会产生新的API。这里就不放具体的代码，代码可以参考编程手册上面。</P>
<P style="TEXT-INDENT: 2em">5.&nbsp;&nbsp;&nbsp;&nbsp; 好了，这里就剩下OpenGL和Direct3D的接口函数了，他也有两个层次的API，有Runtime层次的，也有Driver层次的。Runtime和Driver层面都有调用的API，CUDA2.0在这里也做了一些优化，在内存交换方面。具体代码参考编程手册。</P>
<P style="TEXT-INDENT: 2em">前面说过要讲解函数的定义，其实在以前的帖子里面正好已经翻译过一段，不过现在要讲解，也就提出来，专门再把那些东西翻出来：<A href="http://blog.csdn.net/OpenHero/archive/2007/11/17/1889389.aspx">GPU NVIDIA_CUDA_Programming_Cuide_1.0 Chapter 4. Application Programming Interface</A></P>
<P style="TEXT-INDENT: 2em">4.2.1 Function Type Qualifiers 函数类型</P>
<P style="TEXT-INDENT: 2em">4.2.1.1 __device__</P>
<P style="TEXT-INDENT: 2em">__device__ 规定的函数：</P>
<P style="TEXT-INDENT: 2em">在device上执行。</P>
<P style="TEXT-INDENT: 2em">只能在device上调用。</P>
<P style="TEXT-INDENT: 2em">4.2.1.2 __global__</P>
<P style="TEXT-INDENT: 2em">__global__ 定义了一个kernel函数：</P>
<P style="TEXT-INDENT: 2em">在device上运行。</P>
<P style="TEXT-INDENT: 2em">只能在host上调用</P>
<P style="TEXT-INDENT: 2em">4.2.1.3 __host__</P>
<P style="TEXT-INDENT: 2em">__host__定义的函数：</P>
<P style="TEXT-INDENT: 2em">在host上运行，</P>
<P style="TEXT-INDENT: 2em">只能在host上调用。</P>
<P style="TEXT-INDENT: 2em">没有定义__host__，__device__或者__global__的函数等同于__host__函数，系统都会把函数编译成host函数。</P>
<P style="TEXT-INDENT: 2em">另外，__host__定义可以和__device__定义一起使用，编译器会把这个函数编译为host和device通用的函数。</P>
<P style="TEXT-INDENT: 2em">4.2.1.4 Restrictions (强调，限制)</P>
<P style="TEXT-INDENT: 2em">__device__函数通常是inline，所以如果需要不inline 就需要加上uninline</P>
<P style="TEXT-INDENT: 2em">__device__和__global__函数都不支持递归调用。</P>
<P style="TEXT-INDENT: 2em">__device__和__global__函数都不能定义static变量在函数内部。</P>
<P style="TEXT-INDENT: 2em">__device__和__global__函数不能使用可变参数。</P>
<P style="TEXT-INDENT: 2em">__device__函数没有函数地址，也没有指向它的函数指针，但是__global__函数有。</P>
<P style="TEXT-INDENT: 2em">__global__定义和__host__定义不能一起使用。</P>
<P style="TEXT-INDENT: 2em">__global__函数必须是void返回类型。</P>
<P style="TEXT-INDENT: 2em">任何调用__global__的函数都必须指明运行配置。（Section 4.2.3）也就是线程kernel的调用方法。</P>
<P style="TEXT-INDENT: 2em">__global__函数是异步调用的。在运行结束前就会返回。</P>
<P style="TEXT-INDENT: 2em">__global__函数的参数通常是通过shared memory调用到Device中，最多是265bytes。</P></div>]]></description>
	    <author><![CDATA[pannsp]]></author>
	    <comments>http://pannsp.blog.163.com/blog/static/920232009916105718317</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://pannsp.blog.163.com/blog/static/920232009916105718317</guid>
    <pubDate>Fri, 16 Oct 2009 22:57:18 +0800</pubDate>
    <dcterms:modified>2009-10-16T22:57:18+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[(CUDA 编程5).CUDA编程接口（一）------一十八般武器 (转)]]></title>	
    <link>http://pannsp.blog.163.com/blog/static/920232009916103559796</link>
    <description><![CDATA[<div><P style="TEXT-INDENT: 2em">(CUDA 编程5) CUDA编程接口（一）------一十八般武器</P>
<P style="TEXT-INDENT: 2em">------GPU的革命</P>
<P style="TEXT-INDENT: 2em">序言：所谓一十八般武器，不同的年代又有不同的说法，最早的汉武年间的：矛、镗、刀、戈、槊、鞭、锏、剑、锤、抓、戟、弓、钺、斧、牌、棍、枪、叉。到三国的：九长：刀、矛、戟、槊、镗、钺、棍、枪、叉；九短：斧、戈、牌、箭、鞭、剑、锏、锤、抓。再到明清的：弓、弩、枪、刀、剑、矛、盾、斧、钺、戟、黄、锏、挝、殳（棍）、叉、耙头、锦绳套索、白打（拳术）。《水浒传》里的：矛、锤、弓、弩、铳、鞭、锏、剑、链、挝、斧、钺、戈、戟、牌、棒、枪、扒。今天的武术届又有：刀、枪、剑、戟、斧、钺、钩、叉、鞭、锏、锤、抓、镗、棍、槊、棒、拐、流星。400多种古代冷兵器时代的武器，常用的也只有这么多种。也就像我们的API一样，API有无数多个，你自己都可以给自己造几个API出来，常用的，或者就那么多个。要打天下也不能扛着锄头，竹竿干吧。秦国之所以能统一六国，在武器上的统一，提供同一个的型号的武器装备（看秦的历史，就可以发现所有的兵器都是同一型号生产，弓弩上的器件可以互换，从兵马俑坑中找到的剑戟，箭头的尺寸误差很小，都可以互换），也是他能战胜其他六国的很好的基础。</P>
<P style="TEXT-INDENT: 2em">正文：</P>
<P style="TEXT-INDENT: 2em">子曰：工欲善其事，必先利其器。我们要把显卡作为通用并行处理器来做并行算法处理，就得知道CUDA给我提供了什么样的接口，就得了解CUDA作为通用高性能计算平台上的一十八般武器。（如果你想自己开发驱动，自己写开发库- -那我不得不佩服你很有时间，想必也不会有很多人想自己在去实现一个CUDA吧，呵呵，虽然实现一个也不是太难）。书接上回《&nbsp;<A href="http://www.hpctech.com/2009/0818/202.html">CUDA硬件实现分析（二）------规行矩步------GPU的革命</A>》前面我们讲到了一些简单的CUDA的C语言扩展的规则，下面就具体来讲解CUDA给我听哦买提供了多少方便的API函数。在开发CUDA的时候，CDUA也给我们提供了一套完整的API函数。从一开始就在想，怎么把这些枯燥的API函数，或者这CUDA的一十八般武器说得清楚。如果按照中文的翻译的第四章那么讲解，或许晕的人更多，只知道这些是武器，而不知道什么武器是用来干嘛的。从序言看到，十八般武艺所列兵器大同小异，形式和内容却十分丰富。有长器械，短器械；软器械、双器械；有带钩的、带刺的、带尖的、带刀的；有明的、暗的；有攻的、防的；有打的、杀的击的；也有射的、挡的。我们来看CUDA的时候，一看到这么多的API函数，先来给他分一些类，然后才好徐徐道来。</P>
<P style="TEXT-INDENT: 2em">一、&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; API总结：</P>
<P style="TEXT-INDENT: 2em">1.&nbsp;&nbsp;&nbsp;&nbsp; 通用的一些接口，前一章节也有提高过：数学函数，时间函数，同步函数，原子操作；</P>
<P style="TEXT-INDENT: 2em">2.&nbsp;&nbsp;&nbsp;&nbsp; 控制Device的函数；就是得到设备信息，管理设备信息的函数。设置那块显卡工作，得到那块显卡的性能。这里有分为driver级别的API和runtime级别的API；有人会问什么是driver级别和runtime级别请看图： <IMG title="(CUDA 编程5).CUDA编程接口（一）------一十八般武器 (转) - pannsp - Pan Ant" alt="(CUDA 编程5).CUDA编程接口（一）------一十八般武器 (转) - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0819/20090819121033459.jpg" border=0></P>
<P style="TEXT-INDENT: 2em">这个图我们在前一章已经看到过了。不会不清楚吧～- -！driver级别的API就是提供驱动级别的API，就像写驱动一样的感觉。Runtime级别的API就是封装了一些Driver级别的API，按照一些常规的方法封装了一些底层的API。其实这里就像我们平时生活中一样，最开始对汽车不熟悉的时候，买一辆车回来能开就ok了；能用熟悉Runtime级别的API就行了。慢慢的，感觉汽车自带的音箱不好，自己就开始买一些原始设备回来改装车；慢慢的感觉整车都有点不爽了，然后慢慢的发现想修改发动机，修改外形……就开始改装车了，这样的工作，就得从Driver级别开始做了，玩得更高级一些的就自己设计图纸，自己来用一些零部件来组装车了。这就是Driver API和Runtime API的关系。Runtime的在开始的用起来一般都比较方便，慢慢的发现如果高层（high-level）的Runtime API用起来不方便，就用底层（low-level）的Driver API来自己做改装的车……</P>
<P style="TEXT-INDENT: 2em">3.&nbsp;&nbsp;&nbsp;&nbsp; 内存管理，host的内存，device的内存，global的内存，constant第，shared的，这里会分出来一章单独讲Texture（纹理）内存的使用，说实在的Texture也是内存～非要搞那么神秘，没办法，也只好拿出来单讲……PS：内存管理也分为Runtime级别的API接口，和Device级别的API接口。</P>
<P style="TEXT-INDENT: 2em">4.&nbsp;&nbsp;&nbsp;&nbsp; 程序运行控制：像Stream，Event，Context, Module, Execution control这样的咱都把归类到运行管理的。这里也得分清楚有Runtime级别的，也有Driver级别的。</P>
<P style="TEXT-INDENT: 2em">5.&nbsp;&nbsp;&nbsp;&nbsp; 好了，这里就剩下OpenGL和Direct3D的接口函数了，其实把，这也是为了方便做图来用的，主要是OpenGL和DX都已经成了图形显示方面的标准，so～显卡也得照顾这两个东东了，要不然显卡自己画……hoho要是真自己再来实现OpenGL或者DX，CUDA就真的会头大了，hoho～～还好就借用现有的图形显示的程序来做就行了。牛顿人家都说是站在“牛头人”（巨人）的肩膀上才能看得更远……咱也不要非自己费那么大的经去做一些无用功。想想吃不饱的时候没看到多少人减肥的，倒是现在吃得好了，减肥的人多了……长胖了去健身房减肥，始终感觉有点怪怪的……拿钱去做无用功，（有用功就是减肥）不过现在也有人在做实验，把多余的这些减肥的人的能量转化为电力……扯远了……提一下：他也有两个层次的API，有Runtime层次的，也有Driver层次的。</P>
<P style="TEXT-INDENT: 2em">好了，差不多就这5个部分的API了。下面我们就来个个讲解CUDA的十八般武器。</P>
<P style="TEXT-INDENT: 2em">二、&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; API讲解</P>
<P style="TEXT-INDENT: 2em">记得小时候练过几天棍法，现在好像起式都记不起来啦，哈哈，只记得棍要齐眉，剑齐耳朵，不知道错没，hoho。大学的时候一直想给武术队的张锐学九节鞭，不过一直太忙，除了和他练习过几次散打对抗，看他刀+鞭，双刀，枪，剑，九节鞭练习过几次以外，到大学毕业，也都没机会学九节鞭，～现在也就留着那一根九节鞭在身边。看到它就能想起一些朋友吧：）大学毕业也就各奔东西，都不知道他现在在干嘛……</P>
<P style="TEXT-INDENT: 2em">又走神了……想想前面五个部分，下面一部分一部分的讲解：</P>
<P style="TEXT-INDENT: 2em">1.&nbsp; 一些通用的函数：</P>
<P style="TEXT-INDENT: 2em">数学函数，前面章节已经讲了，提一下CUDA 2.0里面添加了一两个新的函数。</P>
<P style="TEXT-INDENT: 2em">时间函数，clock()这个自己去查C的函数库；还有CUDA提供的几个时间函数，用起来也没什么难的，只是说一些，CUDA提供的几个时间函数在计算Device上的运行时间的时候，和CPU上的时间函数比起来还不是那么的完美的准确，所以在做时间数据的时候，最好多取几次求个平均。</P>
<P style="TEXT-INDENT: 2em">同步函数前面章节讲到了， __syncthreads()函数，原子操作函数也就和以前的原子操作函数一个道理，也不用多加解释。不过其实我都觉得这__syncthreads()函数都应该归到程序运行控制部分。</P>
<P style="TEXT-INDENT: 2em">2．Device管理</P>
<P style="TEXT-INDENT: 2em">Runtime和Driver层面的API都提供了设备管理的函数，其实两个层面的API提供的功能都差不多，可以在API的说明中查到他的区别。这里需要指出来的是虽然Host主机上的多线程程序是应该可以同时访问同一块显卡（Device）的，毕竟显卡就是按照PCIE标准插在PCI插槽上的标准设备（也有AGP接口的），这就是一个多线程访问硬件的问题了。So～本来是应该可以的。但是由于CUDA的设计原因，这里的host上的当个多线程的线程每个线程都要执行CUDA kernel的时候，就必须执行在多个Device上面。保证每个线程访问的Device不是同一个。多个线程线程A不能分享线程B在Device上创建的资源。</P>
<P style="TEXT-INDENT: 2em">Runtime API：cudaGetDeviceCount() 和 cudaGetDeviceProperties() 提供了遍历硬件设备，得到某个设备性能参数的功能。</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">int deviceCount;</P>
<P style="TEXT-INDENT: 2em">cudaGetDeviceCount(&amp;deviceCount);</P>
<P style="TEXT-INDENT: 2em">int device;</P>
<P style="TEXT-INDENT: 2em">for (device = 0; device &lt; deviceCount; ++device) {</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; cudaDeviceProp deviceProp;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; cudaGetDeviceProperties(&amp;deviceProp, device);</P>
<P style="TEXT-INDENT: 2em">}</P>
<P style="TEXT-INDENT: 2em">cudaSetDevice() 设置某一块Device作为这个主机host上的某一个运行线程的设备:</P>
<P style="TEXT-INDENT: 2em">cudaSetDevice(device);</P>
<P style="TEXT-INDENT: 2em">这个函数必须要在使用 __global__ 的函数或者Runtime</P>
<P style="TEXT-INDENT: 2em">的其他的API调用之前才能生效。 如果没有调用cudaSetDevice()，device 0 就会被设置为默认的设备，接下里的如果还有cudaSetDevice()函数也不会有效果。</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">Driver API：</P>
<P style="TEXT-INDENT: 2em">cuDeviceGetCount()和cuDeviceGet() &nbsp;看名字就知道干嘛的～（英语不好的这应该能看明白吧- -！不要被我这个那 国家四级都没过的人BS你哈～！～）</P>
<P style="TEXT-INDENT: 2em">int deviceCount;</P>
<P style="TEXT-INDENT: 2em">cuDeviceGetCount(&amp;deviceCount);</P>
<P style="TEXT-INDENT: 2em">int device;</P>
<P style="TEXT-INDENT: 2em">for (int device = 0; device &lt; deviceCount; ++device) {</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; CUdevice cuDevice;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; cuDeviceGet(&amp;cuDevice, device);</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; int major, minor;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; cuDeviceComputeCapability(&amp;major, &amp;minor, cuDevice);</P>
<P style="TEXT-INDENT: 2em">}</P>
<P style="TEXT-INDENT: 2em">3．内存管理：</P>
<P style="TEXT-INDENT: 2em">Device上的内存可以被分配成线性的，也可以分配为CUDA的数组形式的。CUDA的内存可以为1维，2维，还有3维（2.0版本）。内存的类型有unsigned8，16或者32位的int，16位（只有driver API可以做到）float，32位的float。这里分配的内存也只能通过kernel里面的函数通过处理纹理的方法来处理。这个地方也是GPU的历史原因了，以前都是处理图像的，所以这里叫纹理。……叫啥都是别人取得名字 - -！在计算机里面不就是内存嘛 - -！</P>
<P style="TEXT-INDENT: 2em">Host的runtime的运行库也提供按照page-locked的内存管理的函数，page-locked的内存要比pageable方式快很多。好的东西往往比较少～page-locked就是很稀少的。如果通过减少分配pageable的内存来分配多的page-locked内存，系统需要的分页内存就少了，这也就会让系统的性能降低了。所以在处理这块的时候要合理。</P>
<P style="TEXT-INDENT: 2em">Runtime API：</P>
<P style="TEXT-INDENT: 2em">使用 cudaMalloc() 或者 cudaMallocPitch() 来分配线性内存，通过cudaFree()释放内存.</P>
<P style="TEXT-INDENT: 2em">下面是分配一个大小为256 float数组的方法:</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">float* devPtr;</P>
<P style="TEXT-INDENT: 2em">cudaMalloc((void**)&amp;devPtr, 256 * sizeof(float));</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">在使用2D数组的时候最好用cudaMallocPitch()来分配，在guide的第五章在讲到内存之间的调度的时候，就会看到他的好处。下面是一个分配一个大小为width×height 2D float数组的例子:</P>
<P style="TEXT-INDENT: 2em">// host co<wbr>de</P>
<P style="TEXT-INDENT: 2em">float* devPtr;</P>
<P style="TEXT-INDENT: 2em">int pitch;</P>
<P style="TEXT-INDENT: 2em">cudaMallocPitch((void**)&amp;devPtr, &amp;pitch,</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; width * sizeof(float), height);</P>
<P style="TEXT-INDENT: 2em">myKernel&lt;&lt;&lt;100, 512&gt;&gt;&gt;(devPtr, pitch);</P>
<P style="TEXT-INDENT: 2em">// device co<wbr>de</P>
<P style="TEXT-INDENT: 2em">__global__ void myKernel(float* devPtr, int pitch)</P>
<P style="TEXT-INDENT: 2em">{</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; for (int r = 0; r &lt; height; ++r) {</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; float* row = (float*)((char*)devPtr + r * pitch);</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int c = 0; c &lt; width; ++c) {</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; float element = row[c];</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; }</P>
<P style="TEXT-INDENT: 2em">}</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">CUDA 的数组方式，需要用 cudaMallocArray()和cudaFreeArray(). cudaMallocArray()又需要cudaCreateChannelDesc()来管理，这个其实可以在第guide的第五章里面可以看到，我们后面也会详细的介绍内存的调度和管理，和传统的GPU的内存方式不一样的地方.</P>
<P style="TEXT-INDENT: 2em">分配 width×height 32位float的CUDA array例子:</P>
<P style="TEXT-INDENT: 2em">cudaChannelFormatDesc channelDesc =</P>
<P style="TEXT-INDENT: 2em">cudaCreateChannelDesc&lt;float&gt;();</P>
<P style="TEXT-INDENT: 2em">cudaArray* cuArray;</P>
<P style="TEXT-INDENT: 2em">cudaMallocArray(&amp;cuArray, &amp;channelDesc, width, height);</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">cudaGetSymbolAddress用来在全局中定位一个数组的位置，然后 cudaGetSymbolSize()来回忆他分配的时候大小----如果有全局编程或者多线程编程经验的，或者用过几个函数同时处理一个数据的经验，都会为了把数据的独立性弄出来，不能让数据和函数耦合太大，一般都不会让函数直接牵扯上数据，只是在函数处理的时候重新定位数据----这地方有点绕～～</P>
<P style="TEXT-INDENT: 2em">下面是一些例子，内存之间的拷贝，回想一下有几种内存～linear的有两个函数可以分配的，还有CUDA array的内存:</P>
<P style="TEXT-INDENT: 2em">cudaMemcpy2DToArray(cuArray, 0, 0, devPtr, pitch,</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; width * sizeof(float), height,</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cudaMemcpyDeviceToDevice);</P>
<P style="TEXT-INDENT: 2em">The following co<wbr>de sample copies some host memory array to device memory:</P>
<P style="TEXT-INDENT: 2em">float da<wbr>ta[256];</P>
<P style="TEXT-INDENT: 2em">int size = sizeof(da<wbr>ta);</P>
<P style="TEXT-INDENT: 2em">float* devPtr;</P>
<P style="TEXT-INDENT: 2em">cudaMalloc((void**)&amp;devPtr, size);</P>
<P style="TEXT-INDENT: 2em">cudaMemcpy(devPtr, da<wbr>ta, size, cudaMemcpyHostToDevice);</P>
<P style="TEXT-INDENT: 2em">从host上面拷贝内存到device的constant上面:</P>
<P style="TEXT-INDENT: 2em">__constant__ float constData[256];</P>
<P style="TEXT-INDENT: 2em">float da<wbr>ta[256];</P>
<P style="TEXT-INDENT: 2em">cudaMemcpyToSymbol(constData, da<wbr>ta, sizeof(da<wbr>ta));</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">-#%#$^$^*^&amp;系统int中断：(@$:突然发现已经是早上8点多了 - -！又一夜！@#￥%……！#……</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">Driver API：</P>
<P style="TEXT-INDENT: 2em">cuMemAllocPitch()被推荐来作为2D的数组分配函数，会在内存对齐方面做一些check～然后保证在处理数据（像cuMemcpy2D()这样的拷贝函数)的时候达到最优的速度。下面就是一个float的width×height的2D的数组，并在循环里面处理的例子: ----（这个地方扩展提两个东东：一个是内存对齐，做程序优化的时候，或者处理网络问题的时候，都会遇到这些问题，内存对齐是一个普遍存在的问题，像SSE这样的编程的时候也是要求内存对齐的，看以后要是有机会以单独讲解内存对齐的问题：）第二个是CUDA的内存访问的问题，就是很多朋友都会在写kernel的时候，搞不明白里面的threadid，block id和传进来的内存的关系，这个地方必须要搞清楚的；内存和线程是CUDA编程必须搞明白的两个概念，不然到时候就会很混乱。其实看看前面的章节，应该能看明白的，不明白就用手画图～要是感觉还是有点不太清楚，也不要担心，接下来的章节会单独把一些难懂的问题更详细的讲解。）</P>
<P style="TEXT-INDENT: 2em">// host co<wbr>de</P>
<P style="TEXT-INDENT: 2em">CUdeviceptr devPtr;</P>
<P style="TEXT-INDENT: 2em">int pitch;</P>
<P style="TEXT-INDENT: 2em">cuMemAllocPitch(&amp;devPtr, &amp;pitch,</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; width * sizeof(float), height, 4);</P>
<P style="TEXT-INDENT: 2em">cuModuleGetFunction(&amp;cuFunction, cuModule, “myKernel”);</P>
<P style="TEXT-INDENT: 2em">cuFuncSetBlockShape(cuFunction, 512, 1, 1);</P>
<P style="TEXT-INDENT: 2em">cuParamSeti(cuFunction, 0, devPtr);</P>
<P style="TEXT-INDENT: 2em">cuParamSetSize(cuFunction, sizeof(devPtr));</P>
<P style="TEXT-INDENT: 2em">cuLaunchGrid(cuFunction, 100, 1);</P>
<P style="TEXT-INDENT: 2em">// device co<wbr>de</P>
<P style="TEXT-INDENT: 2em">__global__ void myKernel(float* devPtr)</P>
<P style="TEXT-INDENT: 2em">{</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; for (int r = 0; r &lt; height; ++r) {</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; float* row = (float*)((char*)devPtr + r * pitch);</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int c = 0; c &lt; width; ++c) {</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; float element = row[c];</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; }</P>
<P style="TEXT-INDENT: 2em">}</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">cuArrayCreate()和cuArrayDestroy()来创建和释放CUDA array类型的数组。下面是一个 width×height 32-bit float类型的CUDA array 分配的例子：</P>
<P style="TEXT-INDENT: 2em">CUDA_ARRAY_DESCRIPTOR desc;</P>
<P style="TEXT-INDENT: 2em">desc.Format = CU_AD_FORMAT_FLOAT;</P>
<P style="TEXT-INDENT: 2em">desc.NumChannels = 1;</P>
<P style="TEXT-INDENT: 2em">desc.Width = width;</P>
<P style="TEXT-INDENT: 2em">desc.Height = height;</P>
<P style="TEXT-INDENT: 2em">CUarray cuArray;</P>
<P style="TEXT-INDENT: 2em">cuArrayCreate(&amp;cuArray, &amp;desc);</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">这个也是几个内存之间拷贝的例子:</P>
<P style="TEXT-INDENT: 2em">CUDA_MEMCPY2D copyParam;</P>
<P style="TEXT-INDENT: 2em">memset(&amp;copyParam, 0, sizeof(copyParam));</P>
<P style="TEXT-INDENT: 2em">copyParam.dstMemoryType = CU_MEMORYTYPE_ARRAY;</P>
<P style="TEXT-INDENT: 2em">copyParam.dstArray = cuArray;</P>
<P style="TEXT-INDENT: 2em">copyParam.srcMemoryType = CU_MEMORYTYPE_DEVICE;</P>
<P style="TEXT-INDENT: 2em">copyParam.srcDevice = devPtr;</P>
<P style="TEXT-INDENT: 2em">copyParam.srcPitch = pitch;</P>
<P style="TEXT-INDENT: 2em">copyParam.WidthInBytes = width * sizeof(float);</P>
<P style="TEXT-INDENT: 2em">copyParam.Height = height;</P>
<P style="TEXT-INDENT: 2em">cuMemcpy2D(&amp;copyParam);</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">拷贝host上面的内存到device上面：</P>
<P style="TEXT-INDENT: 2em">float da<wbr>ta[256];</P>
<P style="TEXT-INDENT: 2em">int size = sizeof(da<wbr>ta);</P>
<P style="TEXT-INDENT: 2em">CUdeviceptr devPtr;</P>
<P style="TEXT-INDENT: 2em">cuMemAlloc(&amp;devPtr, size);</P>
<P style="TEXT-INDENT: 2em">cuMemcpyHtoD(devPtr, da<wbr>ta, size);</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">本来想一次把下面的4，5点也讲了～但是如果一下讲出来～lz帖子太长了～～这就不好了～～呵呵，实在的，看第四章的中文翻译的时候，就看了前面几个就不想看了～帖子不能老长老长的又不吸引人- -！hoho～ 所以后面的4，5就在下贴里面发了～～看了这么多也比较累的～好好的休闲一下～你还会发现出了4，5部分，还少了一个部分，那就是CUDA自己的函数，怎么定义，有device的，有global的，这个又有怎么区分啦～且听下回讲解。Hoho</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">ps：熬夜不好～熬夜很伤身……</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">有的时候，我们经常会用旧的东东来和新的东东比较，就像C和C++都不知道争论多少年了。其实很多时候我到觉得是没必要的争论，除非你是做C或者C++本身开发的。就像新的硬件不停的变化，以前的概念或者今天就不能用了，有的时候，我们关心架构在这个之上的程序开发就够了，没有太多的必要去问茴香豆的茴有几种写法。有的时候看到论坛里面的争论的时候，很多都不太清楚问题的也参加到争论里面，感觉就是明白事的没有几个，倒是起哄的不少 - -！人家说当局者迷，旁观者清，我看现在很多时候倒是当局者清，旁观者瞎起哄- -！呵呵，好像合乎了现在很多选秀节目的心理哈，起哄的人越多，人家的节目越红，哈哈～----扯远了……</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">Ps2：在学习API的时候，最好的方法就是多实验，多尝试API的性能，就像练习武术中的器械一样，经常用才会精通的，才能在这个基础上想出新的招式。就像有人在CSDN论坛问，怎么才能弄好ACM比赛，看到像刘汝佳写的《算法艺术与信息学竞赛》的书，都是一些方法，没有代码，就觉得很不解，就像问用什么代码来实现。我的回答就是：阅尽天下A×，心中自然无码。多做代码的练习，找一本数据结构的书，对照上面的代码都自己实现一遍，找一个代码练习的书，自己都重新写一遍。要不然就把像MSDN这样的介绍API的资料上的Demo能实现多少都去实现以下，呵呵。其实学习怎么编码都是一些基础工作。最重要的到最后都是算法的实现。其实到最后你会发现写程序就是数组的处理，就是字符串的处理……仅此而已～。所以不要小看了C语言那些书上的小程序例子，不要小看了输出（**）星星这样的例子，实际程序中很多时候都是处理这些星星～hoho～不要一上来就想去写什么游戏，写什么网络软件～先把字符串处理好了，就nb了，真的～你看ACM的题目，topcoder的题目，几乎都是字符串处理- -！哈哈。多实践，多去做一些demo例子。到真正用的时候就会觉得手到擒来，怎么用怎么顺手了。</P>
<P style="TEXT-INDENT: 2em">还是那句话：约尽天下A×，心中自然无码。</P></div>]]></description>
	    <author><![CDATA[pannsp]]></author>
	    <comments>http://pannsp.blog.163.com/blog/static/920232009916103559796</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://pannsp.blog.163.com/blog/static/920232009916103559796</guid>
    <pubDate>Fri, 16 Oct 2009 22:35:59 +0800</pubDate>
    <dcterms:modified>2009-10-17T16:58:11+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[(CUDA 编程4).CUDA硬件实现分析（二）------规行矩步 (转)]]></title>	
    <link>http://pannsp.blog.163.com/blog/static/92023200991695659484</link>
    <description><![CDATA[<div><P style="TEXT-INDENT: 2em">（CUDA 编程4）CUDA硬件实现分析（二）------规行矩步</P>
<P style="TEXT-INDENT: 2em">------GPU的革命</P>
<P style="TEXT-INDENT: 2em">前面已经讲解了很多概念上的东西，其实CUDA的最重要的两个东西，就是线程和内存。只要掌握了这两个东西，CUDA的东西也就很简单了。它的编写语言是C扩展的，所以，就当C语言用就行了，只是主要它的特殊的几个标志就ok了。前面讲解了线程和内存的模型，大概，应该，似乎，可以在你的脑海里面有一个概念了吧。只要有这个概念，我的文章的目的就达到了。前面的《<A href="http://www.hpctech.com/2009/0818/200.html">CUDA硬件实现分析（一）------安营扎寨-----GPU的革命</A>》已经讲解了线程在CUDA的具体运行过程。下面我们一起来看看内存在CUDA的硬件实现中的一些规定。这也比较合理吧，大军安营扎寨了，就应该颁布规则制度，只有了解CUDA的规则制度，才能真正的把各个线程都管理好。才能在这个平台上让程序高效的运行。</P>
<P style="TEXT-INDENT: 2em">这里我们先明确几个</P>
<P style="TEXT-INDENT: 2em">一．&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Threads，Warps, Blocks</P>
<P style="TEXT-INDENT: 2em">1. 一个warp最多有32个threads。只有在总线程少于32的时候，才可能在一个warp里面少于32个线程。</P>
<P style="TEXT-INDENT: 2em">2. 每一个block最多有16个warp。就是说一个一个block里面最多有512个thread。</P>
<P style="TEXT-INDENT: 2em">3. 每一个Block在同一个SM上执行，也就是同一个block的warp都在同一个SM上运行。</P>
<P style="TEXT-INDENT: 2em">4. G80有16个SM。</P>
<P style="TEXT-INDENT: 2em">5. 所以最少16个blocks才能占全所有的SM。</P>
<P style="TEXT-INDENT: 2em">6. 如果资源（看看前面讲解的线程都要从device哪里分什么资源）够线程分，一个SM上面可以跑多余一个block的线程。就是同时可以跑2个，3……个block的线程。</P>
<P style="TEXT-INDENT: 2em">二．&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 访问速度</P>
<P style="TEXT-INDENT: 2em">Register—HW 一个时间周期</P>
<P style="TEXT-INDENT: 2em">Shared Memory------HW一个时钟周期</P>
<P style="TEXT-INDENT: 2em">Local Memory --- DRAM，no cache， 慢</P>
<P style="TEXT-INDENT: 2em">Global Memory --- DRAM， no cache， 慢</P>
<P style="TEXT-INDENT: 2em">Constant Memory --- DRAM， cached， 1……10s……100s个周期，这个和cache的locality有关。</P>
<P style="TEXT-INDENT: 2em">Texture Memory --- DRAM， cached， 1……10s……100s个周期，这个和cache的locality有关。</P>
<P style="TEXT-INDENT: 2em">Instruction Memory（不可见）--- DRAM，cached</P>
<P style="TEXT-INDENT: 2em">三．&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CUDA程序架构</P>
<P style="TEXT-INDENT: 2em">如图 <IMG title="(CUDA 编程4).CUDA硬件实现分析（二）------规行矩步 (转) - pannsp - Pan Ant" alt="(CUDA 编程4).CUDA硬件实现分析（二）------规行矩步 (转) - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0819/20090819120913393.jpg" border=0></P>
<P style="TEXT-INDENT: 2em">四．&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 语言扩展</P>
<P style="TEXT-INDENT: 2em">单从学习语言来说，我到觉得应该精学一种，然后其他的就触类旁通了。在学习新语言的时候，要想快速的入门，也有诀窍。1.变量的定义方式。2.函数的定义方式。3.逻辑控制方式（if，loop……）。只要把这3个东西弄明白了，管他啥新的语言，20分钟就可以入门……然后入门都可以了，那要慢慢的更入的研究，那就得看你对这门语言的了解了。其实万变不离其中。其实从计算机编程语言的角度出发，就是定义一些数据，然后对数据进行操作，so……学习语言就从这个角度入手，那就很简单了。像java或者C#等一些语言比C语言多的新的特性不外乎就是方便你开发而已。</P>
<P style="TEXT-INDENT: 2em">所以我们这里再来说说CUDA的语言，不外乎就是扩展了C语言，为了方便在GPU显卡上运行，规定一个特定环境。就定义一些特定的变量，说明他们是在GPU上的。这里有说明内存，和函数，是在GPU上的……so，这样一来，CUDA就扩展了C语言的变量分配定义和函数定义。</P>
<P style="TEXT-INDENT: 2em"><IMG title="(CUDA 编程4).CUDA硬件实现分析（二）------规行矩步 (转) - pannsp - Pan Ant" alt="(CUDA 编程4).CUDA硬件实现分析（二）------规行矩步 (转) - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0819/20090819120913952.jpg" border=0></P>
<P style="TEXT-INDENT: 2em">上面这张图是来之Fall 2007 syllabus，上面已经说得很清楚各个变量定义的时候的位置和生存周期。其实就是在C语言的常规变量的时候，定义了变量的位置而已。</P>
<P style="TEXT-INDENT: 2em">其中有一个约束限制，就是指针变量，在kernel里面的指针变量，只能指向从global上面分配的内存。</P>
<P style="TEXT-INDENT: 2em">五．&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 内建的变量</P>
<P style="TEXT-INDENT: 2em">所谓内建，就是CUDA自己在kernel里面定义的一些变量。就像我们以前计算线程id的时候，就利用了他的自己的变量，dim3 gridDim；dim3 blockDim；dim3 blockIdx；dim3 threadIdx；注意gridDim，他的gridDim.z在现在的CUDA1.1版本中没定义。</P>
<P style="TEXT-INDENT: 2em">内建变量，[u]char[1..4], [u]short[1..4], [u]int[1..4], [u]long[1..4], float[1..4] 就是构建了有4个变量的struct；</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; uint4 param; ---》等价位一个struct里面有4个int。</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int y = param.y; </P>
<P style="TEXT-INDENT: 2em">dim3 就是unit3这样的struct。</P>
<P style="TEXT-INDENT: 2em">六．&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 通用的数学函数</P>
<P style="TEXT-INDENT: 2em">·&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pow, sqrt, cbrt, hypot </P>
<P style="TEXT-INDENT: 2em">·&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exp, exp2, expm1</P>
<P style="TEXT-INDENT: 2em">·&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; log, log2, log10, log1p</P>
<P style="TEXT-INDENT: 2em">·&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sin, cos, tan, asin, acos, atan, atan2 </P>
<P style="TEXT-INDENT: 2em">·&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sinh, cosh, tanh, asinh, acosh, atanh</P>
<P style="TEXT-INDENT: 2em">·&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ceil, floor, trunc, round</P>
<P style="TEXT-INDENT: 2em">但是这里要指出，有几个函数是不精确的，但是可以很快的运行：</P>
<P style="TEXT-INDENT: 2em">–&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; __pow </P>
<P style="TEXT-INDENT: 2em">–&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; __log, __log2, __log10 </P>
<P style="TEXT-INDENT: 2em">–&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; __exp </P>
<P style="TEXT-INDENT: 2em">–&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; __sin, __cos, __tan</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">七．&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 在host部分的运行库（CUDA Runtime ）</P>
<P style="TEXT-INDENT: 2em">1.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 提供Device管理的api（有多个显卡的时候怎能来设置，这里我们还没讲当多显卡并行运行库）</P>
<P style="TEXT-INDENT: 2em">2.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 初始化调用的Runtime函数</P>
<P style="TEXT-INDENT: 2em">3.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 每一个host的线程（thread）只能调用一个device的函数在一个device上运行。就是同时不能有几个host（主机）线程在调用同一个device上的运行函数。</P>
<P style="TEXT-INDENT: 2em">八．&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 内存管理函数</P>
<P style="TEXT-INDENT: 2em">我们讲了那么多的内存，下面就来看看到底是什么样的函数：</P>
<P style="TEXT-INDENT: 2em">–&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cudaMalloc（），cudaFree（）；内存分配和释放（device上的）</P>
<P style="TEXT-INDENT: 2em">–&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 内存copy：cudaMemcpy(), cudaMemcpy2D(), cudaMemcpyToSymbol(), cudaMemcpyFromSymbol()；</P>
<P style="TEXT-INDENT: 2em">九．&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 线程同步函数</P>
<P style="TEXT-INDENT: 2em">void __syncthreads();</P>
<P style="TEXT-INDENT: 2em">同步同一个block里面的线程，让block里面的线程都允许到这一点的时候，就等待同一个block里面的其他线程，就像军训的时候，大家吃饭吃完了还不能一个个走，必须得一个桌子的人都吃完了才能走。还得列队一起走，呵呵，这就是同步。所以最好保证每一个kernel里面的处理都是很快的，这样才不会让其他thread等待太久，不然会挨骂的 - -！hoho</P>
<P style="TEXT-INDENT: 2em">哥们来点实际的吧- - 肯定很多人都这么在吼了。嘿嘿，下面让我们看一段代码，example里面的transpose：</P>
<P style="TEXT-INDENT: 2em">#define BLOCK_DIM 16</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">// This kernel is optimized to ensure all global reads and writes are coalesced,</P>
<P style="TEXT-INDENT: 2em">// and to avoid bank conflicts in shared memory.&nbsp; This kernel is up to 11x faster</P>
<P style="TEXT-INDENT: 2em">// than the naive kernel below.&nbsp; Note that the shared memory array is sized to </P>
<P style="TEXT-INDENT: 2em">// (BLOCK_DIM+1)*BLOCK_DIM.&nbsp; This pads each row of the 2D block in shared memory </P>
<P style="TEXT-INDENT: 2em">// so that bank conflicts do not occur when threads address the array column-wise.</P>
<P style="TEXT-INDENT: 2em">__global__ void transpose(float *odata, float *idata, int width, int height)</P>
<P style="TEXT-INDENT: 2em">{</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; __shared__ float block[BLOCK_DIM][BLOCK_DIM+1]; //(1)</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; // read the matrix tile into shared memory</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; unsigned int xIndex = blockIdx.x * BLOCK_DIM + threadIdx.x;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; unsigned int yIndex = blockIdx.y * BLOCK_DIM + threadIdx.y;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; if((xIndex &lt; width) &amp;&amp; (yIndex &lt; height)) // (2)</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; {</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unsigned int index_in = yIndex * width + xIndex;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; block[threadIdx.y][threadIdx.x] = idata[index_in];</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; }</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; __syncthreads(); //(3)</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; // write the transposed matrix tile to global memory</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; xIndex = blockIdx.y * BLOCK_DIM + threadIdx.x;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; yIndex = blockIdx.x * BLOCK_DIM + threadIdx.y;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; if((xIndex &lt; height) &amp;&amp; (yIndex &lt; width))</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; {</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unsigned int index_out = yIndex * height + xIndex;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; odata[index_out] = block[threadIdx.x][threadIdx.y];</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp; }</P>
<P style="TEXT-INDENT: 2em">}</P>
<P style="TEXT-INDENT: 2em">是不是在代码面前都感觉是那么的亲切啊～嘿嘿，有时候看算法的书看久了以后，看到代码实现，那才叫亲切啊&gt;_&lt;!</P>
<P style="TEXT-INDENT: 2em">下面对加红的3处代码分别讲解：</P>
<P style="TEXT-INDENT: 2em">（1）&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; shared__ float block[BLOCK_DIM][BLOCK_DIM+1];</P>
<P style="TEXT-INDENT: 2em">为啥要用shared啊？知道为啥不，快啊～shared在block里面，是大家公用的，不用再从global哪里调换。还记得军训的时候食堂吃饭的时候那个内存讲解吗？桌子上的菜，肯定比你从中间几个桶里面去拿菜要快啊～</P>
<P style="TEXT-INDENT: 2em">（2）&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if((xIndex &lt; width) &amp;&amp; (yIndex &lt; height))</P>
<P style="TEXT-INDENT: 2em">由于CUDA在生成线程的时候，是一块一块的生成的，所以有的时候，x和y有可能越界。数据都是均匀的分配到每一个block里面去的，很可能就会出现有的blcok里面分配到的数据不一定完整，要是想不明白的（先画图想想），等后面的章节，举例子说明。</P>
<P style="TEXT-INDENT: 2em">（3）&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; __syncthreads();</P>
<P style="TEXT-INDENT: 2em">同步，就是等每一个thread都运行到这里的时候，再接下来运行。这里也避免了数据访问的冲突。</P>
<P style="TEXT-INDENT: 2em">（4）&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 还有一个得讲讲，为啥下面的还重新计算一下x和y，因为线程被挂起来了以后，重新进入warp的时候，分配的id就不一定是一样的了。这里就有点像铁打的营盘流水的兵，每一波人都可以分配到同一张桌子吃饭，但是不一定是上一波人了～。碗筷还是那些碗筷，位置还是那个位置，坐的人不一样了。</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">上面的都是C语言扩展库的东西，就是说，这些是扩展C语言的东东，只要是基于C语言的规则都可以运行的，不管是host还是device。</P>
<P style="TEXT-INDENT: 2em">好了，今天要讲的部分应该差不多了，要是再写多一些，或者就有会像手册第4章那样，看了一半很多人就不想看了～- -！太长，lz 帖子太长，顶lz- -！我可不想看到这样的留言，hoho～</P>
<P style="TEXT-INDENT: 2em">从小到大，我们听过好多规则，但是好多时候听进去的有多少？很多玩游戏的人，游戏的规则都不清楚- -ps，俺现在还不清楚StarCraft里面的某些兵的战斗力是多少 - -！就玩- -！小小的bs一下自己。只是为了消遣罢了·</P>
<P style="TEXT-INDENT: 2em">我始终觉得自己不适合玩游戏，我也知道自己玩游戏不会完成专业玩家，so～还是老老实实做自己的工作，做一些能养活自己的事情，或许能让自己更充实一些。在游戏里面充实以后，或许在现实中就会空虚了- -！到时候就不是游戏人生，那就是人生被游戏了。呵呵。想起高中的数学老师，虽然在很多journal上发表了文章，也自己出版了数学的几本书了。但是和别人打麻将的时候总是输多赢少。问他为啥数学这么厉害，玩麻将不行～他说，我玩麻将的时候只是消遣，根本就没用脑袋想麻将这回事。</P>
<P style="TEXT-INDENT: 2em">或许我们做事情的时候，就应该专注做自己的一些方面吧，只要坚持下来做了，做好了，做精了，那就够了。小时候就从长辈哪里听到，半灌水叮当响这样的词，或许我们更应该低调做人，高调做事吧。</P>
<P style="TEXT-INDENT: 2em"></P></div>]]></description>
	    <author><![CDATA[pannsp]]></author>
	    <comments>http://pannsp.blog.163.com/blog/static/92023200991695659484</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://pannsp.blog.163.com/blog/static/92023200991695659484</guid>
    <pubDate>Fri, 16 Oct 2009 21:56:59 +0800</pubDate>
    <dcterms:modified>2009-10-16T21:57:46+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[(CUDA 编程3).CUDA硬件实现分析（一）------安营扎寨 （转）]]></title>	
    <link>http://pannsp.blog.163.com/blog/static/92023200991675920495</link>
    <description><![CDATA[<div><P style="TEXT-INDENT: 2em">（CUDA编程3）CUDA硬件实现分析（一）------安营扎寨</P>
<P style="TEXT-INDENT: 2em">前面我们已经说到线程的一些概念，但是这些概念都是软环节的。我们常常会听到某某单位说他们的软硬件配置如何如何的好。软件再好，每个士兵都是可造之才，但是如果硬件条件跟不上，也没他们的勇武之地。就像国内做过一些跳槽原因的统计，很多人已经跳槽都是为了高工资，但实际统计结果表明，很多人都觉得在以前那个公司里面学不到东西，或者说得不到发挥自己长处的地方。那就得看公司有没有这样的机会让你发挥你的能力了。看到这里，扯远了～很多人都看得不耐烦了……书接上回《&nbsp;<A href="http://www.hpctech.com/2009/0818/199.html">CUDA 线程执行模型分析（二）大军未动粮草先行------GPU的革命</A>》。已经讲到CUDA在线程模型是一个什么样子，经过几天的吸收，也应该在脑子里面有一些印象了。但是你会问一个问题，我们是不是可以开无数个线程来执行啦？或许招兵的人都想找到很多很多的人，但是你得也考虑一下你的粮食有多少，军营有多大。在这么我们得讨论一下现在的支持CUDA的nvidia的显卡现在的硬件情况。</P>
<P style="TEXT-INDENT: 2em"><IMG title="(CUDA 编程3).CUDA硬件实现分析（一）------安营扎寨 （转） - pannsp - Pan Ant" alt="(CUDA 编程3).CUDA硬件实现分析（一）------安营扎寨 （转） - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0819/20090819120630636.jpg" border=0>空洞的讲解或许还是没怎么又说服力，下面以G80为例子。</P>
<P style="TEXT-INDENT: 2em">1. G80里面有16个Multiprocessor.</P>
<P style="TEXT-INDENT: 2em">2. 每个Multiprocessor都有一组（G80里面是8个）32位的Processor（每个Processor都是SIMD架构，什么叫SIMD架构：军训的时候，大家都到了食堂，不是像在学校里面，每个人那自己的碗筷就三三俩俩的去吃饭，那可要讲究纪律，啥叫纪律，一群人站在桌子面前，连长没发话，谁也不敢坐下来……连长一声令下：“坐下”。所有的人才按照敢坐下来，也是同时坐下来- -！要是谁没有同步坐下来，那就惨了- -！再来一次，一定要是同步坐下去的，都能听到声的，咵！恨不得把板凳给坐碎了------还是部队的东西结实，兄弟们怎么坐到军训结束只有坐坏屁股的，没有听说凳子坐坏的- -！所以啊，爱惜公物就是爱惜自己。记住了吧，这就是SIMD：Single Instruction Multiple Da<wbr>ta 。 ）还有共用的Instruction Unit（这玩儿就不用翻译是啥了吧～看SIMD，自己理解去）。在G80里面有两个Instruction Unit模块。</P>
<P style="TEXT-INDENT: 2em">3. 每一个时钟周期内，按照warp（这玩儿咋翻译啦？就理解为运行的时候，一个block里面一起运行的thread，例如block里面有512个thread，但是每次只有32个thread在运行，那么这32个thread就是一个运行的warp组- -! 还好不是rap- -！俺就真没法解释了）</P>
<P style="TEXT-INDENT: 2em">4. 每一个warp里面包含的thread数量是有限的，现在的规定是32个。将来不知道会不会有变化？不知道，这个只有CUDA开发人员知道了。</P>
<P style="TEXT-INDENT: 2em">我们还是按照我们的既定方式学习吧～看图说话- -！接下来又是一张图：&nbsp;<IMG title="(CUDA 编程3).CUDA硬件实现分析（一）------安营扎寨 （转） - pannsp - Pan Ant" alt="(CUDA 编程3).CUDA硬件实现分析（一）------安营扎寨 （转） - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0819/20090819120630387.jpg" border=0></P>
<P style="TEXT-INDENT: 2em">刚才我们已经说到了硬件上的处理器的模型，现在我们来看看内存吧。前面的章节我们已经讲到了内存的register，local，shared，constant，texture，global，现在看到对应的硬件的位置了吧？哦，对了，有人会问，local和global跑那里去了，你看看，是不是多了一个Device Memory嘛。Local和Global 都是在Device Memory上的。只是在线程模型运行的时候，为了方便说明线程的模型，把它单独划分出来好说明。其实Constant和Texture都是在Device Memory里面的，但是人家是只读的Cache，所以比Global和local要快一些。不过Cache和shared Memory比起来，又显得慢一些了。</P>
<P style="TEXT-INDENT: 2em">硬件的架构差不多就是这么多。如果还不是很清楚，我们再讲讲军营的生活。还是讲吃食堂吃饭吧（回想起大学的时候和几个好朋友一起成立了一个编程爱好者协会……结果后来变成了好吃协会- -！）。由于我们现在的连队人数众多，一个grid里面就可以有65535个thread，所以一下子让这么多人一起吃饭，是不可能的。我们有好多个grid怎么办？所以每次Device（就是显卡）只处理一个grid。但是每个grid里面又有那么多人。一来是咱们地方小，不能容纳那么多人一起吃饭，而来是也没必要再建立那么多食堂。大家可以轮流吃，早吃的早走，晚来的也晚不了多久。</P>
<P style="TEXT-INDENT: 2em">这次是Grid1先到达食堂，然后他把士兵分成了几个block，（我们上次已经讲过的，block和grid的关系，应该还记得把）但是现在桌子还是少，为了大家不乱起来，每个block的人就指定到某一个multiprocessor（一个周期只能容纳32个人一起用餐）哪里。</P>
<P style="TEXT-INDENT: 2em">这个block1的人就围着multiprocessor1这张桌子坐，但是这张桌子也不是太大，所以block里面的人又分成32个人一组（warp）来吃饭。那如果这个block没有32个人，就占不完这张桌子，所以一个multiprocessor又可以让别的block的thread一起来用餐。</P>
<P style="TEXT-INDENT: 2em">因为一次就把所有的register和shared memory分配给了所有的block的所有的thread。所以怎么样合理的少用register和shared memory就可以同时让更多的thread得到处理。</P>
<P style="TEXT-INDENT: 2em">讲了这么多，怎么来看自己的显卡那片军营如何拉？下面给出两种方法，一种是用一个小软件，GPU-Z的工具，这个工具可以得到很多显卡的参数，这个可以上网搜索到。</P>
<P style="TEXT-INDENT: 2em">第二种方法是用CUDA的命令，这个只是针对支持CUDA的显卡的，下面给一段代码：</P>
<P style="TEXT-INDENT: 2em">/********************************************************************</P>
<P style="TEXT-INDENT: 2em">*&nbsp; InitCUDA.cu</P>
<P style="TEXT-INDENT: 2em">*&nbsp; This is a init CUDA of the CUDA program.</P>
<P style="TEXT-INDENT: 2em">*********************************************************************/</P>
<P style="TEXT-INDENT: 2em">#include &lt;stdio.h&gt;</P>
<P style="TEXT-INDENT: 2em">#include &lt;stdlib.h&gt;</P>
<P style="TEXT-INDENT: 2em">#include &lt;cuda_runtime.h&gt; </P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">/************************************************************************/</P>
<P style="TEXT-INDENT: 2em">/* Init CUDA&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; */</P>
<P style="TEXT-INDENT: 2em">/************************************************************************/</P>
<P style="TEXT-INDENT: 2em">bool InitCUDA(void)</P>
<P style="TEXT-INDENT: 2em">{</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; int count = 0;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; int i = 0;</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; cudaGetDeviceCount(&amp;count);</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; if(count == 0) {</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fprintf(stderr, "There is no device. ");</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; }</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; for(i = 0; i &lt; count; i++) {</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cudaDeviceProp prop;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(cudaGetDeviceProperties(&amp;prop, i) == cudaSuccess) </P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("name:%s ",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; prop.name);</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("totalGlobalMem:%u ",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; prop.totalGlobalMem);</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("sharedMemPerBlock:%u ",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; prop.sharedMemPerBlock);</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("regsPerBlock:%d ",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; prop.regsPerBlock);</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("warpSize:%d ",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; prop.warpSize);</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("memPitch:%u ",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; prop.memPitch);</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("maxThreadsPerBlock:%d ",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; prop.maxThreadsPerBlock);</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("maxThreadsDim:x %d, y %d, z %d ",&nbsp;&nbsp;&nbsp; prop.maxThreadsDim[0],prop.maxThreadsDim[1],prop.maxThreadsDim[2]);</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("maxGridSize:x %d, y %d, z %d ",&nbsp;&nbsp;&nbsp; prop.maxGridSize[0],prop.maxGridSize[0],prop.maxGridSize[0]);</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("totalConstMem:%u ",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; prop.totalConstMem);</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("major:%d ",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; prop.major);</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("minor:%d ",&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; prop.minor);</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("clockRate:%d ",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; prop.clockRate);</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("textureAlignment:%u ",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; prop.textureAlignment);</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(prop.major &gt;= 1) {</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; }</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; if(i == count) {</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fprintf(stderr, "There is no device supporting CUDA 1.x. ");</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; }</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; cudaSetDevice(i);</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; printf("CUDA initialized. ");</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; return true;</P>
<P style="TEXT-INDENT: 2em">}</P>
<P style="TEXT-INDENT: 2em"></P></div>]]></description>
	    <author><![CDATA[pannsp]]></author>
	    <comments>http://pannsp.blog.163.com/blog/static/92023200991675920495</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://pannsp.blog.163.com/blog/static/92023200991675920495</guid>
    <pubDate>Fri, 16 Oct 2009 19:59:20 +0800</pubDate>
    <dcterms:modified>2009-10-16T19:59:20+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[(CUDA 编程2).CUDA 线程执行模型分析（二）军民未动粮草先行（转） ]]></title>	
    <link>http://pannsp.blog.163.com/blog/static/92023200991674641295</link>
    <description><![CDATA[<div><P style="TEXT-INDENT: 2em">（CUDA编程2）CUDA 线程执行模型分析（二）大军未动粮草先行</P>
<P style="TEXT-INDENT: 2em">------GPU的革命 </P>
<P style="TEXT-INDENT: 2em">书接上回--<A href="http://www.hpctech.com/2009/0818/198.html">《CUDA 线程执行模型分析（一）招兵------ GPU的革命</A><A href="http://www.hpctech.com/2009/0818/198.html">》</A>，经过几天的征兵，将士也招了差不多五六千人，五六千人，就是五六千张嘴，得吃饭。这和我们的CUDA内存模型又有什么相识啦？或许有很多人都参加过军训，知道军队里面吃饭吗？到吃饭的时间，各处训练的队伍都拉到食堂前面。先到的就先进去吃，后到的就在外面拉歌，等待下一波，这里我们暂时还不讲解怎么安排每一对吃饭。我们先讲讲每一个Grid手下的兄弟们吃饭都需要那些家伙。下面来看看站长图：</P>
<P style="TEXT-INDENT: 2em"><IMG title="(CUDA 编程2).CUDA 线程执行模型分析（二）军民未动粮草先行（转）  - pannsp - Pan Ant" alt="(CUDA 编程2).CUDA 线程执行模型分析（二）军民未动粮草先行（转）  - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0819/20090819120454203.jpg" border=0>这里我们有一个block的士兵（16个人）在吃饭，每个人有一双筷子（Registers），一个碗（local memory），大家公用一张桌子（Block 里面的shared memory）。记得军训那年，在河北宣化，八个人一张桌子，一起抢桌子上的吃的时候，那个景象。训练了一天，一个个都是如狼似虎的，嘴巴里面咬着，筷子里面夹着，碗里面盛着，研究还看着桌子上的那几根鱼骨头（鱼肉早就进肚子里面了- -！）那个时候就看谁的筷子夹得多，碗装得多了。不过再多，筷子就那么长，碗就那么大～咱们再来看看block的情况：</P>
<P style="TEXT-INDENT: 2em">如图：&nbsp;<IMG title="(CUDA 编程2).CUDA 线程执行模型分析（二）军民未动粮草先行（转）  - pannsp - Pan Ant" alt="(CUDA 编程2).CUDA 线程执行模型分析（二）军民未动粮草先行（转）  - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0819/20090819120454273.jpg" border=0>……不需要注释了吧……</P>
<P style="TEXT-INDENT: 2em">餐厅里面肯定不止是一张桌子了，我已经不记得当时军训的时候，那个军营的食堂有多少个桌子，不过我们吃饭的时候可以装下几个连一起吃饭……想想那吃饭的阵势……</P>
<P style="TEXT-INDENT: 2em">哦，对了，我还记得在食堂里面有几个大桶，那玩儿应该不能称之为桶了，太大了 - -！可以分为三类，一类是装菜的，一类是装饭的，一类是装水果的。这几个桶都是大家可以共用的，记得有好吃的菜，大家都最先吃完那个菜，然后再跑到中间这个几个桶的地方找这个菜，装完一份跑到桌子那边一起分享……很是怀念那段日子。 Long long ago了……</P>
<P style="TEXT-INDENT: 2em">我能回想起来的那个时候的饭堂，如图（一个大的Grid吃饭图……）：</P>
<P style="TEXT-INDENT: 2em"><IMG title="(CUDA 编程2).CUDA 线程执行模型分析（二）军民未动粮草先行（转）  - pannsp - Pan Ant" alt="(CUDA 编程2).CUDA 线程执行模型分析（二）军民未动粮草先行（转）  - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0819/20090819120455684.jpg" border=0></P>
<P style="TEXT-INDENT: 2em">CUDA的内存模型也就这个样子了～</P>
<P style="TEXT-INDENT: 2em">这些都是我们承诺给士兵的，每个人都要吃上饭。但是实际上我记得食堂再大，也不能一下子装下两个团的人吃饭，那个时候都是每个团里面的几个连队先吃饭，然后后到的就在外面拉歌，等里面的吃好了，后面的再进去吃……不是每个食堂都像北大的“万人坑”那样能装1w人- -实地考察，好像要是真装了一w人，大家也就不用坐着吃饭了～- -</P>
<P style="TEXT-INDENT: 2em">对应手册上的第二章，运行模型就讲完了，接下来就要把队伍拉出来练习练习了……</P>
<P style="TEXT-INDENT: 2em">对了，我们的SIMD还没讲啦～队伍人数那么多，现在就那么几杆枪～怎么练习，还没讲啦……接下来的章节更精彩……</P>
<P style="TEXT-INDENT: 2em">PS: 题外话，或许是小的时候喜欢画图吧～现在还记得小学的美术老师给我们上的第一堂美术课，在黑板上画的正方形。在那个时候好像脑子里面就可以看穿物体一样，看到物体的这一面，就可以在大脑里面勾画出他的另一面～记得大学的时候参加定向越野，看到地图的时候，第一感觉，图形都好熟悉，然后再和实际的物体一对照，都是那么清晰，看到一栋建筑物，一对照地图，就可以知道他的后面是什么样，仿佛整个地图就在脑子里面构建起了地形图～呵呵很是奇妙……不知道大家有没有这样感觉的时候……</P></div>]]></description>
	    <author><![CDATA[pannsp]]></author>
	    <comments>http://pannsp.blog.163.com/blog/static/92023200991674641295</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://pannsp.blog.163.com/blog/static/92023200991674641295</guid>
    <pubDate>Fri, 16 Oct 2009 19:46:41 +0800</pubDate>
    <dcterms:modified>2009-10-16T19:46:41+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[(CUDA 编程1).CUDA 线程执行模型分析（一）招兵 ------ GPU的革命(转) ]]></title>	
    <link>http://pannsp.blog.163.com/blog/static/92023200991645858498</link>
    <description><![CDATA[<div><P style="TEXT-INDENT: 2em">（CUDA 编程1） CUDA 线程执行模型分析（一）招兵</P>
<P style="TEXT-INDENT: 2em">------ GPU的革命</P>
<P style="TEXT-INDENT: 2em">&nbsp;序：或许看到下面的内容的时候，你会觉得和传统的讲解线程，和一些讲解计算机的书的内容不是很相同。我倒觉得有关计算机，编程这些方面的内容，并不都是深奥难懂的，再深奥难懂的事情，其实本质上也是很简单的。一直以为计算机编程就像小时候搭建积木一样，只要知道游戏规则，怎么玩就看你自己了。或许是从小学那会，就喜欢在做数学题的时候用一些简便方法来解题，养成了一些习惯，喜欢 把复杂的问题都会尝试用最简单的额方法来解决，而不喜欢把简单的问题弄得很复杂。不再多说了，有的朋友已经看得不耐烦了……ps：再罗嗦一句，如果下面看不明白的，就当小说看了，要是觉得不像小说，那就当故事看，要是觉得故事不完整，写得太乱，那就当笑话看，在各位学习工作之余能博得大家一笑，也倍儿感荣幸……ps2：想好再说 ……突然想到了，确实是了一段时间再想到的，既然叫GPU革命，那就得招集队伍啊，下面我就开始招兵了。</P>
<P style="TEXT-INDENT: 2em">正题：</P>
<P style="TEXT-INDENT: 2em">要真正进入CUDA并行化开发，就必须先了解CUDA的运行模型，才能在这个基础上做并行程序的开发。</P>
<P style="TEXT-INDENT: 2em">CUDA在执行的时候是让host里面的一个一个的kernel按照线程网格（Grid）的概念在显卡硬件（GPU）上执行。每一个线程网格又可以包含多个线程块（block），每一个线程块中又可以包含多个线程（thread）。</P>
<P style="TEXT-INDENT: 2em">在这里我们可以拿古时候的军队作为一个例子来理解这里的程序执行模型。每一个线程，就相当于我们的每一个士兵，在没有当兵之前，大家都不知道自己做什么。当要执行某一个大的军事任务的时候，大将军发布命令，大家来要把对面的敌人部队的n个敌人消灭了。然后把队伍分成M个部分，每一个部分完成自己的工作，有的是做侦查的工作，有的是做诱敌的工作，有的是做伏击的工作，有的是做后备的工作，有的是做后勤的工作……反正把一个大任务按照不同的类别，不同的流程不同，分别由M个部分来完成。</P>
<P style="TEXT-INDENT: 2em">这里我们可以把大将军看着是Host，它把这次军事行动分解成一个一个的kernel：kernel_1，kernel_2……kernel_M，每一个kernel就交给每一个Grid（副将？千户？就看管的人多人少了，如果GPU硬件支持少一点，那就是千户；要是GPU硬件高级一些，管理的人多一些，那就副将？戚家军也不过四五千人，咱也不能太贪心，一下子就想统军百万，再说了，敢问世上韩信一样的将才又哪有那么多啦？）来完成。当要执行这些任务的时候，每一个Grid又把任务分成一部分一部分的，毕竟人太多，他一个官不过来，他只要管理几个团队中间的高级军官就可以了。Grid又把任务划分为一个个的Block（百户？），这里每一个Grid管理的Block也是有限的，(人就那么多……想管多少得看硬件的支持)。毕竟显卡上的GPU硬件还是很少，Thread（线程）相对于真正的军队来说人还是少了很多。所以到Block这个层的时候，就直接管理每一个Thread（士兵）。</P>
<P style="TEXT-INDENT: 2em">由于古代通信不是很方便（从GPU的发展史来看，如果按照中国的历史，现在的GPU也就还处在战国时代吧……），所以每一个Block（百户）内部的Thread（士兵）才能方便的通信，按照既定的规则进行同步；而各个block之间就没那么方便了，大家不能互相通讯。不过同一个（千户）Grid管理的block之间是共享同一个任务分配的资源的。每一个Grid都可以从大将军那里分配到一些任务，和一些粮食，同一个Grid的block都可以分到这个Grid分配到的粮食。而每一个（千户）Grid本身的任务就不一样，所以Grid除了知道自己做的事情外，其他的Grid他都不会知道了。----这差不多就是一个运行模型。下面让我们来看看在GPU中东图例说明：</P>
<P style="TEXT-INDENT: 2em"><IMG title="(CUDA 编程1).CUDA 线程执行模型分析（一）招兵 ------ GPU的革命(转)  - pannsp - Pan Ant" alt="(CUDA 编程1).CUDA 线程执行模型分析（一）招兵 ------ GPU的革命(转)  - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0818/20090818112020944.jpg" border=0>看到这张图，我们可以对应来讲解我们的Thread部队。一个大将军Host，分配了任务中的两个任务（Kernel1, Kernel2）给了千户（Grid1，Grid2）来完成。千户Grid1里面把自己的队伍分成了6个百户Block，然后每一个百户又把任务分配给了自己的士兵（Thread）来具体完成。这里得说明的是，由于千户拿到的任务Kernel是定了的，所以到每个士兵（Thread）也就那里就只会埋头做同样的事情（就像戚继光招的兵：在胡宗宪的幕僚郑若曾所著的《江南经略》中，有着这样一份详细的招生简章，如果不服气，大可以去对照一下： 凡选入军中之人，以下几等人不可用，在市井里混过的人不能用，喜欢花拳绣腿的人不能用，年纪过四十的人不能用，在政府机关干过的人不能用。 以上尚在其次，更神奇的要求还在下面： 喜欢吹牛、高谈阔论的人不能用，胆子小的人不能用，长得白的人不能用，为保证队伍的心理健康，性格偏激（偏见执拗）的人也不能用。……概括起来，戚继光要找的是这样一群人：四肢发达，头脑简单，为人老实，遵纪守法服从政府，敢打硬仗，敢冲锋不怕死，具备二愣子性格的肌肉男。----《明朝那些事儿》）。</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">为了方便统一管理，大家都去掉了自己的名字，按照Grid1，Block（x，y），Thread（x，y）这样的编号来称呼每一个Thread士兵。如果你要找到某一个Thread，你就跑到军营里面大叫：喂，Grid1手下的Block1管理的三排第二个Thread（1，2）出来。对于每个士兵自己来说，他要知道自己的位置，就得知道自己的长官都是谁。Thread（1，2）要知道自己再整个Grid手下算第几个兵（钢七年第……个兵），当Grid1叫到他的号了，他得马上回答：我在Block（1，1）的编号是： </P>
<P style="TEXT-INDENT: 2em">unsigned int xIndex = blockDim.x * blockIdx.x + threadIdx.x;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; unsigned int yIndex = blockDim.y * blockIdx.y + threadIdx.y;</P>
<P style="TEXT-INDENT: 2em">如果要得到线性的编号，我们可以自己算一下：这个士兵是在Grid部队下的第xIndex行，yIndex列站着如果要得到线性的编号，我们可以自己算一下：这个士兵是在Grid部队下的第xIndex行，yIndex列站着（这里我们必须注意：blockDim.x （这里为5），blockDim.y（这里为3）不是block的坐标，这里是block的size，切记！）。如果从第一个士兵哪里算是线性编号0，那他的线性编号就是</P>
<P style="TEXT-INDENT: 2em">unsigned int index = xIndex（6） + size_x * yIndex（5）; 这里的size_x就是一行一共有多少个士兵（Thread），例如上图，这里一行有3个block每一个block里面的每一行有5个Thread，所以size_x就应该为3×5=15，一个Grid的一行有15个士兵，那刚才叫道的那个人的线性编号就应该是……还要我算吗？如果不得81，自己再算一下……（编号是从0，开始的）计算过程：index = 6+5×15=81；</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">说了那么多，咱也不能光说不练假把式。下面给一个简单的Thread 测试的Demo。本来打算把整个代码都copy过来，但是考虑到又会被别人copy出去，这样copy来copy去，在编程中很容易出现错误，所以作者也不提倡在做编程中直接copy代码，这样很危险的，很多自己都不知道的bug就隐藏在copy的代码当中……so，截图……</P>
<P style="TEXT-INDENT: 2em"><IMG title="(CUDA 编程1).CUDA 线程执行模型分析（一）招兵 ------ GPU的革命(转)  - pannsp - Pan Ant" alt="(CUDA 编程1).CUDA 线程执行模型分析（一）招兵 ------ GPU的革命(转)  - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0818/20090818112020387.jpg" border=0>作者这里做了一个简单的测试，测试了512个线程。这里只有一个grid，从这一个grid里面也只分了一个bock：</P>
<P style="TEXT-INDENT: 2em">dim3&nbsp;&nbsp; grid(size_x / BLOCK_DIM, 1);--》&nbsp;&nbsp; dim3&nbsp;&nbsp; grid(1, 1);一个Grid里面一个blcok</P>
<P style="TEXT-INDENT: 2em">dim3&nbsp;&nbsp; block(BLOCK_DIM, 1, 1);--》dim3 block(512, 1, 1);一个Block里面分配512个Thread；</P>
<P style="TEXT-INDENT: 2em">这里的每一个任务kernel就是：</P>
<P style="TEXT-INDENT: 2em">__global__ static void ThreadDemo1(unsigned int* ret)</P>
<P style="TEXT-INDENT: 2em">{</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; unsigned int xIndex = blockDim.x * blockIdx.x + threadIdx.x;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; unsigned int yIndex = blockDim.y * blockIdx.y + threadIdx.y;</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; if(xIndex &lt; size_x &amp;&amp; yIndex &lt; size_y)</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; {</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; unsigned int index = xIndex + size_x * yIndex;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ret[index] = xIndex;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ret[index + size_x*size_y] = yIndex;</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; }</P>
<P style="TEXT-INDENT: 2em">}</P>
<P style="TEXT-INDENT: 2em">计算自己的线性id然后把自己的坐标写入到线性id对应的数组里面。Ps：说明一下，这个记录id坐标的数组ret[],ret的前一半记录的是线程的x坐标，后一般是记录的y坐标。PS2：题外话，cu是C的扩展，这里的const定义的常量的用法在ANSIC C里面是行不通的，但是在C++中是可用的。</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">每个任务kernel都说好了，然后就是host下达命令</P>
<P style="TEXT-INDENT: 2em">&nbsp;&nbsp; ThreadDemo1&lt;&lt;&lt;grid,block&gt;&gt;&gt;(ret);</P>
<P style="TEXT-INDENT: 2em">来运行程序。所有的士兵都开始工作了，把自己的坐标x，y写入到ret数组里面。</P>
<P style="TEXT-INDENT: 2em">下面是得到的结果：</P>
<P style="TEXT-INDENT: 2em">(0,0) (1,0) (2,0) (3,0) (4,0) (5,0) (6,0) (7,0) (8,0) (9,0) (10,0) (11,0) (12,0) (13,0) (14,0) (15,0) (16,0) (17,0) (18,0) (19,0) (20,0) (21,0) (22,0) (23,0) (24,0) (25,0) (26,0) (27,0) (28,0) (29,0) (30,0) (31,0) (32,0) (33,0) (34,0) (35,0) (36,0) (37,0) (38,0) (39,0) (40,0) (41,0) (42,0) (43,0) (44,0) (45,0) (46,0) (47,0) (48,0) (49,0) (50,0) (51,0) (52,0) (53,0) (54,0) (55,0) (56,0) (57,0) (58,0) (59,0) (60,0) (61,0) (62,0) (63,0) (64,0) (65,0) (66,0) (67,0) (68,0) (69,0) (70,0) (71,0) (72,0) (73,0) (74,0) (75,0) (76,0) (77,0) (78,0) (79,0) (80,0) (81,0) (82,0) (83,0) (84,0) (85,0) (86,0) (87,0) (88,0) (89,0) (90,0) (91,0) (92,0) (93,0) (94,0) (95,0) (96,0) (97,0) (98,0) (99,0) (100,0) (101,0) (102,0) (103,0) (104,0) (105,0) (106,0) (107,0) (108,0) (109,0) (110,0) (111,0) (112,0) (113,0) (114,0) (115,0) (116,0) (117,0) (118,0) (119,0) (120,0) (121,0) (122,0) (123,0) (124,0) (125,0) (126,0) (127,0) (128,0) (129,0) (130,0) (131,0) (132,0) (133,0) (134,0) (135,0) (136,0) (137,0) (138,0) (139,0) (140,0) (141,0) (142,0) (143,0) (144,0) (145,0) (146,0) (147,0) (148,0) (149,0) (150,0) (151,0) (152,0) (153,0) (154,0) (155,0) (156,0) (157,0) (158,0) (159,0) (160,0) (161,0) (162,0) (163,0) (164,0) (165,0) (166,0) (167,0) (168,0) (169,0) (170,0) (171,0) (172,0) (173,0) (174,0) (175,0) (176,0) (177,0) (178,0) (179,0) (180,0) (181,0) (182,0) (183,0) (184,0) (185,0) (186,0) (187,0) (188,0) (189,0) (190,0) (191,0) (192,0) (193,0) (194,0) (195,0) (196,0) (197,0) (198,0) (199,0) (200,0) (201,0) (202,0) (203,0) (204,0) (205,0) (206,0) (207,0) (208,0) (209,0) (210,0) (211,0) (212,0) (213,0) (214,0) (215,0) (216,0) (217,0) (218,0) (219,0) (220,0) (221,0) (222,0) (223,0) (224,0) (225,0) (226,0) (227,0) (228,0) (229,0) (230,0) (231,0) (232,0) (233,0) (234,0) (235,0) (236,0) (237,0) (238,0) (239,0) (240,0) (241,0) (242,0) (243,0) (244,0) (245,0) (246,0) (247,0) (248,0) (249,0) (250,0) (251,0) (252,0) (253,0) (254,0) (255,0) (256,0) (257,0) (258,0) (259,0) (260,0) (261,0) (262,0) (263,0) (264,0) (265,0) (266,0) (267,0) (268,0) (269,0) (270,0) (271,0) (272,0) (273,0) (274,0) (275,0) (276,0) (277,0) (278,0) (279,0) (280,0) (281,0) (282,0) (283,0) (284,0) (285,0) (286,0) (287,0) (288,0) (289,0) (290,0) (291,0) (292,0) (293,0) (294,0) (295,0) (296,0) (297,0) (298,0) (299,0) (300,0) (301,0) (302,0) (303,0) (304,0) (305,0) (306,0) (307,0) (308,0) (309,0) (310,0) (311,0) (312,0) (313,0) (314,0) (315,0) (316,0) (317,0) (318,0) (319,0) (320,0) (321,0) (322,0) (323,0) (324,0) (325,0) (326,0) (327,0) (328,0) (329,0) (330,0) (331,0) (332,0) (333,0) (334,0) (335,0) (336,0) (337,0) (338,0) (339,0) (340,0) (341,0) (342,0) (343,0) (344,0) (345,0) (346,0) (347,0) (348,0) (349,0) (350,0) (351,0) (352,0) (353,0) (354,0) (355,0) (356,0) (357,0) (358,0) (359,0) (360,0) (361,0) (362,0) (363,0) (364,0) (365,0) (366,0) (367,0) (368,0) (369,0) (370,0) (371,0) (372,0) (373,0) (374,0) (375,0) (376,0) (377,0) (378,0) (379,0) (380,0) (381,0) (382,0) (383,0) (384,0) (385,0) (386,0) (387,0) (388,0) (389,0) (390,0) (391,0) (392,0) (393,0) (394,0) (395,0) (396,0) (397,0) (398,0) (399,0) (400,0) (401,0) (402,0) (403,0) (404,0) (405,0) (406,0) (407,0) (408,0) (409,0) (410,0) (411,0) (412,0) (413,0) (414,0) (415,0) (416,0) (417,0) (418,0) (419,0) (420,0) (421,0) (422,0) (423,0) (424,0) (425,0) (426,0) (427,0) (428,0) (429,0) (430,0) (431,0) (432,0) (433,0) (434,0) (435,0) (436,0) (437,0) (438,0) (439,0) (440,0) (441,0) (442,0) (443,0) (444,0) (445,0) (446,0) (447,0) (448,0) (449,0) (450,0) (451,0) (452,0) (453,0) (454,0) (455,0) (456,0) (457,0) (458,0) (459,0) (460,0) (461,0) (462,0) (463,0) (464,0) (465,0) (466,0) (467,0) (468,0) (469,0) (470,0) (471,0) (472,0) (473,0) (474,0) (475,0) (476,0) (477,0) (478,0) (479,0) (480,0) (481,0) (482,0) (483,0) (484,0) (485,0) (486,0) (487,0) (488,0) (489,0) (490,0) (491,0) (492,0) (493,0) (494,0) (495,0) (496,0) (497,0) (498,0) (499,0) (500,0) (501,0) (502,0) (503,0) (504,0) (505,0) (506,0) (507,0) (508,0) (509,0) (510,0) (511,0) </P>
<P style="TEXT-INDENT: 2em">一目了然，正是我们定义的512个线程。</P>
<P style="TEXT-INDENT: 2em">现在大家应该知道CUDA是怎么运行了？0 – 0 要是还不清楚，跟贴问我吧 - - ！</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">好了，现在招兵的任务已经完成了，兵是招来了，下面就改训练了，不过天时已晚，大家还是吃饱喝足，好好的休息一下吧～</P>
<P style="TEXT-INDENT: 2em">明天继续GPU的革命。</P>
<P style="TEXT-INDENT: 2em">且听：《沙家浜》里胡傳魁已經出場了：十幾個人來七八條槍，不是很威風嗎？----我可要说十几个人怎么来分这七八条枪。</P>
<P style="TEXT-INDENT: 2em">士兵们执行任务了，是一哄而上，还是……且听SIMD- -！</P>
<P style="TEXT-INDENT: 2em">&nbsp;</P>
<P style="TEXT-INDENT: 2em">PS:上面在输出结果的时候用了一个小技巧，或许有的人知道，就不用看了，有不知道的，接着看下去，或许可以方便你以后的调试：）</P>
<P style="TEXT-INDENT: 2em"><IMG title="(CUDA 编程1).CUDA 线程执行模型分析（一）招兵 ------ GPU的革命(转)  - pannsp - Pan Ant" alt="(CUDA 编程1).CUDA 线程执行模型分析（一）招兵 ------ GPU的革命(转)  - pannsp - Pan Ant" src="http://www.hpctech.com/uploadfile/2009/0818/20090818112020486.jpg" border=0>再输出的时候，在系统debugging信息里面的command Arguments里面用一个管道输出到一个1.txt文件里面。管道用法大家自己可以到网上查～我就不用讲解了。也很简单，就是在你的哦xx.exe 后面加一个 “&gt;” 管道，对了还有 “&lt;” 管道，呵呵：） 一个是输出，一个是输入。</P></div>]]></description>
	    <author><![CDATA[pannsp]]></author>
	    <comments>http://pannsp.blog.163.com/blog/static/92023200991645858498</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://pannsp.blog.163.com/blog/static/92023200991645858498</guid>
    <pubDate>Fri, 16 Oct 2009 16:58:58 +0800</pubDate>
    <dcterms:modified>2009-10-16T20:01:20+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[Win32 OpenGL编程---寻找缺失的OpenGL函数(转)]]></title>	
    <link>http://pannsp.blog.163.com/blog/static/920232009915516411</link>
    <description><![CDATA[<div><P>一、&nbsp;&nbsp; 提要<BR>以前在《 Win32 OpenGL 编程（1）Win32下的OpenGL编程必须步骤》一文中提供了一个较为完整的Win32下编写OpenGL程序的框架，但是仅仅提到的是此Win32框架所需要的一些东西，事实上由于MS为了维护自家的DirectX，所以很早就放弃了对OpenGL的支持，在Windows XP平台上实现的OpenGL还是1.1版(新版的Windows看资料好像会好一点，但是事实上OpenGL最新版已经是3.2了)。本文讲述的就是使用本机显卡支持的最新的OpenGL所需要的东西，顺便还讲了一下GLUT，这样基本形成一个较为完整的Win32下的OpenGL变成学习环境。由于工作很简单，所以本文会很短，最长的可能就是提要^^</P>
<P>另外，事实上参考资料4中的文章已经较为详细的介绍了本文所要讲的大部分内容，大家可以直接去看，写作此文的目的主要还是希望此系列完整，在后面的文章中已经有个完整可用的编程环境。顺面补上glew和glut简单的安装使用说明。</P>
<P>&nbsp;</P>
<P>二、&nbsp;&nbsp; 寻找缺失的OpenGL函数<BR>OpenGL是由显卡直接支持并实现的，当时在设计的时候考虑到显卡厂商可能会比操作系统/开发软件制造商先行一步提供新的扩展，所以有提供获取新的扩展的方法，在Windows下就是利用wglGetProcAddress函数：</P>
<P>PROC wglGetProcAddress(</P>
<P>&nbsp; LPCSTR&nbsp; lpszProc&nbsp;&nbsp; // name of the extension function</P>
<P>);</P>
<P>此函数与GetProcAddress功能类似，实现也类似，无非就是从动态库中通过名字直接获取到函数的地址。</P>
<P>FARPROC WINAPI GetProcAddress(</P>
<P>&nbsp; __in&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; HMODULE hModule,</P>
<P>&nbsp; __in&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LPCSTR lpProcName</P>
<P>);</P>
<P>&nbsp;</P>
<P>因为wglGetProcAddress很明确的知道该从哪个Dll获取函数，所以省略了第一参数模块名，仅仅通过函数名就能获取到对应的函数地址，使用方法也是类似，通过函数名获取到函数地址后，通过强制转换的方式使用。此方法虽然可行，但是用到大量函数的时候比较麻烦。事实上，此方法提供的时候是作为一种补充的手段，是在显卡生厂商走在操作系统和开发工具提供商前面时的一种暂时应付手段，但是，很不幸的是，Windows是在落后太多了。。。。。。。。</P>
<P>&nbsp;</P>
<P>三、&nbsp;&nbsp; Windows下使用OpenGL的救星<BR>MS当年作为OpenGL的发起人之一，仅仅对OpenGL支持到1.1，然后就退出了OpenGL组织，自己发展DirectX系统去了，我们要在Windows上使用OpenGL光靠MS的话，那么OpenGL 1.1后的所有函数都得通过上述手段来完成，那简直是让人吐血的事情-_-!</P>
<P>以前似乎有关官方（SGI）的OpenGL SDK下载，但是现在没有那么好了，OpenGL的官方网站已经明确说明，不再提供SDK下载，叫咱们自己到其他网站上找去-_-!在OpenGL.org上的OpenGL SDK中的Library为我们提供了几个开源项目，其中有两个就是与扩展有关的。</P>
<P>GLee is a free cross-platform extension loading library that takes the burden off your application. GLee makes it easy to check for OpenGL extension and core version availability, automatically setting up the entry points with no effort on your part.</P>
<P>GLEW is an open-source cross-platform extension loading library with thread-safe support for multiple rendering contexts and automatic co<wbr>de generation capability. GLEW provides easy-to-use and efficient methods for checking OpenGL extensions and core functionality.</P>
<P>我选择了GLEW，这个有线程安全支持的项目，大家各取所需吧。不是开源社区的努力，要在Windows下使用OpenGL还真是让人吐血了-_-!说是救星也不为过。</P>
<P>1.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; glew的安装<BR>上面的链接地址有下载，glew有提供Windows的二进制版本，使用上可以直接在工程中包含头文件，库，然后在程序的运行目录包含动态库，因为考虑到别人不一定下载了glew，按道理来说不论是源代码分发还是可执行文件的分发最好都是利用此方式使用。但是作为学习来说，要在每个工程中都做这么多额外的工作比较让人受不了，所以推荐讲头文件放在windows SDK的include/GL目录下（可以通过找到gl/gl.h的位置找到），lib放在windows SDK的lib目录下，动态库放到windows/system32这个系统目录下，那么以后的OpenGL工程就不需要额外的配置了，以后文章的工程都是在这个环境下建立的。。。。要运行程序要编译代码请首先搭建好环境。。。。。真是方便了我一个，辛苦了大家了^^-_-!</P>
<P>&nbsp;</P>
<P>2.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; glew的使用<BR>以后在使用OpenGL的时候，都请通过下述代码</P>
<P>#include &lt;GL/glew.h&gt;</P>
<P>#include &lt;GL/wglew.h&gt;</P>
<P>包含必要的头文件</P>
<P>通过</P>
<P>#pragma comment( lib, "glew32.lib" )&nbsp; </P>
<P>#pragma comment( lib, "glew32s.lib" )&nbsp; </P>
<P>&nbsp;</P>
<P>然后，在初始化OpenGL的时候，不要忘了将glew初始化，可以这样：</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GLenum err = glewInit();</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(err != GLEW_OK)</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(-2);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</P>
<P>这样就能使用缺失的函数，就像它们原来就被操作系统支持那样，而不用自己通过wglGetProcAddress去获取了，说是能像原来就被操作系统支持的那样使用其实不完成正确，起码在VS中，由于都是通过强转后的函数指针使用，VS和VA都不能正确的解析其函数原型，使得代码编写时智能提醒函数原型的功能不能正常使用，这也算是个小瑕疵吧。。。。。。。。。</P>
<P>&nbsp;</P>
<P>四、&nbsp;&nbsp; 为了红宝书《OpenGL Programming Guide》安装glut<BR>事实上，在《 Win32 OpenGL 编程（1）Win32下的OpenGL编程必须步骤》中我就提到了不想使用glut的窗口管理系统，但是想完整的学习《OpenGL Programming Guide》要完全避过glut好像比较难，就算不想使用其窗口管理系统，但是一些简单易用的快捷函数要是不用，那就太麻烦了。。。。。我想画个立方体都得先去想办法才行-_-!茶壶那就更。。。。。无语了。这里将glut的安装使用顺面也讲一下吧。</P>
<P>glut在<A href="http://www.opengl.org/resources/libraries/glut/">http://www.opengl.org/resources/libraries/glut/</A> 上有介绍，下载页是：<A href="http://www.opengl.org/resources/libraries/glut/glut_downloads.php">http://www.opengl.org/resources/libraries/glut/glut_downloads.php</A></P>
<P>也有Windows的二进制版本下载，安装方法同glew,拷贝头文件，库，动态库到相应的位置，使用方法嘛。。。见红宝书-_-!呵呵，其实glut包含了平台相关的头文件和gl.h,glu.h，所以在使用的时候包含glut.h一个文件就行了。</P>
<P>包含了glut后，基本上Win32下OpenGL的工程可以以下面的代码开始了：</P>
<P>#include &lt;GL/glew.h&gt;</P>
<P>#include &lt;GL/wglew.h&gt;</P>
<P>#include &lt;GL/glut.h&gt;</P>
<P>&nbsp;</P>
<P>#pragma comment( lib, "opengl32.lib" ) </P>
<P>#pragma comment( lib, "glu32.lib" )&nbsp; </P>
<P>#pragma comment( lib, "glew32.lib" )&nbsp; </P>
<P>#pragma comment( lib, "glew32s.lib" )&nbsp; </P>
<P>#pragma comment( lib, "glut32.lib" )&nbsp; </P>
<P>&nbsp;</P>
<P>这些都是必须的。（事实上由于glut32.lib会自动的包含OpenGL核心所需的库，包含glut32.lib时可以省略opengl32.lib和glu32.lib）</P>
<P>&nbsp;</P>
<P>&nbsp;</P>
<P>五、&nbsp;&nbsp; 完整的示例<BR>这里提供一个使用了glew，glut的完整示例，这个示例在上述两个库都安装完成工程配置完成的时候才能正确运行，也可以用此示例来检验安装，完整的示例从《OpenGL 编程指南》（《OpenGL Programming Guide》）的示例代码mvarray.c改过来的。其中的宏GL_VERSION_1_3是由glew定义用来表示支持OpenGL1.3版本，其中的函数glMultiDrawElementsEXT就是Windows本身不支持，用了glew后才能使用的OpenGL扩展接口（首先要显卡支持啊）。glut的使用那就更不用说了。完整的示例在博客示例代码的2009-9-29\TestOpenGL目录下可以找到，下载方式见本文最后的说明。</P>
<P>&nbsp;</P>
<P>/*</P>
<P>&nbsp;* Copyright (c) 1993-2003, Silicon Graphics, Inc.</P>
<P>&nbsp;* All Rights Reserved</P>
<P>&nbsp;*</P>
<P>&nbsp;* Permission to use, copy, modify, and distribute this software for any</P>
<P>&nbsp;* purpose and without fee is hereby granted, provided that the above</P>
<P>&nbsp;* copyright notice appear in all copies and that both the copyright</P>
<P>&nbsp;* notice and this permission notice appear in supporting documentation,</P>
<P>&nbsp;* and that the name of Silicon Graphics, Inc. not be used in</P>
<P>&nbsp;* advertising or publicity pertaining to distribution of the software</P>
<P>&nbsp;* without specific, written prior permission.</P>
<P>&nbsp;*</P>
<P>&nbsp;* THE MATERIAL EMBODIED ON THIS SOFTWARE IS PROVIDED TO YOU "AS-IS" AND</P>
<P>&nbsp;* WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE,</P>
<P>&nbsp;* INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR</P>
<P>&nbsp;* FITNESS FOR A PARTICULAR PURPOSE.&nbsp; IN NO EVENT SHALL SILICON</P>
<P>&nbsp;* GRAPHICS, INC.&nbsp; BE LIABLE TO YOU OR ANYONE ELSE FOR ANY DIRECT,</P>
<P>&nbsp;* SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,</P>
<P>&nbsp;* OR ANY DAMAGES WHATSOEVER, INCLUDING WITHOUT LIMITATION, LOSS OF</P>
<P>&nbsp;* PROFIT, LOSS OF USE, SAVINGS OR REVENUE, OR THE CLAIMS OF THIRD</P>
<P>&nbsp;* PARTIES, WHETHER OR NOT SILICON GRAPHICS, INC.&nbsp; HAS BEEN ADVISED OF</P>
<P>&nbsp;* THE POSSIBILITY OF SUCH LOSS, HOWEVER CAUSED AND ON ANY THEORY OF</P>
<P>&nbsp;* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE POSSESSION, USE</P>
<P>&nbsp;* OR PERFORMANCE OF THIS SOFTWARE.</P>
<P>&nbsp;*</P>
<P>&nbsp;* US Government Users Restricted Rights </P>
<P>&nbsp;* Use, duplication, or disclosure by the Government is subject to</P>
<P>&nbsp;* restrictions set forth in FAR 52.227.19(c)(2) or subparagraph</P>
<P>&nbsp;* (c)(1)(ii) of the Rights in Technical Da<wbr>ta and Computer Software</P>
<P>&nbsp;* clause at DFARS 252.227-7013 and/or in similar or successor clauses</P>
<P>&nbsp;* in the FAR or the DOD or NASA FAR Supplement.&nbsp; Unpublished - rights</P>
<P>&nbsp;* reserved under the copyright laws of the United States.</P>
<P>&nbsp;*</P>
<P>&nbsp;* Contractor/manufacturer is:</P>
<P>&nbsp;*&nbsp;&nbsp;&nbsp; Silicon Graphics, Inc.</P>
<P>&nbsp;*&nbsp;&nbsp;&nbsp; 1500 Crittenden Lane</P>
<P>&nbsp;*&nbsp;&nbsp;&nbsp; Mountain View, CA&nbsp; 94043</P>
<P>&nbsp;*&nbsp;&nbsp;&nbsp; United State of America</P>
<P>&nbsp;*</P>
<P>&nbsp;* OpenGL(R) is a registered trademark of Silicon Graphics, Inc.</P>
<P>&nbsp;*/</P>
<P>&nbsp;</P>
<P>/*</P>
<P>&nbsp;*&nbsp; mvarray.c</P>
<P>&nbsp;*&nbsp; This program demonstrates multiple vertex arrays,</P>
<P>&nbsp;*&nbsp; specifically the OpenGL routine glMultiDrawElements().</P>
<P>&nbsp;*/</P>
<P>#include &lt;GL/glew.h&gt;</P>
<P>#include &lt;GL/wglew.h&gt;</P>
<P>#include &lt;GL/glut.h&gt;</P>
<P>#include &lt;stdio.h&gt;</P>
<P>&nbsp;</P>
<P>#pragma comment( lib, "opengl32.lib" ) </P>
<P>#pragma comment( lib, "glu32.lib" )&nbsp; </P>
<P>#pragma comment( lib, "glew32.lib" )&nbsp; </P>
<P>#pragma comment( lib, "glew32s.lib" )&nbsp; </P>
<P>#pragma comment( lib, "glut32.lib" )&nbsp; </P>
<P>&nbsp;</P>
<P>&nbsp;</P>
<P>#ifdef GL_VERSION_1_3</P>
<P>&nbsp;</P>
<P>void setupPointer(void)</P>
<P>{</P>
<P>&nbsp;&nbsp; static GLint vertices[] = {25, 25,</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 75, 75,</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 100, 125,</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 150, 75,</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 200, 175,</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 250, 150,</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 300, 125,</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 100, 200,</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 150, 250,</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 200, 225,</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 250, 300,</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 300, 250};</P>
<P>&nbsp;</P>
<P>&nbsp;&nbsp; glEnableClientState (GL_VERTEX_ARRAY);</P>
<P>&nbsp;&nbsp; glVertexPointer (2, GL_INT, 0, vertices);</P>
<P>}</P>
<P>&nbsp;</P>
<P>void init(void) </P>
<P>{</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GLenum err = glewInit();</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(err != GLEW_OK)</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(-2);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</P>
<P>&nbsp;</P>
<P>&nbsp;&nbsp; glClearColor (0.0, 0.0, 0.0, 0.0);</P>
<P>&nbsp;&nbsp; glShadeModel (GL_SMOOTH);</P>
<P>&nbsp;&nbsp; setupPointer ();</P>
<P>}</P>
<P>&nbsp;</P>
<P>void display(void)</P>
<P>{</P>
<P>&nbsp;&nbsp; static GLubyte on<wbr>eIndices[] = {0, 1, 2, 3, 4, 5, 6};</P>
<P>&nbsp;&nbsp; static const GLubyte twoIndices[] = {1, 7, 8, 9, 10, 11};</P>
<P>&nbsp;&nbsp; static GLsizei count[] = {7, 6};</P>
<P>&nbsp;&nbsp; static const GLvoid * indices[2] = {on<wbr>eIndices, twoIndices};</P>
<P>&nbsp;&nbsp; </P>
<P>&nbsp;&nbsp; glClear (GL_COLOR_BUFFER_BIT);</P>
<P>&nbsp;&nbsp; glColor3f (1.0, 1.0, 1.0);</P>
<P>&nbsp;</P>
<P>&nbsp;&nbsp; glMultiDrawElementsEXT (GL_LINE_STRIP, count, GL_UNSIGNED_BYTE,</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; indices, 2);</P>
<P>&nbsp;</P>
<P>&nbsp;</P>
<P>&nbsp;&nbsp; // 上面的一句相当于下面的两句</P>
<P>&nbsp;&nbsp; //glDrawElements(GL_LINE_STRIP, count[0], GL_UNSIGNED_BYTE, on<wbr>eIndices);</P>
<P>&nbsp;&nbsp; //glDrawElements(GL_LINE_STRIP, count[1], GL_UNSIGNED_BYTE, twoIndices);</P>
<P>&nbsp;</P>
<P>&nbsp;&nbsp; glFlush ();</P>
<P>}</P>
<P>&nbsp;</P>
<P>void reshape (int w, int h)</P>
<P>{</P>
<P>&nbsp;&nbsp; glViewport (0, 0, (GLsizei) w, (GLsizei) h);</P>
<P>&nbsp;&nbsp; glMatrixMode (GL_PROJECTION);</P>
<P>&nbsp;&nbsp; glLoadIdentity ();</P>
<P>&nbsp;&nbsp; gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);</P>
<P>}</P>
<P>&nbsp;</P>
<P>void keyboard(unsigned char key, int x, int y)</P>
<P>{</P>
<P>&nbsp;&nbsp; switch (key) {</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; case 27:</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit(0);</P>
<P>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break;</P>
<P>&nbsp;&nbsp; }</P>
<P>}</P>
<P>&nbsp;</P>
<P>int main(int argc, char** argv)</P>
<P>{</P>
<P>&nbsp;&nbsp; glutInit(&amp;argc, argv);</P>
<P>&nbsp;&nbsp; glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);</P>
<P>&nbsp;&nbsp; glutInitWindowSize (350, 350); </P>
<P>&nbsp;&nbsp; glutInitWindowPosition (100, 100);</P>
<P>&nbsp;&nbsp; glutCreateWindow (argv[0]);</P>
<P>&nbsp;&nbsp; init ();</P>
<P>&nbsp;&nbsp; glutDisplayFunc(display); </P>
<P>&nbsp;&nbsp; glutReshapeFunc(reshape);</P>
<P>&nbsp;&nbsp; glutKeyboardFunc (keyboard);</P>
<P>&nbsp;&nbsp; glutMainLoop();</P>
<P>&nbsp;&nbsp; return 0;</P>
<P>}</P>
<P>#else</P>
<P>int main(int argc, char** argv)</P>
<P>{</P>
<P>&nbsp;&nbsp;&nbsp; fprintf (stderr, "This program demonstrates a feature which is not in OpenGL Version 1.0.\n");</P>
<P>&nbsp;&nbsp;&nbsp; fprintf (stderr, "If your implementation of OpenGL Version 1.0 has the right extensions,\n");</P>
<P>&nbsp;&nbsp;&nbsp; fprintf (stderr, "you may be able to modify this program to make it run.\n");</P>
<P>&nbsp;&nbsp;&nbsp; return 0;</P>
<P>}</P>
<P>#endif</P>
<P>&nbsp;</P>
<P>六、&nbsp;&nbsp; 参考资料<BR>1.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 《OpenGL Reference Manual》，OpenGL参考手册</P>
<P>2.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 《OpenGL 编程指南》（《OpenGL Programming Guide》），Dave Shreiner，Mason Woo，Jackie Neider，Tom Davis 著，徐波译，机械工业出版社</P>
<P>3.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MSDN，事实上我收回MSDN上有完整的OpenGL参考的话，应该是说MSDN上有OpenGL1.1前版本的较完整资料。</P>
<P>4.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 《如何使用OpenGL扩展》，曹添著，已经包含了本文的大部分内容</P>
<P>&nbsp;</P>
<P>&nbsp;</P>
<P>七、&nbsp;&nbsp; 最后说明<BR>本文中所有代码（如果有的话）都能用Mercurial在Google Co<wbr>de中下载。</P>
<P>文章以博文发表的日期分目录存放，下载地址为：</P>
<P><A href="http://co<wbr>de.google.com/p/jtianling/source/checkout?repo=blog-sample-co<wbr>de">http://co<wbr>de.google.com/p/jtianling/source/checkout?repo=blog-sample-co<wbr>de</A></P>
<P>Mercurial使用方法见《分布式的，新一代版本控制系统Mercurial的介绍及简要入门》</P>
<P><BR>本文来自CSDN博客，转载请标明出处：<A href="http://blog.csdn.net/vagrxie/archive/2009/09/29/4617556.aspx">http://blog.csdn.net/vagrxie/archive/2009/09/29/4617556.aspx</A></P></div>]]></description>
	    <author><![CDATA[pannsp]]></author>
	    <comments>http://pannsp.blog.163.com/blog/static/920232009915516411</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://pannsp.blog.163.com/blog/static/920232009915516411</guid>
    <pubDate>Thu, 15 Oct 2009 17:16:41 +0800</pubDate>
    <dcterms:modified>2009-10-16T11:08:23+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[OpenGL Extension Wrangler Library(转)]]></title>	
    <link>http://pannsp.blog.163.com/blog/static/92023200991551315683</link>
    <description><![CDATA[<div><P><A href="http://glew.sourceforg.net/">http://glew.sourceforg.net</A> </P>
<P>实在不知道Wrangler 应该在这里当什么意思讲，不过他已经被包含到OpenGL SDK了，官方是推荐使用它的。 </P>
<P>GLEW有什么用？你可以把它看作是windows上的OpenGL的基本库，他取代了原来的gl.h wgl.h，让你可以方便的调用最新的OpenGL功能，包括OpenGL众多的Extension。 </P>
<P>实际上我对这个OpenGL的体系也是稀里糊涂，除了核心的概念和功能，对扩展功能基本上是抓瞎的。这就是为什么我要读GLEW的源码。读源码似乎很傻，不过总比等别人出书要聪明的多。 </P>
<P>开始吧。 </P>
<H1>基本特性：</H1>
<UL>
<LI>跨操作系统 </LI>
<LI>支持动态链接、静态库，你还可以把glew.c复制到工程中一起编译 </LI>
<LI>只依赖于OPENGL32.dll和KERNEL32.dll </LI>
<LI>因为不依赖于CRT所有你使用single-threaded, multi-threaded 或者 multi-threaded DLL都没有限制</LI></UL>
<H1>文件结构：</H1>
<P>发布版本包括一个glew32.dll和对应的glew32.lib，头文件glew.h和wglew.h。（windows dll版本）。源文件只有一个glew.c。 </P>
<H1>glew基本使用：</H1>
<P><STRONG>初始化</STRONG> </P>
<P>#include <GL>放在一切和gl有关的头文件之前，就完成了引用 </P>
<P>GLenum err = glewInit();&nbsp; 如果err为真就完成了初始化，应该放在程序的开始部分。现在还是不明白为什么有操作可以早于初始化执行。 </P>
<P><STRONG>检查扩展</STRONG> </P>
<P>if (GLEW_ARB_vertex_program)<BR>{<BR>&nbsp; /* 通过了ARB_vertex_program extension 的检查. */<BR>&nbsp; glGenProgramsARB(...);<BR>} </P>
<P>if (GLEW_VERSION_1_3)<BR>{<BR>&nbsp; /* OpenGL 1.3 是被支持的 */<BR>} </P>
<P>if (glewIsSupported("GL_VERSION_1_4&nbsp; GL_ARB_point_sprite"))<BR>{<BR>&nbsp; /* 两个都通过了检查 */<BR>} </P>
<P>if (glewGetExtension("GL_ARB_fragment_program"))<BR>{<BR>&nbsp; /* 检查ARB_fragment_program 是被支持的. */<BR>} </P>
<P>GLEW为了实现扩展检查的功能，编制了大量的代码。简单的看就是即支持宏标签又支持字符串查询。 </P>
<H1>glew.h源码结构：</H1>
<P>开头就检测有没有和GLEW冲突的库 </P>
<P>#if defined(__gl_h_) || defined(__GL_H__)<BR>#error gl.h included before glew.h<BR>#endif<BR>#if defined(__glext_h_) || defined(__GLEXT_H_)<BR>#error glext.h included before glew.h<BR>#endif<BR>#if defined(__gl_ATI_h_)<BR>#error glATI.h included before glew.h<BR>#endif </P>
<P>这些文件功能上和GLEW是重复的，所有开始就屏蔽掉了。 </P>
<P>接下来的一堆宏只是为了正确的定义 GLEWAPI GLAPI GLAPIENTRY CALLBACK 等等宏。这些定义本来已经在其他的头文件中定义了，比如WINGDIAPI就在winnt.h中定义过了。这里画蛇添足的做法只是为了保证GLEW的纯洁性，为了不依赖任何不必要的库和头文件。 </P>
<P>再下面就是OpenGL1.1的定义了。这里很简单就是类型定义和宏定义，API使用外部函数方式，这就是为什么不需要gl.h了。 </P>
<P>#define GLEW_VERSION_1_1 GLEW_GET_VAR(__GLEW_VERSION_1_1) </P>
<P>唯一让人有点不解的是这句，解析了所有的宏之后GLEW_VERSION_1_1被定义为一个GLboolean类型的变量。之后就让人很烦了__GLEW_VERSION_1_1被初始化为GL_FALSE，然后作者又在检测到支持1.1之后把GLEW_VERSION_1_1赋值为真。宏来宏去的让人郁闷。不过想想也是有道理的，GLEW_VERSION_1_1作为检测标志必须是个宏，而宏是不能修改值的，既然他可能为真也可能为假，那就只有一个办法，找一个变量作为他的存储空间，然后这个宏就可以当变量使用了。 </P>
<P>再下面#include <GL></P>
<P>再下面是OpenGL1.2的定义，和1.1的定义方式类似，只是函数定义方式不同了。 </P>
<P>typedef void (GLAPIENTRY * PFNGLCOPYTEXSUBIMAGE3DPROC) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height);<BR>typedef void (GLAPIENTRY * PFNGLDRAWRANGEELEMENTSPROC) (GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices);<BR>typedef void (GLAPIENTRY * PFNGLTEXIMAGE3DPROC) (GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels);<BR>typedef void (GLAPIENTRY * PFNGLTEXSUBIMAGE3DPROC) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *pixels); </P>
<P>#define glCopyTexSubImage3D GLEW_GET_FUN(__glewCopyTexSubImage3D)<BR>#define glDrawRangeElements GLEW_GET_FUN(__glewDrawRangeElements)<BR>#define glTexImage3D GLEW_GET_FUN(__glewTexImage3D)<BR>#define glTexSubImage3D GLEW_GET_FUN(__glewTexSubImage3D) </P>
<P>4个函数指针定义对应4个define，看名字就知道他们是一一对应的，但是就是对不上，郁闷吧。但是在文件最后的部分能找到这样的代码。 </P>
<P>GLEW_FUN_EXPORT PFNGLCOPYTEXSUBIMAGE3DPROC __glewCopyTexSubImage3D;<BR>GLEW_FUN_EXPORT PFNGLDRAWRANGEELEMENTSPROC __glewDrawRangeElements;<BR>GLEW_FUN_EXPORT PFNGLTEXIMAGE3DPROC __glewTexImage3D;<BR>GLEW_FUN_EXPORT PFNGLTEXSUBIMAGE3DPROC __glewTexSubImage3D; </P>
<P>也是4个，这下对上号了吧。PFNGLCOPYTEXSUBIMAGE3DPROC 是一个函数指针类型定义 __glewCopyTexSubImage3D是实际被定义的指针函数，#define glCopyTexSubImage3D GLEW_GET_FUN(__glewCopyTexSubImage3D)又指向了glCopyTexSubImage3D 。到最后 glCopyTexSubImage3D&nbsp; 才是真正的API接口。真费劲。 </P>
<P>再往下1.3 1.4 1.5 2.0 2.1 3.0结构完全和1.2一致 </P>
<P>再往下就是各种扩展了，形式上还是和1.2定义的方式一致 </P>
<P>再往下就是GLEW_FUN_EXPORT PFNGLCOPYTEXSUBIMAGE3DPROC __glewCopyTexSubImage3D;等等了。 </P>
<P>再往下是GLEW_VAR_EXPORT GLboolean __GLEW_VERSION_1_1;等等。 </P>
<P>最后才是GLEW的主要函数接口。这里支持一个GLEW_MX的定义，就是对GLEW MX的支持。 </P>
<H1>glew.c源码结构：</H1>
<P>相对简单的多，除了一些函数的实现以外，全是初始化函数。 </P>
<P>全部都是从glewInit开始的，一路下去初始化各个版本和所有扩展。 </P>
<H1>wglew</H1>
<P>看明白上面的，这里就不用说了，比葫芦看瓢吧。</P></div>]]></description>
	    <author><![CDATA[pannsp]]></author>
	    <comments>http://pannsp.blog.163.com/blog/static/92023200991551315683</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://pannsp.blog.163.com/blog/static/92023200991551315683</guid>
    <pubDate>Thu, 15 Oct 2009 17:13:15 +0800</pubDate>
    <dcterms:modified>2009-10-15T17:13:36+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[OpenGL Extension Wrangler Library(转)]]></title>	
    <link>http://pannsp.blog.163.com/blog/static/920232009915505232</link>
    <description><![CDATA[<div><P><A href="http://glew.sourceforg.net/">http://glew.sourceforg.net</A> 
</P><P>实在不知道Wrangler 应该在这里当什么意思讲，不过他已经被包含到OpenGL SDK了，官方是推荐使用它的。 
</P><P>GLEW有什么用？你可以把它看作是windows上的OpenGL的基本库，他取代了原来的gl.h wgl.h，让你可以方便的调用最新的OpenGL功能，包括OpenGL众多的Extension。 
</P><P>实际上我对这个OpenGL的体系也是稀里糊涂，除了核心的概念和功能，对扩展功能基本上是抓瞎的。这就是为什么我要读GLEW的源码。读源码似乎很傻，不过总比等别人出书要聪明的多。 
</P><P>开始吧。 
</P><H1>基本特性：</H1>
<UL>
<LI>跨操作系统 
</LI><LI>支持动态链接、静态库，你还可以把glew.c复制到工程中一起编译 
</LI><LI>只依赖于OPENGL32.dll和KERNEL32.dll 
</LI><LI>因为不依赖于CRT所有你使用single-threaded, multi-threaded 或者 multi-threaded DLL都没有限制</LI></UL>
<H1>文件结构：</H1>
<P>发布版本包括一个glew32.dll和对应的glew32.lib，头文件glew.h和wglew.h。（windows dll版本）。源文件只有一个glew.c。 
</P><H1>glew基本使用：</H1>
<P><STRONG>初始化</STRONG> 
</P><P>#include &lt;GL/glew.h&gt;放在一切和gl有关的头文件之前，就完成了引用 
</P><P>GLenum err = glewInit();&nbsp; 如果err为真就完成了初始化，应该放在程序的开始部分。现在还是不明白为什么有操作可以早于初始化执行。 
</P><P><STRONG>检查扩展</STRONG> 
</P><P>if (GLEW_ARB_vertex_program)<BR>{<BR>&nbsp; /* 通过了ARB_vertex_program extension 的检查. */<BR>&nbsp; glGenProgramsARB(...);<BR>} 
</P><P>if (GLEW_VERSION_1_3)<BR>{<BR>&nbsp; /* OpenGL 1.3 是被支持的 */<BR>} 
</P><P>if (glewIsSupported("GL_VERSION_1_4&nbsp; GL_ARB_point_sprite"))<BR>{<BR>&nbsp; /* 两个都通过了检查 */<BR>} 
</P><P>if (glewGetExtension("GL_ARB_fragment_program"))<BR>{<BR>&nbsp; /* 检查ARB_fragment_program 是被支持的. */<BR>} 
</P><P>GLEW为了实现扩展检查的功能，编制了大量的代码。简单的看就是即支持宏标签又支持字符串查询。 
</P><H1>glew.h源码结构：</H1>
<P>开头就检测有没有和GLEW冲突的库 
</P><P>#if defined(__gl_h_) || defined(__GL_H__)<BR>#error gl.h included before glew.h<BR>#endif<BR>#if defined(__glext_h_) || defined(__GLEXT_H_)<BR>#error glext.h included before glew.h<BR>#endif<BR>#if defined(__gl_ATI_h_)<BR>#error glATI.h included before glew.h<BR>#endif 
</P><P>这些文件功能上和GLEW是重复的，所有开始就屏蔽掉了。 
</P><P>接下来的一堆宏只是为了正确的定义 GLEWAPI GLAPI GLAPIENTRY CALLBACK 等等宏。这些定义本来已经在其他的头文件中定义了，比如WINGDIAPI就在winnt.h中定义过了。这里画蛇添足的做法只是为了保证GLEW的纯洁性，为了不依赖任何不必要的库和头文件。 
</P><P>再下面就是OpenGL1.1的定义了。这里很简单就是类型定义和宏定义，API使用外部函数方式，这就是为什么不需要gl.h了。 
</P><P>#define GLEW_VERSION_1_1 GLEW_GET_VAR(__GLEW_VERSION_1_1) 
</P><P>唯一让人有点不解的是这句，解析了所有的宏之后GLEW_VERSION_1_1被定义为一个GLboolean类型的变量。之后就让人很烦了__GLEW_VERSION_1_1被初始化为GL_FALSE，然后作者又在检测到支持1.1之后把GLEW_VERSION_1_1赋值为真。宏来宏去的让人郁闷。不过想想也是有道理的，GLEW_VERSION_1_1作为检测标志必须是个宏，而宏是不能修改值的，既然他可能为真也可能为假，那就只有一个办法，找一个变量作为他的存储空间，然后这个宏就可以当变量使用了。 
</P><P>再下面#include &lt;GL/glu.h&gt; 
</P><P>再下面是OpenGL1.2的定义，和1.1的定义方式类似，只是函数定义方式不同了。 
</P><P>typedef void (GLAPIENTRY * PFNGLCOPYTEXSUBIMAGE3DPROC) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height);<BR>typedef void (GLAPIENTRY * PFNGLDRAWRANGEELEMENTSPROC) (GLenum mode, GLuint start, GLuint end, GLsizei count, GLenum type, const GLvoid *indices);<BR>typedef void (GLAPIENTRY * PFNGLTEXIMAGE3DPROC) (GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels);<BR>typedef void (GLAPIENTRY * PFNGLTEXSUBIMAGE3DPROC) (GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *pixels); 
</P><P>#define glCopyTexSubImage3D GLEW_GET_FUN(__glewCopyTexSubImage3D)<BR>#define glDrawRangeElements GLEW_GET_FUN(__glewDrawRangeElements)<BR>#define glTexImage3D GLEW_GET_FUN(__glewTexImage3D)<BR>#define glTexSubImage3D GLEW_GET_FUN(__glewTexSubImage3D) 
</P><P>4个函数指针定义对应4个define，看名字就知道他们是一一对应的，但是就是对不上，郁闷吧。但是在文件最后的部分能找到这样的代码。 
</P><P>GLEW_FUN_EXPORT PFNGLCOPYTEXSUBIMAGE3DPROC __glewCopyTexSubImage3D;<BR>GLEW_FUN_EXPORT PFNGLDRAWRANGEELEMENTSPROC __glewDrawRangeElements;<BR>GLEW_FUN_EXPORT PFNGLTEXIMAGE3DPROC __glewTexImage3D;<BR>GLEW_FUN_EXPORT PFNGLTEXSUBIMAGE3DPROC __glewTexSubImage3D; 
</P><P>也是4个，这下对上号了吧。PFNGLCOPYTEXSUBIMAGE3DPROC 是一个函数指针类型定义 __glewCopyTexSubImage3D是实际被定义的指针函数，#define glCopyTexSubImage3D GLEW_GET_FUN(__glewCopyTexSubImage3D)又指向了glCopyTexSubImage3D 。到最后 glCopyTexSubImage3D&nbsp; 才是真正的API接口。真费劲。 
</P><P>再往下1.3 1.4 1.5 2.0 2.1 3.0结构完全和1.2一致 
</P><P>再往下就是各种扩展了，形式上还是和1.2定义的方式一致 
</P><P>再往下就是GLEW_FUN_EXPORT PFNGLCOPYTEXSUBIMAGE3DPROC __glewCopyTexSubImage3D;等等了。 
</P><P>再往下是GLEW_VAR_EXPORT GLboolean __GLEW_VERSION_1_1;等等。 
</P><P>最后才是GLEW的主要函数接口。这里支持一个GLEW_MX的定义，就是对GLEW MX的支持。 
</P><H1>glew.c源码结构：</H1>
<P>相对简单的多，除了一些函数的实现以外，全是初始化函数。 
</P><P>全部都是从glewInit开始的，一路下去初始化各个版本和所有扩展。 
</P><H1>wglew</H1>
<P>看明白上面的，这里就不用说了，比葫芦看瓢吧。</P></div>]]></description>
	    <author><![CDATA[pannsp]]></author>
	    <comments>http://pannsp.blog.163.com/blog/static/920232009915505232</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://pannsp.blog.163.com/blog/static/920232009915505232</guid>
    <pubDate>Thu, 15 Oct 2009 17:00:52 +0800</pubDate>
    <dcterms:modified>2009-10-15T17:00:52+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[OpenGL开发库的详细介绍(转)]]></title>	
    <link>http://pannsp.blog.163.com/blog/static/9202320099154242395</link>
    <description><![CDATA[<div><P style="TEXT-INDENT: 2em">开发基于OpenGL的应用程序，必须先了解OpenGL的库函数。它采用C语言风格，提供大量的函数来进行图形的处理和显示。OpenGL库函数的命名方式非常有规律。所有OpenGL函数采用了以下格式</P>
<P style="TEXT-INDENT: 2em">&lt;库前缀&gt;&lt;根命令&gt;&lt;可选的参数个数&gt;&lt;可选的参数类型&gt;</P>
<P style="TEXT-INDENT: 2em">库前缀有gl、glu、aux、glut、wgl、glx、agl等等，分别表示该函数属于OpenGL那个开发库等，从函数名后面中还可以看出需要多少个参数以及参数的类型。I代表int型，f代表float型，d代表double型，u代表无符号整型。例如glVertex3fv()表示了该函数属于gl库，参数是三个float型参数指针。我们用glVertex*()来表示这一类函数。</P>
<P style="TEXT-INDENT: 2em">OpenGL函数库相关的API有核心库(gl)、实用库(glu)、辅助库(aux)、实用<A href="http://www.zaoxue.com/">工具</A>库(glut)、窗口库(glx、agl、wgl)和扩展函数库等。从图1可以看出，gl是核心，glu是对gl的部分封装。glx、agl、wgl 是针对不同窗口系统的函数。glut是为跨平台的OpenGL程序的<A href="http://www.zaoxue.com/">工具</A>包，比aux功能强大。扩展函数库是硬件厂商为实现硬件更新利用OpenGL的扩展机制开发的函数。下面逐一对这些库进行详细介绍。</P>
<P style="TEXT-INDENT: 2em">1．&nbsp; OpenGL核心库</P>
<P style="TEXT-INDENT: 2em">核心库包含有115个函数，函数名的前缀为gl。</P>
<P style="TEXT-INDENT: 2em">这部分函数用于常规的、核心的图形处理。此函数由gl.dll来负责解释执行。由于许多函数可以接收不同数以下几类。据类型的参数，因此派生出来的函数原形多达300多个。核心库中的函数主要可以分为以下几类函数。</P>
<P style="TEXT-INDENT: 2em">绘制基本几何图元的函数。如绘制图元的函数glBegain()、glEnd()、glNormal*()、glVertex*()。</P>
<P style="TEXT-INDENT: 2em">矩阵操作、几何变换和投影变换的函数。如矩阵入栈函数glPushMatrix()、矩阵出栈 函数glPopMatrix()、装载矩阵函数glLoadMatrix()、矩阵相乘函数glMultMatrix()，当前矩阵函数glMatrixMode()和矩阵标准化函数glLoadIdentity()，几何变换函数glTranslate*()、glRotate*()和glScale*()，投影变换函数glOrtho()、glFrustum()和视口变换函数glViewport()等等。</P>
<P style="TEXT-INDENT: 2em">颜色、光照和材质的函数。如设置颜色模式函数glColor*()、glIndex*()，设置光照效果的函数glLight*() 、glLightModel*()和设置材质效果函数glMaterial()等等。</P>
<P style="TEXT-INDENT: 2em">显示列表函数、主要有创建、结束、生成、删除和调用显示列表的函数glNewList()、 glEndList()、glGenLists()、glCallList()和glDeleteLists()。</P>
<P style="TEXT-INDENT: 2em">纹理映射函数，主要有一维纹理函数glTexImage1D()、二维纹理函数glTexImage2D()、 设置纹理参数、纹理环境和纹理坐标的函数glTexParameter*()、glTexEnv*()和glTetCoord*()等。</P>
<P style="TEXT-INDENT: 2em">特殊效果函数。融合函数glBlendFunc()、反走样函数glHint()和雾化效果glFog*()。</P>
<P style="TEXT-INDENT: 2em">光栅化、象素操作函数。如象素位置glRasterPos*()、线型宽度glLineWidth()、多边形绘制模式glPolygonMode()，读取象素glReadPixel()、复制象素glCopyPixel()等。 </P>
<P style="TEXT-INDENT: 2em">选择与反馈函数。主要有渲染模式glRenderMode()、选择缓冲区glSelectBuffer()和反馈缓冲区glFeedbackBuffer()等。</P>
<P style="TEXT-INDENT: 2em">曲线与曲面的绘制函数。生成曲线或曲面的函数glMap*()、glMapGrid*()，求值器的函数glEvalCoord*() glEvalMesh*()。</P>
<P style="TEXT-INDENT: 2em">状态设置与查询函数。主要有glGet*()、glEnable()、glGetError()等。</P>
<P style="TEXT-INDENT: 2em">2．&nbsp; OpenGL实用库The OpenGL Utility Library (GLU)</P>
<P style="TEXT-INDENT: 2em">包含有43个函数，函数名的前缀为glu。</P>
<P style="TEXT-INDENT: 2em">OpenGL提供了强大的但是为数不多的绘图命令，所有较复杂的绘图都必须从点。线、面开始。Glu 为了减轻繁重的<A href="http://www.zaoxue.com/">编程</A>工作，封装了OpenGL函数，Glu函数通过调用核心库的函数，为开发者提供相对简单的用法，实现一些较为复杂的操作。此函数由glu.dll来负责解释执行。OpenGL中的核心库和实用库可以在所有的OpenGL平台上运行。主要包括了以下几种。</P>
<P style="TEXT-INDENT: 2em">辅助纹理贴图函数，有gluScaleImage() 、gluBuild1Dmipmaps()、gluBuild2Dmipmaps()。</P>
<P style="TEXT-INDENT: 2em">坐标转换和投影变换函数，定义投影方式函数gluPerspective()、gluOrtho2D() 、gluLookAt()，拾取投影视景体函数gluPickMatrix()，投影矩阵计算gluProject()和 gluUnProject()等等。</P>
<P style="TEXT-INDENT: 2em">多边形镶嵌<A href="http://www.zaoxue.com/">工具</A>，有gluNewTess()、 gluDeleteTess()、gluTessCallback()、gluBeginPolygon() gluTessVertex()、gluNextContour()、gluEndPolygon()等等。</P>
<P style="TEXT-INDENT: 2em">二次曲面绘制<A href="http://www.zaoxue.com/">工具</A>，主要有绘制球面、锥面、柱面、圆环面gluNewQuadric()、gluSphere()、gluCylinder()、gluDisk()、gluPartialDisk()、gluDeleteQuadric()等等。</P>
<P style="TEXT-INDENT: 2em">非均匀有理B样条绘制<A href="http://www.zaoxue.com/">工具</A>，主要用来定义和绘制Nurbs曲线和曲面，包括gluNewNurbsRenderer()、gluNurbsCurve()、gluBeginSurface()、gluEndSurface()、gluBeginCurve()、gluNurbsProperty()等函数。</P>
<P style="TEXT-INDENT: 2em">错误反馈<A href="http://www.zaoxue.com/">工具</A>，获取出错信息的字符串gluErrorString().</P>
<P style="TEXT-INDENT: 2em">3．&nbsp; OpenGL辅助库</P>
<P style="TEXT-INDENT: 2em">包含有31个函数，函数名前缀为aux。</P>
<P style="TEXT-INDENT: 2em">这部分函数提供窗口管理、输入输出处理以及绘制一些简单三维物体。此函数由glaux.dll来负责解释执行。创建aux库是为了学习和编写OpenGL程序，它更像是一个用于测试创意的预备基础接管。Aux库在windows实现有很多错误，因此很容易导致频繁的崩溃。在跨平台的<A href="http://www.zaoxue.com/">编程</A>实例和演示中，aux很大程度上已经被glut库取代。OpenGL中的辅助库不能在所有的OpenGL平台上运行。</P>
<P style="TEXT-INDENT: 2em">辅助库函数主要包括以下几类。</P>
<P style="TEXT-INDENT: 2em">窗口初始化和退出函数，auxInitDisplayMode()和auxInitPosition()。</P>
<P style="TEXT-INDENT: 2em">窗口处理和时间输入函数，auxReshapeFunc()、auxKeyFunc()和auxMouseFunc()。</P>
<P style="TEXT-INDENT: 2em">颜色索引装入函数，auxSetOneColor()。</P>
<P style="TEXT-INDENT: 2em">三维物体绘制函数。包括了两种形式网状体和实心体，如绘制立方体auxWireCube()和 auxSolidCube()。这里以网状体为例，长方体auxWireBox()、环形圆纹面auxWireTorus()、圆柱auxWireCylinder()、二十面体auxWireIcosahedron()、八面体auxWireOctahedron()、四面体auxWireTetrahedron()、十二面体auxWireDodecahedron()、圆锥体auxWireCone()和茶壶auxWireTeapot()。</P>
<P style="TEXT-INDENT: 2em">背景过程管理函数auxIdleFunc()。</P>
<P style="TEXT-INDENT: 2em">程序运行函数auxMainLoop()。</P>
<P style="TEXT-INDENT: 2em">4．&nbsp; OpenGL<A href="http://www.zaoxue.com/">工具</A>库 OpenGL Utility Toolkit</P>
<P style="TEXT-INDENT: 2em">包含大约30多个函数，函数名前缀为glut。</P>
<P style="TEXT-INDENT: 2em">glut是不依赖于窗口平台的OpenGL<A href="http://www.zaoxue.com/">工具</A>包，由Mark KLilgrad在SGI编写（现在在Nvidia），目的是隐藏不同窗口平台API的复杂度。 函数以glut开头，它们作为aux库功能更强的替代品，提供更为复杂的绘制功能，此函数由glut.dll来负责解释执行。由于glut中的窗口管理函数是不依赖于运行环境的，因此OpenGL中的<A href="http://www.zaoxue.com/">工具</A>库可以在X-Window, Windows NT, OS/2等系统下运行，特别适合于开发不需要复杂界面的OpenGL示例程序。对于有经验的程序员来说，一般先用glut理顺3D图形代码，然后再集成为完整的应用程序。</P>
<P style="TEXT-INDENT: 2em">这部分函数主要包括</P>
<P style="TEXT-INDENT: 2em">窗口操作函数，窗口初始化、窗口大小、窗口位置等函数glutInit() glutInitDisplayMode() glutInitWindowSize() glutInitWindowPosition()等。</P>
<P style="TEXT-INDENT: 2em">回调函数。响应刷新消息、键盘消息、鼠标消息、定时器函数等，GlutDisplayFunc() glutPostRedisplay() glutReshapeFunc() glutTimerFunc() glutKeyboardFunc() glutMouseFunc()。</P>
<P style="TEXT-INDENT: 2em">创建复杂的三维物体。这些和aux库的函数功能相同。创建网状体和实心体。如glutSolidSphere()、glutWireSphere()等。在此不再叙述。</P>
<P style="TEXT-INDENT: 2em">菜单函数。创建添加菜单的函数GlutCreateMenu()、glutSetMenu()、glutAddMenuEntry()、glutAddSubMenu() 和glutAttachMenu()。</P>
<P style="TEXT-INDENT: 2em">程序运行函数，glutMainLoop()。</P>
<P style="TEXT-INDENT: 2em">5．&nbsp; Windows专用库</P>
<P style="TEXT-INDENT: 2em">针对windows平台的扩展。包含有16个函数，函数名前缀为wgl。</P>
<P style="TEXT-INDENT: 2em">这部分函数主要用于连接OpenGL和Windows ，以弥补OpenGL在文本方面的不足。&nbsp; Windows专用库只能用于Windows环境中。</P>
<P style="TEXT-INDENT: 2em">这类函数主要包括以下几类</P>
<P style="TEXT-INDENT: 2em">绘图上下文相关函数wglCreateContext(), wglDeleteContext()wglGetCurrentContent() wglGetCurrentDC() wglDeleteContent()等</P>
<P style="TEXT-INDENT: 2em">文字和文本处理函数 wglUseFontBitmaps()、wglUseFontOutlines()。</P>
<P style="TEXT-INDENT: 2em">覆盖层、地层和主平面层处理函数 wglCopyContext()、wglCreateLayerPlane()、&nbsp; wglDescribeLayerPlane()、wglReakizeLayerPlatte()等</P>
<P style="TEXT-INDENT: 2em">其他函数，wglShareLists()、wglGetProcAddress()等。</P>
<P style="TEXT-INDENT: 2em">6．&nbsp; Win32 API函数库</P>
<P style="TEXT-INDENT: 2em">&nbsp;包含有6个函数，函数名无专用前缀。</P>
<P style="TEXT-INDENT: 2em">是win32扩展函数。这部分函数主要用于处理像素存储格式和双帧缓存。这6个函数将替换Windows GDI中原有的同样的函数。Win32API函数库只能用于Windows 95/98/NT环境中。</P>
<P style="TEXT-INDENT: 2em">7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; X窗口专用库</P>
<P style="TEXT-INDENT: 2em">是针对Unix和Linux的扩展函数。</P>
<P style="TEXT-INDENT: 2em">包括渲染上下文、绘制图元、显示列表、纹理贴图、等等</P>
<P style="TEXT-INDENT: 2em"><TT>初始化 glXQueryExtension()</TT></P>
<P style="TEXT-INDENT: 2em"><TT>渲染上下文函数， glXCreateContext() glXDestroyContext() glXCopyContext() glXMakeCurrent() glXCreateGLXPixmap()</TT></P>
<P style="TEXT-INDENT: 2em"><TT>执行 glXWaitGL()、glXWaitX()</TT></P>
<P style="TEXT-INDENT: 2em"><TT>缓冲区和字体 glXSwapBuffers()、glXUseXFont（）</TT></P>
<P style="TEXT-INDENT: 2em">8 其他扩展库</P>
<P style="TEXT-INDENT: 2em">这些函数可能是新的OpenGL函数，并没有在标准OpenGL库中实现，或者它们是用来扩展已存在的OpenGL函数的功能。和glu、glx和wgl一样，这些OpenGL扩展是由硬件厂商和厂商组织开发的。OpenGL扩展(OpenGL Extention)包含了大量的扩展API函数。</P>
<P style="TEXT-INDENT: 2em">随着硬件的更新，硬件厂商首先向SGI申请登记新的扩展，编写规格说明书（specification）。然后按照说明书进行开发扩展程序。不同的OpenGL实现（OpenGL Implementation）支持的扩展可能不一样，只有随着某一扩展的推广与应用以及硬件技术的提高该扩展才会在所有的OpenGL实现中被给予支持，从而最终成为OpenGL标准库的一部分。扩展由SGI维护，在SGI<A href="http://www.zaoxue.com/">网站</A>上列出了目前公开的已注册的扩展及其官方说明书。</P>
<P style="TEXT-INDENT: 2em">扩展源由扩展函数的后缀来指明(或使用扩展常量后缀)。例如，后缀WIN表明一个符合Windows规范的扩展，EXT或ARB后缀表明该扩展由多个卖主定义。</P>
<P style="TEXT-INDENT: 2em"></P>
<P style="TEXT-INDENT: 2em">下面给出OpenGL官方规定的</P>
<P style="TEXT-INDENT: 2em">命名规则：</P>
<P style="TEXT-INDENT: 2em">ARB</P>
<P style="TEXT-INDENT: 2em">– OpenGL Architecture Review Board 正式核准的扩展，往往由厂商开发的扩展发展而来，如果同时存在厂商开发的扩展和ARB扩展，应该优先使用ARB扩展</P>
<P style="TEXT-INDENT: 2em">EXT</P>
<P style="TEXT-INDENT: 2em">– 多家OpenGL厂商同意支持的扩展</P>
<P style="TEXT-INDENT: 2em">HP</P>
<P style="TEXT-INDENT: 2em">– Hewlett-Packard 惠普</P>
<P style="TEXT-INDENT: 2em">IBM</P>
<P style="TEXT-INDENT: 2em">– International Business Machines </P>
<P style="TEXT-INDENT: 2em">KTX</P>
<P style="TEXT-INDENT: 2em">– Kinetix, maker of 3D Studio Max </P>
<P style="TEXT-INDENT: 2em">INTEL</P>
<P style="TEXT-INDENT: 2em">– Intel 公司</P>
<P style="TEXT-INDENT: 2em">NV</P>
<P style="TEXT-INDENT: 2em">– NVIDIA 公司</P>
<P style="TEXT-INDENT: 2em">MESA</P>
<P style="TEXT-INDENT: 2em">– Brian Paul’s freeware portable OpenGL implementation </P>
<P style="TEXT-INDENT: 2em">SGI</P>
<P style="TEXT-INDENT: 2em">– Silicon Graphics公司开发的扩展</P>
<P style="TEXT-INDENT: 2em">SGIX</P>
<P style="TEXT-INDENT: 2em">– Silicon Graphics (experimental) 公司开发的实验性扩展</P>
<P style="TEXT-INDENT: 2em">SUN</P>
<P style="TEXT-INDENT: 2em">– Sun Microsystems </P>
<P style="TEXT-INDENT: 2em">WIN</P>
<P style="TEXT-INDENT: 2em">– Microsoft </P>
<P style="TEXT-INDENT: 2em">由于OpenGL扩展在针对不同平台和不同<A href="http://www.zaoxue.com/">驱动</A>，OpenGL不可能把所有的接口程序全部放到gl.h、glx.h、wgl.h中，而是将这些函数头放在了glext.h、glxext.h和wglext.h中。这些扩展被看作时OpenGL核心库规范的增加和修改。</P>
<P style="TEXT-INDENT: 2em">OpenGL扩展也不是没有缺点，正因为各个硬件厂商都可以开发自己的扩展，所以扩展的数目比较大，而且有点混乱，有些扩展实现的相同的功能，可因为是不同厂商开发的，接口却不一样，所以程序中为了实现这个功能，往往要为不同的显卡写不同的程序。这个问题在OpenGL 2.0出来后可能会得到解决，OpenGL 2.0的一个目标就是统一扩展，减少扩展数目。</P>
<P style="TEXT-INDENT: 2em"></P></div>]]></description>
	    <author><![CDATA[pannsp]]></author>
	    <comments>http://pannsp.blog.163.com/blog/static/9202320099154242395</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://pannsp.blog.163.com/blog/static/9202320099154242395</guid>
    <pubDate>Thu, 15 Oct 2009 16:02:42 +0800</pubDate>
    <dcterms:modified>2009-10-15T16:03:45+08:00</dcterms:modified>
  </item>    
  <item>
  	<title><![CDATA[微机上的OpenGL开发环境(转)]]></title>	
    <link>http://pannsp.blog.163.com/blog/static/92023200991535823194</link>
    <description><![CDATA[<div>基于OpenGL标准开发的应用程序必须运行于32位Windows <BR>平台下，如WindowsNT或Windows95环境；而且运行时还需有动态 <BR>链接库OpenGL32.DLL、Glu32.DLL，这两个文件在安装WindowsNT时已 <BR>自动装载到C：\WINNT\SYSTEM32目录下(这里假定用户将WindowsNT <BR>安装在C盘上)；而对于使用Windows95平台的用户，则需手工将 <BR>两个动态库复制到Windows95目录的SYSTEM子目录中。安装了 <BR>WindowsNT／95和VC4.1后，用户就具备了基于OpenGL开发三维图 <BR>形软件的基本条件。 <BR><BR>OpenGL程序设计的基本步骤 <BR><BR>1.OpenGL在WindowsNT下的运行机制 <BR><BR>OpenGL工作在客户机／服务器模式下，当客户方(即基 <BR>于OpenGL标准开发的应用程序)向服务器(OpenGL核心机制)发出 <BR>命令时，由服务器负责解释这些命令。通常情况下，客户方 <BR>和服务器是运行在同一台微机上的。由于OpenGL的运行机制 <BR>是客户机／服务器模式，这使得用户能够十分方便地在网 <BR>络环境下使用OpenGL，OpenGL在WindowsNT上的这种实现方式通常 <BR>称为网络透明性。 <BR><BR>OpenGL的图形库函数封装在动态链接库OpenGL32.DLL中， <BR><BR><BR>客户机中的所有OpenGL函数调用，都被传送到服务器上，由 <BR>WinSrv.DLL实现功能，再将经过处理的指令发送到Win32设备驱 <BR>动接口(DDI)，从而实现在计算机屏幕上产生图像。 <BR><BR>若使用OpenGL图形加速卡，则上述机制中将添加两个 <BR>驱动器：OpenGL可装载客户模块(OpenGLICD)将安装在客户端；硬 <BR>件指定DDI将安装在服务器端，与WinDDI同一级别。 <BR><BR>2.OpenGL的库函数 <BR><BR>开发基于OpenGL的应用程序，必须先了解OpenGL的库函 <BR>数。OpenGL函数命令方式十分有规律，每个库函数均有前缀gl <BR>、glu、aux，分别表示该函数属于OpenGL基本库、实用库或辅助 <BR>库。WindowsNT下的OpenGL包含了100多个核心函数，均以gl作为前 <BR>缀，同时还支持另外四类函数： <BR><BR>OpenGL实用库函数：43个，以glu作为前缀； <BR>OpenGL辅助库函数：31个，以aux作为前缀； <BR>Windows专用库函数(WGL)：6个，以wgl作为前缀； <BR>Win32API函数(WGL)：5个，无前缀。 <BR><BR>OpenGL的115个核心函数提供了最基本的功能，可以实 <BR><BR><BR>现三维建模、建立光照模型、反走样、纹理映射等；OpenGL实 <BR>用库函数在核心函数的上一层，这类函数提供了简单的调 <BR>用方法，其实质是调用核心函数，目的是减轻开发者的编程 <BR>工作量；OpenGL辅助库函数是一些特殊的函数，可以供初学者 <BR>熟悉OpenGL的编程机制，然而使用辅助库函数的应用程序只 <BR>能在Win32环境中使用，可移植性较差，所以开发者应尽量避 <BR>免使用辅助库函数；Windows专用库函数(WGL)主要针对WindowsNT <BR>／95环境的OpenGL函数调用；Win32API函数用于处理像素存储格 <BR>式、双缓存等函数调用。 <BR><BR>3.VC环境下基于OpenGL的编程步骤 <BR><BR>下面介绍在VC环境中建立基于Opeetting菜单选项，在Link栏的Lib输入域中 <BR>添加openg132.lib、glu32.lib，若需使用OpenGL的辅助库函数，则还 <BR>需添加glaux.lib。 <BR><BR>(3)选择View／ClassWizard菜单选项，打开MFC对话框，在 <BR>ClassName栏中选择CMyTestView类，进行以下操作： <BR><BR>选择WM_CREATE消息，鼠标单击EditCode，将OpenGL初始化代码 <BR>添加到On<WBR>Create()函数中： <BR><BR><BR><BR>／＊定义像素存储格式＊／ <BR><BR>PIXELFORMATDESCRIPTORpfd= <BR>{ <BR>sizeof(PIXELFORMATDESCRIPTOR)， <BR>1， <BR>PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL, <BR>PFD_TYPE_RGBA, <BR>24， <BR>0，0，0，0，0，0， <BR>0，0，0，0，0，0，0 <BR>32， <BR>0，0， <BR>PFD_MAIN_PLANE, <BR>0, <BR>0,0,0, <BR>} <BR>CCLientdc(this); <BR>intpixelFormat=ChoosePixelFormat(dc.m_hDC,＆pfd); <BR>BOOLsuccess=SetPixelFormat(dc.m_hDC,pixelFormat,＆pfd); <BR>m_hRC=wglCreateContext(dc.m_hDC); <BR><BR><BR><BR>选择WM_DESTORY消息，在On<WBR>Destory()中添加以下代码： <BR><BR>wglDeleteContext(m_hRC); <BR><BR>在MyTestView.cpp中，将以下代码添加到PreCreateWindows()函数中： <BR><BR>cs.style|=WS_CLIPCHILDREN|WS_CLIPSIBLINGS； <BR>OpenGL只对WS_CLIPCHILDREN|WS_CLIPSIBLINGS类型窗口有效； <BR><BR>在MyTestView.cpp中，将以下代码添加到On<WBR>Draw()函数中： <BR><BR>wglMakeCurrent(pDC－&gt;m_hDC,m_hRC); <BR>DrawScene();／／用户自定义函数，用于绘制三维场景； <BR>wglMakeCurrent(pDC－&gt;m_hDC,NULL); <BR><BR>在MyTestView.cpp中，添加成员函数DrawScene()： <BR><BR>voidCMyTestView::DrawScene() <BR>{／＊绘制三维场景＊／} <BR>(4)在MyTestView.h中包含以下头文件并添加类成员说明： <BR>＃include <BR>＃include <BR><BR><BR>＃include <BR>在CTestView类中的protected：段中添加成员变量声明： <BR>HGLRCm_hRC; <BR>同时添加成员函数声明： <BR>DrawScene(); <BR><BR>这样，一个基于OpenGL标准的程序框架已经构造好，用 <BR>户只需在DrawScene()函数中添加程序代码即可。 <BR><BR>建立三维实体模型 <BR><BR>三维实体建模是整个图形学的基础，要生成高逼真 <BR>度的图像，首先要生成高质量的三维实体模型。 <BR><BR>OpenGL中提供了十几个生成三维实体模型的辅助库函 <BR>数，这些函数均以aux作为函数名的前缀。简单的模型，如球 <BR>体、立方体、圆柱等可以使用这些辅助函数来实现，如 <BR>auxWireSphere(GLdoubleradius)(绘制一半径为radius的网状球体)。 <BR>但是这些函数难以满足建立复杂三维实体的需要，所以用 <BR>户可以通过其它建模工具(如3DS等)来辅助建立三维实体模 <BR>型数据库。笔者在三维实体的建模过程中采用3DS提供的2D <BR>Shape、3DLofter和3DEditor进行模型的编辑，最后通过将模型数 <BR><BR><BR>据以DXF文件格式输出存储供应用程序使用。 <BR><BR>真实感图形的绘制 <BR><BR>1.定义光照模型和材质 <BR><BR>(1)光源。OpenGL提供了一系列建立光照模型的库函 <BR>数，使用户可以十分方便地在三维场景中建立所需的光照 <BR>模型。OpenGL中的光照模型由环境光(AmbientLight)、漫射光 <BR>(DiffuseLight)、镜面反射光(SpecularLight)等组成，同时还可设 <BR>置光线衰减因子来模拟真实的光源效果。 <BR><BR>例如，定义一个黄色光源如下： <BR><BR>GlfloatLight_position[]={1.0,1.0,1.0,0.0,}; <BR>GlfloatLight_diffuse[]={1.0,1.0,0.0,1.0,}; <BR>glLightfv(GL_LIGHT0,GL_POSTTION,light_position);／／定义光源位置 <BR>glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse);／／定义光源漫射光 <BR>光源必须经过启动后才会影响三维场景中的实体，可以通过以下指令使光源有效：&lt; <BR>glEnable(LIGHTING);／／启动光照模型； <BR>glEnable(GL_LIGHT0);／／使光源GL_LIGHT0有效； <BR>OpenGL中一共可以定义GL_LIGHT0～GL_LIGHT7八个光源。 <BR><BR><BR><BR>(2)材质。OpenGL中的材质是指构成三维实体的材料在 <BR>光照模型中对于红、绿、蓝三原色的反射率。与光源的定义 <BR>类似，材质的定义分为环境、漫射、镜面反射成分，另外还 <BR>有镜面高光指数、辐射成分等。通过对三维实体的材质定义 <BR>可以大大提高应用程序所绘制的三维场景的逼真程度。例 <BR>如： <BR><BR>／＊设置材质的反射成分＊／ <BR><BR>GLfloatmat_ambient[]={0.8,0.8,0.8,1.0}； <BR>GLfloatmat_diffuse[]={0.8,0.0,0.8,1.0};／＊紫色＊／ <BR>GLfloatmat_specular[]={1.0,0.0,1.0,1.0};／＊镜面高光亮紫色＊／ <BR>GLfloatmat_shiness[]={100.0};／＊高光指数＊／ <BR>glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);／＊定义环境光反射率＊／ <BR>glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);／＊定义漫射光反射率＊／ <BR>glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);／＊定义镜面光反射率＊／ <BR>glMaterialfv(GL_FRONT,GL_SHINESS,mat_shiness);／＊定义高光指数＊／ <BR><BR>(3)材质RGB值与光源RGB值的关系。OpenGL中材质的颜色 <BR>与光照模型中光源的颜色含义略有不同。对于光源，R、G、B <BR>值表示三原色在光源中所占有的比率；而对于材质定义，R、 <BR><BR><BR>G、B的值表示具有这种材质属性的物体对于三原色的反射 <BR>比率，场景中物体所呈现的颜色与光照模型、材质定义都相 <BR>关。例如，若定义的光源颜色是(Lr,Lg,Lb)=(1.0,1.0,1.0)(白光)， <BR>物体的材质颜色定义为(Mr,Mg,Mb)=(0.0,0.0,0.8)，则最终到达人 <BR>眼的物体颜色应当是(Lr＊Mr,Lg＊Mg,Lb＊Mb)=(0.0,0.0,0.8)(蓝色)。 <BR><BR>2.读取三维模型数据 <BR><BR>为了绘制三维实体，我们首先必须将预先生成的三 <BR>维实体模型从三维实体模型库中读出。下图描述了读取三 <BR>维实体模型的流程。 <BR><BR>3.三维实体绘制 <BR><BR>由于3DS的DXF文件中对于三维实体的描述是采用三角 <BR>形面片逼近的方法，而在OpenGL函数库中，提供了绘制三角形 <BR>面片的方法，所以为三维实体的绘制提供了方便。以下提供 <BR>了绘制三角形面片的方法： <BR><BR>glBegin(TRANGLES);／／定义三角形绘制开始 <BR>glVertexf((GLfloat)x1,(GLfloat)y1,(GLfloat)z1);／／第一个顶点 <BR>glVertexf((GLfloat)x2,(GLfloat)y2,(GLfloat)z2);／／第二个顶点 <BR><BR><BR>glVertexf((GLfloat)x3,(GLfloat)y3,(GLfloat)z3);／／第三个顶点 <BR>glEnd();／／绘制结束 <BR><BR>为了提高三维实时动画的显示速度，我们利用了 <BR>OpenGL库中的显示列表(DisplayList)的功能，将三维场景中的实 <BR>体分别定义为单独的显示列表，预先生成三维实体。在图形 <BR>显示时，只需调用所需的显示列表即可显示相应的三维实 <BR>体，而不需要重新计算实体在场景中的坐标，避免了大量的 <BR>浮点运算。在调用显示列表前所作的旋转、平移、光照、材 <BR>质的设定都将影响显示列表中的三维实体的显示效果。具 <BR>体实现算法如下： <BR><BR>for(ObjectNo=0;ObjectNo&lt;实体个数;ObjectNo＋＋) <BR>{ <BR>glNewList(ObjectNo,GL_COMPILE);／／创建第ObjectNo个实体的显示列表 <BR>for(Fac <BR><BR><BR>OpenGL是近几年发展起来的一个性能卓越的三维图形标准，它是在SGI等多家 <BR>世界闻名的计算机公司的倡导下，以SGI的GL三维图形库为基础制定的一个通 <BR>用共享的开放式三维图形标准。目前，包括Microsoft、SGI、IBM、DEC、SUN、 <BR>HP等大公司都采用了OpenGL做为三维图形标准，许多软件厂商也纷纷以OpenGL <BR>为基础开发出自己的产品，其中比较著名的产品包括动画制作软件Soft Image <BR>和3D Studio MAX、仿真软件Open Inventor、VR软件World Tool Kit、CAM软 <BR>件ProEngineer、GIS软ARC/INFO等等。值得一提的是，随着Microsoft公司在 <BR>Windows NT和最新的Windows 95中提供了OpenGL标准及OpenGL三维图形加速卡 <BR>（如北京黎明电子技术公司的AGC-3D系列三维图形加速卡）的推出，OpenGL将 <BR>在微机中有广泛地应用，同时也为广大用户提供了在微机上使用以前只能在高 <BR>性能图形工作站上运行的各种软件的机会。 <BR><BR>OpenGL实际上是一个开放的三维图形软件包，它独立于窗口系统和操作系统， <BR>以它为基础开发的应用程序可以十分方便地在各种平台间移植；OpenGL可以 <BR>与Visual C++紧密接口，便于实现机械手的有关计算和图形算法，可保证算 <BR>法的正确性和可靠性；OpenGL使用简便，效率高。它具有七大功能： <BR><BR>1) 建模 OpenGL图形库除了提供基本的点、线、多边形的绘制函数外，还提 <BR>供了复杂的三维物体（球、锥、多面体、茶壶等）以及复杂曲线和曲面 <BR>（如Bezier、Nurbs等曲线或曲面）绘制函数。 <BR>2) 变换 OpenGL图形库的变换包括基本变换和投影变换。基本变换有平移、 <BR>旋转、变比镜像四种变换，投影变换有平行投影（又称正射投影）和透 <BR>视投影两种变换。其变换方法与机器人运动学中的坐标变换方法完全一 <BR>致，有利于减少算法的运行时间，提高三维图形的显示速度。 <BR>3) 颜色模式设置 OpenGL颜色模式有两种，即RGBA模式和颜色索引(Color Index)。 <BR>4) 光照和材质设置 OpenGL光有辐射光(Emitted Light)、环境光 <BR>(Ambient Light)、漫反射光(Diffuse Light)和镜面光(Specular Light)。 <BR>材质是用光反射率来表示。场景(Scene)中物体最终反映到人眼的颜色是光 <BR>的红绿蓝分量与材质红绿蓝分量的反射率相乘后形成的颜色。 <BR>5) 纹理映射(Texture Mapping) 利用OpenGL纹理映射功能可以十分逼真 <BR>地表达物体表面细节。 <BR>6) 位图显示和图象增强 图象功能除了基本的拷贝和像素读写外，还提供 <BR>融合(Blending)、反走样(Antialiasing)和雾(fog)的特殊图象效果处理。 <BR>以上三条可是被仿真物更具真实感，增强图形显示的效果。 <BR>7) 双缓存(Double Buffering)动画 双缓存即前台缓存和后台缓存，简而言 <BR>之，后台缓存计算场景、生成画面，前台缓存显示后台缓存已画好的画面。 <BR>此外，利用OpenGL还能实现深度暗示(Depth Cue)、运动模糊(Motion Blur)等 <BR>特殊效果。从而实现了消隐算法。</div>]]></description>
	    <author><![CDATA[pannsp]]></author>
	    <comments>http://pannsp.blog.163.com/blog/static/92023200991535823194</comments>
    <slash:comments>0</slash:comments>
    <guid isPermaLink="true">http://pannsp.blog.163.com/blog/static/92023200991535823194</guid>
    <pubDate>Thu, 15 Oct 2009 15:58:23 +0800</pubDate>
    <dcterms:modified>2009-10-15T16:08:26+08:00</dcterms:modified>
  </item>    
 </channel>
</rss>