You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Potential optimization for functions with where argument #2461
Had discussion with @pierce314159 about our current implementation of where functions across several different implemented functions, like power, sqrt, etc.
We want to ensure that we are not wasting computational time while parsing through the where conditions.
Example:
If we are operating on a large array to apply an exponent, with a condition that only has a single value of true, every value in the array will be operated on.
where(condition, a**power, a)
This does the exponentiation for every single element in this large array, but we only take the a single exponentiated value from our calculations, wasting computation time.
There is also concern that we could be applying an illegal computation in places where we know the condition is False and do not want to apply it, like taking the square root of a negative number.
Possible new implementation would only apply the desired operation to the desired positions; we would be creating an additional copy of the array and several variables that may take up significant space.
Example code:
I'm def curious what others in @Bears-R-Us/arkouda-core-dev think about this new approach
Possible new implementation would only apply the desired operation to the desired positions; we would be creating an additional copy of the array and several variables that may take up significant space. Example code:
I want to weigh in a bit on the memory point I think in the worst case it might worse but I think it will be less in the best case.
Let's do some rough comparisons of the worst and best case memory usage and runtime complexity of the new and old approach. We're just gonna look at size of newly created arrays and number of elements touched, but in actuality more temporaries might be created on the chapel side and some operations might take longer than others.
It's worth noting that for all these funcs we have a default of True and we just do the operation without using where at all. So in the most common case none of this factors in-- we only have to consider the cases where the user has passed a boolean pdarray to determine which locations we do the operation
algorithm analysis stuff
For this we'll let n is the length of the arrays and m will be the number of Trues in condition. Note m <= n
The old approach:
where(condition, a**power, a)
Memory: O(2 * n)
This creates two new arrays (a**power and the result of the where operation) so we have 2 new arrays of length n
Runtime: O(2 * n) a**power performs the ** operation on each of n elements. Then we do the where operation on each of the n elements
These don't use m so the complexity of this approach is always the same regardless of how many elements in condition are True.
Memory: O(2*m + n)
We create new_a, new_power, which are both m long arrays. And return_a which is an n long array
Runtime: O(4*m + n)
We index a and power at condition which is m accesses and do new_a ** new_power which operates on m elements. Then a[:] does a deep copy of n elements. Then we copy m elements into return_a
Comparing the memory:
it's pretty clear in the worst case (condition is all Trues, so m = n) that the new approach is worse:
old is 2*n and new would be 3*n
They are equal when half the elements in condition are True (m = n/2), so both are 2*n. And when less than half the elements are True, the new way uses less memory
Comparing the runtime:
It's harder to compare the runtimes since I have to imagine doing the ** operation is more expensive than copying values. But the old way has n copies and n exponentiations and the new way has 3m + n copies and m exponentiations. I don't have a good grasp of what the tipping point to make the new approach faster is
So in summary they both have O(n) memory and runtime. When condition has way more Trues than Falses, i would expect the old way to do better and when vice versa the new way will win. I think we'd need to do some testing to know for sure
There is also concern that we could be applying an illegal computation in places where we know the condition is False and do not want to apply it, like taking the square root of a negative number.
I think this is a really good point and may be more important than the complexity. If we are telling the users that we only perform the operation at the places where condition is True and then we do it everywhere anyway and then filter down, it seems like that could cause some trouble. Like in this example
Example:
>>>numerator=ak.arange(10)
>>>numeratorarray([0123456789])
>>>denominator=ak.arange(10) %2>>>denominatorarray([0101010101])
# with the current approach, we get a divide my zero error even though my condition is denominator != 0>>>ak.divmod(numerator, denominator, where=denominator!=0)
ZeroDivisionError: Cannotdividebyzero
Though in this case it seems to be more an overzealous divide-by-zero check than anything
Activity
[-]Optimize the `where` function implementation[/-][+]Potential optimization for functions with `where` argument[/+]stress-tess commentedon May 25, 2023
I'm def curious what others in @Bears-R-Us/arkouda-core-dev think about this new approach
I want to weigh in a bit on the memory point I think in the worst case it might worse but I think it will be less in the best case.
Let's do some rough comparisons of the worst and best case memory usage and runtime complexity of the new and old approach. We're just gonna look at size of newly created arrays and number of elements touched, but in actuality more temporaries might be created on the chapel side and some operations might take longer than others.
It's worth noting that for all these funcs we have a default of
Trueand we just do the operation without usingwhereat all. So in the most common case none of this factors in-- we only have to consider the cases where the user has passed a boolean pdarray to determine which locations we do the operationalgorithm analysis stuff
For this we'll let
nis the length of the arrays andmwill be the number ofTrues incondition. Notem <= nThe old approach:
Memory:
O(2 * n)This creates two new arrays (
a**powerand the result of the where operation) so we have 2 new arrays of lengthnRuntime:
O(2 * n)a**powerperforms the**operation on each ofnelements. Then we do the where operation on each of thenelementsThese don't use
mso the complexity of this approach is always the same regardless of how many elements inconditionare True.The new approach:
Memory:
O(2*m + n)We create
new_a,new_power, which are bothmlong arrays. Andreturn_awhich is annlong arrayRuntime:
O(4*m + n)We index a and power at
conditionwhich ismaccesses and donew_a ** new_powerwhich operates onmelements. Thena[:]does a deep copy ofnelements. Then we copymelements intoreturn_aComparing the memory:
it's pretty clear in the worst case (
conditionis all Trues, som = n) that the new approach is worse:old is
2*nand new would be3*nThey are equal when half the elements in condition are True (
m = n/2), so both are2*n. And when less than half the elements are True, the new way uses less memoryComparing the runtime:
It's harder to compare the runtimes since I have to imagine doing the
**operation is more expensive than copying values. But the old way hasncopies andnexponentiations and the new way has3m + ncopies andmexponentiations. I don't have a good grasp of what the tipping point to make the new approach faster isSo in summary they both have
O(n)memory and runtime. Whenconditionhas way moreTrues thanFalses, i would expect the old way to do better and when vice versa the new way will win. I think we'd need to do some testing to know for sureI think this is a really good point and may be more important than the complexity. If we are telling the users that we only perform the operation at the places where
conditionisTrueand then we do it everywhere anyway and then filter down, it seems like that could cause some trouble. Like in this exampleExample:
Though in this case it seems to be more an overzealous divide-by-zero check than anything
Closes #2461: Implementation of `where` feature for trig functions (#…