From 7ff8ed32c9884095263f367b46b0e341bf686aea Mon Sep 17 00:00:00 2001 From: "Mads R. B. Kristensen" Date: Tue, 20 Feb 2018 11:31:39 +0100 Subject: [PATCH 1/3] fixed bh_type_limit_max_float(), which returned 1024! --- core/bh_type.cpp | 38 ++++++++++++++------------------ test/python/tests/test_reduce.py | 7 ++++++ 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/core/bh_type.cpp b/core/bh_type.cpp index 35bb8bfcb..1a3bdb871 100644 --- a/core/bh_type.cpp +++ b/core/bh_type.cpp @@ -19,13 +19,9 @@ If not, see . */ #include - #include -#include -#include -#include -#include #include +#include int bh_type_size(bh_type type) { @@ -147,14 +143,14 @@ uint64_t bh_type_limit_max_integer(bh_type type) switch(type) { case bh_type::BOOL: return 1; - case bh_type::INT8: return INT8_MAX; - case bh_type::INT16: return INT16_MAX; - case bh_type::INT32: return INT32_MAX; - case bh_type::INT64: return INT64_MAX; - case bh_type::UINT8: return UINT8_MAX; - case bh_type::UINT16: return UINT16_MAX; - case bh_type::UINT32: return UINT32_MAX; - case bh_type::UINT64: return UINT64_MAX; + case bh_type::INT8: return std::numeric_limits::max(); + case bh_type::INT16: return std::numeric_limits::max(); + case bh_type::INT32: return std::numeric_limits::max(); + case bh_type::INT64: return std::numeric_limits::max(); + case bh_type::UINT8: return std::numeric_limits::max(); + case bh_type::UINT16: return std::numeric_limits::max(); + case bh_type::UINT32: return std::numeric_limits::max(); + case bh_type::UINT64: return std::numeric_limits::max(); default: assert(1 == 2); return 0; @@ -166,10 +162,10 @@ int64_t bh_type_limit_min_integer(bh_type type) switch(type) { case bh_type::BOOL: return 1; - case bh_type::INT8: return INT8_MIN; - case bh_type::INT16: return INT16_MIN; - case bh_type::INT32: return INT32_MIN; - case bh_type::INT64: return INT64_MIN; + case bh_type::INT8: return std::numeric_limits::min(); + case bh_type::INT16: return std::numeric_limits::min(); + case bh_type::INT32: return std::numeric_limits::min(); + case bh_type::INT64: return std::numeric_limits::min(); case bh_type::UINT8: return 0; case bh_type::UINT16: return 0; case bh_type::UINT32: return 0; @@ -184,8 +180,8 @@ double bh_type_limit_max_float(bh_type type) { switch(type) { - case bh_type::FLOAT32: return FLT_MAX_EXP; - case bh_type::FLOAT64: return DBL_MAX_EXP; + case bh_type::FLOAT32: return std::numeric_limits::max(); + case bh_type::FLOAT64: return std::numeric_limits::max(); default: assert(1 == 2); return 0; @@ -196,8 +192,8 @@ double bh_type_limit_min_float(bh_type type) { switch(type) { - case bh_type::FLOAT32: return FLT_MIN_EXP; - case bh_type::FLOAT64: return DBL_MIN_EXP; + case bh_type::FLOAT32: return std::numeric_limits::min(); + case bh_type::FLOAT64: return std::numeric_limits::min(); default: assert(1 == 2); return 0; diff --git a/test/python/tests/test_reduce.py b/test/python/tests/test_reduce.py index a8346f11c..d6e3a6074 100644 --- a/test/python/tests/test_reduce.py +++ b/test/python/tests/test_reduce.py @@ -37,3 +37,10 @@ def test_vector(self, arg): cmd = "R = bh.random.RandomState(42); a = R.random(10, dtype=%s, bohrium=BH); " % dtype cmd += "res = M.%s.reduce(a)" % op return cmd + + def test_vector_large(self, arg): + (op, dtype) = arg + mul_factor = "" if dtype == "np.bool" else "*10**6" # bool shouldn't have any multiplication factor + cmd = "R = bh.random.RandomState(42); a = R.random(10, dtype=%s, bohrium=BH)%s; " % (dtype, mul_factor) + cmd += "res = M.%s.reduce(a)" % op + return cmd From c065854e4892eca8903d0ba109635a6c64ad8146 Mon Sep 17 00:00:00 2001 From: "Mads R. B. Kristensen" Date: Wed, 21 Feb 2018 09:32:59 +0100 Subject: [PATCH 2/3] Fixed #36, we handle all of NumPy's max and min the same! Since reductions in OpenMP ignores NaN values we do the same always --- bridge/npbackend/bohrium/__init__.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/bridge/npbackend/bohrium/__init__.py b/bridge/npbackend/bohrium/__init__.py index ec42b6a3c..f426af78e 100644 --- a/bridge/npbackend/bohrium/__init__.py +++ b/bridge/npbackend/bohrium/__init__.py @@ -70,7 +70,15 @@ def wrapper(*args, **kwargs): _aliases = [ ('abs', 'absolute'), ('round', 'round_'), - ('conjugate', 'conj') + ('conjugate', 'conj'), + # We handle all of NumPy's max and min the same! + # Since reductions in OpenMP ignores NaN values we do the same always + ('fmin', 'minimum'), + ('fmax', 'maximum'), + ('nanmin', 'minimum.reduce'), + ('nanmax', 'maximum.reduce'), + ('amin', 'minimum.reduce'), + ('amax', 'maximum.reduce'), ] for _f, _t in _aliases: exec ("%s = %s" % (_f, _t)) From 9c1cd39caa094524c2429e57f737031951667a95 Mon Sep 17 00:00:00 2001 From: "Mads R. B. Kristensen" Date: Wed, 21 Feb 2018 09:51:59 +0100 Subject: [PATCH 3/3] Fixed #450, ufunc now converts python objects to bharrays --- bridge/npbackend/bohrium/ufuncs.pyx | 5 +++++ test/python/tests/test_conversion.py | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/bridge/npbackend/bohrium/ufuncs.pyx b/bridge/npbackend/bohrium/ufuncs.pyx index 3c7098d75..a0cdf4a0d 100644 --- a/bridge/npbackend/bohrium/ufuncs.pyx +++ b/bridge/npbackend/bohrium/ufuncs.pyx @@ -144,6 +144,11 @@ class Ufunc(object): if val is not None: raise ValueError("Bohrium ufuncs doesn't support the '%s' argument" % str(k)) + # Makes sure that `args` are either bohrium arrays or scalars + for i in range(len(args)): + if not np.isscalar(args[i]) and not bhary.check(args[i]): + args[i] = array_create.array(args[i]) + # Broadcast the args (bargs, out_shape) = broadcast_arrays(*args) diff --git a/test/python/tests/test_conversion.py b/test/python/tests/test_conversion.py index 96c045374..ecfb1de28 100644 --- a/test/python/tests/test_conversion.py +++ b/test/python/tests/test_conversion.py @@ -1,6 +1,6 @@ -import util import sys + class test_conversion: def init(self): yield "a = M.arange(1);" @@ -25,3 +25,16 @@ def test_hex(self, cmd): def test_long(self, cmd): cmd += "res = long(a);" return cmd + + +class test_python_lists: + def init(self): + yield "a = [1,2,3,4]; b = M.arange(4); " + + def test_add(self, cmd): + cmd += "res = a + b" + return cmd + + def test_sum(self, cmd): + cmd += "res = M.sum(a)" + return cmd \ No newline at end of file