简体   繁体   English

为什么在使用CUDA时使用memset?

[英]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. 我在CUDA代码示例中看到, memset用于将向量初始化为全0,以存储其他两个向量的和。 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 您可以在此处查看代码: 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. 当您使用'malloc'获取内存时,它不一定为空,只有'calloc'会为您将内存清零。 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 . 该代码运行CUDA向量和,然后将结果复制到*gpuRef It then performs the same sum on the host CPU, and puts the result in *hostRef . 然后,它在主机CPU上执行相同的总和,并将结果放入*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: 这是njuffa在评论中给出的答案:

...The content of GPU memory doesn't change between invocations of the application. ... GPU内存的内容在应用程序的调用之间不会改变。 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. 因此,最好将结果数据初始化为一个已知值,尽管我会选择0xff而不是0,因为这对应于浮点数据的NaN模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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