0

I have a square grid (ni x nj). In this case, ni=256 and nj=256. This is a part of my code which I have a doubt about.

u = (float *)malloc(ni*nj*sizeof(float));
cudaMallocPitch((void **)&u_data, &pitch, sizeof(float)*ni, nj);
cudaMemset2D((void *)u_data, pitch, 0.2, sizeof(float)*ni, nj);
cudaMemcpy2D((void *)u, pitch, (void *)u_data,
                   sizeof(float)*ni, sizeof(float)*ni,
                           nj, cudaMemcpyDeviceToHost);
printf("%f, %f\n", u[I2D(ni, 23, 54)], u[I2D(ni, 45, 67)]);

From what I understand, the expected output should be : 0.2, 0.2

But it shows : 0.0, 0.0

Can somebody explain to me what the problem could be? What am I doing wrong?

roynalnaruto
  • 329
  • 2
  • 6
  • 17
  • Your expectations are wrong. The `cudaMemset` family of functions, like their C standard library counterparts, set *byte* values. You cannot use them to initialize 32 bit floating point values. – talonmies Mar 13 '15 at 05:33
  • I did not know that. Thank you! Then how should I assign a floating point value to all the elements? – roynalnaruto Mar 13 '15 at 05:53
  • Read the answers in the duplicate question. There is a complete, ready to use function there for standard memset, it requires trivial adaptation to be used on a pitched memory allocation – talonmies Mar 13 '15 at 05:53
  • Can you give me link to the duplicate question? – roynalnaruto Mar 13 '15 at 05:57
  • It is right at the top of this question – talonmies Mar 13 '15 at 06:03
  • thanks! can you tell me if the rest of the code is right or not? Am I using the cudaMemcpy2D function correctly?? – roynalnaruto Mar 13 '15 at 06:36
  • If I may be so bold as to contradict the esteemed @talonmies partially. `cudaMemset()` *can* be used to initialize 32-bit floating-point values, but only very few different values. All CUDA-supported GPUs use the IEEE-754 single-precision format for `float`,and the encoding is therefore "well known". So it is perfectly reasonably to initialize an array of `float` to zero with `cudaMemset (ptr, 0, size)` or to NaN with `cudaMemset (ptr, 0xff, size)`. I use these two idioms frequently. – njuffa Mar 13 '15 at 19:36

0 Answers0