Device GeForce GTX 680
In my program,value is copied from host to device variable using CUDA Memcpy. I could see that previous values are retained in global memory on different executions of program.(Running the executable multiple times)
Code test.cu:
First run:
const test[]="overflowhappen";
cudaMalloc((void **) &test_d, sizeof(char)*strlen(test));
cudaMemcpy(test_d,test,sizeof(char)*strlen(test),cudaMemcpyHostToDevice);
function<<<1,1>>>(testfn);
nvcc test.cu
cuda-gdb a.out
<gdb> b testfn
<gdb>p test_d ->>overflowhappen
Second run(I changed test string to var)
const test[]="var"
cudaMalloc((void **) &test_d, sizeof(char)*strlen(test));
cudaMemcpy(test_d,test,sizeof(char)*strlen(test),cudaMemcpyHostToDevice);
function<<<1,1>>>(testfn);
nvcc test.cu
cuda-gdb a.out
<gdb> b testfn
<gdb>p test_d ->> varrflowhappen
“rflowhappen” is copied from previous run.I tried cudaMemset to the variable but, still it shows values from previous runs as the variable value. Is it a problem with code? .How can I prevent it?
I think the bug may be trivial: you didn’t copy the end-string character 0.
When copying C strings, you should always take strlen+1.
As a result, in your second run, you allocate memory and copy
{'v','a','r'}, instead of{'v','a','r','\0'}.When you then try to print it, you get
var#####where####is whatever garbage was there in memory. My guess is that in the first run, that garbage was 0 so the string ended seemingly correct, while in the second run it was the garbage left by the first program.