Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run Tensorflow on one single core?

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?

like image 418
jojo123456 Avatar asked Jul 04 '16 15:07

jojo123456


People also ask

How many threads can run on a single core?

A single CPU core can have up-to 2 threads per core. For example, if a CPU is dual core (i.e., 2 cores) it will have 4 threads.

Can you run TensorFlow on CPU?

TensorFlow supports running computations on a variety of types of devices, including CPU and GPU.

Does TensorFlow automatically use multiple CPUs?

All cores are wrapped in cpu:0, i.e., TensorFlow does indeed use multiple CPU cores by default.


2 Answers

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.

like image 61
Franck Dernoncourt Avatar answered Sep 20 '22 12:09

Franck Dernoncourt


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())
like image 31
keveman Avatar answered Sep 21 '22 12:09

keveman