How to use only one GPU for tensorflow session?

Viewed 4172

I have two GPUs. My program uses TensorRT and Tensorflow.

When I run only TensorRT part, it is fine. When I run together with Tensorflow part, I have error as

[TensorRT] ERROR: engine.cpp (370) - Cuda Error in ~ExecutionContext: 77 (an illegal memory access was encountered)
terminate called after throwing an instance of 'nvinfer1::CudaError'
  what():  std::exception

The issue is when Tensorflow session starts as follow

self.graph = tf.get_default_graph()
self.persistent_sess = tf.Session(graph=self.graph, config=tf_config)

It loads two GPUs as

2019-06-06 14:15:04.420265: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 6965 MB memory) -> physical GPU (device: 0, name: Quadro P4000, pci bus id: 0000:04:00.0, compute capability: 6.1)
2019-06-06 14:15:04.420713: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:1 with 7159 MB memory) -> physical GPU (device: 1, name: Quadro P4000, pci bus id: 0000:05:00.0, compute capability: 6.1)

I tried to load only one GPU as

(1)Putting on top of the python code

import os
os.environ["CUDA_VISIBLE_DEVICES"]="0"

(2)

with tf.device('/device:GPU:0'):
    self.graph = tf.get_default_graph()
    self.persistent_sess = tf.Session(graph=self.graph, config=tf_config)

Both don't work.

How to solve the problem?

1 Answers

I could manage to load only one GPU be placing the following lines at the first line of the python code.

import sys, os
os.environ["CUDA_VISIBLE_DEVICES"]="0"
Related