在tf源码中有这么一段注释:
// Possible values:
// * global: PluggableDevice uses threads shared with CPU in the main
// compute thread-pool. This is currently the default.
// * gpu_private: PluggableDevice uses threads dedicated to this device.
// * gpu_shared: All PluggableDevices share a dedicated thread pool.
之前只能理解意思,但并不明白为什么要这么设置。在阅读Optimize TensorFlow GPU performance with the TensorFlow Profiler | TensorFlow Core
后,大致理解了其中的意思。
在多卡的场景中(如原生分布式或者其他分布式),在默认的global场景中,cpu和gpu共用一个线程池,而在tf中,线程池的线程数默认为cpu核数。因此当卡数越多,cpu与gpu,gpu与gpu之间就会造成争抢,当某个gpu抢占的线程数过多时,其他的gpu就因无线程可用而陷入了阻塞状态。从而性能下降。
为了解决这个问题,可以设置为gpu_private,再搭配上TF_GPU_THREAD_COUNT,使得每个gpu都有自己的线程池,并限制每个线程池的线程个数,从而避免争抢。