For-loops are an extremely common control structure. In many cases they run from 0 to a certain upper bound (i.e. for (int i = 0; i < N; ++i)). When writing this, you might be tempted to make i an unsigned integer, as it will always be positive. But doing so could be a bad idea. Today we’ll look at an example why.
This post is day 2 of Stream HPC’s Advent of Kernel Optimization, posting every weekday until the 25th of December. Check out all posts so far here.
A tale of two loops
Take the following example, which is just a simple CUDA kernel which sums a sliding window across an input array and stores the sums in the output:
template<typename T>
__global__ void sum_window(const float* input, float* output, int N, int W)
{
const int tid = blockIdx.x * blockDim.x + threadIdx.x;
if (tid + W >= N)
return;
float sum = 0.0f;
#pragma unroll 1
for (T i = 0; i < W; ++i)
sum += input[tid + i];
output[tid] = sum;
}For this example we’ve templated the loop counter type, so that we can invoke it with either an unsigned or a signed loop counter. We also add #pragma unroll 1 to prevent the loop from being unrolled so that we can see the difference more clearly.
If we benchmark this, it turns out the signed integer version is about 16% faster (37.5ms vs 44.5ms. Tested with N=256M, W=64 on a RTX 500 Ada laptop GPU).
Under the hood
Let’s take a look at the generated assembly to figure out why this is. We’ll investigate the PTX assembly, as it’s a bit easier to see the difference there. From the disparity in runtime we already know that some difference remains when the compiler lowers the PTX to SASS.
The preamble for both kernels is identical, so we’ll look at the point where they start to differ. This includes the setup to start the loop and the full iteration body. Below is the generated code for the signed version:
...
mul.wide.s32 %rd7, %r1, 4; // offset = tid * 4
add.s64 %rd11, %rd6, %rd7; // addr = input + offset
mov.f32 %f8, 0f00000000; // sum = 0.0f
mov.u32 %r11, 0; // i = 0
$L__BB0_3:
ld.global.f32 %f6, [%rd11]; // temp = load(addr)
add.ftz.f32 %f8, %f8, %f6; // sum += temp
add.s64 %rd11, %rd11, 4; // addr += 4
add.s32 %r11, %r11, 1; // i += 1
setp.lt.s32 %p3, %r11, %r4; // i < W
@%p3 bra $L__BB0_3; // loop
...And here is the unsigned version:
...
mov.u32 %r13, %r1; // idx = tid
mov.f32 %f8, 0f00000000; // sum = 0.0f
mov.u32 %r11, 0; // i = 0
$L__BB0_3:
mul.wide.u32 %rd12, %r13, 4; // offset = idx * 4
add.s64 %rd11, %rd6, %rd12; // addr = input + offset
ld.global.f32 %f6, [%rd11]; // temp = load(addr)
add.ftz.f32 %f8, %f8, %f6; // sum += temp
add.s32 %r13, %r13, 1; // idx += 1
add.s32 %r11, %r11, 1; // i += 1
setp.lt.u32 %p3, %r11, %r4; // i < W
@%p3 bra $L__BB0_3; // loop
....From this we can see the core difference between the two kernels:
- The signed variant stores the address to load from in
%rd11(lines 2-3) and increments it by 4 every iteration (line 9). - The unsigned variant copies the
tidvariable from%r1to%r13(line 2) and increments that by 1 every iteration (line 10). As a result it also has to calculate the address to load by multiplying it by 4 and adding the base array address (lines 6-7).
To clarify this difference, here’s what it would roughly look like in C++ code for the signed version:
int offset = tid * 4;
long addr = input + offset;
float sum = 0.0f;
for (int i = 0; i < W; ++i)
{
sum += load(addr);
addr += 4;
}And for the unsigned version:
unsigned int idx = tid;
float sum = 0.0f;
for (unsigned int i = 0; i < W; ++i)
{
unsigned int offset = idx * 4;
long addr = input + offset;
sum += load(addr);
idx += 1;
}You can now very easily see why the signed version is so much faster. It saves a multiplication and addition every iteration. Now why is that?
The perks of undefined behavior
It has been a long standing quirk of the C/C++ language standards that overflowing an unsigned integer has strictly defined behavior, but that this definition is absent for signed integers. So when we do an operation on two signed integers (i.e. our tid + i in the example), the compiler is free to optimize that as it sees fit without having to stick to a certain behavior in case of an overflow. As a result it can turn the multiplication into an addition every iteration.
In contrast, the unsigned variant must handle the fact that the array offset might overflow, and in that case must wrap around. Resulting in it having to do the intermediate multiplication every iteration and store that in a 32-bit register before calculating the 64-bit address.
This is why Nvidia recommends always using signed integers as loop counters in their CUDA Best Practices Guide. But there are many more cases where the use of signed integers can allow the compiler to apply better optimization. So much so that when modernizing the C++20 standard for signed integer behavior (P0907R4), the undefined overflow behavior was explicitly kept for performance concerns over replacing it with defined behavior.
Conclusion
So there you have it: If you need a simple integer counter, just make it signed. Even if it will never be negative, your code might just be faster for it.