1

I used global 2d array variable in CUDA, and I tried to use cumulative addition to this global variable. But when I rerun the code it started at the value from the last run. For example, if the value was 50 in the last run the next run would show 100. It does not reset the value to 0.

__device__ double *d_t; 
__device__ size_t d_gridPitch;

__global__ void kernelFunc()
{
    int i = blockIdx.x * blockDim.x + threadIdx.x
    double* rowt = (double*)((char *)d_t + i * d_gridPitch);
    rowt[0] = rowt[0] + 50000;

    printf("%.0f, ",rowt[0]);
}


int main()
{
    int size = 16;
    size_t d_pitchLoc;
    double *d_tLoc;

    cudaMallocPitch((void**)&d_tLoc, &d_pitchLoc, size * sizeof(double), size);
    cudaMemcpyToSymbol(d_gridPitch, &d_pitchLoc, sizeof(int));
    cudaMemcpyToSymbol(d_t, & d_tLoc, sizeof(d_tLoc));

    kernelFunc<<<1,size>>>();
    cudaDeviceReset();

    return 0;
}

1 Answers1

1

It sounds like what you want is to initialise the memory you are allocating (note that is nothing like "reseting a variable"). To do that, use cudaMemset2D to initialise the bytes in the memory allocation returned by cudaMallocPitch. So your host API sequence looks like this:

int size = 16;
size_t d_pitchLoc;
double *d_tLoc;

cudaMallocPitch((void**)&d_tLoc, &d_pitchLoc, size * sizeof(double), size);
cudaMemset2D(d_tLoc, d_pitchLoc, 0, size * sizeof(double), size);

cudaMemcpyToSymbol(d_gridPitch, &d_pitchLoc, sizeof(int));
cudaMemcpyToSymbol(d_t, & d_tLoc, sizeof(d_tLoc));

Note that cudaMemset2D, like cudaMemset, sets bytes from the LSB of the int value supplied.

Community
  • 1
  • 1
talonmies
  • 70,661
  • 34
  • 192
  • 269