17

I'm using Tensorflow on a cluster and I want to tell Tensorflow to run only on one single core (even though there are more available).

Does someone know if this is possible?

Franck Dernoncourt
  • 77,520
  • 72
  • 342
  • 501
jojo123456
  • 341
  • 1
  • 3
  • 11

3 Answers3

32

To run Tensorflow on one single CPU thread, I use:

session_conf = tf.ConfigProto(
      intra_op_parallelism_threads=1,
      inter_op_parallelism_threads=1)
sess = tf.Session(config=session_conf)

device_count limits the number of CPUs being used, not the number of cores or threads.

tensorflow/tensorflow/core/protobuf/config.proto says:

message ConfigProto {
  // Map from device type name (e.g., "CPU" or "GPU" ) to maximum
  // number of devices of that type to use.  If a particular device
  // type is not found in the map, the system picks an appropriate
  // number.
  map<string, int32> device_count = 1;

On Linux you can run sudo dmidecode -t 4 | egrep -i "Designation|Intel|core|thread" to see how many CPUs/cores/threads you have, e.g. the following has 2 CPUs, each of them has 8 cores, each of them has 2 threads, which gives a total of 2*8*2=32 threads:

fra@s:~$ sudo dmidecode -t 4 | egrep -i "Designation|Intel|core|thread"
    Socket Designation: CPU1
    Manufacturer: Intel
            HTT (Multi-threading)
    Version: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
    Core Count: 8
    Core Enabled: 8
    Thread Count: 16
            Multi-Core
            Hardware Thread
    Socket Designation: CPU2
    Manufacturer: Intel
            HTT (Multi-threading)
    Version: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
    Core Count: 8
    Core Enabled: 8
    Thread Count: 16
            Multi-Core
            Hardware Thread

Tested with Tensorflow 0.12.1 and 1.0.0 with Ubuntu 14.04.5 LTS x64 and Ubuntu 16.04 LTS x64.

Franck Dernoncourt
  • 77,520
  • 72
  • 342
  • 501
  • 1
    Unfortunately, this appears to have no effect when running on WIndows 10 (tf 1.5.0). It is a problem not to have a way to leave a core free for other programs. – Elroch Feb 10 '18 at 11:57
  • @LiamRoche I don't think this is supposed to happen. You may want to raise an issue in the tensorflow GitHub repository. – Franck Dernoncourt Feb 10 '18 at 20:05
  • Don't we need to add `device_count={'GPU': 0}` ? – mrgloom May 26 '19 at 20:19
  • 3
    for tf v2: `tf.config.threading.set_inter_op_parallelism_threads(1) tf.config.threading.set_intra_op_parallelism_threads(1)` – caki Jan 06 '20 at 21:30
  • Following your code wht the following CPU I get 8 threads. How can I get only 1 thread? Version: Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz Core Count: 4 Core Enabled: 4 Thread Count: 8 Multi-Core Hardware Thread – fisakhan Aug 14 '20 at 18:15
  • My system has 8 cores, I have set all configuration according to above, It is using all cores but with low percent like 20% using 1 core, 20% second and so on,, – Hamza Jan 05 '22 at 08:40
2

Yes it is possible by thread affinity. Thread affinity allows you to decide which specific thread to be executed by which specific core of the cpu. For thread affinity you can use "taskset" or "numatcl" on linux. You can also use https://man7.org/linux/man-pages/man2/sched_setaffinity.2.html and https://man7.org/linux/man-pages/man3/pthread_setaffinity_np.3.html

The following code will not instruct/direct Tensorflow to run only on one single core.

TensorFlow 1

session_conf = tf.ConfigProto(
      intra_op_parallelism_threads=1,
      inter_op_parallelism_threads=1)
sess = tf.Session(config=session_conf)

TensorFlow 2

import os
# reduce number of threads
os.environ['TF_NUM_INTEROP_THREADS'] = '1'
os.environ['TF_NUM_INTRAOP_THREADS'] = '1'
import tensorflow

This will generate in total at least N threads, where N is the number of cpu cores. Most of the time only one thread will be running while others are in sleeping mode.

Sources: https://github.com/tensorflow/tensorflow/issues/42510 https://github.com/tensorflow/tensorflow/issues/33627

fisakhan
  • 704
  • 1
  • 9
  • 27
1

You can restrict the number of devices of a certain type that TensorFlow uses by passing the appropriate device_count in a ConfigProto as the config argument when creating your session. For instance, you can restrict the number of CPU devices as follows :

config = tf.ConfigProto(device_count={'CPU': 1})
sess = tf.Session(config=config)
with sess.as_default():
  print(tf.constant(42).eval())
keveman
  • 8,427
  • 1
  • 38
  • 46
  • 7
    I have tried this, but it does not work. If I submit a job to the cluster, Tensorflow still works on all available cores of one node. I do the following: init = tf.initialize_all_variables() #launch the graph config = tf.ConfigProto(device_count={'CPU': 1}) sess = tf.Session(config=config) sess.run(init) – jojo123456 Jul 09 '16 at 15:31
  • 1
    I also experienced the same problem. tf.ConfigProto(device_count={'CPU': 1}) does not take effect. intra_op_parallelism_threads=1 and inter_op_parallelism_threads=1 do take effect. – fisakhan Aug 18 '20 at 09:04