This is a follow up question to the selected answer in this post: Output of cuda program is not what was expected.
While the below functions works:
__global__ void setVal(char **word)
{
char *myWord = word[(blockIdx.y * gridDim.x) + blockIdx.x];
myWord[0] = 'H';
myWord[1] = 'e';
myWord[2] = 'l';
myWord[3] = 'l';
myWord[4] = 'o';
}
Why does not this work?
__global__ void setVal(char **word)
{
char *myWord = word[(blockIdx.y * gridDim.x) + blockIdx.x];
myWord = "Hello\0";
}
You should start paying much more attention to the output from the compiler. Your second kernel code:
compiles to a null kernel with nothing inside it:
The reason why is because what you think is a string copy assignment is really just a pointer assignment, and in this case the compiler is smart enough to know that myWord isn’t written to memory, so it just eliminates all the code and warns you that myWord isn’t used.
If I were to ask a rhetorical question and re-write the code this way:
would be more obvious both why the code doesn’t compile and why it could never “implicitly” perform a string copy assignment even if it did compile?