-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
PERF: datetimelike addition #56373
PERF: datetimelike addition #56373
Conversation
jbrockmendel
commented
Dec 7, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting idea!
|
||
|
||
@cython.overflowcheck(True) | ||
cpdef cnp.ndarray add_overflowsafe(cnp.ndarray left, cnp.ndarray right): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these possible to specialize to int64?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not without specifying an ndim.
ndarray iresult = cnp.PyArray_EMPTY( | ||
left.ndim, left.shape, cnp.NPY_INT64, 0 | ||
) | ||
cnp.broadcast mi = cnp.PyArray_MultiIterNew3(iresult, left, right) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pretty cool
@@ -413,17 +413,6 @@ def setup(self): | |||
def time_add_overflow_arr_rev(self): | |||
checked_add_with_arr(self.arr, self.arr_rev) | |||
|
|||
def time_add_overflow_arr_mask_nan(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these considered superfluous?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
they use a no-longer-used mask argument. actually these are all pretty superfluous. will update to remove
|
||
# Note: doing this try/except outside the loop improves performance over | ||
# doing it inside the loop. | ||
try: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By the way I think there's another way of doing this without a loop that could also be more performant. The inspiration from this comes from a book called Hacker's Delight by Henry S Warren in section 4-2
The code would look something like:
uleft = left.view("uint64")
uright = right.view("uint64")
summed = uleft + uright
neg_overflow = uleft & uright & ~summed
pos_overflow = ~uleft & ~uright & summed
overflows = (neg_overflow | pos_overflow) >= 0x8000000000000000
The essence of the algorithm is that you use unsigned arithmetic (which has defined behavior) to do the summation, and then inspect the sign bit to see if "overflow" would have happened
With this would need neither the cython.overflow check nor the try...except block
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's a quick demo
In [42]: left = np.array([2 ** 63 - 1, 2 ** 63 - 1, 2 ** 63 - 1, -(2 ** 63), -(2 ** 63), -(2 ** 63)])
...: right = np.array([42, 1, 0, -42, -1, 0])
...: uleft = left.view("uint64")
...: uright = right.view("uint64")
...: summed = uleft + uright
...: neg_overflow = uleft & uright & ~summed
...: pos_overflow = ~uleft & ~uright & summed
...: overflows = (neg_overflow | pos_overflow) >= 0x8000000000000000
...: overflows
Out[42]: array([ True, True, False, True, True, False])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat. Does this play nicely with the NPY_NAT check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good callout. I suppose you would just mask that as well. So something like:
In [5]: left = np.array([2 ** 63 - 1, 2 ** 63 - 1, 2 ** 63 - 1, -(2 ** 63), -(2 ** 63), -(2 ** 63)])
...: right = np.array([42, 1, 0, -42, -1, 0])
...: is_nat = (left == 2 ** 63 - 1) | (right == 2 ** 63 - 1)
...: uleft = left.view("uint64")
...: uright = right.view("uint64")
...: summed = uleft + uright
...: neg_overflow = uleft & uright & ~summed
...: pos_overflow = ~uleft & ~uright & summed
...: overflows = ((neg_overflow | pos_overflow) >= 0x8000000000000000) & ~is_nat
...: overflows
Out[5]: array([False, False, False, True, True, False])
I just hardcoded the above to 2 ** 63 - 1 but can replace with macro or python variable, whichever is available
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does that actually out-perform? (maybe SIMD or something?) seems like a lot of additional memory allocations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hard to say. In theory yes this would leverage SIMD better if numpy uses it under the hood (not well versed in if NumPy uses that or not). Also the overflow checks on gcc/clang are pretty fast because they use the compiler builtin, but I think MSVC falls back to a slow implementation (there is a builtin for MSVC too but I don't recall Cython using it - would have to double check)
It requires more research so not something that needs to be done in this PR. Just food for future thought
Thanks @jbrockmendel |