0
由于没有空闲的计算机内存,cudaMalloc可能无法分配吗?
尽管GPU内存可用,但当RAM(计算机内存)已满时,cudaMalloc将失败。
我的代码可以在下面的示例代码sumamrized:当使用计算机内存时,cudaMalloc失败
int main()
{
size_t N=sizeof(int)*100000000;
while(true)
{
int *d_a,*d_b;
if (cudaSuccess !=cudaMalloc(&d_a, N)) printf("Error Allocating GPU Memory");
if (cudaSuccess !=cudaMalloc(&d_b, N)) printf("Error Allocating GPU Memory");
cudaMemset(d_a,1,N);
cudaMemset(d_b,2,N);
int *h_a= (int *)malloc(N);
int *h_b=(int *)malloc(N);
if(!h_a || !h_b) printf("Error Allocating CPU Memory");
cudaMemcpy(d_a,h_a, N, cudaMemcpyHostToDevice);
cudaMemcpy(d_b,h_b, N, cudaMemcpyHostToDevice);
cudaFree(d_a);
cudaFree(d_b);
}
getch();
return 1;
}
下面的代码失败,只有当计算机内存已满分配GPU内存! 我得到的错误是Error Allocating GPU Memory
而不是Error Allocating CPU Memory
可能cudaMalloc也需要一些主机内存才能完成。 – Anycorn 2011-12-28 11:18:14