提交 1ec0d8b1 authored 作者: Arnaud Bergeron's avatar Arnaud Bergeron

First try at making visibility=hidden the default.

上级 94e31183
...@@ -150,7 +150,7 @@ class DynamicModule(object): ...@@ -150,7 +150,7 @@ class DynamicModule(object):
self.support_code = [] self.support_code = []
self.functions = [] self.functions = []
self.includes = ["<Python.h>", "<iostream>"] self.includes = ["<Python.h>", "<iostream>", '"theano_mod_helper.h"']
self.init_blocks = [] self.init_blocks = []
def print_methoddef(self, stream): def print_methoddef(self, stream):
...@@ -171,14 +171,14 @@ static struct PyModuleDef moduledef = {{ ...@@ -171,14 +171,14 @@ static struct PyModuleDef moduledef = {{
MyMethods, MyMethods,
}}; }};
""".format(name=self.hash_placeholder), file=stream) """.format(name=self.hash_placeholder), file=stream)
print(("PyMODINIT_FUNC PyInit_%s(void) {" % print(("THEANO_INIT_FUNC PyInit_%s(void) {" %
self.hash_placeholder), file=stream) self.hash_placeholder), file=stream)
for block in self.init_blocks: for block in self.init_blocks:
print(' ', block, file=stream) print(' ', block, file=stream)
print(" PyObject *m = PyModule_Create(&moduledef);", file=stream) print(" PyObject *m = PyModule_Create(&moduledef);", file=stream)
print(" return m;", file=stream) print(" return m;", file=stream)
else: else:
print(("PyMODINIT_FUNC init%s(void){" % print(("THEANO_INIT_FUNC init%s(void){" %
self.hash_placeholder), file=stream) self.hash_placeholder), file=stream)
for block in self.init_blocks: for block in self.init_blocks:
print(' ', block, file=stream) print(' ', block, file=stream)
...@@ -1449,7 +1449,8 @@ def std_include_dirs(): ...@@ -1449,7 +1449,8 @@ def std_include_dirs():
py_plat_spec_inc = distutils.sysconfig.get_python_inc(plat_specific=True) py_plat_spec_inc = distutils.sysconfig.get_python_inc(plat_specific=True)
python_inc_dirs = ([py_inc] if py_inc == py_plat_spec_inc python_inc_dirs = ([py_inc] if py_inc == py_plat_spec_inc
else [py_inc, py_plat_spec_inc]) else [py_inc, py_plat_spec_inc])
return numpy_inc_dirs + python_inc_dirs gof_inc_dir = os.path.dirname(__file__)
return numpy_inc_dirs + python_inc_dirs + [gof_inc_dir]
def std_lib_dirs_and_libs(): def std_lib_dirs_and_libs():
...@@ -1929,7 +1930,7 @@ class GCC_compiler(Compiler): ...@@ -1929,7 +1930,7 @@ class GCC_compiler(Compiler):
@staticmethod @staticmethod
def compile_str(module_name, src_code, location=None, def compile_str(module_name, src_code, location=None,
include_dirs=None, lib_dirs=None, libs=None, include_dirs=None, lib_dirs=None, libs=None,
preargs=None, py_module=True): preargs=None, py_module=True, hide_symbols=True):
""" """
:param module_name: string (this has been embedded in the src_code :param module_name: string (this has been embedded in the src_code
...@@ -1952,6 +1953,10 @@ class GCC_compiler(Compiler): ...@@ -1952,6 +1953,10 @@ class GCC_compiler(Compiler):
:param py_module: if False, compile to a shared library, but do not :param py_module: if False, compile to a shared library, but do not
import it as a Python module. import it as a Python module.
:param hide_symbols: if True (the default) all symbols will be
hidden from the library symbol table (which means that other
objects can't use them.
:returns: dynamically-imported python module of the compiled code. :returns: dynamically-imported python module of the compiled code.
(unless py_module is False, in that case returns None.) (unless py_module is False, in that case returns None.)
""" """
...@@ -2006,6 +2011,14 @@ class GCC_compiler(Compiler): ...@@ -2006,6 +2011,14 @@ class GCC_compiler(Compiler):
else: else:
cmd.extend(preargs) cmd.extend(preargs)
cmd.extend('-I%s' % idir for idir in include_dirs) cmd.extend('-I%s' % idir for idir in include_dirs)
if hide_symbols and sys.platform != 'win32':
# This has been available since gcc 4.0 so we suppose it
# is always available. We pass it here since it
# significantly reduces the size of the symbol table for
# the objects we want to share. This in turns leads to
# improved loading times on most platforms (win32 is
# different, as usual).
cmd.append('-fvisibility=hidden')
cmd.extend(['-o', lib_filename]) cmd.extend(['-o', lib_filename])
cmd.append(cppfilename) cmd.append(cppfilename)
cmd.extend(['-L%s' % ldir for ldir in lib_dirs]) cmd.extend(['-L%s' % ldir for ldir in lib_dirs])
......
...@@ -188,6 +188,7 @@ def compile_cutils(): ...@@ -188,6 +188,7 @@ def compile_cutils():
code = (""" code = ("""
#include <Python.h> #include <Python.h>
#include "numpy/arrayobject.h" #include "numpy/arrayobject.h"
#include "theano_mod_helper.h"
extern "C"{ extern "C"{
static PyObject * static PyObject *
...@@ -236,7 +237,7 @@ def compile_cutils(): ...@@ -236,7 +237,7 @@ def compile_cutils():
CutilsExtMethods, CutilsExtMethods,
}; };
PyMODINIT_FUNC THEANO_INIT_FUNC
PyInit_cutils_ext(void) { PyInit_cutils_ext(void) {
import_array(); import_array();
return PyModule_Create(&moduledef); return PyModule_Create(&moduledef);
...@@ -245,7 +246,7 @@ def compile_cutils(): ...@@ -245,7 +246,7 @@ def compile_cutils():
""" """
else: else:
code += """ code += """
PyMODINIT_FUNC THEANO_INIT_FUNC
initcutils_ext(void) initcutils_ext(void)
{ {
import_array(); import_array();
......
#include <Python.h> #include <Python.h>
#include "theano_mod_helper.h"
#include "structmember.h" #include "structmember.h"
#include <sys/time.h> #include <sys/time.h>
// Old Python compatibility from here:
// http://www.python.org/dev/peps/pep-0353/
#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
typedef int Py_ssize_t;
#define PY_SSIZE_T_MAX INT_MAX
#define PY_SSIZE_T_MIN INT_MIN
// This one was taken from:
// http://svn.python.org/projects/python/trunk/Modules/_ctypes/ctypes.h
#define PyNumber_AsSsize_t(ob, exc) PyInt_AsLong(ob)
#endif
#if PY_VERSION_HEX >= 0x03000000 #if PY_VERSION_HEX >= 0x03000000
#include "numpy/npy_3kcompat.h" #include "numpy/npy_3kcompat.h"
#define PyCObject_AsVoidPtr NpyCapsule_AsVoidPtr #define PyCObject_AsVoidPtr NpyCapsule_AsVoidPtr
...@@ -1033,10 +1023,6 @@ static PyMethodDef lazylinker_ext_methods[] = { ...@@ -1033,10 +1023,6 @@ static PyMethodDef lazylinker_ext_methods[] = {
{NULL, NULL, 0, NULL} /* Sentinel */ {NULL, NULL, 0, NULL} /* Sentinel */
}; };
#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
#define PyMODINIT_FUNC void
#endif
#if defined(NPY_PY3K) #if defined(NPY_PY3K)
static struct PyModuleDef moduledef = { static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT, PyModuleDef_HEAD_INIT,
...@@ -1052,11 +1038,11 @@ static struct PyModuleDef moduledef = { ...@@ -1052,11 +1038,11 @@ static struct PyModuleDef moduledef = {
#endif #endif
#if defined(NPY_PY3K) #if defined(NPY_PY3K)
#define RETVAL m #define RETVAL m
PyMODINIT_FUNC THEANO_INIT_FUNC
PyInit_lazylinker_ext(void) { PyInit_lazylinker_ext(void) {
#else #else
#define RETVAL #define RETVAL
PyMODINIT_FUNC THEANO_INIT_FUNC
initlazylinker_ext(void) initlazylinker_ext(void)
{ {
#endif #endif
...@@ -1076,4 +1062,3 @@ initlazylinker_ext(void) ...@@ -1076,4 +1062,3 @@ initlazylinker_ext(void)
return RETVAL; return RETVAL;
} }
#ifndef THEANO_MOD_HELPER
#define THEANO_MOD_HELPER
#ifndef _WIN32
#define MOD_PUBLIC __attribute__((visibility ("default")))
#else
#define MOD_PUBLIC
#endif
#define THEANO_INIT_FUNC MOD_PUBLIC PyMODINIT_FUNC
#endif
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
#include <Python.h> #include <Python.h>
#include <structmember.h> #include <structmember.h>
#include "theano_mod_helper.h"
#include <numpy/arrayobject.h> #include <numpy/arrayobject.h>
#include <iostream> #include <iostream>
...@@ -3475,10 +3476,6 @@ static PyMethodDef module_methods[] = { ...@@ -3475,10 +3476,6 @@ static PyMethodDef module_methods[] = {
{NULL, NULL, NULL, NULL} /* Sentinel */ {NULL, NULL, NULL, NULL} /* Sentinel */
}; };
#ifndef PyMODINIT_FUNC /* declarations for DLL import/export */
#define PyMODINIT_FUNC void
#endif
#define CNDA_MOD_NAME "cuda_ndarray" #define CNDA_MOD_NAME "cuda_ndarray"
#define CNDA_DOCSTRING "CUDA implementation of a numpy ndarray-like object." #define CNDA_DOCSTRING "CUDA implementation of a numpy ndarray-like object."
...@@ -3493,10 +3490,10 @@ static struct PyModuleDef cuda_ndarray_moduledef = ...@@ -3493,10 +3490,10 @@ static struct PyModuleDef cuda_ndarray_moduledef =
module_methods module_methods
}; };
PyMODINIT_FUNC THEANO_INIT_FUNC
PyInit_cuda_ndarray(void) PyInit_cuda_ndarray(void)
#else #else
PyMODINIT_FUNC THEANO_INIT_FUNC
initcuda_ndarray(void) initcuda_ndarray(void)
#endif #endif
{ {
......
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
#include "numpy/npy_3kcompat.h" #include "numpy/npy_3kcompat.h"
#include "theano_mod_helper.h"
// Py3k strings are unicode, these mimic old functionality. // Py3k strings are unicode, these mimic old functionality.
// //
// NOTE: npy_3kcompat.h replaces PyString_X with PyBytes_X, which breaks // NOTE: npy_3kcompat.h replaces PyString_X with PyBytes_X, which breaks
...@@ -53,7 +55,7 @@ ...@@ -53,7 +55,7 @@
#endif #endif
#define ALWAYS_INLINE #define ALWAYS_INLINE
#else //else _WIN32 #else //else _WIN32
#define DllExport #define DllExport MOD_PUBLIC
#define ALWAYS_INLINE __attribute__((always_inline)) #define ALWAYS_INLINE __attribute__((always_inline))
#endif #endif
......
...@@ -208,7 +208,7 @@ class NVCC_compiler(Compiler): ...@@ -208,7 +208,7 @@ class NVCC_compiler(Compiler):
def compile_str( def compile_str(
module_name, src_code, module_name, src_code,
location=None, include_dirs=[], lib_dirs=[], libs=[], preargs=[], location=None, include_dirs=[], lib_dirs=[], libs=[], preargs=[],
rpaths=rpath_defaults, py_module=True): rpaths=rpath_defaults, py_module=True, hide_symbols=True):
""":param module_name: string (this has been embedded in the src_code """:param module_name: string (this has been embedded in the src_code
:param src_code: a complete c or c++ source listing for the module :param src_code: a complete c or c++ source listing for the module
:param location: a pre-existing filesystem directory where the :param location: a pre-existing filesystem directory where the
...@@ -225,6 +225,9 @@ class NVCC_compiler(Compiler): ...@@ -225,6 +225,9 @@ class NVCC_compiler(Compiler):
:param py_module: if False, compile to a shared library, but :param py_module: if False, compile to a shared library, but
do not import as a Python module. do not import as a Python module.
:param hide_symbols: if True (the default), hide all symbols
from the library symbol table unless explicitely exported.
:returns: dynamically-imported python module of the compiled code. :returns: dynamically-imported python module of the compiled code.
(unless py_module is False, in that case returns None.) (unless py_module is False, in that case returns None.)
...@@ -320,6 +323,9 @@ class NVCC_compiler(Compiler): ...@@ -320,6 +323,9 @@ class NVCC_compiler(Compiler):
# in both math_functions.h and pymath.h, # in both math_functions.h and pymath.h,
# by not including the one in pymath.h # by not including the one in pymath.h
cmd.extend(['-D HAVE_ROUND']) cmd.extend(['-D HAVE_ROUND'])
else:
if hide_symbols:
preargs2.append('-fvisibility=hidden')
if local_bitwidth() == 64: if local_bitwidth() == 64:
cmd.append('-m64') cmd.append('-m64')
......
...@@ -109,7 +109,8 @@ except ImportError: ...@@ -109,7 +109,8 @@ except ImportError:
preargs.append("-D NPY_F_CONTIGUOUS=NPY_ARRAY_F_CONTIGUOUS") preargs.append("-D NPY_F_CONTIGUOUS=NPY_ARRAY_F_CONTIGUOUS")
cmodule.GCC_compiler.compile_str(dirname, code, location=loc, cmodule.GCC_compiler.compile_str(dirname, code, location=loc,
preargs=preargs) preargs=preargs,
hide_symbols=False)
# Save version into the __init__.py file. # Save version into the __init__.py file.
init_py = os.path.join(loc, '__init__.py') init_py = os.path.join(loc, '__init__.py')
open(init_py, 'w').write('_version = %s\n' % version) open(init_py, 'w').write('_version = %s\n' % version)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论