How to use gpu acceleration in python

(*-*)浩
Release: 2019-05-29 15:33:21
Original
13140 people have browsed it

I have read many tutorials and mentioned using os.environ["CUDA_VISIBLE_DEVICES"] = "1" or export CUDA_VISIBLE_DEVICES=1

However, the speed did not improve when I added it to the code. It took me a long time to find it. The problem is, the above two lines of code only work when your computer has more than two GPUs!

How to use gpu acceleration in python

Because my computer only has one GPU, I need to change the parameter "1" to "0", otherwise the device "1" cannot be found. The CPU will be used by default and the speed will not be improved.

If you are running the python program in the terminal, use the command: CUDA_VISIBLE_DEVICES=0 python filename.py

If you have multiple cards and want to specify multiple gpu training, you can set it like this :

def set_gpus(gpu_index):
    if type(gpu_index) == list:
        gpu_index = ','.join(str(_) for _ in gpu_index)
    if type(gpu_index) ==int:
        gpu_index = str(gpu_index)
    os.environ["CUDA_VISIBLE_DEVICES"] = gpu_index
Copy after login

This can be achieved by calling the set_gpu function.

It must be noted that CUDA_VISIBLE_DEVICES, every! one! indivual! Character! mother! It must be written completely correctly. If you accidentally write CUDA_VISIBLE_DIVICES or CUDA_VISIABLE_DEVICES, the code will not report an error, but the GPU call will not succeed,

The above is the detailed content of How to use gpu acceleration in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template