Is it possible to use numba in nopython parallel mode with numpy sum over slices of an array inside a for loop without creating a race condition?

Viewed 255

The initial conditions: The array data is a 1d numpy array of length 50 000 p is an integer that can vary between 10 and 300.

This results in an error:

from numba import jit, njit, prange
import numpy as np

@njit(parallel=True)
def jitmovavg(data, p):
    arr_size=len(data)
    mean_array=np.zeros(arr_size-p+1)
    for i in prange(len(mean_array)):
        mean_array[i]=np.sum(data[i:i+p])/p
    return mean_array

This does not result in an error, but is slow for large numbers of p:

@njit
def jitmovavg(data, p):
    arr_size=len(data)
    mean_array=np.zeros(arr_size-p+1)
    for i in range(len(mean_array)):
        mean_array[i]=np.sum(data[i:i+p])/p
    return mean_array

I am guessing that this has something to do with a race condition in the parallel loop. Is there a way to work around this problem?

0 Answers
Related