Skip to content

Commit

Permalink
Merge pull request #566 from madsbk/version
Browse files Browse the repository at this point in the history
Separating Bohrium and Bohrium API versions
  • Loading branch information
madsbk authored Dec 10, 2018
2 parents 805ca78 + f9da4d0 commit 2d850ac
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 61 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.10.0
0.10.1
14 changes: 11 additions & 3 deletions bridge/npbackend/bohrium/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@

if 'numpy_force' not in sys.modules:
import numpy

sys.modules['numpy_force'] = numpy
del numpy

# We import all of NumPy and overwrite with the objects we implement our self
from numpy_force import *
from numpy_force import testing

import warnings
import bohrium_api
from .version import __version__, __version_info__
from .array_create import *
from .array_manipulation import *
from .reorganization import *
Expand All @@ -29,7 +32,6 @@
from .concatenate import *
from .ufuncs import _handle__array_ufunc__
from . import contexts
from bohrium_api import stack_info as bh_info
from . import interop_pyopencl
from . import interop_pycuda
from . import interop_numpy
Expand All @@ -44,18 +46,23 @@
# However, NumPy and SciPy's functionality differ! Thus, the ND version cannot replace NumPy's 1D version.
from .signal import correlate1d as correlate, convolve1d as convolve
from .signal import correlate as correlate_scipy, convolve as convolve_scipy

from numpy_force import dtype

asarray = array
asanyarray = array

if __version_info__ > bohrium_api.__version_info__:
warnings.warn("The version of Bohrium API (%s) is newer than Bohrium (%s). Please upgrade "
"Bohrium (e.g. `pip install bohrium --upgrade`)" % (__version__, bohrium_api.__version__))


def replace_numpy(function):
def wrapper(*args, **kwargs):
with contexts.EnableBohrium():
# Run your function/program
result = function(*args, **kwargs)
return result

return wrapper


Expand Down Expand Up @@ -112,6 +119,7 @@ def wrapper(*args, **kwargs):
def set_printoptions(*args, **kwargs):
numpy.core.arrayprint.set_printoptions(*args, **kwargs)


def get_printoptions(*args, **kwargs):
return numpy.core.arrayprint.get_printoptions()

Expand Down
6 changes: 4 additions & 2 deletions bridge/npbackend/bohrium/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import os
import argparse
import bohrium
from . import bh_info
import bohrium_api
from . import version


@bohrium.replace_numpy
Expand Down Expand Up @@ -49,7 +50,8 @@ def execfile_wrapper(path):
execfile_wrapper(sys.argv[0])
else:
if args.info:
print(bh_info.pprint())
print("----\nBohrium version: %s" % version.__version__)
print(bohrium_api.stack_info.pprint())

cmd = "import bohrium as bh; import numpy as np; assert((bh.arange(10) == np.arange(10)).all())"
sys.stdout.write('Sanity Check: "%s"' % cmd)
Expand Down
4 changes: 2 additions & 2 deletions bridge/npbackend/bohrium/interop_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
~~~~~~~~~~~~~
"""
from .bhary import get_base
from . import bh_info
from bohrium_api import stack_info


def get_array(bh_ary):
Expand All @@ -24,7 +24,7 @@ def get_array(bh_ary):
"""

if bh_info.is_proxy_in_stack():
if stack_info.is_proxy_in_stack():
raise RuntimeError("Cannot directly access array data through a proxy.")

if get_base(bh_ary) is not bh_ary:
Expand Down
6 changes: 3 additions & 3 deletions bridge/npbackend/bohrium/interop_pycuda.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
Interop PyCUDA
~~~~~~~~~~~~~~
"""
from bohrium_api import stack_info
from .bhary import get_base
from ._bh import get_data_pointer
from .backend_messaging import cuda_use_current_context
from . import bh_info
from . import contexts


Expand All @@ -19,11 +19,11 @@ def _import_pycuda_module():
except ImportError:
raise ImportError("Failed to import the `pycuda` module, please install PyCUDA")

if not bh_info.is_cuda_in_stack():
if not stack_info.is_cuda_in_stack():
raise RuntimeError("No CUDA device in the Bohrium stack! "
"Try defining the environment variable `BH_STACK=cuda`.")

if bh_info.is_proxy_in_stack():
if stack_info.is_proxy_in_stack():
raise RuntimeError("Cannot directly access the CUDA device through a proxy.")
return pycuda

Expand Down
6 changes: 3 additions & 3 deletions bridge/npbackend/bohrium/interop_pyopencl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
Interop PyOpenCL
~~~~~~~~~~~~~~~~
"""
from bohrium_api import stack_info
from .bhary import get_base
from ._bh import get_data_pointer, set_data_pointer, get_device_context
from . import bh_info


def _import_pyopencl_module():
Expand All @@ -14,11 +14,11 @@ def _import_pyopencl_module():
except ImportError:
raise ImportError("Failed to import the `pyopencl` module, please install PyOpenCL")

if not bh_info.is_opencl_in_stack():
if not stack_info.is_opencl_in_stack():
raise RuntimeError("No OpenCL device in the Bohrium stack! "
"Try defining the environment variable `BH_STACK=opencl`.")

if bh_info.is_proxy_in_stack():
if stack_info.is_proxy_in_stack():
raise RuntimeError("Cannot directly access the OpenCL device through a proxy.")
return pyopencl

Expand Down
5 changes: 2 additions & 3 deletions bridge/npbackend/bohrium/nobh/bincount.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import math
import numpy_force as np

from bohrium_api import stack_info
from .. import interop_pyopencl
from .. import interop_pycuda
from .. import array_create
from .. import bh_info
from .bincount_cython import bincount_cython


Expand Down Expand Up @@ -83,7 +82,7 @@ def bincount(x, weights=None, minlength=None):
assert(np.issubdtype(x.dtype.type, np.integer))
assert(np.issubdtype(x.dtype.type, np.integer))

if bh_info.is_proxy_in_stack(): # Cannot directly access array data through a proxy
if stack_info.is_proxy_in_stack(): # Cannot directly access array data through a proxy
return np.bincount(x.copy2numpy(), weights=weights, minlength=minlength)

try:
Expand Down
5 changes: 2 additions & 3 deletions bridge/npbackend/bohrium/ufuncs.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ from . import _util
from . import array_create
from . import loop
import numpy_force as np
from bohrium_api import _info
from bohrium_api import _info, stack_info
from .numpy_backport import as_strided
from ._util import dtype_equal
from .bhary import fix_biclass_wrapper
Expand Down Expand Up @@ -354,7 +354,6 @@ class Ufunc(object):
[ 9, 13]])
"""
from . import _bh
from . import bh_info

if out is not None:
if bhary.check(out):
Expand Down Expand Up @@ -427,7 +426,7 @@ class Ufunc(object):
else:
# Let's sort the axis indexes by their stride
# We use column major when a GPU is in the stack
column_major = bh_info.is_opencl_in_stack() or bh_info.is_cuda_in_stack()
column_major = stack_info.is_opencl_in_stack() or stack_info.is_cuda_in_stack()
strides = []
for i, s in enumerate(ary.strides):
if i in axis:
Expand Down
78 changes: 72 additions & 6 deletions bridge/npbackend/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,59 @@
from setuptools import setup, find_packages
from setuptools.extension import Extension
from setuptools.command.build_ext import build_ext as setup_build_ext
from setuptools.command.build_py import build_py as setup_build_py
from setuptools.command.sdist import sdist as setup_sdist
import numbers
import os
import glob

""" Beside the regular setup arguments, this script reads the follow environment variables:
* USE_CYTHON - if defined, this setup will cythonize all pyx files.
NB: when running the source distribution command, `setup.py sdist`, do it from the same directory as `setup.py`
"""


def script_path(*paths):
prefix = os.path.abspath(os.path.dirname(__file__))
assert len(prefix) > 0
return os.path.join(prefix, *paths)


def version_file_exist():
"""Return whether the version.py file exist or not"""
ver_path = script_path("bohrium", "version.py")
return os.path.exists(ver_path)


def get_version():
"""Returns the version and version_info.
If the version.py file doesn't exist, the version of Bohrium API is returned.
NB: If the version.py file doesn't exist, this function must be called after the call to `setup()`.
"""
ver_path = script_path("bohrium", "version.py")
if os.path.exists(ver_path):
print("Getting version from version.py")
# Loading `__version__` variable from the version file
with open(ver_path, "r") as f:
exec (f.read())
return (__version__, __version_info__)
else:
print("Getting version from bohrium_api")
import bohrium_api
return (bohrium_api.__version__, bohrium_api.__version_info__)


def get_bohrium_api_required_string():
"""Returns the install_requires/setup_requires string for `bohrium_api`"""
try:
ver_tuple = get_version()[1]
return "bohrium_api>=%d.%d.%d" % (ver_tuple[0], ver_tuple[1], ver_tuple[2])
except ImportError:
return "bohrium_api" # If `bohrium_api` is not available, we expect PIP to install the newest package


def get_pyx_extensions():
"""Find and compiles all cython extensions"""
include_dirs = []
Expand All @@ -60,9 +102,34 @@ def get_pyx_extensions():
return ret


def gen_version_file_in_cmd(self, target_dir):
"""We extend the setup commands to also generate the `version.py` file if it doesn't exist already"""
if not self.dry_run:
version, version_info = get_version()
if not version_file_exist():
self.mkpath(target_dir)
p = os.path.join(target_dir, 'version.py')
print("Generating '%s'" % p)
with open(p, 'w') as fobj:
fobj.write("__version__ = \"%s\"\n" % version)
fobj.write("__version_info__ = %s\n" % str(version_info))


class BuildPy(setup_build_py):
def run(self):
gen_version_file_in_cmd(self, os.path.join(self.build_lib, 'bohrium'))
setup_build_py.run(self)


class Sdist(setup_sdist):
def run(self):
gen_version_file_in_cmd(self, "bohrium")
setup_sdist.run(self)


class BuildExt(setup_build_ext):
"""We delay the numpy and bohrium dependency to the build command.
Hopefully, PIP has installed them at this point."""
Hopefully, PIP has installed them at this point."""

def run(self):
if not self.dry_run:
Expand All @@ -77,12 +144,11 @@ class DelayedVersion(numbers.Number):
"""In order to delay the version evaluation that depend on `bohrium_api`, we use this class"""

def __str__(self):
import bohrium_api
return bohrium_api.__version__
return get_version()[0]


setup(
cmdclass={'build_ext': BuildExt},
cmdclass={'build_ext': BuildExt, 'build_py': BuildPy, 'sdist': Sdist},
name='bohrium',
version=DelayedVersion(),
description='Bohrium Python/NumPy Backend',
Expand Down Expand Up @@ -126,8 +192,8 @@ def __str__(self):
keywords='Bohrium, bh107, Python, C, HPC, MPI, PGAS, CUDA, OpenCL, OpenMP',

# Dependencies
install_requires=['numpy>=1.7', 'bohrium_api'],
setup_requires=['numpy>=1.7', 'bohrium_api'],
install_requires=['numpy>=1.7', get_bohrium_api_required_string()],
setup_requires=['numpy>=1.7', get_bohrium_api_required_string()],

# You can just specify the packages manually here if your project is
# simple. Or you can use find_packages().
Expand Down
2 changes: 1 addition & 1 deletion bridge/py_api/bohrium_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""
import os
from ._bh_api import _C_API # Exposing the C_API of `_bh_api.c`
from ._info import __version__
from .version import __version__, __version_info__
from . import stack_info


Expand Down
26 changes: 10 additions & 16 deletions bridge/py_api/bohrium_api/stack_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
from os.path import join
from . import messaging
from . import _info
from .version import __version__, __version_info__

# Some cached info
_opencl_is_in_stack = None
Expand Down Expand Up @@ -34,11 +34,6 @@ def installed_through_pypi():
return os.path.exists(os.path.join(os.path.dirname(os.path.realpath(__file__)), "config.ini"))


def version():
"""Return the version of Bohrium"""
return _info.__version__


def header_dir():
"""Return the path to the C header directory"""
return os.path.join(os.path.abspath(os.path.dirname(__file__)), "include")
Expand All @@ -47,8 +42,9 @@ def header_dir():
def info():
"""Return a dict with all info"""
return {
"config_path" : config_file_path(),
"version": version(),
"config_path": config_file_path(),
"version": __version__,
"version_info": __version_info__,
"runtime_info": messaging.runtime_info(),
"statistics": messaging.statistic(),
"header_dir": header_dir(),
Expand Down Expand Up @@ -82,17 +78,15 @@ def is_proxy_in_stack():
def pprint():
"""Pretty print Bohrium info"""

ret = ""
if not (is_opencl_in_stack() or is_cuda_in_stack()):
ret += "Note: in order to activate and retrieve GPU info, set the `BH_STACK=opencl` " \
"or `BH_STACK=cuda` environment variable.\n"

ret += """----
Bohrium version: %s
ret = """----
Bohrium API version: %s
Installed through PyPI: %s
Config file: %s
Header dir: %s
Backend stack:
%s----
""" % (version(), installed_through_pypi(), config_file_path(), header_dir(), messaging.runtime_info())
""" % (__version__, installed_through_pypi(), config_file_path(), header_dir(), messaging.runtime_info())
if not (is_opencl_in_stack() or is_cuda_in_stack()):
ret += "Note: in order to activate and retrieve GPU info, set the `BH_STACK=opencl` " \
"or `BH_STACK=cuda` environment variable.\n"
return ret
2 changes: 1 addition & 1 deletion bridge/py_api/build.bash
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ if [[ -d "$VR_PATH" ]]; then
echo "Using virtualenv: $VR_PATH"
else
echo "Create virtualenv: $VR_PATH"
${PYTHON_EXECUTABLE} -m virtualenv -p ${PY_EXE} ${VR_PATH}
${PYTHON_EXECUTABLE} -m virtualenv -p ${PY_EXE} --system-site-packages ${VR_PATH}
fi

source ${VR_PATH}/bin/activate
Expand Down
Loading

0 comments on commit 2d850ac

Please sign in to comment.