Skip to content

Potential optimization for functions with where argument #2461

Closed

Description

@jaketrookman

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:

new_a = a[condition]
new_power = power[condition]
new_a = new_a**new_power
return_a = a[:]
return_a[condition] = new_a

Activity

  1. added
    enhancement
    performance
    on May 25, 2023
  2. self-assigned this
    on May 25, 2023
  3. added
    question
    on May 25, 2023
  4. changed the title [-]Optimize the `where` function implementation[/-] [+]Potential optimization for functions with `where` argument[/+] on May 25, 2023
  5. stress-tess commented on May 25, 2023

    @stress-tess
    Member

    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.

    The new approach:

    new_a = a[condition]
    new_power = power[condition]
    new_a **= new_power
    return_a = a[:]
    return_a[condition] = new_a

    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)
    >>> numerator
    array([0 1 2 3 4 5 6 7 8 9])
    
    >>> denominator = ak.arange(10) % 2
    >>> denominator
    array([0 1 0 1 0 1 0 1 0 1])
    
    # 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: Can not divide by zero

    Though in this case it seems to be more an overzealous divide-by-zero check than anything

  6. added a commit that references this issue on Dec 29, 2023
    6632605
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementperformancequestion

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions