Home > Net >  When running a loop using prange from numba to parallelize it, are elements appended in the same ord
When running a loop using prange from numba to parallelize it, are elements appended in the same ord

Time:01-05

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!

CodePudding user response:

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.

CodePudding user response:

You should always expect a random evaluation order when using prange. This can be easily checked:

@nb.njit(parallel=True)
def order():
    for value in nb.prange(5):
        print(value)

Also, if you are concerned with efficiency, avoid using a growing data structure when you know the final size in advance, so follow @Perfi's advice, pre-allocate the array and insert each loop result in its known place.

  •  Tags:  
  • Related