如何设置 Xcode 以运行 OpenCL 代码,以及如何在构建前验证内核

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13046035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:55:20  来源:igfitidea点击:

How to set up Xcode to run OpenCL code, and how to verify the kernels before building

xcodeopencl

提问by

I am looking at the official documentation on the Apple site, and I see that there is a quickstart about how to use OpenCL on Xcode.

我正在查看 Apple 网站上的官方文档,我看到有一个关于如何在 Xcode 上使用 OpenCL 的快速入门。

Maybe it is just me, but I had no luck building the code that is mentioned on the "hello world OCL" section.

也许这只是我,但我没有运气构建“hello world OCL”部分中提到的代码。

I've started Xcode and created an empty project; created a main.c and a .cl kernel file, pasting what is on the Apple developer site, and I am not able to get anything to build, even after adding a target.

我已经启动了 Xcode 并创建了一个空项目;创建了一个 main.c 和一个 .cl 内核文件,粘贴了 Apple 开发人员网站上的内容,即使添加了目标,我也无法构建任何东西。

The AD site does not have a project to download, so I have no clue about the cause of the failure (it may be me most likely, or the site assume steps and does not mention them).

AD站点没有要下载的项目,所以我不知道失败的原因(最有可能是我,或者站点采取了步骤而没有提到它们)。

I've also tried the sample project from macresearch.org, but they are quite ancient, and the test project in the 3rd lesson is not running at all.

我也试过 macresearch.org 上的示例项目,但是它们很古老,而且第三课中的测试项目根本没有运行。

Now, I am pretty sure that others are using Xcode to run OCL code, but I cannot find any single page (except the aforementioned macresearch.org) that gives a clear setup about how to run an Xcode project with OCL. Is there anyone aware of a tutorial that shows how to work with OCL and Xcode?

现在,我很确定其他人正在使用 Xcode 来运行 OCL 代码,但是我找不到任何一个页面(除了前面提到的 macresearch.org),可以清楚地说明如何使用 OCL 运行 Xcode 项目。有没有人知道展示如何使用 OCL 和 Xcode 的教程?

I have purchased 3 books on OCL (Gaster's Heterogeneous computing with OpenCL, Scarpino's OpenCL in action and Munshi's OpenCL programming guide), and neither mention how to set up Xcode, while they go in detail for the visual studio setup and even for the eclipse setup.

我已经购买了 3 本书关于 OCL(Gaster's Heterogeneous computing with OpenCL、Scarpino's OpenCL in action 和 Munshi's OpenCL 编程指南),但都没有提到如何设置 Xcode,而他们详细介绍了 Visual Studio 设置甚至 Eclipse 设置.

On the side; is there any application that is able to validate kernel code before running it in the OCL application?

在一边; 是否有任何应用程序能够在 OCL 应用程序中运行内核代码之前对其进行验证?

Thanks in advance for any suggestion that you may have.

预先感谢您提出的任何建议。

采纳答案by NeXTCoder

So seems that I was able to partially resolve the issue in this way:

所以似乎我能够通过这种方式部分解决这个问题:

  • Create an empty project, create a main.c file and a kernel file

  • create a target as console application

  • in the build settings, add the OpenCL framework (you will see it appearing on the file browser in the left pane, if it will be added correctly).

  • specifythe location where the kernel file is located, hardcoding the path (it is not enough to just say "my kernel.cl"; Xcode for some reasons don't get that the cl file is in the same dir as the main, so you gotta specify the path when loading the kernel file)

  • 创建一个空项目,创建一个main.c文件和一个内核文件

  • 创建一个目标作为控制台应用程序

  • 在构建设置中,添加 OpenCL 框架(如果添加正确,您将看到它出现在左窗格的文件浏览器中)。

  • 指定内核文件所在的位置,对路径进行硬编码(仅说“我的 kernel.cl”是不够的;由于某些原因,Xcode 无法确定 cl 文件与主文件位于同一目录中,因此加载内核文件时必须指定路径)

I was successful using these steps, while using the example code on the Apple developer site.

我成功地使用了这些步骤,同时使用了 Apple 开发人员网站上的示例代码。

I am pretty sure that there are other ways to do this, but at least this may help whom, like me, had no clue about how to start.

我很确定还有其他方法可以做到这一点,但至少这可以帮助像我一样不知道如何开始的人。

回答by NeXTCoder

Short answer: https://developer.apple.com/library/mac/#documentation/Performance/Conceptual/OpenCL_MacProgGuide/XCodeHelloWorld/XCodeHelloWorld.html

简短回答:https: //developer.apple.com/library/mac/#documentation/Performance/Conceptual/OpenCL_MacProgGuide/XCodeHelloWorld/XCodeHelloWorld.html

Long answer for command liners:

命令行的长答案:

With a recent Xcode (and Lion or higher), you don't even need to call clCreateProgramWithSource, just write your kernel and call it from your app, there are some additional compiling steps needed though.

使用最近的 Xcode(和 Lion 或更高版本),您甚至不需要调用clCreateProgramWithSource,只需编写内核并从您的应用程序调用它,尽管需要一些额外的编译步骤。

I'm taking the OpenCL Hello World example from Apple here using default compiler flags (see https://developer.apple.com/library/mac/#documentation/Performance/Conceptual/OpenCL_MacProgGuide/ExampleHelloWorld/Example_HelloWorld.html) and showing the steps Xcode would do in the background.

我在这里使用默认编译器标志(请参阅https://developer.apple.com/library/mac/#documentation/Performance/Conceptual/OpenCL_MacProgGuide/ExampleHelloWorld/Example_HelloWorld.html)从 Apple 获取 OpenCL Hello World 示例并显示Xcode 将在后台执行的步骤。

To get you started, do a:

为了让您开始,请执行以下操作:

/System/Library/Frameworks/OpenCL.framework/Libraries/openclc -x cl -cl-std=CL1.1 -cl-auto-vectorize-enable -emit-gcl mykernel.cl

This will create 2 files, mykernel.cl.h and mykernel.cl.c (and mykernel.cl.c does all the magic of loading your kernel into the app). Next you'll need to compile the kernel:

这将创建 2 个文件,mykernel.cl.h 和 mykernel.cl.c(mykernel.cl.c 完成了将内核加载到应用程序的所有魔法)。接下来你需要编译内核:

/System/Library/Frameworks/OpenCL.framework/Libraries/openclc -x cl -cl-std=CL1.1 -Os -triple i386-applecl-darwin -emit-llvm-bc -o mykernel.cl.i386.bc mykernel.cl
/System/Library/Frameworks/OpenCL.framework/Libraries/openclc -x cl -cl-std=CL1.1 -Os -triple x86_64-applecl-darwin -emit-llvm-bc -o mykernel.cl.x86_64.bc mykernel.cl
/System/Library/Frameworks/OpenCL.framework/Libraries/openclc -x cl -cl-std=CL1.1 -Os -triple gpu_32-applecl-darwin -emit-llvm-bc -o mykernel.cl.gpu_32.bc mykernel.cl

And last but not least, build the app itself:

最后但并非最不重要的是,构建应用程序本身:

clang -c -Os -Wall -arch x86_64 -o mykernel.cl.o mykernel.cl.c
clang -c -Os -Wall -arch x86_64 -o square.o square.c
clang -framework OpenCL -o square mykernel.cl.o square.o

And that's it. You won't see any clCreateProgramWithBinaryor clCreateProgramWithSourcein Apple's sample code. Everything is done via mykernel.cl.c generated by openclc.

就是这样。您不会在 Apple 的示例代码中看到任何clCreateProgramWithBinaryclCreateProgramWithSource。一切都是通过由openclc.

回答by Eric Bainville

To answer the second part of your question, on Mountain Lion you can run the OpenCL compiler on foo.cllike this:

要回答问题的第二部分,在 Mountain Lion 上,您可以foo.cl像这样运行 OpenCL 编译器:

/System/Library/Frameworks/OpenCL.framework/Libraries/openclc \
    -c -Wall -emit-llvm -arch gpu_32 -o foo.bc foo.cl

It will compile foo.clto LLVM bit-code foo.bc. This binary file can be used with clCreateProgramWithBinary, which is faster than clCreateProgramWithSource. Valid values for -archare i386 x86_64 gpu_32.

它将编译foo.cl为 LLVM 位代码foo.bc。这个二进制文件可以与 一起使用clCreateProgramWithBinary,它比clCreateProgramWithSource. 有效值-archi386 x86_64 gpu_32