00001
00002
00003
00004
00005
00006
00007
00008 import numpy as np
00009 import os
00010 from os.path import join as pjoin
00011
00012 from setuptools import setup
00013 from distutils.extension import Extension
00014 from Cython.Distutils import build_ext
00015 import subprocess
00016
00017
00018 nvcc_bin = 'nvcc.exe'
00019 lib_dir = 'lib/x64'
00020
00021 import distutils.msvc9compiler
00022 distutils.msvc9compiler.VERSION = 14.0
00023
00024
00025 def find_in_path(name, path):
00026 "Find a file in a search path"
00027
00028
00029 for dir in path.split(os.pathsep):
00030 binpath = pjoin(dir, name)
00031 if os.path.exists(binpath):
00032 return os.path.abspath(binpath)
00033 return None
00034
00035
00036 def locate_cuda():
00037 """Locate the CUDA environment on the system
00038
00039 Returns a dict with keys 'home', 'nvcc', 'include', and 'lib64'
00040 and values giving the absolute path to each directory.
00041
00042 Starts by looking for the CUDAHOME env variable. If not found, everything
00043 is based on finding 'nvcc' in the PATH.
00044 """
00045
00046
00047 if 'CUDA_PATH' in os.environ:
00048 home = os.environ['CUDA_PATH']
00049 print("home = %s\n" % home)
00050 nvcc = pjoin(home, 'bin', nvcc_bin)
00051 else:
00052
00053 default_path = pjoin(os.sep, 'usr', 'local', 'cuda', 'bin')
00054 nvcc = find_in_path(nvcc_bin, os.environ['PATH'] + os.pathsep + default_path)
00055 if nvcc is None:
00056 raise EnvironmentError('The nvcc binary could not be '
00057 'located in your $PATH. Either add it to your path, or set $CUDA_PATH')
00058 home = os.path.dirname(os.path.dirname(nvcc))
00059 print("home = %s, nvcc = %s\n" % (home, nvcc))
00060
00061
00062 cudaconfig = {'home':home, 'nvcc':nvcc,
00063 'include': pjoin(home, 'include'),
00064 'lib64': pjoin(home, lib_dir)}
00065 for k, v in cudaconfig.iteritems():
00066 if not os.path.exists(v):
00067 raise EnvironmentError('The CUDA %s path could not be located in %s' % (k, v))
00068
00069 return cudaconfig
00070 CUDA = locate_cuda()
00071
00072
00073
00074 try:
00075 numpy_include = np.get_include()
00076 except AttributeError:
00077 numpy_include = np.get_numpy_include()
00078
00079
00080 def customize_compiler_for_nvcc(self):
00081 """inject deep into distutils to customize how the dispatch
00082 to gcc/nvcc works.
00083
00084 If you subclass UnixCCompiler, it's not trivial to get your subclass
00085 injected in, and still have the right customizations (i.e.
00086 distutils.sysconfig.customize_compiler) run on it. So instead of going
00087 the OO route, I have this. Note, it's kindof like a wierd functional
00088 subclassing going on."""
00089
00090
00091
00092
00093
00094
00095
00096
00097 super = self.compile
00098
00099
00100
00101
00102 def compile(sources, output_dir=None, macros=None, include_dirs=None, debug=0, extra_preargs=None, extra_postargs=None, depends=None):
00103 postfix=os.path.splitext(sources[0])[1]
00104
00105 if postfix == '.cu':
00106
00107
00108
00109
00110 postargs = extra_postargs['nvcc']
00111 else:
00112 postargs = extra_postargs['gcc']
00113
00114
00115 return super(sources, output_dir, macros, include_dirs, debug, extra_preargs, postargs, depends)
00116
00117
00118
00119
00120 self.compile = compile
00121
00122
00123
00124 class custom_build_ext(build_ext):
00125 def build_extensions(self):
00126 customize_compiler_for_nvcc(self.compiler)
00127 build_ext.build_extensions(self)
00128
00129
00130 ext_modules = [
00131
00132 Extension(
00133 "cpu_nms",
00134 sources=["cpu_nms.pyx"],
00135 extra_compile_args={'gcc': []},
00136 include_dirs = [numpy_include],
00137 ),
00138 ]
00139
00140 setup(
00141 name='fast_rcnn',
00142 ext_modules=ext_modules,
00143
00144 cmdclass={'build_ext': custom_build_ext},
00145 )