Are variables defined inside prange privatized?

Is it the case that the variable axes_hits defined inside the prange will be independent for each loop even though they are running in parallel? I.e. is axes_hits branched into many independent variables such that changing axes_hits in a loop will not change its value in another loop that is running in parallel? I think I am not finding any clear reference to this in the docs.

I suppose this related to the “privatization” technique of shared-memory programming. (Thanks for the concept’s name, @gmarkall )

@njit(parallel = True)
def get_axis_filter(line_indexes, column_indexes):
    n_solutions = line_indexes.shape[0]
    filter_ = np.full(solutions.shape[0], True)
    
    for i in prange(line_indexes.shape[0]):
        axes_hits = [0]*10 #HARDCODED
        for j in range(line_indexes.shape[1]):
            l_i = line_indexes[i,j]
            c_i = line_indexes[i,j]
            if l_i==c_i:
                axes_hits[l_i] +=  1
            else:
                axes_hits[l_i] += 1
                axes_hits[c_i] += 1
        if max(axes_hits) > 5:
            filter_[i] = False
    return filter_

Thanks for the question / suggestion - linking here to the issue to track update of the documentation: Privatization of variables defined inside prange · Issue #8682 · numba/numba · GitHub

How would this code run if it were range instead of prange? Every time through the loop, you would be getting a different version of axes_hits. It is the same with prange, the loop iterations are logically independent and so axes_hits is different in each iteration of the loop. If you want to think of that as privatization then fine. Numba has no concurrency control so if there were shared variables and different iterations of the loop could be reading or writing to the same shared item then you’d have no way to control that access through concurrency control. If you have an array declared before the prange and use it (set or get) within the prange then the array does have this shared semantics but in this case Numba does index analysis to make sure that you are only accessing the array using an unmodified loop index and in this way the different iterations access distinct parts of the array and so its safe.

1 Like