Parallel Vectorized Numbas Functions

I'm currently experimenting with numba functions and especially vectorized , so I created a vectorized sum function (because it is easy to compare with np.sum .

 import numpy as np import numba as nb @nb.vectorize([nb.float64(nb.float64, nb.float64)]) def numba_sum(element1, element2): return element1 + element2 @nb.vectorize([nb.float64(nb.float64, nb.float64)], target='parallel') def numba_sum_parallel(element1, element2): return element1 + element2 array = np.ones(elements) np.testing.assert_almost_equal(numba_sum.reduce(array), np.sum(array)) np.testing.assert_almost_equal(numba_sum_parallel.reduce(array), np.sum(array)) 

Depending on the number of elements parallel code does not return the same number as the target cpu code. I think it is due to something related to the usual problems with threads (but why? Is it a bug in Numba or something that just happens when using parallel execution?). It’s funny that sometimes it works, sometimes it’s not. Sometimes it fails with elements=1000 , sometimes it starts crashing with elements=100000 .

For example:

 AssertionError: Arrays are not almost equal to 7 decimals ACTUAL: 93238.0 DESIRED: 100000.0 

and if I run it again

 AssertionError: Arrays are not almost equal to 7 decimals ACTUAL: 83883.0 DESIRED: 100000.0 

Now my question is: why do I need a parallel vector function? I understand that the purpose of the vectorized function is to provide numpy-ufunc features , but I tested reduce and accumulate and they stop working on some (variable) number of elements and want an unreliable function?

I am using numba 0.23.1 , numpy 1.10.1 with python 3.5.1 .

+3
python numpy parallel-processing numba
source share
1 answer

You are asking:

wherever "parallel" vectorized functions make sense, given that this can lead to such problems

Given that the ufuncs created by numba.vectorize(target='parallel') have defective reduce() methods, the question is what can we do with them, which is useful?

In your case, ufunc makes an addition. A useful application of this with target='parallel' is to incrementally add two arrays:

 numba_sum(array, array) 

This is really faster than a single-core solution, and it does not seem to be affected by errors that cripple reduce() and friends.

0
source share

All Articles