How can I confine TensorFlow C API to use one and only one thread in total
Asked Answered
G

0

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);
Goon answered 20/8, 2020 at 14:26 Comment(1)
Although hints provided at the following links were very useful they didn't solve the problem github.com/tensorflow/tensorflow/issues/… unix.stackexchange.com/questions/23106/… github.com/tensorflow/tensorflow/issues/13853Goon

© 2022 - 2024 — McMap. All rights reserved.