3

Cuda char * variable assignment

 2 years ago
source link: https://www.codesd.com/item/cuda-char-variable-assignment.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Cuda char * variable assignment

advertisements

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:

__global__ void setVal(char **word)
{
    char *myWord = word[(blockIdx.y * gridDim.x) + blockIdx.x];
    myWord = "Hello\0";
}

compiles to a null kernel with nothing inside it:

$ nvcc -arch=sm_20 -c nullkernel.cu
nullkernel.cu(3): warning: variable "myWord" was set but never used

nullkernel.cu(3): warning: variable "myWord" was set but never used

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:

__global__ void setVal(char **word)
{

    char *myWord = word[(blockIdx.y * gridDim.x) + blockIdx.x];
    const char[] mymsg = "Hello\0";
    myWord = mymsg;
}

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?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK