# Is there a simple example using OpenCLLink that shows performance advantages?

OpenCLLink allows Mathematica to use the OpenCL parallel computing language. I have done some tests on simple examples, but so far, the CPU regularly outperforms the GPU.

Question: What are some nice (inherently parallel) number crunching problems that use OpenCL code and OpenCLLink to show (with AbsoluteTiming) impressive performance benefits, that are not already in the documentation?

-
Good question. I don't have an example, but worth noting is that OpenCL code is not performance-portable and must be optimized for the device it will run on. Many GPUs have no cache, for instance, so unless you have a problem that is nearly stateless and requires a lot of memory bandwidth it will be difficult to outperform the CPU. Newer GPU models are of course better in this respect. – Oleksandr R. Sep 19 '12 at 23:08
Proth's Theorem is parallelized by Prime Grid with separate code for NVIDIA and ATI. – Fred Kline Sep 20 '12 at 5:37
NVIDIA: Fast N-Body Simulation with CUDA could be rewritten for openCL for both ATI and NVIDIA. – Fred Kline Sep 20 '12 at 5:43
I agree with @OleksandrR. that simple problems will be difficult to find. – Fred Kline Sep 20 '12 at 5:56
Five short videos: What is openCL?. – Fred Kline Sep 22 '12 at 4:26

I'm sorry to say that I do not have a definitive such example. Of course, it's easy to play with the examples provided by the built in package, like OpenCLImplicitRender3D and OpenCLFractalRender3D, but you really need to have quite a high end graphics card for those to work.

I am by no means an expert in GPU computing but I have invested some effort into understanding GPU computation as implemented in Mathematica V8 as well as GPU computing in general. As a result, I have some general observations that might explain why finding such an example might not be a trivial task. In short, there are two main obstacles to generating truly high quality GPU applications with Mathematica V8.

1. You really need a very high end GPU to gain serious benefits and
2. Your computational core will need to be written in a compiled language, if you want to go beyond the built-in examples.

In addition, OpenCL in particular is not quite as well supported, I think, in Mathematica as is CUDA.

Here are some more details:

As for point (1), commercial GPUs have been around since the mid-80s but their use in scientific computing is a relatively new innovation. The design of modern GPUs is still heavily dependent on their primary function - the rendering of graphics. The main reason that GPUs are so massively parallel is that graphics rendering can be parallelized so well. On the other hand, the overall processing speed need not be particularly fast, relative to the CPU, and the computations are generally performed in single precision, rather than the double as is typical on a CPU. I think that only the highest end GPUs support double precision these days. Even then, double precision is slower than single.

As for point (2), if you compare GPU coding in Mathematica to, say, the use of Compile to generate C code, then you'll find it's quite a bit more work. For instance, the most basic example in the OpenCL documentation presents code to add two to every element in a vector. To this, you must write the following computational kernel and then load it via OpenCLFunctionLoad.

__kernel void addTwo_kernel(__global mint * arry, mint len) {
int index = get_global_id(0);
if (index >= len) return;
arry[index] += 2;
}


Now, for those who have done a fair amount of C programming, this is probably no big deal, but compared to writing Mathematica code (even code that must work via Compile), it's quite a pain.

In my opinion, the steps that Wolfram Research has taken in the direction of GPU computing are great and a promising sign for the future. However, the full realization of GPU power in Mathematica will have to wait until there is a bit more automation of code generation on the software side and improvement of GPUs for mid-range computers on the hardware side.

-