我在尝试追踪内存使用情况时遇到了 Geforce GTX 690 的问题。一个简单的测试程序:
BOOST_AUTO_TEST_CASE(cudaMemoryTest) {
size_t mem_tot_0 = 0;
size_t mem_free_0 = 0;
size_t mem_tot_1 = 0;
size_t mem_free_1 = 0;
unsigned int mem_size = 100*1000000;
float* h_P = new float[mem_size];
for(size_t i = 0; i < mem_size; i++) {
h_P[i] = 0.f;
}
cudaSetDevice(0);
cudaDeviceReset();
cudaMemGetInfo (&mem_free_0, & mem_tot_0);
std::cout<<"Free memory before copy dev 0: "<<mem_free_0<<std::endl;
cudaSetDevice(1);
cudaDeviceReset();
cudaMemGetInfo (&mem_free_1, &mem_tot_1);
std::cout<<"Free memory before copy dev 1: "<<mem_free_1<<std::endl;
cudaSetDevice(0);
float* P;
cudaMalloc((void**)&P, mem_size*sizeof(float));
cudaMemcpy((void*)P, h_P, mem_size*sizeof(float), cudaMemcpyHostToDevice);
cudaSetDevice(0);
cudaMemGetInfo(&mem_free_0, & mem_tot_0);
std::cout<<"Free memory after copy dev 0: "<<mem_free_0<<std::endl;
cudaSetDevice(1);
cudaMemGetInfo(&mem_free_1, &mem_tot_1);
std::cout<<"Free memory after copy dev 1: "<<mem_free_1<<std::endl;
BOOST_CHECK(mem_free_0 != mem_free_1);
cudaError_t err;
err = cudaGetLastError();
if(err!=cudaSuccess)
std::cout<<"an error occurred"<<std::endl;
cudaSetDevice(0);
destroyMem(P);
delete [] h_P;
}
测试打印出来:
1> Free memory before copy dev 0: 1733173248
1> Free memory before copy dev 1: 1688424448
1> Free memory after copy dev 0: 1289940992
1> Free memory after copy dev 1: 1289940992
CudaUtilsTest.cpp(47): error in "cudaMemoryTest": check mem_free_0 != mem_free_1 failed
问题是分配后设备 1 上的空闲内存量与设备 0 上的完全一样,这不应该是这种情况,因此问题必须在 cudaMemGetInfo 和/或 cudaSetDevice 中。任何人都在同样的问题上运行,或者有人可以指出测试中是否存在其他根本性的错误?
在 Windows 7、Visual Studio 2010、Cuda SDK 5.0 上运行代码,使用代码生成进行编译:compute_30,sm_30
编辑 22.4.2013
我继续试验这个问题,似乎 cudaSetDevice 工作正常,可以从 cudaGetDevice 调用的结果中得到验证。我在内存分配测试之后添加了设备 0 的重置,并且似乎 cudaMemGetInfo 返回的可用内存大小对于两个设备来说再次相同。我已经在自己的代码中检查了 cuda_error_t 的所有返回值,并且所有函数调用都返回 cudaSuccess。有没有人在上面描述的设置中遇到过与 GTX 690 类似的问题?
最新测试代码:
BOOST_AUTO_TEST_CASE(cudaMemoryTest) {
size_t mem_tot_0 = 0;
size_t mem_free_0 = 0;
size_t mem_tot_1 = 0;
size_t mem_free_1 = 0;
int device_num = 0;
unsigned int mem_size = 100*1000000;
float* h_P = new float[mem_size];
for(size_t i = 0; i < mem_size; i++) {
h_P[i] = 0.f;
}
cudaSetDevice(0);
cudaGetDevice(&device_num);
cudaDeviceReset();
cudaMemGetInfo (&mem_free_0, & mem_tot_0);
std::cout<<"Free memory before copy dev 0: "<<mem_free_0<<" Device: "<<device_num<<std::endl;
cudaDeviceSynchronize();
cudaSetDevice(1);
cudaGetDevice(&device_num);
cudaDeviceReset();
cudaMemGetInfo (&mem_free_1, & mem_tot_1);
std::cout<<"Free memory before copy dev 1: "<<mem_free_1<<" Device: "<<device_num<<std::endl;
cudaDeviceSynchronize();
cudaSetDevice(0);
cudaGetDevice(&device_num);
float* P;
cudaMalloc((void**)&P, mem_size*sizeof(float));
cudaMemcpy((void*)P, h_P, mem_size*sizeof(float), cudaMemcpyHostToDevice);
cudaMemGetInfo(&mem_free_0, & mem_tot_0);
std::cout<<"Free memory after copy dev 0: "<<mem_free_0<<" Device: "<<device_num<<std::endl;
cudaDeviceSynchronize();
cudaSetDevice(1);
cudaGetDevice(&device_num);
cudaMemGetInfo(&mem_free_1, &mem_tot_1);
std::cout<<"Free memory after copy dev 1: "<<mem_free_1<<" Device: "<<device_num<<std::endl;
cudaDeviceSynchronize();
BOOST_CHECK(mem_free_0 != mem_free_1);
cudaError_t err;
err = cudaGetLastError();
if(err!=cudaSuccess)
std::cout<<"an error occurred"<<std::endl;
// Reset only device 0 and check both
cudaSetDevice(0);
cudaGetDevice(&device_num);
cudaDeviceReset();
cudaMemGetInfo (&mem_free_0, & mem_tot_0);
std::cout<<"Free memory after second reset of device 0, dev 0: "<<mem_free_0<<" Device: "<<device_num<<std::endl;
cudaDeviceSynchronize();
cudaSetDevice(1);
cudaGetDevice(&device_num);
cudaMemGetInfo (&mem_free_1, & mem_tot_1);
std::cout<<"Free memory after second device reset of device 0, dev 1: "<<mem_free_1<<" Device: "<<device_num<<std::endl;
cudaDeviceSynchronize();
delete [] h_P;
}
测试输出:
1> Free memory before copy dev 0: 1794379776 Device: 0
1> Free memory before copy dev 1: 1751728128 Device: 1
1> Free memory after copy dev 0: 1351696384 Device: 0
1> Free memory after copy dev 1: 1351696384 Device: 1
1> CudaUtilsTest.cpp(353): error in "cudaMemoryTest": check mem_free_0 != mem_free_1 failed
1> Free memory after second reset of device 0, dev 0: 1751728128 Device: 0
1> Free memory after second device reset of device 0, dev 1: 1751728128 Device: 1