目前,NVIDIA 和 AMD 的 Windows driver 均有支援 OpenCL(NVIDIA 的正式版 driver 是從 195.62 版開始,而 AMD 則是從 9.11 版開始)。NVIDIA 的正式版 driver 中包含 OpenCL.dll,因此可以直接使用。AMD 到目前為止,則仍需要安裝其 SDK 才有 OpenCL.dll 檔。不過,在最新的 SDK 中,NVIDIA 和 AMD 使用的 calling convention 是相同的。也就是說,使用 AMD 的 SDK 編譯的 OpenCL 程式,可以直接在 NVIDIA 的 driver 下運作。反過來也是一樣。
#include <OpenCL/opencl.h>
#include <CL/cl.h>
#ifdef __APPLE__#include <OpenCL/opencl.h>#else#include <CL/cl.h>#endif
1. 配置.cl文件支持:
1.1. 打开VS2008, 工具->选项->文本编辑器->文件扩展名,添加一个新的扩展名,指定编辑器为Microsoft Visual C++ 。这样在OpenCL文件中就能显示C++的语法高亮了。
1.2. 配置OpenCL语法高亮
- 打开目录~\NVIDIA Corporation\NVIDIA GPU Computing SDK\OpenCL\doc 可以看到有一个"usertype.dat"文件其中包含了所有的OpenCL的关键字。
- 将这个文件复制到 ~\Program Files\Microsoft Visual Studio 9.0\Common7\IDE
- IMPORTANT NOTE: 如果已经有usertype.dat文件存在,则用文本编辑器打开OpenCL目录下的该文件,复制其中内容并拷贝到原文件中。
2. 配置项目
新建一个新的VC项目之后,右键点击项目->属性(也可以按快捷键ALT+F7)
1. 在 配置属性->C++->常规->附加包含目录 中添加:
- "~\NVIDIA Corporation\NVIDIA GPU Computing SDK\OpenCL\common\inc";
- "~\NVIDIA Corporation\NVIDIA GPU Computing SDK\shared\inc";
2. 在
- "~\NVIDIA Corporation\NVIDIA GPU Computing SDK\OpenCL\common\lib\Win32"; (if you are using Windows 32)
- "~\NVIDIA Corporation\NVIDIA GPU Computing SDK\shared\lib\Win32";
3. 在链接器->输入->附加依赖项 中添加
- shrUtils32D.lib
- OpenCL.lib
- oclUtils32D.lib
3. 配置代码生成属性
到这里其实已经可以编译OpenCL代码了。 最后一步需要做的是点击
转载自http://opencl.codeplex.com/wikipage?title=OpenCL Tutorials - 0&referringTitle=OpenCL Tutorials
Hi,
I've decided to create this short tutorial to help programmers to setup Visual Studio 2008 as an OpenCL developement environment.
Notice that I'm using the NVIDIA version of OpenCL (CUDA SDK 3.0), installed in the default path (C:\Documents and Settings\All Users\Dados de aplicativos\NVIDIA Corporation\, which from now on will be referenced as
1. Configuring Appearance:
1.1. OpenCL files as C++ files:In VS2008,
- Go Tools->Options->Text Editor->File Extension, add a new extension "cl" and select Microsoft Visual C++ as Editor. This will enable C++ syntax highlighting on OpenCL files;
- If you wish, you can also add "cu" extensions for CUDA
1.2. Syntax highlighting (for OpenCL reserved words)
- This should be done using a file called "usertype.dat", located at ~\NVIDIA Corporation\NVIDIA GPU Computing SDK\OpenCL\doc, which contains all OpenCL reserved words
- Copy this file to ~\Program Files\Microsoft Visual Studio 9.0\Common7\IDE (again, it may change depending on your OS version)
- This can be done also for CUDA reserved words. CUDA configuration file is at ~\NVIDIA Corporation\NVIDIA GPU Computing SDK\C\doc\syntaxhighlighting\visualstudio_8 (its also called "usertype.dat"
- IMPORTANT NOTE: if there is already an "usertype.dat" on your VS folder (what should happen when adding CUDA and OpenCL files), you should merge both (copying the content of one and pasting at the end of the other)
2. Setting Up a Project
All you must do to use VS and OpenCL is configure correctly the dependencies. After creating a new VC project, right click on the project->properties (also available up in the menu or with the shortcut ALT+F71. At Configuration Properties->C++->General->Additional Include Directories add:
- "~\NVIDIA Corporation\NVIDIA GPU Computing SDK\OpenCL\common\inc";
- "~\NVIDIA Corporation\NVIDIA GPU Computing SDK\shared\inc";
2. At
- "~\NVIDIA Corporation\NVIDIA GPU Computing SDK\OpenCL\common\lib";
- "~\NVIDIA Corporation\NVIDIA GPU Computing SDK\OpenCL\common\lib\Win32"; (if you are using Windows)
- "~\NVIDIA Corporation\NVIDIA GPU Computing SDK\shared\lib";
3. At
- shrUtils32D.lib
- oclUtils32D.lib
- OpenCL.lib
3. Configuring Code Generation Properties
We're almost ready to use OpenCL now. The final step you need to do is clicking on Project->Properties, and then Configuration Properties->C/C++->Code Generation. You must assert the property "Runtime Library" is set to "Multi-threaded Debug (/MTd)". This property specifies wheter the Runtime Library will be statically or dynamically linked to your project. If your project and the libraries it uses are compiled and linked with different runtime libraries, some weird linker errors will happen. Since the '.libs' we just added uses the static runtime library, our project needs to use it as well. More information about this can be found inhttp://www.cs.unc.edu/~lhp/personal/how2CompileInVC.html.It's ready. You may now be able to create and compile successfully an OpenCL project using VS2008
Feel free to contact us if you have any doubts.