One , Gets a list of specific computing devices on the current host
gpus = tf.config.experimental.list_physical_devices(device_type='GPU') cpus =
tf.config.experimental.list_physical_devices(device_type='CPU') print(gpus,
cpus)
Two , Sets the device range visible to the current program
By default TensorFlow Will use all that it can use GPU.
tf.config.experimental.set_visible_devices(devices=gpus[2:4],
device_type='GPU')
After setting , The current program only uses devices that are visible to it , Invisible devices will not be used by the current program .
Another way is to use environment variables CUDA_VISIBLE_DEVICES You can also control what the program uses GPU.
Input at terminal
export CUDA_VISIBLE_DEVICES=2,3
Or add it to the code
import os os.environ['CUDA_VISIBLE_DEVICES'] = "2,3"
Can achieve the same effect .
Three , Use of video memory
By default ,TensorFlow Almost all of the available video memory will be available , To avoid the performance loss caused by memory fragmentation .
however TensorFlow It provides two strategies for using video memory , Let's have more flexible control over how the program's memory is used :
1. Apply for video memory only when needed ( The program consumes little memory when it is initially run , With the program running and dynamic application of video memory );
2. Limit consumption of fixed size video memory ( The program will not exceed the limited video memory size , If the error is exceeded ).
Set to request video memory only when needed .
for gpu in gpus: tf.config.experimental.set_memory_growth(gpu, True)
The following way is to set Tensorflow Fixed consumption GPU:0 Of 2GB Video memory .
tf.config.experimental.set_virtual_device_configuration( gpus[0],
[tf.config.experimental.VirtualDeviceConfiguration(memory_limit=2048)] )
Four , single GPU Many simulations GPU Environmental Science
The above method can not only set the use of video memory , It can be done in only one GPU More environment simulation GPU Commissioning .
tf.config.experimental.set_virtual_device_configuration( gpus[0],
[tf.config.experimental.VirtualDeviceConfiguration(memory_limit=2048),
tf.config.experimental.VirtualDeviceConfiguration(memory_limit=2048)])
The code above is right there GPU:0 Two video memories are created on the 2GB Virtual GPU.
Technology