CUDA 中的 cudaMalloc使用二重指针(void**)的一些理解

       CUDA C和C有很多相似的地方,其中的cudaMalloc和C语言的malloc就是一粒。但首次使用cudaMalloc时还是难于理解其为啥要使用二重指针。对于这个问题我思考了一段时间,也找了一些资料查看,现在觉得算是理解了,故在此记录整理一下。

       cudaMalloc和C语言的malloc都是为了动态的分配指定字节的内存。首先我们看一下这两个函数的原始定义。

       1、void *malloc(size_t size)

   2、__host__ ​ __device__ ​cudaError_t cudaMalloc ( void** devPtr, size_t size )

            Allocate memory on the device.

          Parameters

           devPtr

          - Pointer to allocated device memory

           size

         - Requested allocation size in bytes

           Returns

          cudaSuccess, cudaErrorInvalidValue, cudaErrorMemoryAllocation

通过上面我看到,两个函数的最大的不同是函数的返回值类型,malloc返回的是void *,而cudaMalloc返回的是cudaError_t(它不是指针类型哦,对其实根本原因就是这个),CUDA中的函数好像都是采用这种结构返回值类型为cudaError_t,但现在我们是想得到函数cudaMalloc在device上申请的内存,这可咋搞?既然要把申请的内存地址返回给一个指针,那就用二重指针解决吧,这样既解决了CUDA函数返回类型的统一,也解决了申请的内存地址返回给一个指针问题

下面是我的一个小实验用于证实这个想法:

#include <stdio.h>
#include<stdlib.h>

void P1Malloc(int* p)
{
	p=(int*)malloc(10);
	printf("P1Malloc,current malloc address:%p\n",p);
}

void P2Malloc(void** p)
{
	*p=malloc(10);
	printf("P2Malloc,current malloc address:%p\n",*p);
}

int main()
{
	int Num=10;
	int* a=&Num;
	printf("initial pointer a:%p\n",a);
	P1Malloc(a);
	printf("after using *,ponter a:%p\n",a);
	P2Malloc((void**)&a);
	printf("after using **,ponter a:%p\n",a);

	return 0;
}

打印结果如下:

initial pointer a:0x7ffd4751803c
P1Malloc,current malloc address:0xd8b500
after using *,ponter a:0x7ffd4751803c
P2Malloc,current malloc address:0xd8b520
after using **,ponter a:0xd8b520

程序不多解释,有点c语言基础一看就懂,实验发现只有void P2Malloc(void** p)真正把新申请的内存地址给了指定变量。

好了理解了这些基本上也就解决了这个疑惑。

在查阅资料时遇到了一些好的blog放置在此

1、https://stackoverflow.com/questions/7989039/use-of-cudamalloc-why-the-double-pointer

这篇blog的问答都很不错,我摘录一段回答得分最好的网友的解释:

All CUDA API functions return an error code (or cudaSuccess if no error occured). All other parameters are passed by reference. However, in plain C you cannot have references, that's why you have to pass an address of the variable that you want the return information to be stored. Since you are returning a pointer, you need to pass a double-pointer.

Another well-known function which operates on addresses for the same reason is the scanf function. How many times have you forgotten to write this & before the variable that you want to store the value to? ;)

int i;
scanf("%d",&i);

2、https://blog.csdn.net/TwT520Ly/article/details/81100301

这篇blog没有过多理论,主要使用了一些示例代码,简洁明了,我的示例程序和她很相似。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值