Skip to content

Commit

Permalink
Merge pull request #508 from madsbk/max_mm
Browse files Browse the repository at this point in the history
Max mm.
  • Loading branch information
madsbk authored Feb 21, 2018
2 parents edf88ab + 9c1cd39 commit 901986f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 23 deletions.
10 changes: 9 additions & 1 deletion bridge/npbackend/bohrium/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
5 changes: 5 additions & 0 deletions bridge/npbackend/bohrium/ufuncs.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
38 changes: 17 additions & 21 deletions core/bh_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,9 @@ If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdint.h>

#include <bh_type.hpp>
#include <climits>
#include <cfloat>
#include <cassert>
#include <sstream>
#include <limits>
#include <cassert>

int bh_type_size(bh_type type)
{
Expand Down Expand Up @@ -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<int8_t >::max();
case bh_type::INT16: return std::numeric_limits<int16_t >::max();
case bh_type::INT32: return std::numeric_limits<int32_t >::max();
case bh_type::INT64: return std::numeric_limits<int64_t >::max();
case bh_type::UINT8: return std::numeric_limits<uint8_t>::max();
case bh_type::UINT16: return std::numeric_limits<uint16_t>::max();
case bh_type::UINT32: return std::numeric_limits<uint32_t>::max();
case bh_type::UINT64: return std::numeric_limits<uint64_t>::max();
default:
assert(1 == 2);
return 0;
Expand All @@ -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<int8_t >::min();
case bh_type::INT16: return std::numeric_limits<int16_t >::min();
case bh_type::INT32: return std::numeric_limits<int32_t >::min();
case bh_type::INT64: return std::numeric_limits<int64_t >::min();
case bh_type::UINT8: return 0;
case bh_type::UINT16: return 0;
case bh_type::UINT32: return 0;
Expand All @@ -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<float>::max();
case bh_type::FLOAT64: return std::numeric_limits<double>::max();
default:
assert(1 == 2);
return 0;
Expand All @@ -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<float>::min();
case bh_type::FLOAT64: return std::numeric_limits<double>::min();
default:
assert(1 == 2);
return 0;
Expand Down
15 changes: 14 additions & 1 deletion test/python/tests/test_conversion.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import util
import sys


class test_conversion:
def init(self):
yield "a = M.arange(1);"
Expand All @@ -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
7 changes: 7 additions & 0 deletions test/python/tests/test_reduce.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 901986f

Please sign in to comment.