6

cythonファイルのコンパイルを並行して実行したいと思います。

そこで、Cython.Buildソース ファイルを調べたところ、次のcythonize関数のシグネチャが見つかりました。

def cythonize(module_list, exclude=None, nthreads=0, aliases=None,
              quiet=False, force=False, language=None,
              exclude_failures=False, **options):

そして、cythonizenthreadsオプションに関する次のコメント:

"For parallel compilation, set the 'nthreads' option to the number of
concurrent builds."

setup.pyそのため、次のように、ファイルでこのオプションを使用しようとしました。

from setuptools import setup
from Cython.Build import cythonize
from Cython.Distutils.extension import Extension

EXTENSIONS = [Extension(...)
              ...
              Extension(...)]

setup(name='...',
      ...
      ext_modules=cythonize(EXTENSIONS, nthreads=8),
      ...)

しかし、私の.pyxファイルはまだ 1 つのスレッドを使用して順次コンパイルされています。

nthreadsここで間違っていることと、オプションを使用してcythonizeコンパイルを並行して実行する方法がわかりませんか?

4

2 に答える 2

6

最終的に、cython ファイルを並行してコンパイルする解決策を見つけました。

from setuptools import setup
from Cython.Build import cythonize
from Cython.Distutils.extension import Extension

NB_COMPILE_JOBS = 8

EXTENSIONS = [Extension(...)
              ...
              Extension(...)]

def setup_given_extensions(extensions):
    setup(name='...',
          ...
          ext_modules=cythonize(extensions),
          ...)

def setup_extensions_in_sequential():
    setup_given_extensions(EXTENSIONS)

def setup_extensions_in_parallel():
    cythonize(EXTENSIONS, nthreads=NB_COMPILE_JOBS)
    pool = multiprocessing.Pool(processes=NB_COMPILE_JOBS)
    pool.map(setup_given_extensions, EXTENSIONS)
    pool.close()
    pool.join()

if "build_ext" in sys.argv:
    setup_extensions_in_parallel()
else:
    setup_extensions_in_sequential()
于 2016-11-10T16:53:15.477 に答える