Mac OS X v10.6 and later ships with a native implementation of the OpenCL 1.0 specification.
OpenCL for Mac OS X is implemented as a framework that contains the OpenCL application programming interface (API), the OpenCL runtime engine, and an OpenCL compiler.
The following sections provide an overview of the OpenCL components on the Mac OS X platform.
The Open CL framework (OpenCL.framework) provides all the necessary headers for compiling OpenCL source code as well as interfacing with the OpenCL runtime. The OpenCL API is available in the C programming language and you can include it in any C, C++, or Objective-C project.
Using the OpenCL framework API you can create OpenCL contexts (see “Contexts”), find out what compute devices (such as CPUs and GPUs) are available on the machine, issue commands for OpenCL to perform, and query the status of the running commands. You use the OpenCL-C language to write a kernel (“Kernels”), which defines the specific task you’d like to perform in parallel, and then use the compiler to compile those instructions. In order to run the compiled code, the system on which it runs must include the OpenCL runtime.
Apple’s implementation of OpenCL is built on top of Grand Central Dispatch (GCD). For more information on GCD, see Concurrency Programming Guide.
Mac OS X’s OpenCL compiler uses the clang and LLVM technologies to compile the kernels written in OpenCL and translate those instructions into optimized machine code targeted for the hardware on the host machine. Mac OS X v10.5 and later uses LLVM technology in other environments as well, such as for compiling OpenGL code. When the compiler converts your OpenCL code it first translates your instructions from OpenCL to an intermediate representation (IR). After that, LLVM does its best to optimize the IR before passing it along to the code generators for the devices that the code must execute on. The importance of this pipeline is that it makes it possible to write a single OpenCL program that can execute natively on various machine architectures—such as those of the various CPUs and GPUs available on your system. Although LLVM is fast, compiling is always expensive and the resulting programs should be cached by your application to avoid unnecessary re-compilation.
The Mac OS X OpenCL framework is thread-safe for all functions that create, retain, or release objects such as command queue objects, memory objects, program and kernel objects. Any functions that enqueue commands to the command queue, or change the state of one of the aforementioned objects is not thread-safe.
OpenCL should be able to create multiple command queues for a given OpenCL context, and multiple OpenCL contexts in an application running on the host processor.
If you enqueue commands for multiple threads, or access information from multiple threads, your application must do its own locking to prevent concurrent access. Although you can have multiple command queues and safely submit to them from separate threads, each command queue uses resources that may make this more expensive than implementing the correct locks.
Last updated: 2009-06-10