The cuda programming guide has some details about using a pointer to shared memory…
"Pointers in code that is executed on the device are supported as long as the compileis able to resolve whether they point to either the shared memory space or the
global memory space, otherwise they are restricted to only point to memory allocated or declared in the global memory space."
I have found that this is VERY touchy in the current compiler (Windows XP, v0.8.1). For instance, I am trying to load shared memory in 16 byte global memory chunks with the following code.
__global__ void kernel(uint4 *puCoeff)
{
__shared__ int16 aiCoef[256];
... Bunch of initialization of variables...
// Load in one 16 byte line in one instruction
// The following line is to work around a compiler issue
// where the pointer is incorrectly being interpreted as global memory
// aiCoef[uOffset] = aiCoef[uOffset]; <----------
*((uint4 *)&aiCoef[uOffset]) = puCoeff[uBlockBaseAddr + uThreadID];
__syncthreads();
}
this compiles to:
...
ld.global.v4.u32 {$r20,$r21,$r22,$r23}, [$r15+0]; #
st.global.v4.u32 [$r19+0], {$r20,$r21,$r22,$r23}; #
...
But, if I do the funny assignment (aiCoef[uOffset] = aiCoef[uOffset]), then I get:
...
ld.global.v4.u32 {$r21,$r22,$r23,$r24}, [$r19+0]; #
st.shared.u32 [$r20+0], $r21; # id:1139
st.shared.u32 [$r20+4], $r22; # id:1139
st.shared.u32 [$r20+8], $r23; # id:1139
st.shared.u32 [$r20+12], $r24; # id:1139
...
Which is what I want.
So, a couple questions/suggestions.
-
Can NVIDIA publish the rules the compiler is using about using shared memory pointers? Is there a better way around this problem?
-
There should be a way to hint to the compiler that a given pointer is in shared memory (i.e. (shared*) cast)
-
Why does it take 4 instructions to move the 16 bytes into shared memory? Is there an even faster way that moves global to shared in one instruction?
-
Is there a way to add inline assembly yet?
Forgive me if this thread already exists on the board, but I couldn’t find anything that answered the questions. It seems like this would be a common problem for folks?
Thanks!