0

I am trying to restrict the number of threads that TensorFlow spawns. In python, I understand we need to use the following steps as pointed out Here. I was trying to do the same in CPP, but it doesn't seem that straight forward. Questions:

  1. How to modify intra_op_parallelism_threads and inter_op_parallelism_threads correctly?
  2. How to modify the device_count to control the core as well?
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);
genpfault
  • 51,148
  • 11
  • 85
  • 139
amit pandey
  • 86
  • 1
  • 8
  • If you know, can you please share the solution for how to run TensorFlow on single core, single thread? – fisakhan Sep 28 '20 at 10:21

2 Answers2

0

Answer to question 1:

tensorflow::SessionOptions options;
tensorflow::ConfigProto & config = options.config;
config.set_inter_op_parallelism_threads(1);
config.set_intra_op_parallelism_threads(1);
session->reset(tensorflow::NewSession(options));

This will reduce the total number of threads (but not to 1) generated by TensorFlow. The total number of threads generated by TensorFlow will still be multiple, depending on the number of cores in the CPU. In most of the cases, only one thread will be active while others will be in sleeping mode. I don't think it is possible to have a single-threaded TensorFlow.

The following github issues support my point of view. https://github.com/tensorflow/tensorflow/issues/33627 https://github.com/usnistgov/frvt/issues/30

fisakhan
  • 704
  • 1
  • 9
  • 27
  • You are absolutely right @fisa. What worked for me was the following: 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)); – amit pandey Oct 02 '20 at 17:18
  • What is the answer to Question 2? – fisakhan Oct 03 '20 at 20:09
0

Answer to 1 as Fisa pointed out is correct. With just minor adjustment as config is a pointer.

 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));
amit pandey
  • 86
  • 1
  • 8