LoginSignup
3
3

More than 3 years have passed since last update.

numpy 内部で使われるスレッド数を確認する

Last updated at Posted at 2021-03-02

numpy は、例えば conda で入れて mkl にリンクされているバージョンでは、内部で使える場所では mkl 側でスレッドを(勝手に)使って計算している。そのため、Python レイヤのプログラミングでスレッドを使わなくても利用できる。

環境変数などを明示的に指定しなくても、勝手にマルチスレッドで動いていた(CPU利用率が複数コア使っている感じだった)ので、どの情報を見ればよいのかと調べると、今は threadpool_infoを見れば解る らしい。

from threadpoolctl import threadpool_info
from pprint import pp
import numpy as np

pp(threadpool_info())
[{'filepath': 'C:\\Users\\hotch\\scoop\\apps\\anaconda3\\2020.11\\Library\\bin\\mkl_rt.dll',
  'prefix': 'mkl_rt',
  'user_api': 'blas',
  'internal_api': 'mkl',
  'version': '2020.0.2',
  'num_threads': 12,
  'threading_layer': 'intel'}]

なるほど、mkl_rt では 12 スレッドと。

環境変数で値をセットしてから見ると

import os
os.environ["MKL_NUM_THREADS"] = "4"
[{'filepath': 'C:\\Users\\hotch\\scoop\\apps\\anaconda3\\2020.11\\Library\\bin\\mkl_rt.dll',
  'prefix': 'mkl_rt',
  'user_api': 'blas',
  'internal_api': 'mkl',
  'version': '2020.0.2',
  'num_threads': 4,
  'threading_layer': 'intel'}]

4に抑えられている。

3
3
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
3
3