简体   繁体   中英

Why use memset when using CUDA?

I saw in a CUDA code example that memset is used to initialize vectors to all 0's that will store the sum of two others vectors. For example:

hostRef = (float *)malloc(nBytes);
gpuRef = (float *)malloc(nBytes);    
memset(hostRef, 0, nBytes);
memset(gpuRef, 0, nBytes);

What purpose does this serve if nothing else is done with these vectors?

You can see the code here: https://books.google.com/books?id=Jgx_BAAAQBAJ&pg=PA42#v=onepage&q&f=false

Not sure how long the link will work though.

When you acquire memory using 'malloc' it is not necessarily empty, only 'calloc' will zero the memory for you. It's recommended to initialize your memory for sanity and debugging purposes.

It would serve no purpose if nothing else were done with those vectors, but that is not the case.

The code runs a CUDA vector sum, and then copies the result into *gpuRef . It then performs the same sum on the host CPU, and puts the result in *hostRef . Finally, it compares the two results.

Of course, it doesn't do anything with either array before copying new data into it, so the initialization to zero still serves no purpose.

This is the answer given by njuffa in the comments:

...The content of GPU memory doesn't change between invocations of the application. In case of a program failure, we would want to avoid picking up good data from a previous run, which may lead (erroneously) to a belief that the program executed fine. I have seen such cases in real-life, and it was very confusing to the affected programmers. Thus it is better to initialize result data to a known value, although I would have chosen 0xff instead of 0 as this corresponds to a NaN pattern for floating-point data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM