Skip to content
Advertisement

Find minimum difference between two vectors with numba

I’ve tried to optimize searching for minimum value between two numpy vectors with numba. There is speed up and result is correct until I use prange and parallel=True option. I understand that the issue is in sharing variables min_val, tmp, min_val_idx_a, min_val_idx_b during parallel execution (maybe with parallel threads). Is there way to overcome the problem and use numba in with parallel=True option ? (which makes my simple code 300x faster)

JavaScript

output which is incorrect (it should be 0.0 9999 9999):

JavaScript

but for compilation with parallel=False output is correct (last values are most close to each other):

JavaScript

Advertisement

Answer

You can avoid the issues commented about cross iteration dependencies if you parallelize by rows. For example:

JavaScript

Results are consistent with your implementation (tested with parallel=False and for j in prange(n)) and with the brute force Numpy approach:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement