How does warp divergence manifest in SASS? - cuda

When different threads in a warp execute divergent code, divergent branches are serialized, and inactive warps are "disabled."
If the divergent paths contain a small number of instructions, such that branch predication is used, it's pretty clear what "disabled" means (threads are turned on/off by the predicate), and it's also clearly visible in the sass dump.
If the divergent execution paths contain larger numbers of instructions (exact number dependent on some compiler heuristics) branch instructions are inserted to potentially skip one execution path or the other. This makes sense: if one long branch is seldom taken, or not taken by any threads in a certain warp, it's advantageous to allow the warp to skip those instructions (rather than being forced to execute both paths in all cases as for predication).
My question is: How are inactive threads "disabled" in the case of divergence with branches? The slide on page 2, lower left of this presentation seems to indicate that branches are taken based on a condition and threads that do not participate are switched off via predicates attached to the instructions at the branch targets. However, this is not the behavior I observe in SASS.
Here's a minimal compilable sample:
#include <stdio.h>
__global__ void nonpredicated( int* a, int iter )
{
if( a[threadIdx.x] == 0 )
// Make the number of divergent instructions unknown at
// compile time so the compiler is forced to create branches
for( int i = 0; i < iter; i++ )
{
a[threadIdx.x] += 5;
a[threadIdx.x] *= 5;
}
else
for( int i = 0; i < iter; i++ )
{
a[threadIdx.x] += 2;
a[threadIdx.x] *= 2;
}
}
int main(){}
Here's the SASS dump showing that the branch instructions are predicated, but the code at the branch targets is not predicated. Are the threads that did not take the branch switched off implicitly during execution of those branch targets, in some way that is not directly visible in the SASS? I often see terminology like "active mask" alluded to in various Cuda documents, but I'm wondering how this manifests in SASS, if it is a separate mechanism from predication.
Additionally, for pre-Volta architectures, the program counter is shared per-warp, so the idea of a predicated branch instruction is confusing to me. Why would you attach a per-thread predicate to an instruction that might change something (the program counter) that is shared by all threads in the warp?
code for sm_20
Function : _Z13nonpredicatedPii
.headerflags #"EF_CUDA_SM20 EF_CUDA_PTX_SM(EF_CUDA_SM20)"
/*0000*/ MOV R1, c[0x1][0x100]; /* 0x2800440400005de4 */
/*0008*/ S2R R0, SR_TID.X; /* 0x2c00000084001c04 */
/*0010*/ MOV32I R3, 0x4; /* 0x180000001000dde2 */
/*0018*/ IMAD.U32.U32 R2.CC, R0, R3, c[0x0][0x20]; /* 0x2007800080009c03 */
/*0020*/ IMAD.U32.U32.HI.X R3, R0, R3, c[0x0][0x24]; /* 0x208680009000dc43 */
/*0028*/ LD.E R0, [R2]; /* 0x8400000000201c85 */
/*0030*/ ISETP.EQ.AND P0, PT, R0, RZ, PT; /* 0x190e0000fc01dc23 */
/*0038*/ #P0 BRA 0xd0; /* 0x40000002400001e7 */
/*0040*/ MOV R4, c[0x0][0x28]; /* 0x28004000a0011de4 */
/*0048*/ ISETP.LT.AND P0, PT, R4, 0x1, PT; /* 0x188ec0000441dc23 */
/*0050*/ MOV R4, RZ; /* 0x28000000fc011de4 */
/*0058*/ #P0 EXIT; /* 0x80000000000001e7 */
/*0060*/ NOP; /* 0x4000000000001de4 */
/*0068*/ NOP; /* 0x4000000000001de4 */
/*0070*/ NOP; /* 0x4000000000001de4 */
/*0078*/ NOP; /* 0x4000000000001de4 */
/*0080*/ IADD R4, R4, 0x1; /* 0x4800c00004411c03 */
/*0088*/ IADD R0, R0, 0x2; /* 0x4800c00008001c03 */
/*0090*/ ISETP.LT.AND P0, PT, R4, c[0x0][0x28], PT; /* 0x188e4000a041dc23 */
/*0098*/ SHL R0, R0, 0x1; /* 0x6000c00004001c03 */
/*00a0*/ #P0 BRA 0x80; /* 0x4003ffff600001e7 */
/*00a8*/ ST.E [R2], R0; /* 0x9400000000201c85 */
/*00b0*/ BRA 0x128; /* 0x40000001c0001de7 */
/*00b8*/ NOP; /* 0x4000000000001de4 */
/*00c0*/ NOP; /* 0x4000000000001de4 */
/*00c8*/ NOP; /* 0x4000000000001de4 */
/*00d0*/ MOV R0, c[0x0][0x28]; /* 0x28004000a0001de4 */
/*00d8*/ MOV R4, RZ; /* 0x28000000fc011de4 */
/*00e0*/ ISETP.LT.AND P0, PT, R0, 0x1, PT; /* 0x188ec0000401dc23 */
/*00e8*/ MOV R0, RZ; /* 0x28000000fc001de4 */
/*00f0*/ #P0 EXIT; /* 0x80000000000001e7 */
/*00f8*/ MOV32I R5, 0x19; /* 0x1800000064015de2 */
/*0100*/ IADD R0, R0, 0x1; /* 0x4800c00004001c03 */
/*0108*/ IMAD R4, R4, 0x5, R5; /* 0x200ac00014411ca3 */
/*0110*/ ISETP.LT.AND P0, PT, R0, c[0x0][0x28], PT; /* 0x188e4000a001dc23 */
/*0118*/ #P0 BRA 0x100; /* 0x4003ffff800001e7 */
/*0120*/ ST.E [R2], R4; /* 0x9400000000211c85 */
/*0128*/ EXIT; /* 0x8000000000001de7 */
.....................................

Are the threads that did not take the branch switched off implicitly during execution of those branch targets, in some way that is not directly visible in the SASS?
Yes.
There is a warp execution or "active" mask which is separate from the formal concept of predication as defined in the PTX ISA manual.
Predicated execution may allow instructions to be executed (or not) for a particular thread on an instruction-by-instruction basis. The compiler may also emit predicated instructions to enact a conditional jump or branch.
However the GPU also maintains a warp active mask. When the machine observes that thread execution within a warp has diverged (for example at the point of a predicated branch, or perhaps any predicated instruction), it will set the active mask accordingly. This process isn't really "visible" at the SASS level. AFAIK the low level execution process for a diverged warp (not via predication) isn't well specified, so questions around how long the warp stays diverged and the exact mechanism for re-synchronization aren't well specified, and AFAIK can be affected by compiler choices, on some architectures. This is one recent discussion (note particularly the remarks by #njuffa).
Why would you attach a per-thread predicate to an instruction that might change something (the program counter) that is shared by all threads in the warp?
This is how you perform a conditional jump or branch. Since all execution is lock-step, if we are going to execute a particular instruction (regardless of mask status or predication status) the PC had better point to that instruction. However, the GPU can perform instruction replay to handle different cases, as needed at execution time.
A few other notes:
a mention of the "active mask" is here:
The scheduler dispatches all 32 lanes of the warp to the execution units with an active mask. Non-active threads execute through the pipe.
some NVIDIA tools allow for inspection of the active mask.

Related

Register consumption of per-thread Program Counters in Volta

I was curious to know the meaning behind the footnote at the bottom of Table 2 in page 18 in Volta whitepaper. While the table indicates that Volta has 256 KB registers per SM similar to its predecessors, the footprint mentions that
The per-thread program counter (PC) that forms part of the improved SIMT model typically requires two of the
register slots per thread.
Does it mean that for every running thread in Volta you have 2 reserved 32-bit registers that keep track of the PC? If yes, does it also mean that this reservation is static in a sense that regardless of how many threads are residing on your SM, 2048(maximum number of threads allowed on SM)*2=4096 registers are taken? Also, can this reservation be eliminated by compiling for a CC lower than 7.0?
It seems that for every running thread, 2 additional registers are allocated from SM's register file when compiling for Compute Capability 7.0.
Using CUDA 9.1, I compiled the following simple saxpy kernel
__global__ void saxpy(float* out, float a, float* x, float* y) {
out[ threadIdx.x ] = a * x[ threadIdx.x ] + y[ threadIdx.x ];
}
for CC 6.1 and 7.0 with maximum compiler optimization flag (-03) applied. While using cuobjdump -res-usage on the binary for CC 6.1 shows that 8 registers are used for every thread in the kernel, the same command on the binary for CC 7.0 reports that register usage per thread is 10. I also printed the sass using cuobjdump -sass. Below is the content for the binary for CC 6.1. You can see architected registers with indices 0 to 7 are all used.
code for sm_61
Function : _Z5saxpyPffS_S_
.headerflags #"EF_CUDA_SM61 EF_CUDA_PTX_SM(EF_CUDA_SM61)"
/* 0x083fc400e3e007f6 */
/*0008*/ MOV R1, c[0x0][0x20]; /* 0x4c98078000870001 */
/*0010*/ S2R R0, SR_TID.X; /* 0xf0c8000002170000 */
/*0018*/ SHL R6, R0.reuse, 0x2; /* 0x3848000000270006 */
/* 0x081fc840fec007f5 */
/*0028*/ SHR.U32 R0, R0, 0x1e; /* 0x3828000001e70000 */
/*0030*/ IADD R2.CC, R6.reuse, c[0x0][0x150]; /* 0x4c10800005470602 */
/*0038*/ IADD.X R3, R0.reuse, c[0x0][0x154]; /* 0x4c10080005570003 */
/* 0x001f8800eec007f0 */
/*0048*/ { IADD R4.CC, R6, c[0x0][0x158]; /* 0x4c10800005670604 */
/*0050*/ LDG.E R2, [R2]; } /* 0xeed4200000070202 */
/*0058*/ IADD.X R5, R0, c[0x0][0x15c]; /* 0x4c10080005770005 */
/* 0x001fdc00fec00771 */
/*0068*/ LDG.E R4, [R4]; /* 0xeed4200000070404 */
/*0070*/ IADD R6.CC, R6, c[0x0][0x140]; /* 0x4c10800005070606 */
/*0078*/ IADD.X R7, R0, c[0x0][0x144]; /* 0x4c10080005170007 */
/* 0x001ffc001e2047f2 */
/*0088*/ FFMA R0, R2, c[0x0][0x148], R4; /* 0x4980020005270200 */
/*0090*/ STG.E [R6], R0; /* 0xeedc200000070600 */
/*0098*/ EXIT; /* 0xe30000000007000f */
/* 0x001f8000fc0007ff */
/*00a8*/ BRA 0xa0; /* 0xe2400fffff07000f */
/*00b0*/ NOP; /* 0x50b0000000070f00 */
/*00b8*/ NOP; /* 0x50b0000000070f00 */
..........................
Now for CC 7.0.
code for sm_70
Function : _Z5saxpyPffS_S_
.headerflags #"EF_CUDA_SM70 EF_CUDA_PTX_SM(EF_CUDA_SM70)"
/*0000*/ #!PT SHFL.IDX PT, RZ, RZ, RZ, RZ; /* 0x000000fffffff389 */
/* 0x000fe200000e00ff */
/*0010*/ MOV R1, c[0x0][0x28]; /* 0x00000a0000017a02 */
/* 0x000fd00000000f00 */
/*0020*/ S2R R6, SR_TID.X; /* 0x0000000000067919 */
/* 0x000e220000002100 */
/*0030*/ MOV R7, 0x4; /* 0x0000000400077802 */
/* 0x000fca0000000f00 */
/*0040*/ IMAD.WIDE.U32 R2, R6.reuse, R7.reuse, c[0x0][0x170]; /* 0x00005c0006027625 */
/* 0x0c1fe400078e0007 */
/*0050*/ IMAD.WIDE.U32 R4, R6, R7, c[0x0][0x178]; /* 0x00005e0006047625 */
/* 0x000fd000078e0007 */
/*0060*/ LDG.E.SYS R2, [R2]; /* 0x0000000002027381 */
/* 0x000e2800001ee900 */
/*0070*/ LDG.E.SYS R4, [R4]; /* 0x0000000004047381 */
/* 0x000e2200001ee900 */
/*0080*/ IMAD.WIDE.U32 R6, R6, R7, c[0x0][0x160]; /* 0x0000580006067625 */
/* 0x000fe400078e0007 */
/*0090*/ FFMA R0, R2, c[0x0][0x168], R4; /* 0x00005a0002007a23 */
/* 0x001fd00000000004 */
/*00a0*/ STG.E.SYS [R6], R0; /* 0x0000000006007386 */
/* 0x0001e2000010e900 */
/*00b0*/ EXIT; /* 0x000000000000794d */
/* 0x000fea0003800000 */
/*00c0*/ BRA 0xc0; /* 0xfffffff000007947 */
/* 0x000fc0000383ffff */
/*00d0*/ NOP; /* 0x0000000000007918 */
/* 0x000fc00000000000 */
/*00e0*/ NOP; /* 0x0000000000007918 */
/* 0x000fc00000000000 */
/*00f0*/ NOP; /* 0x0000000000007918 */
/* 0x000fc00000000000 */
You see that again only architected register 0 to 7 (except for R3 and R5) are used inside the code block. There is also the use of RZ at the beginning of the kernel. Now I do not see where three other registers are, which makes me inclined to believe that two registers are reserved for tracking thread's PC.
Anyway, I came to the conclusion I stated at the beginning of the post with clearly insufficient observations. Any contribution to improve this answer is appreciated.

FMAD format in CUDA

I cannot find a document that explains the the following instruction format in CUDA
FMAD R6, -R6, c [0x1] [0x1], R5;
What is the format (source, destination, ...) and what is that -R6?
The PTX reference guide describes fma as follows
fma.rnd{.ftz}{.sat}.f32 d, a, b, c;
fma.rnd.f64 d, a, b, c;
performs
d = a*b + c;
in either single or double precision.
You are looking at disassembled SASS, the instruction set references for that show FMAD as being the (non IEEE 754 compliant) single precision form from the GT200 instruction set. That is a little bit problematic, because I don't presently have a toolchain which supports that deprecated instruction set. However, if I use the Fermi instruction set instead and compile this kernel:
__global__ void kernel(const float *x, const float *y, float *a)
{
float xval = x[threadIdx.x];
float yval = y[threadIdx.x];
float aval = -xval * xval + yval;
a[threadIdx.x] = aval;:
}
I get this SASS:
code for sm_20
Function : _Z6kernelPKfS0_Pf
.headerflags #"EF_CUDA_SM20 EF_CUDA_PTX_SM(EF_CUDA_SM20)"
/*0000*/ MOV R1, c[0x1][0x100]; /* 0x2800440400005de4 */
/*0008*/ S2R R3, SR_TID.X; /* 0x2c0000008400dc04 */
/*0010*/ MOV32I R5, 0x4; /* 0x1800000010015de2 */
/*0018*/ IMAD.U32.U32 R8.CC, R3, R5, c[0x0][0x20]; /* 0x200b800080321c03 */
/*0020*/ IMAD.U32.U32.HI.X R9, R3, R5, c[0x0][0x24]; /* 0x208a800090325c43 */
/*0028*/ IMAD.U32.U32 R6.CC, R3, R5, c[0x0][0x28]; /* 0x200b8000a0319c03 */
/*0030*/ LD.E R0, [R8]; /* 0x8400000000801c85 */
/*0038*/ IMAD.U32.U32.HI.X R7, R3, R5, c[0x0][0x2c]; /* 0x208a8000b031dc43 */
/*0040*/ IMAD.U32.U32 R4.CC, R3, R5, c[0x0][0x30]; /* 0x200b8000c0311c03 */
/*0048*/ LD.E R2, [R6]; /* 0x8400000000609c85 */
/*0050*/ IMAD.U32.U32.HI.X R5, R3, R5, c[0x0][0x34]; /* 0x208a8000d0315c43 */
/*0058*/ FFMA.FTZ R0, -R0, R0, R2; /* 0x3004000000001e40 */
/*0060*/ ST.E [R4], R0; /* 0x9400000000401c85 */
/*0068*/ EXIT; /* 0x8000000000001de7 */
..................................
Note that I also have the negated register in the FFMA.FTZ arguments. So I would guess that your:
FMAD R6, -R6, c [0x1] [0x1], R5;
is the equivalent of
R6 = -R6 * const + R5
where c [0x1] [0x1] is a compile time constant, and that the GPU has some sort of instruction modifier which it can set to control negation of a floating point value as part of a floating point operation without explicitly twiddling the sign bit of the register before the call.
(I look forward to #njuffa tearing this answer to shreds).

Launch terminated in cudaDeviceSynchronize after timeout

I try to run a simple program with 3 dimensional grid but for some reason when I launch it with cuda-memcheck it just gets stuck, and after the timeout it's terminated. The problem has nothing to do with a short timeout cause I changed it just for this manner to 60 seconds.
The code I run has a grid of 45x1575x1575 and it runs an empty __global__ function. My compute capability is 2.1 and I compile with the flag -maxrregcount=24 to limit the number of registers the device functions can use (saw in some other program of mine that it gives the best results with the occupancy calculator)
Here's my code:
#include "cuda_runtime.h"
#include "device_launch_parameters.h"
#include <stdio.h>
__global__ void stam(int a){
}
int main()
{
// Choose which GPU to run on, change this on a multi-GPU system.
cudaError_t cudaStatus = cudaSetDevice(0);
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaSetDevice failed! Do you have a CUDA-capable GPU installed?");
return;
}
dim3 gridSize(45,1575,1575);
stam<<<gridSize,224>>>(4);
cudaStatus = cudaDeviceSynchronize(); // This function gets stuck
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaSetDevice failed!!");
return;
}
cudaStatus = cudaDeviceReset();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "cudaDeviceReset failed!");
return 1;
}
return 0;
}
Isn't the max grid size 65535x65535x65535? What is the problem in here?
Edit: it only crashes when I compile it with the -G flag. Otherwise it's just slow, but it doesn't exceed the 60 seconds.
Your code is simply taking too long (yes, longer than 60 seconds) to run.
Even though your kernel "does nothing" it still represents a __global__ function call. To facilitate it, a fair amount of preamble code gets generated by the compiler. Normally the compiler would optimize much of that preamble code away, since your function does nothing (e.g. it does nothing with the variable passed to it, which the preamble code makes available to each thread.) However when you pass the -G switch, you eliminate nearly all compiler optimizations. You can get a sense of the size of the code that is actually running for each threadblock, by taking your executable and inspecting the code with cuobjdump -sass ....
Secondly, running code with cuda-memcheck usually increases execution time. The cuda-memcheck executive adjusts the order and reduces the rate at which threadblocks get executed, so it can do full analysis of the memory access pattern of each threadblock, among other things.
The net effect is that your empty kernel call, in part due to the very large grid (over 100 million threadblocks need to be processed), is taking longer than 60 seconds to execute. If you want to verify this, increase your TDR timeout to 5 minutes or 10 minutes, and eventually you will see the program return normally.
In my case, with -G and cuda-memcheck your program takes about 30 seconds to run on a Quadro5000 GPU, which has 11 SMs. Your cc2.1 GPU may have around 2 SMs, and so will run even slower than mine. If I compile without the -G switch, the runtime drops to about 2 seconds. If I compile with the -G switch, but run without cuda-memcheck, it takes about 4 seconds. If I eliminate the int a parameter from the kernel (which drastically reduces the preamble code), I can compile with -G and run with cuda-memcheck and it only takes 2 seconds.
Kernel machine code with -G and int a parameter:
Function : _Z4stami
.headerflags #"EF_CUDA_SM20 EF_CUDA_PTX_SM(EF_CUDA_SM20)"
/*0000*/ MOV R1, c[0x1][0x100]; /* 0x2800440400005de4 */
/*0008*/ ISUB R1, R1, 0x8; /* 0x4800c00020105d03 */
/*0010*/ S2R R0, SR_LMEMHIOFF; /* 0x2c000000dc001c04 */
/*0018*/ ISETP.GE.AND P0, PT, R1, R0, PT; /* 0x1b0e00000011dc23 */
/*0020*/ #P0 BRA 0x30; /* 0x40000000200001e7 */
/*0028*/ BPT.TRAP; /* 0xd00000000000c007 */
/*0030*/ IADD R0, R1, RZ; /* 0x48000000fc101c03 */
/*0038*/ MOV R2, R0; /* 0x2800000000009de4 */
/*0040*/ MOV R3, RZ; /* 0x28000000fc00dde4 */
/*0048*/ MOV R2, R2; /* 0x2800000008009de4 */
/*0050*/ MOV R3, R3; /* 0x280000000c00dde4 */
/*0058*/ MOV R4, c[0x0][0x4]; /* 0x2800400010011de4 */
/*0060*/ MOV R5, RZ; /* 0x28000000fc015de4 */
/*0068*/ IADD R2.CC, R2, R4; /* 0x4801000010209c03 */
/*0070*/ IADD.X R3, R3, R5; /* 0x480000001430dc43 */
/*0078*/ MOV32I R0, 0x20; /* 0x1800000080001de2 */
/*0080*/ LDC R0, c[0x0][R0]; /* 0x1400000000001c86 */
/*0088*/ IADD R2.CC, R2, RZ; /* 0x48010000fc209c03 */
/*0090*/ IADD.X R3, R3, RZ; /* 0x48000000fc30dc43 */
/*0098*/ MOV R2, R2; /* 0x2800000008009de4 */
/*00a0*/ MOV R3, R3; /* 0x280000000c00dde4 */
/*00a8*/ ST.E [R2], R0; /* 0x9400000000201c85 */
/*00b0*/ BRA 0xc8; /* 0x4000000040001de7 */
/*00b8*/ EXIT; /* 0x8000000000001de7 */
/*00c0*/ EXIT; /* 0x8000000000001de7 */
/*00c8*/ EXIT; /* 0x8000000000001de7 */
/*00d0*/ EXIT; /* 0x8000000000001de7 */
.........................
Kernel machine code with -G but without int a parameter:
Function : _Z4stamv
.headerflags #"EF_CUDA_SM20 EF_CUDA_PTX_SM(EF_CUDA_SM20)"
/*0000*/ MOV R1, c[0x1][0x100]; /* 0x2800440400005de4 */
/*0008*/ BRA 0x20; /* 0x4000000040001de7 */
/*0010*/ EXIT; /* 0x8000000000001de7 */
/*0018*/ EXIT; /* 0x8000000000001de7 */
/*0020*/ EXIT; /* 0x8000000000001de7 */
/*0028*/ EXIT; /* 0x8000000000001de7 */
.........................
I've just run your code with no problems on a C2050 (capability 2.0) under both cuda-memcheck and cuda-gdb. This suggests the problem is more likely related to your card/setup than the code itself.
If you were exceeding capability, you should get a launch error code, not a hang (you can check max sizes using deviceQuery SDK code if you're unsure).
It may be that cuda-memcheck is trying to gain exclusive control of the GPU, and is timing out as something else is using it [e.g. your X server] - does cuda-gdb work any better, do these tools work for other codes?

Inconsistent behavior for negative zeroes in Debug vs Release builds

I was under impression that adding a positive zero to negative zero should produce a positive zero. To quote IEEE 754 2008:
When the sum of two operands with opposite signs (or the difference of two operands with like signs) is exactly zero, the sign of that sum (or difference) shall be +0 in all rounding-direction attributes except roundTowardNegative; under that attribute, the sign of an exact zero sum (or difference) shall be −0. However, x + x = x − (−x) retains the same sign as x even when x is zero.
However, in case of CUDA, it looks like compiler is being too aggressive in optimizing away addition of a positive zero in Release builds. Plain C/C++ (or C#/.NET) are working as expected. I’ve looked at PTX code produced by the compiler for different builds, and add.f32 instruction is indeed missing in Release build.
Am I missing anything here?
__global__ void convertToPositiveZero(float* dst, int size)
{
int index = blockIdx.x * blockDim.x + threadIdx.x;
if (index < size)
{
dst[index] += 0;
}
}
// Host code
int size = 100;
float* zzh = (float*)malloc(size * sizeof(float));
zzh[0] = -0.0f;
zzh[1] = 0.0f;
assert(0x80000000 == *((int*)&zzh[0]));
if (0x80000000 != *((int*)&zzh[0]))
{
printf("Expected negative zero.\n");
exit(-1);
}
assert(0x00000000 == *((int*)&zzh[1]));
float* zzd;
cudaMalloc(&zzd, size * sizeof(float));
cudaMemcpy(zzd, zzh, size * sizeof(float), cudaMemcpyHostToDevice);
convertToPositiveZero<<<1, 100>>>(zzd, size);
cudaMemcpy(zzh, zzd, size * sizeof(float), cudaMemcpyDeviceToHost);
//zzh[0] += 0.0f;
assert(0x00000000 == *((int*)&zzh[0]));
if (0x00000000 != *((int*)&zzh[0]))
{
printf("Expected positive zero.\n");
exit(-1);
}
assert(0x00000000 == *((int*)&zzh[1]));
printf("Done.\n");
Your problem seems to be due to the optimizations carried out by nvcc when fusing FADD and FMUL into FMAD operations.
I was able to reproduce your problem under a Release modality. The resulting disassembled code, compiled by CUDA 5.5 and for a sm=2.1, is
code for sm_21
Function : _Z21convertToPositiveZeroPfi
.headerflags #"EF_CUDA_SM20 EF_CUDA_PTX_SM(EF_CUDA_SM20)
/*0000*/ MOV R1, c[0x1][0x100];
/*0008*/ S2R R0, SR_CTAID.X;
/*0010*/ S2R R2, SR_TID.X;
/*0018*/ IMAD R0, R0, c[0x0][0x8], R2;
/*0020*/ ISETP.GE.AND P0, PT, R0, c[0x0][0x28], PT;
/*0028*/ #P0 BRA.U 0x60;
/*0030*/ #!P0 MOV32I R3, 0x4;
/*0038*/ #!P0 IMAD R2.CC, R0, R3, c[0x0][0x20];
/*0040*/ #!P0 IMAD.HI.X R3, R0, R3, c[0x0][0x24];
/*0048*/ #!P0 LD.E R0, [R2];
/*0050*/ #!P0 F2F.F32.F32 R0, R0;
/*0058*/ #!P0 ST.E [R2], R0;
/*0060*/ EXIT ;
As you also noticed from the PTX file, there is no floating point add operations. Now, if you compile with -fmad=false option, the disassembled code becomes
code for sm_21
Function : _Z21convertToPositiveZeroPfi
.headerflags #"EF_CUDA_SM20 EF_CUDA_PTX_SM(EF_CUDA_SM20)
/*0000*/ MOV R1, c[0x1][0x100];
/*0008*/ S2R R0, SR_CTAID.X;
/*0010*/ S2R R2, SR_TID.X;
/*0018*/ IMAD R0, R0, c[0x0][0x8], R2;
/*0020*/ ISETP.GE.AND P0, PT, R0, c[0x0][0x28], PT;
/*0028*/ #P0 BRA.U 0x60;
/*0030*/ #!P0 MOV32I R3, 0x4;
/*0038*/ #!P0 IMAD R2.CC, R0, R3, c[0x0][0x20];
/*0040*/ #!P0 IMAD.HI.X R3, R0, R3, c[0x0][0x24];
/*0048*/ #!P0 LD.E R0, [R2];
/*0050*/ #!P0 FADD R0, R0, RZ;
/*0058*/ #!P0 ST.E [R2], R0;
/*0060*/ EXIT ;
As you can see, the presence of a FADD operation is restored and the "correct" sign of 0 is restored as well.

CUDA device stack and synchronization; SSY instruction

Edit: this question is a re-done version of the original, so the first several responses may no longer be relevant.
I'm curious about what impact a device function call with forced no-inlining has on synchronization within a device function. I have a simple test kernel that illustrates the behavior in question.
The kernel takes a buffer and passes it to a device function, along with a shared buffer and an indicator variable which identifies a single thread as the "boss" thread. The device function has divergent code: the boss thread first spends time doing trivial operations on the shared buffer, then writes to the global buffer. After a synchronization call, all threads write to the global buffer. After the kernel call, the host prints the contents of the global buffer. Here is the code:
CUDA CODE:
test_main.cu
#include<cutil_inline.h>
#include "test_kernel.cu"
int main()
{
int scratchBufferLength = 100;
int *scratchBuffer;
int *d_scratchBuffer;
int b = 1;
int t = 64;
// copy scratch buffer to device
scratchBuffer = (int *)calloc(scratchBufferLength,sizeof(int));
cutilSafeCall( cudaMalloc(&d_scratchBuffer,
sizeof(int) * scratchBufferLength) );
cutilSafeCall( cudaMemcpy(d_scratchBuffer, scratchBuffer,
sizeof(int)*scratchBufferLength, cudaMemcpyHostToDevice) );
// kernel call
testKernel<<<b, t>>>(d_scratchBuffer);
cudaThreadSynchronize();
// copy data back to host
cutilSafeCall( cudaMemcpy(scratchBuffer, d_scratchBuffer,
sizeof(int) * scratchBufferLength, cudaMemcpyDeviceToHost) );
// print results
printf("Scratch buffer contents: \t");
for(int i=0; i < scratchBufferLength; ++i)
{
if(i % 25 == 0)
printf("\n");
printf("%d ", scratchBuffer[i]);
}
printf("\n");
//cleanup
cudaFree(d_scratchBuffer);
free(scratchBuffer);
return 0;
}
test_kernel.cu
#ifndef __TEST_KERNEL_CU
#define __TEST_KERNEL_CU
#define IS_BOSS() (threadIdx.x == blockDim.x - 1)
__device__
__noinline__
void testFunc(int *sA, int *scratchBuffer, bool isBoss) {
if(isBoss) { // produces unexpected output-- "broken" code
//if(IS_BOSS()) { // produces expected output-- "working" code
for (int c = 0; c < 10000; c++) {
sA[0] = 1;
}
}
if(isBoss) {
scratchBuffer[0] = 1;
}
__syncthreads();
scratchBuffer[threadIdx.x ] = threadIdx.x;
return;
}
__global__
void testKernel(int *scratchBuffer)
{
__shared__ int sA[4];
bool isBoss = IS_BOSS();
testFunc(sA, scratchBuffer, isBoss);
return;
}
#endif
I compiled this code from within the CUDA SDK to take advantage of the "cutilsafecall()" functions in test_main.cu, but of course these could be taken out if you'd like to compile outside the SDK. I compiled with CUDA Driver/Toolkit version 4.0, compute capability 2.0, and the code was run on a GeForce GTX 480, which has the Fermi architecture.
The expected output is
0 1 2 3 ... blockDim.x-1
However, the output I get is
1 1 2 3 ... blockDim.x-1
This seems to indicate that the boss thread executed the conditional "scratchBuffer[0] = 1;" statement AFTER all threads execute the "scratchBuffer[threadIdx.x] = threadIdx.x;" statement, even though they are separated by a __syncthreads() barrier.
This occurs even if the boss thread is instructed to write a sentinel value into the buffer position of a thread in its same warp; the sentinel is the final value present in the buffer, rather than the appropriate threadIdx.x .
One modification that causes the code to produce expected output is to change the conditional statement
if(isBoss) {
to
if(IS_BOSS()) {
; i.e., to change the divergence-controlling variable from being stored in a parameter register to being computed in a macro function. (Note the comments on the appropriate lines in the source code.) It's this particular change I've been focusing on to try and track down the problem. In looking at the disassembled .cubins of the kernel with the 'isBoss' conditional (i.e., broken code) and the 'IS_BOSS()' conditional (i.e., working code), the most conspicuous difference in the instructions seems to be the absence of an SSY instruction in the disassembled broken code.
Here are the disassembled kernels generated by disassembling the .cubin files with
"cuobjdump -sass test_kernel.cubin" . everything up to the first 'EXIT' is the kernel, and everything after that is the device function. The only differences are in the device function.
DISASSEMBLED OBJECT CODE:
"broken" code
code for sm_20
Function : _Z10testKernelPi
/*0000*/ /*0x00005de428004404*/ MOV R1, c [0x1] [0x100];
/*0008*/ /*0x20009de428004000*/ MOV R2, c [0x0] [0x8];
/*0010*/ /*0x84001c042c000000*/ S2R R0, SR_Tid_X;
/*0018*/ /*0xfc015de428000000*/ MOV R5, RZ;
/*0020*/ /*0x00011de428004000*/ MOV R4, c [0x0] [0x0];
/*0028*/ /*0xfc209c034800ffff*/ IADD R2, R2, 0xfffff;
/*0030*/ /*0x9001dde428004000*/ MOV R7, c [0x0] [0x24];
/*0038*/ /*0x80019de428004000*/ MOV R6, c [0x0] [0x20];
/*0040*/ /*0x08001c03110e0000*/ ISET.EQ.U32.AND R0, R0, R2, pt;
/*0048*/ /*0x01221f841c000000*/ I2I.S32.S32 R8, -R0;
/*0050*/ /*0x2001000750000000*/ CAL 0x60;
/*0058*/ /*0x00001de780000000*/ EXIT;
/*0060*/ /*0x20201e841c000000*/ I2I.S32.S8 R0, R8;
/*0068*/ /*0xfc01dc231a8e0000*/ ISETP.NE.AND P0, pt, R0, RZ, pt;
/*0070*/ /*0xc00021e740000000*/ #!P0 BRA 0xa8;
/*0078*/ /*0xfc001de428000000*/ MOV R0, RZ;
/*0080*/ /*0x04001c034800c000*/ IADD R0, R0, 0x1;
/*0088*/ /*0x04009de218000000*/ MOV32I R2, 0x1;
/*0090*/ /*0x4003dc231a8ec09c*/ ISETP.NE.AND P1, pt, R0, 0x2710, pt;
/*0098*/ /*0x00409c8594000000*/ ST.E [R4], R2;
/*00a0*/ /*0x600005e74003ffff*/ #P1 BRA 0x80;
/*00a8*/ /*0x040001e218000000*/ #P0 MOV32I R0, 0x1;
/*00b0*/ /*0x0060008594000000*/ #P0 ST.E [R6], R0;
/*00b8*/ /*0xffffdc0450ee0000*/ BAR.RED.POPC RZ, RZ;
/*00c0*/ /*0x84001c042c000000*/ S2R R0, SR_Tid_X;
/*00c8*/ /*0x10011c03200dc000*/ IMAD.U32.U32 R4.CC, R0, 0x4, R6;
/*00d0*/ /*0x10009c435000c000*/ IMUL.U32.U32.HI R2, R0, 0x4;
/*00d8*/ /*0x08715c4348000000*/ IADD.X R5, R7, R2;
/*00e0*/ /*0x00401c8594000000*/ ST.E [R4], R0;
/*00e8*/ /*0x00001de790000000*/ RET;
.................................
"working" code
code for sm_20
Function : _Z10testKernelPi
/*0000*/ /*0x00005de428004404*/ MOV R1, c [0x1] [0x100];
/*0008*/ /*0x20009de428004000*/ MOV R2, c [0x0] [0x8];
/*0010*/ /*0x84001c042c000000*/ S2R R0, SR_Tid_X;
/*0018*/ /*0xfc015de428000000*/ MOV R5, RZ;
/*0020*/ /*0x00011de428004000*/ MOV R4, c [0x0] [0x0];
/*0028*/ /*0xfc209c034800ffff*/ IADD R2, R2, 0xfffff;
/*0030*/ /*0x9001dde428004000*/ MOV R7, c [0x0] [0x24];
/*0038*/ /*0x80019de428004000*/ MOV R6, c [0x0] [0x20];
/*0040*/ /*0x08001c03110e0000*/ ISET.EQ.U32.AND R0, R0, R2, pt;
/*0048*/ /*0x01221f841c000000*/ I2I.S32.S32 R8, -R0;
/*0050*/ /*0x2001000750000000*/ CAL 0x60;
/*0058*/ /*0x00001de780000000*/ EXIT;
/*0060*/ /*0x20009de428004000*/ MOV R2, c [0x0] [0x8];
/*0068*/ /*0x8400dc042c000000*/ S2R R3, SR_Tid_X;
/*0070*/ /*0x20201e841c000000*/ I2I.S32.S8 R0, R8;
/*0078*/ /*0x4000000760000001*/ SSY 0xd0;
/*0080*/ /*0xfc209c034800ffff*/ IADD R2, R2, 0xfffff;
/*0088*/ /*0x0831dc031a8e0000*/ ISETP.NE.U32.AND P0, pt, R3, R2, pt;
/*0090*/ /*0xc00001e740000000*/ #P0 BRA 0xc8;
/*0098*/ /*0xfc009de428000000*/ MOV R2, RZ;
/*00a0*/ /*0x04209c034800c000*/ IADD R2, R2, 0x1;
/*00a8*/ /*0x04021de218000000*/ MOV32I R8, 0x1;
/*00b0*/ /*0x4021dc231a8ec09c*/ ISETP.NE.AND P0, pt, R2, 0x2710, pt;
/*00b8*/ /*0x00421c8594000000*/ ST.E [R4], R8;
/*00c0*/ /*0x600001e74003ffff*/ #P0 BRA 0xa0;
/*00c8*/ /*0xfc01dc33190e0000*/ ISETP.EQ.AND.S P0, pt, R0, RZ, pt;
/*00d0*/ /*0x040021e218000000*/ #!P0 MOV32I R0, 0x1;
/*00d8*/ /*0x0060208594000000*/ #!P0 ST.E [R6], R0;
/*00e0*/ /*0xffffdc0450ee0000*/ BAR.RED.POPC RZ, RZ;
/*00e8*/ /*0x10311c03200dc000*/ IMAD.U32.U32 R4.CC, R3, 0x4, R6;
/*00f0*/ /*0x10309c435000c000*/ IMUL.U32.U32.HI R2, R3, 0x4;
/*00f8*/ /*0x84001c042c000000*/ S2R R0, SR_Tid_X;
/*0100*/ /*0x08715c4348000000*/ IADD.X R5, R7, R2;
/*0108*/ /*0x00401c8594000000*/ ST.E [R4], R0;
/*0110*/ /*0x00001de790000000*/ RET;
.................................
The "SSY" instruction is present in the working code but not the broken code. The cuobjdump manual describes the instruction with, "Set synchronization point; used before potentially divergent instructions." This makes me think that for some reason the compiler does not recognize the possibility of divergence in the broken code.
I also found that if I comment out the __noinline__ directive, then the code produces the expected output, and indeed the assembly produced by the otherwise "broken" and "working" versions is exactly identical. So, this makes me think that when a variable is passed via the call stack, that variable cannot be used to control divergence and a subsequent synchronization call; the compiler does not seem to recognize the possibility of divergence in that case, and therefore doesn't insert an "SSY" instruction. Does anyone know if this is indeed a legitimate limitation of CUDA, and if so, if this is documented anywhere?
Thanks in advance.
This appears to have simply been a compiler bug fixed in CUDA 4.1/4.2. Does not reproduce for the asker on CUDA 4.2.