Tensorflow
CPU
Machine Learning
Beginners Guide
Deep Learning

How to run Tensorflow on CPU

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

If you want TensorFlow to run on CPU 1, you are really asking about CPU affinity at the operating-system level. TensorFlow can be told to avoid GPUs, and it can be tuned for thread counts, but choosing a specific CPU core is normally done by the OS scheduler or by process-affinity tools such as taskset on Linux. The clean solution is to pin the process first and then let TensorFlow run normally inside that CPU set.

Disable GPU Use First if Necessary

On machines that also have GPUs, hide them before TensorFlow starts so the workload stays on the CPU.

bash
CUDA_VISIBLE_DEVICES=-1 python train.py

Or inside Python before creating TensorFlow devices:

python
1import tensorflow as tf
2
3tf.config.set_visible_devices([], "GPU")
4print(tf.config.list_physical_devices("GPU"))

That handles CPU versus GPU selection. It does not choose which CPU core will run the process.

Pin the Process to CPU 1 on Linux

On Linux, the usual way to target a specific core is taskset.

bash
taskset -c 1 python train.py

This tells the kernel to run the Python process only on CPU core 1. TensorFlow then uses whatever CPU threads are available inside that affinity mask. If the mask contains only core 1, TensorFlow effectively runs there.

You can verify the affinity from the shell:

bash
taskset -cp $$

Or inside Python:

python
import os

print(sorted(os.sched_getaffinity(0)))

If the output is [1], the process is pinned to CPU 1.

Keep TensorFlow Thread Counts Consistent

If the process is pinned to a single core but TensorFlow is still configured with many threads, the runtime may spend effort scheduling threads that all compete for the same CPU. For a strict single-core run, reduce the thread counts.

python
1import tensorflow as tf
2
3tf.config.threading.set_intra_op_parallelism_threads(1)
4tf.config.threading.set_inter_op_parallelism_threads(1)

This does not pin the process by itself, but it matches TensorFlow’s thread model to the CPU-affinity constraint.

A Small CPU-1 Example

bash
taskset -c 1 python cpu_only_demo.py
python
1import os
2import tensorflow as tf
3
4tf.config.set_visible_devices([], "GPU")
5tf.config.threading.set_intra_op_parallelism_threads(1)
6tf.config.threading.set_inter_op_parallelism_threads(1)
7
8print("Affinity:", sorted(os.sched_getaffinity(0)))
9
10a = tf.constant([[1.0, 2.0]])
11b = tf.constant([[3.0], [4.0]])
12c = tf.matmul(a, b)
13print(c.numpy())

This example does three things clearly:

  • hides GPUs
  • limits TensorFlow thread parallelism
  • confirms the process affinity includes only CPU 1

What TensorFlow Itself Can and Cannot Do

TensorFlow can:

  • run on CPU instead of GPU
  • control thread parallelism
  • log device placement

TensorFlow does not normally expose a high-level API that says “bind all compute to OS core 1.” That decision belongs to the operating system scheduler and affinity controls.

So if the real requirement is core-specific execution for benchmarking or isolation, use OS tooling first.

Common Pitfalls

  • Hiding GPUs and thinking that also pins the process to a particular CPU core.
  • Trying to choose CPU 1 only through TensorFlow thread settings without setting process affinity.
  • Pinning the process to one core but leaving TensorFlow configured with many competing threads.
  • Forgetting that CPU numbering starts at 0 on most systems, so CPU 1 means the second logical core.
  • Expecting portable behavior from taskset on non-Linux operating systems.

Summary

  • Running TensorFlow on CPU 1 is mainly a CPU-affinity problem, not a TensorFlow device-selection problem.
  • Hide GPUs if necessary so TensorFlow stays on the CPU.
  • Use taskset -c 1 on Linux to pin the process to CPU 1.
  • Match TensorFlow thread settings to the restricted affinity when you want predictable single-core behavior.
  • Let the OS choose the core unless you have a concrete reason to pin the process manually.

Course illustration
Course illustration

All Rights Reserved.