cudaMemGetInfo占用的可用内存
我有以下简单的代码来查找可用的GPUcudaMemGetInfo占用的可用内存,cuda,gpu,Cuda,Gpu,我有以下简单的代码来查找可用的GPU int * getFreeGpuList(int *numFree) { int * gpuList; int nDevices; int i, j = 0, count = 0; cudaGetDeviceCount(&nDevices); gpuList = (int *) malloc(nDevices * sizeof(int)); for (i = 0; i < nDevices; +
int * getFreeGpuList(int *numFree) {
int * gpuList;
int nDevices;
int i, j = 0, count = 0;
cudaGetDeviceCount(&nDevices);
gpuList = (int *) malloc(nDevices * sizeof(int));
for (i = 0; i < nDevices; ++i) {
cudaSetDevice(i);
size_t freeMem;
size_t totalMem;
cudaMemGetInfo(&freeMem, &totalMem);
if (freeMem > .9 * totalMem) {
gpuList[j] = i;
count++;
j++;
}
}
*numFree = count;
return gpuList;
}
int*getFreeGpuList(int*numFree){
int*gpuList;
内部设备;
int i,j=0,count=0;
cudaGetDeviceCount(和设备);
gpuList=(int*)malloc(nDevices*sizeof(int));
对于(i=0;i.9*totalMem){
gpuList[j]=i;
计数++;
j++;
}
}
*numFree=计数;
返回gpuList;
}
问题是
cudaMemGetInfo
在每个GPU中都会占用一些内存(在我的例子中约为150MB)。这段代码是一个运行时间很长的大型程序的一部分,我经常同时运行多个进程,因此最终这个函数占用的内存非常大。您能告诉我如何释放cudaMemGetInfo所占用的GPU内存吗?谢谢 根据上面的一些见解,创建了一个上下文并在设备中占用了一些内存,我发现它可以“在当前进程中显式地销毁和清理与当前设备相关的所有资源”,而不会影响同一设备上的其他进程
11月26日更新:如果想要查询GPU信息,最好使用库。根据我的经验,它的速度要快得多,而且不占用简单内存和名称查询的内存。
cudaSetDevice
建立了一个上下文,即内存所在的位置,对于i@talonmies您无能为力。我找到了这个函数cudadeviceset
。我试过了,似乎达到了我的目的。我把它放在for循环的末尾(在设置下一个设备之前),但我不确定它是否包含任何潜在的危险。你知道吗?那会破坏环境的。但是如果你的意图是为了某件事而使用上下文,那么你会阻止这件事发生working@talonmies不,这之后我唯一需要的就是免费设备的列表。从documentation>显式销毁和清除当前进程中与当前设备关联的所有资源。对该设备的任何后续API调用都将重新初始化该设备。这正是我需要的。谢谢!你想用你的答案写一个简短的回答吗。我很乐意投它一票。那么我们就有了下一个问题的答案,谢谢你添加这个答案