为什么 cudaMalloc() 使用指针指向指针?

2021-12-13 00:00:00 pointers c cuda c++

例如,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?

推荐答案

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

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函数.

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.

相关文章