5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

pythonからGPUの枚数を知る方法〜multiprocessingをpytorchで使う際の注意点〜

Posted at

はじめに

pytorchでmultiprocessingを利用するとCUDAの初期化で怒られることがあります.

RuntimeError: cuda runtime error (3) : initialization error at /pytorch/aten/src/THC/THCGeneral.cpp:50
THCudaCheck FAIL file=/pytorch/aten/src/THC/THCGeneral.cpp line=50 error=3 : initialization error

なぜ怒られたか

色々調査すると,spawnがどうとか,様々な文献が見当たりましたが,自分の場合はtorch.cuda.device_count()を利用していたことが原因だったようです.

ということで,GPUの枚数をtorch.cuda.device_count()抜きで知りたい.

GPUの枚数をpythonから取得

nvidia-smiに頼ります.
linuxの場合は以下の通り.

import subprocess
msg = subprocess.check_output("nvidia-smi --query-gpu=index --format=csv", shell=True)
n_devices = max(0, len(msg.decode().split("\n")) - 2)

pytorchのCUDA初期化問題には皆様も気をつけてください.

5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?