简体   繁体   中英

How to build single-threaded TensorFlow 2.x from source

While building TensorFlow 2.x (for CPU) from source, what change should I make to force the TensorFlow not to use more than 1 threads ? If this is not possible, what specific c++ statements (and in which cpp files) should I change to suppress the generation of multiple threads?

No matter what the number of cpus/cores are, I need 1 thread in total from TensorFlow 2.x.

Use top -H -b -n1 | grep program_name | wc -l top -H -b -n1 | grep program_name | wc -l top -H -b -n1 | grep program_name | wc -l to count the total number of threads.

The solution is in C++ the options you can give to a session:

// 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));

In this way you will have only a worker thread.

But this not ensure that top -H -b -n1 | grep program_name | wc -l top -H -b -n1 | grep program_name | wc -l top -H -b -n1 | grep program_name | wc -l command return 1 thread only. In fact in the above example we speeking about a worker thread, but for sure there is at least the main thread that manage the spawn and the return of the working threads.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM