正确使用cudaDeviceReset() [英] Proper use of cudaDeviceReset()

查看:1445
本文介绍了正确使用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()中的最后一个命令,对吗?

$ b $如Talonmies所示,在已经调用cudaDeviceReset()函数之后调用类A的析构函数,即当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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆