0
votes

I am trying to perform parallel computing using numba, however, I encountered a problem: using the following code, it is significantly slower if I switch parallel=True on compare to switching it off.

import numpy as np
from numba import njit, prange
from numba.typed import List
import time


n = 1000

@njit(parallel = True)
def lamb(now):
    timez = List()
    k1 = List()
    k = 0
    for i in range(n):
        timez.append(np.arange(i+1))
        k1.append( len(timez[i][timez[i]<=now]) )

    for i in prange(n):
       k += k1[i]

    return k


lamb(21)

start = time.time()
lamb(21)
end = time.time()
print("Elapsed (after compilation) = %s" % (end - start))

If parallel = True, the elapsed time is Elapsed (after compilation) = 0.012674093246459961. In comparasion, if parallel = False, the elapsed time is Elapsed (after compilation) = 0.007932901382446289.

Any idea why this is the case?

1
1) You are measuring compile time+runtime. parallel takes significantly more time to compile. You should be also aware of the fact that the global variable is a compile time constant. If you change it you have to manually recompile the function to see any effect. 2) At least with version 0.45.1 you would see that no parallelization is possible. If you measure only runtime you would see that the two versions perfom aprox. the same.max9111

1 Answers

0
votes

Coordination between different processes adds a cost to performance. That is the performance penalty you see, if the work was truly worth the effort to parallelize then you would reap net benefits assuming the work was large enough.