1

I'm using TensorFlow C API in C++ on linux to load a face detection model for inference. TensorFlow generates multiple threads and I want to restrict or confine the process to use one and only one thread. How can I confine TensorFlow C API to generate only one thread.

Linux command lscpu shows CPU(s) = 8 and top -H -b -n1 | grep validate11 | wc -l returns 8 after using the following code

Graph = TF_NewGraph();
Status = TF_NewStatus();
SessionOpts = TF_NewSessionOptions(); 

// limit number of threads  
uint8_t intra_op_parallelism_threads = 1;
uint8_t inter_op_parallelism_threads = 1;
uint8_t device_count = 1;//device_count limits the number of CPUs.**strong text**
uint8_t config[15] = {0xa, 0x7, 0xa, 0x3, 0x43, 0x50, 0x55, 0x10, device_count, 0x10, intra_op_parallelism_threads, 0x28, intra_op_parallelism_threads,0x40, 0x1};
TF_SetConfig(SessionOpts, (void*)config, 13, Status);

if (TF_GetCode(Status)!= TF_OK)
  std::cout << "\nERROR: " << TF_Message(Status);
RunOpts = NULL;

// load model
Session = TF_LoadSessionFromSavedModel(SessionOpts, RunOpts, saved_model_dir, &tags, ntags, Graph, NULL, Status);
if( TF_GetCode(Status) != TF_OK ) {
   std::cout << "\nERROR: Failed to load SavedModel." << TF_Message(Status);
   return -1;  }
assert( TF_GetCode(Status) == TF_OK);
fisakhan
  • 704
  • 1
  • 9
  • 27
  • Although hints provided at the following links were very useful they didn't solve the problem https://github.com/tensorflow/tensorflow/issues/33627#issuecomment-553265199 https://unix.stackexchange.com/questions/23106/how-to-limit-a-process-to-one-cpu-core-in-linux https://github.com/tensorflow/tensorflow/issues/13853 – fisakhan Aug 20 '20 at 14:39

0 Answers0