Search code examples
pythonnumpyloopsparallel-processingnumba

When running a loop using prange from numba to parallelize it, are elements appended in the same order?


I am using numba prange to try to paralelize the following function:

@njit(parallel=True)
def contrast_16bit(video):

    n = video.shape[0]

    #New max value
    high = 65535
    video16 = []

    for i in prange(n):
        data = video[i,:,:]
        
        #Old max and min
        vmin = np.min(data)
        vmax = np.max(data)
        #Scale factor
        scale = high/(vmax-vmin)
        
        #Scale data
        data = data - vmin
        data = scale * data
        #Append data
        video16.append(data)

My question is, when using prange for the loop, when each data is appended to the video16 list, does it keep the same order it had in the original array?

So the first array from video is appended first, and so on.

Thanks!


Solution

  • As for the literal question you're asking - no idea, and it's honestly hard to know without digging in the implementation, so I would honestly be scared to depend on the ordering of this parallelized operation.

    However, you can sidestep the whole issue by allocating an array instead: video16 = np.empty(n, dtype=float), then setting video16[i] = data in the last step! And that's safer and more understandable.