为什么cudaMalloc()使用指针指针? [英] Why does cudaMalloc() use pointer to pointer?

查看:387
本文介绍了为什么cudaMalloc()使用指针指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如, cudaMalloc((void **)& device_array,num_bytes);

之前曾要求,答复是因为 cudaMalloc 返回一个错误代码,但我不知道 - 有一个双指针与返回错误代码有什么关系?为什么一个简单的指针不能做这项工作?

This question has been asked before, and the reply was "because cudaMalloc returns an error code", but I don't get it - what has a double pointer got to do with returning an error code? Why can't a simple pointer do the job?

如果我写

cudaError_t catch_status;
catch_status = cudaMalloc((void**)&device_array, num_bytes);

错误代码将放在 catch_status ,并且返回一个简单的指针到分配的GPU内存应该足够了,不应该吗?

the error code will be put in catch_status, and returning a simple pointer to the allocated GPU memory should suffice, shouldn't it?

推荐答案

通过值或通过模拟传递引用(即通过指向数据的指针)。按值是一种单向方法,通过指针允许函数及其调用环境之间的双向数据流。

In C, data can be passed to functions by value or via simulated pass-by-reference (i.e. by a pointer to the data). By value is a one-way methodology, by pointer allows for two-way data flow between the function and its calling environment.

当数据项传递给函数通过函数参数列表,并且该函数期望修改原始数据项,使得修改的值出现在调用环境中,正确的C方法是通过指针传递数据项。在C中,当我们通过指针时,我们获取要修改的项的地址,创建一个指针(在这种情况下可能是一个指针),并将地址传递给函数。这允许函数在调用环境中修改原始项(通过指针)。

When a data item is passed to a function via the function parameter list, and the function is expected to modify the original data item so that the modified value shows up in the calling environment, the correct C method for this is to pass the data item by pointer. In C, when we pass by pointer, we take the address of the item to be modified, creating a pointer (perhaps a pointer to a pointer in this case) and hand the address to the function. This allows the function to modify the original item (via the pointer) in the calling environment.

通常 malloc 返回一个指针,我们可以在调用环境中使用赋值将这个返回的值赋给所需的指针。在 cudaMalloc 的情况下,CUDA设计者选择使用返回值携带错误状态而不是指针。因此,调用环境中的指针的设置必须通过参考(即通过指针)传递给函数的参数之一发生。因为它是一个我们想要设置的指针值,所以我们必须取指针的地址(创建一个指向指针的指针),并将该地址传递给 cudaMalloc code>函数。

Normally malloc returns a pointer, and we can use assignment in the calling environment to assign this returned value to the desired pointer. In the case of cudaMalloc, the CUDA designers chose to use the returned value to carry an error status rather than a pointer. Therefore the setting of the pointer in the calling environment must occur via one of the parameters passed to the function, by reference (i.e. by pointer). Since it is a pointer value that we want to set, we must take the address of the pointer (creating a pointer to a pointer) and pass that address to the cudaMalloc function.

这篇关于为什么cudaMalloc()使用指针指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆