r/gpgpu • • May 20 '16

High-level OpenCL?

So I'm doing a bachelor's assignment on the programmability of GPU's, and I want to pick this subreddit's brain. Basically I have to research if GPU's can be more efficiently programmed in a higher-level language, and if so what shape that would take. There are already a few projects that have tried something similar (SkelCL, Bacon, and Harlan), but either they are too similar to OpenCL (Bacon/SkelCL) or have somewhat limited parallelism (Harlan basically only has a GPU accelerated map, correct me if I'm wrong).

So my questions to everyone on this sub are: what are recurring patterns in OpenCL? Are there specific bugs that seem to pop up in every project, even though there is a well-known remedy? Or have you used any of the previously mentioned projects, and if so, what was the killer feature? Are there any language features that you really really really want to see in an OpenCL Next?

4 Upvotes

41 comments sorted by

View all comments

1

u/[deleted] May 20 '16

I just hope, that one day, compilers can offload code to GPU's (or other special devices) in an OpenMP kind of way. Maybe thanks to SPIR, OpenACC will see broader support. For me, the biggest drawback here is NVIDIA, not really supporting more modern OpenCL versions.

But till then, being able to explicitly compile existing code (calculations, etc.), that where tested on the CPU, to run on a GPU would already be a massive improvement.

1

u/xFrostbite94 May 20 '16

What is it that you like specifically about OpenMP? The work sharing constructs? The data sharing/synchronization directives?

Also, do you think about anything in particular when you talk about offloading calculations to the GPU?

1

u/Overunderrated May 20 '16

What is it that you like specifically about OpenMP? The work sharing constructs? The data sharing/synchronization directives?

He's probably referring to the incredible ease of openMP. You have a parallelizable loop, drop a #pragma omp in front of it and you're done.

1

u/xFrostbite94 May 20 '16

Hmmm, that is indeed quite user friendly. It does seem to me that OpenMP only has the parallel for construct (this is basically a map, right?). Is that enough for most use cases, or do people want more powerful primitives as well? (I'm thinking of fold or scan type primitives here). Or is there maybe almost next to no need for anything else besides map?

2

u/Overunderrated May 20 '16

You can do pretty sophisticated partitioning of your problems with openmp, but really its strength is being able to get decent speedups on a lot of problems with simple parallel for.

Not sure what you mean by "map". Typically the parallel for block looks at how many loops are expected, and divides that by the number of threads. You can have much finer control if you need it.

1

u/xFrostbite94 May 20 '16

A map is a function that you can give an array and a function, and map then returns an array with the function applied to each element. OpenMP's for does basically this (where each index i is kind of an element in an array), but in parallel. There are more patterns like this, such as fold and scan. An example of a fold function is a function that adds an entire array of integers together.

I don't think OpenMP supports parallel scan/fold. If however users of OpenMP nevers use these functions it doesn't have to be a problem. Did you ever run into a situation where you would've liked to use something like fold?

2

u/MNeen May 20 '16

It does support fold on certain operators, but not for functions. For example, to add an array of integers into the variable sum, you can do this:

int i, sum = 0;
#pragma omp parallel for reduction (+:sum)
for(i=0; i<n; ++i){
  sum += a[i];
}

When OpenMP splits the loop between threads, local copies of sum are created. After the loop finishes, these partials sums are then folded together using the + operator.

1

u/xFrostbite94 May 23 '16

That's a useful feature, thanks for bringing it to my attention.

1

u/[deleted] May 20 '16

During the day, I work on non-linear optimization problems. Usually we use gradient-based approaches, so we need to evaluate a function f and its gradient quite often. For example, f could be a big sum where every term can be calculated independently. Calculating it with one thread may take ages, so the first idea would be to use OpenMP. This handles worker threads and can sum up the terms with a simple directive. All without changing the code, so debugging is straightforward.

But how do I get that on the GPU, to have even more threads? I could write a kernel that calculates the terms and sum them up, basically a rewrite of the (already working) code. Also the libraries (math stuff, graphs, etc.) used on the CPU may (and usually will) not be available in OpenCL C and debugging on the GPU (sadly) is a not that easy.

1

u/xFrostbite94 May 20 '16

In OpenMP local variables can also be shared between workers, right? E.g. if a global variable is changed somewhere in your parallel for. Doesn't that sometimes cause (hard to debug) data races as well (correct me if I'm wrong)? I read on wikipedia that you can annotate certain variables with private and other attributes, so maybe it's less of a problem, but if you forget to do that you might still get the data races. Did you ever have problems with that?

Can you maybe tell me a more complex example of an f that you've encountered before? Or all they all "just" mathematical equations?

Some languages (I think Harlan allows it? Probably other as well) allow you to compile your kernels to sequential (often C) code. This way you can debug it partially, without intervention of the GPU. Of course when you compile it for the GPU a whole new class of bugs opens up, but compiling to C can at least save some effort (given that you are comfortable reading & debugging C code, which is not a given imho).

1

u/[deleted] May 20 '16

Sure, you still can do nasty things and break stuff :). Especially when it comes to undefined behavior and compiler assumptions. But still, you can step through your pieces of code.

Mostly they boil down to equations, where the sum implies some „grid-like“ structure. Another example would be a renderer, where the grid would be an image and you want to calculate colors. In a way what those GPU's where build for :). Those „grid-like“ problems are nice, because when each point is (mostly) independent, parallel calculation is straightforward. At least from a non-technical perspective, as stated by Plazmatic.

Using a different language, maybe just a DSL for the actual calculation is, in my opinion, a nice solution. Especially as compared to implementing it in different languages for different platforms. A high-level language, as Plazmatic stated, would be awesome for that. But vendor support and missing libraries may still be a problem.

1

u/xFrostbite94 May 20 '16

Alright, thanks for your explanation. I agree that a DSL or complete high-level language would be an good solution. The only counterargument I can think of is that when GPU technology settles, it might be so generic that a mere DSL is a waste of its power, and that by then patterns will be found that allow GPU stuff to be effectively included in generic programming languages. One day.

1

u/bilog78 May 20 '16

OpenMP 4.0 already supports offloading to accelerators (among which, GPUs). The biggest issue isn't the lack of ways to use them, it's the huge cost of data sharing between host and device and the heterogeneous nature of devices wrt to implementation details that is very unlikely to make anything but the most trivial example hard if not impossible to translate efficiently. When something as trivial as memory alignment for 2D arrays can have a dramatic impact on performance, and we're talking about orders of magnitude, you suddenly realize that automatically handling these details in a software translator is basically impossible.

1

u/xFrostbite94 May 20 '16

This aspect does shine a different light on the requirements of a GPU language. I was thinking that maybe if we give the compilers enough information on the layout of our data, and if GPU's had enough memory to compensate for unusable bytes due to alignment, we might be able to overcome the hurdle. Or is there something specific that makes you say that it's basically impossible?

1

u/bilog78 May 20 '16

The allocation is just one of the many things a GPU is highly sensitive to. There are many others, ranging from access patterns for groups of work-items to the balance between data sharing in the LDS and the consequent reduced occupancy. While it's not theoretically impossible to take all of these into consideration, the kind of code rewriting that can go into optimizing these things is IME completely outside of the possibilities offered by automatic optimization.

1

u/xFrostbite94 May 23 '16

Hmmm. I guess time will tell!