为什么第一个Cudamaloc是唯一的瓶颈?
我定义了这个函数:为什么第一个Cudamaloc是唯一的瓶颈?,cuda,Cuda,我定义了这个函数: void cuda_entering_function(...) { StructA *host_input, *dev_input; StructB *host_output, *dev_output; host_input = (StructA*)malloc(sizeof(StructA)); host_output = (StructB*)malloc(sizeof(StructB)); cudaMalloc(&dev
void cuda_entering_function(...)
{
StructA *host_input, *dev_input;
StructB *host_output, *dev_output;
host_input = (StructA*)malloc(sizeof(StructA));
host_output = (StructB*)malloc(sizeof(StructB));
cudaMalloc(&dev_input, sizeof(StructA));
cudaMalloc(&dev_output, sizeof(StructB));
... some more other cudaMalloc()s and cudaMemcpy()s ...
cudaKernel<< ... >>(dev_input, dev_output);
...
}
void cuda\u进入函数(…)
{
StructA*主机输入,*开发输入;
StructB*主机输出,*开发输出;
主机输入=(StructA*)malloc(sizeof(StructA));
主机输出=(StructB*)malloc(sizeof(StructB));
cudamaloc(&dev_input,sizeof(StructA));
cudamaloc(&dev_output,sizeof(StructB));
…其他一些cudamaloc()和cudaMemcpy()的。。。
cudaKernel>(开发输入,开发输出);
...
}
这个函数在我的程序中被调用了几次(大约5~15次),我使用gettimeofday()
测量了这个程序的性能
然后我发现cuda\u进入函数()
的瓶颈是我整个程序中的第一个cudamaloc()
——第一个cudamaloc()
。cuda\u entering\u function()
总执行时间的95%以上被第一个cudamaloc()
消耗,当我更改第一个cudamaloc()
分配内存的大小或更改cudamaloc()
的执行顺序时,也会发生这种情况
原因是什么?有没有办法缩短第一次cuda分配时间?第一次
cudaMalloc
也负责设备的初始化,因为它是对涉及设备的任何函数的第一次调用。这就是为什么你会受到这样的打击:这是由于使用CUDA和GPU造成的开销。您应该确保应用程序能够获得足够的加速,以补偿开销
通常,人们通过调用初始化函数来设置设备。作为回答,您可以看到调用cudaFree(0)
显然是实现这一点的标准方法。显示了CUDASETDEVIECE的使用,如果您在具有多个CUDA就绪设备的机器上工作,这可能是一个好习惯