Muhammad Ahsan
Muhammad Ahsan

Reputation: 49

How to prevent specific GPU to be used in Python code

My this problem is same as 1: How to set specific gpu in tensorflow? but it didn't solve my problem.

I have 4 GPUs in my PC and I want to run code on GPU 0 but whenever I run my tensorflow code, my code is always running only on GPU 2. As reading these (2, 3, 4) solutions and information I tried to solve my problem by adding:

Above solutions are not working for me. Code is always running on GPU 2. I checked it by watch nvidia-smi. My system environment is

Any suggestions for this problem? It's wired that adding environment variable in project settings in PyCharm or in python code... still only GPU 2 is visible. When I remove CUDA_VISIBLE_DEVICESthen tensorflow detects all 4 GPUs but code run on only GPU 2.

Upvotes: 0

Views: 1221

Answers (1)

Venkataraman
Venkataraman

Reputation: 138

I tried this in tensorflow 2.0.0

physical_devices = tf.config.experimental.list_physical_devices('GPU')

tf.config.experimental.set_visible_devices(physical_devices[0], 'GPU')

logical_devices = tf.config.experimental.list_logical_devices('GPU')

This should make u r code run in GPU index 0

Upvotes: 1

Related Questions