def _compile_cu(self, sources, output_dir=None, macros=None,
                    include_dirs=None, debug=0, extra_preargs=None,
                    extra_postargs=None, depends=None):
        # Compile CUDA C files, mainly derived from UnixCCompiler._compile().

        macros, objects, extra_postargs, pp_opts, _build = \
            self._setup_compile(output_dir, macros, include_dirs, sources,
                                depends, extra_postargs)

        compiler_so = build.get_nvcc_path()
        cc_args = self._get_cc_args(pp_opts, debug, extra_preargs)
        cuda_version = build.get_cuda_version()
        postargs = _nvcc_gencode_options(cuda_version) + ['-O2']
        postargs += ['-Xcompiler', '/MD']
        print('NVCC options:', postargs)

        for obj in objects:
            try:
                src, ext = _build[obj]
            except KeyError:
                continue
            try:
                self.spawn(compiler_so + cc_args + [src, '-o', obj] + postargs)
            except errors.DistutilsExecError as e:
                raise errors.CompileError(str(e))

        return objects
Beispiel #2
0
    def _compile_cu(self,
                    sources,
                    output_dir=None,
                    macros=None,
                    include_dirs=None,
                    debug=0,
                    extra_preargs=None,
                    extra_postargs=None,
                    depends=None):
        # Compile CUDA C files, mainly derived from UnixCCompiler._compile().
        macros, objects, extra_postargs, pp_opts, _build = \
            self._setup_compile(output_dir, macros, include_dirs, sources,
                                depends, extra_postargs)

        compiler_so = CUDA['nvcc']
        cc_args = self._get_cc_args(pp_opts, debug, extra_preargs)
        post_args = CUDA['post_args']

        for obj in objects:
            try:
                src, _ = _build[obj]
            except KeyError:
                continue
            try:
                self.spawn([compiler_so] + cc_args + [src, '-o', obj] +
                           post_args)
            except errors.DistutilsExecError as e:
                raise errors.CompileError(str(e))

        return objects
Beispiel #3
0
def compile_using_nvcc(source, options=None, arch=None, filename='kern.cu'):
    options = options or []

    if arch is None:
        cuda_info = get_cuda_info()
        arch = min(
            [dev['major'] * 10 + dev['minor'] for dev in cuda_info['devices']])

    cc = get_compiler()
    settings = get_compiler_setting()
    arch = "--generate-code=arch=compute_{a},code=sm_{a}".format(a=arch)

    options += ['-cubin']

    cupy_path = resource_filename("cupy", pjoin("core", "include"))
    settings['include_dirs'].append(cupy_path)

    with _tempdir() as tmpdir:
        tmpfile = pjoin(tmpdir, filename)

        with open(tmpfile, "w") as f:
            f.write(source)

        try:
            stderr_file = pjoin(tmpdir, "stderr.txt")

            with stdchannel_redirected(sys.stderr, stderr_file):
                objects = cc.compile([tmpfile],
                                     include_dirs=settings['include_dirs'],
                                     macros=settings['define_macros'],
                                     extra_postargs=options)
        except errors.CompileError as e:
            with open(stderr_file, "r") as f:
                errs = f.read()

            lines = [
                "The following source code",
                format_code(source), "",
                "created the following compilation errors", "",
                errs.strip(),
                str(e).strip()
            ]

            ex = errors.CompileError("\n".join(lines))
            raise (ex, None, sys.exc_info()[2])

        assert len(objects) == 1

        mod = cp.cuda.function.Module()
        mod.load_file(objects[0])

        return mod
Beispiel #4
0
 def _compile_unix_hipcc(self, obj, src, ext, cc_args, extra_postargs,
                         pp_opts):
     # For CUDA C source files, compile them with HIPCC.
     rocm_path = build.get_hipcc_path()
     base_opts = build.get_compiler_base_options(rocm_path)
     compiler_so = rocm_path
     postargs = ['-O2', '-fPIC', '--include', 'hip_runtime.h']
     print('HIPCC options:', postargs)
     try:
         self.spawn(compiler_so + base_opts + cc_args + [src, '-o', obj] +
                    postargs)
     except errors.DistutilsExecError as e:
         raise errors.CompileError(str(e))
Beispiel #5
0
    def test(self):
        try:
            compiler = ccompiler.new_compiler()
            if not isinstance(compiler, ccompiler.CCompiler):
                raise errors.CompileError("Compiler is not valid!")
            sysconfig.customize_compiler(compiler)

            self.preCompilationTest(compiler)
            o = compiler.compile([self.src])
            self.postCompilationTest(compiler, o)

        except errors.CompileError as e:
            raise DependencyError('Compilation problems', inner=e)
        except Exception as e:
            raise DependencyError('General error', inner=e)
Beispiel #6
0
    def _compile_unix_nvcc(self,
                           obj, src, ext, cc_args, extra_postargs, pp_opts):
        # For CUDA C source files, compile them with NVCC.
        nvcc_path = build.get_nvcc_path()
        base_opts = build.get_compiler_base_options()
        compiler_so = nvcc_path

        cuda_version = build.get_cuda_version()
        postargs = _nvcc_gencode_options(cuda_version) + [
            '-O2', '--compiler-options="-fPIC"', '--std=c++11']
        print('NVCC options:', postargs)
        try:
            self.spawn(compiler_so + base_opts + cc_args + [src, '-o', obj] +
                       postargs)
        except errors.DistutilsExecError as e:
            raise errors.CompileError(str(e))
Beispiel #7
0
    def _compile_cu(self,
                    sources,
                    output_dir=None,
                    macros=None,
                    include_dirs=None,
                    debug=0,
                    extra_preargs=None,
                    extra_postargs=None,
                    depends=None):
        # Compile CUDA C files, mainly derived from UnixCCompiler._compile().
        macros, objects, extra_postargs, pp_opts, _build = \
            self._setup_compile(output_dir, macros, include_dirs, sources,
                                depends, extra_postargs)

        compiler_so = build.get_nvcc_path()
        cc_args = self._get_cc_args(pp_opts, debug, extra_preargs)
        cuda_version = build.get_cuda_version()
        postargs = _nvcc_gencode_options(cuda_version) + ['-O2']
        if cuda_version >= 11020:
            # MSVC 14.0 (2015) is deprecated for CUDA 11.2 but we need it
            # to build CuPy because some Python versions were built using it.
            # REF: https://wiki.python.org/moin/WindowsCompilers
            postargs += ['-allow-unsupported-compiler']
        postargs += ['-Xcompiler', '/MD']
        # This is to compile thrust with MSVC2015
        if cuda_version >= 11020:
            postargs += ['--std=c++14']
            num_threads = int(os.environ.get('CUPY_NUM_NVCC_THREADS', '2'))
            postargs += [f'-t{num_threads}']
        print('NVCC options:', postargs)

        for obj in objects:
            try:
                src, ext = _build[obj]
            except KeyError:
                continue
            try:
                self.spawn(compiler_so + cc_args + [src, '-o', obj] + postargs)
            except errors.DistutilsExecError as e:
                raise errors.CompileError(str(e))

        return objects
Beispiel #8
0
    def __call__(self):
        try:
            self._makeSource()
        except Exception as e:
            raise DependencyError('Cannot create source file', inner=e)

        try:
            compiler = ccompiler.new_compiler()
            if not isinstance(compiler, ccompiler.CCompiler):
                raise errors.CompileError("Compiler is not valid!")
            sysconfig.customize_compiler(compiler)

            self.preCompilationTest(compiler)
            o = compiler.compile([self.src])
            self.postCompilationTest(compiler, o)

        except errors.CompileError as e:
            raise DependencyError('Compilation problems', inner=e)
        except Exception as e:
            raise DependencyError('General error', inner=e)
        finally:
            self._cleanup()
Beispiel #9
0
    def _compile_unix_nvcc(self,
                           obj, src, ext, cc_args, extra_postargs, pp_opts):
        # For CUDA C source files, compile them with NVCC.
        nvcc_path = build.get_nvcc_path()
        base_opts = build.get_compiler_base_options(nvcc_path)
        compiler_so = nvcc_path

        cuda_version = build.get_cuda_version()
        postargs = _nvcc_gencode_options(cuda_version) + [
            '-O2', '--compiler-options="-fPIC"']
        if cuda_version >= 11020:
            postargs += ['--std=c++14']
            num_threads = int(os.environ.get('CUPY_NUM_NVCC_THREADS', '2'))
            postargs += [f'-t{num_threads}']
        else:
            postargs += ['--std=c++11']
        print('NVCC options:', postargs)
        try:
            self.spawn(compiler_so + base_opts + cc_args + [src, '-o', obj] +
                       postargs)
        except errors.DistutilsExecError as e:
            raise errors.CompileError(str(e))