首页 > 解决方案 > 如何在单核、单线程 CPP 上运行 Tensorflow?

问题描述

我正在尝试限制 TensorFlow 生成的线程数。在 python 中,我知道我们需要使用此处指出的以下步骤。我试图在 CPP 中做同样的事情,但看起来并不那么简单。问题:

  1. 如何正确修改intra_op_parallelism_threads和inter_op_parallelism_threads?
  2. 如何修改 device_count 来控制核心?
SessionOptions options;
ConfigProto* config = &options.config;
string key = "CPU";
//not sure if this is the correct way to do it.
(*config->mutable_device_count())[key] = 1; 
config->set_inter_op_parallelism_threads(1);
config->set_intra_op_parallelism_threads(1);

标签: c++multithreadingtensorflowdeep-learningtensorflow-c++

解决方案


正如 Fisa 指出的那样,对 1 的回答是正确的。只需稍作调整,因为 config 是一个指针。

 SessionOptions options;
ConfigProto* config = &options.config;
//single thread control//
config->set_inter_op_parallelism_threads(1);
config->set_intra_op_parallelism_threads(1);    
fSession.reset(NewSession(options));

推荐阅读