1
votes

I have a CUDA code, which I want to optimize. My kernel is working with dim3 grid=(35,48) and dim3 threads=(18,18). First of all, each block performs independent 290 vector calculations, where each thread perform 1 vector calculation (which is 1024 additions-multiplications).

However, the input data for the first 17*17=289 of this calculations is stored in the shared array im1, and data for the last is stored in im2 (also output arrays are different). After that I use all obtained data to perform further calculations.

I implemented it as follows:

if ((threadIdx.x < 17) && (threadIdx.y < 17)){
    **instructions for 289s vector calculations**
}
else if ((threadIdx.x == 17) && (threadIdx.y == 17)){
    **instruction for 290 vector calculation**
}
__syncthreads();
***further calculations***

So, if I understand correct, my first 289 follow 1 branch, and thread #324 follows another. As long as first group of threads are in warps #0,1,..,10, and thread #324 is in the warp #11, there is no divergent branches. However, I read, that typically it is better to avoid any if statements in such kernels and replace them with strided index, or something like that. So, can I improve this code somehow?

My GPU is GTX 980 with cc 5.2, I use VS2013 for coding.

Thanks, Mikhail

2
Did you profile your code to measure the actual amount of warp divergence? - void_ptr
You mean with NSight? No, for some reason I cannot force it to work, but this is different story. - Mikhail Genkin
If you want to improve the performance of your code, you should first measure your code's performance, determine the bottlenecks, and only then consider divergence if it is indeed a bottleneck. - Jared Hoberock
According to your question, with a block of 18 x 18 threads, the else if statement will never been executed. No divergence in this code. - pQB
Shared memory access is very sensitive to which banks are accessed. Best would be that each warp for each command only accesses each bank only once. (except if it is the same memory location). But cannot tell more without knowing how the memory locations for the vector calculations are arranged. - Sebastian

2 Answers

2
votes

Let's consider a block of 18 * 18 threads, numbered from 0 (0, 0) to 323 (17, 17).

So, if I understand correct, my first 289 follow 1 branch [...]

If by "first 289" you refer to the threads numbered from 0 (0, 0) to 288 (16, 16), then no, not all of them take the first branch. For example, thread 17 (0, 17) does not take the branch (see illustration below). Over the span of a block, however, 289 threads take that branch indeed.

[...] and thread #324 follows another

That is right, thread 323 (17, 17) takes the second branch.

Threads 17 (0, 17), 35 (1, 17) ... 305 (16, 17) and 306 (17, 0), 307 (17, 1) ... 322 (17, 16) (totaling 35 threads) do not take any branch and are wasted. From a performance standpoint, that's bad, but it's not really catastrophic either.

But consider the following schema of what you are doing:

    0  1  2  … 15 16 17     
0   *  *  *  *  *  *  -      * represents a thread that takes branch 1
1   *  *  *  *  *  *  -      X represents a thread that takes branch 2
2   *  *  *  *  *  *  -      - represents a thread that takes no branch
…   *  *  *  *  *  *  -
15  *  *  *  *  *  *  -
16  *  *  *  *  *  *  -
17  -  -  -  -  -  -  X

Remember that warps are composed of 32 threads. So threads 0..31, 32..63, etc, execute in a lockstep. As you may notice on the schema above, you have one inactive thread every 18 threads. In other words, that means all your warps diverge.

It's probably not an enormous performance hit (if at all) though, because one of the branch is always "do nothing". That being said, I would definitely encourage you to fix your design, and I am confident you could notice a performance improvement (more due to memory access patterns than divergence itself, though).

An obvious solution would be to launch only 290 threads instead of 324, and do the mapping to x and y coordinates yourself, but then your last warp would diverge in a noticeable way.

Another solution would be to launch enough warps to cover the first 289 threads (that means 10 warps and the last one wastes 31 threads) and run a supplementary warp in which you use a thread for the second branch (the last one, for example). So that would be 11 warps, 352 threads, 62 wasted. That might seem worse in terms of efficiency, but it's actually more complicated than that because of memory access patterns, so give it a try.

Also note that if the bodies of the if/else statements do not actually differ on the code but on the data (as you kind of seem to imply...), then using a branch is pointless. Just play with pointers. Other issues may arise (related to memory access coalescing), but there will be no code flow divergence.

I would suggest more improvements but without seeing your code or knowing how your data is laid out, it's kind of a shot in the dark. You say in the comments you can't get NSIGHT to work: I strongly recommend you make that a priority.

0
votes

According to my understanding, if the branches are to be optimized, the data must be processed in advance (that is, those data located at the 18th should be clustered together in advance and deleted at the original location).