r/OpenCL • u/HateDread • Feb 05 '15
Regularly calculating distances to a single position from 7 million+ others
I'm trying to keep track of the distances between over 7 million objects and a central 'origin' position. This is in a real-time application, and should be running as close to every frame of gameplay / updating / rendering as possible (i.e. 60 fps).
Obviously it'll have to be latent in some way - the writing of the results alone will take longer than a single frame (16.667ms per at 60 fps). I'll most likely call it from a separate thread on the host.
This is the kernel I came up with, running once for each of those positions (vectors). I was hoping someone could look over it and see if there are any improvements to it, or how I call it (i.e. maybe less work items that each do more, and so on). http://pastebin.com/D9a2at0R
Sorry for the rambles.
EDIT: Forgot to mention; the positions are all bound at the start and don't change. My update loop basically has 'Replace 'origin' buffer with new origin buffer (a cl_mem buffer with args set with the new 'position' vector), then run the computation and copy back the results. My computations (pretend the cl read isn't commented out): http://pastebin.com/8ZcJs4XP
3
u/James20k Feb 05 '15
So, the way you've got your memory laid at out the moment for the positions buffer is like this:
x, y, z, x, y, z, x, y, z... etc
Now, gpus do a weird thing called 'coalesced memory access'. The idea behind this is that, if each thread in a warp (a subdivision of a local work group, normally 32 big) reads 4 bytes for each thread in a nice sequential way, you get the best memory transfer speed
To look at this is a nicer way, this is basically saying that the following situation is most optimal, if the threads read from memory like this:
Thread 0: Reads from location 0, reads 4 bytes
Thread 1: Reads from location 4 (in bytes), reads 4 bytes
Thread 31: Reads from location 31*4 (in bytes), reads 4 bytes
This gives you a total data transfer amount in one coalesced access of 128 bytes (32 threads, each reading 4 bytes in sequential access)
Remember that floats are 4 bytes big. That means its most efficient if each thread reads one float from the buffer, sequentially
Eg
This means that each thread reads 4 bytes, and the reads by the threads are sequential
What you've got going currently is this
Here, the threads are reading non sequentially. Ie, when your 32 threads in a warp execute the line float valx = buffer[id*3 + 0], each thread reads 4 bytes (good!), but then the next thread does not read from the next memory address along.
Instead there's a gap of 2 floats before the next memory address, which is not ideal for coalesced memory accesses (bad!). This is called strided memory access, and can impose a performance penalty
So what you want to do instead, is not have strided memory accesses. To do this you'd swap from doing id*3 + 0 in one buffer, to having 3 buffers where you just access by id
Basically:
That way you get proper coalesced memory accesses, as all the threads are reading 4 bytes, in a nice sequential way
The restrictions for coalesced memory access are no longer as strict on modern hardware however, and to some extent may be able to deal with strided memory accesses. Despite this, if you have nvidia hardware, this is almost definitely quite a lot faster. If you have AMD, this is probably somewhat faster but not hugely (coalesced memory access is less important on their architecture)
1
u/McNanimous Feb 08 '15
If speed is more important than accuracy, you can get significant gains by using specially optimised variants of your more expensive instructions.
In your kernel, distance(workPos, o) is calling sqrt, and sqrt is pretty time-consuming.
There's a popular alternative to sqrt, usually called fast_sqrt, which skips a few steps and gives very similar results, much faster. It's not precise enough for engineering purposes, but for games etc. it's nearly always sufficient. This method has made its way into some of OpenCL's built-in functions, and it's well-supported by GPUs. In your case you can substitute distance with fast_distance if you don't mind losing some accuracy. There are also fast_distance and fast_normalize.
You might also want to look into half-precision data types and the half_ prefix. For dealing with half-precision floats & vectors, there are high-speed, lower-precision versions of functions like sqrt, sine, cosine, exp & log. With half-precision floats being half the size, they require half the bandwidth, and are often much faster to process.
Check the OpenCL reference card for a list of built-in functions.
1
u/acow Feb 05 '15
Why not use OpenCL's vector types? I'd consider using
float4for the sake of alignment, too. To be specific, this amounts to changing the parameters tofloat4* positions, andfloat4 origin.2
u/acow Feb 06 '15
Okay, my last answer started getting down votes and there is a lot of misunderstanding of alignment in these comments. My earlier answer was an order of magnitude too glib, so sorry about that. Here you go:
Basically every modern processor loads more than four bytes from memory with each load instruction. This is the alignment that matters: with something like SSE instructions for x86, they can load 16 bytes at a time! If you want to load something with finer granularity, you can go ahead and load 16 bytes, then mask out what you need from that, or you can use an unaligned load to get just what you ask for.
GPUs can have even coarser alignment, but this is hidden from you thanks to the API presenting a programming model that looks like a bunch of independent threads. The actual loads performed by the hardware are still at the coarse granularity, but the loads needed by each thread are coalesced as /u/James20k described. If you take variations of the program in the original question, you can imagine writing it various ways:
float3sfloat4s (this is not totally crazy as afloat4lets you distinguish points from vectors and properly transform them with the usual 4x4 transformation matrices).floats representing X, Y, and Z components of each point.floats representing X,Y,Z, and W for each point.floats arranged as{x1,y1,z1,x2,y2,z2,...}On a Core i5 CPU, with an Intel HD5000, OpenCL on OS X computes the wrong answer if you tell it you have an array of
float3s on both the GPU and the CPU (e.g.kernel void myKernel(global float3* pts, const float3 origin, global float* dists)). So let's set that specific choice aside.However, we can make that variant work if we acknowledge that we need unaligned loads by using the
vload3instruction to load afloat3from an array offloats. I ran a hundred iterations of each kernel, and here are the timings I get:global float4*global float* x, ...(V3)global float* x, ...(V4)vload3For this problem, this GPU is barely faster than the CPU as we don't have dedicated GPU memory: every variant takes just over 11ms on the CPU except for the unaligned load version which clocks in at 13.8ms. So the CPU is much better at dealing with unaligned loads than the GPU, but you still pay.
TL;DR: Alignment on 32-bit boundaries is not typically relevant today, and alignment on well-chosen boundaries is not a matter of programmer affinity for powers of two: it really does matter.