我曾与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软件开发包5.0上运行的代码,代码生成编译:compute_30,sm_30
编辑2013年4月22日
我这个问题继续进行试验,似乎cudaSetDevice工作正常,这可以从cudaGetDevice调用的结果进行验证。 我添加的设备0的复位内存分配测试后,似乎由cudaMemGetInfo返回可用内存大小是两个设备再次相同。 我已经检查cuda_error_t的所有返回值在我自己的代码和所有的函数调用返回cudaSuccess。 有没有人遇到与GTX 690以上descibed设置类似的问题?
最反感的测试代码:
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