Skip to content

Commit

Permalink
OSX + Python 2 build fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
soumith committed Sep 25, 2016
1 parent 0be5031 commit 1cf87e8
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
6 changes: 6 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@ def make_relative_rpath(path):
)
extensions.append(C)

DL = Extension("torch._dl",
sources=["torch/csrc/dl.c"],
language='c',
)
extensions.append(DL)

THNN = Extension("torch._thnn._THNN",
libraries=['TH', 'THNN'],
sources=['torch/csrc/nn/THNN.cpp'],
Expand Down
10 changes: 9 additions & 1 deletion torch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@
# modules against the _C shared object. Their missing THP symbols will be
# automatically filled by the dynamic loader.
import os as _dl_flags

# first check if the os package has the required flags
if not hasattr(_dl_flags, 'RTLD_GLOBAL') or not hasattr(_dl_flags, 'RTLD_NOW'):
import DLFCN as _dl_flags
try:
# next try if DLFCN exists
import DLFCN as _dl_flags
except ImportError:
# as a last attempt, use compile-time constants
import torch._dl as _dl_flags

old_flags = sys.getdlopenflags()
sys.setdlopenflags(_dl_flags.RTLD_GLOBAL | _dl_flags.RTLD_NOW)
from torch._C import *
Expand Down
47 changes: 47 additions & 0 deletions torch/csrc/dl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <dlfcn.h>
#include <Python.h>

PyObject* module;

static PyMethodDef TorchDlMethods[] = {
{NULL, NULL, 0, NULL}
};

#if PY_MAJOR_VERSION != 2
static struct PyModuleDef torchdlmodule = {
PyModuleDef_HEAD_INIT,
"torch._dl",
NULL,
-1,
TorchDlMethods
};
#endif

#if PY_MAJOR_VERSION == 2
PyMODINIT_FUNC init_dl()
#else
PyMODINIT_FUNC PyInit__dl()
#endif
{

#if PY_MAJOR_VERSION == 2
#define ASSERT_TRUE(cmd) if (!(cmd)) {PyErr_SetString(PyExc_ImportError, "initialization error"); return;}
#else
#define ASSERT_TRUE(cmd) if (!(cmd)) return NULL
#endif

#if PY_MAJOR_VERSION == 2
ASSERT_TRUE(module = Py_InitModule("torch._dl", TorchDlMethods));
#else
ASSERT_TRUE(module = PyModule_Create(&torchdlmodule));
#endif
ASSERT_TRUE(PyModule_AddIntConstant(module, "RTLD_GLOBAL", (long) RTLD_GLOBAL) == 0);
ASSERT_TRUE(PyModule_AddIntConstant(module, "RTLD_NOW", (long) RTLD_NOW) == 0);

#if PY_MAJOR_VERSION == 2
#else
return module;
#endif

#undef ASSERT_TRUE
}
15 changes: 9 additions & 6 deletions torch/lib/build_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ 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 "
BASIC_C_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"
C_FLAGS="$BASIC_C_FLAGS $LDFLAGS"
function build() {
mkdir -p build/$1
cd build/$1
cmake ../../$1 -DCMAKE_MODULE_PATH="$BASE_DIR/cmake/FindCUDA" \
-DTorch_FOUND="1" \
-DCMAKE_INSTALL_PREFIX="$INSTALL_DIR" \
-DCMAKE_C_FLAGS="$FLAGS" \
-DCMAKE_CXX_FLAGS="$FLAGS" \
-DCUDA_NVCC_FLAGS="$BASIC_FLAGS" \
-DCMAKE_C_FLAGS="$C_FLAGS" \
-DCMAKE_CXX_FLAGS="$C_FLAGS $CPP_FLAGS" \
-DCUDA_NVCC_FLAGS="$BASIC_C_FLAGS" \
-DTH_INCLUDE_PATH="$INSTALL_DIR/include" \
-DTH_LIB_PATH="$INSTALL_DIR/lib"
make install -j$(getconf _NPROCESSORS_ONLN)
Expand All @@ -39,13 +39,16 @@ function build() {
mkdir -p tmp_install
build TH
build THNN
build libshm

if [[ "$1" == "--with-cuda" ]]; then
build THC
build THCUNN
fi

CPP_FLAGS=" -std=c++11 "
build libshm


cp $INSTALL_DIR/lib/* .
cp THNN/generic/THNN.h .
cp THCUNN/THCUNN.h .
Expand Down

0 comments on commit 1cf87e8

Please sign in to comment.