Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ufunc refactoring + a bunch of missing ufuncs #223

Merged
merged 20 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cunumeric/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@
from cunumeric import linalg, random
from cunumeric.array import ndarray
from cunumeric.module import *
from cunumeric.ufunc import *
from cunumeric.ufunc.bit_twiddling import *
from cunumeric.ufunc.comparison import *
from cunumeric.ufunc.math import *
from cunumeric.ufunc.floating import *
from cunumeric.ufunc.trigonometric import *
from cunumeric.coverage import clone_module

clone_module(_np, globals())
Expand Down
28 changes: 2 additions & 26 deletions cunumeric/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -1204,12 +1204,6 @@ def __neg__(self):
Multiple GPUs, Multiple CPUs

"""
if (
self.dtype.type == np.uint16
or self.dtype.type == np.uint32
or self.dtype.type == np.uint64
):
raise TypeError("cannot negate unsigned type " + str(self.dtype))
return self._perform_unary_op(UnaryOpCode.NEGATIVE, self)

# __new__
Expand Down Expand Up @@ -1279,15 +1273,8 @@ def __pos__(self):
Multiple GPUs, Multiple CPUs

"""
# We know these types are already positive
if (
self.dtype.type == np.uint16
or self.dtype.type == np.uint32
or self.dtype.type == np.uint64
or self.dtype.type == np.bool_
):
return self
return self._perform_unary_op(UnaryOpCode.POSITIVE, self)
# the positive opeartor is equivalent to copy
return self._perform_unary_op(UnaryOpCode.COPY, self)

def __pow__(self, rhs):
"""a.__pow__(value, /)
Expand Down Expand Up @@ -3148,13 +3135,6 @@ def _perform_unary_op(
if where is False:
return dst

op_dtype = (
dst.dtype
if out_dtype is None
and not (op == UnaryOpCode.ABSOLUTE and src.dtype.kind == "c")
else src.dtype
)

if out_dtype is None:
if dst.dtype != src.dtype and not (
op == UnaryOpCode.ABSOLUTE and src.dtype.kind == "c"
Expand All @@ -3166,7 +3146,6 @@ def _perform_unary_op(
)
temp._thunk.unary_op(
op,
op_dtype,
src._thunk,
cls._get_where_thunk(where, dst.shape),
extra_args,
Expand All @@ -3175,7 +3154,6 @@ def _perform_unary_op(
else:
dst._thunk.unary_op(
op,
op_dtype,
src._thunk,
cls._get_where_thunk(where, dst.shape),
extra_args,
Expand All @@ -3189,7 +3167,6 @@ def _perform_unary_op(
)
temp._thunk.unary_op(
op,
op_dtype,
src._thunk,
cls._get_where_thunk(where, dst.shape),
extra_args,
Expand All @@ -3198,7 +3175,6 @@ def _perform_unary_op(
else:
dst._thunk.unary_op(
op,
op_dtype,
src._thunk,
cls._get_where_thunk(where, dst.shape),
extra_args,
Expand Down
164 changes: 98 additions & 66 deletions cunumeric/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,76 +116,108 @@ class CuNumericOpCode(IntEnum):
WRITE = _cunumeric.CUNUMERIC_WRITE


# Match these to BinaryOpCode in binary_op_util.h
@unique
class BinaryOpCode(IntEnum):
ADD = 1
DIVIDE = 2
EQUAL = 3
FLOOR_DIVIDE = 4
GREATER = 5
GREATER_EQUAL = 6
LESS = 7
LESS_EQUAL = 8
LOGICAL_AND = 9
LOGICAL_OR = 10
LOGICAL_XOR = 11
MAXIMUM = 12
MINIMUM = 13
MOD = 14
MULTIPLY = 15
NOT_EQUAL = 16
POWER = 17
SUBTRACT = 18
ALLCLOSE = 19


# Match these to UnaryOpCode in unary_op_util.h
# Match these to CuNumericUnaryOpCode in cunumeric_c.h
@unique
class UnaryOpCode(IntEnum):
ABSOLUTE = 1
ARCCOS = 2
ARCSIN = 3
ARCTAN = 4
CEIL = 5
CLIP = 6
COPY = 7
COS = 8
EXP = 9
EXP2 = 10
FLOOR = 11
INVERT = 12
ISINF = 13
ISNAN = 14
LOG = 15
LOG10 = 16
LOGICAL_NOT = 17
NEGATIVE = 18
RINT = 19
SIGN = 20
SIN = 21
SQRT = 22
TAN = 23
TANH = 24
CONJ = 25
REAL = 26
IMAG = 27
GETARG = 28


# Match these to UnaryRedCode in unary_red_util.h
ABSOLUTE = _cunumeric.CUNUMERIC_UOP_ABSOLUTE
ARCCOS = _cunumeric.CUNUMERIC_UOP_ARCCOS
ARCCOSH = _cunumeric.CUNUMERIC_UOP_ARCCOSH
ARCSIN = _cunumeric.CUNUMERIC_UOP_ARCSIN
ARCSINH = _cunumeric.CUNUMERIC_UOP_ARCSINH
ARCTAN = _cunumeric.CUNUMERIC_UOP_ARCTAN
ARCTANH = _cunumeric.CUNUMERIC_UOP_ARCTANH
CBRT = _cunumeric.CUNUMERIC_UOP_CBRT
CEIL = _cunumeric.CUNUMERIC_UOP_CEIL
CLIP = _cunumeric.CUNUMERIC_UOP_CLIP
CONJ = _cunumeric.CUNUMERIC_UOP_CONJ
COPY = _cunumeric.CUNUMERIC_UOP_COPY
COS = _cunumeric.CUNUMERIC_UOP_COS
COSH = _cunumeric.CUNUMERIC_UOP_COSH
DEG2RAD = _cunumeric.CUNUMERIC_UOP_DEG2RAD
EXP = _cunumeric.CUNUMERIC_UOP_EXP
EXP2 = _cunumeric.CUNUMERIC_UOP_EXP2
EXPM1 = _cunumeric.CUNUMERIC_UOP_EXPM1
FLOOR = _cunumeric.CUNUMERIC_UOP_FLOOR
GETARG = _cunumeric.CUNUMERIC_UOP_GETARG
IMAG = _cunumeric.CUNUMERIC_UOP_IMAG
INVERT = _cunumeric.CUNUMERIC_UOP_INVERT
ISFINITE = _cunumeric.CUNUMERIC_UOP_ISFINITE
ISINF = _cunumeric.CUNUMERIC_UOP_ISINF
ISNAN = _cunumeric.CUNUMERIC_UOP_ISNAN
LOG = _cunumeric.CUNUMERIC_UOP_LOG
LOG10 = _cunumeric.CUNUMERIC_UOP_LOG10
LOG1P = _cunumeric.CUNUMERIC_UOP_LOG1P
LOG2 = _cunumeric.CUNUMERIC_UOP_LOG2
LOGICAL_NOT = _cunumeric.CUNUMERIC_UOP_LOGICAL_NOT
NEGATIVE = _cunumeric.CUNUMERIC_UOP_NEGATIVE
POSITIVE = _cunumeric.CUNUMERIC_UOP_POSITIVE
RAD2DEG = _cunumeric.CUNUMERIC_UOP_RAD2DEG
REAL = _cunumeric.CUNUMERIC_UOP_REAL
RECIPROCAL = _cunumeric.CUNUMERIC_UOP_RECIPROCAL
RINT = _cunumeric.CUNUMERIC_UOP_RINT
SIGN = _cunumeric.CUNUMERIC_UOP_SIGN
SIGNBIT = _cunumeric.CUNUMERIC_UOP_SIGNBIT
SIN = _cunumeric.CUNUMERIC_UOP_SIN
SINH = _cunumeric.CUNUMERIC_UOP_SINH
SQUARE = _cunumeric.CUNUMERIC_UOP_SQUARE
SQRT = _cunumeric.CUNUMERIC_UOP_SQRT
TAN = _cunumeric.CUNUMERIC_UOP_TAN
TANH = _cunumeric.CUNUMERIC_UOP_TANH
TRUNC = _cunumeric.CUNUMERIC_UOP_TRUNC


# Match these to CuNumericRedopCode in cunumeric_c.h
@unique
class UnaryRedCode(IntEnum):
ALL = 1
ANY = 2
MAX = 3
MIN = 4
PROD = 5
SUM = 6
ARGMAX = 7
ARGMIN = 8
CONTAINS = 9
COUNT_NONZERO = 10
ALL = _cunumeric.CUNUMERIC_RED_ALL
ANY = _cunumeric.CUNUMERIC_RED_ANY
ARGMAX = _cunumeric.CUNUMERIC_RED_ARGMAX
ARGMIN = _cunumeric.CUNUMERIC_RED_ARGMIN
CONTAINS = _cunumeric.CUNUMERIC_RED_CONTAINS
COUNT_NONZERO = _cunumeric.CUNUMERIC_RED_COUNT_NONZERO
MAX = _cunumeric.CUNUMERIC_RED_MAX
MIN = _cunumeric.CUNUMERIC_RED_MIN
PROD = _cunumeric.CUNUMERIC_RED_PROD
SUM = _cunumeric.CUNUMERIC_RED_SUM


# Match these to CuNumericBinaryOpCode in cunumeric_c.h
@unique
class BinaryOpCode(IntEnum):
ADD = _cunumeric.CUNUMERIC_BINOP_ADD
ALLCLOSE = _cunumeric.CUNUMERIC_BINOP_ALLCLOSE
ARCTAN2 = _cunumeric.CUNUMERIC_BINOP_ARCTAN2
BITWISE_AND = _cunumeric.CUNUMERIC_BINOP_BITWISE_AND
BITWISE_OR = _cunumeric.CUNUMERIC_BINOP_BITWISE_OR
BITWISE_XOR = _cunumeric.CUNUMERIC_BINOP_BITWISE_XOR
COPYSIGN = _cunumeric.CUNUMERIC_BINOP_COPYSIGN
DIVIDE = _cunumeric.CUNUMERIC_BINOP_DIVIDE
EQUAL = _cunumeric.CUNUMERIC_BINOP_EQUAL
FLOAT_POWER = _cunumeric.CUNUMERIC_BINOP_FLOAT_POWER
FLOOR_DIVIDE = _cunumeric.CUNUMERIC_BINOP_FLOOR_DIVIDE
FMOD = _cunumeric.CUNUMERIC_BINOP_FMOD
GCD = _cunumeric.CUNUMERIC_BINOP_GCD
GREATER = _cunumeric.CUNUMERIC_BINOP_GREATER
GREATER_EQUAL = _cunumeric.CUNUMERIC_BINOP_GREATER_EQUAL
HYPOT = _cunumeric.CUNUMERIC_BINOP_HYPOT
LCM = _cunumeric.CUNUMERIC_BINOP_LCM
LEFT_SHIFT = _cunumeric.CUNUMERIC_BINOP_LEFT_SHIFT
LESS = _cunumeric.CUNUMERIC_BINOP_LESS
LESS_EQUAL = _cunumeric.CUNUMERIC_BINOP_LESS_EQUAL
LOGADDEXP = _cunumeric.CUNUMERIC_BINOP_LOGADDEXP
LOGADDEXP2 = _cunumeric.CUNUMERIC_BINOP_LOGADDEXP2
LOGICAL_AND = _cunumeric.CUNUMERIC_BINOP_LOGICAL_AND
LOGICAL_OR = _cunumeric.CUNUMERIC_BINOP_LOGICAL_OR
LOGICAL_XOR = _cunumeric.CUNUMERIC_BINOP_LOGICAL_XOR
MAXIMUM = _cunumeric.CUNUMERIC_BINOP_MAXIMUM
MINIMUM = _cunumeric.CUNUMERIC_BINOP_MINIMUM
MOD = _cunumeric.CUNUMERIC_BINOP_MOD
MULTIPLY = _cunumeric.CUNUMERIC_BINOP_MULTIPLY
NEXTAFTER = _cunumeric.CUNUMERIC_BINOP_NEXTAFTER
NOT_EQUAL = _cunumeric.CUNUMERIC_BINOP_NOT_EQUAL
POWER = _cunumeric.CUNUMERIC_BINOP_POWER
RIGHT_SHIFT = _cunumeric.CUNUMERIC_BINOP_RIGHT_SHIFT
SUBTRACT = _cunumeric.CUNUMERIC_BINOP_SUBTRACT


# Match these to RandGenCode in rand_util.h
Expand Down
9 changes: 2 additions & 7 deletions cunumeric/deferred.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ def imag(self):

result.unary_op(
UnaryOpCode.IMAG,
result.dtype,
self,
True,
[],
Expand All @@ -260,7 +259,6 @@ def real(self):

result.unary_op(
UnaryOpCode.REAL,
result.dtype,
self,
True,
[],
Expand All @@ -277,7 +275,6 @@ def conj(self):

result.unary_op(
UnaryOpCode.CONJ,
result.dtype,
self,
True,
[],
Expand All @@ -293,7 +290,6 @@ def copy(self, rhs, deep=False):
return
self.unary_op(
UnaryOpCode.COPY,
rhs.dtype,
rhs,
True,
[],
Expand Down Expand Up @@ -1360,8 +1356,8 @@ def random_integer(self, low, high):
self.random(RandGenCode.INTEGER, [low, high])

# Perform the unary operation and put the result in the array
@auto_convert([3])
def unary_op(self, op, op_dtype, src, where, args):
@auto_convert([2])
def unary_op(self, op, src, where, args):
lhs = self.base
rhs = src._broadcast(lhs.shape)

Expand Down Expand Up @@ -1469,7 +1465,6 @@ def unary_reduction(
if argred:
self.unary_op(
UnaryOpCode.GETARG,
self.dtype,
lhs_array,
True,
[],
Expand Down
Loading