Skip to content

Commit

Permalink
Fixes for OS X
Browse files Browse the repository at this point in the history
  • Loading branch information
apaszke committed Aug 23, 2016
1 parent d467a06 commit 8d933cb
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ torch.egg-info/
*/**/__pycache__
torch/csrc/generic/TensorMethods.cpp
torch/lib/*.so*
torch/lib/*.dylib*
torch/lib/*.h
torch/lib/build
torch/lib/tmp_install
Expand All @@ -12,3 +13,5 @@ torch/csrc/nn/THNN.cpp
torch/csrc/nn/THCUNN.cwrap
torch/csrc/nn/THCUNN.cpp
*/**/*.pyc
*/**/*.so*
*/**/*.dylib*
24 changes: 19 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,16 @@ def run(self):

if WITH_CUDA:
if platform.system() == 'Darwin':
include_dirs += ['/Developer/NVIDIA/CUDA-7.5/include']
cuda_path = '/Developer/NVIDIA/CUDA-7.5'
cuda_include_path = cuda_path + '/include'
cuda_lib_path = cuda_path + '/lib'
else:
include_dirs += ['/usr/local/cuda/include']
cuda_path = '/usr/local/cuda'
cuda_include_path = cuda_path + '/include'
cuda_lib_path = cuda_path + '/lib64'
include_dirs.append(cuda_include_path)
extra_link_args.append('-L' + cuda_lib_path)
extra_link_args.append('-Wl,-rpath,' + cuda_lib_path)
extra_compile_args += ['-DWITH_CUDA']
main_libraries += ['THC']
main_sources += [
Expand All @@ -167,6 +174,13 @@ def run(self):
extra_compile_args += ['-O0', '-g']
extra_link_args += ['-O0', '-g']


def make_relative_rpath(path):
if platform.system() == 'Darwin':
return '-Wl,-rpath,@loader_path/' + path
else:
return '-Wl,-rpath,$ORIGIN/' + path

################################################################################
# Declare extensions and package
################################################################################
Expand All @@ -180,7 +194,7 @@ def run(self):
language='c++',
extra_compile_args=extra_compile_args,
include_dirs=include_dirs,
extra_link_args=extra_link_args + ['-Wl,-rpath,$ORIGIN/lib'],
extra_link_args=extra_link_args + [make_relative_rpath('lib')]
)
extensions.append(C)

Expand All @@ -190,7 +204,7 @@ def run(self):
language='c++',
extra_compile_args=extra_compile_args,
include_dirs=include_dirs,
extra_link_args=extra_link_args + ['-Wl,-rpath,$ORIGIN/../lib'],
extra_link_args=extra_link_args + [make_relative_rpath('../lib')]
)
extensions.append(THNN)

Expand All @@ -201,7 +215,7 @@ def run(self):
language='c++',
extra_compile_args=extra_compile_args,
include_dirs=include_dirs,
extra_link_args=extra_link_args + ['-Wl,-rpath,$ORIGIN/../lib'],
extra_link_args=extra_link_args + [make_relative_rpath('../lib')]
)
extensions.append(THCUNN)
packages += ['torch.cuda', 'torch.legacy.cunn']
Expand Down
2 changes: 1 addition & 1 deletion torch/csrc/cuda/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ PyObject * THCPModule_initExtension(PyObject *self)
PyObject *torch_module = PyImport_ImportModule("torch.cuda");
if (!torch_module) {
THPUtils_setError("class loader couldn't access torch module");
return false;
return NULL;
}
PyObject* module_dict = PyModule_GetDict(torch_module);
return PyBool_FromLong(THCPModule_initCuda(module_dict));
Expand Down
19 changes: 17 additions & 2 deletions torch/lib/build_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ set -e
BASE_DIR=$(pwd)
cd torch/lib
INSTALL_DIR=$(pwd)/tmp_install
BASIC_FLAGS=" -DTH_INDEX_BASE=0 -I$INSTALL_DIR/include -I$INSTALL_DIR/include/TH -I$INSTALL_DIR/include/THC -L$INSTALL_DIR/lib "
FLAGS="$BASIC_FLAGS -Wl,-rpath,\$ORIGIN"
BASIC_FLAGS=" -DTH_INDEX_BASE=0 -I$INSTALL_DIR/include -I$INSTALL_DIR/include/TH -I$INSTALL_DIR/include/THC "
LDFLAGS="-L$INSTALL_DIR/lib "
if [[ $(uname) == 'Darwin' ]]; then
LDFLAGS="$LDFLAGS -Wl,-rpath,@loader_path"
else
LDFLAGS="$LDFLAGS -Wl,-rpath,\$ORIGIN"
fi
FLAGS="$BASIC_FLAGS $LDFLAGS"
function build() {
mkdir -p build/$1
cd build/$1
Expand All @@ -18,6 +24,15 @@ function build() {
-DTH_INCLUDE_PATH="$INSTALL_DIR/include"
make install -j$(getconf _NPROCESSORS_ONLN)
cd ../..

if [[ $(uname) == 'Darwin' ]]; then
cd tmp_install/lib
for lib in *.dylib; do
echo "Updating install_name for $lib"
install_name_tool -id @rpath/$lib $lib
done
cd ../..
fi
}

mkdir -p tmp_install
Expand Down

0 comments on commit 8d933cb

Please sign in to comment.