正确使用cudaDeviceReset() [英] Proper use of cudaDeviceReset()
问题描述
因为我怀疑黑盒子(GPU)在一些更大的代码中没有干净地关闭(其他也许),我会在 main()$结尾处添加
cudaDeviceReset()
c $ c>。可是等等!这将分段故障
在 main()
中静态创建的类的所有实例在析构函数中具有非平凡的CUDA代码,右?例如
class A {
public:
cudaEvent_t tt;
cudaEvent_t uu;
A(){
cudaEventCreate(& tt);
cudaEventCreate(& uu);
}
〜A(){
cudaEventDestroy(tt);
cudaEventDestroy(uu);
}
};
静态实例化:
int main(){
A t;
cudaDeviceReset();
return 0;出口处的
}
segfaults。问题:或许 cudaDeviceReset()
在退出 main()时自动调用
?
否则 main()
的整个有用代码应该转移到一些 run()
和 cudaDeviceReset()
应该是 main()中的最后一个命令
,对吗?
我想,你可以将cudaDeviceReset()传递给一个atexit(..)函数。
void myexit(){
cudaDeviceReset();
}
int main(...){
atexit(myexit);
A t;
return 0;
}
Since I'm having suspicions the "black box" (GPU) is not shutting down cleanly in some larger code (others perhaps too), I would include a cudaDeviceReset()
at the end of main()
. But wait! This would Segmentation fault
all instances of classes statically created in main()
with non-trivial CUDA code in destructors, right? E.g.
class A {
public:
cudaEvent_t tt;
cudaEvent_t uu;
A() {
cudaEventCreate(&tt);
cudaEventCreate(&uu);
}
~A(){
cudaEventDestroy(tt);
cudaEventDestroy(uu);
}
};
instantiated statically:
int main() {
A t;
cudaDeviceReset();
return 0;
}
segfaults on exit. Question: is perhaps cudaDeviceReset()
invoked automatically on exit from main()
?
Otherwise whole useful code of main()
should be shifted to some run()
, and cudaDeviceReset()
should be the as last command in main()
, right?
As indicated by Talonmies, the destructor of class A is called after the cudaDeviceReset() function is already called, namely when the main(..) function finishes.
I think, you may take cudaDeviceReset() to an atexit(..) function.
void myexit() {
cudaDeviceReset();
}
int main(...) {
atexit(myexit);
A t;
return 0;
}
这篇关于正确使用cudaDeviceReset()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!