【问题标题】:How to build single-threaded TensorFlow 2.x from source如何从源代码构建单线程 TensorFlow 2.x
【发布时间】:2020-10-21 08:42:49
【问题描述】:

从源代码构建 TensorFlow 2.x(用于 CPU)时,我应该进行哪些更改来强制 TensorFlow不要使用超过 1 个线程?如果这不可能,我应该更改哪些特定的 c++ 语句(以及在哪些 cpp 文件中)以抑制多线程的生成?

不管cpus/cores是多少,我从TensorFlow 2.x一共需要1个线程。

使用top -H -b -n1 | grep program_name | wc -l统计线程总数。

【问题讨论】:

标签: c++ c++11 tensorflow2.0 tensorflow-c++


【解决方案1】:

解决方案是在 C++ 中您可以为会话提供的选项:

// set the number of worker threads
    tensorflow::SessionOptions options;
    tensorflow::ConfigProto & configuration = options.config;
    configuration.set_inter_op_parallelism_threads(1);
    configuration.set_intra_op_parallelism_threads(1);
    configuration.set_use_per_session_threads(false); 

  mySession->reset(tensorflow::NewSession(options));

这样你就只有一个工作线程了。

但这并不能确保top -H -b -n1 | grep program_name | wc -l 命令只返回 1 个线程。事实上,在上面的例子中,我们谈到了一个工作线程,但可以肯定的是,至少有一个主线程来管理工作线程的生成和返回。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多