我定义了这个函数:
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);
...
}
Run Code Online (Sandbox Code Playgroud)
在我的程序中,这个函数被调用了几次(大约5~15次),我用这个函数测量了这个程序的性能gettimeofday()
.
然后我发现瓶颈cuda_entering_function()
是第一个cudaMalloc()
- cudaMalloc()
整个程序中的第一个.第一次cuda_entering_function()
占用总执行时间的95%以上,cudaMalloc()
当我改变第一次cudaMalloc()
分配内存的大小或改变cudaMalloc()
s 的执行顺序时也会发生这种情况.
是什么原因,有没有办法减少第一个cuda分配时间?
cuda ×1