setup_windows_cuda.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import numpy as np
00004 import os
00005 # on Windows, we need the original PATH without Anaconda's compiler in it:
00006 PATH = os.environ.get('PATH') + ';C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\bin'
00007 from distutils.spawn import spawn, find_executable
00008 from setuptools import setup, find_packages, Extension
00009 from setuptools.command.build_ext import build_ext
00010 import sys
00011 
00012 # CUDA specific config
00013 # nvcc is assumed to be in user's PATH
00014 nvcc_compile_args = ['-O', '--ptxas-options=-v', '-arch=compute_35', '-code=sm_35,sm_52,sm_61', '-c', '--compiler-options=-fPIC']
00015 nvcc_compile_args = os.environ.get('NVCCFLAGS', '').split() + nvcc_compile_args
00016 cuda_libs = ['cublas']
00017 nvcc_bin = 'nvcc.exe'
00018 lib_dir = 'lib/x64'
00019 
00020 
00021 import distutils.msvc9compiler
00022 distutils.msvc9compiler.VERSION = 14.0
00023 
00024 # Obtain the numpy include directory.  This logic works across numpy versions.
00025 try:
00026     numpy_include = np.get_include()
00027 except AttributeError:
00028     numpy_include = np.get_numpy_include()
00029 
00030 
00031 cudamat_ext = Extension('gpu_nms',
00032                         sources=[
00033                                 'gpu_nms.cu'
00034                                 ],
00035                         language='c++',
00036                         libraries=cuda_libs,
00037                         extra_compile_args=nvcc_compile_args,
00038                         include_dirs = [numpy_include, 'C:\\Programming\\CUDA\\v8.0\\include'])
00039 
00040 
00041 class CUDA_build_ext(build_ext):
00042     """
00043     Custom build_ext command that compiles CUDA files.
00044     Note that all extension source files will be processed with this compiler.
00045     """
00046     def build_extensions(self):
00047         self.compiler.src_extensions.append('.cu')
00048         self.compiler.set_executable('compiler_so', 'nvcc')
00049         self.compiler.set_executable('linker_so', 'nvcc --shared')
00050         if hasattr(self.compiler, '_c_extensions'):
00051             self.compiler._c_extensions.append('.cu')  # needed for Windows
00052         self.compiler.spawn = self.spawn
00053         build_ext.build_extensions(self)
00054 
00055     def spawn(self, cmd, search_path=1, verbose=0, dry_run=0):
00056         """
00057         Perform any CUDA specific customizations before actually launching
00058         compile/link etc. commands.
00059         """
00060         if (sys.platform == 'darwin' and len(cmd) >= 2 and cmd[0] == 'nvcc' and
00061                 cmd[1] == '--shared' and cmd.count('-arch') > 0):
00062             # Versions of distutils on OSX earlier than 2.7.9 inject
00063             # '-arch x86_64' which we need to strip while using nvcc for
00064             # linking
00065             while True:
00066                 try:
00067                     index = cmd.index('-arch')
00068                     del cmd[index:index+2]
00069                 except ValueError:
00070                     break
00071         elif self.compiler.compiler_type == 'msvc':
00072             # There are several things we need to do to change the commands
00073             # issued by MSVCCompiler into one that works with nvcc. In the end,
00074             # it might have been easier to write our own CCompiler class for
00075             # nvcc, as we're only interested in creating a shared library to
00076             # load with ctypes, not in creating an importable Python extension.
00077             # - First, we replace the cl.exe or link.exe call with an nvcc
00078             #   call. In case we're running Anaconda, we search cl.exe in the
00079             #   original search path we captured further above -- Anaconda
00080             #   inserts a MSVC version into PATH that is too old for nvcc.
00081             cmd[:1] = ['nvcc', '--compiler-bindir',
00082                        os.path.dirname(find_executable("cl.exe", PATH))
00083                        or cmd[0]]
00084             # - Secondly, we fix a bunch of command line arguments.
00085             for idx, c in enumerate(cmd):
00086                 # create .dll instead of .pyd files
00087                 #if '.pyd' in c: cmd[idx] = c = c.replace('.pyd', '.dll')  #20160601, by MrX
00088                 # replace /c by -c
00089                 if c == '/c': cmd[idx] = '-c'
00090                 # replace /DLL by --shared
00091                 elif c == '/DLL': cmd[idx] = '--shared'
00092                 # remove --compiler-options=-fPIC
00093                 elif '-fPIC' in c: del cmd[idx]
00094                 # replace /Tc... by ...
00095                 elif c.startswith('/Tc'): cmd[idx] = c[3:]
00096                 # replace /Fo... by -o ...
00097                 elif c.startswith('/Fo'): cmd[idx:idx+1] = ['-o', c[3:]]
00098                 # replace /LIBPATH:... by -L...
00099                 elif c.startswith('/LIBPATH:'): cmd[idx] = '-L' + c[9:]
00100                 # replace /OUT:... by -o ...
00101                 elif c.startswith('/OUT:'): cmd[idx:idx+1] = ['-o', c[5:]]
00102                 # remove /EXPORT:initlibcudamat or /EXPORT:initlibcudalearn
00103                 elif c.startswith('/EXPORT:'): del cmd[idx]
00104                 # replace cublas.lib by -lcublas
00105                 elif c == 'cublas.lib': cmd[idx] = '-lcublas'
00106             # - Finally, we pass on all arguments starting with a '/' to the
00107             #   compiler or linker, and have nvcc handle all other arguments
00108             if '--shared' in cmd:
00109                 pass_on = '--linker-options='
00110                 # we only need MSVCRT for a .dll, remove CMT if it sneaks in:
00111                 cmd.append('/NODEFAULTLIB:libcmt.lib')
00112             else:
00113                 pass_on = '--compiler-options='
00114             cmd = ([c for c in cmd if c[0] != '/'] +
00115                    [pass_on + ','.join(c for c in cmd if c[0] == '/')])
00116             # For the future: Apart from the wrongly set PATH by Anaconda, it
00117             # would suffice to run the following for compilation on Windows:
00118             # nvcc -c -O -o <file>.obj <file>.cu
00119             # And the following for linking:
00120             # nvcc --shared -o <file>.dll <file1>.obj <file2>.obj -lcublas
00121             # This could be done by a NVCCCompiler class for all platforms.
00122         spawn(cmd, search_path, verbose, dry_run)
00123 
00124 setup(name="py_fast_rcnn_gpu",
00125       description="Performs linear algebra computation on the GPU via CUDA",
00126       ext_modules=[cudamat_ext],
00127       cmdclass={'build_ext': CUDA_build_ext},
00128 )


rail_object_detector
Author(s):
autogenerated on Sat Jun 8 2019 20:26:30