IntelLabs / numba

NumPy aware dynamic Python compiler using LLVM
http://numba.pydata.org/
BSD 2-Clause "Simplified" License
12 stars 2 forks source link

Support Reduction of Arrays #47

Open ehsantn opened 6 years ago

ehsantn commented 6 years ago

We need to support reduction of arrays in the gufunc backend. Example below.

import numpy as np
import numba

@numba.njit(parallel=True)
def f(n):
    A = np.ones(3)
    B = np.ones(3)
    for i in numba.prange(n):
        A += B

    return A

print(f(10))
ninegua commented 6 years ago

Right now since we don't translate A += B to parfor, the above example should be relatively easier to handle.

But more generally, if it was written as A = A + B, then the reduction code becomes a parfor before lowering, and then at lowering stage, it would be rather difficult to extract the set of reduction instructions.

ninegua commented 6 years ago

Also, with prange it is possible to write:

 for i in numba.prange(n):
        A += B
        C[i] = A

This is a lot more difficult to handle correctly. In general, there are a lot of restrictions on what can be safely parallelized for an arbitrary loop. We are not doing a good job at checking them.

ehsantn commented 6 years ago

Yes, it can get very complicated. We should check as many conditions as possible. They should be documented for sure.