Compilers and More: OpenCL Promises and Potential
OpenCL was introduced with great fanfare and promise. Apple has served a key role, perhaps the key role, in pushing OpenCL to ratification and public release. In fact, Apple has apparently filed for trademark protection on OpenCL technology, presumably (or hopefully) to prevent anyone else from claiming prior art and thereby preventing Apple from using it themselves. The development of OpenCL included dozens of industrial and institutional partners, including AMD, ARM, IBM, Intel, NVIDIA, and Texas Instruments.
The excitement is intensified by a quote from Steve Jobs saying, “It’s way beyond what NVIDIA or anyone else has, and it’s really simple,” (more on this later), and the first line on the Khronos OpenCL page: “OpenCL is the first open, royalty-free standard for cross-platform, parallel programming of modern processors found in personal computers, servers and handheld/embedded devices.” That last comment leaves me rankled, since I’m personally deeply involved in OpenMP, another open, royalty-free standard for parallel programming of modern processors; perhaps there are enough adjectival phrases in the OpenCL statement to distinguish it. At the SC08 OpenCL Technical Briefing, Tim Mattson from Intel, who is also deeply involved in OpenMP language development, gushed about how we now finally have a portable parallel programming model, and predicted that half the people in the room would be using OpenCL by the next SC conference. I guess we’ll see in just a couple of months.
So, given all the hype, what can we expect from OpenCL? Is it really simple? Is it portable? Will it replace other parallel programming models? It’s still a little early; we’ve seen a multicore demonstration of OpenCL from AMD, a limited developer release from NVIDIA, and Apple is planning to release its next generation operating system in September, including OpenCL support. Yet we can prognosticate, given what we know about the language and related technologies.
Is it simple?
Simple is a loaded term. One can claim anything is simple if it’s simpler than something else that’s even more complex. But I’ve said before that parallel programming is hard, and is going to remain so. Perhaps that’s overstating the case: we can probably make it easy to write parallel programs that perform badly, but I’m assuming that isn’t the intent and is obviously unacceptable.
So, is there a metric by which we can claim that OpenCL is simple? I’m going to give a qualified no. In OpenCL, you have a host and one or more compute devices; you have a host program that launches kernels; you have task parallelism and data parallelism, work-items and work-groups; you have contexts and command queues; you have global memory, local memory, and private memory, not to mention constant memory, with different consistency models across the memory types; you have buffer objects, image objects, and sampler objects; and you have language restrictions and language extensions. It’s not simple.
Over fifty years of programming language development has led us to expect certain features in our programming environment, such as simple access to modular programming (procedures, argument passing, linkers), which are not so well represented in OpenCL. There’s no linker for GPU kernels in OpenCL; you must include library calls to build a device program and extract a handle to the kernel function. There’s no direct function call to the kernel; you must set up the arguments one at a time with a series of library calls. The OpenCL strategy was to not perturb the host programming language with additional keywords or syntax, but to encapsulate additional functionality in a host-side library. You can expect any number of OpenCL preprocessors to convert a higher-level representation (a la NVIDIA’s CUDA) to the lower-level OpenCL. If I haven’t convinced you that OpenCL isn’t simple, look at an OpenCL example program, in this case to compute the sum of a vector of numbers (provided by Apple).
So, what’s my qualification? If you had looked at the hoops programmers had to jump through to program GPUs for general purpose computing before OpenCL and CUDA, you’d agree that these languages are a very large step in the right direction. Much simpler, though not really simple.
Is it portable?
Again, we should define our metric. We’ve grown accustomed to being able to write programs that will run across a wide variety of target systems with almost no modifications. Compiled languages (Fortran, C, C++) will require a recompile for a new processor or operating system, and may need changes for system calls. Just-in-time platforms (for Java, C#) and interpreted languages (Perl, Python) don’t even need that.
However, in the HPC world we want more than the program to just work; we want it to deliver high performance. The gold standard in this regard has been vectorizing compilers. When the Cray-1 was delivered in 1976, the primary method to access the vector instructions was to use the vectorizing Fortran compiler, CFT (Cray Fortran Translator). The compiler generated a vectorization listing telling the user what loops were or were not vectorized; if there were vector hazards (data dependences) or other limitations (IO statements, procedure calls, conditionals), the listing would call them out. Programmers learned to read the listing and, if the loop were critical to performance, rewrite the loop so it would vectorize. They were rewarded with vector performance on that application, and they learned how to write code that would vectorize for their next project. Moreover, and this is the key, those programs would also vectorize on subsequent generations of vector machines from Fujitsu, NEC, IBM, Convex, Alliant, Hitachi, and others. Those vector programs were not just portable, they were performance portable.
Side note: We use exactly the same compiler technology for today’s packed instruction set extensions, like SSE on X64 and Altivec on IBM Power. And enjoy the same performance portability benefits as well.
So, are OpenCL programs going to be performance portable or not? Sadly, not. An OpenCL kernel is a low-level representation of the work to be done on the accelerator or GPU itself. To optimize the performance, you must know and take advantage of device-specific information such as the optimal number of work-items in a work-group, the amount of local memory and perhaps the number of banks, and trade-offs between parallelism and efficiency. An optimized kernel for one device may or may not perform well on another, but is unlikely to be optimal for that second device. This is not a condemnation of OpenCL, or even a failure of the language. It was intended and designed to be a low-level, high-performance, close-to-the-metal, efficient programming interface, borrowing phrases. The intent is apparently that OpenCL will support an ecosystem of tools, middleware and applications, not to be the portable parallel abstraction. Even though the kernels are not performance portable, even if you have to tune your kernels for each device, the ability to write your kernels for different devices in the same language is a great leap forward from where we are today.
Will it replace or supersede other parallel programming models?
What other models might it replace? The dominant parallel programming models are MPI for cluster programming and OpenMP for shared memory programming. Many others exist: Java threads, TBB, Map/Reduce, Unified Parallel C, Cilk, and many more, but let’s stick with the classics.
MPI programming targets a network of homogeneous or heterogeneous nodes running in MIMD mode, each communicating and synchronizing with other nodes, one-to-one or collectively. The node program is a complete scalar program; from the language perspective, it just happens to use a library that happens to communicate through I/O channels with other copies of itself that just happen to be running at the same time. OpenMP programming targets a homogeneous shared-memory multiprocessor running in MIMD mode, with a master thread that controls when the other threads running on other processors start to cooperate to help with parallel work. Unlike an MPI node program, the OpenMP program is a global program; it describes all the work to be done, with directives telling the compiler which parts to run on the master thread, which parts to run redundantly on all threads, and (most importantly) which parts to split up among the worker threads.
OpenCL kernel routines are like MPI node programs; they describe the work to be done by one thread. The OpenCL host program is like the OpenMP master thread; it controls the execution of the kernels and the parallel threads. OpenCL uses shared memory in the compute devices, like OpenMP, but has a weaker consistency model and weaker synchronization semantics. The requirement to break up the parallel part of the program into kernels, with a single host thread acting as master, and the shared memory among the compute devices, makes OpenCL an unlikely candidate to replace MPI on large network supercomputers. The separate host and compute device memory, very limited synchronization, and non-incremental nature make it unlikely to replace OpenMP as the preferred method to program multicore or other shared memory multiprocessors.
Will OpenCL replace CUDA?
This is a more interesting question. The OpenCL JumpStart Guide from NVIDIA points out that CUDA has two library API layers. There is a Runtime API, which includes some language syntax extensions, and which automates some of the handshaking between the host thread and the compute device. There is also a lower level, Driver API, which requires the programmer to manage all the interaction between the host and the device. OpenCL is quite similar in many respects to CUDA using the Driver API, and the above mentioned JumpStart Guide gives the correspondence between the two.
Will NVIDIA adopt OpenCL in place of CUDA? That would be silly; NVIDIA already has a large contingent of CUDA users, and CUDA C does include the higher level, easier-to-use Runtime API. NVIDIA has had a two-year head start in the GPU/Accelerator programming game. While OpenCL has the potential to level the playing field somewhat, the CUDA Runtime API gives NVIDIA a slight edge in programmability, so NVIDIA will not likely abandon it.
Does OpenCL have any value at all?
We certainly hope so, given how much effort has gone into its definition and various implementations from AMD, Apple, NVIDIA, and certainly others. Today, if you have the right kind of parallelism in your application, and you’re willing to expend the effort to recast your program in CUDA kernels and write the host program interface, you can see some dramatic performance improvements. Replacing CUDA with OpenCL may allow you to port your program from an NVIDIA device to an ATI GPU or Intel Larrabee with only an incremental effort. So, yes, there is definite value here. The question remaining to be answered is how much value, and how long will it continue to be valuable. I can’t answer that, and I don’t want to raise the FUD (fear, uncertainty, doubt) factor. If Intel’s Larrabee and other similar devices, which support a richer programming model, replace GPUs as the dominant compute accelerator, OpenCL may join the dead language heap, along with Thinking Machine’s C*, MasPar’s MPL, and ClearSpeed’s Cn. However, if GPUs and other accelerators can continue to use the OpenCL programming model efficiently, and deliver high performance at relatively low cost, then OpenCL can enjoy a long, useful life. My opinion: it’s likely to be more useful as a target language for higher level programming languages, tools, and environments, or as a language to implement optimized libraries, than as a language for a more general programming community.
So let’s accept and even celebrate OpenCL for what it is, and not try to make it what it can’t be. There’s danger is raising expectations too high, or claiming too much (a la Bernie Madoff); OpenCL can be influential and succeed without replacing other parallel languages. To correct Steve Job’s quote: “While OpenCL is very similar in many respects to NVIDIA’s CUDA, it adds features to take advantage of other targets; and though it’s quite complex, it has the potential to deliver very high performance, and is much easier than trying to map your computation into OpenGL or graphics primitives.” Hype I can agree with; but then, I’m not the Apple CEO.
Michael Wolfe has developed compilers for over 30 years in both academia and industry, and is now a senior compiler engineer at The Portland Group, Inc. (www.pgroup.com), a wholly-owned subsidiary of STMicroelectronics, Inc. The opinions stated here are those of the author, and do not represent opinions of The Portland Group, Inc. or STMicroelectronics, Inc.