Hello World!

Support built into Xcode in OS X v10.7 and later makes developing OpenCL applications much easier than it used to be. This chapter describes how to create an OpenCL project in Xcode. (You don’t have to regenerate OpenCL projects that are already working.)

Creating An Application That Uses OpenCL In Xcode

To create a project that uses OpenCL in OS X v10.7 or later:

  1. Create your OpenCL project in Xcode as a new OS X project (empty is fine).

  2. Place your kernel code in one or more .cl files in your Xcode project. You can place all your kernels into a single .cl file, or you can separate them as you choose. You can also include non-kernel code that will run on the same OpenCL device as the kernel in each .cl file.

    Each .cl file is compiled by default into three files containing bitcode for i386, x86_64, and gpu_32 architectures. (You can change which bitcodes are generated using the OpenCL Architectures build setting.)

    At runtime your host application discovers the available devices and determines which of the compiled kernels to enqueue and execute. Figure 1-1 shows a very simple OpenCL project in Xcode.

    Figure 1-1  A simple OpenCL kernel in Xcode
  3. You can set the following build settings for your OpenCL apps:

    • OpenCL—Build

      • OpenCL Architectures. The default is that the product is built for all three architectures. The dropdown allows you to choose up to three of -triple i386-applecl-darwin, -triple x86_64-applecl-darwin, and -triple gpu_32-applecl-darwin.

      • OpenCL Compiler Version. The default is OpenCL C 1.1.

    • OpenCL—Code Generation

      • Auto-vectorizer. Choose Yes to turn the autovectorizer on or No to turn the autovectorizer off. This setting takes effect only for the CPU.

      • Double as single. If you set this parameter to Yes, the compiler treats double-precision floating-point expressions as single-precision floating-point expressions. This option is available for GPUs only. The default is No.

      • Flush denorms to zero. This Boolean controls how single- and double-precision denormalized numbers are handled. If you set this parameter to Yes, the compiler may flush single-precision denormalized numbers to zero; it may also flush double-precision denormalized numbers to zero if the optional extension for double-precision is supported. This is intended to be a performance hint and the OpenCL compiler can choose not to flush denorms to zero if the device supports single-precision (or double-precision) denormalized numbers (that is, if the CL_FP_DENORM bit is not set in CL_DEVICE_SINGLE_FP_CONFIG). This flag only applies for scalar and vector single-precision floating-point variables and computations on these floating-point variables inside a program. It does not apply to reading from or writing to image objects. The default is No.

      • Optimization Level. You can choose between several types of optimization from fastest performance to smallest code size. The default is to optimize for fastest performance.

      • Relax IEEE Compliance. If you set this parameter to Yes, the compiler allows optimizations for floating-point arithmetic that may violate the IEEE 754 standard and the OpenCL numerical compliance requirements defined in section 7.4 for single-precision floating-point, section 9.3.9 for double-precision floating-point, and edge case behavior as defined in section 7.5 of the OpenCL 1.1 specification. This is intended to be a performance optimization. This option causes the preprocessor macro __FAST_RELAXED_MATH__ to be defined in the OpenCL program. The default is No.

      • Use MAD. If you set this parameter to Yes, you allow expressions of the type a * b + c to be replaced by a Multiply-Add (MAD) instruction. If MAD is enabled, multistep instructions in the form a * b + c are performed in a single step, but the accuracy of the results may be compromised. For example, to optimize performance, some OpenCL devices implement MAD by truncating the result of the a * b operation before adding it to c. The default for this parameter is No.

    • OpenCL—Preprocessing

      You can enter a space-separated list of preprocessor macros of the form “foo” or “foo=bar” here if you wish.

  4. Place your host code in one or more .c files in your Xcode project. Compiling From the Command Line shows host code in an Xcode project.

    Figure 1-2  OpenCL host code in Xcode
  5. Link to the OpenCL framework in your project. See Adding an Existing Framework to a Project.

  6. Build.

    Because you are compiling your host and your kernel code before you run them, you can see compiler errors and warnings before you run your application.

  7. Run.

    Results are shown in the Xcode output pane as shown in Figure 1-3.

    Figure 1-3  Results

See Basic Programming Sample for a line-by-line description of the host and kernel code in the Hello World sample project.

Debugging

Here are a few hints to help you debug your OpenCL application:

Compiling From the Command Line

You can also compile and run your OpenCL application outside of Xcode.

To compile from the command line, call openclc.

You can set the following compile line parameters: