我读过其他地方cudaMalloc将在内核之间进行同步。 (例如will cudaMalloc synchronize host and device?) 但是,我只是测试了这个代码,并根据我在视觉分析器中看到的情况,似乎cudaMalloc不同步。如果将cudaFree添加到循环中,那么确实会同步。我正在使用CUDA 7.5。有谁知道cudaMalloc是否改变了它的行为?或者我错过了一些微妙之处?非常感谢!已将cudaMalloc更改为异步?
__global__ void slowKernel()
{
float input = 5;
for(int i = 0; i < 1000000; i++){
input = input * .9999999;
}
}
__global__ void fastKernel()
{
float input = 5;
for(int i = 0; i < 100000; i++){
input = input * .9999999;
}
}
void mallocSynchronize(){
cudaStream_t stream1, stream2;
cudaStreamCreate(&stream1);
cudaStreamCreate(&stream2);
slowKernel <<<1, 1, 0, stream1 >>>();
int *dev_a = 0;
for(int i = 0; i < 10; i++){
cudaMalloc(&dev_a, 4 * 1024 * 1024);
fastKernel <<<1, 1, 0, stream2 >>>();
// cudaFree(dev_a); // If you uncomment this, the second fastKernel launch will wait until slowKernel completes
}
}
@RobertCrovella:在Maxwell设备上我正在测试它,它看起来像cudaMalloc不是同步调用。当我在上面的代码中修复内核中的明显缺陷并添加一些明智的nanosleep调用来扩展配置文件时间轴时,我看到cudaMalloc正在运行,而慢速和快速内核的两个实例在GPU上都处于活动状态。这可能是一个探查器问题,但它看起来不像我的眼睛:http://pastebin.com/rC8XxKmT – talonmies
我想我应该仔细看看。我也见证了它。更令人好奇的是,如果我让两个内核实际上都依赖于'dev_a'并且使得这两个内核实际上触及'dev_a'中的全局内容,我*仍然*见证了前两个内核的重叠,'cudaMalloc'在两者之间操作。我无法解释这一点。 –