Run tensorflow python program, limit the occupation of GPU and CPU

Under normal circumstances, when running tensorflow, all visible GPUs will be occupied by default, which will cause other users or programs to have no GPUs available, so it is necessary to limit the program's occupancy of GPUs. In addition, generally our programs can't use all the GPU resources, but forcefully occupy them, most of the resources will not be used, and the running speed will not be improved.

Use nvidia-smi to check the GPU usage of this machine, as shown in the figure below, here we can see that the GPU model of this machine is K80, there are two K80s, and four are available (one K80 includes two K40s).



1. If you only need to use one or several GPUs, you can run the program with the following command: CUDA_VISIBLE_DEVICES=0,1 python test.py

This means that only GPU 0 and 1 are visible to the program, so the program can only use GPU 0 and 1

Similarly, you can also specify in the code

import them
os.environ["CUDA_VISIBLE_DEVICES"] = "0,1"


If you want to run the program on the CPU only, without the CPU, you can use the following command (all GPUs are not visible):

CUDA_VISIBLE_DEVICES='' python test.py

or

CUDA_VISIBLE_DEVICES="-1" python test.py


2. Let tensorflow only request video memory on demand, as shown in the following code

#only minimum use gpu
gpu_config = tf.ConfigProto()
gpu_config.gpu_options.allow_growth = True
with tf.Session(config = gpu_config) as sess:


The front is the limitation of GPU, so what if you don't use GPU and only use CPU? How to limit the use of CPU?

As mentioned earlier, if you use the command CUDA_VISIBLE_DEVICES=""python test.py to only use the CPU, what if you want to use only part of the CPU? You can restrict it through the following code


cpu_config = tf.ConfigProto(intra_op_parallelism_threads = 8, inter_op_parallelism_threads = 8, device_count = {'CPU': 8})
with tf.Session(config = cpu_config) as sess:


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325723006&siteId=291194637