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

[lang] [refactor] deprecate @boardcast_if_scalar, all use @binary and @unary #943

Merged
merged 19 commits into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
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
56 changes: 56 additions & 0 deletions python/taichi/lang/common_ops.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class TaichiOperations:
def __neg__(self):
import taichi as ti
k-ye marked this conversation as resolved.
Show resolved Hide resolved
return ti.neg(self)

def __abs__(self):
import taichi as ti
return ti.abs(self)

def __add__(self, other):
import taichi as ti
return ti.add(self, other)

__radd__ = __add__
archibate marked this conversation as resolved.
Show resolved Hide resolved

def __sub__(self, other):
import taichi as ti
return ti.sub(self, other)

def __rsub__(self, other):
import taichi as ti
return ti.sub(other, self)

def __mul__(self, other):
import taichi as ti
return ti.mul(self, other)

__rmul__ = __mul__
archibate marked this conversation as resolved.
Show resolved Hide resolved

def __truediv__(self, other):
import taichi as ti
return ti.truediv(self, other)

def __rtruediv__(self, other):
import taichi as ti
return ti.truediv(other, self)

def __floordiv__(self, other):
import taichi as ti
return ti.floordiv(self, other)

def __rfloordiv__(self, other):
import taichi as ti
return ti.floordiv(other, self)

def __mod__(self, other):
import taichi as ti
return ti.mod(self, other)

def __pow__(self, other, modulo=None):
import taichi as ti
return ti.pow(self, other)

def __rpow__(self, other, modulo=None):
import taichi as ti
return ti.pow(other, self)
108 changes: 43 additions & 65 deletions python/taichi/lang/expr.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from .core import taichi_lang_core
from .util import *
from .common_ops import TaichiOperations
import traceback


# Scalar, basic data type
class Expr:
class Expr(TaichiOperations):
materialize_layout_callback = None
layout_materialized = False

Expand Down Expand Up @@ -46,51 +47,61 @@ def stack_info():
# remove the confusing last line
return '\n'.join(raw.split('\n')[:-3]) + '\n'

def __neg__(self):
archibate marked this conversation as resolved.
Show resolved Hide resolved
import taichi as ti
return ti.neg(self)

def __abs__(self):
import taichi as ti
return ti.abs(self)

def __add__(self, other):
other = Expr(other)
return Expr(taichi_lang_core.expr_add(self.ptr, other.ptr),
tb=self.stack_info())
import taichi as ti
return ti.add(self, other)

__radd__ = __add__

def __neg__(self):
return Expr(taichi_lang_core.expr_neg(self.ptr), tb=self.stack_info())

def __sub__(self, other):
other = Expr(other)
return Expr(taichi_lang_core.expr_sub(self.ptr, other.ptr),
tb=self.stack_info())
import taichi as ti
return ti.sub(self, other)

def __rsub__(self, other):
other = Expr(other)
return Expr(taichi_lang_core.expr_sub(other.ptr, self.ptr))
import taichi as ti
return ti.sub(other, self)

def __mul__(self, other):
if is_taichi_class(other) and hasattr(other, '__rmul__'):
return other.__rmul__(self)
else:
other = Expr(other)
return Expr(taichi_lang_core.expr_mul(self.ptr, other.ptr))
import taichi as ti
return ti.mul(self, other)

__rmul__ = __mul__

def __truediv__(self, other):
return Expr(taichi_lang_core.expr_truediv(self.ptr, Expr(other).ptr))
import taichi as ti
return ti.truediv(self, other)

def __rtruediv__(self, other):
return Expr(taichi_lang_core.expr_truediv(Expr(other).ptr, self.ptr))
import taichi as ti
return ti.truediv(other, self)

def __floordiv__(self, other):
return Expr(taichi_lang_core.expr_floordiv(self.ptr, Expr(other).ptr))
import taichi as ti
return ti.floordiv(self, other)

def __rfloordiv__(self, other):
return Expr(taichi_lang_core.expr_floordiv(Expr(other).ptr, self.ptr))
import taichi as ti
return ti.floordiv(other, self)

def __mod__(self, other):
other = Expr(other)
quotient = Expr(taichi_lang_core.expr_floordiv(self.ptr, other.ptr))
multiply = Expr(taichi_lang_core.expr_mul(other.ptr, quotient.ptr))
return Expr(taichi_lang_core.expr_sub(self.ptr, multiply.ptr))
import taichi as ti
return ti.mod(self, other)

def __pow__(self, other, modulo=None):
import taichi as ti
return ti.pow(self, other)

def __rpow__(self, other, modulo=None):
import taichi as ti
return ti.pow(other, self)

def __iadd__(self, other):
self.atomic_add(other)
Expand All @@ -99,17 +110,16 @@ def __isub__(self, other):
self.atomic_sub(other)

def __imul__(self, other):
self.assign(Expr(taichi_lang_core.expr_mul(self.ptr, other.ptr)))
import taichi as ti
self.assign(ti.mul(self, other))

def __itruediv__(self, other):
self.assign(
Expr(taichi_lang_core.expr_truediv(self.ptr,
Expr(other).ptr)))
import taichi as ti
self.assign(ti.truediv(self, other))

def __ifloordiv__(self, other):
self.assign(
Expr(taichi_lang_core.expr_floordiv(self.ptr,
Expr(other).ptr)))
import taichi as ti
self.assign(ti.floordiv(self, other))

def __iand__(self, other):
self.atomic_and(other)
Expand All @@ -120,6 +130,7 @@ def __ior__(self, other):
def __ixor__(self, other):
self.atomic_xor(other)

# TODO: ti.cmp_le
def __le__(self, other):
other = Expr(other)
return Expr(taichi_lang_core.expr_cmp_le(self.ptr, other.ptr))
Expand Down Expand Up @@ -314,39 +325,6 @@ def fill(self, val):
from .meta import fill_tensor
fill_tensor(self, val)

def __rpow__(self, power, modulo=None):
# Python will try Matrix.__pow__ first so we don't have to worry whether `power` is `Matrix`
return Expr(power).__pow__(self, modulo)

def __pow__(self, power, modulo=None):
import taichi as ti
if ti.is_taichi_class(power):
return power.element_wise_binary(lambda x, y: pow(y, x), self)
if not isinstance(power, int) or abs(power) > 100:
return Expr(taichi_lang_core.expr_pow(self.ptr, Expr(power).ptr))
if power == 0:
return Expr(1)
negative = power < 0
power = abs(power)
tmp = self
ret = None
while power:
if power & 1:
if ret is None:
ret = tmp
else:
ret = ti.expr_init(ret * tmp)
tmp = ti.expr_init(tmp * tmp)
power >>= 1
if negative:
return 1 / ret
else:
return ret

def __abs__(self):
import taichi as ti
return ti.abs(self)

def __ti_int__(self):
import taichi as ti
return ti.cast(self, ti.get_runtime().default_ip)
Expand Down
Loading