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

Refactor OperatorMsg #3764

Closed
9 changes: 9 additions & 0 deletions .configs/registration-config-multi-dim.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
"bool",
"bigint"
]
},
"binop": {
"dtype": [
"int",
"uint",
"real",
"bool",
"bigint"
]
}
}
}
9 changes: 9 additions & 0 deletions .configs/registration-config-single-dim.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
"bool",
"bigint"
]
},
"binop": {
"dtype": [
"int",
"uint",
"real",
"bool",
"bigint"
]
}
}
}
44 changes: 36 additions & 8 deletions arkouda/pdarrayclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,22 @@ def format_other(self, other) -> str:
fmt = NUMBER_FORMAT_STRINGS[self.dtype.name]
return fmt.format(other)

def _binop_cmd_group(self, op: str) -> str:
"""
get the name of the command that implements an operator
"""
if op in ["+", "-", "*", "%", "**", "//"]:
op_cmd = "arithmeticOp"
elif op in ["==", "!=", "<", ">", "<=", ">="]:
op_cmd = "comparisonOp"
elif op in ["&", "|", "^", "<<", ">>", ">>>", "<<<"]:
op_cmd = "bitwiseOp"
elif op == "/":
op_cmd = "divOp"
else:
raise ValueError(f"unrecognized operator {op}")
return op_cmd

# binary operators
def _binop(self, other: pdarray, op: str) -> pdarray:
"""
Expand Down Expand Up @@ -514,13 +530,19 @@ def _binop(self, other: pdarray, op: str) -> pdarray:
return NotImplemented
if op not in self.BinOps:
raise ValueError(f"bad operator {op}")
op_cmd = self._binop_cmd_group(op)

# pdarray binop pdarray
if isinstance(other, pdarray):
try:
x1, x2, tmp_x1, tmp_x2 = broadcast_if_needed(self, other)
except ValueError:
raise ValueError(f"shape mismatch {self.shape} {other.shape}")
repMsg = generic_msg(cmd=f"binopvv{x1.ndim}D", args={"op": op, "a": x1, "b": x2})

repMsg = generic_msg(
cmd=f"{op_cmd}VV<{x1.dtype},{x2.dtype},{x1.ndim}>",
args={"op": op, "a": x1, "b": x2}
)
if tmp_x1:
del x1
if tmp_x2:
Expand All @@ -537,8 +559,8 @@ def _binop(self, other: pdarray, op: str) -> pdarray:
if dt not in DTypes:
raise TypeError(f"Unhandled scalar type: {other} ({type(other)})")
repMsg = generic_msg(
cmd=f"binopvs{self.ndim}D",
args={"op": op, "a": self, "dtype": dt, "value": other},
cmd=f"{op_cmd}VS<{self.dtype},{dt},{self.ndim}>",
args={"op": op, "a": self, "value": other},
)
return create_pdarray(repMsg)

Expand Down Expand Up @@ -581,9 +603,10 @@ def _r_binop(self, other: pdarray, op: str) -> pdarray:
other = self.dtype.type(other)
if dt not in DTypes:
raise TypeError(f"Unhandled scalar type: {other} ({type(other)})")
op_cmd = self._binop_cmd_group(op)
repMsg = generic_msg(
cmd=f"binopsv{self.ndim}D",
args={"op": op, "dtype": dt, "value": other, "a": self},
cmd=f"{op_cmd}SV<{dt},{self.dtype},{self.ndim}>",
args={"op": op, "value": other, "b": self},
)
return create_pdarray(repMsg)

Expand Down Expand Up @@ -772,9 +795,13 @@ def opeq(self, other, op):
raise ValueError(f"bad operator {op}")
# pdarray op= pdarray
if isinstance(other, pdarray):
# TODO: shape broadcasting here if needed
if self.shape != other.shape:
raise ValueError(f"shape mismatch {self.shape} {other.shape}")
generic_msg(cmd=f"opeqvv{self.ndim}D", args={"op": op, "a": self, "b": other})
generic_msg(
cmd=f"opeqVV<{self.dtype},{other.dtype},{self.ndim}>",
args={"op": op, "a": self, "b": other}
)
return self
# pdarray binop scalar
# opeq requires scalar to be cast as pdarray dtype
Expand All @@ -787,9 +814,10 @@ def opeq(self, other, op):
# Can't cast other as dtype of pdarray
raise TypeError(f"Unhandled scalar type: {other} ({type(other)})")

# TODO: shouldn't we use the dtype of 'other' here?
generic_msg(
cmd=f"opeqvs{self.ndim}D",
args={"op": op, "a": self, "dtype": self.dtype.name, "value": self.format_other(other)},
cmd=f"opeqVS<{self.dtype},{self.dtype},{self.ndim}>",
args={"op": op, "a": self, "value": self.format_other(other)},
)
return self

Expand Down
9 changes: 9 additions & 0 deletions registration-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
"bool",
"bigint"
]
},
"binop": {
"dtype": [
"int",
"uint",
"real",
"bool",
"bigint"
]
}
}
}
Loading
Loading