0

I have two nested loops which I try to parallelize to save time however I do not know how I will be able to proceed.

here is my function:

def delta(A,B,U):
    for i in range(len(U)):
        x = 0
        for j in range(len(U)):
            x = x + (1-A[i][j])*V[j]
        U[i] = U[i] + (-x + B * h(i,A))

U is an array, A a matrix and B is an integer and h() returns 0 or 1.

Does somebody have an idea? I'll take it.

Thank you in advance for your comments ;).

  • If parallelism is not "MUST", you can convert it into vector operator to speed up the processing. For parallelism please [refer](https://stackoverflow.com/questions/9786102/how-do-i-parallelize-a-simple-python-loop) – Mohbat Tharani May 07 '21 at 04:19

1 Answers1

0

You can use Numba both to speed-up the computation and parallelize it. Note however that this is likely better to use Numpy vectorized calls for the reduction. Here is an example:

import numba as nb

# Note that you can precise the types of the vector to avoid the compilation time at runtime
@nb.njit(parallel=True)
def delta(A,B,U):
    for i in nb.prange(len(U)):
        x = 0
        for j in range(len(U)):
            x = x + (1-A[i][j])*V[j]
        U[i] = U[i] + (-x + B * h(i,A))
Jérôme Richard
  • 41,678
  • 6
  • 29
  • 59